blob: 0b431d493768614625866fd383dcf59058fa8bbc [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 },
Patrick McHardy53b2bf32008-01-23 20:36:30 -080045};
46
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040047static int tcf_police_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070048 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030049 int ovr, int bind, bool rtnl_held,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020050 struct tcf_proto *tp, u32 flags,
Alexander Aring589dad62018-02-15 10:54:56 -050051 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Davide Carattifd6d4332018-11-28 18:43:42 +010053 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080054 struct nlattr *tb[TCA_POLICE_MAX + 1];
Davide Carattid6124d62019-03-20 15:00:08 +010055 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070057 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
WANG Congddf97cc2016-02-22 15:57:53 -080059 struct tc_action_net *tn = net_generic(net, police_net_id);
Davide Caratti2d550db2018-09-13 19:29:13 +020060 struct tcf_police_params *new;
WANG Cong0852e452016-08-13 22:35:01 -070061 bool exists = false;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000062 u32 index;
David Daid1967e42019-09-04 10:03:43 -050063 u64 rate64, prate64;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Patrick McHardycee63722008-01-23 20:33:32 -080065 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -EINVAL;
67
Johannes Berg8cb08172019-04-26 14:07:28 +020068 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
69 police_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080070 if (err < 0)
71 return err;
72
Patrick McHardy7ba699c2008-01-22 22:11:50 -080073 if (tb[TCA_POLICE_TBF] == NULL)
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080074 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080075 size = nla_len(tb[TCA_POLICE_TBF]);
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080076 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
WANG Cong0852e452016-08-13 22:35:01 -070079 parm = nla_data(tb[TCA_POLICE_TBF]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000080 index = parm->index;
81 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030082 if (err < 0)
83 return err;
84 exists = err;
WANG Cong0852e452016-08-13 22:35:01 -070085 if (exists && bind)
86 return 0;
87
88 if (!exists) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000089 ret = tcf_idr_create(tn, index, NULL, a,
Vlad Buslove3822672019-10-30 16:09:06 +020090 &act_police_ops, bind, true, 0);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030091 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000092 tcf_idr_cleanup(tn, index);
WANG Conga03e6fe2016-06-06 09:54:30 -070093 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030094 }
WANG Conga03e6fe2016-06-06 09:54:30 -070095 ret = ACT_P_CREATED;
Davide Caratti484afd12018-11-21 18:23:53 +010096 spin_lock_init(&(to_police(*a)->tcfp_lock));
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030097 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -040098 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030099 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Davide Carattid6124d62019-03-20 15:00:08 +0100101 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
102 if (err < 0)
103 goto release_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
WANG Conga85a9702016-07-25 16:09:41 -0700105 police = to_police(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (parm->rate.rate) {
107 err = -ENOMEM;
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500108 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (R_tab == NULL)
110 goto failure;
Stephen Hemmingerc1b56872008-11-25 21:14:06 -0800111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (parm->peakrate.rate) {
113 P_tab = qdisc_get_rtab(&parm->peakrate,
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500114 tb[TCA_POLICE_PEAKRATE], NULL);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800115 if (P_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800119
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800120 if (est) {
Davide Caratti93be42f2018-09-13 19:29:12 +0200121 err = gen_replace_estimator(&police->tcf_bstats,
122 police->common.cpu_bstats,
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800123 &police->tcf_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700124 &police->tcf_lock,
125 NULL, est);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800126 if (err)
WANG Cong74030602017-06-13 13:36:24 -0700127 goto failure;
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800128 } else if (tb[TCA_POLICE_AVRATE] &&
129 (ret == ACT_P_CREATED ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800130 !gen_estimator_active(&police->tcf_rate_est))) {
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800131 err = -EINVAL;
WANG Cong74030602017-06-13 13:36:24 -0700132 goto failure;
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800133 }
134
Davide Carattifd6d4332018-11-28 18:43:42 +0100135 if (tb[TCA_POLICE_RESULT]) {
136 tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
137 if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
138 NL_SET_ERR_MSG(extack,
139 "goto chain not allowed on fallback");
140 err = -EINVAL;
141 goto failure;
142 }
143 }
144
Davide Caratti2d550db2018-09-13 19:29:13 +0200145 new = kzalloc(sizeof(*new), GFP_KERNEL);
146 if (unlikely(!new)) {
147 err = -ENOMEM;
148 goto failure;
149 }
150
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800151 /* No failure allowed after this point */
Davide Carattifd6d4332018-11-28 18:43:42 +0100152 new->tcfp_result = tcfp_result;
Davide Caratti2d550db2018-09-13 19:29:13 +0200153 new->tcfp_mtu = parm->mtu;
154 if (!new->tcfp_mtu) {
155 new->tcfp_mtu = ~0;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000156 if (R_tab)
Davide Caratti2d550db2018-09-13 19:29:13 +0200157 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000159 if (R_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200160 new->rate_present = true;
David Daid1967e42019-09-04 10:03:43 -0500161 rate64 = tb[TCA_POLICE_RATE64] ?
162 nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
163 psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000164 qdisc_put_rtab(R_tab);
165 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200166 new->rate_present = false;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000167 }
168 if (P_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200169 new->peak_present = true;
David Daid1967e42019-09-04 10:03:43 -0500170 prate64 = tb[TCA_POLICE_PEAKRATE64] ?
171 nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
172 psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000173 qdisc_put_rtab(P_tab);
174 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200175 new->peak_present = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Davide Caratti2d550db2018-09-13 19:29:13 +0200178 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
Davide Carattif2cbd482018-11-20 22:18:44 +0100179 if (new->peak_present)
Davide Caratti2d550db2018-09-13 19:29:13 +0200180 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
181 new->tcfp_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800183 if (tb[TCA_POLICE_AVRATE])
Davide Caratti2d550db2018-09-13 19:29:13 +0200184 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Davide Caratti2d550db2018-09-13 19:29:13 +0200186 spin_lock_bh(&police->tcf_lock);
Davide Carattif2cbd482018-11-20 22:18:44 +0100187 spin_lock_bh(&police->tcfp_lock);
188 police->tcfp_t_c = ktime_get_ns();
189 police->tcfp_toks = new->tcfp_burst;
190 if (new->peak_present)
191 police->tcfp_ptoks = new->tcfp_mtu_ptoks;
192 spin_unlock_bh(&police->tcfp_lock);
Davide Carattid6124d62019-03-20 15:00:08 +0100193 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Paul E. McKenney445d3742019-09-23 16:09:18 -0700194 new = rcu_replace_pointer(police->params,
195 new,
196 lockdep_is_held(&police->tcf_lock));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Davide Carattid6124d62019-03-20 15:00:08 +0100199 if (goto_ch)
200 tcf_chain_put_by_act(goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200201 if (new)
202 kfree_rcu(new, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Davide Caratti2d550db2018-09-13 19:29:13 +0200204 if (ret == ACT_P_CREATED)
205 tcf_idr_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return ret;
207
208failure:
Yang Yingliang3b69a4c2013-12-17 15:29:16 +0800209 qdisc_put_rtab(P_tab);
210 qdisc_put_rtab(R_tab);
Davide Carattid6124d62019-03-20 15:00:08 +0100211 if (goto_ch)
212 tcf_chain_put_by_act(goto_ch);
213release_idr:
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300214 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return err;
216}
217
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400218static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900219 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
WANG Conga85a9702016-07-25 16:09:41 -0700221 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200222 struct tcf_police_params *p;
Davide Caratti93be42f2018-09-13 19:29:12 +0200223 s64 now, toks, ptoks = 0;
224 int ret;
225
226 tcf_lastuse_update(&police->tcf_tm);
227 bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Davide Caratti2d550db2018-09-13 19:29:13 +0200229 ret = READ_ONCE(police->tcf_action);
230 p = rcu_dereference_bh(police->params);
231
232 if (p->tcfp_ewma_rate) {
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800233 struct gnet_stats_rate_est64 sample;
234
235 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
Davide Caratti2d550db2018-09-13 19:29:13 +0200236 sample.bps >= p->tcfp_ewma_rate)
Davide Caratti93be42f2018-09-13 19:29:12 +0200237 goto inc_overlimits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Davide Caratti2d550db2018-09-13 19:29:13 +0200240 if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
241 if (!p->rate_present) {
242 ret = p->tcfp_result;
243 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
Eric Dumazetd2de8752014-08-22 18:32:09 -0700246 now = ktime_get_ns();
Davide Carattif2cbd482018-11-20 22:18:44 +0100247 spin_lock_bh(&police->tcfp_lock);
248 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
Davide Caratti2d550db2018-09-13 19:29:13 +0200249 if (p->peak_present) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100250 ptoks = toks + police->tcfp_ptoks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200251 if (ptoks > p->tcfp_mtu_ptoks)
252 ptoks = p->tcfp_mtu_ptoks;
253 ptoks -= (s64)psched_l2t_ns(&p->peak,
254 qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100256 toks += police->tcfp_toks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200257 if (toks > p->tcfp_burst)
258 toks = p->tcfp_burst;
259 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if ((toks|ptoks) >= 0) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100261 police->tcfp_t_c = now;
262 police->tcfp_toks = toks;
263 police->tcfp_ptoks = ptoks;
264 spin_unlock_bh(&police->tcfp_lock);
Davide Caratti2d550db2018-09-13 19:29:13 +0200265 ret = p->tcfp_result;
Davide Caratti93be42f2018-09-13 19:29:12 +0200266 goto inc_drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100268 spin_unlock_bh(&police->tcfp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Davide Caratti93be42f2018-09-13 19:29:12 +0200271inc_overlimits:
272 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
273inc_drops:
274 if (ret == TC_ACT_SHOT)
275 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
Davide Caratti2d550db2018-09-13 19:29:13 +0200276end:
Davide Caratti93be42f2018-09-13 19:29:12 +0200277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Davide Caratti2d550db2018-09-13 19:29:13 +0200280static void tcf_police_cleanup(struct tc_action *a)
281{
282 struct tcf_police *police = to_police(a);
283 struct tcf_police_params *p;
284
285 p = rcu_dereference_protected(police->params, 1);
286 if (p)
287 kfree_rcu(p, rcu);
288}
289
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700290static void tcf_police_stats_update(struct tc_action *a,
Po Liu4b61d3e2020-06-19 14:01:07 +0800291 u64 bytes, u64 packets, u64 drops,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700292 u64 lastuse, bool hw)
293{
294 struct tcf_police *police = to_police(a);
295 struct tcf_t *tm = &police->tcf_tm;
296
Po Liu4b61d3e2020-06-19 14:01:07 +0800297 tcf_action_update_stats(a, bytes, packets, drops, hw);
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700298 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
299}
300
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400301static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400302 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700304 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700305 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200306 struct tcf_police_params *p;
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000307 struct tc_police opt = {
308 .index = police->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300309 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
310 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000311 };
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400312 struct tcf_t t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Vlad Buslove329bc42018-08-10 20:51:55 +0300314 spin_lock_bh(&police->tcf_lock);
315 opt.action = police->tcf_action;
Davide Caratti2d550db2018-09-13 19:29:13 +0200316 p = rcu_dereference_protected(police->params,
317 lockdep_is_held(&police->tcf_lock));
318 opt.mtu = p->tcfp_mtu;
319 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
David Daid1967e42019-09-04 10:03:43 -0500320 if (p->rate_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200321 psched_ratecfg_getrate(&opt.rate, &p->rate);
David Daid1967e42019-09-04 10:03:43 -0500322 if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) &&
323 nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
324 police->params->rate.rate_bytes_ps,
325 TCA_POLICE_PAD))
326 goto nla_put_failure;
327 }
328 if (p->peak_present) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200329 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
David Daid1967e42019-09-04 10:03:43 -0500330 if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) &&
331 nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
332 police->params->peak.rate_bytes_ps,
333 TCA_POLICE_PAD))
334 goto nla_put_failure;
335 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400336 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
337 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200338 if (p->tcfp_result &&
339 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
David S. Miller1b34ec42012-03-29 05:11:39 -0400340 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200341 if (p->tcfp_ewma_rate &&
342 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
David S. Miller1b34ec42012-03-29 05:11:39 -0400343 goto nla_put_failure;
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400344
Davide Caratti985fd982019-10-19 18:49:32 +0200345 tcf_tm_dump(&t, &police->tcf_tm);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400346 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
347 goto nla_put_failure;
Vlad Buslove329bc42018-08-10 20:51:55 +0300348 spin_unlock_bh(&police->tcf_lock);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return skb->len;
351
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800352nla_put_failure:
Vlad Buslove329bc42018-08-10 20:51:55 +0300353 spin_unlock_bh(&police->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700354 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -1;
356}
357
Cong Wangf061b482018-08-29 10:15:35 -0700358static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800359{
360 struct tc_action_net *tn = net_generic(net, police_net_id);
361
Chris Mi65a206c2017-08-30 02:31:59 -0400362 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365MODULE_AUTHOR("Alexey Kuznetsov");
366MODULE_DESCRIPTION("Policing actions");
367MODULE_LICENSE("GPL");
368
369static struct tc_action_ops act_police_ops = {
370 .kind = "police",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200371 .id = TCA_ID_POLICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .owner = THIS_MODULE,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700373 .stats_update = tcf_police_stats_update,
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400374 .act = tcf_police_act,
375 .dump = tcf_police_dump,
376 .init = tcf_police_init,
377 .walk = tcf_police_walker,
WANG Congddf97cc2016-02-22 15:57:53 -0800378 .lookup = tcf_police_search,
Davide Caratti2d550db2018-09-13 19:29:13 +0200379 .cleanup = tcf_police_cleanup,
WANG Conga85a9702016-07-25 16:09:41 -0700380 .size = sizeof(struct tcf_police),
WANG Congddf97cc2016-02-22 15:57:53 -0800381};
382
383static __net_init int police_init_net(struct net *net)
384{
385 struct tc_action_net *tn = net_generic(net, police_net_id);
386
Cong Wang981471b2019-08-25 10:01:32 -0700387 return tc_action_net_init(net, tn, &act_police_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800388}
389
Cong Wang039af9c2017-12-11 15:35:03 -0800390static void __net_exit police_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800391{
Cong Wang039af9c2017-12-11 15:35:03 -0800392 tc_action_net_exit(net_list, police_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800393}
394
395static struct pernet_operations police_net_ops = {
396 .init = police_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800397 .exit_batch = police_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800398 .id = &police_net_id,
399 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400402static int __init police_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
WANG Congddf97cc2016-02-22 15:57:53 -0800404 return tcf_register_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400407static void __exit police_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
WANG Congddf97cc2016-02-22 15:57:53 -0800409 tcf_unregister_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412module_init(police_init_module);
413module_exit(police_cleanup_module);