]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/sched/act_police.c
fq_codel: return non zero qlen in class dumps
[thirdparty/kernel/stable.git] / net / sched / act_police.c
CommitLineData
1da177e4 1/*
0c6965dd 2 * net/sched/act_police.c Input police filter
1da177e4
LT
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim (action changes)
11 */
12
1da177e4
LT
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
1da177e4 16#include <linux/string.h>
1da177e4 17#include <linux/errno.h>
1da177e4 18#include <linux/skbuff.h>
1da177e4
LT
19#include <linux/rtnetlink.h>
20#include <linux/init.h>
5a0e3ad6 21#include <linux/slab.h>
1da177e4 22#include <net/act_api.h>
dc5fc579 23#include <net/netlink.h>
1da177e4 24
0e243218
JP
25struct tcf_police {
26 struct tcf_common common;
27 int tcfp_result;
28 u32 tcfp_ewma_rate;
c6d14ff1 29 s64 tcfp_burst;
0e243218 30 u32 tcfp_mtu;
c6d14ff1
JP
31 s64 tcfp_toks;
32 s64 tcfp_ptoks;
33 s64 tcfp_mtu_ptoks;
34 s64 tcfp_t_c;
35 struct psched_ratecfg rate;
36 bool rate_present;
37 struct psched_ratecfg peak;
38 bool peak_present;
0e243218
JP
39};
40#define to_police(pc) \
41 container_of(pc, struct tcf_police, common)
42
e9ce1cd3 43#define POL_TAB_MASK 15
1da177e4 44
1e9b3d53 45/* old policer structure from before tc actions */
cc7ec456 46struct tc_police_compat {
1e9b3d53
PM
47 u32 index;
48 int action;
49 u32 limit;
50 u32 burst;
51 u32 mtu;
52 struct tc_ratespec rate;
53 struct tc_ratespec peakrate;
54};
55
e9ce1cd3 56/* Each policer is serialized by its individual spinlock */
1da177e4 57
ddf97ccd
WC
58static int police_net_id;
59
60static int tcf_act_police_walker(struct net *net, struct sk_buff *skb,
61 struct netlink_callback *cb, int type,
62 struct tc_action *a)
1da177e4 63{
ddf97ccd
WC
64 struct tc_action_net *tn = net_generic(net, police_net_id);
65 struct tcf_hashinfo *hinfo = tn->hinfo;
89819dc0 66 struct hlist_head *head;
e9ce1cd3 67 struct tcf_common *p;
1da177e4 68 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
4b3550ef 69 struct nlattr *nest;
1da177e4 70
c779f7af 71 spin_lock_bh(&hinfo->lock);
1da177e4
LT
72
73 s_i = cb->args[0];
74
e9ce1cd3 75 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
c779f7af 76 head = &hinfo->htab[tcf_hash(i, POL_TAB_MASK)];
1da177e4 77
89819dc0 78 hlist_for_each_entry_rcu(p, head, tcfc_head) {
1da177e4
LT
79 index++;
80 if (index < s_i)
81 continue;
82 a->priv = p;
83 a->order = index;
4b3550ef
PM
84 nest = nla_nest_start(skb, a->order);
85 if (nest == NULL)
86 goto nla_put_failure;
1da177e4
LT
87 if (type == RTM_DELACTION)
88 err = tcf_action_dump_1(skb, a, 0, 1);
89 else
90 err = tcf_action_dump_1(skb, a, 0, 0);
91 if (err < 0) {
92 index--;
4b3550ef 93 nla_nest_cancel(skb, nest);
1da177e4
LT
94 goto done;
95 }
4b3550ef 96 nla_nest_end(skb, nest);
1da177e4
LT
97 n_i++;
98 }
99 }
100done:
c779f7af 101 spin_unlock_bh(&hinfo->lock);
1da177e4
LT
102 if (n_i)
103 cb->args[0] += n_i;
104 return n_i;
105
7ba699c6 106nla_put_failure:
4b3550ef 107 nla_nest_cancel(skb, nest);
1da177e4
LT
108 goto done;
109}
1da177e4 110
53b2bf3f
PM
111static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
112 [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
113 [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
114 [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
115 [TCA_POLICE_RESULT] = { .type = NLA_U32 },
116};
117
c1b52739
BL
118static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
119 struct nlattr *est, struct tc_action *a,
120 int ovr, int bind)
1da177e4 121{
cc7ec456 122 unsigned int h;
1da177e4 123 int ret = 0, err;
7ba699c6 124 struct nlattr *tb[TCA_POLICE_MAX + 1];
1da177e4 125 struct tc_police *parm;
e9ce1cd3 126 struct tcf_police *police;
1da177e4 127 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
ddf97ccd
WC
128 struct tc_action_net *tn = net_generic(net, police_net_id);
129 struct tcf_hashinfo *hinfo = tn->hinfo;
1e9b3d53 130 int size;
1da177e4 131
cee63723 132 if (nla == NULL)
1da177e4
LT
133 return -EINVAL;
134
53b2bf3f 135 err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
cee63723
PM
136 if (err < 0)
137 return err;
138
7ba699c6 139 if (tb[TCA_POLICE_TBF] == NULL)
1e9b3d53 140 return -EINVAL;
7ba699c6 141 size = nla_len(tb[TCA_POLICE_TBF]);
1e9b3d53 142 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1da177e4 143 return -EINVAL;
7ba699c6 144 parm = nla_data(tb[TCA_POLICE_TBF]);
1da177e4 145
e9ce1cd3 146 if (parm->index) {
ddf97ccd 147 if (tcf_hash_search(tn, a, parm->index)) {
6e6a50c2 148 police = to_police(a->priv);
e9ce1cd3
DM
149 if (bind) {
150 police->tcf_bindcnt += 1;
151 police->tcf_refcnt += 1;
1a29321e 152 return 0;
e9ce1cd3
DM
153 }
154 if (ovr)
155 goto override;
1a29321e
JHS
156 /* not replacing */
157 return -EEXIST;
1da177e4 158 }
1da177e4
LT
159 }
160
e9ce1cd3
DM
161 police = kzalloc(sizeof(*police), GFP_KERNEL);
162 if (police == NULL)
1da177e4 163 return -ENOMEM;
1da177e4 164 ret = ACT_P_CREATED;
e9ce1cd3
DM
165 police->tcf_refcnt = 1;
166 spin_lock_init(&police->tcf_lock);
1da177e4 167 if (bind)
e9ce1cd3 168 police->tcf_bindcnt = 1;
1da177e4
LT
169override:
170 if (parm->rate.rate) {
171 err = -ENOMEM;
7ba699c6 172 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
1da177e4
LT
173 if (R_tab == NULL)
174 goto failure;
c1b56878 175
1da177e4
LT
176 if (parm->peakrate.rate) {
177 P_tab = qdisc_get_rtab(&parm->peakrate,
7ba699c6 178 tb[TCA_POLICE_PEAKRATE]);
71bcb09a 179 if (P_tab == NULL)
1da177e4 180 goto failure;
1da177e4
LT
181 }
182 }
71bcb09a 183
e9ce1cd3 184 spin_lock_bh(&police->tcf_lock);
71bcb09a 185 if (est) {
22e0f8b9 186 err = gen_replace_estimator(&police->tcf_bstats, NULL,
71bcb09a
SH
187 &police->tcf_rate_est,
188 &police->tcf_lock, est);
189 if (err)
190 goto failure_unlock;
a883bf56
JP
191 } else if (tb[TCA_POLICE_AVRATE] &&
192 (ret == ACT_P_CREATED ||
193 !gen_estimator_active(&police->tcf_bstats,
194 &police->tcf_rate_est))) {
195 err = -EINVAL;
196 goto failure_unlock;
71bcb09a
SH
197 }
198
199 /* No failure allowed after this point */
c6d14ff1
JP
200 police->tcfp_mtu = parm->mtu;
201 if (police->tcfp_mtu == 0) {
202 police->tcfp_mtu = ~0;
203 if (R_tab)
204 police->tcfp_mtu = 255 << R_tab->rate.cell_log;
205 }
206 if (R_tab) {
207 police->rate_present = true;
3e1e3aae 208 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
c6d14ff1
JP
209 qdisc_put_rtab(R_tab);
210 } else {
211 police->rate_present = false;
1da177e4 212 }
c6d14ff1
JP
213 if (P_tab) {
214 police->peak_present = true;
3e1e3aae 215 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
c6d14ff1
JP
216 qdisc_put_rtab(P_tab);
217 } else {
218 police->peak_present = false;
1da177e4
LT
219 }
220
7ba699c6 221 if (tb[TCA_POLICE_RESULT])
1587bac4 222 police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
c6d14ff1
JP
223 police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
224 police->tcfp_toks = police->tcfp_burst;
225 if (police->peak_present) {
226 police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
227 police->tcfp_mtu);
228 police->tcfp_ptoks = police->tcfp_mtu_ptoks;
1da177e4 229 }
e9ce1cd3 230 police->tcf_action = parm->action;
1da177e4 231
7ba699c6 232 if (tb[TCA_POLICE_AVRATE])
1587bac4 233 police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1da177e4 234
e9ce1cd3 235 spin_unlock_bh(&police->tcf_lock);
1da177e4
LT
236 if (ret != ACT_P_CREATED)
237 return ret;
238
d2de875c 239 police->tcfp_t_c = ktime_get_ns();
e9ce1cd3 240 police->tcf_index = parm->index ? parm->index :
ddf97ccd 241 tcf_hash_new_index(tn);
3d3ed181
JHS
242 police->tcf_tm.install = jiffies;
243 police->tcf_tm.lastuse = jiffies;
e9ce1cd3 244 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
c779f7af
WC
245 spin_lock_bh(&hinfo->lock);
246 hlist_add_head(&police->tcf_head, &hinfo->htab[h]);
247 spin_unlock_bh(&hinfo->lock);
1da177e4 248
e9ce1cd3 249 a->priv = police;
1da177e4
LT
250 return ret;
251
71bcb09a
SH
252failure_unlock:
253 spin_unlock_bh(&police->tcf_lock);
1da177e4 254failure:
3b69a4c9
YY
255 qdisc_put_rtab(P_tab);
256 qdisc_put_rtab(R_tab);
1da177e4 257 if (ret == ACT_P_CREATED)
e9ce1cd3 258 kfree(police);
1da177e4
LT
259 return err;
260}
261
dc7f9f6e 262static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
10297b99 263 struct tcf_result *res)
1da177e4 264{
e9ce1cd3 265 struct tcf_police *police = a->priv;
c6d14ff1
JP
266 s64 now;
267 s64 toks;
268 s64 ptoks = 0;
1da177e4 269
e9ce1cd3 270 spin_lock(&police->tcf_lock);
1da177e4 271
bfe0d029 272 bstats_update(&police->tcf_bstats, skb);
3d3ed181 273 tcf_lastuse_update(&police->tcf_tm);
1da177e4 274
e9ce1cd3
DM
275 if (police->tcfp_ewma_rate &&
276 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
277 police->tcf_qstats.overlimits++;
b9647580
JP
278 if (police->tcf_action == TC_ACT_SHOT)
279 police->tcf_qstats.drops++;
e9ce1cd3
DM
280 spin_unlock(&police->tcf_lock);
281 return police->tcf_action;
1da177e4 282 }
1da177e4 283
0abf77e5 284 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
c6d14ff1 285 if (!police->rate_present) {
e9ce1cd3
DM
286 spin_unlock(&police->tcf_lock);
287 return police->tcfp_result;
1da177e4
LT
288 }
289
d2de875c 290 now = ktime_get_ns();
c6d14ff1
JP
291 toks = min_t(s64, now - police->tcfp_t_c,
292 police->tcfp_burst);
293 if (police->peak_present) {
e9ce1cd3 294 ptoks = toks + police->tcfp_ptoks;
c6d14ff1
JP
295 if (ptoks > police->tcfp_mtu_ptoks)
296 ptoks = police->tcfp_mtu_ptoks;
297 ptoks -= (s64) psched_l2t_ns(&police->peak,
298 qdisc_pkt_len(skb));
1da177e4 299 }
e9ce1cd3 300 toks += police->tcfp_toks;
c6d14ff1 301 if (toks > police->tcfp_burst)
e9ce1cd3 302 toks = police->tcfp_burst;
c6d14ff1 303 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
1da177e4 304 if ((toks|ptoks) >= 0) {
e9ce1cd3
DM
305 police->tcfp_t_c = now;
306 police->tcfp_toks = toks;
307 police->tcfp_ptoks = ptoks;
308 spin_unlock(&police->tcf_lock);
309 return police->tcfp_result;
1da177e4
LT
310 }
311 }
312
e9ce1cd3 313 police->tcf_qstats.overlimits++;
b9647580
JP
314 if (police->tcf_action == TC_ACT_SHOT)
315 police->tcf_qstats.drops++;
e9ce1cd3
DM
316 spin_unlock(&police->tcf_lock);
317 return police->tcf_action;
1da177e4
LT
318}
319
320static int
321tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
322{
27a884dc 323 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 324 struct tcf_police *police = a->priv;
0f04cfd0
JM
325 struct tc_police opt = {
326 .index = police->tcf_index,
327 .action = police->tcf_action,
328 .mtu = police->tcfp_mtu,
c6d14ff1 329 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
0f04cfd0
JM
330 .refcnt = police->tcf_refcnt - ref,
331 .bindcnt = police->tcf_bindcnt - bind,
332 };
3d3ed181 333 struct tcf_t t;
0f04cfd0 334
c6d14ff1 335 if (police->rate_present)
01cb71d2 336 psched_ratecfg_getrate(&opt.rate, &police->rate);
c6d14ff1 337 if (police->peak_present)
01cb71d2 338 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
1b34ec43
DM
339 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
340 goto nla_put_failure;
341 if (police->tcfp_result &&
342 nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
343 goto nla_put_failure;
344 if (police->tcfp_ewma_rate &&
345 nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
346 goto nla_put_failure;
3d3ed181
JHS
347
348 t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
349 t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
350 t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
351 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
352 goto nla_put_failure;
353
1da177e4
LT
354 return skb->len;
355
7ba699c6 356nla_put_failure:
dc5fc579 357 nlmsg_trim(skb, b);
1da177e4
LT
358 return -1;
359}
360
ddf97ccd
WC
361static int tcf_police_search(struct net *net, struct tc_action *a, u32 index)
362{
363 struct tc_action_net *tn = net_generic(net, police_net_id);
364
365 return tcf_hash_search(tn, a, index);
366}
367
1da177e4
LT
368MODULE_AUTHOR("Alexey Kuznetsov");
369MODULE_DESCRIPTION("Policing actions");
370MODULE_LICENSE("GPL");
371
372static struct tc_action_ops act_police_ops = {
373 .kind = "police",
374 .type = TCA_ID_POLICE,
1da177e4
LT
375 .owner = THIS_MODULE,
376 .act = tcf_act_police,
377 .dump = tcf_act_police_dump,
1da177e4 378 .init = tcf_act_police_locate,
ddf97ccd
WC
379 .walk = tcf_act_police_walker,
380 .lookup = tcf_police_search,
381};
382
383static __net_init int police_init_net(struct net *net)
384{
385 struct tc_action_net *tn = net_generic(net, police_net_id);
386
387 return tc_action_net_init(tn, &act_police_ops, POL_TAB_MASK);
388}
389
390static void __net_exit police_exit_net(struct net *net)
391{
392 struct tc_action_net *tn = net_generic(net, police_net_id);
393
394 tc_action_net_exit(tn);
395}
396
397static struct pernet_operations police_net_ops = {
398 .init = police_init_net,
399 .exit = police_exit_net,
400 .id = &police_net_id,
401 .size = sizeof(struct tc_action_net),
1da177e4
LT
402};
403
404static int __init
405police_init_module(void)
406{
ddf97ccd 407 return tcf_register_action(&act_police_ops, &police_net_ops);
1da177e4
LT
408}
409
410static void __exit
411police_cleanup_module(void)
412{
ddf97ccd 413 tcf_unregister_action(&act_police_ops, &police_net_ops);
1da177e4
LT
414}
415
416module_init(police_init_module);
417module_exit(police_cleanup_module);