Merge git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux-tile
[linux-2.6-block.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);
e9ce1cd3 242 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
c779f7af
WC
243 spin_lock_bh(&hinfo->lock);
244 hlist_add_head(&police->tcf_head, &hinfo->htab[h]);
245 spin_unlock_bh(&hinfo->lock);
1da177e4 246
e9ce1cd3 247 a->priv = police;
1da177e4
LT
248 return ret;
249
71bcb09a
SH
250failure_unlock:
251 spin_unlock_bh(&police->tcf_lock);
1da177e4 252failure:
3b69a4c9
YY
253 qdisc_put_rtab(P_tab);
254 qdisc_put_rtab(R_tab);
1da177e4 255 if (ret == ACT_P_CREATED)
e9ce1cd3 256 kfree(police);
1da177e4
LT
257 return err;
258}
259
dc7f9f6e 260static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
10297b99 261 struct tcf_result *res)
1da177e4 262{
e9ce1cd3 263 struct tcf_police *police = a->priv;
c6d14ff1
JP
264 s64 now;
265 s64 toks;
266 s64 ptoks = 0;
1da177e4 267
e9ce1cd3 268 spin_lock(&police->tcf_lock);
1da177e4 269
bfe0d029 270 bstats_update(&police->tcf_bstats, skb);
1da177e4 271
e9ce1cd3
DM
272 if (police->tcfp_ewma_rate &&
273 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
274 police->tcf_qstats.overlimits++;
b9647580
JP
275 if (police->tcf_action == TC_ACT_SHOT)
276 police->tcf_qstats.drops++;
e9ce1cd3
DM
277 spin_unlock(&police->tcf_lock);
278 return police->tcf_action;
1da177e4 279 }
1da177e4 280
0abf77e5 281 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
c6d14ff1 282 if (!police->rate_present) {
e9ce1cd3
DM
283 spin_unlock(&police->tcf_lock);
284 return police->tcfp_result;
1da177e4
LT
285 }
286
d2de875c 287 now = ktime_get_ns();
c6d14ff1
JP
288 toks = min_t(s64, now - police->tcfp_t_c,
289 police->tcfp_burst);
290 if (police->peak_present) {
e9ce1cd3 291 ptoks = toks + police->tcfp_ptoks;
c6d14ff1
JP
292 if (ptoks > police->tcfp_mtu_ptoks)
293 ptoks = police->tcfp_mtu_ptoks;
294 ptoks -= (s64) psched_l2t_ns(&police->peak,
295 qdisc_pkt_len(skb));
1da177e4 296 }
e9ce1cd3 297 toks += police->tcfp_toks;
c6d14ff1 298 if (toks > police->tcfp_burst)
e9ce1cd3 299 toks = police->tcfp_burst;
c6d14ff1 300 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
1da177e4 301 if ((toks|ptoks) >= 0) {
e9ce1cd3
DM
302 police->tcfp_t_c = now;
303 police->tcfp_toks = toks;
304 police->tcfp_ptoks = ptoks;
305 spin_unlock(&police->tcf_lock);
306 return police->tcfp_result;
1da177e4
LT
307 }
308 }
309
e9ce1cd3 310 police->tcf_qstats.overlimits++;
b9647580
JP
311 if (police->tcf_action == TC_ACT_SHOT)
312 police->tcf_qstats.drops++;
e9ce1cd3
DM
313 spin_unlock(&police->tcf_lock);
314 return police->tcf_action;
1da177e4
LT
315}
316
317static int
318tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
319{
27a884dc 320 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 321 struct tcf_police *police = a->priv;
0f04cfd0
JM
322 struct tc_police opt = {
323 .index = police->tcf_index,
324 .action = police->tcf_action,
325 .mtu = police->tcfp_mtu,
c6d14ff1 326 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
0f04cfd0
JM
327 .refcnt = police->tcf_refcnt - ref,
328 .bindcnt = police->tcf_bindcnt - bind,
329 };
330
c6d14ff1 331 if (police->rate_present)
01cb71d2 332 psched_ratecfg_getrate(&opt.rate, &police->rate);
c6d14ff1 333 if (police->peak_present)
01cb71d2 334 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
1b34ec43
DM
335 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
336 goto nla_put_failure;
337 if (police->tcfp_result &&
338 nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
339 goto nla_put_failure;
340 if (police->tcfp_ewma_rate &&
341 nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
342 goto nla_put_failure;
1da177e4
LT
343 return skb->len;
344
7ba699c6 345nla_put_failure:
dc5fc579 346 nlmsg_trim(skb, b);
1da177e4
LT
347 return -1;
348}
349
ddf97ccd
WC
350static int tcf_police_search(struct net *net, struct tc_action *a, u32 index)
351{
352 struct tc_action_net *tn = net_generic(net, police_net_id);
353
354 return tcf_hash_search(tn, a, index);
355}
356
1da177e4
LT
357MODULE_AUTHOR("Alexey Kuznetsov");
358MODULE_DESCRIPTION("Policing actions");
359MODULE_LICENSE("GPL");
360
361static struct tc_action_ops act_police_ops = {
362 .kind = "police",
363 .type = TCA_ID_POLICE,
1da177e4
LT
364 .owner = THIS_MODULE,
365 .act = tcf_act_police,
366 .dump = tcf_act_police_dump,
1da177e4 367 .init = tcf_act_police_locate,
ddf97ccd
WC
368 .walk = tcf_act_police_walker,
369 .lookup = tcf_police_search,
370};
371
372static __net_init int police_init_net(struct net *net)
373{
374 struct tc_action_net *tn = net_generic(net, police_net_id);
375
376 return tc_action_net_init(tn, &act_police_ops, POL_TAB_MASK);
377}
378
379static void __net_exit police_exit_net(struct net *net)
380{
381 struct tc_action_net *tn = net_generic(net, police_net_id);
382
383 tc_action_net_exit(tn);
384}
385
386static struct pernet_operations police_net_ops = {
387 .init = police_init_net,
388 .exit = police_exit_net,
389 .id = &police_net_id,
390 .size = sizeof(struct tc_action_net),
1da177e4
LT
391};
392
393static int __init
394police_init_module(void)
395{
ddf97ccd 396 return tcf_register_action(&act_police_ops, &police_net_ops);
1da177e4
LT
397}
398
399static void __exit
400police_cleanup_module(void)
401{
ddf97ccd 402 tcf_unregister_action(&act_police_ops, &police_net_ops);
1da177e4
LT
403}
404
405module_init(police_init_module);
406module_exit(police_cleanup_module);