8e573c0f874237b86a2c1407c62f6ee5c05c275c
[cascardo/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
32 static int skbedit_net_id;
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         tcf_lastuse_update(&d->tcf_tm);
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         if (d->flags & SKBEDIT_F_PTYPE)
51                 skb->pkt_type = d->ptype;
52
53         spin_unlock(&d->tcf_lock);
54         return d->tcf_action;
55 }
56
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) },
63 };
64
65 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
66                             struct nlattr *est, struct tc_action *a,
67                             int ovr, int bind)
68 {
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;
75         bool exists = false;
76         int ret = 0, err;
77
78         if (nla == NULL)
79                 return -EINVAL;
80
81         err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
82         if (err < 0)
83                 return err;
84
85         if (tb[TCA_SKBEDIT_PARMS] == NULL)
86                 return -EINVAL;
87
88         if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
89                 flags |= SKBEDIT_F_PRIORITY;
90                 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
91         }
92
93         if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
94                 flags |= SKBEDIT_F_QUEUE_MAPPING;
95                 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
96         }
97
98         if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
99                 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
100                 if (!skb_pkt_type_ok(*ptype))
101                         return -EINVAL;
102                 flags |= SKBEDIT_F_PTYPE;
103         }
104
105         if (tb[TCA_SKBEDIT_MARK] != NULL) {
106                 flags |= SKBEDIT_F_MARK;
107                 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
108         }
109
110         parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
111
112         exists = tcf_hash_check(tn, parm->index, a, bind);
113         if (exists && bind)
114                 return 0;
115
116         if (!flags) {
117                 tcf_hash_release(a, bind);
118                 return -EINVAL;
119         }
120
121         if (!exists) {
122                 ret = tcf_hash_create(tn, parm->index, est, a,
123                                       sizeof(*d), bind, false);
124                 if (ret)
125                         return ret;
126
127                 d = to_skbedit(a);
128                 ret = ACT_P_CREATED;
129         } else {
130                 d = to_skbedit(a);
131                 tcf_hash_release(a, bind);
132                 if (!ovr)
133                         return -EEXIST;
134         }
135
136         spin_lock_bh(&d->tcf_lock);
137
138         d->flags = flags;
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)
144                 d->mark = *mark;
145         if (flags & SKBEDIT_F_PTYPE)
146                 d->ptype = *ptype;
147
148         d->tcf_action = parm->action;
149
150         spin_unlock_bh(&d->tcf_lock);
151
152         if (ret == ACT_P_CREATED)
153                 tcf_hash_insert(tn, a);
154         return ret;
155 }
156
157 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
158                             int bind, int ref)
159 {
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,
167         };
168         struct tcf_t t;
169
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;
184
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;
188         return skb->len;
189
190 nla_put_failure:
191         nlmsg_trim(skb, b);
192         return -1;
193 }
194
195 static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
196                               struct netlink_callback *cb, int type,
197                               struct tc_action *a)
198 {
199         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
200
201         return tcf_generic_walker(tn, skb, cb, type, a);
202 }
203
204 static int tcf_skbedit_search(struct net *net, struct tc_action *a, u32 index)
205 {
206         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
207
208         return tcf_hash_search(tn, a, index);
209 }
210
211 static struct tc_action_ops act_skbedit_ops = {
212         .kind           =       "skbedit",
213         .type           =       TCA_ACT_SKBEDIT,
214         .owner          =       THIS_MODULE,
215         .act            =       tcf_skbedit,
216         .dump           =       tcf_skbedit_dump,
217         .init           =       tcf_skbedit_init,
218         .walk           =       tcf_skbedit_walker,
219         .lookup         =       tcf_skbedit_search,
220 };
221
222 static __net_init int skbedit_init_net(struct net *net)
223 {
224         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
225
226         return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
227 }
228
229 static void __net_exit skbedit_exit_net(struct net *net)
230 {
231         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
232
233         tc_action_net_exit(tn);
234 }
235
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),
241 };
242
243 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
244 MODULE_DESCRIPTION("SKB Editing");
245 MODULE_LICENSE("GPL");
246
247 static int __init skbedit_init_module(void)
248 {
249         return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
250 }
251
252 static void __exit skbedit_cleanup_module(void)
253 {
254         tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
255 }
256
257 module_init(skbedit_init_module);
258 module_exit(skbedit_cleanup_module);