Blame SOURCES/0015-net-virtio-user-support-control-VQ-for-packed.patch

2c1bf6
From 74bbcd238093edc81b1a1f0b9b6e0d3c3fe32584 Mon Sep 17 00:00:00 2001
2c1bf6
From: Jens Freimann <jfreimann@redhat.com>
2c1bf6
Date: Fri, 11 Jan 2019 10:39:29 +0100
2c1bf6
Subject: [PATCH] net/virtio-user: support control VQ for packed
2c1bf6
2c1bf6
[ upstream commit 48a4464029a7f76dfb2c1f09146a391917b075e5 ]
2c1bf6
2c1bf6
Add support to virtio-user for control virtqueues.
2c1bf6
2c1bf6
Signed-off-by: Jens Freimann <jfreimann@redhat.com>
2c1bf6
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
2c1bf6
(cherry picked from commit 48a4464029a7f76dfb2c1f09146a391917b075e5)
2c1bf6
Signed-off-by: Jens Freimann <jfreimann@redhat.com>
2c1bf6
---
2c1bf6
 .../net/virtio/virtio_user/virtio_user_dev.c  | 102 ++++++++++++++++--
2c1bf6
 .../net/virtio/virtio_user/virtio_user_dev.h  |  15 ++-
2c1bf6
 drivers/net/virtio/virtio_user_ethdev.c       |  56 +++++++++-
2c1bf6
 3 files changed, 157 insertions(+), 16 deletions(-)
2c1bf6
2c1bf6
diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c
2c1bf6
index 2caaaad5f..83d3fb531 100644
2c1bf6
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
2c1bf6
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
2c1bf6
@@ -43,15 +43,26 @@ virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
2c1bf6
 	struct vhost_vring_file file;
2c1bf6
 	struct vhost_vring_state state;
2c1bf6
 	struct vring *vring = &dev->vrings[queue_sel];
2c1bf6
+	struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
2c1bf6
 	struct vhost_vring_addr addr = {
2c1bf6
 		.index = queue_sel,
2c1bf6
-		.desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
2c1bf6
-		.avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
2c1bf6
-		.used_user_addr = (uint64_t)(uintptr_t)vring->used,
2c1bf6
 		.log_guest_addr = 0,
2c1bf6
 		.flags = 0, /* disable log */
2c1bf6
 	};
2c1bf6
 
2c1bf6
+	if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
2c1bf6
+		addr.desc_user_addr =
2c1bf6
+			(uint64_t)(uintptr_t)pq_vring->desc_packed;
2c1bf6
+		addr.avail_user_addr =
2c1bf6
+			(uint64_t)(uintptr_t)pq_vring->driver_event;
2c1bf6
+		addr.used_user_addr =
2c1bf6
+			(uint64_t)(uintptr_t)pq_vring->device_event;
2c1bf6
+	} else {
2c1bf6
+		addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
2c1bf6
+		addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
2c1bf6
+		addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
2c1bf6
+	}
2c1bf6
+
2c1bf6
 	state.index = queue_sel;
2c1bf6
 	state.num = vring->num;
2c1bf6
 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
2c1bf6
@@ -468,15 +479,8 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
2c1bf6
 	if (!in_order)
2c1bf6
 		dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
2c1bf6
 
2c1bf6
-	if (packed_vq) {
2c1bf6
-		if (cq) {
2c1bf6
-			PMD_INIT_LOG(ERR, "control vq not supported yet with "
2c1bf6
-					  "packed virtqueues\n");
2c1bf6
-			return -1;
2c1bf6
-		}
2c1bf6
-	} else {
2c1bf6
+	if (!packed_vq)
2c1bf6
 		dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
2c1bf6
-	}
2c1bf6
 
2c1bf6
 	if (dev->mac_specified)
2c1bf6
 		dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
2c1bf6
@@ -621,6 +625,82 @@ virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
2c1bf6
 	return n_descs;
2c1bf6
 }
2c1bf6
 
