[dpdk-dev] [PATCH v7 2/4] vhost: allocate virtio_net in memzone

Jianfeng Tan jianfeng.tan at intel.com
Fri Oct 13 04:04:10 CEST 2017


Instead of allocate on the stack, change to allocate in memzone
so that we can retrieve them in secondary processes.

TODO: numa awareness.

Signed-off-by: Jianfeng Tan <jianfeng.tan at intel.com>
---
 lib/librte_vhost/socket.c |  2 ++
 lib/librte_vhost/vhost.c  | 34 ++++++++++++++++++++++++++++++++--
 lib/librte_vhost/vhost.h  |  4 +++-
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
index 41aa3f9..35b9751 100644
--- a/lib/librte_vhost/socket.c
+++ b/lib/librte_vhost/socket.c
@@ -606,6 +606,8 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
 	int ret = -1;
 	struct vhost_user_socket *vsocket;
 
+	alloc_vhost_devices();
+
 	if (!path)
 		return -1;
 
diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
index 0b6aa1c..2b687ea 100644
--- a/lib/librte_vhost/vhost.c
+++ b/lib/librte_vhost/vhost.c
@@ -47,15 +47,45 @@
 #include <rte_memory.h>
 #include <rte_malloc.h>
 #include <rte_vhost.h>
+#include <rte_memzone.h>
 
 #include "vhost.h"
 
-struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
+#define MZ_VHOST_DEVICES "mz_vhost_devices"
+struct virtio_net **vhost_devices;
+
+void
+alloc_vhost_devices(void)
+{
+	const struct rte_memzone *mz;
+
+	if (vhost_devices != NULL)
+		return;
+
+	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+		mz = rte_memzone_reserve(MZ_VHOST_DEVICES,
+				MAX_VHOST_DEVICE * sizeof(*vhost_devices),
+				rte_socket_id(), 0);
+	} else
+		mz = rte_memzone_lookup(MZ_VHOST_DEVICES);
+
+	if (mz == NULL)
+		rte_panic("Cannot allocate memzone for vhost_devices\n");
+
+	vhost_devices = mz->addr;
+	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
+		memset(vhost_devices, 0,
+		       MAX_VHOST_DEVICE * sizeof(*vhost_devices));
+}
 
 struct virtio_net *
 get_device(int vid)
 {
-	struct virtio_net *dev = vhost_devices[vid];
+	struct virtio_net *dev;
+
+	alloc_vhost_devices();
+
+	dev = vhost_devices[vid];
 
 	if (unlikely(!dev)) {
 		RTE_LOG(ERR, VHOST_CONFIG,
diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index 6fe72ae..bc1f31e 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -278,7 +278,7 @@ vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
 extern uint64_t VHOST_FEATURES;
 #define MAX_VHOST_DEVICE	1024
-extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
+extern struct virtio_net **vhost_devices;
 
 /* Convert guest physical address to host physical address */
 static __rte_always_inline phys_addr_t
@@ -300,6 +300,8 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
 	return 0;
 }
 
+
+void alloc_vhost_devices(void);
 struct virtio_net *get_device(int vid);
 
 int vhost_new_device(void);
-- 
2.7.4



More information about the dev mailing list