vhost: realloc device and queues to same numa node as vring desc
authorHuawei Xie <huawei.xie@intel.com>
Thu, 25 Jun 2015 05:47:37 +0000 (13:47 +0800)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Mon, 29 Jun 2015 16:57:33 +0000 (18:57 +0200)
When we get the address of vring descriptor table in VHOST_SET_VRING_ADDR
message, will try to reallocate vhost device and virt queue to the same
numa node.

Signed-off-by: Huawei Xie <huawei.xie@intel.com>
config/common_linuxapp
lib/librte_vhost/Makefile
lib/librte_vhost/virtio-net.c
mk/rte.app.mk

index aae22f4..f0bfce5 100644 (file)
@@ -429,6 +429,7 @@ CONFIG_RTE_KNI_VHOST_DEBUG_TX=n
 #
 CONFIG_RTE_LIBRTE_VHOST=n
 CONFIG_RTE_LIBRTE_VHOST_USER=y
+CONFIG_RTE_LIBRTE_VHOST_NUMA=n
 CONFIG_RTE_LIBRTE_VHOST_DEBUG=n
 
 #
index a8645a6..6681f22 100644 (file)
@@ -46,6 +46,10 @@ CFLAGS += -I vhost_cuse -lfuse
 LDFLAGS += -lfuse
 endif
 
+ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
+LDFLAGS += -lnuma
+endif
+
 # all source are stored in SRCS-y
 SRCS-$(CONFIG_RTE_LIBRTE_VHOST) := virtio-net.c vhost_rxtx.c
 ifeq ($(CONFIG_RTE_LIBRTE_VHOST_USER),y)
index 8b59c02..b520ec5 100644 (file)
@@ -38,6 +38,9 @@
 #include <stdlib.h>
 #include <sys/mman.h>
 #include <unistd.h>
+#ifdef RTE_LIBRTE_VHOST_NUMA
+#include <numaif.h>
+#endif
 
 #include <sys/socket.h>
 
@@ -481,6 +484,88 @@ set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
        return 0;
 }
 
+/*
+ * Reallocate virtio_det and vhost_virtqueue data structure to make them on the
+ * same numa node as the memory of vring descriptor.
+ */
+#ifdef RTE_LIBRTE_VHOST_NUMA
+static struct virtio_net*
+numa_realloc(struct virtio_net *dev, int index)
+{
+       int oldnode, newnode;
+       struct virtio_net_config_ll *old_ll_dev, *new_ll_dev = NULL;
+       struct vhost_virtqueue *old_vq, *new_vq = NULL;
+       int ret;
+       int realloc_dev = 0, realloc_vq = 0;
+
+       old_ll_dev = (struct virtio_net_config_ll *)dev;
+       old_vq = dev->virtqueue[index];
+
+       ret  = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
+                       MPOL_F_NODE | MPOL_F_ADDR);
+       ret = ret | get_mempolicy(&oldnode, NULL, 0, old_ll_dev,
+                       MPOL_F_NODE | MPOL_F_ADDR);
+       if (ret) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "Unable to get vring desc or dev numa information.\n");
+               return dev;
+       }
+       if (oldnode != newnode)
+               realloc_dev = 1;
+
+       ret = get_mempolicy(&oldnode, NULL, 0, old_vq,
+                       MPOL_F_NODE | MPOL_F_ADDR);
+       if (ret) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "Unable to get vq numa information.\n");
+               return dev;
+       }
+       if (oldnode != newnode)
+               realloc_vq = 1;
+
+       if (realloc_dev == 0 && realloc_vq == 0)
+               return dev;
+
+       if (realloc_dev)
+               new_ll_dev = rte_malloc_socket(NULL,
+                       sizeof(struct virtio_net_config_ll), 0, newnode);
+       if (realloc_vq)
+               new_vq = rte_malloc_socket(NULL,
+                       sizeof(struct vhost_virtqueue), 0, newnode);
+       if (!new_ll_dev && !new_vq)
+               return dev;
+
+       if (realloc_vq)
+               memcpy(new_vq, old_vq, sizeof(*new_vq));
+       if (realloc_dev)
+               memcpy(new_ll_dev, old_ll_dev, sizeof(*new_ll_dev));
+       (new_ll_dev ? new_ll_dev : old_ll_dev)->dev.virtqueue[index] =
+               new_vq ? new_vq : old_vq;
+       if (realloc_vq)
+               rte_free(old_vq);
+       if (realloc_dev) {
+               if (ll_root == old_ll_dev)
+                       ll_root = new_ll_dev;
+               else {
+                       struct virtio_net_config_ll *prev = ll_root;
+                       while (prev->next != old_ll_dev)
+                               prev = prev->next;
+                       prev->next = new_ll_dev;
+                       new_ll_dev->next = old_ll_dev->next;
+               }
+               rte_free(old_ll_dev);
+       }
+
+       return realloc_dev ? &new_ll_dev->dev : dev;
+}
+#else
+static struct virtio_net*
+numa_realloc(struct virtio_net *dev, int index __rte_unused)
+{
+       return dev;
+}
+#endif
+
 /*
  * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
  * The virtio device sends us the desc, used and avail ring addresses.
@@ -509,6 +594,9 @@ set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
                return -1;
        }
 
+       dev = numa_realloc(dev, addr->index);
+       vq = dev->virtqueue[addr->index];
+
        vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
                        addr->avail_user_addr);
        if (vq->avail == 0) {
index 1a2043a..0d6c14a 100644 (file)
@@ -92,6 +92,10 @@ endif # ! CONFIG_RTE_BUILD_COMBINE_LIBS
 
 _LDLIBS-$(CONFIG_RTE_LIBRTE_PMD_PCAP)       += -lpcap
 
+ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y)
+_LDLIBS-$(CONFIG_RTE_LIBRTE_VHOST)          += -lnuma
+endif
+
 ifeq ($(CONFIG_RTE_LIBRTE_VHOST_USER),n)
 _LDLIBS-$(CONFIG_RTE_LIBRTE_VHOST)          += -lfuse
 endif