From: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
To: "Toke Høiland-Jørgensen" <toke@redhat.com>
Cc: Jesper Dangaard Brouer <hawk@kernel.org>,
Arthur Fabre <afabre@cloudflare.com>,
Jakub Sitnicki <jakub@cloudflare.com>,
Alexander Lobakin <aleksander.lobakin@intel.com>,
Lorenzo Bianconi <lorenzo@kernel.org>,
bpf@vger.kernel.org, netdev@vger.kernel.org, ast@kernel.org,
daniel@iogearbox.net, davem@davemloft.net, kuba@kernel.org,
john.fastabend@gmail.com, edumazet@google.com, pabeni@redhat.com,
sdf@fomichev.me, tariqt@nvidia.com, saeedm@nvidia.com,
anthony.l.nguyen@intel.com, przemyslaw.kitszel@intel.com,
intel-wired-lan@lists.osuosl.org, mst@redhat.com,
jasowang@redhat.com, mcoquelin.stm32@gmail.com,
alexandre.torgue@foss.st.com,
kernel-team <kernel-team@cloudflare.com>,
Yan Zhai <yan@cloudflare.com>
Subject: Re: [RFC bpf-next 0/4] Add XDP rx hw hints support performing XDP_REDIRECT
Date: Sun, 22 Sep 2024 17:40:52 +0200 [thread overview]
Message-ID: <ZvA6hIl6XWJ4UEJW@lore-desk> (raw)
In-Reply-To: <87ldzkndqk.fsf@toke.dk>
[-- Attachment #1: Type: text/plain, Size: 9826 bytes --]
> Lorenzo Bianconi <lorenzo.bianconi@redhat.com> writes:
>
> >>
> >>
> >> On 21/09/2024 22.17, Alexander Lobakin wrote:
> >> > From: Lorenzo Bianconi <lorenzo@kernel.org>
> >> > Date: Sat, 21 Sep 2024 18:52:56 +0200
> >> >
> >> > > This series introduces the xdp_rx_meta struct in the xdp_buff/xdp_frame
> >> >
> >> > &xdp_buff is on the stack.
> >> > &xdp_frame consumes headroom.
> >> >
> >> > IOW they're size-sensitive and putting metadata directly there might
> >> > play bad; if not now, then later.
> >> >
> >> > Our idea (me + Toke) was as follows:
> >> >
> >> > - new BPF kfunc to build generic meta. If called, the driver builds a
> >> > generic meta with hash, csum etc., in the data_meta area.
> >>
> >> I do agree that it should be the XDP prog (via a new BPF kfunc) that
> >> decide if xdp_frame should be updated to contain a generic meta struct.
> >> *BUT* I think we should use the xdp_frame area, and not the
> >> xdp->data_meta area.
> >
> > ack, I will add a new kfunc for it.
> >
> >>
> >> A details is that I think this kfunc should write data directly into
> >> xdp_frame area, even then we are only operating on the xdp_buff, as we
> >> do have access to the area xdp_frame will be created in.
> >
> > this would avoid to copy it when we convert from xdp_buff to xdp_frame, nice :)
> >
> >>
> >>
> >> When using data_meta area, then netstack encap/decap needs to move the
> >> data_meta area (extra cycles). The xdp_frame area (live in top) don't
> >> have this issue.
> >>
> >> It is easier to allow xdp_frame area to survive longer together with the
> >> SKB. Today we "release" this xdp_frame area to be used by SKB for extra
> >> headroom (see xdp_scrub_frame). I can imagine that we can move SKB
> >> fields to this area, and reduce the size of the SKB alloc. (This then
> >> becomes the mini-SKB we discussed a couple of years ago).
> >>
> >>
> >> > Yes, this also consumes headroom, but only when the corresponding func
> >> > is called. Introducing new fields like you're doing will consume it
> >> > unconditionally;
> >>
> >> We agree on the kfunc call marks area as consumed/in-use. We can extend
> >> xdp_frame statically like Lorenzo does (with struct xdp_rx_meta), but
> >> xdp_frame->flags can be used for marking this area as used or not.
> >
> > the only downside with respect to a TLV approach would be to consume all the
> > xdp_rx_meta as soon as we set a single xdp rx hw hint in it, right?
> > The upside is it is easier and it requires less instructions.
>
> FYI, we also had a discussion related to this at LPC on Friday, in this
> session: https://lpc.events/event/18/contributions/1935/
Hi Toke,
thx for the pointer
>
> The context here was that Arthur and Jakub want to also support extended
> rich metadata all the way through the SKB path, and are looking at the
> same area used for XDP metadata to store it. So there's a need to manage
> both the kernel's own usage of that area, and userspace/BPF usage of it.
it would be cool if we can collaborate on it.
>
> I'll try to summarise some of the points of that discussion (all
> interpretations are my own, of course):
>
> - We want something that can be carried with a frame all the way from
> the XDP layer, through all SKB layers and to userspace (to replace the
> use of skb->mark for this purpose).
>
> - We want different applications running on the system (of which the
> kernel itself if one, cf this discussion) to be able to share this
> field, without having to have an out of band registry (like a Github
> repository where applications can agree on which bits to use). Which
> probably means that the kernel needs to be in the loop somehow to
> explicitly allocate space in the metadata area and track offsets.
>
> - Having an explicit API to access this from userspace, without having
> to go through BPF (i.e., a socket- or CMSG-based API) would be useful.
>
>
> The TLV format was one of the suggestions in Arthur and Jakub's talk,
> but AFAICT, there was not a lot of enthusiasm about this in the room
> (myself included), because of the parsing overhead and complexity. I
> believe the alternative that was seen as most favourable was a map
> lookup-style API, where applications can request a metadata area of
> arbitrary size and get an ID assigned that they can then use to set/get
> values in the data path.
>
> So, sketching this out, this could be realised by something like:
>
> /* could be called from BPF, or through netlink or sysfs; may fail, if
> * there is no more space
> */
> int metadata_id = register_packet_metadata_field(sizeof(struct my_meta));
>
> The ID is just an opaque identifier that can then be passed to
> getter/setter functions (for both SKB and XDP), like:
>
> ret = bpf_set_packet_metadata_field(pkt, metadata_id,
> &my_meta_value, sizeof(my_meta_value))
>
> ret = bpf_get_packet_metadata_field(pkt, metadata_id,
> &my_meta_value, sizeof(my_meta_value))
>
>
> On the kernel side, the implementation would track registered fields in
> a global structure somewhere, say:
>
> struct pkt_metadata_entry {
> int id;
> u8 sz;
> u8 offset;
> u8 bit;
> };
>
> struct pkt_metadata_registry { /* allocated as a system-wide global */
> u8 num_entries;
> u8 total_size;
> struct pkt_metadata_entry entries[MAX_ENTRIES];
> };
>
> struct xdp_rx_meta { /* at then end of xdp_frame */
> u8 sz; /* set to pkt_metadata_registry->total_size on alloc */
> u8 fields_set; /* bitmap of fields that have been set, see below */
> u8 data[];
> };
>
> int register_packet_metadata_field(u8 size) {
> struct pkt_metadata_registry *reg = get_global_registry();
> struct pkt_metadata_entry *entry;
>
> if (size + reg->total_size > MAX_METADATA_SIZE)
> return -ENOSPC;
>
> entry = ®->entries[reg->num_entries++];
> entry->id = assign_id();
> entry->sz = size;
> entry->offset = reg->total_size;
> entry->bit = reg->num_entries - 1;
> reg->total_size += size;
>
> return entry->id;
> }
>
> int bpf_set_packet_metadata_field(struct xdp_frame *frm, int id, void
> *value, size_t sz)
> {
> struct pkt_metadata_entry *entry = get_metadata_entry_by_id(id);
>
> if (!entry)
> return -ENOENT;
>
> if (entry->sz != sz)
> return -EINVAL; /* user error */
>
> if (frm->rx_meta.sz < entry->offset + sz)
> return -EFAULT; /* entry allocated after xdp_frame was initialised */
>
> memcpy(&frm->rx_meta.data + entry->offset, value, sz);
> frm->rx_meta.fields_set |= BIT(entry->bit);
>
> return 0;
> }
>
> int bpf_get_packet_metadata_field(struct xdp_frame *frm, int id, void
> *value, size_t sz)
> {
> struct pkt_metadata_entry *entry = get_metadata_entry_by_id(id);
>
> if (!entry)
> return -ENOENT;
>
> if (entry->sz != sz)
> return -EINVAL;
>
> if (frm->rx_meta.sz < entry->offset + sz)
> return -EFAULT; /* entry allocated after xdp_frame was initialised */
>
> if (!(frm->rx_meta.fields_set & BIT(entry->bit)))
> return -ENOENT;
>
> memcpy(value, &frm->rx_meta.data + entry->offset, sz);
>
> return 0;
> }
>
> I'm hinting at some complications here (with the EFAULT return) that
> needs to be resolved: there is no guarantee that a given packet will be
> in sync with the current status of the registered metadata, so we need
> explicit checks for this. If metadata entries are de-registered again
> this also means dealing with holes and/or reshuffling the metadata
> layout to reuse the released space (incidentally, this is the one place
> where a TLV format would have advantages).
I like this approach but it seems to me more suitable for 'sw' metadata
(this is main Arthur and Jakub use case iiuc) where the userspace would
enable/disable these functionalities system-wide.
Regarding device hw metadata (e.g. checksum offload) I can see some issues
since on a system we can have multiple NICs with different capabilities.
If we consider current codebase, stmmac driver supports only rx timestamp,
while mlx5 supports all of them. In a theoretical system with these two
NICs, since pkt_metadata_registry is global system-wide, we will end-up
with quite a lot of holes for the stmmac, right? (I am not sure if this
case is relevant or not). In other words, we will end-up with a fixed
struct for device rx hw metadata (like xdp_rx_meta). So I am wondering
if we really need all this complexity for xdp rx hw metadata?
Maybe we can start with a simple approach for xdp rx hw metadata putting
the struct in xdp_frame as suggested by Jesper and covering the most common
use-cases. We can then integrate this approach with Arthur/Jakub's solution
without introducing any backward compatibility issue since these field are
not visible to userspace.
Regards,
Lorenzo
>
> The nice thing about an API like this, though, is that it's extensible,
> and the kernel itself can be just another consumer of it for the
> metadata fields Lorenzo is adding in this series. I.e., we could just
> pre-define some IDs for metadata vlan, timestamp etc, and use the same
> functions as above from within the kernel to set and get those values;
> using the registry, there could even be an option to turn those off if
> an application wants more space for its own usage. Or, alternatively, we
> could keep the kernel-internal IDs hardcoded and always allocated, and
> just use the getter/setter functions as the BPF API for accessing them.
>
> -Toke
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
next prev parent reply other threads:[~2024-09-22 15:40 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-21 16:52 [RFC bpf-next 0/4] Add XDP rx hw hints support performing XDP_REDIRECT Lorenzo Bianconi
2024-09-21 16:52 ` [RFC bpf-next 1/4] net: xdp: Add xdp_rx_meta structure Lorenzo Bianconi
2024-09-21 16:52 ` [RFC bpf-next 2/4] net: xdp: Update rx_hash of xdp_rx_meta struct running xmo_rx_hash callback Lorenzo Bianconi
2024-09-21 16:52 ` [RFC bpf-next 3/4] net: xdp: Update rx_vlan of xdp_rx_meta struct running xmo_rx_vlan_tag callback Lorenzo Bianconi
2024-09-21 16:53 ` [RFC bpf-next 4/4] net: xdp: Update rx timestamp of xdp_rx_meta struct running xmo_rx_timestamp callback Lorenzo Bianconi
2024-09-21 20:17 ` [RFC bpf-next 0/4] Add XDP rx hw hints support performing XDP_REDIRECT Alexander Lobakin
2024-09-21 21:36 ` Jesper Dangaard Brouer
2024-09-22 9:17 ` Lorenzo Bianconi
2024-09-22 11:12 ` Toke Høiland-Jørgensen
2024-09-22 15:40 ` Lorenzo Bianconi [this message]
2024-09-26 10:54 ` Toke Høiland-Jørgensen
2024-09-26 14:57 ` Lorenzo Bianconi
2024-09-27 1:43 ` Stanislav Fomichev
2024-09-26 11:31 ` Arthur Fabre
2024-09-26 12:41 ` Toke Høiland-Jørgensen
2024-09-26 15:44 ` Arthur Fabre
2024-09-27 10:24 ` Toke Høiland-Jørgensen
2024-09-27 14:46 ` Arthur Fabre
2024-09-27 15:06 ` Lorenzo Bianconi
2024-09-30 10:58 ` Toke Høiland-Jørgensen
2024-09-30 11:49 ` Lorenzo Bianconi
2024-10-01 14:16 ` Arthur Fabre
2024-10-01 14:54 ` Lorenzo Bianconi
2024-10-01 15:14 ` Toke Høiland-Jørgensen
2024-10-02 17:02 ` Stanislav Fomichev
2024-10-02 18:38 ` Toke Høiland-Jørgensen
2024-10-02 22:49 ` Stanislav Fomichev
2024-10-03 6:35 ` Arthur Fabre
2024-10-03 20:26 ` Stanislav Fomichev
2024-10-04 2:13 ` Daniel Xu
2024-10-04 10:38 ` Jesper Dangaard Brouer
2024-10-04 13:55 ` Arthur Fabre
2024-10-04 14:14 ` Jesper Dangaard Brouer
2024-10-04 14:18 ` Lorenzo Bianconi
2024-10-04 14:29 ` Arthur Fabre
2024-10-04 17:53 ` Stanislav Fomichev
2024-10-06 10:27 ` Toke Høiland-Jørgensen
2024-10-07 18:48 ` Stanislav Fomichev
2024-10-08 7:15 ` Arthur Fabre
2024-10-04 16:27 ` Stanislav Fomichev
2024-09-30 10:52 ` Toke Høiland-Jørgensen
2024-10-01 14:06 ` Arthur Fabre
2024-10-01 15:28 ` Toke Høiland-Jørgensen
2024-10-03 6:51 ` Arthur Fabre
2024-09-22 9:08 ` Lorenzo Bianconi
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=ZvA6hIl6XWJ4UEJW@lore-desk \
--to=lorenzo.bianconi@redhat.com \
--cc=afabre@cloudflare.com \
--cc=aleksander.lobakin@intel.com \
--cc=alexandre.torgue@foss.st.com \
--cc=anthony.l.nguyen@intel.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=intel-wired-lan@lists.osuosl.org \
--cc=jakub@cloudflare.com \
--cc=jasowang@redhat.com \
--cc=john.fastabend@gmail.com \
--cc=kernel-team@cloudflare.com \
--cc=kuba@kernel.org \
--cc=lorenzo@kernel.org \
--cc=mcoquelin.stm32@gmail.com \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=saeedm@nvidia.com \
--cc=sdf@fomichev.me \
--cc=tariqt@nvidia.com \
--cc=toke@redhat.com \
--cc=yan@cloudflare.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 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).