netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 00/10] Add a dynptr type for skb metadata for TC BPF
@ 2025-07-21 10:52 Jakub Sitnicki
  2025-07-21 10:52 ` [PATCH bpf-next v3 01/10] bpf: Add dynptr type for skb metadata Jakub Sitnicki
                   ` (9 more replies)
  0 siblings, 10 replies; 25+ messages in thread
From: Jakub Sitnicki @ 2025-07-21 10:52 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Arthur Fabre,
	Daniel Borkmann, Eric Dumazet, Jakub Kicinski,
	Jesper Dangaard Brouer, Jesse Brandeburg, Joanne Koong,
	Lorenzo Bianconi, Martin KaFai Lau,
	Toke Høiland-Jørgensen, Yan Zhai, kernel-team, netdev,
	Jakub Sitnicki, Stanislav Fomichev

TL;DR
-----

This is the first step in an effort which aims to enable skb metadata
access for all BPF programs which operate on an skb context.

By skb metadata we mean the custom metadata area which can be allocated
from an XDP program with the bpf_xdp_adjust_meta helper [1]. Network stack
code accesses it using the skb_metadata_* helpers.

Changelog
---------
Changes in v3:
- Add a kfunc set for skb metadata access. Limited to TC BPF. (Martin)
- Drop patches related to skb metadata access outside of TC BPF:
      net: Clear skb metadata on handover from device to protocol
      selftests/bpf: Cover lack of access to skb metadata at ip layer
      selftests/bpf: Count successful bpf program runs
- Link to v2: https://lore.kernel.org/r/20250716-skb-metadata-thru-dynptr-v2-0-5f580447e1df@cloudflare.com

Changes in v2:
- Switch to a dedicated dynptr type for skb metadata (Andrii)
- Add verifier test coverage since we now touch its code
- Add missing test coverage for bpf_dynptr_adjust and access at an offset
- Link to v1: https://lore.kernel.org/r/20250630-skb-metadata-thru-dynptr-v1-0-f17da13625d8@cloudflare.com

Overview
--------

Today, the skb metadata is accessible only by the BPF TC ingress programs
through the __sk_buff->data_meta pointer. We propose a three step plan to
make skb metadata available to all other BPF programs which operate on skb
objects:

 1) Add a dynptr type for skb metadata (this patch set)

    This is a preparatory step, but it also stands on its own. Here we
    enable access to the skb metadata through a bpf_dynptr, the same way we
    can already access the skb payload today.

    As the the next step (2), we want to relocate the metadata as skb
    travels through the network stack in order to persist it. That will
    require a safe way to access the metadata area irrespective of its
    location.

    This is where the dynptr [2] comes into play. It solves exactly that
    problem. A dynptr to skb metadata can be backed by a memory area that
    resides in a different location depending on the code path.

 2) Persist skb metadata past the TC hook (future)

    Having the metadata in front of the packet headers as the skb travels
    through the network stack is problematic - see the discussion of
    alternative approaches below. Hence, we plan to relocate it as
    necessary past the TC hook.

    Where to relocate it? We don't know yet. There are a couple of
    options: (i) move it to the top of skb headroom, or (ii) allocate
    dedicated memory for it.  They are not mutually exclusive. The right
    solution might be a mix.

    When to relocate it? That is also an open question. It could be done
    during device to protocol handover or lazily when headers get pushed or
    headroom gets resized.

 3) skb dynptr for sockops, sk_lookup, etc. (future)

    There are BPF program types don't operate on __sk_buff context, but
    either have, or could have, access to the skb itself. As a final touch,
    we want to provide a way to create an skb metadata dynptr for these
    program types.

TIMTOWDI
--------

Alternative approaches which we considered:

* Keep the metadata always in front of skb->data

We think it is a bad idea for two reasons, outlined below. Nevertheless we
are open to it, if necessary.

 1) Performance concerns

    It would require the network stack to move the metadata on each header
    pull/push - see skb_reorder_vlan_header() [3] for an example. While
    doable, there is an expected performance overhead.

 2) Potential for bugs

    In addition to updating skb_push/pull and pskp_expand_head, we would
    need to audit any code paths which operate on skb->data pointer
    directly without going through the helpers. This creates a "known
    unknown" risk.

* Design a new custom metadata area from scratch

We have tried that in Arthur's patch set [4]. One of the outcomes of the
discussion there was that we don't want to have two places to store custom
metadata. Hence the change of approach to make the existing custom metadata
area work.

-jkbs

[1] https://docs.ebpf.io/linux/helper-function/bpf_xdp_adjust_meta/
[2] https://docs.ebpf.io/linux/concepts/dynptrs/
[3] https://elixir.bootlin.com/linux/v6.16-rc6/source/net/core/skbuff.c#L6211
[4] https://lore.kernel.org/all/20250422-afabre-traits-010-rfc2-v2-0-92bcc6b146c9@arthurfabre.com/

