Skip to content

RFC bpf: add bpf_sk_assign support on netkit egress

Nikolay Aleksandrov bpf netdev sashiko-reviews 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest4 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4net: track references owned by prefetched socket assignmentsinclude/net/inet6_hashtables.h include/net/inet_hashtables.h include/net/request_sock.h +3 more+38 -190 replies
2/4bpf: preserve owned socket assignments on ingress redirectsnet/core/filter.c+37 -00 replies
3/4bpf: support bpf_sk_assign in netkit tx pathdrivers/net/netkit.c include/linux/filter.h include/net/netkit.h +3 more+53 -71 reply
4/4selftests/bpf: test netkit socket assignment to a transparent proxytools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c tools/testing/selftests/bpf/progs/sk_assign_netkit.c+284 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress

Hi,
This RFC narrows the earlier egress series [1] to netkit's transmit path. NETKIT_PASS queues the assigned skb directly to the peer's receive backlog. NETKIT_REDIRECT clears the assignment before forwarding.

General TC egress support would require changes across the networking stack. Many transmit paths rely on skb->sk and skb->destructor for send-side ownership and socket write-memory accounting, and would need to handle an assigned socket reference in place of the transmit owner.

There is a new sk_assign_skb added to bpf_net_context that is used to record the exact skb netkit is running BPF on, bpf_sk_assign uses that identity to allow egress assignment for that skb. It is valid only when the new kern_flags bit is set, that is used to avoid always initializing it to NULL in bpf_net_ctx_set.
A nested tc egress program running on a clone still gets -EOPNOTSUPP.

This series builds on Daniel's work [2] to steer netkit traffic to a transparent proxy in the receiving namespace. The receive backlog can outlive the transmit path's RCU read-side section, so the skb takes its own socket reference even for SOCK_RCU_FREE listeners.

The selftests exercise the new netkit sk assign for both IPv4/IPv6 and TCP/UDP.

Feedback is most welcome, more importantly if the approach taken in patch 03 is acceptable.

Changes since the original posting:
- Use a new approach to limit the egress sk assign only to netkit
- Reduce the number of selftests
- Drop some unnecessary fixes of pre-existing problems

Pre-existing problems found during reviews that will be dealt with separately:
- Receive lookup could use a prefetched socket whose protocol, family or network namespace did not match the packet
- GRO via gro_cells can merge an assigned skb after tunnel decapsulation and discard its socket reference without calling the destructor
- When a reuseport program rejects an assigned UDP socket, receive can report an error pointer as rx_sk to the kfree_skb tracepoint
- A generic XDP program, an rx_handler or a classic mirred action on the receiving device can forward the assigned skb to another network namespace. Generic XDP can also drop the assignment when it copies the skb, so the packet falls back to normal socket lookup
- nsim_forward_skb() queues the skb for napi_gro_receive() without orphaning it. GRO can discard its socket reference when merging it

Thanks,
Nik

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://github.com/cilium/linux/commits/pr/bpf-tproxy

Nikolay Aleksandrov (4):
net: track references owned by prefetched socket assignments
bpf: preserve owned socket assignments on ingress redirects
bpf: support bpf_sk_assign in netkit tx path selftests/bpf: test netkit socket assignment to a transparent proxy

 drivers/net/netkit.c                          |  10 +
 include/linux/filter.h                        |   4 +
 include/net/inet6_hashtables.h                |  10 +-
 include/net/inet_hashtables.h                 |  10 +-
 include/net/netkit.h                          |  15 ++
 include/net/request_sock.h                    |  10 +-
 include/net/sock.h                            |   4 +-
 include/uapi/linux/bpf.h                      |   9 +-
 net/core/filter.c                             |  50 ++++-
 net/core/sock.c                               |   7 +
 net/netfilter/nf_queue.c                      |  16 +-
 tools/include/uapi/linux/bpf.h                |   9 +-
 .../bpf/prog_tests/sk_assign_netkit.c         | 189 ++++++++++++++++++
 .../selftests/bpf/progs/sk_assign_netkit.c    |  95 +++++++++
 14 files changed, 412 insertions(+), 26 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c
 create mode 100644 tools/testing/selftests/bpf/progs/sk_assign_netkit.c
Signature · 1 line
2.47.3