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;
172 police = kzalloc(sizeof(*police), GFP_KERNEL);
176 police->tcf_refcnt = 1;
177 spin_lock_init(&police->tcf_lock);
179 police->tcf_bindcnt = 1;
181 if (parm->rate.rate) {
183 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
187 if (parm->peakrate.rate) {
188 P_tab = qdisc_get_rtab(&parm->peakrate,
189 tb[TCA_POLICE_PEAKRATE]);
195 spin_lock_bh(&police->tcf_lock);
197 err = gen_replace_estimator(&police->tcf_bstats,
198 &police->tcf_rate_est,
199 &police->tcf_lock, est);
202 } else if (tb[TCA_POLICE_AVRATE] &&
203 (ret == ACT_P_CREATED ||
204 !gen_estimator_active(&police->tcf_bstats,
205 &police->tcf_rate_est))) {
210 /* No failure allowed after this point */
211 police->tcfp_mtu = parm->mtu;
212 if (police->tcfp_mtu == 0) {
213 police->tcfp_mtu = ~0;
215 police->tcfp_mtu = 255 << R_tab->rate.cell_log;
218 police->rate_present = true;
219 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
220 qdisc_put_rtab(R_tab);
222 police->rate_present = false;
225 police->peak_present = true;
226 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
227 qdisc_put_rtab(P_tab);
229 police->peak_present = false;
232 if (tb[TCA_POLICE_RESULT])
233 police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
234 police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
235 police->tcfp_toks = police->tcfp_burst;
236 if (police->peak_present) {
237 police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
239 police->tcfp_ptoks = police->tcfp_mtu_ptoks;
241 police->tcf_action = parm->action;
243 if (tb[TCA_POLICE_AVRATE])
244 police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
246 spin_unlock_bh(&police->tcf_lock);
247 if (ret != ACT_P_CREATED)
250 police->tcfp_t_c = ktime_to_ns(ktime_get());
251 police->tcf_index = parm->index ? parm->index :
252 tcf_hash_new_index(&police_idx_gen, &police_hash_info);
253 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
254 spin_lock_bh(&police_hash_info.lock);
255 hlist_add_head(&police->tcf_head, &police_hash_info.htab[h]);
256 spin_unlock_bh(&police_hash_info.lock);
262 spin_unlock_bh(&police->tcf_lock);
264 qdisc_put_rtab(P_tab);
265 qdisc_put_rtab(R_tab);
266 if (ret == ACT_P_CREATED)
271 static int tcf_act_police_cleanup(struct tc_action *a, int bind)
273 struct tcf_police *p = a->priv;
281 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
282 tcf_police_destroy(p);
289 static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
290 struct tcf_result *res)
292 struct tcf_police *police = a->priv;
297 spin_lock(&police->tcf_lock);
299 bstats_update(&police->tcf_bstats, skb);
301 if (police->tcfp_ewma_rate &&
302 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
303 police->tcf_qstats.overlimits++;
304 if (police->tcf_action == TC_ACT_SHOT)
305 police->tcf_qstats.drops++;
306 spin_unlock(&police->tcf_lock);
307 return police->tcf_action;
310 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
311 if (!police->rate_present) {
312 spin_unlock(&police->tcf_lock);
313 return police->tcfp_result;
316 now = ktime_to_ns(ktime_get());
317 toks = min_t(s64, now - police->tcfp_t_c,
319 if (police->peak_present) {
320 ptoks = toks + police->tcfp_ptoks;
321 if (ptoks > police->tcfp_mtu_ptoks)
322 ptoks = police->tcfp_mtu_ptoks;
323 ptoks -= (s64) psched_l2t_ns(&police->peak,
326 toks += police->tcfp_toks;
327 if (toks > police->tcfp_burst)
328 toks = police->tcfp_burst;
329 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
330 if ((toks|ptoks) >= 0) {
331 police->tcfp_t_c = now;
332 police->tcfp_toks = toks;
333 police->tcfp_ptoks = ptoks;
334 spin_unlock(&police->tcf_lock);
335 return police->tcfp_result;
339 police->tcf_qstats.overlimits++;
340 if (police->tcf_action == TC_ACT_SHOT)
341 police->tcf_qstats.drops++;
342 spin_unlock(&police->tcf_lock);
343 return police->tcf_action;
347 tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
349 unsigned char *b = skb_tail_pointer(skb);
350 struct tcf_police *police = a->priv;
351 struct tc_police opt = {
352 .index = police->tcf_index,
353 .action = police->tcf_action,
354 .mtu = police->tcfp_mtu,
355 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
356 .refcnt = police->tcf_refcnt - ref,
357 .bindcnt = police->tcf_bindcnt - bind,
360 if (police->rate_present)
361 psched_ratecfg_getrate(&opt.rate, &police->rate);
362 if (police->peak_present)
363 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
364 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
365 goto nla_put_failure;
366 if (police->tcfp_result &&
367 nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
368 goto nla_put_failure;
369 if (police->tcfp_ewma_rate &&
370 nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
371 goto nla_put_failure;
379 MODULE_AUTHOR("Alexey Kuznetsov");
380 MODULE_DESCRIPTION("Policing actions");
381 MODULE_LICENSE("GPL");
383 static struct tc_action_ops act_police_ops = {
385 .hinfo = &police_hash_info,
386 .type = TCA_ID_POLICE,
387 .capab = TCA_CAP_NONE,
388 .owner = THIS_MODULE,
389 .act = tcf_act_police,
390 .dump = tcf_act_police_dump,
391 .cleanup = tcf_act_police_cleanup,
392 .init = tcf_act_police_locate,
393 .walk = tcf_act_police_walker
397 police_init_module(void)
399 int err = tcf_hashinfo_init(&police_hash_info, POL_TAB_MASK);
402 err = tcf_register_action(&act_police_ops);
404 tcf_hashinfo_destroy(&police_hash_info);
409 police_cleanup_module(void)
411 tcf_hashinfo_destroy(&police_hash_info);
412 tcf_unregister_action(&act_police_ops);
415 module_init(police_init_module);
416 module_exit(police_cleanup_module);