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 <linux/slab.h>
22 #include <net/act_api.h>
23 #include <net/netlink.h>
26 struct tcf_common common;
35 struct psched_ratecfg rate;
37 struct psched_ratecfg peak;
40 #define to_police(pc) \
41 container_of(pc, struct tcf_police, common)
43 #define POL_TAB_MASK 15
44 static u32 police_idx_gen;
45 static struct tcf_hashinfo police_hash_info;
47 /* old policer structure from before tc actions */
48 struct tc_police_compat {
54 struct tc_ratespec rate;
55 struct tc_ratespec peakrate;
58 /* Each policer is serialized by its individual spinlock */
60 static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
61 int type, struct tc_action *a)
63 struct hlist_head *head;
65 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
68 spin_lock_bh(&police_hash_info.lock);
72 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
73 head = &police_hash_info.htab[tcf_hash(i, POL_TAB_MASK)];
75 hlist_for_each_entry_rcu(p, head, tcfc_head) {
81 nest = nla_nest_start(skb, a->order);
84 if (type == RTM_DELACTION)
85 err = tcf_action_dump_1(skb, a, 0, 1);
87 err = tcf_action_dump_1(skb, a, 0, 0);
90 nla_nest_cancel(skb, nest);
93 nla_nest_end(skb, nest);
98 spin_unlock_bh(&police_hash_info.lock);
104 nla_nest_cancel(skb, nest);
108 static void tcf_police_destroy(struct tcf_police *p)
110 spin_lock_bh(&police_hash_info.lock);
111 hlist_del(&p->tcf_head);
112 spin_unlock_bh(&police_hash_info.lock);
113 gen_kill_estimator(&p->tcf_bstats,
116 * gen_estimator est_timer() might access p->tcf_lock
117 * or bstats, wait a RCU grace period before freeing p
119 kfree_rcu(p, tcf_rcu);
122 static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
123 [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
124 [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
125 [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
126 [TCA_POLICE_RESULT] = { .type = NLA_U32 },
129 static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
130 struct nlattr *est, struct tc_action *a,
135 struct nlattr *tb[TCA_POLICE_MAX + 1];
136 struct tc_police *parm;
137 struct tcf_police *police;
138 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
144 err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
148 if (tb[TCA_POLICE_TBF] == NULL)
150 size = nla_len(tb[TCA_POLICE_TBF]);
151 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
153 parm = nla_data(tb[TCA_POLICE_TBF]);
156 struct tcf_common *pc;
158 pc = tcf_hash_lookup(parm->index, &police_hash_info);
161 police = to_police(pc);
163 police->tcf_bindcnt += 1;
164 police->tcf_refcnt += 1;
174 police = kzalloc(sizeof(*police), GFP_KERNEL);
178 police->tcf_refcnt = 1;
179 spin_lock_init(&police->tcf_lock);
181 police->tcf_bindcnt = 1;
183 if (parm->rate.rate) {
185 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
189 if (parm->peakrate.rate) {
190 P_tab = qdisc_get_rtab(&parm->peakrate,
191 tb[TCA_POLICE_PEAKRATE]);
197 spin_lock_bh(&police->tcf_lock);
199 err = gen_replace_estimator(&police->tcf_bstats,
200 &police->tcf_rate_est,
201 &police->tcf_lock, est);
204 } else if (tb[TCA_POLICE_AVRATE] &&
205 (ret == ACT_P_CREATED ||
206 !gen_estimator_active(&police->tcf_bstats,
207 &police->tcf_rate_est))) {
212 /* No failure allowed after this point */
213 police->tcfp_mtu = parm->mtu;
214 if (police->tcfp_mtu == 0) {
215 police->tcfp_mtu = ~0;
217 police->tcfp_mtu = 255 << R_tab->rate.cell_log;
220 police->rate_present = true;
221 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
222 qdisc_put_rtab(R_tab);
224 police->rate_present = false;
227 police->peak_present = true;
228 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
229 qdisc_put_rtab(P_tab);
231 police->peak_present = false;
234 if (tb[TCA_POLICE_RESULT])
235 police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
236 police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
237 police->tcfp_toks = police->tcfp_burst;
238 if (police->peak_present) {
239 police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
241 police->tcfp_ptoks = police->tcfp_mtu_ptoks;
243 police->tcf_action = parm->action;
245 if (tb[TCA_POLICE_AVRATE])
246 police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
248 spin_unlock_bh(&police->tcf_lock);
249 if (ret != ACT_P_CREATED)
252 police->tcfp_t_c = ktime_to_ns(ktime_get());
253 police->tcf_index = parm->index ? parm->index :
254 tcf_hash_new_index(&police_idx_gen, &police_hash_info);
255 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
256 spin_lock_bh(&police_hash_info.lock);
257 hlist_add_head(&police->tcf_head, &police_hash_info.htab[h]);
258 spin_unlock_bh(&police_hash_info.lock);
264 spin_unlock_bh(&police->tcf_lock);
266 qdisc_put_rtab(P_tab);
267 qdisc_put_rtab(R_tab);
268 if (ret == ACT_P_CREATED)
273 static int tcf_act_police_cleanup(struct tc_action *a, int bind)
275 struct tcf_police *p = a->priv;
283 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
284 tcf_police_destroy(p);
291 static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
292 struct tcf_result *res)
294 struct tcf_police *police = a->priv;
299 spin_lock(&police->tcf_lock);
301 bstats_update(&police->tcf_bstats, skb);
303 if (police->tcfp_ewma_rate &&
304 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
305 police->tcf_qstats.overlimits++;
306 if (police->tcf_action == TC_ACT_SHOT)
307 police->tcf_qstats.drops++;
308 spin_unlock(&police->tcf_lock);
309 return police->tcf_action;
312 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
313 if (!police->rate_present) {
314 spin_unlock(&police->tcf_lock);
315 return police->tcfp_result;
318 now = ktime_to_ns(ktime_get());
319 toks = min_t(s64, now - police->tcfp_t_c,
321 if (police->peak_present) {
322 ptoks = toks + police->tcfp_ptoks;
323 if (ptoks > police->tcfp_mtu_ptoks)
324 ptoks = police->tcfp_mtu_ptoks;
325 ptoks -= (s64) psched_l2t_ns(&police->peak,
328 toks += police->tcfp_toks;
329 if (toks > police->tcfp_burst)
330 toks = police->tcfp_burst;
331 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
332 if ((toks|ptoks) >= 0) {
333 police->tcfp_t_c = now;
334 police->tcfp_toks = toks;
335 police->tcfp_ptoks = ptoks;
336 spin_unlock(&police->tcf_lock);
337 return police->tcfp_result;
341 police->tcf_qstats.overlimits++;
342 if (police->tcf_action == TC_ACT_SHOT)
343 police->tcf_qstats.drops++;
344 spin_unlock(&police->tcf_lock);
345 return police->tcf_action;
349 tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
351 unsigned char *b = skb_tail_pointer(skb);
352 struct tcf_police *police = a->priv;
353 struct tc_police opt = {
354 .index = police->tcf_index,
355 .action = police->tcf_action,
356 .mtu = police->tcfp_mtu,
357 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
358 .refcnt = police->tcf_refcnt - ref,
359 .bindcnt = police->tcf_bindcnt - bind,
362 if (police->rate_present)
363 psched_ratecfg_getrate(&opt.rate, &police->rate);
364 if (police->peak_present)
365 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
366 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
367 goto nla_put_failure;
368 if (police->tcfp_result &&
369 nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
370 goto nla_put_failure;
371 if (police->tcfp_ewma_rate &&
372 nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
373 goto nla_put_failure;
381 MODULE_AUTHOR("Alexey Kuznetsov");
382 MODULE_DESCRIPTION("Policing actions");
383 MODULE_LICENSE("GPL");
385 static struct tc_action_ops act_police_ops = {
387 .hinfo = &police_hash_info,
388 .type = TCA_ID_POLICE,
389 .capab = TCA_CAP_NONE,
390 .owner = THIS_MODULE,
391 .act = tcf_act_police,
392 .dump = tcf_act_police_dump,
393 .cleanup = tcf_act_police_cleanup,
394 .init = tcf_act_police_locate,
395 .walk = tcf_act_police_walker
399 police_init_module(void)
401 int err = tcf_hashinfo_init(&police_hash_info, POL_TAB_MASK);
404 err = tcf_register_action(&act_police_ops);
406 tcf_hashinfo_destroy(&police_hash_info);
411 police_cleanup_module(void)
413 tcf_hashinfo_destroy(&police_hash_info);
414 tcf_unregister_action(&act_police_ops);
417 module_init(police_init_module);
418 module_exit(police_cleanup_module);