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),
  85. bind, false);
  86. if (ret)
  87. return ret;
  88. d = to_skbedit(a);
  89. ret = ACT_P_CREATED;
  90. } else {
  91. d = to_skbedit(a);
  92. if (bind)
  93. return 0;
  94. tcf_hash_release(a, bind);
  95. if (!ovr)
  96. return -EEXIST;
  97. }
  98. spin_lock_bh(&d->tcf_lock);
  99. d->flags = flags;
  100. if (flags & SKBEDIT_F_PRIORITY)
  101. d->priority = *priority;
  102. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  103. d->queue_mapping = *queue_mapping;
  104. if (flags & SKBEDIT_F_MARK)
  105. d->mark = *mark;
  106. d->tcf_action = parm->action;
  107. spin_unlock_bh(&d->tcf_lock);
  108. if (ret == ACT_P_CREATED)
  109. tcf_hash_insert(a);
  110. return ret;
  111. }
  112. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  113. int bind, int ref)
  114. {
  115. unsigned char *b = skb_tail_pointer(skb);
  116. struct tcf_skbedit *d = a->priv;
  117. struct tc_skbedit opt = {
  118. .index = d->tcf_index,
  119. .refcnt = d->tcf_refcnt - ref,
  120. .bindcnt = d->tcf_bindcnt - bind,
  121. .action = d->tcf_action,
  122. };
  123. struct tcf_t t;
  124. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  125. goto nla_put_failure;
  126. if ((d->flags & SKBEDIT_F_PRIORITY) &&
  127. nla_put(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
  128. &d->priority))
  129. goto nla_put_failure;
  130. if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  131. nla_put(skb, TCA_SKBEDIT_QUEUE_MAPPING,
  132. sizeof(d->queue_mapping), &d->queue_mapping))
  133. goto nla_put_failure;
  134. if ((d->flags & SKBEDIT_F_MARK) &&
  135. nla_put(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
  136. &d->mark))
  137. goto nla_put_failure;
  138. t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
  139. t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
  140. t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
  141. if (nla_put(skb, TCA_SKBEDIT_TM, sizeof(t), &t))
  142. goto nla_put_failure;
  143. return skb->len;
  144. nla_put_failure:
  145. nlmsg_trim(skb, b);
  146. return -1;
  147. }
  148. static struct tc_action_ops act_skbedit_ops = {
  149. .kind = "skbedit",
  150. .type = TCA_ACT_SKBEDIT,
  151. .owner = THIS_MODULE,
  152. .act = tcf_skbedit,
  153. .dump = tcf_skbedit_dump,
  154. .init = tcf_skbedit_init,
  155. };
  156. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  157. MODULE_DESCRIPTION("SKB Editing");
  158. MODULE_LICENSE("GPL");
  159. static int __init skbedit_init_module(void)
  160. {
  161. return tcf_register_action(&act_skbedit_ops, SKBEDIT_TAB_MASK);
  162. }
  163. static void __exit skbedit_cleanup_module(void)
  164. {
  165. tcf_unregister_action(&act_skbedit_ops);
  166. }
  167. module_init(skbedit_init_module);
  168. module_exit(skbedit_cleanup_module);