blob: 98725080b5aa73a1d402a704a5a316eead4410a8 [file] [log] [blame]
Alexander Duyckca9b0e22008-09-12 16:30:20 -07001/*
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
Jeff Kirsherc057b192013-12-06 09:13:44 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Alexander Duyckca9b0e22008-09-12 16:30:20 -070015 *
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
WANG Cong369ba562013-12-15 20:15:08 -080031static struct tcf_hashinfo skbedit_hash_info;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070032
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000033static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
Alexander Duyckca9b0e22008-09-12 16:30:20 -070034 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;
Eric Dumazetbfe0d022011-01-09 08:30:54 +000040 bstats_update(&d->tcf_bstats, skb);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070041
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);
jamal1c55d622009-10-15 03:09:18 +000047 if (d->flags & SKBEDIT_F_MARK)
48 skb->mark = d->mark;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070049
50 spin_unlock(&d->tcf_lock);
51 return d->tcf_action;
52}
53
54static 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) },
jamal1c55d622009-10-15 03:09:18 +000058 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
Alexander Duyckca9b0e22008-09-12 16:30:20 -070059};
60
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000061static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
62 struct nlattr *est, struct tc_action *a,
63 int ovr, int bind)
Alexander Duyckca9b0e22008-09-12 16:30:20 -070064{
65 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
66 struct tc_skbedit *parm;
67 struct tcf_skbedit *d;
68 struct tcf_common *pc;
jamal1c55d622009-10-15 03:09:18 +000069 u32 flags = 0, *priority = NULL, *mark = NULL;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070070 u16 *queue_mapping = NULL;
71 int ret = 0, err;
72
73 if (nla == NULL)
74 return -EINVAL;
75
76 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
77 if (err < 0)
78 return err;
79
80 if (tb[TCA_SKBEDIT_PARMS] == NULL)
81 return -EINVAL;
82
83 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
84 flags |= SKBEDIT_F_PRIORITY;
85 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
86 }
87
88 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
89 flags |= SKBEDIT_F_QUEUE_MAPPING;
90 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
91 }
jamal1c55d622009-10-15 03:09:18 +000092
93 if (tb[TCA_SKBEDIT_MARK] != NULL) {
94 flags |= SKBEDIT_F_MARK;
95 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
96 }
97
Alexander Duyckca9b0e22008-09-12 16:30:20 -070098 if (!flags)
99 return -EINVAL;
100
101 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
102
WANG Congc779f7a2014-01-17 11:37:02 -0800103 pc = tcf_hash_check(parm->index, a, bind);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700104 if (!pc) {
WANG Congc779f7a2014-01-17 11:37:02 -0800105 pc = tcf_hash_create(parm->index, est, a, sizeof(*d), bind);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800106 if (IS_ERR(pc))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000107 return PTR_ERR(pc);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700108
109 d = to_skbedit(pc);
110 ret = ACT_P_CREATED;
111 } else {
112 d = to_skbedit(pc);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500113 if (bind)
114 return 0;
WANG Congc779f7a2014-01-17 11:37:02 -0800115 tcf_hash_release(pc, bind, a->ops->hinfo);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500116 if (!ovr)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700117 return -EEXIST;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700118 }
119
120 spin_lock_bh(&d->tcf_lock);
121
122 d->flags = flags;
123 if (flags & SKBEDIT_F_PRIORITY)
124 d->priority = *priority;
125 if (flags & SKBEDIT_F_QUEUE_MAPPING)
126 d->queue_mapping = *queue_mapping;
jamal1c55d622009-10-15 03:09:18 +0000127 if (flags & SKBEDIT_F_MARK)
128 d->mark = *mark;
129
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700130 d->tcf_action = parm->action;
131
132 spin_unlock_bh(&d->tcf_lock);
133
134 if (ret == ACT_P_CREATED)
WANG Congc779f7a2014-01-17 11:37:02 -0800135 tcf_hash_insert(pc, a->ops->hinfo);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700136 return ret;
137}
138
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000139static int tcf_skbedit_cleanup(struct tc_action *a, int bind)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700140{
141 struct tcf_skbedit *d = a->priv;
142
143 if (d)
144 return tcf_hash_release(&d->common, bind, &skbedit_hash_info);
145 return 0;
146}
147
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000148static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
149 int bind, int ref)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700150{
151 unsigned char *b = skb_tail_pointer(skb);
152 struct tcf_skbedit *d = a->priv;
Eric Dumazet1c40be12010-08-16 20:04:22 +0000153 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,
158 };
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700159 struct tcf_t t;
160
David S. Miller1b34ec42012-03-29 05:11:39 -0400161 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),
165 &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),
173 &d->mark))
174 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700175 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);
David S. Miller1b34ec42012-03-29 05:11:39 -0400178 if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
179 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700180 return skb->len;
181
182nla_put_failure:
183 nlmsg_trim(skb, b);
184 return -1;
185}
186
187static struct tc_action_ops act_skbedit_ops = {
188 .kind = "skbedit",
189 .hinfo = &skbedit_hash_info,
190 .type = TCA_ACT_SKBEDIT,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700191 .owner = THIS_MODULE,
192 .act = tcf_skbedit,
193 .dump = tcf_skbedit_dump,
194 .cleanup = tcf_skbedit_cleanup,
195 .init = tcf_skbedit_init,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700196};
197
198MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
199MODULE_DESCRIPTION("SKB Editing");
200MODULE_LICENSE("GPL");
201
202static int __init skbedit_init_module(void)
203{
WANG Cong568a1532013-12-20 00:08:51 -0800204 int err = tcf_hashinfo_init(&skbedit_hash_info, SKBEDIT_TAB_MASK);
WANG Cong369ba562013-12-15 20:15:08 -0800205 if (err)
206 return err;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700207 return tcf_register_action(&act_skbedit_ops);
208}
209
210static void __exit skbedit_cleanup_module(void)
211{
WANG Cong369ba562013-12-15 20:15:08 -0800212 tcf_hashinfo_destroy(&skbedit_hash_info);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700213 tcf_unregister_action(&act_skbedit_ops);
214}
215
216module_init(skbedit_init_module);
217module_exit(skbedit_cleanup_module);