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