diff mbox series

[net-next,v2,14/21] virtio_net: xsk: tx: virtnet_free_old_xmit() distinguishes xsk buffer

Message ID 20231107031227.100015-15-xuanzhuo@linux.alibaba.com (mailing list archive)
State Deferred
Delegated to: Netdev Maintainers
Headers show
Series virtio-net: support AF_XDP zero copy | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next, async
netdev/apply fail Patch does not apply to net-next

Commit Message

Xuan Zhuo Nov. 7, 2023, 3:12 a.m. UTC
virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
buffer) by the last bits of the pointer.

Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
 drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
 drivers/net/virtio/xsk.h        |  5 +++++
 2 files changed, 21 insertions(+), 2 deletions(-)

Comments

Michael S. Tsirkin Nov. 9, 2023, 11:11 a.m. UTC | #1
On Tue, Nov 07, 2023 at 11:12:20AM +0800, Xuan Zhuo wrote:
> virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
> buffer) by the last bits of the pointer.
> 
> Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> ---
>  drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
>  drivers/net/virtio/xsk.h        |  5 +++++
>  2 files changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
> index a431a2c1ee47..a13d6d301fdb 100644
> --- a/drivers/net/virtio/virtio_net.h
> +++ b/drivers/net/virtio/virtio_net.h
> @@ -225,6 +225,11 @@ struct virtnet_info {
>  	struct failover *failover;
>  };
>  
> +static inline bool virtnet_is_skb_ptr(void *ptr)
> +{
> +	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
> +}
> +
>  static inline bool virtnet_is_xdp_frame(void *ptr)
>  {
>  	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> @@ -235,6 +240,8 @@ static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
>  	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
>  }
>  
> +static inline u32 virtnet_ptr_to_xsk(void *ptr);
> +

I don't understand why you need this here.


>  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
>  {
>  	struct virtnet_sq_dma *next, *head;
> @@ -261,11 +268,12 @@ static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
>  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
>  					 u64 *bytes, u64 *packets)
>  {
> +	unsigned int xsknum = 0;
>  	unsigned int len;
>  	void *ptr;
>  
>  	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> -		if (!virtnet_is_xdp_frame(ptr)) {
> +		if (virtnet_is_skb_ptr(ptr)) {
>  			struct sk_buff *skb;
>  
>  			if (sq->do_dma)
> @@ -277,7 +285,7 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
>  
>  			*bytes += skb->len;
>  			napi_consume_skb(skb, in_napi);
> -		} else {
> +		} else if (virtnet_is_xdp_frame(ptr)) {
>  			struct xdp_frame *frame;
>  
>  			if (sq->do_dma)
> @@ -287,9 +295,15 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
>  
>  			*bytes += xdp_get_frame_len(frame);
>  			xdp_return_frame(frame);
> +		} else {
> +			*bytes += virtnet_ptr_to_xsk(ptr);
> +			++xsknum;
>  		}
>  		(*packets)++;
>  	}
> +
> +	if (xsknum)
> +		xsk_tx_completed(sq->xsk.pool, xsknum);
>  }
>  
>  static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
> diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
> index 1bd19dcda649..7ebc9bda7aee 100644
> --- a/drivers/net/virtio/xsk.h
> +++ b/drivers/net/virtio/xsk.h
> @@ -14,6 +14,11 @@ static inline void *virtnet_xsk_to_ptr(u32 len)
>  	return (void *)(p | VIRTIO_XSK_FLAG);
>  }
>  
> +static inline u32 virtnet_ptr_to_xsk(void *ptr)
> +{
> +	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
> +}
> +
>  int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
>  bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
>  		      int budget);
> -- 
> 2.32.0.3.g01195cf9f
Xuan Zhuo Nov. 9, 2023, 11:16 a.m. UTC | #2
On Thu, 9 Nov 2023 06:11:49 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> On Tue, Nov 07, 2023 at 11:12:20AM +0800, Xuan Zhuo wrote:
> > virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
> > buffer) by the last bits of the pointer.
> >
> > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > ---
> >  drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
> >  drivers/net/virtio/xsk.h        |  5 +++++
> >  2 files changed, 21 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
> > index a431a2c1ee47..a13d6d301fdb 100644
> > --- a/drivers/net/virtio/virtio_net.h
> > +++ b/drivers/net/virtio/virtio_net.h
> > @@ -225,6 +225,11 @@ struct virtnet_info {
> >  	struct failover *failover;
> >  };
> >
> > +static inline bool virtnet_is_skb_ptr(void *ptr)
> > +{
> > +	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
> > +}
> > +
> >  static inline bool virtnet_is_xdp_frame(void *ptr)
> >  {
> >  	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> > @@ -235,6 +240,8 @@ static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
> >  	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
> >  }
> >
> > +static inline u32 virtnet_ptr_to_xsk(void *ptr);
> > +
>
> I don't understand why you need this here.

