]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame_incremental - net/sched/act_police.c
net: macb: Add null check for PCLK and HCLK
[thirdparty/kernel/stable.git] / net / sched / act_police.c
... / ...
CommitLineData
1/*
2 * net/sched/act_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
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>
24
25struct tcf_police_params {
26 int tcfp_result;
27 u32 tcfp_ewma_rate;
28 s64 tcfp_burst;
29 u32 tcfp_mtu;
30 s64 tcfp_mtu_ptoks;
31 struct psched_ratecfg rate;
32 bool rate_present;
33 struct psched_ratecfg peak;
34 bool peak_present;
35 struct rcu_head rcu;
36};
37
38struct tcf_police {
39 struct tc_action common;
40 struct tcf_police_params __rcu *params;
41
42 spinlock_t tcfp_lock ____cacheline_aligned_in_smp;
43 s64 tcfp_toks;
44 s64 tcfp_ptoks;
45 s64 tcfp_t_c;
46};
47
48#define to_police(pc) ((struct tcf_police *)pc)
49
50/* old policer structure from before tc actions */
51struct tc_police_compat {
52 u32 index;
53 int action;
54 u32 limit;
55 u32 burst;
56 u32 mtu;
57 struct tc_ratespec rate;
58 struct tc_ratespec peakrate;
59};
60
61/* Each policer is serialized by its individual spinlock */
62
63static unsigned int police_net_id;
64static struct tc_action_ops act_police_ops;
65
66static int tcf_police_walker(struct net *net, struct sk_buff *skb,
67 struct netlink_callback *cb, int type,
68 const struct tc_action_ops *ops,
69 struct netlink_ext_ack *extack)
70{
71 struct tc_action_net *tn = net_generic(net, police_net_id);
72
73 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
74}
75
76static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
77 [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
78 [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
79 [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
80 [TCA_POLICE_RESULT] = { .type = NLA_U32 },
81};
82
83static int tcf_police_init(struct net *net, struct nlattr *nla,
84 struct nlattr *est, struct tc_action **a,
85 int ovr, int bind, bool rtnl_held,
86 struct netlink_ext_ack *extack)
87{
88 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
89 struct nlattr *tb[TCA_POLICE_MAX + 1];
90 struct tc_police *parm;
91 struct tcf_police *police;
92 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
93 struct tc_action_net *tn = net_generic(net, police_net_id);
94 struct tcf_police_params *new;
95 bool exists = false;
96
97 if (nla == NULL)
98 return -EINVAL;
99
100 err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
101 if (err < 0)
102 return err;
103
104 if (tb[TCA_POLICE_TBF] == NULL)
105 return -EINVAL;
106 size = nla_len(tb[TCA_POLICE_TBF]);
107 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
108 return -EINVAL;
109
110 parm = nla_data(tb[TCA_POLICE_TBF]);
111 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
112 if (err < 0)
113 return err;
114 exists = err;
115 if (exists && bind)
116 return 0;
117
118 if (!exists) {
119 ret = tcf_idr_create(tn, parm->index, NULL, a,
120 &act_police_ops, bind, true);
121 if (ret) {
122 tcf_idr_cleanup(tn, parm->index);
123 return ret;
124 }
125 ret = ACT_P_CREATED;
126 spin_lock_init(&(to_police(*a)->tcfp_lock));
127 } else if (!ovr) {
128 tcf_idr_release(*a, bind);
129 return -EEXIST;
130 }
131
132 police = to_police(*a);
133 if (parm->rate.rate) {
134 err = -ENOMEM;
135 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
136 if (R_tab == NULL)
137 goto failure;
138
139 if (parm->peakrate.rate) {
140 P_tab = qdisc_get_rtab(&parm->peakrate,
141 tb[TCA_POLICE_PEAKRATE], NULL);
142 if (P_tab == NULL)
143 goto failure;
144 }
145 }
146
147 if (est) {
148 err = gen_replace_estimator(&police->tcf_bstats,
149 police->common.cpu_bstats,
150 &police->tcf_rate_est,
151 &police->tcf_lock,
152 NULL, est);
153 if (err)
154 goto failure;
155 } else if (tb[TCA_POLICE_AVRATE] &&
156 (ret == ACT_P_CREATED ||
157 !gen_estimator_active(&police->tcf_rate_est))) {
158 err = -EINVAL;
159 goto failure;
160 }
161
162 if (tb[TCA_POLICE_RESULT]) {
163 tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
164 if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
165 NL_SET_ERR_MSG(extack,
166 "goto chain not allowed on fallback");
167 err = -EINVAL;
168 goto failure;
169 }
170 }
171
172 new = kzalloc(sizeof(*new), GFP_KERNEL);
173 if (unlikely(!new)) {
174 err = -ENOMEM;
175 goto failure;
176 }
177
178 /* No failure allowed after this point */
179 new->tcfp_result = tcfp_result;
180 new->tcfp_mtu = parm->mtu;
181 if (!new->tcfp_mtu) {
182 new->tcfp_mtu = ~0;
183 if (R_tab)
184 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
185 }
186 if (R_tab) {
187 new->rate_present = true;
188 psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
189 qdisc_put_rtab(R_tab);
190 } else {
191 new->rate_present = false;
192 }
193 if (P_tab) {
194 new->peak_present = true;
195 psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
196 qdisc_put_rtab(P_tab);
197 } else {
198 new->peak_present = false;
199 }
200
201 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
202 if (new->peak_present)
203 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
204 new->tcfp_mtu);
205
206 if (tb[TCA_POLICE_AVRATE])
207 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
208
209 spin_lock_bh(&police->tcf_lock);
210 spin_lock_bh(&police->tcfp_lock);
211 police->tcfp_t_c = ktime_get_ns();
212 police->tcfp_toks = new->tcfp_burst;
213 if (new->peak_present)
214 police->tcfp_ptoks = new->tcfp_mtu_ptoks;
215 spin_unlock_bh(&police->tcfp_lock);
216 police->tcf_action = parm->action;
217 rcu_swap_protected(police->params,
218 new,
219 lockdep_is_held(&police->tcf_lock));
220 spin_unlock_bh(&police->tcf_lock);
221
222 if (new)
223 kfree_rcu(new, rcu);
224
225 if (ret == ACT_P_CREATED)
226 tcf_idr_insert(tn, *a);
227 return ret;
228
229failure:
230 qdisc_put_rtab(P_tab);
231 qdisc_put_rtab(R_tab);
232 tcf_idr_release(*a, bind);
233 return err;
234}
235
236static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
237 struct tcf_result *res)
238{
239 struct tcf_police *police = to_police(a);
240 struct tcf_police_params *p;
241 s64 now, toks, ptoks = 0;
242 int ret;
243
244 tcf_lastuse_update(&police->tcf_tm);
245 bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
246
247 ret = READ_ONCE(police->tcf_action);
248 p = rcu_dereference_bh(police->params);
249
250 if (p->tcfp_ewma_rate) {
251 struct gnet_stats_rate_est64 sample;
252
253 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
254 sample.bps >= p->tcfp_ewma_rate)
255 goto inc_overlimits;
256 }
257
258 if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
259 if (!p->rate_present) {
260 ret = p->tcfp_result;
261 goto end;
262 }
263
264 now = ktime_get_ns();
265 spin_lock_bh(&police->tcfp_lock);
266 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
267 if (p->peak_present) {
268 ptoks = toks + police->tcfp_ptoks;
269 if (ptoks > p->tcfp_mtu_ptoks)
270 ptoks = p->tcfp_mtu_ptoks;
271 ptoks -= (s64)psched_l2t_ns(&p->peak,
272 qdisc_pkt_len(skb));
273 }
274 toks += police->tcfp_toks;
275 if (toks > p->tcfp_burst)
276 toks = p->tcfp_burst;
277 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
278 if ((toks|ptoks) >= 0) {
279 police->tcfp_t_c = now;
280 police->tcfp_toks = toks;
281 police->tcfp_ptoks = ptoks;
282 spin_unlock_bh(&police->tcfp_lock);
283 ret = p->tcfp_result;
284 goto inc_drops;
285 }
286 spin_unlock_bh(&police->tcfp_lock);
287 }
288
289inc_overlimits:
290 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
291inc_drops:
292 if (ret == TC_ACT_SHOT)
293 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
294end:
295 return ret;
296}
297
298static void tcf_police_cleanup(struct tc_action *a)
299{
300 struct tcf_police *police = to_police(a);
301 struct tcf_police_params *p;
302
303 p = rcu_dereference_protected(police->params, 1);
304 if (p)
305 kfree_rcu(p, rcu);
306}
307
308static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
309 int bind, int ref)
310{
311 unsigned char *b = skb_tail_pointer(skb);
312 struct tcf_police *police = to_police(a);
313 struct tcf_police_params *p;
314 struct tc_police opt = {
315 .index = police->tcf_index,
316 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
317 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
318 };
319 struct tcf_t t;
320
321 spin_lock_bh(&police->tcf_lock);
322 opt.action = police->tcf_action;
323 p = rcu_dereference_protected(police->params,
324 lockdep_is_held(&police->tcf_lock));
325 opt.mtu = p->tcfp_mtu;
326 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
327 if (p->rate_present)
328 psched_ratecfg_getrate(&opt.rate, &p->rate);
329 if (p->peak_present)
330 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
331 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
332 goto nla_put_failure;
333 if (p->tcfp_result &&
334 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
335 goto nla_put_failure;
336 if (p->tcfp_ewma_rate &&
337 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
338 goto nla_put_failure;
339
340 t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
341 t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
342 t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
343 t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
344 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
345 goto nla_put_failure;
346 spin_unlock_bh(&police->tcf_lock);
347
348 return skb->len;
349
350nla_put_failure:
351 spin_unlock_bh(&police->tcf_lock);
352 nlmsg_trim(skb, b);
353 return -1;
354}
355
356static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
357{
358 struct tc_action_net *tn = net_generic(net, police_net_id);
359
360 return tcf_idr_search(tn, a, index);
361}
362
363MODULE_AUTHOR("Alexey Kuznetsov");
364MODULE_DESCRIPTION("Policing actions");
365MODULE_LICENSE("GPL");
366
367static struct tc_action_ops act_police_ops = {
368 .kind = "police",
369 .id = TCA_ID_POLICE,
370 .owner = THIS_MODULE,
371 .act = tcf_police_act,
372 .dump = tcf_police_dump,
373 .init = tcf_police_init,
374 .walk = tcf_police_walker,
375 .lookup = tcf_police_search,
376 .cleanup = tcf_police_cleanup,
377 .size = sizeof(struct tcf_police),
378};
379
380static __net_init int police_init_net(struct net *net)
381{
382 struct tc_action_net *tn = net_generic(net, police_net_id);
383
384 return tc_action_net_init(tn, &act_police_ops);
385}
386
387static void __net_exit police_exit_net(struct list_head *net_list)
388{
389 tc_action_net_exit(net_list, police_net_id);
390}
391
392static struct pernet_operations police_net_ops = {
393 .init = police_init_net,
394 .exit_batch = police_exit_net,
395 .id = &police_net_id,
396 .size = sizeof(struct tc_action_net),
397};
398
399static int __init police_init_module(void)
400{
401 return tcf_register_action(&act_police_ops, &police_net_ops);
402}
403
404static void __exit police_cleanup_module(void)
405{
406 tcf_unregister_action(&act_police_ops, &police_net_ops);
407}
408
409module_init(police_init_module);
410module_exit(police_cleanup_module);