[dpdk-dev] [PATCH v4 06/12] vhost: support protocol feature

Ouyang Changchun changchun.ouyang at intel.com
Wed Aug 12 10:02:41 CEST 2015


Support new protocol feature to communicate with qemu:
Add set and get protocol feature bits;
Add VRING_FLAG for mq feature to set vring flag, which
indicates the vq is enabled or disabled.

Reserve values as follows:
VHOST_USER_SEND_RARP = 17 (merge from qemu community)
VHOST_USER_SET_VRING_FLAG = 18 (reserve for vhost mq)

These reservation need sync up with qemu community before finalizing.

Signed-off-by: Changchun Ouyang <changchun.ouyang at intel.com>
---
This is added since v4.

 lib/librte_vhost/rte_virtio_net.h             |  2 +
 lib/librte_vhost/vhost-net.h                  |  3 ++
 lib/librte_vhost/vhost_rxtx.c                 | 21 ++++++++++
 lib/librte_vhost/vhost_user/vhost-net-user.c  | 21 +++++++++-
 lib/librte_vhost/vhost_user/vhost-net-user.h  |  4 ++
 lib/librte_vhost/vhost_user/virtio-net-user.c | 29 ++++++++++++++
 lib/librte_vhost/vhost_user/virtio-net-user.h |  2 +
 lib/librte_vhost/virtio-net.c                 | 56 ++++++++++++++++++++++++++-
 lib/librte_vhost/virtio-net.h                 |  2 +
 9 files changed, 138 insertions(+), 2 deletions(-)

diff --git a/lib/librte_vhost/rte_virtio_net.h b/lib/librte_vhost/rte_virtio_net.h
index 8520d96..e16ad3a 100644
--- a/lib/librte_vhost/rte_virtio_net.h
+++ b/lib/librte_vhost/rte_virtio_net.h
@@ -88,6 +88,7 @@ struct vhost_virtqueue {
 	volatile uint16_t	last_used_idx_res;	/**< Used for multiple devices reserving buffers. */
 	eventfd_t		callfd;			/**< Used to notify the guest (trigger interrupt). */
 	eventfd_t		kickfd;			/**< Currently unused as polling mode is enabled. */
+	uint32_t		enabled;		/**< Indicate the queue is enabled or not. */
 	struct buf_vector	buf_vec[BUF_VECTOR_MAX];	/**< for scatter RX. */
 } __rte_cache_aligned;
 
@@ -98,6 +99,7 @@ struct virtio_net {
 	struct vhost_virtqueue	**virtqueue;    /**< Contains all virtqueue information. */
 	struct virtio_memory    **mem_arr;      /**< Array for QEMU memory and memory region information. */
 	uint64_t		features;	/**< Negotiated feature set. */
+	uint64_t		protocol_features;	/**< Negotiated protocol feature set. */
 	uint64_t		device_fh;	/**< device identifier. */
 	uint32_t		flags;		/**< Device flags. Only used to check if device is running on data core. */
 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
diff --git a/lib/librte_vhost/vhost-net.h b/lib/librte_vhost/vhost-net.h
index 7dff14d..bc88bad 100644
--- a/lib/librte_vhost/vhost-net.h
+++ b/lib/librte_vhost/vhost-net.h
@@ -99,6 +99,9 @@ struct vhost_net_device_ops {
 	int (*get_features)(struct vhost_device_ctx, uint64_t *);
 	int (*set_features)(struct vhost_device_ctx, uint64_t *);
 
+	int (*get_protocol_features)(struct vhost_device_ctx, uint64_t *);
+	int (*set_protocol_features)(struct vhost_device_ctx, uint64_t *);
+
 	int (*set_vring_num)(struct vhost_device_ctx, struct vhost_vring_state *);
 	int (*set_vring_addr)(struct vhost_device_ctx, struct vhost_vring_addr *);
 	int (*set_vring_base)(struct vhost_device_ctx, struct vhost_vring_state *);
diff --git a/lib/librte_vhost/vhost_rxtx.c b/lib/librte_vhost/vhost_rxtx.c
index a60b542..3af0326 100644
--- a/lib/librte_vhost/vhost_rxtx.c
+++ b/lib/librte_vhost/vhost_rxtx.c
@@ -89,6 +89,14 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 	}
 
 	vq = dev->virtqueue[queue_id];
+
+	if (unlikely(vq->enabled == 0)) {
+		RTE_LOG(ERR, VHOST_DATA,
+			"%s (%"PRIu64"): virtqueue idx:%d not enabled.\n",
+			 __func__, dev->device_fh, queue_id);
+		return 0;
+	}
+
 	count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
 
 	/*
@@ -281,6 +289,7 @@ copy_from_mbuf_to_vring(struct virtio_net *dev, uint16_t queue_id,
 	 * (guest physical addr -> vhost virtual addr)
 	 */
 	vq = dev->virtqueue[queue_id];
+
 	vb_addr = gpa_to_vva(dev, queue_id / VIRTIO_QNUM,
 			vq->buf_vec[vec_idx].buf_addr);
 	vb_hdr_addr = vb_addr;
@@ -491,6 +500,14 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 	}
 
 	vq = dev->virtqueue[queue_id];