The below function virtnet_free_old_xmit needs this.

Thanks.

>
>
> >  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> >  {
> >  	struct virtnet_sq_dma *next, *head;
> > @@ -261,11 +268,12 @@ static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> >  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> >  					 u64 *bytes, u64 *packets)
> >  {
> > +	unsigned int xsknum = 0;
> >  	unsigned int len;
> >  	void *ptr;
> >
> >  	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> > -		if (!virtnet_is_xdp_frame(ptr)) {
> > +		if (virtnet_is_skb_ptr(ptr)) {
> >  			struct sk_buff *skb;
> >
> >  			if (sq->do_dma)
> > @@ -277,7 +285,7 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> >
> >  			*bytes += skb->len;
> >  			napi_consume_skb(skb, in_napi);
> > -		} else {
> > +		} else if (virtnet_is_xdp_frame(ptr)) {
> >  			struct xdp_frame *frame;
> >
> >  			if (sq->do_dma)
> > @@ -287,9 +295,15 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> >
> >  			*bytes += xdp_get_frame_len(frame);
> >  			xdp_return_frame(frame);
> > +		} else {
> > +			*bytes += virtnet_ptr_to_xsk(ptr);
> > +			++xsknum;
> >  		}
> >  		(*packets)++;
> >  	}
> > +
> > +	if (xsknum)
> > +		xsk_tx_completed(sq->xsk.pool, xsknum);
> >  }
> >
> >  static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
> > diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
> > index 1bd19dcda649..7ebc9bda7aee 100644
> > --- a/drivers/net/virtio/xsk.h
> > +++ b/drivers/net/virtio/xsk.h
> > @@ -14,6 +14,11 @@ static inline void *virtnet_xsk_to_ptr(u32 len)
> >  	return (void *)(p | VIRTIO_XSK_FLAG);
> >  }
> >
> > +static inline u32 virtnet_ptr_to_xsk(void *ptr)
> > +{
> > +	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
> > +}
> > +
> >  int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
> >  bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
> >  		      int budget);
> > --
> > 2.32.0.3.g01195cf9f
>
>
Michael S. Tsirkin Nov. 9, 2023, 11:59 a.m. UTC | #3
On Thu, Nov 09, 2023 at 07:16:08PM +0800, Xuan Zhuo wrote:
> On Thu, 9 Nov 2023 06:11:49 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > On Tue, Nov 07, 2023 at 11:12:20AM +0800, Xuan Zhuo wrote:
> > > virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
> > > buffer) by the last bits of the pointer.
> > >
> > > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > > ---
> > >  drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
> > >  drivers/net/virtio/xsk.h        |  5 +++++
> > >  2 files changed, 21 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
> > > index a431a2c1ee47..a13d6d301fdb 100644
> > > --- a/drivers/net/virtio/virtio_net.h
> > > +++ b/drivers/net/virtio/virtio_net.h
> > > @@ -225,6 +225,11 @@ struct virtnet_info {
> > >  	struct failover *failover;
> > >  };
> > >
> > > +static inline bool virtnet_is_skb_ptr(void *ptr)
> > > +{
> > > +	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
> > > +}
> > > +
> > >  static inline bool virtnet_is_xdp_frame(void *ptr)
> > >  {
> > >  	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> > > @@ -235,6 +240,8 @@ static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
> > >  	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
> > >  }
> > >
> > > +static inline u32 virtnet_ptr_to_xsk(void *ptr);
> > > +
> >
> > I don't understand why you need this here.
> 
> The below function virtnet_free_old_xmit needs this.
> 
> Thanks.

