act_simple.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. nla_strscpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
  66. spin_unlock_bh(&d->tcf_lock);
  67. if (goto_ch)
  68. tcf_chain_put_by_act(goto_ch);
  69. return 0;
  70. }
  71. static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = {
  72. [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) },
  73. [TCA_DEF_DATA] = { .type = NLA_STRING, .len = SIMP_MAX_DATA },
  74. };
  75. static int tcf_simp_init(struct net *net, struct nlattr *nla,
  76. struct nlattr *est, struct tc_action **a,
  77. struct tcf_proto *tp, u32 flags,
  78. struct netlink_ext_ack *extack)
  79. {
  80. struct tc_action_net *tn = net_generic(net, act_simp_ops.net_id);
  81. bool bind = flags & TCA_ACT_FLAGS_BIND;
  82. struct nlattr *tb[TCA_DEF_MAX + 1];
  83. struct tcf_chain *goto_ch = NULL;
  84. struct tc_defact *parm;
  85. struct tcf_defact *d;
  86. bool exists = false;
  87. int ret = 0, err;
  88. u32 index;
  89. if (nla == NULL)
  90. return -EINVAL;
  91. err = nla_parse_nested_deprecated(tb, TCA_DEF_MAX, nla, simple_policy,
  92. NULL);
  93. if (err < 0)
  94. return err;
  95. if (tb[TCA_DEF_PARMS] == NULL)
  96. return -EINVAL;
  97. parm = nla_data(tb[TCA_DEF_PARMS]);
  98. index = parm->index;
  99. err = tcf_idr_check_alloc(tn, &index, a, bind);
  100. if (err < 0)
  101. return err;
  102. exists = err;
  103. if (exists && bind)
  104. return ACT_P_BOUND;
  105. if (tb[TCA_DEF_DATA] == NULL) {
  106. if (exists)
  107. tcf_idr_release(*a, bind);
  108. else
  109. tcf_idr_cleanup(tn, index);
  110. return -EINVAL;
  111. }
  112. if (!exists) {
  113. ret = tcf_idr_create(tn, index, est, a,
  114. &act_simp_ops, bind, false, flags);
  115. if (ret) {
  116. tcf_idr_cleanup(tn, index);
  117. return ret;
  118. }
  119. d = to_defact(*a);
  120. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch,
  121. extack);
  122. if (err < 0)
  123. goto release_idr;
  124. err = alloc_defdata(d, tb[TCA_DEF_DATA]);
  125. if (err < 0)
  126. goto put_chain;
  127. tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  128. ret = ACT_P_CREATED;
  129. } else {
  130. if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  131. err = -EEXIST;
  132. goto release_idr;
  133. }
  134. err = reset_policy(*a, tb[TCA_DEF_DATA], parm, tp, extack);
  135. if (err)
  136. goto release_idr;
  137. }
  138. return ret;
  139. put_chain:
  140. if (goto_ch)
  141. tcf_chain_put_by_act(goto_ch);
  142. release_idr:
  143. tcf_idr_release(*a, bind);
  144. return err;
  145. }
  146. static int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a,
  147. int bind, int ref)
  148. {
  149. unsigned char *b = skb_tail_pointer(skb);
  150. struct tcf_defact *d = to_defact(a);
  151. struct tc_defact opt = {
  152. .index = d->tcf_index,
  153. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  154. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  155. };
  156. struct tcf_t t;
  157. spin_lock_bh(&d->tcf_lock);
  158. opt.action = d->tcf_action;
  159. if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) ||
  160. nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata))
  161. goto nla_put_failure;
  162. tcf_tm_dump(&t, &d->tcf_tm);
  163. if (nla_put_64bit(skb, TCA_DEF_TM, sizeof(t), &t, TCA_DEF_PAD))
  164. goto nla_put_failure;
  165. spin_unlock_bh(&d->tcf_lock);
  166. return skb->len;
  167. nla_put_failure:
  168. spin_unlock_bh(&d->tcf_lock);
  169. nlmsg_trim(skb, b);
  170. return -1;
  171. }
  172. static struct tc_action_ops act_simp_ops = {
  173. .kind = "simple",
  174. .id = TCA_ID_SIMP,
  175. .owner = THIS_MODULE,
  176. .act = tcf_simp_act,
  177. .dump = tcf_simp_dump,
  178. .cleanup = tcf_simp_release,
  179. .init = tcf_simp_init,
  180. .size = sizeof(struct tcf_defact),
  181. };
  182. MODULE_ALIAS_NET_ACT("simple");
  183. static __net_init int simp_init_net(struct net *net)
  184. {
  185. struct tc_action_net *tn = net_generic(net, act_simp_ops.net_id);
  186. return tc_action_net_init(net, tn, &act_simp_ops);
  187. }
  188. static void __net_exit simp_exit_net(struct list_head *net_list)
  189. {
  190. tc_action_net_exit(net_list, act_simp_ops.net_id);
  191. }
  192. static struct pernet_operations simp_net_ops = {
  193. .init = simp_init_net,
  194. .exit_batch = simp_exit_net,
  195. .id = &act_simp_ops.net_id,
  196. .size = sizeof(struct tc_action_net),
  197. };
  198. MODULE_AUTHOR("Jamal Hadi Salim(2005)");
  199. MODULE_DESCRIPTION("Simple example action");
  200. MODULE_LICENSE("GPL");
  201. static int __init simp_init_module(void)
  202. {
  203. int ret = tcf_register_action(&act_simp_ops, &simp_net_ops);
  204. if (!ret)
  205. pr_info("Simple TC action Loaded\n");
  206. return ret;
  207. }
  208. static void __exit simp_cleanup_module(void)
  209. {
  210. tcf_unregister_action(&act_simp_ops, &simp_net_ops);
  211. }
  212. module_init(simp_init_module);
  213. module_exit(simp_cleanup_module);