BPF List
 help / color / mirror / Atom feed
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
To: Jason Wang <jasowang@redhat.com>
Cc: netdev@vger.kernel.org, "Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Jesper Dangaard Brouer" <hawk@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	virtualization@lists.linux.dev, bpf@vger.kernel.org
Subject: Re: [PATCH net-next 07/13] virtio_net: refactor the xmit type
Date: Thu, 12 Sep 2024 15:50:09 +0800	[thread overview]
Message-ID: <1726127409.3427224-4-xuanzhuo@linux.alibaba.com> (raw)
In-Reply-To: <CACGkMEuDg800zy+-W7VRY5Ns4COsmvMP_kpHdzJ-ws8PuMoGhA@mail.gmail.com>

On Wed, 11 Sep 2024 12:04:16 +0800, Jason Wang <jasowang@redhat.com> wrote:
> On Tue, Aug 20, 2024 at 3:33 PM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
> >
> > Because the af-xdp will introduce a new xmit type, so I refactor the
> > xmit type mechanism first.
> >
> > We use the last two bits of the pointer to distinguish the xmit type,
> > so we can distinguish four xmit types. Now we have three types: skb,
> > orphan and xdp.
>
> And if I was not wrong, we do not anymore use bitmasks. If yes, let's
> explain the reason here.

In general, pointers are aligned to 4 or 8 bytes. If it is aligned to 4 bytes,
then only two bits are free for a pointer. So we can only use two bits.

But there are 4 types here, so we can't use bits to distinguish them.

b00 for skb
b01 for SKB_ORPHAN
b10 for XDP
b11 for af-xdp tx


