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, <kuznet@ms2.inr.ac.ru> | |
10 | * J Hadi Salim (action changes) | |
11 | */ | |
12 | ||
13 | #include <asm/uaccess.h> | |
14 | #include <asm/system.h> | |
15 | #include <linux/bitops.h> | |
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/types.h> | |
18 | #include <linux/kernel.h> | |
1da177e4 LT |
19 | #include <linux/string.h> |
20 | #include <linux/mm.h> | |
21 | #include <linux/socket.h> | |
22 | #include <linux/sockios.h> | |
23 | #include <linux/in.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/netdevice.h> | |
27 | #include <linux/skbuff.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/rtnetlink.h> | |
30 | #include <linux/init.h> | |
31 | #include <net/sock.h> | |
32 | #include <net/act_api.h> | |
dc5fc579 | 33 | #include <net/netlink.h> |
1da177e4 | 34 | |
e9ce1cd3 DM |
35 | #define L2T(p,L) ((p)->tcfp_R_tab->data[(L)>>(p)->tcfp_R_tab->rate.cell_log]) |
36 | #define L2T_P(p,L) ((p)->tcfp_P_tab->data[(L)>>(p)->tcfp_P_tab->rate.cell_log]) | |
1da177e4 | 37 | |
e9ce1cd3 DM |
38 | #define POL_TAB_MASK 15 |
39 | static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1]; | |
40 | static u32 police_idx_gen; | |
41 | static DEFINE_RWLOCK(police_lock); | |
1da177e4 | 42 | |
e9ce1cd3 DM |
43 | static struct tcf_hashinfo police_hash_info = { |
44 | .htab = tcf_police_ht, | |
45 | .hmask = POL_TAB_MASK, | |
46 | .lock = &police_lock, | |
47 | }; | |
1da177e4 | 48 | |
1e9b3d53 PM |
49 | /* old policer structure from before tc actions */ |
50 | struct tc_police_compat | |
51 | { | |
52 | u32 index; | |
53 | int action; | |
54 | u32 limit; | |
55 | u32 burst; | |
56 | u32 mtu; | |
57 | struct tc_ratespec rate; | |
58 | struct tc_ratespec peakrate; | |
59 | }; | |
60 | ||
e9ce1cd3 | 61 | /* Each policer is serialized by its individual spinlock */ |
1da177e4 LT |
62 | |
63 | #ifdef CONFIG_NET_CLS_ACT | |
83b950c8 | 64 | static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb, |
10297b99 | 65 | int type, struct tc_action *a) |
1da177e4 | 66 | { |
e9ce1cd3 | 67 | struct tcf_common *p; |
1da177e4 LT |
68 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
69 | struct rtattr *r; | |
70 | ||
71 | read_lock(&police_lock); | |
72 | ||
73 | s_i = cb->args[0]; | |
74 | ||
e9ce1cd3 DM |
75 | for (i = 0; i < (POL_TAB_MASK + 1); i++) { |
76 | p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)]; | |
1da177e4 | 77 | |
e9ce1cd3 | 78 | for (; p; p = p->tcfc_next) { |
1da177e4 LT |
79 | index++; |
80 | if (index < s_i) | |
81 | continue; | |
82 | a->priv = p; | |
83 | a->order = index; | |
27a884dc | 84 | r = (struct rtattr *)skb_tail_pointer(skb); |
1da177e4 LT |
85 | RTA_PUT(skb, a->order, 0, NULL); |
86 | if (type == RTM_DELACTION) | |
87 | err = tcf_action_dump_1(skb, a, 0, 1); | |
88 | else | |
89 | err = tcf_action_dump_1(skb, a, 0, 0); | |
90 | if (err < 0) { | |
91 | index--; | |
dc5fc579 | 92 | nlmsg_trim(skb, r); |
1da177e4 LT |
93 | goto done; |
94 | } | |
27a884dc | 95 | r->rta_len = skb_tail_pointer(skb) - (u8 *)r; |
1da177e4 LT |
96 | n_i++; |
97 | } | |
98 | } | |
99 | done: | |
100 | read_unlock(&police_lock); | |
101 | if (n_i) | |
102 | cb->args[0] += n_i; | |
103 | return n_i; | |
104 | ||
105 | rtattr_failure: | |
dc5fc579 | 106 | nlmsg_trim(skb, r); |
1da177e4 LT |
107 | goto done; |
108 | } | |
1da177e4 LT |
109 | #endif |
110 | ||
1da177e4 LT |
111 | void tcf_police_destroy(struct tcf_police *p) |
112 | { | |
e9ce1cd3 DM |
113 | unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK); |
114 | struct tcf_common **p1p; | |
10297b99 | 115 | |
e9ce1cd3 DM |
116 | for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) { |
117 | if (*p1p == &p->common) { | |
1da177e4 | 118 | write_lock_bh(&police_lock); |
e9ce1cd3 | 119 | *p1p = p->tcf_next; |
1da177e4 LT |
120 | write_unlock_bh(&police_lock); |
121 | #ifdef CONFIG_NET_ESTIMATOR | |
e9ce1cd3 DM |
122 | gen_kill_estimator(&p->tcf_bstats, |
123 | &p->tcf_rate_est); | |
1da177e4 | 124 | #endif |
e9ce1cd3 DM |
125 | if (p->tcfp_R_tab) |
126 | qdisc_put_rtab(p->tcfp_R_tab); | |
127 | if (p->tcfp_P_tab) | |
128 | qdisc_put_rtab(p->tcfp_P_tab); | |
1da177e4 LT |
129 | kfree(p); |
130 | return; | |
131 | } | |
132 | } | |
133 | BUG_TRAP(0); | |
134 | } | |
135 | ||
136 | #ifdef CONFIG_NET_CLS_ACT | |
137 | static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est, | |
10297b99 | 138 | struct tc_action *a, int ovr, int bind) |
1da177e4 LT |
139 | { |
140 | unsigned h; | |
141 | int ret = 0, err; | |
142 | struct rtattr *tb[TCA_POLICE_MAX]; | |
143 | struct tc_police *parm; | |
e9ce1cd3 | 144 | struct tcf_police *police; |
1da177e4 | 145 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
1e9b3d53 | 146 | int size; |
1da177e4 LT |
147 | |
148 | if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0) | |
149 | return -EINVAL; | |
150 | ||
1e9b3d53 PM |
151 | if (tb[TCA_POLICE_TBF-1] == NULL) |
152 | return -EINVAL; | |
153 | size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]); | |
154 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) | |
1da177e4 LT |
155 | return -EINVAL; |
156 | parm = RTA_DATA(tb[TCA_POLICE_TBF-1]); | |
157 | ||
158 | if (tb[TCA_POLICE_RESULT-1] != NULL && | |
159 | RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32)) | |
160 | return -EINVAL; | |
161 | if (tb[TCA_POLICE_RESULT-1] != NULL && | |
162 | RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32)) | |
163 | return -EINVAL; | |
164 | ||
e9ce1cd3 DM |
165 | if (parm->index) { |
166 | struct tcf_common *pc; | |
167 | ||
168 | pc = tcf_hash_lookup(parm->index, &police_hash_info); | |
169 | if (pc != NULL) { | |
170 | a->priv = pc; | |
171 | police = to_police(pc); | |
172 | if (bind) { | |
173 | police->tcf_bindcnt += 1; | |
174 | police->tcf_refcnt += 1; | |
175 | } | |
176 | if (ovr) | |
177 | goto override; | |
178 | return ret; | |
1da177e4 | 179 | } |
1da177e4 LT |
180 | } |
181 | ||
e9ce1cd3 DM |
182 | police = kzalloc(sizeof(*police), GFP_KERNEL); |
183 | if (police == NULL) | |
1da177e4 | 184 | return -ENOMEM; |
1da177e4 | 185 | ret = ACT_P_CREATED; |
e9ce1cd3 DM |
186 | police->tcf_refcnt = 1; |
187 | spin_lock_init(&police->tcf_lock); | |
188 | police->tcf_stats_lock = &police->tcf_lock; | |
1da177e4 | 189 | if (bind) |
e9ce1cd3 | 190 | police->tcf_bindcnt = 1; |
1da177e4 LT |
191 | override: |
192 | if (parm->rate.rate) { | |
193 | err = -ENOMEM; | |
194 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]); | |
195 | if (R_tab == NULL) | |
196 | goto failure; | |
197 | if (parm->peakrate.rate) { | |
198 | P_tab = qdisc_get_rtab(&parm->peakrate, | |
199 | tb[TCA_POLICE_PEAKRATE-1]); | |
e9ce1cd3 | 200 | if (P_tab == NULL) { |
1da177e4 LT |
201 | qdisc_put_rtab(R_tab); |
202 | goto failure; | |
203 | } | |
204 | } | |
205 | } | |
206 | /* No failure allowed after this point */ | |
e9ce1cd3 | 207 | spin_lock_bh(&police->tcf_lock); |
1da177e4 | 208 | if (R_tab != NULL) { |
e9ce1cd3 DM |
209 | qdisc_put_rtab(police->tcfp_R_tab); |
210 | police->tcfp_R_tab = R_tab; | |
1da177e4 LT |
211 | } |
212 | if (P_tab != NULL) { | |
e9ce1cd3 DM |
213 | qdisc_put_rtab(police->tcfp_P_tab); |
214 | police->tcfp_P_tab = P_tab; | |
1da177e4 LT |
215 | } |
216 | ||
217 | if (tb[TCA_POLICE_RESULT-1]) | |
e9ce1cd3 DM |
218 | police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]); |
219 | police->tcfp_toks = police->tcfp_burst = parm->burst; | |
220 | police->tcfp_mtu = parm->mtu; | |
221 | if (police->tcfp_mtu == 0) { | |
222 | police->tcfp_mtu = ~0; | |
223 | if (police->tcfp_R_tab) | |
224 | police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log; | |
1da177e4 | 225 | } |
e9ce1cd3 DM |
226 | if (police->tcfp_P_tab) |
227 | police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu); | |
228 | police->tcf_action = parm->action; | |
1da177e4 LT |
229 | |
230 | #ifdef CONFIG_NET_ESTIMATOR | |
231 | if (tb[TCA_POLICE_AVRATE-1]) | |
e9ce1cd3 DM |
232 | police->tcfp_ewma_rate = |
233 | *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]); | |
1da177e4 | 234 | if (est) |
e9ce1cd3 DM |
235 | gen_replace_estimator(&police->tcf_bstats, |
236 | &police->tcf_rate_est, | |
237 | police->tcf_stats_lock, est); | |
1da177e4 LT |
238 | #endif |
239 | ||
e9ce1cd3 | 240 | spin_unlock_bh(&police->tcf_lock); |
1da177e4 LT |
241 | if (ret != ACT_P_CREATED) |
242 | return ret; | |
243 | ||
3bebcda2 | 244 | police->tcfp_t_c = psched_get_time(); |
e9ce1cd3 DM |
245 | police->tcf_index = parm->index ? parm->index : |
246 | tcf_hash_new_index(&police_idx_gen, &police_hash_info); | |
247 | h = tcf_hash(police->tcf_index, POL_TAB_MASK); | |
1da177e4 | 248 | write_lock_bh(&police_lock); |
e9ce1cd3 DM |
249 | police->tcf_next = tcf_police_ht[h]; |
250 | tcf_police_ht[h] = &police->common; | |
1da177e4 LT |
251 | write_unlock_bh(&police_lock); |
252 | ||
e9ce1cd3 | 253 | a->priv = police; |
1da177e4 LT |
254 | return ret; |
255 | ||
256 | failure: | |
257 | if (ret == ACT_P_CREATED) | |
e9ce1cd3 | 258 | kfree(police); |
1da177e4 LT |
259 | return err; |
260 | } | |
261 | ||
262 | static int tcf_act_police_cleanup(struct tc_action *a, int bind) | |
263 | { | |
e9ce1cd3 | 264 | struct tcf_police *p = a->priv; |
1da177e4 LT |
265 | |
266 | if (p != NULL) | |
267 | return tcf_police_release(p, bind); | |
268 | return 0; | |
269 | } | |
270 | ||
f43c5a0d | 271 | static int tcf_act_police(struct sk_buff *skb, struct tc_action *a, |
10297b99 | 272 | struct tcf_result *res) |
1da177e4 | 273 | { |
e9ce1cd3 | 274 | struct tcf_police *police = a->priv; |
1da177e4 | 275 | psched_time_t now; |
1da177e4 LT |
276 | long toks; |
277 | long ptoks = 0; | |
278 | ||
e9ce1cd3 | 279 | spin_lock(&police->tcf_lock); |
1da177e4 | 280 | |
e9ce1cd3 DM |
281 | police->tcf_bstats.bytes += skb->len; |
282 | police->tcf_bstats.packets++; | |
1da177e4 LT |
283 | |
284 | #ifdef CONFIG_NET_ESTIMATOR | |
e9ce1cd3 DM |
285 | if (police->tcfp_ewma_rate && |
286 | police->tcf_rate_est.bps >= police->tcfp_ewma_rate) { | |
287 | police->tcf_qstats.overlimits++; | |
288 | spin_unlock(&police->tcf_lock); | |
289 | return police->tcf_action; | |
1da177e4 LT |
290 | } |
291 | #endif | |
292 | ||
e9ce1cd3 DM |
293 | if (skb->len <= police->tcfp_mtu) { |
294 | if (police->tcfp_R_tab == NULL) { | |
295 | spin_unlock(&police->tcf_lock); | |
296 | return police->tcfp_result; | |
1da177e4 LT |
297 | } |
298 | ||
3bebcda2 | 299 | now = psched_get_time(); |
03cc45c0 PM |
300 | toks = psched_tdiff_bounded(now, police->tcfp_t_c, |
301 | police->tcfp_burst); | |
e9ce1cd3 DM |
302 | if (police->tcfp_P_tab) { |
303 | ptoks = toks + police->tcfp_ptoks; | |
304 | if (ptoks > (long)L2T_P(police, police->tcfp_mtu)) | |
305 | ptoks = (long)L2T_P(police, police->tcfp_mtu); | |
306 | ptoks -= L2T_P(police, skb->len); | |
1da177e4 | 307 | } |
e9ce1cd3 DM |
308 | toks += police->tcfp_toks; |
309 | if (toks > (long)police->tcfp_burst) | |
310 | toks = police->tcfp_burst; | |
311 | toks -= L2T(police, skb->len); | |
1da177e4 | 312 | if ((toks|ptoks) >= 0) { |
e9ce1cd3 DM |
313 | police->tcfp_t_c = now; |
314 | police->tcfp_toks = toks; | |
315 | police->tcfp_ptoks = ptoks; | |
316 | spin_unlock(&police->tcf_lock); | |
317 | return police->tcfp_result; | |
1da177e4 LT |
318 | } |
319 | } | |
320 | ||
e9ce1cd3 DM |
321 | police->tcf_qstats.overlimits++; |
322 | spin_unlock(&police->tcf_lock); | |
323 | return police->tcf_action; | |
1da177e4 LT |
324 | } |
325 | ||
326 | static int | |
327 | tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
328 | { | |
27a884dc | 329 | unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3 | 330 | struct tcf_police *police = a->priv; |
1da177e4 | 331 | struct tc_police opt; |
e9ce1cd3 DM |
332 | |
333 | opt.index = police->tcf_index; | |
334 | opt.action = police->tcf_action; | |
335 | opt.mtu = police->tcfp_mtu; | |
336 | opt.burst = police->tcfp_burst; | |
337 | opt.refcnt = police->tcf_refcnt - ref; | |
338 | opt.bindcnt = police->tcf_bindcnt - bind; | |
339 | if (police->tcfp_R_tab) | |
340 | opt.rate = police->tcfp_R_tab->rate; | |
1da177e4 LT |
341 | else |
342 | memset(&opt.rate, 0, sizeof(opt.rate)); | |
e9ce1cd3 DM |
343 | if (police->tcfp_P_tab) |
344 | opt.peakrate = police->tcfp_P_tab->rate; | |
1da177e4 LT |
345 | else |
346 | memset(&opt.peakrate, 0, sizeof(opt.peakrate)); | |
347 | RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt); | |
e9ce1cd3 DM |
348 | if (police->tcfp_result) |
349 | RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), | |
350 | &police->tcfp_result); | |
1da177e4 | 351 | #ifdef CONFIG_NET_ESTIMATOR |
e9ce1cd3 DM |
352 | if (police->tcfp_ewma_rate) |
353 | RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate); | |
1da177e4 LT |
354 | #endif |
355 | return skb->len; | |
356 | ||
357 | rtattr_failure: | |
dc5fc579 | 358 | nlmsg_trim(skb, b); |
1da177e4 LT |
359 | return -1; |
360 | } | |
361 | ||
362 | MODULE_AUTHOR("Alexey Kuznetsov"); | |
363 | MODULE_DESCRIPTION("Policing actions"); | |
364 | MODULE_LICENSE("GPL"); | |
365 | ||
366 | static struct tc_action_ops act_police_ops = { | |
367 | .kind = "police", | |
e9ce1cd3 | 368 | .hinfo = &police_hash_info, |
1da177e4 LT |
369 | .type = TCA_ID_POLICE, |
370 | .capab = TCA_CAP_NONE, | |
371 | .owner = THIS_MODULE, | |
372 | .act = tcf_act_police, | |
373 | .dump = tcf_act_police_dump, | |
374 | .cleanup = tcf_act_police_cleanup, | |
e9ce1cd3 | 375 | .lookup = tcf_hash_search, |
1da177e4 | 376 | .init = tcf_act_police_locate, |
83b950c8 | 377 | .walk = tcf_act_police_walker |
1da177e4 LT |
378 | }; |
379 | ||
380 | static int __init | |
381 | police_init_module(void) | |
382 | { | |
383 | return tcf_register_action(&act_police_ops); | |
384 | } | |
385 | ||
386 | static void __exit | |
387 | police_cleanup_module(void) | |
388 | { | |
389 | tcf_unregister_action(&act_police_ops); | |
390 | } | |
391 | ||
392 | module_init(police_init_module); | |
393 | module_exit(police_cleanup_module); | |
394 | ||
31bd06eb | 395 | #else /* CONFIG_NET_CLS_ACT */ |
1da177e4 | 396 | |
e9ce1cd3 | 397 | static struct tcf_common *tcf_police_lookup(u32 index) |
1da177e4 | 398 | { |
e9ce1cd3 DM |
399 | struct tcf_hashinfo *hinfo = &police_hash_info; |
400 | struct tcf_common *p; | |
401 | ||
402 | read_lock(hinfo->lock); | |
403 | for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p; | |
404 | p = p->tcfc_next) { | |
405 | if (p->tcfc_index == index) | |
406 | break; | |
407 | } | |
408 | read_unlock(hinfo->lock); | |
409 | ||
410 | return p; | |
411 | } | |
412 | ||
413 | static u32 tcf_police_new_index(void) | |
414 | { | |
415 | u32 *idx_gen = &police_idx_gen; | |
416 | u32 val = *idx_gen; | |
417 | ||
418 | do { | |
419 | if (++val == 0) | |
420 | val = 1; | |
421 | } while (tcf_police_lookup(val)); | |
422 | ||
423 | return (*idx_gen = val); | |
424 | } | |
425 | ||
426 | struct tcf_police *tcf_police_locate(struct rtattr *rta, struct rtattr *est) | |
427 | { | |
428 | unsigned int h; | |
429 | struct tcf_police *police; | |
1da177e4 LT |
430 | struct rtattr *tb[TCA_POLICE_MAX]; |
431 | struct tc_police *parm; | |
1e9b3d53 | 432 | int size; |
1da177e4 LT |
433 | |
434 | if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0) | |
435 | return NULL; | |
436 | ||
1e9b3d53 PM |
437 | if (tb[TCA_POLICE_TBF-1] == NULL) |
438 | return NULL; | |
439 | size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]); | |
440 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) | |
1da177e4 LT |
441 | return NULL; |
442 | ||
443 | parm = RTA_DATA(tb[TCA_POLICE_TBF-1]); | |
444 | ||
e9ce1cd3 DM |
445 | if (parm->index) { |
446 | struct tcf_common *pc; | |
1da177e4 | 447 | |
e9ce1cd3 DM |
448 | pc = tcf_police_lookup(parm->index); |
449 | if (pc) { | |
450 | police = to_police(pc); | |
451 | police->tcf_refcnt++; | |
452 | return police; | |
453 | } | |
454 | } | |
455 | police = kzalloc(sizeof(*police), GFP_KERNEL); | |
456 | if (unlikely(!police)) | |
1da177e4 LT |
457 | return NULL; |
458 | ||
e9ce1cd3 DM |
459 | police->tcf_refcnt = 1; |
460 | spin_lock_init(&police->tcf_lock); | |
461 | police->tcf_stats_lock = &police->tcf_lock; | |
1da177e4 | 462 | if (parm->rate.rate) { |
e9ce1cd3 DM |
463 | police->tcfp_R_tab = |
464 | qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]); | |
465 | if (police->tcfp_R_tab == NULL) | |
1da177e4 LT |
466 | goto failure; |
467 | if (parm->peakrate.rate) { | |
e9ce1cd3 DM |
468 | police->tcfp_P_tab = |
469 | qdisc_get_rtab(&parm->peakrate, | |
470 | tb[TCA_POLICE_PEAKRATE-1]); | |
471 | if (police->tcfp_P_tab == NULL) | |
1da177e4 LT |
472 | goto failure; |
473 | } | |
474 | } | |
475 | if (tb[TCA_POLICE_RESULT-1]) { | |
476 | if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32)) | |
477 | goto failure; | |
e9ce1cd3 | 478 | police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]); |
1da177e4 LT |
479 | } |
480 | #ifdef CONFIG_NET_ESTIMATOR | |
481 | if (tb[TCA_POLICE_AVRATE-1]) { | |
482 | if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32)) | |
483 | goto failure; | |
e9ce1cd3 DM |
484 | police->tcfp_ewma_rate = |
485 | *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]); | |
1da177e4 LT |
486 | } |
487 | #endif | |
e9ce1cd3 DM |
488 | police->tcfp_toks = police->tcfp_burst = parm->burst; |
489 | police->tcfp_mtu = parm->mtu; | |
490 | if (police->tcfp_mtu == 0) { | |
491 | police->tcfp_mtu = ~0; | |
492 | if (police->tcfp_R_tab) | |
493 | police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log; | |
1da177e4 | 494 | } |
e9ce1cd3 DM |
495 | if (police->tcfp_P_tab) |
496 | police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu); | |
3bebcda2 | 497 | police->tcfp_t_c = psched_get_time(); |
e9ce1cd3 DM |
498 | police->tcf_index = parm->index ? parm->index : |
499 | tcf_police_new_index(); | |
500 | police->tcf_action = parm->action; | |
1da177e4 LT |
501 | #ifdef CONFIG_NET_ESTIMATOR |
502 | if (est) | |
e9ce1cd3 DM |
503 | gen_new_estimator(&police->tcf_bstats, &police->tcf_rate_est, |
504 | police->tcf_stats_lock, est); | |
1da177e4 | 505 | #endif |
e9ce1cd3 | 506 | h = tcf_hash(police->tcf_index, POL_TAB_MASK); |
1da177e4 | 507 | write_lock_bh(&police_lock); |
e9ce1cd3 DM |
508 | police->tcf_next = tcf_police_ht[h]; |
509 | tcf_police_ht[h] = &police->common; | |
1da177e4 | 510 | write_unlock_bh(&police_lock); |
e9ce1cd3 | 511 | return police; |
1da177e4 LT |
512 | |
513 | failure: | |
e9ce1cd3 DM |
514 | if (police->tcfp_R_tab) |
515 | qdisc_put_rtab(police->tcfp_R_tab); | |
516 | kfree(police); | |
1da177e4 LT |
517 | return NULL; |
518 | } | |
519 | ||
e9ce1cd3 | 520 | int tcf_police(struct sk_buff *skb, struct tcf_police *police) |
1da177e4 LT |
521 | { |
522 | psched_time_t now; | |
523 | long toks; | |
524 | long ptoks = 0; | |
525 | ||
e9ce1cd3 | 526 | spin_lock(&police->tcf_lock); |
1da177e4 | 527 | |
e9ce1cd3 DM |
528 | police->tcf_bstats.bytes += skb->len; |
529 | police->tcf_bstats.packets++; | |
1da177e4 LT |
530 | |
531 | #ifdef CONFIG_NET_ESTIMATOR | |
e9ce1cd3 DM |
532 | if (police->tcfp_ewma_rate && |
533 | police->tcf_rate_est.bps >= police->tcfp_ewma_rate) { | |
534 | police->tcf_qstats.overlimits++; | |
535 | spin_unlock(&police->tcf_lock); | |
536 | return police->tcf_action; | |
1da177e4 LT |
537 | } |
538 | #endif | |
e9ce1cd3 DM |
539 | if (skb->len <= police->tcfp_mtu) { |
540 | if (police->tcfp_R_tab == NULL) { | |
541 | spin_unlock(&police->tcf_lock); | |
542 | return police->tcfp_result; | |
1da177e4 LT |
543 | } |
544 | ||
3bebcda2 | 545 | now = psched_get_time(); |
03cc45c0 PM |
546 | toks = psched_tdiff_bounded(now, police->tcfp_t_c, |
547 | police->tcfp_burst); | |
e9ce1cd3 DM |
548 | if (police->tcfp_P_tab) { |
549 | ptoks = toks + police->tcfp_ptoks; | |
550 | if (ptoks > (long)L2T_P(police, police->tcfp_mtu)) | |
551 | ptoks = (long)L2T_P(police, police->tcfp_mtu); | |
552 | ptoks -= L2T_P(police, skb->len); | |
1da177e4 | 553 | } |
e9ce1cd3 DM |
554 | toks += police->tcfp_toks; |
555 | if (toks > (long)police->tcfp_burst) | |
556 | toks = police->tcfp_burst; | |
557 | toks -= L2T(police, skb->len); | |
1da177e4 | 558 | if ((toks|ptoks) >= 0) { |
e9ce1cd3 DM |
559 | police->tcfp_t_c = now; |
560 | police->tcfp_toks = toks; | |
561 | police->tcfp_ptoks = ptoks; | |
562 | spin_unlock(&police->tcf_lock); | |
563 | return police->tcfp_result; | |
1da177e4 LT |
564 | } |
565 | } | |
566 | ||
e9ce1cd3 DM |
567 | police->tcf_qstats.overlimits++; |
568 | spin_unlock(&police->tcf_lock); | |
569 | return police->tcf_action; | |
1da177e4 | 570 | } |
31bd06eb | 571 | EXPORT_SYMBOL(tcf_police); |
1da177e4 | 572 | |
e9ce1cd3 | 573 | int tcf_police_dump(struct sk_buff *skb, struct tcf_police *police) |
1da177e4 | 574 | { |
27a884dc | 575 | unsigned char *b = skb_tail_pointer(skb); |
1da177e4 LT |
576 | struct tc_police opt; |
577 | ||
e9ce1cd3 DM |
578 | opt.index = police->tcf_index; |
579 | opt.action = police->tcf_action; | |
580 | opt.mtu = police->tcfp_mtu; | |
581 | opt.burst = police->tcfp_burst; | |
582 | if (police->tcfp_R_tab) | |
583 | opt.rate = police->tcfp_R_tab->rate; | |
1da177e4 LT |
584 | else |
585 | memset(&opt.rate, 0, sizeof(opt.rate)); | |
e9ce1cd3 DM |
586 | if (police->tcfp_P_tab) |
587 | opt.peakrate = police->tcfp_P_tab->rate; | |
1da177e4 LT |
588 | else |
589 | memset(&opt.peakrate, 0, sizeof(opt.peakrate)); | |
590 | RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt); | |
e9ce1cd3 DM |
591 | if (police->tcfp_result) |
592 | RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), | |
593 | &police->tcfp_result); | |
1da177e4 | 594 | #ifdef CONFIG_NET_ESTIMATOR |
e9ce1cd3 DM |
595 | if (police->tcfp_ewma_rate) |
596 | RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate); | |
1da177e4 LT |
597 | #endif |
598 | return skb->len; | |
599 | ||
600 | rtattr_failure: | |
dc5fc579 | 601 | nlmsg_trim(skb, b); |
1da177e4 LT |
602 | return -1; |
603 | } | |
604 | ||
e9ce1cd3 | 605 | int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *police) |
1da177e4 LT |
606 | { |
607 | struct gnet_dump d; | |
10297b99 | 608 | |
1da177e4 | 609 | if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, |
e9ce1cd3 DM |
610 | TCA_XSTATS, police->tcf_stats_lock, |
611 | &d) < 0) | |
1da177e4 | 612 | goto errout; |
10297b99 | 613 | |
e9ce1cd3 | 614 | if (gnet_stats_copy_basic(&d, &police->tcf_bstats) < 0 || |
1da177e4 | 615 | #ifdef CONFIG_NET_ESTIMATOR |
e9ce1cd3 | 616 | gnet_stats_copy_rate_est(&d, &police->tcf_rate_est) < 0 || |
1da177e4 | 617 | #endif |
e9ce1cd3 | 618 | gnet_stats_copy_queue(&d, &police->tcf_qstats) < 0) |
1da177e4 LT |
619 | goto errout; |
620 | ||
621 | if (gnet_stats_finish_copy(&d) < 0) | |
622 | goto errout; | |
623 | ||
624 | return 0; | |
625 | ||
626 | errout: | |
627 | return -1; | |
628 | } | |
629 | ||
31bd06eb | 630 | #endif /* CONFIG_NET_CLS_ACT */ |