2 * Copyright (c) 2008, Intel Corporation.
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.
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
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/>.
16 * Author: Alexander Duyck <alexander.h.duyck@intel.com>
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>
27 #include <linux/tc_act/tc_skbedit.h>
28 #include <net/tc_act/tc_skbedit.h>
30 #define SKBEDIT_TAB_MASK 15
32 static int skbedit_net_id
;
34 static int tcf_skbedit(struct sk_buff
*skb
, const struct tc_action
*a
,
35 struct tcf_result
*res
)
37 struct tcf_skbedit
*d
= a
->priv
;
39 spin_lock(&d
->tcf_lock
);
40 d
->tcf_tm
.lastuse
= jiffies
;
41 bstats_update(&d
->tcf_bstats
, skb
);
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
)
51 spin_unlock(&d
->tcf_lock
);
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
) },
62 static int tcf_skbedit_init(struct net
*net
, struct nlattr
*nla
,
63 struct nlattr
*est
, struct tc_action
*a
,
66 struct tc_action_net
*tn
= net_generic(net
, skbedit_net_id
);
67 struct nlattr
*tb
[TCA_SKBEDIT_MAX
+ 1];
68 struct tc_skbedit
*parm
;
69 struct tcf_skbedit
*d
;
70 u32 flags
= 0, *priority
= NULL
, *mark
= NULL
;
71 u16
*queue_mapping
= NULL
;
72 int ret
= 0, err
, exists
= 0;
77 err
= nla_parse_nested(tb
, TCA_SKBEDIT_MAX
, nla
, skbedit_policy
);
81 if (tb
[TCA_SKBEDIT_PARMS
] == NULL
)
84 if (tb
[TCA_SKBEDIT_PRIORITY
] != NULL
) {
85 flags
|= SKBEDIT_F_PRIORITY
;
86 priority
= nla_data(tb
[TCA_SKBEDIT_PRIORITY
]);
89 if (tb
[TCA_SKBEDIT_QUEUE_MAPPING
] != NULL
) {
90 flags
|= SKBEDIT_F_QUEUE_MAPPING
;
91 queue_mapping
= nla_data(tb
[TCA_SKBEDIT_QUEUE_MAPPING
]);
94 if (tb
[TCA_SKBEDIT_MARK
] != NULL
) {
95 flags
|= SKBEDIT_F_MARK
;
96 mark
= nla_data(tb
[TCA_SKBEDIT_MARK
]);
99 parm
= nla_data(tb
[TCA_SKBEDIT_PARMS
]);
101 exists
= tcf_hash_check(tn
, parm
->index
, a
, bind
);
106 tcf_hash_release(a
, bind
);
111 ret
= tcf_hash_create(tn
, parm
->index
, est
, a
,
112 sizeof(*d
), bind
, false);
120 tcf_hash_release(a
, bind
);
125 spin_lock_bh(&d
->tcf_lock
);
128 if (flags
& SKBEDIT_F_PRIORITY
)
129 d
->priority
= *priority
;
130 if (flags
& SKBEDIT_F_QUEUE_MAPPING
)
131 d
->queue_mapping
= *queue_mapping
;
132 if (flags
& SKBEDIT_F_MARK
)
135 d
->tcf_action
= parm
->action
;
137 spin_unlock_bh(&d
->tcf_lock
);
139 if (ret
== ACT_P_CREATED
)
140 tcf_hash_insert(tn
, a
);
144 static int tcf_skbedit_dump(struct sk_buff
*skb
, struct tc_action
*a
,
147 unsigned char *b
= skb_tail_pointer(skb
);
148 struct tcf_skbedit
*d
= a
->priv
;
149 struct tc_skbedit opt
= {
150 .index
= d
->tcf_index
,
151 .refcnt
= d
->tcf_refcnt
- ref
,
152 .bindcnt
= d
->tcf_bindcnt
- bind
,
153 .action
= d
->tcf_action
,
157 if (nla_put(skb
, TCA_SKBEDIT_PARMS
, sizeof(opt
), &opt
))
158 goto nla_put_failure
;
159 if ((d
->flags
& SKBEDIT_F_PRIORITY
) &&
160 nla_put(skb
, TCA_SKBEDIT_PRIORITY
, sizeof(d
->priority
),
162 goto nla_put_failure
;
163 if ((d
->flags
& SKBEDIT_F_QUEUE_MAPPING
) &&
164 nla_put(skb
, TCA_SKBEDIT_QUEUE_MAPPING
,
165 sizeof(d
->queue_mapping
), &d
->queue_mapping
))
166 goto nla_put_failure
;
167 if ((d
->flags
& SKBEDIT_F_MARK
) &&
168 nla_put(skb
, TCA_SKBEDIT_MARK
, sizeof(d
->mark
),
170 goto nla_put_failure
;
171 t
.install
= jiffies_to_clock_t(jiffies
- d
->tcf_tm
.install
);
172 t
.lastuse
= jiffies_to_clock_t(jiffies
- d
->tcf_tm
.lastuse
);
173 t
.expires
= jiffies_to_clock_t(d
->tcf_tm
.expires
);
174 if (nla_put_64bit(skb
, TCA_SKBEDIT_TM
, sizeof(t
), &t
, TCA_SKBEDIT_PAD
))
175 goto nla_put_failure
;
183 static int tcf_skbedit_walker(struct net
*net
, struct sk_buff
*skb
,
184 struct netlink_callback
*cb
, int type
,
187 struct tc_action_net
*tn
= net_generic(net
, skbedit_net_id
);
189 return tcf_generic_walker(tn
, skb
, cb
, type
, a
);
192 static int tcf_skbedit_search(struct net
*net
, struct tc_action
*a
, u32 index
)
194 struct tc_action_net
*tn
= net_generic(net
, skbedit_net_id
);
196 return tcf_hash_search(tn
, a
, index
);
199 static struct tc_action_ops act_skbedit_ops
= {
201 .type
= TCA_ACT_SKBEDIT
,
202 .owner
= THIS_MODULE
,
204 .dump
= tcf_skbedit_dump
,
205 .init
= tcf_skbedit_init
,
206 .walk
= tcf_skbedit_walker
,
207 .lookup
= tcf_skbedit_search
,
210 static __net_init
int skbedit_init_net(struct net
*net
)
212 struct tc_action_net
*tn
= net_generic(net
, skbedit_net_id
);
214 return tc_action_net_init(tn
, &act_skbedit_ops
, SKBEDIT_TAB_MASK
);
217 static void __net_exit
skbedit_exit_net(struct net
*net
)
219 struct tc_action_net
*tn
= net_generic(net
, skbedit_net_id
);
221 tc_action_net_exit(tn
);
224 static struct pernet_operations skbedit_net_ops
= {
225 .init
= skbedit_init_net
,
226 .exit
= skbedit_exit_net
,
227 .id
= &skbedit_net_id
,
228 .size
= sizeof(struct tc_action_net
),
231 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
232 MODULE_DESCRIPTION("SKB Editing");
233 MODULE_LICENSE("GPL");
235 static int __init
skbedit_init_module(void)
237 return tcf_register_action(&act_skbedit_ops
, &skbedit_net_ops
);
240 static void __exit
skbedit_cleanup_module(void)
242 tcf_unregister_action(&act_skbedit_ops
, &skbedit_net_ops
);
245 module_init(skbedit_init_module
);
246 module_exit(skbedit_cleanup_module
);