act_skbedit.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 int skbedit_net_id;
  29. static struct tc_action_ops act_skbedit_ops;
  30. static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
  31. struct tcf_result *res)
  32. {
  33. struct tcf_skbedit *d = to_skbedit(a);
  34. spin_lock(&d->tcf_lock);
  35. tcf_lastuse_update(&d->tcf_tm);
  36. bstats_update(&d->tcf_bstats, skb);
  37. if (d->flags & SKBEDIT_F_PRIORITY)
  38. skb->priority = d->priority;
  39. if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
  40. skb->dev->real_num_tx_queues > d->queue_mapping)
  41. skb_set_queue_mapping(skb, d->queue_mapping);
  42. if (d->flags & SKBEDIT_F_MARK)
  43. skb->mark = d->mark;
  44. if (d->flags & SKBEDIT_F_PTYPE)
  45. skb->pkt_type = d->ptype;
  46. spin_unlock(&d->tcf_lock);
  47. return d->tcf_action;
  48. }
  49. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  50. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  51. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  52. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  53. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  54. [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
  55. };
  56. static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
  57. struct nlattr *est, struct tc_action **a,
  58. int ovr, int bind)
  59. {
  60. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  61. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  62. struct tc_skbedit *parm;
  63. struct tcf_skbedit *d;
  64. u32 flags = 0, *priority = NULL, *mark = NULL;
  65. u16 *queue_mapping = NULL, *ptype = NULL;
  66. bool exists = false;
  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 (tb[TCA_SKBEDIT_PTYPE] != NULL) {
  84. ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
  85. if (!skb_pkt_type_ok(*ptype))
  86. return -EINVAL;
  87. flags |= SKBEDIT_F_PTYPE;
  88. }
  89. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  90. flags |= SKBEDIT_F_MARK;
  91. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  92. }
  93. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  94. exists = tcf_hash_check(tn, parm->index, a, bind);
  95. if (exists && bind)
  96. return 0;
  97. if (!flags) {
  98. tcf_hash_release(*a, bind);
  99. return -EINVAL;
  100. }
  101. if (!exists) {
  102. ret = tcf_hash_create(tn, parm->index, est, a,
  103. &act_skbedit_ops, bind, false);
  104. if (ret)
  105. return ret;
  106. d = to_skbedit(*a);
  107. ret = ACT_P_CREATED;
  108. } else {
  109. d = to_skbedit(*a);
  110. tcf_hash_release(*a, bind);
  111. if (!ovr)
  112. return -EEXIST;
  113. }
  114. spin_lock_bh(&d->tcf_lock);
  115. d->flags = flags;
  116. if (flags & SKBEDIT_F_PRIORITY)
  117. d->priority = *priority;
  118. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  119. d->queue_mapping = *queue_mapping;
  120. if (flags & SKBEDIT_F_MARK)
  121. d->mark = *mark;
  122. if (flags & SKBEDIT_F_PTYPE)
  123. d->ptype = *ptype;
  124. d->tcf_action = parm->action;
  125. spin_unlock_bh(&d->tcf_lock);
  126. if (ret == ACT_P_CREATED)
  127. tcf_hash_insert(tn, *a);
  128. return ret;
  129. }
  130. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  131. int bind, int ref)
  132. {
  133. unsigned char *b = skb_tail_pointer(skb);
  134. struct tcf_skbedit *d = to_skbedit(a);
  135. struct tc_skbedit opt = {
  136. .index = d->tcf_index,
  137. .refcnt = d->tcf_refcnt - ref,
  138. .bindcnt = d->tcf_bindcnt - bind,
  139. .action = d->tcf_action,
  140. };
  141. struct tcf_t t;
  142. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  143. goto nla_put_failure;
  144. if ((d->flags & SKBEDIT_F_PRIORITY) &&
  145. nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
  146. goto nla_put_failure;
  147. if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  148. nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
  149. goto nla_put_failure;
  150. if ((d->flags & SKBEDIT_F_MARK) &&
  151. nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
  152. goto nla_put_failure;
  153. if ((d->flags & SKBEDIT_F_PTYPE) &&
  154. nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
  155. goto nla_put_failure;
  156. tcf_tm_dump(&t, &d->tcf_tm);
  157. if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
  158. goto nla_put_failure;
  159. return skb->len;
  160. nla_put_failure:
  161. nlmsg_trim(skb, b);
  162. return -1;
  163. }
  164. static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
  165. struct netlink_callback *cb, int type,
  166. const struct tc_action_ops *ops)
  167. {
  168. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  169. return tcf_generic_walker(tn, skb, cb, type, ops);
  170. }
  171. static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
  172. {
  173. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  174. return tcf_hash_search(tn, a, index);
  175. }
  176. static struct tc_action_ops act_skbedit_ops = {
  177. .kind = "skbedit",
  178. .type = TCA_ACT_SKBEDIT,
  179. .owner = THIS_MODULE,
  180. .act = tcf_skbedit,
  181. .dump = tcf_skbedit_dump,
  182. .init = tcf_skbedit_init,
  183. .walk = tcf_skbedit_walker,
  184. .lookup = tcf_skbedit_search,
  185. .size = sizeof(struct tcf_skbedit),
  186. };
  187. static __net_init int skbedit_init_net(struct net *net)
  188. {
  189. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  190. return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK);
  191. }
  192. static void __net_exit skbedit_exit_net(struct net *net)
  193. {
  194. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  195. tc_action_net_exit(tn);
  196. }
  197. static struct pernet_operations skbedit_net_ops = {
  198. .init = skbedit_init_net,
  199. .exit = skbedit_exit_net,
  200. .id = &skbedit_net_id,
  201. .size = sizeof(struct tc_action_net),
  202. };
  203. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  204. MODULE_DESCRIPTION("SKB Editing");
  205. MODULE_LICENSE("GPL");
  206. static int __init skbedit_init_module(void)
  207. {
  208. return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
  209. }
  210. static void __exit skbedit_cleanup_module(void)
  211. {
  212. tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
  213. }
  214. module_init(skbedit_init_module);
  215. module_exit(skbedit_cleanup_module);