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