>
> >
> > Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
> > ---
> >  drivers/net/virtio_net.c | 90 +++++++++++++++++++++++-----------------
> >  1 file changed, 51 insertions(+), 39 deletions(-)
> >
> > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> > index 41aaea3b90fd..96abee36738b 100644
> > --- a/drivers/net/virtio_net.c
> > +++ b/drivers/net/virtio_net.c
> > @@ -45,9 +45,6 @@ module_param(napi_tx, bool, 0644);
> >  #define VIRTIO_XDP_TX          BIT(0)
> >  #define VIRTIO_XDP_REDIR       BIT(1)
> >
> > -#define VIRTIO_XDP_FLAG                BIT(0)
> > -#define VIRTIO_ORPHAN_FLAG     BIT(1)
> > -
> >  /* RX packet size EWMA. The average packet size is used to determine the packet
> >   * buffer size when refilling RX rings. As the entire RX ring may be refilled
> >   * at once, the weight is chosen so that the EWMA will be insensitive to short-
> > @@ -509,34 +506,35 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
> >                                                struct page *page, void *buf,
> >                                                int len, int truesize);
> >
> > -static bool is_xdp_frame(void *ptr)
> > -{
> > -       return (unsigned long)ptr & VIRTIO_XDP_FLAG;
> > -}
> > +enum virtnet_xmit_type {
> > +       VIRTNET_XMIT_TYPE_SKB,
> > +       VIRTNET_XMIT_TYPE_ORPHAN,
>
> Let's rename this to SKB_ORPHAN?
>
> > +       VIRTNET_XMIT_TYPE_XDP,
> > +};
> >
> > -static void *xdp_to_ptr(struct xdp_frame *ptr)
> > -{
> > -       return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
> > -}
> > +#define VIRTNET_XMIT_TYPE_MASK (VIRTNET_XMIT_TYPE_SKB | VIRTNET_XMIT_TYPE_ORPHAN \
> > +                               | VIRTNET_XMIT_TYPE_XDP)
>

Maybe I should define VIRTNET_XMIT_TYPE_MASK to 0x3 directly with some comments.

Thanks.


> I may miss something but it seems not a correct bitmask definition as
> each member is not a bit actually?
>
> >
> > -static struct xdp_frame *ptr_to_xdp(void *ptr)
> > +static enum virtnet_xmit_type virtnet_xmit_ptr_strip(void **ptr)
> >  {
> > -       return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
> > -}
> > +       unsigned long p = (unsigned long)*ptr;
> >
> > -static bool is_orphan_skb(void *ptr)
> > -{
> > -       return (unsigned long)ptr & VIRTIO_ORPHAN_FLAG;
> > +       *ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
> > +
> > +       return p & VIRTNET_XMIT_TYPE_MASK;
> >  }
> >
> > -static void *skb_to_ptr(struct sk_buff *skb, bool orphan)
> > +static void *virtnet_xmit_ptr_mix(void *ptr, enum virtnet_xmit_type type)
> >  {
> > -       return (void *)((unsigned long)skb | (orphan ? VIRTIO_ORPHAN_FLAG : 0));
> > +       return (void *)((unsigned long)ptr | type);
> >  }
> >
> > -static struct sk_buff *ptr_to_skb(void *ptr)
> > +static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
> > +                             enum virtnet_xmit_type type)
> >  {
> > -       return (struct sk_buff *)((unsigned long)ptr & ~VIRTIO_ORPHAN_FLAG);
> > +       return virtqueue_add_outbuf(sq->vq, sq->sg, num,
> > +                                   virtnet_xmit_ptr_mix(data, type),
> > +                                   GFP_ATOMIC);
> >  }
> >
> >  static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
> > @@ -549,29 +547,37 @@ static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
> >  static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
> >                             bool in_napi, struct virtnet_sq_free_stats *stats)
> >  {
> > +       struct xdp_frame *frame;
> > +       struct sk_buff *skb;
> >         unsigned int len;
> >         void *ptr;
> >
> >         while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
> > -               if (!is_xdp_frame(ptr)) {
> > -                       struct sk_buff *skb = ptr_to_skb(ptr);
> > +               switch (virtnet_xmit_ptr_strip(&ptr)) {
> > +               case VIRTNET_XMIT_TYPE_SKB:
> > +                       skb = ptr;
> >
> >                         pr_debug("Sent skb %p\n", skb);
> > +                       stats->napi_packets++;
> > +                       stats->napi_bytes += skb->len;
> > +                       napi_consume_skb(skb, in_napi);
> > +                       break;
> >
> > -                       if (is_orphan_skb(ptr)) {
> > -                               stats->packets++;
> > -                               stats->bytes += skb->len;
> > -                       } else {
> > -                               stats->napi_packets++;
> > -                               stats->napi_bytes += skb->len;
> > -                       }
> > +               case VIRTNET_XMIT_TYPE_ORPHAN:
> > +                       skb = ptr;
> > +
> > +                       stats->packets++;
> > +                       stats->bytes += skb->len;
> >                         napi_consume_skb(skb, in_napi);
> > -               } else {
> > -                       struct xdp_frame *frame = ptr_to_xdp(ptr);
> > +                       break;
> > +
> > +               case VIRTNET_XMIT_TYPE_XDP:
> > +                       frame = ptr;
> >
> >                         stats->packets++;
> >                         stats->bytes += xdp_get_frame_len(frame);
> >                         xdp_return_frame(frame);
> > +                       break;
> >                 }
> >         }
> >         netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
> > @@ -1421,8 +1427,7 @@ static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
> >                             skb_frag_size(frag), skb_frag_off(frag));
> >         }
> >
> > -       err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
> > -                                  xdp_to_ptr(xdpf), GFP_ATOMIC);
> > +       err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
> >         if (unlikely(err))
> >                 return -ENOSPC; /* Caller handle free/refcnt */
> >
> > @@ -3028,8 +3033,9 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
> >                         return num_sg;
> >                 num_sg++;
> >         }
> > -       return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg,
> > -                                   skb_to_ptr(skb, orphan), GFP_ATOMIC);
> > +
> > +       return virtnet_add_outbuf(sq, num_sg, skb,
> > +                                 orphan ? VIRTNET_XMIT_TYPE_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
> >  }
> >
> >  static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
> > @@ -5906,10 +5912,16 @@ static void free_receive_page_frags(struct virtnet_info *vi)
> >
> >  static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
> >  {
> > -       if (!is_xdp_frame(buf))
> > +       switch (virtnet_xmit_ptr_strip(&buf)) {
> > +       case VIRTNET_XMIT_TYPE_SKB:
> > +       case VIRTNET_XMIT_TYPE_ORPHAN:
> >                 dev_kfree_skb(buf);
> > -       else
> > -               xdp_return_frame(ptr_to_xdp(buf));
> > +               break;
> > +
> > +       case VIRTNET_XMIT_TYPE_XDP:
> > +               xdp_return_frame(buf);
> > +               break;
> > +       }
> >  }
>
> Others look fine.
>
> Thanks
>
> >
> >  static void free_unused_bufs(struct virtnet_info *vi)
> > --
> > 2.32.0.3.g01195cf9f
> >
>

  reply	other threads:[~2024-09-12  7:54 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-20  7:33 [PATCH net-next 00/13] virtio-net: support AF_XDP zero copy (tx) Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 01/13] virtio_ring: introduce vring_need_unmap_buffer Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 02/13] virtio_ring: split: harden dma unmap for indirect Xuan Zhuo