I don't understand why is virtnet_free_old_xmit inline, either.

> >
> >
> > >  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > >  {
> > >  	struct virtnet_sq_dma *next, *head;
> > > @@ -261,11 +268,12 @@ static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > >  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > >  					 u64 *bytes, u64 *packets)
> > >  {
> > > +	unsigned int xsknum = 0;
> > >  	unsigned int len;
> > >  	void *ptr;
> > >
> > >  	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> > > -		if (!virtnet_is_xdp_frame(ptr)) {
> > > +		if (virtnet_is_skb_ptr(ptr)) {
> > >  			struct sk_buff *skb;
> > >
> > >  			if (sq->do_dma)
> > > @@ -277,7 +285,7 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > >
> > >  			*bytes += skb->len;
> > >  			napi_consume_skb(skb, in_napi);
> > > -		} else {
> > > +		} else if (virtnet_is_xdp_frame(ptr)) {
> > >  			struct xdp_frame *frame;
> > >
> > >  			if (sq->do_dma)
> > > @@ -287,9 +295,15 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > >
> > >  			*bytes += xdp_get_frame_len(frame);
> > >  			xdp_return_frame(frame);
> > > +		} else {
> > > +			*bytes += virtnet_ptr_to_xsk(ptr);
> > > +			++xsknum;
> > >  		}
> > >  		(*packets)++;
> > >  	}
> > > +
> > > +	if (xsknum)
> > > +		xsk_tx_completed(sq->xsk.pool, xsknum);
> > >  }
> > >
> > >  static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
> > > diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
> > > index 1bd19dcda649..7ebc9bda7aee 100644
> > > --- a/drivers/net/virtio/xsk.h
> > > +++ b/drivers/net/virtio/xsk.h
> > > @@ -14,6 +14,11 @@ static inline void *virtnet_xsk_to_ptr(u32 len)
> > >  	return (void *)(p | VIRTIO_XSK_FLAG);
> > >  }
> > >
> > > +static inline u32 virtnet_ptr_to_xsk(void *ptr)
> > > +{
> > > +	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
> > > +}
> > > +
> > >  int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
> > >  bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
> > >  		      int budget);
> > > --
> > > 2.32.0.3.g01195cf9f
> >
> >
Xuan Zhuo Nov. 10, 2023, 1:44 a.m. UTC | #4
On Thu, 9 Nov 2023 06:59:48 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> On Thu, Nov 09, 2023 at 07:16:08PM +0800, Xuan Zhuo wrote:
> > On Thu, 9 Nov 2023 06:11:49 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > > On Tue, Nov 07, 2023 at 11:12:20AM +0800, Xuan Zhuo wrote:
> > > > virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
> > > > buffer) by the last bits of the pointer.
> > > >
> > > > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > > > ---
> > > >  drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
> > > >  drivers/net/virtio/xsk.h        |  5 +++++
> > > >  2 files changed, 21 insertions(+), 2 deletions(-)
> > > >
> > > > diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
> > > > index a431a2c1ee47..a13d6d301fdb 100644
> > > > --- a/drivers/net/virtio/virtio_net.h
> > > > +++ b/drivers/net/virtio/virtio_net.h
> > > > @@ -225,6 +225,11 @@ struct virtnet_info {
> > > >  	struct failover *failover;
> > > >  };
> > > >
> > > > +static inline bool virtnet_is_skb_ptr(void *ptr)
> > > > +{
> > > > +	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
> > > > +}
> > > > +
> > > >  static inline bool virtnet_is_xdp_frame(void *ptr)
> > > >  {
> > > >  	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> > > > @@ -235,6 +240,8 @@ static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
> > > >  	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
> > > >  }
> > > >
> > > > +static inline u32 virtnet_ptr_to_xsk(void *ptr);
> > > > +
> > >
> > > I don't understand why you need this here.
> >
> > The below function virtnet_free_old_xmit needs this.
> >
> > Thanks.
>
> I don't understand why is virtnet_free_old_xmit inline, either.

