]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/act_police.c
act_police: remove unnecessary null pointer check
[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:
3b69a4c9
YY
264 qdisc_put_rtab(P_tab);
265 qdisc_put_rtab(R_tab);
1da177e4 266 if (ret == ACT_P_CREATED)
e9ce1cd3 267 kfree(police);
1da177e4
LT
268 return err;
269}
270
271static int tcf_act_police_cleanup(struct tc_action *a, int bind)
272{
e9ce1cd3 273 struct tcf_police *p = a->priv;
c3bc7cff 274 int ret = 0;
1da177e4 275
c3bc7cff
PM
276 if (p != NULL) {
277 if (bind)
278 p->tcf_bindcnt--;
279
280 p->tcf_refcnt--;
281 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
282 tcf_police_destroy(p);
283 ret = 1;
284 }
285 }
286 return ret;
1da177e4
LT
287}
288
dc7f9f6e 289static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
10297b99 290 struct tcf_result *res)
1da177e4 291{
e9ce1cd3 292 struct tcf_police *police = a->priv;
c6d14ff1
JP
293 s64 now;
294 s64 toks;
295 s64 ptoks = 0;
1da177e4 296
e9ce1cd3 297 spin_lock(&police->tcf_lock);
1da177e4 298
bfe0d029 299 bstats_update(&police->tcf_bstats, skb);
1da177e4 300
e9ce1cd3
DM
301 if (police->tcfp_ewma_rate &&
302 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
303 police->tcf_qstats.overlimits++;
b9647580
JP
304 if (police->tcf_action == TC_ACT_SHOT)
305 police->tcf_qstats.drops++;
e9ce1cd3
DM
306 spin_unlock(&police->tcf_lock);
307 return police->tcf_action;
1da177e4 308 }
1da177e4 309
0abf77e5 310 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
c6d14ff1 311 if (!police->rate_present) {
e9ce1cd3
DM
312 spin_unlock(&police->tcf_lock);
313 return police->tcfp_result;
1da177e4
LT
314 }
315
c6d14ff1
JP
316 now = ktime_to_ns(ktime_get());
317 toks = min_t(s64, now - police->tcfp_t_c,
318 police->tcfp_burst);
319 if (police->peak_present) {
e9ce1cd3 320 ptoks = toks + police->tcfp_ptoks;
c6d14ff1
JP
321 if (ptoks > police->tcfp_mtu_ptoks)
322 ptoks = police->tcfp_mtu_ptoks;
323 ptoks -= (s64) psched_l2t_ns(&police->peak,
324 qdisc_pkt_len(skb));
1da177e4 325 }
e9ce1cd3 326 toks += police->tcfp_toks;
c6d14ff1 327 if (toks > police->tcfp_burst)
e9ce1cd3 328 toks = police->tcfp_burst;
c6d14ff1 329 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
1da177e4 330 if ((toks|ptoks) >= 0) {
e9ce1cd3
DM
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;
1da177e4
LT
336 }
337 }
338
e9ce1cd3 339 police->tcf_qstats.overlimits++;
b9647580
JP
340 if (police->tcf_action == TC_ACT_SHOT)
341 police->tcf_qstats.drops++;
e9ce1cd3
DM
342 spin_unlock(&police->tcf_lock);
343 return police->tcf_action;
1da177e4
LT
344}
345
346static int
347tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
348{
27a884dc 349 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 350 struct tcf_police *police = a->priv;
0f04cfd0
JM
351 struct tc_police opt = {
352 .index = police->tcf_index,
353 .action = police->tcf_action,
354 .mtu = police->tcfp_mtu,
c6d14ff1 355 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
0f04cfd0
JM
356 .refcnt = police->tcf_refcnt - ref,
357 .bindcnt = police->tcf_bindcnt - bind,
358 };
359
c6d14ff1 360 if (police->rate_present)
01cb71d2 361 psched_ratecfg_getrate(&opt.rate, &police->rate);
c6d14ff1 362 if (police->peak_present)
01cb71d2 363 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
1b34ec43
DM
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;
1da177e4
LT
372 return skb->len;
373
7ba699c6 374nla_put_failure:
dc5fc579 375 nlmsg_trim(skb, b);
1da177e4
LT
376 return -1;
377}
378
379MODULE_AUTHOR("Alexey Kuznetsov");
380MODULE_DESCRIPTION("Policing actions");
381MODULE_LICENSE("GPL");
382
383static struct tc_action_ops act_police_ops = {
384 .kind = "police",
e9ce1cd3 385 .hinfo = &police_hash_info,
1da177e4
LT
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,
1da177e4 392 .init = tcf_act_police_locate,
83b950c8 393 .walk = tcf_act_police_walker
1da177e4
LT
394};
395
396static int __init
397police_init_module(void)
398{
369ba567
WC
399 int err = tcf_hashinfo_init(&police_hash_info, POL_TAB_MASK+1);
400 if (err)
401 return err;
402 err = tcf_register_action(&act_police_ops);
403 if (err)
404 tcf_hashinfo_destroy(&police_hash_info);
405 return err;
1da177e4
LT
406}
407
408static void __exit
409police_cleanup_module(void)
410{
369ba567 411 tcf_hashinfo_destroy(&police_hash_info);
1da177e4
LT
412 tcf_unregister_action(&act_police_ops);
413}
414
415module_init(police_init_module);
416module_exit(police_cleanup_module);