]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/sched/act_police.c
Merge branch 'hv_netvsc-Fix-default-and-limit-of-recv-buffer'
[thirdparty/kernel/stable.git] / net / sched / act_police.c
CommitLineData
1da177e4 1/*
0c6965dd 2 * net/sched/act_police.c Input police filter
1da177e4
LT
3 *
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
1da177e4
LT
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
1da177e4 16#include <linux/string.h>
1da177e4 17#include <linux/errno.h>
1da177e4 18#include <linux/skbuff.h>
1da177e4
LT
19#include <linux/rtnetlink.h>
20#include <linux/init.h>
5a0e3ad6 21#include <linux/slab.h>
1da177e4 22#include <net/act_api.h>
dc5fc579 23#include <net/netlink.h>
1da177e4 24
0e243218 25struct tcf_police {
ec0595cc 26 struct tc_action common;
0e243218
JP
27 int tcfp_result;
28 u32 tcfp_ewma_rate;
c6d14ff1 29 s64 tcfp_burst;
0e243218 30 u32 tcfp_mtu;
c6d14ff1
JP
31 s64 tcfp_toks;
32 s64 tcfp_ptoks;
33 s64 tcfp_mtu_ptoks;
34 s64 tcfp_t_c;
35 struct psched_ratecfg rate;
36 bool rate_present;
37 struct psched_ratecfg peak;
38 bool peak_present;
0e243218 39};
a85a970a
WC
40
41#define to_police(pc) ((struct tcf_police *)pc)
0e243218 42
1e9b3d53 43/* old policer structure from before tc actions */
cc7ec456 44struct tc_police_compat {
1e9b3d53
PM
45 u32 index;
46 int action;
47 u32 limit;
48 u32 burst;
49 u32 mtu;
50 struct tc_ratespec rate;
51 struct tc_ratespec peakrate;
52};
53
e9ce1cd3 54/* Each policer is serialized by its individual spinlock */
1da177e4 55
c7d03a00 56static unsigned int police_net_id;
a85a970a 57static struct tc_action_ops act_police_ops;
ddf97ccd
WC
58
59static int tcf_act_police_walker(struct net *net, struct sk_buff *skb,
60 struct netlink_callback *cb, int type,
a85a970a 61 const struct tc_action_ops *ops)
1da177e4 62{
ddf97ccd 63 struct tc_action_net *tn = net_generic(net, police_net_id);
1da177e4 64
b5ac8518 65 return tcf_generic_walker(tn, skb, cb, type, ops);
1da177e4 66}
1da177e4 67
53b2bf3f
PM
68static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
69 [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
70 [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
71 [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
72 [TCA_POLICE_RESULT] = { .type = NLA_U32 },
73};
74
d9fa17ef 75static int tcf_act_police_init(struct net *net, struct nlattr *nla,
a85a970a 76 struct nlattr *est, struct tc_action **a,
d9fa17ef 77 int ovr, int bind)
1da177e4 78{
1da177e4 79 int ret = 0, err;
7ba699c6 80 struct nlattr *tb[TCA_POLICE_MAX + 1];
1da177e4 81 struct tc_police *parm;
e9ce1cd3 82 struct tcf_police *police;
1da177e4 83 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
ddf97ccd 84 struct tc_action_net *tn = net_generic(net, police_net_id);
0852e455 85 bool exists = false;
1e9b3d53 86 int size;
1da177e4 87
cee63723 88 if (nla == NULL)
1da177e4
LT
89 return -EINVAL;
90
fceb6435 91 err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
cee63723
PM
92 if (err < 0)
93 return err;
94
7ba699c6 95 if (tb[TCA_POLICE_TBF] == NULL)
1e9b3d53 96 return -EINVAL;
7ba699c6 97 size = nla_len(tb[TCA_POLICE_TBF]);
1e9b3d53 98 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1da177e4 99 return -EINVAL;
0852e455 100
7ba699c6 101 parm = nla_data(tb[TCA_POLICE_TBF]);
65a206c0 102 exists = tcf_idr_check(tn, parm->index, a, bind);
0852e455
WC
103 if (exists && bind)
104 return 0;
1da177e4 105
0852e455 106 if (!exists) {
65a206c0
CM
107 ret = tcf_idr_create(tn, parm->index, NULL, a,
108 &act_police_ops, bind, false);
a03e6fe5
WC
109 if (ret)
110 return ret;
111 ret = ACT_P_CREATED;
0852e455 112 } else {
65a206c0 113 tcf_idr_release(*a, bind);
0852e455
WC
114 if (!ovr)
115 return -EEXIST;
1da177e4
LT
116 }
117
a85a970a 118 police = to_police(*a);
1da177e4
LT
119 if (parm->rate.rate) {
120 err = -ENOMEM;
7ba699c6 121 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
1da177e4
LT
122 if (R_tab == NULL)
123 goto failure;
c1b56878 124
1da177e4
LT
125 if (parm->peakrate.rate) {
126 P_tab = qdisc_get_rtab(&parm->peakrate,
7ba699c6 127 tb[TCA_POLICE_PEAKRATE]);
71bcb09a 128 if (P_tab == NULL)
1da177e4 129 goto failure;
1da177e4
LT
130 }
131 }
71bcb09a 132
71bcb09a 133 if (est) {
22e0f8b9 134 err = gen_replace_estimator(&police->tcf_bstats, NULL,
71bcb09a 135 &police->tcf_rate_est,
edb09eb1
ED
136 &police->tcf_lock,
137 NULL, est);
71bcb09a 138 if (err)
74030603 139 goto failure;
a883bf56
JP
140 } else if (tb[TCA_POLICE_AVRATE] &&
141 (ret == ACT_P_CREATED ||
1c0d32fd 142 !gen_estimator_active(&police->tcf_rate_est))) {
a883bf56 143 err = -EINVAL;
74030603 144 goto failure;
71bcb09a
SH
145 }
146
74030603 147 spin_lock_bh(&police->tcf_lock);
71bcb09a 148 /* No failure allowed after this point */
c6d14ff1
JP
149 police->tcfp_mtu = parm->mtu;
150 if (police->tcfp_mtu == 0) {
151 police->tcfp_mtu = ~0;
152 if (R_tab)
153 police->tcfp_mtu = 255 << R_tab->rate.cell_log;
154 }
155 if (R_tab) {
156 police->rate_present = true;
3e1e3aae 157 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
c6d14ff1
JP
158 qdisc_put_rtab(R_tab);
159 } else {
160 police->rate_present = false;
1da177e4 161 }
c6d14ff1
JP
162 if (P_tab) {
163 police->peak_present = true;
3e1e3aae 164 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
c6d14ff1
JP
165 qdisc_put_rtab(P_tab);
166 } else {
167 police->peak_present = false;
1da177e4
LT
168 }
169
7ba699c6 170 if (tb[TCA_POLICE_RESULT])
1587bac4 171 police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
c6d14ff1
JP
172 police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
173 police->tcfp_toks = police->tcfp_burst;
174 if (police->peak_present) {
175 police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
176 police->tcfp_mtu);
177 police->tcfp_ptoks = police->tcfp_mtu_ptoks;
1da177e4 178 }
e9ce1cd3 179 police->tcf_action = parm->action;
1da177e4 180
7ba699c6 181 if (tb[TCA_POLICE_AVRATE])
1587bac4 182 police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1da177e4 183
e9ce1cd3 184 spin_unlock_bh(&police->tcf_lock);
1da177e4
LT
185 if (ret != ACT_P_CREATED)
186 return ret;
187
d2de875c 188 police->tcfp_t_c = ktime_get_ns();
65a206c0 189 tcf_idr_insert(tn, *a);
1da177e4 190
1da177e4
LT
191 return ret;
192
193failure:
3b69a4c9
YY
194 qdisc_put_rtab(P_tab);
195 qdisc_put_rtab(R_tab);
1da177e4 196 if (ret == ACT_P_CREATED)
65a206c0 197 tcf_idr_cleanup(*a, est);
1da177e4
LT
198 return err;
199}
200
dc7f9f6e 201static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
10297b99 202 struct tcf_result *res)
1da177e4 203{
a85a970a 204 struct tcf_police *police = to_police(a);
c6d14ff1
JP
205 s64 now;
206 s64 toks;
207 s64 ptoks = 0;
1da177e4 208
e9ce1cd3 209 spin_lock(&police->tcf_lock);
1da177e4 210
bfe0d029 211 bstats_update(&police->tcf_bstats, skb);
3d3ed181 212 tcf_lastuse_update(&police->tcf_tm);
1da177e4 213
1c0d32fd
ED
214 if (police->tcfp_ewma_rate) {
215 struct gnet_stats_rate_est64 sample;
216
217 if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
218 sample.bps >= police->tcfp_ewma_rate) {
219 police->tcf_qstats.overlimits++;
220 if (police->tcf_action == TC_ACT_SHOT)
221 police->tcf_qstats.drops++;
222 spin_unlock(&police->tcf_lock);
223 return police->tcf_action;
224 }
1da177e4 225 }
1da177e4 226
0abf77e5 227 if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
c6d14ff1 228 if (!police->rate_present) {
e9ce1cd3
DM
229 spin_unlock(&police->tcf_lock);
230 return police->tcfp_result;
1da177e4
LT
231 }
232
d2de875c 233 now = ktime_get_ns();
c6d14ff1
JP
234 toks = min_t(s64, now - police->tcfp_t_c,
235 police->tcfp_burst);
236 if (police->peak_present) {
e9ce1cd3 237 ptoks = toks + police->tcfp_ptoks;
c6d14ff1
JP
238 if (ptoks > police->tcfp_mtu_ptoks)
239 ptoks = police->tcfp_mtu_ptoks;
240 ptoks -= (s64) psched_l2t_ns(&police->peak,
241 qdisc_pkt_len(skb));
1da177e4 242 }
e9ce1cd3 243 toks += police->tcfp_toks;
c6d14ff1 244 if (toks > police->tcfp_burst)
e9ce1cd3 245 toks = police->tcfp_burst;
c6d14ff1 246 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
1da177e4 247 if ((toks|ptoks) >= 0) {
e9ce1cd3
DM
248 police->tcfp_t_c = now;
249 police->tcfp_toks = toks;
250 police->tcfp_ptoks = ptoks;
f71b109f
RM
251 if (police->tcfp_result == TC_ACT_SHOT)
252 police->tcf_qstats.drops++;
e9ce1cd3
DM
253 spin_unlock(&police->tcf_lock);
254 return police->tcfp_result;
1da177e4
LT
255 }
256 }
257
e9ce1cd3 258 police->tcf_qstats.overlimits++;
b9647580
JP
259 if (police->tcf_action == TC_ACT_SHOT)
260 police->tcf_qstats.drops++;
e9ce1cd3
DM
261 spin_unlock(&police->tcf_lock);
262 return police->tcf_action;
1da177e4
LT
263}
264
5a7a5555
JHS
265static int tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a,
266 int bind, int ref)
1da177e4 267{
27a884dc 268 unsigned char *b = skb_tail_pointer(skb);
a85a970a 269 struct tcf_police *police = to_police(a);
0f04cfd0
JM
270 struct tc_police opt = {
271 .index = police->tcf_index,
272 .action = police->tcf_action,
273 .mtu = police->tcfp_mtu,
c6d14ff1 274 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
0f04cfd0
JM
275 .refcnt = police->tcf_refcnt - ref,
276 .bindcnt = police->tcf_bindcnt - bind,
277 };
3d3ed181 278 struct tcf_t t;
0f04cfd0 279
c6d14ff1 280 if (police->rate_present)
01cb71d2 281 psched_ratecfg_getrate(&opt.rate, &police->rate);
c6d14ff1 282 if (police->peak_present)
01cb71d2 283 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
1b34ec43
DM
284 if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
285 goto nla_put_failure;
286 if (police->tcfp_result &&
287 nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
288 goto nla_put_failure;
289 if (police->tcfp_ewma_rate &&
290 nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
291 goto nla_put_failure;
3d3ed181
JHS
292
293 t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
294 t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
53eb440f 295 t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
3d3ed181
JHS
296 t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
297 if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
298 goto nla_put_failure;
299
1da177e4
LT
300 return skb->len;
301
7ba699c6 302nla_put_failure:
dc5fc579 303 nlmsg_trim(skb, b);
1da177e4
LT
304 return -1;
305}
306
a85a970a 307static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
308{
309 struct tc_action_net *tn = net_generic(net, police_net_id);
310
65a206c0 311 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
312}
313
1da177e4
LT
314MODULE_AUTHOR("Alexey Kuznetsov");
315MODULE_DESCRIPTION("Policing actions");
316MODULE_LICENSE("GPL");
317
318static struct tc_action_ops act_police_ops = {
319 .kind = "police",
320 .type = TCA_ID_POLICE,
1da177e4
LT
321 .owner = THIS_MODULE,
322 .act = tcf_act_police,
323 .dump = tcf_act_police_dump,
d9fa17ef 324 .init = tcf_act_police_init,
ddf97ccd
WC
325 .walk = tcf_act_police_walker,
326 .lookup = tcf_police_search,
a85a970a 327 .size = sizeof(struct tcf_police),
ddf97ccd
WC
328};
329
330static __net_init int police_init_net(struct net *net)
331{
332 struct tc_action_net *tn = net_generic(net, police_net_id);
333
c7e460ce 334 return tc_action_net_init(tn, &act_police_ops);
ddf97ccd
WC
335}
336
337static void __net_exit police_exit_net(struct net *net)
338{
339 struct tc_action_net *tn = net_generic(net, police_net_id);
340
341 tc_action_net_exit(tn);
342}
343
344static struct pernet_operations police_net_ops = {
345 .init = police_init_net,
346 .exit = police_exit_net,
347 .id = &police_net_id,
348 .size = sizeof(struct tc_action_net),
1da177e4
LT
349};
350
5a7a5555 351static int __init police_init_module(void)
1da177e4 352{
ddf97ccd 353 return tcf_register_action(&act_police_ops, &police_net_ops);
1da177e4
LT
354}
355
5a7a5555 356static void __exit police_cleanup_module(void)
1da177e4 357{
ddf97ccd 358 tcf_unregister_action(&act_police_ops, &police_net_ops);
1da177e4
LT
359}
360
361module_init(police_init_module);
362module_exit(police_cleanup_module);