xref: /openbmc/linux/net/sched/act_skbedit.c (revision 48d8ee1694dd1ab25614b58f968123a4598f887e)
1ca9b0e27SAlexander Duyck /*
2ca9b0e27SAlexander Duyck  * Copyright (c) 2008, Intel Corporation.
3ca9b0e27SAlexander Duyck  *
4ca9b0e27SAlexander Duyck  * This program is free software; you can redistribute it and/or modify it
5ca9b0e27SAlexander Duyck  * under the terms and conditions of the GNU General Public License,
6ca9b0e27SAlexander Duyck  * version 2, as published by the Free Software Foundation.
7ca9b0e27SAlexander Duyck  *
8ca9b0e27SAlexander Duyck  * This program is distributed in the hope it will be useful, but WITHOUT
9ca9b0e27SAlexander Duyck  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10ca9b0e27SAlexander Duyck  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11ca9b0e27SAlexander Duyck  * more details.
12ca9b0e27SAlexander Duyck  *
13ca9b0e27SAlexander Duyck  * You should have received a copy of the GNU General Public License along with
14c057b190SJeff Kirsher  * this program; if not, see <http://www.gnu.org/licenses/>.
15ca9b0e27SAlexander Duyck  *
16ca9b0e27SAlexander Duyck  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
17ca9b0e27SAlexander Duyck  */
18ca9b0e27SAlexander Duyck 
19ca9b0e27SAlexander Duyck #include <linux/module.h>
20ca9b0e27SAlexander Duyck #include <linux/init.h>
21ca9b0e27SAlexander Duyck #include <linux/kernel.h>
22ca9b0e27SAlexander Duyck #include <linux/skbuff.h>
23ca9b0e27SAlexander Duyck #include <linux/rtnetlink.h>
24ca9b0e27SAlexander Duyck #include <net/netlink.h>
25ca9b0e27SAlexander Duyck #include <net/pkt_sched.h>
26ca9b0e27SAlexander Duyck 
27ca9b0e27SAlexander Duyck #include <linux/tc_act/tc_skbedit.h>
28ca9b0e27SAlexander Duyck #include <net/tc_act/tc_skbedit.h>
29ca9b0e27SAlexander Duyck 
30ca9b0e27SAlexander Duyck #define SKBEDIT_TAB_MASK     15
31ca9b0e27SAlexander Duyck 
32ddf97ccdSWANG Cong static int skbedit_net_id;
33ddf97ccdSWANG Cong 
34dc7f9f6eSEric Dumazet static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
35ca9b0e27SAlexander Duyck 		       struct tcf_result *res)
36ca9b0e27SAlexander Duyck {
37ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d = a->priv;
38ca9b0e27SAlexander Duyck 
39ca9b0e27SAlexander Duyck 	spin_lock(&d->tcf_lock);
409c4a4e48SJamal Hadi Salim 	tcf_lastuse_update(&d->tcf_tm);
41bfe0d029SEric Dumazet 	bstats_update(&d->tcf_bstats, skb);
42ca9b0e27SAlexander Duyck 
43ca9b0e27SAlexander Duyck 	if (d->flags & SKBEDIT_F_PRIORITY)
44ca9b0e27SAlexander Duyck 		skb->priority = d->priority;
45ca9b0e27SAlexander Duyck 	if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
46ca9b0e27SAlexander Duyck 	    skb->dev->real_num_tx_queues > d->queue_mapping)
47ca9b0e27SAlexander Duyck 		skb_set_queue_mapping(skb, d->queue_mapping);
481c55d62eSjamal 	if (d->flags & SKBEDIT_F_MARK)
491c55d62eSjamal 		skb->mark = d->mark;
50ca9b0e27SAlexander Duyck 
51ca9b0e27SAlexander Duyck 	spin_unlock(&d->tcf_lock);
52ca9b0e27SAlexander Duyck 	return d->tcf_action;
53ca9b0e27SAlexander Duyck }
54ca9b0e27SAlexander Duyck 
55ca9b0e27SAlexander Duyck static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
56ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_PARMS]		= { .len = sizeof(struct tc_skbedit) },
57ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_PRIORITY]		= { .len = sizeof(u32) },
58ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_QUEUE_MAPPING]	= { .len = sizeof(u16) },
591c55d62eSjamal 	[TCA_SKBEDIT_MARK]		= { .len = sizeof(u32) },
60ca9b0e27SAlexander Duyck };
61ca9b0e27SAlexander Duyck 
62c1b52739SBenjamin LaHaise static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
63c1b52739SBenjamin LaHaise 			    struct nlattr *est, struct tc_action *a,
64c1b52739SBenjamin LaHaise 			    int ovr, int bind)
65ca9b0e27SAlexander Duyck {
66ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
67ca9b0e27SAlexander Duyck 	struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
68ca9b0e27SAlexander Duyck 	struct tc_skbedit *parm;
69ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d;
701c55d62eSjamal 	u32 flags = 0, *priority = NULL, *mark = NULL;
71ca9b0e27SAlexander Duyck 	u16 *queue_mapping = NULL;
725e1567aeSJamal Hadi Salim 	int ret = 0, err, exists = 0;
73ca9b0e27SAlexander Duyck 
74ca9b0e27SAlexander Duyck 	if (nla == NULL)
75ca9b0e27SAlexander Duyck 		return -EINVAL;
76ca9b0e27SAlexander Duyck 
77ca9b0e27SAlexander Duyck 	err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
78ca9b0e27SAlexander Duyck 	if (err < 0)
79ca9b0e27SAlexander Duyck 		return err;
80ca9b0e27SAlexander Duyck 
81ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_PARMS] == NULL)
82ca9b0e27SAlexander Duyck 		return -EINVAL;
83ca9b0e27SAlexander Duyck 
84ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
85ca9b0e27SAlexander Duyck 		flags |= SKBEDIT_F_PRIORITY;
86ca9b0e27SAlexander Duyck 		priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
87ca9b0e27SAlexander Duyck 	}
88ca9b0e27SAlexander Duyck 
89ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
90ca9b0e27SAlexander Duyck 		flags |= SKBEDIT_F_QUEUE_MAPPING;
91ca9b0e27SAlexander Duyck 		queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
92ca9b0e27SAlexander Duyck 	}
931c55d62eSjamal 
941c55d62eSjamal 	if (tb[TCA_SKBEDIT_MARK] != NULL) {
951c55d62eSjamal 		flags |= SKBEDIT_F_MARK;
961c55d62eSjamal 		mark = nla_data(tb[TCA_SKBEDIT_MARK]);
971c55d62eSjamal 	}
981c55d62eSjamal 
99ca9b0e27SAlexander Duyck 	parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
100ca9b0e27SAlexander Duyck 
1015e1567aeSJamal Hadi Salim 	exists = tcf_hash_check(tn, parm->index, a, bind);
1025e1567aeSJamal Hadi Salim 	if (exists && bind)
1035e1567aeSJamal Hadi Salim 		return 0;
1045e1567aeSJamal Hadi Salim 
1055e1567aeSJamal Hadi Salim 	if (!flags) {
1065e1567aeSJamal Hadi Salim 		tcf_hash_release(a, bind);
1075e1567aeSJamal Hadi Salim 		return -EINVAL;
1085e1567aeSJamal Hadi Salim 	}
1095e1567aeSJamal Hadi Salim 
1105e1567aeSJamal Hadi Salim 	if (!exists) {
111ddf97ccdSWANG Cong 		ret = tcf_hash_create(tn, parm->index, est, a,
112ddf97ccdSWANG Cong 				      sizeof(*d), bind, false);
11386062033SWANG Cong 		if (ret)
11486062033SWANG Cong 			return ret;
115ca9b0e27SAlexander Duyck 
11686062033SWANG Cong 		d = to_skbedit(a);
117ca9b0e27SAlexander Duyck 		ret = ACT_P_CREATED;
118ca9b0e27SAlexander Duyck 	} else {
11986062033SWANG Cong 		d = to_skbedit(a);
12086062033SWANG Cong 		tcf_hash_release(a, bind);
1211a29321eSJamal Hadi Salim 		if (!ovr)
122ca9b0e27SAlexander Duyck 			return -EEXIST;
123ca9b0e27SAlexander Duyck 	}
124ca9b0e27SAlexander Duyck 
125ca9b0e27SAlexander Duyck 	spin_lock_bh(&d->tcf_lock);
126ca9b0e27SAlexander Duyck 
127ca9b0e27SAlexander Duyck 	d->flags = flags;
128ca9b0e27SAlexander Duyck 	if (flags & SKBEDIT_F_PRIORITY)
129ca9b0e27SAlexander Duyck 		d->priority = *priority;
130ca9b0e27SAlexander Duyck 	if (flags & SKBEDIT_F_QUEUE_MAPPING)
131ca9b0e27SAlexander Duyck 		d->queue_mapping = *queue_mapping;
1321c55d62eSjamal 	if (flags & SKBEDIT_F_MARK)
1331c55d62eSjamal 		d->mark = *mark;
1341c55d62eSjamal 
135ca9b0e27SAlexander Duyck 	d->tcf_action = parm->action;
136ca9b0e27SAlexander Duyck 
137ca9b0e27SAlexander Duyck 	spin_unlock_bh(&d->tcf_lock);
138ca9b0e27SAlexander Duyck 
139ca9b0e27SAlexander Duyck 	if (ret == ACT_P_CREATED)
140ddf97ccdSWANG Cong 		tcf_hash_insert(tn, a);
141ca9b0e27SAlexander Duyck 	return ret;
142ca9b0e27SAlexander Duyck }
143ca9b0e27SAlexander Duyck 
144cc7ec456SEric Dumazet static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
145ca9b0e27SAlexander Duyck 			    int bind, int ref)
146ca9b0e27SAlexander Duyck {
147ca9b0e27SAlexander Duyck 	unsigned char *b = skb_tail_pointer(skb);
148ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d = a->priv;
1491c40be12SEric Dumazet 	struct tc_skbedit opt = {
1501c40be12SEric Dumazet 		.index   = d->tcf_index,
1511c40be12SEric Dumazet 		.refcnt  = d->tcf_refcnt - ref,
1521c40be12SEric Dumazet 		.bindcnt = d->tcf_bindcnt - bind,
1531c40be12SEric Dumazet 		.action  = d->tcf_action,
1541c40be12SEric Dumazet 	};
155ca9b0e27SAlexander Duyck 	struct tcf_t t;
156ca9b0e27SAlexander Duyck 
1571b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
1581b34ec43SDavid S. Miller 		goto nla_put_failure;
1591b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_PRIORITY) &&
1601b34ec43SDavid S. Miller 	    nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
1611b34ec43SDavid S. Miller 		    &d->priority))
1621b34ec43SDavid S. Miller 		goto nla_put_failure;
1631b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
1641b34ec43SDavid S. Miller 	    nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
1651b34ec43SDavid S. Miller 		    sizeof(d->queue_mapping), &d->queue_mapping))
1661b34ec43SDavid S. Miller 		goto nla_put_failure;
1671b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_MARK) &&
1681b34ec43SDavid S. Miller 	    nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
1691b34ec43SDavid S. Miller 		    &d->mark))
1701b34ec43SDavid S. Miller 		goto nla_put_failure;
171*48d8ee16SJamal Hadi Salim 
172*48d8ee16SJamal Hadi Salim 	tcf_tm_dump(&t, &d->tcf_tm);
1739854518eSNicolas Dichtel 	if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
1741b34ec43SDavid S. Miller 		goto nla_put_failure;
175ca9b0e27SAlexander Duyck 	return skb->len;
176ca9b0e27SAlexander Duyck 
177ca9b0e27SAlexander Duyck nla_put_failure:
178ca9b0e27SAlexander Duyck 	nlmsg_trim(skb, b);
179ca9b0e27SAlexander Duyck 	return -1;
180ca9b0e27SAlexander Duyck }
181ca9b0e27SAlexander Duyck 
182ddf97ccdSWANG Cong static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
183ddf97ccdSWANG Cong 			      struct netlink_callback *cb, int type,
184ddf97ccdSWANG Cong 			      struct tc_action *a)
185ddf97ccdSWANG Cong {
186ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
187ddf97ccdSWANG Cong 
188ddf97ccdSWANG Cong 	return tcf_generic_walker(tn, skb, cb, type, a);
189ddf97ccdSWANG Cong }
190ddf97ccdSWANG Cong 
191ddf97ccdSWANG Cong static int tcf_skbedit_search(struct net *net, struct tc_action *a, u32 index)
192ddf97ccdSWANG Cong {
193ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
194ddf97ccdSWANG Cong 
195ddf97ccdSWANG Cong 	return tcf_hash_search(tn, a, index);
196ddf97ccdSWANG Cong }
197ddf97ccdSWANG Cong 
198ca9b0e27SAlexander Duyck static struct tc_action_ops act_skbedit_ops = {
199ca9b0e27SAlexander Duyck 	.kind		=	"skbedit",
200ca9b0e27SAlexander Duyck 	.type		=	TCA_ACT_SKBEDIT,
201ca9b0e27SAlexander Duyck 	.owner		=	THIS_MODULE,
202ca9b0e27SAlexander Duyck 	.act		=	tcf_skbedit,
203ca9b0e27SAlexander Duyck 	.dump		=	tcf_skbedit_dump,
204ca9b0e27SAlexander Duyck 	.init		=	tcf_skbedit_init,
205ddf97ccdSWANG Cong 	.walk		=	tcf_skbedit_walker,
206ddf97ccdSWANG Cong 	.lookup		=	tcf_skbedit_search,
207ddf97ccdSWANG Cong };
208ddf97ccdSWANG Cong 
209ddf97ccdSWANG Cong static __net_init int skbedit_init_net(struct net *net)
210ddf97ccdSWANG Cong {
211ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
212ddf97ccdSWANG Cong 
213ddf97ccdSWANG Cong 	return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
214ddf97ccdSWANG Cong }
215ddf97ccdSWANG Cong 
216ddf97ccdSWANG Cong static void __net_exit skbedit_exit_net(struct net *net)
217ddf97ccdSWANG Cong {
218ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, skbedit_net_id);
219ddf97ccdSWANG Cong 
220ddf97ccdSWANG Cong 	tc_action_net_exit(tn);
221ddf97ccdSWANG Cong }
222ddf97ccdSWANG Cong 
223ddf97ccdSWANG Cong static struct pernet_operations skbedit_net_ops = {
224ddf97ccdSWANG Cong 	.init = skbedit_init_net,
225ddf97ccdSWANG Cong 	.exit = skbedit_exit_net,
226ddf97ccdSWANG Cong 	.id   = &skbedit_net_id,
227ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
228ca9b0e27SAlexander Duyck };
229ca9b0e27SAlexander Duyck 
230ca9b0e27SAlexander Duyck MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
231ca9b0e27SAlexander Duyck MODULE_DESCRIPTION("SKB Editing");
232ca9b0e27SAlexander Duyck MODULE_LICENSE("GPL");
233ca9b0e27SAlexander Duyck 
234ca9b0e27SAlexander Duyck static int __init skbedit_init_module(void)
235ca9b0e27SAlexander Duyck {
236ddf97ccdSWANG Cong 	return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
237ca9b0e27SAlexander Duyck }
238ca9b0e27SAlexander Duyck 
239ca9b0e27SAlexander Duyck static void __exit skbedit_cleanup_module(void)
240ca9b0e27SAlexander Duyck {
241ddf97ccdSWANG Cong 	tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
242ca9b0e27SAlexander Duyck }
243ca9b0e27SAlexander Duyck 
244ca9b0e27SAlexander Duyck module_init(skbedit_init_module);
245ca9b0e27SAlexander Duyck module_exit(skbedit_cleanup_module);
246