| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
// Copyright (c) 2020 Facebook Inc. |
| 3 |
|
| 4 |
#include <linux/debugfs.h> |
| 5 |
#include <linux/netdevice.h> |
| 6 |
#include <linux/slab.h> |
| 7 |
#include <net/udp_tunnel.h> |
| 8 |
|
| 9 |
#include "netdevsim.h" |
| 10 |
|
| 11 |
static int |
| 12 |
nsim_udp_tunnel_set_port(struct net_device *dev, unsigned int table, |
| 13 |
unsigned int entry, struct udp_tunnel_info *ti) |
| 14 |
{ |
| 15 |
struct netdevsim *ns = netdev_priv(dev); |
| 16 |
int ret; |
| 17 |
|
| 18 |
ret = -ns->udp_ports.inject_error; |
| 19 |
ns->udp_ports.inject_error = 0; |
| 20 |
|
| 21 |
if (!ret) { |
| 22 |
if (ns->udp_ports.ports[table][entry]) { |
| 23 |
WARN(1, "entry already in use\n"); |
| 24 |
ret = -EBUSY; |
| 25 |
} else { |
| 26 |
ns->udp_ports.ports[table][entry] = |
| 27 |
be16_to_cpu(ti->port) << 16 | ti->type; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
netdev_info(dev, "set [%d, %d] type %d family %d port %d - %d\n", |
| 32 |
table, entry, ti->type, ti->sa_family, ntohs(ti->port), |
| 33 |
ret); |
| 34 |
return ret; |
| 35 |
} |
| 36 |
|
| 37 |
static int |
| 38 |
nsim_udp_tunnel_unset_port(struct net_device *dev, unsigned int table, |
| 39 |
unsigned int entry, struct udp_tunnel_info *ti) |
| 40 |
{ |
| 41 |
struct netdevsim *ns = netdev_priv(dev); |
| 42 |
int ret; |
| 43 |
|
| 44 |
ret = -ns->udp_ports.inject_error; |
| 45 |
ns->udp_ports.inject_error = 0; |
| 46 |
|
| 47 |
if (!ret) { |
| 48 |
u32 val = be16_to_cpu(ti->port) << 16 | ti->type; |
| 49 |
|
| 50 |
if (val == ns->udp_ports.ports[table][entry]) { |
| 51 |
ns->udp_ports.ports[table][entry] = 0; |
| 52 |
} else { |
| 53 |
WARN(1, "entry not installed %x vs %x\n", |
| 54 |
val, ns->udp_ports.ports[table][entry]); |
| 55 |
ret = -ENOENT; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
netdev_info(dev, "unset [%d, %d] type %d family %d port %d - %d\n", |
| 60 |
table, entry, ti->type, ti->sa_family, ntohs(ti->port), |
| 61 |
ret); |
| 62 |
return ret; |
| 63 |
} |
| 64 |
|
| 65 |
static int |
| 66 |
nsim_udp_tunnel_sync_table(struct net_device *dev, unsigned int table) |
| 67 |
{ |
| 68 |
struct netdevsim *ns = netdev_priv(dev); |
| 69 |
struct udp_tunnel_info ti; |
| 70 |
unsigned int i; |
| 71 |
int ret; |
| 72 |
|
| 73 |
ret = -ns->udp_ports.inject_error; |
| 74 |
ns->udp_ports.inject_error = 0; |
| 75 |
|
| 76 |
for (i = 0; i < NSIM_UDP_TUNNEL_N_PORTS; i++) { |
| 77 |
udp_tunnel_nic_get_port(dev, table, i, &ti); |
| 78 |
ns->udp_ports.ports[table][i] = |
| 79 |
be16_to_cpu(ti.port) << 16 | ti.type; |
| 80 |
} |
| 81 |
|
| 82 |
return ret; |
| 83 |
} |
| 84 |
|
| 85 |
static const struct udp_tunnel_nic_info nsim_udp_tunnel_info = { |
| 86 |
.set_port = nsim_udp_tunnel_set_port, |
| 87 |
.unset_port = nsim_udp_tunnel_unset_port, |
| 88 |
.sync_table = nsim_udp_tunnel_sync_table, |
| 89 |
|
| 90 |
.tables = { |
| 91 |
{ |
| 92 |
.n_entries = NSIM_UDP_TUNNEL_N_PORTS, |
| 93 |
.tunnel_types = UDP_TUNNEL_TYPE_VXLAN, |
| 94 |
}, |
| 95 |
{ |
| 96 |
.n_entries = NSIM_UDP_TUNNEL_N_PORTS, |
| 97 |
.tunnel_types = UDP_TUNNEL_TYPE_GENEVE | |
| 98 |
UDP_TUNNEL_TYPE_VXLAN_GPE, |
| 99 |
}, |
| 100 |
}, |
| 101 |
}; |
| 102 |
|
| 103 |
static ssize_t |
| 104 |
nsim_udp_tunnels_info_reset_write(struct file *file, const char __user *data, |
| 105 |
size_t count, loff_t *ppos) |
| 106 |
{ |
| 107 |
struct net_device *dev = file->private_data; |
| 108 |
struct netdevsim *ns = netdev_priv(dev); |
| 109 |
|
| 110 |
if (dev->reg_state == NETREG_REGISTERED) { |
| 111 |
memset(ns->udp_ports.ports, 0, sizeof(ns->udp_ports.__ports)); |
| 112 |
udp_tunnel_nic_reset_ntf(dev); |
| 113 |
} |
| 114 |
|
| 115 |
return count; |
| 116 |
} |
| 117 |
|
| 118 |
static const struct file_operations nsim_udp_tunnels_info_reset_fops = { |
| 119 |
.open = simple_open, |
| 120 |
.write = nsim_udp_tunnels_info_reset_write, |
| 121 |
.llseek = generic_file_llseek, |
| 122 |
.owner = THIS_MODULE, |
| 123 |
}; |
| 124 |
|
| 125 |
int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev, |
| 126 |
struct net_device *dev) |
| 127 |
{ |
| 128 |
struct netdevsim *ns = netdev_priv(dev); |
| 129 |
struct udp_tunnel_nic_info *info; |
| 130 |
|
| 131 |
if (nsim_dev->udp_ports.shared && nsim_dev->udp_ports.open_only) { |
| 132 |
dev_err(&nsim_dev->nsim_bus_dev->dev, |
| 133 |
"shared can't be used in conjunction with open_only\n"); |
| 134 |
return -EINVAL; |
| 135 |
} |
| 136 |
|
| 137 |
if (!nsim_dev->udp_ports.shared) |
| 138 |
ns->udp_ports.ports = ns->udp_ports.__ports; |
| 139 |
else |
| 140 |
ns->udp_ports.ports = nsim_dev->udp_ports.__ports; |
| 141 |
|
| 142 |
ns->udp_ports.ddir = debugfs_create_dir("udp_ports", |
| 143 |
ns->nsim_dev_port->ddir); |
| 144 |
|
| 145 |
debugfs_create_u32("inject_error", 0600, ns->udp_ports.ddir, |
| 146 |
&ns->udp_ports.inject_error); |
| 147 |
|
| 148 |
ns->udp_ports.dfs_ports[0].array = ns->udp_ports.ports[0]; |
| 149 |
ns->udp_ports.dfs_ports[0].n_elements = NSIM_UDP_TUNNEL_N_PORTS; |
| 150 |
debugfs_create_u32_array("table0", 0400, ns->udp_ports.ddir, |
| 151 |
&ns->udp_ports.dfs_ports[0]); |
| 152 |
|
| 153 |
ns->udp_ports.dfs_ports[1].array = ns->udp_ports.ports[1]; |
| 154 |
ns->udp_ports.dfs_ports[1].n_elements = NSIM_UDP_TUNNEL_N_PORTS; |
| 155 |
debugfs_create_u32_array("table1", 0400, ns->udp_ports.ddir, |
| 156 |
&ns->udp_ports.dfs_ports[1]); |
| 157 |
|
| 158 |
debugfs_create_file("reset", 0200, ns->udp_ports.ddir, |
| 159 |
dev, &nsim_udp_tunnels_info_reset_fops); |
| 160 |
|
| 161 |
/* Note: it's not normal to allocate the info struct like this! |
| 162 |
* Drivers are expected to use a static const one, here we're testing. |
| 163 |
*/ |
| 164 |
info = kmemdup(&nsim_udp_tunnel_info, sizeof(nsim_udp_tunnel_info), |
| 165 |
GFP_KERNEL); |
| 166 |
if (!info) |
| 167 |
return -ENOMEM; |
| 168 |
|
| 169 |
if (nsim_dev->udp_ports.sync_all) { |
| 170 |
info->set_port = NULL; |
| 171 |
info->unset_port = NULL; |
| 172 |
} else { |
| 173 |
info->sync_table = NULL; |
| 174 |
} |
| 175 |
|
| 176 |
if (nsim_dev->udp_ports.open_only) |
| 177 |
info->flags |= UDP_TUNNEL_NIC_INFO_OPEN_ONLY; |
| 178 |
if (nsim_dev->udp_ports.ipv4_only) |
| 179 |
info->flags |= UDP_TUNNEL_NIC_INFO_IPV4_ONLY; |
| 180 |
if (nsim_dev->udp_ports.shared) |
| 181 |
info->shared = &nsim_dev->udp_ports.utn_shared; |
| 182 |
if (nsim_dev->udp_ports.static_iana_vxlan) |
| 183 |
info->flags |= UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN; |
| 184 |
|
| 185 |
dev->udp_tunnel_nic_info = info; |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
void nsim_udp_tunnels_info_destroy(struct net_device *dev) |
| 190 |
{ |
| 191 |
struct netdevsim *ns = netdev_priv(dev); |
| 192 |
|
| 193 |
debugfs_remove_recursive(ns->udp_ports.ddir); |
| 194 |
kfree(dev->udp_tunnel_nic_info); |
| 195 |
dev->udp_tunnel_nic_info = NULL; |
| 196 |
} |
| 197 |
|
| 198 |
void nsim_udp_tunnels_debugfs_create(struct nsim_dev *nsim_dev) |
| 199 |
{ |
| 200 |
debugfs_create_bool("udp_ports_sync_all", 0600, nsim_dev->ddir, |
| 201 |
&nsim_dev->udp_ports.sync_all); |
| 202 |
debugfs_create_bool("udp_ports_open_only", 0600, nsim_dev->ddir, |
| 203 |
&nsim_dev->udp_ports.open_only); |
| 204 |
debugfs_create_bool("udp_ports_ipv4_only", 0600, nsim_dev->ddir, |
| 205 |
&nsim_dev->udp_ports.ipv4_only); |
| 206 |
debugfs_create_bool("udp_ports_shared", 0600, nsim_dev->ddir, |
| 207 |
&nsim_dev->udp_ports.shared); |
| 208 |
debugfs_create_bool("udp_ports_static_iana_vxlan", 0600, nsim_dev->ddir, |
| 209 |
&nsim_dev->udp_ports.static_iana_vxlan); |
| 210 |
} |