That is in the header file.


>
> > >
> > >
> > > >  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > > >  {
> > > >  	struct virtnet_sq_dma *next, *head;
> > > > @@ -261,11 +268,12 @@ static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > > >  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > >  					 u64 *bytes, u64 *packets)
> > > >  {
> > > > +	unsigned int xsknum = 0;
> > > >  	unsigned int len;
> > > >  	void *ptr;
> > > >
> > > >  	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> > > > -		if (!virtnet_is_xdp_frame(ptr)) {
> > > > +		if (virtnet_is_skb_ptr(ptr)) {
> > > >  			struct sk_buff *skb;
> > > >
> > > >  			if (sq->do_dma)
> > > > @@ -277,7 +285,7 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > >
> > > >  			*bytes += skb->len;
> > > >  			napi_consume_skb(skb, in_napi);
> > > > -		} else {
> > > > +		} else if (virtnet_is_xdp_frame(ptr)) {
> > > >  			struct xdp_frame *frame;
> > > >
> > > >  			if (sq->do_dma)
> > > > @@ -287,9 +295,15 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > >
> > > >  			*bytes += xdp_get_frame_len(frame);
> > > >  			xdp_return_frame(frame);
> > > > +		} else {
> > > > +			*bytes += virtnet_ptr_to_xsk(ptr);
> > > > +			++xsknum;
> > > >  		}
> > > >  		(*packets)++;
> > > >  	}
> > > > +
> > > > +	if (xsknum)
> > > > +		xsk_tx_completed(sq->xsk.pool, xsknum);
> > > >  }
> > > >
> > > >  static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
> > > > diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
> > > > index 1bd19dcda649..7ebc9bda7aee 100644
> > > > --- a/drivers/net/virtio/xsk.h
> > > > +++ b/drivers/net/virtio/xsk.h
> > > > @@ -14,6 +14,11 @@ static inline void *virtnet_xsk_to_ptr(u32 len)
> > > >  	return (void *)(p | VIRTIO_XSK_FLAG);
> > > >  }
> > > >
> > > > +static inline u32 virtnet_ptr_to_xsk(void *ptr)
> > > > +{
> > > > +	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
> > > > +}
> > > > +
> > > >  int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
> > > >  bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
> > > >  		      int budget);
> > > > --
> > > > 2.32.0.3.g01195cf9f
> > >
> > >
>
Michael S. Tsirkin Nov. 10, 2023, 5:32 a.m. UTC | #5
On Fri, Nov 10, 2023 at 09:44:32AM +0800, Xuan Zhuo wrote:
> On Thu, 9 Nov 2023 06:59:48 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > On Thu, Nov 09, 2023 at 07:16:08PM +0800, Xuan Zhuo wrote:
> > > On Thu, 9 Nov 2023 06:11:49 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > > > On Tue, Nov 07, 2023 at 11:12:20AM +0800, Xuan Zhuo wrote:
> > > > > virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
> > > > > buffer) by the last bits of the pointer.
> > > > >
> > > > > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > > > > ---
> > > > >  drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
> > > > >  drivers/net/virtio/xsk.h        |  5 +++++
> > > > >  2 files changed, 21 insertions(+), 2 deletions(-)
> > > > >
> > > > > diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
> > > > > index a431a2c1ee47..a13d6d301fdb 100644
> > > > > --- a/drivers/net/virtio/virtio_net.h
> > > > > +++ b/drivers/net/virtio/virtio_net.h
> > > > > @@ -225,6 +225,11 @@ struct virtnet_info {
> > > > >  	struct failover *failover;
> > > > >  };
> > > > >
> > > > > +static inline bool virtnet_is_skb_ptr(void *ptr)
> > > > > +{
> > > > > +	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
> > > > > +}
> > > > > +
> > > > >  static inline bool virtnet_is_xdp_frame(void *ptr)
> > > > >  {
> > > > >  	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> > > > > @@ -235,6 +240,8 @@ static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
> > > > >  	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
> > > > >  }
> > > > >
> > > > > +static inline u32 virtnet_ptr_to_xsk(void *ptr);
> > > > > +
> > > >
> > > > I don't understand why you need this here.
> > >
> > > The below function virtnet_free_old_xmit needs this.
> > >
> > > Thanks.
> >
> > I don't understand why is virtnet_free_old_xmit inline, either.
> 
> That is in the header file.
> 

