Blame SOURCES/0005-vhost-add-flag-for-built-in-virtio-driver.patch

c7ffa4
From 8db980965f3d8cde1abbdb89eaecbc829460133e Mon Sep 17 00:00:00 2001
c7ffa4
From: Stefan Hajnoczi <stefanha@redhat.com>
c7ffa4
Date: Wed, 31 Jan 2018 17:46:50 +0000
c7ffa4
Subject: [PATCH 5/6] vhost: add flag for built-in virtio driver
c7ffa4
c7ffa4
The librte_vhost API is used in two ways:
c7ffa4
1. As a vhost net device backend via rte_vhost_enqueue/dequeue_burst().
c7ffa4
2. As a library for implementing vhost device backends.
c7ffa4
c7ffa4
There is no distinction between the two at the API level or in the
c7ffa4
librte_vhost implementation.  For example, device state is kept in
c7ffa4
"struct virtio_net" regardless of whether this is actually a net device
c7ffa4
backend or whether the built-in virtio_net.c driver is in use.
c7ffa4
c7ffa4
The virtio_net.c driver should be a librte_vhost API client just like
c7ffa4
the vhost-scsi code and have no special access to vhost.h internals.
c7ffa4
Unfortunately, fixing this requires significant librte_vhost API
c7ffa4
changes.
c7ffa4
c7ffa4
This patch takes a different approach: keep the librte_vhost API
c7ffa4
unchanged but track whether the built-in virtio_net.c driver is in use.
c7ffa4
See the next patch for a bug fix that requires knowledge of whether
c7ffa4
virtio_net.c is in use.
c7ffa4
c7ffa4
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
c7ffa4
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
c7ffa4
Acked-by: Yuanhan Liu <yliu@fridaylinux.org>
c7ffa4
(cherry picked from commit 1c717af4c699e60081feb1d645f86189551f9a9c)
c7ffa4
Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
c7ffa4
---
c7ffa4
 dpdk-17.11/lib/librte_vhost/socket.c     | 15 +++++++++++++++
c7ffa4
 dpdk-17.11/lib/librte_vhost/vhost.c      | 17 ++++++++++++++++-
c7ffa4
 dpdk-17.11/lib/librte_vhost/vhost.h      |  3 +++
c7ffa4
 dpdk-17.11/lib/librte_vhost/virtio_net.c | 14 ++++++++++++++
c7ffa4
 4 files changed, 48 insertions(+), 1 deletion(-)
c7ffa4
c7ffa4
diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
c7ffa4
index 422da002f..ceecc6149 100644
c7ffa4
--- a/lib/librte_vhost/socket.c
c7ffa4
+++ b/lib/librte_vhost/socket.c
c7ffa4
@@ -69,6 +69,7 @@ struct vhost_user_socket {
c7ffa4
 	bool reconnect;
c7ffa4
 	bool dequeue_zero_copy;
c7ffa4
 	bool iommu_support;
c7ffa4
+	bool use_builtin_virtio_net;
c7ffa4
 
c7ffa4
 	/*
c7ffa4
 	 * The "supported_features" indicates the feature bits the
c7ffa4
@@ -224,6 +225,8 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
c7ffa4
 	size = strnlen(vsocket->path, PATH_MAX);
c7ffa4
 	vhost_set_ifname(vid, vsocket->path, size);
c7ffa4
 
c7ffa4
+	vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
c7ffa4
+
c7ffa4
 	if (vsocket->dequeue_zero_copy)
c7ffa4
 		vhost_enable_dequeue_zero_copy(vid);
c7ffa4
 
c7ffa4
@@ -547,6 +550,12 @@ rte_vhost_driver_disable_features(const char *path, uint64_t features)
c7ffa4
 
c7ffa4
 	pthread_mutex_lock(&vhost_user.mutex);
c7ffa4
 	vsocket = find_vhost_user_socket(path);
c7ffa4
+
c7ffa4
+	/* Note that use_builtin_virtio_net is not affected by this function
c7ffa4
+	 * since callers may want to selectively disable features of the
c7ffa4
+	 * built-in vhost net device backend.
c7ffa4
+	 */
c7ffa4
+
c7ffa4
 	if (vsocket)
c7ffa4
 		vsocket->features &= ~features;
c7ffa4
 	pthread_mutex_unlock(&vhost_user.mutex);
c7ffa4
@@ -587,6 +596,11 @@ rte_vhost_driver_set_features(const char *path, uint64_t features)
c7ffa4
 	if (vsocket) {
c7ffa4
 		vsocket->supported_features = features;
c7ffa4
 		vsocket->features = features;
c7ffa4
+
c7ffa4
+		/* Anyone setting feature bits is implementing their own vhost
c7ffa4
+		 * device backend.
c7ffa4
+		 */
c7ffa4
+		vsocket->use_builtin_virtio_net = false;
c7ffa4
 	}
c7ffa4
 	pthread_mutex_unlock(&vhost_user.mutex);
c7ffa4
 
c7ffa4
@@ -667,6 +681,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
c7ffa4
 	 * rte_vhost_driver_set_features(), which will overwrite following
c7ffa4
 	 * two values.
c7ffa4
 	 */
c7ffa4
+	vsocket->use_builtin_virtio_net = true;
c7ffa4
 	vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
c7ffa4
 	vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
c7ffa4
 
c7ffa4
diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
c7ffa4
index df528a4ea..75deaa877 100644
c7ffa4
--- a/lib/librte_vhost/vhost.c
c7ffa4
+++ b/lib/librte_vhost/vhost.c
c7ffa4
@@ -279,7 +279,7 @@ reset_device(struct virtio_net *dev)
c7ffa4
 
c7ffa4
 	dev->features = 0;
c7ffa4
 	dev->protocol_features = 0;
c7ffa4
-	dev->flags = 0;
c7ffa4
+	dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
c7ffa4
 
c7ffa4
 	for (i = 0; i < dev->nr_vring; i++)
c7ffa4
 		reset_vring_queue(dev, i);
c7ffa4
@@ -315,6 +315,7 @@ vhost_new_device(void)
c7ffa4
 
c7ffa4
 	vhost_devices[i] = dev;
c7ffa4
 	dev->vid = i;
c7ffa4
+	dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
c7ffa4
 	dev->slave_req_fd = -1;
c7ffa4
 
c7ffa4
 	return i;
c7ffa4
@@ -371,6 +372,20 @@ vhost_enable_dequeue_zero_copy(int vid)
c7ffa4
 	dev->dequeue_zero_copy = 1;
c7ffa4
 }
