psample.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2021 Mellanox Technologies. All rights reserved */
  3. #include <linux/debugfs.h>
  4. #include <linux/err.h>
  5. #include <linux/etherdevice.h>
  6. #include <linux/inet.h>
  7. #include <linux/kernel.h>
  8. #include <linux/random.h>
  9. #include <linux/slab.h>
  10. #include <net/devlink.h>
  11. #include <net/ip.h>
  12. #include <net/psample.h>
  13. #include <uapi/linux/ip.h>
  14. #include <uapi/linux/udp.h>
  15. #include "netdevsim.h"
  16. #define NSIM_PSAMPLE_REPORT_INTERVAL_MS 100
  17. #define NSIM_PSAMPLE_INVALID_TC 0xFFFF
  18. #define NSIM_PSAMPLE_L4_DATA_LEN 100
  19. struct nsim_dev_psample {
  20. struct delayed_work psample_dw;
  21. struct dentry *ddir;
  22. struct psample_group *group;
  23. u32 rate;
  24. u32 group_num;
  25. u32 trunc_size;
  26. int in_ifindex;
  27. int out_ifindex;
  28. u16 out_tc;
  29. u64 out_tc_occ_max;
  30. u64 latency_max;
  31. bool is_active;
  32. };
  33. static struct sk_buff *nsim_dev_psample_skb_build(void)
  34. {
  35. int tot_len, data_len = NSIM_PSAMPLE_L4_DATA_LEN;
  36. struct sk_buff *skb;
  37. struct udphdr *udph;
  38. struct ethhdr *eth;
  39. struct iphdr *iph;
  40. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  41. if (!skb)
  42. return NULL;
  43. tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
  44. skb_reset_mac_header(skb);
  45. eth = skb_put(skb, sizeof(struct ethhdr));
  46. eth_random_addr(eth->h_dest);
  47. eth_random_addr(eth->h_source);
  48. eth->h_proto = htons(ETH_P_IP);
  49. skb->protocol = htons(ETH_P_IP);
  50. skb_set_network_header(skb, skb->len);
  51. iph = skb_put(skb, sizeof(struct iphdr));
  52. iph->protocol = IPPROTO_UDP;
  53. iph->saddr = in_aton("192.0.2.1");
  54. iph->daddr = in_aton("198.51.100.1");
  55. iph->version = 0x4;
  56. iph->frag_off = 0;
  57. iph->ihl = 0x5;
  58. iph->tot_len = htons(tot_len);
  59. iph->id = 0;
  60. iph->ttl = 100;
  61. iph->check = 0;
  62. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  63. skb_set_transport_header(skb, skb->len);
  64. udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
  65. get_random_bytes(&udph->source, sizeof(u16));
  66. get_random_bytes(&udph->dest, sizeof(u16));
  67. udph->len = htons(sizeof(struct udphdr) + data_len);
  68. return skb;
  69. }
  70. static void nsim_dev_psample_md_prepare(const struct nsim_dev_psample *psample,
  71. struct psample_metadata *md,
  72. unsigned int len)
  73. {
  74. md->trunc_size = psample->trunc_size ? psample->trunc_size : len;
  75. md->in_ifindex = psample->in_ifindex;
  76. md->out_ifindex = psample->out_ifindex;
  77. if (psample->out_tc != NSIM_PSAMPLE_INVALID_TC) {
  78. md->out_tc = psample->out_tc;
  79. md->out_tc_valid = 1;
  80. }
  81. if (psample->out_tc_occ_max) {
  82. u64 out_tc_occ;
  83. get_random_bytes(&out_tc_occ, sizeof(u64));
  84. md->out_tc_occ = out_tc_occ & (psample->out_tc_occ_max - 1);
  85. md->out_tc_occ_valid = 1;
  86. }
  87. if (psample->latency_max) {
  88. u64 latency;
  89. get_random_bytes(&latency, sizeof(u64));
  90. md->latency = latency & (psample->latency_max - 1);
  91. md->latency_valid = 1;
  92. }
  93. }
  94. static void nsim_dev_psample_report_work(struct work_struct *work)
  95. {
  96. struct nsim_dev_psample *psample;
  97. struct psample_metadata md = {};
  98. struct sk_buff *skb;
  99. unsigned long delay;
  100. psample = container_of(work, struct nsim_dev_psample, psample_dw.work);
  101. skb = nsim_dev_psample_skb_build();
  102. if (!skb)
  103. goto out;
  104. nsim_dev_psample_md_prepare(psample, &md, skb->len);
  105. psample_sample_packet(psample->group, skb, psample->rate, &md);
  106. consume_skb(skb);
  107. out:
  108. delay = msecs_to_jiffies(NSIM_PSAMPLE_REPORT_INTERVAL_MS);
  109. schedule_delayed_work(&psample->psample_dw, delay);
  110. }
  111. static int nsim_dev_psample_enable(struct nsim_dev *nsim_dev)
  112. {
  113. struct nsim_dev_psample *psample = nsim_dev->psample;
  114. struct devlink *devlink;
  115. unsigned long delay;
  116. if (psample->is_active)
  117. return -EBUSY;
  118. devlink = priv_to_devlink(nsim_dev);
  119. psample->group = psample_group_get(devlink_net(devlink),
  120. psample->group_num);
  121. if (!psample->group)
  122. return -EINVAL;
  123. delay = msecs_to_jiffies(NSIM_PSAMPLE_REPORT_INTERVAL_MS);
  124. schedule_delayed_work(&psample->psample_dw, delay);
  125. psample->is_active = true;
  126. return 0;
  127. }
  128. static int nsim_dev_psample_disable(struct nsim_dev *nsim_dev)
  129. {
  130. struct nsim_dev_psample *psample = nsim_dev->psample;
  131. if (!psample->is_active)
  132. return -EINVAL;
  133. psample->is_active = false;
  134. cancel_delayed_work_sync(&psample->psample_dw);
  135. psample_group_put(psample->group);
  136. return 0;
  137. }
  138. static ssize_t nsim_dev_psample_enable_write(struct file *file,
  139. const char __user *data,
  140. size_t count, loff_t *ppos)
  141. {
  142. struct nsim_dev *nsim_dev = file->private_data;
  143. bool enable;
  144. int err;
  145. err = kstrtobool_from_user(data, count, &enable);
  146. if (err)
  147. return err;
  148. if (enable)
  149. err = nsim_dev_psample_enable(nsim_dev);
  150. else
  151. err = nsim_dev_psample_disable(nsim_dev);
  152. return err ? err : count;
  153. }
  154. static const struct file_operations nsim_psample_enable_fops = {
  155. .open = simple_open,
  156. .write = nsim_dev_psample_enable_write,
  157. .llseek = generic_file_llseek,
  158. .owner = THIS_MODULE,
  159. };
  160. int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
  161. {
  162. struct nsim_dev_psample *psample;
  163. int err;
  164. psample = kzalloc(sizeof(*psample), GFP_KERNEL);
  165. if (!psample)
  166. return -ENOMEM;
  167. nsim_dev->psample = psample;
  168. INIT_DELAYED_WORK(&psample->psample_dw, nsim_dev_psample_report_work);
  169. psample->ddir = debugfs_create_dir("psample", nsim_dev->ddir);
  170. if (IS_ERR(psample->ddir)) {
  171. err = PTR_ERR(psample->ddir);
  172. goto err_psample_free;
  173. }
  174. /* Populate sampling parameters with sane defaults. */
  175. psample->rate = 100;
  176. debugfs_create_u32("rate", 0600, psample->ddir, &psample->rate);
  177. psample->group_num = 10;
  178. debugfs_create_u32("group_num", 0600, psample->ddir,
  179. &psample->group_num);
  180. psample->trunc_size = 0;
  181. debugfs_create_u32("trunc_size", 0600, psample->ddir,
  182. &psample->trunc_size);
  183. psample->in_ifindex = 1;
  184. debugfs_create_u32("in_ifindex", 0600, psample->ddir,
  185. &psample->in_ifindex);
  186. psample->out_ifindex = 2;
  187. debugfs_create_u32("out_ifindex", 0600, psample->ddir,
  188. &psample->out_ifindex);
  189. psample->out_tc = 0;
  190. debugfs_create_u16("out_tc", 0600, psample->ddir, &psample->out_tc);
  191. psample->out_tc_occ_max = 10000;
  192. debugfs_create_u64("out_tc_occ_max", 0600, psample->ddir,
  193. &psample->out_tc_occ_max);
  194. psample->latency_max = 50;
  195. debugfs_create_u64("latency_max", 0600, psample->ddir,
  196. &psample->latency_max);
  197. debugfs_create_file("enable", 0200, psample->ddir, nsim_dev,
  198. &nsim_psample_enable_fops);
  199. return 0;
  200. err_psample_free:
  201. kfree(nsim_dev->psample);
  202. return err;
  203. }
  204. void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
  205. {
  206. debugfs_remove_recursive(nsim_dev->psample->ddir);
  207. if (nsim_dev->psample->is_active) {
  208. cancel_delayed_work_sync(&nsim_dev->psample->psample_dw);
  209. psample_group_put(nsim_dev->psample->group);
  210. }
  211. kfree(nsim_dev->psample);
  212. }