app/crypto-perf: support lookaside IPsec
[dpdk.git] / lib / vhost / vhost_user.c
index d6ec400..5a894ca 100644 (file)
@@ -188,7 +188,7 @@ vhost_backend_cleanup(struct virtio_net *dev)
                        dev->inflight_info->fd = -1;
                }
 
-               free(dev->inflight_info);
+               rte_free(dev->inflight_info);
                dev->inflight_info = NULL;
        }
 
@@ -433,10 +433,10 @@ vhost_user_set_vring_num(struct virtio_net **pdev,
        if (vq_is_packed(dev)) {
                if (vq->shadow_used_packed)
                        rte_free(vq->shadow_used_packed);
-               vq->shadow_used_packed = rte_malloc(NULL,
+               vq->shadow_used_packed = rte_malloc_socket(NULL,
                                vq->size *
                                sizeof(struct vring_used_elem_packed),
-                               RTE_CACHE_LINE_SIZE);
+                               RTE_CACHE_LINE_SIZE, vq->numa_node);
                if (!vq->shadow_used_packed) {
                        VHOST_LOG_CONFIG(ERR,
                                        "failed to allocate memory for shadow used ring.\n");
@@ -447,9 +447,9 @@ vhost_user_set_vring_num(struct virtio_net **pdev,
                if (vq->shadow_used_split)
                        rte_free(vq->shadow_used_split);
 
-               vq->shadow_used_split = rte_malloc(NULL,
+               vq->shadow_used_split = rte_malloc_socket(NULL,
                                vq->size * sizeof(struct vring_used_elem),
-                               RTE_CACHE_LINE_SIZE);
+                               RTE_CACHE_LINE_SIZE, vq->numa_node);
 
                if (!vq->shadow_used_split) {
                        VHOST_LOG_CONFIG(ERR,
@@ -460,9 +460,9 @@ vhost_user_set_vring_num(struct virtio_net **pdev,
 
        if (vq->batch_copy_elems)
                rte_free(vq->batch_copy_elems);
-       vq->batch_copy_elems = rte_malloc(NULL,
+       vq->batch_copy_elems = rte_malloc_socket(NULL,
                                vq->size * sizeof(struct batch_copy_elem),
-                               RTE_CACHE_LINE_SIZE);
+                               RTE_CACHE_LINE_SIZE, vq->numa_node);
        if (!vq->batch_copy_elems) {
                VHOST_LOG_CONFIG(ERR,
                        "failed to allocate memory for batching copy.\n");
@@ -505,6 +505,9 @@ numa_realloc(struct virtio_net *dev, int index)
                return dev;
        }
 
+       if (node == vq->numa_node)
+               goto out_dev_realloc;
+
        vq = rte_realloc_socket(vq, sizeof(*vq), 0, node);
        if (!vq) {
                VHOST_LOG_CONFIG(ERR, "Failed to realloc virtqueue %d on node %d\n",
@@ -559,6 +562,35 @@ numa_realloc(struct virtio_net *dev, int index)
                vq->log_cache = lc;
        }
 
+       if (vq->resubmit_inflight) {
+               struct rte_vhost_resubmit_info *ri;
+
+               ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
+               if (!ri) {
+                       VHOST_LOG_CONFIG(ERR, "Failed to realloc resubmit inflight on node %d\n",
+                                       node);
+                       return dev;
+               }
+               vq->resubmit_inflight = ri;
+
+               if (ri->resubmit_list) {
+                       struct rte_vhost_resubmit_desc *rd;
+
+                       rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
+                                       0, node);
+                       if (!rd) {
+                               VHOST_LOG_CONFIG(ERR, "Failed to realloc resubmit list on node %d\n",
+                                               node);
+                               return dev;
+                       }
+                       ri->resubmit_list = rd;
+               }
+       }
+
+       vq->numa_node = node;
+
+out_dev_realloc:
+
        if (dev->flags & VIRTIO_DEV_RUNNING)
                return dev;
 
@@ -1213,9 +1245,10 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
        struct virtio_net *dev = *pdev;
        struct VhostUserMemory *memory = &msg->payload.memory;
        struct rte_vhost_mem_region *reg;
-
+       int numa_node = SOCKET_ID_ANY;
        uint64_t mmap_offset;
        uint32_t i;
+       bool async_notify = false;
 
        if (validate_msg_fds(msg, memory->nregions) != 0)
                return RTE_VHOST_MSG_RESULT_ERR;
@@ -1243,6 +1276,16 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
                                vdpa_dev->ops->dev_close(dev->vid);
                        dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
                }
+
+               /* notify the vhost application to stop DMA transfers */
+               if (dev->async_copy && dev->notify_ops->vring_state_changed) {
+                       for (i = 0; i < dev->nr_vring; i++) {
+                               dev->notify_ops->vring_state_changed(dev->vid,
+                                               i, 0);
+                       }
+                       async_notify = true;
+               }
+
                free_mem_region(dev);
                rte_free(dev->mem);
                dev->mem = NULL;
@@ -1253,13 +1296,21 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
                for (i = 0; i < dev->nr_vring; i++)
                        vhost_user_iotlb_flush_all(dev->virtqueue[i]);
 
+       /*
+        * If VQ 0 has already been allocated, try to allocate on the same
+        * NUMA node. It can be reallocated later in numa_realloc().
+        */
+       if (dev->nr_vring > 0)
+               numa_node = dev->virtqueue[0]->numa_node;
+
        dev->nr_guest_pages = 0;
        if (dev->guest_pages == NULL) {
                dev->max_guest_pages = 8;
-               dev->guest_pages = rte_zmalloc(NULL,
+               dev->guest_pages = rte_zmalloc_socket(NULL,
                                        dev->max_guest_pages *
                                        sizeof(struct guest_page),
-                                       RTE_CACHE_LINE_SIZE);
+                                       RTE_CACHE_LINE_SIZE,
+                                       numa_node);
                if (dev->guest_pages == NULL) {
                        VHOST_LOG_CONFIG(ERR,
                                "(%d) failed to allocate memory "
@@ -1269,8 +1320,8 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
                }
        }
 
-       dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
-               sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
+       dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
+               sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
        if (dev->mem == NULL) {
                VHOST_LOG_CONFIG(ERR,
                        "(%d) failed to allocate memory for dev->mem\n",
@@ -1331,6 +1382,11 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 
        dump_guest_pages(dev);
 
+       if (async_notify) {
+               for (i = 0; i < dev->nr_vring; i++)
+                       dev->notify_ops->vring_state_changed(dev->vid, i, 1);
+       }
+
        return RTE_VHOST_MSG_RESULT_OK;
 
 free_mem_table:
@@ -1476,6 +1532,7 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev,
        uint16_t num_queues, queue_size;
        struct virtio_net *dev = *pdev;
        int fd, i, j;
+       int numa_node = SOCKET_ID_ANY;
        void *addr;
 
        if (msg->size != sizeof(msg->payload.inflight)) {
@@ -1485,9 +1542,16 @@ vhost_user_get_inflight_fd(struct virtio_net **pdev,
                return RTE_VHOST_MSG_RESULT_ERR;
        }
 
+       /*
+        * If VQ 0 has already been allocated, try to allocate on the same
+        * NUMA node. It can be reallocated later in numa_realloc().
+        */
+       if (dev->nr_vring > 0)
+               numa_node = dev->virtqueue[0]->numa_node;
+
        if (dev->inflight_info == NULL) {
-               dev->inflight_info = calloc(1,
-                                           sizeof(struct inflight_mem_info));
+               dev->inflight_info = rte_zmalloc_socket("inflight_info",
+                               sizeof(struct inflight_mem_info), 0, numa_node);
                if (!dev->inflight_info) {
                        VHOST_LOG_CONFIG(ERR,
                                "failed to alloc dev inflight area\n");
@@ -1570,6 +1634,7 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg,
        struct vhost_virtqueue *vq;
        void *addr;
        int fd, i;
+       int numa_node = SOCKET_ID_ANY;
 
        fd = msg->fds[0];
        if (msg->size != sizeof(msg->payload.inflight) || fd < 0) {
@@ -1603,9 +1668,16 @@ vhost_user_set_inflight_fd(struct virtio_net **pdev, VhostUserMsg *msg,
                "set_inflight_fd pervq_inflight_size: %d\n",
                pervq_inflight_size);
 
+       /*
+        * If VQ 0 has already been allocated, try to allocate on the same
+        * NUMA node. It can be reallocated later in numa_realloc().
+        */
+       if (dev->nr_vring > 0)
+               numa_node = dev->virtqueue[0]->numa_node;
+
        if (!dev->inflight_info) {
-               dev->inflight_info = calloc(1,
-                                           sizeof(struct inflight_mem_info));
+               dev->inflight_info = rte_zmalloc_socket("inflight_info",
+                               sizeof(struct inflight_mem_info), 0, numa_node);
                if (dev->inflight_info == NULL) {
                        VHOST_LOG_CONFIG(ERR,
                                "failed to alloc dev inflight area\n");
@@ -1764,19 +1836,21 @@ vhost_check_queue_inflights_split(struct virtio_net *dev,
        vq->last_avail_idx += resubmit_num;
 
        if (resubmit_num) {
-               resubmit  = calloc(1, sizeof(struct rte_vhost_resubmit_info));
+               resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
+                               0, vq->numa_node);
                if (!resubmit) {
                        VHOST_LOG_CONFIG(ERR,
                                "failed to allocate memory for resubmit info.\n");
                        return RTE_VHOST_MSG_RESULT_ERR;
                }
 
-               resubmit->resubmit_list = calloc(resubmit_num,
-                       sizeof(struct rte_vhost_resubmit_desc));
+               resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
+                               resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
+                               0, vq->numa_node);
                if (!resubmit->resubmit_list) {
                        VHOST_LOG_CONFIG(ERR,
                                "failed to allocate memory for inflight desc.\n");
-                       free(resubmit);
+                       rte_free(resubmit);
                        return RTE_VHOST_MSG_RESULT_ERR;
                }
 
@@ -1858,19 +1932,21 @@ vhost_check_queue_inflights_packed(struct virtio_net *dev,
        }
 
        if (resubmit_num) {
-               resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
+               resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
+                               0, vq->numa_node);
                if (resubmit == NULL) {
                        VHOST_LOG_CONFIG(ERR,
                                "failed to allocate memory for resubmit info.\n");
                        return RTE_VHOST_MSG_RESULT_ERR;
                }
 
-               resubmit->resubmit_list = calloc(resubmit_num,
-                       sizeof(struct rte_vhost_resubmit_desc));
+               resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
+                               resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
+                               0, vq->numa_node);
                if (resubmit->resubmit_list == NULL) {
                        VHOST_LOG_CONFIG(ERR,
                                "failed to allocate memory for resubmit desc.\n");
-                       free(resubmit);
+                       rte_free(resubmit);
                        return RTE_VHOST_MSG_RESULT_ERR;
                }
 
@@ -2037,6 +2113,8 @@ vhost_user_get_vring_base(struct virtio_net **pdev,
        msg->size = sizeof(msg->payload.state);
        msg->fd_num = 0;
 
+       vhost_user_iotlb_flush_all(vq);
+
        vring_invalidate(dev, vq);
 
        return RTE_VHOST_MSG_RESULT_REPLY;
@@ -2193,9 +2271,9 @@ vhost_user_set_log_base(struct virtio_net **pdev, struct VhostUserMsg *msg,
                rte_free(vq->log_cache);
                vq->log_cache = NULL;
                vq->log_cache_nb_elem = 0;
-               vq->log_cache = rte_zmalloc("vq log cache",
+               vq->log_cache = rte_malloc_socket("vq log cache",
                                sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
-                               0);
+                               0, vq->numa_node);
                /*
                 * If log cache alloc fail, don't fail migration, but no
                 * caching will be done, which will impact performance
@@ -2251,7 +2329,7 @@ vhost_user_send_rarp(struct virtio_net **pdev, struct VhostUserMsg *msg,
                return RTE_VHOST_MSG_RESULT_ERR;
 
        VHOST_LOG_CONFIG(DEBUG,
-               ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
+               ":: mac: " RTE_ETHER_ADDR_PRT_FMT "\n",
                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        memcpy(dev->mac.addr_bytes, mac, 6);
 
@@ -2720,6 +2798,7 @@ vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
                break;
        case VHOST_USER_SET_VRING_NUM:
        case VHOST_USER_SET_VRING_BASE:
+       case VHOST_USER_GET_VRING_BASE:
        case VHOST_USER_SET_VRING_ENABLE:
                vring_idx = msg->payload.state.index;
                break;
@@ -2935,9 +3014,6 @@ skip_to_post_handle:
                }
        }
 
-       if (unlock_required)
-               vhost_user_unlock_all_queue_pairs(dev);
-
        /* If message was not handled at this stage, treat it as an error */
        if (!handled) {
                VHOST_LOG_CONFIG(ERR,
@@ -2972,6 +3048,8 @@ skip_to_post_handle:
                }
        }
 
+       if (unlock_required)
+               vhost_user_unlock_all_queue_pairs(dev);
 
        if (!virtio_is_ready(dev))
                goto out;