netdevsim: add all ports in nsim_dev_create() and del them in destroy()
authorJiri Pirko <jiri@mellanox.com>
Thu, 3 Oct 2019 09:49:35 +0000 (11:49 +0200)
committerDavid S. Miller <davem@davemloft.net>
Fri, 4 Oct 2019 18:10:56 +0000 (11:10 -0700)
Currently the probe/remove function does this separately. Put the
addition an deletion of ports into nsim_dev_create() and
nsim_dev_destroy().

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/netdevsim/dev.c

index 6087f5b99e47f1a12042d321c42000db0d0f7874..3cc101aee9917e7f7ec428d73fc4160ab4ea4006 100644 (file)
@@ -603,8 +603,92 @@ static const struct devlink_ops nsim_dev_devlink_ops = {
 #define NSIM_DEV_MAX_MACS_DEFAULT 32
 #define NSIM_DEV_TEST1_DEFAULT true
 
+static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
+                              unsigned int port_index)
+{
+       struct nsim_dev_port *nsim_dev_port;
+       struct devlink_port *devlink_port;
+       int err;
+
+       nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
+       if (!nsim_dev_port)
+               return -ENOMEM;
+       nsim_dev_port->port_index = port_index;
+
+       devlink_port = &nsim_dev_port->devlink_port;
+       devlink_port_attrs_set(devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
+                              port_index + 1, 0, 0,
+                              nsim_dev->switch_id.id,
+                              nsim_dev->switch_id.id_len);
+       err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
+                                   port_index);
+       if (err)
+               goto err_port_free;
+
+       err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
+       if (err)
+               goto err_dl_port_unregister;
+
+       nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
+       if (IS_ERR(nsim_dev_port->ns)) {
+               err = PTR_ERR(nsim_dev_port->ns);
+               goto err_port_debugfs_exit;
+       }
+
+       devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
+       list_add(&nsim_dev_port->list, &nsim_dev->port_list);
+
+       return 0;
+
+err_port_debugfs_exit:
+       nsim_dev_port_debugfs_exit(nsim_dev_port);
+err_dl_port_unregister:
+       devlink_port_unregister(devlink_port);
+err_port_free:
+       kfree(nsim_dev_port);
+       return err;
+}
+
+static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
+{
+       struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
+
+       list_del(&nsim_dev_port->list);
+       devlink_port_type_clear(devlink_port);
+       nsim_destroy(nsim_dev_port->ns);
+       nsim_dev_port_debugfs_exit(nsim_dev_port);
+       devlink_port_unregister(devlink_port);
+       kfree(nsim_dev_port);
+}
+
+static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
+{
+       struct nsim_dev_port *nsim_dev_port, *tmp;
+
+       list_for_each_entry_safe(nsim_dev_port, tmp,
+                                &nsim_dev->port_list, list)
+               __nsim_dev_port_del(nsim_dev_port);
+}
+
+static int nsim_dev_port_add_all(struct nsim_dev *nsim_dev,
+                                unsigned int port_count)
+{
+       int i, err;
+
+       for (i = 0; i < port_count; i++) {
+               err = __nsim_dev_port_add(nsim_dev, i);
+               if (err)
+                       goto err_port_del_all;
+       }
+       return 0;
+
+err_port_del_all:
+       nsim_dev_port_del_all(nsim_dev);
+       return err;
+}
+
 static struct nsim_dev *
-nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
+nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev)
 {
        struct nsim_dev *nsim_dev;
        struct devlink *devlink;
@@ -659,9 +743,15 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
        if (err)
                goto err_debugfs_exit;
 
+       err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
+       if (err)
+               goto err_bpf_dev_exit;
+
        devlink_params_publish(devlink);
        return nsim_dev;
 
+err_bpf_dev_exit:
+       nsim_bpf_dev_exit(nsim_dev);
 err_debugfs_exit:
        nsim_dev_debugfs_exit(nsim_dev);
 err_traps_exit:
@@ -686,6 +776,7 @@ static void nsim_dev_destroy(struct nsim_dev *nsim_dev)
 {
        struct devlink *devlink = priv_to_devlink(nsim_dev);
 
+       nsim_dev_port_del_all(nsim_dev);
        nsim_bpf_dev_exit(nsim_dev);
        nsim_dev_debugfs_exit(nsim_dev);
        nsim_dev_traps_exit(devlink);
@@ -699,102 +790,22 @@ static void nsim_dev_destroy(struct nsim_dev *nsim_dev)
        devlink_free(devlink);
 }
 
-static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
-                              unsigned int port_index)
-{
-       struct nsim_dev_port *nsim_dev_port;
-       struct devlink_port *devlink_port;
-       int err;
-
-       nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
-       if (!nsim_dev_port)
-               return -ENOMEM;
-       nsim_dev_port->port_index = port_index;
-
-       devlink_port = &nsim_dev_port->devlink_port;
-       devlink_port_attrs_set(devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
-                              port_index + 1, 0, 0,
-                              nsim_dev->switch_id.id,
-                              nsim_dev->switch_id.id_len);
-       err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
-                                   port_index);
-       if (err)
-               goto err_port_free;
-
-       err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
-       if (err)
-               goto err_dl_port_unregister;
-
-       nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
-       if (IS_ERR(nsim_dev_port->ns)) {
-               err = PTR_ERR(nsim_dev_port->ns);
-               goto err_port_debugfs_exit;
-       }
-
-       devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
-       list_add(&nsim_dev_port->list, &nsim_dev->port_list);
-
-       return 0;
-
-err_port_debugfs_exit:
-       nsim_dev_port_debugfs_exit(nsim_dev_port);
-err_dl_port_unregister:
-       devlink_port_unregister(devlink_port);
-err_port_free:
-       kfree(nsim_dev_port);
-       return err;
-}
-
-static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
-{
-       struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
-
-       list_del(&nsim_dev_port->list);
-       devlink_port_type_clear(devlink_port);
-       nsim_destroy(nsim_dev_port->ns);
-       nsim_dev_port_debugfs_exit(nsim_dev_port);
-       devlink_port_unregister(devlink_port);
-       kfree(nsim_dev_port);
-}
-
-static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
-{
-       struct nsim_dev_port *nsim_dev_port, *tmp;
-
-       list_for_each_entry_safe(nsim_dev_port, tmp,
-                                &nsim_dev->port_list, list)
-               __nsim_dev_port_del(nsim_dev_port);
-}
-
 int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
 {
        struct nsim_dev *nsim_dev;
-       int i;
-       int err;
 
-       nsim_dev = nsim_dev_create(nsim_bus_dev, nsim_bus_dev->port_count);
+       nsim_dev = nsim_dev_create(nsim_bus_dev);
        if (IS_ERR(nsim_dev))
                return PTR_ERR(nsim_dev);
        dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
 
-       for (i = 0; i < nsim_bus_dev->port_count; i++) {
-               err = __nsim_dev_port_add(nsim_dev, i);
-               if (err)
-                       goto err_port_del_all;
-       }
        return 0;
-
-err_port_del_all:
-       nsim_dev_port_del_all(nsim_dev);
-       nsim_dev_destroy(nsim_dev);
-       return err;
 }
 
 void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
 {
        struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
 
-       nsim_dev_port_del_all(nsim_dev);
        nsim_dev_destroy(nsim_dev);
 }