vhost: use new APIs to handle features
[dpdk.git] / lib / librte_vhost / vhost_user.c
index a92377a..65fd0fc 100644 (file)
@@ -51,6 +51,9 @@
 #include "vhost.h"
 #include "vhost_user.h"
 
+#define VIRTIO_MIN_MTU 68
+#define VIRTIO_MAX_MTU 65535
+
 static const char *vhost_message_str[VHOST_USER_MAX] = {
        [VHOST_USER_NONE] = "VHOST_USER_NONE",
        [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
@@ -72,6 +75,7 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
        [VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
        [VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
        [VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
+       [VHOST_USER_NET_SET_MTU]  = "VHOST_USER_NET_SET_MTU",
 };
 
 static uint64_t
@@ -143,9 +147,12 @@ vhost_user_reset_owner(struct virtio_net *dev)
  * The features that we support are requested.
  */
 static uint64_t
-vhost_user_get_features(void)
+vhost_user_get_features(struct virtio_net *dev)
 {
-       return VHOST_FEATURES;
+       uint64_t features = 0;
+
+       rte_vhost_driver_get_features(dev->ifname, &features);
+       return features;
 }
 
 /*
@@ -154,7 +161,10 @@ vhost_user_get_features(void)
 static int
 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
 {
-       if (features & ~VHOST_FEATURES)
+       uint64_t vhost_features = 0;
+
+       rte_vhost_driver_get_features(dev->ifname, &vhost_features);
+       if (features & ~vhost_features)
                return -1;
 
        dev->features = features;
@@ -180,7 +190,32 @@ static int
 vhost_user_set_vring_num(struct virtio_net *dev,
                         struct vhost_vring_state *state)
 {
-       dev->virtqueue[state->index]->size = state->num;
+       struct vhost_virtqueue *vq = dev->virtqueue[state->index];
+
+       vq->size = state->num;
+
+       if (dev->dequeue_zero_copy) {
+               vq->nr_zmbuf = 0;
+               vq->last_zmbuf_idx = 0;
+               vq->zmbuf_size = vq->size;
+               vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
+                                        sizeof(struct zcopy_mbuf), 0);
+               if (vq->zmbufs == NULL) {
+                       RTE_LOG(WARNING, VHOST_CONFIG,
+                               "failed to allocate mem for zero copy; "
+                               "zero copy is force disabled\n");
+                       dev->dequeue_zero_copy = 0;
+               }
+       }
+
+       vq->shadow_used_ring = rte_malloc(NULL,
+                               vq->size * sizeof(struct vring_used_elem),
+                               RTE_CACHE_LINE_SIZE);
+       if (!vq->shadow_used_ring) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "failed to allocate memory for shadow used ring.\n");
+               return -1;
+       }
 
        return 0;
 }
@@ -422,14 +457,14 @@ add_guest_pages(struct virtio_net *dev, struct virtio_memory_region *reg,
        reg_size -= size;
 
        while (reg_size > 0) {
+               size = RTE_MIN(reg_size, page_size);
                host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
                                                  host_user_addr);
-               add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
-                                  page_size);
+               add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
 
-               host_user_addr  += page_size;
-               guest_phys_addr += page_size;
-               reg_size -= page_size;
+               host_user_addr  += size;
+               guest_phys_addr += size;
+               reg_size -= size;
        }
 }
 
@@ -542,7 +577,8 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
                                      mmap_offset;
 
-               add_guest_pages(dev, reg, alignment);
+               if (dev->dequeue_zero_copy)
+                       add_guest_pages(dev, reg, alignment);
 
                RTE_LOG(INFO, VHOST_CONFIG,
                        "guest memory region %u, size: 0x%" PRIx64 "\n"
@@ -609,7 +645,6 @@ vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
 {
        struct vhost_vring_file file;
        struct vhost_virtqueue *vq;
-       uint32_t cur_qp_idx;
 
        file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
        if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
@@ -619,19 +654,7 @@ vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring call idx:%d file:%d\n", file.index, file.fd);
 
-       /*
-        * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
-        * we get, so we do vring queue pair allocation here.
-        */
-       cur_qp_idx = file.index / VIRTIO_QNUM;
-       if (cur_qp_idx + 1 > dev->virt_qp_nb) {
-               if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
-                       return;
-       }
-
        vq = dev->virtqueue[file.index];
-       assert(vq != NULL);
-
        if (vq->callfd >= 0)
                close(vq->callfd);
 
@@ -661,10 +684,35 @@ vhost_user_set_vring_kick(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                close(vq->kickfd);
        vq->kickfd = file.fd;
 
-       if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
-               if (notify_ops->new_device(dev->vid) == 0)
-                       dev->flags |= VIRTIO_DEV_RUNNING;
+       if (virtio_is_ready(dev)) {
+               dev->flags |= VIRTIO_DEV_READY;
+
+               if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
+                       if (dev->dequeue_zero_copy) {
+                               RTE_LOG(INFO, VHOST_CONFIG,
+                                               "dequeue zero copy is enabled\n");
+                       }
+
+                       if (notify_ops->new_device(dev->vid) == 0)
+                               dev->flags |= VIRTIO_DEV_RUNNING;
+               }
+       }
+}
+
+static void
+free_zmbufs(struct vhost_virtqueue *vq)
+{
+       struct zcopy_mbuf *zmbuf, *next;
+
+       for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
+            zmbuf != NULL; zmbuf = next) {
+               next = TAILQ_NEXT(zmbuf, next);
+
+               rte_pktmbuf_free(zmbuf->mbuf);
+               TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
        }
+
+       rte_free(vq->zmbufs);
 }
 
 /*
@@ -674,14 +722,18 @@ static int
 vhost_user_get_vring_base(struct virtio_net *dev,
                          struct vhost_vring_state *state)
 {
+       struct vhost_virtqueue *vq = dev->virtqueue[state->index];
+
        /* We have to stop the queue (virtio) if it is running. */
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
                notify_ops->destroy_device(dev->vid);
        }
 
+       dev->flags &= ~VIRTIO_DEV_READY;
+
        /* Here we are safe to get the last used index */
-       state->num = dev->virtqueue[state->index]->last_used_idx;
+       state->num = vq->last_used_idx;
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring base idx:%d file:%d\n", state->index, state->num);
@@ -690,10 +742,15 @@ vhost_user_get_vring_base(struct virtio_net *dev,
         * sent and only sent in vhost_vring_stop.
         * TODO: cleanup the vring, it isn't usable since here.
         */
-       if (dev->virtqueue[state->index]->kickfd >= 0)
-               close(dev->virtqueue[state->index]->kickfd);
+       if (vq->kickfd >= 0)
+               close(vq->kickfd);
 
-       dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
+       vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
+
+       if (dev->dequeue_zero_copy)
+               free_zmbufs(vq);
+       rte_free(vq->shadow_used_ring);
+       vq->shadow_used_ring = NULL;
 
        return 0;
 }
@@ -811,6 +868,22 @@ vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
        return 0;
 }
 
