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

Message ID 20180702081629.29258-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 2, 2018, 8:16 a.m. UTC
  Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost.c         | 73 ++++++++++++++++++++++++++++++++++++----
 lib/librte_vhost/vhost.h         | 71 ++++++++++++++++++++++++++++++++++----
 lib/librte_vhost/vhost_user.c    | 24 +++++++++++++
 lib/librte_vhost/virtio-packed.h | 11 ++++++
 lib/librte_vhost/virtio_net.c    | 12 +++----
 5 files changed, 172 insertions(+), 19 deletions(-)
  

Comments

Jason Wang July 3, 2018, 5:57 a.m. UTC | #1
On 2018年07月02日 16:16, Maxime Coquelin wrote:
> +static inline int
> +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;
> +
> +	return 0;
> +}
> +
> +static inline int
> +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 = RING_EVENT_FLAGS_DISABLE;
> +		return 0;
> +	}
> +
> +	flags = RING_EVENT_FLAGS_ENABLE;
> +	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
> +		flags = RING_EVENT_FLAGS_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;
> +
> +	rte_smp_wmb();
> +

We don't do this for split version. Any specific reason for this?

Btw, looks like we don't care about the return value so using void instead?

Thanks

> +	return 0;
> +}
> +
  
Jason Wang July 3, 2018, 6:06 a.m. UTC | #2
On 2018年07月02日 16:16, Maxime Coquelin wrote:
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>   lib/librte_vhost/vhost.c         | 73 ++++++++++++++++++++++++++++++++++++----
>   lib/librte_vhost/vhost.h         | 71 ++++++++++++++++++++++++++++++++++----
>   lib/librte_vhost/vhost_user.c    | 24 +++++++++++++
>   lib/librte_vhost/virtio-packed.h | 11 ++++++
>   lib/librte_vhost/virtio_net.c    | 12 +++----
>   5 files changed, 172 insertions(+), 19 deletions(-)
>
> diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
> index 8538302c9..78f20c402 100644
> --- a/lib/librte_vhost/vhost.c
> +++ b/lib/librte_vhost/vhost.c
> @@ -171,6 +171,24 @@ vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
>   	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;
>   }
>   
> @@ -595,7 +613,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;
>   }
>   
> @@ -616,20 +638,59 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
>   	return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
>   }
>   
> +static inline int
> +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;
> +
> +	return 0;
> +}
> +
> +static inline int
> +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 = RING_EVENT_FLAGS_DISABLE;
> +		return 0;
> +	}
> +
> +	flags = RING_EVENT_FLAGS_ENABLE;
> +	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
> +		flags = RING_EVENT_FLAGS_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;
> +
> +	rte_smp_wmb();
> +
> +	return 0;
> +}
> +
>   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))
> +		return vhost_enable_notify_packed(dev, vq, enable);
>   	else
> -		dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
> -	return 0;
> +		return vhost_enable_notify_split(vq, enable);
>   }
>   
>   void
> diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
> index 6ea8fb896..728fd2f6b 100644
> --- a/lib/librte_vhost/vhost.h
> +++ b/lib/librte_vhost/vhost.h
> @@ -21,6 +21,7 @@
>   
>   #include "rte_vhost.h"
>   #include "rte_vdpa.h"
> +#include "virtio-packed.h"
>   
>   /* Used to indicate that the device is running on a data core */
>   #define VIRTIO_DEV_RUNNING 1
> @@ -95,8 +96,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;
> @@ -613,7 +620,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_mb();
> @@ -624,11 +631,11 @@ vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
>   		uint16_t new = vq->last_used_idx;
>   
>   		VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
> -			__func__,
> -			vhost_used_event(vq),
> -			old, new);
> +				__func__,
> +				vhost_used_event(vq),
> +				old, new);
>   		if (vhost_need_event(vhost_used_event(vq), new, old)
> -			&& (vq->callfd >= 0)) {
> +				&& (vq->callfd >= 0)) {
>   			vq->signalled_used = vq->last_used_idx;
>   			eventfd_write(vq->callfd, (eventfd_t) 1);
>   		}

Looks like a style fixes, move those to another patch?

