act_skbedit.c 5.9 KB

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