blob: 8555125ed34d83eaadd36391da869bd46f2372fe [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01003 * net/sched/act_police.c Input police filter
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 * J Hadi Salim (action changes)
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/rtnetlink.h>
16#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/act_api.h>
Eric Dumazetd457a0e2023-06-08 19:17:37 +000019#include <net/gso.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070020#include <net/netlink.h>
Davide Carattid6124d62019-03-20 15:00:08 +010021#include <net/pkt_cls.h>
Pieter Jansen van Vuurenfa762da2019-05-04 04:46:21 -070022#include <net/tc_act/tc_police.h>
Pedro Tammela871cf382022-12-06 10:55:12 -030023#include <net/tc_wrapper.h>
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Each policer is serialized by its individual spinlock */
26
WANG Conga85a9702016-07-25 16:09:41 -070027static struct tc_action_ops act_police_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080028
Patrick McHardy53b2bf32008-01-23 20:36:30 -080029static 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 Daid1967e42019-09-04 10:03:43 -050034 [TCA_POLICE_RATE64] = { .type = NLA_U64 },
35 [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
Baowen Zheng2ffe0392021-03-12 15:08:31 +010036 [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 },
37 [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 },
Patrick McHardy53b2bf32008-01-23 20:36:30 -080038};
39
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040040static int tcf_police_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070041 struct nlattr *est, struct tc_action **a,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020042 struct tcf_proto *tp, u32 flags,
Alexander Aring589dad62018-02-15 10:54:56 -050043 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Davide Carattifd6d4332018-11-28 18:43:42 +010045 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
Cong Wang695176b2021-07-29 16:12:14 -070046 bool bind = flags & TCA_ACT_FLAGS_BIND;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080047 struct nlattr *tb[TCA_POLICE_MAX + 1];
Davide Carattid6124d62019-03-20 15:00:08 +010048 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070050 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +080052 struct tc_action_net *tn = net_generic(net, act_police_ops.net_id);
Davide Caratti2d550db2018-09-13 19:29:13 +020053 struct tcf_police_params *new;
WANG Cong0852e452016-08-13 22:35:01 -070054 bool exists = false;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000055 u32 index;
David Daid1967e42019-09-04 10:03:43 -050056 u64 rate64, prate64;
Baowen Zheng2ffe0392021-03-12 15:08:31 +010057 u64 pps, ppsburst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Patrick McHardycee63722008-01-23 20:33:32 -080059 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EINVAL;
61
Johannes Berg8cb08172019-04-26 14:07:28 +020062 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
63 police_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080064 if (err < 0)
65 return err;
66
Patrick McHardy7ba699c2008-01-22 22:11:50 -080067 if (tb[TCA_POLICE_TBF] == NULL)
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080068 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080069 size = nla_len(tb[TCA_POLICE_TBF]);
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080070 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
WANG Cong0852e452016-08-13 22:35:01 -070073 parm = nla_data(tb[TCA_POLICE_TBF]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000074 index = parm->index;
75 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030076 if (err < 0)
77 return err;
78 exists = err;
WANG Cong0852e452016-08-13 22:35:01 -070079 if (exists && bind)
Pedro Tammelac2a67de92023-12-29 10:26:41 -030080 return ACT_P_BOUND;
WANG Cong0852e452016-08-13 22:35:01 -070081
82 if (!exists) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000083 ret = tcf_idr_create(tn, index, NULL, a,
Baowen Zheng40bd0942021-12-17 19:16:17 +010084 &act_police_ops, bind, true, flags);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030085 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000086 tcf_idr_cleanup(tn, index);
WANG Conga03e6fe2016-06-06 09:54:30 -070087 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030088 }
WANG Conga03e6fe2016-06-06 09:54:30 -070089 ret = ACT_P_CREATED;
Davide Caratti484afd12018-11-21 18:23:53 +010090 spin_lock_init(&(to_police(*a)->tcfp_lock));
Cong Wang695176b2021-07-29 16:12:14 -070091 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
Chris Mi65a206c2017-08-30 02:31:59 -040092 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030093 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Davide Carattid6124d62019-03-20 15:00:08 +010095 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
96 if (err < 0)
97 goto release_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
WANG Conga85a9702016-07-25 16:09:41 -070099 police = to_police(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (parm->rate.rate) {
101 err = -ENOMEM;
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500102 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (R_tab == NULL)
104 goto failure;
Stephen Hemmingerc1b56872008-11-25 21:14:06 -0800105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (parm->peakrate.rate) {
107 P_tab = qdisc_get_rtab(&parm->peakrate,
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500108 tb[TCA_POLICE_PEAKRATE], NULL);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800109 if (P_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800113
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800114 if (est) {
Davide Caratti93be42f2018-09-13 19:29:12 +0200115 err = gen_replace_estimator(&police->tcf_bstats,
116 police->common.cpu_bstats,
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800117 &police->tcf_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700118 &police->tcf_lock,
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +0200119 false, est);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800120 if (err)
WANG Cong74030602017-06-13 13:36:24 -0700121 goto failure;
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800122 } else if (tb[TCA_POLICE_AVRATE] &&
123 (ret == ACT_P_CREATED ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800124 !gen_estimator_active(&police->tcf_rate_est))) {
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800125 err = -EINVAL;
WANG Cong74030602017-06-13 13:36:24 -0700126 goto failure;
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800127 }
128
Davide Carattifd6d4332018-11-28 18:43:42 +0100129 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 Zheng2ffe0392021-03-12 15:08:31 +0100139 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 Caratti2d550db2018-09-13 19:29:13 +0200154 new = kzalloc(sizeof(*new), GFP_KERNEL);
155 if (unlikely(!new)) {
156 err = -ENOMEM;
157 goto failure;
158 }
159
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800160 /* No failure allowed after this point */
Davide Carattifd6d4332018-11-28 18:43:42 +0100161 new->tcfp_result = tcfp_result;
Davide Caratti2d550db2018-09-13 19:29:13 +0200162 new->tcfp_mtu = parm->mtu;
163 if (!new->tcfp_mtu) {
164 new->tcfp_mtu = ~0;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000165 if (R_tab)
Davide Caratti2d550db2018-09-13 19:29:13 +0200166 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000168 if (R_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200169 new->rate_present = true;
David Daid1967e42019-09-04 10:03:43 -0500170 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 Pirkoc6d14ff2013-02-12 00:12:07 +0000173 qdisc_put_rtab(R_tab);
174 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200175 new->rate_present = false;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000176 }
177 if (P_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200178 new->peak_present = true;
David Daid1967e42019-09-04 10:03:43 -0500179 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 Pirkoc6d14ff2013-02-12 00:12:07 +0000182 qdisc_put_rtab(P_tab);
183 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200184 new->peak_present = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Davide Caratti2d550db2018-09-13 19:29:13 +0200187 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
Davide Carattif2cbd482018-11-20 22:18:44 +0100188 if (new->peak_present)
Davide Caratti2d550db2018-09-13 19:29:13 +0200189 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
190 new->tcfp_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800192 if (tb[TCA_POLICE_AVRATE])
Davide Caratti2d550db2018-09-13 19:29:13 +0200193 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100195 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 Caratti2d550db2018-09-13 19:29:13 +0200203 spin_lock_bh(&police->tcf_lock);
Davide Carattif2cbd482018-11-20 22:18:44 +0100204 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 Carattid6124d62019-03-20 15:00:08 +0100210 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Paul E. McKenney445d3742019-09-23 16:09:18 -0700211 new = rcu_replace_pointer(police->params,
212 new,
213 lockdep_is_held(&police->tcf_lock));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Davide Carattid6124d62019-03-20 15:00:08 +0100216 if (goto_ch)
217 tcf_chain_put_by_act(goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200218 if (new)
219 kfree_rcu(new, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return ret;
222
223failure:
Yang Yingliang3b69a4c2013-12-17 15:29:16 +0800224 qdisc_put_rtab(P_tab);
225 qdisc_put_rtab(R_tab);
Davide Carattid6124d62019-03-20 15:00:08 +0100226 if (goto_ch)
227 tcf_chain_put_by_act(goto_ch);
228release_idr:
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300229 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return err;
231}
232
Davide Caratti4ddc8442022-02-10 18:56:08 +0100233static 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 Tammela871cf382022-12-06 10:55:12 -0300247TC_INDIRECT_SCOPE int tcf_police_act(struct sk_buff *skb,
248 const struct tc_action *a,
249 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
WANG Conga85a9702016-07-25 16:09:41 -0700251 struct tcf_police *police = to_police(a);
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100252 s64 now, toks, ppstoks = 0, ptoks = 0;
Davide Caratti2d550db2018-09-13 19:29:13 +0200253 struct tcf_police_params *p;
Davide Caratti93be42f2018-09-13 19:29:12 +0200254 int ret;
255
256 tcf_lastuse_update(&police->tcf_tm);
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200257 bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Davide Caratti2d550db2018-09-13 19:29:13 +0200259 ret = READ_ONCE(police->tcf_action);
260 p = rcu_dereference_bh(police->params);
261
262 if (p->tcfp_ewma_rate) {
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800263 struct gnet_stats_rate_est64 sample;
264
265 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
Davide Caratti2d550db2018-09-13 19:29:13 +0200266 sample.bps >= p->tcfp_ewma_rate)
Davide Caratti93be42f2018-09-13 19:29:12 +0200267 goto inc_overlimits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Davide Caratti4ddc8442022-02-10 18:56:08 +0100270 if (tcf_police_mtu_check(skb, p->tcfp_mtu)) {
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100271 if (!p->rate_present && !p->pps_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200272 ret = p->tcfp_result;
273 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Eric Dumazetd2de8752014-08-22 18:32:09 -0700276 now = ktime_get_ns();
Davide Carattif2cbd482018-11-20 22:18:44 +0100277 spin_lock_bh(&police->tcfp_lock);
278 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
Davide Caratti2d550db2018-09-13 19:29:13 +0200279 if (p->peak_present) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100280 ptoks = toks + police->tcfp_ptoks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200281 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 Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100286 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 Carattif2cbd482018-11-20 22:18:44 +0100299 police->tcfp_t_c = now;
300 police->tcfp_toks = toks;
301 police->tcfp_ptoks = ptoks;
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100302 police->tcfp_pkttoks = ppstoks;
Davide Carattif2cbd482018-11-20 22:18:44 +0100303 spin_unlock_bh(&police->tcfp_lock);
Davide Caratti2d550db2018-09-13 19:29:13 +0200304 ret = p->tcfp_result;
Davide Caratti93be42f2018-09-13 19:29:12 +0200305 goto inc_drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100307 spin_unlock_bh(&police->tcfp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
Davide Caratti93be42f2018-09-13 19:29:12 +0200310inc_overlimits:
311 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
312inc_drops:
313 if (ret == TC_ACT_SHOT)
314 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
Davide Caratti2d550db2018-09-13 19:29:13 +0200315end:
Davide Caratti93be42f2018-09-13 19:29:12 +0200316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Davide Caratti2d550db2018-09-13 19:29:13 +0200319static 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 Vuuren12f02b62019-05-04 04:46:24 -0700329static void tcf_police_stats_update(struct tc_action *a,
Po Liu4b61d3e2020-06-19 14:01:07 +0800330 u64 bytes, u64 packets, u64 drops,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700331 u64 lastuse, bool hw)
332{
333 struct tcf_police *police = to_police(a);
334 struct tcf_t *tm = &police->tcf_tm;
335
Po Liu4b61d3e2020-06-19 14:01:07 +0800336 tcf_action_update_stats(a, bytes, packets, drops, hw);
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700337 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
338}
339
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400340static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400341 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700343 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700344 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200345 struct tcf_police_params *p;
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000346 struct tc_police opt = {
347 .index = police->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300348 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
349 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000350 };
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400351 struct tcf_t t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Vlad Buslove329bc42018-08-10 20:51:55 +0300353 spin_lock_bh(&police->tcf_lock);
354 opt.action = police->tcf_action;
Davide Caratti2d550db2018-09-13 19:29:13 +0200355 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 Daid1967e42019-09-04 10:03:43 -0500359 if (p->rate_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200360 psched_ratecfg_getrate(&opt.rate, &p->rate);
Eric Dumazet682881e2023-06-06 13:13:04 +0000361 if ((p->rate.rate_bytes_ps >= (1ULL << 32)) &&
David Daid1967e42019-09-04 10:03:43 -0500362 nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
Eric Dumazet682881e2023-06-06 13:13:04 +0000363 p->rate.rate_bytes_ps,
David Daid1967e42019-09-04 10:03:43 -0500364 TCA_POLICE_PAD))
365 goto nla_put_failure;
366 }
367 if (p->peak_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200368 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
Eric Dumazet682881e2023-06-06 13:13:04 +0000369 if ((p->peak.rate_bytes_ps >= (1ULL << 32)) &&
David Daid1967e42019-09-04 10:03:43 -0500370 nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
Eric Dumazet682881e2023-06-06 13:13:04 +0000371 p->peak.rate_bytes_ps,
David Daid1967e42019-09-04 10:03:43 -0500372 TCA_POLICE_PAD))
373 goto nla_put_failure;
374 }
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100375 if (p->pps_present) {
376 if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64,
Eric Dumazet682881e2023-06-06 13:13:04 +0000377 p->ppsrate.rate_pkts_ps,
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100378 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. Miller1b34ec42012-03-29 05:11:39 -0400385 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
386 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200387 if (p->tcfp_result &&
388 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
David S. Miller1b34ec42012-03-29 05:11:39 -0400389 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200390 if (p->tcfp_ewma_rate &&
391 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
David S. Miller1b34ec42012-03-29 05:11:39 -0400392 goto nla_put_failure;
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400393
Davide Caratti985fd982019-10-19 18:49:32 +0200394 tcf_tm_dump(&t, &police->tcf_tm);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400395 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
396 goto nla_put_failure;
Vlad Buslove329bc42018-08-10 20:51:55 +0300397 spin_unlock_bh(&police->tcf_lock);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return skb->len;
400
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800401nla_put_failure:
Vlad Buslove329bc42018-08-10 20:51:55 +0300402 spin_unlock_bh(&police->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700403 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -1;
405}
406
Ido Schimmelb50e4622022-04-07 10:35:27 +0300407static int tcf_police_act_to_flow_act(int tc_act, u32 *extval,
408 struct netlink_ext_ack *extack)
Jianbo Liub8cd5832022-02-24 10:29:07 +0000409{
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 Schimmelb50e4622022-04-07 10:35:27 +0300419 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 Liub8cd5832022-02-24 10:29:07 +0000423 } 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 Schimmelb50e4622022-04-07 10:35:27 +0300429 } else if (tc_act == TC_ACT_UNSPEC) {
Vlad Buslov052f7442022-07-04 22:44:04 +0200430 act_id = FLOW_ACTION_CONTINUE;
Ido Schimmelb50e4622022-04-07 10:35:27 +0300431 } else {
432 NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
Jianbo Liub8cd5832022-02-24 10:29:07 +0000433 }
434
435 return act_id;
436}
437
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100438static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data,
Ido Schimmelc2ccf84e2022-04-07 10:35:22 +0300439 u32 *index_inc, bool bind,
440 struct netlink_ext_ack *extack)
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100441{
442 if (bind) {
443 struct flow_action_entry *entry = entry_data;
Jianbo Liub8cd5832022-02-24 10:29:07 +0000444 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 Zhengc54e1d92021-12-17 19:16:21 +0100450
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 Liub8cd5832022-02-24 10:29:07 +0000455 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 Zhengc54e1d92021-12-17 19:16:21 +0100458 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 Liub8cd5832022-02-24 10:29:07 +0000462
463 act_id = tcf_police_act_to_flow_act(police->tcf_action,
Ido Schimmelb50e4622022-04-07 10:35:27 +0300464 &entry->police.exceed.extval,
465 extack);
Jianbo Liub8cd5832022-02-24 10:29:07 +0000466 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 Schimmelb50e4622022-04-07 10:35:27 +0300472 &entry->police.notexceed.extval,
473 extack);
Jianbo Liub8cd5832022-02-24 10:29:07 +0000474 if (act_id < 0)
475 return act_id;
476
477 entry->police.notexceed.act_id = act_id;
478
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100479 *index_inc = 1;
480 } else {
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100481 struct flow_offload_action *fl_action = entry_data;
482
483 fl_action->id = FLOW_ACTION_POLICE;
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100484 }
485
486 return 0;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489MODULE_AUTHOR("Alexey Kuznetsov");
490MODULE_DESCRIPTION("Policing actions");
491MODULE_LICENSE("GPL");
492
493static struct tc_action_ops act_police_ops = {
494 .kind = "police",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200495 .id = TCA_ID_POLICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 .owner = THIS_MODULE,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700497 .stats_update = tcf_police_stats_update,
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400498 .act = tcf_police_act,
499 .dump = tcf_police_dump,
500 .init = tcf_police_init,
Davide Caratti2d550db2018-09-13 19:29:13 +0200501 .cleanup = tcf_police_cleanup,
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100502 .offload_act_setup = tcf_police_offload_act_setup,
WANG Conga85a9702016-07-25 16:09:41 -0700503 .size = sizeof(struct tcf_police),
WANG Congddf97cc2016-02-22 15:57:53 -0800504};
Michal Koutný241a94a2024-02-01 14:09:41 +0100505MODULE_ALIAS_NET_ACT("police");
WANG Congddf97cc2016-02-22 15:57:53 -0800506
507static __net_init int police_init_net(struct net *net)
508{
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800509 struct tc_action_net *tn = net_generic(net, act_police_ops.net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800510
Cong Wang981471bd2019-08-25 10:01:32 -0700511 return tc_action_net_init(net, tn, &act_police_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800512}
513
Cong Wang039af9c2017-12-11 15:35:03 -0800514static void __net_exit police_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800515{
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800516 tc_action_net_exit(net_list, act_police_ops.net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800517}
518
519static struct pernet_operations police_net_ops = {
520 .init = police_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800521 .exit_batch = police_exit_net,
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800522 .id = &act_police_ops.net_id,
WANG Congddf97cc2016-02-22 15:57:53 -0800523 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400526static int __init police_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
WANG Congddf97cc2016-02-22 15:57:53 -0800528 return tcf_register_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400531static void __exit police_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
WANG Congddf97cc2016-02-22 15:57:53 -0800533 tcf_unregister_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536module_init(police_init_module);
537module_exit(police_cleanup_module);