blob: b48e40c69ad0617b8c9c1989044fee648c8f7a0e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01002 * net/sched/act_police.c Input police filter
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim (action changes)
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/rtnetlink.h>
20#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070023#include <net/netlink.h>
Davide Carattid6124d62019-03-20 15:00:08 +010024#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Davide Caratti2d550db2018-09-13 19:29:13 +020026struct tcf_police_params {
Jiri Pirko0e243212013-02-12 00:12:06 +000027 int tcfp_result;
28 u32 tcfp_ewma_rate;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +000029 s64 tcfp_burst;
Jiri Pirko0e243212013-02-12 00:12:06 +000030 u32 tcfp_mtu;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +000031 s64 tcfp_mtu_ptoks;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +000032 struct psched_ratecfg rate;
33 bool rate_present;
34 struct psched_ratecfg peak;
35 bool peak_present;
Davide Caratti2d550db2018-09-13 19:29:13 +020036 struct rcu_head rcu;
37};
38
39struct tcf_police {
40 struct tc_action common;
41 struct tcf_police_params __rcu *params;
Davide Carattif2cbd482018-11-20 22:18:44 +010042
43 spinlock_t tcfp_lock ____cacheline_aligned_in_smp;
44 s64 tcfp_toks;
45 s64 tcfp_ptoks;
46 s64 tcfp_t_c;
Jiri Pirko0e243212013-02-12 00:12:06 +000047};
WANG Conga85a9702016-07-25 16:09:41 -070048
49#define to_police(pc) ((struct tcf_police *)pc)
Jiri Pirko0e243212013-02-12 00:12:06 +000050
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080051/* old policer structure from before tc actions */
Eric Dumazetcc7ec452011-01-19 19:26:56 +000052struct tc_police_compat {
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080053 u32 index;
54 int action;
55 u32 limit;
56 u32 burst;
57 u32 mtu;
58 struct tc_ratespec rate;
59 struct tc_ratespec peakrate;
60};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Each policer is serialized by its individual spinlock */
63
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030064static unsigned int police_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070065static struct tc_action_ops act_police_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080066
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040067static int tcf_police_walker(struct net *net, struct sk_buff *skb,
WANG Congddf97cc2016-02-22 15:57:53 -080068 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -050069 const struct tc_action_ops *ops,
70 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
WANG Congddf97cc2016-02-22 15:57:53 -080072 struct tc_action_net *tn = net_generic(net, police_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alexander Aringb3620142018-02-15 10:54:59 -050074 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Patrick McHardy53b2bf32008-01-23 20:36:30 -080077static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
78 [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
79 [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
80 [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
81 [TCA_POLICE_RESULT] = { .type = NLA_U32 },
82};
83
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -040084static int tcf_police_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070085 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030086 int ovr, int bind, bool rtnl_held,
Davide Caratti85d09662019-03-20 14:59:59 +010087 struct tcf_proto *tp,
Alexander Aring589dad62018-02-15 10:54:56 -050088 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Davide Carattifd6d4332018-11-28 18:43:42 +010090 int ret = 0, tcfp_result = TC_ACT_OK, err, size;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080091 struct nlattr *tb[TCA_POLICE_MAX + 1];
Davide Carattid6124d62019-03-20 15:00:08 +010092 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070094 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
WANG Congddf97cc2016-02-22 15:57:53 -080096 struct tc_action_net *tn = net_generic(net, police_net_id);
Davide Caratti2d550db2018-09-13 19:29:13 +020097 struct tcf_police_params *new;
WANG Cong0852e452016-08-13 22:35:01 -070098 bool exists = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardycee63722008-01-23 20:33:32 -0800100 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -EINVAL;
102
Johannes Berg8cb08172019-04-26 14:07:28 +0200103 err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
104 police_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800105 if (err < 0)
106 return err;
107
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800108 if (tb[TCA_POLICE_TBF] == NULL)
Patrick McHardy1e9b3d52006-11-30 19:54:05 -0800109 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800110 size = nla_len(tb[TCA_POLICE_TBF]);
Patrick McHardy1e9b3d52006-11-30 19:54:05 -0800111 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
WANG Cong0852e452016-08-13 22:35:01 -0700114 parm = nla_data(tb[TCA_POLICE_TBF]);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300115 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
116 if (err < 0)
117 return err;
118 exists = err;
WANG Cong0852e452016-08-13 22:35:01 -0700119 if (exists && bind)
120 return 0;
121
122 if (!exists) {
Chris Mi65a206c2017-08-30 02:31:59 -0400123 ret = tcf_idr_create(tn, parm->index, NULL, a,
Davide Caratti93be42f2018-09-13 19:29:12 +0200124 &act_police_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300125 if (ret) {
126 tcf_idr_cleanup(tn, parm->index);
WANG Conga03e6fe2016-06-06 09:54:30 -0700127 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300128 }
WANG Conga03e6fe2016-06-06 09:54:30 -0700129 ret = ACT_P_CREATED;
Davide Caratti484afd12018-11-21 18:23:53 +0100130 spin_lock_init(&(to_police(*a)->tcfp_lock));
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300131 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400132 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300133 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Davide Carattid6124d62019-03-20 15:00:08 +0100135 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
136 if (err < 0)
137 goto release_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
WANG Conga85a9702016-07-25 16:09:41 -0700139 police = to_police(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (parm->rate.rate) {
141 err = -ENOMEM;
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500142 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (R_tab == NULL)
144 goto failure;
Stephen Hemmingerc1b56872008-11-25 21:14:06 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (parm->peakrate.rate) {
147 P_tab = qdisc_get_rtab(&parm->peakrate,
Alexander Aringe9bc3fa2017-12-20 12:35:18 -0500148 tb[TCA_POLICE_PEAKRATE], NULL);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800149 if (P_tab == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 }
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800153
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800154 if (est) {
Davide Caratti93be42f2018-09-13 19:29:12 +0200155 err = gen_replace_estimator(&police->tcf_bstats,
156 police->common.cpu_bstats,
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800157 &police->tcf_rate_est,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700158 &police->tcf_lock,
159 NULL, est);
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800160 if (err)
WANG Cong74030602017-06-13 13:36:24 -0700161 goto failure;
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800162 } else if (tb[TCA_POLICE_AVRATE] &&
163 (ret == ACT_P_CREATED ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800164 !gen_estimator_active(&police->tcf_rate_est))) {
Jarek Poplawskia883bf52009-03-04 17:38:10 -0800165 err = -EINVAL;
WANG Cong74030602017-06-13 13:36:24 -0700166 goto failure;
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800167 }
168
Davide Carattifd6d4332018-11-28 18:43:42 +0100169 if (tb[TCA_POLICE_RESULT]) {
170 tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
171 if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
172 NL_SET_ERR_MSG(extack,
173 "goto chain not allowed on fallback");
174 err = -EINVAL;
175 goto failure;
176 }
177 }
178
Davide Caratti2d550db2018-09-13 19:29:13 +0200179 new = kzalloc(sizeof(*new), GFP_KERNEL);
180 if (unlikely(!new)) {
181 err = -ENOMEM;
182 goto failure;
183 }
184
Stephen Hemminger71bcb092008-11-25 21:13:31 -0800185 /* No failure allowed after this point */
Davide Carattifd6d4332018-11-28 18:43:42 +0100186 new->tcfp_result = tcfp_result;
Davide Caratti2d550db2018-09-13 19:29:13 +0200187 new->tcfp_mtu = parm->mtu;
188 if (!new->tcfp_mtu) {
189 new->tcfp_mtu = ~0;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000190 if (R_tab)
Davide Caratti2d550db2018-09-13 19:29:13 +0200191 new->tcfp_mtu = 255 << R_tab->rate.cell_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000193 if (R_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200194 new->rate_present = true;
195 psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000196 qdisc_put_rtab(R_tab);
197 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200198 new->rate_present = false;
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000199 }
200 if (P_tab) {
Davide Caratti2d550db2018-09-13 19:29:13 +0200201 new->peak_present = true;
202 psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
Jiri Pirkoc6d14ff2013-02-12 00:12:07 +0000203 qdisc_put_rtab(P_tab);
204 } else {
Davide Caratti2d550db2018-09-13 19:29:13 +0200205 new->peak_present = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
Davide Caratti2d550db2018-09-13 19:29:13 +0200208 new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
Davide Carattif2cbd482018-11-20 22:18:44 +0100209 if (new->peak_present)
Davide Caratti2d550db2018-09-13 19:29:13 +0200210 new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
211 new->tcfp_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800213 if (tb[TCA_POLICE_AVRATE])
Davide Caratti2d550db2018-09-13 19:29:13 +0200214 new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Davide Caratti2d550db2018-09-13 19:29:13 +0200216 spin_lock_bh(&police->tcf_lock);
Davide Carattif2cbd482018-11-20 22:18:44 +0100217 spin_lock_bh(&police->tcfp_lock);
218 police->tcfp_t_c = ktime_get_ns();
219 police->tcfp_toks = new->tcfp_burst;
220 if (new->peak_present)
221 police->tcfp_ptoks = new->tcfp_mtu_ptoks;
222 spin_unlock_bh(&police->tcfp_lock);
Davide Carattid6124d62019-03-20 15:00:08 +0100223 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200224 rcu_swap_protected(police->params,
225 new,
226 lockdep_is_held(&police->tcf_lock));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700227 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Davide Carattid6124d62019-03-20 15:00:08 +0100229 if (goto_ch)
230 tcf_chain_put_by_act(goto_ch);
Davide Caratti2d550db2018-09-13 19:29:13 +0200231 if (new)
232 kfree_rcu(new, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Davide Caratti2d550db2018-09-13 19:29:13 +0200234 if (ret == ACT_P_CREATED)
235 tcf_idr_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ret;
237
238failure:
Yang Yingliang3b69a4c2013-12-17 15:29:16 +0800239 qdisc_put_rtab(P_tab);
240 qdisc_put_rtab(R_tab);
Davide Carattid6124d62019-03-20 15:00:08 +0100241 if (goto_ch)
242 tcf_chain_put_by_act(goto_ch);
243release_idr:
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300244 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return err;
246}
247
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400248static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900249 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);
Davide Caratti2d550db2018-09-13 19:29:13 +0200252 struct tcf_police_params *p;
Davide Caratti93be42f2018-09-13 19:29:12 +0200253 s64 now, toks, ptoks = 0;
254 int ret;
255
256 tcf_lastuse_update(&police->tcf_tm);
257 bstats_cpu_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 Caratti2d550db2018-09-13 19:29:13 +0200270 if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
271 if (!p->rate_present) {
272 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 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100286 toks += police->tcfp_toks;
Davide Caratti2d550db2018-09-13 19:29:13 +0200287 if (toks > p->tcfp_burst)
288 toks = p->tcfp_burst;
289 toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if ((toks|ptoks) >= 0) {
Davide Carattif2cbd482018-11-20 22:18:44 +0100291 police->tcfp_t_c = now;
292 police->tcfp_toks = toks;
293 police->tcfp_ptoks = ptoks;
294 spin_unlock_bh(&police->tcfp_lock);
Davide Caratti2d550db2018-09-13 19:29:13 +0200295 ret = p->tcfp_result;
Davide Caratti93be42f2018-09-13 19:29:12 +0200296 goto inc_drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Davide Carattif2cbd482018-11-20 22:18:44 +0100298 spin_unlock_bh(&police->tcfp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Davide Caratti93be42f2018-09-13 19:29:12 +0200301inc_overlimits:
302 qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
303inc_drops:
304 if (ret == TC_ACT_SHOT)
305 qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
Davide Caratti2d550db2018-09-13 19:29:13 +0200306end:
Davide Caratti93be42f2018-09-13 19:29:12 +0200307 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Davide Caratti2d550db2018-09-13 19:29:13 +0200310static void tcf_police_cleanup(struct tc_action *a)
311{
312 struct tcf_police *police = to_police(a);
313 struct tcf_police_params *p;
314
315 p = rcu_dereference_protected(police->params, 1);
316 if (p)
317 kfree_rcu(p, rcu);
318}
319
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400320static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400321 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700323 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700324 struct tcf_police *police = to_police(a);
Davide Caratti2d550db2018-09-13 19:29:13 +0200325 struct tcf_police_params *p;
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000326 struct tc_police opt = {
327 .index = police->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300328 .refcnt = refcount_read(&police->tcf_refcnt) - ref,
329 .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
Jeff Mahoney0f04cfd2010-08-31 13:21:42 +0000330 };
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400331 struct tcf_t t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Vlad Buslove329bc42018-08-10 20:51:55 +0300333 spin_lock_bh(&police->tcf_lock);
334 opt.action = police->tcf_action;
Davide Caratti2d550db2018-09-13 19:29:13 +0200335 p = rcu_dereference_protected(police->params,
336 lockdep_is_held(&police->tcf_lock));
337 opt.mtu = p->tcfp_mtu;
338 opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
339 if (p->rate_present)
340 psched_ratecfg_getrate(&opt.rate, &p->rate);
341 if (p->peak_present)
342 psched_ratecfg_getrate(&opt.peakrate, &p->peak);
David S. Miller1b34ec42012-03-29 05:11:39 -0400343 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
344 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200345 if (p->tcfp_result &&
346 nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
David S. Miller1b34ec42012-03-29 05:11:39 -0400347 goto nla_put_failure;
Davide Caratti2d550db2018-09-13 19:29:13 +0200348 if (p->tcfp_ewma_rate &&
349 nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
David S. Miller1b34ec42012-03-29 05:11:39 -0400350 goto nla_put_failure;
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400351
352 t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
353 t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
Jamal Hadi Salim53eb4402016-06-06 06:32:54 -0400354 t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400355 t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
356 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
357 goto nla_put_failure;
Vlad Buslove329bc42018-08-10 20:51:55 +0300358 spin_unlock_bh(&police->tcf_lock);
Jamal Hadi Salim3d3ed182016-05-23 21:07:20 -0400359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return skb->len;
361
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800362nla_put_failure:
Vlad Buslove329bc42018-08-10 20:51:55 +0300363 spin_unlock_bh(&police->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700364 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -1;
366}
367
Cong Wangf061b482018-08-29 10:15:35 -0700368static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800369{
370 struct tc_action_net *tn = net_generic(net, police_net_id);
371
Chris Mi65a206c2017-08-30 02:31:59 -0400372 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375MODULE_AUTHOR("Alexey Kuznetsov");
376MODULE_DESCRIPTION("Policing actions");
377MODULE_LICENSE("GPL");
378
379static struct tc_action_ops act_police_ops = {
380 .kind = "police",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200381 .id = TCA_ID_POLICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 .owner = THIS_MODULE,
Jamal Hadi Salim2ac063472018-08-12 09:34:56 -0400383 .act = tcf_police_act,
384 .dump = tcf_police_dump,
385 .init = tcf_police_init,
386 .walk = tcf_police_walker,
WANG Congddf97cc2016-02-22 15:57:53 -0800387 .lookup = tcf_police_search,
Davide Caratti2d550db2018-09-13 19:29:13 +0200388 .cleanup = tcf_police_cleanup,
WANG Conga85a9702016-07-25 16:09:41 -0700389 .size = sizeof(struct tcf_police),
WANG Congddf97cc2016-02-22 15:57:53 -0800390};
391
392static __net_init int police_init_net(struct net *net)
393{
394 struct tc_action_net *tn = net_generic(net, police_net_id);
395
Cong Wangc7e460c2017-11-06 13:47:18 -0800396 return tc_action_net_init(tn, &act_police_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800397}
398
Cong Wang039af9c2017-12-11 15:35:03 -0800399static void __net_exit police_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800400{
Cong Wang039af9c2017-12-11 15:35:03 -0800401 tc_action_net_exit(net_list, police_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800402}
403
404static struct pernet_operations police_net_ops = {
405 .init = police_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800406 .exit_batch = police_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800407 .id = &police_net_id,
408 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400411static int __init police_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
WANG Congddf97cc2016-02-22 15:57:53 -0800413 return tcf_register_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400416static void __exit police_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
WANG Congddf97cc2016-02-22 15:57:53 -0800418 tcf_unregister_action(&act_police_ops, &police_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421module_init(police_init_module);
422module_exit(police_cleanup_module);