act_skbedit.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, 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. d->tcf_bstats.bytes += qdisc_pkt_len(skb);
  44. d->tcf_bstats.packets++;
  45. if (d->flags & SKBEDIT_F_PRIORITY)
  46. skb->priority = d->priority;
  47. if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
  48. skb->dev->real_num_tx_queues > d->queue_mapping)
  49. skb_set_queue_mapping(skb, d->queue_mapping);
  50. spin_unlock(&d->tcf_lock);
  51. return d->tcf_action;
  52. }
  53. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  54. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  55. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  56. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  57. };
  58. static int tcf_skbedit_init(struct nlattr *nla, struct nlattr *est,
  59. struct tc_action *a, int ovr, int bind)
  60. {
  61. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  62. struct tc_skbedit *parm;
  63. struct tcf_skbedit *d;
  64. struct tcf_common *pc;
  65. u32 flags = 0, *priority = NULL;
  66. u16 *queue_mapping = NULL;
  67. int ret = 0, err;
  68. if (nla == NULL)
  69. return -EINVAL;
  70. err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy);
  71. if (err < 0)
  72. return err;
  73. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  74. return -EINVAL;
  75. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  76. flags |= SKBEDIT_F_PRIORITY;
  77. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  78. }
  79. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  80. flags |= SKBEDIT_F_QUEUE_MAPPING;
  81. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  82. }
  83. if (!flags)
  84. return -EINVAL;
  85. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  86. pc = tcf_hash_check(parm->index, a, bind, &skbedit_hash_info);
  87. if (!pc) {
  88. pc = tcf_hash_create(parm->index, est, a, sizeof(*d), bind,
  89. &skbedit_idx_gen, &skbedit_hash_info);
  90. if (IS_ERR(pc))
  91. return PTR_ERR(pc);
  92. d = to_skbedit(pc);
  93. ret = ACT_P_CREATED;
  94. } else {
  95. d = to_skbedit(pc);
  96. if (!ovr) {
  97. tcf_hash_release(pc, bind, &skbedit_hash_info);
  98. return -EEXIST;
  99. }
  100. }
  101. spin_lock_bh(&d->tcf_lock);
  102. d->flags = flags;
  103. if (flags & SKBEDIT_F_PRIORITY)
  104. d->priority = *priority;
  105. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  106. d->queue_mapping = *queue_mapping;
  107. d->tcf_action = parm->action;
  108. spin_unlock_bh(&d->tcf_lock);
  109. if (ret == ACT_P_CREATED)
  110. tcf_hash_insert(pc, &skbedit_hash_info);
  111. return ret;
  112. }
  113. static inline int tcf_skbedit_cleanup(struct tc_action *a, int bind)
  114. {
  115. struct tcf_skbedit *d = a->priv;
  116. if (d)
  117. return tcf_hash_release(&d->common, bind, &skbedit_hash_info);
  118. return 0;
  119. }
  120. static inline int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  121. int bind, int ref)
  122. {
  123. unsigned char *b = skb_tail_pointer(skb);
  124. struct tcf_skbedit *d = a->priv;
  125. struct tc_skbedit opt;
  126. struct tcf_t t;
  127. opt.index = d->tcf_index;
  128. opt.refcnt = d->tcf_refcnt - ref;
  129. opt.bindcnt = d->tcf_bindcnt - bind;
  130. opt.action = d->tcf_action;
  131. NLA_PUT(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt);
  132. if (d->flags & SKBEDIT_F_PRIORITY)
  133. NLA_PUT(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
  134. &d->priority);
  135. if (d->flags & SKBEDIT_F_QUEUE_MAPPING)
  136. NLA_PUT(skb, TCA_SKBEDIT_QUEUE_MAPPING,
  137. sizeof(d->queue_mapping), &d->queue_mapping);
  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. NLA_PUT(skb, TCA_SKBEDIT_TM, sizeof(t), &t);
  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. .hinfo = &skbedit_hash_info,
  150. .type = TCA_ACT_SKBEDIT,
  151. .capab = TCA_CAP_NONE,
  152. .owner = THIS_MODULE,
  153. .act = tcf_skbedit,
  154. .dump = tcf_skbedit_dump,
  155. .cleanup = tcf_skbedit_cleanup,
  156. .init = tcf_skbedit_init,
  157. .walk = tcf_generic_walker,
  158. };
  159. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  160. MODULE_DESCRIPTION("SKB Editing");
  161. MODULE_LICENSE("GPL");
  162. static int __init skbedit_init_module(void)
  163. {
  164. return tcf_register_action(&act_skbedit_ops);
  165. }
  166. static void __exit skbedit_cleanup_module(void)
  167. {
  168. tcf_unregister_action(&act_skbedit_ops);
  169. }
  170. module_init(skbedit_init_module);
  171. module_exit(skbedit_cleanup_module);