blob: fcfeeaf838beb9e75f07f7cbda7fb2b73237a17f [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
Alexander Duyckca9b0e22008-09-12 16:30:20 -070031
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000032static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
Alexander Duyckca9b0e22008-09-12 16:30:20 -070033 struct tcf_result *res)
34{
35 struct tcf_skbedit *d = a->priv;
36
37 spin_lock(&d->tcf_lock);
38 d->tcf_tm.lastuse = jiffies;
Eric Dumazetbfe0d022011-01-09 08:30:54 +000039 bstats_update(&d->tcf_bstats, skb);
Alexander Duyckca9b0e22008-09-12 16:30:20 -070040
41 if (d->flags & SKBEDIT_F_PRIORITY)
42 skb->priority = d->priority;
43 if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
44 skb->dev->real_num_tx_queues > d->queue_mapping)
45 skb_set_queue_mapping(skb, d->queue_mapping);
jamal1c55d622009-10-15 03:09:18 +000046 if (d->flags & SKBEDIT_F_MARK)
47 skb->mark = d->mark;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070048
49 spin_unlock(&d->tcf_lock);
50 return d->tcf_action;
51}
52
53static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
54 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
55 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
56 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
jamal1c55d622009-10-15 03:09:18 +000057 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
Alexander Duyckca9b0e22008-09-12 16:30:20 -070058};
59
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000060static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
61 struct nlattr *est, struct tc_action *a,
62 int ovr, int bind)
Alexander Duyckca9b0e22008-09-12 16:30:20 -070063{
64 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
65 struct tc_skbedit *parm;
66 struct tcf_skbedit *d;
jamal1c55d622009-10-15 03:09:18 +000067 u32 flags = 0, *priority = NULL, *mark = NULL;
Alexander Duyckca9b0e22008-09-12 16:30:20 -070068 u16 *queue_mapping = NULL;
69 int ret = 0, err;
70
71 if (nla == NULL)
72 return -EINVAL;
73
74 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
75 if (err < 0)
76 return err;
77
78 if (tb[TCA_SKBEDIT_PARMS] == NULL)
79 return -EINVAL;
80
81 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
82 flags |= SKBEDIT_F_PRIORITY;
83 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
84 }
85
86 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
87 flags |= SKBEDIT_F_QUEUE_MAPPING;
88 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
89 }
jamal1c55d622009-10-15 03:09:18 +000090
91 if (tb[TCA_SKBEDIT_MARK] != NULL) {
92 flags |= SKBEDIT_F_MARK;
93 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
94 }
95
Alexander Duyckca9b0e22008-09-12 16:30:20 -070096 if (!flags)
97 return -EINVAL;
98
99 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
100
WANG Cong86062032014-02-11 17:07:31 -0800101 if (!tcf_hash_check(parm->index, a, bind)) {
102 ret = tcf_hash_create(parm->index, est, a, sizeof(*d), bind);
103 if (ret)
104 return ret;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700105
WANG Cong86062032014-02-11 17:07:31 -0800106 d = to_skbedit(a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700107 ret = ACT_P_CREATED;
108 } else {
WANG Cong86062032014-02-11 17:07:31 -0800109 d = to_skbedit(a);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500110 if (bind)
111 return 0;
WANG Cong86062032014-02-11 17:07:31 -0800112 tcf_hash_release(a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500113 if (!ovr)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700114 return -EEXIST;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700115 }
116
117 spin_lock_bh(&d->tcf_lock);
118
119 d->flags = flags;
120 if (flags & SKBEDIT_F_PRIORITY)
121 d->priority = *priority;
122 if (flags & SKBEDIT_F_QUEUE_MAPPING)
123 d->queue_mapping = *queue_mapping;
jamal1c55d622009-10-15 03:09:18 +0000124 if (flags & SKBEDIT_F_MARK)
125 d->mark = *mark;
126
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700127 d->tcf_action = parm->action;
128
129 spin_unlock_bh(&d->tcf_lock);
130
131 if (ret == ACT_P_CREATED)
WANG Cong86062032014-02-11 17:07:31 -0800132 tcf_hash_insert(a);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700133 return ret;
134}
135
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000136static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
137 int bind, int ref)
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700138{
139 unsigned char *b = skb_tail_pointer(skb);
140 struct tcf_skbedit *d = a->priv;
Eric Dumazet1c40be12010-08-16 20:04:22 +0000141 struct tc_skbedit opt = {
142 .index = d->tcf_index,
143 .refcnt = d->tcf_refcnt - ref,
144 .bindcnt = d->tcf_bindcnt - bind,
145 .action = d->tcf_action,
146 };
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700147 struct tcf_t t;
148
David S. Miller1b34ec42012-03-29 05:11:39 -0400149 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
150 goto nla_put_failure;
151 if ((d->flags & SKBEDIT_F_PRIORITY) &&
152 nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
153 &d->priority))
154 goto nla_put_failure;
155 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
156 nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
157 sizeof(d->queue_mapping), &d->queue_mapping))
158 goto nla_put_failure;
159 if ((d->flags & SKBEDIT_F_MARK) &&
160 nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
161 &d->mark))
162 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700163 t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
164 t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
165 t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
David S. Miller1b34ec42012-03-29 05:11:39 -0400166 if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
167 goto nla_put_failure;
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700168 return skb->len;
169
170nla_put_failure:
171 nlmsg_trim(skb, b);
172 return -1;
173}
174
175static struct tc_action_ops act_skbedit_ops = {
176 .kind = "skbedit",
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700177 .type = TCA_ACT_SKBEDIT,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700178 .owner = THIS_MODULE,
179 .act = tcf_skbedit,
180 .dump = tcf_skbedit_dump,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700181 .init = tcf_skbedit_init,
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700182};
183
184MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
185MODULE_DESCRIPTION("SKB Editing");
186MODULE_LICENSE("GPL");
187
188static int __init skbedit_init_module(void)
189{
WANG Cong4f1e9d82014-02-11 17:07:33 -0800190 return tcf_register_action(&act_skbedit_ops, SKBEDIT_TAB_MASK);
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700191}
192
193static void __exit skbedit_cleanup_module(void)
194{
Alexander Duyckca9b0e22008-09-12 16:30:20 -0700195 tcf_unregister_action(&act_skbedit_ops);
196}
197
198module_init(skbedit_init_module);
199module_exit(skbedit_cleanup_module);