> @@ -640,4 +647,54 @@ 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, 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 !=
> +				RING_EVENT_FLAGS_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 != RING_EVENT_FLAGS_DESC) {
> +		if (vq->driver_event->desc_event_flags !=
> +				RING_EVENT_FLAGS_DISABLE)
> +			kick = true;
> +		goto kick;
> +	}
> +
> +	rte_smp_rmb();
> +
> +	off_wrap = vq->driver_event->desc_event_off_wrap;
> +	off = off_wrap & ~(1 << 15);
> +	wrap = vq->used_wrap_counter;
> +
> +	if (new < old) {
> +		new += vq->size;
> +		wrap ^= 1;
> +	}
> +
> +	if (wrap != off_wrap >> 15)
> +		off += vq->size;
> +

Jusy FYI. Maybe we can switch to a more compact version like Tiwei used:

...
         wrap_counter = off_wrap >> 15;
         event_idx = off_wrap & ~(1<<15);
         if (wrap_counter != vq->avail_wrap_counter)
                 event_idx -= vq->vring_packed.num;

         if (flags == VRING_EVENT_F_DESC)
                 needs_kick = vring_need_event(event_idx, new, old);
         else
                 needs_kick = (flags != VRING_EVENT_F_DISABLE);

(I've switched to this version in vhost).

Thanks

> +	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 b2b57de57..bda515bdb 100644
> --- a/lib/librte_vhost/vhost_user.c
> +++ b/lib/librte_vhost/vhost_user.c
> @@ -523,6 +523,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 == 0 ||
> +				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 == 0 ||
> +				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-packed.h b/lib/librte_vhost/virtio-packed.h
> index d386cb6df..ce3b28313 100644
> --- a/lib/librte_vhost/virtio-packed.h
> +++ b/lib/librte_vhost/virtio-packed.h
> @@ -19,6 +19,17 @@ struct vring_desc_packed {
>   	uint16_t flags;
>   };
>   
> +#define RING_EVENT_FLAGS_ENABLE 0x0
> +#define RING_EVENT_FLAGS_DISABLE 0x1
> +#define RING_EVENT_FLAGS_DESC 0x2
> +#define RING_EVENT_FLAGS_MASK 0xFFFC
> +#define RING_EVENT_WRAP_MASK 0x8000
> +#define RING_EVENT_OFF_MASK 0x7FFF
> +
> +struct vring_packed_desc_event {
> +	uint16_t desc_event_off_wrap;
> +	uint16_t desc_event_flags;
> +};
>   
>   static inline bool
>   desc_is_avail(struct vring_desc_packed *desc, bool wrap_counter)
> diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
> index 03dd38235..11c10aaf8 100644
> --- a/lib/librte_vhost/virtio_net.c
> +++ b/lib/librte_vhost/virtio_net.c
> @@ -824,7 +824,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;
> @@ -877,7 +877,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;
> @@ -1360,7 +1360,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)]);
> @@ -1439,7 +1439,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;
  
Maxime Coquelin July 3, 2018, 6:43 a.m. UTC | #3
On 07/03/2018 07:57 AM, Jason Wang wrote:
> 
> 
> On 2018年07月02日 16:16, Maxime Coquelin wrote:
>> +static inline int
>> +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;
>> +
>> +    return 0;
>> +}
>> +
>> +static inline int
>> +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 = RING_EVENT_FLAGS_DISABLE;
>> +        return 0;
>> +    }
>> +
>> +    flags = RING_EVENT_FLAGS_ENABLE;
>> +    if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
>> +        flags = RING_EVENT_FLAGS_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;
>> +
>> +    rte_smp_wmb();
>> +
> 
> We don't do this for split version. Any specific reason for this?

Yeah, maybe this is no necessary.

> Btw, looks like we don't care about the return value so using void instead?

Right, it could be a void.

Thanks!
Maxime

> Thanks
> 
>> +    return 0;
>> +}
>> +
> 
>
  
Tiwei Bie July 4, 2018, 6:25 a.m. UTC | #4
On Mon, Jul 02, 2018 at 10:16:28AM +0200, Maxime Coquelin wrote:
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  lib/librte_vhost/vhost.c         | 73 ++++++++++++++++++++++++++++++++++++----
>  lib/librte_vhost/vhost.h         | 71 ++++++++++++++++++++++++++++++++++----
>  lib/librte_vhost/vhost_user.c    | 24 +++++++++++++
>  lib/librte_vhost/virtio-packed.h | 11 ++++++
>  lib/librte_vhost/virtio_net.c    | 12 +++----
>  5 files changed, 172 insertions(+), 19 deletions(-)
> 
> diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
> index 8538302c9..78f20c402 100644
> --- a/lib/librte_vhost/vhost.c
> +++ b/lib/librte_vhost/vhost.c
> @@ -171,6 +171,24 @@ vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
>  	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);

