BPF List
 help / color / mirror / Atom feed
* [PATCH net-next v7] tcp: add BPF kfuncs to set per-socket ECN mode and AccECN option
       [not found] <CGME20261016100913epcas5p13a41d34ac5d203bff1d220b3d19dee67@epcas5p1.samsung.com>
@ 2026-10-16 10:10 ` Irlanki Sandeep
  2026-10-16 18:03   ` Alexei Starovoitov
  0 siblings, 1 reply; 6+ messages in thread
From: Irlanki Sandeep @ 2026-10-16 10:10 UTC (permalink / raw)
  To: netdev, bpf, kuba
  Cc: edumazet, davem, pabeni, ncardwell, ast, daniel, andrii, corbet,
	linux-kernel, lorenzo, maze, sporeba, motomuman, madhan.raj,
	srihari.k, g.pokhra, r.kumawat, raj.kumars, ts413.lee,
	ramyashree.s, daeil2.hwang, irlanki.s

Currently, ECN and Accurate ECN (AccECN) configurations in the Linux
kernel are controlled globally via the net.ipv4.tcp_ecn and
net.ipv4.tcp_ecn_option sysctl variables. This global approach lacks the
granularity required for per-connection ECN configuration in modern
multi-application runtimes.

In diverse network environments, such as mobile operating systems like
Android, different applications route their traffic over distinct
physical and virtual interfaces. Certain legacy network paths or
misconfigured middleboxes are known to blackhole packets with ECN
negotiation flags or AccECN option headers.

To provide granular control, introduce two new BPF kfuncs that can be
called from sockops programs:

1. bpf_sock_ops_set_ecn_mode(): Overrides the ECN negotiation mode for
   a socket.

2. bpf_sock_ops_set_accecn_option(): Overrides the AccECN option
   sending frequency for a socket, allowing for dynamic runtime
   adaptation if middlebox dropping is observed.

New helpers tcp_ecn_mode_eff() and tcp_accecn_option_eff() resolve the
effective per-socket value, transparently falling back to the global
sysctl defaults when no per-socket override is set.

The ECN mode kfunc is restricted to the CONNECT_CB and LISTEN_CB
callbacks since the mode is only consulted during connection
establishment, while the AccECN option kfunc may be called from any
callback on a full TCP socket to allow runtime adaptation. Both kfuncs
reject non-TCP sockets, as sockops programs also run for the TX
timestamping callbacks of other socket types.

Values set on a listener socket are consulted for each incoming handshake,
including SYN cookie validation, and, like other per-socket settings, are
inherited by the accepted child sockets. Note that a listener is already
hashed before the LISTEN_CB callback runs, so connections accepted in that
window follow the sysctl defaults: for the AccECN option this is transient,
as the program can update the value at runtime, while the ECN mode decision
made at SYN time is final for the connection.

Signed-off-by: Irlanki Sandeep <irlanki.s@samsung.com>
---
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.
---
 Documentation/networking/ip-sysctl.rst        |   6 +
 .../networking/net_cachelines/tcp_sock.rst    |   2 +
 include/linux/tcp.h                           |   6 +
 include/net/tcp.h                             |   6 -
 include/net/tcp_ecn.h                         |  31 ++++-
 net/core/filter.c                             |  75 ++++++++++++
 net/ipv4/syncookies.c                         |   2 +-
 net/ipv4/tcp.c                                |   2 +
 net/ipv4/tcp_input.c                          |   5 +-
 net/ipv4/tcp_output.c                         |   6 +-
 net/ipv6/syncookies.c                         |   2 +-
 .../bpf/prog_tests/sockops_ecn_kfunc.c        |  91 +++++++++++++++
 .../selftests/bpf/progs/sockops_ecn_kfunc.c   | 110 ++++++++++++++++++
 13 files changed, 329 insertions(+), 15 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockops_ecn_kfunc.c
 create mode 100644 tools/testing/selftests/bpf/progs/sockops_ecn_kfunc.c

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index f7af0286341c..0dc5d1d09f6a 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -469,6 +469,9 @@ tcp_ecn - INTEGER
 	that both peers support is chosen by the ECN negotiation (Accurate ECN,
 	ECN, or no ECN).
 
+	Note that a per-socket mode set by a BPF sockops program via the
+	bpf_sock_ops_set_ecn_mode() kfunc has higher precedence.
+
 	The highest negotiated variant for incoming connection requests
 	and the highest variant requested by outgoing connection
 	attempts:
@@ -492,6 +495,9 @@ tcp_ecn_option - INTEGER
 	sending AccECN options regardless of this setting when no AccECN
 	option has been seen for the reverse direction.
 
+	Note that a per-socket value set by a BPF sockops program via the
+	bpf_sock_ops_set_accecn_option() kfunc has higher precedence.
+
 	Possible values are:
 
 	= ============================================================
diff --git a/Documentation/networking/net_cachelines/tcp_sock.rst b/Documentation/networking/net_cachelines/tcp_sock.rst
index 0f6088c4ab8b..b12cc823b601 100644
--- a/Documentation/networking/net_cachelines/tcp_sock.rst
+++ b/Documentation/networking/net_cachelines/tcp_sock.rst
@@ -114,6 +114,8 @@ u8:2                          accecn_opt_demand       read_mostly         read_w
 u8:2                          prev_ecnfield                               read_write
 u64                           accecn_opt_tstamp       read_write
 u8:4                          accecn_fail_mode
+u8                            ecn_mode
+u8                            ecn_option
 u32                           lost                                        read_mostly         tcp_ack
 u32                           app_limited             read_write          read_mostly         tcp_rate_check_app_limited,tcp_rate_skb_sent(tx);tcp_rate_gen(rx)
 u64                           first_tx_mstamp         read_write                              tcp_rate_skb_sent
