Commit 794b2c05 authored by Jiri Pirko's avatar Jiri Pirko Committed by David S. Miller

netdevsim: extend device attrs to support port addition and deletion

In order to test flows in core, it is beneficial to maintain previously
supported possibility to add and delete ports during netdevsim lifetime.
Do it by extending device sysfs attrs by "new_port" and "del_port".
Signed-off-by: default avatarJiri Pirko <jiri@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 8320d145
...@@ -91,8 +91,44 @@ static struct device_attribute nsim_bus_dev_numvfs_attr = ...@@ -91,8 +91,44 @@ static struct device_attribute nsim_bus_dev_numvfs_attr =
__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show, __ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
nsim_bus_dev_numvfs_store); nsim_bus_dev_numvfs_store);
static ssize_t
new_port_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
unsigned int port_index;
int ret;
ret = kstrtouint(buf, 0, &port_index);
if (ret)
return ret;
ret = nsim_dev_port_add(nsim_bus_dev, port_index);
return ret ? ret : count;
}
static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
static ssize_t
del_port_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
unsigned int port_index;
int ret;
ret = kstrtouint(buf, 0, &port_index);
if (ret)
return ret;
ret = nsim_dev_port_del(nsim_bus_dev, port_index);
return ret ? ret : count;
}
static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
static struct attribute *nsim_bus_dev_attrs[] = { static struct attribute *nsim_bus_dev_attrs[] = {
&nsim_bus_dev_numvfs_attr.attr, &nsim_bus_dev_numvfs_attr.attr,
&nsim_bus_dev_new_port_attr.attr,
&nsim_bus_dev_del_port_attr.attr,
NULL, NULL,
}; };
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/mutex.h>
#include <linux/random.h> #include <linux/random.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <net/devlink.h> #include <net/devlink.h>
...@@ -238,6 +239,7 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count) ...@@ -238,6 +239,7 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id); nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len); get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
INIT_LIST_HEAD(&nsim_dev->port_list); INIT_LIST_HEAD(&nsim_dev->port_list);
mutex_init(&nsim_dev->port_list_lock);
nsim_dev->fib_data = nsim_fib_create(); nsim_dev->fib_data = nsim_fib_create();
if (IS_ERR(nsim_dev->fib_data)) { if (IS_ERR(nsim_dev->fib_data)) {
...@@ -285,10 +287,12 @@ void nsim_dev_destroy(struct nsim_dev *nsim_dev) ...@@ -285,10 +287,12 @@ void nsim_dev_destroy(struct nsim_dev *nsim_dev)
devlink_unregister(devlink); devlink_unregister(devlink);
devlink_resources_unregister(devlink, NULL); devlink_resources_unregister(devlink, NULL);
nsim_fib_destroy(nsim_dev->fib_data); nsim_fib_destroy(nsim_dev->fib_data);
mutex_destroy(&nsim_dev->port_list_lock);
devlink_free(devlink); devlink_free(devlink);
} }
static int nsim_dev_port_add(struct nsim_dev *nsim_dev, unsigned int port_index) static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
unsigned int port_index)
{ {
struct nsim_dev_port *nsim_dev_port; struct nsim_dev_port *nsim_dev_port;
struct devlink_port *devlink_port; struct devlink_port *devlink_port;
...@@ -324,7 +328,7 @@ static int nsim_dev_port_add(struct nsim_dev *nsim_dev, unsigned int port_index) ...@@ -324,7 +328,7 @@ static int nsim_dev_port_add(struct nsim_dev *nsim_dev, unsigned int port_index)
return err; return err;
} }
static void nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port) static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
{ {
struct devlink_port *devlink_port = &nsim_dev_port->devlink_port; struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
...@@ -340,7 +344,7 @@ static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev) ...@@ -340,7 +344,7 @@ static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
list_for_each_entry_safe(nsim_dev_port, tmp, list_for_each_entry_safe(nsim_dev_port, tmp,
&nsim_dev->port_list, list) &nsim_dev->port_list, list)
nsim_dev_port_del(nsim_dev_port); __nsim_dev_port_del(nsim_dev_port);
} }
int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev) int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
...@@ -355,7 +359,7 @@ int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev) ...@@ -355,7 +359,7 @@ int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev); dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
for (i = 0; i < nsim_bus_dev->port_count; i++) { for (i = 0; i < nsim_bus_dev->port_count; i++) {
err = nsim_dev_port_add(nsim_dev, i); err = __nsim_dev_port_add(nsim_dev, i);
if (err) if (err)
goto err_port_del_all; goto err_port_del_all;
} }
...@@ -375,6 +379,49 @@ void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev) ...@@ -375,6 +379,49 @@ void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
nsim_dev_destroy(nsim_dev); nsim_dev_destroy(nsim_dev);
} }
static struct nsim_dev_port *
__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
{
struct nsim_dev_port *nsim_dev_port;
list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
if (nsim_dev_port->port_index == port_index)
return nsim_dev_port;
return NULL;
}
int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
unsigned int port_index)
{
struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
int err;
mutex_lock(&nsim_dev->port_list_lock);
if (__nsim_dev_port_lookup(nsim_dev, port_index))
err = -EEXIST;
else
err = __nsim_dev_port_add(nsim_dev, port_index);
mutex_unlock(&nsim_dev->port_list_lock);
return err;
}
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
unsigned int port_index)
{
struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
struct nsim_dev_port *nsim_dev_port;
int err = 0;
mutex_lock(&nsim_dev->port_list_lock);
nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
if (!nsim_dev_port)
err = -ENOENT;
else
__nsim_dev_port_del(nsim_dev_port);
mutex_unlock(&nsim_dev->port_list_lock);
return err;
}
int nsim_dev_init(void) int nsim_dev_init(void)
{ {
nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL); nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
......
...@@ -152,12 +152,17 @@ struct nsim_dev { ...@@ -152,12 +152,17 @@ struct nsim_dev {
struct list_head bpf_bound_maps; struct list_head bpf_bound_maps;
struct netdev_phys_item_id switch_id; struct netdev_phys_item_id switch_id;
struct list_head port_list; struct list_head port_list;
struct mutex port_list_lock; /* protects port list */
}; };
int nsim_dev_init(void); int nsim_dev_init(void);
void nsim_dev_exit(void); void nsim_dev_exit(void);
int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev); int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev); void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
unsigned int port_index);
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
unsigned int port_index);
struct nsim_fib_data *nsim_fib_create(void); struct nsim_fib_data *nsim_fib_create(void);
void nsim_fib_destroy(struct nsim_fib_data *fib_data); void nsim_fib_destroy(struct nsim_fib_data *fib_data);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment