[v7,14/15] vhost: add notification for packed ring

Message ID 20180704215438.5579-15-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series Vhost: add support to packed ring layout |

Checks

Context Check Description
ci/Intel-compilation fail Compilation issues

Commit Message

Maxime Coquelin July 4, 2018, 9:54 p.m. UTC
  Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost.c      | 70 +++++++++++++++++++++++++++++++++++++------
 lib/librte_vhost/vhost.h      | 63 ++++++++++++++++++++++++++++++++++++--
 lib/librte_vhost/vhost_user.c | 24 +++++++++++++++
 lib/librte_vhost/virtio_net.c | 12 ++++----
 4 files changed, 151 insertions(+), 18 deletions(-)
  

Comments

Tiwei Bie July 5, 2018, 5:12 a.m. UTC | #1
On Wed, Jul 04, 2018 at 11:54:37PM +0200, Maxime Coquelin wrote:
[...]
> @@ -225,6 +231,15 @@ struct vring_desc_packed {
>  	uint16_t index;
>  	uint16_t flags;
>  };
> +
> +#define VRING_EVENT_F_ENABLE 0x0
> +#define VRING_EVENT_F_DISABLE 0x1
> +#define VRING_EVENT_F_DESC 0x2
> +
> +struct vring_packed_desc_event {
> +	uint16_t desc_event_off_wrap;
> +	uint16_t desc_event_flags;
> +};

As all above types (including struct vring_desc_packed)
and macros are being protected by VIRTIO_F_RING_PACKED,
and they won't be defined if VIRTIO_F_RING_PACKED is
defined in kernel header. We may want to unify the names.

For the types, we may have below types defined in
linux uapi:

struct vring_packed;
struct vring_packed_desc;
struct vring_packed_desc_event;

They can also be named as:

struct vring_packed;
struct vring_desc_packed;
struct vring_packed_desc_event;

We need to choose one of them or something else.

For the `struct vring_packed_desc_event`, it can
be defined as:

struct vring_packed_desc_event {
	uint16_t off_wrap;
	uint16_t flags;
};

or

struct vring_packed_desc_event {
	uint16_t desc_event_off_wrap;
	uint16_t desc_event_flags;
};

We need to choose one of them or something else.

For the `struct vring_packed_desc`, it can be
defined as:

struct vring_packed_desc {
	uint64_t addr;
	uint32_t len;
	uint16_t index;
	uint16_t flags;
};

or

struct vring_packed_desc {
	uint64_t addr;
	uint32_t len;
	uint16_t id;		// index -> id
	uint16_t flags;
};

We need to choose one of them or something else.


>  #endif
>  
[...]
> +static __rte_always_inline void
> +vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
> +{
> +	uint16_t old, new, off, off_wrap;
> +	bool kick = false;
> +
> +	/* Flush used desc update. */
> +	rte_smp_mb();
> +
> +	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
> +		if (vq->driver_event->desc_event_flags !=
> +				VRING_EVENT_F_DISABLE)
> +			kick = true;
> +		goto kick;
> +	}
> +
> +	old = vq->signalled_used;

We also need to check whether vq->signalled_used is valid?

> +	new = vq->last_used_idx;
> +	vq->signalled_used = new;
> +
> +	if (vq->driver_event->desc_event_flags != VRING_EVENT_F_DESC) {
> +		if (vq->driver_event->desc_event_flags !=
> +				VRING_EVENT_F_DISABLE)
> +			kick = true;
> +		goto kick;
> +	}
> +
> +	rte_smp_rmb();
> +
> +	off_wrap = vq->driver_event->desc_event_off_wrap;
> +	off = off_wrap & ~(1 << 15);
> +
> +	if (vq->used_wrap_counter != off_wrap >> 15)
> +		off -= vq->size;
> +
> +	if (vhost_need_event(off, new, old))
> +		kick = true;

If new <= old, old needs to -= vq->size?

> +kick:
> +	if (kick)
> +		eventfd_write(vq->callfd, (eventfd_t)1);
> +}
> +
[...]
  
