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
31 static struct tcf_hashinfo skbedit_hash_info;
33 static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
34 struct tcf_result *res)
36 struct tcf_skbedit *d = a->priv;
38 spin_lock(&d->tcf_lock);
39 d->tcf_tm.lastuse = jiffies;
40 bstats_update(&d->tcf_bstats, skb);
42 if (d->flags & SKBEDIT_F_PRIORITY)
43 skb->priority = d->priority;
44 if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
45 skb->dev->real_num_tx_queues > d->queue_mapping)
46 skb_set_queue_mapping(skb, d->queue_mapping);
47 if (d->flags & SKBEDIT_F_MARK)
50 spin_unlock(&d->tcf_lock);
54 static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
55 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
56 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
57 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
58 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
61 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
62 struct nlattr *est, struct tc_action *a,
65 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
66 struct tc_skbedit *parm;
67 struct tcf_skbedit *d;
68 struct tcf_common *pc;
69 u32 flags = 0, *priority = NULL, *mark = NULL;
70 u16 *queue_mapping = NULL;
76 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
80 if (tb[TCA_SKBEDIT_PARMS] == NULL)
83 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
84 flags |= SKBEDIT_F_PRIORITY;
85 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
88 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
89 flags |= SKBEDIT_F_QUEUE_MAPPING;
90 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
93 if (tb[TCA_SKBEDIT_MARK] != NULL) {
94 flags |= SKBEDIT_F_MARK;
95 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
101 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
103 pc = tcf_hash_check(parm->index, a, bind);
105 pc = tcf_hash_create(parm->index, est, a, sizeof(*d), bind);
115 tcf_hash_release(pc, bind, a->ops->hinfo);
120 spin_lock_bh(&d->tcf_lock);
123 if (flags & SKBEDIT_F_PRIORITY)
124 d->priority = *priority;
125 if (flags & SKBEDIT_F_QUEUE_MAPPING)
126 d->queue_mapping = *queue_mapping;
127 if (flags & SKBEDIT_F_MARK)
130 d->tcf_action = parm->action;
132 spin_unlock_bh(&d->tcf_lock);
134 if (ret == ACT_P_CREATED)
135 tcf_hash_insert(pc, a->ops->hinfo);
139 static int tcf_skbedit_cleanup(struct tc_action *a, int bind)
141 struct tcf_skbedit *d = a->priv;
144 return tcf_hash_release(&d->common, bind, &skbedit_hash_info);
148 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
151 unsigned char *b = skb_tail_pointer(skb);
152 struct tcf_skbedit *d = a->priv;
153 struct tc_skbedit opt = {
154 .index = d->tcf_index,
155 .refcnt = d->tcf_refcnt - ref,
156 .bindcnt = d->tcf_bindcnt - bind,
157 .action = d->tcf_action,
161 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
162 goto nla_put_failure;
163 if ((d->flags & SKBEDIT_F_PRIORITY) &&
164 nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
166 goto nla_put_failure;
167 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
168 nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
169 sizeof(d->queue_mapping), &d->queue_mapping))
170 goto nla_put_failure;
171 if ((d->flags & SKBEDIT_F_MARK) &&
172 nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
174 goto nla_put_failure;
175 t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
176 t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
177 t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
178 if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
179 goto nla_put_failure;
187 static struct tc_action_ops act_skbedit_ops = {
189 .hinfo = &skbedit_hash_info,
190 .type = TCA_ACT_SKBEDIT,
191 .owner = THIS_MODULE,
193 .dump = tcf_skbedit_dump,
194 .cleanup = tcf_skbedit_cleanup,
195 .init = tcf_skbedit_init,
198 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
199 MODULE_DESCRIPTION("SKB Editing");
200 MODULE_LICENSE("GPL");
202 static int __init skbedit_init_module(void)
204 int err = tcf_hashinfo_init(&skbedit_hash_info, SKBEDIT_TAB_MASK);
207 return tcf_register_action(&act_skbedit_ops);
210 static void __exit skbedit_cleanup_module(void)
212 tcf_hashinfo_destroy(&skbedit_hash_info);
213 tcf_unregister_action(&act_skbedit_ops);
216 module_init(skbedit_init_module);
217 module_exit(skbedit_cleanup_module);