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, 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. if (d->flags & SKBEDIT_F_MARK)
  51. skb->mark = d->mark;
  52. spin_unlock(&d->tcf_lock);
  53. return d->tcf_action;
  54. }
  55. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  56. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  57. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  58. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  59. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  60. };
  61. static int tcf_skbedit_init(struct nlattr *nla, struct nlattr *est,
  62. struct tc_action *a, 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 inline 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 inline 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. NLA_PUT(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt);
  142. if (d->flags & SKBEDIT_F_PRIORITY)
  143. NLA_PUT(skb, TCA_SKBEDIT_PRIORITY, sizeof(d->priority),
  144. &d->priority);
  145. if (d->flags & SKBEDIT_F_QUEUE_MAPPING)
  146. NLA_PUT(skb, TCA_SKBEDIT_QUEUE_MAPPING,
  147. sizeof(d->queue_mapping), &d->queue_mapping);
  148. if (d->flags & SKBEDIT_F_MARK)
  149. NLA_PUT(skb, TCA_SKBEDIT_MARK, sizeof(d->mark),
  150. &d->mark);
  151. t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
  152. t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
  153. t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
  154. NLA_PUT(skb, TCA_SKBEDIT_TM, sizeof(t), &t);
  155. return skb->len;
  156. nla_put_failure:
  157. nlmsg_trim(skb, b);
  158. return -1;
  159. }
  160. static struct tc_action_ops act_skbedit_ops = {
  161. .kind = "skbedit",
  162. .hinfo = &skbedit_hash_info,
  163. .type = TCA_ACT_SKBEDIT,
  164. .capab = TCA_CAP_NONE,
  165. .owner = THIS_MODULE,
  166. .act = tcf_skbedit,
  167. .dump = tcf_skbedit_dump,
  168. .cleanup = tcf_skbedit_cleanup,
  169. .init = tcf_skbedit_init,
  170. .walk = tcf_generic_walker,
  171. };
  172. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  173. MODULE_DESCRIPTION("SKB Editing");
  174. MODULE_LICENSE("GPL");
  175. static int __init skbedit_init_module(void)
  176. {
  177. return tcf_register_action(&act_skbedit_ops);
  178. }
  179. static void __exit skbedit_cleanup_module(void)
  180. {
  181. tcf_unregister_action(&act_skbedit_ops);
  182. }
  183. module_init(skbedit_init_module);
  184. module_exit(skbedit_cleanup_module);