net/virtio-user: do not stop stopped device again
[dpdk.git] / drivers / net / virtio / virtio_user / virtio_user_dev.c
index 6522bec..0e35630 100644 (file)
@@ -13,6 +13,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <rte_eal_memconfig.h>
+
 #include "vhost.h"
 #include "virtio_user_dev.h"
 #include "../virtio_ethdev.h"
@@ -109,9 +111,24 @@ is_vhost_user_by_type(const char *path)
 int
 virtio_user_start_device(struct virtio_user_dev *dev)
 {
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        uint64_t features;
        int ret;
 
+       /*
+        * XXX workaround!
+        *
+        * We need to make sure that the locks will be
+        * taken in the correct order to avoid deadlocks.
+        *
+        * Before releasing this lock, this thread should
+        * not trigger any memory hotplug events.
+        *
+        * This is a temporary workaround, and should be
+        * replaced when we get proper supports from the
+        * memory subsystem in the future.
+        */
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
        pthread_mutex_lock(&dev->mutex);
 
        if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
@@ -152,10 +169,12 @@ virtio_user_start_device(struct virtio_user_dev *dev)
 
        dev->started = true;
        pthread_mutex_unlock(&dev->mutex);
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
 
        return 0;
 error:
        pthread_mutex_unlock(&dev->mutex);
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
        /* TODO: free resource here or caller to check */
        return -1;
 }
@@ -165,6 +184,9 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)
        uint32_t i;
 
        pthread_mutex_lock(&dev->mutex);
+       if (!dev->started)
+               goto out;
+
        for (i = 0; i < dev->max_queue_pairs; ++i)
                dev->ops->enable_qp(dev, i, 0);
 
@@ -174,6 +196,7 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)
                return -1;
        }
        dev->started = false;
+out:
        pthread_mutex_unlock(&dev->mutex);
 
        return 0;
@@ -282,8 +305,14 @@ virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
                                                 void *arg)
 {
        struct virtio_user_dev *dev = arg;
+       struct rte_memseg_list *msl;
        uint16_t i;
 
+       /* ignore externally allocated memory */
+       msl = rte_mem_virt2memseg_list(addr);
+       if (msl->external)
+               return;
+
        pthread_mutex_lock(&dev->mutex);
 
        if (dev->started == false)
@@ -319,12 +348,12 @@ virtio_user_dev_setup(struct virtio_user_dev *dev)
                        PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
                        return -1;
                }
-               dev->ops = &ops_user;
+               dev->ops = &virtio_ops_user;
        } else {
                if (is_vhost_user_by_type(dev->path)) {
-                       dev->ops = &ops_user;
+                       dev->ops = &virtio_ops_user;
                } else {
-                       dev->ops = &ops_kernel;
+                       dev->ops = &virtio_ops_kernel;
 
                        dev->vhostfds = malloc(dev->max_queue_pairs *
                                               sizeof(int));
@@ -371,11 +400,13 @@ virtio_user_dev_setup(struct virtio_user_dev *dev)
         1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
         1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
         1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
+        1ULL << VIRTIO_F_IN_ORDER              |       \
         1ULL << VIRTIO_F_VERSION_1)
 
 int
 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
-                    int cq, int queue_size, const char *mac, char **ifname)
+                    int cq, int queue_size, const char *mac, char **ifname,
+                    int mrg_rxbuf, int in_order)
 {
        pthread_mutex_init(&dev->mutex, NULL);
        snprintf(dev->path, PATH_MAX, "%s", path);
@@ -384,6 +415,7 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
        dev->queue_pairs = 1; /* mq disabled by default */
        dev->queue_size = queue_size;
        dev->mac_specified = 0;
+       dev->unsupported_features = 0;
        parse_mac(dev, mac);
 
        if (*ifname) {
@@ -396,7 +428,7 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
                return -1;
        }
 
-       if (dev->vhostfd >= 0) {
+       if (!dev->is_server) {
                if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
                                           NULL) < 0) {
                        PMD_INIT_LOG(ERR, "set_owner fails: %s",
@@ -419,8 +451,22 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
                dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
        }
 
-       if (dev->mac_specified)
+       if (!mrg_rxbuf) {
+               dev->device_features &= ~(1ull << VIRTIO_NET_F_MRG_RXBUF);
+               dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
+       }
+
+       if (!in_order) {
+               dev->device_features &= ~(1ull << VIRTIO_F_IN_ORDER);
+               dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
+       }
+
+       if (dev->mac_specified) {
                dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
+       } else {
+               dev->device_features &= ~(1ull << VIRTIO_NET_F_MAC);
+               dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
+       }
 
        if (cq) {
                /* device does not really need to know anything about CQ,
@@ -435,6 +481,14 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
                dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
                dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
                dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
+               dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
+               dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
+               dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
+               dev->unsupported_features |=
+                       (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
+               dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
+               dev->unsupported_features |=
+                       (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
        }
 
        /* The backend will not report this feature, we add it explicitly */
@@ -442,11 +496,15 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
                dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
 
        dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
+       dev->unsupported_features |= ~VIRTIO_USER_SUPPORTED_FEATURES;
 
        if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
                                virtio_user_mem_event_cb, dev)) {
-               PMD_INIT_LOG(ERR, "Failed to register mem event callback\n");
-               return -1;
+               if (rte_errno != ENOTSUP) {
+                       PMD_INIT_LOG(ERR, "Failed to register mem event"
+                                       " callback\n");
+                       return -1;
+               }
        }
 
        return 0;
@@ -486,7 +544,7 @@ virtio_user_dev_uninit(struct virtio_user_dev *dev)
                unlink(dev->path);
 }
 
-static uint8_t
+uint8_t
 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
 {
        uint16_t i;
@@ -498,11 +556,15 @@ virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
                return -1;
        }
 
-       for (i = 0; i < q_pairs; ++i)
-               ret |= dev->ops->enable_qp(dev, i, 1);
-       for (i = q_pairs; i < dev->max_queue_pairs; ++i)
-               ret |= dev->ops->enable_qp(dev, i, 0);
-
+       /* Server mode can't enable queue pairs if vhostfd is invalid,
+        * always return 0 in this case.
+        */
+       if (!dev->is_server || dev->vhostfd >= 0) {
+               for (i = 0; i < q_pairs; ++i)
+                       ret |= dev->ops->enable_qp(dev, i, 1);
+               for (i = q_pairs; i < dev->max_queue_pairs; ++i)
+                       ret |= dev->ops->enable_qp(dev, i, 0);
+       }
        dev->queue_pairs = q_pairs;
 
        return ret;