]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sched/police.c Input police filter. | |
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; | |
29 | u32 tcfp_burst; | |
30 | u32 tcfp_mtu; | |
31 | u32 tcfp_toks; | |
32 | u32 tcfp_ptoks; | |
33 | psched_time_t tcfp_t_c; | |
34 | struct qdisc_rate_table *tcfp_R_tab; | |
35 | struct qdisc_rate_table *tcfp_P_tab; | |
36 | }; | |
37 | #define to_police(pc) \ | |
38 | container_of(pc, struct tcf_police, common) | |
39 | ||
cc7ec456 ED |
40 | #define L2T(p, L) qdisc_l2t((p)->tcfp_R_tab, L) |
41 | #define L2T_P(p, L) qdisc_l2t((p)->tcfp_P_tab, L) | |
1da177e4 | 42 | |
e9ce1cd3 DM |
43 | #define POL_TAB_MASK 15 |
44 | static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1]; | |
45 | static u32 police_idx_gen; | |
46 | static DEFINE_RWLOCK(police_lock); | |
1da177e4 | 47 | |
e9ce1cd3 DM |
48 | static struct tcf_hashinfo police_hash_info = { |
49 | .htab = tcf_police_ht, | |
50 | .hmask = POL_TAB_MASK, | |
51 | .lock = &police_lock, | |
52 | }; | |
1da177e4 | 53 | |
1e9b3d53 | 54 | /* old policer structure from before tc actions */ |
cc7ec456 | 55 | struct tc_police_compat { |
1e9b3d53 PM |
56 | u32 index; |
57 | int action; | |
58 | u32 limit; | |
59 | u32 burst; | |
60 | u32 mtu; | |
61 | struct tc_ratespec rate; | |
62 | struct tc_ratespec peakrate; | |
63 | }; | |
64 | ||
e9ce1cd3 | 65 | /* Each policer is serialized by its individual spinlock */ |
1da177e4 | 66 | |
83b950c8 | 67 | static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb, |
10297b99 | 68 | int type, struct tc_action *a) |
1da177e4 | 69 | { |
e9ce1cd3 | 70 | struct tcf_common *p; |
1da177e4 | 71 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
4b3550ef | 72 | struct nlattr *nest; |
1da177e4 | 73 | |
e1e992e5 | 74 | read_lock_bh(&police_lock); |
1da177e4 LT |
75 | |
76 | s_i = cb->args[0]; | |
77 | ||
e9ce1cd3 DM |
78 | for (i = 0; i < (POL_TAB_MASK + 1); i++) { |
79 | p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)]; | |
1da177e4 | 80 | |
e9ce1cd3 | 81 | for (; p; p = p->tcfc_next) { |
1da177e4 LT |
82 | index++; |
83 | if (index < s_i) | |
84 | continue; | |
85 | a->priv = p; | |
86 | a->order = index; | |
4b3550ef PM |
87 | nest = nla_nest_start(skb, a->order); |
88 | if (nest == NULL) | |
89 | goto nla_put_failure; | |
1da177e4 LT |
90 | if (type == RTM_DELACTION) |
91 | err = tcf_action_dump_1(skb, a, 0, 1); | |
92 | else | |
93 | err = tcf_action_dump_1(skb, a, 0, 0); | |
94 | if (err < 0) { | |
95 | index--; | |
4b3550ef | 96 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
97 | goto done; |
98 | } | |
4b3550ef | 99 | nla_nest_end(skb, nest); |
1da177e4 LT |
100 | n_i++; |
101 | } | |
102 | } | |
103 | done: | |
e1e992e5 | 104 | read_unlock_bh(&police_lock); |
1da177e4 LT |
105 | if (n_i) |
106 | cb->args[0] += n_i; | |
107 | return n_i; | |
108 | ||
7ba699c6 | 109 | nla_put_failure: |
4b3550ef | 110 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
111 | goto done; |
112 | } | |
1da177e4 | 113 | |
c3bc7cff | 114 | static void tcf_police_destroy(struct tcf_police *p) |
1da177e4 | 115 | { |
e9ce1cd3 DM |
116 | unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK); |
117 | struct tcf_common **p1p; | |
10297b99 | 118 | |
e9ce1cd3 DM |
119 | for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) { |
120 | if (*p1p == &p->common) { | |
1da177e4 | 121 | write_lock_bh(&police_lock); |
e9ce1cd3 | 122 | *p1p = p->tcf_next; |
1da177e4 | 123 | write_unlock_bh(&police_lock); |
e9ce1cd3 DM |
124 | gen_kill_estimator(&p->tcf_bstats, |
125 | &p->tcf_rate_est); | |
e9ce1cd3 DM |
126 | if (p->tcfp_R_tab) |
127 | qdisc_put_rtab(p->tcfp_R_tab); | |
128 | if (p->tcfp_P_tab) | |
129 | qdisc_put_rtab(p->tcfp_P_tab); | |
c7de2cf0 ED |
130 | /* |
131 | * gen_estimator est_timer() might access p->tcf_lock | |
132 | * or bstats, wait a RCU grace period before freeing p | |
133 | */ | |
5957b1ac | 134 | kfree_rcu(p, tcf_rcu); |
1da177e4 LT |
135 | return; |
136 | } | |
137 | } | |
547b792c | 138 | WARN_ON(1); |
1da177e4 LT |
139 | } |
140 | ||
53b2bf3f PM |
141 | static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = { |
142 | [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE }, | |
143 | [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE }, | |
144 | [TCA_POLICE_AVRATE] = { .type = NLA_U32 }, | |
145 | [TCA_POLICE_RESULT] = { .type = NLA_U32 }, | |
146 | }; | |
147 | ||
c1b52739 BL |
148 | static int tcf_act_police_locate(struct net *net, struct nlattr *nla, |
149 | struct nlattr *est, struct tc_action *a, | |
150 | int ovr, int bind) | |
1da177e4 | 151 | { |
cc7ec456 | 152 | unsigned int h; |
1da177e4 | 153 | int ret = 0, err; |
7ba699c6 | 154 | struct nlattr *tb[TCA_POLICE_MAX + 1]; |
1da177e4 | 155 | struct tc_police *parm; |
e9ce1cd3 | 156 | struct tcf_police *police; |
1da177e4 | 157 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
1e9b3d53 | 158 | int size; |
1da177e4 | 159 | |
cee63723 | 160 | if (nla == NULL) |
1da177e4 LT |
161 | return -EINVAL; |
162 | ||
53b2bf3f | 163 | err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy); |
cee63723 PM |
164 | if (err < 0) |
165 | return err; | |
166 | ||
7ba699c6 | 167 | if (tb[TCA_POLICE_TBF] == NULL) |
1e9b3d53 | 168 | return -EINVAL; |
7ba699c6 | 169 | size = nla_len(tb[TCA_POLICE_TBF]); |
1e9b3d53 | 170 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) |
1da177e4 | 171 | return -EINVAL; |
7ba699c6 | 172 | parm = nla_data(tb[TCA_POLICE_TBF]); |
1da177e4 | 173 | |
e9ce1cd3 DM |
174 | if (parm->index) { |
175 | struct tcf_common *pc; | |
176 | ||
177 | pc = tcf_hash_lookup(parm->index, &police_hash_info); | |
178 | if (pc != NULL) { | |
179 | a->priv = pc; | |
180 | police = to_police(pc); | |
181 | if (bind) { | |
182 | police->tcf_bindcnt += 1; | |
183 | police->tcf_refcnt += 1; | |
184 | } | |
185 | if (ovr) | |
186 | goto override; | |
187 | return ret; | |
1da177e4 | 188 | } |
1da177e4 LT |
189 | } |
190 | ||
e9ce1cd3 DM |
191 | police = kzalloc(sizeof(*police), GFP_KERNEL); |
192 | if (police == NULL) | |
1da177e4 | 193 | return -ENOMEM; |
1da177e4 | 194 | ret = ACT_P_CREATED; |
e9ce1cd3 DM |
195 | police->tcf_refcnt = 1; |
196 | spin_lock_init(&police->tcf_lock); | |
1da177e4 | 197 | if (bind) |
e9ce1cd3 | 198 | police->tcf_bindcnt = 1; |
1da177e4 LT |
199 | override: |
200 | if (parm->rate.rate) { | |
201 | err = -ENOMEM; | |
7ba699c6 | 202 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]); |
1da177e4 LT |
203 | if (R_tab == NULL) |
204 | goto failure; | |
c1b56878 | 205 | |
1da177e4 LT |
206 | if (parm->peakrate.rate) { |
207 | P_tab = qdisc_get_rtab(&parm->peakrate, | |
7ba699c6 | 208 | tb[TCA_POLICE_PEAKRATE]); |
71bcb09a | 209 | if (P_tab == NULL) |
1da177e4 | 210 | goto failure; |
1da177e4 LT |
211 | } |
212 | } | |
71bcb09a | 213 | |
e9ce1cd3 | 214 | spin_lock_bh(&police->tcf_lock); |
71bcb09a SH |
215 | if (est) { |
216 | err = gen_replace_estimator(&police->tcf_bstats, | |
217 | &police->tcf_rate_est, | |
218 | &police->tcf_lock, est); | |
219 | if (err) | |
220 | goto failure_unlock; | |
a883bf56 JP |
221 | } else if (tb[TCA_POLICE_AVRATE] && |
222 | (ret == ACT_P_CREATED || | |
223 | !gen_estimator_active(&police->tcf_bstats, | |
224 | &police->tcf_rate_est))) { | |
225 | err = -EINVAL; | |
226 | goto failure_unlock; | |
71bcb09a SH |
227 | } |
228 | ||
229 | /* No failure allowed after this point */ | |
1da177e4 | 230 | if (R_tab != NULL) { |
e9ce1cd3 DM |
231 | qdisc_put_rtab(police->tcfp_R_tab); |
232 | police->tcfp_R_tab = R_tab; | |
1da177e4 LT |
233 | } |
234 | if (P_tab != NULL) { | |
e9ce1cd3 DM |
235 | qdisc_put_rtab(police->tcfp_P_tab); |
236 | police->tcfp_P_tab = P_tab; | |
1da177e4 LT |
237 | } |
238 | ||
7ba699c6 | 239 | if (tb[TCA_POLICE_RESULT]) |
1587bac4 | 240 | police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]); |
e9ce1cd3 DM |
241 | police->tcfp_toks = police->tcfp_burst = parm->burst; |
242 | police->tcfp_mtu = parm->mtu; | |
243 | if (police->tcfp_mtu == 0) { | |
244 | police->tcfp_mtu = ~0; | |
245 | if (police->tcfp_R_tab) | |
246 | police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log; | |
1da177e4 | 247 | } |
e9ce1cd3 DM |
248 | if (police->tcfp_P_tab) |
249 | police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu); | |
250 | police->tcf_action = parm->action; | |
1da177e4 | 251 | |
7ba699c6 | 252 | if (tb[TCA_POLICE_AVRATE]) |
1587bac4 | 253 | police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]); |
1da177e4 | 254 | |
e9ce1cd3 | 255 | spin_unlock_bh(&police->tcf_lock); |
1da177e4 LT |
256 | if (ret != ACT_P_CREATED) |
257 | return ret; | |
258 | ||
3bebcda2 | 259 | police->tcfp_t_c = psched_get_time(); |
e9ce1cd3 DM |
260 | police->tcf_index = parm->index ? parm->index : |
261 | tcf_hash_new_index(&police_idx_gen, &police_hash_info); | |
262 | h = tcf_hash(police->tcf_index, POL_TAB_MASK); | |
1da177e4 | 263 | write_lock_bh(&police_lock); |
e9ce1cd3 DM |
264 | police->tcf_next = tcf_police_ht[h]; |
265 | tcf_police_ht[h] = &police->common; | |
1da177e4 LT |
266 | write_unlock_bh(&police_lock); |
267 | ||
e9ce1cd3 | 268 | a->priv = police; |
1da177e4 LT |
269 | return ret; |
270 | ||
71bcb09a SH |
271 | failure_unlock: |
272 | spin_unlock_bh(&police->tcf_lock); | |
1da177e4 | 273 | failure: |
71bcb09a SH |
274 | if (P_tab) |
275 | qdisc_put_rtab(P_tab); | |
276 | if (R_tab) | |
277 | qdisc_put_rtab(R_tab); | |
1da177e4 | 278 | if (ret == ACT_P_CREATED) |
e9ce1cd3 | 279 | kfree(police); |
1da177e4 LT |
280 | return err; |
281 | } | |
282 | ||
283 | static int tcf_act_police_cleanup(struct tc_action *a, int bind) | |
284 | { | |
e9ce1cd3 | 285 | struct tcf_police *p = a->priv; |
c3bc7cff | 286 | int ret = 0; |
1da177e4 | 287 | |
c3bc7cff PM |
288 | if (p != NULL) { |
289 | if (bind) | |
290 | p->tcf_bindcnt--; | |
291 | ||
292 | p->tcf_refcnt--; | |
293 | if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) { | |
294 | tcf_police_destroy(p); | |
295 | ret = 1; | |
296 | } | |
297 | } | |
298 | return ret; | |
1da177e4 LT |
299 | } |
300 | ||
dc7f9f6e | 301 | static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a, |
10297b99 | 302 | struct tcf_result *res) |
1da177e4 | 303 | { |
e9ce1cd3 | 304 | struct tcf_police *police = a->priv; |
1da177e4 | 305 | psched_time_t now; |
1da177e4 LT |
306 | long toks; |
307 | long ptoks = 0; | |
308 | ||
e9ce1cd3 | 309 | spin_lock(&police->tcf_lock); |
1da177e4 | 310 | |
bfe0d029 | 311 | bstats_update(&police->tcf_bstats, skb); |
1da177e4 | 312 | |
e9ce1cd3 DM |
313 | if (police->tcfp_ewma_rate && |
314 | police->tcf_rate_est.bps >= police->tcfp_ewma_rate) { | |
315 | police->tcf_qstats.overlimits++; | |
b9647580 JP |
316 | if (police->tcf_action == TC_ACT_SHOT) |
317 | police->tcf_qstats.drops++; | |
e9ce1cd3 DM |
318 | spin_unlock(&police->tcf_lock); |
319 | return police->tcf_action; | |
1da177e4 | 320 | } |
1da177e4 | 321 | |
0abf77e5 | 322 | if (qdisc_pkt_len(skb) <= police->tcfp_mtu) { |
e9ce1cd3 DM |
323 | if (police->tcfp_R_tab == NULL) { |
324 | spin_unlock(&police->tcf_lock); | |
325 | return police->tcfp_result; | |
1da177e4 LT |
326 | } |
327 | ||
3bebcda2 | 328 | now = psched_get_time(); |
03cc45c0 PM |
329 | toks = psched_tdiff_bounded(now, police->tcfp_t_c, |
330 | police->tcfp_burst); | |
e9ce1cd3 DM |
331 | if (police->tcfp_P_tab) { |
332 | ptoks = toks + police->tcfp_ptoks; | |
333 | if (ptoks > (long)L2T_P(police, police->tcfp_mtu)) | |
334 | ptoks = (long)L2T_P(police, police->tcfp_mtu); | |
0abf77e5 | 335 | ptoks -= L2T_P(police, qdisc_pkt_len(skb)); |
1da177e4 | 336 | } |
e9ce1cd3 DM |
337 | toks += police->tcfp_toks; |
338 | if (toks > (long)police->tcfp_burst) | |
339 | toks = police->tcfp_burst; | |
0abf77e5 | 340 | toks -= L2T(police, qdisc_pkt_len(skb)); |
1da177e4 | 341 | if ((toks|ptoks) >= 0) { |
e9ce1cd3 DM |
342 | police->tcfp_t_c = now; |
343 | police->tcfp_toks = toks; | |
344 | police->tcfp_ptoks = ptoks; | |
345 | spin_unlock(&police->tcf_lock); | |
346 | return police->tcfp_result; | |
1da177e4 LT |
347 | } |
348 | } | |
349 | ||
e9ce1cd3 | 350 | police->tcf_qstats.overlimits++; |
b9647580 JP |
351 | if (police->tcf_action == TC_ACT_SHOT) |
352 | police->tcf_qstats.drops++; | |
e9ce1cd3 DM |
353 | spin_unlock(&police->tcf_lock); |
354 | return police->tcf_action; | |
1da177e4 LT |
355 | } |
356 | ||
357 | static int | |
358 | tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
359 | { | |
27a884dc | 360 | unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3 | 361 | struct tcf_police *police = a->priv; |
0f04cfd0 JM |
362 | struct tc_police opt = { |
363 | .index = police->tcf_index, | |
364 | .action = police->tcf_action, | |
365 | .mtu = police->tcfp_mtu, | |
366 | .burst = police->tcfp_burst, | |
367 | .refcnt = police->tcf_refcnt - ref, | |
368 | .bindcnt = police->tcf_bindcnt - bind, | |
369 | }; | |
370 | ||
e9ce1cd3 DM |
371 | if (police->tcfp_R_tab) |
372 | opt.rate = police->tcfp_R_tab->rate; | |
e9ce1cd3 DM |
373 | if (police->tcfp_P_tab) |
374 | opt.peakrate = police->tcfp_P_tab->rate; | |
1b34ec43 DM |
375 | if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) |
376 | goto nla_put_failure; | |
377 | if (police->tcfp_result && | |
378 | nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result)) | |
379 | goto nla_put_failure; | |
380 | if (police->tcfp_ewma_rate && | |
381 | nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate)) | |
382 | goto nla_put_failure; | |
1da177e4 LT |
383 | return skb->len; |
384 | ||
7ba699c6 | 385 | nla_put_failure: |
dc5fc579 | 386 | nlmsg_trim(skb, b); |
1da177e4 LT |
387 | return -1; |
388 | } | |
389 | ||
390 | MODULE_AUTHOR("Alexey Kuznetsov"); | |
391 | MODULE_DESCRIPTION("Policing actions"); | |
392 | MODULE_LICENSE("GPL"); | |
393 | ||
394 | static struct tc_action_ops act_police_ops = { | |
395 | .kind = "police", | |
e9ce1cd3 | 396 | .hinfo = &police_hash_info, |
1da177e4 LT |
397 | .type = TCA_ID_POLICE, |
398 | .capab = TCA_CAP_NONE, | |
399 | .owner = THIS_MODULE, | |
400 | .act = tcf_act_police, | |
401 | .dump = tcf_act_police_dump, | |
402 | .cleanup = tcf_act_police_cleanup, | |
e9ce1cd3 | 403 | .lookup = tcf_hash_search, |
1da177e4 | 404 | .init = tcf_act_police_locate, |
83b950c8 | 405 | .walk = tcf_act_police_walker |
1da177e4 LT |
406 | }; |
407 | ||
408 | static int __init | |
409 | police_init_module(void) | |
410 | { | |
411 | return tcf_register_action(&act_police_ops); | |
412 | } | |
413 | ||
414 | static void __exit | |
415 | police_cleanup_module(void) | |
416 | { | |
417 | tcf_unregister_action(&act_police_ops); | |
418 | } | |
419 | ||
420 | module_init(police_init_module); | |
421 | module_exit(police_cleanup_module); |