Skip to content

Commit 794b2c0

Browse files
jpirkodavem330
authored andcommitted
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: Jiri Pirko <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8320d14 commit 794b2c0

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

drivers/net/netdevsim/bus.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,44 @@ static struct device_attribute nsim_bus_dev_numvfs_attr =
9191
__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
9292
nsim_bus_dev_numvfs_store);
9393

94+
static ssize_t
95+
new_port_store(struct device *dev, struct device_attribute *attr,
96+
const char *buf, size_t count)
97+
{
98+
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
99+
unsigned int port_index;
100+
int ret;
101+
102+
ret = kstrtouint(buf, 0, &port_index);
103+
if (ret)
104+
return ret;
105+
ret = nsim_dev_port_add(nsim_bus_dev, port_index);
106+
return ret ? ret : count;
107+
}
108+
109+
static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
110+
111+
static ssize_t
112+
del_port_store(struct device *dev, struct device_attribute *attr,
113+
const char *buf, size_t count)
114+
{
115+
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
116+
unsigned int port_index;
117+
int ret;
118+
119+
ret = kstrtouint(buf, 0, &port_index);
120+
if (ret)
121+
return ret;
122+
ret = nsim_dev_port_del(nsim_bus_dev, port_index);
123+
return ret ? ret : count;
124+
}
125+
126+
static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
127+
94128
static struct attribute *nsim_bus_dev_attrs[] = {
95129
&nsim_bus_dev_numvfs_attr.attr,
130+
&nsim_bus_dev_new_port_attr.attr,
131+
&nsim_bus_dev_del_port_attr.attr,
96132
NULL,
97133
};
98134

drivers/net/netdevsim/dev.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/debugfs.h>
1919
#include <linux/device.h>
2020
#include <linux/list.h>
21+
#include <linux/mutex.h>
2122
#include <linux/random.h>
2223
#include <linux/rtnetlink.h>
2324
#include <net/devlink.h>
@@ -238,6 +239,7 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
238239
nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
239240
get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
240241
INIT_LIST_HEAD(&nsim_dev->port_list);
242+
mutex_init(&nsim_dev->port_list_lock);
241243

242244
nsim_dev->fib_data = nsim_fib_create();
243245
if (IS_ERR(nsim_dev->fib_data)) {
@@ -285,10 +287,12 @@ void nsim_dev_destroy(struct nsim_dev *nsim_dev)
285287
devlink_unregister(devlink);
286288
devlink_resources_unregister(devlink, NULL);
287289
nsim_fib_destroy(nsim_dev->fib_data);
290+
mutex_destroy(&nsim_dev->port_list_lock);
288291
devlink_free(devlink);
289292
}
290293

291-
static int nsim_dev_port_add(struct nsim_dev *nsim_dev, unsigned int port_index)
294+
static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
295+
unsigned int port_index)
292296
{
293297
struct nsim_dev_port *nsim_dev_port;
294298
struct devlink_port *devlink_port;
@@ -324,7 +328,7 @@ static int nsim_dev_port_add(struct nsim_dev *nsim_dev, unsigned int port_index)
324328
return err;
325329
}
326330

327-
static void nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
331+
static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
328332
{
329333
struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
330334

@@ -340,7 +344,7 @@ static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
340344

341345
list_for_each_entry_safe(nsim_dev_port, tmp,
342346
&nsim_dev->port_list, list)
343-
nsim_dev_port_del(nsim_dev_port);
347+
__nsim_dev_port_del(nsim_dev_port);
344348
}
345349

346350
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)
355359
dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
356360

357361
for (i = 0; i < nsim_bus_dev->port_count; i++) {
358-
err = nsim_dev_port_add(nsim_dev, i);
362+
err = __nsim_dev_port_add(nsim_dev, i);
359363
if (err)
360364
goto err_port_del_all;
361365
}
@@ -375,6 +379,49 @@ void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
375379
nsim_dev_destroy(nsim_dev);
376380
}
377381

382+
static struct nsim_dev_port *
383+
__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
384+
{
385+
struct nsim_dev_port *nsim_dev_port;
386+
387+
list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
388+
if (nsim_dev_port->port_index == port_index)
389+
return nsim_dev_port;
390+
return NULL;
391+
}
392+
393+
int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
394+
unsigned int port_index)
395+
{
396+
struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
397+
int err;
398+
399+
mutex_lock(&nsim_dev->port_list_lock);
400+
if (__nsim_dev_port_lookup(nsim_dev, port_index))
401+
err = -EEXIST;
402+
else
403+
err = __nsim_dev_port_add(nsim_dev, port_index);
404+
mutex_unlock(&nsim_dev->port_list_lock);
405+
return err;
406+
}
407+
408+
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
409+
unsigned int port_index)
410+
{
411+
struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
412+
struct nsim_dev_port *nsim_dev_port;
413+
int err = 0;
414+
415+
mutex_lock(&nsim_dev->port_list_lock);
416+
nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
417+
if (!nsim_dev_port)
418+
err = -ENOENT;
419+
else
420+
__nsim_dev_port_del(nsim_dev_port);
421+
mutex_unlock(&nsim_dev->port_list_lock);
422+
return err;
423+
}
424+
378425
int nsim_dev_init(void)
379426
{
380427
nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);

drivers/net/netdevsim/netdevsim.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,17 @@ struct nsim_dev {
152152
struct list_head bpf_bound_maps;
153153
struct netdev_phys_item_id switch_id;
154154
struct list_head port_list;
155+
struct mutex port_list_lock; /* protects port list */
155156
};
156157

157158
int nsim_dev_init(void);
158159
void nsim_dev_exit(void);
159160
int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev);
160161
void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev);
162+
int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
163+
unsigned int port_index);
164+
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
165+
unsigned int port_index);
161166

162167
struct nsim_fib_data *nsim_fib_create(void);
163168
void nsim_fib_destroy(struct nsim_fib_data *fib_data);

0 commit comments

Comments
 (0)