Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
0c6965dd | 2 | * net/sched/act_gact.c Generic actions |
1da177e4 LT |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * copyright Jamal Hadi Salim (2002-4) | |
10 | * | |
11 | */ | |
12 | ||
1da177e4 LT |
13 | #include <linux/types.h> |
14 | #include <linux/kernel.h> | |
1da177e4 | 15 | #include <linux/string.h> |
1da177e4 | 16 | #include <linux/errno.h> |
1da177e4 LT |
17 | #include <linux/skbuff.h> |
18 | #include <linux/rtnetlink.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
dc5fc579 | 21 | #include <net/netlink.h> |
1da177e4 LT |
22 | #include <net/pkt_sched.h> |
23 | #include <linux/tc_act/tc_gact.h> | |
24 | #include <net/tc_act/tc_gact.h> | |
25 | ||
e9ce1cd3 | 26 | #define GACT_TAB_MASK 15 |
1da177e4 | 27 | |
ddf97ccd | 28 | static int gact_net_id; |
a85a970a | 29 | static struct tc_action_ops act_gact_ops; |
ddf97ccd | 30 | |
1da177e4 | 31 | #ifdef CONFIG_GACT_PROB |
e9ce1cd3 | 32 | static int gact_net_rand(struct tcf_gact *gact) |
1da177e4 | 33 | { |
cef5ecf9 ED |
34 | smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ |
35 | if (prandom_u32() % gact->tcfg_pval) | |
e9ce1cd3 DM |
36 | return gact->tcf_action; |
37 | return gact->tcfg_paction; | |
1da177e4 LT |
38 | } |
39 | ||
e9ce1cd3 | 40 | static int gact_determ(struct tcf_gact *gact) |
1da177e4 | 41 | { |
cc6510a9 ED |
42 | u32 pack = atomic_inc_return(&gact->packets); |
43 | ||
cef5ecf9 | 44 | smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ |
cc6510a9 | 45 | if (pack % gact->tcfg_pval) |
e9ce1cd3 DM |
46 | return gact->tcf_action; |
47 | return gact->tcfg_paction; | |
1da177e4 LT |
48 | } |
49 | ||
e9ce1cd3 | 50 | typedef int (*g_rand)(struct tcf_gact *gact); |
cc7ec456 | 51 | static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ }; |
e9ce1cd3 | 52 | #endif /* CONFIG_GACT_PROB */ |
1da177e4 | 53 | |
53b2bf3f PM |
54 | static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = { |
55 | [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) }, | |
56 | [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) }, | |
57 | }; | |
58 | ||
c1b52739 | 59 | static int tcf_gact_init(struct net *net, struct nlattr *nla, |
a85a970a | 60 | struct nlattr *est, struct tc_action **a, |
c1b52739 | 61 | int ovr, int bind) |
1da177e4 | 62 | { |
ddf97ccd | 63 | struct tc_action_net *tn = net_generic(net, gact_net_id); |
7ba699c6 | 64 | struct nlattr *tb[TCA_GACT_MAX + 1]; |
1da177e4 | 65 | struct tc_gact *parm; |
e9ce1cd3 | 66 | struct tcf_gact *gact; |
1da177e4 | 67 | int ret = 0; |
cee63723 | 68 | int err; |
696ecdc1 HS |
69 | #ifdef CONFIG_GACT_PROB |
70 | struct tc_gact_p *p_parm = NULL; | |
71 | #endif | |
1da177e4 | 72 | |
cee63723 | 73 | if (nla == NULL) |
1da177e4 LT |
74 | return -EINVAL; |
75 | ||
53b2bf3f | 76 | err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy); |
cee63723 PM |
77 | if (err < 0) |
78 | return err; | |
79 | ||
53b2bf3f | 80 | if (tb[TCA_GACT_PARMS] == NULL) |
1da177e4 | 81 | return -EINVAL; |
7ba699c6 | 82 | parm = nla_data(tb[TCA_GACT_PARMS]); |
1da177e4 | 83 | |
53b2bf3f | 84 | #ifndef CONFIG_GACT_PROB |
7ba699c6 | 85 | if (tb[TCA_GACT_PROB] != NULL) |
1da177e4 | 86 | return -EOPNOTSUPP; |
696ecdc1 HS |
87 | #else |
88 | if (tb[TCA_GACT_PROB]) { | |
89 | p_parm = nla_data(tb[TCA_GACT_PROB]); | |
90 | if (p_parm->ptype >= MAX_RAND) | |
91 | return -EINVAL; | |
92 | } | |
1da177e4 LT |
93 | #endif |
94 | ||
ddf97ccd WC |
95 | if (!tcf_hash_check(tn, parm->index, a, bind)) { |
96 | ret = tcf_hash_create(tn, parm->index, est, a, | |
a85a970a | 97 | &act_gact_ops, bind, true); |
86062033 WC |
98 | if (ret) |
99 | return ret; | |
1da177e4 LT |
100 | ret = ACT_P_CREATED; |
101 | } else { | |
1a29321e JHS |
102 | if (bind)/* dont override defaults */ |
103 | return 0; | |
a85a970a | 104 | tcf_hash_release(*a, bind); |
1a29321e | 105 | if (!ovr) |
1da177e4 | 106 | return -EEXIST; |
1da177e4 LT |
107 | } |
108 | ||
a85a970a | 109 | gact = to_gact(*a); |
e9ce1cd3 | 110 | |
56e5d1ca | 111 | ASSERT_RTNL(); |
e9ce1cd3 | 112 | gact->tcf_action = parm->action; |
1da177e4 | 113 | #ifdef CONFIG_GACT_PROB |
696ecdc1 | 114 | if (p_parm) { |
e9ce1cd3 | 115 | gact->tcfg_paction = p_parm->paction; |
cef5ecf9 ED |
116 | gact->tcfg_pval = max_t(u16, 1, p_parm->pval); |
117 | /* Make sure tcfg_pval is written before tcfg_ptype | |
118 | * coupled with smp_rmb() in gact_net_rand() & gact_determ() | |
119 | */ | |
120 | smp_wmb(); | |
e9ce1cd3 | 121 | gact->tcfg_ptype = p_parm->ptype; |
1da177e4 LT |
122 | } |
123 | #endif | |
1da177e4 | 124 | if (ret == ACT_P_CREATED) |
a85a970a | 125 | tcf_hash_insert(tn, *a); |
1da177e4 LT |
126 | return ret; |
127 | } | |
128 | ||
dc7f9f6e ED |
129 | static int tcf_gact(struct sk_buff *skb, const struct tc_action *a, |
130 | struct tcf_result *res) | |
1da177e4 | 131 | { |
a85a970a | 132 | struct tcf_gact *gact = to_gact(a); |
56e5d1ca | 133 | int action = READ_ONCE(gact->tcf_action); |
1da177e4 | 134 | |
1da177e4 | 135 | #ifdef CONFIG_GACT_PROB |
8f2ae965 ED |
136 | { |
137 | u32 ptype = READ_ONCE(gact->tcfg_ptype); | |
138 | ||
139 | if (ptype) | |
140 | action = gact_rand[ptype](gact); | |
141 | } | |
1da177e4 | 142 | #endif |
56e5d1ca | 143 | bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb); |
1da177e4 | 144 | if (action == TC_ACT_SHOT) |
56e5d1ca ED |
145 | qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats)); |
146 | ||
147 | tcf_lastuse_update(&gact->tcf_tm); | |
1da177e4 LT |
148 | |
149 | return action; | |
150 | } | |
151 | ||
9fea47d9 AV |
152 | static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets, |
153 | u64 lastuse) | |
154 | { | |
a85a970a | 155 | struct tcf_gact *gact = to_gact(a); |
9fea47d9 AV |
156 | int action = READ_ONCE(gact->tcf_action); |
157 | struct tcf_t *tm = &gact->tcf_tm; | |
158 | ||
159 | _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes, packets); | |
160 | if (action == TC_ACT_SHOT) | |
161 | this_cpu_ptr(gact->common.cpu_qstats)->drops += packets; | |
162 | ||
163 | tm->lastuse = lastuse; | |
164 | } | |
165 | ||
0b0f43fe JHS |
166 | static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, |
167 | int bind, int ref) | |
1da177e4 | 168 | { |
27a884dc | 169 | unsigned char *b = skb_tail_pointer(skb); |
a85a970a | 170 | struct tcf_gact *gact = to_gact(a); |
1c40be12 ED |
171 | struct tc_gact opt = { |
172 | .index = gact->tcf_index, | |
173 | .refcnt = gact->tcf_refcnt - ref, | |
174 | .bindcnt = gact->tcf_bindcnt - bind, | |
175 | .action = gact->tcf_action, | |
176 | }; | |
1da177e4 LT |
177 | struct tcf_t t; |
178 | ||
1b34ec43 DM |
179 | if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt)) |
180 | goto nla_put_failure; | |
1da177e4 | 181 | #ifdef CONFIG_GACT_PROB |
e9ce1cd3 | 182 | if (gact->tcfg_ptype) { |
1c40be12 ED |
183 | struct tc_gact_p p_opt = { |
184 | .paction = gact->tcfg_paction, | |
185 | .pval = gact->tcfg_pval, | |
186 | .ptype = gact->tcfg_ptype, | |
187 | }; | |
188 | ||
1b34ec43 DM |
189 | if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt)) |
190 | goto nla_put_failure; | |
1da177e4 LT |
191 | } |
192 | #endif | |
48d8ee16 | 193 | tcf_tm_dump(&t, &gact->tcf_tm); |
9854518e | 194 | if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD)) |
1b34ec43 | 195 | goto nla_put_failure; |
1da177e4 LT |
196 | return skb->len; |
197 | ||
7ba699c6 | 198 | nla_put_failure: |
dc5fc579 | 199 | nlmsg_trim(skb, b); |
1da177e4 LT |
200 | return -1; |
201 | } | |
202 | ||
ddf97ccd WC |
203 | static int tcf_gact_walker(struct net *net, struct sk_buff *skb, |
204 | struct netlink_callback *cb, int type, | |
a85a970a | 205 | const struct tc_action_ops *ops) |
ddf97ccd WC |
206 | { |
207 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
208 | ||
a85a970a | 209 | return tcf_generic_walker(tn, skb, cb, type, ops); |
ddf97ccd WC |
210 | } |
211 | ||
a85a970a | 212 | static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
213 | { |
214 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
215 | ||
216 | return tcf_hash_search(tn, a, index); | |
217 | } | |
218 | ||
1da177e4 LT |
219 | static struct tc_action_ops act_gact_ops = { |
220 | .kind = "gact", | |
221 | .type = TCA_ACT_GACT, | |
1da177e4 LT |
222 | .owner = THIS_MODULE, |
223 | .act = tcf_gact, | |
9fea47d9 | 224 | .stats_update = tcf_gact_stats_update, |
1da177e4 | 225 | .dump = tcf_gact_dump, |
1da177e4 | 226 | .init = tcf_gact_init, |
ddf97ccd WC |
227 | .walk = tcf_gact_walker, |
228 | .lookup = tcf_gact_search, | |
a85a970a | 229 | .size = sizeof(struct tcf_gact), |
ddf97ccd WC |
230 | }; |
231 | ||
232 | static __net_init int gact_init_net(struct net *net) | |
233 | { | |
234 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
235 | ||
236 | return tc_action_net_init(tn, &act_gact_ops, GACT_TAB_MASK); | |
237 | } | |
238 | ||
239 | static void __net_exit gact_exit_net(struct net *net) | |
240 | { | |
241 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
242 | ||
243 | tc_action_net_exit(tn); | |
244 | } | |
245 | ||
246 | static struct pernet_operations gact_net_ops = { | |
247 | .init = gact_init_net, | |
248 | .exit = gact_exit_net, | |
249 | .id = &gact_net_id, | |
250 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
251 | }; |
252 | ||
253 | MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); | |
254 | MODULE_DESCRIPTION("Generic Classifier actions"); | |
255 | MODULE_LICENSE("GPL"); | |
256 | ||
e9ce1cd3 | 257 | static int __init gact_init_module(void) |
1da177e4 LT |
258 | { |
259 | #ifdef CONFIG_GACT_PROB | |
cc7ec456 | 260 | pr_info("GACT probability on\n"); |
1da177e4 | 261 | #else |
cc7ec456 | 262 | pr_info("GACT probability NOT on\n"); |
1da177e4 | 263 | #endif |
ddf97ccd WC |
264 | |
265 | return tcf_register_action(&act_gact_ops, &gact_net_ops); | |
1da177e4 LT |
266 | } |
267 | ||
e9ce1cd3 | 268 | static void __exit gact_cleanup_module(void) |
1da177e4 | 269 | { |
ddf97ccd | 270 | tcf_unregister_action(&act_gact_ops, &gact_net_ops); |
1da177e4 LT |
271 | } |
272 | ||
273 | module_init(gact_init_module); | |
274 | module_exit(gact_cleanup_module); |