2 * net/sched/police.c Input police filter.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim (action changes)
13 #include <asm/uaccess.h>
14 #include <asm/system.h>
15 #include <linux/bitops.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/module.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/init.h>
33 #include <net/act_api.h>
35 #define L2T(p,L) ((p)->R_tab->data[(L)>>(p)->R_tab->rate.cell_log])
36 #define L2T_P(p,L) ((p)->P_tab->data[(L)>>(p)->P_tab->rate.cell_log])
37 #define PRIV(a) ((struct tcf_police *) (a)->priv)
39 /* use generic hash table */
40 #define MY_TAB_SIZE 16
41 #define MY_TAB_MASK 15
43 static struct tcf_police *tcf_police_ht[MY_TAB_SIZE];
44 /* Policer hash table lock */
45 static DEFINE_RWLOCK(police_lock);
47 /* Each policer is serialized by its individual spinlock */
49 static __inline__ unsigned tcf_police_hash(u32 index)
54 static __inline__ struct tcf_police * tcf_police_lookup(u32 index)
58 read_lock(&police_lock);
59 for (p = tcf_police_ht[tcf_police_hash(index)]; p; p = p->next) {
60 if (p->index == index)
63 read_unlock(&police_lock);
67 #ifdef CONFIG_NET_CLS_ACT
68 static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
69 int type, struct tc_action *a)
72 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
75 read_lock(&police_lock);
79 for (i = 0; i < MY_TAB_SIZE; i++) {
80 p = tcf_police_ht[tcf_police_hash(i)];
82 for (; p; p = p->next) {
88 r = (struct rtattr*) skb->tail;
89 RTA_PUT(skb, a->order, 0, NULL);
90 if (type == RTM_DELACTION)
91 err = tcf_action_dump_1(skb, a, 0, 1);
93 err = tcf_action_dump_1(skb, a, 0, 0);
96 skb_trim(skb, (u8*)r - skb->data);
99 r->rta_len = skb->tail - (u8*)r;
104 read_unlock(&police_lock);
110 skb_trim(skb, (u8*)r - skb->data);
115 tcf_act_police_hash_search(struct tc_action *a, u32 index)
117 struct tcf_police *p = tcf_police_lookup(index);
128 static inline u32 tcf_police_new_index(void)
133 } while (tcf_police_lookup(idx_gen));
138 void tcf_police_destroy(struct tcf_police *p)
140 unsigned h = tcf_police_hash(p->index);
141 struct tcf_police **p1p;
143 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->next) {
145 write_lock_bh(&police_lock);
147 write_unlock_bh(&police_lock);
148 #ifdef CONFIG_NET_ESTIMATOR
149 gen_kill_estimator(&p->bstats, &p->rate_est);
152 qdisc_put_rtab(p->R_tab);
154 qdisc_put_rtab(p->P_tab);
162 #ifdef CONFIG_NET_CLS_ACT
163 static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
164 struct tc_action *a, int ovr, int bind)
168 struct rtattr *tb[TCA_POLICE_MAX];
169 struct tc_police *parm;
170 struct tcf_police *p;
171 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
173 if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
176 if (tb[TCA_POLICE_TBF-1] == NULL ||
177 RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm))
179 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
181 if (tb[TCA_POLICE_RESULT-1] != NULL &&
182 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
184 if (tb[TCA_POLICE_RESULT-1] != NULL &&
185 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
188 if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
199 p = kzalloc(sizeof(*p), GFP_KERNEL);
205 spin_lock_init(&p->lock);
206 p->stats_lock = &p->lock;
210 if (parm->rate.rate) {
212 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
215 if (parm->peakrate.rate) {
216 P_tab = qdisc_get_rtab(&parm->peakrate,
217 tb[TCA_POLICE_PEAKRATE-1]);
218 if (p->P_tab == NULL) {
219 qdisc_put_rtab(R_tab);
224 /* No failure allowed after this point */
225 spin_lock_bh(&p->lock);
227 qdisc_put_rtab(p->R_tab);
231 qdisc_put_rtab(p->P_tab);
235 if (tb[TCA_POLICE_RESULT-1])
236 p->result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
237 p->toks = p->burst = parm->burst;
242 p->mtu = 255<<p->R_tab->rate.cell_log;
245 p->ptoks = L2T_P(p, p->mtu);
246 p->action = parm->action;
248 #ifdef CONFIG_NET_ESTIMATOR
249 if (tb[TCA_POLICE_AVRATE-1])
250 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
252 gen_replace_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
255 spin_unlock_bh(&p->lock);
256 if (ret != ACT_P_CREATED)
259 PSCHED_GET_TIME(p->t_c);
260 p->index = parm->index ? : tcf_police_new_index();
261 h = tcf_police_hash(p->index);
262 write_lock_bh(&police_lock);
263 p->next = tcf_police_ht[h];
264 tcf_police_ht[h] = p;
265 write_unlock_bh(&police_lock);
271 if (ret == ACT_P_CREATED)
276 static int tcf_act_police_cleanup(struct tc_action *a, int bind)
278 struct tcf_police *p = PRIV(a);
281 return tcf_police_release(p, bind);
285 static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
286 struct tcf_result *res)
289 struct tcf_police *p = PRIV(a);
295 p->bstats.bytes += skb->len;
298 #ifdef CONFIG_NET_ESTIMATOR
299 if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
300 p->qstats.overlimits++;
301 spin_unlock(&p->lock);
306 if (skb->len <= p->mtu) {
307 if (p->R_tab == NULL) {
308 spin_unlock(&p->lock);
312 PSCHED_GET_TIME(now);
314 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
317 ptoks = toks + p->ptoks;
318 if (ptoks > (long)L2T_P(p, p->mtu))
319 ptoks = (long)L2T_P(p, p->mtu);
320 ptoks -= L2T_P(p, skb->len);
323 if (toks > (long)p->burst)
325 toks -= L2T(p, skb->len);
327 if ((toks|ptoks) >= 0) {
331 spin_unlock(&p->lock);
336 p->qstats.overlimits++;
337 spin_unlock(&p->lock);
342 tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
344 unsigned char *b = skb->tail;
345 struct tc_police opt;
346 struct tcf_police *p = PRIV(a);
348 opt.index = p->index;
349 opt.action = p->action;
351 opt.burst = p->burst;
352 opt.refcnt = p->refcnt - ref;
353 opt.bindcnt = p->bindcnt - bind;
355 opt.rate = p->R_tab->rate;
357 memset(&opt.rate, 0, sizeof(opt.rate));
359 opt.peakrate = p->P_tab->rate;
361 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
362 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
364 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
365 #ifdef CONFIG_NET_ESTIMATOR
367 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
372 skb_trim(skb, b - skb->data);
376 MODULE_AUTHOR("Alexey Kuznetsov");
377 MODULE_DESCRIPTION("Policing actions");
378 MODULE_LICENSE("GPL");
380 static struct tc_action_ops act_police_ops = {
382 .type = TCA_ID_POLICE,
383 .capab = TCA_CAP_NONE,
384 .owner = THIS_MODULE,
385 .act = tcf_act_police,
386 .dump = tcf_act_police_dump,
387 .cleanup = tcf_act_police_cleanup,
388 .lookup = tcf_act_police_hash_search,
389 .init = tcf_act_police_locate,
390 .walk = tcf_act_police_walker
394 police_init_module(void)
396 return tcf_register_action(&act_police_ops);
400 police_cleanup_module(void)
402 tcf_unregister_action(&act_police_ops);
405 module_init(police_init_module);
406 module_exit(police_cleanup_module);
408 #else /* CONFIG_NET_CLS_ACT */
410 struct tcf_police * tcf_police_locate(struct rtattr *rta, struct rtattr *est)
413 struct tcf_police *p;
414 struct rtattr *tb[TCA_POLICE_MAX];
415 struct tc_police *parm;
417 if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
420 if (tb[TCA_POLICE_TBF-1] == NULL ||
421 RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm))
424 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
426 if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
431 p = kzalloc(sizeof(*p), GFP_KERNEL);
436 spin_lock_init(&p->lock);
437 p->stats_lock = &p->lock;
438 if (parm->rate.rate) {
439 p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
440 if (p->R_tab == NULL)
442 if (parm->peakrate.rate) {
443 p->P_tab = qdisc_get_rtab(&parm->peakrate,
444 tb[TCA_POLICE_PEAKRATE-1]);
445 if (p->P_tab == NULL)
449 if (tb[TCA_POLICE_RESULT-1]) {
450 if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
452 p->result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
454 #ifdef CONFIG_NET_ESTIMATOR
455 if (tb[TCA_POLICE_AVRATE-1]) {
456 if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32))
458 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
461 p->toks = p->burst = parm->burst;
466 p->mtu = 255<<p->R_tab->rate.cell_log;
469 p->ptoks = L2T_P(p, p->mtu);
470 PSCHED_GET_TIME(p->t_c);
471 p->index = parm->index ? : tcf_police_new_index();
472 p->action = parm->action;
473 #ifdef CONFIG_NET_ESTIMATOR
475 gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
477 h = tcf_police_hash(p->index);
478 write_lock_bh(&police_lock);
479 p->next = tcf_police_ht[h];
480 tcf_police_ht[h] = p;
481 write_unlock_bh(&police_lock);
486 qdisc_put_rtab(p->R_tab);
491 int tcf_police(struct sk_buff *skb, struct tcf_police *p)
499 p->bstats.bytes += skb->len;
502 #ifdef CONFIG_NET_ESTIMATOR
503 if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
504 p->qstats.overlimits++;
505 spin_unlock(&p->lock);
510 if (skb->len <= p->mtu) {
511 if (p->R_tab == NULL) {
512 spin_unlock(&p->lock);
516 PSCHED_GET_TIME(now);
518 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
521 ptoks = toks + p->ptoks;
522 if (ptoks > (long)L2T_P(p, p->mtu))
523 ptoks = (long)L2T_P(p, p->mtu);
524 ptoks -= L2T_P(p, skb->len);
527 if (toks > (long)p->burst)
529 toks -= L2T(p, skb->len);
531 if ((toks|ptoks) >= 0) {
535 spin_unlock(&p->lock);
540 p->qstats.overlimits++;
541 spin_unlock(&p->lock);
544 EXPORT_SYMBOL(tcf_police);
546 int tcf_police_dump(struct sk_buff *skb, struct tcf_police *p)
548 unsigned char *b = skb->tail;
549 struct tc_police opt;
551 opt.index = p->index;
552 opt.action = p->action;
554 opt.burst = p->burst;
556 opt.rate = p->R_tab->rate;
558 memset(&opt.rate, 0, sizeof(opt.rate));
560 opt.peakrate = p->P_tab->rate;
562 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
563 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
565 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
566 #ifdef CONFIG_NET_ESTIMATOR
568 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
573 skb_trim(skb, b - skb->data);
577 int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *p)
581 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
582 TCA_XSTATS, p->stats_lock, &d) < 0)
585 if (gnet_stats_copy_basic(&d, &p->bstats) < 0 ||
586 #ifdef CONFIG_NET_ESTIMATOR
587 gnet_stats_copy_rate_est(&d, &p->rate_est) < 0 ||
589 gnet_stats_copy_queue(&d, &p->qstats) < 0)
592 if (gnet_stats_finish_copy(&d) < 0)
601 #endif /* CONFIG_NET_CLS_ACT */