net/virtio: add virtio-user vring address ops
[dpdk.git] / drivers / net / virtio / virtio_user / virtio_user_dev.c
index 1c575d0..7e365d0 100644 (file)
 
 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
 
+const char * const virtio_user_backend_strings[] = {
+       [VIRTIO_USER_BACKEND_UNKNOWN] = "VIRTIO_USER_BACKEND_UNKNOWN",
+       [VIRTIO_USER_BACKEND_VHOST_USER] = "VHOST_USER",
+       [VIRTIO_USER_BACKEND_VHOST_KERNEL] = "VHOST_NET",
+       [VIRTIO_USER_BACKEND_VHOST_VDPA] = "VHOST_VDPA",
+};
+
 static int
 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
 {
@@ -33,7 +40,7 @@ virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
 
        file.index = queue_sel;
        file.fd = dev->callfds[queue_sel];
-       dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
+       dev->ops->set_vring_call(dev, &file);
 
        return 0;
 }
@@ -66,15 +73,15 @@ virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
 
        state.index = queue_sel;
        state.num = vring->num;
-       dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
+       dev->ops->set_vring_num(dev, &state);
 
        state.index = queue_sel;
        state.num = 0; /* no reservation */
        if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
                state.num |= (1 << 15);
-       dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
+       dev->ops->set_vring_base(dev, &state);
 
-       dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
+       dev->ops->set_vring_addr(dev, &addr);
 
        /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
         * lastly because vhost depends on this msg to judge if
@@ -82,7 +89,7 @@ virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
         */
        file.index = queue_sel;
        file.fd = dev->kickfds[queue_sel];
-       dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
+       dev->ops->set_vring_kick(dev, &file);
 
        return 0;
 }
@@ -112,20 +119,41 @@ virtio_user_queue_setup(struct virtio_user_dev *dev,
 }
 
 int
-is_vhost_user_by_type(const char *path)
+virtio_user_dev_set_features(struct virtio_user_dev *dev)
 {
-       struct stat sb;
+       uint64_t features;
+       int ret = -1;
+
+       pthread_mutex_lock(&dev->mutex);
+
+       if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER &&
+                       dev->vhostfd < 0)
+               goto error;
 
-       if (stat(path, &sb) == -1)
-               return 0;
+       /* Step 0: tell vhost to create queues */
+       if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
+               goto error;
 
-       return S_ISSOCK(sb.st_mode);
+       features = dev->features;
+
+       /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
+       features &= ~(1ull << VIRTIO_NET_F_MAC);
+       /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
+       features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
+       features &= ~(1ull << VIRTIO_NET_F_STATUS);
+       ret = dev->ops->set_features(dev, features);
+       if (ret < 0)
+               goto error;
+       PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
+error:
+       pthread_mutex_unlock(&dev->mutex);
+
+       return ret;
 }
 
 int
 virtio_user_start_device(struct virtio_user_dev *dev)
 {
-       uint64_t features;
        int ret;
 
        /*
@@ -144,27 +172,12 @@ virtio_user_start_device(struct virtio_user_dev *dev)
        rte_mcfg_mem_read_lock();
        pthread_mutex_lock(&dev->mutex);
 
-       if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
-               goto error;
-
-       /* Step 0: tell vhost to create queues */
-       if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
-               goto error;
-
-       /* Step 1: set features */
-       features = dev->features;
-       /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
-       features &= ~(1ull << VIRTIO_NET_F_MAC);
-       /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
-       features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
-       features &= ~(1ull << VIRTIO_NET_F_STATUS);
-       ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
-       if (ret < 0)
+       if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER &&
+                       dev->vhostfd < 0)
                goto error;
-       PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
 
        /* Step 2: share memory regions */
-       ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
+       ret = dev->ops->set_memory_table(dev);
        if (ret < 0)
                goto error;
 
@@ -205,9 +218,8 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)
        /* Stop the backend. */
        for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
                state.index = i;
-               if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE,
-                                          &state) < 0) {
-                       PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
+               if (dev->ops->get_vring_base(dev, &state) < 0) {
+                       PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u",
                                    i);
                        error = -1;
                        goto out;
@@ -263,6 +275,7 @@ virtio_user_dev_init_notify(struct virtio_user_dev *dev)
                }
                kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
                if (kickfd < 0) {
+                       close(callfd);
                        PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
                        break;
                }
