act_skbedit.c 5.6 KB

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