act_sample.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, struct netlink_ext_ack *extack)
  36. {
  37. struct tc_action_net *tn = net_generic(net, sample_net_id);
  38. struct nlattr *tb[TCA_SAMPLE_MAX + 1];
  39. struct psample_group *psample_group;
  40. struct tc_sample *parm;
  41. struct tcf_sample *s;
  42. bool exists = false;
  43. int ret;
  44. if (!nla)
  45. return -EINVAL;
  46. ret = nla_parse_nested(tb, TCA_SAMPLE_MAX, nla, sample_policy, NULL);
  47. if (ret < 0)
  48. return ret;
  49. if (!tb[TCA_SAMPLE_PARMS] || !tb[TCA_SAMPLE_RATE] ||
  50. !tb[TCA_SAMPLE_PSAMPLE_GROUP])
  51. return -EINVAL;
  52. parm = nla_data(tb[TCA_SAMPLE_PARMS]);
  53. exists = tcf_idr_check(tn, parm->index, a, bind);
  54. if (exists && bind)
  55. return 0;
  56. if (!exists) {
  57. ret = tcf_idr_create(tn, parm->index, est, a,
  58. &act_sample_ops, bind, false);
  59. if (ret)
  60. return ret;
  61. ret = ACT_P_CREATED;
  62. } else {
  63. tcf_idr_release(*a, bind);
  64. if (!ovr)
  65. return -EEXIST;
  66. }
  67. s = to_sample(*a);
  68. s->tcf_action = parm->action;
  69. s->rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
  70. s->psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
  71. psample_group = psample_group_get(net, s->psample_group_num);
  72. if (!psample_group) {
  73. if (ret == ACT_P_CREATED)
  74. tcf_idr_release(*a, bind);
  75. return -ENOMEM;
  76. }
  77. RCU_INIT_POINTER(s->psample_group, psample_group);
  78. if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
  79. s->truncate = true;
  80. s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
  81. }
  82. if (ret == ACT_P_CREATED)
  83. tcf_idr_insert(tn, *a);
  84. return ret;
  85. }
  86. static void tcf_sample_cleanup(struct tc_action *a)
  87. {
  88. struct tcf_sample *s = to_sample(a);
  89. struct psample_group *psample_group;
  90. psample_group = rtnl_dereference(s->psample_group);
  91. RCU_INIT_POINTER(s->psample_group, NULL);
  92. if (psample_group)
  93. psample_group_put(psample_group);
  94. }
  95. static bool tcf_sample_dev_ok_push(struct net_device *dev)
  96. {
  97. switch (dev->type) {
  98. case ARPHRD_TUNNEL:
  99. case ARPHRD_TUNNEL6:
  100. case ARPHRD_SIT:
  101. case ARPHRD_IPGRE:
  102. case ARPHRD_VOID:
  103. case ARPHRD_NONE:
  104. return false;
  105. default:
  106. return true;
  107. }
  108. }
  109. static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
  110. struct tcf_result *res)
  111. {
  112. struct tcf_sample *s = to_sample(a);
  113. struct psample_group *psample_group;
  114. int retval;
  115. int size;
  116. int iif;
  117. int oif;
  118. tcf_lastuse_update(&s->tcf_tm);
  119. bstats_cpu_update(this_cpu_ptr(s->common.cpu_bstats), skb);
  120. retval = READ_ONCE(s->tcf_action);
  121. rcu_read_lock();
  122. psample_group = rcu_dereference(s->psample_group);
  123. /* randomly sample packets according to rate */
  124. if (psample_group && (prandom_u32() % s->rate == 0)) {
  125. if (!skb_at_tc_ingress(skb)) {
  126. iif = skb->skb_iif;
  127. oif = skb->dev->ifindex;
  128. } else {
  129. iif = skb->dev->ifindex;
  130. oif = 0;
  131. }
  132. /* on ingress, the mac header gets popped, so push it back */
  133. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  134. skb_push(skb, skb->mac_len);
  135. size = s->truncate ? s->trunc_size : skb->len;
  136. psample_sample_packet(psample_group, skb, size, iif, oif,
  137. s->rate);
  138. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  139. skb_pull(skb, skb->mac_len);
  140. }
  141. rcu_read_unlock();
  142. return retval;
  143. }
  144. static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a,
  145. int bind, int ref)
  146. {
  147. unsigned char *b = skb_tail_pointer(skb);
  148. struct tcf_sample *s = to_sample(a);
  149. struct tc_sample opt = {
  150. .index = s->tcf_index,
  151. .action = s->tcf_action,
  152. .refcnt = s->tcf_refcnt - ref,
  153. .bindcnt = s->tcf_bindcnt - bind,
  154. };
  155. struct tcf_t t;
  156. if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt))
  157. goto nla_put_failure;
  158. tcf_tm_dump(&t, &s->tcf_tm);
  159. if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD))
  160. goto nla_put_failure;
  161. if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate))
  162. goto nla_put_failure;
  163. if (s->truncate)
  164. if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size))
  165. goto nla_put_failure;
  166. if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num))
  167. goto nla_put_failure;
  168. return skb->len;
  169. nla_put_failure:
  170. nlmsg_trim(skb, b);
  171. return -1;
  172. }
  173. static int tcf_sample_walker(struct net *net, struct sk_buff *skb,
  174. struct netlink_callback *cb, int type,
  175. const struct tc_action_ops *ops,
  176. struct netlink_ext_ack *extack)
  177. {
  178. struct tc_action_net *tn = net_generic(net, sample_net_id);
  179. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  180. }
  181. static int tcf_sample_search(struct net *net, struct tc_action **a, u32 index,
  182. struct netlink_ext_ack *extack)
  183. {
  184. struct tc_action_net *tn = net_generic(net, sample_net_id);
  185. return tcf_idr_search(tn, a, index);
  186. }
  187. static struct tc_action_ops act_sample_ops = {
  188. .kind = "sample",
  189. .type = TCA_ACT_SAMPLE,
  190. .owner = THIS_MODULE,
  191. .act = tcf_sample_act,
  192. .dump = tcf_sample_dump,
  193. .init = tcf_sample_init,
  194. .cleanup = tcf_sample_cleanup,
  195. .walk = tcf_sample_walker,
  196. .lookup = tcf_sample_search,
  197. .size = sizeof(struct tcf_sample),
  198. };
  199. static __net_init int sample_init_net(struct net *net)
  200. {
  201. struct tc_action_net *tn = net_generic(net, sample_net_id);
  202. return tc_action_net_init(tn, &act_sample_ops);
  203. }
  204. static void __net_exit sample_exit_net(struct list_head *net_list)
  205. {
  206. tc_action_net_exit(net_list, sample_net_id);
  207. }
  208. static struct pernet_operations sample_net_ops = {
  209. .init = sample_init_net,
  210. .exit_batch = sample_exit_net,
  211. .id = &sample_net_id,
  212. .size = sizeof(struct tc_action_net),
  213. };
  214. static int __init sample_init_module(void)
  215. {
  216. return tcf_register_action(&act_sample_ops, &sample_net_ops);
  217. }
  218. static void __exit sample_cleanup_module(void)
  219. {
  220. tcf_unregister_action(&act_sample_ops, &sample_net_ops);
  221. }
  222. module_init(sample_init_module);
  223. module_exit(sample_cleanup_module);
  224. MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
  225. MODULE_DESCRIPTION("Packet sampling action");
  226. MODULE_LICENSE("GPL v2");