All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC bpf-next 0/9] Make TC BPF helpers preserve skb metadata
@ 2025-09-29 14:09 Jakub Sitnicki
  2025-09-29 14:09 ` [PATCH RFC bpf-next 1/9] net: Preserve metadata on pskb_expand_head Jakub Sitnicki
                   ` (8 more replies)
  0 siblings, 9 replies; 11+ messages in thread
From: Jakub Sitnicki @ 2025-09-29 14:09 UTC (permalink / raw)
  To: bpf; +Cc: netdev, kernel-team

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
operating on skb->data.

Currently, several helpers that adjust the skb->data pointer or reallocate
skb->head do not preserve metadata at its expected location (before the MAC
header) after the operation. Affected helpers include:

- bpf_skb_adjust_room
- bpf_skb_change_head
- bpf_skb_change_proto
- bpf_skb_change_tail
- bpf_skb_vlan_push
- bpf_skb_vlan_pop
- (did I miss any?)

Sadly, in TC BPF context, metadata must be moved whenever headroom changes
to keep the skb->data_meta pointer valid (unless someone can come up with a
workaround for that...).

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 RFC implements option (1), expecting that "you can have just one
memmove" will be the most obvious feedback, while readability is a somewhat
more subjective matter of taste (which I don't claim to have ;-).

TODO:

- Extend skb metadata tests inselftests/bpf. So far, I've only adapted
tests for cloned skbs. However, the changes have been tested using a shell
script–based test suite [2], which allowed for faster iteration in this
early phase.

PTAL. Early comments and feedback much appreciated.

Thanks,
-jkbs

[1] https://lore.kernel.org/all/20250814-skb-metadata-thru-dynptr-v7-0-8a39e636e0fb@cloudflare.com/
[2] https://github.com/jsitnicki/skb-metadata-tests

---
Jakub Sitnicki (9):
      net: Preserve metadata on pskb_expand_head
      net: Helper to move packet data and metadata after skb_push/pull
      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: Expect unclone to preserve metadata

 include/linux/if_vlan.h                            | 13 ++-
 include/linux/skbuff.h                             | 74 +++++++++++++++++
 net/core/filter.c                                  | 16 ++--
 net/core/skbuff.c                                  |  2 -
 .../bpf/prog_tests/xdp_context_test_run.c          | 20 ++---
 tools/testing/selftests/bpf/progs/test_xdp_meta.c  | 94 +++++++++++++---------
 6 files changed, 156 insertions(+), 63 deletions(-)


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

end of thread, other threads:[~2025-10-03 12:03 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-29 14:09 [PATCH RFC bpf-next 0/9] Make TC BPF helpers preserve skb metadata Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 1/9] net: Preserve metadata on pskb_expand_head Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 2/9] net: Helper to move packet data and metadata after skb_push/pull Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 3/9] vlan: Make vlan_remove_tag return nothing Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 4/9] bpf: Make bpf_skb_vlan_pop helper metadata-safe Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 5/9] bpf: Make bpf_skb_vlan_push " Jakub Sitnicki
2025-10-03 12:03   ` Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 6/9] bpf: Make bpf_skb_adjust_room metadata-safe Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 7/9] bpf: Make bpf_skb_change_proto helper metadata-safe Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 8/9] bpf: Make bpf_skb_change_head " Jakub Sitnicki
2025-09-29 14:09 ` [PATCH RFC bpf-next 9/9] selftests/bpf: Expect unclone to preserve metadata Jakub Sitnicki

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.