It should be a space instead of a tab after "vq,"

Why not put "vq, vq->ring_addrs.avail_user_addr,"
and "vhost_iova_to_vva(dev," on the same line?

> +	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,

It's better to put "vhost_iova_to_vva(dev," and
"vq, vq->ring_addrs.used_user_addr," on the same line.

Currently, it looks like something as this in my editor:

¦       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);

> +					&size, VHOST_ACCESS_RW);
> +	if (!vq->device_event || size != req_size)
> +		return -1;
> +
>  	return 0;
>  }
>  
[...]
> @@ -640,4 +647,54 @@ 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, wrap;
> +	bool kick = false;
> +
> +

There is no need to have two blank lines.

> +	/*  Flush used desc update. */

Just need one space between "/*" and "Flush".

> +	rte_smp_mb();
> +
> +	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
> +		if (vq->driver_event->desc_event_flags !=
> +				RING_EVENT_FLAGS_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 != RING_EVENT_FLAGS_DESC) {
> +		if (vq->driver_event->desc_event_flags !=
> +				RING_EVENT_FLAGS_DISABLE)
> +			kick = true;
> +		goto kick;
> +	}
> +
> +	rte_smp_rmb();
> +
> +	off_wrap = vq->driver_event->desc_event_off_wrap;
> +	off = off_wrap & ~(1 << 15);

Maybe it's better to use: RING_EVENT_OFF_MASK

