From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> To: netdev@vger.kernel.org Cc: "David S. Miller" <davem@davemloft.net>, "Jakub Kicinski" <kuba@kernel.org>, "Michael S. Tsirkin" <mst@redhat.com>, "Jason Wang" <jasowang@redhat.com>, "Björn Töpel" <bjorn@kernel.org>, "Magnus Karlsson" <magnus.karlsson@intel.com>, "Jonathan Lemon" <jonathan.lemon@gmail.com>, "Alexei Starovoitov" <ast@kernel.org>, "Daniel Borkmann" <daniel@iogearbox.net>, "Jesper Dangaard Brouer" <hawk@kernel.org>, "John Fastabend" <john.fastabend@gmail.com>, "Andrii Nakryiko" <andrii@kernel.org>, "Martin KaFai Lau" <kafai@fb.com>, "Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>, "KP Singh" <kpsingh@kernel.org>, "Xuan Zhuo" <xuanzhuo@linux.alibaba.com>, virtualization@lists.linux-foundation.org, bpf@vger.kernel.org, "dust . li" <dust.li@linux.alibaba.com> Subject: [PATCH net-next v5 06/15] virtio-net: unify the code for recycling the xmit ptr Date: Thu, 10 Jun 2021 16:22:00 +0800 [thread overview] Message-ID: <20210610082209.91487-7-xuanzhuo@linux.alibaba.com> (raw) In-Reply-To: <20210610082209.91487-1-xuanzhuo@linux.alibaba.com> Now there are two types of "skb" and "xdp frame" during recycling old xmit. There are two completely similar and independent implementations. This is inconvenient for the subsequent addition of new types. So extract a function from this piece of code and call this function uniformly to recover old xmit ptr. Rename free_old_xmit_skbs() to free_old_xmit(). Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com> --- drivers/net/virtio_net.c | 86 ++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 6c1233f0ab3e..d791543a8dd8 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -264,6 +264,30 @@ static struct xdp_frame *ptr_to_xdp(void *ptr) return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); } +static void __free_old_xmit(struct send_queue *sq, bool in_napi, + struct virtnet_sq_stats *stats) +{ + unsigned int len; + void *ptr; + + while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { + if (!is_xdp_frame(ptr)) { + struct sk_buff *skb = ptr; + + pr_debug("Sent skb %p\n", skb); + + stats->bytes += skb->len; + napi_consume_skb(skb, in_napi); + } else { + struct xdp_frame *frame = ptr_to_xdp(ptr); + + stats->bytes += frame->len; + xdp_return_frame(frame); + } + stats->packets++; + } +} + /* Converting between virtqueue no. and kernel tx/rx queue no. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq */ @@ -572,15 +596,12 @@ static int virtnet_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames, u32 flags) { struct virtnet_info *vi = netdev_priv(dev); + struct virtnet_sq_stats stats = {}; struct receive_queue *rq = vi->rq; struct bpf_prog *xdp_prog; struct send_queue *sq; - unsigned int len; - int packets = 0; - int bytes = 0; int nxmit = 0; int kicks = 0; - void *ptr; int ret; int i; @@ -599,20 +620,7 @@ static int virtnet_xdp_xmit(struct net_device *dev, } /* Free up any pending old buffers before queueing new ones. */ - while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { - if (likely(is_xdp_frame(ptr))) { - struct xdp_frame *frame = ptr_to_xdp(ptr); - - bytes += frame->len; - xdp_return_frame(frame); - } else { - struct sk_buff *skb = ptr; - - bytes += skb->len; - napi_consume_skb(skb, false); - } - packets++; - } + __free_old_xmit(sq, false, &stats); for (i = 0; i < n; i++) { struct xdp_frame *xdpf = frames[i]; @@ -629,8 +637,8 @@ static int virtnet_xdp_xmit(struct net_device *dev, } out: u64_stats_update_begin(&sq->stats.syncp); - sq->stats.bytes += bytes; - sq->stats.packets += packets; + sq->stats.bytes += stats.bytes; + sq->stats.packets += stats.packets; sq->stats.xdp_tx += n; sq->stats.xdp_tx_drops += n - nxmit; sq->stats.kicks += kicks; @@ -1459,39 +1467,21 @@ static int virtnet_receive(struct receive_queue *rq, int budget, return stats.packets; } -static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) +static void free_old_xmit(struct send_queue *sq, bool in_napi) { - unsigned int len; - unsigned int packets = 0; - unsigned int bytes = 0; - void *ptr; + struct virtnet_sq_stats stats = {}; - while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { - if (likely(!is_xdp_frame(ptr))) { - struct sk_buff *skb = ptr; - - pr_debug("Sent skb %p\n", skb); - - bytes += skb->len; - napi_consume_skb(skb, in_napi); - } else { - struct xdp_frame *frame = ptr_to_xdp(ptr); - - bytes += frame->len; - xdp_return_frame(frame); - } - packets++; - } + __free_old_xmit(sq, in_napi, &stats); /* Avoid overhead when no packets have been processed * happens when called speculatively from start_xmit. */ - if (!packets) + if (!stats.packets) return; u64_stats_update_begin(&sq->stats.syncp); - sq->stats.bytes += bytes; - sq->stats.packets += packets; + sq->stats.bytes += stats.bytes; + sq->stats.packets += stats.packets; u64_stats_update_end(&sq->stats.syncp); } @@ -1516,7 +1506,7 @@ static void virtnet_poll_cleantx(struct receive_queue *rq) return; if (__netif_tx_trylock(txq)) { - free_old_xmit_skbs(sq, true); + free_old_xmit(sq, true); __netif_tx_unlock(txq); } @@ -1601,7 +1591,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget) txq = netdev_get_tx_queue(vi->dev, index); __netif_tx_lock(txq, raw_smp_processor_id()); - free_old_xmit_skbs(sq, true); + free_old_xmit(sq, true); __netif_tx_unlock(txq); virtqueue_napi_complete(napi, sq->vq, 0); @@ -1670,7 +1660,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) bool use_napi = sq->napi.weight; /* Free up any pending old buffers before queueing new ones. */ - free_old_xmit_skbs(sq, false); + free_old_xmit(sq, false); if (use_napi && kick) virtqueue_enable_cb_delayed(sq->vq); @@ -1714,7 +1704,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) if (!use_napi && unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { /* More just got used, free them then recheck. */ - free_old_xmit_skbs(sq, false); + free_old_xmit(sq, false); if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { netif_start_subqueue(dev, qnum); virtqueue_disable_cb(sq->vq); -- 2.31.0
WARNING: multiple messages have this Message-ID (diff)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> To: netdev@vger.kernel.org Cc: "Song Liu" <songliubraving@fb.com>, "Martin KaFai Lau" <kafai@fb.com>, "Jesper Dangaard Brouer" <hawk@kernel.org>, "Daniel Borkmann" <daniel@iogearbox.net>, "Michael S. Tsirkin" <mst@redhat.com>, "Yonghong Song" <yhs@fb.com>, "John Fastabend" <john.fastabend@gmail.com>, "Alexei Starovoitov" <ast@kernel.org>, "Andrii Nakryiko" <andrii@kernel.org>, "Björn Töpel" <bjorn@kernel.org>, "dust . li" <dust.li@linux.alibaba.com>, "Jonathan Lemon" <jonathan.lemon@gmail.com>, "KP Singh" <kpsingh@kernel.org>, "Jakub Kicinski" <kuba@kernel.org>, bpf@vger.kernel.org, virtualization@lists.linux-foundation.org, "David S. Miller" <davem@davemloft.net>, "Magnus Karlsson" <magnus.karlsson@intel.com> Subject: [PATCH net-next v5 06/15] virtio-net: unify the code for recycling the xmit ptr Date: Thu, 10 Jun 2021 16:22:00 +0800 [thread overview] Message-ID: <20210610082209.91487-7-xuanzhuo@linux.alibaba.com> (raw) In-Reply-To: <20210610082209.91487-1-xuanzhuo@linux.alibaba.com> Now there are two types of "skb" and "xdp frame" during recycling old xmit. There are two completely similar and independent implementations. This is inconvenient for the subsequent addition of new types. So extract a function from this piece of code and call this function uniformly to recover old xmit ptr. Rename free_old_xmit_skbs() to free_old_xmit(). Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com> --- drivers/net/virtio_net.c | 86 ++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 6c1233f0ab3e..d791543a8dd8 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -264,6 +264,30 @@ static struct xdp_frame *ptr_to_xdp(void *ptr) return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG); } +static void __free_old_xmit(struct send_queue *sq, bool in_napi, + struct virtnet_sq_stats *stats) +{ + unsigned int len; + void *ptr; + + while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { + if (!is_xdp_frame(ptr)) { + struct sk_buff *skb = ptr; + + pr_debug("Sent skb %p\n", skb); + + stats->bytes += skb->len; + napi_consume_skb(skb, in_napi); + } else { + struct xdp_frame *frame = ptr_to_xdp(ptr); + + stats->bytes += frame->len; + xdp_return_frame(frame); + } + stats->packets++; + } +} + /* Converting between virtqueue no. and kernel tx/rx queue no. * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq */ @@ -572,15 +596,12 @@ static int virtnet_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames, u32 flags) { struct virtnet_info *vi = netdev_priv(dev); + struct virtnet_sq_stats stats = {}; struct receive_queue *rq = vi->rq; struct bpf_prog *xdp_prog; struct send_queue *sq; - unsigned int len; - int packets = 0; - int bytes = 0; int nxmit = 0; int kicks = 0; - void *ptr; int ret; int i; @@ -599,20 +620,7 @@ static int virtnet_xdp_xmit(struct net_device *dev, } /* Free up any pending old buffers before queueing new ones. */ - while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { - if (likely(is_xdp_frame(ptr))) { - struct xdp_frame *frame = ptr_to_xdp(ptr); - - bytes += frame->len; - xdp_return_frame(frame); - } else { - struct sk_buff *skb = ptr; - - bytes += skb->len; - napi_consume_skb(skb, false); - } - packets++; - } + __free_old_xmit(sq, false, &stats); for (i = 0; i < n; i++) { struct xdp_frame *xdpf = frames[i]; @@ -629,8 +637,8 @@ static int virtnet_xdp_xmit(struct net_device *dev, } out: u64_stats_update_begin(&sq->stats.syncp); - sq->stats.bytes += bytes; - sq->stats.packets += packets; + sq->stats.bytes += stats.bytes; + sq->stats.packets += stats.packets; sq->stats.xdp_tx += n; sq->stats.xdp_tx_drops += n - nxmit; sq->stats.kicks += kicks; @@ -1459,39 +1467,21 @@ static int virtnet_receive(struct receive_queue *rq, int budget, return stats.packets; } -static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi) +static void free_old_xmit(struct send_queue *sq, bool in_napi) { - unsigned int len; - unsigned int packets = 0; - unsigned int bytes = 0; - void *ptr; + struct virtnet_sq_stats stats = {}; - while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) { - if (likely(!is_xdp_frame(ptr))) { - struct sk_buff *skb = ptr; - - pr_debug("Sent skb %p\n", skb); - - bytes += skb->len; - napi_consume_skb(skb, in_napi); - } else { - struct xdp_frame *frame = ptr_to_xdp(ptr); - - bytes += frame->len; - xdp_return_frame(frame); - } - packets++; - } + __free_old_xmit(sq, in_napi, &stats); /* Avoid overhead when no packets have been processed * happens when called speculatively from start_xmit. */ - if (!packets) + if (!stats.packets) return; u64_stats_update_begin(&sq->stats.syncp); - sq->stats.bytes += bytes; - sq->stats.packets += packets; + sq->stats.bytes += stats.bytes; + sq->stats.packets += stats.packets; u64_stats_update_end(&sq->stats.syncp); } @@ -1516,7 +1506,7 @@ static void virtnet_poll_cleantx(struct receive_queue *rq) return; if (__netif_tx_trylock(txq)) { - free_old_xmit_skbs(sq, true); + free_old_xmit(sq, true); __netif_tx_unlock(txq); } @@ -1601,7 +1591,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget) txq = netdev_get_tx_queue(vi->dev, index); __netif_tx_lock(txq, raw_smp_processor_id()); - free_old_xmit_skbs(sq, true); + free_old_xmit(sq, true); __netif_tx_unlock(txq); virtqueue_napi_complete(napi, sq->vq, 0); @@ -1670,7 +1660,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) bool use_napi = sq->napi.weight; /* Free up any pending old buffers before queueing new ones. */ - free_old_xmit_skbs(sq, false); + free_old_xmit(sq, false); if (use_napi && kick) virtqueue_enable_cb_delayed(sq->vq); @@ -1714,7 +1704,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) if (!use_napi && unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { /* More just got used, free them then recheck. */ - free_old_xmit_skbs(sq, false); + free_old_xmit(sq, false); if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { netif_start_subqueue(dev, qnum); virtqueue_disable_cb(sq->vq); -- 2.31.0 _______________________________________________ Virtualization mailing list Virtualization@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/virtualization
next prev parent reply other threads:[~2021-06-10 8:22 UTC|newest] Thread overview: 80+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-06-10 8:21 [PATCH net-next v5 00/15] virtio-net: support xdp socket zero copy Xuan Zhuo 2021-06-10 8:21 ` Xuan Zhuo 2021-06-10 8:21 ` [PATCH net-next v5 01/15] netdevice: priv_flags extend to 64bit Xuan Zhuo 2021-06-10 8:21 ` Xuan Zhuo 2021-06-10 8:21 ` [PATCH net-next v5 02/15] netdevice: add priv_flags IFF_NOT_USE_DMA_ADDR Xuan Zhuo 2021-06-10 8:21 ` Xuan Zhuo 2021-06-10 8:21 ` [PATCH net-next v5 03/15] virtio-net: " Xuan Zhuo 2021-06-10 8:21 ` Xuan Zhuo 2021-06-16 9:27 ` Jason Wang 2021-06-16 9:27 ` Jason Wang 2021-06-16 10:27 ` Xuan Zhuo 2021-06-10 8:21 ` [PATCH net-next v5 04/15] xsk: XDP_SETUP_XSK_POOL support option IFF_NOT_USE_DMA_ADDR Xuan Zhuo 2021-06-10 8:21 ` Xuan Zhuo 2021-06-10 8:21 ` [PATCH net-next v5 05/15] virtio: support virtqueue_detach_unused_buf_ctx Xuan Zhuo 2021-06-10 8:21 ` Xuan Zhuo 2021-06-17 2:48 ` kernel test robot 2021-06-17 2:48 ` kernel test robot 2021-06-10 8:22 ` Xuan Zhuo [this message] 2021-06-10 8:22 ` [PATCH net-next v5 06/15] virtio-net: unify the code for recycling the xmit ptr Xuan Zhuo 2021-06-16 2:42 ` Jason Wang 2021-06-16 2:42 ` Jason Wang 2021-06-10 8:22 ` [PATCH net-next v5 07/15] virtio-net: standalone virtnet_aloc_frag function Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-16 2:45 ` Jason Wang 2021-06-16 2:45 ` Jason Wang 2021-06-10 8:22 ` [PATCH net-next v5 08/15] virtio-net: split the receive_mergeable function Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-16 7:33 ` Jason Wang 2021-06-16 7:33 ` Jason Wang 2021-06-16 7:52 ` Xuan Zhuo 2021-06-10 8:22 ` [PATCH net-next v5 09/15] virtio-net: virtnet_poll_tx support budget check Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-10 8:22 ` [PATCH net-next v5 10/15] virtio-net: independent directory Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-16 7:34 ` Jason Wang 2021-06-16 7:34 ` Jason Wang 2021-06-10 8:22 ` [PATCH net-next v5 11/15] virtio-net: move to virtio_net.h Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-16 7:35 ` Jason Wang 2021-06-16 7:35 ` Jason Wang 2021-06-10 8:22 ` [PATCH net-next v5 12/15] virtio-net: support AF_XDP zc tx Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-16 9:26 ` Jason Wang 2021-06-16 9:26 ` Jason Wang 2021-06-16 10:10 ` Magnus Karlsson 2021-06-16 10:19 ` Xuan Zhuo 2021-06-16 12:51 ` Jason Wang 2021-06-16 12:51 ` Jason Wang 2021-06-16 12:57 ` Xuan Zhuo 2021-06-17 2:36 ` Jason Wang 2021-06-17 2:36 ` Jason Wang 2021-06-10 8:22 ` [PATCH net-next v5 13/15] virtio-net: support AF_XDP zc rx Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-17 2:48 ` kernel test robot 2021-06-17 2:48 ` kernel test robot 2021-06-17 3:23 ` Jason Wang 2021-06-17 3:23 ` Jason Wang 2021-06-17 5:53 ` Xuan Zhuo 2021-06-17 6:03 ` Jason Wang 2021-06-17 6:03 ` Jason Wang 2021-06-17 6:37 ` Xuan Zhuo 2021-06-17 6:58 ` Jason Wang 2021-06-17 6:58 ` Jason Wang 2021-06-21 3:26 ` kernel test robot 2021-06-21 3:26 ` kernel test robot 2021-06-10 8:22 ` [PATCH net-next v5 14/15] virtio-net: xsk direct xmit inside xsk wakeup Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-17 3:07 ` Jason Wang 2021-06-17 3:07 ` Jason Wang 2021-06-17 5:55 ` Xuan Zhuo 2021-06-17 6:01 ` Jason Wang 2021-06-17 6:01 ` Jason Wang 2021-06-10 8:22 ` [PATCH net-next v5 15/15] virtio-net: xsk zero copy xmit kick by threshold Xuan Zhuo 2021-06-10 8:22 ` Xuan Zhuo 2021-06-17 3:08 ` Jason Wang 2021-06-17 3:08 ` Jason Wang 2021-06-17 5:56 ` Xuan Zhuo 2021-06-17 6:00 ` Jason Wang 2021-06-17 6:00 ` Jason Wang 2021-06-17 6:43 ` Xuan Zhuo
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=20210610082209.91487-7-xuanzhuo@linux.alibaba.com \ --to=xuanzhuo@linux.alibaba.com \ --cc=andrii@kernel.org \ --cc=ast@kernel.org \ --cc=bjorn@kernel.org \ --cc=bpf@vger.kernel.org \ --cc=daniel@iogearbox.net \ --cc=davem@davemloft.net \ --cc=dust.li@linux.alibaba.com \ --cc=hawk@kernel.org \ --cc=jasowang@redhat.com \ --cc=john.fastabend@gmail.com \ --cc=jonathan.lemon@gmail.com \ --cc=kafai@fb.com \ --cc=kpsingh@kernel.org \ --cc=kuba@kernel.org \ --cc=magnus.karlsson@intel.com \ --cc=mst@redhat.com \ --cc=netdev@vger.kernel.org \ --cc=songliubraving@fb.com \ --cc=virtualization@lists.linux-foundation.org \ --cc=yhs@fb.com \ /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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.