replace snprintf with strlcpy without adding extra include
[dpdk.git] / drivers / bus / vdev / vdev.c
index 3f27f35..04f76a6 100644 (file)
@@ -41,7 +41,7 @@ static struct vdev_device_list vdev_device_list =
 static rte_spinlock_recursive_t vdev_device_list_lock =
        RTE_SPINLOCK_RECURSIVE_INITIALIZER;
 
-struct vdev_driver_list vdev_driver_list =
+static struct vdev_driver_list vdev_driver_list =
        TAILQ_HEAD_INITIALIZER(vdev_driver_list);
 
 struct vdev_custom_scan {
@@ -143,10 +143,11 @@ vdev_probe_all_drivers(struct rte_vdev_device *dev)
        struct rte_vdev_driver *driver;
        int ret;
 
-       name = rte_vdev_device_name(dev);
+       if (rte_dev_is_probed(&dev->device))
+               return -EEXIST;
 
-       VDEV_LOG(DEBUG, "Search driver %s to probe device %s", name,
-               rte_vdev_device_name(dev));
+       name = rte_vdev_device_name(dev);
+       VDEV_LOG(DEBUG, "Search driver to probe device %s", name);
 
        if (vdev_parse(name, &driver))
                return -1;
@@ -191,7 +192,7 @@ alloc_devargs(const char *name, const char *args)
        else
                devargs->args = strdup("");
 
-       ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
+       ret = strlcpy(devargs->name, name, sizeof(devargs->name));
        if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
                free(devargs->args);
                free(devargs);
@@ -202,7 +203,9 @@ alloc_devargs(const char *name, const char *args)
 }
 
 static int
-insert_vdev(const char *name, const char *args, struct rte_vdev_device **p_dev)
+insert_vdev(const char *name, const char *args,
+               struct rte_vdev_device **p_dev,
+               bool init)
 {
        struct rte_vdev_device *dev;
        struct rte_devargs *devargs;
@@ -221,17 +224,24 @@ insert_vdev(const char *name, const char *args, struct rte_vdev_device **p_dev)
                goto fail;
        }
 
-       dev->device.devargs = devargs;
+       dev->device.bus = &rte_vdev_bus;
        dev->device.numa_node = SOCKET_ID_ANY;
        dev->device.name = devargs->name;
 
        if (find_vdev(name)) {
+               /*
+                * A vdev is expected to have only one port.
+                * So there is no reason to try probing again,
+                * even with new arguments.
+                */
                ret = -EEXIST;
                goto fail;
        }
 
+       if (init)
+               rte_devargs_insert(&devargs);
+       dev->device.devargs = devargs;
        TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
-       rte_devargs_insert(devargs);
 
        if (p_dev)
                *p_dev = dev;
@@ -251,7 +261,7 @@ rte_vdev_init(const char *name, const char *args)
        int ret;
 
        rte_spinlock_recursive_lock(&vdev_device_list_lock);
-       ret = insert_vdev(name, args, &dev);
+       ret = insert_vdev(name, args, &dev, true);
        if (ret == 0) {
                ret = vdev_probe_all_drivers(dev);
                if (ret) {
@@ -377,7 +387,7 @@ vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
                break;
        case VDEV_SCAN_ONE:
                VDEV_LOG(INFO, "receive vdev, %s", in->name);
-               ret = insert_vdev(in->name, NULL, NULL);
+               ret = insert_vdev(in->name, NULL, NULL, false);
                if (ret == -EEXIST)
                        VDEV_LOG(DEBUG, "device already exist, %s", in->name);
                else if (ret < 0)
@@ -419,6 +429,7 @@ vdev_scan(void)
                        mp_rep = &mp_reply.msgs[0];
                        resp = (struct vdev_param *)mp_rep->param;
                        VDEV_LOG(INFO, "Received %d vdevs", resp->num);
+                       free(mp_reply.msgs);
                } else
                        VDEV_LOG(ERR, "Failed to request vdev from primary");
 
@@ -472,7 +483,7 @@ static int
 vdev_probe(void)
 {
        struct rte_vdev_device *dev;
-       int ret = 0;
+       int r, ret = 0;
 
        /* call the init function for each virtual device */
        TAILQ_FOREACH(dev, &vdev_device_list, next) {
@@ -481,10 +492,10 @@ vdev_probe(void)
                 * we call each driver probe.
                 */
 
-               if (dev->device.driver)
-                       continue;
-
-               if (vdev_probe_all_drivers(dev)) {
+               r = vdev_probe_all_drivers(dev);
+               if (r != 0) {
+                       if (r == -EEXIST)
+                               continue;
                        VDEV_LOG(ERR, "failed to initialize %s device",
                                rte_vdev_device_name(dev));
                        ret = -1;