Skip to content

bpf: Add bpf_tcp_ops hooks for TCP AutoLOWAT.

Kuniyuki Iwashima bpf netdev 5 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest10 patches 4 repliesApplied 10/10
  2. v410 patches 23 repliesSuperseded
  3. v39 patches 9 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v28 patches16 repliesSuperseded
v17 patches13 repliesSuperseded

Changes in v5 from the cover letter

* Patch 4 & 5
* Always inline bpf_tcp_ops_hdr_opt_len() and bpf_skops_hdr_opt_len() to tcp_established_options().

Patches in v5 All 10 applied

#Subject and filesSizeRepliesApplied
1/10bpf: tcp: Convert deny-list for bpf_{get,set}sockopt() to allow-list.net/ipv4/bpf_tcp_ops.c+24 -150 repliesApplied
2/10bpf: tcp: Add a new per-socket flag and kfunc for bpf_tcp_ops.Documentation/networking/net_cachelines/tcp_sock.rst include/linux/tcp.h include/net/tcp.h +4 more+127 -20 repliesApplied
3/10selftest: bpf: Use bpf_tcp_ops_set_flags() in bpf_tcp_ops_hdr.c.tools/testing/selftests/bpf/progs/bpf_tcp_ops_hdr.c+20 -00 repliesApplied
4/10bpf: tcp: Guard fast-path bpf_tcp_ops_call() under per-socket flag.include/net/tcp.h net/ipv4/tcp_input.c net/ipv4/tcp_output.c+74 -371 replyApplied
5/10bpf: tcp: Check BPF_SOCK_OPS_TEST_FLAG() after cgroup_bpf_enabled(CGROUP_SOCK_OPS).include/linux/bpf-cgroup.h include/net/tcp.h net/ipv4/af_inet.c +5 more+70 -922 repliesApplied
6/10bpf: tcp: Introduce bpf_tcp_ops.{enqueue,dequeue}_rcvq().Documentation/networking/net_cachelines/tcp_sock.rst include/net/tcp.h include/uapi/linux/bpf.h +5 more+37 -30 repliesApplied
7/10tcp: Split out __tcp_set_rcvlowat().include/net/tcp.h net/ipv4/tcp.c+10 -30 repliesApplied
8/10bpf: mptcp: Don't support BPF_TCP_OPS_FLAG_RCVQ.net/ipv4/bpf_tcp_ops.c+3 -00 repliesApplied
9/10bpf: tcp: Add kfunc to adjust sk->sk_rcvlowat.net/ipv4/bpf_tcp_ops.c+46 -00 repliesApplied
10/10selftest: bpf: Add test for bpf_tcp_ops.{enqueue,dequeue}_rcvq().tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c tools/testing/selftests/bpf/progs/bpf_tracing_net.h tools/testing/selftests/bpf/progs/tcp_autolowat.c+646 -01 replyApplied

Reviews trailers per patch

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

PatchEmil TsalapatisStanislav FomichevAmery HungClément Léger
1/10R-b A-b R-b
2/10A-b R-b
3/10A-b R-b
4/10A-b
5/10A-b R-b
6/10A-b R-b
7/10R-b A-b R-b
8/10A-b R-b
9/10R-b A-b R-b T-b
10/10A-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v5 thread

Cover letter [PATCH v5 bpf-next 00/10] bpf: Add bpf_tcp_ops hooks for TCP AutoLOWAT.

This series introduces two callbacks for bpf_tcp_ops:

.enqueue_rcvq(): invoked when TCP stack enqueues skb to

                   sk->sk_receive_queue

.dequeue_rcvq(): invoked in tcp_cleanup_rbuf() after data

                   is dequeued from sk->sk_receive_queue

Those callbacks can be enabled on a per-socket basis by a new kfunc bpf_tcp_ops_set_flags():

bpf_tcp_ops_set_flags((struct tcp_sock *)sk,

                        BPF_TCP_OPS_FLAG_RCVQ, 0);

This allows the BPF prog to dynamically adjust sk->sk_rcvlowat, suppressing unnecessary EPOLLIN wakeups until sufficient data is available in the receive queue.

This functionality, which we call "TCP AutoLOWAT", was originally developed in 2020 by Tenzin Ukyab with the help of Soheil Hassas Yeganeh, Arjun Roy, and Eric Dumazet. It has served Google RPC workloads for more than 5 years.

