netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
To: netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	Xuan Zhuo <xuanzhuo@linux.alibaba.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-foundation.org, bpf@vger.kernel.org
Subject: [PATCH net-next v3 15/27] virtio_net: move some api to header
Date: Fri, 29 Dec 2023 15:30:56 +0800	[thread overview]
Message-ID: <20231229073108.57778-16-xuanzhuo@linux.alibaba.com> (raw)
In-Reply-To: <20231229073108.57778-1-xuanzhuo@linux.alibaba.com>

__free_old_xmit
is_xdp_raw_buffer_queue

These two APIs are needed by the xsk part.
So this commit move theses to the header. And add prefix "virtnet_".

Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
 drivers/net/virtio/main.c       | 86 +++------------------------------
 drivers/net/virtio/virtio_net.h | 72 +++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 79 deletions(-)

diff --git a/drivers/net/virtio/main.c b/drivers/net/virtio/main.c
index 180153dba4f2..6ab1f3418139 100644
--- a/drivers/net/virtio/main.c
+++ b/drivers/net/virtio/main.c
@@ -45,8 +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 VIRTNET_DRIVER_VERSION "1.0.0"
 
 static const unsigned long guest_offloads[] = {
@@ -149,71 +147,11 @@ struct virtio_net_common_hdr {
 	};
 };
 
-static bool is_xdp_frame(void *ptr)
-{
-	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
-}
-
 static void *xdp_to_ptr(struct xdp_frame *ptr)
 {
 	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
 }
 
