Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netdevsim: Add dummy psample implementation

Allow netdevsim to report "sampled" packets to the psample module by
periodically generating packets from a work queue. The behavior can be
enabled / disabled (default) and the various meta data attributes can be
controlled via debugfs knobs.

This implementation enables both testing of the psample module with all
the optional attributes as well as development of user space
applications on top of psample such as hsflowd and a Wireshark dissector
for psample generic netlink packets.

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Ido Schimmel and committed by
David S. Miller
a8700c3d 07e1a580

+299 -2
+1
drivers/net/Kconfig
··· 579 579 depends on DEBUG_FS 580 580 depends on INET 581 581 depends on IPV6 || IPV6=n 582 + depends on PSAMPLE || PSAMPLE=n 582 583 select NET_DEVLINK 583 584 help 584 585 This driver is a developer testing tool and software model that can
+4
drivers/net/netdevsim/Makefile
··· 13 13 ifneq ($(CONFIG_XFRM_OFFLOAD),) 14 14 netdevsim-objs += ipsec.o 15 15 endif 16 + 17 + ifneq ($(CONFIG_PSAMPLE),) 18 + netdevsim-objs += psample.o 19 + endif
+15 -2
drivers/net/netdevsim/dev.c
··· 1032 1032 if (err) 1033 1033 goto err_fib_destroy; 1034 1034 1035 - err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count); 1035 + err = nsim_dev_psample_init(nsim_dev); 1036 1036 if (err) 1037 1037 goto err_health_exit; 1038 + 1039 + err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count); 1040 + if (err) 1041 + goto err_psample_exit; 1038 1042 1039 1043 nsim_dev->take_snapshot = debugfs_create_file("take_snapshot", 1040 1044 0200, ··· 1047 1043 &nsim_dev_take_snapshot_fops); 1048 1044 return 0; 1049 1045 1046 + err_psample_exit: 1047 + nsim_dev_psample_exit(nsim_dev); 1050 1048 err_health_exit: 1051 1049 nsim_dev_health_exit(nsim_dev); 1052 1050 err_fib_destroy: ··· 1124 1118 if (err) 1125 1119 goto err_health_exit; 1126 1120 1127 - err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count); 1121 + err = nsim_dev_psample_init(nsim_dev); 1128 1122 if (err) 1129 1123 goto err_bpf_dev_exit; 1124 + 1125 + err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count); 1126 + if (err) 1127 + goto err_psample_exit; 1130 1128 1131 1129 devlink_params_publish(devlink); 1132 1130 devlink_reload_enable(devlink); 1133 1131 return 0; 1134 1132 1133 + err_psample_exit: 1134 + nsim_dev_psample_exit(nsim_dev); 1135 1135 err_bpf_dev_exit: 1136 1136 nsim_bpf_dev_exit(nsim_dev); 1137 1137 err_health_exit: ··· 1170 1158 return; 1171 1159 debugfs_remove(nsim_dev->take_snapshot); 1172 1160 nsim_dev_port_del_all(nsim_dev); 1161 + nsim_dev_psample_exit(nsim_dev); 1173 1162 nsim_dev_health_exit(nsim_dev); 1174 1163 nsim_fib_destroy(devlink, nsim_dev->fib_data); 1175 1164 nsim_dev_traps_exit(devlink);
+15
drivers/net/netdevsim/netdevsim.h
··· 180 180 int nsim_dev_health_init(struct nsim_dev *nsim_dev, struct devlink *devlink); 181 181 void nsim_dev_health_exit(struct nsim_dev *nsim_dev); 182 182 183 + #if IS_ENABLED(CONFIG_PSAMPLE) 184 + int nsim_dev_psample_init(struct nsim_dev *nsim_dev); 185 + void nsim_dev_psample_exit(struct nsim_dev *nsim_dev); 186 + #else 187 + static inline int nsim_dev_psample_init(struct nsim_dev *nsim_dev) 188 + { 189 + return 0; 190 + } 191 + 192 + static inline void nsim_dev_psample_exit(struct nsim_dev *nsim_dev) 193 + { 194 + } 195 + #endif 196 + 183 197 struct nsim_dev_port { 184 198 struct list_head list; 185 199 struct devlink_port devlink_port; ··· 243 229 bool static_iana_vxlan; 244 230 u32 sleep; 245 231 } udp_ports; 232 + struct nsim_dev_psample *psample; 246 233 }; 247 234 248 235 static inline struct net *nsim_dev_net(struct nsim_dev *nsim_dev)
+264
drivers/net/netdevsim/psample.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2021 Mellanox Technologies. All rights reserved */ 3 + 4 + #include <linux/debugfs.h> 5 + #include <linux/err.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> 12 + #include <net/ip.h> 13 + #include <net/psample.h> 14 + #include <uapi/linux/ip.h> 15 + #include <uapi/linux/udp.h> 16 + 17 + #include "netdevsim.h" 18 + 19 + #define NSIM_PSAMPLE_REPORT_INTERVAL_MS 100 20 + #define NSIM_PSAMPLE_INVALID_TC 0xFFFF 21 + #define NSIM_PSAMPLE_L4_DATA_LEN 100 22 + 23 + struct nsim_dev_psample { 24 + struct delayed_work psample_dw; 25 + struct dentry *ddir; 26 + struct psample_group *group; 27 + u32 rate; 28 + u32 group_num; 29 + u32 trunc_size; 30 + int in_ifindex; 31 + int out_ifindex; 32 + u16 out_tc; 33 + u64 out_tc_occ_max; 34 + u64 latency_max; 35 + bool is_active; 36 + }; 37 + 38 + static struct sk_buff *nsim_dev_psample_skb_build(void) 39 + { 40 + int tot_len, data_len = NSIM_PSAMPLE_L4_DATA_LEN; 41 + struct sk_buff *skb; 42 + struct udphdr *udph; 43 + struct ethhdr *eth; 44 + struct iphdr *iph; 45 + 46 + skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 47 + if (!skb) 48 + return NULL; 49 + tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len; 50 + 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); 57 + 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"); 63 + iph->version = 0x4; 64 + iph->frag_off = 0; 65 + iph->ihl = 0x5; 66 + iph->tot_len = htons(tot_len); 67 + iph->id = 0; 68 + iph->ttl = 100; 69 + iph->check = 0; 70 + iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 71 + 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); 77 + 78 + return skb; 79 + } 80 + 81 + static void nsim_dev_psample_md_prepare(const struct nsim_dev_psample *psample, 82 + struct psample_metadata *md) 83 + { 84 + md->trunc_size = psample->trunc_size; 85 + md->in_ifindex = psample->in_ifindex; 86 + md->out_ifindex = psample->out_ifindex; 87 + 88 + if (psample->out_tc != NSIM_PSAMPLE_INVALID_TC) { 89 + md->out_tc = psample->out_tc; 90 + md->out_tc_valid = 1; 91 + } 92 + 93 + if (psample->out_tc_occ_max) { 94 + u64 out_tc_occ; 95 + 96 + get_random_bytes(&out_tc_occ, sizeof(u64)); 97 + md->out_tc_occ = out_tc_occ & (psample->out_tc_occ_max - 1); 98 + md->out_tc_occ_valid = 1; 99 + } 100 + 101 + if (psample->latency_max) { 102 + u64 latency; 103 + 104 + get_random_bytes(&latency, sizeof(u64)); 105 + md->latency = latency & (psample->latency_max - 1); 106 + md->latency_valid = 1; 107 + } 108 + } 109 + 110 + static void nsim_dev_psample_report_work(struct work_struct *work) 111 + { 112 + struct nsim_dev_psample *psample; 113 + struct psample_metadata md = {}; 114 + struct sk_buff *skb; 115 + unsigned long delay; 116 + 117 + psample = container_of(work, struct nsim_dev_psample, psample_dw.work); 118 + 119 + skb = nsim_dev_psample_skb_build(); 120 + if (!skb) 121 + goto out; 122 + 123 + nsim_dev_psample_md_prepare(psample, &md); 124 + psample_sample_packet(psample->group, skb, psample->rate, &md); 125 + consume_skb(skb); 126 + 127 + out: 128 + delay = msecs_to_jiffies(NSIM_PSAMPLE_REPORT_INTERVAL_MS); 129 + schedule_delayed_work(&psample->psample_dw, delay); 130 + } 131 + 132 + static int nsim_dev_psample_enable(struct nsim_dev *nsim_dev) 133 + { 134 + struct nsim_dev_psample *psample = nsim_dev->psample; 135 + struct devlink *devlink; 136 + unsigned long delay; 137 + 138 + if (psample->is_active) 139 + return -EBUSY; 140 + 141 + devlink = priv_to_devlink(nsim_dev); 142 + psample->group = psample_group_get(devlink_net(devlink), 143 + psample->group_num); 144 + if (!psample->group) 145 + return -EINVAL; 146 + 147 + delay = msecs_to_jiffies(NSIM_PSAMPLE_REPORT_INTERVAL_MS); 148 + schedule_delayed_work(&psample->psample_dw, delay); 149 + 150 + psample->is_active = true; 151 + 152 + return 0; 153 + } 154 + 155 + static int nsim_dev_psample_disable(struct nsim_dev *nsim_dev) 156 + { 157 + struct nsim_dev_psample *psample = nsim_dev->psample; 158 + 159 + if (!psample->is_active) 160 + return -EINVAL; 161 + 162 + psample->is_active = false; 163 + 164 + cancel_delayed_work_sync(&psample->psample_dw); 165 + psample_group_put(psample->group); 166 + 167 + return 0; 168 + } 169 + 170 + static ssize_t nsim_dev_psample_enable_write(struct file *file, 171 + const char __user *data, 172 + size_t count, loff_t *ppos) 173 + { 174 + struct nsim_dev *nsim_dev = file->private_data; 175 + bool enable; 176 + int err; 177 + 178 + err = kstrtobool_from_user(data, count, &enable); 179 + if (err) 180 + return err; 181 + 182 + if (enable) 183 + err = nsim_dev_psample_enable(nsim_dev); 184 + else 185 + err = nsim_dev_psample_disable(nsim_dev); 186 + 187 + return err ? err : count; 188 + } 189 + 190 + static const struct file_operations nsim_psample_enable_fops = { 191 + .open = simple_open, 192 + .write = nsim_dev_psample_enable_write, 193 + .llseek = generic_file_llseek, 194 + .owner = THIS_MODULE, 195 + }; 196 + 197 + int nsim_dev_psample_init(struct nsim_dev *nsim_dev) 198 + { 199 + struct nsim_dev_psample *psample; 200 + int err; 201 + 202 + psample = kzalloc(sizeof(*psample), GFP_KERNEL); 203 + if (!psample) 204 + return -ENOMEM; 205 + nsim_dev->psample = psample; 206 + 207 + INIT_DELAYED_WORK(&psample->psample_dw, nsim_dev_psample_report_work); 208 + 209 + psample->ddir = debugfs_create_dir("psample", nsim_dev->ddir); 210 + if (IS_ERR(psample->ddir)) { 211 + err = PTR_ERR(psample->ddir); 212 + goto err_psample_free; 213 + } 214 + 215 + /* Populate sampling parameters with sane defaults. */ 216 + psample->rate = 100; 217 + debugfs_create_u32("rate", 0600, psample->ddir, &psample->rate); 218 + 219 + psample->group_num = 10; 220 + debugfs_create_u32("group_num", 0600, psample->ddir, 221 + &psample->group_num); 222 + 223 + psample->trunc_size = 0; 224 + debugfs_create_u32("trunc_size", 0600, psample->ddir, 225 + &psample->trunc_size); 226 + 227 + psample->in_ifindex = 1; 228 + debugfs_create_u32("in_ifindex", 0600, psample->ddir, 229 + &psample->in_ifindex); 230 + 231 + psample->out_ifindex = 2; 232 + debugfs_create_u32("out_ifindex", 0600, psample->ddir, 233 + &psample->out_ifindex); 234 + 235 + psample->out_tc = 0; 236 + debugfs_create_u16("out_tc", 0600, psample->ddir, &psample->out_tc); 237 + 238 + psample->out_tc_occ_max = 10000; 239 + debugfs_create_u64("out_tc_occ_max", 0600, psample->ddir, 240 + &psample->out_tc_occ_max); 241 + 242 + psample->latency_max = 50; 243 + debugfs_create_u64("latency_max", 0600, psample->ddir, 244 + &psample->latency_max); 245 + 246 + debugfs_create_file("enable", 0200, psample->ddir, nsim_dev, 247 + &nsim_psample_enable_fops); 248 + 249 + return 0; 250 + 251 + err_psample_free: 252 + kfree(nsim_dev->psample); 253 + return err; 254 + } 255 + 256 + void nsim_dev_psample_exit(struct nsim_dev *nsim_dev) 257 + { 258 + debugfs_remove_recursive(nsim_dev->psample->ddir); 259 + if (nsim_dev->psample->is_active) { 260 + cancel_delayed_work_sync(&nsim_dev->psample->psample_dw); 261 + psample_group_put(nsim_dev->psample->group); 262 + } 263 + kfree(nsim_dev->psample); 264 + }