act_simple.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. static unsigned int simp_net_id;
  24. static struct tc_action_ops act_simp_ops;
  25. #define SIMP_MAX_DATA 32
  26. static int tcf_simp_act(struct sk_buff *skb, const struct tc_action *a,
  27. struct tcf_result *res)
  28. {
  29. struct tcf_defact *d = to_defact(a);
  30. spin_lock(&d->tcf_lock);
  31. tcf_lastuse_update(&d->tcf_tm);
  32. bstats_update(&d->tcf_bstats, skb);
  33. /* print policy string followed by _ then packet count
  34. * Example if this was the 3rd packet and the string was "hello"
  35. * then it would look like "hello_3" (without quotes)
  36. */
  37. pr_info("simple: %s_%d\n",
  38. (char *)d->tcfd_defdata, d->tcf_bstats.packets);
  39. spin_unlock(&d->tcf_lock);
  40. return d->tcf_action;
  41. }
  42. static void tcf_simp_release(struct tc_action *a)
  43. {
  44. struct tcf_defact *d = to_defact(a);
  45. kfree(d->tcfd_defdata);
  46. }
  47. static int alloc_defdata(struct tcf_defact *d, const struct nlattr *defdata)
  48. {
  49. d->tcfd_defdata = kzalloc(SIMP_MAX_DATA, GFP_KERNEL);
  50. if (unlikely(!d->tcfd_defdata))
  51. return -ENOMEM;
  52. nla_strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
  53. return 0;
  54. }
  55. static void reset_policy(struct tcf_defact *d, const struct nlattr *defdata,
  56. struct tc_defact *p)
  57. {
  58. spin_lock_bh(&d->tcf_lock);
  59. d->tcf_action = p->action;
  60. memset(d->tcfd_defdata, 0, SIMP_MAX_DATA);
  61. nla_strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
  62. spin_unlock_bh(&d->tcf_lock);
  63. }
  64. static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = {
  65. [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) },
  66. [TCA_DEF_DATA] = { .type = NLA_STRING, .len = SIMP_MAX_DATA },
  67. };
  68. static int tcf_simp_init(struct net *net, struct nlattr *nla,
  69. struct nlattr *est, struct tc_action **a,
  70. int ovr, int bind, bool rtnl_held,
  71. struct netlink_ext_ack *extack)
  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. u32 index;
  80. if (nla == NULL)
  81. return -EINVAL;
  82. err = nla_parse_nested(tb, TCA_DEF_MAX, nla, simple_policy, NULL);
  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. index = parm->index;
  89. err = tcf_idr_check_alloc(tn, &index, a, bind);
  90. if (err < 0)
  91. return err;
  92. exists = err;
  93. if (exists && bind)
  94. return 0;
  95. if (tb[TCA_DEF_DATA] == NULL) {
  96. if (exists)
  97. tcf_idr_release(*a, bind);
  98. else
  99. tcf_idr_cleanup(tn, index);
  100. return -EINVAL;
  101. }
  102. if (!exists) {
  103. ret = tcf_idr_create(tn, index, est, a,
  104. &act_simp_ops, bind, false);
  105. if (ret) {
  106. tcf_idr_cleanup(tn, index);
  107. return ret;
  108. }
  109. d = to_defact(*a);
  110. ret = alloc_defdata(d, tb[TCA_DEF_DATA]);
  111. if (ret < 0) {
  112. tcf_idr_release(*a, bind);
  113. return ret;
  114. }
  115. d->tcf_action = parm->action;
  116. ret = ACT_P_CREATED;
  117. } else {
  118. d = to_defact(*a);
  119. if (!ovr) {
  120. tcf_idr_release(*a, bind);
  121. return -EEXIST;
  122. }
  123. reset_policy(d, tb[TCA_DEF_DATA], parm);
  124. }
  125. if (ret == ACT_P_CREATED)
  126. tcf_idr_insert(tn, *a);
  127. return ret;
  128. }
  129. static int tcf_simp_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_defact *d = to_defact(a);
  134. struct tc_defact opt = {
  135. .index = d->tcf_index,
  136. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  137. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  138. };
  139. struct tcf_t t;
  140. spin_lock_bh(&d->tcf_lock);
  141. opt.action = d->tcf_action;
  142. if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) ||
  143. nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata))
  144. goto nla_put_failure;
  145. tcf_tm_dump(&t, &d->tcf_tm);
  146. if (nla_put_64bit(skb, TCA_DEF_TM, sizeof(t), &t, TCA_DEF_PAD))
  147. goto nla_put_failure;
  148. spin_unlock_bh(&d->tcf_lock);
  149. return skb->len;
  150. nla_put_failure:
  151. spin_unlock_bh(&d->tcf_lock);
  152. nlmsg_trim(skb, b);
  153. return -1;
  154. }
  155. static int tcf_simp_walker(struct net *net, struct sk_buff *skb,
  156. struct netlink_callback *cb, int type,
  157. const struct tc_action_ops *ops,
  158. struct netlink_ext_ack *extack)
  159. {
  160. struct tc_action_net *tn = net_generic(net, simp_net_id);
  161. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  162. }
  163. static int tcf_simp_search(struct net *net, struct tc_action **a, u32 index,
  164. struct netlink_ext_ack *extack)
  165. {
  166. struct tc_action_net *tn = net_generic(net, simp_net_id);
  167. return tcf_idr_search(tn, a, index);
  168. }
  169. static struct tc_action_ops act_simp_ops = {
  170. .kind = "simple",
  171. .type = TCA_ACT_SIMP,
  172. .owner = THIS_MODULE,
  173. .act = tcf_simp_act,
  174. .dump = tcf_simp_dump,
  175. .cleanup = tcf_simp_release,
  176. .init = tcf_simp_init,
  177. .walk = tcf_simp_walker,
  178. .lookup = tcf_simp_search,
  179. .size = sizeof(struct tcf_defact),
  180. };
  181. static __net_init int simp_init_net(struct net *net)
  182. {
  183. struct tc_action_net *tn = net_generic(net, simp_net_id);
  184. return tc_action_net_init(net, tn, &act_simp_ops);
  185. }
  186. static void __net_exit simp_exit_net(struct list_head *net_list)
  187. {
  188. tc_action_net_exit(net_list, simp_net_id);
  189. }
  190. static struct pernet_operations simp_net_ops = {
  191. .init = simp_init_net,
  192. .exit_batch = simp_exit_net,
  193. .id = &simp_net_id,
  194. .size = sizeof(struct tc_action_net),
  195. };
  196. MODULE_AUTHOR("Jamal Hadi Salim(2005)");
  197. MODULE_DESCRIPTION("Simple example action");
  198. MODULE_LICENSE("GPL");
  199. static int __init simp_init_module(void)
  200. {
  201. int ret = tcf_register_action(&act_simp_ops, &simp_net_ops);
  202. if (!ret)
  203. pr_info("Simple TC action Loaded\n");
  204. return ret;
  205. }
  206. static void __exit simp_cleanup_module(void)
  207. {
  208. tcf_unregister_action(&act_simp_ops, &simp_net_ops);
  209. }
  210. module_init(simp_init_module);
  211. module_exit(simp_cleanup_module);