@@ -271,7 +284,7 @@ virtio_user_dev_init_notify(struct virtio_user_dev *dev)
        }
 
        if (i < VIRTIO_MAX_VIRTQUEUES) {
-               for (j = 0; j <= i; ++j) {
+               for (j = 0; j < i; ++j) {
                        close(dev->callfds[j]);
                        close(dev->kickfds[j]);
                }
@@ -315,9 +328,9 @@ virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
 
 static void
 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
-                                                const void *addr __rte_unused,
-                                                size_t len __rte_unused,
-                                                void *arg)
+                        const void *addr,
+                        size_t len __rte_unused,
+                        void *arg)
 {
        struct virtio_user_dev *dev = arg;
        struct rte_memseg_list *msl;
@@ -338,7 +351,7 @@ virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
                dev->ops->enable_qp(dev, i, 0);
 
        /* Step 2: update memory regions */
-       dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
+       dev->ops->set_memory_table(dev);
 
        /* Step 3: resume the active queues */
        for (i = 0; i < dev->queue_pairs; i++)
@@ -358,16 +371,16 @@ virtio_user_dev_setup(struct virtio_user_dev *dev)
        dev->tapfds = NULL;
 
        if (dev->is_server) {
-               if (access(dev->path, F_OK) == 0 &&
-                   !is_vhost_user_by_type(dev->path)) {
-                       PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
+               if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
+                       PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
                        return -1;
                }
                dev->ops = &virtio_ops_user;
        } else {
-               if (is_vhost_user_by_type(dev->path)) {
+               if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
                        dev->ops = &virtio_ops_user;
-               } else {
+               } else if (dev->backend_type ==
+                                       VIRTIO_USER_BACKEND_VHOST_KERNEL) {
                        dev->ops = &virtio_ops_kernel;
 
                        dev->vhostfds = malloc(dev->max_queue_pairs *
@@ -383,6 +396,12 @@ virtio_user_dev_setup(struct virtio_user_dev *dev)
                                dev->vhostfds[q] = -1;
                                dev->tapfds[q] = -1;
                        }
+               } else if (dev->backend_type ==
+                               VIRTIO_USER_BACKEND_VHOST_VDPA) {
+                       dev->ops = &virtio_ops_vdpa;
+               } else {
+                       PMD_DRV_LOG(ERR, "Unknown backend type");
+                       return -1;
                }
        }
 
@@ -417,13 +436,25 @@ virtio_user_dev_setup(struct virtio_user_dev *dev)
         1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
         1ULL << VIRTIO_F_IN_ORDER              |       \
         1ULL << VIRTIO_F_VERSION_1             |       \
-        1ULL << VIRTIO_F_RING_PACKED)
+        1ULL << VIRTIO_F_RING_PACKED           |       \
+        1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
+
+#define VHOST_USER_SUPPORTED_PROTOCOL_FEATURES         \
+       (1ULL << VHOST_USER_PROTOCOL_F_MQ |             \
+        1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK |      \
+        1ULL << VHOST_USER_PROTOCOL_F_STATUS)
 
+#define VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES         \
+       (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2   |       \
+       1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
 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 server, int mrg_rxbuf, int in_order, int packed_vq)
+                    int server, int mrg_rxbuf, int in_order, int packed_vq,
+                    enum virtio_user_backend_type backend_type)
 {
+       uint64_t protocol_features = 0;
+
        pthread_mutex_init(&dev->mutex, NULL);
        strlcpy(dev->path, path, PATH_MAX);
        dev->started = 0;
@@ -434,6 +465,13 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
        dev->mac_specified = 0;
        dev->frontend_features = 0;
        dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
+       dev->backend_type = backend_type;
+
+       if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
+               dev->protocol_features = VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
+       else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
+               dev->protocol_features = VHOST_VDPA_SUPPORTED_PROTOCOL_FEATURES;
+
        parse_mac(dev, mac);
 
        if (*ifname) {
@@ -446,20 +484,37 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
                return -1;
        }
 
+       if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER)
+               dev->unsupported_features |=
+                       (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
+
        if (!dev->is_server) {
-               if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
-                                          NULL) < 0) {
+               if (dev->ops->set_owner(dev) < 0) {
                        PMD_INIT_LOG(ERR, "set_owner fails: %s",
                                     strerror(errno));
                        return -1;
                }
 
-               if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
-                                          &dev->device_features) < 0) {
+               if (dev->ops->get_features(dev, &dev->device_features) < 0) {
                        PMD_INIT_LOG(ERR, "get_features failed: %s",
                                     strerror(errno));
                        return -1;
                }
+
+
+               if ((dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
+                               (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)) {
+                       if (dev->ops->get_protocol_features(dev, &protocol_features))
+                               return -1;
+
+                       dev->protocol_features &= protocol_features;
+
+                       if (dev->ops->set_protocol_features(dev, dev->protocol_features))
+                               return -1;
+
+                       if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
+                               dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
+               }
        } else {
                /* We just pretend vhost-user can support all these features.
                 * Note that this could be problematic that if some feature is
@@ -467,8 +522,16 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
                 * later.
                 */
                dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
+
+               /* We cannot assume VHOST_USER_PROTOCOL_F_STATUS is supported
+                * until it's negotiated
+                */
+               dev->protocol_features &=
+                       ~(1ULL << VHOST_USER_PROTOCOL_F_STATUS);
        }
 
+
+
        if (!mrg_rxbuf)
                dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
 
@@ -501,7 +564,7 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
        }
 
        /* The backend will not report this feature, we add it explicitly */
