act_skbedit.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Author: Alexander Duyck <alexander.h.duyck@intel.com>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/rtnetlink.h>
  23. #include <net/netlink.h>
  24. #include <net/pkt_sched.h>
  25. #include <linux/tc_act/tc_skbedit.h>
  26. #include <net/tc_act/tc_skbedit.h>
  27. #define SKBEDIT_TAB_MASK 15
  28. static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
  29. struct tcf_result *res)
  30. {
  31. struct tcf_skbedit *d = a->priv;
  32. spin_lock(&d->tcf_lock);
  33. d->tcf_tm.lastuse = jiffies;
  34. bstats_update(&d->tcf_bstats, skb);
  35. if (d->flags & SKBEDIT_F_PRIORITY)
  36. skb->priority = d->priority;
  37. if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
  38. skb->dev->real_num_tx_queues > d->queue_mapping)
  39. skb_set_queue_mapping(skb, d->queue_mapping);
  40. if (d->flags & SKBEDIT_F_MARK)
  41. skb->mark = d->mark;
  42. spin_unlock(&d->tcf_lock);
  43. return d->tcf_action;
  44. }
  45. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  46. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  47. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  48. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  49. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  50. };
  51. static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
  52. struct nlattr *est, struct tc_action *a,
  53. int ovr, int bind)
  54. {
  55. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  56. struct tc_skbedit *parm;
  57. struct tcf_skbedit *d;
  58. u32 flags = 0, *priority = NULL, *mark = NULL;
  59. u16 *queue_mapping = NULL;
  60. int ret = 0, err;
  61. if (nla == NULL)
  62. return -EINVAL;
  63. err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
  64. if (err < 0)
  65. return err;
  66. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  67. return -EINVAL;
  68. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  69. flags |= SKBEDIT_F_PRIORITY;
  70. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  71. }
  72. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  73. flags |= SKBEDIT_F_QUEUE_MAPPING;
  74. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  75. }
  76. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  77. flags |= SKBEDIT_F_MARK;
  78. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  79. }
  80. if (!flags)
  81. return -EINVAL;
  82. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  83. if (!tcf_hash_check(parm->index, a, bind)) {
  84. ret = tcf_hash_create(parm->index, est, a, sizeof(*d), bind);
  85. if (ret)
  86. return ret;
  87. d = to_skbedit(a);
  88. ret = ACT_P_CREATED;
  89. } else {
  90. d = to_skbedit(a);
  91. if (bind)
  92. return 0;
  93. tcf_hash_release(a, bind);
  94. if (!ovr)
  95. return -EEXIST;
  96. }
  97. spin_lock_bh(&d->tcf_lock);
  98. d->flags = flags;
  99. if (flags & SKBEDIT_F_PRIORITY)
  100. d->priority = *priority;
  101. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  102. d->queue_mapping = *queue_mapping;
  103. if (flags & SKBEDIT_F_MARK)
  104. d->mark = *mark;
  105. d->tcf_action = parm->action;
  106. spin_unlock_bh(&d->tcf_lock);
  107. if (ret == ACT_P_CREATED)
  108. tcf_hash_insert(a);
  109. return ret;
  110. }
  111. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  112. int bind, int ref)
  113. {
  114. unsigned char *b = skb_tail_pointer(skb);
  115. struct tcf_skbedit *d = a->priv;
  116. struct tc_skbedit opt = {
  117. .index = d->tcf_index,
  118. .refcnt = d->tcf_refcnt - ref,
  119. .bindcnt = d->tcf_bindcnt - bind,
  120. .action = d->tcf_action,
  121. };
  122. struct tcf_t t;
  123. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  124. goto nla_put_failure;
  125. if ((d->flags & SKBEDIT_F_PRIORITY) &&
  126. nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
  127. &d->priority))
  128. goto nla_put_failure;
  129. if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  130. nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
  131. sizeof(d->queue_mapping), &d->queue_mapping))
  132. goto nla_put_failure;
  133. if ((d->flags & SKBEDIT_F_MARK) &&
  134. nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
  135. &d->mark))
  136. goto nla_put_failure;
  137. t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
  138. t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
  139. t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
  140. if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
  141. goto nla_put_failure;
  142. return skb->len;
  143. nla_put_failure:
  144. nlmsg_trim(skb, b);
  145. return -1;
  146. }
  147. static struct tc_action_ops act_skbedit_ops = {
  148. .kind = "skbedit",
  149. .type = TCA_ACT_SKBEDIT,
  150. .owner = THIS_MODULE,
  151. .act = tcf_skbedit,
  152. .dump = tcf_skbedit_dump,
  153. .init = tcf_skbedit_init,
  154. };
  155. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  156. MODULE_DESCRIPTION("SKB Editing");
  157. MODULE_LICENSE("GPL");
  158. static int __init skbedit_init_module(void)
  159. {
  160. return tcf_register_action(&act_skbedit_ops, SKBEDIT_TAB_MASK);
  161. }
  162. static void __exit skbedit_cleanup_module(void)
  163. {
  164. tcf_unregister_action(&act_skbedit_ops);
  165. }
  166. module_init(skbedit_init_module);
  167. module_exit(skbedit_cleanup_module);