]> git.ipfire.org Git - people/ms/linux.git/blame - net/sched/act_skbedit.c
Merge tag 'tegra-for-5.20-arm64-defconfig' of git://git.kernel.org/pub/scm/linux...
[people/ms/linux.git] / net / sched / act_skbedit.c
CommitLineData
9952f691 1// SPDX-License-Identifier: GPL-2.0-only
ca9b0e27
AD
2/*
3 * Copyright (c) 2008, Intel Corporation.
4 *
ca9b0e27
AD
5 * Author: Alexander Duyck <alexander.h.duyck@intel.com>
6 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/skbuff.h>
12#include <linux/rtnetlink.h>
13#include <net/netlink.h>
14#include <net/pkt_sched.h>
e7e3728b
QF
15#include <net/ip.h>
16#include <net/ipv6.h>
17#include <net/dsfield.h>
ec7727bb 18#include <net/pkt_cls.h>
ca9b0e27
AD
19
20#include <linux/tc_act/tc_skbedit.h>
21#include <net/tc_act/tc_skbedit.h>
22
c7d03a00 23static unsigned int skbedit_net_id;
a85a970a 24static struct tc_action_ops act_skbedit_ops;
ddf97ccd 25
38a6f086
TZ
26static u16 tcf_skbedit_hash(struct tcf_skbedit_params *params,
27 struct sk_buff *skb)
28{
29 u16 queue_mapping = params->queue_mapping;
30
31 if (params->flags & SKBEDIT_F_TXQ_SKBHASH) {
32 u32 hash = skb_get_hash(skb);
33
34 queue_mapping += hash % params->mapping_mod;
35 }
36
37 return netdev_cap_txqueue(skb->dev, queue_mapping);
38}
39
45da1dac
JHS
40static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,
41 struct tcf_result *res)
ca9b0e27 42{
a85a970a 43 struct tcf_skbedit *d = to_skbedit(a);
c749cdda
DC
44 struct tcf_skbedit_params *params;
45 int action;
ca9b0e27 46
9c4a4e48 47 tcf_lastuse_update(&d->tcf_tm);
50dc9a85 48 bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb);
ca9b0e27 49
7fd4b288 50 params = rcu_dereference_bh(d->params);
c749cdda
DC
51 action = READ_ONCE(d->tcf_action);
52
53 if (params->flags & SKBEDIT_F_PRIORITY)
54 skb->priority = params->priority;
55 if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
e7e3728b
QF
56 int wlen = skb_network_offset(skb);
57
d7bf2ebe 58 switch (skb_protocol(skb, true)) {
e7e3728b
QF
59 case htons(ETH_P_IP):
60 wlen += sizeof(struct iphdr);
61 if (!pskb_may_pull(skb, wlen))
62 goto err;
63 skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
64 break;
65
66 case htons(ETH_P_IPV6):
67 wlen += sizeof(struct ipv6hdr);
68 if (!pskb_may_pull(skb, wlen))
69 goto err;
70 skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
71 break;
72 }
73 }
c749cdda 74 if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
2f1e85b1
TZ
75 skb->dev->real_num_tx_queues > params->queue_mapping) {
76#ifdef CONFIG_NET_EGRESS
77 netdev_xmit_skip_txqueue(true);
78#endif
38a6f086 79 skb_set_queue_mapping(skb, tcf_skbedit_hash(params, skb));
2f1e85b1 80 }
c749cdda
DC
81 if (params->flags & SKBEDIT_F_MARK) {
82 skb->mark &= ~params->mask;
83 skb->mark |= params->mark & params->mask;
4fe77d82 84 }
c749cdda
DC
85 if (params->flags & SKBEDIT_F_PTYPE)
86 skb->pkt_type = params->ptype;
c749cdda 87 return action;
7fd4b288 88
e7e3728b 89err:
6f3dfb0d 90 qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats));
7fd4b288 91 return TC_ACT_SHOT;
ca9b0e27
AD
92}
93
837cb17d 94static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes,
4b61d3e8
PL
95 u64 packets, u64 drops,
96 u64 lastuse, bool hw)
837cb17d
PM
97{
98 struct tcf_skbedit *d = to_skbedit(a);
99 struct tcf_t *tm = &d->tcf_tm;
100
4b61d3e8 101 tcf_action_update_stats(a, bytes, packets, drops, hw);
837cb17d
PM
102 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
103}
104
ca9b0e27
AD
105static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
106 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
107 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
108 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
1c55d62e 109 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
ff202ee1 110 [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
4fe77d82 111 [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
e7e3728b 112 [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) },
38a6f086 113 [TCA_SKBEDIT_QUEUE_MAPPING_MAX] = { .len = sizeof(u16) },
ca9b0e27
AD
114};
115
c1b52739 116static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
a85a970a 117 struct nlattr *est, struct tc_action **a,
abbb0d33 118 struct tcf_proto *tp, u32 act_flags,
789871bb 119 struct netlink_ext_ack *extack)
ca9b0e27 120{
ddf97ccd 121 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
695176bf 122 bool bind = act_flags & TCA_ACT_FLAGS_BIND;
6d7a8df6 123 struct tcf_skbedit_params *params_new;
ca9b0e27 124 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
ec7727bb 125 struct tcf_chain *goto_ch = NULL;
ca9b0e27
AD
126 struct tc_skbedit *parm;
127 struct tcf_skbedit *d;
4fe77d82 128 u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
ff202ee1 129 u16 *queue_mapping = NULL, *ptype = NULL;
38a6f086 130 u16 mapping_mod = 1;
b2313077
WC
131 bool exists = false;
132 int ret = 0, err;
7be8ef2c 133 u32 index;
ca9b0e27
AD
134
135 if (nla == NULL)
136 return -EINVAL;
137
8cb08174
JB
138 err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla,
139 skbedit_policy, NULL);
ca9b0e27
AD
140 if (err < 0)
141 return err;
142
143 if (tb[TCA_SKBEDIT_PARMS] == NULL)
144 return -EINVAL;
145
146 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
147 flags |= SKBEDIT_F_PRIORITY;
148 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
149 }
150
151 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
152 flags |= SKBEDIT_F_QUEUE_MAPPING;
153 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
154 }
1c55d62e 155
ff202ee1
JHS
156 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
157 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
158 if (!skb_pkt_type_ok(*ptype))
159 return -EINVAL;
160 flags |= SKBEDIT_F_PTYPE;
161 }
162
1c55d62e 163 if (tb[TCA_SKBEDIT_MARK] != NULL) {
164 flags |= SKBEDIT_F_MARK;
165 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
166 }
167
4fe77d82
AQ
168 if (tb[TCA_SKBEDIT_MASK] != NULL) {
169 flags |= SKBEDIT_F_MASK;
170 mask = nla_data(tb[TCA_SKBEDIT_MASK]);
171 }
172
e7e3728b
QF
173 if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
174 u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
175
38a6f086
TZ
176 if (*pure_flags & SKBEDIT_F_TXQ_SKBHASH) {
177 u16 *queue_mapping_max;
178
179 if (!tb[TCA_SKBEDIT_QUEUE_MAPPING] ||
180 !tb[TCA_SKBEDIT_QUEUE_MAPPING_MAX]) {
181 NL_SET_ERR_MSG_MOD(extack, "Missing required range of queue_mapping.");
182 return -EINVAL;
183 }
184
185 queue_mapping_max =
186 nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING_MAX]);
187 if (*queue_mapping_max < *queue_mapping) {
188 NL_SET_ERR_MSG_MOD(extack, "The range of queue_mapping is invalid, max < min.");
189 return -EINVAL;
190 }
191
192 mapping_mod = *queue_mapping_max - *queue_mapping + 1;
193 flags |= SKBEDIT_F_TXQ_SKBHASH;
194 }
e7e3728b
QF
195 if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
196 flags |= SKBEDIT_F_INHERITDSFIELD;
197 }
198
ca9b0e27 199 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
7be8ef2c
DL
200 index = parm->index;
201 err = tcf_idr_check_alloc(tn, &index, a, bind);
0190c1d4
VB
202 if (err < 0)
203 return err;
204 exists = err;
5e1567ae
JHS
205 if (exists && bind)
206 return 0;
207
208 if (!flags) {
af5d0184
RM
209 if (exists)
210 tcf_idr_release(*a, bind);
0190c1d4 211 else
7be8ef2c 212 tcf_idr_cleanup(tn, index);
5e1567ae
JHS
213 return -EINVAL;
214 }
215
216 if (!exists) {
7be8ef2c 217 ret = tcf_idr_create(tn, index, est, a,
40bd094d 218 &act_skbedit_ops, bind, true, act_flags);
0190c1d4 219 if (ret) {
7be8ef2c 220 tcf_idr_cleanup(tn, index);
86062033 221 return ret;
0190c1d4 222 }
ca9b0e27 223
a85a970a 224 d = to_skbedit(*a);
ca9b0e27
AD
225 ret = ACT_P_CREATED;
226 } else {
a85a970a 227 d = to_skbedit(*a);
695176bf 228 if (!(act_flags & TCA_ACT_FLAGS_REPLACE)) {
4e8ddd7f 229 tcf_idr_release(*a, bind);
ca9b0e27 230 return -EEXIST;
4e8ddd7f 231 }
ca9b0e27 232 }
ec7727bb
DC
233 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
234 if (err < 0)
235 goto release_idr;
ca9b0e27 236
c749cdda
DC
237 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
238 if (unlikely(!params_new)) {
ec7727bb
DC
239 err = -ENOMEM;
240 goto put_chain;
c749cdda
DC
241 }
242
243 params_new->flags = flags;
ca9b0e27 244 if (flags & SKBEDIT_F_PRIORITY)
c749cdda 245 params_new->priority = *priority;
38a6f086 246 if (flags & SKBEDIT_F_QUEUE_MAPPING) {
c749cdda 247 params_new->queue_mapping = *queue_mapping;
38a6f086
TZ
248 params_new->mapping_mod = mapping_mod;
249 }
1c55d62e 250 if (flags & SKBEDIT_F_MARK)
c749cdda 251 params_new->mark = *mark;
ff202ee1 252 if (flags & SKBEDIT_F_PTYPE)
c749cdda 253 params_new->ptype = *ptype;
4fe77d82 254 /* default behaviour is to use all the bits */
c749cdda 255 params_new->mask = 0xffffffff;
4fe77d82 256 if (flags & SKBEDIT_F_MASK)
c749cdda 257 params_new->mask = *mask;
1c55d62e 258
6d7a8df6 259 spin_lock_bh(&d->tcf_lock);
ec7727bb 260 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
445d3749
PM
261 params_new = rcu_replace_pointer(d->params, params_new,
262 lockdep_is_held(&d->tcf_lock));
6d7a8df6
VB
263 spin_unlock_bh(&d->tcf_lock);
264 if (params_new)
265 kfree_rcu(params_new, rcu);
ec7727bb
DC
266 if (goto_ch)
267 tcf_chain_put_by_act(goto_ch);
ca9b0e27 268
ca9b0e27 269 return ret;
ec7727bb
DC
270put_chain:
271 if (goto_ch)
272 tcf_chain_put_by_act(goto_ch);
273release_idr:
274 tcf_idr_release(*a, bind);
275 return err;
ca9b0e27
AD
276}
277
cc7ec456
ED
278static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
279 int bind, int ref)
ca9b0e27
AD
280{
281 unsigned char *b = skb_tail_pointer(skb);
a85a970a 282 struct tcf_skbedit *d = to_skbedit(a);
c749cdda 283 struct tcf_skbedit_params *params;
1c40be12
ED
284 struct tc_skbedit opt = {
285 .index = d->tcf_index,
036bb443
VB
286 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
287 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
1c40be12 288 };
e7e3728b 289 u64 pure_flags = 0;
c749cdda
DC
290 struct tcf_t t;
291
6d7a8df6
VB
292 spin_lock_bh(&d->tcf_lock);
293 params = rcu_dereference_protected(d->params,
294 lockdep_is_held(&d->tcf_lock));
295 opt.action = d->tcf_action;
ca9b0e27 296
1b34ec43
DM
297 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
298 goto nla_put_failure;
c749cdda
DC
299 if ((params->flags & SKBEDIT_F_PRIORITY) &&
300 nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
1b34ec43 301 goto nla_put_failure;
c749cdda
DC
302 if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
303 nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
1b34ec43 304 goto nla_put_failure;
c749cdda
DC
305 if ((params->flags & SKBEDIT_F_MARK) &&
306 nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
1b34ec43 307 goto nla_put_failure;
c749cdda
DC
308 if ((params->flags & SKBEDIT_F_PTYPE) &&
309 nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
ff202ee1 310 goto nla_put_failure;
c749cdda
DC
311 if ((params->flags & SKBEDIT_F_MASK) &&
312 nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
4fe77d82 313 goto nla_put_failure;
c749cdda 314 if (params->flags & SKBEDIT_F_INHERITDSFIELD)
e7e3728b 315 pure_flags |= SKBEDIT_F_INHERITDSFIELD;
38a6f086
TZ
316 if (params->flags & SKBEDIT_F_TXQ_SKBHASH) {
317 if (nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING_MAX,
318 params->queue_mapping + params->mapping_mod - 1))
319 goto nla_put_failure;
320
321 pure_flags |= SKBEDIT_F_TXQ_SKBHASH;
322 }
e7e3728b
QF
323 if (pure_flags != 0 &&
324 nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
325 goto nla_put_failure;
48d8ee16
JHS
326
327 tcf_tm_dump(&t, &d->tcf_tm);
9854518e 328 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
1b34ec43 329 goto nla_put_failure;
6d7a8df6
VB
330 spin_unlock_bh(&d->tcf_lock);
331
ca9b0e27
AD
332 return skb->len;
333
334nla_put_failure:
6d7a8df6 335 spin_unlock_bh(&d->tcf_lock);
ca9b0e27
AD
336 nlmsg_trim(skb, b);
337 return -1;
338}
339
c749cdda
DC
340static void tcf_skbedit_cleanup(struct tc_action *a)
341{
342 struct tcf_skbedit *d = to_skbedit(a);
343 struct tcf_skbedit_params *params;
344
345 params = rcu_dereference_protected(d->params, 1);
346 if (params)
347 kfree_rcu(params, rcu);
348}
349
ddf97ccd
WC
350static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
351 struct netlink_callback *cb, int type,
41780105
AA
352 const struct tc_action_ops *ops,
353 struct netlink_ext_ack *extack)
ddf97ccd
WC
354{
355 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
356
b3620145 357 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
358}
359
f061b48c 360static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
361{
362 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
363
65a206c0 364 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
365}
366
e1fea322
RM
367static size_t tcf_skbedit_get_fill_size(const struct tc_action *act)
368{
369 return nla_total_size(sizeof(struct tc_skbedit))
370 + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */
371 + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */
38a6f086 372 + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING_MAX */
e1fea322
RM
373 + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */
374 + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */
375 + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */
376 + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */
377}
378
c54e1d92 379static int tcf_skbedit_offload_act_setup(struct tc_action *act, void *entry_data,
c2ccf84e
IS
380 u32 *index_inc, bool bind,
381 struct netlink_ext_ack *extack)
c54e1d92
BZ
382{
383 if (bind) {
384 struct flow_action_entry *entry = entry_data;
385
386 if (is_tcf_skbedit_mark(act)) {
387 entry->id = FLOW_ACTION_MARK;
388 entry->mark = tcf_skbedit_mark(act);
389 } else if (is_tcf_skbedit_ptype(act)) {
390 entry->id = FLOW_ACTION_PTYPE;
391 entry->ptype = tcf_skbedit_ptype(act);
392 } else if (is_tcf_skbedit_priority(act)) {
393 entry->id = FLOW_ACTION_PRIORITY;
394 entry->priority = tcf_skbedit_priority(act);
a9c64939
IS
395 } else if (is_tcf_skbedit_queue_mapping(act)) {
396 NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"queue_mapping\" option is used");
397 return -EOPNOTSUPP;
398 } else if (is_tcf_skbedit_inheritdsfield(act)) {
399 NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"inheritdsfield\" option is used");
400 return -EOPNOTSUPP;
c54e1d92 401 } else {
a9c64939 402 NL_SET_ERR_MSG_MOD(extack, "Unsupported skbedit option offload");
c54e1d92
BZ
403 return -EOPNOTSUPP;
404 }
405 *index_inc = 1;
406 } else {
8cbfe939
BZ
407 struct flow_offload_action *fl_action = entry_data;
408
409 if (is_tcf_skbedit_mark(act))
410 fl_action->id = FLOW_ACTION_MARK;
411 else if (is_tcf_skbedit_ptype(act))
412 fl_action->id = FLOW_ACTION_PTYPE;
413 else if (is_tcf_skbedit_priority(act))
414 fl_action->id = FLOW_ACTION_PRIORITY;
415 else
416 return -EOPNOTSUPP;
c54e1d92
BZ
417 }
418
419 return 0;
420}
421
ca9b0e27
AD
422static struct tc_action_ops act_skbedit_ops = {
423 .kind = "skbedit",
eddd2cf1 424 .id = TCA_ID_SKBEDIT,
ca9b0e27 425 .owner = THIS_MODULE,
45da1dac 426 .act = tcf_skbedit_act,
837cb17d 427 .stats_update = tcf_skbedit_stats_update,
ca9b0e27 428 .dump = tcf_skbedit_dump,
ca9b0e27 429 .init = tcf_skbedit_init,
c749cdda 430 .cleanup = tcf_skbedit_cleanup,
ddf97ccd 431 .walk = tcf_skbedit_walker,
e1fea322 432 .get_fill_size = tcf_skbedit_get_fill_size,
ddf97ccd 433 .lookup = tcf_skbedit_search,
c54e1d92 434 .offload_act_setup = tcf_skbedit_offload_act_setup,
a85a970a 435 .size = sizeof(struct tcf_skbedit),
ddf97ccd
WC
436};
437
438static __net_init int skbedit_init_net(struct net *net)
439{
440 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
441
981471bd 442 return tc_action_net_init(net, tn, &act_skbedit_ops);
ddf97ccd
WC
443}
444
039af9c6 445static void __net_exit skbedit_exit_net(struct list_head *net_list)
ddf97ccd 446{
039af9c6 447 tc_action_net_exit(net_list, skbedit_net_id);
ddf97ccd
WC
448}
449
450static struct pernet_operations skbedit_net_ops = {
451 .init = skbedit_init_net,
039af9c6 452 .exit_batch = skbedit_exit_net,
ddf97ccd
WC
453 .id = &skbedit_net_id,
454 .size = sizeof(struct tc_action_net),
ca9b0e27
AD
455};
456
457MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
458MODULE_DESCRIPTION("SKB Editing");
459MODULE_LICENSE("GPL");
460
461static int __init skbedit_init_module(void)
462{
ddf97ccd 463 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
ca9b0e27
AD
464}
465
466static void __exit skbedit_cleanup_module(void)
467{
ddf97ccd 468 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
ca9b0e27
AD
469}
470
471module_init(skbedit_init_module);
472module_exit(skbedit_cleanup_module);