]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
0c6965dd | 3 | * net/sched/act_gact.c Generic actions |
1da177e4 | 4 | * |
1da177e4 | 5 | * copyright Jamal Hadi Salim (2002-4) |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 LT |
8 | #include <linux/types.h> |
9 | #include <linux/kernel.h> | |
1da177e4 | 10 | #include <linux/string.h> |
1da177e4 | 11 | #include <linux/errno.h> |
1da177e4 LT |
12 | #include <linux/skbuff.h> |
13 | #include <linux/rtnetlink.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/init.h> | |
dc5fc579 | 16 | #include <net/netlink.h> |
1da177e4 | 17 | #include <net/pkt_sched.h> |
0da2dbd6 | 18 | #include <net/pkt_cls.h> |
1da177e4 LT |
19 | #include <linux/tc_act/tc_gact.h> |
20 | #include <net/tc_act/tc_gact.h> | |
21 | ||
c7d03a00 | 22 | static unsigned int gact_net_id; |
a85a970a | 23 | static struct tc_action_ops act_gact_ops; |
ddf97ccd | 24 | |
1da177e4 | 25 | #ifdef CONFIG_GACT_PROB |
e9ce1cd3 | 26 | static int gact_net_rand(struct tcf_gact *gact) |
1da177e4 | 27 | { |
cef5ecf9 ED |
28 | smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ |
29 | if (prandom_u32() % gact->tcfg_pval) | |
e9ce1cd3 DM |
30 | return gact->tcf_action; |
31 | return gact->tcfg_paction; | |
1da177e4 LT |
32 | } |
33 | ||
e9ce1cd3 | 34 | static int gact_determ(struct tcf_gact *gact) |
1da177e4 | 35 | { |
cc6510a9 ED |
36 | u32 pack = atomic_inc_return(&gact->packets); |
37 | ||
cef5ecf9 | 38 | smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ |
cc6510a9 | 39 | if (pack % gact->tcfg_pval) |
e9ce1cd3 DM |
40 | return gact->tcf_action; |
41 | return gact->tcfg_paction; | |
1da177e4 LT |
42 | } |
43 | ||
e9ce1cd3 | 44 | typedef int (*g_rand)(struct tcf_gact *gact); |
cc7ec456 | 45 | static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ }; |
e9ce1cd3 | 46 | #endif /* CONFIG_GACT_PROB */ |
1da177e4 | 47 | |
53b2bf3f PM |
48 | static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = { |
49 | [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) }, | |
50 | [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) }, | |
51 | }; | |
52 | ||
c1b52739 | 53 | static int tcf_gact_init(struct net *net, struct nlattr *nla, |
a85a970a | 54 | struct nlattr *est, struct tc_action **a, |
789871bb | 55 | int ovr, int bind, bool rtnl_held, |
85d0966f | 56 | struct tcf_proto *tp, struct netlink_ext_ack *extack) |
1da177e4 | 57 | { |
ddf97ccd | 58 | struct tc_action_net *tn = net_generic(net, gact_net_id); |
7ba699c6 | 59 | struct nlattr *tb[TCA_GACT_MAX + 1]; |
0da2dbd6 | 60 | struct tcf_chain *goto_ch = NULL; |
1da177e4 | 61 | struct tc_gact *parm; |
e9ce1cd3 | 62 | struct tcf_gact *gact; |
1da177e4 | 63 | int ret = 0; |
7be8ef2c | 64 | u32 index; |
cee63723 | 65 | int err; |
696ecdc1 HS |
66 | #ifdef CONFIG_GACT_PROB |
67 | struct tc_gact_p *p_parm = NULL; | |
68 | #endif | |
1da177e4 | 69 | |
cee63723 | 70 | if (nla == NULL) |
1da177e4 LT |
71 | return -EINVAL; |
72 | ||
8cb08174 JB |
73 | err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy, |
74 | NULL); | |
cee63723 PM |
75 | if (err < 0) |
76 | return err; | |
77 | ||
53b2bf3f | 78 | if (tb[TCA_GACT_PARMS] == NULL) |
1da177e4 | 79 | return -EINVAL; |
7ba699c6 | 80 | parm = nla_data(tb[TCA_GACT_PARMS]); |
7be8ef2c | 81 | index = parm->index; |
1da177e4 | 82 | |
53b2bf3f | 83 | #ifndef CONFIG_GACT_PROB |
7ba699c6 | 84 | if (tb[TCA_GACT_PROB] != NULL) |
1da177e4 | 85 | return -EOPNOTSUPP; |
696ecdc1 HS |
86 | #else |
87 | if (tb[TCA_GACT_PROB]) { | |
88 | p_parm = nla_data(tb[TCA_GACT_PROB]); | |
89 | if (p_parm->ptype >= MAX_RAND) | |
90 | return -EINVAL; | |
9469f375 DC |
91 | if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) { |
92 | NL_SET_ERR_MSG(extack, | |
93 | "goto chain not allowed on fallback"); | |
94 | return -EINVAL; | |
95 | } | |
696ecdc1 | 96 | } |
1da177e4 LT |
97 | #endif |
98 | ||
7be8ef2c | 99 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
0190c1d4 | 100 | if (!err) { |
7be8ef2c | 101 | ret = tcf_idr_create(tn, index, est, a, |
65a206c0 | 102 | &act_gact_ops, bind, true); |
0190c1d4 | 103 | if (ret) { |
7be8ef2c | 104 | tcf_idr_cleanup(tn, index); |
86062033 | 105 | return ret; |
0190c1d4 | 106 | } |
1da177e4 | 107 | ret = ACT_P_CREATED; |
0190c1d4 | 108 | } else if (err > 0) { |
1a29321e JHS |
109 | if (bind)/* dont override defaults */ |
110 | return 0; | |
4e8ddd7f VB |
111 | if (!ovr) { |
112 | tcf_idr_release(*a, bind); | |
1da177e4 | 113 | return -EEXIST; |
4e8ddd7f | 114 | } |
0190c1d4 VB |
115 | } else { |
116 | return err; | |
1da177e4 LT |
117 | } |
118 | ||
0da2dbd6 DC |
119 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
120 | if (err < 0) | |
121 | goto release_idr; | |
a85a970a | 122 | gact = to_gact(*a); |
e9ce1cd3 | 123 | |
653cd284 | 124 | spin_lock_bh(&gact->tcf_lock); |
0da2dbd6 | 125 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
1da177e4 | 126 | #ifdef CONFIG_GACT_PROB |
696ecdc1 | 127 | if (p_parm) { |
e9ce1cd3 | 128 | gact->tcfg_paction = p_parm->paction; |
cef5ecf9 ED |
129 | gact->tcfg_pval = max_t(u16, 1, p_parm->pval); |
130 | /* Make sure tcfg_pval is written before tcfg_ptype | |
131 | * coupled with smp_rmb() in gact_net_rand() & gact_determ() | |
132 | */ | |
133 | smp_wmb(); | |
e9ce1cd3 | 134 | gact->tcfg_ptype = p_parm->ptype; |
1da177e4 LT |
135 | } |
136 | #endif | |
653cd284 | 137 | spin_unlock_bh(&gact->tcf_lock); |
e8917f43 | 138 | |
0da2dbd6 DC |
139 | if (goto_ch) |
140 | tcf_chain_put_by_act(goto_ch); | |
141 | ||
1da177e4 | 142 | if (ret == ACT_P_CREATED) |
65a206c0 | 143 | tcf_idr_insert(tn, *a); |
1da177e4 | 144 | return ret; |
0da2dbd6 DC |
145 | release_idr: |
146 | tcf_idr_release(*a, bind); | |
147 | return err; | |
1da177e4 LT |
148 | } |
149 | ||
1740005e JHS |
150 | static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a, |
151 | struct tcf_result *res) | |
1da177e4 | 152 | { |
a85a970a | 153 | struct tcf_gact *gact = to_gact(a); |
56e5d1ca | 154 | int action = READ_ONCE(gact->tcf_action); |
1da177e4 | 155 | |
1da177e4 | 156 | #ifdef CONFIG_GACT_PROB |
8f2ae965 ED |
157 | { |
158 | u32 ptype = READ_ONCE(gact->tcfg_ptype); | |
159 | ||
160 | if (ptype) | |
161 | action = gact_rand[ptype](gact); | |
162 | } | |
1da177e4 | 163 | #endif |
56e5d1ca | 164 | bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb); |
1da177e4 | 165 | if (action == TC_ACT_SHOT) |
56e5d1ca ED |
166 | qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats)); |
167 | ||
168 | tcf_lastuse_update(&gact->tcf_tm); | |
1da177e4 LT |
169 | |
170 | return action; | |
171 | } | |
172 | ||
9fea47d9 | 173 | static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets, |
28169aba | 174 | u64 lastuse, bool hw) |
9fea47d9 | 175 | { |
a85a970a | 176 | struct tcf_gact *gact = to_gact(a); |
9fea47d9 AV |
177 | int action = READ_ONCE(gact->tcf_action); |
178 | struct tcf_t *tm = &gact->tcf_tm; | |
179 | ||
5a7a5555 JHS |
180 | _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes, |
181 | packets); | |
9fea47d9 AV |
182 | if (action == TC_ACT_SHOT) |
183 | this_cpu_ptr(gact->common.cpu_qstats)->drops += packets; | |
184 | ||
28169aba EC |
185 | if (hw) |
186 | _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats_hw), | |
187 | bytes, packets); | |
188 | ||
3bb23421 | 189 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
9fea47d9 AV |
190 | } |
191 | ||
0b0f43fe JHS |
192 | static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, |
193 | int bind, int ref) | |
1da177e4 | 194 | { |
27a884dc | 195 | unsigned char *b = skb_tail_pointer(skb); |
a85a970a | 196 | struct tcf_gact *gact = to_gact(a); |
1c40be12 ED |
197 | struct tc_gact opt = { |
198 | .index = gact->tcf_index, | |
036bb443 VB |
199 | .refcnt = refcount_read(&gact->tcf_refcnt) - ref, |
200 | .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind, | |
1c40be12 | 201 | }; |
1da177e4 LT |
202 | struct tcf_t t; |
203 | ||
653cd284 | 204 | spin_lock_bh(&gact->tcf_lock); |
e8917f43 | 205 | opt.action = gact->tcf_action; |
1b34ec43 DM |
206 | if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt)) |
207 | goto nla_put_failure; | |
1da177e4 | 208 | #ifdef CONFIG_GACT_PROB |
e9ce1cd3 | 209 | if (gact->tcfg_ptype) { |
1c40be12 ED |
210 | struct tc_gact_p p_opt = { |
211 | .paction = gact->tcfg_paction, | |
212 | .pval = gact->tcfg_pval, | |
213 | .ptype = gact->tcfg_ptype, | |
214 | }; | |
215 | ||
1b34ec43 DM |
216 | if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt)) |
217 | goto nla_put_failure; | |
1da177e4 LT |
218 | } |
219 | #endif | |
48d8ee16 | 220 | tcf_tm_dump(&t, &gact->tcf_tm); |
9854518e | 221 | if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD)) |
1b34ec43 | 222 | goto nla_put_failure; |
653cd284 | 223 | spin_unlock_bh(&gact->tcf_lock); |
e8917f43 | 224 | |
1da177e4 LT |
225 | return skb->len; |
226 | ||
7ba699c6 | 227 | nla_put_failure: |
653cd284 | 228 | spin_unlock_bh(&gact->tcf_lock); |
dc5fc579 | 229 | nlmsg_trim(skb, b); |
1da177e4 LT |
230 | return -1; |
231 | } | |
232 | ||
ddf97ccd WC |
233 | static int tcf_gact_walker(struct net *net, struct sk_buff *skb, |
234 | struct netlink_callback *cb, int type, | |
41780105 AA |
235 | const struct tc_action_ops *ops, |
236 | struct netlink_ext_ack *extack) | |
ddf97ccd WC |
237 | { |
238 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
239 | ||
b3620145 | 240 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccd WC |
241 | } |
242 | ||
f061b48c | 243 | static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
244 | { |
245 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
246 | ||
65a206c0 | 247 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
248 | } |
249 | ||
9c5c9c57 RM |
250 | static size_t tcf_gact_get_fill_size(const struct tc_action *act) |
251 | { | |
252 | size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */ | |
253 | ||
254 | #ifdef CONFIG_GACT_PROB | |
255 | if (to_gact(act)->tcfg_ptype) | |
256 | /* TCA_GACT_PROB */ | |
257 | sz += nla_total_size(sizeof(struct tc_gact_p)); | |
258 | #endif | |
259 | ||
260 | return sz; | |
261 | } | |
262 | ||
1da177e4 LT |
263 | static struct tc_action_ops act_gact_ops = { |
264 | .kind = "gact", | |
eddd2cf1 | 265 | .id = TCA_ID_GACT, |
1da177e4 | 266 | .owner = THIS_MODULE, |
1740005e | 267 | .act = tcf_gact_act, |
9fea47d9 | 268 | .stats_update = tcf_gact_stats_update, |
1da177e4 | 269 | .dump = tcf_gact_dump, |
1da177e4 | 270 | .init = tcf_gact_init, |
ddf97ccd WC |
271 | .walk = tcf_gact_walker, |
272 | .lookup = tcf_gact_search, | |
9c5c9c57 | 273 | .get_fill_size = tcf_gact_get_fill_size, |
a85a970a | 274 | .size = sizeof(struct tcf_gact), |
ddf97ccd WC |
275 | }; |
276 | ||
277 | static __net_init int gact_init_net(struct net *net) | |
278 | { | |
279 | struct tc_action_net *tn = net_generic(net, gact_net_id); | |
280 | ||
981471bd | 281 | return tc_action_net_init(net, tn, &act_gact_ops); |
ddf97ccd WC |
282 | } |
283 | ||
039af9c6 | 284 | static void __net_exit gact_exit_net(struct list_head *net_list) |
ddf97ccd | 285 | { |
039af9c6 | 286 | tc_action_net_exit(net_list, gact_net_id); |
ddf97ccd WC |
287 | } |
288 | ||
289 | static struct pernet_operations gact_net_ops = { | |
290 | .init = gact_init_net, | |
039af9c6 | 291 | .exit_batch = gact_exit_net, |
ddf97ccd WC |
292 | .id = &gact_net_id, |
293 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
294 | }; |
295 | ||
296 | MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); | |
297 | MODULE_DESCRIPTION("Generic Classifier actions"); | |
298 | MODULE_LICENSE("GPL"); | |
299 | ||
e9ce1cd3 | 300 | static int __init gact_init_module(void) |
1da177e4 LT |
301 | { |
302 | #ifdef CONFIG_GACT_PROB | |
cc7ec456 | 303 | pr_info("GACT probability on\n"); |
1da177e4 | 304 | #else |
cc7ec456 | 305 | pr_info("GACT probability NOT on\n"); |
1da177e4 | 306 | #endif |
ddf97ccd WC |
307 | |
308 | return tcf_register_action(&act_gact_ops, &gact_net_ops); | |
1da177e4 LT |
309 | } |
310 | ||
e9ce1cd3 | 311 | static void __exit gact_cleanup_module(void) |
1da177e4 | 312 | { |
ddf97ccd | 313 | tcf_unregister_action(&act_gact_ops, &gact_net_ops); |
1da177e4 LT |
314 | } |
315 | ||
316 | module_init(gact_init_module); | |
317 | module_exit(gact_cleanup_module); |