act_sample.c 7.4 KB

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