From: Jakub Sitnicki <jakub@cloudflare.com>
To: bpf@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>, Simon Horman <horms@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Stanislav Fomichev <sdf@fomichev.me>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
KP Singh <kpsingh@kernel.org>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
Arthur Fabre <arthur@arthurfabre.com>,
Jesper Dangaard Brouer <hawk@kernel.org>,
netdev@vger.kernel.org, kernel-team@cloudflare.com
Subject: [PATCH bpf-next v4 00/16] Make TC BPF helpers preserve skb metadata
Date: Wed, 05 Nov 2025 21:19:37 +0100 [thread overview]
Message-ID: <20251105-skb-meta-rx-path-v4-0-5ceb08a9b37b@cloudflare.com> (raw)
Changes in v4:
- Fix copy-paste bug in check_metadata() test helper (AI review)
- Add "out of scope" section (at the bottom)
- Link to v3: https://lore.kernel.org/r/20251026-skb-meta-rx-path-v3-0-37cceebb95d3@cloudflare.com
Changes in v3:
- Use the already existing BPF_STREAM_STDERR const in tests (Martin)
- Unclone skb head on bpf_dynptr_write to skb metadata (patch 3) (Martin)
- Swap order of patches 1 & 2 to refer to skb_postpush_data_move() in docs
- Mention in skb_data_move() docs how to move just the metadata
- Note in pskb_expand_head() docs to move metadata after skb_push() (Jakub)
- Link to v2: https://lore.kernel.org/r/20251019-skb-meta-rx-path-v2-0-f9a58f3eb6d6@cloudflare.com
Changes in v2:
- Tweak WARN_ON_ONCE check in skb_data_move() (patch 2)
- Convert all tests to verify skb metadata in BPF (patches 9-10)
- Add test coverage for modified BPF helpers (patches 12-15)
- Link to RFCv1: https://lore.kernel.org/r/20250929-skb-meta-rx-path-v1-0-de700a7ab1cb@cloudflare.com
This patch set continues our work [1] to allow BPF programs and user-space
applications to attach multiple bytes of metadata to packets via the
XDP/skb metadata area.
The focus of this patch set it to ensure that skb metadata remains intact
when packets pass through a chain of TC BPF programs that call helpers
which operate on skb head.
Currently, several helpers that either adjust the skb->data pointer or
reallocate skb->head do not preserve metadata at its expected location,
that is immediately in front of the MAC header. These are:
- bpf_skb_adjust_room
- bpf_skb_change_head
- bpf_skb_change_proto
- bpf_skb_change_tail
- bpf_skb_vlan_pop
- bpf_skb_vlan_push
In TC BPF context, metadata must be moved whenever skb->data changes to
keep the skb->data_meta pointer valid. I don't see any way around
it. Creative ideas how to avoid that would be very welcome.
With that in mind, we can patch the helpers in at least two different ways:
1. Integrate metadata move into header move
Replace the existing memmove, which follows skb_push/pull, with a helper
that moves both headers and metadata in a single call. This avoids an
extra memmove but reduces transparency.
skb_pull(skb, len);
- memmove(skb->data, skb->data - len, n);
+ skb_postpull_data_move(skb, len, n);
skb->mac_header += len;
skb_push(skb, len)
- memmove(skb->data, skb->data + len, n);
+ skb_postpush_data_move(skb, len, n);
skb->mac_header -= len;
2. Move metadata separately
Add a dedicated metadata move after the header move. This is more
explicit but costs an additional memmove.
skb_pull(skb, len);
memmove(skb->data, skb->data - len, n);
+ skb_metadata_postpull_move(skb, len);
skb->mac_header += len;
skb_push(skb, len)
+ skb_metadata_postpush_move(skb, len);
memmove(skb->data, skb->data + len, n);
skb->mac_header -= len;
This patch set implements option (1), expecting that "you can have just one
memmove" will be the most obvious feedback, while readability is a,
somewhat subjective, matter of taste, which I don't claim to have ;-)
The structure of the patch set is as follows:
- patches 1-4 prepare ground for safe-proofing the BPF helpers
- patches 5-9 modify the BPF helpers to preserve skb metadata
- patches 10-11 prepare ground for metadata tests with BPF helper calls
- patches 12-16 adapt and expand tests to cover the made changes
Out of scope for this series:
- safe-proofing tunnel & tagging devices - VLAN, GRE, ...
(next in line, in development preview at [2])
- metadata access after packet foward
(to do after Rx path - once metadata reliably reaches sk_filter)
Thanks,
-jkbs
[1] https://lore.kernel.org/all/20250814-skb-metadata-thru-dynptr-v7-0-8a39e636e0fb@cloudflare.com/
[2] https://github.com/jsitnicki/linux/commits/skb-meta/safeproof-netdevs/
---
Jakub Sitnicki (16):
net: Helper to move packet data and metadata after skb_push/pull
net: Preserve metadata on pskb_expand_head
bpf: Unclone skb head on bpf_dynptr_write to skb metadata
vlan: Make vlan_remove_tag return nothing
bpf: Make bpf_skb_vlan_pop helper metadata-safe
bpf: Make bpf_skb_vlan_push helper metadata-safe
bpf: Make bpf_skb_adjust_room metadata-safe
bpf: Make bpf_skb_change_proto helper metadata-safe
bpf: Make bpf_skb_change_head helper metadata-safe
selftests/bpf: Verify skb metadata in BPF instead of userspace
selftests/bpf: Dump skb metadata on verification failure
selftests/bpf: Expect unclone to preserve skb metadata
selftests/bpf: Cover skb metadata access after vlan push/pop helper
selftests/bpf: Cover skb metadata access after bpf_skb_adjust_room
selftests/bpf: Cover skb metadata access after change_head/tail helper
selftests/bpf: Cover skb metadata access after bpf_skb_change_proto
include/linux/filter.h | 9 +
include/linux/if_vlan.h | 13 +-
include/linux/skbuff.h | 75 ++++
kernel/bpf/helpers.c | 6 +-
net/core/filter.c | 34 +-
net/core/skbuff.c | 6 +-
.../bpf/prog_tests/xdp_context_test_run.c | 129 ++++---
tools/testing/selftests/bpf/progs/test_xdp_meta.c | 386 +++++++++++++++------
8 files changed, 475 insertions(+), 183 deletions(-)
next reply other threads:[~2025-11-05 20:19 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-05 20:19 Jakub Sitnicki [this message]
2025-11-05 20:19 ` [PATCH bpf-next v4 01/16] net: Helper to move packet data and metadata after skb_push/pull Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 02/16] net: Preserve metadata on pskb_expand_head Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 03/16] bpf: Unclone skb head on bpf_dynptr_write to skb metadata Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 04/16] vlan: Make vlan_remove_tag return nothing Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 05/16] bpf: Make bpf_skb_vlan_pop helper metadata-safe Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 06/16] bpf: Make bpf_skb_vlan_push " Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 07/16] bpf: Make bpf_skb_adjust_room metadata-safe Jakub Sitnicki
2025-11-05 20:42 ` bot+bpf-ci
2025-11-10 12:17 ` Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 08/16] bpf: Make bpf_skb_change_proto helper metadata-safe Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 09/16] bpf: Make bpf_skb_change_head " Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 10/16] selftests/bpf: Verify skb metadata in BPF instead of userspace Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 11/16] selftests/bpf: Dump skb metadata on verification failure Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 12/16] selftests/bpf: Expect unclone to preserve skb metadata Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 13/16] selftests/bpf: Cover skb metadata access after vlan push/pop helper Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 14/16] selftests/bpf: Cover skb metadata access after bpf_skb_adjust_room Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 15/16] selftests/bpf: Cover skb metadata access after change_head/tail helper Jakub Sitnicki
2025-11-05 20:19 ` [PATCH bpf-next v4 16/16] selftests/bpf: Cover skb metadata access after bpf_skb_change_proto Jakub Sitnicki
2025-11-10 19:30 ` [PATCH bpf-next v4 00/16] Make TC BPF helpers preserve skb metadata patchwork-bot+netdevbpf
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=20251105-skb-meta-rx-path-v4-0-5ceb08a9b37b@cloudflare.com \
--to=jakub@cloudflare.com \
--cc=andrii@kernel.org \
--cc=arthur@arthurfabre.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=haoluo@google.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-team@cloudflare.com \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=martin.lau@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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).