blob: 0923aa2b8f8a7137f3c15083ced5b3d0640910e1 [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>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070019#include <net/netlink.h>
Davide Carattid6124d62019-03-20 15:00:08 +010020#include <net/pkt_cls.h>
Pieter Jansen van Vuurenfa762da2019-05-04 04:46:21 -070021#include <net/tc_act/tc_police.h>
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* Each policer is serialized by its individual spinlock */
24
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030025static unsigned int police_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070026static struct tc_action_ops act_police_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080027
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040028static int tcf_police_walker(struct net *net, struct sk_buff *skb,
WANG Congddf97cc2016-02-22 15:57:53 -080029 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -050030 const struct tc_action_ops *ops,
31 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
WANG Congddf97cc2016-02-22 15:57:53 -080033 struct tc_action_net *tn = net_generic(net, police_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Alexander Aringb3620142018-02-15 10:54:59 -050035 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Patrick McHardy53b2bf32008-01-23 20:36:30 -080038static 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 },
David Daid1967e42019-09-04 10:03:43 -050043 [TCA_POLICE_RATE64] = { .type = NLA_U64 },
44 [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
Baowen Zheng2ffe0392021-03-12 15:08:31 +010045 [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 },
46 [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 },
Patrick McHardy53b2bf32008-01-23 20:36:30 -080047};
48
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040049static int tcf_police_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070050 struct nlattr *est, struct tc_action **a,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020051 struct tcf_proto *tp, u32 flags,
Alexander Aring589dad62018-02-15 10:54:56 -050052 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Davide Carattifd6d4332018-11-28 18:43:42 +010054 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
Cong Wang695176b2021-07-29 16:12:14 -070055 bool bind = flags & TCA_ACT_FLAGS_BIND;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080056 struct nlattr *tb[TCA_POLICE_MAX + 1];
Davide Carattid6124d62019-03-20 15:00:08 +010057 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070059 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
WANG Congddf97cc2016-02-22 15:57:53 -080061 struct tc_action_net *tn = net_generic(net, police_net_id);
Davide Caratti2d550db2018-09-13 19:29:13 +020062 struct tcf_police_params *new;
WANG Cong0852e452016-08-13 22:35:01 -070063 bool exists = false;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000064 u32 index;
David Daid1967e42019-09-04 10:03:43 -050065 u64 rate64, prate64;
Baowen Zheng2ffe0392021-03-12 15:08:31 +010066 u64 pps, ppsburst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Patrick McHardycee63722008-01-23 20:33:32 -080068 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -EINVAL;
70
Johannes Berg8cb08172019-04-26 14:07:28 +020071 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
72 police_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080073 if (err < 0)
74 return err;
75
Patrick McHardy7ba699c2008-01-22 22:11:50 -080076 if (tb[TCA_POLICE_TBF] == NULL)
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080077 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080078 size = nla_len(tb[TCA_POLICE_TBF]);
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080079 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
WANG Cong0852e452016-08-13 22:35:01 -070082 parm = nla_data(tb[TCA_POLICE_TBF]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000083 index = parm->index;
84 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030085 if (err < 0)
86 return err;
87 exists = err;
WANG Cong0852e452016-08-13 22:35:01 -070088 if (exists && bind)
89 return 0;
90
91 if (!exists) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000092 ret = tcf_idr_create(tn, index, NULL, a,
Baowen Zheng40bd0942021-12-17 19:16:17 +010093 &act_police_ops, bind, true, flags);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030094 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000095 tcf_idr_cleanup(tn, index);
WANG Conga03e6fe2016-06-06 09:54:30 -070096 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030097 }
WANG Conga03e6fe2016-06-06 09:54:30 -070098 ret = ACT_P_CREATED;
Davide Caratti484afd12018-11-21 18:23:53 +010099 spin_lock_init(&(to_police(*a)->tcfp_lock));
Cong Wang695176b2021-07-29 16:12:14 -0700100 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
Chris Mi65a206c2017-08-30 02:31:59 -0400101 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300102 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Davide Carattid6124d62019-03-20 15:00:08 +0100104 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
105 if (err < 0)
106 goto release_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
WANG Conga85a9702016-07-25 16:09:41 -0700108 police = to_police(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (parm->rate.rate) {
110 err = -ENOMEM;
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500111 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (R_tab == NULL)
113 goto failure;
Stephen Hemmingerc1b56872008-11-25 21:14:06 -0800114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (parm->peakrate.rate) {
116 P_tab = qdisc_get_rtab(&parm->peakrate,
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500117 tb[TCA_POLICE_PEAKRATE], NULL);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800118 if (P_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800122
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800123 if (est) {
Davide Caratti93be42f2018-09-13 19:29:12 +0200124 err = gen_replace_estimator(&police->tcf_bstats,
125 police->common.cpu_bstats,
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800126 &police->tcf_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700127 &police->tcf_lock,
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +0200128 false, est);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800129 if (err)
WANG Cong74030602017-06-13 13:36:24 -0700130 goto failure;
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800131 } else if (tb[TCA_POLICE_AVRATE] &&
132 (ret == ACT_P_CREATED ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800133 !gen_estimator_active(&police->tcf_rate_est))) {
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800134 err = -EINVAL;
WANG Cong74030602017-06-13 13:36:24 -0700135 goto failure;
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800136 }
137
Davide Carattifd6d4332018-11-28 18:43:42 +0100138 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");
143 err = -EINVAL;
144 goto failure;
145 }
146 }
147
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100148 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");
152 err = -EINVAL;
153 goto failure;
154 }
155
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");
159 err = -EINVAL;
160 goto failure;
161 }
162
Davide Caratti2d550db2018-09-13 19:29:13 +0200163 new = kzalloc(sizeof(*new), GFP_KERNEL);
164 if (unlikely(!new)) {
165 err = -ENOMEM;
166 goto failure;
167 }
168
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800169 /* No failure allowed after this point */
Davide Carattifd6d4332018-11-28 18:43:42 +0100170 new->tcfp_result = tcfp_result;
Davide Caratti2d550db2018-09-13 19:29:13 +0200171 new->tcfp_mtu = parm->mtu;
172 if (!new->tcfp_mtu) {
173 new->tcfp_mtu = ~0;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000174 if (R_tab)
Davide Caratti2d550db2018-09-13 19:29:13 +0200175 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000177 if (R_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200178 new->rate_present = true;
David Daid1967e42019-09-04 10:03:43 -0500179 rate64 = tb[TCA_POLICE_RATE64] ?
180 nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
181 psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000182 qdisc_put_rtab(R_tab);
183 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200184 new->rate_present = false;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000185 }
186 if (P_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200187 new->peak_present = true;
David Daid1967e42019-09-04 10:03:43 -0500188 prate64 = tb[TCA_POLICE_PEAKRATE64] ?
189 nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
190 psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000191 qdisc_put_rtab(P_tab);
192 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200193 new->peak_present = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
Davide Caratti2d550db2018-09-13 19:29:13 +0200196 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
Davide Carattif2cbd482018-11-20 22:18:44 +0100197 if (new->peak_present)
Davide Caratti2d550db2018-09-13 19:29:13 +0200198 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
199 new->tcfp_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800201 if (tb[TCA_POLICE_AVRATE])
Davide Caratti2d550db2018-09-13 19:29:13 +0200202 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100204 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);
210 }
211
Davide Caratti2d550db2018-09-13 19:29:13 +0200212 spin_lock_bh(&police->tcf_lock);
Davide Carattif2cbd482018-11-20 22:18:44 +0100213 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);
Davide Carattid6124d62019-03-20 15:00:08 +0100219 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Paul E. McKenney445d3742019-09-23 16:09:18 -0700220 new = rcu_replace_pointer(police->params,
221 new,
222 lockdep_is_held(&police->tcf_lock));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Davide Carattid6124d62019-03-20 15:00:08 +0100225 if (goto_ch)
226 tcf_chain_put_by_act(goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200227 if (new)
228 kfree_rcu(new, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return ret;
231
232failure:
Yang Yingliang3b69a4c2013-12-17 15:29:16 +0800233 qdisc_put_rtab(P_tab);
234 qdisc_put_rtab(R_tab);
Davide Carattid6124d62019-03-20 15:00:08 +0100235 if (goto_ch)
236 tcf_chain_put_by_act(goto_ch);
237release_idr:
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300238 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return err;
240}
241
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400242static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900243 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
WANG Conga85a9702016-07-25 16:09:41 -0700245 struct tcf_police *police = to_police(a);
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100246 s64 now, toks, ppstoks = 0, ptoks = 0;
Davide Caratti2d550db2018-09-13 19:29:13 +0200247 struct tcf_police_params *p;
Davide Caratti93be42f2018-09-13 19:29:12 +0200248 int ret;
249
250 tcf_lastuse_update(&police->tcf_tm);
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200251 bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Davide Caratti2d550db2018-09-13 19:29:13 +0200253 ret = READ_ONCE(police->tcf_action);
254 p = rcu_dereference_bh(police->params);
255
256 if (p->tcfp_ewma_rate) {
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800257 struct gnet_stats_rate_est64 sample;
258
259 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
Davide Caratti2d550db2018-09-13 19:29:13 +0200260 sample.bps >= p->tcfp_ewma_rate)
Davide Caratti93be42f2018-09-13 19:29:12 +0200261 goto inc_overlimits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Davide Caratti2d550db2018-09-13 19:29:13 +0200264 if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100265 if (!p->rate_present && !p->pps_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200266 ret = p->tcfp_result;
267 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Eric Dumazetd2de8752014-08-22 18:32:09 -0700270 now = ktime_get_ns();
Davide Carattif2cbd482018-11-20 22:18:44 +0100271 spin_lock_bh(&police->tcfp_lock);
272 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
Davide Caratti2d550db2018-09-13 19:29:13 +0200273 if (p->peak_present) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100274 ptoks = toks + police->tcfp_ptoks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200275 if (ptoks > p->tcfp_mtu_ptoks)
276 ptoks = p->tcfp_mtu_ptoks;
277 ptoks -= (s64)psched_l2t_ns(&p->peak,
278 qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100280 if (p->rate_present) {
281 toks += police->tcfp_toks;
282 if (toks > p->tcfp_burst)
283 toks = p->tcfp_burst;
284 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
285 } else if (p->pps_present) {
286 ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst);
287 ppstoks += police->tcfp_pkttoks;
288 if (ppstoks > p->tcfp_pkt_burst)
289 ppstoks = p->tcfp_pkt_burst;
290 ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1);
291 }
292 if ((toks | ptoks | ppstoks) >= 0) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100293 police->tcfp_t_c = now;
294 police->tcfp_toks = toks;
295 police->tcfp_ptoks = ptoks;
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100296 police->tcfp_pkttoks = ppstoks;
Davide Carattif2cbd482018-11-20 22:18:44 +0100297 spin_unlock_bh(&police->tcfp_lock);
Davide Caratti2d550db2018-09-13 19:29:13 +0200298 ret = p->tcfp_result;
Davide Caratti93be42f2018-09-13 19:29:12 +0200299 goto inc_drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100301 spin_unlock_bh(&police->tcfp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Davide Caratti93be42f2018-09-13 19:29:12 +0200304inc_overlimits:
305 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
306inc_drops:
307 if (ret == TC_ACT_SHOT)
308 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
Davide Caratti2d550db2018-09-13 19:29:13 +0200309end:
Davide Caratti93be42f2018-09-13 19:29:12 +0200310 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Davide Caratti2d550db2018-09-13 19:29:13 +0200313static void tcf_police_cleanup(struct tc_action *a)
314{
315 struct tcf_police *police = to_police(a);
316 struct tcf_police_params *p;
317
318 p = rcu_dereference_protected(police->params, 1);
319 if (p)
320 kfree_rcu(p, rcu);
321}
322
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700323static void tcf_police_stats_update(struct tc_action *a,
Po Liu4b61d3e2020-06-19 14:01:07 +0800324 u64 bytes, u64 packets, u64 drops,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700325 u64 lastuse, bool hw)
326{
327 struct tcf_police *police = to_police(a);
328 struct tcf_t *tm = &police->tcf_tm;
329
Po Liu4b61d3e2020-06-19 14:01:07 +0800330 tcf_action_update_stats(a, bytes, packets, drops, hw);
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700331 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
332}
333
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400334static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400335 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700337 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700338 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200339 struct tcf_police_params *p;
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000340 struct tc_police opt = {
341 .index = police->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300342 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
343 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000344 };
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400345 struct tcf_t t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Vlad Buslove329bc42018-08-10 20:51:55 +0300347 spin_lock_bh(&police->tcf_lock);
348 opt.action = police->tcf_action;
Davide Caratti2d550db2018-09-13 19:29:13 +0200349 p = rcu_dereference_protected(police->params,
350 lockdep_is_held(&police->tcf_lock));
351 opt.mtu = p->tcfp_mtu;
352 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
David Daid1967e42019-09-04 10:03:43 -0500353 if (p->rate_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200354 psched_ratecfg_getrate(&opt.rate, &p->rate);
David Daid1967e42019-09-04 10:03:43 -0500355 if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) &&
356 nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
357 police->params->rate.rate_bytes_ps,
358 TCA_POLICE_PAD))
359 goto nla_put_failure;
360 }
361 if (p->peak_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200362 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
David Daid1967e42019-09-04 10:03:43 -0500363 if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) &&
364 nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
365 police->params->peak.rate_bytes_ps,
366 TCA_POLICE_PAD))
367 goto nla_put_failure;
368 }
Baowen Zheng2ffe0392021-03-12 15:08:31 +0100369 if (p->pps_present) {
370 if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64,
371 police->params->ppsrate.rate_pkts_ps,
372 TCA_POLICE_PAD))
373 goto nla_put_failure;
374 if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64,
375 PSCHED_NS2TICKS(p->tcfp_pkt_burst),
376 TCA_POLICE_PAD))
377 goto nla_put_failure;
378 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400379 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
380 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200381 if (p->tcfp_result &&
382 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
David S. Miller1b34ec42012-03-29 05:11:39 -0400383 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200384 if (p->tcfp_ewma_rate &&
385 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
David S. Miller1b34ec42012-03-29 05:11:39 -0400386 goto nla_put_failure;
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400387
Davide Caratti985fd982019-10-19 18:49:32 +0200388 tcf_tm_dump(&t, &police->tcf_tm);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400389 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
390 goto nla_put_failure;
Vlad Buslove329bc42018-08-10 20:51:55 +0300391 spin_unlock_bh(&police->tcf_lock);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return skb->len;
394
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800395nla_put_failure:
Vlad Buslove329bc42018-08-10 20:51:55 +0300396 spin_unlock_bh(&police->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700397 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -1;
399}
400
Cong Wangf061b482018-08-29 10:15:35 -0700401static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800402{
403 struct tc_action_net *tn = net_generic(net, police_net_id);
404
Chris Mi65a206c2017-08-30 02:31:59 -0400405 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800406}
407
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100408static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data,
409 u32 *index_inc, bool bind)
410{
411 if (bind) {
412 struct flow_action_entry *entry = entry_data;
413
414 entry->id = FLOW_ACTION_POLICE;
415 entry->police.burst = tcf_police_burst(act);
416 entry->police.rate_bytes_ps =
417 tcf_police_rate_bytes_ps(act);
418 entry->police.burst_pkt = tcf_police_burst_pkt(act);
419 entry->police.rate_pkt_ps =
420 tcf_police_rate_pkt_ps(act);
421 entry->police.mtu = tcf_police_tcfp_mtu(act);
422 *index_inc = 1;
423 } else {
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100424 struct flow_offload_action *fl_action = entry_data;
425
426 fl_action->id = FLOW_ACTION_POLICE;
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100427 }
428
429 return 0;
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432MODULE_AUTHOR("Alexey Kuznetsov");
433MODULE_DESCRIPTION("Policing actions");
434MODULE_LICENSE("GPL");
435
436static struct tc_action_ops act_police_ops = {
437 .kind = "police",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200438 .id = TCA_ID_POLICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 .owner = THIS_MODULE,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700440 .stats_update = tcf_police_stats_update,
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400441 .act = tcf_police_act,
442 .dump = tcf_police_dump,
443 .init = tcf_police_init,
444 .walk = tcf_police_walker,
WANG Congddf97cc2016-02-22 15:57:53 -0800445 .lookup = tcf_police_search,
Davide Caratti2d550db2018-09-13 19:29:13 +0200446 .cleanup = tcf_police_cleanup,
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100447 .offload_act_setup = tcf_police_offload_act_setup,
WANG Conga85a9702016-07-25 16:09:41 -0700448 .size = sizeof(struct tcf_police),
WANG Congddf97cc2016-02-22 15:57:53 -0800449};
450
451static __net_init int police_init_net(struct net *net)
452{
453 struct tc_action_net *tn = net_generic(net, police_net_id);
454
Cong Wang981471b2019-08-25 10:01:32 -0700455 return tc_action_net_init(net, tn, &act_police_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800456}
457
Cong Wang039af9c2017-12-11 15:35:03 -0800458static void __net_exit police_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800459{
Cong Wang039af9c2017-12-11 15:35:03 -0800460 tc_action_net_exit(net_list, police_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800461}
462
463static struct pernet_operations police_net_ops = {
464 .init = police_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800465 .exit_batch = police_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800466 .id = &police_net_id,
467 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468};
469
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400470static int __init police_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
WANG Congddf97cc2016-02-22 15:57:53 -0800472 return tcf_register_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400475static void __exit police_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
WANG Congddf97cc2016-02-22 15:57:53 -0800477 tcf_unregister_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480module_init(police_init_module);
481module_exit(police_cleanup_module);