It does not belong there.


> >
> > > >
> > > >
> > > > >  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > > > >  {
> > > > >  	struct virtnet_sq_dma *next, *head;
> > > > > @@ -261,11 +268,12 @@ static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > > > >  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > > >  					 u64 *bytes, u64 *packets)
> > > > >  {
> > > > > +	unsigned int xsknum = 0;
> > > > >  	unsigned int len;
> > > > >  	void *ptr;
> > > > >
> > > > >  	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> > > > > -		if (!virtnet_is_xdp_frame(ptr)) {
> > > > > +		if (virtnet_is_skb_ptr(ptr)) {
> > > > >  			struct sk_buff *skb;
> > > > >
> > > > >  			if (sq->do_dma)
> > > > > @@ -277,7 +285,7 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > > >
> > > > >  			*bytes += skb->len;
> > > > >  			napi_consume_skb(skb, in_napi);
> > > > > -		} else {
> > > > > +		} else if (virtnet_is_xdp_frame(ptr)) {
> > > > >  			struct xdp_frame *frame;
> > > > >
> > > > >  			if (sq->do_dma)
> > > > > @@ -287,9 +295,15 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > > >
> > > > >  			*bytes += xdp_get_frame_len(frame);
> > > > >  			xdp_return_frame(frame);
> > > > > +		} else {
> > > > > +			*bytes += virtnet_ptr_to_xsk(ptr);
> > > > > +			++xsknum;
> > > > >  		}
> > > > >  		(*packets)++;
> > > > >  	}
> > > > > +
> > > > > +	if (xsknum)
> > > > > +		xsk_tx_completed(sq->xsk.pool, xsknum);
> > > > >  }
> > > > >
> > > > >  static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
> > > > > diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
> > > > > index 1bd19dcda649..7ebc9bda7aee 100644
> > > > > --- a/drivers/net/virtio/xsk.h
> > > > > +++ b/drivers/net/virtio/xsk.h
> > > > > @@ -14,6 +14,11 @@ static inline void *virtnet_xsk_to_ptr(u32 len)
> > > > >  	return (void *)(p | VIRTIO_XSK_FLAG);
> > > > >  }
> > > > >
> > > > > +static inline u32 virtnet_ptr_to_xsk(void *ptr)
> > > > > +{
> > > > > +	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
> > > > > +}
> > > > > +
> > > > >  int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
> > > > >  bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
> > > > >  		      int budget);
> > > > > --
> > > > > 2.32.0.3.g01195cf9f
> > > >
> > > >
> >
Xuan Zhuo Nov. 10, 2023, 5:50 a.m. UTC | #6
On Fri, 10 Nov 2023 00:32:50 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> On Fri, Nov 10, 2023 at 09:44:32AM +0800, Xuan Zhuo wrote:
> > On Thu, 9 Nov 2023 06:59:48 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > > On Thu, Nov 09, 2023 at 07:16:08PM +0800, Xuan Zhuo wrote:
> > > > On Thu, 9 Nov 2023 06:11:49 -0500, "Michael S. Tsirkin" <mst@redhat.com> wrote:
> > > > > On Tue, Nov 07, 2023 at 11:12:20AM +0800, Xuan Zhuo wrote:
> > > > > > virtnet_free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk
> > > > > > buffer) by the last bits of the pointer.
> > > > > >
> > > > > > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > > > > > ---
> > > > > >  drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
> > > > > >  drivers/net/virtio/xsk.h        |  5 +++++
> > > > > >  2 files changed, 21 insertions(+), 2 deletions(-)
> > > > > >
> > > > > > diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
> > > > > > index a431a2c1ee47..a13d6d301fdb 100644
> > > > > > --- a/drivers/net/virtio/virtio_net.h
> > > > > > +++ b/drivers/net/virtio/virtio_net.h
> > > > > > @@ -225,6 +225,11 @@ struct virtnet_info {
> > > > > >  	struct failover *failover;
> > > > > >  };
> > > > > >
> > > > > > +static inline bool virtnet_is_skb_ptr(void *ptr)
> > > > > > +{
> > > > > > +	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
> > > > > > +}
> > > > > > +
> > > > > >  static inline bool virtnet_is_xdp_frame(void *ptr)
> > > > > >  {
> > > > > >  	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> > > > > > @@ -235,6 +240,8 @@ static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
> > > > > >  	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
> > > > > >  }
> > > > > >
> > > > > > +static inline u32 virtnet_ptr_to_xsk(void *ptr);
> > > > > > +
> > > > >
> > > > > I don't understand why you need this here.
> > > >
> > > > The below function virtnet_free_old_xmit needs this.
> > > >
> > > > Thanks.
> > >
> > > I don't understand why is virtnet_free_old_xmit inline, either.
> >
> > That is in the header file.
> >
>
> It does not belong there.