> +	wrap = vq->used_wrap_counter;
> +
> +	if (new < old) {
> +		new += vq->size;
> +		wrap ^= 1;
> +	}
> +
> +	if (wrap != 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 b2b57de57..bda515bdb 100644
> --- a/lib/librte_vhost/vhost_user.c
> +++ b/lib/librte_vhost/vhost_user.c
> @@ -523,6 +523,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 == 0 ||

It's better to compare with 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 == 0 ||

It's better to compare with 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-packed.h b/lib/librte_vhost/virtio-packed.h
> index d386cb6df..ce3b28313 100644
> --- a/lib/librte_vhost/virtio-packed.h
> +++ b/lib/librte_vhost/virtio-packed.h

Normally, we name c/h files as something like: virtio_packed.h

Besides, it'd be better to move the definitions into
vhost.h and define the types and flags for packed
ring only when e.g. VIRTIO_F_RING_PACKED not defined.
Just like how VIRTIO_F_IOMMU_PLATFORM works:

/* Declare IOMMU related bits for older kernels */
#ifndef VIRTIO_F_IOMMU_PLATFORM

#define VIRTIO_F_IOMMU_PLATFORM 33

struct vhost_iotlb_msg {
	__u64 iova;
	__u64 size;
	__u64 uaddr;
#define VHOST_ACCESS_RO      0x1
#define VHOST_ACCESS_WO      0x2
#define VHOST_ACCESS_RW      0x3
	__u8 perm;
#define VHOST_IOTLB_MISS           1
#define VHOST_IOTLB_UPDATE         2
#define VHOST_IOTLB_INVALIDATE     3
#define VHOST_IOTLB_ACCESS_FAIL    4
	__u8 type;
};

#define VHOST_IOTLB_MSG 0x1

struct vhost_msg {
	int type;
	union {
		struct vhost_iotlb_msg iotlb;
		__u8 padding[64];
	};
};
#endif

> @@ -19,6 +19,17 @@ struct vring_desc_packed {
>  	uint16_t flags;
>  };
>  
> +#define RING_EVENT_FLAGS_ENABLE 0x0
> +#define RING_EVENT_FLAGS_DISABLE 0x1
> +#define RING_EVENT_FLAGS_DESC 0x2
> +#define RING_EVENT_FLAGS_MASK 0xFFFC

RING_EVENT_FLAGS_MASK should be 0x3?

> +#define RING_EVENT_WRAP_MASK 0x8000
> +#define RING_EVENT_OFF_MASK 0x7FFF

It's better to define above macros as VRING_EVENT_*

> +
> +struct vring_packed_desc_event {
> +	uint16_t desc_event_off_wrap;
> +	uint16_t desc_event_flags;
> +};
>  
[...]
  
Maxime Coquelin July 4, 2018, 4:02 p.m. UTC | #5
On 07/03/2018 08:06 AM, Jason Wang wrote:
> 
> 
> On 2018年07月02日 16:16, Maxime Coquelin wrote:
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> ---
>>   lib/librte_vhost/vhost.c         | 73 
>> ++++++++++++++++++++++++++++++++++++----
>>   lib/librte_vhost/vhost.h         | 71 
>> ++++++++++++++++++++++++++++++++++----
>>   lib/librte_vhost/vhost_user.c    | 24 +++++++++++++
>>   lib/librte_vhost/virtio-packed.h | 11 ++++++
>>   lib/librte_vhost/virtio_net.c    | 12 +++----
>>   5 files changed, 172 insertions(+), 19 deletions(-)
>>
>> diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
>> index 8538302c9..78f20c402 100644
>> --- a/lib/librte_vhost/vhost.c
>> +++ b/lib/librte_vhost/vhost.c
>> @@ -171,6 +171,24 @@ vring_translate_packed(struct virtio_net *dev, 
>> struct vhost_virtqueue *vq)
>>       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;
>>   }
>> @@ -595,7 +613,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;
>>   }
>> @@ -616,20 +638,59 @@ rte_vhost_avail_entries(int vid, uint16_t queue_id)
>>       return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
>>   }
>> +static inline int
>> +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;
>> +
>> +    return 0;
>> +}
>> +
>> +static inline int
>> +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 = RING_EVENT_FLAGS_DISABLE;
>> +        return 0;
>> +    }
>> +
>> +    flags = RING_EVENT_FLAGS_ENABLE;
>> +    if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
>> +        flags = RING_EVENT_FLAGS_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;
>> +
>> +    rte_smp_wmb();
>> +
>> +    return 0;
>> +}
>> +
>>   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))
>> +        return vhost_enable_notify_packed(dev, vq, enable);
>>       else
>> -        dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
>> -    return 0;
>> +        return vhost_enable_notify_split(vq, enable);
>>   }
>>   void
>> diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
>> index 6ea8fb896..728fd2f6b 100644
>> --- a/lib/librte_vhost/vhost.h
>> +++ b/lib/librte_vhost/vhost.h
>> @@ -21,6 +21,7 @@
>>   #include "rte_vhost.h"
>>   #include "rte_vdpa.h"
>> +#include "virtio-packed.h"
>>   /* Used to indicate that the device is running on a data core */
>>   #define VIRTIO_DEV_RUNNING 1
>> @@ -95,8 +96,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;
>> @@ -613,7 +620,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_mb();
>> @@ -624,11 +631,11 @@ vhost_vring_call(struct virtio_net *dev, struct 
>> vhost_virtqueue *vq)
>>           uint16_t new = vq->last_used_idx;
>>           VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, 
>> new=%d\n",
>> -            __func__,
>> -            vhost_used_event(vq),
>> -            old, new);
>> +                __func__,
>> +                vhost_used_event(vq),
>> +                old, new);
>>           if (vhost_need_event(vhost_used_event(vq), new, old)
>> -            && (vq->callfd >= 0)) {
>> +                && (vq->callfd >= 0)) {
>>               vq->signalled_used = vq->last_used_idx;
>>               eventfd_write(vq->callfd, (eventfd_t) 1);
>>           }
> 
> Looks like a style fixes, move those to another patch?

Indeed, I'll remove this style fixes in next version.