Maxime Coquelin July 5, 2018, 7:20 a.m. UTC | #2
On 07/05/2018 07:12 AM, Tiwei Bie wrote:
> On Wed, Jul 04, 2018 at 11:54:37PM +0200, Maxime Coquelin wrote:
> [...]
>> @@ -225,6 +231,15 @@ struct vring_desc_packed {
>>   	uint16_t index;
>>   	uint16_t flags;
>>   };
>> +
>> +#define VRING_EVENT_F_ENABLE 0x0
>> +#define VRING_EVENT_F_DISABLE 0x1
>> +#define VRING_EVENT_F_DESC 0x2
>> +
>> +struct vring_packed_desc_event {
>> +	uint16_t desc_event_off_wrap;
>> +	uint16_t desc_event_flags;
>> +};
> 
> As all above types (including struct vring_desc_packed)
> and macros are being protected by VIRTIO_F_RING_PACKED,
> and they won't be defined if VIRTIO_F_RING_PACKED is
> defined in kernel header. We may want to unify the names.
> 
> For the types, we may have below types defined in
> linux uapi:
> 
> struct vring_packed;
> struct vring_packed_desc;
> struct vring_packed_desc_event;
> 
> They can also be named as:
> 
> struct vring_packed;
> struct vring_desc_packed;
> struct vring_packed_desc_event;
> 
> We need to choose one of them or something else.
> 
> For the `struct vring_packed_desc_event`, it can
> be defined as:
> 
> struct vring_packed_desc_event {
> 	uint16_t off_wrap;
> 	uint16_t flags;
> };
> 
> or
> 
> struct vring_packed_desc_event {
> 	uint16_t desc_event_off_wrap;
> 	uint16_t desc_event_flags;
> };
> 
> We need to choose one of them or something else.
> 
> For the `struct vring_packed_desc`, it can be
> defined as:
> 
> struct vring_packed_desc {
> 	uint64_t addr;
> 	uint32_t len;
> 	uint16_t index;
> 	uint16_t flags;
> };
> 
> or
> 
> struct vring_packed_desc {
> 	uint64_t addr;
> 	uint32_t len;
> 	uint16_t id;		// index -> id
> 	uint16_t flags;
> };
> 
> We need to choose one of them or something else.
> 

I will align on Kernel header.

>>   #endif
>>   
> [...]
>> +static __rte_always_inline void
>> +vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
>> +{
>> +	uint16_t old, new, off, off_wrap;
>> +	bool kick = false;
>> +
>> +	/* Flush used desc update. */
>> +	rte_smp_mb();
>> +
>> +	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
>> +		if (vq->driver_event->desc_event_flags !=
>> +				VRING_EVENT_F_DISABLE)
>> +			kick = true;
>> +		goto kick;
>> +	}
>> +
>> +	old = vq->signalled_used;
> 
> We also need to check whether vq->signalled_used is valid?

Yes, thanks for pointing this out.

So if not valid, I'll kick if desc_event_flags != VRING_EVENT_F_DISABLE.

>> +	new = vq->last_used_idx;
>> +	vq->signalled_used = new;
>> +
>> +	if (vq->driver_event->desc_event_flags != VRING_EVENT_F_DESC) {
>> +		if (vq->driver_event->desc_event_flags !=
>> +				VRING_EVENT_F_DISABLE)
>> +			kick = true;
>> +		goto kick;
>> +	}
>> +
>> +	rte_smp_rmb();
>> +
>> +	off_wrap = vq->driver_event->desc_event_off_wrap;
>> +	off = off_wrap & ~(1 << 15);
>> +
>> +	if (vq->used_wrap_counter != off_wrap >> 15)
>> +		off -= vq->size;
>> +
>> +	if (vhost_need_event(off, new, old))
>> +		kick = true;
> 
> If new <= old, old needs to -= vq->size?

Right, I'll fix it in next version.

Thanks!
Maxime
>> +kick:
>> +	if (kick)
>> +		eventfd_write(vq->callfd, (eventfd_t)1);
>> +}
>> +
> [...]
>
  

Patch

diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
index b5c649281..13e37825b 100644
--- a/lib/librte_vhost/vhost.c
+++ b/lib/librte_vhost/vhost.c
@@ -163,13 +163,28 @@  vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
 
 	req_size = sizeof(struct vring_desc_packed) * vq->size;
 	size = req_size;
