2 * Copyright (c) 2008, Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
16 * Author: Alexander Duyck <alexander.h.duyck@intel.com>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
27 #include <linux/tc_act/tc_skbedit.h>
28 #include <net/tc_act/tc_skbedit.h>
30 #define SKBEDIT_TAB_MASK 15
32 static int skbedit_net_id;
34 static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
35 struct tcf_result *res)
37 struct tcf_skbedit *d = a->priv;
39 spin_lock(&d->tcf_lock);
40 tcf_lastuse_update(&d->tcf_tm);
41 bstats_update(&d->tcf_bstats, skb);
43 if (d->flags & SKBEDIT_F_PRIORITY)
44 skb->priority = d->priority;
45 if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
46 skb->dev->real_num_tx_queues > d->queue_mapping)
47 skb_set_queue_mapping(skb, d->queue_mapping);
48 if (d->flags & SKBEDIT_F_MARK)
50 if (d->flags & SKBEDIT_F_PTYPE)
51 skb->pkt_type = d->ptype;
53 spin_unlock(&d->tcf_lock);
57 static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
58 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
59 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
60 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
61 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
62 [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
65 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
66 struct nlattr *est, struct tc_action *a,
69 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
70 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
71 struct tc_skbedit *parm;
72 struct tcf_skbedit *d;
73 u32 flags = 0, *priority = NULL, *mark = NULL;
74 u16 *queue_mapping = NULL, *ptype = NULL;
81 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
85 if (tb[TCA_SKBEDIT_PARMS] == NULL)
88 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
89 flags |= SKBEDIT_F_PRIORITY;
90 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
93 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
94 flags |= SKBEDIT_F_QUEUE_MAPPING;
95 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
98 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
99 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
100 if (!skb_pkt_type_ok(*ptype))
102 flags |= SKBEDIT_F_PTYPE;
105 if (tb[TCA_SKBEDIT_MARK] != NULL) {
106 flags |= SKBEDIT_F_MARK;
107 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
110 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
112 exists = tcf_hash_check(tn, parm->index, a, bind);
117 tcf_hash_release(a, bind);
122 ret = tcf_hash_create(tn, parm->index, est, a,
123 sizeof(*d), bind, false);
131 tcf_hash_release(a, bind);
136 spin_lock_bh(&d->tcf_lock);
139 if (flags & SKBEDIT_F_PRIORITY)
140 d->priority = *priority;
141 if (flags & SKBEDIT_F_QUEUE_MAPPING)
142 d->queue_mapping = *queue_mapping;
143 if (flags & SKBEDIT_F_MARK)
145 if (flags & SKBEDIT_F_PTYPE)
148 d->tcf_action = parm->action;
150 spin_unlock_bh(&d->tcf_lock);
152 if (ret == ACT_P_CREATED)
153 tcf_hash_insert(tn, a);
157 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
160 unsigned char *b = skb_tail_pointer(skb);
161 struct tcf_skbedit *d = a->priv;
162 struct tc_skbedit opt = {
163 .index = d->tcf_index,
164 .refcnt = d->tcf_refcnt - ref,
165 .bindcnt = d->tcf_bindcnt - bind,
166 .action = d->tcf_action,
170 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
171 goto nla_put_failure;
172 if ((d->flags & SKBEDIT_F_PRIORITY) &&
173 nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
174 goto nla_put_failure;
175 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
176 nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
177 goto nla_put_failure;
178 if ((d->flags & SKBEDIT_F_MARK) &&
179 nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
180 goto nla_put_failure;
181 if ((d->flags & SKBEDIT_F_PTYPE) &&
182 nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
183 goto nla_put_failure;
185 tcf_tm_dump(&t, &d->tcf_tm);
186 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
187 goto nla_put_failure;
195 static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
196 struct netlink_callback *cb, int type,
199 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
201 return tcf_generic_walker(tn, skb, cb, type, a);
204 static int tcf_skbedit_search(struct net *net, struct tc_action *a, u32 index)
206 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
208 return tcf_hash_search(tn, a, index);
211 static struct tc_action_ops act_skbedit_ops = {
213 .type = TCA_ACT_SKBEDIT,
214 .owner = THIS_MODULE,
216 .dump = tcf_skbedit_dump,
217 .init = tcf_skbedit_init,
218 .walk = tcf_skbedit_walker,
219 .lookup = tcf_skbedit_search,
222 static __net_init int skbedit_init_net(struct net *net)
224 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
226 return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
229 static void __net_exit skbedit_exit_net(struct net *net)
231 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
233 tc_action_net_exit(tn);
236 static struct pernet_operations skbedit_net_ops = {
237 .init = skbedit_init_net,
238 .exit = skbedit_exit_net,
239 .id = &skbedit_net_id,
240 .size = sizeof(struct tc_action_net),
243 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
244 MODULE_DESCRIPTION("SKB Editing");
245 MODULE_LICENSE("GPL");
247 static int __init skbedit_init_module(void)
249 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
252 static void __exit skbedit_cleanup_module(void)
254 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
257 module_init(skbedit_init_module);
258 module_exit(skbedit_cleanup_module);