+
+	if (unlikely(vq->enabled == 0)) {
+		RTE_LOG(ERR, VHOST_DATA,
+			"%s (%"PRIu64"): virtqueue idx:%d not enabled.\n",
+			 __func__, dev->device_fh, queue_id);
+		return 0;
+	}
+
 	count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
 
 	if (count == 0)
@@ -590,6 +607,10 @@ rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
 	}
 
 	vq = dev->virtqueue[queue_id];
+
+	if (unlikely(vq->enabled == 0))
+		return 0;
+
 	avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
 
 	/* If there are no available buffers then return. */
diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.c b/lib/librte_vhost/vhost_user/vhost-net-user.c
index e926ed7..f7a24e9 100644
--- a/lib/librte_vhost/vhost_user/vhost-net-user.c
+++ b/lib/librte_vhost/vhost_user/vhost-net-user.c
@@ -95,7 +95,11 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
 	[VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
 	[VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
 	[VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
-	[VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR"
+	[VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
+	[VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
+	[VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
+	[VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
+	[VHOST_USER_SET_VRING_FLAG]  = "VHOST_USER_SET_VRING_FLAG"
 };
 
 /**
@@ -379,6 +383,17 @@ vserver_message_handler(int connfd, void *dat, int *remove)
 		ops->set_features(ctx, &features);
 		break;
 
+	case VHOST_USER_GET_PROTOCOL_FEATURES:
+		ret = ops->get_protocol_features(ctx, &features);
+		msg.payload.u64 = features;
+		msg.size = sizeof(msg.payload.u64);
+		send_vhost_message(connfd, &msg);
+		break;
+	case VHOST_USER_SET_PROTOCOL_FEATURES:
+		features = msg.payload.u64;
+		ops->set_protocol_features(ctx, &features);
+		break;
+
 	case VHOST_USER_SET_OWNER:
 		ops->set_owner(ctx);
 		break;
@@ -424,6 +439,10 @@ vserver_message_handler(int connfd, void *dat, int *remove)
 		user_set_vring_call(ctx, &msg);
 		break;
 
+	case VHOST_USER_SET_VRING_FLAG:
+		user_set_vring_flag(ctx, &msg.payload.state);
+		break;
+
 	case VHOST_USER_SET_VRING_ERR:
 		if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
 			close(msg.fds[0]);
diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.h b/lib/librte_vhost/vhost_user/vhost-net-user.h
index 2e72f3c..54e95aa 100644
--- a/lib/librte_vhost/vhost_user/vhost-net-user.h
+++ b/lib/librte_vhost/vhost_user/vhost-net-user.h
@@ -63,6 +63,10 @@ typedef enum VhostUserRequest {
 	VHOST_USER_SET_VRING_KICK = 12,
 	VHOST_USER_SET_VRING_CALL = 13,
 	VHOST_USER_SET_VRING_ERR = 14,
+	VHOST_USER_GET_PROTOCOL_FEATURES = 15,
+	VHOST_USER_SET_PROTOCOL_FEATURES = 16,
+	VHOST_USER_SEND_RARP = 17,
+	VHOST_USER_SET_VRING_FLAG = 18,
 	VHOST_USER_MAX
 } VhostUserRequest;
 
diff --git a/lib/librte_vhost/vhost_user/virtio-net-user.c b/lib/librte_vhost/vhost_user/virtio-net-user.c
index d749f27..6a12d96 100644
--- a/lib/librte_vhost/vhost_user/virtio-net-user.c
+++ b/lib/librte_vhost/vhost_user/virtio-net-user.c
@@ -229,6 +229,13 @@ virtio_is_ready(struct virtio_net *dev)
 				"virtio isn't ready for processing.\n");
 			return 0;
 		}
+		if ((dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_VRING_FLAG)) == 0) {
+			/* Without VRING_FLAG feature, only 1 vq pair is supported */
+			if (q_idx == 0) {
+				rvq->enabled = 1;
+				tvq->enabled = 1;
+			}
+		}
 	}
 	RTE_LOG(INFO, VHOST_CONFIG,
 		"virtio is now ready for processing.\n");
@@ -343,6 +350,28 @@ user_reset_owner(struct vhost_device_ctx ctx,
 	return 0;
 }
 
+/*
+ * when virtio queues are ready to work, qemu will send us to enable the virtio queue pair.
+ */
+int
+user_set_vring_flag(struct vhost_device_ctx ctx,
+	struct vhost_vring_state *state)
+{
+	struct virtio_net *dev = get_device(ctx);
+
+	RTE_LOG(INFO, VHOST_CONFIG,
+		"set queue enable --- state idx:%d state num:%d\n", state->index, state->num);
+
+	/*
+	 * The state->index indicate the qeueu pair index,
+	 * need set for both Rx and Tx.
+	 */
+	dev->virtqueue[state->index * VIRTIO_QNUM + VIRTIO_RXQ]->enabled = state->num;
+	dev->virtqueue[state->index * VIRTIO_QNUM + VIRTIO_TXQ]->enabled = state->num;
+
+	return 0;
+}
+
 void
 user_destroy_device(struct vhost_device_ctx ctx)
 {
diff --git a/lib/librte_vhost/vhost_user/virtio-net-user.h b/lib/librte_vhost/vhost_user/virtio-net-user.h
index 2429836..10a3fff 100644
--- a/lib/librte_vhost/vhost_user/virtio-net-user.h
+++ b/lib/librte_vhost/vhost_user/virtio-net-user.h
@@ -45,6 +45,8 @@ void user_set_vring_kick(struct vhost_device_ctx, struct VhostUserMsg *);
 
 int user_get_vring_base(struct vhost_device_ctx, struct vhost_vring_state *);
 
+int user_set_vring_flag(struct vhost_device_ctx ctx, struct vhost_vring_state *state);
+
 void user_destroy_device(struct vhost_device_ctx);
 
 int user_reset_owner(struct vhost_device_ctx ctx, struct vhost_vring_state *state);
diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c
index 8901aa5..24d0c53 100644
--- a/lib/librte_vhost/virtio-net.c
+++ b/lib/librte_vhost/virtio-net.c
@@ -67,15 +67,23 @@ struct virtio_net_device_ops const *notify_ops;
 /* root address of the linked list of managed virtio devices */
 static struct virtio_net_config_ll *ll_root;
 
+#define VHOST_USER_F_PROTOCOL_FEATURES 30
+
 /* Features supported by this lib. */
 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
 				(1ULL << VIRTIO_NET_F_CTRL_VQ) | \
 				(1ULL << VIRTIO_NET_F_CTRL_RX) | \
 				(1ULL << VHOST_F_LOG_ALL) | \
-				(1ULL << VIRTIO_NET_F_MQ))
+				(1ULL << VIRTIO_NET_F_MQ) | \
+				(1ULL << VHOST_USER_F_PROTOCOL_FEATURES))
 
 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
 
+/* Protocol features supported by this lib. */
+#define VHOST_SUPPORTED_PROTOCOL_FEATURES ((1ULL << VHOST_USER_PROTOCOL_F_VRING_FLAG))
+
+static uint64_t VHOST_PROTOCOL_FEATURES = VHOST_SUPPORTED_PROTOCOL_FEATURES;
+
 /*
  * Converts QEMU virtual address to Vhost virtual address. This function is
  * used to convert the ring addresses to our address space.
@@ -533,6 +541,45 @@ set_features(struct vhost_device_ctx ctx, uint64_t *pu)
 }
 
 /*
+ * Called from VHOST-USER SOCKET: VHOST_GET_PROTOCOL_FEATURES
+ * The features that we support are requested.
+ */
+static int
+get_protocol_features(struct vhost_device_ctx ctx, uint64_t *pu)
+{
+	struct virtio_net *dev;
+
+	dev = get_device(ctx);
+	if (dev == NULL)
+		return -1;
+
+	/* Send our supported features. */
+	*pu = VHOST_PROTOCOL_FEATURES;
+	return 0;
+}
+
+/*
+ * Called from VHOST-USER SOCKET: VHOST_SET_PROTOCOL_FEATURES
+ * We receive the negotiated features supported by us and the virtio device.
+ */
+static int
+set_protocol_features(struct vhost_device_ctx ctx, uint64_t *pu)
+{
+	struct virtio_net *dev;
+
+	dev = get_device(ctx);
+	if (dev == NULL)
+		return -1;
+	if (*pu & ~VHOST_PROTOCOL_FEATURES)
+		return -1;
+
+	/* Store the negotiated feature list for the device. */
+	dev->protocol_features = *pu;
+
+	return 0;
+}
+
+/*
  * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
  * The virtio device sends us the size of the descriptor ring.
  */
@@ -824,6 +871,10 @@ set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
 	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
 		if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
 			((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
+			if ((dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_VRING_FLAG)) == 0) {
+				dev->virtqueue[VIRTIO_RXQ]->enabled = 1;
+				dev->virtqueue[VIRTIO_TXQ]->enabled = 1;
+			}
 			return notify_ops->new_device(dev);
 		}
 	/* Otherwise we remove it. */
@@ -846,6 +897,9 @@ static const struct vhost_net_device_ops vhost_device_ops = {
 	.get_features = get_features,
 	.set_features = set_features,
 
+	.get_protocol_features = get_protocol_features,
+	.set_protocol_features = set_protocol_features,
+
 	.set_vring_num = set_vring_num,
 	.set_vring_addr = set_vring_addr,
 	.set_vring_base = set_vring_base,
diff --git a/lib/librte_vhost/virtio-net.h b/lib/librte_vhost/virtio-net.h
index 75fb57e..ef6efae 100644
--- a/lib/librte_vhost/virtio-net.h
+++ b/lib/librte_vhost/virtio-net.h
@@ -37,6 +37,8 @@
 #include "vhost-net.h"
 #include "rte_virtio_net.h"
 
+#define VHOST_USER_PROTOCOL_F_VRING_FLAG 2
+
 struct virtio_net_device_ops const *notify_ops;
 struct virtio_net *get_device(struct vhost_device_ctx ctx);
 
-- 
1.8.4.2



More information about the dev mailing list