act_sample.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/sched/act_sample.c - Packet sampling tc action
  4. * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/errno.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/gfp.h>
  15. #include <net/net_namespace.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <linux/tc_act/tc_sample.h>
  19. #include <net/tc_act/tc_sample.h>
  20. #include <net/psample.h>
  21. #include <net/pkt_cls.h>
  22. #include <linux/if_arp.h>
  23. static unsigned int sample_net_id;
  24. static struct tc_action_ops act_sample_ops;
  25. static const struct nla_policy sample_policy[TCA_SAMPLE_MAX + 1] = {
  26. [TCA_SAMPLE_PARMS] = { .len = sizeof(struct tc_sample) },
  27. [TCA_SAMPLE_RATE] = { .type = NLA_U32 },
  28. [TCA_SAMPLE_TRUNC_SIZE] = { .type = NLA_U32 },
  29. [TCA_SAMPLE_PSAMPLE_GROUP] = { .type = NLA_U32 },
  30. };
  31. static int tcf_sample_init(struct net *net, struct nlattr *nla,
  32. struct nlattr *est, struct tc_action **a, int ovr,
  33. int bind, bool rtnl_held, struct tcf_proto *tp,
  34. u32 flags, struct netlink_ext_ack *extack)
  35. {
  36. struct tc_action_net *tn = net_generic(net, sample_net_id);
  37. struct nlattr *tb[TCA_SAMPLE_MAX + 1];
  38. struct psample_group *psample_group;
  39. u32 psample_group_num, rate, index;
  40. struct tcf_chain *goto_ch = NULL;
  41. struct tc_sample *parm;
  42. struct tcf_sample *s;
  43. bool exists = false;
  44. int ret, err;
  45. if (!nla)
  46. return -EINVAL;
  47. ret = nla_parse_nested_deprecated(tb, TCA_SAMPLE_MAX, nla,
  48. sample_policy, NULL);
  49. if (ret < 0)
  50. return ret;
  51. if (!tb[TCA_SAMPLE_PARMS] || !tb[TCA_SAMPLE_RATE] ||
  52. !tb[TCA_SAMPLE_PSAMPLE_GROUP])
  53. return -EINVAL;
  54. parm = nla_data(tb[TCA_SAMPLE_PARMS]);
  55. index = parm->index;
  56. err = tcf_idr_check_alloc(tn, &index, a, bind);
  57. if (err < 0)
  58. return err;
  59. exists = err;
  60. if (exists && bind)
  61. return 0;
  62. if (!exists) {
  63. ret = tcf_idr_create(tn, index, est, a,
  64. &act_sample_ops, bind, true, 0);
  65. if (ret) {
  66. tcf_idr_cleanup(tn, index);
  67. return ret;
  68. }
  69. ret = ACT_P_CREATED;
  70. } else if (!ovr) {
  71. tcf_idr_release(*a, bind);
  72. return -EEXIST;
  73. }
  74. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  75. if (err < 0)
  76. goto release_idr;
  77. rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
  78. if (!rate) {
  79. NL_SET_ERR_MSG(extack, "invalid sample rate");
  80. err = -EINVAL;
  81. goto put_chain;
  82. }
  83. psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
  84. psample_group = psample_group_get(net, psample_group_num);
  85. if (!psample_group) {
  86. err = -ENOMEM;
  87. goto put_chain;
  88. }
  89. s = to_sample(*a);
  90. spin_lock_bh(&s->tcf_lock);
  91. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  92. s->rate = rate;
  93. s->psample_group_num = psample_group_num;
  94. psample_group = rcu_replace_pointer(s->psample_group, psample_group,
  95. lockdep_is_held(&s->tcf_lock));
  96. if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
  97. s->truncate = true;
  98. s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
  99. }
  100. spin_unlock_bh(&s->tcf_lock);
  101. if (psample_group)
  102. psample_group_put(psample_group);
  103. if (goto_ch)
  104. tcf_chain_put_by_act(goto_ch);
  105. return ret;
  106. put_chain:
  107. if (goto_ch)
  108. tcf_chain_put_by_act(goto_ch);
  109. release_idr:
  110. tcf_idr_release(*a, bind);
  111. return err;
  112. }
  113. static void tcf_sample_cleanup(struct tc_action *a)
  114. {
  115. struct tcf_sample *s = to_sample(a);
  116. struct psample_group *psample_group;
  117. /* last reference to action, no need to lock */
  118. psample_group = rcu_dereference_protected(s->psample_group, 1);
  119. RCU_INIT_POINTER(s->psample_group, NULL);
  120. if (psample_group)
  121. psample_group_put(psample_group);
  122. }
  123. static bool tcf_sample_dev_ok_push(struct net_device *dev)
  124. {
  125. switch (dev->type) {
  126. case ARPHRD_TUNNEL:
  127. case ARPHRD_TUNNEL6:
  128. case ARPHRD_SIT:
  129. case ARPHRD_IPGRE:
  130. case ARPHRD_IP6GRE:
  131. case ARPHRD_VOID:
  132. case ARPHRD_NONE:
  133. return false;
  134. default:
  135. return true;
  136. }
  137. }
  138. static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
  139. struct tcf_result *res)
  140. {
  141. struct tcf_sample *s = to_sample(a);
  142. struct psample_group *psample_group;
  143. int retval;
  144. int size;
  145. int iif;
  146. int oif;
  147. tcf_lastuse_update(&s->tcf_tm);
  148. bstats_cpu_update(this_cpu_ptr(s->common.cpu_bstats), skb);
  149. retval = READ_ONCE(s->tcf_action);
  150. psample_group = rcu_dereference_bh(s->psample_group);
  151. /* randomly sample packets according to rate */
  152. if (psample_group && (prandom_u32() % s->rate == 0)) {
  153. if (!skb_at_tc_ingress(skb)) {
  154. iif = skb->skb_iif;
  155. oif = skb->dev->ifindex;
  156. } else {
  157. iif = skb->dev->ifindex;
  158. oif = 0;
  159. }
  160. /* on ingress, the mac header gets popped, so push it back */
  161. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  162. skb_push(skb, skb->mac_len);
  163. size = s->truncate ? s->trunc_size : skb->len;
  164. psample_sample_packet(psample_group, skb, size, iif, oif,
  165. s->rate);
  166. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  167. skb_pull(skb, skb->mac_len);
  168. }
  169. return retval;
  170. }
  171. static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a,
  172. int bind, int ref)
  173. {
  174. unsigned char *b = skb_tail_pointer(skb);
  175. struct tcf_sample *s = to_sample(a);
  176. struct tc_sample opt = {
  177. .index = s->tcf_index,
  178. .refcnt = refcount_read(&s->tcf_refcnt) - ref,
  179. .bindcnt = atomic_read(&s->tcf_bindcnt) - bind,
  180. };
  181. struct tcf_t t;
  182. spin_lock_bh(&s->tcf_lock);
  183. opt.action = s->tcf_action;
  184. if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt))
  185. goto nla_put_failure;
  186. tcf_tm_dump(&t, &s->tcf_tm);
  187. if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD))
  188. goto nla_put_failure;
  189. if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate))
  190. goto nla_put_failure;
  191. if (s->truncate)
  192. if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size))
  193. goto nla_put_failure;
  194. if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num))
  195. goto nla_put_failure;
  196. spin_unlock_bh(&s->tcf_lock);
  197. return skb->len;
  198. nla_put_failure:
  199. spin_unlock_bh(&s->tcf_lock);
  200. nlmsg_trim(skb, b);
  201. return -1;
  202. }
  203. static int tcf_sample_walker(struct net *net, struct sk_buff *skb,
  204. struct netlink_callback *cb, int type,
  205. const struct tc_action_ops *ops,
  206. struct netlink_ext_ack *extack)
  207. {
  208. struct tc_action_net *tn = net_generic(net, sample_net_id);
  209. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  210. }
  211. static int tcf_sample_search(struct net *net, struct tc_action **a, u32 index)
  212. {
  213. struct tc_action_net *tn = net_generic(net, sample_net_id);
  214. return tcf_idr_search(tn, a, index);
  215. }
  216. static void tcf_psample_group_put(void *priv)
  217. {
  218. struct psample_group *group = priv;
  219. psample_group_put(group);
  220. }
  221. static struct psample_group *
  222. tcf_sample_get_group(const struct tc_action *a,
  223. tc_action_priv_destructor *destructor)
  224. {
  225. struct tcf_sample *s = to_sample(a);
  226. struct psample_group *group;
  227. group = rcu_dereference_protected(s->psample_group,
  228. lockdep_is_held(&s->tcf_lock));
  229. if (group) {
  230. psample_group_take(group);
  231. *destructor = tcf_psample_group_put;
  232. }
  233. return group;
  234. }
  235. static struct tc_action_ops act_sample_ops = {
  236. .kind = "sample",
  237. .id = TCA_ID_SAMPLE,
  238. .owner = THIS_MODULE,
  239. .act = tcf_sample_act,
  240. .dump = tcf_sample_dump,
  241. .init = tcf_sample_init,
  242. .cleanup = tcf_sample_cleanup,
  243. .walk = tcf_sample_walker,
  244. .lookup = tcf_sample_search,
  245. .get_psample_group = tcf_sample_get_group,
  246. .size = sizeof(struct tcf_sample),
  247. };
  248. static __net_init int sample_init_net(struct net *net)
  249. {
  250. struct tc_action_net *tn = net_generic(net, sample_net_id);
  251. return tc_action_net_init(net, tn, &act_sample_ops);
  252. }
  253. static void __net_exit sample_exit_net(struct list_head *net_list)
  254. {
  255. tc_action_net_exit(net_list, sample_net_id);
  256. }
  257. static struct pernet_operations sample_net_ops = {
  258. .init = sample_init_net,
  259. .exit_batch = sample_exit_net,
  260. .id = &sample_net_id,
  261. .size = sizeof(struct tc_action_net),
  262. };
  263. static int __init sample_init_module(void)
  264. {
  265. return tcf_register_action(&act_sample_ops, &sample_net_ops);
  266. }
  267. static void __exit sample_cleanup_module(void)
  268. {
  269. tcf_unregister_action(&act_sample_ops, &sample_net_ops);
  270. }
  271. module_init(sample_init_module);
  272. module_exit(sample_cleanup_module);
  273. MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
  274. MODULE_DESCRIPTION("Packet sampling action");
  275. MODULE_LICENSE("GPL v2");