]>
Commit | Line | Data |
---|---|---|
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, <[email protected]> | |
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 | |
2d550dba | 25 | struct tcf_police_params { |
0e243218 JP |
26 | int tcfp_result; |
27 | u32 tcfp_ewma_rate; | |
c6d14ff1 | 28 | s64 tcfp_burst; |
0e243218 | 29 | u32 tcfp_mtu; |
c6d14ff1 | 30 | s64 tcfp_mtu_ptoks; |
c6d14ff1 JP |
31 | struct psched_ratecfg rate; |
32 | bool rate_present; | |
33 | struct psched_ratecfg peak; | |
34 | bool peak_present; | |
2d550dba DC |
35 | struct rcu_head rcu; |
36 | }; | |
37 | ||
38 | struct tcf_police { | |
39 | struct tc_action common; | |
40 | struct tcf_police_params __rcu *params; | |
f2cbd485 DC |
41 | |
42 | spinlock_t tcfp_lock ____cacheline_aligned_in_smp; | |
43 | s64 tcfp_toks; | |
44 | s64 tcfp_ptoks; | |
45 | s64 tcfp_t_c; | |
0e243218 | 46 | }; |
a85a970a WC |
47 | |
48 | #define to_police(pc) ((struct tcf_police *)pc) | |
0e243218 | 49 | |
1e9b3d53 | 50 | /* old policer structure from before tc actions */ |
cc7ec456 | 51 | struct tc_police_compat { |
1e9b3d53 PM |
52 | u32 index; |
53 | int action; | |
54 | u32 limit; | |
55 | u32 burst; | |
56 | u32 mtu; | |
57 | struct tc_ratespec rate; | |
58 | struct tc_ratespec peakrate; | |
59 | }; | |
60 | ||
e9ce1cd3 | 61 | /* Each policer is serialized by its individual spinlock */ |
1da177e4 | 62 | |
c7d03a00 | 63 | static unsigned int police_net_id; |
a85a970a | 64 | static struct tc_action_ops act_police_ops; |
ddf97ccd | 65 | |
2ac06347 | 66 | static int tcf_police_walker(struct net *net, struct sk_buff *skb, |
ddf97ccd | 67 | struct netlink_callback *cb, int type, |
41780105 AA |
68 | const struct tc_action_ops *ops, |
69 | struct netlink_ext_ack *extack) | |
1da177e4 | 70 | { |
ddf97ccd | 71 | struct tc_action_net *tn = net_generic(net, police_net_id); |
1da177e4 | 72 | |
b3620145 | 73 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
1da177e4 | 74 | } |
1da177e4 | 75 | |
53b2bf3f PM |
76 | static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = { |
77 | [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE }, | |
78 | [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE }, | |
79 | [TCA_POLICE_AVRATE] = { .type = NLA_U32 }, | |
80 | [TCA_POLICE_RESULT] = { .type = NLA_U32 }, | |
81 | }; | |
82 | ||
2ac06347 | 83 | static int tcf_police_init(struct net *net, struct nlattr *nla, |
a85a970a | 84 | struct nlattr *est, struct tc_action **a, |
789871bb | 85 | int ovr, int bind, bool rtnl_held, |
589dad6d | 86 | struct netlink_ext_ack *extack) |
1da177e4 | 87 | { |
1da177e4 | 88 | int ret = 0, err; |
7ba699c6 | 89 | struct nlattr *tb[TCA_POLICE_MAX + 1]; |
1da177e4 | 90 | struct tc_police *parm; |
e9ce1cd3 | 91 | struct tcf_police *police; |
1da177e4 | 92 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
ddf97ccd | 93 | struct tc_action_net *tn = net_generic(net, police_net_id); |
2d550dba | 94 | struct tcf_police_params *new; |
0852e455 | 95 | bool exists = false; |
1e9b3d53 | 96 | int size; |
1da177e4 | 97 | |
cee63723 | 98 | if (nla == NULL) |
1da177e4 LT |
99 | return -EINVAL; |
100 | ||
fceb6435 | 101 | err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL); |
cee63723 PM |
102 | if (err < 0) |
103 | return err; | |
104 | ||
7ba699c6 | 105 | if (tb[TCA_POLICE_TBF] == NULL) |
1e9b3d53 | 106 | return -EINVAL; |
7ba699c6 | 107 | size = nla_len(tb[TCA_POLICE_TBF]); |
1e9b3d53 | 108 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) |
1da177e4 | 109 | return -EINVAL; |
0852e455 | 110 | |
7ba699c6 | 111 | parm = nla_data(tb[TCA_POLICE_TBF]); |
0190c1d4 VB |
112 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
113 | if (err < 0) | |
114 | return err; | |
115 | exists = err; | |
0852e455 WC |
116 | if (exists && bind) |
117 | return 0; | |
1da177e4 | 118 | |
0852e455 | 119 | if (!exists) { |
65a206c0 | 120 | ret = tcf_idr_create(tn, parm->index, NULL, a, |
93be42f9 | 121 | &act_police_ops, bind, true); |
0190c1d4 VB |
122 | if (ret) { |
123 | tcf_idr_cleanup(tn, parm->index); | |
a03e6fe5 | 124 | return ret; |
0190c1d4 | 125 | } |
a03e6fe5 | 126 | ret = ACT_P_CREATED; |
4e8ddd7f | 127 | } else if (!ovr) { |
65a206c0 | 128 | tcf_idr_release(*a, bind); |
4e8ddd7f | 129 | return -EEXIST; |
1da177e4 LT |
130 | } |
131 | ||
a85a970a | 132 | police = to_police(*a); |
1da177e4 LT |
133 | if (parm->rate.rate) { |
134 | err = -ENOMEM; | |
e9bc3fa2 | 135 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL); |
1da177e4 LT |
136 | if (R_tab == NULL) |
137 | goto failure; | |
c1b56878 | 138 | |
1da177e4 LT |
139 | if (parm->peakrate.rate) { |
140 | P_tab = qdisc_get_rtab(&parm->peakrate, | |
e9bc3fa2 | 141 | tb[TCA_POLICE_PEAKRATE], NULL); |
71bcb09a | 142 | if (P_tab == NULL) |
1da177e4 | 143 | goto failure; |
1da177e4 LT |
144 | } |
145 | } | |
71bcb09a | 146 | |
71bcb09a | 147 | if (est) { |
93be42f9 DC |
148 | err = gen_replace_estimator(&police->tcf_bstats, |
149 | police->common.cpu_bstats, | |
71bcb09a | 150 | &police->tcf_rate_est, |
edb09eb1 ED |
151 | &police->tcf_lock, |
152 | NULL, est); | |
71bcb09a | 153 | if (err) |
74030603 | 154 | goto failure; |
a883bf56 JP |
155 | } else if (tb[TCA_POLICE_AVRATE] && |
156 | (ret == ACT_P_CREATED || | |
1c0d32fd | 157 | !gen_estimator_active(&police->tcf_rate_est))) { |
a883bf56 | 158 | err = -EINVAL; |
74030603 | 159 | goto failure; |
71bcb09a SH |
160 | } |
161 | ||
2d550dba DC |
162 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
163 | if (unlikely(!new)) { | |
164 | err = -ENOMEM; | |
165 | goto failure; | |
166 | } | |
167 | ||
71bcb09a | 168 | /* No failure allowed after this point */ |
2d550dba DC |
169 | new->tcfp_mtu = parm->mtu; |
170 | if (!new->tcfp_mtu) { | |
171 | new->tcfp_mtu = ~0; | |
c6d14ff1 | 172 | if (R_tab) |
2d550dba | 173 | new->tcfp_mtu = 255 << R_tab->rate.cell_log; |
c6d14ff1 JP |
174 | } |
175 | if (R_tab) { | |
2d550dba DC |
176 | new->rate_present = true; |
177 | psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0); | |
c6d14ff1 JP |
178 | qdisc_put_rtab(R_tab); |
179 | } else { | |
2d550dba | 180 | new->rate_present = false; |
1da177e4 | 181 | } |
c6d14ff1 | 182 | if (P_tab) { |
2d550dba DC |
183 | new->peak_present = true; |
184 | psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0); | |
c6d14ff1 JP |
185 | qdisc_put_rtab(P_tab); |
186 | } else { | |
2d550dba | 187 | new->peak_present = false; |
1da177e4 LT |
188 | } |
189 | ||
2d550dba | 190 | new->tcfp_burst = PSCHED_TICKS2NS(parm->burst); |
f2cbd485 | 191 | if (new->peak_present) |
2d550dba DC |
192 | new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak, |
193 | new->tcfp_mtu); | |
1da177e4 | 194 | |
7ba699c6 | 195 | if (tb[TCA_POLICE_AVRATE]) |
2d550dba | 196 | new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]); |
1da177e4 | 197 | |
c08f5ed5 DC |
198 | if (tb[TCA_POLICE_RESULT]) { |
199 | new->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]); | |
200 | if (TC_ACT_EXT_CMP(new->tcfp_result, TC_ACT_GOTO_CHAIN)) { | |
201 | NL_SET_ERR_MSG(extack, | |
202 | "goto chain not allowed on fallback"); | |
203 | err = -EINVAL; | |
204 | goto failure; | |
205 | } | |
206 | } | |
207 | ||
2d550dba | 208 | spin_lock_bh(&police->tcf_lock); |
f2cbd485 DC |
209 | spin_lock_bh(&police->tcfp_lock); |
210 | police->tcfp_t_c = ktime_get_ns(); | |
211 | police->tcfp_toks = new->tcfp_burst; | |
212 | if (new->peak_present) | |
213 | police->tcfp_ptoks = new->tcfp_mtu_ptoks; | |
214 | spin_unlock_bh(&police->tcfp_lock); | |
2d550dba DC |
215 | police->tcf_action = parm->action; |
216 | rcu_swap_protected(police->params, | |
217 | new, | |
218 | lockdep_is_held(&police->tcf_lock)); | |
e9ce1cd3 | 219 | spin_unlock_bh(&police->tcf_lock); |
1da177e4 | 220 | |
2d550dba DC |
221 | if (new) |
222 | kfree_rcu(new, rcu); | |
1da177e4 | 223 | |
2d550dba DC |
224 | if (ret == ACT_P_CREATED) |
225 | tcf_idr_insert(tn, *a); | |
1da177e4 LT |
226 | return ret; |
227 | ||
228 | failure: | |
3b69a4c9 YY |
229 | qdisc_put_rtab(P_tab); |
230 | qdisc_put_rtab(R_tab); | |
4e8ddd7f | 231 | tcf_idr_release(*a, bind); |
1da177e4 LT |
232 | return err; |
233 | } | |
234 | ||
2ac06347 | 235 | static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a, |
10297b99 | 236 | struct tcf_result *res) |
1da177e4 | 237 | { |
a85a970a | 238 | struct tcf_police *police = to_police(a); |
2d550dba | 239 | struct tcf_police_params *p; |
93be42f9 DC |
240 | s64 now, toks, ptoks = 0; |
241 | int ret; | |
1da177e4 | 242 | |
3d3ed181 | 243 | tcf_lastuse_update(&police->tcf_tm); |
93be42f9 | 244 | bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb); |
1da177e4 | 245 | |
2d550dba DC |
246 | ret = READ_ONCE(police->tcf_action); |
247 | p = rcu_dereference_bh(police->params); | |
248 | ||
249 | if (p->tcfp_ewma_rate) { | |
1c0d32fd ED |
250 | struct gnet_stats_rate_est64 sample; |
251 | ||
252 | if (!gen_estimator_read(&police->tcf_rate_est, &sample) || | |
2d550dba | 253 | sample.bps >= p->tcfp_ewma_rate) |
93be42f9 | 254 | goto inc_overlimits; |
1da177e4 | 255 | } |
1da177e4 | 256 | |
2d550dba DC |
257 | if (qdisc_pkt_len(skb) <= p->tcfp_mtu) { |
258 | if (!p->rate_present) { | |
259 | ret = p->tcfp_result; | |
260 | goto end; | |
1da177e4 LT |
261 | } |
262 | ||
d2de875c | 263 | now = ktime_get_ns(); |
f2cbd485 DC |
264 | spin_lock_bh(&police->tcfp_lock); |
265 | toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst); | |
2d550dba | 266 | if (p->peak_present) { |
f2cbd485 | 267 | ptoks = toks + police->tcfp_ptoks; |
2d550dba DC |
268 | if (ptoks > p->tcfp_mtu_ptoks) |
269 | ptoks = p->tcfp_mtu_ptoks; | |
270 | ptoks -= (s64)psched_l2t_ns(&p->peak, | |
271 | qdisc_pkt_len(skb)); | |
1da177e4 | 272 | } |
f2cbd485 | 273 | toks += police->tcfp_toks; |
2d550dba DC |
274 | if (toks > p->tcfp_burst) |
275 | toks = p->tcfp_burst; | |
276 | toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb)); | |
1da177e4 | 277 | if ((toks|ptoks) >= 0) { |
f2cbd485 DC |
278 | police->tcfp_t_c = now; |
279 | police->tcfp_toks = toks; | |
280 | police->tcfp_ptoks = ptoks; | |
281 | spin_unlock_bh(&police->tcfp_lock); | |
2d550dba | 282 | ret = p->tcfp_result; |
93be42f9 | 283 | goto inc_drops; |
1da177e4 | 284 | } |
f2cbd485 | 285 | spin_unlock_bh(&police->tcfp_lock); |
1da177e4 | 286 | } |
93be42f9 DC |
287 | |
288 | inc_overlimits: | |
289 | qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats)); | |
290 | inc_drops: | |
291 | if (ret == TC_ACT_SHOT) | |
292 | qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats)); | |
2d550dba | 293 | end: |
93be42f9 | 294 | return ret; |
1da177e4 LT |
295 | } |
296 | ||
2d550dba DC |
297 | static void tcf_police_cleanup(struct tc_action *a) |
298 | { | |
299 | struct tcf_police *police = to_police(a); | |
300 | struct tcf_police_params *p; | |
301 | ||
302 | p = rcu_dereference_protected(police->params, 1); | |
303 | if (p) | |
304 | kfree_rcu(p, rcu); | |
305 | } | |
306 | ||
2ac06347 | 307 | static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a, |
5a7a5555 | 308 | int bind, int ref) |
1da177e4 | 309 | { |
27a884dc | 310 | unsigned char *b = skb_tail_pointer(skb); |
a85a970a | 311 | struct tcf_police *police = to_police(a); |
2d550dba | 312 | struct tcf_police_params *p; |
0f04cfd0 JM |
313 | struct tc_police opt = { |
314 | .index = police->tcf_index, | |
036bb443 VB |
315 | .refcnt = refcount_read(&police->tcf_refcnt) - ref, |
316 | .bindcnt = atomic_read(&police->tcf_bindcnt) - bind, | |
0f04cfd0 | 317 | }; |
3d3ed181 | 318 | struct tcf_t t; |
0f04cfd0 | 319 | |
e329bc42 VB |
320 | spin_lock_bh(&police->tcf_lock); |
321 | opt.action = police->tcf_action; | |
2d550dba DC |
322 | p = rcu_dereference_protected(police->params, |
323 | lockdep_is_held(&police->tcf_lock)); | |
324 | opt.mtu = p->tcfp_mtu; | |
325 | opt.burst = PSCHED_NS2TICKS(p->tcfp_burst); | |
326 | if (p->rate_present) | |
327 | psched_ratecfg_getrate(&opt.rate, &p->rate); | |
328 | if (p->peak_present) | |
329 | psched_ratecfg_getrate(&opt.peakrate, &p->peak); | |
1b34ec43 DM |
330 | if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) |
331 | goto nla_put_failure; | |
2d550dba DC |
332 | if (p->tcfp_result && |
333 | nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result)) | |
1b34ec43 | 334 | goto nla_put_failure; |
2d550dba DC |
335 | if (p->tcfp_ewma_rate && |
336 | nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate)) | |
1b34ec43 | 337 | goto nla_put_failure; |
3d3ed181 JHS |
338 | |
339 | t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install); | |
340 | t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse); | |
53eb440f | 341 | t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse); |
3d3ed181 JHS |
342 | t.expires = jiffies_to_clock_t(police->tcf_tm.expires); |
343 | if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD)) | |
344 | goto nla_put_failure; | |
e329bc42 | 345 | spin_unlock_bh(&police->tcf_lock); |
3d3ed181 | 346 | |
1da177e4 LT |
347 | return skb->len; |
348 | ||
7ba699c6 | 349 | nla_put_failure: |
e329bc42 | 350 | spin_unlock_bh(&police->tcf_lock); |
dc5fc579 | 351 | nlmsg_trim(skb, b); |
1da177e4 LT |
352 | return -1; |
353 | } | |
354 | ||
f061b48c | 355 | static int tcf_police_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
356 | { |
357 | struct tc_action_net *tn = net_generic(net, police_net_id); | |
358 | ||
65a206c0 | 359 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
360 | } |
361 | ||
1da177e4 LT |
362 | MODULE_AUTHOR("Alexey Kuznetsov"); |
363 | MODULE_DESCRIPTION("Policing actions"); | |
364 | MODULE_LICENSE("GPL"); | |
365 | ||
366 | static struct tc_action_ops act_police_ops = { | |
367 | .kind = "police", | |
368 | .type = TCA_ID_POLICE, | |
1da177e4 | 369 | .owner = THIS_MODULE, |
2ac06347 JHS |
370 | .act = tcf_police_act, |
371 | .dump = tcf_police_dump, | |
372 | .init = tcf_police_init, | |
373 | .walk = tcf_police_walker, | |
ddf97ccd | 374 | .lookup = tcf_police_search, |
2d550dba | 375 | .cleanup = tcf_police_cleanup, |
a85a970a | 376 | .size = sizeof(struct tcf_police), |
ddf97ccd WC |
377 | }; |
378 | ||
379 | static __net_init int police_init_net(struct net *net) | |
380 | { | |
381 | struct tc_action_net *tn = net_generic(net, police_net_id); | |
382 | ||
c7e460ce | 383 | return tc_action_net_init(tn, &act_police_ops); |
ddf97ccd WC |
384 | } |
385 | ||
039af9c6 | 386 | static void __net_exit police_exit_net(struct list_head *net_list) |
ddf97ccd | 387 | { |
039af9c6 | 388 | tc_action_net_exit(net_list, police_net_id); |
ddf97ccd WC |
389 | } |
390 | ||
391 | static struct pernet_operations police_net_ops = { | |
392 | .init = police_init_net, | |
039af9c6 | 393 | .exit_batch = police_exit_net, |
ddf97ccd WC |
394 | .id = &police_net_id, |
395 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
396 | }; |
397 | ||
5a7a5555 | 398 | static int __init police_init_module(void) |
1da177e4 | 399 | { |
ddf97ccd | 400 | return tcf_register_action(&act_police_ops, &police_net_ops); |
1da177e4 LT |
401 | } |
402 | ||
5a7a5555 | 403 | static void __exit police_cleanup_module(void) |
1da177e4 | 404 | { |
ddf97ccd | 405 | tcf_unregister_action(&act_police_ops, &police_net_ops); |
1da177e4 LT |
406 | } |
407 | ||
408 | module_init(police_init_module); | |
409 | module_exit(police_cleanup_module); |