xref: /openbmc/linux/net/sched/act_skbedit.c (revision 1b34ec43c9b3de44a5420841ab293d1b2035a94c)
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
14ca9b0e27SAlexander Duyck  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15ca9b0e27SAlexander Duyck  * Place - Suite 330, Boston, MA 02111-1307 USA.
16ca9b0e27SAlexander Duyck  *
17ca9b0e27SAlexander Duyck  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
18ca9b0e27SAlexander Duyck  */
19ca9b0e27SAlexander Duyck 
20ca9b0e27SAlexander Duyck #include <linux/module.h>
21ca9b0e27SAlexander Duyck #include <linux/init.h>
22ca9b0e27SAlexander Duyck #include <linux/kernel.h>
23ca9b0e27SAlexander Duyck #include <linux/skbuff.h>
24ca9b0e27SAlexander Duyck #include <linux/rtnetlink.h>
25ca9b0e27SAlexander Duyck #include <net/netlink.h>
26ca9b0e27SAlexander Duyck #include <net/pkt_sched.h>
27ca9b0e27SAlexander Duyck 
28ca9b0e27SAlexander Duyck #include <linux/tc_act/tc_skbedit.h>
29ca9b0e27SAlexander Duyck #include <net/tc_act/tc_skbedit.h>
30ca9b0e27SAlexander Duyck 
31ca9b0e27SAlexander Duyck #define SKBEDIT_TAB_MASK     15
32ca9b0e27SAlexander Duyck static struct tcf_common *tcf_skbedit_ht[SKBEDIT_TAB_MASK + 1];
33ca9b0e27SAlexander Duyck static u32 skbedit_idx_gen;
34ca9b0e27SAlexander Duyck static DEFINE_RWLOCK(skbedit_lock);
35ca9b0e27SAlexander Duyck 
36ca9b0e27SAlexander Duyck static struct tcf_hashinfo skbedit_hash_info = {
37ca9b0e27SAlexander Duyck 	.htab	=	tcf_skbedit_ht,
38ca9b0e27SAlexander Duyck 	.hmask	=	SKBEDIT_TAB_MASK,
39ca9b0e27SAlexander Duyck 	.lock	=	&skbedit_lock,
40ca9b0e27SAlexander Duyck };
41ca9b0e27SAlexander Duyck 
42dc7f9f6eSEric Dumazet static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
43ca9b0e27SAlexander Duyck 		       struct tcf_result *res)
44ca9b0e27SAlexander Duyck {
45ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d = a->priv;
46ca9b0e27SAlexander Duyck 
47ca9b0e27SAlexander Duyck 	spin_lock(&d->tcf_lock);
48ca9b0e27SAlexander Duyck 	d->tcf_tm.lastuse = jiffies;
49bfe0d029SEric Dumazet 	bstats_update(&d->tcf_bstats, skb);
50ca9b0e27SAlexander Duyck 
51ca9b0e27SAlexander Duyck 	if (d->flags & SKBEDIT_F_PRIORITY)
52ca9b0e27SAlexander Duyck 		skb->priority = d->priority;
53ca9b0e27SAlexander Duyck 	if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
54ca9b0e27SAlexander Duyck 	    skb->dev->real_num_tx_queues > d->queue_mapping)
55ca9b0e27SAlexander Duyck 		skb_set_queue_mapping(skb, d->queue_mapping);
561c55d62eSjamal 	if (d->flags & SKBEDIT_F_MARK)
571c55d62eSjamal 		skb->mark = d->mark;
58ca9b0e27SAlexander Duyck 
59ca9b0e27SAlexander Duyck 	spin_unlock(&d->tcf_lock);
60ca9b0e27SAlexander Duyck 	return d->tcf_action;
61ca9b0e27SAlexander Duyck }
62ca9b0e27SAlexander Duyck 
63ca9b0e27SAlexander Duyck static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
64ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_PARMS]		= { .len = sizeof(struct tc_skbedit) },
65ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_PRIORITY]		= { .len = sizeof(u32) },
66ca9b0e27SAlexander Duyck 	[TCA_SKBEDIT_QUEUE_MAPPING]	= { .len = sizeof(u16) },
671c55d62eSjamal 	[TCA_SKBEDIT_MARK]		= { .len = sizeof(u32) },
68ca9b0e27SAlexander Duyck };
69ca9b0e27SAlexander Duyck 
70ca9b0e27SAlexander Duyck static int tcf_skbedit_init(struct nlattr *nla, struct nlattr *est,
71ca9b0e27SAlexander Duyck 			 struct tc_action *a, int ovr, int bind)
72ca9b0e27SAlexander Duyck {
73ca9b0e27SAlexander Duyck 	struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
74ca9b0e27SAlexander Duyck 	struct tc_skbedit *parm;
75ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d;
76ca9b0e27SAlexander Duyck 	struct tcf_common *pc;
771c55d62eSjamal 	u32 flags = 0, *priority = NULL, *mark = NULL;
78ca9b0e27SAlexander Duyck 	u16 *queue_mapping = NULL;
79ca9b0e27SAlexander Duyck 	int ret = 0, err;
80ca9b0e27SAlexander Duyck 
81ca9b0e27SAlexander Duyck 	if (nla == NULL)
82ca9b0e27SAlexander Duyck 		return -EINVAL;
83ca9b0e27SAlexander Duyck 
84ca9b0e27SAlexander Duyck 	err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
85ca9b0e27SAlexander Duyck 	if (err < 0)
86ca9b0e27SAlexander Duyck 		return err;
87ca9b0e27SAlexander Duyck 
88ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_PARMS] == NULL)
89ca9b0e27SAlexander Duyck 		return -EINVAL;
90ca9b0e27SAlexander Duyck 
91ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
92ca9b0e27SAlexander Duyck 		flags |= SKBEDIT_F_PRIORITY;
93ca9b0e27SAlexander Duyck 		priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
94ca9b0e27SAlexander Duyck 	}
95ca9b0e27SAlexander Duyck 
96ca9b0e27SAlexander Duyck 	if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
97ca9b0e27SAlexander Duyck 		flags |= SKBEDIT_F_QUEUE_MAPPING;
98ca9b0e27SAlexander Duyck 		queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
99ca9b0e27SAlexander Duyck 	}
1001c55d62eSjamal 
1011c55d62eSjamal 	if (tb[TCA_SKBEDIT_MARK] != NULL) {
1021c55d62eSjamal 		flags |= SKBEDIT_F_MARK;
1031c55d62eSjamal 		mark = nla_data(tb[TCA_SKBEDIT_MARK]);
1041c55d62eSjamal 	}
1051c55d62eSjamal 
106ca9b0e27SAlexander Duyck 	if (!flags)
107ca9b0e27SAlexander Duyck 		return -EINVAL;
108ca9b0e27SAlexander Duyck 
109ca9b0e27SAlexander Duyck 	parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
110ca9b0e27SAlexander Duyck 
111ca9b0e27SAlexander Duyck 	pc = tcf_hash_check(parm->index, a, bind, &skbedit_hash_info);
112ca9b0e27SAlexander Duyck 	if (!pc) {
113ca9b0e27SAlexander Duyck 		pc = tcf_hash_create(parm->index, est, a, sizeof(*d), bind,
114ca9b0e27SAlexander Duyck 				     &skbedit_idx_gen, &skbedit_hash_info);
1150e991ec6SStephen Hemminger 		if (IS_ERR(pc))
1160e991ec6SStephen Hemminger 			return PTR_ERR(pc);
117ca9b0e27SAlexander Duyck 
118ca9b0e27SAlexander Duyck 		d = to_skbedit(pc);
119ca9b0e27SAlexander Duyck 		ret = ACT_P_CREATED;
120ca9b0e27SAlexander Duyck 	} else {
121ca9b0e27SAlexander Duyck 		d = to_skbedit(pc);
122ca9b0e27SAlexander Duyck 		if (!ovr) {
123ca9b0e27SAlexander Duyck 			tcf_hash_release(pc, bind, &skbedit_hash_info);
124ca9b0e27SAlexander Duyck 			return -EEXIST;
125ca9b0e27SAlexander Duyck 		}
126ca9b0e27SAlexander Duyck 	}
127ca9b0e27SAlexander Duyck 
128ca9b0e27SAlexander Duyck 	spin_lock_bh(&d->tcf_lock);
129ca9b0e27SAlexander Duyck 
130ca9b0e27SAlexander Duyck 	d->flags = flags;
131ca9b0e27SAlexander Duyck 	if (flags & SKBEDIT_F_PRIORITY)
132ca9b0e27SAlexander Duyck 		d->priority = *priority;
133ca9b0e27SAlexander Duyck 	if (flags & SKBEDIT_F_QUEUE_MAPPING)
134ca9b0e27SAlexander Duyck 		d->queue_mapping = *queue_mapping;
1351c55d62eSjamal 	if (flags & SKBEDIT_F_MARK)
1361c55d62eSjamal 		d->mark = *mark;
1371c55d62eSjamal 
138ca9b0e27SAlexander Duyck 	d->tcf_action = parm->action;
139ca9b0e27SAlexander Duyck 
140ca9b0e27SAlexander Duyck 	spin_unlock_bh(&d->tcf_lock);
141ca9b0e27SAlexander Duyck 
142ca9b0e27SAlexander Duyck 	if (ret == ACT_P_CREATED)
143ca9b0e27SAlexander Duyck 		tcf_hash_insert(pc, &skbedit_hash_info);
144ca9b0e27SAlexander Duyck 	return ret;
145ca9b0e27SAlexander Duyck }
146ca9b0e27SAlexander Duyck 
147cc7ec456SEric Dumazet static int tcf_skbedit_cleanup(struct tc_action *a, int bind)
148ca9b0e27SAlexander Duyck {
149ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d = a->priv;
150ca9b0e27SAlexander Duyck 
151ca9b0e27SAlexander Duyck 	if (d)
152ca9b0e27SAlexander Duyck 		return tcf_hash_release(&d->common, bind, &skbedit_hash_info);
153ca9b0e27SAlexander Duyck 	return 0;
154ca9b0e27SAlexander Duyck }
155ca9b0e27SAlexander Duyck 
156cc7ec456SEric Dumazet static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
157ca9b0e27SAlexander Duyck 			    int bind, int ref)
158ca9b0e27SAlexander Duyck {
159ca9b0e27SAlexander Duyck 	unsigned char *b = skb_tail_pointer(skb);
160ca9b0e27SAlexander Duyck 	struct tcf_skbedit *d = a->priv;
1611c40be12SEric Dumazet 	struct tc_skbedit opt = {
1621c40be12SEric Dumazet 		.index   = d->tcf_index,
1631c40be12SEric Dumazet 		.refcnt  = d->tcf_refcnt - ref,
1641c40be12SEric Dumazet 		.bindcnt = d->tcf_bindcnt - bind,
1651c40be12SEric Dumazet 		.action  = d->tcf_action,
1661c40be12SEric Dumazet 	};
167ca9b0e27SAlexander Duyck 	struct tcf_t t;
168ca9b0e27SAlexander Duyck 
169*1b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
170*1b34ec43SDavid S. Miller 		goto nla_put_failure;
171*1b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_PRIORITY) &&
172*1b34ec43SDavid S. Miller 	    nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
173*1b34ec43SDavid S. Miller 		    &d->priority))
174*1b34ec43SDavid S. Miller 		goto nla_put_failure;
175*1b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
176*1b34ec43SDavid S. Miller 	    nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
177*1b34ec43SDavid S. Miller 		    sizeof(d->queue_mapping), &d->queue_mapping))
178*1b34ec43SDavid S. Miller 		goto nla_put_failure;
179*1b34ec43SDavid S. Miller 	if ((d->flags & SKBEDIT_F_MARK) &&
180*1b34ec43SDavid S. Miller 	    nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
181*1b34ec43SDavid S. Miller 		    &d->mark))
182*1b34ec43SDavid S. Miller 		goto nla_put_failure;
183ca9b0e27SAlexander Duyck 	t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
184ca9b0e27SAlexander Duyck 	t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
185ca9b0e27SAlexander Duyck 	t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
186*1b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
187*1b34ec43SDavid S. Miller 		goto nla_put_failure;
188ca9b0e27SAlexander Duyck 	return skb->len;
189ca9b0e27SAlexander Duyck 
190ca9b0e27SAlexander Duyck nla_put_failure:
191ca9b0e27SAlexander Duyck 	nlmsg_trim(skb, b);
192ca9b0e27SAlexander Duyck 	return -1;
193ca9b0e27SAlexander Duyck }
194ca9b0e27SAlexander Duyck 
195ca9b0e27SAlexander Duyck static struct tc_action_ops act_skbedit_ops = {
196ca9b0e27SAlexander Duyck 	.kind		=	"skbedit",
197ca9b0e27SAlexander Duyck 	.hinfo		=	&skbedit_hash_info,
198ca9b0e27SAlexander Duyck 	.type		=	TCA_ACT_SKBEDIT,
199ca9b0e27SAlexander Duyck 	.capab		=	TCA_CAP_NONE,
200ca9b0e27SAlexander Duyck 	.owner		=	THIS_MODULE,
201ca9b0e27SAlexander Duyck 	.act		=	tcf_skbedit,
202ca9b0e27SAlexander Duyck 	.dump		=	tcf_skbedit_dump,
203ca9b0e27SAlexander Duyck 	.cleanup	=	tcf_skbedit_cleanup,
204ca9b0e27SAlexander Duyck 	.init		=	tcf_skbedit_init,
205ca9b0e27SAlexander Duyck 	.walk		=	tcf_generic_walker,
206ca9b0e27SAlexander Duyck };
207ca9b0e27SAlexander Duyck 
208ca9b0e27SAlexander Duyck MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
209ca9b0e27SAlexander Duyck MODULE_DESCRIPTION("SKB Editing");
210ca9b0e27SAlexander Duyck MODULE_LICENSE("GPL");
211ca9b0e27SAlexander Duyck 
212ca9b0e27SAlexander Duyck static int __init skbedit_init_module(void)
213ca9b0e27SAlexander Duyck {
214ca9b0e27SAlexander Duyck 	return tcf_register_action(&act_skbedit_ops);
215ca9b0e27SAlexander Duyck }
216ca9b0e27SAlexander Duyck 
217ca9b0e27SAlexander Duyck static void __exit skbedit_cleanup_module(void)
218ca9b0e27SAlexander Duyck {
219ca9b0e27SAlexander Duyck 	tcf_unregister_action(&act_skbedit_ops);
220ca9b0e27SAlexander Duyck }
221ca9b0e27SAlexander Duyck 
222ca9b0e27SAlexander Duyck module_init(skbedit_init_module);
223ca9b0e27SAlexander Duyck module_exit(skbedit_cleanup_module);
224