2c1bf6
+static inline int
2c1bf6
+desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
2c1bf6
+{
2c1bf6
+	return wrap_counter == !!(desc->flags & VRING_DESC_F_AVAIL(1)) &&
2c1bf6
+		wrap_counter != !!(desc->flags & VRING_DESC_F_USED(1));
2c1bf6
+}
2c1bf6
+
2c1bf6
+static uint32_t
2c1bf6
+virtio_user_handle_ctrl_msg_pq(struct virtio_user_dev *dev,
2c1bf6
+			    struct vring_packed *vring,
2c1bf6
+			    uint16_t idx_hdr)
2c1bf6
+{
2c1bf6
+	struct virtio_net_ctrl_hdr *hdr;
2c1bf6
+	virtio_net_ctrl_ack status = ~0;
2c1bf6
+	uint16_t idx_data, idx_status;
2c1bf6
+	/* initialize to one, header is first */
2c1bf6
+	uint32_t n_descs = 1;
2c1bf6
+
2c1bf6
+	/* locate desc for header, data, and status */
2c1bf6
+	idx_data = idx_hdr + 1;
2c1bf6
+	if (idx_data >= dev->queue_size)
2c1bf6
+		idx_data -= dev->queue_size;
2c1bf6
+
2c1bf6
+	n_descs++;
2c1bf6
+
2c1bf6
+	idx_status = idx_data;
2c1bf6
+	while (vring->desc_packed[idx_status].flags & VRING_DESC_F_NEXT) {
2c1bf6
+		idx_status++;
2c1bf6
+		if (idx_status >= dev->queue_size)
2c1bf6
+			idx_status -= dev->queue_size;
2c1bf6
+		n_descs++;
2c1bf6
+	}
2c1bf6
+
2c1bf6
+	hdr = (void *)(uintptr_t)vring->desc_packed[idx_hdr].addr;
2c1bf6
+	if (hdr->class == VIRTIO_NET_CTRL_MQ &&
2c1bf6
+	    hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
2c1bf6
+		uint16_t queues;
2c1bf6
+
2c1bf6
+		queues = *(uint16_t *)(uintptr_t)
2c1bf6
+				vring->desc_packed[idx_data].addr;
2c1bf6
+		status = virtio_user_handle_mq(dev, queues);
2c1bf6
+	}
2c1bf6
+
2c1bf6
+	/* Update status */
2c1bf6
+	*(virtio_net_ctrl_ack *)(uintptr_t)
2c1bf6
+		vring->desc_packed[idx_status].addr = status;
2c1bf6
+
2c1bf6
+	return n_descs;
2c1bf6
+}
2c1bf6
+
2c1bf6
+void
2c1bf6
+virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
2c1bf6
+{
2c1bf6
+	struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
2c1bf6
+	struct vring_packed *vring = &dev->packed_vrings[queue_idx];
2c1bf6
+	uint16_t id, n_descs;
2c1bf6
+
2c1bf6
+	while (desc_is_avail(&vring->desc_packed[vq->used_idx],
2c1bf6
+			     vq->used_wrap_counter)) {
2c1bf6
+		id = vring->desc_packed[vq->used_idx].id;
2c1bf6
+
2c1bf6
+		n_descs = virtio_user_handle_ctrl_msg_pq(dev, vring, id);
2c1bf6
+
2c1bf6
+		do {
2c1bf6
+			vring->desc_packed[vq->used_idx].flags =
2c1bf6
+				VRING_DESC_F_AVAIL(vq->used_wrap_counter) |
2c1bf6
+				VRING_DESC_F_USED(vq->used_wrap_counter);
2c1bf6
+			if (++vq->used_idx >= dev->queue_size) {
2c1bf6
+				vq->used_idx -= dev->queue_size;
2c1bf6
+				vq->used_wrap_counter ^= 1;
2c1bf6
+			}
2c1bf6
+			n_descs--;
2c1bf6
+		} while (n_descs);
2c1bf6
+	}
2c1bf6
+}
2c1bf6
+
2c1bf6
 void
2c1bf6
 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
2c1bf6
 {
2c1bf6
diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.h b/drivers/net/virtio/virtio_user/virtio_user_dev.h
2c1bf6
index 67a9c01ac..c6c2f7d6e 100644
2c1bf6
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.h
2c1bf6
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.h
2c1bf6
@@ -11,6 +11,12 @@
2c1bf6
 #include "../virtio_ring.h"
2c1bf6
 #include "vhost.h"
2c1bf6
 
2c1bf6
+struct virtio_user_queue {
2c1bf6
+	uint16_t used_idx;
2c1bf6
+	bool avail_wrap_counter;
2c1bf6
+	bool used_wrap_counter;
2c1bf6
+};
2c1bf6
+
2c1bf6
 struct virtio_user_dev {
2c1bf6
 	/* for vhost_user backend */
2c1bf6
 	int		vhostfd;
2c1bf6
@@ -39,7 +45,12 @@ struct virtio_user_dev {
2c1bf6
 	uint16_t	port_id;
2c1bf6
 	uint8_t		mac_addr[ETHER_ADDR_LEN];
2c1bf6
 	char		path[PATH_MAX];
2c1bf6
-	struct vring	vrings[VIRTIO_MAX_VIRTQUEUES];
2c1bf6
+	union {
2c1bf6
+		struct vring		vrings[VIRTIO_MAX_VIRTQUEUES];
2c1bf6
+		struct vring_packed	packed_vrings[VIRTIO_MAX_VIRTQUEUES];
2c1bf6
+	};
2c1bf6
+	struct virtio_user_queue packed_queues[VIRTIO_MAX_VIRTQUEUES];
2c1bf6
+
2c1bf6
 	struct virtio_user_backend_ops *ops;
2c1bf6
 	pthread_mutex_t	mutex;
2c1bf6
 	bool		started;
2c1bf6
@@ -53,5 +64,7 @@ int virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
2c1bf6
 			 int server, int mrg_rxbuf, int in_order, int packed_vq);
2c1bf6
 void virtio_user_dev_uninit(struct virtio_user_dev *dev);
2c1bf6
 void virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx);
2c1bf6
+void virtio_user_handle_cq_packed(struct virtio_user_dev *dev,
2c1bf6
+				  uint16_t queue_idx);
2c1bf6
 uint8_t virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs);
