1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Mellanox Technologies. All rights reserved */
4 #include <linux/debugfs.h>
6 #include <linux/etherdevice.h>
7 #include <linux/inet.h>
8 #include <linux/kernel.h>
9 #include <linux/random.h>
10 #include <linux/slab.h>
11 #include <net/devlink.h>
13 #include <net/psample.h>
14 #include <uapi/linux/ip.h>
15 #include <uapi/linux/udp.h>
17 #include "netdevsim.h"
19 #define NSIM_PSAMPLE_REPORT_INTERVAL_MS 100
20 #define NSIM_PSAMPLE_INVALID_TC 0xFFFF
21 #define NSIM_PSAMPLE_L4_DATA_LEN 100
23 struct nsim_dev_psample {
24 struct delayed_work psample_dw;
26 struct psample_group *group;
38 static struct sk_buff *nsim_dev_psample_skb_build(void)
40 int tot_len, data_len = NSIM_PSAMPLE_L4_DATA_LEN;
46 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
49 tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
51 skb_reset_mac_header(skb);
52 eth = skb_put(skb, sizeof(struct ethhdr));
53 eth_random_addr(eth->h_dest);
54 eth_random_addr(eth->h_source);
55 eth->h_proto = htons(ETH_P_IP);
56 skb->protocol = htons(ETH_P_IP);
58 skb_set_network_header(skb, skb->len);
59 iph = skb_put(skb, sizeof(struct iphdr));
60 iph->protocol = IPPROTO_UDP;
61 iph->saddr = in_aton("192.0.2.1");
62 iph->daddr = in_aton("198.51.100.1");
66 iph->tot_len = htons(tot_len);
70 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
72 skb_set_transport_header(skb, skb->len);
73 udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
74 get_random_bytes(&udph->source, sizeof(u16));
75 get_random_bytes(&udph->dest, sizeof(u16));
76 udph->len = htons(sizeof(struct udphdr) + data_len);
81 static void nsim_dev_psample_md_prepare(const struct nsim_dev_psample *psample,
82 struct psample_metadata *md,
85 md->trunc_size = psample->trunc_size ? psample->trunc_size : len;
86 md->in_ifindex = psample->in_ifindex;
87 md->out_ifindex = psample->out_ifindex;
89 if (psample->out_tc != NSIM_PSAMPLE_INVALID_TC) {
90 md->out_tc = psample->out_tc;
94 if (psample->out_tc_occ_max) {
97 get_random_bytes(&out_tc_occ, sizeof(u64));
98 md->out_tc_occ = out_tc_occ & (psample->out_tc_occ_max - 1);
99 md->out_tc_occ_valid = 1;
102 if (psample->latency_max) {
105 get_random_bytes(&latency, sizeof(u64));
106 md->latency = latency & (psample->latency_max - 1);
107 md->latency_valid = 1;
111 static void nsim_dev_psample_report_work(struct work_struct *work)
113 struct nsim_dev_psample *psample;
114 struct psample_metadata md = {};
118 psample = container_of(work, struct nsim_dev_psample, psample_dw.work);
120 skb = nsim_dev_psample_skb_build();
124 nsim_dev_psample_md_prepare(psample, &md, skb->len);
125 psample_sample_packet(psample->group, skb, psample->rate, &md);
129 delay = msecs_to_jiffies(NSIM_PSAMPLE_REPORT_INTERVAL_MS);
130 schedule_delayed_work(&psample->psample_dw, delay);
133 static int nsim_dev_psample_enable(struct nsim_dev *nsim_dev)
135 struct nsim_dev_psample *psample = nsim_dev->psample;
136 struct devlink *devlink;
139 if (psample->is_active)
142 devlink = priv_to_devlink(nsim_dev);
143 psample->group = psample_group_get(devlink_net(devlink),
148 delay = msecs_to_jiffies(NSIM_PSAMPLE_REPORT_INTERVAL_MS);
149 schedule_delayed_work(&psample->psample_dw, delay);
151 psample->is_active = true;
156 static int nsim_dev_psample_disable(struct nsim_dev *nsim_dev)
158 struct nsim_dev_psample *psample = nsim_dev->psample;
160 if (!psample->is_active)
163 psample->is_active = false;
165 cancel_delayed_work_sync(&psample->psample_dw);
166 psample_group_put(psample->group);
171 static ssize_t nsim_dev_psample_enable_write(struct file *file,
172 const char __user *data,
173 size_t count, loff_t *ppos)
175 struct nsim_dev *nsim_dev = file->private_data;
179 err = kstrtobool_from_user(data, count, &enable);
184 err = nsim_dev_psample_enable(nsim_dev);
186 err = nsim_dev_psample_disable(nsim_dev);
188 return err ? err : count;
191 static const struct file_operations nsim_psample_enable_fops = {
193 .write = nsim_dev_psample_enable_write,
194 .llseek = generic_file_llseek,
195 .owner = THIS_MODULE,
198 int nsim_dev_psample_init(struct nsim_dev *nsim_dev)
200 struct nsim_dev_psample *psample;
203 psample = kzalloc(sizeof(*psample), GFP_KERNEL);
206 nsim_dev->psample = psample;
208 INIT_DELAYED_WORK(&psample->psample_dw, nsim_dev_psample_report_work);
210 psample->ddir = debugfs_create_dir("psample", nsim_dev->ddir);
211 if (IS_ERR(psample->ddir)) {
212 err = PTR_ERR(psample->ddir);
213 goto err_psample_free;
216 /* Populate sampling parameters with sane defaults. */
218 debugfs_create_u32("rate", 0600, psample->ddir, &psample->rate);
220 psample->group_num = 10;
221 debugfs_create_u32("group_num", 0600, psample->ddir,
222 &psample->group_num);
224 psample->trunc_size = 0;
225 debugfs_create_u32("trunc_size", 0600, psample->ddir,
226 &psample->trunc_size);
228 psample->in_ifindex = 1;
229 debugfs_create_u32("in_ifindex", 0600, psample->ddir,
230 &psample->in_ifindex);
232 psample->out_ifindex = 2;
233 debugfs_create_u32("out_ifindex", 0600, psample->ddir,
234 &psample->out_ifindex);
237 debugfs_create_u16("out_tc", 0600, psample->ddir, &psample->out_tc);
239 psample->out_tc_occ_max = 10000;
240 debugfs_create_u64("out_tc_occ_max", 0600, psample->ddir,
241 &psample->out_tc_occ_max);
243 psample->latency_max = 50;
244 debugfs_create_u64("latency_max", 0600, psample->ddir,
245 &psample->latency_max);
247 debugfs_create_file("enable", 0200, psample->ddir, nsim_dev,
248 &nsim_psample_enable_fops);
253 kfree(nsim_dev->psample);
257 void nsim_dev_psample_exit(struct nsim_dev *nsim_dev)
259 debugfs_remove_recursive(nsim_dev->psample->ddir);
260 if (nsim_dev->psample->is_active) {
261 cancel_delayed_work_sync(&nsim_dev->psample->psample_dw);
262 psample_group_put(nsim_dev->psample->group);
264 kfree(nsim_dev->psample);