blob: 49cec3e64a4d5b15b781151715347cccff1833ca [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 },
43};
44
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040045static int tcf_police_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070046 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030047 int ovr, int bind, bool rtnl_held,
Davide Caratti85d09662019-03-20 14:59:59 +010048 struct tcf_proto *tp,
Alexander Aring589dad62018-02-15 10:54:56 -050049 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Davide Carattifd6d4332018-11-28 18:43:42 +010051 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080052 struct nlattr *tb[TCA_POLICE_MAX + 1];
Davide Carattid6124d62019-03-20 15:00:08 +010053 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070055 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
WANG Congddf97cc2016-02-22 15:57:53 -080057 struct tc_action_net *tn = net_generic(net, police_net_id);
Davide Caratti2d550db2018-09-13 19:29:13 +020058 struct tcf_police_params *new;
WANG Cong0852e452016-08-13 22:35:01 -070059 bool exists = false;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000060 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Patrick McHardycee63722008-01-23 20:33:32 -080062 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return -EINVAL;
64
Johannes Berg8cb08172019-04-26 14:07:28 +020065 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
66 police_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080067 if (err < 0)
68 return err;
69
Patrick McHardy7ba699c2008-01-22 22:11:50 -080070 if (tb[TCA_POLICE_TBF] == NULL)
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080071 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080072 size = nla_len(tb[TCA_POLICE_TBF]);
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080073 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
WANG Cong0852e452016-08-13 22:35:01 -070076 parm = nla_data(tb[TCA_POLICE_TBF]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000077 index = parm->index;
78 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030079 if (err < 0)
80 return err;
81 exists = err;
WANG Cong0852e452016-08-13 22:35:01 -070082 if (exists && bind)
83 return 0;
84
85 if (!exists) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000086 ret = tcf_idr_create(tn, index, NULL, a,
Davide Caratti93be42f2018-09-13 19:29:12 +020087 &act_police_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030088 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000089 tcf_idr_cleanup(tn, index);
WANG Conga03e6fe2016-06-06 09:54:30 -070090 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030091 }
WANG Conga03e6fe2016-06-06 09:54:30 -070092 ret = ACT_P_CREATED;
Davide Caratti484afd12018-11-21 18:23:53 +010093 spin_lock_init(&(to_police(*a)->tcfp_lock));
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030094 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -040095 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030096 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Davide Carattid6124d62019-03-20 15:00:08 +010098 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
99 if (err < 0)
100 goto release_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
WANG Conga85a9702016-07-25 16:09:41 -0700102 police = to_police(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (parm->rate.rate) {
104 err = -ENOMEM;
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500105 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (R_tab == NULL)
107 goto failure;
Stephen Hemmingerc1b56872008-11-25 21:14:06 -0800108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (parm->peakrate.rate) {
110 P_tab = qdisc_get_rtab(&parm->peakrate,
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500111 tb[TCA_POLICE_PEAKRATE], NULL);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800112 if (P_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800116
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800117 if (est) {
Davide Caratti93be42f2018-09-13 19:29:12 +0200118 err = gen_replace_estimator(&police->tcf_bstats,
119 police->common.cpu_bstats,
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800120 &police->tcf_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700121 &police->tcf_lock,
122 NULL, est);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800123 if (err)
WANG Cong74030602017-06-13 13:36:24 -0700124 goto failure;
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800125 } else if (tb[TCA_POLICE_AVRATE] &&
126 (ret == ACT_P_CREATED ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800127 !gen_estimator_active(&police->tcf_rate_est))) {
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800128 err = -EINVAL;
WANG Cong74030602017-06-13 13:36:24 -0700129 goto failure;
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800130 }
131
Davide Carattifd6d4332018-11-28 18:43:42 +0100132 if (tb[TCA_POLICE_RESULT]) {
133 tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
134 if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
135 NL_SET_ERR_MSG(extack,
136 "goto chain not allowed on fallback");
137 err = -EINVAL;
138 goto failure;
139 }
140 }
141
Davide Caratti2d550db2018-09-13 19:29:13 +0200142 new = kzalloc(sizeof(*new), GFP_KERNEL);
143 if (unlikely(!new)) {
144 err = -ENOMEM;
145 goto failure;
146 }
147
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800148 /* No failure allowed after this point */
Davide Carattifd6d4332018-11-28 18:43:42 +0100149 new->tcfp_result = tcfp_result;
Davide Caratti2d550db2018-09-13 19:29:13 +0200150 new->tcfp_mtu = parm->mtu;
151 if (!new->tcfp_mtu) {
152 new->tcfp_mtu = ~0;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000153 if (R_tab)
Davide Caratti2d550db2018-09-13 19:29:13 +0200154 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000156 if (R_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200157 new->rate_present = true;
158 psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000159 qdisc_put_rtab(R_tab);
160 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200161 new->rate_present = false;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000162 }
163 if (P_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200164 new->peak_present = true;
165 psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000166 qdisc_put_rtab(P_tab);
167 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200168 new->peak_present = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Davide Caratti2d550db2018-09-13 19:29:13 +0200171 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
Davide Carattif2cbd482018-11-20 22:18:44 +0100172 if (new->peak_present)
Davide Caratti2d550db2018-09-13 19:29:13 +0200173 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
174 new->tcfp_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800176 if (tb[TCA_POLICE_AVRATE])
Davide Caratti2d550db2018-09-13 19:29:13 +0200177 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Davide Caratti2d550db2018-09-13 19:29:13 +0200179 spin_lock_bh(&police->tcf_lock);
Davide Carattif2cbd482018-11-20 22:18:44 +0100180 spin_lock_bh(&police->tcfp_lock);
181 police->tcfp_t_c = ktime_get_ns();
182 police->tcfp_toks = new->tcfp_burst;
183 if (new->peak_present)
184 police->tcfp_ptoks = new->tcfp_mtu_ptoks;
185 spin_unlock_bh(&police->tcfp_lock);
Davide Carattid6124d62019-03-20 15:00:08 +0100186 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200187 rcu_swap_protected(police->params,
188 new,
189 lockdep_is_held(&police->tcf_lock));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Davide Carattid6124d62019-03-20 15:00:08 +0100192 if (goto_ch)
193 tcf_chain_put_by_act(goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200194 if (new)
195 kfree_rcu(new, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Davide Caratti2d550db2018-09-13 19:29:13 +0200197 if (ret == ACT_P_CREATED)
198 tcf_idr_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return ret;
200
201failure:
Yang Yingliang3b69a4c2013-12-17 15:29:16 +0800202 qdisc_put_rtab(P_tab);
203 qdisc_put_rtab(R_tab);
Davide Carattid6124d62019-03-20 15:00:08 +0100204 if (goto_ch)
205 tcf_chain_put_by_act(goto_ch);
206release_idr:
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300207 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return err;
209}
210
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400211static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900212 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
WANG Conga85a9702016-07-25 16:09:41 -0700214 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200215 struct tcf_police_params *p;
Davide Caratti93be42f2018-09-13 19:29:12 +0200216 s64 now, toks, ptoks = 0;
217 int ret;
218
219 tcf_lastuse_update(&police->tcf_tm);
220 bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Davide Caratti2d550db2018-09-13 19:29:13 +0200222 ret = READ_ONCE(police->tcf_action);
223 p = rcu_dereference_bh(police->params);
224
225 if (p->tcfp_ewma_rate) {
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800226 struct gnet_stats_rate_est64 sample;
227
228 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
Davide Caratti2d550db2018-09-13 19:29:13 +0200229 sample.bps >= p->tcfp_ewma_rate)
Davide Caratti93be42f2018-09-13 19:29:12 +0200230 goto inc_overlimits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Davide Caratti2d550db2018-09-13 19:29:13 +0200233 if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
234 if (!p->rate_present) {
235 ret = p->tcfp_result;
236 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
Eric Dumazetd2de8752014-08-22 18:32:09 -0700239 now = ktime_get_ns();
Davide Carattif2cbd482018-11-20 22:18:44 +0100240 spin_lock_bh(&police->tcfp_lock);
241 toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
Davide Caratti2d550db2018-09-13 19:29:13 +0200242 if (p->peak_present) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100243 ptoks = toks + police->tcfp_ptoks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200244 if (ptoks > p->tcfp_mtu_ptoks)
245 ptoks = p->tcfp_mtu_ptoks;
246 ptoks -= (s64)psched_l2t_ns(&p->peak,
247 qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100249 toks += police->tcfp_toks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200250 if (toks > p->tcfp_burst)
251 toks = p->tcfp_burst;
252 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if ((toks|ptoks) >= 0) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100254 police->tcfp_t_c = now;
255 police->tcfp_toks = toks;
256 police->tcfp_ptoks = ptoks;
257 spin_unlock_bh(&police->tcfp_lock);
Davide Caratti2d550db2018-09-13 19:29:13 +0200258 ret = p->tcfp_result;
Davide Caratti93be42f2018-09-13 19:29:12 +0200259 goto inc_drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100261 spin_unlock_bh(&police->tcfp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Davide Caratti93be42f2018-09-13 19:29:12 +0200264inc_overlimits:
265 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
266inc_drops:
267 if (ret == TC_ACT_SHOT)
268 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
Davide Caratti2d550db2018-09-13 19:29:13 +0200269end:
Davide Caratti93be42f2018-09-13 19:29:12 +0200270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Davide Caratti2d550db2018-09-13 19:29:13 +0200273static void tcf_police_cleanup(struct tc_action *a)
274{
275 struct tcf_police *police = to_police(a);
276 struct tcf_police_params *p;
277
278 p = rcu_dereference_protected(police->params, 1);
279 if (p)
280 kfree_rcu(p, rcu);
281}
282
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700283static void tcf_police_stats_update(struct tc_action *a,
284 u64 bytes, u32 packets,
285 u64 lastuse, bool hw)
286{
287 struct tcf_police *police = to_police(a);
288 struct tcf_t *tm = &police->tcf_tm;
289
290 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
291 if (hw)
292 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
293 bytes, packets);
294 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
295}
296
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400297static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400298 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700300 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700301 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200302 struct tcf_police_params *p;
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000303 struct tc_police opt = {
304 .index = police->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300305 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
306 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000307 };
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400308 struct tcf_t t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Vlad Buslove329bc42018-08-10 20:51:55 +0300310 spin_lock_bh(&police->tcf_lock);
311 opt.action = police->tcf_action;
Davide Caratti2d550db2018-09-13 19:29:13 +0200312 p = rcu_dereference_protected(police->params,
313 lockdep_is_held(&police->tcf_lock));
314 opt.mtu = p->tcfp_mtu;
315 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
316 if (p->rate_present)
317 psched_ratecfg_getrate(&opt.rate, &p->rate);
318 if (p->peak_present)
319 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
David S. Miller1b34ec42012-03-29 05:11:39 -0400320 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
321 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200322 if (p->tcfp_result &&
323 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
David S. Miller1b34ec42012-03-29 05:11:39 -0400324 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200325 if (p->tcfp_ewma_rate &&
326 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
David S. Miller1b34ec42012-03-29 05:11:39 -0400327 goto nla_put_failure;
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400328
329 t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
330 t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
Jamal Hadi Salim53eb4402016-06-06 06:32:54 -0400331 t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400332 t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
333 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
334 goto nla_put_failure;
Vlad Buslove329bc42018-08-10 20:51:55 +0300335 spin_unlock_bh(&police->tcf_lock);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return skb->len;
338
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800339nla_put_failure:
Vlad Buslove329bc42018-08-10 20:51:55 +0300340 spin_unlock_bh(&police->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700341 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -1;
343}
344
Cong Wangf061b482018-08-29 10:15:35 -0700345static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800346{
347 struct tc_action_net *tn = net_generic(net, police_net_id);
348
Chris Mi65a206c2017-08-30 02:31:59 -0400349 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352MODULE_AUTHOR("Alexey Kuznetsov");
353MODULE_DESCRIPTION("Policing actions");
354MODULE_LICENSE("GPL");
355
356static struct tc_action_ops act_police_ops = {
357 .kind = "police",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200358 .id = TCA_ID_POLICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .owner = THIS_MODULE,
Pieter Jansen van Vuuren12f02b62019-05-04 04:46:24 -0700360 .stats_update = tcf_police_stats_update,
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400361 .act = tcf_police_act,
362 .dump = tcf_police_dump,
363 .init = tcf_police_init,
364 .walk = tcf_police_walker,
WANG Congddf97cc2016-02-22 15:57:53 -0800365 .lookup = tcf_police_search,
Davide Caratti2d550db2018-09-13 19:29:13 +0200366 .cleanup = tcf_police_cleanup,
WANG Conga85a9702016-07-25 16:09:41 -0700367 .size = sizeof(struct tcf_police),
WANG Congddf97cc2016-02-22 15:57:53 -0800368};
369
370static __net_init int police_init_net(struct net *net)
371{
372 struct tc_action_net *tn = net_generic(net, police_net_id);
373
Cong Wangc7e460c2017-11-06 13:47:18 -0800374 return tc_action_net_init(tn, &act_police_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800375}
376
Cong Wang039af9c2017-12-11 15:35:03 -0800377static void __net_exit police_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800378{
Cong Wang039af9c2017-12-11 15:35:03 -0800379 tc_action_net_exit(net_list, police_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800380}
381
382static struct pernet_operations police_net_ops = {
383 .init = police_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800384 .exit_batch = police_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800385 .id = &police_net_id,
386 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400389static int __init police_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
WANG Congddf97cc2016-02-22 15:57:53 -0800391 return tcf_register_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400394static void __exit police_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
WANG Congddf97cc2016-02-22 15:57:53 -0800396 tcf_unregister_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399module_init(police_init_module);
400module_exit(police_cleanup_module);