Do you mean virtnet_free_old_xmit?

That will be called by xsk.c. So I move it to the header file.

Thanks.


>
>
> > >
> > > > >
> > > > >
> > > > > >  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > > > > >  {
> > > > > >  	struct virtnet_sq_dma *next, *head;
> > > > > > @@ -261,11 +268,12 @@ static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
> > > > > >  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > > > >  					 u64 *bytes, u64 *packets)
> > > > > >  {
> > > > > > +	unsigned int xsknum = 0;
> > > > > >  	unsigned int len;
> > > > > >  	void *ptr;
> > > > > >
> > > > > >  	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> > > > > > -		if (!virtnet_is_xdp_frame(ptr)) {
> > > > > > +		if (virtnet_is_skb_ptr(ptr)) {
> > > > > >  			struct sk_buff *skb;
> > > > > >
> > > > > >  			if (sq->do_dma)
> > > > > > @@ -277,7 +285,7 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > > > >
> > > > > >  			*bytes += skb->len;
> > > > > >  			napi_consume_skb(skb, in_napi);
> > > > > > -		} else {
> > > > > > +		} else if (virtnet_is_xdp_frame(ptr)) {
> > > > > >  			struct xdp_frame *frame;
> > > > > >
> > > > > >  			if (sq->do_dma)
> > > > > > @@ -287,9 +295,15 @@ static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
> > > > > >
> > > > > >  			*bytes += xdp_get_frame_len(frame);
> > > > > >  			xdp_return_frame(frame);
> > > > > > +		} else {
> > > > > > +			*bytes += virtnet_ptr_to_xsk(ptr);
> > > > > > +			++xsknum;
> > > > > >  		}
> > > > > >  		(*packets)++;
> > > > > >  	}
> > > > > > +
> > > > > > +	if (xsknum)
> > > > > > +		xsk_tx_completed(sq->xsk.pool, xsknum);
> > > > > >  }
> > > > > >
> > > > > >  static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
> > > > > > diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
> > > > > > index 1bd19dcda649..7ebc9bda7aee 100644
> > > > > > --- a/drivers/net/virtio/xsk.h
> > > > > > +++ b/drivers/net/virtio/xsk.h
> > > > > > @@ -14,6 +14,11 @@ static inline void *virtnet_xsk_to_ptr(u32 len)
> > > > > >  	return (void *)(p | VIRTIO_XSK_FLAG);
> > > > > >  }
> > > > > >
> > > > > > +static inline u32 virtnet_ptr_to_xsk(void *ptr)
> > > > > > +{
> > > > > > +	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
> > > > > > +}
> > > > > > +
> > > > > >  int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
> > > > > >  bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
> > > > > >  		      int budget);
> > > > > > --
> > > > > > 2.32.0.3.g01195cf9f
> > > > >
> > > > >
> > >
>
diff mbox series

