act_sample.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <[email protected]>
  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 struct tc_action_ops act_sample_ops;
  24. static const struct nla_policy sample_policy[TCA_SAMPLE_MAX + 1] = {
  25. [TCA_SAMPLE_PARMS] = { .len = sizeof(struct tc_sample) },
  26. [TCA_SAMPLE_RATE] = { .type = NLA_U32 },
  27. [TCA_SAMPLE_TRUNC_SIZE] = { .type = NLA_U32 },
  28. [TCA_SAMPLE_PSAMPLE_GROUP] = { .type = NLA_U32 },
  29. };
  30. static int tcf_sample_init(struct net *net, struct nlattr *nla,
  31. struct nlattr *est, struct tc_action **a,
  32. struct tcf_proto *tp,
  33. u32 flags, struct netlink_ext_ack *extack)
  34. {
  35. struct tc_action_net *tn = net_generic(net, act_sample_ops.net_id);
  36. bool bind = flags & TCA_ACT_FLAGS_BIND;
  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])
  52. return -EINVAL;
  53. parm = nla_data(tb[TCA_SAMPLE_PARMS]);
  54. index = parm->index;
  55. err = tcf_idr_check_alloc(tn, &index, a, bind);
  56. if (err < 0)
  57. return err;
  58. exists = err;
  59. if (exists && bind)
  60. return 0;
  61. if (!exists) {
  62. ret = tcf_idr_create(tn, index, est, a,
  63. &act_sample_ops, bind, true, flags);
  64. if (ret) {
  65. tcf_idr_cleanup(tn, index);
  66. return ret;
  67. }
  68. ret = ACT_P_CREATED;
  69. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  70. tcf_idr_release(*a, bind);
  71. return -EEXIST;
  72. }
  73. if (!tb[TCA_SAMPLE_RATE] || !tb[TCA_SAMPLE_PSAMPLE_GROUP]) {
  74. NL_SET_ERR_MSG(extack, "sample rate and group are required");
  75. err = -EINVAL;
  76. goto release_idr;
  77. }
  78. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  79. if (err < 0)
  80. goto release_idr;
  81. rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
  82. if (!rate) {
  83. NL_SET_ERR_MSG(extack, "invalid sample rate");
  84. err = -EINVAL;
  85. goto put_chain;
  86. }
  87. psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
  88. psample_group = psample_group_get(net, psample_group_num);
  89. if (!psample_group) {
  90. err = -ENOMEM;
  91. goto put_chain;
  92. }
  93. s = to_sample(*a);
  94. spin_lock_bh(&s->tcf_lock);
  95. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  96. s->rate = rate;
  97. s->psample_group_num = psample_group_num;
  98. psample_group = rcu_replace_pointer(s->psample_group, psample_group,
  99. lockdep_is_held(&s->tcf_lock));
  100. if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
  101. s->truncate = true;
  102. s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
  103. }
  104. spin_unlock_bh(&s->tcf_lock);
  105. if (psample_group)
  106. psample_group_put(psample_group);
  107. if (goto_ch)
  108. tcf_chain_put_by_act(goto_ch);
  109. return ret;
  110. put_chain:
  111. if (goto_ch)
  112. tcf_chain_put_by_act(goto_ch);
  113. release_idr:
  114. tcf_idr_release(*a, bind);
  115. return err;
  116. }
  117. static void tcf_sample_cleanup(struct tc_action *a)
  118. {
  119. struct tcf_sample *s = to_sample(a);
  120. struct psample_group *psample_group;
  121. /* last reference to action, no need to lock */
  122. psample_group = rcu_dereference_protected(s->psample_group, 1);
  123. RCU_INIT_POINTER(s->psample_group, NULL);
  124. if (psample_group)
  125. psample_group_put(psample_group);
  126. }
  127. static bool tcf_sample_dev_ok_push(struct net_device *dev)
  128. {
  129. switch (dev->type) {
  130. case ARPHRD_TUNNEL:
  131. case ARPHRD_TUNNEL6:
  132. case ARPHRD_SIT:
  133. case ARPHRD_IPGRE:
  134. case ARPHRD_IP6GRE:
  135. case ARPHRD_VOID:
  136. case ARPHRD_NONE:
  137. return false;
  138. default:
  139. return true;
  140. }
  141. }
  142. static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
  143. struct tcf_result *res)
  144. {
  145. struct tcf_sample *s = to_sample(a);
  146. struct psample_group *psample_group;
  147. struct psample_metadata md = {};
  148. int retval;
  149. tcf_lastuse_update(&s->tcf_tm);
  150. bstats_update(this_cpu_ptr(s->common.cpu_bstats), skb);
  151. retval = READ_ONCE(s->tcf_action);
  152. psample_group = rcu_dereference_bh(s->psample_group);
  153. /* randomly sample packets according to rate */
  154. if (psample_group && (prandom_u32_max(s->rate) == 0)) {
  155. if (!skb_at_tc_ingress(skb)) {
  156. md.in_ifindex = skb->skb_iif;
  157. md.out_ifindex = skb->dev->ifindex;
  158. } else {
  159. md.in_ifindex = skb->dev->ifindex;
  160. }
  161. /* on ingress, the mac header gets popped, so push it back */
  162. if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
  163. skb_push(skb, skb->mac_len);
  164. md.trunc_size = s->truncate ? s->trunc_size : skb->len;
  165. psample_sample_packet(psample_group, skb, s->rate, &md);
  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 void tcf_sample_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  172. u64 drops, u64 lastuse, bool hw)
  173. {
  174. struct tcf_sample *s = to_sample(a);
  175. struct tcf_t *tm = &s->tcf_tm;
  176. tcf_action_update_stats(a, bytes, packets, drops, hw);
  177. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  178. }
  179. static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a,
  180. int bind, int ref)
  181. {
  182. unsigned char *b = skb_tail_pointer(skb);
  183. struct tcf_sample *s = to_sample(a);
  184. struct tc_sample opt = {
  185. .index = s->tcf_index,
  186. .refcnt = refcount_read(&s->tcf_refcnt) - ref,
  187. .bindcnt = atomic_read(&s->tcf_bindcnt) - bind,
  188. };
  189. struct tcf_t t;
  190. spin_lock_bh(&s->tcf_lock);
  191. opt.action = s->tcf_action;
  192. if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt))
  193. goto nla_put_failure;
  194. tcf_tm_dump(&t, &s->tcf_tm);
  195. if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD))
  196. goto nla_put_failure;
  197. if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate))
  198. goto nla_put_failure;
  199. if (s->truncate)
  200. if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size))
  201. goto nla_put_failure;
  202. if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num))
  203. goto nla_put_failure;
  204. spin_unlock_bh(&s->tcf_lock);
  205. return skb->len;
  206. nla_put_failure:
  207. spin_unlock_bh(&s->tcf_lock);
  208. nlmsg_trim(skb, b);
  209. return -1;
  210. }
  211. static void tcf_psample_group_put(void *priv)
  212. {
  213. struct psample_group *group = priv;
  214. psample_group_put(group);
  215. }
  216. static struct psample_group *
  217. tcf_sample_get_group(const struct tc_action *a,
  218. tc_action_priv_destructor *destructor)
  219. {
  220. struct tcf_sample *s = to_sample(a);
  221. struct psample_group *group;
  222. group = rcu_dereference_protected(s->psample_group,
  223. lockdep_is_held(&s->tcf_lock));
  224. if (group) {
  225. psample_group_take(group);
  226. *destructor = tcf_psample_group_put;
  227. }
  228. return group;
  229. }
  230. static void tcf_offload_sample_get_group(struct flow_action_entry *entry,
  231. const struct tc_action *act)
  232. {
  233. entry->sample.psample_group =
  234. act->ops->get_psample_group(act, &entry->destructor);
  235. entry->destructor_priv = entry->sample.psample_group;
  236. }
  237. static int tcf_sample_offload_act_setup(struct tc_action *act, void *entry_data,
  238. u32 *index_inc, bool bind,
  239. struct netlink_ext_ack *extack)
  240. {
  241. if (bind) {
  242. struct flow_action_entry *entry = entry_data;
  243. entry->id = FLOW_ACTION_SAMPLE;
  244. entry->sample.trunc_size = tcf_sample_trunc_size(act);
  245. entry->sample.truncate = tcf_sample_truncate(act);
  246. entry->sample.rate = tcf_sample_rate(act);
  247. tcf_offload_sample_get_group(entry, act);
  248. *index_inc = 1;
  249. } else {
  250. struct flow_offload_action *fl_action = entry_data;
  251. fl_action->id = FLOW_ACTION_SAMPLE;
  252. }
  253. return 0;
  254. }
  255. static struct tc_action_ops act_sample_ops = {
  256. .kind = "sample",
  257. .id = TCA_ID_SAMPLE,
  258. .owner = THIS_MODULE,
  259. .act = tcf_sample_act,
  260. .stats_update = tcf_sample_stats_update,
  261. .dump = tcf_sample_dump,
  262. .init = tcf_sample_init,
  263. .cleanup = tcf_sample_cleanup,
  264. .get_psample_group = tcf_sample_get_group,
  265. .offload_act_setup = tcf_sample_offload_act_setup,
  266. .size = sizeof(struct tcf_sample),
  267. };
  268. static __net_init int sample_init_net(struct net *net)
  269. {
  270. struct tc_action_net *tn = net_generic(net, act_sample_ops.net_id);
  271. return tc_action_net_init(net, tn, &act_sample_ops);
  272. }
  273. static void __net_exit sample_exit_net(struct list_head *net_list)
  274. {
  275. tc_action_net_exit(net_list, act_sample_ops.net_id);
  276. }
  277. static struct pernet_operations sample_net_ops = {
  278. .init = sample_init_net,
  279. .exit_batch = sample_exit_net,
  280. .id = &act_sample_ops.net_id,
  281. .size = sizeof(struct tc_action_net),
  282. };
  283. static int __init sample_init_module(void)
  284. {
  285. return tcf_register_action(&act_sample_ops, &sample_net_ops);
  286. }
  287. static void __exit sample_cleanup_module(void)
  288. {
  289. tcf_unregister_action(&act_sample_ops, &sample_net_ops);
  290. }
  291. module_init(sample_init_module);
  292. module_exit(sample_cleanup_module);
  293. MODULE_AUTHOR("Yotam Gigi <[email protected]>");
  294. MODULE_DESCRIPTION("Packet sampling action");
  295. MODULE_LICENSE("GPL v2");