1ca9b0e27SAlexander Duyck /* 2ca9b0e27SAlexander Duyck * Copyright (c) 2008, Intel Corporation. 3ca9b0e27SAlexander Duyck * 4ca9b0e27SAlexander Duyck * This program is free software; you can redistribute it and/or modify it 5ca9b0e27SAlexander Duyck * under the terms and conditions of the GNU General Public License, 6ca9b0e27SAlexander Duyck * version 2, as published by the Free Software Foundation. 7ca9b0e27SAlexander Duyck * 8ca9b0e27SAlexander Duyck * This program is distributed in the hope it will be useful, but WITHOUT 9ca9b0e27SAlexander Duyck * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10ca9b0e27SAlexander Duyck * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11ca9b0e27SAlexander Duyck * more details. 12ca9b0e27SAlexander Duyck * 13ca9b0e27SAlexander Duyck * You should have received a copy of the GNU General Public License along with 14ca9b0e27SAlexander Duyck * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 15ca9b0e27SAlexander Duyck * Place - Suite 330, Boston, MA 02111-1307 USA. 16ca9b0e27SAlexander Duyck * 17ca9b0e27SAlexander Duyck * Author: Alexander Duyck <alexander.h.duyck@intel.com> 18ca9b0e27SAlexander Duyck */ 19ca9b0e27SAlexander Duyck 20ca9b0e27SAlexander Duyck #include <linux/module.h> 21ca9b0e27SAlexander Duyck #include <linux/init.h> 22ca9b0e27SAlexander Duyck #include <linux/kernel.h> 23ca9b0e27SAlexander Duyck #include <linux/skbuff.h> 24ca9b0e27SAlexander Duyck #include <linux/rtnetlink.h> 25ca9b0e27SAlexander Duyck #include <net/netlink.h> 26ca9b0e27SAlexander Duyck #include <net/pkt_sched.h> 27ca9b0e27SAlexander Duyck 28ca9b0e27SAlexander Duyck #include <linux/tc_act/tc_skbedit.h> 29ca9b0e27SAlexander Duyck #include <net/tc_act/tc_skbedit.h> 30ca9b0e27SAlexander Duyck 31ca9b0e27SAlexander Duyck #define SKBEDIT_TAB_MASK 15 32ca9b0e27SAlexander Duyck static struct tcf_common *tcf_skbedit_ht[SKBEDIT_TAB_MASK + 1]; 33ca9b0e27SAlexander Duyck static u32 skbedit_idx_gen; 34ca9b0e27SAlexander Duyck static DEFINE_RWLOCK(skbedit_lock); 35ca9b0e27SAlexander Duyck 36ca9b0e27SAlexander Duyck static struct tcf_hashinfo skbedit_hash_info = { 37ca9b0e27SAlexander Duyck .htab = tcf_skbedit_ht, 38ca9b0e27SAlexander Duyck .hmask = SKBEDIT_TAB_MASK, 39ca9b0e27SAlexander Duyck .lock = &skbedit_lock, 40ca9b0e27SAlexander Duyck }; 41ca9b0e27SAlexander Duyck 42dc7f9f6eSEric Dumazet static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a, 43ca9b0e27SAlexander Duyck struct tcf_result *res) 44ca9b0e27SAlexander Duyck { 45ca9b0e27SAlexander Duyck struct tcf_skbedit *d = a->priv; 46ca9b0e27SAlexander Duyck 47ca9b0e27SAlexander Duyck spin_lock(&d->tcf_lock); 48ca9b0e27SAlexander Duyck d->tcf_tm.lastuse = jiffies; 49bfe0d029SEric Dumazet bstats_update(&d->tcf_bstats, skb); 50ca9b0e27SAlexander Duyck 51ca9b0e27SAlexander Duyck if (d->flags & SKBEDIT_F_PRIORITY) 52ca9b0e27SAlexander Duyck skb->priority = d->priority; 53ca9b0e27SAlexander Duyck if (d->flags & SKBEDIT_F_QUEUE_MAPPING && 54ca9b0e27SAlexander Duyck skb->dev->real_num_tx_queues > d->queue_mapping) 55ca9b0e27SAlexander Duyck skb_set_queue_mapping(skb, d->queue_mapping); 561c55d62eSjamal if (d->flags & SKBEDIT_F_MARK) 571c55d62eSjamal skb->mark = d->mark; 58ca9b0e27SAlexander Duyck 59ca9b0e27SAlexander Duyck spin_unlock(&d->tcf_lock); 60ca9b0e27SAlexander Duyck return d->tcf_action; 61ca9b0e27SAlexander Duyck } 62ca9b0e27SAlexander Duyck 63ca9b0e27SAlexander Duyck static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { 64ca9b0e27SAlexander Duyck [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, 65ca9b0e27SAlexander Duyck [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, 66ca9b0e27SAlexander Duyck [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, 671c55d62eSjamal [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, 68ca9b0e27SAlexander Duyck }; 69ca9b0e27SAlexander Duyck 70c1b52739SBenjamin LaHaise static int tcf_skbedit_init(struct net *net, struct nlattr *nla, 71c1b52739SBenjamin LaHaise struct nlattr *est, struct tc_action *a, 72c1b52739SBenjamin LaHaise int ovr, int bind) 73ca9b0e27SAlexander Duyck { 74ca9b0e27SAlexander Duyck struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; 75ca9b0e27SAlexander Duyck struct tc_skbedit *parm; 76ca9b0e27SAlexander Duyck struct tcf_skbedit *d; 77ca9b0e27SAlexander Duyck struct tcf_common *pc; 781c55d62eSjamal u32 flags = 0, *priority = NULL, *mark = NULL; 79ca9b0e27SAlexander Duyck u16 *queue_mapping = NULL; 80ca9b0e27SAlexander Duyck int ret = 0, err; 81ca9b0e27SAlexander Duyck 82ca9b0e27SAlexander Duyck if (nla == NULL) 83ca9b0e27SAlexander Duyck return -EINVAL; 84ca9b0e27SAlexander Duyck 85ca9b0e27SAlexander Duyck err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy); 86ca9b0e27SAlexander Duyck if (err < 0) 87ca9b0e27SAlexander Duyck return err; 88ca9b0e27SAlexander Duyck 89ca9b0e27SAlexander Duyck if (tb[TCA_SKBEDIT_PARMS] == NULL) 90ca9b0e27SAlexander Duyck return -EINVAL; 91ca9b0e27SAlexander Duyck 92ca9b0e27SAlexander Duyck if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { 93ca9b0e27SAlexander Duyck flags |= SKBEDIT_F_PRIORITY; 94ca9b0e27SAlexander Duyck priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); 95ca9b0e27SAlexander Duyck } 96ca9b0e27SAlexander Duyck 97ca9b0e27SAlexander Duyck if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { 98ca9b0e27SAlexander Duyck flags |= SKBEDIT_F_QUEUE_MAPPING; 99ca9b0e27SAlexander Duyck queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); 100ca9b0e27SAlexander Duyck } 1011c55d62eSjamal 1021c55d62eSjamal if (tb[TCA_SKBEDIT_MARK] != NULL) { 1031c55d62eSjamal flags |= SKBEDIT_F_MARK; 1041c55d62eSjamal mark = nla_data(tb[TCA_SKBEDIT_MARK]); 1051c55d62eSjamal } 1061c55d62eSjamal 107ca9b0e27SAlexander Duyck if (!flags) 108ca9b0e27SAlexander Duyck return -EINVAL; 109ca9b0e27SAlexander Duyck 110ca9b0e27SAlexander Duyck parm = nla_data(tb[TCA_SKBEDIT_PARMS]); 111ca9b0e27SAlexander Duyck 112ca9b0e27SAlexander Duyck pc = tcf_hash_check(parm->index, a, bind, &skbedit_hash_info); 113ca9b0e27SAlexander Duyck if (!pc) { 114ca9b0e27SAlexander Duyck pc = tcf_hash_create(parm->index, est, a, sizeof(*d), bind, 115ca9b0e27SAlexander Duyck &skbedit_idx_gen, &skbedit_hash_info); 1160e991ec6SStephen Hemminger if (IS_ERR(pc)) 1170e991ec6SStephen Hemminger return PTR_ERR(pc); 118ca9b0e27SAlexander Duyck 119ca9b0e27SAlexander Duyck d = to_skbedit(pc); 120ca9b0e27SAlexander Duyck ret = ACT_P_CREATED; 121ca9b0e27SAlexander Duyck } else { 122ca9b0e27SAlexander Duyck d = to_skbedit(pc); 123*1a29321eSJamal Hadi Salim if (bind) 124*1a29321eSJamal Hadi Salim return 0; 125ca9b0e27SAlexander Duyck tcf_hash_release(pc, bind, &skbedit_hash_info); 126*1a29321eSJamal Hadi Salim if (!ovr) 127ca9b0e27SAlexander Duyck return -EEXIST; 128ca9b0e27SAlexander Duyck } 129ca9b0e27SAlexander Duyck 130ca9b0e27SAlexander Duyck spin_lock_bh(&d->tcf_lock); 131ca9b0e27SAlexander Duyck 132ca9b0e27SAlexander Duyck d->flags = flags; 133ca9b0e27SAlexander Duyck if (flags & SKBEDIT_F_PRIORITY) 134ca9b0e27SAlexander Duyck d->priority = *priority; 135ca9b0e27SAlexander Duyck if (flags & SKBEDIT_F_QUEUE_MAPPING) 136ca9b0e27SAlexander Duyck d->queue_mapping = *queue_mapping; 1371c55d62eSjamal if (flags & SKBEDIT_F_MARK) 1381c55d62eSjamal d->mark = *mark; 1391c55d62eSjamal 140ca9b0e27SAlexander Duyck d->tcf_action = parm->action; 141ca9b0e27SAlexander Duyck 142ca9b0e27SAlexander Duyck spin_unlock_bh(&d->tcf_lock); 143ca9b0e27SAlexander Duyck 144ca9b0e27SAlexander Duyck if (ret == ACT_P_CREATED) 145ca9b0e27SAlexander Duyck tcf_hash_insert(pc, &skbedit_hash_info); 146ca9b0e27SAlexander Duyck return ret; 147ca9b0e27SAlexander Duyck } 148ca9b0e27SAlexander Duyck 149cc7ec456SEric Dumazet static int tcf_skbedit_cleanup(struct tc_action *a, int bind) 150ca9b0e27SAlexander Duyck { 151ca9b0e27SAlexander Duyck struct tcf_skbedit *d = a->priv; 152ca9b0e27SAlexander Duyck 153ca9b0e27SAlexander Duyck if (d) 154ca9b0e27SAlexander Duyck return tcf_hash_release(&d->common, bind, &skbedit_hash_info); 155ca9b0e27SAlexander Duyck return 0; 156ca9b0e27SAlexander Duyck } 157ca9b0e27SAlexander Duyck 158cc7ec456SEric Dumazet static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, 159ca9b0e27SAlexander Duyck int bind, int ref) 160ca9b0e27SAlexander Duyck { 161ca9b0e27SAlexander Duyck unsigned char *b = skb_tail_pointer(skb); 162ca9b0e27SAlexander Duyck struct tcf_skbedit *d = a->priv; 1631c40be12SEric Dumazet struct tc_skbedit opt = { 1641c40be12SEric Dumazet .index = d->tcf_index, 1651c40be12SEric Dumazet .refcnt = d->tcf_refcnt - ref, 1661c40be12SEric Dumazet .bindcnt = d->tcf_bindcnt - bind, 1671c40be12SEric Dumazet .action = d->tcf_action, 1681c40be12SEric Dumazet }; 169ca9b0e27SAlexander Duyck struct tcf_t t; 170ca9b0e27SAlexander Duyck 1711b34ec43SDavid S. Miller if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) 1721b34ec43SDavid S. Miller goto nla_put_failure; 1731b34ec43SDavid S. Miller if ((d->flags & SKBEDIT_F_PRIORITY) && 1741b34ec43SDavid S. Miller nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority), 1751b34ec43SDavid S. Miller &d->priority)) 1761b34ec43SDavid S. Miller goto nla_put_failure; 1771b34ec43SDavid S. Miller if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) && 1781b34ec43SDavid S. Miller nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING, 1791b34ec43SDavid S. Miller sizeof(d->queue_mapping), &d->queue_mapping)) 1801b34ec43SDavid S. Miller goto nla_put_failure; 1811b34ec43SDavid S. Miller if ((d->flags & SKBEDIT_F_MARK) && 1821b34ec43SDavid S. Miller nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark), 1831b34ec43SDavid S. Miller &d->mark)) 1841b34ec43SDavid S. Miller goto nla_put_failure; 185ca9b0e27SAlexander Duyck t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install); 186ca9b0e27SAlexander Duyck t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse); 187ca9b0e27SAlexander Duyck t.expires = jiffies_to_clock_t(d->tcf_tm.expires); 1881b34ec43SDavid S. Miller if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t)) 1891b34ec43SDavid S. Miller goto nla_put_failure; 190ca9b0e27SAlexander Duyck return skb->len; 191ca9b0e27SAlexander Duyck 192ca9b0e27SAlexander Duyck nla_put_failure: 193ca9b0e27SAlexander Duyck nlmsg_trim(skb, b); 194ca9b0e27SAlexander Duyck return -1; 195ca9b0e27SAlexander Duyck } 196ca9b0e27SAlexander Duyck 197ca9b0e27SAlexander Duyck static struct tc_action_ops act_skbedit_ops = { 198ca9b0e27SAlexander Duyck .kind = "skbedit", 199ca9b0e27SAlexander Duyck .hinfo = &skbedit_hash_info, 200ca9b0e27SAlexander Duyck .type = TCA_ACT_SKBEDIT, 201ca9b0e27SAlexander Duyck .capab = TCA_CAP_NONE, 202ca9b0e27SAlexander Duyck .owner = THIS_MODULE, 203ca9b0e27SAlexander Duyck .act = tcf_skbedit, 204ca9b0e27SAlexander Duyck .dump = tcf_skbedit_dump, 205ca9b0e27SAlexander Duyck .cleanup = tcf_skbedit_cleanup, 206ca9b0e27SAlexander Duyck .init = tcf_skbedit_init, 207ca9b0e27SAlexander Duyck }; 208ca9b0e27SAlexander Duyck 209ca9b0e27SAlexander Duyck MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); 210ca9b0e27SAlexander Duyck MODULE_DESCRIPTION("SKB Editing"); 211ca9b0e27SAlexander Duyck MODULE_LICENSE("GPL"); 212ca9b0e27SAlexander Duyck 213ca9b0e27SAlexander Duyck static int __init skbedit_init_module(void) 214ca9b0e27SAlexander Duyck { 215ca9b0e27SAlexander Duyck return tcf_register_action(&act_skbedit_ops); 216ca9b0e27SAlexander Duyck } 217ca9b0e27SAlexander Duyck 218ca9b0e27SAlexander Duyck static void __exit skbedit_cleanup_module(void) 219ca9b0e27SAlexander Duyck { 220ca9b0e27SAlexander Duyck tcf_unregister_action(&act_skbedit_ops); 221ca9b0e27SAlexander Duyck } 222ca9b0e27SAlexander Duyck 223ca9b0e27SAlexander Duyck module_init(skbedit_init_module); 224ca9b0e27SAlexander Duyck module_exit(skbedit_cleanup_module); 225