act_simple.c 6.4 KB

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