---
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Arthur Fabre <arthur@arthurfabre.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Jesper Dangaard Brouer <hawk@kernel.org>
Cc: Jesse Brandeburg <jbrandeburg@cloudflare.com>
Cc: Joanne Koong <joannelkoong@gmail.com>
Cc: Lorenzo Bianconi <lorenzo@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Stanislav Fomichev <stfomichev@gmail.com>
Cc: Toke Høiland-Jørgensen <thoiland@redhat.com>
Cc: Yan Zhai <yan@cloudflare.com>
Cc: kernel-team@cloudflare.com
Cc: netdev@vger.kernel.org
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>

---
Jakub Sitnicki (10):
      bpf: Add dynptr type for skb metadata
      bpf: Enable read access to skb metadata with bpf_dynptr_read
      bpf: Enable write access to skb metadata with bpf_dynptr_write
      bpf: Enable read-write access to skb metadata with dynptr slice
      selftests/bpf: Cover verifier checks for skb_meta dynptr type
      selftests/bpf: Pass just bpf_map to xdp_context_test helper
      selftests/bpf: Parametrize test_xdp_context_tuntap
      selftests/bpf: Cover read access to skb metadata via dynptr
      selftests/bpf: Cover write access to skb metadata via dynptr
      selftests/bpf: Cover read/write to skb metadata at an offset

 include/linux/bpf.h                                |  14 +-
 include/linux/filter.h                             |  22 ++
 kernel/bpf/helpers.c                               |   7 +
 kernel/bpf/log.c                                   |   2 +
 kernel/bpf/verifier.c                              |  23 +-
 net/core/filter.c                                  |  76 ++++++
 tools/testing/selftests/bpf/bpf_kfuncs.h           |   3 +
 tools/testing/selftests/bpf/prog_tests/dynptr.c    |   1 +
 .../bpf/prog_tests/xdp_context_test_run.c          |  92 ++++++--
 tools/testing/selftests/bpf/progs/dynptr_fail.c    | 258 +++++++++++++++++++++
 tools/testing/selftests/bpf/progs/dynptr_success.c |  22 ++
 tools/testing/selftests/bpf/progs/test_xdp_meta.c  | 182 +++++++++++++++
 12 files changed, 682 insertions(+), 20 deletions(-)


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2025-07-23 16:50 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-21 10:52 [PATCH bpf-next v3 00/10] Add a dynptr type for skb metadata for TC BPF Jakub Sitnicki
2025-07-21 10:52 ` [PATCH bpf-next v3 01/10] bpf: Add dynptr type for skb metadata Jakub Sitnicki
2025-07-22 18:46   ` Eduard Zingerman
2025-07-22 19:10     ` Eduard Zingerman
2025-07-23  0:37   ` Martin KaFai Lau
2025-07-23  9:02     ` Jakub Sitnicki
2025-07-21 10:52 ` [PATCH bpf-next v3 02/10] bpf: Enable read access to skb metadata with bpf_dynptr_read Jakub Sitnicki
2025-07-22 18:49   ` Eduard Zingerman
2025-07-23 16:50     ` Jakub Sitnicki
2025-07-21 10:52 ` [PATCH bpf-next v3 03/10] bpf: Enable write access to skb metadata with bpf_dynptr_write Jakub Sitnicki
2025-07-21 10:52 ` [PATCH bpf-next v3 04/10] bpf: Enable read-write access to skb metadata with dynptr slice Jakub Sitnicki
2025-07-21 10:52 ` [PATCH bpf-next v3 05/10] selftests/bpf: Cover verifier checks for skb_meta dynptr type Jakub Sitnicki
2025-07-22 19:22   ` Eduard Zingerman
2025-07-21 10:52 ` [PATCH bpf-next v3 06/10] selftests/bpf: Pass just bpf_map to xdp_context_test helper Jakub Sitnicki
2025-07-22 20:24   ` Eduard Zingerman
2025-07-21 10:52 ` [PATCH bpf-next v3 07/10] selftests/bpf: Parametrize test_xdp_context_tuntap Jakub Sitnicki
2025-07-22 20:24   ` Eduard Zingerman
2025-07-21 10:52 ` [PATCH bpf-next v3 08/10] selftests/bpf: Cover read access to skb metadata via dynptr Jakub Sitnicki
2025-07-22 20:24   ` Eduard Zingerman
2025-07-21 10:52 ` [PATCH bpf-next v3 09/10] selftests/bpf: Cover write " Jakub Sitnicki
2025-07-22 20:25   ` Eduard Zingerman
2025-07-21 10:52 ` [PATCH bpf-next v3 10/10] selftests/bpf: Cover read/write to skb metadata at an offset Jakub Sitnicki
2025-07-22 20:26   ` Eduard Zingerman
2025-07-22 20:30   ` Eduard Zingerman
2025-07-23  9:09     ` Jakub Sitnicki

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