act_skbedit.c 5.7 KB

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