Blame view
net/sched/act_simple.c
4.53 KB
db7530797
|
1 2 3 4 5 6 7 8 |
/* * net/sched/simp.c Simple example of an action * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * |
fa1b1cff3
|
9 |
* Authors: Jamal Hadi Salim (2005-8) |
db7530797
|
10 11 |
* */ |
cbdbf00aa
|
12 |
#include <linux/module.h> |
5a0e3ad6a
|
13 |
#include <linux/slab.h> |
cbdbf00aa
|
14 |
#include <linux/init.h> |
db7530797
|
15 |
#include <linux/kernel.h> |
db7530797
|
16 17 |
#include <linux/skbuff.h> #include <linux/rtnetlink.h> |
dc5fc579b
|
18 |
#include <net/netlink.h> |
db7530797
|
19 20 21 |
#include <net/pkt_sched.h> #define TCA_ACT_SIMP 22 |
db7530797
|
22 23 |
#include <linux/tc_act/tc_defact.h> #include <net/tc_act/tc_defact.h> |
e9ce1cd3c
|
24 |
#define SIMP_TAB_MASK 7 |
db7530797
|
25 |
|
fa1b1cff3
|
26 |
#define SIMP_MAX_DATA 32 |
dc7f9f6e8
|
27 28 |
static int tcf_simp(struct sk_buff *skb, const struct tc_action *a, struct tcf_result *res) |
db7530797
|
29 |
{ |
e9ce1cd3c
|
30 |
struct tcf_defact *d = a->priv; |
db7530797
|
31 |
|
e9ce1cd3c
|
32 33 |
spin_lock(&d->tcf_lock); d->tcf_tm.lastuse = jiffies; |
bfe0d0298
|
34 |
bstats_update(&d->tcf_bstats, skb); |
db7530797
|
35 |
|
10297b993
|
36 37 38 |
/* print policy string followed by _ then packet count * Example if this was the 3rd packet and the string was "hello" * then it would look like "hello_3" (without quotes) |
cc7ec456f
|
39 |
*/ |
6ff9c3644
|
40 41 |
pr_info("simple: %s_%d ", |
e9ce1cd3c
|
42 43 44 45 |
(char *)d->tcfd_defdata, d->tcf_bstats.packets); spin_unlock(&d->tcf_lock); return d->tcf_action; } |
a5b5c958f
|
46 |
static void tcf_simp_release(struct tc_action *a, int bind) |
e9ce1cd3c
|
47 |
{ |
86062033f
|
48 |
struct tcf_defact *d = to_defact(a); |
a5b5c958f
|
49 |
kfree(d->tcfd_defdata); |
e9ce1cd3c
|
50 |
} |
fa1b1cff3
|
51 |
static int alloc_defdata(struct tcf_defact *d, char *defdata) |
e9ce1cd3c
|
52 |
{ |
0eff683f7
|
53 |
d->tcfd_defdata = kzalloc(SIMP_MAX_DATA, GFP_KERNEL); |
e9ce1cd3c
|
54 55 |
if (unlikely(!d->tcfd_defdata)) return -ENOMEM; |
0eff683f7
|
56 |
strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA); |
e9ce1cd3c
|
57 58 |
return 0; } |
9d1045ad6
|
59 60 |
static void reset_policy(struct tcf_defact *d, char *defdata, struct tc_defact *p) |
e9ce1cd3c
|
61 |
{ |
9d1045ad6
|
62 63 64 65 66 |
spin_lock_bh(&d->tcf_lock); d->tcf_action = p->action; memset(d->tcfd_defdata, 0, SIMP_MAX_DATA); strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA); spin_unlock_bh(&d->tcf_lock); |
e9ce1cd3c
|
67 |
} |
53b2bf3f8
|
68 69 |
static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = { [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) }, |
fa1b1cff3
|
70 |
[TCA_DEF_DATA] = { .type = NLA_STRING, .len = SIMP_MAX_DATA }, |
53b2bf3f8
|
71 |
}; |
c1b52739e
|
72 73 74 |
static int tcf_simp_init(struct net *net, struct nlattr *nla, struct nlattr *est, struct tc_action *a, int ovr, int bind) |
e9ce1cd3c
|
75 |
{ |
7ba699c60
|
76 |
struct nlattr *tb[TCA_DEF_MAX + 1]; |
e9ce1cd3c
|
77 78 |
struct tc_defact *parm; struct tcf_defact *d; |
fa1b1cff3
|
79 |
char *defdata; |
cee63723b
|
80 |
int ret = 0, err; |
e9ce1cd3c
|
81 |
|
cee63723b
|
82 |
if (nla == NULL) |
e9ce1cd3c
|
83 |
return -EINVAL; |
fa1b1cff3
|
84 |
err = nla_parse_nested(tb, TCA_DEF_MAX, nla, simple_policy); |
cee63723b
|
85 86 |
if (err < 0) return err; |
53b2bf3f8
|
87 |
if (tb[TCA_DEF_PARMS] == NULL) |
e9ce1cd3c
|
88 |
return -EINVAL; |
fa1b1cff3
|
89 |
if (tb[TCA_DEF_DATA] == NULL) |
e9ce1cd3c
|
90 |
return -EINVAL; |
fa1b1cff3
|
91 92 |
parm = nla_data(tb[TCA_DEF_PARMS]); defdata = nla_data(tb[TCA_DEF_DATA]); |
e9ce1cd3c
|
93 |
|
86062033f
|
94 95 96 97 |
if (!tcf_hash_check(parm->index, a, bind)) { ret = tcf_hash_create(parm->index, est, a, sizeof(*d), bind); if (ret) return ret; |
e9ce1cd3c
|
98 |
|
86062033f
|
99 |
d = to_defact(a); |
fa1b1cff3
|
100 |
ret = alloc_defdata(d, defdata); |
e9ce1cd3c
|
101 |
if (ret < 0) { |
86062033f
|
102 |
tcf_hash_cleanup(a, est); |
e9ce1cd3c
|
103 104 |
return ret; } |
9d1045ad6
|
105 |
d->tcf_action = parm->action; |
e9ce1cd3c
|
106 107 |
ret = ACT_P_CREATED; } else { |
86062033f
|
108 |
d = to_defact(a); |
1a29321ed
|
109 110 111 |
if (bind) return 0; |
a5b5c958f
|
112 |
tcf_hash_release(a, bind); |
1a29321ed
|
113 |
if (!ovr) |
e9ce1cd3c
|
114 |
return -EEXIST; |
1a29321ed
|
115 |
|
9d1045ad6
|
116 |
reset_policy(d, defdata, parm); |
e9ce1cd3c
|
117 |
} |
e9ce1cd3c
|
118 |
if (ret == ACT_P_CREATED) |
86062033f
|
119 |
tcf_hash_insert(a); |
e9ce1cd3c
|
120 121 |
return ret; } |
cc7ec456f
|
122 123 |
static int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
e9ce1cd3c
|
124 |
{ |
27a884dc3
|
125 |
unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3c
|
126 |
struct tcf_defact *d = a->priv; |
1c40be12f
|
127 128 129 130 131 132 |
struct tc_defact opt = { .index = d->tcf_index, .refcnt = d->tcf_refcnt - ref, .bindcnt = d->tcf_bindcnt - bind, .action = d->tcf_action, }; |
e9ce1cd3c
|
133 |
struct tcf_t t; |
1b34ec43c
|
134 135 136 |
if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) || nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata)) goto nla_put_failure; |
e9ce1cd3c
|
137 138 139 |
t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install); t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse); t.expires = jiffies_to_clock_t(d->tcf_tm.expires); |
1b34ec43c
|
140 141 |
if (nla_put(skb, TCA_DEF_TM, sizeof(t), &t)) goto nla_put_failure; |
e9ce1cd3c
|
142 |
return skb->len; |
7ba699c60
|
143 |
nla_put_failure: |
dc5fc579b
|
144 |
nlmsg_trim(skb, b); |
e9ce1cd3c
|
145 |
return -1; |
db7530797
|
146 147 148 |
} static struct tc_action_ops act_simp_ops = { |
e9ce1cd3c
|
149 |
.kind = "simple", |
e9ce1cd3c
|
150 |
.type = TCA_ACT_SIMP, |
e9ce1cd3c
|
151 152 153 |
.owner = THIS_MODULE, .act = tcf_simp, .dump = tcf_simp_dump, |
86062033f
|
154 |
.cleanup = tcf_simp_release, |
e9ce1cd3c
|
155 |
.init = tcf_simp_init, |
db7530797
|
156 157 158 159 160 161 162 163 |
}; MODULE_AUTHOR("Jamal Hadi Salim(2005)"); MODULE_DESCRIPTION("Simple example action"); MODULE_LICENSE("GPL"); static int __init simp_init_module(void) { |
4f1e9d894
|
164 165 |
int ret; ret = tcf_register_action(&act_simp_ops, SIMP_TAB_MASK); |
db7530797
|
166 |
if (!ret) |
6ff9c3644
|
167 168 |
pr_info("Simple TC action Loaded "); |
db7530797
|
169 170 171 172 173 174 175 176 177 178 |
return ret; } static void __exit simp_cleanup_module(void) { tcf_unregister_action(&act_simp_ops); } module_init(simp_init_module); module_exit(simp_cleanup_module); |