2c1bf6
 #endif
2c1bf6
diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c
2c1bf6
index a2911febf..dddb7dd23 100644
2c1bf6
--- a/drivers/net/virtio/virtio_user_ethdev.c
2c1bf6
+++ b/drivers/net/virtio/virtio_user_ethdev.c
2c1bf6
@@ -271,10 +271,44 @@ virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
2c1bf6
 	return dev->queue_size;
2c1bf6
 }
2c1bf6
 
2c1bf6
-static int
2c1bf6
-virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
2c1bf6
+static void
2c1bf6
+virtio_user_setup_queue_packed(struct virtqueue *vq,
2c1bf6
+			       struct virtio_user_dev *dev)
2c1bf6
+
2c1bf6
+{
2c1bf6
+	uint16_t queue_idx = vq->vq_queue_index;
2c1bf6
+	struct vring_packed *vring;
2c1bf6
+	uint64_t desc_addr;
2c1bf6
+	uint64_t avail_addr;
2c1bf6
+	uint64_t used_addr;
2c1bf6
+	uint16_t i;
2c1bf6
+
2c1bf6
+	vring  = &dev->packed_vrings[queue_idx];
2c1bf6
+	desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
2c1bf6
+	avail_addr = desc_addr + vq->vq_nentries *
2c1bf6
+		sizeof(struct vring_packed_desc);
2c1bf6
+	used_addr = RTE_ALIGN_CEIL(avail_addr +
2c1bf6
+			   sizeof(struct vring_packed_desc_event),
2c1bf6
+			   VIRTIO_PCI_VRING_ALIGN);
2c1bf6
+	vring->num = vq->vq_nentries;
2c1bf6
+	vring->desc_packed =
2c1bf6
+		(void *)(uintptr_t)desc_addr;
2c1bf6
+	vring->driver_event =
2c1bf6
+		(void *)(uintptr_t)avail_addr;
2c1bf6
+	vring->device_event =
2c1bf6
+		(void *)(uintptr_t)used_addr;
2c1bf6
+	dev->packed_queues[queue_idx].avail_wrap_counter = true;
2c1bf6
+	dev->packed_queues[queue_idx].used_wrap_counter = true;
2c1bf6
+
2c1bf6
+	for (i = 0; i < vring->num; i++) {
2c1bf6
+		vring->desc_packed[i].flags = VRING_DESC_F_USED(1) |
2c1bf6
+					      VRING_DESC_F_AVAIL(1);
2c1bf6
+	}
2c1bf6
+}
2c1bf6
+
2c1bf6
+static void
2c1bf6
+virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
2c1bf6
 {
2c1bf6
-	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
2c1bf6
 	uint16_t queue_idx = vq->vq_queue_index;
2c1bf6
 	uint64_t desc_addr, avail_addr, used_addr;
2c1bf6
 
2c1bf6
@@ -288,6 +322,17 @@ virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
2c1bf6
 	dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
2c1bf6
 	dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
2c1bf6
 	dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
2c1bf6
+}
2c1bf6
+
2c1bf6
+static int
2c1bf6
+virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
2c1bf6
+{
2c1bf6
+	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
2c1bf6
+
2c1bf6
+	if (vtpci_packed_queue(hw))
2c1bf6
+		virtio_user_setup_queue_packed(vq, dev);
2c1bf6
+	else
2c1bf6
+		virtio_user_setup_queue_split(vq, dev);
2c1bf6
 
2c1bf6
 	return 0;
2c1bf6
 }
2c1bf6
@@ -317,7 +362,10 @@ virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
2c1bf6
 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
2c1bf6
 
2c1bf6
 	if (hw->cvq && (hw->cvq->vq == vq)) {
2c1bf6
-		virtio_user_handle_cq(dev, vq->vq_queue_index);
2c1bf6
+		if (vtpci_packed_queue(vq->hw))
2c1bf6
+			virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
2c1bf6
+		else
2c1bf6
+			virtio_user_handle_cq(dev, vq->vq_queue_index);
2c1bf6
 		return;
2c1bf6
 	}
2c1bf6
 
2c1bf6
-- 
2c1bf6
2.21.0
2c1bf6