Combined with TCP RX zerocopy, this typically allows us to read an entire RPC frame with just a single wakeup and a single system call.

While the original implementation was specialised for our internal RPC format, this series introduces a more flexible version by leveraging BPF.

The bpf prog in the last selftest patch closely mirrors the core logic of the original implementation to provide a real-world example.

Note that the new callbacks are not supported on legacy SOCK_OPS.

Changes:
v5:
* Patch 4 & 5
* Always inline bpf_tcp_ops_hdr_opt_len() and bpf_skops_hdr_opt_len() to tcp_established_options().

v4: https://lore.kernel.org/bpf/[email protected]/
* Patch 2
* Allow-listed BPF_PROG_TYPE_CGROUP_SOCKOPT and _SKB
in bpf_tcp_ops_set_flags_kfunc_filter()
* Add doc for flag enum and bpf_tcp_ops members
* Patch 4
* Check static key first and then flags
* Patch 5 (new)
* Bubble up static key check for SOCK_OPS similar to bpf_tcp_ops
* Patch 6
* Use bpf_tcp_ops_call_flag()

v3: https://lore.kernel.org/bpf/[email protected]/
* Add patch 2 ~ 4 (guard bpf_tcp_ops w/ a new flag)
* Patch 5 & 9 : Adapt to a new flag and kfunc

v2: https://lore.kernel.org/netdev/[email protected]/
* Add patch 1 not to allow setsockopt() from new callbacks
* Patch 8 (selftest)
* Avoid address comparison for a specific version of gcc.
* Make rpc_test_cases[] static.
* Update comment in rpc_test_case[].

v1: https://lore.kernel.org/bpf/[email protected]/

Legacy SOCK_OPS version:
v3: https://lore.kernel.org/bpf/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/

Kuniyuki Iwashima (10):
bpf: tcp: Convert deny-list for bpf_{get,set}sockopt() to allow-list.
bpf: tcp: Add a new per-socket flag and kfunc for bpf_tcp_ops.
selftest: bpf: Use bpf_tcp_ops_set_flags() in bpf_tcp_ops_hdr.c.
bpf: tcp: Guard fast-path bpf_tcp_ops_call() under per-socket flag.
bpf: tcp: Check BPF_SOCK_OPS_TEST_FLAG() after
cgroup_bpf_enabled(CGROUP_SOCK_OPS).
bpf: tcp: Introduce bpf_tcp_ops.{enqueue,dequeue}_rcvq().
tcp: Split out __tcp_set_rcvlowat().
bpf: mptcp: Don't support BPF_TCP_OPS_FLAG_RCVQ.
bpf: tcp: Add kfunc to adjust sk->sk_rcvlowat.
selftest: bpf: Add test for bpf_tcp_ops.{enqueue,dequeue}_rcvq().

 .../networking/net_cachelines/tcp_sock.rst    |   1 +
 include/linux/bpf-cgroup.h                    |  36 +-
 include/linux/tcp.h                           |   7 +
 include/net/tcp.h                             | 122 +++---
 include/uapi/linux/bpf.h                      |  25 ++
 net/ipv4/af_inet.c                            |   2 +-
 net/ipv4/bpf_tcp_ops.c                        | 159 +++++++-
 net/ipv4/tcp.c                                |  20 +-
 net/ipv4/tcp_fastopen.c                       |   2 +
 net/ipv4/tcp_input.c                          |  37 +-
 net/ipv4/tcp_nv.c                             |   2 +-
 net/ipv4/tcp_output.c                         |  89 ++---
 net/ipv4/tcp_timer.c                          |   6 +-
 tools/include/uapi/linux/bpf.h                |  25 ++
 .../selftests/bpf/prog_tests/tcp_autolowat.c  | 350 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_tcp_ops_hdr.c     |  20 +
 .../selftests/bpf/progs/bpf_tracing_net.h     |   2 +
 .../selftests/bpf/progs/tcp_autolowat.c       | 294 +++++++++++++++
 18 files changed, 1052 insertions(+), 147 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c
 create mode 100644 tools/testing/selftests/bpf/progs/tcp_autolowat.c
Signature · 1 line
2.56.0.360.g66cac248cb-goog