Merge branch 'create-netdevsim-instances-in-namespace'

Jiri Pirko says:

====================
create netdevsim instances in namespace

Allow user to create netdevsim devlink and netdevice instances in a
network namespace according to the namespace where the user resides in.
Add a selftest to test this.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
David S. Miller 2019-10-05 16:34:15 -07:00
commit fbe3d0c77c
7 changed files with 97 additions and 4 deletions
drivers/net/netdevsim
include/net
net/core
tools/testing/selftests
drivers/net/netdevsim
net/forwarding

View File

@ -283,6 +283,7 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count)
nsim_bus_dev->dev.bus = &nsim_bus;
nsim_bus_dev->dev.type = &nsim_bus_dev_type;
nsim_bus_dev->port_count = port_count;
nsim_bus_dev->initial_net = current->nsproxy->net_ns;
err = device_register(&nsim_bus_dev->dev);
if (err)

View File

@ -726,6 +726,7 @@ static struct nsim_dev *nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev)
devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
if (!devlink)
return ERR_PTR(-ENOMEM);
devlink_net_set(devlink, nsim_bus_dev->initial_net);
nsim_dev = devlink_priv(devlink);
nsim_dev->nsim_bus_dev = nsim_bus_dev;
nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);

View File

@ -220,6 +220,9 @@ struct nsim_bus_dev {
struct device dev;
struct list_head list;
unsigned int port_count;
struct net *initial_net; /* Purpose of this is to carry net pointer
* during the probe time only.
*/
unsigned int num_vfs;
struct nsim_vf_config *vfconfigs;
};

View File

@ -39,6 +39,7 @@ struct devlink {
possible_net_t _net;
struct mutex lock;
bool reload_failed;
bool registered;
char priv[0] __aligned(NETDEV_ALIGN);
};
@ -772,6 +773,7 @@ static inline struct devlink *netdev_to_devlink(struct net_device *dev)
struct ib_device;
struct net *devlink_net(const struct devlink *devlink);
void devlink_net_set(struct devlink *devlink, struct net *net);
struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
int devlink_register(struct devlink *devlink, struct device *dev);
void devlink_unregister(struct devlink *devlink);

View File

@ -101,11 +101,19 @@ struct net *devlink_net(const struct devlink *devlink)
}
EXPORT_SYMBOL_GPL(devlink_net);
static void devlink_net_set(struct devlink *devlink, struct net *net)
static void __devlink_net_set(struct devlink *devlink, struct net *net)
{
write_pnet(&devlink->_net, net);
}
void devlink_net_set(struct devlink *devlink, struct net *net)
{
if (WARN_ON(devlink->registered))
return;
__devlink_net_set(devlink, net);
}
EXPORT_SYMBOL_GPL(devlink_net_set);
static struct devlink *devlink_get_from_attrs(struct net *net,
struct nlattr **attrs)
{
@ -2750,7 +2758,7 @@ static void devlink_reload_netns_change(struct devlink *devlink,
DEVLINK_CMD_PARAM_DEL);
devlink_notify(devlink, DEVLINK_CMD_DEL);
devlink_net_set(devlink, dest_net);
__devlink_net_set(devlink, dest_net);
devlink_notify(devlink, DEVLINK_CMD_NEW);
list_for_each_entry(param_item, &devlink->param_list, list)
@ -6278,7 +6286,7 @@ struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
if (!devlink)
return NULL;
devlink->ops = ops;
devlink_net_set(devlink, &init_net);
__devlink_net_set(devlink, &init_net);
INIT_LIST_HEAD(&devlink->port_list);
INIT_LIST_HEAD(&devlink->sb_list);
INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
@ -6304,6 +6312,7 @@ int devlink_register(struct devlink *devlink, struct device *dev)
{
mutex_lock(&devlink_mutex);
devlink->dev = dev;
devlink->registered = true;
list_add_tail(&devlink->list, &devlink_list);
devlink_notify(devlink, DEVLINK_CMD_NEW);
mutex_unlock(&devlink_mutex);

View File

@ -0,0 +1,72 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
lib_dir=$(dirname $0)/../../../net/forwarding
ALL_TESTS="check_devlink_test check_ports_test"
NUM_NETIFS=0
source $lib_dir/lib.sh
BUS_ADDR=10
PORT_COUNT=4
DEV_NAME=netdevsim$BUS_ADDR
SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV_NAME/net/
DL_HANDLE=netdevsim/$DEV_NAME
NETNS_NAME=testns1
port_netdev_get()
{
local port_index=$1
cmd_jq "devlink -N $NETNS_NAME port show -j" \
".[][\"$DL_HANDLE/$port_index\"].netdev" "-e"
}
check_ports_test()
{
RET=0
for i in $(seq 0 $(expr $PORT_COUNT - 1)); do
netdev_name=$(port_netdev_get $i)
check_err $? "Failed to get netdev name for port $DL_HANDLE/$i"
ip -n $NETNS_NAME link show $netdev_name &> /dev/null
check_err $? "Failed to find netdev $netdev_name"
done
log_test "check ports test"
}
check_devlink_test()
{
RET=0
devlink -N $NETNS_NAME dev show $DL_HANDLE &> /dev/null
check_err $? "Failed to show devlink instance"
log_test "check devlink test"
}
setup_prepare()
{
modprobe netdevsim
ip netns add $NETNS_NAME
ip netns exec $NETNS_NAME \
echo "$BUS_ADDR $PORT_COUNT" > /sys/bus/netdevsim/new_device
while [ ! -d $SYSFS_NET_DIR ] ; do :; done
}
cleanup()
{
pre_cleanup
echo "$BUS_ADDR" > /sys/bus/netdevsim/del_device
ip netns del $NETNS_NAME
modprobe -r netdevsim
}
trap cleanup EXIT
setup_prepare
tests_run
exit $EXIT_STATUS

View File

@ -254,6 +254,7 @@ cmd_jq()
{
local cmd=$1
local jq_exp=$2
local jq_opts=$3
local ret
local output
@ -263,7 +264,11 @@ cmd_jq()
if [[ $ret -ne 0 ]]; then
return $ret
fi
output=$(echo $output | jq -r "$jq_exp")
output=$(echo $output | jq -r $jq_opts "$jq_exp")
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
echo $output
# return success only in case of non-empty output
[ ! -z "$output" ]