act_simple.c 6.0 KB

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