1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/act_police.c Input police filter
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 * J Hadi Salim (action changes)
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/skbuff.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <net/act_api.h>
19 #include <net/netlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_police.h>
23 /* Each policer is serialized by its individual spinlock */
25 static unsigned int police_net_id
;
26 static struct tc_action_ops act_police_ops
;
28 static int tcf_police_walker(struct net
*net
, struct sk_buff
*skb
,
29 struct netlink_callback
*cb
, int type
,
30 const struct tc_action_ops
*ops
,
31 struct netlink_ext_ack
*extack
)
33 struct tc_action_net
*tn
= net_generic(net
, police_net_id
);
35 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
38 static 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
},
45 static int tcf_police_init(struct net
*net
, struct nlattr
*nla
,
46 struct nlattr
*est
, struct tc_action
**a
,
47 int ovr
, int bind
, bool rtnl_held
,
49 struct netlink_ext_ack
*extack
)
51 int ret
= 0, tcfp_result
= TC_ACT_OK
, err
, size
;
52 struct nlattr
*tb
[TCA_POLICE_MAX
+ 1];
53 struct tcf_chain
*goto_ch
= NULL
;
54 struct tc_police
*parm
;
55 struct tcf_police
*police
;
56 struct qdisc_rate_table
*R_tab
= NULL
, *P_tab
= NULL
;
57 struct tc_action_net
*tn
= net_generic(net
, police_net_id
);
58 struct tcf_police_params
*new;
64 err
= nla_parse_nested_deprecated(tb
, TCA_POLICE_MAX
, nla
,
69 if (tb
[TCA_POLICE_TBF
] == NULL
)
71 size
= nla_len(tb
[TCA_POLICE_TBF
]);
72 if (size
!= sizeof(*parm
) && size
!= sizeof(struct tc_police_compat
))
75 parm
= nla_data(tb
[TCA_POLICE_TBF
]);
76 err
= tcf_idr_check_alloc(tn
, &parm
->index
, a
, bind
);
84 ret
= tcf_idr_create(tn
, parm
->index
, NULL
, a
,
85 &act_police_ops
, bind
, true);
87 tcf_idr_cleanup(tn
, parm
->index
);
91 spin_lock_init(&(to_police(*a
)->tcfp_lock
));
93 tcf_idr_release(*a
, bind
);
96 err
= tcf_action_check_ctrlact(parm
->action
, tp
, &goto_ch
, extack
);
100 police
= to_police(*a
);
101 if (parm
->rate
.rate
) {
103 R_tab
= qdisc_get_rtab(&parm
->rate
, tb
[TCA_POLICE_RATE
], NULL
);
107 if (parm
->peakrate
.rate
) {
108 P_tab
= qdisc_get_rtab(&parm
->peakrate
,
109 tb
[TCA_POLICE_PEAKRATE
], NULL
);
116 err
= gen_replace_estimator(&police
->tcf_bstats
,
117 police
->common
.cpu_bstats
,
118 &police
->tcf_rate_est
,
123 } else if (tb
[TCA_POLICE_AVRATE
] &&
124 (ret
== ACT_P_CREATED
||
125 !gen_estimator_active(&police
->tcf_rate_est
))) {
130 if (tb
[TCA_POLICE_RESULT
]) {
131 tcfp_result
= nla_get_u32(tb
[TCA_POLICE_RESULT
]);
132 if (TC_ACT_EXT_CMP(tcfp_result
, TC_ACT_GOTO_CHAIN
)) {
133 NL_SET_ERR_MSG(extack
,
134 "goto chain not allowed on fallback");
140 new = kzalloc(sizeof(*new), GFP_KERNEL
);
141 if (unlikely(!new)) {
146 /* No failure allowed after this point */
147 new->tcfp_result
= tcfp_result
;
148 new->tcfp_mtu
= parm
->mtu
;
149 if (!new->tcfp_mtu
) {
152 new->tcfp_mtu
= 255 << R_tab
->rate
.cell_log
;
155 new->rate_present
= true;
156 psched_ratecfg_precompute(&new->rate
, &R_tab
->rate
, 0);
157 qdisc_put_rtab(R_tab
);
159 new->rate_present
= false;
162 new->peak_present
= true;
163 psched_ratecfg_precompute(&new->peak
, &P_tab
->rate
, 0);
164 qdisc_put_rtab(P_tab
);
166 new->peak_present
= false;
169 new->tcfp_burst
= PSCHED_TICKS2NS(parm
->burst
);
170 if (new->peak_present
)
171 new->tcfp_mtu_ptoks
= (s64
)psched_l2t_ns(&new->peak
,
174 if (tb
[TCA_POLICE_AVRATE
])
175 new->tcfp_ewma_rate
= nla_get_u32(tb
[TCA_POLICE_AVRATE
]);
177 spin_lock_bh(&police
->tcf_lock
);
178 spin_lock_bh(&police
->tcfp_lock
);
179 police
->tcfp_t_c
= ktime_get_ns();
180 police
->tcfp_toks
= new->tcfp_burst
;
181 if (new->peak_present
)
182 police
->tcfp_ptoks
= new->tcfp_mtu_ptoks
;
183 spin_unlock_bh(&police
->tcfp_lock
);
184 goto_ch
= tcf_action_set_ctrlact(*a
, parm
->action
, goto_ch
);
185 rcu_swap_protected(police
->params
,
187 lockdep_is_held(&police
->tcf_lock
));
188 spin_unlock_bh(&police
->tcf_lock
);
191 tcf_chain_put_by_act(goto_ch
);
195 if (ret
== ACT_P_CREATED
)
196 tcf_idr_insert(tn
, *a
);
200 qdisc_put_rtab(P_tab
);
201 qdisc_put_rtab(R_tab
);
203 tcf_chain_put_by_act(goto_ch
);
205 tcf_idr_release(*a
, bind
);
209 static int tcf_police_act(struct sk_buff
*skb
, const struct tc_action
*a
,
210 struct tcf_result
*res
)
212 struct tcf_police
*police
= to_police(a
);
213 struct tcf_police_params
*p
;
214 s64 now
, toks
, ptoks
= 0;
217 tcf_lastuse_update(&police
->tcf_tm
);
218 bstats_cpu_update(this_cpu_ptr(police
->common
.cpu_bstats
), skb
);
220 ret
= READ_ONCE(police
->tcf_action
);
221 p
= rcu_dereference_bh(police
->params
);
223 if (p
->tcfp_ewma_rate
) {
224 struct gnet_stats_rate_est64 sample
;
226 if (!gen_estimator_read(&police
->tcf_rate_est
, &sample
) ||
227 sample
.bps
>= p
->tcfp_ewma_rate
)
231 if (qdisc_pkt_len(skb
) <= p
->tcfp_mtu
) {
232 if (!p
->rate_present
) {
233 ret
= p
->tcfp_result
;
237 now
= ktime_get_ns();
238 spin_lock_bh(&police
->tcfp_lock
);
239 toks
= min_t(s64
, now
- police
->tcfp_t_c
, p
->tcfp_burst
);
240 if (p
->peak_present
) {
241 ptoks
= toks
+ police
->tcfp_ptoks
;
242 if (ptoks
> p
->tcfp_mtu_ptoks
)
243 ptoks
= p
->tcfp_mtu_ptoks
;
244 ptoks
-= (s64
)psched_l2t_ns(&p
->peak
,
247 toks
+= police
->tcfp_toks
;
248 if (toks
> p
->tcfp_burst
)
249 toks
= p
->tcfp_burst
;
250 toks
-= (s64
)psched_l2t_ns(&p
->rate
, qdisc_pkt_len(skb
));
251 if ((toks
|ptoks
) >= 0) {
252 police
->tcfp_t_c
= now
;
253 police
->tcfp_toks
= toks
;
254 police
->tcfp_ptoks
= ptoks
;
255 spin_unlock_bh(&police
->tcfp_lock
);
256 ret
= p
->tcfp_result
;
259 spin_unlock_bh(&police
->tcfp_lock
);
263 qstats_overlimit_inc(this_cpu_ptr(police
->common
.cpu_qstats
));
265 if (ret
== TC_ACT_SHOT
)
266 qstats_drop_inc(this_cpu_ptr(police
->common
.cpu_qstats
));
271 static void tcf_police_cleanup(struct tc_action
*a
)
273 struct tcf_police
*police
= to_police(a
);
274 struct tcf_police_params
*p
;
276 p
= rcu_dereference_protected(police
->params
, 1);
281 static void tcf_police_stats_update(struct tc_action
*a
,
282 u64 bytes
, u32 packets
,
283 u64 lastuse
, bool hw
)
285 struct tcf_police
*police
= to_police(a
);
286 struct tcf_t
*tm
= &police
->tcf_tm
;
288 _bstats_cpu_update(this_cpu_ptr(a
->cpu_bstats
), bytes
, packets
);
290 _bstats_cpu_update(this_cpu_ptr(a
->cpu_bstats_hw
),
292 tm
->lastuse
= max_t(u64
, tm
->lastuse
, lastuse
);
295 static int tcf_police_dump(struct sk_buff
*skb
, struct tc_action
*a
,
298 unsigned char *b
= skb_tail_pointer(skb
);
299 struct tcf_police
*police
= to_police(a
);
300 struct tcf_police_params
*p
;
301 struct tc_police opt
= {
302 .index
= police
->tcf_index
,
303 .refcnt
= refcount_read(&police
->tcf_refcnt
) - ref
,
304 .bindcnt
= atomic_read(&police
->tcf_bindcnt
) - bind
,
308 spin_lock_bh(&police
->tcf_lock
);
309 opt
.action
= police
->tcf_action
;
310 p
= rcu_dereference_protected(police
->params
,
311 lockdep_is_held(&police
->tcf_lock
));
312 opt
.mtu
= p
->tcfp_mtu
;
313 opt
.burst
= PSCHED_NS2TICKS(p
->tcfp_burst
);
315 psched_ratecfg_getrate(&opt
.rate
, &p
->rate
);
317 psched_ratecfg_getrate(&opt
.peakrate
, &p
->peak
);
318 if (nla_put(skb
, TCA_POLICE_TBF
, sizeof(opt
), &opt
))
319 goto nla_put_failure
;
320 if (p
->tcfp_result
&&
321 nla_put_u32(skb
, TCA_POLICE_RESULT
, p
->tcfp_result
))
322 goto nla_put_failure
;
323 if (p
->tcfp_ewma_rate
&&
324 nla_put_u32(skb
, TCA_POLICE_AVRATE
, p
->tcfp_ewma_rate
))
325 goto nla_put_failure
;
327 t
.install
= jiffies_to_clock_t(jiffies
- police
->tcf_tm
.install
);
328 t
.lastuse
= jiffies_to_clock_t(jiffies
- police
->tcf_tm
.lastuse
);
329 t
.firstuse
= jiffies_to_clock_t(jiffies
- police
->tcf_tm
.firstuse
);
330 t
.expires
= jiffies_to_clock_t(police
->tcf_tm
.expires
);
331 if (nla_put_64bit(skb
, TCA_POLICE_TM
, sizeof(t
), &t
, TCA_POLICE_PAD
))
332 goto nla_put_failure
;
333 spin_unlock_bh(&police
->tcf_lock
);
338 spin_unlock_bh(&police
->tcf_lock
);
343 static int tcf_police_search(struct net
*net
, struct tc_action
**a
, u32 index
)
345 struct tc_action_net
*tn
= net_generic(net
, police_net_id
);
347 return tcf_idr_search(tn
, a
, index
);
350 MODULE_AUTHOR("Alexey Kuznetsov");
351 MODULE_DESCRIPTION("Policing actions");
352 MODULE_LICENSE("GPL");
354 static struct tc_action_ops act_police_ops
= {
357 .owner
= THIS_MODULE
,
358 .stats_update
= tcf_police_stats_update
,
359 .act
= tcf_police_act
,
360 .dump
= tcf_police_dump
,
361 .init
= tcf_police_init
,
362 .walk
= tcf_police_walker
,
363 .lookup
= tcf_police_search
,
364 .cleanup
= tcf_police_cleanup
,
365 .size
= sizeof(struct tcf_police
),
368 static __net_init
int police_init_net(struct net
*net
)
370 struct tc_action_net
*tn
= net_generic(net
, police_net_id
);
372 return tc_action_net_init(tn
, &act_police_ops
);
375 static void __net_exit
police_exit_net(struct list_head
*net_list
)
377 tc_action_net_exit(net_list
, police_net_id
);
380 static struct pernet_operations police_net_ops
= {
381 .init
= police_init_net
,
382 .exit_batch
= police_exit_net
,
383 .id
= &police_net_id
,
384 .size
= sizeof(struct tc_action_net
),
387 static int __init
police_init_module(void)
389 return tcf_register_action(&act_police_ops
, &police_net_ops
);
392 static void __exit
police_cleanup_module(void)
394 tcf_unregister_action(&act_police_ops
, &police_net_ops
);
397 module_init(police_init_module
);
398 module_exit(police_cleanup_module
);