+static int
+vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
+{
+       if (msg->payload.u64 < VIRTIO_MIN_MTU ||
+                       msg->payload.u64 > VIRTIO_MAX_MTU) {
+               RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
+                               msg->payload.u64);
+
+               return -1;
+       }
+
+       dev->mtu = msg->payload.u64;
+
+       return 0;
+}
+
 /* return bytes# of read on success or negative val on failure. */
 static int
 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
@@ -850,6 +923,7 @@ send_vhost_message(int sockfd, struct VhostUserMsg *msg)
                return 0;
 
        msg->flags &= ~VHOST_USER_VERSION_MASK;
+       msg->flags &= ~VHOST_USER_NEED_REPLY;
        msg->flags |= VHOST_USER_VERSION;
        msg->flags |= VHOST_USER_REPLY_MASK;
 
@@ -859,6 +933,46 @@ send_vhost_message(int sockfd, struct VhostUserMsg *msg)
        return ret;
 }
 
+/*
+ * Allocate a queue pair if it hasn't been allocated yet
+ */
+static int
+vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
+{
+       uint16_t vring_idx;
+       uint16_t qp_idx;
+
+       switch (msg->request) {
+       case VHOST_USER_SET_VRING_KICK:
+       case VHOST_USER_SET_VRING_CALL:
+       case VHOST_USER_SET_VRING_ERR:
+               vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
+               break;
+       case VHOST_USER_SET_VRING_NUM:
+       case VHOST_USER_SET_VRING_BASE:
+       case VHOST_USER_SET_VRING_ENABLE:
+               vring_idx = msg->payload.state.index;
+               break;
+       case VHOST_USER_SET_VRING_ADDR:
+               vring_idx = msg->payload.addr.index;
+               break;
+       default:
+               return 0;
+       }
+
+       qp_idx = vring_idx / VIRTIO_QNUM;
+       if (qp_idx >= VHOST_MAX_QUEUE_PAIRS) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "invalid vring index: %u\n", vring_idx);
+               return -1;
+       }
+
+       if (dev->virtqueue[qp_idx * VIRTIO_QNUM])
+               return 0;
+
+       return alloc_vring_queue_pair(dev, qp_idx);
+}
+
 int
 vhost_user_msg_handler(int vid, int fd)
 {
@@ -885,11 +999,20 @@ vhost_user_msg_handler(int vid, int fd)
                return -1;
        }
 
+       ret = 0;
        RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
                vhost_message_str[msg.request]);
+
+       ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
+       if (ret < 0) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "failed to alloc queue\n");
+               return -1;
+       }
+
        switch (msg.request) {
        case VHOST_USER_GET_FEATURES:
-               msg.payload.u64 = vhost_user_get_features();
+               msg.payload.u64 = vhost_user_get_features(dev);
                msg.size = sizeof(msg.payload.u64);
                send_vhost_message(fd, &msg);
                break;
@@ -914,7 +1037,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_SET_MEM_TABLE:
-               vhost_user_set_mem_table(dev, &msg);
+               ret = vhost_user_set_mem_table(dev, &msg);
                break;
 
        case VHOST_USER_SET_LOG_BASE:
@@ -940,7 +1063,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_GET_VRING_BASE:
-               ret = vhost_user_get_vring_base(dev, &msg.payload.state);
+               vhost_user_get_vring_base(dev, &msg.payload.state);
                msg.size = sizeof(msg.payload.state);
                send_vhost_message(fd, &msg);
                break;
@@ -971,10 +1094,21 @@ vhost_user_msg_handler(int vid, int fd)
                vhost_user_send_rarp(dev, &msg);
                break;
 
+       case VHOST_USER_NET_SET_MTU:
+               ret = vhost_user_net_set_mtu(dev, &msg);
+               break;
+
        default:
+               ret = -1;
                break;
 
        }
 
+       if (msg.flags & VHOST_USER_NEED_REPLY) {
+               msg.payload.u64 = !!ret;
+               msg.size = sizeof(msg.payload.u64);
+               send_vhost_message(fd, &msg);
+       }
+
        return 0;
 }