act_simple.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * net/sched/act_simple.c Simple example of an action
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Jamal Hadi Salim (2005-8)
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/netlink.h>
  19. #include <net/pkt_sched.h>
  20. #define TCA_ACT_SIMP 22
  21. #include <linux/tc_act/tc_defact.h>
  22. #include <net/tc_act/tc_defact.h>
  23. #define SIMP_TAB_MASK 7
  24. static int simp_net_id;
  25. static struct tc_action_ops act_simp_ops;
  26. #define SIMP_MAX_DATA 32
  27. static int tcf_simp(struct sk_buff *skb, const struct tc_action *a,
  28. struct tcf_result *res)
  29. {
  30. struct tcf_defact *d = to_defact(a);
  31. spin_lock(&d->tcf_lock);
  32. tcf_lastuse_update(&d->tcf_tm);
  33. bstats_update(&d->tcf_bstats, skb);
  34. /* print policy string followed by _ then packet count
  35. * Example if this was the 3rd packet and the string was "hello"
  36. * then it would look like "hello_3" (without quotes)
  37. */
  38. pr_info("simple: %s_%d\n",
  39. (char *)d->tcfd_defdata, d->tcf_bstats.packets);
  40. spin_unlock(&d->tcf_lock);
  41. return d->tcf_action;
  42. }
  43. static void tcf_simp_release(struct tc_action *a, int bind)
  44. {
  45. struct tcf_defact *d = to_defact(a);
  46. kfree(d->tcfd_defdata);
  47. }
  48. static int alloc_defdata(struct tcf_defact *d, char *defdata)
  49. {
  50. d->tcfd_defdata = kzalloc(SIMP_MAX_DATA, GFP_KERNEL);
  51. if (unlikely(!d->tcfd_defdata))
  52. return -ENOMEM;
  53. strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
  54. return 0;
  55. }
  56. static void reset_policy(struct tcf_defact *d, char *defdata,
  57. struct tc_defact *p)
  58. {
  59. spin_lock_bh(&d->tcf_lock);
  60. d->tcf_action = p->action;
  61. memset(d->tcfd_defdata, 0, SIMP_MAX_DATA);
  62. strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
  63. spin_unlock_bh(&d->tcf_lock);
  64. }
  65. static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = {
  66. [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) },
  67. [TCA_DEF_DATA] = { .type = NLA_STRING, .len = SIMP_MAX_DATA },
  68. };
  69. static int tcf_simp_init(struct net *net, struct nlattr *nla,
  70. struct nlattr *est, struct tc_action **a,
  71. int ovr, int bind)
  72. {
  73. struct tc_action_net *tn = net_generic(net, simp_net_id);
  74. struct nlattr *tb[TCA_DEF_MAX + 1];
  75. struct tc_defact *parm;
  76. struct tcf_defact *d;
  77. bool exists = false;
  78. int ret = 0, err;
  79. char *defdata;
  80. if (nla == NULL)
  81. return -EINVAL;
  82. err = nla_parse_nested(tb, TCA_DEF_MAX, nla, simple_policy);
  83. if (err < 0)
  84. return err;
  85. if (tb[TCA_DEF_PARMS] == NULL)
  86. return -EINVAL;
  87. parm = nla_data(tb[TCA_DEF_PARMS]);
  88. exists = tcf_hash_check(tn, parm->index, a, bind);
  89. if (exists && bind)
  90. return 0;
  91. if (tb[TCA_DEF_DATA] == NULL) {
  92. if (exists)
  93. tcf_hash_release(*a, bind);
  94. return -EINVAL;
  95. }
  96. defdata = nla_data(tb[TCA_DEF_DATA]);
  97. if (!exists) {
  98. ret = tcf_hash_create(tn, parm->index, est, a,
  99. &act_simp_ops, bind, false);
  100. if (ret)
  101. return ret;
  102. d = to_defact(*a);
  103. ret = alloc_defdata(d, defdata);
  104. if (ret < 0) {
  105. tcf_hash_cleanup(*a, est);
  106. return ret;
  107. }
  108. d->tcf_action = parm->action;
  109. ret = ACT_P_CREATED;
  110. } else {
  111. d = to_defact(*a);
  112. tcf_hash_release(*a, bind);
  113. if (!ovr)
  114. return -EEXIST;
  115. reset_policy(d, defdata, parm);
  116. }
  117. if (ret == ACT_P_CREATED)
  118. tcf_hash_insert(tn, *a);
  119. return ret;
  120. }
  121. static int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a,
  122. int bind, int ref)
  123. {
  124. unsigned char *b = skb_tail_pointer(skb);
  125. struct tcf_defact *d = to_defact(a);
  126. struct tc_defact opt = {
  127. .index = d->tcf_index,
  128. .refcnt = d->tcf_refcnt - ref,
  129. .bindcnt = d->tcf_bindcnt - bind,
  130. .action = d->tcf_action,
  131. };
  132. struct tcf_t t;
  133. if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) ||
  134. nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata))
  135. goto nla_put_failure;
  136. tcf_tm_dump(&t, &d->tcf_tm);
  137. if (nla_put_64bit(skb, TCA_DEF_TM, sizeof(t), &t, TCA_DEF_PAD))
  138. goto nla_put_failure;
  139. return skb->len;
  140. nla_put_failure:
  141. nlmsg_trim(skb, b);
  142. return -1;
  143. }
  144. static int tcf_simp_walker(struct net *net, struct sk_buff *skb,
  145. struct netlink_callback *cb, int type,
  146. const struct tc_action_ops *ops)
  147. {
  148. struct tc_action_net *tn = net_generic(net, simp_net_id);
  149. return tcf_generic_walker(tn, skb, cb, type, ops);
  150. }
  151. static int tcf_simp_search(struct net *net, struct tc_action **a, u32 index)
  152. {
  153. struct tc_action_net *tn = net_generic(net, simp_net_id);
  154. return tcf_hash_search(tn, a, index);
  155. }
  156. static struct tc_action_ops act_simp_ops = {
  157. .kind = "simple",
  158. .type = TCA_ACT_SIMP,
  159. .owner = THIS_MODULE,
  160. .act = tcf_simp,
  161. .dump = tcf_simp_dump,
  162. .cleanup = tcf_simp_release,
  163. .init = tcf_simp_init,
  164. .walk = tcf_simp_walker,
  165. .lookup = tcf_simp_search,
  166. .size = sizeof(struct tcf_defact),
  167. };
  168. static __net_init int simp_init_net(struct net *net)
  169. {
  170. struct tc_action_net *tn = net_generic(net, simp_net_id);
  171. return tc_action_net_init(tn, &act_simp_ops, SIMP_TAB_MASK);
  172. }
  173. static void __net_exit simp_exit_net(struct net *net)
  174. {
  175. struct tc_action_net *tn = net_generic(net, simp_net_id);
  176. tc_action_net_exit(tn);
  177. }
  178. static struct pernet_operations simp_net_ops = {
  179. .init = simp_init_net,
  180. .exit = simp_exit_net,
  181. .id = &simp_net_id,
  182. .size = sizeof(struct tc_action_net),
  183. };
  184. MODULE_AUTHOR("Jamal Hadi Salim(2005)");
  185. MODULE_DESCRIPTION("Simple example action");
  186. MODULE_LICENSE("GPL");
  187. static int __init simp_init_module(void)
  188. {
  189. int ret = tcf_register_action(&act_simp_ops, &simp_net_ops);
  190. if (!ret)
  191. pr_info("Simple TC action Loaded\n");
  192. return ret;
  193. }
  194. static void __exit simp_cleanup_module(void)
  195. {
  196. tcf_unregister_action(&act_simp_ops, &simp_net_ops);
  197. }
  198. module_init(simp_init_module);
  199. module_exit(simp_cleanup_module);