act_sample.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * net/sched/act_sample.c - Packet sampling tc action
  3. * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/gfp.h>
  18. #include <net/net_namespace.h>
  19. #include <net/netlink.h>
  20. #include <net/pkt_sched.h>
  21. #include <linux/tc_act/tc_sample.h>
  22. #include <net/tc_act/tc_sample.h>
  23. #include <net/psample.h>
  24. #include <linux/if_arp.h>
  25. static unsigned int sample_net_id;
  26. static struct tc_action_ops act_sample_ops;
  27. static const struct nla_policy sample_policy[TCA_SAMPLE_MAX + 1] = {
  28. [TCA_SAMPLE_PARMS] = { .len = sizeof(struct tc_sample) },
  29. [TCA_SAMPLE_RATE] = { .type = NLA_U32 },
  30. [TCA_SAMPLE_TRUNC_SIZE] = { .type = NLA_U32 },
  31. [TCA_SAMPLE_PSAMPLE_GROUP] = { .type = NLA_U32 },
  32. };
  33. static int tcf_sample_init(struct net *net, struct nlattr *nla,
  34. struct nlattr *est, struct tc_action **a, int ovr,
  35. int bind, bool rtnl_held,
  36. struct netlink_ext_ack *extack)
  37. {
  38. struct tc_action_net *tn = net_generic(net, sample_net_id);
  39. struct nlattr *tb[TCA_SAMPLE_MAX + 1];
  40. struct psample_group *psample_group;
  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(tb, TCA_SAMPLE_MAX, nla, sample_policy, NULL);
  48. if (ret < 0)
  49. return ret;
  50. if (!tb[TCA_SAMPLE_PARMS] || !tb[TCA_SAMPLE_RATE] ||
  51. !tb[TCA_SAMPLE_PSAMPLE_GROUP])
  52. return -EINVAL;
  53. parm = nla_data(tb[TCA_SAMPLE_PARMS]);
  54. err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
  55. if (err < 0)
  56. return err;
  57. exists = err;
  58. if (exists && bind)
  59. return 0;
  60. if (!exists) {
  61. ret = tcf_idr_create(tn, parm->index, est, a,
  62. &act_sample_ops, bind, false);
  63. if (ret) {
  64. tcf_idr_cleanup(tn, parm->index);
  65. return ret;
  66. }
  67. ret = ACT_P_CREATED;
  68. } else if (!ovr) {
  69. tcf_idr_release(*a, bind);
  70. return -EEXIST;
  71. }
  72. s = to_sample(*a);
  73. s->tcf_action = parm->action;
  74. s->rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
  75. s->psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
  76. psample_group = psample_group_get(net, s->psample_group_num);
  77. if (!psample_group) {
  78. tcf_idr_release(*a, bind);
  79. return -ENOMEM;
  80. }
  81. RCU_INIT_POINTER(s->psample_group, psample_group);
  82. if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
  83. s->truncate = true;
  84. s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
  85. }
  86. if (ret == ACT_P_CREATED)
  87. tcf_idr_insert(tn, *a);
  88. return ret;
  89. }
  90. static void tcf_sample_cleanup(struct tc_action *a)
  91. {
  92. struct tcf_sample *s = to_sample(a);
  93. struct psample_group *psample_group;
  94. psample_group = rtnl_dereference(s->psample_group);
  95. RCU_INIT_POINTER(s->psample_group, NULL);
  96. if (psample_group)
  97. psample_group_put(psample_group);
  98. }
  99. static bool tcf_sample_dev_ok_push(struct net_device *dev)
  100. {
  101. switch (dev->type) {
  102. case ARPHRD_TUNNEL:
  103. case ARPHRD_TUNNEL6:
  104. case ARPHRD_SIT:
  105. case ARPHRD_IPGRE:
  106. case ARPHRD_VOID:
  107. case ARPHRD_NONE:
  108. return false;
  109. default:
  110. return true;
  111. }
  112. }
  113. static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
  114. struct tcf_result *res)
  115. {
  116. struct tcf_sample *s = to_sample(a);
  117. struct psample_group *psample_group;
  118. int retval;
  119. int size;
  120. int iif;
  121. int oif;
  122. tcf_lastuse_update(&s->tcf_tm);
  123. bstats_cpu_update(this_cpu_ptr(s->common.cpu_bstats), skb);
  124. retval = READ_ONCE(s->tcf_action);
  125. rcu_read_lock();
  126. psample_group = rcu_dereference(s->psample_group);
  127. /* randomly sample packets according to rate */
  128. if (psample_group && (prandom_u32() % s->rate == 0)) {
  129. if (!skb_at_tc_ingress(skb)) {
  130. iif = skb->skb_iif;
  131. oif = skb->dev->ifindex;
  132. } else {
  133. iif = skb->dev->ifindex;
  134. oif = 0;
  135. }
  136. /* on ingress, the mac header gets popped, so push it back */
  137. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  138. skb_push(skb, skb->mac_len);
  139. size = s->truncate ? s->trunc_size : skb->len;
  140. psample_sample_packet(psample_group, skb, size, iif, oif,
  141. s->rate);
  142. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  143. skb_pull(skb, skb->mac_len);
  144. }
  145. rcu_read_unlock();
  146. return retval;
  147. }
  148. static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a,
  149. int bind, int ref)
  150. {
  151. unsigned char *b = skb_tail_pointer(skb);
  152. struct tcf_sample *s = to_sample(a);
  153. struct tc_sample opt = {
  154. .index = s->tcf_index,
  155. .action = s->tcf_action,
  156. .refcnt = refcount_read(&s->tcf_refcnt) - ref,
  157. .bindcnt = atomic_read(&s->tcf_bindcnt) - bind,
  158. };
  159. struct tcf_t t;
  160. if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt))
  161. goto nla_put_failure;
  162. tcf_tm_dump(&t, &s->tcf_tm);
  163. if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD))
  164. goto nla_put_failure;
  165. if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate))
  166. goto nla_put_failure;
  167. if (s->truncate)
  168. if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size))
  169. goto nla_put_failure;
  170. if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num))
  171. goto nla_put_failure;
  172. return skb->len;
  173. nla_put_failure:
  174. nlmsg_trim(skb, b);
  175. return -1;
  176. }
  177. static int tcf_sample_walker(struct net *net, struct sk_buff *skb,
  178. struct netlink_callback *cb, int type,
  179. const struct tc_action_ops *ops,
  180. struct netlink_ext_ack *extack)
  181. {
  182. struct tc_action_net *tn = net_generic(net, sample_net_id);
  183. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  184. }
  185. static int tcf_sample_search(struct net *net, struct tc_action **a, u32 index,
  186. struct netlink_ext_ack *extack)
  187. {
  188. struct tc_action_net *tn = net_generic(net, sample_net_id);
  189. return tcf_idr_search(tn, a, index);
  190. }
  191. static int tcf_sample_delete(struct net *net, u32 index)
  192. {
  193. struct tc_action_net *tn = net_generic(net, sample_net_id);
  194. return tcf_idr_delete_index(tn, index);
  195. }
  196. static struct tc_action_ops act_sample_ops = {
  197. .kind = "sample",
  198. .type = TCA_ACT_SAMPLE,
  199. .owner = THIS_MODULE,
  200. .act = tcf_sample_act,
  201. .dump = tcf_sample_dump,
  202. .init = tcf_sample_init,
  203. .cleanup = tcf_sample_cleanup,
  204. .walk = tcf_sample_walker,
  205. .lookup = tcf_sample_search,
  206. .delete = tcf_sample_delete,
  207. .size = sizeof(struct tcf_sample),
  208. };
  209. static __net_init int sample_init_net(struct net *net)
  210. {
  211. struct tc_action_net *tn = net_generic(net, sample_net_id);
  212. return tc_action_net_init(tn, &act_sample_ops);
  213. }
  214. static void __net_exit sample_exit_net(struct list_head *net_list)
  215. {
  216. tc_action_net_exit(net_list, sample_net_id);
  217. }
  218. static struct pernet_operations sample_net_ops = {
  219. .init = sample_init_net,
  220. .exit_batch = sample_exit_net,
  221. .id = &sample_net_id,
  222. .size = sizeof(struct tc_action_net),
  223. };
  224. static int __init sample_init_module(void)
  225. {
  226. return tcf_register_action(&act_sample_ops, &sample_net_ops);
  227. }
  228. static void __exit sample_cleanup_module(void)
  229. {
  230. tcf_unregister_action(&act_sample_ops, &sample_net_ops);
  231. }
  232. module_init(sample_init_module);
  233. module_exit(sample_cleanup_module);
  234. MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
  235. MODULE_DESCRIPTION("Packet sampling action");
  236. MODULE_LICENSE("GPL v2");