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 <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/skbuff.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/init.h>
21 #include <net/act_api.h>
22 #include <net/netlink.h>
24 #define L2T(p,L) qdisc_l2t((p)->tcfp_R_tab, L)
25 #define L2T_P(p,L) qdisc_l2t((p)->tcfp_P_tab, L)
27 #define POL_TAB_MASK 15
28 static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
29 static u32 police_idx_gen;
30 static DEFINE_RWLOCK(police_lock);
32 static struct tcf_hashinfo police_hash_info = {
33 .htab = tcf_police_ht,
34 .hmask = POL_TAB_MASK,
38 /* old policer structure from before tc actions */
39 struct tc_police_compat
46 struct tc_ratespec rate;
47 struct tc_ratespec peakrate;
50 /* Each policer is serialized by its individual spinlock */
52 static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
53 int type, struct tc_action *a)
56 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
59 read_lock_bh(&police_lock);
63 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
64 p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
66 for (; p; p = p->tcfc_next) {
72 r = (struct rtattr *)skb_tail_pointer(skb);
73 RTA_PUT(skb, a->order, 0, NULL);
74 if (type == RTM_DELACTION)
75 err = tcf_action_dump_1(skb, a, 0, 1);
77 err = tcf_action_dump_1(skb, a, 0, 0);
83 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
88 read_unlock_bh(&police_lock);
98 static void tcf_police_destroy(struct tcf_police *p)
100 unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
101 struct tcf_common **p1p;
103 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
104 if (*p1p == &p->common) {
105 write_lock_bh(&police_lock);
107 write_unlock_bh(&police_lock);
108 gen_kill_estimator(&p->tcf_bstats,
111 qdisc_put_rtab(p->tcfp_R_tab);
113 qdisc_put_rtab(p->tcfp_P_tab);
121 static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
122 struct tc_action *a, int ovr, int bind)
126 struct rtattr *tb[TCA_POLICE_MAX];
127 struct tc_police *parm;
128 struct tcf_police *police;
129 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
132 if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
135 if (tb[TCA_POLICE_TBF-1] == NULL)
137 size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
138 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
140 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
142 if (tb[TCA_POLICE_RESULT-1] != NULL &&
143 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
145 if (tb[TCA_POLICE_RESULT-1] != NULL &&
146 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
150 struct tcf_common *pc;
152 pc = tcf_hash_lookup(parm->index, &police_hash_info);
155 police = to_police(pc);
157 police->tcf_bindcnt += 1;
158 police->tcf_refcnt += 1;
166 police = kzalloc(sizeof(*police), GFP_KERNEL);
170 police->tcf_refcnt = 1;
171 spin_lock_init(&police->tcf_lock);
173 police->tcf_bindcnt = 1;
175 if (parm->rate.rate) {
177 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
180 if (parm->peakrate.rate) {
181 P_tab = qdisc_get_rtab(&parm->peakrate,
182 tb[TCA_POLICE_PEAKRATE-1]);
184 qdisc_put_rtab(R_tab);
189 /* No failure allowed after this point */
190 spin_lock_bh(&police->tcf_lock);
192 qdisc_put_rtab(police->tcfp_R_tab);
193 police->tcfp_R_tab = R_tab;
196 qdisc_put_rtab(police->tcfp_P_tab);
197 police->tcfp_P_tab = P_tab;
200 if (tb[TCA_POLICE_RESULT-1])
201 police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
202 police->tcfp_toks = police->tcfp_burst = parm->burst;
203 police->tcfp_mtu = parm->mtu;
204 if (police->tcfp_mtu == 0) {
205 police->tcfp_mtu = ~0;
206 if (police->tcfp_R_tab)
207 police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
209 if (police->tcfp_P_tab)
210 police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
211 police->tcf_action = parm->action;
213 if (tb[TCA_POLICE_AVRATE-1])
214 police->tcfp_ewma_rate =
215 *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
217 gen_replace_estimator(&police->tcf_bstats,
218 &police->tcf_rate_est,
219 &police->tcf_lock, est);
221 spin_unlock_bh(&police->tcf_lock);
222 if (ret != ACT_P_CREATED)
225 police->tcfp_t_c = psched_get_time();
226 police->tcf_index = parm->index ? parm->index :
227 tcf_hash_new_index(&police_idx_gen, &police_hash_info);
228 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
229 write_lock_bh(&police_lock);
230 police->tcf_next = tcf_police_ht[h];
231 tcf_police_ht[h] = &police->common;
232 write_unlock_bh(&police_lock);
238 if (ret == ACT_P_CREATED)
243 static int tcf_act_police_cleanup(struct tc_action *a, int bind)
245 struct tcf_police *p = a->priv;
253 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
254 tcf_police_destroy(p);
261 static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
262 struct tcf_result *res)
264 struct tcf_police *police = a->priv;
269 spin_lock(&police->tcf_lock);
271 police->tcf_bstats.bytes += skb->len;
272 police->tcf_bstats.packets++;
274 if (police->tcfp_ewma_rate &&
275 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
276 police->tcf_qstats.overlimits++;
277 spin_unlock(&police->tcf_lock);
278 return police->tcf_action;
281 if (skb->len <= police->tcfp_mtu) {
282 if (police->tcfp_R_tab == NULL) {
283 spin_unlock(&police->tcf_lock);
284 return police->tcfp_result;
287 now = psched_get_time();
288 toks = psched_tdiff_bounded(now, police->tcfp_t_c,
290 if (police->tcfp_P_tab) {
291 ptoks = toks + police->tcfp_ptoks;
292 if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
293 ptoks = (long)L2T_P(police, police->tcfp_mtu);
294 ptoks -= L2T_P(police, skb->len);
296 toks += police->tcfp_toks;
297 if (toks > (long)police->tcfp_burst)
298 toks = police->tcfp_burst;
299 toks -= L2T(police, skb->len);
300 if ((toks|ptoks) >= 0) {
301 police->tcfp_t_c = now;
302 police->tcfp_toks = toks;
303 police->tcfp_ptoks = ptoks;
304 spin_unlock(&police->tcf_lock);
305 return police->tcfp_result;
309 police->tcf_qstats.overlimits++;
310 spin_unlock(&police->tcf_lock);
311 return police->tcf_action;
315 tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
317 unsigned char *b = skb_tail_pointer(skb);
318 struct tcf_police *police = a->priv;
319 struct tc_police opt;
321 opt.index = police->tcf_index;
322 opt.action = police->tcf_action;
323 opt.mtu = police->tcfp_mtu;
324 opt.burst = police->tcfp_burst;
325 opt.refcnt = police->tcf_refcnt - ref;
326 opt.bindcnt = police->tcf_bindcnt - bind;
327 if (police->tcfp_R_tab)
328 opt.rate = police->tcfp_R_tab->rate;
330 memset(&opt.rate, 0, sizeof(opt.rate));
331 if (police->tcfp_P_tab)
332 opt.peakrate = police->tcfp_P_tab->rate;
334 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
335 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
336 if (police->tcfp_result)
337 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
338 &police->tcfp_result);
339 if (police->tcfp_ewma_rate)
340 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
348 MODULE_AUTHOR("Alexey Kuznetsov");
349 MODULE_DESCRIPTION("Policing actions");
350 MODULE_LICENSE("GPL");
352 static struct tc_action_ops act_police_ops = {
354 .hinfo = &police_hash_info,
355 .type = TCA_ID_POLICE,
356 .capab = TCA_CAP_NONE,
357 .owner = THIS_MODULE,
358 .act = tcf_act_police,
359 .dump = tcf_act_police_dump,
360 .cleanup = tcf_act_police_cleanup,
361 .lookup = tcf_hash_search,
362 .init = tcf_act_police_locate,
363 .walk = tcf_act_police_walker
367 police_init_module(void)
369 return tcf_register_action(&act_police_ops);
373 police_cleanup_module(void)
375 tcf_unregister_action(&act_police_ops);
378 module_init(police_init_module);
379 module_exit(police_cleanup_module);