Patch

diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
index a431a2c1ee47..a13d6d301fdb 100644
--- a/drivers/net/virtio/virtio_net.h
+++ b/drivers/net/virtio/virtio_net.h
@@ -225,6 +225,11 @@  struct virtnet_info {
 	struct failover *failover;
 };
 
+static inline bool virtnet_is_skb_ptr(void *ptr)
+{
+	return !((unsigned long)ptr & VIRTIO_XMIT_DATA_MASK);
+}
+
 static inline bool virtnet_is_xdp_frame(void *ptr)
 {
 	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
@@ -235,6 +240,8 @@  static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
 	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
 }
 
+static inline u32 virtnet_ptr_to_xsk(void *ptr);
+
 static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
 {
 	struct virtnet_sq_dma *next, *head;
@@ -261,11 +268,12 @@  static inline void *virtnet_sq_unmap(struct virtnet_sq *sq, void *data)
 static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
 					 u64 *bytes, u64 *packets)
 {
+	unsigned int xsknum = 0;
 	unsigned int len;
 	void *ptr;
 
 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
-		if (!virtnet_is_xdp_frame(ptr)) {
+		if (virtnet_is_skb_ptr(ptr)) {
 			struct sk_buff *skb;
 
 			if (sq->do_dma)
@@ -277,7 +285,7 @@  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
 
 			*bytes += skb->len;
 			napi_consume_skb(skb, in_napi);
-		} else {
+		} else if (virtnet_is_xdp_frame(ptr)) {
 			struct xdp_frame *frame;
 
 			if (sq->do_dma)
@@ -287,9 +295,15 @@  static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
 
 			*bytes += xdp_get_frame_len(frame);
 			xdp_return_frame(frame);
+		} else {
+			*bytes += virtnet_ptr_to_xsk(ptr);
+			++xsknum;
 		}
 		(*packets)++;
 	}
+
+	if (xsknum)
+		xsk_tx_completed(sq->xsk.pool, xsknum);
 }
 
 static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
diff --git a/drivers/net/virtio/xsk.h b/drivers/net/virtio/xsk.h
index 1bd19dcda649..7ebc9bda7aee 100644
--- a/drivers/net/virtio/xsk.h
+++ b/drivers/net/virtio/xsk.h
@@ -14,6 +14,11 @@  static inline void *virtnet_xsk_to_ptr(u32 len)
 	return (void *)(p | VIRTIO_XSK_FLAG);
 }
 
+static inline u32 virtnet_ptr_to_xsk(void *ptr)
+{
+	return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
+}
+
 int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp);
 bool virtnet_xsk_xmit(struct virtnet_sq *sq, struct xsk_buff_pool *pool,
 		      int budget);