]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
0c6965dd | 2 | * net/sched/act_police.c Input police filter |
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 | * Authors: Alexey Kuznetsov, <[email protected]> | |
10 | * J Hadi Salim (action changes) | |
11 | */ | |
12 | ||
1da177e4 LT |
13 | #include <linux/module.h> |
14 | #include <linux/types.h> | |
15 | #include <linux/kernel.h> | |
1da177e4 | 16 | #include <linux/string.h> |
1da177e4 | 17 | #include <linux/errno.h> |
1da177e4 | 18 | #include <linux/skbuff.h> |
1da177e4 LT |
19 | #include <linux/rtnetlink.h> |
20 | #include <linux/init.h> | |
5a0e3ad6 | 21 | #include <linux/slab.h> |
1da177e4 | 22 | #include <net/act_api.h> |
dc5fc579 | 23 | #include <net/netlink.h> |
1da177e4 | 24 | |
0e243218 JP |
25 | struct tcf_police { |
26 | struct tcf_common common; | |
27 | int tcfp_result; | |
28 | u32 tcfp_ewma_rate; | |
c6d14ff1 | 29 | s64 tcfp_burst; |
0e243218 | 30 | u32 tcfp_mtu; |
c6d14ff1 JP |
31 | s64 tcfp_toks; |
32 | s64 tcfp_ptoks; | |
33 | s64 tcfp_mtu_ptoks; | |
34 | s64 tcfp_t_c; | |
35 | struct psched_ratecfg rate; | |
36 | bool rate_present; | |
37 | struct psched_ratecfg peak; | |
38 | bool peak_present; | |
0e243218 JP |
39 | }; |
40 | #define to_police(pc) \ | |
41 | container_of(pc, struct tcf_police, common) | |
42 | ||
e9ce1cd3 | 43 | #define POL_TAB_MASK 15 |
1da177e4 | 44 | |
1e9b3d53 | 45 | /* old policer structure from before tc actions */ |
cc7ec456 | 46 | struct tc_police_compat { |
1e9b3d53 PM |
47 | u32 index; |
48 | int action; | |
49 | u32 limit; | |
50 | u32 burst; | |
51 | u32 mtu; | |
52 | struct tc_ratespec rate; | |
53 | struct tc_ratespec peakrate; | |
54 | }; | |
55 | ||
e9ce1cd3 | 56 | /* Each policer is serialized by its individual spinlock */ |
1da177e4 | 57 | |
ddf97ccd WC |
58 | static int police_net_id; |
59 | ||
60 | static int tcf_act_police_walker(struct net *net, struct sk_buff *skb, | |
61 | struct netlink_callback *cb, int type, | |
62 | struct tc_action *a) | |
1da177e4 | 63 | { |
ddf97ccd WC |
64 | struct tc_action_net *tn = net_generic(net, police_net_id); |
65 | struct tcf_hashinfo *hinfo = tn->hinfo; | |
89819dc0 | 66 | struct hlist_head *head; |
e9ce1cd3 | 67 | struct tcf_common *p; |
1da177e4 | 68 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
4b3550ef | 69 | struct nlattr *nest; |
1da177e4 | 70 | |
c779f7af | 71 | spin_lock_bh(&hinfo->lock); |
1da177e4 LT |
72 | |
73 | s_i = cb->args[0]; | |
74 | ||
e9ce1cd3 | 75 | for (i = 0; i < (POL_TAB_MASK + 1); i++) { |
c779f7af | 76 | head = &hinfo->htab[tcf_hash(i, POL_TAB_MASK)]; |
1da177e4 | 77 | |
89819dc0 | 78 | hlist_for_each_entry_rcu(p, head, tcfc_head) { |
1da177e4 LT |
79 | index++; |
80 | if (index < s_i) | |
81 | continue; | |
82 | a->priv = p; | |
83 | a->order = index; | |
4b3550ef PM |
84 | nest = nla_nest_start(skb, a->order); |
85 | if (nest == NULL) | |
86 | goto nla_put_failure; | |
1da177e4 LT |
87 | if (type == RTM_DELACTION) |
88 | err = tcf_action_dump_1(skb, a, 0, 1); | |
89 | else | |
90 | err = tcf_action_dump_1(skb, a, 0, 0); | |
91 | if (err < 0) { | |
92 | index--; | |
4b3550ef | 93 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
94 | goto done; |
95 | } | |
4b3550ef | 96 | nla_nest_end(skb, nest); |
1da177e4 LT |
97 | n_i++; |
98 | } | |
99 | } | |
100 | done: | |
c779f7af | 101 | spin_unlock_bh(&hinfo->lock); |
1da177e4 LT |
102 | if (n_i) |
103 | cb->args[0] += n_i; | |
104 | return n_i; | |
105 | ||
7ba699c6 | 106 | nla_put_failure: |
4b3550ef | 107 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
108 | goto done; |
109 | } | |
1da177e4 | 110 | |
53b2bf3f PM |
111 | static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = { |
112 | [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE }, | |
113 | [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE }, | |
114 | [TCA_POLICE_AVRATE] = { .type = NLA_U32 }, | |
115 | [TCA_POLICE_RESULT] = { .type = NLA_U32 }, | |
116 | }; | |
117 | ||
c1b52739 BL |
118 | static int tcf_act_police_locate(struct net *net, struct nlattr *nla, |
119 | struct nlattr *est, struct tc_action *a, | |
120 | int ovr, int bind) | |
1da177e4 | 121 | { |
cc7ec456 | 122 | unsigned int h; |
1da177e4 | 123 | int ret = 0, err; |
7ba699c6 | 124 | struct nlattr *tb[TCA_POLICE_MAX + 1]; |
1da177e4 | 125 | struct tc_police *parm; |
e9ce1cd3 | 126 | struct tcf_police *police; |
1da177e4 | 127 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
ddf97ccd WC |
128 | struct tc_action_net *tn = net_generic(net, police_net_id); |
129 | struct tcf_hashinfo *hinfo = tn->hinfo; | |
1e9b3d53 | 130 | int size; |
1da177e4 | 131 | |
cee63723 | 132 | if (nla == NULL) |
1da177e4 LT |
133 | return -EINVAL; |
134 | ||
53b2bf3f | 135 | err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy); |
cee63723 PM |
136 | if (err < 0) |
137 | return err; | |
138 | ||
7ba699c6 | 139 | if (tb[TCA_POLICE_TBF] == NULL) |
1e9b3d53 | 140 | return -EINVAL; |
7ba699c6 | 141 | size = nla_len(tb[TCA_POLICE_TBF]); |
1e9b3d53 | 142 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) |
1da177e4 | 143 | return -EINVAL; |
7ba699c6 | 144 | parm = nla_data(tb[TCA_POLICE_TBF]); |
1da177e4 | 145 | |
e9ce1cd3 | 146 | if (parm->index) { |
ddf97ccd | 147 | if (tcf_hash_search(tn, a, parm->index)) { |
6e6a50c2 | 148 | police = to_police(a->priv); |
e9ce1cd3 DM |
149 | if (bind) { |
150 | police->tcf_bindcnt += 1; | |
151 | police->tcf_refcnt += 1; | |
1a29321e | 152 | return 0; |
e9ce1cd3 DM |
153 | } |
154 | if (ovr) | |
155 | goto override; | |
1a29321e JHS |
156 | /* not replacing */ |
157 | return -EEXIST; | |
1da177e4 | 158 | } |
1da177e4 LT |
159 | } |
160 | ||
e9ce1cd3 DM |
161 | police = kzalloc(sizeof(*police), GFP_KERNEL); |
162 | if (police == NULL) | |
1da177e4 | 163 | return -ENOMEM; |
1da177e4 | 164 | ret = ACT_P_CREATED; |
e9ce1cd3 DM |
165 | police->tcf_refcnt = 1; |
166 | spin_lock_init(&police->tcf_lock); | |
1da177e4 | 167 | if (bind) |
e9ce1cd3 | 168 | police->tcf_bindcnt = 1; |
1da177e4 LT |
169 | override: |
170 | if (parm->rate.rate) { | |
171 | err = -ENOMEM; | |
7ba699c6 | 172 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]); |
1da177e4 LT |
173 | if (R_tab == NULL) |
174 | goto failure; | |
c1b56878 | 175 | |
1da177e4 LT |
176 | if (parm->peakrate.rate) { |
177 | P_tab = qdisc_get_rtab(&parm->peakrate, | |
7ba699c6 | 178 | tb[TCA_POLICE_PEAKRATE]); |
71bcb09a | 179 | if (P_tab == NULL) |
1da177e4 | 180 | goto failure; |
1da177e4 LT |
181 | } |
182 | } | |
71bcb09a | 183 | |
e9ce1cd3 | 184 | spin_lock_bh(&police->tcf_lock); |
71bcb09a | 185 | if (est) { |
22e0f8b9 | 186 | err = gen_replace_estimator(&police->tcf_bstats, NULL, |
71bcb09a SH |
187 | &police->tcf_rate_est, |
188 | &police->tcf_lock, est); | |
189 | if (err) | |
190 | goto failure_unlock; | |
a883bf56 JP |
191 | } else if (tb[TCA_POLICE_AVRATE] && |
192 | (ret == ACT_P_CREATED || | |
193 | !gen_estimator_active(&police->tcf_bstats, | |
194 | &police->tcf_rate_est))) { | |
195 | err = -EINVAL; | |
196 | goto failure_unlock; | |
71bcb09a SH |
197 | } |
198 | ||
199 | /* No failure allowed after this point */ | |
c6d14ff1 JP |
200 | police->tcfp_mtu = parm->mtu; |
201 | if (police->tcfp_mtu == 0) { | |
202 | police->tcfp_mtu = ~0; | |
203 | if (R_tab) | |
204 | police->tcfp_mtu = 255 << R_tab->rate.cell_log; | |
205 | } | |
206 | if (R_tab) { | |
207 | police->rate_present = true; | |
3e1e3aae | 208 | psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0); |
c6d14ff1 JP |
209 | qdisc_put_rtab(R_tab); |
210 | } else { | |
211 | police->rate_present = false; | |
1da177e4 | 212 | } |
c6d14ff1 JP |
213 | if (P_tab) { |
214 | police->peak_present = true; | |
3e1e3aae | 215 | psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0); |
c6d14ff1 JP |
216 | qdisc_put_rtab(P_tab); |
217 | } else { | |
218 | police->peak_present = false; | |
1da177e4 LT |
219 | } |
220 | ||
7ba699c6 | 221 | if (tb[TCA_POLICE_RESULT]) |
1587bac4 | 222 | police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]); |
c6d14ff1 JP |
223 | police->tcfp_burst = PSCHED_TICKS2NS(parm->burst); |
224 | police->tcfp_toks = police->tcfp_burst; | |
225 | if (police->peak_present) { | |
226 | police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak, | |
227 | police->tcfp_mtu); | |
228 | police->tcfp_ptoks = police->tcfp_mtu_ptoks; | |
1da177e4 | 229 | } |
e9ce1cd3 | 230 | police->tcf_action = parm->action; |
1da177e4 | 231 | |
7ba699c6 | 232 | if (tb[TCA_POLICE_AVRATE]) |
1587bac4 | 233 | police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]); |
1da177e4 | 234 | |
e9ce1cd3 | 235 | spin_unlock_bh(&police->tcf_lock); |
1da177e4 LT |
236 | if (ret != ACT_P_CREATED) |
237 | return ret; | |
238 | ||
d2de875c | 239 | police->tcfp_t_c = ktime_get_ns(); |
e9ce1cd3 | 240 | police->tcf_index = parm->index ? parm->index : |
ddf97ccd | 241 | tcf_hash_new_index(tn); |
3d3ed181 JHS |
242 | police->tcf_tm.install = jiffies; |
243 | police->tcf_tm.lastuse = jiffies; | |
53eb440f | 244 | police->tcf_tm.firstuse = 0; |
e9ce1cd3 | 245 | h = tcf_hash(police->tcf_index, POL_TAB_MASK); |
c779f7af WC |
246 | spin_lock_bh(&hinfo->lock); |
247 | hlist_add_head(&police->tcf_head, &hinfo->htab[h]); | |
248 | spin_unlock_bh(&hinfo->lock); | |
1da177e4 | 249 | |
e9ce1cd3 | 250 | a->priv = police; |
1da177e4 LT |
251 | return ret; |
252 | ||
71bcb09a SH |
253 | failure_unlock: |
254 | spin_unlock_bh(&police->tcf_lock); | |
1da177e4 | 255 | failure: |
3b69a4c9 YY |
256 | qdisc_put_rtab(P_tab); |
257 | qdisc_put_rtab(R_tab); | |
1da177e4 | 258 | if (ret == ACT_P_CREATED) |
e9ce1cd3 | 259 | kfree(police); |
1da177e4 LT |
260 | return err; |
261 | } | |
262 | ||
dc7f9f6e | 263 | static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a, |
10297b99 | 264 | struct tcf_result *res) |
1da177e4 | 265 | { |
e9ce1cd3 | 266 | struct tcf_police *police = a->priv; |
c6d14ff1 JP |
267 | s64 now; |
268 | s64 toks; | |
269 | s64 ptoks = 0; | |
1da177e4 | 270 | |
e9ce1cd3 | 271 | spin_lock(&police->tcf_lock); |
1da177e4 | 272 | |
bfe0d029 | 273 | bstats_update(&police->tcf_bstats, skb); |
3d3ed181 | 274 | tcf_lastuse_update(&police->tcf_tm); |
1da177e4 | 275 | |
e9ce1cd3 DM |
276 | if (police->tcfp_ewma_rate && |
277 | police->tcf_rate_est.bps >= police->tcfp_ewma_rate) { | |
278 | police->tcf_qstats.overlimits++; | |
b9647580 JP |
279 | if (police->tcf_action == TC_ACT_SHOT) |
280 | police->tcf_qstats.drops++; | |
e9ce1cd3 DM |
281 | spin_unlock(&police->tcf_lock); |
282 | return police->tcf_action; | |
1da177e4 | 283 | } |
1da177e4 | 284 | |
0abf77e5 | 285 | if (qdisc_pkt_len(skb) <= police->tcfp_mtu) { |
c6d14ff1 | 286 | if (!police->rate_present) { |
e9ce1cd3 DM |
287 | spin_unlock(&police->tcf_lock); |
288 | return police->tcfp_result; | |
1da177e4 LT |
289 | } |
290 | ||
d2de875c | 291 | now = ktime_get_ns(); |
c6d14ff1 JP |
292 | toks = min_t(s64, now - police->tcfp_t_c, |
293 | police->tcfp_burst); | |
294 | if (police->peak_present) { | |
e9ce1cd3 | 295 | ptoks = toks + police->tcfp_ptoks; |
c6d14ff1 JP |
296 | if (ptoks > police->tcfp_mtu_ptoks) |
297 | ptoks = police->tcfp_mtu_ptoks; | |
298 | ptoks -= (s64) psched_l2t_ns(&police->peak, | |
299 | qdisc_pkt_len(skb)); | |
1da177e4 | 300 | } |
e9ce1cd3 | 301 | toks += police->tcfp_toks; |
c6d14ff1 | 302 | if (toks > police->tcfp_burst) |
e9ce1cd3 | 303 | toks = police->tcfp_burst; |
c6d14ff1 | 304 | toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb)); |
1da177e4 | 305 | if ((toks|ptoks) >= 0) { |
e9ce1cd3 DM |
306 | police->tcfp_t_c = now; |
307 | police->tcfp_toks = toks; | |
308 | police->tcfp_ptoks = ptoks; | |
309 | spin_unlock(&police->tcf_lock); | |
310 | return police->tcfp_result; | |
1da177e4 LT |
311 | } |
312 | } | |
313 | ||
e9ce1cd3 | 314 | police->tcf_qstats.overlimits++; |
b9647580 JP |
315 | if (police->tcf_action == TC_ACT_SHOT) |
316 | police->tcf_qstats.drops++; | |
e9ce1cd3 DM |
317 | spin_unlock(&police->tcf_lock); |
318 | return police->tcf_action; | |
1da177e4 LT |
319 | } |
320 | ||
321 | static int | |
322 | tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
323 | { | |
27a884dc | 324 | unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3 | 325 | struct tcf_police *police = a->priv; |
0f04cfd0 JM |
326 | struct tc_police opt = { |
327 | .index = police->tcf_index, | |
328 | .action = police->tcf_action, | |
329 | .mtu = police->tcfp_mtu, | |
c6d14ff1 | 330 | .burst = PSCHED_NS2TICKS(police->tcfp_burst), |
0f04cfd0 JM |
331 | .refcnt = police->tcf_refcnt - ref, |
332 | .bindcnt = police->tcf_bindcnt - bind, | |
333 | }; | |
3d3ed181 | 334 | struct tcf_t t; |
0f04cfd0 | 335 | |
c6d14ff1 | 336 | if (police->rate_present) |
01cb71d2 | 337 | psched_ratecfg_getrate(&opt.rate, &police->rate); |
c6d14ff1 | 338 | if (police->peak_present) |
01cb71d2 | 339 | psched_ratecfg_getrate(&opt.peakrate, &police->peak); |
1b34ec43 DM |
340 | if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) |
341 | goto nla_put_failure; | |
342 | if (police->tcfp_result && | |
343 | nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result)) | |
344 | goto nla_put_failure; | |
345 | if (police->tcfp_ewma_rate && | |
346 | nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate)) | |
347 | goto nla_put_failure; | |
3d3ed181 JHS |
348 | |
349 | t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install); | |
350 | t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse); | |
53eb440f | 351 | t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse); |
3d3ed181 JHS |
352 | t.expires = jiffies_to_clock_t(police->tcf_tm.expires); |
353 | if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD)) | |
354 | goto nla_put_failure; | |
355 | ||
1da177e4 LT |
356 | return skb->len; |
357 | ||
7ba699c6 | 358 | nla_put_failure: |
dc5fc579 | 359 | nlmsg_trim(skb, b); |
1da177e4 LT |
360 | return -1; |
361 | } | |
362 | ||
ddf97ccd WC |
363 | static int tcf_police_search(struct net *net, struct tc_action *a, u32 index) |
364 | { | |
365 | struct tc_action_net *tn = net_generic(net, police_net_id); | |
366 | ||
367 | return tcf_hash_search(tn, a, index); | |
368 | } | |
369 | ||
1da177e4 LT |
370 | MODULE_AUTHOR("Alexey Kuznetsov"); |
371 | MODULE_DESCRIPTION("Policing actions"); | |
372 | MODULE_LICENSE("GPL"); | |
373 | ||
374 | static struct tc_action_ops act_police_ops = { | |
375 | .kind = "police", | |
376 | .type = TCA_ID_POLICE, | |
1da177e4 LT |
377 | .owner = THIS_MODULE, |
378 | .act = tcf_act_police, | |
379 | .dump = tcf_act_police_dump, | |
1da177e4 | 380 | .init = tcf_act_police_locate, |
ddf97ccd WC |
381 | .walk = tcf_act_police_walker, |
382 | .lookup = tcf_police_search, | |
383 | }; | |
384 | ||
385 | static __net_init int police_init_net(struct net *net) | |
386 | { | |
387 | struct tc_action_net *tn = net_generic(net, police_net_id); | |
388 | ||
389 | return tc_action_net_init(tn, &act_police_ops, POL_TAB_MASK); | |
390 | } | |
391 | ||
392 | static void __net_exit police_exit_net(struct net *net) | |
393 | { | |
394 | struct tc_action_net *tn = net_generic(net, police_net_id); | |
395 | ||
396 | tc_action_net_exit(tn); | |
397 | } | |
398 | ||
399 | static struct pernet_operations police_net_ops = { | |
400 | .init = police_init_net, | |
401 | .exit = police_exit_net, | |
402 | .id = &police_net_id, | |
403 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
404 | }; |
405 | ||
406 | static int __init | |
407 | police_init_module(void) | |
408 | { | |
ddf97ccd | 409 | return tcf_register_action(&act_police_ops, &police_net_ops); |
1da177e4 LT |
410 | } |
411 | ||
412 | static void __exit | |
413 | police_cleanup_module(void) | |
414 | { | |
ddf97ccd | 415 | tcf_unregister_action(&act_police_ops, &police_net_ops); |
1da177e4 LT |
416 | } |
417 | ||
418 | module_init(police_init_module); | |
419 | module_exit(police_cleanup_module); |