-       if (is_vhost_user_by_type(dev->path))
+       if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
                dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
 
        /*
@@ -537,7 +600,8 @@ virtio_user_dev_uninit(struct virtio_user_dev *dev)
                close(dev->kickfds[i]);
        }
 
-       close(dev->vhostfd);
+       if (dev->vhostfd >= 0)
+               close(dev->vhostfd);
 
        if (dev->is_server && dev->listenfd >= 0) {
                close(dev->listenfd);
@@ -545,8 +609,11 @@ virtio_user_dev_uninit(struct virtio_user_dev *dev)
        }
 
        if (dev->vhostfds) {
-               for (i = 0; i < dev->max_queue_pairs; ++i)
+               for (i = 0; i < dev->max_queue_pairs; ++i) {
                        close(dev->vhostfds[i]);
+                       if (dev->tapfds[i] >= 0)
+                               close(dev->tapfds[i]);
+               }
                free(dev->vhostfds);
                free(dev->tapfds);
        }
@@ -613,6 +680,10 @@ virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
 
                queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
                status = virtio_user_handle_mq(dev, queues);
+       } else if (hdr->class == VIRTIO_NET_CTRL_RX  ||
+                  hdr->class == VIRTIO_NET_CTRL_MAC ||
+                  hdr->class == VIRTIO_NET_CTRL_VLAN) {
+               status = 0;
        }
 
        /* Update status */
@@ -664,6 +735,10 @@ virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
                queues = *(uint16_t *)(uintptr_t)
                                vring->desc[idx_data].addr;
                status = virtio_user_handle_mq(dev, queues);
+       } else if (hdr->class == VIRTIO_NET_CTRL_RX  ||
+                  hdr->class == VIRTIO_NET_CTRL_MAC ||
+                  hdr->class == VIRTIO_NET_CTRL_VLAN) {
+               status = 0;
        }
 
        /* Update status */
@@ -718,8 +793,10 @@ virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
        struct vring *vring = &dev->vrings[queue_idx];
 
        /* Consume avail ring, using used ring idx as first one */
-       while (vring->used->idx != vring->avail->idx) {
-               avail_idx = (vring->used->idx) & (vring->num - 1);
+       while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
+              != vring->avail->idx) {
+               avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
+                           & (vring->num - 1);
                desc_idx = vring->avail->ring[avail_idx];
 
                n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
@@ -729,6 +806,85 @@ virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
                uep->id = desc_idx;
                uep->len = n_descs;
 
-               vring->used->idx++;
+               __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
+       }
+}
+
+int
+virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
+{
+       int ret;
+       uint64_t arg = status;
+
+       pthread_mutex_lock(&dev->mutex);
+       dev->status = status;
+       if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
+               ret = dev->ops->send_request(dev,
+                               VHOST_USER_SET_STATUS, &arg);
+       else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
+               ret = dev->ops->send_request(dev,
+                               VHOST_USER_SET_STATUS, &status);
+       else
+               ret = -ENOTSUP;
+
+       if (ret && ret != -ENOTSUP) {
+               PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret,
+                            strerror(errno));
+       }
+
+       pthread_mutex_unlock(&dev->mutex);
+       return ret;
+}
+
+int
+virtio_user_dev_update_status(struct virtio_user_dev *dev)
+{
+       uint64_t ret;
+       uint8_t status;
+       int err;
+
+       pthread_mutex_lock(&dev->mutex);
+       if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) {
+               err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS, &ret);
+               if (!err && ret > UINT8_MAX) {
+                       PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS "
+                                       "response 0x%" PRIx64 "\n", ret);
+                       err = -1;
+                       goto error;
+               }
+
+               status = ret;
+       } else if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA) {
+               err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS,
+                               &status);
+       } else {
+               err = -ENOTSUP;
        }
+
+       if (!err) {
+               dev->status = status;
+               PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
+                       "\t-RESET: %u\n"
+                       "\t-ACKNOWLEDGE: %u\n"
+                       "\t-DRIVER: %u\n"
+                       "\t-DRIVER_OK: %u\n"
+                       "\t-FEATURES_OK: %u\n"
+                       "\t-DEVICE_NEED_RESET: %u\n"
+                       "\t-FAILED: %u\n",
+                       dev->status,
+                       (dev->status == VIRTIO_CONFIG_STATUS_RESET),
+                       !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
+                       !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
+                       !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
+                       !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
+                       !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
+                       !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
+       } else if (err != -ENOTSUP) {
+               PMD_INIT_LOG(ERR, "VHOST_USER_GET_STATUS failed (%d): %s", err,
+                            strerror(errno));
+       }
+
+error:
+       pthread_mutex_unlock(&dev->mutex);
+       return err;
 }