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