All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jesper Dangaard Brouer <brouer@redhat.com>
To: David Ahern <dsahern@kernel.org>
Cc: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org,
	prashantbhole.linux@gmail.com, jasowang@redhat.com,
	toke@redhat.com, toshiaki.makita1@gmail.com,
	daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org,
	kafai@fb.com, songliubraving@fb.com, yhs@fb.com, andriin@fb.com,
	dsahern@gmail.com, David Ahern <dahern@digitalocean.com>,
	brouer@redhat.com
Subject: Re: [PATCH RFC-v5 bpf-next 09/12] dev: Support xdp in the Tx path for xdp_frames
Date: Fri, 17 Apr 2020 10:30:55 +0200	[thread overview]
Message-ID: <20200417103055.51a25b2d@carbon> (raw)
In-Reply-To: <20200413171801.54406-10-dsahern@kernel.org>

On Mon, 13 Apr 2020 11:17:58 -0600
David Ahern <dsahern@kernel.org> wrote:

> From: David Ahern <dahern@digitalocean.com>
> 
> Add support to run Tx path program on xdp_frames by adding a hook to
> bq_xmit_all before xdp_frames are passed to ndo_xdp_xmit for the device.
> 
> If an xdp_frame is dropped by the program, it is removed from the
> xdp_frames array with subsequent entries moved up.
> 
> Signed-off-by: David Ahern <dahern@digitalocean.com>
> ---
>  include/linux/netdevice.h |  3 ++
>  kernel/bpf/devmap.c       | 19 ++++++++---
>  net/core/dev.c            | 70 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 87 insertions(+), 5 deletions(-)
> 
[...]
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 1bbaeb8842ed..f23dc6043329 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -4720,6 +4720,76 @@ u32 do_xdp_egress_skb(struct net_device *dev, struct sk_buff *skb)
>  }
>  EXPORT_SYMBOL_GPL(do_xdp_egress_skb);
>  
> +static u32 __xdp_egress_frame(struct net_device *dev,
> +			      struct bpf_prog *xdp_prog,
> +			      struct xdp_frame *xdp_frame,
> +			      struct xdp_txq_info *txq)
> +{
> +	struct xdp_buff xdp;
> +	u32 act;
> +
> +	xdp.data_hard_start = xdp_frame->data - xdp_frame->headroom;

You also need: minus sizeof(*xdp_frame).

The BPF-helper xdp_adjust_head will not allow BPF-prog to access the
memory area that is used for xdp_frame, thus it still is safe.


> +	xdp.data = xdp_frame->data;
> +	xdp.data_end = xdp.data + xdp_frame->len;
> +	xdp_set_data_meta_invalid(&xdp);
> +	xdp.txq = txq;

I think this will be the 3rd place we convert xdp-frame to xdp_buff,
perhaps we should introduce a helper function call.

> +	act = bpf_prog_run_xdp(xdp_prog, &xdp);
> +	act = handle_xdp_egress_act(act, dev, xdp_prog);
> +
> +	/* if not dropping frame, readjust pointers in case
> +	 * program made changes to the buffer
> +	 */
> +	if (act != XDP_DROP) {
> +		int headroom = xdp.data - xdp.data_hard_start;
> +		int metasize = xdp.data - xdp.data_meta;
> +
> +		metasize = metasize > 0 ? metasize : 0;
> +		if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
> +			return XDP_DROP;
> +
> +		xdp_frame = xdp.data_hard_start;

Is this needed?

> +		xdp_frame->data = xdp.data;
> +		xdp_frame->len  = xdp.data_end - xdp.data;
> +		xdp_frame->headroom = headroom - sizeof(*xdp_frame);
> +		xdp_frame->metasize = metasize;
> +		/* xdp_frame->mem is unchanged */

This looks very similar to convert_to_xdp_frame.
Maybe we need an central update_xdp_frame(xdp_buff) call?


Untested code-up:

diff --git a/include/net/xdp.h b/include/net/xdp.h
index 40c6d3398458..180800c4e7d1 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -93,6 +93,24 @@ static inline void xdp_scrub_frame(struct xdp_frame *frame)
 
 struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
 
+static inline
+bool update_xdp_frame(struct xdp_buff *xdp, struct xdp_frame *xdp_frame)
+{
+       /* Assure headroom is available for storing info */
+       headroom = xdp->data - xdp->data_hard_start;
+       metasize = xdp->data - xdp->data_meta;
+       metasize = metasize > 0 ? metasize : 0;
+       if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
+               return false;
+
+       xdp_frame->data = xdp->data;
+       xdp_frame->len  = xdp->data_end - xdp->data;
+       xdp_frame->headroom = headroom - sizeof(*xdp_frame);
+       xdp_frame->metasize = metasize;
+
+       return true;
+}
+
 /* Convert xdp_buff to xdp_frame */
 static inline
 struct xdp_frame *convert_to_xdp_frame(struct xdp_buff *xdp)
@@ -104,20 +122,11 @@ struct xdp_frame *convert_to_xdp_frame(struct xdp_buff *xdp)
        if (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY)
                return xdp_convert_zc_to_xdp_frame(xdp);
 
-       /* Assure headroom is available for storing info */
-       headroom = xdp->data - xdp->data_hard_start;
-       metasize = xdp->data - xdp->data_meta;
-       metasize = metasize > 0 ? metasize : 0;
-       if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
-               return NULL;
-
        /* Store info in top of packet */
        xdp_frame = xdp->data_hard_start;
 
-       xdp_frame->data = xdp->data;
-       xdp_frame->len  = xdp->data_end - xdp->data;
-       xdp_frame->headroom = headroom - sizeof(*xdp_frame);
-       xdp_frame->metasize = metasize;
+       if (unlikely(!update_xdp_frame(xdp, xdp_frame))
+           return NULL;
 
        /* rxq only valid until napi_schedule ends, convert to xdp_mem_info */
        xdp_frame->mem = xdp->rxq->mem;

-- 
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer


  parent reply	other threads:[~2020-04-17  8:31 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-13 17:17 [PATCH RFC-v5 bpf-next 00/12] Add support for XDP in egress path David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 01/12] net: Add XDP setup and query commands for Tx programs David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 02/12] net: Add BPF_XDP_EGRESS as a bpf_attach_type David Ahern
2020-04-16 14:01   ` Toke Høiland-Jørgensen
2020-04-16 16:35     ` David Ahern
2020-04-17  9:23       ` Toke Høiland-Jørgensen
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 03/12] xdp: Add xdp_txq_info to xdp_buff David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 04/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 05/12] net: core: rename netif_receive_generic_xdp to do_generic_xdp_core David Ahern
2020-04-16 14:00   ` Toke Høiland-Jørgensen
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 06/12] net: core: Rename do_xdp_generic to do_xdp_generic_rx David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 07/12] dev: set egress XDP program David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 08/12] dev: Support xdp in the Tx path for packets as an skb David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 09/12] dev: Support xdp in the Tx path for xdp_frames David Ahern
2020-04-16 14:02   ` Toke Høiland-Jørgensen
2020-04-16 23:50     ` David Ahern
2020-04-17  9:25       ` Toke Høiland-Jørgensen
2020-04-17 20:06         ` David Ahern
2020-04-17  8:30   ` Jesper Dangaard Brouer [this message]
2020-04-17 21:29     ` David Ahern
2020-04-13 17:17 ` [PATCH RFC-v5 bpf-next 10/12] libbpf: Add egress XDP support David Ahern
2020-04-13 17:18 ` [PATCH RFC-v5 bpf-next 11/12] bpftool: Add support for XDP egress David Ahern
2020-04-13 17:18 ` [PATCH RFC-v5 bpf-next 12/12] samples/bpf: add XDP egress support to xdp1 David Ahern
2020-04-15 15:01   ` Alexei Starovoitov
2020-04-16 13:59 ` [PATCH RFC-v5 bpf-next 00/12] Add support for XDP in egress path Toke Høiland-Jørgensen
2020-04-16 23:55   ` David Ahern
2020-04-17  9:28     ` Toke Høiland-Jørgensen

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=20200417103055.51a25b2d@carbon \
    --to=brouer@redhat.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=dahern@digitalocean.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=dsahern@kernel.org \
    --cc=jasowang@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=prashantbhole.linux@gmail.com \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.com \
    --cc=toshiaki.makita1@gmail.com \
    --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: link
Be 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.