Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jiri Pirko | 0c6965d | 2014-11-05 20:51:51 +0100 | [diff] [blame] | 3 | * net/sched/act_police.c Input police filter |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 6 | * J Hadi Salim (action changes) |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/rtnetlink.h> |
| 16 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <net/act_api.h> |
Eric Dumazet | d457a0e | 2023-06-08 19:17:37 +0000 | [diff] [blame] | 19 | #include <net/gso.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 20 | #include <net/netlink.h> |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 21 | #include <net/pkt_cls.h> |
Pieter Jansen van Vuuren | fa762da | 2019-05-04 04:46:21 -0700 | [diff] [blame] | 22 | #include <net/tc_act/tc_police.h> |
Pedro Tammela | 871cf38 | 2022-12-06 10:55:12 -0300 | [diff] [blame] | 23 | #include <net/tc_wrapper.h> |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | /* Each policer is serialized by its individual spinlock */ |
| 26 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 27 | static struct tc_action_ops act_police_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 28 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 29 | static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = { |
| 30 | [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE }, |
| 31 | [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE }, |
| 32 | [TCA_POLICE_AVRATE] = { .type = NLA_U32 }, |
| 33 | [TCA_POLICE_RESULT] = { .type = NLA_U32 }, |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 34 | [TCA_POLICE_RATE64] = { .type = NLA_U64 }, |
| 35 | [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 }, |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 36 | [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 }, |
| 37 | [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 }, |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 40 | static int tcf_police_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 41 | struct nlattr *est, struct tc_action **a, |
Vlad Buslov | abbb0d3 | 2019-10-30 16:09:05 +0200 | [diff] [blame] | 42 | struct tcf_proto *tp, u32 flags, |
Alexander Aring | 589dad6 | 2018-02-15 10:54:56 -0500 | [diff] [blame] | 43 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Davide Caratti | fd6d433 | 2018-11-28 18:43:42 +0100 | [diff] [blame] | 45 | int ret = 0, tcfp_result = TC_ACT_OK, err, size; |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 46 | bool bind = flags & TCA_ACT_FLAGS_BIND; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 47 | struct nlattr *tb[TCA_POLICE_MAX + 1]; |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 48 | struct tcf_chain *goto_ch = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct tc_police *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 50 | struct tcf_police *police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
Zhengchao Shao | acd0a7a | 2022-09-08 12:14:33 +0800 | [diff] [blame] | 52 | struct tc_action_net *tn = net_generic(net, act_police_ops.net_id); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 53 | struct tcf_police_params *new; |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 54 | bool exists = false; |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 55 | u32 index; |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 56 | u64 rate64, prate64; |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 57 | u64 pps, ppsburst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 59 | if (nla == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return -EINVAL; |
| 61 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 62 | err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla, |
| 63 | police_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 64 | if (err < 0) |
| 65 | return err; |
| 66 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 67 | if (tb[TCA_POLICE_TBF] == NULL) |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 68 | return -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 69 | size = nla_len(tb[TCA_POLICE_TBF]); |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 70 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 73 | parm = nla_data(tb[TCA_POLICE_TBF]); |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 74 | index = parm->index; |
| 75 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 76 | if (err < 0) |
| 77 | return err; |
| 78 | exists = err; |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 79 | if (exists && bind) |
Pedro Tammela | c2a67de9 | 2023-12-29 10:26:41 -0300 | [diff] [blame] | 80 | return ACT_P_BOUND; |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 81 | |
| 82 | if (!exists) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 83 | ret = tcf_idr_create(tn, index, NULL, a, |
Baowen Zheng | 40bd094 | 2021-12-17 19:16:17 +0100 | [diff] [blame] | 84 | &act_police_ops, bind, true, flags); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 85 | if (ret) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 86 | tcf_idr_cleanup(tn, index); |
WANG Cong | a03e6fe | 2016-06-06 09:54:30 -0700 | [diff] [blame] | 87 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 88 | } |
WANG Cong | a03e6fe | 2016-06-06 09:54:30 -0700 | [diff] [blame] | 89 | ret = ACT_P_CREATED; |
Davide Caratti | 484afd1 | 2018-11-21 18:23:53 +0100 | [diff] [blame] | 90 | spin_lock_init(&(to_police(*a)->tcfp_lock)); |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 91 | } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 92 | tcf_idr_release(*a, bind); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 93 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 95 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 96 | if (err < 0) |
| 97 | goto release_idr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 99 | police = to_police(*a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (parm->rate.rate) { |
| 101 | err = -ENOMEM; |
Alexander Aring | e9bc3fa | 2017-12-20 12:35:18 -0500 | [diff] [blame] | 102 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (R_tab == NULL) |
| 104 | goto failure; |
Stephen Hemminger | c1b5687 | 2008-11-25 21:14:06 -0800 | [diff] [blame] | 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (parm->peakrate.rate) { |
| 107 | P_tab = qdisc_get_rtab(&parm->peakrate, |
Alexander Aring | e9bc3fa | 2017-12-20 12:35:18 -0500 | [diff] [blame] | 108 | tb[TCA_POLICE_PEAKRATE], NULL); |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 109 | if (P_tab == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | goto failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 113 | |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 114 | if (est) { |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 115 | err = gen_replace_estimator(&police->tcf_bstats, |
| 116 | police->common.cpu_bstats, |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 117 | &police->tcf_rate_est, |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 118 | &police->tcf_lock, |
Ahmed S. Darwish | 29cbcd8 | 2021-10-16 10:49:10 +0200 | [diff] [blame] | 119 | false, est); |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 120 | if (err) |
WANG Cong | 7403060 | 2017-06-13 13:36:24 -0700 | [diff] [blame] | 121 | goto failure; |
Jarek Poplawski | a883bf5 | 2009-03-04 17:38:10 -0800 | [diff] [blame] | 122 | } else if (tb[TCA_POLICE_AVRATE] && |
| 123 | (ret == ACT_P_CREATED || |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 124 | !gen_estimator_active(&police->tcf_rate_est))) { |
Jarek Poplawski | a883bf5 | 2009-03-04 17:38:10 -0800 | [diff] [blame] | 125 | err = -EINVAL; |
WANG Cong | 7403060 | 2017-06-13 13:36:24 -0700 | [diff] [blame] | 126 | goto failure; |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Davide Caratti | fd6d433 | 2018-11-28 18:43:42 +0100 | [diff] [blame] | 129 | if (tb[TCA_POLICE_RESULT]) { |
| 130 | tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]); |
| 131 | if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) { |
| 132 | NL_SET_ERR_MSG(extack, |
| 133 | "goto chain not allowed on fallback"); |
| 134 | err = -EINVAL; |
| 135 | goto failure; |
| 136 | } |
| 137 | } |
| 138 | |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 139 | if ((tb[TCA_POLICE_PKTRATE64] && !tb[TCA_POLICE_PKTBURST64]) || |
| 140 | (!tb[TCA_POLICE_PKTRATE64] && tb[TCA_POLICE_PKTBURST64])) { |
| 141 | NL_SET_ERR_MSG(extack, |
| 142 | "Both or neither packet-per-second burst and rate must be provided"); |
| 143 | err = -EINVAL; |
| 144 | goto failure; |
| 145 | } |
| 146 | |
| 147 | if (tb[TCA_POLICE_PKTRATE64] && R_tab) { |
| 148 | NL_SET_ERR_MSG(extack, |
| 149 | "packet-per-second and byte-per-second rate limits not allowed in same action"); |
| 150 | err = -EINVAL; |
| 151 | goto failure; |
| 152 | } |
| 153 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 154 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 155 | if (unlikely(!new)) { |
| 156 | err = -ENOMEM; |
| 157 | goto failure; |
| 158 | } |
| 159 | |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 160 | /* No failure allowed after this point */ |
Davide Caratti | fd6d433 | 2018-11-28 18:43:42 +0100 | [diff] [blame] | 161 | new->tcfp_result = tcfp_result; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 162 | new->tcfp_mtu = parm->mtu; |
| 163 | if (!new->tcfp_mtu) { |
| 164 | new->tcfp_mtu = ~0; |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 165 | if (R_tab) |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 166 | new->tcfp_mtu = 255 << R_tab->rate.cell_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 168 | if (R_tab) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 169 | new->rate_present = true; |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 170 | rate64 = tb[TCA_POLICE_RATE64] ? |
| 171 | nla_get_u64(tb[TCA_POLICE_RATE64]) : 0; |
| 172 | psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64); |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 173 | qdisc_put_rtab(R_tab); |
| 174 | } else { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 175 | new->rate_present = false; |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 176 | } |
| 177 | if (P_tab) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 178 | new->peak_present = true; |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 179 | prate64 = tb[TCA_POLICE_PEAKRATE64] ? |
| 180 | nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0; |
| 181 | psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64); |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 182 | qdisc_put_rtab(P_tab); |
| 183 | } else { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 184 | new->peak_present = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 187 | new->tcfp_burst = PSCHED_TICKS2NS(parm->burst); |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 188 | if (new->peak_present) |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 189 | new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak, |
| 190 | new->tcfp_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 192 | if (tb[TCA_POLICE_AVRATE]) |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 193 | new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 195 | if (tb[TCA_POLICE_PKTRATE64]) { |
| 196 | pps = nla_get_u64(tb[TCA_POLICE_PKTRATE64]); |
| 197 | ppsburst = nla_get_u64(tb[TCA_POLICE_PKTBURST64]); |
| 198 | new->pps_present = true; |
| 199 | new->tcfp_pkt_burst = PSCHED_TICKS2NS(ppsburst); |
| 200 | psched_ppscfg_precompute(&new->ppsrate, pps); |
| 201 | } |
| 202 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 203 | spin_lock_bh(&police->tcf_lock); |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 204 | spin_lock_bh(&police->tcfp_lock); |
| 205 | police->tcfp_t_c = ktime_get_ns(); |
| 206 | police->tcfp_toks = new->tcfp_burst; |
| 207 | if (new->peak_present) |
| 208 | police->tcfp_ptoks = new->tcfp_mtu_ptoks; |
| 209 | spin_unlock_bh(&police->tcfp_lock); |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 210 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
Paul E. McKenney | 445d374 | 2019-09-23 16:09:18 -0700 | [diff] [blame] | 211 | new = rcu_replace_pointer(police->params, |
| 212 | new, |
| 213 | lockdep_is_held(&police->tcf_lock)); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 214 | spin_unlock_bh(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 216 | if (goto_ch) |
| 217 | tcf_chain_put_by_act(goto_ch); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 218 | if (new) |
| 219 | kfree_rcu(new, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return ret; |
| 222 | |
| 223 | failure: |
Yang Yingliang | 3b69a4c | 2013-12-17 15:29:16 +0800 | [diff] [blame] | 224 | qdisc_put_rtab(P_tab); |
| 225 | qdisc_put_rtab(R_tab); |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 226 | if (goto_ch) |
| 227 | tcf_chain_put_by_act(goto_ch); |
| 228 | release_idr: |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 229 | tcf_idr_release(*a, bind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return err; |
| 231 | } |
| 232 | |
Davide Caratti | 4ddc844 | 2022-02-10 18:56:08 +0100 | [diff] [blame] | 233 | static bool tcf_police_mtu_check(struct sk_buff *skb, u32 limit) |
| 234 | { |
| 235 | u32 len; |
| 236 | |
| 237 | if (skb_is_gso(skb)) |
| 238 | return skb_gso_validate_mac_len(skb, limit); |
| 239 | |
| 240 | len = qdisc_pkt_len(skb); |
| 241 | if (skb_at_tc_ingress(skb)) |
| 242 | len += skb->mac_len; |
| 243 | |
| 244 | return len <= limit; |
| 245 | } |
| 246 | |
Pedro Tammela | 871cf38 | 2022-12-06 10:55:12 -0300 | [diff] [blame] | 247 | TC_INDIRECT_SCOPE int tcf_police_act(struct sk_buff *skb, |
| 248 | const struct tc_action *a, |
| 249 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 251 | struct tcf_police *police = to_police(a); |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 252 | s64 now, toks, ppstoks = 0, ptoks = 0; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 253 | struct tcf_police_params *p; |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 254 | int ret; |
| 255 | |
| 256 | tcf_lastuse_update(&police->tcf_tm); |
Ahmed S. Darwish | 50dc9a8 | 2021-10-16 10:49:09 +0200 | [diff] [blame] | 257 | bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 259 | ret = READ_ONCE(police->tcf_action); |
| 260 | p = rcu_dereference_bh(police->params); |
| 261 | |
| 262 | if (p->tcfp_ewma_rate) { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 263 | struct gnet_stats_rate_est64 sample; |
| 264 | |
| 265 | if (!gen_estimator_read(&police->tcf_rate_est, &sample) || |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 266 | sample.bps >= p->tcfp_ewma_rate) |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 267 | goto inc_overlimits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Davide Caratti | 4ddc844 | 2022-02-10 18:56:08 +0100 | [diff] [blame] | 270 | if (tcf_police_mtu_check(skb, p->tcfp_mtu)) { |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 271 | if (!p->rate_present && !p->pps_present) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 272 | ret = p->tcfp_result; |
| 273 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Eric Dumazet | d2de875 | 2014-08-22 18:32:09 -0700 | [diff] [blame] | 276 | now = ktime_get_ns(); |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 277 | spin_lock_bh(&police->tcfp_lock); |
| 278 | toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 279 | if (p->peak_present) { |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 280 | ptoks = toks + police->tcfp_ptoks; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 281 | if (ptoks > p->tcfp_mtu_ptoks) |
| 282 | ptoks = p->tcfp_mtu_ptoks; |
| 283 | ptoks -= (s64)psched_l2t_ns(&p->peak, |
| 284 | qdisc_pkt_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 286 | if (p->rate_present) { |
| 287 | toks += police->tcfp_toks; |
| 288 | if (toks > p->tcfp_burst) |
| 289 | toks = p->tcfp_burst; |
| 290 | toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb)); |
| 291 | } else if (p->pps_present) { |
| 292 | ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst); |
| 293 | ppstoks += police->tcfp_pkttoks; |
| 294 | if (ppstoks > p->tcfp_pkt_burst) |
| 295 | ppstoks = p->tcfp_pkt_burst; |
| 296 | ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1); |
| 297 | } |
| 298 | if ((toks | ptoks | ppstoks) >= 0) { |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 299 | police->tcfp_t_c = now; |
| 300 | police->tcfp_toks = toks; |
| 301 | police->tcfp_ptoks = ptoks; |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 302 | police->tcfp_pkttoks = ppstoks; |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 303 | spin_unlock_bh(&police->tcfp_lock); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 304 | ret = p->tcfp_result; |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 305 | goto inc_drops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 307 | spin_unlock_bh(&police->tcfp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 310 | inc_overlimits: |
| 311 | qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats)); |
| 312 | inc_drops: |
| 313 | if (ret == TC_ACT_SHOT) |
| 314 | qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats)); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 315 | end: |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 316 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 319 | static void tcf_police_cleanup(struct tc_action *a) |
| 320 | { |
| 321 | struct tcf_police *police = to_police(a); |
| 322 | struct tcf_police_params *p; |
| 323 | |
| 324 | p = rcu_dereference_protected(police->params, 1); |
| 325 | if (p) |
| 326 | kfree_rcu(p, rcu); |
| 327 | } |
| 328 | |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 329 | static void tcf_police_stats_update(struct tc_action *a, |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 330 | u64 bytes, u64 packets, u64 drops, |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 331 | u64 lastuse, bool hw) |
| 332 | { |
| 333 | struct tcf_police *police = to_police(a); |
| 334 | struct tcf_t *tm = &police->tcf_tm; |
| 335 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 336 | tcf_action_update_stats(a, bytes, packets, drops, hw); |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 337 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
| 338 | } |
| 339 | |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 340 | static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 341 | int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 343 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 344 | struct tcf_police *police = to_police(a); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 345 | struct tcf_police_params *p; |
Jeff Mahoney | 0f04cfd | 2010-08-31 13:21:42 +0000 | [diff] [blame] | 346 | struct tc_police opt = { |
| 347 | .index = police->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 348 | .refcnt = refcount_read(&police->tcf_refcnt) - ref, |
| 349 | .bindcnt = atomic_read(&police->tcf_bindcnt) - bind, |
Jeff Mahoney | 0f04cfd | 2010-08-31 13:21:42 +0000 | [diff] [blame] | 350 | }; |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 351 | struct tcf_t t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Vlad Buslov | e329bc4 | 2018-08-10 20:51:55 +0300 | [diff] [blame] | 353 | spin_lock_bh(&police->tcf_lock); |
| 354 | opt.action = police->tcf_action; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 355 | p = rcu_dereference_protected(police->params, |
| 356 | lockdep_is_held(&police->tcf_lock)); |
| 357 | opt.mtu = p->tcfp_mtu; |
| 358 | opt.burst = PSCHED_NS2TICKS(p->tcfp_burst); |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 359 | if (p->rate_present) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 360 | psched_ratecfg_getrate(&opt.rate, &p->rate); |
Eric Dumazet | 682881e | 2023-06-06 13:13:04 +0000 | [diff] [blame] | 361 | if ((p->rate.rate_bytes_ps >= (1ULL << 32)) && |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 362 | nla_put_u64_64bit(skb, TCA_POLICE_RATE64, |
Eric Dumazet | 682881e | 2023-06-06 13:13:04 +0000 | [diff] [blame] | 363 | p->rate.rate_bytes_ps, |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 364 | TCA_POLICE_PAD)) |
| 365 | goto nla_put_failure; |
| 366 | } |
| 367 | if (p->peak_present) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 368 | psched_ratecfg_getrate(&opt.peakrate, &p->peak); |
Eric Dumazet | 682881e | 2023-06-06 13:13:04 +0000 | [diff] [blame] | 369 | if ((p->peak.rate_bytes_ps >= (1ULL << 32)) && |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 370 | nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64, |
Eric Dumazet | 682881e | 2023-06-06 13:13:04 +0000 | [diff] [blame] | 371 | p->peak.rate_bytes_ps, |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 372 | TCA_POLICE_PAD)) |
| 373 | goto nla_put_failure; |
| 374 | } |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 375 | if (p->pps_present) { |
| 376 | if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64, |
Eric Dumazet | 682881e | 2023-06-06 13:13:04 +0000 | [diff] [blame] | 377 | p->ppsrate.rate_pkts_ps, |
Baowen Zheng | 2ffe039 | 2021-03-12 15:08:31 +0100 | [diff] [blame] | 378 | TCA_POLICE_PAD)) |
| 379 | goto nla_put_failure; |
| 380 | if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64, |
| 381 | PSCHED_NS2TICKS(p->tcfp_pkt_burst), |
| 382 | TCA_POLICE_PAD)) |
| 383 | goto nla_put_failure; |
| 384 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 385 | if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) |
| 386 | goto nla_put_failure; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 387 | if (p->tcfp_result && |
| 388 | nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 389 | goto nla_put_failure; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 390 | if (p->tcfp_ewma_rate && |
| 391 | nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 392 | goto nla_put_failure; |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 393 | |
Davide Caratti | 985fd98 | 2019-10-19 18:49:32 +0200 | [diff] [blame] | 394 | tcf_tm_dump(&t, &police->tcf_tm); |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 395 | if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD)) |
| 396 | goto nla_put_failure; |
Vlad Buslov | e329bc4 | 2018-08-10 20:51:55 +0300 | [diff] [blame] | 397 | spin_unlock_bh(&police->tcf_lock); |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return skb->len; |
| 400 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 401 | nla_put_failure: |
Vlad Buslov | e329bc4 | 2018-08-10 20:51:55 +0300 | [diff] [blame] | 402 | spin_unlock_bh(&police->tcf_lock); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 403 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return -1; |
| 405 | } |
| 406 | |
Ido Schimmel | b50e462 | 2022-04-07 10:35:27 +0300 | [diff] [blame] | 407 | static int tcf_police_act_to_flow_act(int tc_act, u32 *extval, |
| 408 | struct netlink_ext_ack *extack) |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 409 | { |
| 410 | int act_id = -EOPNOTSUPP; |
| 411 | |
| 412 | if (!TC_ACT_EXT_OPCODE(tc_act)) { |
| 413 | if (tc_act == TC_ACT_OK) |
| 414 | act_id = FLOW_ACTION_ACCEPT; |
| 415 | else if (tc_act == TC_ACT_SHOT) |
| 416 | act_id = FLOW_ACTION_DROP; |
| 417 | else if (tc_act == TC_ACT_PIPE) |
| 418 | act_id = FLOW_ACTION_PIPE; |
Ido Schimmel | b50e462 | 2022-04-07 10:35:27 +0300 | [diff] [blame] | 419 | else if (tc_act == TC_ACT_RECLASSIFY) |
| 420 | NL_SET_ERR_MSG_MOD(extack, "Offload not supported when conform/exceed action is \"reclassify\""); |
| 421 | else |
| 422 | NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload"); |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 423 | } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_GOTO_CHAIN)) { |
| 424 | act_id = FLOW_ACTION_GOTO; |
| 425 | *extval = tc_act & TC_ACT_EXT_VAL_MASK; |
| 426 | } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_JUMP)) { |
| 427 | act_id = FLOW_ACTION_JUMP; |
| 428 | *extval = tc_act & TC_ACT_EXT_VAL_MASK; |
Ido Schimmel | b50e462 | 2022-04-07 10:35:27 +0300 | [diff] [blame] | 429 | } else if (tc_act == TC_ACT_UNSPEC) { |
Vlad Buslov | 052f744 | 2022-07-04 22:44:04 +0200 | [diff] [blame] | 430 | act_id = FLOW_ACTION_CONTINUE; |
Ido Schimmel | b50e462 | 2022-04-07 10:35:27 +0300 | [diff] [blame] | 431 | } else { |
| 432 | NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload"); |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return act_id; |
| 436 | } |
| 437 | |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 438 | static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data, |
Ido Schimmel | c2ccf84e | 2022-04-07 10:35:22 +0300 | [diff] [blame] | 439 | u32 *index_inc, bool bind, |
| 440 | struct netlink_ext_ack *extack) |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 441 | { |
| 442 | if (bind) { |
| 443 | struct flow_action_entry *entry = entry_data; |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 444 | struct tcf_police *police = to_police(act); |
| 445 | struct tcf_police_params *p; |
| 446 | int act_id; |
| 447 | |
| 448 | p = rcu_dereference_protected(police->params, |
| 449 | lockdep_is_held(&police->tcf_lock)); |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 450 | |
| 451 | entry->id = FLOW_ACTION_POLICE; |
| 452 | entry->police.burst = tcf_police_burst(act); |
| 453 | entry->police.rate_bytes_ps = |
| 454 | tcf_police_rate_bytes_ps(act); |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 455 | entry->police.peakrate_bytes_ps = tcf_police_peakrate_bytes_ps(act); |
| 456 | entry->police.avrate = tcf_police_tcfp_ewma_rate(act); |
| 457 | entry->police.overhead = tcf_police_rate_overhead(act); |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 458 | entry->police.burst_pkt = tcf_police_burst_pkt(act); |
| 459 | entry->police.rate_pkt_ps = |
| 460 | tcf_police_rate_pkt_ps(act); |
| 461 | entry->police.mtu = tcf_police_tcfp_mtu(act); |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 462 | |
| 463 | act_id = tcf_police_act_to_flow_act(police->tcf_action, |
Ido Schimmel | b50e462 | 2022-04-07 10:35:27 +0300 | [diff] [blame] | 464 | &entry->police.exceed.extval, |
| 465 | extack); |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 466 | if (act_id < 0) |
| 467 | return act_id; |
| 468 | |
| 469 | entry->police.exceed.act_id = act_id; |
| 470 | |
| 471 | act_id = tcf_police_act_to_flow_act(p->tcfp_result, |
Ido Schimmel | b50e462 | 2022-04-07 10:35:27 +0300 | [diff] [blame] | 472 | &entry->police.notexceed.extval, |
| 473 | extack); |
Jianbo Liu | b8cd583 | 2022-02-24 10:29:07 +0000 | [diff] [blame] | 474 | if (act_id < 0) |
| 475 | return act_id; |
| 476 | |
| 477 | entry->police.notexceed.act_id = act_id; |
| 478 | |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 479 | *index_inc = 1; |
| 480 | } else { |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 481 | struct flow_offload_action *fl_action = entry_data; |
| 482 | |
| 483 | fl_action->id = FLOW_ACTION_POLICE; |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | MODULE_AUTHOR("Alexey Kuznetsov"); |
| 490 | MODULE_DESCRIPTION("Policing actions"); |
| 491 | MODULE_LICENSE("GPL"); |
| 492 | |
| 493 | static struct tc_action_ops act_police_ops = { |
| 494 | .kind = "police", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 495 | .id = TCA_ID_POLICE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | .owner = THIS_MODULE, |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 497 | .stats_update = tcf_police_stats_update, |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 498 | .act = tcf_police_act, |
| 499 | .dump = tcf_police_dump, |
| 500 | .init = tcf_police_init, |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 501 | .cleanup = tcf_police_cleanup, |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 502 | .offload_act_setup = tcf_police_offload_act_setup, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 503 | .size = sizeof(struct tcf_police), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 504 | }; |
Michal Koutný | 241a94a | 2024-02-01 14:09:41 +0100 | [diff] [blame] | 505 | MODULE_ALIAS_NET_ACT("police"); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 506 | |
| 507 | static __net_init int police_init_net(struct net *net) |
| 508 | { |
Zhengchao Shao | acd0a7a | 2022-09-08 12:14:33 +0800 | [diff] [blame] | 509 | struct tc_action_net *tn = net_generic(net, act_police_ops.net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 510 | |
Cong Wang | 981471bd | 2019-08-25 10:01:32 -0700 | [diff] [blame] | 511 | return tc_action_net_init(net, tn, &act_police_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 514 | static void __net_exit police_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 515 | { |
Zhengchao Shao | acd0a7a | 2022-09-08 12:14:33 +0800 | [diff] [blame] | 516 | tc_action_net_exit(net_list, act_police_ops.net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static struct pernet_operations police_net_ops = { |
| 520 | .init = police_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 521 | .exit_batch = police_exit_net, |
Zhengchao Shao | acd0a7a | 2022-09-08 12:14:33 +0800 | [diff] [blame] | 522 | .id = &act_police_ops.net_id, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 523 | .size = sizeof(struct tc_action_net), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | }; |
| 525 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 526 | static int __init police_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 528 | return tcf_register_action(&act_police_ops, &police_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 531 | static void __exit police_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 533 | tcf_unregister_action(&act_police_ops, &police_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | module_init(police_init_module); |
| 537 | module_exit(police_cleanup_module); |