>> @@ -640,4 +647,54 @@ 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, 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 !=
>> +                RING_EVENT_FLAGS_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 != RING_EVENT_FLAGS_DESC) {
>> +        if (vq->driver_event->desc_event_flags !=
>> +                RING_EVENT_FLAGS_DISABLE)
>> +            kick = true;
>> +        goto kick;
>> +    }
>> +
>> +    rte_smp_rmb();
>> +
>> +    off_wrap = vq->driver_event->desc_event_off_wrap;
>> +    off = off_wrap & ~(1 << 15);
>> +    wrap = vq->used_wrap_counter;
>> +
>> +    if (new < old) {
>> +        new += vq->size;
>> +        wrap ^= 1;
>> +    }
>> +
>> +    if (wrap != off_wrap >> 15)
>> +        off += vq->size;
>> +
> 
> Jusy FYI. Maybe we can switch to a more compact version like Tiwei used:
> 
> ...
>          wrap_counter = off_wrap >> 15;
>          event_idx = off_wrap & ~(1<<15);
>          if (wrap_counter != vq->avail_wrap_counter)
>                  event_idx -= vq->vring_packed.num;
> 
>          if (flags == VRING_EVENT_F_DESC)
>                  needs_kick = vring_need_event(event_idx, new, old);
>          else
>                  needs_kick = (flags != VRING_EVENT_F_DISABLE);
> 
> (I've switched to this version in vhost).

Sure, it is indeed simpler.

Thanks,
Maxime

> Thanks
> 
>> +    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 b2b57de57..bda515bdb 100644
>> --- a/lib/librte_vhost/vhost_user.c
>> +++ b/lib/librte_vhost/vhost_user.c
>> @@ -523,6 +523,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 == 0 ||
>> +                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 == 0 ||
>> +                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-packed.h 
>> b/lib/librte_vhost/virtio-packed.h
>> index d386cb6df..ce3b28313 100644
>> --- a/lib/librte_vhost/virtio-packed.h
>> +++ b/lib/librte_vhost/virtio-packed.h
>> @@ -19,6 +19,17 @@ struct vring_desc_packed {
>>       uint16_t flags;
>>   };
>> +#define RING_EVENT_FLAGS_ENABLE 0x0
>> +#define RING_EVENT_FLAGS_DISABLE 0x1
>> +#define RING_EVENT_FLAGS_DESC 0x2
>> +#define RING_EVENT_FLAGS_MASK 0xFFFC
>> +#define RING_EVENT_WRAP_MASK 0x8000
>> +#define RING_EVENT_OFF_MASK 0x7FFF
>> +
>> +struct vring_packed_desc_event {
>> +    uint16_t desc_event_off_wrap;
>> +    uint16_t desc_event_flags;
>> +};
>>   static inline bool
>>   desc_is_avail(struct vring_desc_packed *desc, bool wrap_counter)
>> diff --git a/lib/librte_vhost/virtio_net.c 
>> b/lib/librte_vhost/virtio_net.c
>> index 03dd38235..11c10aaf8 100644
>> --- a/lib/librte_vhost/virtio_net.c
>> +++ b/lib/librte_vhost/virtio_net.c
>> @@ -824,7 +824,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;
>> @@ -877,7 +877,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;
>> @@ -1360,7 +1360,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)]);
>> @@ -1439,7 +1439,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;
>
  
Maxime Coquelin July 4, 2018, 8:20 p.m. UTC | #6
On 07/04/2018 08:25 AM, Tiwei Bie wrote:
> On Mon, Jul 02, 2018 at 10:16:28AM +0200, Maxime Coquelin wrote:
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> ---
>>   lib/librte_vhost/vhost.c         | 73 ++++++++++++++++++++++++++++++++++++----
>>   lib/librte_vhost/vhost.h         | 71 ++++++++++++++++++++++++++++++++++----
>>   lib/librte_vhost/vhost_user.c    | 24 +++++++++++++
>>   lib/librte_vhost/virtio-packed.h | 11 ++++++
>>   lib/librte_vhost/virtio_net.c    | 12 +++----
>>   5 files changed, 172 insertions(+), 19 deletions(-)
>>
>> diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
>> index 8538302c9..78f20c402 100644
>> --- a/lib/librte_vhost/vhost.c
>> +++ b/lib/librte_vhost/vhost.c
>> @@ -171,6 +171,24 @@ vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
>>   	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);
> 
> It should be a space instead of a tab after "vq,"
> 
> Why not put "vq, vq->ring_addrs.avail_user_addr,"
> and "vhost_iova_to_vva(dev," on the same line?
> 
>> +	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,
> 
> It's better to put "vhost_iova_to_vva(dev," and
> "vq, vq->ring_addrs.used_user_addr," on the same line.
> 
> Currently, it looks like something as this in my editor:
> 
> ¦       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);
> 

