]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/sched/act_police.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / net / sched / act_police.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
0c6965dd 3 * net/sched/act_police.c Input police filter
1da177e4 4 *
1da177e4
LT
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 * J Hadi Salim (action changes)
7 */
8
1da177e4
LT
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
1da177e4 12#include <linux/string.h>
1da177e4 13#include <linux/errno.h>
1da177e4 14#include <linux/skbuff.h>
1da177e4
LT
15#include <linux/rtnetlink.h>
16#include <linux/init.h>
5a0e3ad6 17#include <linux/slab.h>
1da177e4 18#include <net/act_api.h>
dc5fc579 19#include <net/netlink.h>
d6124d6b 20#include <net/pkt_cls.h>
fa762da9 21#include <net/tc_act/tc_police.h>
1e9b3d53 22
e9ce1cd3 23/* Each policer is serialized by its individual spinlock */
1da177e4 24
c7d03a00 25static unsigned int police_net_id;
a85a970a 26static struct tc_action_ops act_police_ops;
ddf97ccd 27
2ac06347 28static int tcf_police_walker(struct net *net, struct sk_buff *skb,
ddf97ccd 29 struct netlink_callback *cb, int type,
41780105
AA
30 const struct tc_action_ops *ops,
31 struct netlink_ext_ack *extack)
1da177e4 32{
ddf97ccd 33 struct tc_action_net *tn = net_generic(net, police_net_id);
1da177e4 34
b3620145 35 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
1da177e4 36}
1da177e4 37
53b2bf3f
PM
38static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
39 [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
40 [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
41 [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
42 [TCA_POLICE_RESULT] = { .type = NLA_U32 },
43};
44
2ac06347 45static int tcf_police_init(struct net *net, struct nlattr *nla,
a85a970a 46 struct nlattr *est, struct tc_action **a,
789871bb 47 int ovr, int bind, bool rtnl_held,
85d0966f 48 struct tcf_proto *tp,
589dad6d 49 struct netlink_ext_ack *extack)
1da177e4 50{
fd6d4338 51 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
7ba699c6 52 struct nlattr *tb[TCA_POLICE_MAX + 1];
d6124d6b 53 struct tcf_chain *goto_ch = NULL;
1da177e4 54 struct tc_police *parm;
e9ce1cd3 55 struct tcf_police *police;
1da177e4 56 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
ddf97ccd 57 struct tc_action_net *tn = net_generic(net, police_net_id);
2d550dba 58 struct tcf_police_params *new;
0852e455 59 bool exists = false;
1da177e4 60
cee63723 61 if (nla == NULL)
1da177e4
LT
62 return -EINVAL;
63
8cb08174
JB
64 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
65 police_policy, NULL);
cee63723
PM
66 if (err < 0)
67 return err;
68
7ba699c6 69 if (tb[TCA_POLICE_TBF] == NULL)
1e9b3d53 70 return -EINVAL;
7ba699c6 71 size = nla_len(tb[TCA_POLICE_TBF]);
1e9b3d53 72 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1da177e4 73 return -EINVAL;
0852e455 74
7ba699c6 75 parm = nla_data(tb[TCA_POLICE_TBF]);
0190c1d4
VB
76 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
77 if (err < 0)
78 return err;
79 exists = err;
0852e455
WC
80 if (exists && bind)
81 return 0;
1da177e4 82
0852e455 83 if (!exists) {
65a206c0 84 ret = tcf_idr_create(tn, parm->index, NULL, a,
93be42f9 85 &act_police_ops, bind, true);
0190c1d4
VB
86 if (ret) {
87 tcf_idr_cleanup(tn, parm->index);
a03e6fe5 88 return ret;
0190c1d4 89 }
a03e6fe5 90 ret = ACT_P_CREATED;
484afd1b 91 spin_lock_init(&(to_police(*a)->tcfp_lock));
4e8ddd7f 92 } else if (!ovr) {
65a206c0 93 tcf_idr_release(*a, bind);
4e8ddd7f 94 return -EEXIST;
1da177e4 95 }
d6124d6b
DC
96 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
97 if (err < 0)
98 goto release_idr;
1da177e4 99
a85a970a 100 police = to_police(*a);
1da177e4
LT
101 if (parm->rate.rate) {
102 err = -ENOMEM;
e9bc3fa2 103 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1da177e4
LT
104 if (R_tab == NULL)
105 goto failure;
c1b56878 106
1da177e4
LT
107 if (parm->peakrate.rate) {
108 P_tab = qdisc_get_rtab(&parm->peakrate,
e9bc3fa2 109 tb[TCA_POLICE_PEAKRATE], NULL);
71bcb09a 110 if (P_tab == NULL)
1da177e4 111 goto failure;
1da177e4
LT
112 }
113 }
71bcb09a 114
71bcb09a 115 if (est) {
93be42f9
DC
116 err = gen_replace_estimator(&police->tcf_bstats,
117 police->common.cpu_bstats,
71bcb09a 118 &police->tcf_rate_est,
edb09eb1
ED
119 &police->tcf_lock,
120 NULL, est);
71bcb09a 121 if (err)
74030603 122 goto failure;
a883bf56
JP
123 } else if (tb[TCA_POLICE_AVRATE] &&
124 (ret == ACT_P_CREATED ||
1c0d32fd 125 !gen_estimator_active(&police->tcf_rate_est))) {
a883bf56 126 err = -EINVAL;
74030603 127 goto failure;
71bcb09a
SH
128 }
129
fd6d4338
DC
130 if (tb[TCA_POLICE_RESULT]) {
131 tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
132 if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
133 NL_SET_ERR_MSG(extack,
134 "goto chain not allowed on fallback");
135 err = -EINVAL;
136 goto failure;
137 }
138 }
139
2d550dba
DC
140 new = kzalloc(sizeof(*new), GFP_KERNEL);
141 if (unlikely(!new)) {
142 err = -ENOMEM;
143 goto failure;
144 }
145
71bcb09a 146 /* No failure allowed after this point */
fd6d4338 147 new->tcfp_result = tcfp_result;
2d550dba
DC
148 new->tcfp_mtu = parm->mtu;
149 if (!new->tcfp_mtu) {
150 new->tcfp_mtu = ~0;
c6d14ff1 151 if (R_tab)
2d550dba 152 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
c6d14ff1
JP
153 }
154 if (R_tab) {
2d550dba
DC
155 new->rate_present = true;
156 psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
c6d14ff1
JP
157 qdisc_put_rtab(R_tab);
158 } else {
2d550dba 159 new->rate_present = false;
1da177e4 160 }
c6d14ff1 161 if (P_tab) {
2d550dba
DC
162 new->peak_present = true;
163 psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
c6d14ff1
JP
164 qdisc_put_rtab(P_tab);
165 } else {
2d550dba 166 new->peak_present = false;
1da177e4
LT
167 }
168
2d550dba 169 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
f2cbd485 170 if (new->peak_present)
2d550dba
DC
171 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
172 new->tcfp_mtu);
1da177e4 173
7ba699c6 174 if (tb[TCA_POLICE_AVRATE])
2d550dba 175 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1da177e4 176
2d550dba 177 spin_lock_bh(&police->tcf_lock);
f2cbd485
DC
178 spin_lock_bh(&police->tcfp_lock);
179 police->tcfp_t_c = ktime_get_ns();
180 police->tcfp_toks = new->tcfp_burst;
181 if (new->peak_present)
182 police->tcfp_ptoks = new->tcfp_mtu_ptoks;
183 spin_unlock_bh(&police->tcfp_lock);
d6124d6b 184 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
2d550dba
DC
185 rcu_swap_protected(police->params,
186 new,
187 lockdep_is_held(&police->tcf_lock));
e9ce1cd3 188 spin_unlock_bh(&police->tcf_lock);
1da177e4 189
d6124d6b
DC
190 if (goto_ch)
191 tcf_chain_put_by_act(goto_ch);
2d550dba
DC
192 if (new)
193 kfree_rcu(new, rcu);
1da177e4 194
2d550dba
DC
195 if (ret == ACT_P_CREATED)
196 tcf_idr_insert(tn, *a);
1da177e4
LT
197 return ret;
198
199failure:
3b69a4c9
YY
200 qdisc_put_rtab(P_tab);
201 qdisc_put_rtab(R_tab);
d6124d6b
DC
202 if (goto_ch)
203 tcf_chain_put_by_act(goto_ch);
204release_idr:
4e8ddd7f 205 tcf_idr_release(*a, bind);
1da177e4
LT
206 return err;
207}
208
2ac06347 209static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
10297b99 210 struct tcf_result *res)
1da177e4 211{
a85a970a 212 struct tcf_police *police = to_police(a);
2d550dba 213 struct tcf_police_params *p;
93be42f9
DC
214 s64 now, toks, ptoks = 0;
215 int ret;
1da177e4 216
3d3ed181 217 tcf_lastuse_update(&police->tcf_tm);
93be42f9 218 bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
1da177e4 219
2d550dba
DC
220 ret = READ_ONCE(police->tcf_action);
221 p = rcu_dereference_bh(police->params);
222
223 if (p->tcfp_ewma_rate) {
1c0d32fd
ED
224 struct gnet_stats_rate_est64 sample;
225
226 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2d550dba 227 sample.bps >= p->tcfp_ewma_rate)
93be42f9 228 goto inc_overlimits;
1da177e4 229 }
1da177e4 230
2d550dba
DC
231 if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
232 if (!p->rate_present) {
233 ret = p->tcfp_result;
234 goto end;
1da177e4
LT
235 }
236
d2de875c 237 now = ktime_get_ns();
f2cbd485
DC
238 spin_lock_bh(&police->tcfp_lock);
239 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2d550dba 240 if (p->peak_present) {
f2cbd485 241 ptoks = toks + police->tcfp_ptoks;
2d550dba
DC
242 if (ptoks > p->tcfp_mtu_ptoks)
243 ptoks = p->tcfp_mtu_ptoks;
244 ptoks -= (s64)psched_l2t_ns(&p->peak,
245 qdisc_pkt_len(skb));
1da177e4 246 }
f2cbd485 247 toks += police->tcfp_toks;
2d550dba
DC
248 if (toks > p->tcfp_burst)
249 toks = p->tcfp_burst;
250 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
1da177e4 251 if ((toks|ptoks) >= 0) {
f2cbd485
DC
252 police->tcfp_t_c = now;
253 police->tcfp_toks = toks;
254 police->tcfp_ptoks = ptoks;
255 spin_unlock_bh(&police->tcfp_lock);
2d550dba 256 ret = p->tcfp_result;
93be42f9 257 goto inc_drops;
1da177e4 258 }
f2cbd485 259 spin_unlock_bh(&police->tcfp_lock);
1da177e4 260 }
93be42f9
DC
261
262inc_overlimits:
263 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
264inc_drops:
265 if (ret == TC_ACT_SHOT)
266 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
2d550dba 267end:
93be42f9 268 return ret;
1da177e4
LT
269}
270
2d550dba
DC
271static void tcf_police_cleanup(struct tc_action *a)
272{
273 struct tcf_police *police = to_police(a);
274 struct tcf_police_params *p;
275
276 p = rcu_dereference_protected(police->params, 1);
277 if (p)
278 kfree_rcu(p, rcu);
279}
280
12f02b6b
PJV
281static void tcf_police_stats_update(struct tc_action *a,
282 u64 bytes, u32 packets,
283 u64 lastuse, bool hw)
284{
285 struct tcf_police *police = to_police(a);
286 struct tcf_t *tm = &police->tcf_tm;
287
288 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
289 if (hw)
290 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
291 bytes, packets);
292 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
293}
294
2ac06347 295static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
5a7a5555 296 int bind, int ref)
1da177e4 297{
27a884dc 298 unsigned char *b = skb_tail_pointer(skb);
a85a970a 299 struct tcf_police *police = to_police(a);
2d550dba 300 struct tcf_police_params *p;
0f04cfd0
JM
301 struct tc_police opt = {
302 .index = police->tcf_index,
036bb443
VB
303 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
304 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
0f04cfd0 305 };
3d3ed181 306 struct tcf_t t;
0f04cfd0 307
e329bc42
VB
308 spin_lock_bh(&police->tcf_lock);
309 opt.action = police->tcf_action;
2d550dba
DC
310 p = rcu_dereference_protected(police->params,
311 lockdep_is_held(&police->tcf_lock));
312 opt.mtu = p->tcfp_mtu;
313 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
314 if (p->rate_present)
315 psched_ratecfg_getrate(&opt.rate, &p->rate);
316 if (p->peak_present)
317 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
1b34ec43
DM
318 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
319 goto nla_put_failure;
2d550dba
DC
320 if (p->tcfp_result &&
321 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
1b34ec43 322 goto nla_put_failure;
2d550dba
DC
323 if (p->tcfp_ewma_rate &&
324 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
1b34ec43 325 goto nla_put_failure;
3d3ed181
JHS
326
327 t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
328 t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
53eb440f 329 t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
3d3ed181
JHS
330 t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
331 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
332 goto nla_put_failure;
e329bc42 333 spin_unlock_bh(&police->tcf_lock);
3d3ed181 334
1da177e4
LT
335 return skb->len;
336
7ba699c6 337nla_put_failure:
e329bc42 338 spin_unlock_bh(&police->tcf_lock);
dc5fc579 339 nlmsg_trim(skb, b);
1da177e4
LT
340 return -1;
341}
342
f061b48c 343static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
344{
345 struct tc_action_net *tn = net_generic(net, police_net_id);
346
65a206c0 347 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
348}
349
1da177e4
LT
350MODULE_AUTHOR("Alexey Kuznetsov");
351MODULE_DESCRIPTION("Policing actions");
352MODULE_LICENSE("GPL");
353
354static struct tc_action_ops act_police_ops = {
355 .kind = "police",
eddd2cf1 356 .id = TCA_ID_POLICE,
1da177e4 357 .owner = THIS_MODULE,
12f02b6b 358 .stats_update = tcf_police_stats_update,
2ac06347
JHS
359 .act = tcf_police_act,
360 .dump = tcf_police_dump,
361 .init = tcf_police_init,
362 .walk = tcf_police_walker,
ddf97ccd 363 .lookup = tcf_police_search,
2d550dba 364 .cleanup = tcf_police_cleanup,
a85a970a 365 .size = sizeof(struct tcf_police),
ddf97ccd
WC
366};
367
368static __net_init int police_init_net(struct net *net)
369{
370 struct tc_action_net *tn = net_generic(net, police_net_id);
371
c7e460ce 372 return tc_action_net_init(tn, &act_police_ops);
ddf97ccd
WC
373}
374
039af9c6 375static void __net_exit police_exit_net(struct list_head *net_list)
ddf97ccd 376{
039af9c6 377 tc_action_net_exit(net_list, police_net_id);
ddf97ccd
WC
378}
379
380static struct pernet_operations police_net_ops = {
381 .init = police_init_net,
039af9c6 382 .exit_batch = police_exit_net,
ddf97ccd
WC
383 .id = &police_net_id,
384 .size = sizeof(struct tc_action_net),
1da177e4
LT
385};
386
5a7a5555 387static int __init police_init_module(void)
1da177e4 388{
ddf97ccd 389 return tcf_register_action(&act_police_ops, &police_net_ops);
1da177e4
LT
390}
391
5a7a5555 392static void __exit police_cleanup_module(void)
1da177e4 393{
ddf97ccd 394 tcf_unregister_action(&act_police_ops, &police_net_ops);
1da177e4
LT
395}
396
397module_init(police_init_module);
398module_exit(police_cleanup_module);