9f91928fcaebdd1d9effb25d15f57814ee92f8cf
[deliverable/linux.git] / net / sched / act_skbedit.c
1 /*
2 * Copyright (c) 2008, Intel Corporation.
3 *
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.
7 *
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
11 * more details.
12 *
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/>.
15 *
16 * Author: Alexander Duyck <alexander.h.duyck@intel.com>
17 */
18
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>
26
27 #include <linux/tc_act/tc_skbedit.h>
28 #include <net/tc_act/tc_skbedit.h>
29
30 #define SKBEDIT_TAB_MASK 15
31 static struct tcf_hashinfo skbedit_hash_info;
32
33 static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
34 struct tcf_result *res)
35 {
36 struct tcf_skbedit *d = a->priv;
37
38 spin_lock(&d->tcf_lock);
39 d->tcf_tm.lastuse = jiffies;
40 bstats_update(&d->tcf_bstats, skb);
41
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)
48 skb->mark = d->mark;
49
50 spin_unlock(&d->tcf_lock);
51 return d->tcf_action;
52 }
53
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) },
59 };
60
61 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
62 struct nlattr *est, struct tc_action *a,
63 int ovr, int bind)
64 {
65 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
66 struct tc_skbedit *parm;
67 struct tcf_skbedit *d;
68 u32 flags = 0, *priority = NULL, *mark = NULL;
69 u16 *queue_mapping = NULL;
70 int ret = 0, err;
71
72 if (nla == NULL)
73 return -EINVAL;
74
75 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
76 if (err < 0)
77 return err;
78
79 if (tb[TCA_SKBEDIT_PARMS] == NULL)
80 return -EINVAL;
81
82 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
83 flags |= SKBEDIT_F_PRIORITY;
84 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
85 }
86
87 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
88 flags |= SKBEDIT_F_QUEUE_MAPPING;
89 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
90 }
91
92 if (tb[TCA_SKBEDIT_MARK] != NULL) {
93 flags |= SKBEDIT_F_MARK;
94 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
95 }
96
97 if (!flags)
98 return -EINVAL;
99
100 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
101
102 if (!tcf_hash_check(parm->index, a, bind)) {
103 ret = tcf_hash_create(parm->index, est, a, sizeof(*d), bind);
104 if (ret)
105 return ret;
106
107 d = to_skbedit(a);
108 ret = ACT_P_CREATED;
109 } else {
110 d = to_skbedit(a);
111 if (bind)
112 return 0;
113 tcf_hash_release(a, bind);
114 if (!ovr)
115 return -EEXIST;
116 }
117
118 spin_lock_bh(&d->tcf_lock);
119
120 d->flags = flags;
121 if (flags & SKBEDIT_F_PRIORITY)
122 d->priority = *priority;
123 if (flags & SKBEDIT_F_QUEUE_MAPPING)
124 d->queue_mapping = *queue_mapping;
125 if (flags & SKBEDIT_F_MARK)
126 d->mark = *mark;
127
128 d->tcf_action = parm->action;
129
130 spin_unlock_bh(&d->tcf_lock);
131
132 if (ret == ACT_P_CREATED)
133 tcf_hash_insert(a);
134 return ret;
135 }
136
137 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
138 int bind, int ref)
139 {
140 unsigned char *b = skb_tail_pointer(skb);
141 struct tcf_skbedit *d = a->priv;
142 struct tc_skbedit opt = {
143 .index = d->tcf_index,
144 .refcnt = d->tcf_refcnt - ref,
145 .bindcnt = d->tcf_bindcnt - bind,
146 .action = d->tcf_action,
147 };
148 struct tcf_t t;
149
150 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
151 goto nla_put_failure;
152 if ((d->flags & SKBEDIT_F_PRIORITY) &&
153 nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
154 &d->priority))
155 goto nla_put_failure;
156 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
157 nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
158 sizeof(d->queue_mapping), &d->queue_mapping))
159 goto nla_put_failure;
160 if ((d->flags & SKBEDIT_F_MARK) &&
161 nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
162 &d->mark))
163 goto nla_put_failure;
164 t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
165 t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
166 t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
167 if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
168 goto nla_put_failure;
169 return skb->len;
170
171 nla_put_failure:
172 nlmsg_trim(skb, b);
173 return -1;
174 }
175
176 static struct tc_action_ops act_skbedit_ops = {
177 .kind = "skbedit",
178 .hinfo = &skbedit_hash_info,
179 .type = TCA_ACT_SKBEDIT,
180 .owner = THIS_MODULE,
181 .act = tcf_skbedit,
182 .dump = tcf_skbedit_dump,
183 .init = tcf_skbedit_init,
184 };
185
186 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
187 MODULE_DESCRIPTION("SKB Editing");
188 MODULE_LICENSE("GPL");
189
190 static int __init skbedit_init_module(void)
191 {
192 int err = tcf_hashinfo_init(&skbedit_hash_info, SKBEDIT_TAB_MASK);
193 if (err)
194 return err;
195 return tcf_register_action(&act_skbedit_ops);
196 }
197
198 static void __exit skbedit_cleanup_module(void)
199 {
200 tcf_hashinfo_destroy(&skbedit_hash_info);
201 tcf_unregister_action(&act_skbedit_ops);
202 }
203
204 module_init(skbedit_init_module);
205 module_exit(skbedit_cleanup_module);
This page took 0.033387 seconds and 4 git commands to generate.