-	vq->desc_packed =
-		(struct vring_desc_packed *)(uintptr_t)vhost_iova_to_vva(dev,
-					vq, vq->ring_addrs.desc_user_addr,
-					&size, VHOST_ACCESS_RW);
+	vq->desc_packed = (struct vring_desc_packed *)(uintptr_t)
+		vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
+				&size, VHOST_ACCESS_RW);
 	if (!vq->desc_packed || size != req_size)
 		return -1;
 
+	req_size = sizeof(struct vring_packed_desc_event);
+	size = req_size;
+	vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
+		vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
+				&size, VHOST_ACCESS_RW);
+	if (!vq->driver_event || size != req_size)
+		return -1;
+
+	req_size = sizeof(struct vring_packed_desc_event);
+	size = req_size;
+	vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
+		vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
+				&size, VHOST_ACCESS_RW);
+	if (!vq->device_event || size != req_size)
+		return -1;
+
 	return 0;
 }
 
@@ -604,7 +619,11 @@  rte_vhost_vring_call(int vid, uint16_t vring_idx)
 	if (!vq)
 		return -1;
 
-	vhost_vring_call(dev, vq);
+	if (vq_is_packed(dev))
+		vhost_vring_call_packed(dev, vq);
+	else
+		vhost_vring_call_split(dev, vq);
+
 	return 0;
 }
 
@@ -625,19 +644,52 @@  rte_vhost_avail_entries(int vid, uint16_t queue_id)
 	return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
 }
 
+static inline void
+vhost_enable_notify_split(struct vhost_virtqueue *vq, int enable)
+{
+	if (enable)
+		vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
+	else
+		vq->used->flags |= VRING_USED_F_NO_NOTIFY;
+}
+
+static inline void
+vhost_enable_notify_packed(struct virtio_net *dev,
+		struct vhost_virtqueue *vq, int enable)
+{
+	uint16_t flags;
+
+	if (!enable)
+		vq->device_event->desc_event_flags = VRING_EVENT_F_DISABLE;
+
+	flags = VRING_EVENT_F_ENABLE;
+	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
+		flags = VRING_EVENT_F_DESC;
+		vq->device_event->desc_event_off_wrap = vq->last_avail_idx |
+			vq->avail_wrap_counter << 15;
+	}
+
+	rte_smp_wmb();
+
+	vq->device_event->desc_event_flags = flags;
+}
+
 int
 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
 {
 	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;
 
 	if (!dev)
 		return -1;
 
-	if (enable)
-		dev->virtqueue[queue_id]->used->flags &=
-			~VRING_USED_F_NO_NOTIFY;
+	vq = dev->virtqueue[queue_id];
+
+	if (vq_is_packed(dev))
+		vhost_enable_notify_packed(dev, vq, enable);
 	else
-		dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
+		vhost_enable_notify_split(vq, enable);
+
 	return 0;
 }
 
diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index 92a87712e..5c7effabe 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -95,8 +95,14 @@  struct vhost_virtqueue {
 		struct vring_desc	*desc;
 		struct vring_desc_packed   *desc_packed;
 	};
-	struct vring_avail	*avail;
-	struct vring_used	*used;
+	union {
+		struct vring_avail	*avail;
+		struct vring_packed_desc_event *driver_event;
+	};
+	union {
+		struct vring_used	*used;
+		struct vring_packed_desc_event *device_event;
+	};
 	uint32_t		size;
 
 	uint16_t		last_avail_idx;
@@ -225,6 +231,15 @@  struct vring_desc_packed {
 	uint16_t index;
 	uint16_t flags;
 };
+
+#define VRING_EVENT_F_ENABLE 0x0
+#define VRING_EVENT_F_DISABLE 0x1
+#define VRING_EVENT_F_DESC 0x2
+
+struct vring_packed_desc_event {
+	uint16_t desc_event_off_wrap;
+	uint16_t desc_event_flags;
+};
 #endif
 
 /*
@@ -648,7 +663,7 @@  vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
 }
 
 static __rte_always_inline void
-vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
+vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
 {
 	/* Flush used->idx update before we read avail->flags. */
 	rte_smp_mb();
@@ -675,4 +690,46 @@  vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
 	}
 }
 