c7ffa4
 
c7ffa4
+void
c7ffa4
+vhost_set_builtin_virtio_net(int vid, bool enable)
c7ffa4
+{
c7ffa4
+	struct virtio_net *dev = get_device(vid);
c7ffa4
+
c7ffa4
+	if (dev == NULL)
c7ffa4
+		return;
c7ffa4
+
c7ffa4
+	if (enable)
c7ffa4
+		dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
c7ffa4
+	else
c7ffa4
+		dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
c7ffa4
+}
c7ffa4
+
c7ffa4
 int
c7ffa4
 rte_vhost_get_mtu(int vid, uint16_t *mtu)
c7ffa4
 {
c7ffa4
diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
c7ffa4
index 9cad1bb3c..e06531e6b 100644
c7ffa4
--- a/lib/librte_vhost/vhost.h
c7ffa4
+++ b/lib/librte_vhost/vhost.h
c7ffa4
@@ -53,6 +53,8 @@
c7ffa4
 #define VIRTIO_DEV_RUNNING 1
c7ffa4
 /* Used to indicate that the device is ready to operate */
c7ffa4
 #define VIRTIO_DEV_READY 2
c7ffa4
+/* Used to indicate that the built-in vhost net device backend is enabled */
c7ffa4
+#define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
c7ffa4
 
c7ffa4
 /* Backend value set by guest. */
c7ffa4
 #define VIRTIO_DEV_STOPPED -1
c7ffa4
@@ -371,6 +373,7 @@ int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
c7ffa4
 
c7ffa4
 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
c7ffa4
 void vhost_enable_dequeue_zero_copy(int vid);
c7ffa4
+void vhost_set_builtin_virtio_net(int vid, bool enable);
c7ffa4
 
c7ffa4
 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
c7ffa4
 
c7ffa4
diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
c7ffa4
index 6fee16e55..3bfd71945 100644
c7ffa4
--- a/lib/librte_vhost/virtio_net.c
c7ffa4
+++ b/lib/librte_vhost/virtio_net.c
c7ffa4
@@ -727,6 +727,13 @@ rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
c7ffa4
 	if (!dev)
c7ffa4
 		return 0;
c7ffa4
 
c7ffa4
+	if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
c7ffa4
+		RTE_LOG(ERR, VHOST_DATA,
c7ffa4
+			"(%d) %s: built-in vhost net backend is disabled.\n",
c7ffa4
+			dev->vid, __func__);
c7ffa4
+		return 0;
c7ffa4
+	}
c7ffa4
+
c7ffa4
 	if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
c7ffa4
 		return virtio_dev_merge_rx(dev, queue_id, pkts, count);
c7ffa4
 	else
c7ffa4
@@ -1173,6 +1180,13 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
c7ffa4
 	if (!dev)
c7ffa4
 		return 0;
c7ffa4
 
c7ffa4
+	if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
c7ffa4
+		RTE_LOG(ERR, VHOST_DATA,
c7ffa4
+			"(%d) %s: built-in vhost net backend is disabled.\n",
c7ffa4
+			dev->vid, __func__);
c7ffa4
+		return 0;
c7ffa4
+	}
c7ffa4
+
c7ffa4
 	if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
c7ffa4
 		RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
c7ffa4
 			dev->vid, __func__, queue_id);
c7ffa4
-- 
c7ffa4
2.14.3
c7ffa4