-static struct xdp_frame *ptr_to_xdp(void *ptr)
-{
-	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
-}
-
-static void virtnet_sq_unmap_buf(struct virtnet_sq *sq, struct virtio_dma_head *dma)
-{
-	int i;
-
-	if (!dma)
-		return;
-
-	for (i = 0; i < dma->next; ++i)
-		virtqueue_dma_unmap_single_attrs(sq->vq,
-						 dma->items[i].addr,
-						 dma->items[i].length,
-						 DMA_TO_DEVICE, 0);
-	dma->next = 0;
-}
-
-static void __free_old_xmit(struct virtnet_sq *sq, bool in_napi,
-			    u64 *bytes, u64 *packets)
-{
-	struct virtio_dma_head *dma;
-	unsigned int len;
-	void *ptr;
-
-	if (virtqueue_get_dma_premapped(sq->vq)) {
-		dma = &sq->dma.head;
-		dma->num = ARRAY_SIZE(sq->dma.items);
-		dma->next = 0;
-	} else {
-		dma = NULL;
-	}
-
-	while ((ptr = virtqueue_get_buf_ctx_dma(sq->vq, &len, dma, NULL)) != NULL) {
-		virtnet_sq_unmap_buf(sq, dma);
-
-		if (!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 += xdp_get_frame_len(frame);
-			xdp_return_frame(frame);
-		}
-		(*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
  */
@@ -660,7 +598,7 @@ static void free_old_xmit(struct virtnet_sq *sq, bool in_napi)
 {
 	u64 bytes = 0, packets = 0;
 
-	__free_old_xmit(sq, in_napi, &bytes, &packets);
+	virtnet_free_old_xmit(sq, in_napi, &bytes, &packets);
 
 	/* Avoid overhead when no packets have been processed
 	 * happens when called speculatively from start_xmit.
@@ -674,16 +612,6 @@ static void free_old_xmit(struct virtnet_sq *sq, bool in_napi)
 	u64_stats_update_end(&sq->stats.syncp);
 }
 
-static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
-{
-	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
-		return false;
-	else if (q < vi->curr_queue_pairs)
-		return true;
-	else
-		return false;
-}
-
 static void check_sq_full_and_disable(struct virtnet_info *vi,
 				      struct net_device *dev,
 				      struct virtnet_sq *sq)
@@ -832,7 +760,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,
 	}
 
 	/* Free up any pending old buffers before queueing new ones. */
-	__free_old_xmit(sq, false, &bytes, &packets);
+	virtnet_free_old_xmit(sq, false, &bytes, &packets);
 
 	for (i = 0; i < n; i++) {
 		struct xdp_frame *xdpf = frames[i];
@@ -843,7 +771,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,
 	}
 	ret = nxmit;
 
-	if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
+	if (!virtnet_is_xdp_raw_buffer_queue(vi, sq - vi->sq))
 		check_sq_full_and_disable(vi, dev, sq);
 
 	if (flags & XDP_XMIT_FLUSH) {
@@ -1993,7 +1921,7 @@ static void virtnet_poll_cleantx(struct virtnet_rq *rq)
 	struct virtnet_sq *sq = &vi->sq[index];
 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
 
-	if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
+	if (!sq->napi.weight || virtnet_is_xdp_raw_buffer_queue(vi, index))
 		return;
 
 	if (__netif_tx_trylock(txq)) {
@@ -2117,7 +2045,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 	int opaque;
 	bool done;
 
-	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
+	if (unlikely(virtnet_is_xdp_raw_buffer_queue(vi, index))) {
 		/* We don't need to enable cb for XDP */
 		napi_complete_done(napi, 0);
 		return 0;
@@ -3967,10 +3895,10 @@ void virtnet_sq_free_unused_bufs(struct virtqueue *vq)
 	while ((buf = virtqueue_detach_unused_buf_dma(vq, dma)) != NULL) {
 		virtnet_sq_unmap_buf(sq, dma);
 
-		if (!is_xdp_frame(buf))
+		if (!virtnet_is_xdp_frame(buf))
 			dev_kfree_skb(buf);
 		else
-			xdp_return_frame(ptr_to_xdp(buf));
+			xdp_return_frame(virtnet_ptr_to_xdp(buf));
 	}
 }
 
diff --git a/drivers/net/virtio/virtio_net.h b/drivers/net/virtio/virtio_net.h
index 1adebcb2a6cc..6888b0b767c6 100644
--- a/drivers/net/virtio/virtio_net.h
+++ b/drivers/net/virtio/virtio_net.h
@@ -8,6 +8,8 @@
 #include <linux/virtio_net.h>
 #include <net/xdp_sock_drv.h>
 
+#define VIRTIO_XDP_FLAG	BIT(0)
+
 /* 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-
@@ -211,6 +213,76 @@ struct virtnet_info {
 	struct failover *failover;
 };
 
+static inline bool virtnet_is_xdp_frame(void *ptr)
+{
+	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
+}
+
+static inline struct xdp_frame *virtnet_ptr_to_xdp(void *ptr)
+{
+	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
+}
+
+static inline void virtnet_sq_unmap_buf(struct virtnet_sq *sq, struct virtio_dma_head *dma)
+{
+	int i;
+
+	if (!dma)
+		return;
+
+	for (i = 0; i < dma->next; ++i)
+		virtqueue_dma_unmap_single_attrs(sq->vq,
+						 dma->items[i].addr,
+						 dma->items[i].length,
+						 DMA_TO_DEVICE, 0);
+	dma->next = 0;
+}
+
+static inline void virtnet_free_old_xmit(struct virtnet_sq *sq, bool in_napi,
+					 u64 *bytes, u64 *packets)
+{
+	struct virtio_dma_head *dma;
+	unsigned int len;
+	void *ptr;
+
+	if (virtqueue_get_dma_premapped(sq->vq)) {
+		dma = &sq->dma.head;
+		dma->num = ARRAY_SIZE(sq->dma.items);
+		dma->next = 0;
+	} else {
+		dma = NULL;
+	}
+
+	while ((ptr = virtqueue_get_buf_ctx_dma(sq->vq, &len, dma, NULL)) != NULL) {
+		virtnet_sq_unmap_buf(sq, dma);
+
+		if (!virtnet_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 = virtnet_ptr_to_xdp(ptr);
+
+			*bytes += xdp_get_frame_len(frame);
+			xdp_return_frame(frame);
+		}
+		(*packets)++;
+	}
+}
+
+static inline bool virtnet_is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
+{
+	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
+		return false;
+	else if (q < vi->curr_queue_pairs)
+		return true;
+	else
+		return false;
+}
+
 void virtnet_rx_pause(struct virtnet_info *vi, struct virtnet_rq *rq);
 void virtnet_rx_resume(struct virtnet_info *vi, struct virtnet_rq *rq);
 void virtnet_tx_pause(struct virtnet_info *vi, struct virtnet_sq *sq);
-- 
2.32.0.3.g01195cf9f


  parent reply	other threads:[~2023-12-29  7:31 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-29  7:30 [PATCH net-next v3 00/27] virtio-net: support AF_XDP zero copy Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 01/27] virtio_net: rename free_old_xmit_skbs to free_old_xmit Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 02/27] virtio_net: unify the code for recycling the xmit ptr Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 03/27] virtio_net: independent directory Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 04/27] virtio_net: move core structures to virtio_net.h Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 05/27] virtio_net: add prefix virtnet to all struct inside virtio_net.h Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 06/27] virtio_ring: introduce virtqueue_get_buf_ctx_dma() Xuan Zhuo
2024-01-11  8:34   ` Jason Wang
2024-01-16  7:32     ` Xuan Zhuo
2024-01-22  4:18       ` Jason Wang
2024-01-22  6:04         ` Xuan Zhuo
2024-01-22  6:54           ` Jason Wang
2023-12-29  7:30 ` [PATCH net-next v3 07/27] virtio_ring: virtqueue_disable_and_recycle let the callback detach bufs Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 08/27] virtio_ring: introduce virtqueue_detach_unused_buf_dma() Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 09/27] virtio_ring: introduce virtqueue_get_dma_premapped() Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 10/27] virtio_net: sq support premapped mode Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 11/27] virtio_net: separate virtnet_rx_resize() Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 12/27] virtio_net: separate virtnet_tx_resize() Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 13/27] virtio_net: xsk: bind/unbind xsk Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 14/27] virtio_net: xsk: prevent disable tx napi Xuan Zhuo
2023-12-29  7:30 ` Xuan Zhuo [this message]
2023-12-29  7:30 ` [PATCH net-next v3 16/27] virtio_net: xsk: tx: support xmit xsk buffer Xuan Zhuo
2023-12-30  0:28   ` kernel test robot
2023-12-29  7:30 ` [PATCH net-next v3 17/27] virtio_net: xsk: tx: support wakeup Xuan Zhuo
2023-12-29  7:30 ` [PATCH net-next v3 18/27] virtio_net: xsk: tx: handle the transmitted xsk buffer Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 19/27] virtio_net: xsk: tx: free the unused " Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 20/27] virtio_net: separate receive_mergeable Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 21/27] virtio_net: separate receive_buf Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 22/27] virtio_net: xsk: rx: support fill with xsk buffer Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 23/27] virtio_net: xsk: rx: support recv merge mode Xuan Zhuo
2023-12-29 21:03   ` kernel test robot
2023-12-30  1:01   ` kernel test robot
2023-12-29  7:31 ` [PATCH net-next v3 24/27] virtio_net: xsk: rx: support recv small mode Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 25/27] virtio_net: xsk: rx: free the unused xsk buffer Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 26/27] virtio_net: update tx timeout record Xuan Zhuo
2023-12-29  7:31 ` [PATCH net-next v3 27/27] virtio_net: xdp_features add NETDEV_XDP_ACT_XSK_ZEROCOPY Xuan Zhuo
2024-01-11  3:27 ` [PATCH net-next v3 00/27] virtio-net: support AF_XDP zero copy 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=20231229073108.57778-16-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=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-foundation.org \
    /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;
as well as URLs for NNTP newsgroup(s).