+static __rte_always_inline void
+vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
+{
+	uint16_t old, new, off, off_wrap;
+	bool kick = false;
+
+	/* Flush used desc update. */
+	rte_smp_mb();
+
+	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
+		if (vq->driver_event->desc_event_flags !=
+				VRING_EVENT_F_DISABLE)
+			kick = true;
+		goto kick;
+	}
+
+	old = vq->signalled_used;
+	new = vq->last_used_idx;
+	vq->signalled_used = new;
+
+	if (vq->driver_event->desc_event_flags != VRING_EVENT_F_DESC) {
+		if (vq->driver_event->desc_event_flags !=
+				VRING_EVENT_F_DISABLE)
+			kick = true;
+		goto kick;
+	}
+
+	rte_smp_rmb();
+
+	off_wrap = vq->driver_event->desc_event_off_wrap;
+	off = off_wrap & ~(1 << 15);
+
+	if (vq->used_wrap_counter != off_wrap >> 15)
+		off -= vq->size;
+
+	if (vhost_need_event(off, new, old))
+		kick = true;
+kick:
+	if (kick)
+		eventfd_write(vq->callfd, (eventfd_t)1);
+}
+
 #endif /* _VHOST_NET_CDEV_H_ */
diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 4ad7c3e55..4fa8dbad3 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -512,6 +512,30 @@  translate_ring_addresses(struct virtio_net *dev, int vq_index)
 		vq = dev->virtqueue[vq_index];
 		addr = &vq->ring_addrs;
 
+		len = sizeof(struct vring_packed_desc_event);
+		vq->driver_event = (struct vring_packed_desc_event *)
+					(uintptr_t)ring_addr_to_vva(dev,
+					vq, addr->avail_user_addr, &len);
+		if (vq->driver_event == NULL ||
+				len != sizeof(struct vring_packed_desc_event)) {
+			RTE_LOG(DEBUG, VHOST_CONFIG,
+				"(%d) failed to find driver area address.\n",
+				dev->vid);
+			return dev;
+		}
+
+		len = sizeof(struct vring_packed_desc_event);
+		vq->device_event = (struct vring_packed_desc_event *)
+					(uintptr_t)ring_addr_to_vva(dev,
+					vq, addr->used_user_addr, &len);
+		if (vq->device_event == NULL ||
+				len != sizeof(struct vring_packed_desc_event)) {
+			RTE_LOG(DEBUG, VHOST_CONFIG,
+				"(%d) failed to find device area address.\n",
+				dev->vid);
+			return dev;
+		}
+
 		return dev;
 	}
 
diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
index 7210cb241..e7dff36fa 100644
--- a/lib/librte_vhost/virtio_net.c
+++ b/lib/librte_vhost/virtio_net.c
@@ -826,7 +826,7 @@  virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
 	if (likely(vq->shadow_used_idx)) {
 		flush_shadow_used_ring_split(dev, vq);
-		vhost_vring_call(dev, vq);
+		vhost_vring_call_split(dev, vq);
 	}
 
 	return pkt_idx;
@@ -879,7 +879,7 @@  virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
 	if (likely(vq->shadow_used_idx)) {
 		flush_shadow_used_ring_packed(dev, vq);
-		vhost_vring_call(dev, vq);
+		vhost_vring_call_packed(dev, vq);
 	}
 
 	return pkt_idx;
@@ -1362,7 +1362,7 @@  virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		}
 
 		flush_shadow_used_ring_split(dev, vq);
-		vhost_vring_call(dev, vq);
+		vhost_vring_call_split(dev, vq);
 	}
 
 	rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
@@ -1441,7 +1441,7 @@  virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		if (unlikely(i < count))
 			vq->shadow_used_idx = i;
 		flush_shadow_used_ring_split(dev, vq);
-		vhost_vring_call(dev, vq);
+		vhost_vring_call_split(dev, vq);
 	}
 
 	return i;
@@ -1477,7 +1477,7 @@  virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		}
 
 		flush_shadow_used_ring_packed(dev, vq);
-		vhost_vring_call(dev, vq);
+		vhost_vring_call_packed(dev, vq);
 	}
 
 	VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
@@ -1555,7 +1555,7 @@  virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		if (unlikely(i < count))
 			vq->shadow_used_idx = i;
 		flush_shadow_used_ring_packed(dev, vq);
-		vhost_vring_call(dev, vq);
+		vhost_vring_call_packed(dev, vq);
 	}
 
 	return i;