Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 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 |
Jeff Kirsher | c057b19 | 2013-12-06 09:13:44 -0800 | [diff] [blame] | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 15 | * |
| 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 Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 31 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 32 | static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 33 | 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 Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 39 | bstats_update(&d->tcf_bstats, skb); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 40 | |
| 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); |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 46 | if (d->flags & SKBEDIT_F_MARK) |
| 47 | skb->mark = d->mark; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 48 | |
| 49 | spin_unlock(&d->tcf_lock); |
| 50 | return d->tcf_action; |
| 51 | } |
| 52 | |
| 53 | static 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) }, |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 57 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 60 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
| 61 | struct nlattr *est, struct tc_action *a, |
| 62 | int ovr, int bind) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 63 | { |
| 64 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
| 65 | struct tc_skbedit *parm; |
| 66 | struct tcf_skbedit *d; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 67 | u32 flags = 0, *priority = NULL, *mark = NULL; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 68 | 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 | } |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 90 | |
| 91 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
| 92 | flags |= SKBEDIT_F_MARK; |
| 93 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); |
| 94 | } |
| 95 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 96 | if (!flags) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
| 100 | |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 101 | if (!tcf_hash_check(parm->index, a, bind)) { |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 102 | ret = tcf_hash_create(parm->index, est, a, sizeof(*d), |
| 103 | bind, false); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 104 | if (ret) |
| 105 | return ret; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 106 | |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 107 | d = to_skbedit(a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 108 | ret = ACT_P_CREATED; |
| 109 | } else { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 110 | d = to_skbedit(a); |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 111 | if (bind) |
| 112 | return 0; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 113 | tcf_hash_release(a, bind); |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 114 | if (!ovr) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 115 | return -EEXIST; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | spin_lock_bh(&d->tcf_lock); |
| 119 | |
| 120 | d->flags = flags; |
| 121 | if (flags & SKBEDIT_F_PRIORITY) |
| 122 | d->priority = *priority; |
| 123 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
| 124 | d->queue_mapping = *queue_mapping; |
jamal | 1c55d62 | 2009-10-15 03:09:18 +0000 | [diff] [blame] | 125 | if (flags & SKBEDIT_F_MARK) |
| 126 | d->mark = *mark; |
| 127 | |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 128 | d->tcf_action = parm->action; |
| 129 | |
| 130 | spin_unlock_bh(&d->tcf_lock); |
| 131 | |
| 132 | if (ret == ACT_P_CREATED) |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 133 | tcf_hash_insert(a); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 134 | return ret; |
| 135 | } |
| 136 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 137 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
| 138 | int bind, int ref) |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 139 | { |
| 140 | unsigned char *b = skb_tail_pointer(skb); |
| 141 | struct tcf_skbedit *d = a->priv; |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 142 | struct tc_skbedit opt = { |
| 143 | .index = d->tcf_index, |
| 144 | .refcnt = d->tcf_refcnt - ref, |
| 145 | .bindcnt = d->tcf_bindcnt - bind, |
| 146 | .action = d->tcf_action, |
| 147 | }; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 148 | struct tcf_t t; |
| 149 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 150 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
| 151 | goto nla_put_failure; |
| 152 | if ((d->flags & SKBEDIT_F_PRIORITY) && |
| 153 | nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority), |
| 154 | &d->priority)) |
| 155 | goto nla_put_failure; |
| 156 | if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) && |
| 157 | nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING, |
| 158 | sizeof(d->queue_mapping), &d->queue_mapping)) |
| 159 | goto nla_put_failure; |
| 160 | if ((d->flags & SKBEDIT_F_MARK) && |
| 161 | nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark), |
| 162 | &d->mark)) |
| 163 | goto nla_put_failure; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 164 | t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install); |
| 165 | t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse); |
| 166 | t.expires = jiffies_to_clock_t(d->tcf_tm.expires); |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 167 | if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t)) |
| 168 | goto nla_put_failure; |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 169 | return skb->len; |
| 170 | |
| 171 | nla_put_failure: |
| 172 | nlmsg_trim(skb, b); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | static struct tc_action_ops act_skbedit_ops = { |
| 177 | .kind = "skbedit", |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 178 | .type = TCA_ACT_SKBEDIT, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 179 | .owner = THIS_MODULE, |
| 180 | .act = tcf_skbedit, |
| 181 | .dump = tcf_skbedit_dump, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 182 | .init = tcf_skbedit_init, |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); |
| 186 | MODULE_DESCRIPTION("SKB Editing"); |
| 187 | MODULE_LICENSE("GPL"); |
| 188 | |
| 189 | static int __init skbedit_init_module(void) |
| 190 | { |
WANG Cong | 4f1e9d8 | 2014-02-11 17:07:33 -0800 | [diff] [blame] | 191 | return tcf_register_action(&act_skbedit_ops, SKBEDIT_TAB_MASK); |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void __exit skbedit_cleanup_module(void) |
| 195 | { |
Alexander Duyck | ca9b0e2 | 2008-09-12 16:30:20 -0700 | [diff] [blame] | 196 | tcf_unregister_action(&act_skbedit_ops); |
| 197 | } |
| 198 | |
| 199 | module_init(skbedit_init_module); |
| 200 | module_exit(skbedit_cleanup_module); |