It looked a bit cleaner in my editor :) This is fixed now.

>> +					&size, VHOST_ACCESS_RW);
>> +	if (!vq->device_event || size != req_size)
>> +		return -1;
>> +
>>   	return 0;
>>   }
>>   
> [...]
>> @@ -640,4 +647,54 @@ 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, wrap;
>> +	bool kick = false;
>> +
>> +
> 
> There is no need to have two blank lines.
> 
>> +	/*  Flush used desc update. */
> 
> Just need one space between "/*" and "Flush".
> 
>> +	rte_smp_mb();
>> +
>> +	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
>> +		if (vq->driver_event->desc_event_flags !=
>> +				RING_EVENT_FLAGS_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 != RING_EVENT_FLAGS_DESC) {
>> +		if (vq->driver_event->desc_event_flags !=
>> +				RING_EVENT_FLAGS_DISABLE)
>> +			kick = true;
>> +		goto kick;
>> +	}
>> +
>> +	rte_smp_rmb();
>> +
>> +	off_wrap = vq->driver_event->desc_event_off_wrap;
>> +	off = off_wrap & ~(1 << 15);
> 
> Maybe it's better to use: RING_EVENT_OFF_MASK

I planned to remove its definition to be consistent with
the virtio_ring.h kernel header.

> 
>> +	wrap = vq->used_wrap_counter;
>> +
>> +	if (new < old) {
>> +		new += vq->size;
>> +		wrap ^= 1;
>> +	}
>> +
>> +	if (wrap != 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 b2b57de57..bda515bdb 100644
>> --- a/lib/librte_vhost/vhost_user.c
>> +++ b/lib/librte_vhost/vhost_user.c
>> @@ -523,6 +523,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 == 0 ||
> 
> It's better to compare with NULL.

Yes.

>> +				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 == 0 ||
> 
> It's better to compare with 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-packed.h b/lib/librte_vhost/virtio-packed.h
>> index d386cb6df..ce3b28313 100644
>> --- a/lib/librte_vhost/virtio-packed.h
>> +++ b/lib/librte_vhost/virtio-packed.h
> 
> Normally, we name c/h files as something like: virtio_packed.h
> 
> Besides, it'd be better to move the definitions into
> vhost.h and define the types and flags for packed
> ring only when e.g. VIRTIO_F_RING_PACKED not defined.
> Just like how VIRTIO_F_IOMMU_PLATFORM works:
> 
> /* Declare IOMMU related bits for older kernels */
> #ifndef VIRTIO_F_IOMMU_PLATFORM
> 
> #define VIRTIO_F_IOMMU_PLATFORM 33
> 
> struct vhost_iotlb_msg {
> 	__u64 iova;
> 	__u64 size;
> 	__u64 uaddr;
> #define VHOST_ACCESS_RO      0x1
> #define VHOST_ACCESS_WO      0x2
> #define VHOST_ACCESS_RW      0x3
> 	__u8 perm;
> #define VHOST_IOTLB_MISS           1
> #define VHOST_IOTLB_UPDATE         2
> #define VHOST_IOTLB_INVALIDATE     3
> #define VHOST_IOTLB_ACCESS_FAIL    4
> 	__u8 type;
> };
> 
> #define VHOST_IOTLB_MSG 0x1
> 
> struct vhost_msg {
> 	int type;
> 	union {
> 		struct vhost_iotlb_msg iotlb;
> 		__u8 padding[64];
> 	};
> };
> #endif

I agree. I replied to Jason series so that the defines name are
consistent.

>> @@ -19,6 +19,17 @@ struct vring_desc_packed {
>>   	uint16_t flags;
>>   };
>>   
>> +#define RING_EVENT_FLAGS_ENABLE 0x0
>> +#define RING_EVENT_FLAGS_DISABLE 0x1
>> +#define RING_EVENT_FLAGS_DESC 0x2
>> +#define RING_EVENT_FLAGS_MASK 0xFFFC
> 
> RING_EVENT_FLAGS_MASK should be 0x3?

I actually removed it, but yes I agree.

>> +#define RING_EVENT_WRAP_MASK 0x8000
>> +#define RING_EVENT_OFF_MASK 0x7FFF
> 
> It's better to define above macros as VRING_EVENT_*

Yes, I change it to VRING_EVENT_F_* as you suggested to Jason.

>> +
>> +struct vring_packed_desc_event {
>> +	uint16_t desc_event_off_wrap;
>> +	uint16_t desc_event_flags;
>> +};
>>   
> [...]
>
  

Patch

diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c
index 8538302c9..78f20c402 100644
--- a/lib/librte_vhost/vhost.c
+++ b/lib/librte_vhost/vhost.c
@@ -171,6 +171,24 @@  vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
 	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;
 }
 
