[PATCH bpf-next v10 0/2] bpf: tcp: add ecn_init callback to bpf_tcp_ops

From: Irlanki Sandeep

Date: Fri Nov 27 2026 - 08:23:18 EST


Hi,

This series adds an 'ecn_init' callback to struct bpf_tcp_ops. A
cgroup-attached BPF program can use it to choose the ECN mode (off,
RFC3168, or AccECN) for each connection during the handshake. The
net.ipv4.tcp_ecn sysctl stays the default: the program sees it through
bpf_get_retval(), and out-of-range return values fall back to it.

v9 -> v10:
- Split the selftests into a separate patch (2/2).
- Skip the ecn_init BPF call in tcp_ecn_create_request() for SYNs that
neither request AccECN nor carry ECE+CWR (Kuniyuki Iwashima).
- Sort variable declarations in reverse xmas tree order inside
tcp_ecn_send_syn() and tcp_ecn_create_request() (Kuniyuki Iwashima).

v8 -> v9:
- Fix multi-line comment style in
selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c (sashiko).
- Add copyright headers to both new selftest files.
- Update the bpf_get_retval() comment in net/ipv4/bpf_tcp_ops.c to
include ecn_init in the list of bpf_get_retval() users (BPF CI).

v7 -> v8:
- Respin on top of bpf-next and convert from the legacy sock_ops
kfuncs to a bpf_tcp_ops struct_ops callback 'ecn_init' (Alexei
Starovoitov).
- Drop tp->ecn_mode, TCP_ECN_MODE_UNSPEC, tcp_ecn_mode_eff() and the
kfuncs; the prog is queried on demand during the handshake like
timeout_init()/rwnd_init(), which also enables per-peer decisions
on the passive side (Alexei Starovoitov).
- Clarify the per-application motivation (AccECN for latency-sensitive
apps vs bulk traffic on the same route) in the commit log (Alexei
Starovoitov).
- Drop the AccECN option kfunc to focus on ECN mode negotiation;
to be discussed separately.
- Replace the sockops selftest with a bpf_tcp_ops_ecn struct_ops
selftest covering IPv4 and IPv6.

v6 -> v7:
- Document BPF_SOCK_OPS_TCP_LISTEN_CB timing window in kernel-doc
for both kfuncs: connections arriving before the callback runs
will use the sysctl value rather than per-socket overrides.
- Add fallback behavior note to commit message.
- Remove Suggested-by tag per maintainer guidance.

v5 -> v6:
- Reject non-TCP sockets in both kfuncs with sk_is_tcp(). The TX
timestamping sockops callbacks run with is_fullsock set for UDP
sockets too, so tcp_sk() on them was an out-of-bounds write (sashiko).
- Fix selftest and comments: accepted children are cloned from the
listener and inherit ecn_mode/ecn_option (sashiko).
- Use standard multi-line comment style in the BPF selftests (sashiko).
- Document per-socket kfunc override precedence for tcp_ecn and
tcp_ecn_option in ip-sysctl.rst.
- Reword subject: the TCP_ECN/TCP_ECN_OPTION optnames no longer exist.

v4 -> v5:
- Replaced setsockopt implementation with BPF kfuncs.
- Placed ecn_mode/ecn_option in the cold section of tcp_sock to avoid
polluting hot cachelines (suggested by Eric Dumazet).
- Updated Documentation/networking/net_cachelines/tcp_sock.rst.
- Removed userspace UAPI and documentation for the sockopt.
- Added new selftest for the kfunc implementation.

v3 -> v4:
- Define TCP_ECN and TCP_ECN_OPTION in bpf_tracing_net.h to fix
BPF selftest compilation failure in progs/setget_sockopt.c (bpf-ci).

v2 -> v3:
- Use READ_ONCE() when reading ecn_mode and ecn_option in
tcp_ecn_mode_eff() and tcp_accecn_option_eff() to prevent compiler
double-fetches (sashiko-bot).
- Move cookie_ecn_ok() to include/net/tcp_ecn.h and update syncookies.

v1 -> v2:
- Remove unused variable 'net' in tcp_ecn_create_request() reported
by kernel test robot.

Irlanki Sandeep (2):
bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN
selftests/bpf: add selftests for bpf_tcp_ops ecn_init

Documentation/networking/ip-sysctl.rst | 8 +
include/net/tcp.h | 6 +
include/net/tcp_ecn.h | 16 +-
net/ipv4/bpf_tcp_ops.c | 21 +-
net/ipv4/tcp_input.c | 11 +-
.../bpf/prog_tests/bpf_tcp_ops_ecn.c | 224 ++++++++++++++++++
.../selftests/bpf/progs/bpf_tcp_ops_ecn.c | 57 +++++
7 files changed, 333 insertions(+), 10 deletions(-)
create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c
create mode 100644 tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c

--
2.34.1