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)->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])
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
);
43 static struct tcf_hashinfo police_hash_info
= {
44 .htab
= tcf_police_ht
,
45 .hmask
= POL_TAB_MASK
,
49 /* old policer structure from before tc actions */
50 struct tc_police_compat
57 struct tc_ratespec rate
;
58 struct tc_ratespec peakrate
;
61 /* Each policer is serialized by its individual spinlock */
63 #ifdef CONFIG_NET_CLS_ACT
64 static int tcf_act_police_walker(struct sk_buff
*skb
, struct netlink_callback
*cb
,
65 int type
, struct tc_action
*a
)
68 int err
= 0, index
= -1, i
= 0, s_i
= 0, n_i
= 0;
71 read_lock(&police_lock
);
75 for (i
= 0; i
< (POL_TAB_MASK
+ 1); i
++) {
76 p
= tcf_police_ht
[tcf_hash(i
, POL_TAB_MASK
)];
78 for (; p
; p
= p
->tcfc_next
) {
84 r
= (struct rtattr
*) skb
->tail
;
85 RTA_PUT(skb
, a
->order
, 0, NULL
);
86 if (type
== RTM_DELACTION
)
87 err
= tcf_action_dump_1(skb
, a
, 0, 1);
89 err
= tcf_action_dump_1(skb
, a
, 0, 0);
92 skb_trim(skb
, (u8
*)r
- skb
->data
);
95 r
->rta_len
= skb
->tail
- (u8
*)r
;
100 read_unlock(&police_lock
);
106 skb_trim(skb
, (u8
*)r
- skb
->data
);
111 void tcf_police_destroy(struct tcf_police
*p
)
113 unsigned int h
= tcf_hash(p
->tcf_index
, POL_TAB_MASK
);
114 struct tcf_common
**p1p
;
116 for (p1p
= &tcf_police_ht
[h
]; *p1p
; p1p
= &(*p1p
)->tcfc_next
) {
117 if (*p1p
== &p
->common
) {
118 write_lock_bh(&police_lock
);
120 write_unlock_bh(&police_lock
);
121 #ifdef CONFIG_NET_ESTIMATOR
122 gen_kill_estimator(&p
->tcf_bstats
,
126 qdisc_put_rtab(p
->tcfp_R_tab
);
128 qdisc_put_rtab(p
->tcfp_P_tab
);
136 #ifdef CONFIG_NET_CLS_ACT
137 static int tcf_act_police_locate(struct rtattr
*rta
, struct rtattr
*est
,
138 struct tc_action
*a
, int ovr
, int bind
)
142 struct rtattr
*tb
[TCA_POLICE_MAX
];
143 struct tc_police
*parm
;
144 struct tcf_police
*police
;
145 struct qdisc_rate_table
*R_tab
= NULL
, *P_tab
= NULL
;
148 if (rta
== NULL
|| rtattr_parse_nested(tb
, TCA_POLICE_MAX
, rta
) < 0)
151 if (tb
[TCA_POLICE_TBF
-1] == NULL
)
153 size
= RTA_PAYLOAD(tb
[TCA_POLICE_TBF
-1]);
154 if (size
!= sizeof(*parm
) && size
!= sizeof(struct tc_police_compat
))
156 parm
= RTA_DATA(tb
[TCA_POLICE_TBF
-1]);
158 if (tb
[TCA_POLICE_RESULT
-1] != NULL
&&
159 RTA_PAYLOAD(tb
[TCA_POLICE_RESULT
-1]) != sizeof(u32
))
161 if (tb
[TCA_POLICE_RESULT
-1] != NULL
&&
162 RTA_PAYLOAD(tb
[TCA_POLICE_RESULT
-1]) != sizeof(u32
))
166 struct tcf_common
*pc
;
168 pc
= tcf_hash_lookup(parm
->index
, &police_hash_info
);
171 police
= to_police(pc
);
173 police
->tcf_bindcnt
+= 1;
174 police
->tcf_refcnt
+= 1;
182 police
= kzalloc(sizeof(*police
), GFP_KERNEL
);
186 police
->tcf_refcnt
= 1;
187 spin_lock_init(&police
->tcf_lock
);
188 police
->tcf_stats_lock
= &police
->tcf_lock
;
190 police
->tcf_bindcnt
= 1;
192 if (parm
->rate
.rate
) {
194 R_tab
= qdisc_get_rtab(&parm
->rate
, tb
[TCA_POLICE_RATE
-1]);
197 if (parm
->peakrate
.rate
) {
198 P_tab
= qdisc_get_rtab(&parm
->peakrate
,
199 tb
[TCA_POLICE_PEAKRATE
-1]);
201 qdisc_put_rtab(R_tab
);
206 /* No failure allowed after this point */
207 spin_lock_bh(&police
->tcf_lock
);
209 qdisc_put_rtab(police
->tcfp_R_tab
);
210 police
->tcfp_R_tab
= R_tab
;
213 qdisc_put_rtab(police
->tcfp_P_tab
);
214 police
->tcfp_P_tab
= P_tab
;
217 if (tb
[TCA_POLICE_RESULT
-1])
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
;
226 if (police
->tcfp_P_tab
)
227 police
->tcfp_ptoks
= L2T_P(police
, police
->tcfp_mtu
);
228 police
->tcf_action
= parm
->action
;
230 #ifdef CONFIG_NET_ESTIMATOR
231 if (tb
[TCA_POLICE_AVRATE
-1])
232 police
->tcfp_ewma_rate
=
233 *(u32
*)RTA_DATA(tb
[TCA_POLICE_AVRATE
-1]);
235 gen_replace_estimator(&police
->tcf_bstats
,
236 &police
->tcf_rate_est
,
237 police
->tcf_stats_lock
, est
);
240 spin_unlock_bh(&police
->tcf_lock
);
241 if (ret
!= ACT_P_CREATED
)
244 PSCHED_GET_TIME(police
->tcfp_t_c
);
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
);
248 write_lock_bh(&police_lock
);
249 police
->tcf_next
= tcf_police_ht
[h
];
250 tcf_police_ht
[h
] = &police
->common
;
251 write_unlock_bh(&police_lock
);
257 if (ret
== ACT_P_CREATED
)
262 static int tcf_act_police_cleanup(struct tc_action
*a
, int bind
)
264 struct tcf_police
*p
= a
->priv
;
267 return tcf_police_release(p
, bind
);
271 static int tcf_act_police(struct sk_buff
*skb
, struct tc_action
*a
,
272 struct tcf_result
*res
)
274 struct tcf_police
*police
= a
->priv
;
279 spin_lock(&police
->tcf_lock
);
281 police
->tcf_bstats
.bytes
+= skb
->len
;
282 police
->tcf_bstats
.packets
++;
284 #ifdef CONFIG_NET_ESTIMATOR
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
;
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
;
299 PSCHED_GET_TIME(now
);
301 toks
= PSCHED_TDIFF_SAFE(now
, police
->tcfp_t_c
,
303 if (police
->tcfp_P_tab
) {
304 ptoks
= toks
+ police
->tcfp_ptoks
;
305 if (ptoks
> (long)L2T_P(police
, police
->tcfp_mtu
))
306 ptoks
= (long)L2T_P(police
, police
->tcfp_mtu
);
307 ptoks
-= L2T_P(police
, skb
->len
);
309 toks
+= police
->tcfp_toks
;
310 if (toks
> (long)police
->tcfp_burst
)
311 toks
= police
->tcfp_burst
;
312 toks
-= L2T(police
, skb
->len
);
313 if ((toks
|ptoks
) >= 0) {
314 police
->tcfp_t_c
= now
;
315 police
->tcfp_toks
= toks
;
316 police
->tcfp_ptoks
= ptoks
;
317 spin_unlock(&police
->tcf_lock
);
318 return police
->tcfp_result
;
322 police
->tcf_qstats
.overlimits
++;
323 spin_unlock(&police
->tcf_lock
);
324 return police
->tcf_action
;
328 tcf_act_police_dump(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
330 unsigned char *b
= skb
->tail
;
331 struct tcf_police
*police
= a
->priv
;
332 struct tc_police opt
;
334 opt
.index
= police
->tcf_index
;
335 opt
.action
= police
->tcf_action
;
336 opt
.mtu
= police
->tcfp_mtu
;
337 opt
.burst
= police
->tcfp_burst
;
338 opt
.refcnt
= police
->tcf_refcnt
- ref
;
339 opt
.bindcnt
= police
->tcf_bindcnt
- bind
;
340 if (police
->tcfp_R_tab
)
341 opt
.rate
= police
->tcfp_R_tab
->rate
;
343 memset(&opt
.rate
, 0, sizeof(opt
.rate
));
344 if (police
->tcfp_P_tab
)
345 opt
.peakrate
= police
->tcfp_P_tab
->rate
;
347 memset(&opt
.peakrate
, 0, sizeof(opt
.peakrate
));
348 RTA_PUT(skb
, TCA_POLICE_TBF
, sizeof(opt
), &opt
);
349 if (police
->tcfp_result
)
350 RTA_PUT(skb
, TCA_POLICE_RESULT
, sizeof(int),
351 &police
->tcfp_result
);
352 #ifdef CONFIG_NET_ESTIMATOR
353 if (police
->tcfp_ewma_rate
)
354 RTA_PUT(skb
, TCA_POLICE_AVRATE
, 4, &police
->tcfp_ewma_rate
);
359 skb_trim(skb
, b
- skb
->data
);
363 MODULE_AUTHOR("Alexey Kuznetsov");
364 MODULE_DESCRIPTION("Policing actions");
365 MODULE_LICENSE("GPL");
367 static struct tc_action_ops act_police_ops
= {
369 .hinfo
= &police_hash_info
,
370 .type
= TCA_ID_POLICE
,
371 .capab
= TCA_CAP_NONE
,
372 .owner
= THIS_MODULE
,
373 .act
= tcf_act_police
,
374 .dump
= tcf_act_police_dump
,
375 .cleanup
= tcf_act_police_cleanup
,
376 .lookup
= tcf_hash_search
,
377 .init
= tcf_act_police_locate
,
378 .walk
= tcf_act_police_walker
382 police_init_module(void)
384 return tcf_register_action(&act_police_ops
);
388 police_cleanup_module(void)
390 tcf_unregister_action(&act_police_ops
);
393 module_init(police_init_module
);
394 module_exit(police_cleanup_module
);
396 #else /* CONFIG_NET_CLS_ACT */
398 static struct tcf_common
*tcf_police_lookup(u32 index
)
400 struct tcf_hashinfo
*hinfo
= &police_hash_info
;
401 struct tcf_common
*p
;
403 read_lock(hinfo
->lock
);
404 for (p
= hinfo
->htab
[tcf_hash(index
, hinfo
->hmask
)]; p
;
406 if (p
->tcfc_index
== index
)
409 read_unlock(hinfo
->lock
);
414 static u32
tcf_police_new_index(void)
416 u32
*idx_gen
= &police_idx_gen
;
422 } while (tcf_police_lookup(val
));
424 return (*idx_gen
= val
);
427 struct tcf_police
*tcf_police_locate(struct rtattr
*rta
, struct rtattr
*est
)
430 struct tcf_police
*police
;
431 struct rtattr
*tb
[TCA_POLICE_MAX
];
432 struct tc_police
*parm
;
435 if (rtattr_parse_nested(tb
, TCA_POLICE_MAX
, rta
) < 0)
438 if (tb
[TCA_POLICE_TBF
-1] == NULL
)
440 size
= RTA_PAYLOAD(tb
[TCA_POLICE_TBF
-1]);
441 if (size
!= sizeof(*parm
) && size
!= sizeof(struct tc_police_compat
))
444 parm
= RTA_DATA(tb
[TCA_POLICE_TBF
-1]);
447 struct tcf_common
*pc
;
449 pc
= tcf_police_lookup(parm
->index
);
451 police
= to_police(pc
);
452 police
->tcf_refcnt
++;
456 police
= kzalloc(sizeof(*police
), GFP_KERNEL
);
457 if (unlikely(!police
))
460 police
->tcf_refcnt
= 1;
461 spin_lock_init(&police
->tcf_lock
);
462 police
->tcf_stats_lock
= &police
->tcf_lock
;
463 if (parm
->rate
.rate
) {
465 qdisc_get_rtab(&parm
->rate
, tb
[TCA_POLICE_RATE
-1]);
466 if (police
->tcfp_R_tab
== NULL
)
468 if (parm
->peakrate
.rate
) {
470 qdisc_get_rtab(&parm
->peakrate
,
471 tb
[TCA_POLICE_PEAKRATE
-1]);
472 if (police
->tcfp_P_tab
== NULL
)
476 if (tb
[TCA_POLICE_RESULT
-1]) {
477 if (RTA_PAYLOAD(tb
[TCA_POLICE_RESULT
-1]) != sizeof(u32
))
479 police
->tcfp_result
= *(u32
*)RTA_DATA(tb
[TCA_POLICE_RESULT
-1]);
481 #ifdef CONFIG_NET_ESTIMATOR
482 if (tb
[TCA_POLICE_AVRATE
-1]) {
483 if (RTA_PAYLOAD(tb
[TCA_POLICE_AVRATE
-1]) != sizeof(u32
))
485 police
->tcfp_ewma_rate
=
486 *(u32
*)RTA_DATA(tb
[TCA_POLICE_AVRATE
-1]);
489 police
->tcfp_toks
= police
->tcfp_burst
= parm
->burst
;
490 police
->tcfp_mtu
= parm
->mtu
;
491 if (police
->tcfp_mtu
== 0) {
492 police
->tcfp_mtu
= ~0;
493 if (police
->tcfp_R_tab
)
494 police
->tcfp_mtu
= 255<<police
->tcfp_R_tab
->rate
.cell_log
;
496 if (police
->tcfp_P_tab
)
497 police
->tcfp_ptoks
= L2T_P(police
, police
->tcfp_mtu
);
498 PSCHED_GET_TIME(police
->tcfp_t_c
);
499 police
->tcf_index
= parm
->index
? parm
->index
:
500 tcf_police_new_index();
501 police
->tcf_action
= parm
->action
;
502 #ifdef CONFIG_NET_ESTIMATOR
504 gen_new_estimator(&police
->tcf_bstats
, &police
->tcf_rate_est
,
505 police
->tcf_stats_lock
, est
);
507 h
= tcf_hash(police
->tcf_index
, POL_TAB_MASK
);
508 write_lock_bh(&police_lock
);
509 police
->tcf_next
= tcf_police_ht
[h
];
510 tcf_police_ht
[h
] = &police
->common
;
511 write_unlock_bh(&police_lock
);
515 if (police
->tcfp_R_tab
)
516 qdisc_put_rtab(police
->tcfp_R_tab
);
521 int tcf_police(struct sk_buff
*skb
, struct tcf_police
*police
)
527 spin_lock(&police
->tcf_lock
);
529 police
->tcf_bstats
.bytes
+= skb
->len
;
530 police
->tcf_bstats
.packets
++;
532 #ifdef CONFIG_NET_ESTIMATOR
533 if (police
->tcfp_ewma_rate
&&
534 police
->tcf_rate_est
.bps
>= police
->tcfp_ewma_rate
) {
535 police
->tcf_qstats
.overlimits
++;
536 spin_unlock(&police
->tcf_lock
);
537 return police
->tcf_action
;
540 if (skb
->len
<= police
->tcfp_mtu
) {
541 if (police
->tcfp_R_tab
== NULL
) {
542 spin_unlock(&police
->tcf_lock
);
543 return police
->tcfp_result
;
546 PSCHED_GET_TIME(now
);
547 toks
= PSCHED_TDIFF_SAFE(now
, police
->tcfp_t_c
,
549 if (police
->tcfp_P_tab
) {
550 ptoks
= toks
+ police
->tcfp_ptoks
;
551 if (ptoks
> (long)L2T_P(police
, police
->tcfp_mtu
))
552 ptoks
= (long)L2T_P(police
, police
->tcfp_mtu
);
553 ptoks
-= L2T_P(police
, skb
->len
);
555 toks
+= police
->tcfp_toks
;
556 if (toks
> (long)police
->tcfp_burst
)
557 toks
= police
->tcfp_burst
;
558 toks
-= L2T(police
, skb
->len
);
559 if ((toks
|ptoks
) >= 0) {
560 police
->tcfp_t_c
= now
;
561 police
->tcfp_toks
= toks
;
562 police
->tcfp_ptoks
= ptoks
;
563 spin_unlock(&police
->tcf_lock
);
564 return police
->tcfp_result
;
568 police
->tcf_qstats
.overlimits
++;
569 spin_unlock(&police
->tcf_lock
);
570 return police
->tcf_action
;
572 EXPORT_SYMBOL(tcf_police
);
574 int tcf_police_dump(struct sk_buff
*skb
, struct tcf_police
*police
)
576 unsigned char *b
= skb
->tail
;
577 struct tc_police opt
;
579 opt
.index
= police
->tcf_index
;
580 opt
.action
= police
->tcf_action
;
581 opt
.mtu
= police
->tcfp_mtu
;
582 opt
.burst
= police
->tcfp_burst
;
583 if (police
->tcfp_R_tab
)
584 opt
.rate
= police
->tcfp_R_tab
->rate
;
586 memset(&opt
.rate
, 0, sizeof(opt
.rate
));
587 if (police
->tcfp_P_tab
)
588 opt
.peakrate
= police
->tcfp_P_tab
->rate
;
590 memset(&opt
.peakrate
, 0, sizeof(opt
.peakrate
));
591 RTA_PUT(skb
, TCA_POLICE_TBF
, sizeof(opt
), &opt
);
592 if (police
->tcfp_result
)
593 RTA_PUT(skb
, TCA_POLICE_RESULT
, sizeof(int),
594 &police
->tcfp_result
);
595 #ifdef CONFIG_NET_ESTIMATOR
596 if (police
->tcfp_ewma_rate
)
597 RTA_PUT(skb
, TCA_POLICE_AVRATE
, 4, &police
->tcfp_ewma_rate
);
602 skb_trim(skb
, b
- skb
->data
);
606 int tcf_police_dump_stats(struct sk_buff
*skb
, struct tcf_police
*police
)
610 if (gnet_stats_start_copy_compat(skb
, TCA_STATS2
, TCA_STATS
,
611 TCA_XSTATS
, police
->tcf_stats_lock
,
615 if (gnet_stats_copy_basic(&d
, &police
->tcf_bstats
) < 0 ||
616 #ifdef CONFIG_NET_ESTIMATOR
617 gnet_stats_copy_rate_est(&d
, &police
->tcf_rate_est
) < 0 ||
619 gnet_stats_copy_queue(&d
, &police
->tcf_qstats
) < 0)
622 if (gnet_stats_finish_copy(&d
) < 0)
631 #endif /* CONFIG_NET_CLS_ACT */