@@ -595,7 +613,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;
 }
 
@@ -616,20 +638,59 @@  rte_vhost_avail_entries(int vid, uint16_t queue_id)
 	return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
 }
 
+static inline int
+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;
+
+	return 0;
+}
+
+static inline int
+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 = RING_EVENT_FLAGS_DISABLE;
+		return 0;
+	}
+
+	flags = RING_EVENT_FLAGS_ENABLE;
+	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
+		flags = RING_EVENT_FLAGS_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;
+
+	rte_smp_wmb();
+
+	return 0;
+}
+
 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))
+		return vhost_enable_notify_packed(dev, vq, enable);
 	else
-		dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
-	return 0;
+		return vhost_enable_notify_split(vq, enable);
 }
 
 void
diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index 6ea8fb896..728fd2f6b 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -21,6 +21,7 @@ 
 
 #include "rte_vhost.h"
 #include "rte_vdpa.h"
+#include "virtio-packed.h"
 
 /* Used to indicate that the device is running on a data core */
 #define VIRTIO_DEV_RUNNING 1
@@ -95,8 +96,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;
@@ -613,7 +620,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_mb();
@@ -624,11 +631,11 @@  vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
 		uint16_t new = vq->last_used_idx;
 
 		VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
-			__func__,
-			vhost_used_event(vq),
-			old, new);
+				__func__,
+				vhost_used_event(vq),
+				old, new);
 		if (vhost_need_event(vhost_used_event(vq), new, old)
-			&& (vq->callfd >= 0)) {
+				&& (vq->callfd >= 0)) {
 			vq->signalled_used = vq->last_used_idx;
 			eventfd_write(vq->callfd, (eventfd_t) 1);
 		}
@@ -640,4 +647,54 @@  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, 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 !=
+				RING_EVENT_FLAGS_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 != RING_EVENT_FLAGS_DESC) {
+		if (vq->driver_event->desc_event_flags !=
+				RING_EVENT_FLAGS_DISABLE)
+			kick = true;
+		goto kick;
+	}
+
+	rte_smp_rmb();
+
+	off_wrap = vq->driver_event->desc_event_off_wrap;
+	off = off_wrap & ~(1 << 15);
+	wrap = vq->used_wrap_counter;
+
+	if (new < old) {
+		new += vq->size;
+		wrap ^= 1;
+	}
+
+	if (wrap != 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 b2b57de57..bda515bdb 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -523,6 +523,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 == 0 ||
+				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 == 0 ||
+				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-packed.h b/lib/librte_vhost/virtio-packed.h
index d386cb6df..ce3b28313 100644
--- a/lib/librte_vhost/virtio-packed.h
+++ b/lib/librte_vhost/virtio-packed.h
@@ -19,6 +19,17 @@  struct vring_desc_packed {
 	uint16_t flags;
 };
 
+#define RING_EVENT_FLAGS_ENABLE 0x0
+#define RING_EVENT_FLAGS_DISABLE 0x1
+#define RING_EVENT_FLAGS_DESC 0x2
+#define RING_EVENT_FLAGS_MASK 0xFFFC
+#define RING_EVENT_WRAP_MASK 0x8000
+#define RING_EVENT_OFF_MASK 0x7FFF
+
+struct vring_packed_desc_event {
+	uint16_t desc_event_off_wrap;
+	uint16_t desc_event_flags;
+};
 
 static inline bool
 desc_is_avail(struct vring_desc_packed *desc, bool wrap_counter)
diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.c
index 03dd38235..11c10aaf8 100644
--- a/lib/librte_vhost/virtio_net.c
+++ b/lib/librte_vhost/virtio_net.c
@@ -824,7 +824,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;
@@ -877,7 +877,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;
@@ -1360,7 +1360,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)]);
@@ -1439,7 +1439,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;