diff --git a/include/linux/tcp.h b/include/linux/tcp.h
index 6a8c77719322..c9be37c01ac2 100644
--- a/include/linux/tcp.h
+++ b/include/linux/tcp.h
@@ -412,6 +412,12 @@ struct tcp_sock {
 	u8	keepalive_probes; /* num of allowed keep alive probes	*/
 	u8	accecn_fail_mode:4,	/* AccECN failure handling */
 		saw_accecn_opt:2;	/* An AccECN option was seen */
+	u8	ecn_mode;	/* Per-socket ECN mode set via BPF
+				 * (TCP_ECN_MODE_UNSPEC = use sysctl)
+				 */
+	u8	ecn_option;	/* Per-socket AccECN option set via BPF
+				 * (TCP_ACCECN_OPTION_UNSPEC = use sysctl)
+				 */
 	u32	tcp_tx_delay;	/* delay (in usec) added to TX packets */
 
 /* RTT measurement */
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 96a1a23e47fa..0aa74ac92058 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -707,12 +707,6 @@ u64 cookie_init_timestamp(struct request_sock *req, u64 now);
 bool cookie_timestamp_decode(const struct net *net,
 			     struct tcp_options_received *opt);
 
-static inline bool cookie_ecn_ok(const struct net *net, const struct dst_entry *dst)
-{
-	return READ_ONCE(net->ipv4.sysctl_tcp_ecn) ||
-		dst_feature(dst, RTAX_FEATURE_ECN);
-}
-
 #if IS_ENABLED(CONFIG_BPF)
 static inline bool cookie_bpf_ok(struct sk_buff *skb)
 {
diff --git a/include/net/tcp_ecn.h b/include/net/tcp_ecn.h
index 865d5c5a7718..48e364c458b2 100644
--- a/include/net/tcp_ecn.h
+++ b/include/net/tcp_ecn.h
@@ -22,6 +22,7 @@ enum tcp_ecn_mode {
 	TCP_ECN_IN_ACCECN_OUT_ACCECN = 3,
 	TCP_ECN_IN_ACCECN_OUT_ECN = 4,
 	TCP_ECN_IN_ACCECN_OUT_NOECN = 5,
+	TCP_ECN_MODE_UNSPEC = 255,	/* Use sysctl default (per-socket) */
 };
 
 /* AccECN option sending when AccECN has been successfully negotiated */
@@ -30,8 +31,36 @@ enum tcp_accecn_option {
 	TCP_ACCECN_OPTION_MINIMUM = 1,
 	TCP_ACCECN_OPTION_FULL = 2,
 	TCP_ACCECN_OPTION_PERSIST = 3,
+	TCP_ACCECN_OPTION_UNSPEC = 255,	/* Use sysctl default (per-socket) */
 };
 
+/* Resolve the effective ECN mode: per-socket override or sysctl fallback */
+static inline u8 tcp_ecn_mode_eff(const struct sock *sk)
+{
+	u8 mode = READ_ONCE(tcp_sk(sk)->ecn_mode);
+
+	if (mode == TCP_ECN_MODE_UNSPEC)
+		return READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	return mode;
+}
+
+/* Resolve the effective AccECN option: per-socket override or sysctl fallback */
+static inline u8 tcp_accecn_option_eff(const struct sock *sk)
+{
+	u8 opt = READ_ONCE(tcp_sk(sk)->ecn_option);
+
+	if (opt == TCP_ACCECN_OPTION_UNSPEC)
+		return READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option);
+	return opt;
+}
+
+/* ECN support for SYN cookies: per-socket override or route feature */
+static inline bool cookie_ecn_ok(const struct sock *sk, const struct dst_entry *dst)
+{
+	return tcp_ecn_mode_eff(sk) ||
+		dst_feature(dst, RTAX_FEATURE_ECN);
+}
+
 /* Apply either ECT(0) or ECT(1) based on TCP_CONG_ECT_1_NEGOTIATION flag */
 static inline void INET_ECN_xmit_ect_1_negotiation(struct sock *sk)
 {
@@ -599,7 +628,7 @@ static inline void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
 	struct tcp_sock *tp = tcp_sk(sk);
 	bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
 	bool use_ecn, use_accecn;
-	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	u8 tcp_ecn = tcp_ecn_mode_eff(sk);
 
 	use_accecn = tcp_ecn == TCP_ECN_IN_ACCECN_OUT_ACCECN ||
 		     tcp_ca_needs_accecn(sk);
diff --git a/net/core/filter.c b/net/core/filter.c
index 70dc621672f2..9dbbccd4e0ea 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -56,6 +56,7 @@
 #include <net/sock_reuseport.h>
 #include <net/busy_poll.h>
 #include <net/tcp.h>
+#include <net/tcp_ecn.h>
 #include <net/gre.h>
 #include <net/xfrm.h>
 #include <net/udp.h>
@@ -12609,6 +12610,78 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
 	return 0;
 }
 
+/**
+ * bpf_sock_ops_set_ecn_mode() - Set the per-socket TCP ECN negotiation mode.
+ * @skops: &bpf_sock_ops_kern context of the sockops program
+ * @mode: value in the range accepted by the tcp_ecn sysctl (0-5), or
+ *	TCP_ECN_MODE_UNSPEC to defer to the sysctl
+ *
+ * Overrides net.ipv4.tcp_ecn for this socket only. The mode is consulted
+ * during connection establishment, so the kfunc may only be called from
+ * BPF_SOCK_OPS_TCP_CONNECT_CB (before the SYN is built) or from
+ * BPF_SOCK_OPS_TCP_LISTEN_CB, in which case the mode is consulted for each
+ * incoming connection to decide ECN negotiation during the handshake.
+ *
+ * Note that the listener is already hashed before BPF_SOCK_OPS_TCP_LISTEN_CB
+ * runs, so connections arriving in that window negotiate ECN using the
+ * sysctl value rather than the per-socket override, since ECN negotiation
+ * happens at SYN time and cannot be changed afterwards.
+ *
+ * Return: 0 on success, -EOPNOTSUPP when called from any other sockops
+ *	callback or if @skops is not a full TCP socket, -EINVAL if @mode is
+ *	out of range.
+ */
+__bpf_kfunc int bpf_sock_ops_set_ecn_mode(struct bpf_sock_ops_kern *skops,
+					  u32 mode)
+{
+	if (skops->op != BPF_SOCK_OPS_TCP_CONNECT_CB &&
+	    skops->op != BPF_SOCK_OPS_TCP_LISTEN_CB)
+		return -EOPNOTSUPP;
+
+	if (!skops->is_fullsock || !sk_is_tcp(skops->sk))
+		return -EOPNOTSUPP;
+
+	if (mode != TCP_ECN_MODE_UNSPEC && mode > TCP_ECN_IN_ACCECN_OUT_NOECN)
+		return -EINVAL;
+
+	WRITE_ONCE(tcp_sk(skops->sk)->ecn_mode, mode);
+	return 0;
+}
+
+/**
+ * bpf_sock_ops_set_accecn_option() - Set the per-socket AccECN option mode.
+ * @skops: &bpf_sock_ops_kern context of the sockops program
+ * @opt: value in the range accepted by the tcp_ecn_option sysctl (0-3), or
+ *	TCP_ACCECN_OPTION_UNSPEC to defer to the sysctl
+ *
+ * Overrides net.ipv4.tcp_ecn_option for this socket only. Unlike the ECN
+ * mode, the option setting is consulted for the whole lifetime of an AccECN
+ * connection, so it may be changed from any sockops callback that runs on a
+ * full TCP socket, e.g. to stop sending the option once a middlebox is
+ * observed to drop segments carrying it. Like other per-socket settings, a
+ * value set on a listener is inherited by the sockets it accepts.
+ *
+ * Note that the listener is already hashed before BPF_SOCK_OPS_TCP_LISTEN_CB
+ * runs, so connections accepted in that window follow the sysctl defaults
+ * until the sockops program updates the option at runtime, which it may do
+ * from any callback on the full socket.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if @skops is not a full TCP socket,
+ *	-EINVAL if @opt is out of range.
+ */
+__bpf_kfunc int bpf_sock_ops_set_accecn_option(struct bpf_sock_ops_kern *skops,
+					       u32 opt)
+{
+	if (!skops->is_fullsock || !sk_is_tcp(skops->sk))
+		return -EOPNOTSUPP;
+
+	if (opt != TCP_ACCECN_OPTION_UNSPEC && opt > TCP_ACCECN_OPTION_PERSIST)
+		return -EINVAL;
+
+	WRITE_ONCE(tcp_sk(skops->sk)->ecn_option, opt);
+	return 0;
+}
+
 /**
  * bpf_xdp_pull_data() - Pull in non-linear xdp data.
  * @x: &xdp_md associated with the XDP buffer
@@ -12822,6 +12895,8 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_tcp_reqsk)
 
 BTF_KFUNCS_START(bpf_kfunc_check_set_sock_ops)
 BTF_ID_FLAGS(func, bpf_sock_ops_enable_tx_tstamp)
+BTF_ID_FLAGS(func, bpf_sock_ops_set_ecn_mode)
+BTF_ID_FLAGS(func, bpf_sock_ops_set_accecn_option)
 BTF_KFUNCS_END(bpf_kfunc_check_set_sock_ops)
 
 BTF_KFUNCS_START(bpf_kfunc_check_set_icmp_send)
diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
index 73e129768184..ebc438bc6a46 100644
--- a/net/ipv4/syncookies.c
+++ b/net/ipv4/syncookies.c
@@ -490,7 +490,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
 	 */
 	if (!req->syncookie)
 		ireq->rcv_wscale = rcv_wscale;
-	ireq->ecn_ok &= cookie_ecn_ok(net, &rt->dst);
+	ireq->ecn_ok &= cookie_ecn_ok(sk, &rt->dst);
 	treq->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
 
 	ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst);
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 3ac485685279..0303f7c2ec18 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -463,6 +463,8 @@ void tcp_init_sock(struct sock *sk)
 
 	tp->tsoffset = 0;
 	tp->rack.reo_wnd_steps = 1;