2024-09-11  3:46   ` Jason Wang
2024-09-11 10:30     ` Michael S. Tsirkin
2024-09-12  7:30     ` Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 03/13] virtio_ring: packed: " Xuan Zhuo
2024-08-21  8:54   ` Dan Carpenter
2024-09-11 11:28   ` Michael S. Tsirkin
2024-09-12  6:55     ` Xuan Zhuo
2024-09-12  7:38       ` Michael S. Tsirkin
2024-09-12  7:43         ` Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 04/13] virtio_ring: perform premapped operations based on per-buffer Xuan Zhuo
2024-09-11  3:54   ` Jason Wang
2024-09-12  7:36     ` Xuan Zhuo
2024-09-13  3:36       ` Jason Wang
2024-08-20  7:33 ` [PATCH net-next 05/13] virtio-net: rq submits premapped buffer per buffer Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 06/13] virtio_ring: remove API virtqueue_set_dma_premapped Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 07/13] virtio_net: refactor the xmit type Xuan Zhuo
2024-09-11  4:04   ` Jason Wang
2024-09-12  7:50     ` Xuan Zhuo [this message]
2024-09-13  3:22       ` Jason Wang
2024-08-20  7:33 ` [PATCH net-next 08/13] virtio_net: xsk: bind/unbind xsk for tx Xuan Zhuo
2024-09-11  4:08   ` Jason Wang
2024-09-12  7:54     ` Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 09/13] virtio_net: xsk: prevent disable tx napi Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 10/13] virtio_net: xsk: tx: support xmit xsk buffer Xuan Zhuo
2024-09-11  4:31   ` Jason Wang
2024-09-12  8:48     ` Xuan Zhuo
2024-09-13  3:21       ` Jason Wang
2024-08-20  7:33 ` [PATCH net-next 11/13] virtio_net: xsk: tx: handle the transmitted " Xuan Zhuo
2024-09-11  4:32   ` Jason Wang
2024-09-12  7:55     ` Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 12/13] virtio_net: update tx timeout record Xuan Zhuo
2024-08-20  7:33 ` [PATCH net-next 13/13] virtio_net: xdp_features add NETDEV_XDP_ACT_XSK_ZEROCOPY Xuan Zhuo
2024-09-11  4:33   ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1726127409.3427224-4-xuanzhuo@linux.alibaba.com \
    --to=xuanzhuo@linux.alibaba.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eperezma@redhat.com \
    --cc=hawk@kernel.org \
    --cc=jasowang@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox