Skip to content

implement bpf iterator for tcp and udp sockets

Yonghong Song bpf netdev 5 versions – Applied

Status from this message: Applied. Thanks

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest15 patches 1 replyApplied
  2. v415 patches 3 repliesSuperseded
  3. v315 patches 19 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v215 patches8 repliesSuperseded
v113 patches9 repliesSuperseded

Changes in v5 from the cover letter

- fix bpf_skc_to_udp6_sock helper as besides sk_protocol, sk_family, sk_type == SOCK_DGRAM is also needed to differentiate from SOCK_RAW (Eric)

Patches in v5 Applied

#Subject and filesSizeRepliesApplied
1/15net: bpf: add bpf_seq_afinfo in tcp_iter_stateinclude/net/tcp.h net/ipv4/tcp_ipv4.c+25 -60 replies
2/15net: bpf: implement bpf iterator for tcpnet/ipv4/tcp_ipv4.c+123 -00 replies
3/15bpf: support 'X' in bpf_seq_printf() helperkernel/trace/bpf_trace.c+2 -10 replies
4/15bpf: allow tracing programs to use bpf_jiffies64() helperkernel/trace/bpf_trace.c+2 -00 replies
5/15bpf: add bpf_skc_to_tcp6_sock() helperinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/btf.c +5 more+148 -120 replies
6/15bpf: add bpf_skc_to_{tcp,tcp_timewait,tcp_request}_sock() helpersinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/trace/bpf_trace.c +3 more+121 -20 replies
7/15net: bpf: add bpf_seq_afinfo in udp_iter_stateinclude/net/udp.h net/ipv4/udp.c+24 -50 replies
8/15net: bpf: implement bpf iterator for udpnet/ipv4/udp.c+116 -00 replies
9/15bpf: add bpf_skc_to_udp6_sock() helperinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/trace/bpf_trace.c +3 more+43 -20 replies
10/15selftests/bpf: move newer bpf_iter_* type redefining to a new header filetools/testing/selftests/bpf/progs/bpf_iter.h tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c tools/testing/selftests/bpf/progs/bpf_iter_ipv6_route.c +6 more+57 -1360 replies
11/15selftests/bpf: refactor some net macros to bpf_tracing_net.htools/testing/selftests/bpf/progs/bpf_iter_ipv6_route.c tools/testing/selftests/bpf/progs/bpf_iter_netlink.c tools/testing/selftests/bpf/progs/bpf_tracing_net.h+18 -90 replies
12/15selftests/bpf: add more common macros to bpf_tracing_net.htools/testing/selftests/bpf/progs/bpf_tracing_net.h+35 -00 replies
13/15selftests/bpf: implement sample tcp/tcp6 bpf_iter programstools/testing/selftests/bpf/progs/bpf_iter.h tools/testing/selftests/bpf/progs/bpf_iter_tcp4.c tools/testing/selftests/bpf/progs/bpf_iter_tcp6.c+499 -00 replies
14/15selftests/bpf: implement sample udp/udp6 bpf_iter programstools/testing/selftests/bpf/progs/bpf_iter.h tools/testing/selftests/bpf/progs/bpf_iter_udp4.c tools/testing/selftests/bpf/progs/bpf_iter_udp6.c+166 -00 replies
15/15selftests/bpf: add tcp/udp iterator programs to selfteststools/testing/selftests/bpf/prog_tests/bpf_iter.c+68 -00 replies

Reviews trailers per patch

All 15 patches have a Reviewed-by or Acked-by.

PatchMartin KaFai LauAndrii Nakryiko
1/15A-b
2/15A-b
3/15A-b
4/15A-b
5/15A-b A-b
6/15A-b
7/15A-b
8/15A-b
9/15A-b
10/15A-b A-b
11/15A-b A-b
12/15A-b A-b
13/15A-b A-b
14/15A-b A-b
15/15A-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v5 thread

Cover letter [PATCH bpf-next v5 00/15] implement bpf iterator for tcp and udp sockets

bpf iterator implments traversal of kernel data structures and these data structures are passed to a bpf program for processing. This gives great flexibility for users to examine kernel data structure without using e.g. /proc/net which has limited and fixed format.

Commit 138d0be35b14 ("net: bpf: Add netlink and ipv6_route bpf_iter targets") implemented bpf iterators for netlink and ipv6_route.
This patch set intends to implement bpf iterators for tcp and udp.

Currently, /proc/net/tcp is used to print tcp4 stats and /proc/net/tcp6 is used to print tcp6 stats. /proc/net/udp[6] have similar usage model. In contrast, only one tcp iterator is implemented and it is bpf program resposibility to filter based on socket family. The same is for udp. This will avoid another unnecessary traversal pass if users want to check both tcp4 and tcp6.

Several helpers are also implemented in this patch
bpf_skc_to_{tcp, tcp6, tcp_timewait, tcp_request, udp6}_sock
The argument for these helpers is not a fixed btf_id. For example,
bpf_skc_to_tcp(struct sock_common *), or bpf_skc_to_tcp(struct sock *), or
bpf_skc_to_tcp(struct inet_sock *), ...
are all valid. At runtime, the helper will check whether pointer cast is legal or not. Please see Patch #5 for details.

Since btf_id's for both arguments and return value are known at build time, the btf_id's are pre-computed once vmlinux btf becomes valid. Jiri's "adding d_path helper" patch set
https://lore.kernel.org/bpf/[email protected]/T/
provides a way to pre-compute btf id during vmlinux build time. This can be applied here as well. A followup patch can convert to build time btf id computation after Jiri's patch landed.

Changelogs:
v4 -> v5:
- fix bpf_skc_to_udp6_sock helper as besides sk_protocol, sk_family, sk_type == SOCK_DGRAM is also needed to differentiate from SOCK_RAW (Eric)
v3 -> v4:
- fix bpf_skc_to_{tcp_timewait, tcp_request}_sock helper implementation as just checking sk->sk_state is not enough (Martin)
- fix a few kernel test robot reported failures
- move bpf_tracing_net.h from libbpf to selftests (Andrii)
- remove __weak attribute from selftests CONFIG_HZ variables (Andrii)
v2 -> v3:
- change sock_cast*/SOCK_CAST* names to btf_sock* names for generality (Martin)
- change gpl_license to false (Martin)
- fix helper to cast to tcp timewait/request socket. (Martin)
v1 -> v2:
- guard init_sock_cast_types() defination properly with CONFIG_NET (Martin)
- reuse the btf_ids, computed for new helper argument, for return values (Martin)
- using BTF_TYPE_EMIT to express intent of btf type generation (Andrii)
- abstract out common net macros into bpf_tracing_net.h (Andrii)

Yonghong Song (15):
net: bpf: add bpf_seq_afinfo in tcp_iter_state
net: bpf: implement bpf iterator for tcp
bpf: support 'X' in bpf_seq_printf() helper
bpf: allow tracing programs to use bpf_jiffies64() helper
bpf: add bpf_skc_to_tcp6_sock() helper
bpf: add bpf_skc_to_{tcp,tcp_timewait,tcp_request}_sock() helpers
net: bpf: add bpf_seq_afinfo in udp_iter_state
net: bpf: implement bpf iterator for udp
bpf: add bpf_skc_to_udp6_sock() helper
selftests/bpf: move newer bpf_iter_* type redefining to a new header
file
selftests/bpf: refactor some net macros to bpf_tracing_net.h selftests/bpf: add more common macros to bpf_tracing_net.h selftests/bpf: implement sample tcp/tcp6 bpf_iter programs selftests/bpf: implement sample udp/udp6 bpf_iter programs selftests/bpf: add tcp/udp iterator programs to selftests

 include/linux/bpf.h                           |  16 ++
 include/net/tcp.h                             |   1 +
 include/net/udp.h                             |   1 +
 include/uapi/linux/bpf.h                      |  37 ++-
 kernel/bpf/btf.c                              |   1 +
 kernel/bpf/verifier.c                         |  43 ++-
 kernel/trace/bpf_trace.c                      |  15 +-
 net/core/filter.c                             | 166 ++++++++++++
 net/ipv4/tcp_ipv4.c                           | 153 ++++++++++-
 net/ipv4/udp.c                                | 144 +++++++++-
 scripts/bpf_helpers_doc.py                    |  10 +
 tools/include/uapi/linux/bpf.h                |  37 ++-
 .../selftests/bpf/prog_tests/bpf_iter.c       |  68 +++++
 tools/testing/selftests/bpf/progs/bpf_iter.h  |  80 ++++++
 .../selftests/bpf/progs/bpf_iter_bpf_map.c    |  18 +-
 .../selftests/bpf/progs/bpf_iter_ipv6_route.c |  25 +-
 .../selftests/bpf/progs/bpf_iter_netlink.c    |  22 +-
 .../selftests/bpf/progs/bpf_iter_task.c       |  18 +-
 .../selftests/bpf/progs/bpf_iter_task_file.c  |  20 +-
 .../selftests/bpf/progs/bpf_iter_tcp4.c       | 234 ++++++++++++++++
 .../selftests/bpf/progs/bpf_iter_tcp6.c       | 250 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_iter_test_kern3.c |  17 +-
 .../selftests/bpf/progs/bpf_iter_test_kern4.c |  17 +-
 .../bpf/progs/bpf_iter_test_kern_common.h     |  18 +-
 .../selftests/bpf/progs/bpf_iter_udp4.c       |  71 +++++
 .../selftests/bpf/progs/bpf_iter_udp6.c       |  79 ++++++
 .../selftests/bpf/progs/bpf_tracing_net.h     |  51 ++++
 27 files changed, 1443 insertions(+), 169 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter.h
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_tcp4.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_tcp6.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_udp4.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_udp6.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_tracing_net.h
Signature · 1 line
2.24.1