+	tp->ecn_mode = TCP_ECN_MODE_UNSPEC;
+	tp->ecn_option = TCP_ACCECN_OPTION_UNSPEC;
 
 	sk->sk_write_space = sk_stream_write_space;
 	sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 892ff256e235..78603f68bc5a 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -7453,13 +7453,12 @@ static void tcp_ecn_create_request(struct request_sock *req,
 				   const struct dst_entry *dst)
 {
 	const struct tcphdr *th = tcp_hdr(skb);
-	const struct net *net = sock_net(listen_sk);
 	bool th_ecn = th->ece && th->cwr;
 	bool ect, ecn_ok;
 	u32 ecn_ok_dst;
 
 	if (tcp_accecn_syn_requested(th) &&
-	    (READ_ONCE(net->ipv4.sysctl_tcp_ecn) >= 3 ||
+	    (tcp_ecn_mode_eff(listen_sk) >= 3 ||
 	     tcp_ca_needs_accecn(listen_sk))) {
 		inet_rsk(req)->ecn_ok = 1;
 		tcp_rsk(req)->accecn_ok = 1;
@@ -7473,7 +7472,7 @@ static void tcp_ecn_create_request(struct request_sock *req,
 
 	ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
 	ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
-	ecn_ok = READ_ONCE(net->ipv4.sysctl_tcp_ecn) || ecn_ok_dst;
+	ecn_ok = tcp_ecn_mode_eff(listen_sk) || ecn_ok_dst;
 
 	if (((!ect || th->res1 || th->ae) && ecn_ok) ||
 	    tcp_ca_needs_ecn(listen_sk) ||
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index c8865205cdc8..8833578bbd84 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -1045,7 +1045,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
 	if (unlikely((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) &&
 		     tcp_ecn_mode_accecn(tp) &&
 		     inet_csk(sk)->icsk_retransmits < 2 &&
-		     READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option) &&
+		     tcp_accecn_option_eff(sk) &&
 		     remaining >= TCPOLEN_ACCECN_BASE)) {
 		opts->use_synack_ecn_bytes = 1;
 		remaining -= tcp_options_fit_accecn(opts, 0, remaining);
@@ -1133,7 +1133,7 @@ static unsigned int tcp_synack_options(const struct sock *sk,
 	smc_set_option_cond(tcp_sk(sk), ireq, opts, &remaining);
 
 	if (treq->accecn_ok &&
-	    READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option) &&
+	    tcp_accecn_option_eff(sk) &&
 	    synack_type != TCP_SYNACK_RETRANS && remaining >= TCPOLEN_ACCECN_BASE) {
 		opts->use_synack_ecn_bytes = 1;
 		remaining -= tcp_options_fit_accecn(opts, 0, remaining);
@@ -1221,7 +1221,7 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
 	}
 
 	if (tcp_ecn_mode_accecn(tp)) {
-		int ecn_opt = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option);
+		int ecn_opt = tcp_accecn_option_eff(sk);
 
 		if (ecn_opt && tp->saw_accecn_opt &&
 		    (ecn_opt >= TCP_ACCECN_OPTION_PERSIST ||
diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c
index b581cb1ee2e8..06efcbf46346 100644
--- a/net/ipv6/syncookies.c
+++ b/net/ipv6/syncookies.c
@@ -271,7 +271,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
 	 */
 	if (!req->syncookie)
 		ireq->rcv_wscale = rcv_wscale;
-	ireq->ecn_ok &= cookie_ecn_ok(net, dst);
+	ireq->ecn_ok &= cookie_ecn_ok(sk, dst);
 	tcp_rsk(req)->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
 
 	ret = tcp_get_cookie_sock(sk, skb, req, dst);
diff --git a/tools/testing/selftests/bpf/prog_tests/sockops_ecn_kfunc.c b/tools/testing/selftests/bpf/prog_tests/sockops_ecn_kfunc.c
new file mode 100644
index 000000000000..6c6f046a1dc5
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/sockops_ecn_kfunc.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+
+#include "test_progs.h"
+#include "cgroup_helpers.h"
+#include "network_helpers.h"
+
+#include "sockops_ecn_kfunc.skel.h"
+
+/* From include/net/tcp_ecn.h */
+#define TCP_ECN_IN_ACCECN_OUT_ACCECN	3
+#define TCP_ECN_IN_ACCECN_OUT_ECN	4
+#define TCP_ECN_MODE_UNSPEC		255
+#define TCP_ACCECN_OPTION_MINIMUM	1
+#define TCP_ACCECN_OPTION_FULL		2
+#define TCP_ACCECN_OPTION_PERSIST	3
+
+void test_sockops_ecn_kfunc(void)
+{
+	struct sockops_ecn_kfunc__bss *bss;
+	struct sockops_ecn_kfunc *skel;
+	int cg_fd, sfd = -1, cfd = -1, afd = -1;
+
+	cg_fd = test__join_cgroup("/sockops_ecn_kfunc");
+	if (!ASSERT_OK_FD(cg_fd, "join_cgroup"))
+		return;
+
+	skel = sockops_ecn_kfunc__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		goto out;
+	bss = skel->bss;
+
+	skel->links.skops_ecn =
+		bpf_program__attach_cgroup(skel->progs.skops_ecn, cg_fd);
+	if (!ASSERT_OK_PTR(skel->links.skops_ecn, "attach_cgroup"))
+		goto out;
+
+	sfd = start_server(AF_INET, SOCK_STREAM, "127.0.0.1", 0, 0);
+	if (!ASSERT_OK_FD(sfd, "start_server"))
+		goto out;
+
+	cfd = connect_to_fd(sfd, 0);
+	if (!ASSERT_OK_FD(cfd, "connect_to_fd"))
+		goto out;
+
+	/* accept() returning guarantees PASSIVE_ESTABLISHED_CB has run. */
+	afd = accept(sfd, NULL, NULL);
+	if (!ASSERT_OK_FD(afd, "accept"))
+		goto out;
+
+	/* BPF_SOCK_OPS_TCP_CONNECT_CB */
+	ASSERT_EQ(bss->connect_bad_mode_ret, -EINVAL, "connect_bad_mode_ret");
+	ASSERT_EQ(bss->connect_bad_opt_ret, -EINVAL, "connect_bad_opt_ret");
+	ASSERT_EQ(bss->connect_set_mode_ret, 0, "connect_set_mode_ret");
+	ASSERT_EQ(bss->connect_mode_read, TCP_ECN_IN_ACCECN_OUT_ACCECN,
+		  "connect_mode_read");
+	ASSERT_EQ(bss->connect_set_opt_ret, 0, "connect_set_opt_ret");
+	ASSERT_EQ(bss->connect_opt_read, TCP_ACCECN_OPTION_FULL,
+		  "connect_opt_read");
+	ASSERT_EQ(bss->connect_unspec_mode_ret, 0, "connect_unspec_mode_ret");
+	ASSERT_EQ(bss->connect_unspec_mode_read, TCP_ECN_MODE_UNSPEC,
+		  "connect_unspec_mode_read");
+
+	/* BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB */
+	ASSERT_EQ(bss->estab_set_mode_ret, -EOPNOTSUPP, "estab_set_mode_ret");
+	ASSERT_EQ(bss->estab_set_opt_ret, 0, "estab_set_opt_ret");
+	ASSERT_EQ(bss->estab_opt_read, TCP_ACCECN_OPTION_MINIMUM,
+		  "estab_opt_read");
+
+	/*
+	 * BPF_SOCK_OPS_TCP_LISTEN_CB -> BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB
+	 * The listener's values are consulted for SYN negotiation and, like
+	 * other per-socket settings, inherited by the accepted child.
+	 */
+	ASSERT_EQ(bss->listen_set_mode_ret, 0, "listen_set_mode_ret");
+	ASSERT_EQ(bss->listen_set_opt_ret, 0, "listen_set_opt_ret");
+	ASSERT_EQ(bss->passive_mode_read, TCP_ECN_IN_ACCECN_OUT_ECN,
+		  "passive_mode_read");
+	ASSERT_EQ(bss->passive_opt_read, TCP_ACCECN_OPTION_PERSIST,
+		  "passive_opt_read");
+
+out:
+	if (afd >= 0)
+		close(afd);
+	if (cfd >= 0)
+		close(cfd);
+	if (sfd >= 0)
+		close(sfd);
+	sockops_ecn_kfunc__destroy(skel);
+	close(cg_fd);
+}
diff --git a/tools/testing/selftests/bpf/progs/sockops_ecn_kfunc.c b/tools/testing/selftests/bpf/progs/sockops_ecn_kfunc.c
new file mode 100644
index 000000000000..17c874f66b9c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/sockops_ecn_kfunc.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_kfuncs.h"
+
+/* From include/net/tcp_ecn.h */
+#define TCP_ECN_IN_ACCECN_OUT_ACCECN	3
+#define TCP_ECN_IN_ACCECN_OUT_ECN	4
+#define TCP_ECN_IN_ACCECN_OUT_NOECN	5
+#define TCP_ECN_MODE_UNSPEC		255
+#define TCP_ACCECN_OPTION_MINIMUM	1
+#define TCP_ACCECN_OPTION_FULL		2
+#define TCP_ACCECN_OPTION_PERSIST	3
+#define TCP_ACCECN_OPTION_UNSPEC	255
+
+extern int bpf_sock_ops_set_ecn_mode(struct bpf_sock_ops_kern *skops,
+				     u32 mode) __ksym;
+extern int bpf_sock_ops_set_accecn_option(struct bpf_sock_ops_kern *skops,
+					  u32 opt) __ksym;
+
+int connect_bad_mode_ret;
+int connect_bad_opt_ret;
+int connect_set_mode_ret;
+int connect_mode_read;
+int connect_set_opt_ret;
+int connect_opt_read;
+int connect_unspec_mode_ret;
+int connect_unspec_mode_read;
+int estab_set_mode_ret;
+int estab_set_opt_ret;
+int estab_opt_read;
+int listen_set_mode_ret;
+int listen_set_opt_ret;
+int passive_mode_read;
+int passive_opt_read;
+
+SEC("sockops")
+int skops_ecn(struct bpf_sock_ops *skops)
+{
+	struct bpf_sock_ops_kern *skops_kern;
+	struct bpf_sock *sk = skops->sk;
+	struct tcp_sock *tp;
+
+	if (!sk)
+		return 1;
+
+	tp = bpf_skc_to_tcp_sock(sk);
+	if (!tp)
+		return 1;
+
+	skops_kern = bpf_cast_to_kern_ctx(skops);
+
+	switch (skops->op) {
+	case BPF_SOCK_OPS_TCP_CONNECT_CB:
+		connect_bad_mode_ret =
+			bpf_sock_ops_set_ecn_mode(skops_kern,
+						  TCP_ECN_IN_ACCECN_OUT_NOECN + 1);
+		connect_bad_opt_ret =
+			bpf_sock_ops_set_accecn_option(skops_kern,
+						       TCP_ACCECN_OPTION_PERSIST + 1);
+
+		connect_set_mode_ret =
+			bpf_sock_ops_set_ecn_mode(skops_kern,
+						  TCP_ECN_IN_ACCECN_OUT_ACCECN);
+		connect_mode_read = tp->ecn_mode;
+
+		connect_set_opt_ret =
+			bpf_sock_ops_set_accecn_option(skops_kern,
+						       TCP_ACCECN_OPTION_FULL);
+		connect_opt_read = tp->ecn_option;
+
+		/*
+		 * Leave the socket on the sysctl defaults so the loopback
+		 * handshake driven by the test is not altered.
+		 */
+		connect_unspec_mode_ret =
+			bpf_sock_ops_set_ecn_mode(skops_kern, TCP_ECN_MODE_UNSPEC);
+		connect_unspec_mode_read = tp->ecn_mode;
+		bpf_sock_ops_set_accecn_option(skops_kern,
+					       TCP_ACCECN_OPTION_UNSPEC);
+		break;
+	case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
+		/* The mode is handshake-only; the option is not. */
+		estab_set_mode_ret =
+			bpf_sock_ops_set_ecn_mode(skops_kern,
+						  TCP_ECN_IN_ACCECN_OUT_ACCECN);
+		estab_set_opt_ret =
+			bpf_sock_ops_set_accecn_option(skops_kern,
+						       TCP_ACCECN_OPTION_MINIMUM);
+		estab_opt_read = tp->ecn_option;
+		break;
+	case BPF_SOCK_OPS_TCP_LISTEN_CB:
+		listen_set_mode_ret =
+			bpf_sock_ops_set_ecn_mode(skops_kern,
+						  TCP_ECN_IN_ACCECN_OUT_ECN);
+		listen_set_opt_ret =
+			bpf_sock_ops_set_accecn_option(skops_kern,
+						       TCP_ACCECN_OPTION_PERSIST);
+		break;
+	case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
+		/* The accepted child inherits the values set on the listener. */
+		passive_mode_read = tp->ecn_mode;
+		passive_opt_read = tp->ecn_option;
+		break;
+	}
+
+	return 1;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.34.1


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

* Re: [PATCH net-next v7] tcp: add BPF kfuncs to set per-socket ECN mode and AccECN option
  2026-10-16 10:10 ` [PATCH net-next v7] tcp: add BPF kfuncs to set per-socket ECN mode and AccECN option Irlanki Sandeep
@ 2026-10-16 18:03   ` Alexei Starovoitov
  2026-11-16 13:46     ` [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN Irlanki Sandeep
  2026-11-16 15:13     ` [PATCH bpf-next v9] " Irlanki Sandeep
  0 siblings, 2 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-10-16 18:03 UTC (permalink / raw)
  To: Irlanki Sandeep, netdev, bpf, kuba
  Cc: edumazet, davem, pabeni, ncardwell, daniel, andrii, corbet,
	linux-kernel, lorenzo, maze, sporeba, motomuman, madhan.raj,
	srihari.k, g.pokhra, r.kumawat, raj.kumars, ts413.lee,
	ramyashree.s, daeil2.hwang

On Fri, Oct 16, 2026 at 03:40 PM Irlanki Sandeep <irlanki.s@samsung.com> wrote:
> To provide granular control, introduce two new BPF kfuncs that can be
> called from sockops programs:

sockops is legacy. New tcp hooks go into bpf_tcp_ops. See
commit 5ac77ae32940 ("bpf: tcp: Support selected sock_ops callbacks as struct_ops")
kfuncs that take bpf_sock_ops_kern cannot be called from there.
It's in bpf-next only, so pls respin on top of bpf-next and
cc Amery, Kuniyuki.

> The ECN mode kfunc is restricted to the CONNECT_CB and LISTEN_CB
> callbacks since the mode is only consulted during connection
> establishment, while the AccECN option kfunc may be called from any

[...]

> inherited by the accepted child sockets. Note that a listener is already
> hashed before the LISTEN_CB callback runs, so connections accepted in that
> window follow the sysctl defaults: for the AccECN option this is transient,

The mode is read once per handshake. No need to store it in tcp_sock.
Ask the prog at the time the sysctl is read, like timeout_init() and
rwnd_init() do. On the passive side the prog gets the listener and
the req, so it can decide per peer or per incoming interface.
One byte in the listener cannot do that.
Then no need for tp->ecn_mode, TCP_ECN_MODE_UNSPEC, tcp_ecn_mode_eff(),
bpf_sock_ops_set_ecn_mode() with its op check, and the LISTEN_CB window
is gone.
With syncookies rsk_listener is NULL and the sysctl is used.
rwnd_init() has the same limitation.
Something like this (compile tested only):

diff --git a/include/net/tcp.h b/include/net/tcp.h
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2971,6 +2971,12 @@ struct bpf_tcp_ops {
 	 */
 	int (*rwnd_init)(struct sock *sk, struct request_sock *req);
 
+	/* Should return the ECN variant to negotiate in this handshake:
+	 * one of net.ipv4.tcp_ecn values. bpf_get_retval() has the sysctl.
+	 * @req as in timeout_init().
+	 */
+	int (*ecn_init)(struct sock *sk, struct request_sock *req);
+
 	/* Called when an active connection becomes established.
 	 * @skb is the SYNACK that completed the 3WHS, or NULL for a
 	 * TCP_REPAIR socket (tcp_finish_connect() with no skb).
diff --git a/include/net/tcp_ecn.h b/include/net/tcp_ecn.h
--- a/include/net/tcp_ecn.h
+++ b/include/net/tcp_ecn.h
@@ -32,6 +32,17 @@ enum tcp_accecn_option {
 	TCP_ACCECN_OPTION_PERSIST = 3,
 };
 
+static inline u8 tcp_ecn_init_bpf(struct sock *sk)
+{
+	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	int ret;
+
+	ret = bpf_tcp_ops_call_int(ecn_init, tcp_ecn, sk);
+	if (ret < 0 || ret > TCP_ECN_IN_ACCECN_OUT_NOECN)
+		return tcp_ecn;
+	return ret;
+}
+
 /* Apply either ECT(0) or ECT(1) based on TCP_CONG_ECT_1_NEGOTIATION flag */
 static inline void INET_ECN_xmit_ect_1_negotiation(struct sock *sk)
 {
@@ -599,7 +610,7 @@ static inline void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
 	struct tcp_sock *tp = tcp_sk(sk);
 	bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
 	bool use_ecn, use_accecn;
-	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	u8 tcp_ecn = tcp_ecn_init_bpf(sk);
 
 	use_accecn = tcp_ecn == TCP_ECN_IN_ACCECN_OUT_ACCECN ||
 		     tcp_ca_needs_accecn(sk);
diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
--- a/net/ipv4/bpf_tcp_ops.c
+++ b/net/ipv4/bpf_tcp_ops.c
@@ -24,6 +24,14 @@ static int rwnd_init_stub(struct sock *sk, struct request_sock *req__nullable)
 	return ctx->retval;
 }
 
+static int ecn_init_stub(struct sock *sk, struct request_sock *req__nullable)
+{
+	struct bpf_tramp_run_ctx *ctx =
+		container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
+
+	return ctx->retval;
+}
+
 static void active_established_stub(struct sock *sk, struct sk_buff *skb__nullable)
 {
 }
@@ -87,6 +95,7 @@ static void dequeue_rcvq_stub(struct sock *sk)
 static struct bpf_tcp_ops __bpf_tcp_ops = {
 	.timeout_init = timeout_init_stub,
 	.rwnd_init = rwnd_init_stub,
+	.ecn_init = ecn_init_stub,
 	.active_established = active_established_stub,
 	.passive_established = passive_established_stub,
 	.rto = rto_stub,
@@ -258,7 +267,8 @@ get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return NULL;
 	case BPF_FUNC_get_retval:
 		if (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
-		    moff == offsetof(struct bpf_tcp_ops, rwnd_init))
+		    moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
+		    moff == offsetof(struct bpf_tcp_ops, ecn_init))
 			return &bpf_tcp_ops_get_retval_proto;
 		return NULL;
 	case BPF_FUNC_reserve_hdr_opt:
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -7500,13 +7500,13 @@ static void tcp_ecn_create_request(struct request_sock *req,
 				   const struct dst_entry *dst)
 {
 	const struct tcphdr *th = tcp_hdr(skb);
-	const struct net *net = sock_net(listen_sk);
+	u8 tcp_ecn = tcp_ecn_init_bpf((struct sock *)req);
 	bool th_ecn = th->ece && th->cwr;
 	bool ect, ecn_ok;
 	u32 ecn_ok_dst;
 
 	if (tcp_accecn_syn_requested(th) &&
-	    (READ_ONCE(net->ipv4.sysctl_tcp_ecn) >= 3 ||
+	    (tcp_ecn >= 3 ||
 	     tcp_ca_needs_accecn(listen_sk))) {
 		inet_rsk(req)->ecn_ok = 1;
 		tcp_rsk(req)->accecn_ok = 1;
@@ -7520,7 +7520,7 @@ static void tcp_ecn_create_request(struct request_sock *req,
 
 	ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
 	ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
-	ecn_ok = READ_ONCE(net->ipv4.sysctl_tcp_ecn) || ecn_ok_dst;
+	ecn_ok = tcp_ecn || ecn_ok_dst;
 
 	if (((!ect || th->res1 || th->ae) && ecn_ok) ||
 	    tcp_ca_needs_ecn(listen_sk) ||

[...]


The commit log says that the problem is in the path, and in v4 Jakub
said "This is a routing property".
RFC3168 ECN has a per-route knob for that: RTAX_FEATURE_ECN.
usec TS got RTAX_FEATURE_TCP_USEC_TS.
Your reply to Eric in that thread had a per-app reason too: AccECN for
latency sensitive apps and not for bulk traffic.
Make it clear in the commit log if this is must-have part of design.

I'm not saying that this bpf ops should be the way to go.
If it's only the path, one more route feature bit
might be all that Android needs.

pw-bot: cr

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

* [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN
  2026-10-16 18:03   ` Alexei Starovoitov
@ 2026-11-16 13:46     ` Irlanki Sandeep
  2026-11-16 13:58       ` sashiko-bot
  2026-11-16 14:28       ` bot+bpf-ci
  2026-11-16 15:13     ` [PATCH bpf-next v9] " Irlanki Sandeep
  1 sibling, 2 replies; 6+ messages in thread
From: Irlanki Sandeep @ 2026-11-16 13:46 UTC (permalink / raw)
  To: netdev, bpf, kuba
  Cc: cpgs, edumazet, davem, pabeni, ncardwell, ast, alexei.starovoitov,
	ameryhung, kuniyu, daniel, andrii, corbet, linux-kernel, lorenzo,
	maze, sporeba, motomuman, madhan.raj, srihari.k, g.pokhra,
	r.kumawat, raj.kumars, ts413.lee, ramyashree.s, daeil2.hwang,
	irlanki.s

TCP ECN and Accurate ECN (AccECN) negotiation are currently decided
globally by the net.ipv4.tcp_ecn sysctl; the per-route knob
(RTAX_FEATURE_ECN) can only force-enable classic ECN on a path. This
lacks the granularity required for per-application and per-connection
ECN policies in multi-application runtimes such as Android.

ECN/L4S selection is an application-level property, not only a path
property: on the exact same route, latency-sensitive applications
(real-time gaming, interactive video/VoIP, low-latency messaging)
benefit from negotiating AccECN for low-queuing-delay L4S treatment,
while bulk background transfers or legacy applications in other
cgroups prefer classic RFC3168 ECN, or no ECN at all where middlebox
traversal is a concern. On the passive side, a server may further
want to decide the ECN mode per peer at handshake time. A route
feature bit cannot express any of this when multiple applications
share one route.

To support this without adding per-socket state to struct tcp_sock or
resorting to the legacy sock_ops interface, add an 'ecn_init' callback
to struct bpf_tcp_ops, following the existing timeout_init()/
rwnd_init() pattern. The ECN mode is only consulted once per handshake,
so the kernel queries the BPF program via tcp_ecn_init_bpf() when the
sysctl value is read: before the SYN is built on active open, and when
an incoming SYN creates a request_sock on passive open, where the
program sees the listener and the request_sock and can thus decide per
peer. The sysctl value is passed to the program as the default via
bpf_get_retval(); an out-of-range return keeps it, and with syncookies
there is no listener, so the program is not invoked at all. No
negotiated-mode state is stored; the decision taken at handshake time
stands for the connection. The struct_ops program is attached
per-cgroup, giving the per-application policy point (containers,
Android app sandboxes).

Document the sysctl precedence in ip-sysctl.rst and add BPF selftests
covering IPv4 and IPv6 handshakes across active, passive, downgrade,
refusal and out-of-range fallback cases.

Signed-off-by: Irlanki Sandeep <irlanki.s@samsung.com>
---
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.

 Documentation/networking/ip-sysctl.rst        |   7 +
 include/net/tcp.h                             |   6 +
 include/net/tcp_ecn.h                         |  14 +-
 net/ipv4/bpf_tcp_ops.c                        |  12 +-
 net/ipv4/tcp_input.c                          |   6 +-
 .../bpf/prog_tests/bpf_tcp_ops_ecn.c          | 206 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_tcp_ops_ecn.c     |  56 +++++
 7 files changed, 302 insertions(+), 5 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

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index f7af0286341c..bb99a9bddd63 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -484,6 +484,13 @@ tcp_ecn - INTEGER
 	5     AccECN               No ECN
 	===== ==================== ====================
 
+	Note that a BPF program implementing the bpf_tcp_ops 'ecn_init'
+	struct_ops callback, attached to a cgroup, can override this value
+	per connection for the sockets of that cgroup. The callback sees
+	this sysctl value as the default via bpf_get_retval(), and return
+	values outside the table above fall back to it. Connections
+	established via syncookies always use the sysctl value.
+
 	Default: 2
 
 tcp_ecn_option - INTEGER
diff --git a/include/net/tcp.h b/include/net/tcp.h
index f7624ab9b420..19fd7652b1c1 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2971,6 +2971,12 @@ struct bpf_tcp_ops {
 	 */
 	int (*rwnd_init)(struct sock *sk, struct request_sock *req);
 
+	/* Should return the ECN variant to negotiate in this handshake:
+	 * one of net.ipv4.tcp_ecn values. bpf_get_retval() has the sysctl.
+	 * @req as in timeout_init().
+	 */
+	int (*ecn_init)(struct sock *sk, struct request_sock *req);
+
 	/* Called when an active connection becomes established.
 	 * @skb is the SYNACK that completed the 3WHS, or NULL for a
 	 * TCP_REPAIR socket (tcp_finish_connect() with no skb).
diff --git a/include/net/tcp_ecn.h b/include/net/tcp_ecn.h
index 865d5c5a7718..fc76b0b5341f 100644
--- a/include/net/tcp_ecn.h
+++ b/include/net/tcp_ecn.h
@@ -32,6 +32,18 @@ enum tcp_accecn_option {
 	TCP_ACCECN_OPTION_PERSIST = 3,
 };
 
+/* Resolve the effective ECN mode: BPF callback or sysctl fallback */
+static inline u8 tcp_ecn_init_bpf(struct sock *sk)
+{
+	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	int ret;
+
+	ret = bpf_tcp_ops_call_int(ecn_init, tcp_ecn, sk);
+	if (ret < 0 || ret > TCP_ECN_IN_ACCECN_OUT_NOECN)
+		return tcp_ecn;
+	return ret;
+}
+
 /* Apply either ECT(0) or ECT(1) based on TCP_CONG_ECT_1_NEGOTIATION flag */
 static inline void INET_ECN_xmit_ect_1_negotiation(struct sock *sk)
 {
@@ -599,7 +611,7 @@ static inline void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
 	struct tcp_sock *tp = tcp_sk(sk);
 	bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
 	bool use_ecn, use_accecn;
-	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	u8 tcp_ecn = tcp_ecn_init_bpf(sk);
 
 	use_accecn = tcp_ecn == TCP_ECN_IN_ACCECN_OUT_ACCECN ||
 		     tcp_ca_needs_accecn(sk);
diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
index 2ba73dd6c52c..30d65a1cd78f 100644
--- a/net/ipv4/bpf_tcp_ops.c
+++ b/net/ipv4/bpf_tcp_ops.c
@@ -24,6 +24,14 @@ static int rwnd_init_stub(struct sock *sk, struct request_sock *req__nullable)
 	return ctx->retval;
 }
 
+static int ecn_init_stub(struct sock *sk, struct request_sock *req__nullable)
+{
+	struct bpf_tramp_run_ctx *ctx =
+		container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
+
+	return ctx->retval;
+}
+
 static void active_established_stub(struct sock *sk, struct sk_buff *skb__nullable)
 {
 }
@@ -87,6 +95,7 @@ static void dequeue_rcvq_stub(struct sock *sk)
 static struct bpf_tcp_ops __bpf_tcp_ops = {
 	.timeout_init = timeout_init_stub,
 	.rwnd_init = rwnd_init_stub,
+	.ecn_init = ecn_init_stub,
 	.active_established = active_established_stub,
 	.passive_established = passive_established_stub,
 	.rto = rto_stub,
@@ -258,7 +267,8 @@ get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return NULL;
 	case BPF_FUNC_get_retval:
 		if (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
-		    moff == offsetof(struct bpf_tcp_ops, rwnd_init))
+		    moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
+		    moff == offsetof(struct bpf_tcp_ops, ecn_init))
 			return &bpf_tcp_ops_get_retval_proto;
 		return NULL;
 	case BPF_FUNC_reserve_hdr_opt:
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 46f5c4fafa8d..1e39c0ec437e 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -7500,13 +7500,13 @@ static void tcp_ecn_create_request(struct request_sock *req,
 				   const struct dst_entry *dst)
 {
 	const struct tcphdr *th = tcp_hdr(skb);
-	const struct net *net = sock_net(listen_sk);
+	u8 tcp_ecn = tcp_ecn_init_bpf((struct sock *)req);
 	bool th_ecn = th->ece && th->cwr;
 	bool ect, ecn_ok;
 	u32 ecn_ok_dst;
 
 	if (tcp_accecn_syn_requested(th) &&
-	    (READ_ONCE(net->ipv4.sysctl_tcp_ecn) >= 3 ||
+	    (tcp_ecn >= 3 ||
 	     tcp_ca_needs_accecn(listen_sk))) {
 		inet_rsk(req)->ecn_ok = 1;
 		tcp_rsk(req)->accecn_ok = 1;
@@ -7520,7 +7520,7 @@ static void tcp_ecn_create_request(struct request_sock *req,
 
 	ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
 	ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
-	ecn_ok = READ_ONCE(net->ipv4.sysctl_tcp_ecn) || ecn_ok_dst;
+	ecn_ok = tcp_ecn || ecn_ok_dst;
 
 	if (((!ect || th->res1 || th->ae) && ecn_ok) ||
 	    tcp_ca_needs_ecn(listen_sk) ||
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c
new file mode 100644
index 000000000000..077148a6127c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "cgroup_helpers.h"
+#include "bpf_tcp_ops_ecn.skel.h"
+
+#define CGROUP_PATH	"/bpf_tcp_ops_ecn"
+#define TEST_NETNS	"bpf_tcp_ops_ecn"
+
+/* Must match progs/bpf_tcp_ops_ecn.c */
+#define TCP_ECN_MODE_DISABLED	0x00
+#define TCP_ECN_MODE_RFC3168	0x01
+#define TCP_ECN_MODE_ACCECN	0x10
+
+struct ecn_case {
+	const char *name;
+	int sysctl_ecn;		/* net.ipv4.tcp_ecn in the test netns */
+	int retval_active;	/* ecn_init() return value, active open */
+	int retval_passive;	/* ecn_init() return value, passive open */
+	__u32 mode_client;	/* expected ECN mode on the client socket */
+	__u32 mode_server;	/* expected ECN mode on the accepted socket */
+};
+
+/* net.ipv4.tcp_ecn / ecn_init() values (enum tcp_ecn_mode): 0 == no ECN,
+ * 1 == accept + request RFC3168 ECN, 3 == accept + request AccECN.
+ */
+static const struct ecn_case ecn_cases[] = {
+	{
+		/* The sysctl disables ECN; BPF negotiates RFC3168 ECN. */
+		.name = "bpf_enables_classic",
+		.sysctl_ecn = 0,
+		.retval_active = 1,
+		.retval_passive = 1,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+	{
+		/* The sysctl disables ECN; BPF negotiates AccECN. */
+		.name = "bpf_enables_accecn",
+		.sysctl_ecn = 0,
+		.retval_active = 3,
+		.retval_passive = 3,
+		.mode_client = TCP_ECN_MODE_ACCECN,
+		.mode_server = TCP_ECN_MODE_ACCECN,
+	},
+	{
+		/* The client requests AccECN but the server only accepts
+		 * RFC3168 ECN (< 3): negotiation degrades to RFC3168.
+		 */
+		.name = "passive_clamps_accecn",
+		.sysctl_ecn = 0,
+		.retval_active = 3,
+		.retval_passive = 1,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+	{
+		/* The server refuses ECN although the sysctl allows it. */
+		.name = "passive_refuses_ecn",
+		.sysctl_ecn = 1,
+		.retval_active = 3,
+		.retval_passive = 0,
+		.mode_client = TCP_ECN_MODE_DISABLED,
+		.mode_server = TCP_ECN_MODE_DISABLED,
+	},
+	{
+		/* The client does not request ECN although the sysctl
+		 * allows it, so there is nothing for the server to accept.
+		 */
+		.name = "active_disables_ecn",
+		.sysctl_ecn = 1,
+		.retval_active = 0,
+		.retval_passive = 3,
+		.mode_client = TCP_ECN_MODE_DISABLED,
+		.mode_server = TCP_ECN_MODE_DISABLED,
+	},
+	{
+		/* Return values above TCP_ECN_IN_ACCECN_OUT_NOECN fall back
+		 * to the sysctl on both sides.
+		 */
+		.name = "fallback_above_range",
+		.sysctl_ecn = 1,
+		.retval_active = 6,
+		.retval_passive = 6,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+	{
+		/* Negative return values fall back to the sysctl too. */
+		.name = "fallback_negative",
+		.sysctl_ecn = 1,
+		.retval_active = -1,
+		.retval_passive = -1,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+};
+
+/* Connect over loopback and accept() the child socket, so that
+ * passive_established is guaranteed to have run: the listener is woken
+ * up only after tcp_rcv_state_process() moved the child to established.
+ */
+static void do_ecn_connect(int family)
+{
+	const char *addr = family == AF_INET ? "127.0.0.1" : "::1";
+	int server_fd, client_fd, accepted_fd;
+
+	server_fd = start_server(family, SOCK_STREAM, addr, 0, 0);
+	if (!ASSERT_GE(server_fd, 0, "start_server"))
+		return;
+
+	client_fd = connect_to_fd(server_fd, 0);
+	if (ASSERT_OK_FD(client_fd, "connect_to_fd")) {
+		accepted_fd = accept(server_fd, NULL, NULL);
+		if (ASSERT_OK_FD(accepted_fd, "accept"))
+			close(accepted_fd);
+		close(client_fd);
+	}
+
+	close(server_fd);
+}
+
+static void test_ecn(int cgroup_fd, struct bpf_tcp_ops_ecn *skel, int family)
+{
+	struct bpf_link *link;
+	char buf[128];
+	int i;
+
+	link = bpf_map__attach_cgroup_opts(skel->maps.ecn_ops, cgroup_fd, NULL);
+	if (!ASSERT_OK_PTR(link, "attach_ecn_ops"))
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(ecn_cases); i++) {
+		const struct ecn_case *c = &ecn_cases[i];
+
+		snprintf(buf, sizeof(buf), "%d", c->sysctl_ecn);
+		if (!ASSERT_OK(write_sysctl("/proc/sys/net/ipv4/tcp_ecn", buf),
+			       "write_sysctl_tcp_ecn"))
+			break;
+
+		skel->bss->ecn_retval_active = c->retval_active;
+		skel->bss->ecn_retval_passive = c->retval_passive;
+		skel->bss->ecn_seed_active = -1;
+		skel->bss->ecn_seed_passive = -1;
+		skel->bss->ecn_active_cnt = 0;
+		skel->bss->ecn_passive_cnt = 0;
+		skel->bss->ecn_mode_client = 0xdead;
+		skel->bss->ecn_mode_server = 0xdead;
+
+		do_ecn_connect(family);
+
+		/* ecn_init() runs once per side per handshake. */
+		snprintf(buf, sizeof(buf), "%s: active_cnt", c->name);
+		ASSERT_EQ(skel->bss->ecn_active_cnt, 1, buf);
+		snprintf(buf, sizeof(buf), "%s: passive_cnt", c->name);
+		ASSERT_EQ(skel->bss->ecn_passive_cnt, 1, buf);
+
+		/* bpf_get_retval() must be seeded with the sysctl value. */
+		snprintf(buf, sizeof(buf), "%s: seed_active", c->name);
+		ASSERT_EQ(skel->bss->ecn_seed_active, c->sysctl_ecn, buf);
+		snprintf(buf, sizeof(buf), "%s: seed_passive", c->name);
+		ASSERT_EQ(skel->bss->ecn_seed_passive, c->sysctl_ecn, buf);
+
+		snprintf(buf, sizeof(buf), "%s: mode_client", c->name);
+		ASSERT_EQ(skel->bss->ecn_mode_client, c->mode_client, buf);
+		snprintf(buf, sizeof(buf), "%s: mode_server", c->name);
+		ASSERT_EQ(skel->bss->ecn_mode_server, c->mode_server, buf);
+	}
+
+	bpf_link__destroy(link);
+}
+
+static void run_ecn_subtest(int family)
+{
+	struct bpf_tcp_ops_ecn *skel = NULL;
+	struct netns_obj *ns = NULL;
+	int cgroup_fd;
+
+	cgroup_fd = test__join_cgroup(CGROUP_PATH);
+	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+		return;
+
+	ns = netns_new(TEST_NETNS, true);
+	if (!ASSERT_OK_PTR(ns, "netns_new"))
+		goto done;
+
+	skel = bpf_tcp_ops_ecn__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		goto done;
+
+	test_ecn(cgroup_fd, skel, family);
+
+done:
+	bpf_tcp_ops_ecn__destroy(skel);
+	netns_free(ns);
+	close(cgroup_fd);
+}
+
+void test_bpf_tcp_ops_ecn(void)
+{
+	if (test__start_subtest("ipv4"))
+		run_ecn_subtest(AF_INET);
+	if (test__start_subtest("ipv6"))
+		run_ecn_subtest(AF_INET6);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c
new file mode 100644
index 000000000000..1def088db908
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* TCP_ECN_MODE_* bits of tcp_sock::ecn_flags, from include/net/tcp.h */
+#define TCP_ECN_MODE_RFC3168	0x01
+#define TCP_ECN_MODE_ACCECN	0x10
+#define TCP_ECN_MODE_ANY	(TCP_ECN_MODE_RFC3168 | TCP_ECN_MODE_ACCECN)
+
+int ecn_retval_active;		/* ecn_init() return value, active open */
+int ecn_retval_passive;		/* ecn_init() return value, passive open */
+int ecn_seed_active;		/* bpf_get_retval() seen on active open */
+int ecn_seed_passive;		/* bpf_get_retval() seen on passive open */
+__u32 ecn_active_cnt;
+__u32 ecn_passive_cnt;
+__u32 ecn_mode_client;		/* negotiated ECN mode, client socket */
+__u32 ecn_mode_server;		/* negotiated ECN mode, accepted socket */
+
+SEC("struct_ops")
+int BPF_PROG(ecn_ops_ecn_init, struct sock *sk, struct request_sock *req)
+{
+	if (req) {
+		ecn_passive_cnt++;
+		ecn_seed_passive = bpf_get_retval();
+		return ecn_retval_passive;
+	}
+
+	ecn_active_cnt++;
+	ecn_seed_active = bpf_get_retval();
+	return ecn_retval_active;
+}
+
+SEC("struct_ops")
+void BPF_PROG(ecn_ops_active_established, struct sock *sk,
+	      struct sk_buff *skb)
+{
+	ecn_mode_client = ((struct tcp_sock *)sk)->ecn_flags & TCP_ECN_MODE_ANY;
+}
+
+SEC("struct_ops")
+void BPF_PROG(ecn_ops_passive_established, struct sock *sk,
+	      struct sk_buff *skb)
+{
+	ecn_mode_server = ((struct tcp_sock *)sk)->ecn_flags & TCP_ECN_MODE_ANY;
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops ecn_ops = {
+	.ecn_init = (void *)ecn_ops_ecn_init,
+	.active_established = (void *)ecn_ops_active_established,
+	.passive_established = (void *)ecn_ops_passive_established,
+};
+
+char _license[] SEC("license") = "GPL";
-- 
2.34.1


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

* Re: [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN
  2026-11-16 13:46     ` [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN Irlanki Sandeep
@ 2026-11-16 13:58       ` sashiko-bot
  2026-11-16 14:28       ` bot+bpf-ci
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-16 13:58 UTC (permalink / raw)
  To: Irlanki Sandeep, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/bpf: multi-line comment style in bpf_tcp_ops_ecn.c
--

commit 826a67ec5267cf10887c1bb0ea3883a7cff12093
Author: Irlanki Sandeep <irlanki.s@samsung.com>

bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN

This commit adds an ecn_init callback to struct bpf_tcp_ops so cgroup
BPF programs can override net.ipv4.tcp_ecn per connection during TCP
handshakes, along with documentation and BPF selftests.

> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c
> new file mode 100644
> index 0000000000000..077148a6127c1
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c

[ ... ]

> +/* net.ipv4.tcp_ecn / ecn_init() values (enum tcp_ecn_mode): 0 == no ECN,
> + * 1 == accept + request RFC3168 ECN, 3 == accept + request AccECN.
> + */
> +static const struct ecn_case ecn_cases[] = {
> +	{
> +		/* The sysctl disables ECN; BPF negotiates RFC3168 ECN. */
> +		.name = "bpf_enables_classic",
> +		.sysctl_ecn = 0,
> +		.retval_active = 1,
> +		.retval_passive = 1,
> +		.mode_client = TCP_ECN_MODE_RFC3168,
> +		.mode_server = TCP_ECN_MODE_RFC3168,
> +	},
> +	{
> +		/* The sysctl disables ECN; BPF negotiates AccECN. */
> +		.name = "bpf_enables_accecn",
> +		.sysctl_ecn = 0,
> +		.retval_active = 3,
> +		.retval_passive = 3,
> +		.mode_client = TCP_ECN_MODE_ACCECN,
> +		.mode_server = TCP_ECN_MODE_ACCECN,
> +	},
> +	{
> +		/* The client requests AccECN but the server only accepts
> +		 * RFC3168 ECN (< 3): negotiation degrades to RFC3168.
> +		 */
> +		.name = "passive_clamps_accecn",
> +		.sysctl_ecn = 0,
> +		.retval_active = 3,
> +		.retval_passive = 1,
> +		.mode_client = TCP_ECN_MODE_RFC3168,
> +		.mode_server = TCP_ECN_MODE_RFC3168,
> +	},
> +	{
> +		/* The server refuses ECN although the sysctl allows it. */
> +		.name = "passive_refuses_ecn",
> +		.sysctl_ecn = 1,
> +		.retval_active = 3,
> +		.retval_passive = 0,
> +		.mode_client = TCP_ECN_MODE_DISABLED,
> +		.mode_server = TCP_ECN_MODE_DISABLED,
> +	},
> +	{
> +		/* The client does not request ECN although the sysctl
> +		 * allows it, so there is nothing for the server to accept.
> +		 */
> +		.name = "active_disables_ecn",
> +		.sysctl_ecn = 1,
> +		.retval_active = 0,
> +		.retval_passive = 3,
> +		.mode_client = TCP_ECN_MODE_DISABLED,
> +		.mode_server = TCP_ECN_MODE_DISABLED,
> +	},
> +	{
> +		/* Return values above TCP_ECN_IN_ACCECN_OUT_NOECN fall back
> +		 * to the sysctl on both sides.
> +		 */
> +		.name = "fallback_above_range",
> +		.sysctl_ecn = 1,
> +		.retval_active = 6,
> +		.retval_passive = 6,
> +		.mode_client = TCP_ECN_MODE_RFC3168,
> +		.mode_server = TCP_ECN_MODE_RFC3168,
> +	},
> +	{
> +		/* Negative return values fall back to the sysctl too. */
> +		.name = "fallback_negative",
> +		.sysctl_ecn = 1,
> +		.retval_active = -1,
> +		.retval_passive = -1,
> +		.mode_client = TCP_ECN_MODE_RFC3168,
> +		.mode_server = TCP_ECN_MODE_RFC3168,
> +	},
> +};
> +
> +/* Connect over loopback and accept() the child socket, so that
> + * passive_established is guaranteed to have run: the listener is woken
> + * up only after tcp_rcv_state_process() moved the child to established.
> + */
> +static void do_ecn_connect(int family)

[Severity: Low]
This isn't a bug, but BPF code uses the multi-line comment style where
the opening /* sits on its own line with the comment text starting on
the next line. Could the multi-line comments in ecn_cases[] and above
do_ecn_connect() be formatted that way?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116134652.322156-1-irlanki.s@samsung.com?part=1

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

* Re: [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN
  2026-11-16 13:46     ` [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN Irlanki Sandeep
  2026-11-16 13:58       ` sashiko-bot
@ 2026-11-16 14:28       ` bot+bpf-ci
  1 sibling, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-11-16 14:28 UTC (permalink / raw)
  To: irlanki.s, netdev, bpf, kuba
  Cc: cpgs, edumazet, davem, pabeni, ncardwell, ast, alexei.starovoitov,
	ameryhung, kuniyu, daniel, andrii, corbet, linux-kernel, lorenzo,
	maze, sporeba, motomuman, madhan.raj, srihari.k, g.pokhra,
	r.kumawat, raj.kumars, ts413.lee, ramyashree.s, daeil2.hwang,
	irlanki.s, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 1604 bytes --]

> bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN

TL;DR: Nit only: a comment in bpf_tcp_ops_get_retval() still lists
timeout_init/rwnd_init as the only bpf_get_retval() users, but ecn_init now
uses it too; the code is fine.

> diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
> --- a/net/ipv4/bpf_tcp_ops.c
> +++ b/net/ipv4/bpf_tcp_ops.c

[ ... ]

> @@ -258,7 +267,8 @@ get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>  		return NULL;
>  	case BPF_FUNC_get_retval:
>  		if (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
> -		    moff == offsetof(struct bpf_tcp_ops, rwnd_init))
> +		    moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
> +		    moff == offsetof(struct bpf_tcp_ops, ecn_init))
>  			return &bpf_tcp_ops_get_retval_proto;
>  		return NULL;

This isn't a bug, but bpf_get_retval() is now also exposed to ecn_init,
while the comment in bpf_tcp_ops_get_retval() still reads:

	/* bpf_get_retval() is only exposed to timeout_init/rwnd_init, which
	 * always run via bpf_tcp_ops_call_int(). ...
	 */
	if (WARN_ON_ONCE(!ctx->saved_run_ctx))

The invariant still holds, since ecn_init is only invoked through
tcp_ecn_init_bpf() -> bpf_tcp_ops_call_int(), but the comment now lists
an incomplete set of members.

Could it name ecn_init as well, or say "the int-returning *_init
members"?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/43881864185

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

* [PATCH bpf-next v9] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN
  2026-10-16 18:03   ` Alexei Starovoitov
  2026-11-16 13:46     ` [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN Irlanki Sandeep
@ 2026-11-16 15:13     ` Irlanki Sandeep
  1 sibling, 0 replies; 6+ messages in thread
From: Irlanki Sandeep @ 2026-11-16 15:13 UTC (permalink / raw)
  To: netdev, bpf, kuba
  Cc: cpgs, edumazet, davem, pabeni, ncardwell, ast, alexei.starovoitov,
	ameryhung, kuniyu, daniel, andrii, corbet, linux-kernel, lorenzo,
	maze, sporeba, motomuman, madhan.raj, srihari.k, g.pokhra,
	r.kumawat, raj.kumars, ts413.lee, ramyashree.s, daeil2.hwang,
	irlanki.s

TCP ECN and Accurate ECN (AccECN) negotiation are currently decided
globally by the net.ipv4.tcp_ecn sysctl; the per-route knob
(RTAX_FEATURE_ECN) can only force-enable classic ECN on a path. This
lacks the granularity required for per-application and per-connection
ECN policies in multi-application runtimes such as Android.

ECN/L4S selection is an application-level property, not only a path
property: on the exact same route, latency-sensitive applications
(real-time gaming, interactive video/VoIP, low-latency messaging)
benefit from negotiating AccECN for low-queuing-delay L4S treatment,
while bulk background transfers or legacy applications in other
cgroups prefer classic RFC3168 ECN, or no ECN at all where middlebox
traversal is a concern. On the passive side, a server may further
want to decide the ECN mode per peer at handshake time. A route
feature bit cannot express any of this when multiple applications
share one route.

To support this without adding per-socket state to struct tcp_sock or
resorting to the legacy sock_ops interface, add an 'ecn_init' callback
to struct bpf_tcp_ops, following the existing timeout_init()/
rwnd_init() pattern. The ECN mode is only consulted once per handshake,
so the kernel queries the BPF program via tcp_ecn_init_bpf() when the
sysctl value is read: before the SYN is built on active open, and when
an incoming SYN creates a request_sock on passive open, where the
program sees the listener and the request_sock and can thus decide per
peer. The sysctl value is passed to the program as the default via
bpf_get_retval(); an out-of-range return keeps it, and with syncookies
there is no listener, so the program is not invoked at all. No
negotiated-mode state is stored; the decision taken at handshake time
stands for the connection. The struct_ops program is attached
per-cgroup, giving the per-application policy point (containers,
Android app sandboxes).

Document the sysctl precedence in ip-sysctl.rst and add BPF selftests
covering IPv4 and IPv6 handshakes across active, passive, downgrade,
refusal and out-of-range fallback cases.

Signed-off-by: Irlanki Sandeep <irlanki.s@samsung.com>
---
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.

 Documentation/networking/ip-sysctl.rst        |   7 +
 include/net/tcp.h                             |   6 +
 include/net/tcp_ecn.h                         |  14 +-
 net/ipv4/bpf_tcp_ops.c                        |  21 +-
 net/ipv4/tcp_input.c                          |   6 +-
 .../bpf/prog_tests/bpf_tcp_ops_ecn.c          | 212 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_tcp_ops_ecn.c     |  57 +++++
 7 files changed, 314 insertions(+), 9 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

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index f7af0286341c..bb99a9bddd63 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -484,6 +484,13 @@ tcp_ecn - INTEGER
 	5     AccECN               No ECN
 	===== ==================== ====================
 
+	Note that a BPF program implementing the bpf_tcp_ops 'ecn_init'
+	struct_ops callback, attached to a cgroup, can override this value
+	per connection for the sockets of that cgroup. The callback sees
+	this sysctl value as the default via bpf_get_retval(), and return
+	values outside the table above fall back to it. Connections
+	established via syncookies always use the sysctl value.
+
 	Default: 2
 
 tcp_ecn_option - INTEGER
diff --git a/include/net/tcp.h b/include/net/tcp.h
index f7624ab9b420..19fd7652b1c1 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2971,6 +2971,12 @@ struct bpf_tcp_ops {
 	 */
 	int (*rwnd_init)(struct sock *sk, struct request_sock *req);
 
+	/* Should return the ECN variant to negotiate in this handshake:
+	 * one of net.ipv4.tcp_ecn values. bpf_get_retval() has the sysctl.
+	 * @req as in timeout_init().
+	 */
+	int (*ecn_init)(struct sock *sk, struct request_sock *req);
+
 	/* Called when an active connection becomes established.
 	 * @skb is the SYNACK that completed the 3WHS, or NULL for a
 	 * TCP_REPAIR socket (tcp_finish_connect() with no skb).
diff --git a/include/net/tcp_ecn.h b/include/net/tcp_ecn.h
index 865d5c5a7718..fc76b0b5341f 100644
--- a/include/net/tcp_ecn.h
+++ b/include/net/tcp_ecn.h
@@ -32,6 +32,18 @@ enum tcp_accecn_option {
 	TCP_ACCECN_OPTION_PERSIST = 3,
 };
 
+/* Resolve the effective ECN mode: BPF callback or sysctl fallback */
+static inline u8 tcp_ecn_init_bpf(struct sock *sk)
+{
+	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	int ret;
+
+	ret = bpf_tcp_ops_call_int(ecn_init, tcp_ecn, sk);
+	if (ret < 0 || ret > TCP_ECN_IN_ACCECN_OUT_NOECN)
+		return tcp_ecn;
+	return ret;
+}
+
 /* Apply either ECT(0) or ECT(1) based on TCP_CONG_ECT_1_NEGOTIATION flag */
 static inline void INET_ECN_xmit_ect_1_negotiation(struct sock *sk)
 {
@@ -599,7 +611,7 @@ static inline void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
 	struct tcp_sock *tp = tcp_sk(sk);
 	bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
 	bool use_ecn, use_accecn;
-	u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
+	u8 tcp_ecn = tcp_ecn_init_bpf(sk);
 
 	use_accecn = tcp_ecn == TCP_ECN_IN_ACCECN_OUT_ACCECN ||
 		     tcp_ca_needs_accecn(sk);
diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c
index 2ba73dd6c52c..785517364f0b 100644
--- a/net/ipv4/bpf_tcp_ops.c
+++ b/net/ipv4/bpf_tcp_ops.c
@@ -24,6 +24,14 @@ static int rwnd_init_stub(struct sock *sk, struct request_sock *req__nullable)
 	return ctx->retval;
 }
 
+static int ecn_init_stub(struct sock *sk, struct request_sock *req__nullable)
+{
+	struct bpf_tramp_run_ctx *ctx =
+		container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
+
+	return ctx->retval;
+}
+
 static void active_established_stub(struct sock *sk, struct sk_buff *skb__nullable)
 {
 }
@@ -87,6 +95,7 @@ static void dequeue_rcvq_stub(struct sock *sk)
 static struct bpf_tcp_ops __bpf_tcp_ops = {
 	.timeout_init = timeout_init_stub,
 	.rwnd_init = rwnd_init_stub,
+	.ecn_init = ecn_init_stub,
 	.active_established = active_established_stub,
 	.passive_established = passive_established_stub,
 	.rto = rto_stub,
@@ -202,10 +211,11 @@ BPF_CALL_0(bpf_tcp_ops_get_retval)
 	struct bpf_tramp_run_ctx *ctx =
 		container_of(current->bpf_ctx, struct bpf_tramp_run_ctx, run_ctx);
 
-	/* bpf_get_retval() is only exposed to timeout_init/rwnd_init, which
-	 * always run via bpf_tcp_ops_call_int(). Its run_ctx carries the int
-	 * return value chained across the bpf_tcp_ops attached to the cgroup
-	 * and is this program's saved_run_ctx.
+	/* bpf_get_retval() is only exposed to timeout_init, rwnd_init and
+	 * ecn_init, which always run via bpf_tcp_ops_call_int(). Its
+	 * run_ctx carries the int return value chained across the
+	 * bpf_tcp_ops attached to the cgroup and is this program's
+	 * saved_run_ctx.
 	 */
 	if (WARN_ON_ONCE(!ctx->saved_run_ctx))
 		return 0;
@@ -258,7 +268,8 @@ get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return NULL;
 	case BPF_FUNC_get_retval:
 		if (moff == offsetof(struct bpf_tcp_ops, timeout_init) ||
-		    moff == offsetof(struct bpf_tcp_ops, rwnd_init))
+		    moff == offsetof(struct bpf_tcp_ops, rwnd_init) ||
+		    moff == offsetof(struct bpf_tcp_ops, ecn_init))
 			return &bpf_tcp_ops_get_retval_proto;
 		return NULL;
 	case BPF_FUNC_reserve_hdr_opt:
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 46f5c4fafa8d..1e39c0ec437e 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -7500,13 +7500,13 @@ static void tcp_ecn_create_request(struct request_sock *req,
 				   const struct dst_entry *dst)
 {
 	const struct tcphdr *th = tcp_hdr(skb);
-	const struct net *net = sock_net(listen_sk);
+	u8 tcp_ecn = tcp_ecn_init_bpf((struct sock *)req);
 	bool th_ecn = th->ece && th->cwr;
 	bool ect, ecn_ok;
 	u32 ecn_ok_dst;
 
 	if (tcp_accecn_syn_requested(th) &&
-	    (READ_ONCE(net->ipv4.sysctl_tcp_ecn) >= 3 ||
+	    (tcp_ecn >= 3 ||
 	     tcp_ca_needs_accecn(listen_sk))) {
 		inet_rsk(req)->ecn_ok = 1;
 		tcp_rsk(req)->accecn_ok = 1;
@@ -7520,7 +7520,7 @@ static void tcp_ecn_create_request(struct request_sock *req,
 
 	ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
 	ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
-	ecn_ok = READ_ONCE(net->ipv4.sysctl_tcp_ecn) || ecn_ok_dst;
+	ecn_ok = tcp_ecn || ecn_ok_dst;
 
 	if (((!ect || th->res1 || th->ae) && ecn_ok) ||
 	    tcp_ca_needs_ecn(listen_sk) ||
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c
new file mode 100644
index 000000000000..59cfec70758d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ops_ecn.c
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Samsung Electronics Co., Ltd. */
+
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "cgroup_helpers.h"
+#include "bpf_tcp_ops_ecn.skel.h"
+
+#define CGROUP_PATH	"/bpf_tcp_ops_ecn"
+#define TEST_NETNS	"bpf_tcp_ops_ecn"
+
+/* Must match progs/bpf_tcp_ops_ecn.c */
+#define TCP_ECN_MODE_DISABLED	0x00
+#define TCP_ECN_MODE_RFC3168	0x01
+#define TCP_ECN_MODE_ACCECN	0x10
+
+struct ecn_case {
+	const char *name;
+	int sysctl_ecn;		/* net.ipv4.tcp_ecn in the test netns */
+	int retval_active;	/* ecn_init() return value, active open */
+	int retval_passive;	/* ecn_init() return value, passive open */
+	__u32 mode_client;	/* expected ECN mode on the client socket */
+	__u32 mode_server;	/* expected ECN mode on the accepted socket */
+};
+
+/*
+ * net.ipv4.tcp_ecn / ecn_init() values (enum tcp_ecn_mode): 0 == no ECN,
+ * 1 == accept + request RFC3168 ECN, 3 == accept + request AccECN.
+ */
+static const struct ecn_case ecn_cases[] = {
+	{
+		/* The sysctl disables ECN; BPF negotiates RFC3168 ECN. */
+		.name = "bpf_enables_classic",
+		.sysctl_ecn = 0,
+		.retval_active = 1,
+		.retval_passive = 1,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+	{
+		/* The sysctl disables ECN; BPF negotiates AccECN. */
+		.name = "bpf_enables_accecn",
+		.sysctl_ecn = 0,
+		.retval_active = 3,
+		.retval_passive = 3,
+		.mode_client = TCP_ECN_MODE_ACCECN,
+		.mode_server = TCP_ECN_MODE_ACCECN,
+	},
+	{
+		/*
+		 * The client requests AccECN but the server only accepts
+		 * RFC3168 ECN (< 3): negotiation degrades to RFC3168.
+		 */
+		.name = "passive_clamps_accecn",
+		.sysctl_ecn = 0,
+		.retval_active = 3,
+		.retval_passive = 1,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+	{
+		/* The server refuses ECN although the sysctl allows it. */
+		.name = "passive_refuses_ecn",
+		.sysctl_ecn = 1,
+		.retval_active = 3,
+		.retval_passive = 0,
+		.mode_client = TCP_ECN_MODE_DISABLED,
+		.mode_server = TCP_ECN_MODE_DISABLED,
+	},
+	{
+		/*
+		 * The client does not request ECN although the sysctl
+		 * allows it, so there is nothing for the server to accept.
+		 */
+		.name = "active_disables_ecn",
+		.sysctl_ecn = 1,
+		.retval_active = 0,
+		.retval_passive = 3,
+		.mode_client = TCP_ECN_MODE_DISABLED,
+		.mode_server = TCP_ECN_MODE_DISABLED,
+	},
+	{
+		/*
+		 * Return values above TCP_ECN_IN_ACCECN_OUT_NOECN fall back
+		 * to the sysctl on both sides.
+		 */
+		.name = "fallback_above_range",
+		.sysctl_ecn = 1,
+		.retval_active = 6,
+		.retval_passive = 6,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+	{
+		/* Negative return values fall back to the sysctl too. */
+		.name = "fallback_negative",
+		.sysctl_ecn = 1,
+		.retval_active = -1,
+		.retval_passive = -1,
+		.mode_client = TCP_ECN_MODE_RFC3168,
+		.mode_server = TCP_ECN_MODE_RFC3168,
+	},
+};
+
+/*
+ * Connect over loopback and accept() the child socket, so that
+ * passive_established is guaranteed to have run: the listener is woken
+ * up only after tcp_rcv_state_process() moved the child to established.
+ */
+static void do_ecn_connect(int family)
+{
+	const char *addr = family == AF_INET ? "127.0.0.1" : "::1";
+	int server_fd, client_fd, accepted_fd;
+
+	server_fd = start_server(family, SOCK_STREAM, addr, 0, 0);
+	if (!ASSERT_GE(server_fd, 0, "start_server"))
+		return;
+
+	client_fd = connect_to_fd(server_fd, 0);
+	if (ASSERT_OK_FD(client_fd, "connect_to_fd")) {
+		accepted_fd = accept(server_fd, NULL, NULL);
+		if (ASSERT_OK_FD(accepted_fd, "accept"))
+			close(accepted_fd);
+		close(client_fd);
+	}
+
+	close(server_fd);
+}
+
+static void test_ecn(int cgroup_fd, struct bpf_tcp_ops_ecn *skel, int family)
+{
+	struct bpf_link *link;
+	char buf[128];
+	int i;
+
+	link = bpf_map__attach_cgroup_opts(skel->maps.ecn_ops, cgroup_fd, NULL);
+	if (!ASSERT_OK_PTR(link, "attach_ecn_ops"))
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(ecn_cases); i++) {
+		const struct ecn_case *c = &ecn_cases[i];
+
+		snprintf(buf, sizeof(buf), "%d", c->sysctl_ecn);
+		if (!ASSERT_OK(write_sysctl("/proc/sys/net/ipv4/tcp_ecn", buf),
+			       "write_sysctl_tcp_ecn"))
+			break;
+
+		skel->bss->ecn_retval_active = c->retval_active;
+		skel->bss->ecn_retval_passive = c->retval_passive;
+		skel->bss->ecn_seed_active = -1;
+		skel->bss->ecn_seed_passive = -1;
+		skel->bss->ecn_active_cnt = 0;
+		skel->bss->ecn_passive_cnt = 0;
+		skel->bss->ecn_mode_client = 0xdead;
+		skel->bss->ecn_mode_server = 0xdead;
+
+		do_ecn_connect(family);
+
+		/* ecn_init() runs once per side per handshake. */
+		snprintf(buf, sizeof(buf), "%s: active_cnt", c->name);
+		ASSERT_EQ(skel->bss->ecn_active_cnt, 1, buf);
+		snprintf(buf, sizeof(buf), "%s: passive_cnt", c->name);
+		ASSERT_EQ(skel->bss->ecn_passive_cnt, 1, buf);
+
+		/* bpf_get_retval() must be seeded with the sysctl value. */
+		snprintf(buf, sizeof(buf), "%s: seed_active", c->name);
+		ASSERT_EQ(skel->bss->ecn_seed_active, c->sysctl_ecn, buf);
+		snprintf(buf, sizeof(buf), "%s: seed_passive", c->name);
+		ASSERT_EQ(skel->bss->ecn_seed_passive, c->sysctl_ecn, buf);
+
+		snprintf(buf, sizeof(buf), "%s: mode_client", c->name);
+		ASSERT_EQ(skel->bss->ecn_mode_client, c->mode_client, buf);
+		snprintf(buf, sizeof(buf), "%s: mode_server", c->name);
+		ASSERT_EQ(skel->bss->ecn_mode_server, c->mode_server, buf);
+	}
+
+	bpf_link__destroy(link);
+}
+
+static void run_ecn_subtest(int family)
+{
+	struct bpf_tcp_ops_ecn *skel = NULL;
+	struct netns_obj *ns = NULL;
+	int cgroup_fd;
+
+	cgroup_fd = test__join_cgroup(CGROUP_PATH);
+	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+		return;
+
+	ns = netns_new(TEST_NETNS, true);
+	if (!ASSERT_OK_PTR(ns, "netns_new"))
+		goto done;
+
+	skel = bpf_tcp_ops_ecn__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		goto done;
+
+	test_ecn(cgroup_fd, skel, family);
+
+done:
+	bpf_tcp_ops_ecn__destroy(skel);
+	netns_free(ns);
+	close(cgroup_fd);
+}
+
+void test_bpf_tcp_ops_ecn(void)
+{
+	if (test__start_subtest("ipv4"))
+		run_ecn_subtest(AF_INET);
+	if (test__start_subtest("ipv6"))
+		run_ecn_subtest(AF_INET6);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c
new file mode 100644
index 000000000000..596566148858
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_tcp_ops_ecn.c
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Samsung Electronics Co., Ltd. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* TCP_ECN_MODE_* bits of tcp_sock::ecn_flags, from include/net/tcp.h */
+#define TCP_ECN_MODE_RFC3168	0x01
+#define TCP_ECN_MODE_ACCECN	0x10
+#define TCP_ECN_MODE_ANY	(TCP_ECN_MODE_RFC3168 | TCP_ECN_MODE_ACCECN)
+
+int ecn_retval_active;		/* ecn_init() return value, active open */
+int ecn_retval_passive;		/* ecn_init() return value, passive open */
+int ecn_seed_active;		/* bpf_get_retval() seen on active open */
+int ecn_seed_passive;		/* bpf_get_retval() seen on passive open */
+__u32 ecn_active_cnt;
+__u32 ecn_passive_cnt;
+__u32 ecn_mode_client;		/* negotiated ECN mode, client socket */
+__u32 ecn_mode_server;		/* negotiated ECN mode, accepted socket */
+
+SEC("struct_ops")
+int BPF_PROG(ecn_ops_ecn_init, struct sock *sk, struct request_sock *req)
+{
+	if (req) {
+		ecn_passive_cnt++;
+		ecn_seed_passive = bpf_get_retval();
+		return ecn_retval_passive;
+	}
+
+	ecn_active_cnt++;
+	ecn_seed_active = bpf_get_retval();
+	return ecn_retval_active;
+}
+
+SEC("struct_ops")
+void BPF_PROG(ecn_ops_active_established, struct sock *sk,
+	      struct sk_buff *skb)
+{
+	ecn_mode_client = ((struct tcp_sock *)sk)->ecn_flags & TCP_ECN_MODE_ANY;
+}
+
+SEC("struct_ops")
+void BPF_PROG(ecn_ops_passive_established, struct sock *sk,
+	      struct sk_buff *skb)
+{
+	ecn_mode_server = ((struct tcp_sock *)sk)->ecn_flags & TCP_ECN_MODE_ANY;
+}
+
+SEC(".struct_ops.link")
+struct bpf_tcp_ops ecn_ops = {
+	.ecn_init = (void *)ecn_ops_ecn_init,
+	.active_established = (void *)ecn_ops_active_established,
+	.passive_established = (void *)ecn_ops_passive_established,
+};
+
+char _license[] SEC("license") = "GPL";
-- 
2.34.1


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

end of thread, other threads:[~2026-11-16 15:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20261016100913epcas5p13a41d34ac5d203bff1d220b3d19dee67@epcas5p1.samsung.com>
2026-10-16 10:10 ` [PATCH net-next v7] tcp: add BPF kfuncs to set per-socket ECN mode and AccECN option Irlanki Sandeep
2026-10-16 18:03   ` Alexei Starovoitov
2026-11-16 13:46     ` [PATCH bpf-next v8] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN Irlanki Sandeep
2026-11-16 13:58       ` sashiko-bot
2026-11-16 14:28       ` bot+bpf-ci
2026-11-16 15:13     ` [PATCH bpf-next v9] " Irlanki Sandeep

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox