bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jesper Dangaard Brouer <brouer@redhat.com>
To: sameehj@amazon.com
Cc: "Jesper Dangaard Brouer" <brouer@redhat.com>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Daniel Borkmann" <borkmann@iogearbox.net>,
	"Alexei Starovoitov" <alexei.starovoitov@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Alexander Duyck" <alexander.duyck@gmail.com>,
	"Jeff Kirsher" <jeffrey.t.kirsher@intel.com>,
	"David Ahern" <dsahern@gmail.com>,
	"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
	"Lorenzo Bianconi" <lorenzo@kernel.org>,
	"Saeed Mahameed" <saeedm@mellanox.com>,
	"Tariq Toukan" <tariqt@mellanox.com>
Subject: [PATCH net-next v4 21/33] virtio_net: add XDP frame size in two code paths
Date: Thu, 14 May 2020 12:50:44 +0200	[thread overview]
Message-ID: <158945344436.97035.9445115070189151680.stgit@firesoul> (raw)
In-Reply-To: <158945314698.97035.5286827951225578467.stgit@firesoul>

The virtio_net driver is running inside the guest-OS. There are two
XDP receive code-paths in virtio_net, namely receive_small() and
receive_mergeable(). The receive_big() function does not support XDP.

In receive_small() the frame size is available in buflen. The buffer
backing these frames are allocated in add_recvbuf_small() with same
size, except for the headroom, but tailroom have reserved room for
skb_shared_info. The headroom is encoded in ctx pointer as a value.

In receive_mergeable() the frame size is more dynamic. There are two
basic cases: (1) buffer size is based on a exponentially weighted
moving average (see DECLARE_EWMA) of packet length. Or (2) in case
virtnet_get_headroom() have any headroom then buffer size is
PAGE_SIZE. The ctx pointer is this time used for encoding two values;
the buffer len "truesize" and headroom. In case (1) if the rx buffer
size is underestimated, the packet will have been split over more
buffers (num_buf info in virtio_net_hdr_mrg_rxbuf placed in top of
buffer area). If that happens the XDP path does a xdp_linearize_page
operation.

V3: Adjust frame_sz in receive_mergeable() case, spotted by Jason Wang.

The code is really hard to follow, so some hints to reviewers.
The receive_mergeable() case gets frames that were allocated in
add_recvbuf_mergeable() which uses headroom=virtnet_get_headroom(),
and 'buf' ptr is advanced this headroom.  The headroom can only
be 0 or VIRTIO_XDP_HEADROOM, as virtnet_get_headroom is really
simple:

  static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
  {
	return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0;
  }

As frame_sz is an offset size from xdp.data_hard_start, reviewers
should notice how this is calculated in receive_mergeable():

  int offset = buf - page_address(page);
  [...]
  data = page_address(xdp_page) + offset;
  xdp.data_hard_start = data - VIRTIO_XDP_HEADROOM + vi->hdr_len;

The calculated offset will always be VIRTIO_XDP_HEADROOM when
reaching this code.  Thus, xdp.data_hard_start will be page-start
address plus vi->hdr_len.  Given this xdp.frame_sz need to be
reduced with vi->hdr_len size.

IMHO a followup patch should cleanup this code to make it easier
to maintain and understand, but it is outside the scope of this
patchset.

Cc: Jason Wang <jasowang@redhat.com>
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
---
 drivers/net/virtio_net.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 11f722460513..9e1b5d748586 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -689,6 +689,7 @@ static struct sk_buff *receive_small(struct net_device *dev,
 		xdp.data_end = xdp.data + len;
 		xdp.data_meta = xdp.data;
 		xdp.rxq = &rq->xdp_rxq;
+		xdp.frame_sz = buflen;
 		orig_data = xdp.data;
 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
 		stats->xdp_packets++;
@@ -797,10 +798,11 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 	int offset = buf - page_address(page);
 	struct sk_buff *head_skb, *curr_skb;
 	struct bpf_prog *xdp_prog;
-	unsigned int truesize;
+	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
-	int err;
 	unsigned int metasize = 0;
+	unsigned int frame_sz;
+	int err;
 
 	head_skb = NULL;
 	stats->bytes += len - vi->hdr_len;
@@ -821,6 +823,11 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		if (unlikely(hdr->hdr.gso_type))
 			goto err_xdp;
 
+		/* Buffers with headroom use PAGE_SIZE as alloc size,
+		 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
+		 */
+		frame_sz = headroom ? PAGE_SIZE : truesize;
+
 		/* This happens when rx buffer size is underestimated
 		 * or headroom is not enough because of the buffer
 		 * was refilled before XDP is set. This should only
@@ -834,6 +841,8 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 						      page, offset,
 						      VIRTIO_XDP_HEADROOM,
 						      &len);
+			frame_sz = PAGE_SIZE;
+
 			if (!xdp_page)
 				goto err_xdp;
 			offset = VIRTIO_XDP_HEADROOM;
@@ -850,6 +859,7 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		xdp.data_end = xdp.data + (len - vi->hdr_len);
 		xdp.data_meta = xdp.data;
 		xdp.rxq = &rq->xdp_rxq;
+		xdp.frame_sz = frame_sz - vi->hdr_len;
 
 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
 		stats->xdp_packets++;
@@ -924,7 +934,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 	}
 	rcu_read_unlock();
 
-	truesize = mergeable_ctx_to_truesize(ctx);
 	if (unlikely(len > truesize)) {
 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
 			 dev->name, len, (unsigned long)ctx);



  parent reply	other threads:[~2020-05-14 10:51 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-14 10:48 [PATCH net-next v4 00/33] XDP extend with knowledge of frame size Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 01/33] xdp: add frame size to xdp_buff Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 02/33] bnxt: add XDP frame size to driver Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 03/33] sfc: add XDP frame size Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 04/33] mvneta: add XDP frame size to driver Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 05/33] net: netsec: Add support for XDP frame size Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 06/33] net: XDP-generic determining " Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 07/33] xdp: xdp_frame add member frame_sz and handle in convert_to_xdp_frame Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 08/33] xdp: cpumap redirect use frame_sz and increase skb_tailroom Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 09/33] veth: adjust hard_start offset on redirect XDP frames Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 10/33] veth: xdp using frame_sz in veth driver Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 11/33] dpaa2-eth: add XDP frame size Jesper Dangaard Brouer
2020-05-14 10:49 ` [PATCH net-next v4 12/33] hv_netvsc: add XDP frame size to driver Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 13/33] qlogic/qede: " Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 14/33] net: ethernet: ti: add XDP frame size to driver cpsw Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 15/33] ena: add XDP frame size to amazon NIC driver Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 16/33] mlx4: add XDP frame size and adjust max XDP MTU Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 17/33] net: thunderx: add XDP frame size Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 18/33] nfp: add XDP frame size to netronome driver Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 19/33] tun: add XDP frame size Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 20/33] vhost_net: also populate " Jesper Dangaard Brouer
2020-05-14 10:50 ` Jesper Dangaard Brouer [this message]
2020-05-14 10:50 ` [PATCH net-next v4 22/33] ixgbe: fix XDP redirect on archs with PAGE_SIZE above 4K Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 23/33] ixgbe: add XDP frame size to driver Jesper Dangaard Brouer
2020-05-14 10:50 ` [PATCH net-next v4 24/33] ixgbevf: add XDP frame size to VF driver Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 25/33] i40e: add XDP frame size to driver Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 26/33] ice: " Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 27/33] xdp: for Intel AF_XDP drivers add XDP frame_sz Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 28/33] mlx5: rx queue setup time determine frame_sz for XDP Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 29/33] xdp: allow bpf_xdp_adjust_tail() to grow packet size Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 30/33] xdp: clear grow memory in bpf_xdp_adjust_tail() Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 31/33] bpf: add xdp.frame_sz in bpf_prog_test_run_xdp() Jesper Dangaard Brouer
2020-05-17  4:02   ` unstable xdp tests. Was: " Alexei Starovoitov
2020-05-18  9:52     ` Jesper Dangaard Brouer
2020-05-18 10:30       ` Jesper Dangaard Brouer
2020-05-18 19:19         ` Andrii Nakryiko
2020-05-14 10:51 ` [PATCH net-next v4 32/33] selftests/bpf: adjust BPF selftest for xdp_adjust_tail Jesper Dangaard Brouer
2020-05-14 10:51 ` [PATCH net-next v4 33/33] selftests/bpf: xdp_adjust_tail add grow tail tests Jesper Dangaard Brouer
2020-05-15  4:52 ` [PATCH net-next v4 00/33] XDP extend with knowledge of frame size Alexei Starovoitov

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=158945344436.97035.9445115070189151680.stgit@firesoul \
    --to=brouer@redhat.com \
    --cc=alexander.duyck@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=borkmann@iogearbox.net \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jeffrey.t.kirsher@intel.com \
    --cc=john.fastabend@gmail.com \
    --cc=lorenzo@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=saeedm@mellanox.com \
    --cc=sameehj@amazon.com \
    --cc=tariqt@mellanox.com \
    --cc=toke@redhat.com \
    --subject='Re: [PATCH net-next v4 21/33] virtio_net: add XDP frame size in two code paths' \
    /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

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).