1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/act_police.c Input police filter
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 * J Hadi Salim (action changes)
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/skbuff.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <net/act_api.h>
19 #include <net/netlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_police.h>
23 /* Each policer is serialized by its individual spinlock */
25 static unsigned int police_net_id;
26 static struct tc_action_ops act_police_ops;
28 static int tcf_police_walker(struct net *net, struct sk_buff *skb,
29 struct netlink_callback *cb, int type,
30 const struct tc_action_ops *ops,
31 struct netlink_ext_ack *extack)
33 struct tc_action_net *tn = net_generic(net, police_net_id);
35 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
38 static 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 [TCA_POLICE_RATE64] = { .type = NLA_U64 },
44 [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
45 [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 },
46 [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 },
49 static int tcf_police_init(struct net *net, struct nlattr *nla,
50 struct nlattr *est, struct tc_action **a,
51 struct tcf_proto *tp, u32 flags,
52 struct netlink_ext_ack *extack)
54 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
55 bool bind = flags & TCA_ACT_FLAGS_BIND;
56 struct nlattr *tb[TCA_POLICE_MAX + 1];
57 struct tcf_chain *goto_ch = NULL;
58 struct tc_police *parm;
59 struct tcf_police *police;
60 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
61 struct tc_action_net *tn = net_generic(net, police_net_id);
62 struct tcf_police_params *new;
71 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
76 if (tb[TCA_POLICE_TBF] == NULL)
78 size = nla_len(tb[TCA_POLICE_TBF]);
79 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
82 parm = nla_data(tb[TCA_POLICE_TBF]);
84 err = tcf_idr_check_alloc(tn, &index, a, bind);
92 ret = tcf_idr_create(tn, index, NULL, a,
93 &act_police_ops, bind, true, flags);
95 tcf_idr_cleanup(tn, index);
99 spin_lock_init(&(to_police(*a)->tcfp_lock));
100 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
101 tcf_idr_release(*a, bind);
104 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
108 police = to_police(*a);
109 if (parm->rate.rate) {
111 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
115 if (parm->peakrate.rate) {
116 P_tab = qdisc_get_rtab(&parm->peakrate,
117 tb[TCA_POLICE_PEAKRATE], NULL);
124 err = gen_replace_estimator(&police->tcf_bstats,
125 police->common.cpu_bstats,
126 &police->tcf_rate_est,
131 } else if (tb[TCA_POLICE_AVRATE] &&
132 (ret == ACT_P_CREATED ||
133 !gen_estimator_active(&police->tcf_rate_est))) {
138 if (tb[TCA_POLICE_RESULT]) {
139 tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
140 if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
141 NL_SET_ERR_MSG(extack,
142 "goto chain not allowed on fallback");
148 if ((tb[TCA_POLICE_PKTRATE64] && !tb[TCA_POLICE_PKTBURST64]) ||
149 (!tb[TCA_POLICE_PKTRATE64] && tb[TCA_POLICE_PKTBURST64])) {
150 NL_SET_ERR_MSG(extack,
151 "Both or neither packet-per-second burst and rate must be provided");
156 if (tb[TCA_POLICE_PKTRATE64] && R_tab) {
157 NL_SET_ERR_MSG(extack,
158 "packet-per-second and byte-per-second rate limits not allowed in same action");
163 new = kzalloc(sizeof(*new), GFP_KERNEL);
164 if (unlikely(!new)) {
169 /* No failure allowed after this point */
170 new->tcfp_result = tcfp_result;
171 new->tcfp_mtu = parm->mtu;
172 if (!new->tcfp_mtu) {
175 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
178 new->rate_present = true;
179 rate64 = tb[TCA_POLICE_RATE64] ?
180 nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
181 psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
182 qdisc_put_rtab(R_tab);
184 new->rate_present = false;
187 new->peak_present = true;
188 prate64 = tb[TCA_POLICE_PEAKRATE64] ?
189 nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
190 psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
191 qdisc_put_rtab(P_tab);
193 new->peak_present = false;
196 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
197 if (new->peak_present)
198 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
201 if (tb[TCA_POLICE_AVRATE])
202 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
204 if (tb[TCA_POLICE_PKTRATE64]) {
205 pps = nla_get_u64(tb[TCA_POLICE_PKTRATE64]);
206 ppsburst = nla_get_u64(tb[TCA_POLICE_PKTBURST64]);
207 new->pps_present = true;
208 new->tcfp_pkt_burst = PSCHED_TICKS2NS(ppsburst);
209 psched_ppscfg_precompute(&new->ppsrate, pps);
212 spin_lock_bh(&police->tcf_lock);
213 spin_lock_bh(&police->tcfp_lock);
214 police->tcfp_t_c = ktime_get_ns();
215 police->tcfp_toks = new->tcfp_burst;
216 if (new->peak_present)
217 police->tcfp_ptoks = new->tcfp_mtu_ptoks;
218 spin_unlock_bh(&police->tcfp_lock);
219 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
220 new = rcu_replace_pointer(police->params,
222 lockdep_is_held(&police->tcf_lock));
223 spin_unlock_bh(&police->tcf_lock);
226 tcf_chain_put_by_act(goto_ch);
233 qdisc_put_rtab(P_tab);
234 qdisc_put_rtab(R_tab);
236 tcf_chain_put_by_act(goto_ch);
238 tcf_idr_release(*a, bind);
242 static bool tcf_police_mtu_check(struct sk_buff *skb, u32 limit)
247 return skb_gso_validate_mac_len(skb, limit);
249 len = qdisc_pkt_len(skb);
250 if (skb_at_tc_ingress(skb))
256 static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
257 struct tcf_result *res)
259 struct tcf_police *police = to_police(a);
260 s64 now, toks, ppstoks = 0, ptoks = 0;
261 struct tcf_police_params *p;
264 tcf_lastuse_update(&police->tcf_tm);
265 bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb);
267 ret = READ_ONCE(police->tcf_action);
268 p = rcu_dereference_bh(police->params);
270 if (p->tcfp_ewma_rate) {
271 struct gnet_stats_rate_est64 sample;
273 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
274 sample.bps >= p->tcfp_ewma_rate)
278 if (tcf_police_mtu_check(skb, p->tcfp_mtu)) {
279 if (!p->rate_present && !p->pps_present) {
280 ret = p->tcfp_result;
284 now = ktime_get_ns();
285 spin_lock_bh(&police->tcfp_lock);
286 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
287 if (p->peak_present) {
288 ptoks = toks + police->tcfp_ptoks;
289 if (ptoks > p->tcfp_mtu_ptoks)
290 ptoks = p->tcfp_mtu_ptoks;
291 ptoks -= (s64)psched_l2t_ns(&p->peak,
294 if (p->rate_present) {
295 toks += police->tcfp_toks;
296 if (toks > p->tcfp_burst)
297 toks = p->tcfp_burst;
298 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
299 } else if (p->pps_present) {
300 ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst);
301 ppstoks += police->tcfp_pkttoks;
302 if (ppstoks > p->tcfp_pkt_burst)
303 ppstoks = p->tcfp_pkt_burst;
304 ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1);
306 if ((toks | ptoks | ppstoks) >= 0) {
307 police->tcfp_t_c = now;
308 police->tcfp_toks = toks;
309 police->tcfp_ptoks = ptoks;
310 police->tcfp_pkttoks = ppstoks;
311 spin_unlock_bh(&police->tcfp_lock);
312 ret = p->tcfp_result;
315 spin_unlock_bh(&police->tcfp_lock);
319 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
321 if (ret == TC_ACT_SHOT)
322 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
327 static void tcf_police_cleanup(struct tc_action *a)
329 struct tcf_police *police = to_police(a);
330 struct tcf_police_params *p;
332 p = rcu_dereference_protected(police->params, 1);
337 static void tcf_police_stats_update(struct tc_action *a,
338 u64 bytes, u64 packets, u64 drops,
339 u64 lastuse, bool hw)
341 struct tcf_police *police = to_police(a);
342 struct tcf_t *tm = &police->tcf_tm;
344 tcf_action_update_stats(a, bytes, packets, drops, hw);
345 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
348 static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
351 unsigned char *b = skb_tail_pointer(skb);
352 struct tcf_police *police = to_police(a);
353 struct tcf_police_params *p;
354 struct tc_police opt = {
355 .index = police->tcf_index,
356 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
357 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
361 spin_lock_bh(&police->tcf_lock);
362 opt.action = police->tcf_action;
363 p = rcu_dereference_protected(police->params,
364 lockdep_is_held(&police->tcf_lock));
365 opt.mtu = p->tcfp_mtu;
366 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
367 if (p->rate_present) {
368 psched_ratecfg_getrate(&opt.rate, &p->rate);
369 if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) &&
370 nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
371 police->params->rate.rate_bytes_ps,
373 goto nla_put_failure;
375 if (p->peak_present) {
376 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
377 if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) &&
378 nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
379 police->params->peak.rate_bytes_ps,
381 goto nla_put_failure;
383 if (p->pps_present) {
384 if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64,
385 police->params->ppsrate.rate_pkts_ps,
387 goto nla_put_failure;
388 if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64,
389 PSCHED_NS2TICKS(p->tcfp_pkt_burst),
391 goto nla_put_failure;
393 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
394 goto nla_put_failure;
395 if (p->tcfp_result &&
396 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
397 goto nla_put_failure;
398 if (p->tcfp_ewma_rate &&
399 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
400 goto nla_put_failure;
402 tcf_tm_dump(&t, &police->tcf_tm);
403 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
404 goto nla_put_failure;
405 spin_unlock_bh(&police->tcf_lock);
410 spin_unlock_bh(&police->tcf_lock);
415 static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
417 struct tc_action_net *tn = net_generic(net, police_net_id);
419 return tcf_idr_search(tn, a, index);
422 static int tcf_police_act_to_flow_act(int tc_act, u32 *extval)
424 int act_id = -EOPNOTSUPP;
426 if (!TC_ACT_EXT_OPCODE(tc_act)) {
427 if (tc_act == TC_ACT_OK)
428 act_id = FLOW_ACTION_ACCEPT;
429 else if (tc_act == TC_ACT_SHOT)
430 act_id = FLOW_ACTION_DROP;
431 else if (tc_act == TC_ACT_PIPE)
432 act_id = FLOW_ACTION_PIPE;
433 } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_GOTO_CHAIN)) {
434 act_id = FLOW_ACTION_GOTO;
435 *extval = tc_act & TC_ACT_EXT_VAL_MASK;
436 } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_JUMP)) {
437 act_id = FLOW_ACTION_JUMP;
438 *extval = tc_act & TC_ACT_EXT_VAL_MASK;
444 static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data,
445 u32 *index_inc, bool bind)
448 struct flow_action_entry *entry = entry_data;
449 struct tcf_police *police = to_police(act);
450 struct tcf_police_params *p;
453 p = rcu_dereference_protected(police->params,
454 lockdep_is_held(&police->tcf_lock));
456 entry->id = FLOW_ACTION_POLICE;
457 entry->police.burst = tcf_police_burst(act);
458 entry->police.rate_bytes_ps =
459 tcf_police_rate_bytes_ps(act);
460 entry->police.peakrate_bytes_ps = tcf_police_peakrate_bytes_ps(act);
461 entry->police.avrate = tcf_police_tcfp_ewma_rate(act);
462 entry->police.overhead = tcf_police_rate_overhead(act);
463 entry->police.burst_pkt = tcf_police_burst_pkt(act);
464 entry->police.rate_pkt_ps =
465 tcf_police_rate_pkt_ps(act);
466 entry->police.mtu = tcf_police_tcfp_mtu(act);
468 act_id = tcf_police_act_to_flow_act(police->tcf_action,
469 &entry->police.exceed.extval);
473 entry->police.exceed.act_id = act_id;
475 act_id = tcf_police_act_to_flow_act(p->tcfp_result,
476 &entry->police.notexceed.extval);
480 entry->police.notexceed.act_id = act_id;
484 struct flow_offload_action *fl_action = entry_data;
486 fl_action->id = FLOW_ACTION_POLICE;
492 MODULE_AUTHOR("Alexey Kuznetsov");
493 MODULE_DESCRIPTION("Policing actions");
494 MODULE_LICENSE("GPL");
496 static struct tc_action_ops act_police_ops = {
499 .owner = THIS_MODULE,
500 .stats_update = tcf_police_stats_update,
501 .act = tcf_police_act,
502 .dump = tcf_police_dump,
503 .init = tcf_police_init,
504 .walk = tcf_police_walker,
505 .lookup = tcf_police_search,
506 .cleanup = tcf_police_cleanup,
507 .offload_act_setup = tcf_police_offload_act_setup,
508 .size = sizeof(struct tcf_police),
511 static __net_init int police_init_net(struct net *net)
513 struct tc_action_net *tn = net_generic(net, police_net_id);
515 return tc_action_net_init(net, tn, &act_police_ops);
518 static void __net_exit police_exit_net(struct list_head *net_list)
520 tc_action_net_exit(net_list, police_net_id);
523 static struct pernet_operations police_net_ops = {
524 .init = police_init_net,
525 .exit_batch = police_exit_net,
526 .id = &police_net_id,
527 .size = sizeof(struct tc_action_net),
530 static int __init police_init_module(void)
532 return tcf_register_action(&act_police_ops, &police_net_ops);
535 static void __exit police_cleanup_module(void)
537 tcf_unregister_action(&act_police_ops, &police_net_ops);
540 module_init(police_init_module);
541 module_exit(police_cleanup_module);