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, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 * Author: Alexander Duyck <alexander.h.duyck@intel.com>
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>
28 #include <linux/tc_act/tc_skbedit.h>
29 #include <net/tc_act/tc_skbedit.h>
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
);
36 static struct tcf_hashinfo skbedit_hash_info
= {
37 .htab
= tcf_skbedit_ht
,
38 .hmask
= SKBEDIT_TAB_MASK
,
39 .lock
= &skbedit_lock
,
42 static int tcf_skbedit(struct sk_buff
*skb
, struct tc_action
*a
,
43 struct tcf_result
*res
)
45 struct tcf_skbedit
*d
= a
->priv
;
47 spin_lock(&d
->tcf_lock
);
48 d
->tcf_tm
.lastuse
= jiffies
;
49 d
->tcf_bstats
.bytes
+= qdisc_pkt_len(skb
);
50 d
->tcf_bstats
.packets
++;
52 if (d
->flags
& SKBEDIT_F_PRIORITY
)
53 skb
->priority
= d
->priority
;
54 if (d
->flags
& SKBEDIT_F_QUEUE_MAPPING
&&
55 skb
->dev
->real_num_tx_queues
> d
->queue_mapping
)
56 skb_set_queue_mapping(skb
, d
->queue_mapping
);
57 if (d
->flags
& SKBEDIT_F_MARK
)
60 spin_unlock(&d
->tcf_lock
);
64 static const struct nla_policy skbedit_policy
[TCA_SKBEDIT_MAX
+ 1] = {
65 [TCA_SKBEDIT_PARMS
] = { .len
= sizeof(struct tc_skbedit
) },
66 [TCA_SKBEDIT_PRIORITY
] = { .len
= sizeof(u32
) },
67 [TCA_SKBEDIT_QUEUE_MAPPING
] = { .len
= sizeof(u16
) },
68 [TCA_SKBEDIT_MARK
] = { .len
= sizeof(u32
) },
71 static int tcf_skbedit_init(struct nlattr
*nla
, struct nlattr
*est
,
72 struct tc_action
*a
, int ovr
, int bind
)
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
;
85 err
= nla_parse_nested(tb
, TCA_SKBEDIT_MAX
, nla
, skbedit_policy
);
89 if (tb
[TCA_SKBEDIT_PARMS
] == NULL
)
92 if (tb
[TCA_SKBEDIT_PRIORITY
] != NULL
) {
93 flags
|= SKBEDIT_F_PRIORITY
;
94 priority
= nla_data(tb
[TCA_SKBEDIT_PRIORITY
]);
97 if (tb
[TCA_SKBEDIT_QUEUE_MAPPING
] != NULL
) {
98 flags
|= SKBEDIT_F_QUEUE_MAPPING
;
99 queue_mapping
= nla_data(tb
[TCA_SKBEDIT_QUEUE_MAPPING
]);
102 if (tb
[TCA_SKBEDIT_MARK
] != NULL
) {
103 flags
|= SKBEDIT_F_MARK
;
104 mark
= nla_data(tb
[TCA_SKBEDIT_MARK
]);
110 parm
= nla_data(tb
[TCA_SKBEDIT_PARMS
]);
112 pc
= tcf_hash_check(parm
->index
, a
, bind
, &skbedit_hash_info
);
114 pc
= tcf_hash_create(parm
->index
, est
, a
, sizeof(*d
), bind
,
115 &skbedit_idx_gen
, &skbedit_hash_info
);
124 tcf_hash_release(pc
, bind
, &skbedit_hash_info
);
129 spin_lock_bh(&d
->tcf_lock
);
132 if (flags
& SKBEDIT_F_PRIORITY
)
133 d
->priority
= *priority
;
134 if (flags
& SKBEDIT_F_QUEUE_MAPPING
)
135 d
->queue_mapping
= *queue_mapping
;
136 if (flags
& SKBEDIT_F_MARK
)
139 d
->tcf_action
= parm
->action
;
141 spin_unlock_bh(&d
->tcf_lock
);
143 if (ret
== ACT_P_CREATED
)
144 tcf_hash_insert(pc
, &skbedit_hash_info
);
148 static inline int tcf_skbedit_cleanup(struct tc_action
*a
, int bind
)
150 struct tcf_skbedit
*d
= a
->priv
;
153 return tcf_hash_release(&d
->common
, bind
, &skbedit_hash_info
);
157 static inline int tcf_skbedit_dump(struct sk_buff
*skb
, struct tc_action
*a
,
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
,
170 NLA_PUT(skb
, TCA_SKBEDIT_PARMS
, sizeof(opt
), &opt
);
171 if (d
->flags
& SKBEDIT_F_PRIORITY
)
172 NLA_PUT(skb
, TCA_SKBEDIT_PRIORITY
, sizeof(d
->priority
),
174 if (d
->flags
& SKBEDIT_F_QUEUE_MAPPING
)
175 NLA_PUT(skb
, TCA_SKBEDIT_QUEUE_MAPPING
,
176 sizeof(d
->queue_mapping
), &d
->queue_mapping
);
177 if (d
->flags
& SKBEDIT_F_MARK
)
178 NLA_PUT(skb
, TCA_SKBEDIT_MARK
, sizeof(d
->mark
),
180 t
.install
= jiffies_to_clock_t(jiffies
- d
->tcf_tm
.install
);
181 t
.lastuse
= jiffies_to_clock_t(jiffies
- d
->tcf_tm
.lastuse
);
182 t
.expires
= jiffies_to_clock_t(d
->tcf_tm
.expires
);
183 NLA_PUT(skb
, TCA_SKBEDIT_TM
, sizeof(t
), &t
);
191 static struct tc_action_ops act_skbedit_ops
= {
193 .hinfo
= &skbedit_hash_info
,
194 .type
= TCA_ACT_SKBEDIT
,
195 .capab
= TCA_CAP_NONE
,
196 .owner
= THIS_MODULE
,
198 .dump
= tcf_skbedit_dump
,
199 .cleanup
= tcf_skbedit_cleanup
,
200 .init
= tcf_skbedit_init
,
201 .walk
= tcf_generic_walker
,
204 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
205 MODULE_DESCRIPTION("SKB Editing");
206 MODULE_LICENSE("GPL");
208 static int __init
skbedit_init_module(void)
210 return tcf_register_action(&act_skbedit_ops
);
213 static void __exit
skbedit_cleanup_module(void)
215 tcf_unregister_action(&act_skbedit_ops
);
218 module_init(skbedit_init_module
);
219 module_exit(skbedit_cleanup_module
);