]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/act_police.c
sfc: fix sparse non static symbol warning
[mirror_ubuntu-bionic-kernel.git] / net / sched / act_police.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/police.c Input police filter.
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
e9ce1cd3 44static u32 police_idx_gen;
369ba567 45static struct tcf_hashinfo police_hash_info;
1da177e4 46
1e9b3d53 47/* old policer structure from before tc actions */
cc7ec456 48struct tc_police_compat {
1e9b3d53
PM
49 u32 index;
50 int action;
51 u32 limit;
52 u32 burst;
53 u32 mtu;
54 struct tc_ratespec rate;
55 struct tc_ratespec peakrate;
56};
57
e9ce1cd3 58/* Each policer is serialized by its individual spinlock */
1da177e4 59
83b950c8 60static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
10297b99 61 int type, struct tc_action *a)
1da177e4 62{
89819dc0 63 struct hlist_head *head;
e9ce1cd3 64 struct tcf_common *p;
1da177e4 65 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
4b3550ef 66 struct nlattr *nest;
1da177e4 67
89819dc0 68 spin_lock_bh(&police_hash_info.lock);
1da177e4
LT
69
70 s_i = cb->args[0];
71
e9ce1cd3 72 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
89819dc0 73 head = &police_hash_info.htab[tcf_hash(i, POL_TAB_MASK)];
1da177e4 74
89819dc0 75 hlist_for_each_entry_rcu(p, head, tcfc_head) {
1da177e4
LT
76 index++;
77 if (index < s_i)
78 continue;
79 a->priv = p;
80 a->order = index;
4b3550ef
PM
81 nest = nla_nest_start(skb, a->order);
82 if (nest == NULL)
83 goto nla_put_failure;
1da177e4
LT
84 if (type == RTM_DELACTION)
85 err = tcf_action_dump_1(skb, a, 0, 1);
86 else
87 err = tcf_action_dump_1(skb, a, 0, 0);
88 if (err < 0) {
89 index--;
4b3550ef 90 nla_nest_cancel(skb, nest);
1da177e4
LT
91 goto done;
92 }
4b3550ef 93 nla_nest_end(skb, nest);
1da177e4
LT
94 n_i++;
95 }
96 }
97done:
89819dc0 98 spin_unlock_bh(&police_hash_info.lock);
1da177e4
LT
99 if (n_i)
100 cb->args[0] += n_i;
101 return n_i;
102
7ba699c6 103nla_put_failure:
4b3550ef 104 nla_nest_cancel(skb, nest);
1da177e4
LT
105 goto done;
106}
1da177e4 107
c3bc7cff 108static void tcf_police_destroy(struct tcf_police *p)
1da177e4 109{
89819dc0
WC
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,
114 &p->tcf_rate_est);
115 /*
116 * gen_estimator est_timer() might access p->tcf_lock
117 * or bstats, wait a RCU grace period before freeing p
118 */
119 kfree_rcu(p, tcf_rcu);
1da177e4
LT
120}
121
53b2bf3f
PM
122static 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 },
127};
128
c1b52739
BL
129static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
130 struct nlattr *est, struct tc_action *a,
131 int ovr, int bind)
1da177e4 132{
cc7ec456 133 unsigned int h;
1da177e4 134 int ret = 0, err;
7ba699c6 135 struct nlattr *tb[TCA_POLICE_MAX + 1];
1da177e4 136 struct tc_police *parm;
e9ce1cd3 137 struct tcf_police *police;
1da177e4 138 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
1e9b3d53 139 int size;
1da177e4 140
cee63723 141 if (nla == NULL)
1da177e4
LT
142 return -EINVAL;
143
53b2bf3f 144 err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
cee63723
PM
145 if (err < 0)
146 return err;
147
7ba699c6 148 if (tb[TCA_POLICE_TBF] == NULL)
1e9b3d53 149 return -EINVAL;
7ba699c6 150 size = nla_len(tb[TCA_POLICE_TBF]);
1e9b3d53 151 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1da177e4 152 return -EINVAL;
7ba699c6 153 parm = nla_data(tb[TCA_POLICE_TBF]);
1da177e4 154
e9ce1cd3
DM
155 if (parm->index) {
156 struct tcf_common *pc;
157
158 pc = tcf_hash_lookup(parm->index, &police_hash_info);
159 if (pc != NULL) {
160 a->priv = pc;
161 police = to_police(pc);
162 if (bind) {
163 police->tcf_bindcnt += 1;
164 police->tcf_refcnt += 1;
165 }
166 if (ovr)
167 goto override;
168 return ret;
1da177e4 169 }
1da177e4
LT
170 }
171
e9ce1cd3
DM
172 police = kzalloc(sizeof(*police), GFP_KERNEL);
173 if (police == NULL)
1da177e4 174 return -ENOMEM;
1da177e4 175 ret = ACT_P_CREATED;
e9ce1cd3
DM
176 police->tcf_refcnt = 1;
177 spin_lock_init(&police->tcf_lock);
1da177e4 178 if (bind)
e9ce1cd3 179 police->tcf_bindcnt = 1;
1da177e4
LT
180override:
181 if (parm->rate.rate) {
182 err = -ENOMEM;
7ba699c6 183 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
1da177e4
LT
184 if (R_tab == NULL)
185 goto failure;
c1b56878 186
1da177e4
LT
187 if (parm->peakrate.rate) {
188 P_tab = qdisc_get_rtab(&parm->peakrate,
7ba699c6 189 tb[TCA_POLICE_PEAKRATE]);
71bcb09a 190 if (P_tab == NULL)
1da177e4 191 goto failure;
1da177e4
LT
192 }
193 }
71bcb09a 194
e9ce1cd3 195 spin_lock_bh(&police->tcf_lock);
71bcb09a
SH
196 if (est) {
197 err = gen_replace_estimator(&police->tcf_bstats,
198 &police->tcf_rate_est,
199 &police->tcf_lock, est);
200 if (err)
201 goto failure_unlock;
a883bf56
JP
202 } else if (tb[TCA_POLICE_AVRATE] &&
203 (ret == ACT_P_CREATED ||
204 !gen_estimator_active(&police->tcf_bstats,
205 &police->tcf_rate_est))) {
206 err = -EINVAL;
207 goto failure_unlock;
71bcb09a
SH
208 }
209
210 /* No failure allowed after this point */
c6d14ff1
JP
211 police->tcfp_mtu = parm->mtu;
212 if (police->tcfp_mtu == 0) {
213 police->tcfp_mtu = ~0;
214 if (R_tab)
215 police->tcfp_mtu = 255 << R_tab->rate.cell_log;
216 }
217 if (R_tab) {
218 police->rate_present = true;
3e1e3aae 219 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
c6d14ff1
JP
220 qdisc_put_rtab(R_tab);
221 } else {
222 police->rate_present = false;
1da177e4 223 }
c6d14ff1
JP
224 if (P_tab) {
225 police->peak_present = true;
3e1e3aae 226 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
c6d14ff1
JP
227 qdisc_put_rtab(P_tab);
228 } else {
229 police->peak_present = false;
1da177e4
LT
230 }
231
7ba699c6 232 if (tb[TCA_POLICE_RESULT])
1587bac4 233 police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
c6d14ff1
JP
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,
238 police->tcfp_mtu);
239 police->tcfp_ptoks = police->tcfp_mtu_ptoks;
1da177e4 240 }
e9ce1cd3 241 police->tcf_action = parm->action;
1da177e4 242
7ba699c6 243 if (tb[TCA_POLICE_AVRATE])
1587bac4 244 police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1da177e4 245
e9ce1cd3 246 spin_unlock_bh(&police->tcf_lock);
1da177e4
LT
247 if (ret != ACT_P_CREATED)
248 return ret;
249
c6d14ff1 250 police->tcfp_t_c = ktime_to_ns(ktime_get());
e9ce1cd3
DM
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);
89819dc0
WC
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);
1da177e4 257
e9ce1cd3 258 a->priv = police;
1da177e4
LT
259 return ret;
260
71bcb09a
SH
261failure_unlock:
262 spin_unlock_bh(&police->tcf_lock);
1da177e4 263failure:
71bcb09a
SH
264 if (P_tab)
265 qdisc_put_rtab(P_tab);
266 if (R_tab)
267 qdisc_put_rtab(R_tab);
1da177e4 268 if (ret == ACT_P_CREATED)
e9ce1cd3 269 kfree(police);
1da177e4
LT
270 return err;
271}
272
273static int tcf_act_police_cleanup(struct tc_action *a, int bind)
274{
e9ce1cd3 275 struct tcf_police *p = a->priv;
c3bc7cff 276 int ret = 0;
1da177e4 277
c3bc7cff
PM
278 if (p != NULL) {
279 if (bind)
280 p->tcf_bindcnt--;
281
282 p->tcf_refcnt--;
283 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
284 tcf_police_destroy(p);
285 ret = 1;
286 }
287 }
288 return ret;
1da177e4
LT
289}
290
dc7f9f6e 291static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
10297b99 292 struct tcf_result *res)
1da177e4 293{
e9ce1cd3 294 struct tcf_police *police = a->priv;
c6d14ff1
JP
295 s64 now;
296 s64 toks;
297 s64 ptoks = 0;
1da177e4 298
e9ce1cd3 299 spin_lock(&police->tcf_lock);
1da177e4 300
bfe0d029 301 bstats_update(&police->tcf_bstats, skb);
1da177e4 302
e9ce1cd3
DM
303 if (police->tcfp_ewma_rate &&
304 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
305 police->tcf_qstats.overlimits++;
b9647580
JP
306 if (police->tcf_action == TC_ACT_SHOT)
307 police->tcf_qstats.drops++;
e9ce1cd3
DM
308 spin_unlock(&police->tcf_lock);
309 return police->tcf_action;
1da177e4 310 }
1da177e4 311
0abf77e5 312 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
c6d14ff1 313 if (!police->rate_present) {
e9ce1cd3
DM
314 spin_unlock(&police->tcf_lock);
315 return police->tcfp_result;
1da177e4
LT
316 }
317
c6d14ff1
JP
318 now = ktime_to_ns(ktime_get());
319 toks = min_t(s64, now - police->tcfp_t_c,
320 police->tcfp_burst);
321 if (police->peak_present) {
e9ce1cd3 322 ptoks = toks + police->tcfp_ptoks;
c6d14ff1
JP
323 if (ptoks > police->tcfp_mtu_ptoks)
324 ptoks = police->tcfp_mtu_ptoks;
325 ptoks -= (s64) psched_l2t_ns(&police->peak,
326 qdisc_pkt_len(skb));
1da177e4 327 }
e9ce1cd3 328 toks += police->tcfp_toks;
c6d14ff1 329 if (toks > police->tcfp_burst)
e9ce1cd3 330 toks = police->tcfp_burst;
c6d14ff1 331 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
1da177e4 332 if ((toks|ptoks) >= 0) {
e9ce1cd3
DM
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;
1da177e4
LT
338 }
339 }
340
e9ce1cd3 341 police->tcf_qstats.overlimits++;
b9647580
JP
342 if (police->tcf_action == TC_ACT_SHOT)
343 police->tcf_qstats.drops++;
e9ce1cd3
DM
344 spin_unlock(&police->tcf_lock);
345 return police->tcf_action;
1da177e4
LT
346}
347
348static int
349tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
350{
27a884dc 351 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 352 struct tcf_police *police = a->priv;
0f04cfd0
JM
353 struct tc_police opt = {
354 .index = police->tcf_index,
355 .action = police->tcf_action,
356 .mtu = police->tcfp_mtu,
c6d14ff1 357 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
0f04cfd0
JM
358 .refcnt = police->tcf_refcnt - ref,
359 .bindcnt = police->tcf_bindcnt - bind,
360 };
361
c6d14ff1 362 if (police->rate_present)
01cb71d2 363 psched_ratecfg_getrate(&opt.rate, &police->rate);
c6d14ff1 364 if (police->peak_present)
01cb71d2 365 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
1b34ec43
DM
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;
1da177e4
LT
374 return skb->len;
375
7ba699c6 376nla_put_failure:
dc5fc579 377 nlmsg_trim(skb, b);
1da177e4
LT
378 return -1;
379}
380
381MODULE_AUTHOR("Alexey Kuznetsov");
382MODULE_DESCRIPTION("Policing actions");
383MODULE_LICENSE("GPL");
384
385static struct tc_action_ops act_police_ops = {
386 .kind = "police",
e9ce1cd3 387 .hinfo = &police_hash_info,
1da177e4
LT
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,
1da177e4 394 .init = tcf_act_police_locate,
83b950c8 395 .walk = tcf_act_police_walker
1da177e4
LT
396};
397
398static int __init
399police_init_module(void)
400{
369ba567
WC
401 int err = tcf_hashinfo_init(&police_hash_info, POL_TAB_MASK+1);
402 if (err)
403 return err;
404 err = tcf_register_action(&act_police_ops);
405 if (err)
406 tcf_hashinfo_destroy(&police_hash_info);
407 return err;
1da177e4
LT
408}
409
410static void __exit
411police_cleanup_module(void)
412{
369ba567 413 tcf_hashinfo_destroy(&police_hash_info);
1da177e4
LT
414 tcf_unregister_action(&act_police_ops);
415}
416
417module_init(police_init_module);
418module_exit(police_cleanup_module);