[PATCH bpf-next v10 1/2] bpf: tcp: add ecn_init callback to bpf_tcp_ops for per-connection ECN
From: Irlanki Sandeep
Date: Fri Nov 27 2026 - 08:30:02 EST
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. RTAX_FEATURE_ECN
cannot select AccECN, and it cannot distinguish applications that 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 on
passive open only when the incoming SYN requests ECN (AccECN or
ECE+CWR), 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. With
syncookies no request_sock is created, so the program is not invoked and
the sysctl and route decide as before. 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.
Signed-off-by: Irlanki Sandeep <irlanki.s@xxxxxxxxxxx>
---
v9 -> v10:
- 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).
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 ++++++++---
5 files changed, 52 insertions(+), 10 deletions(-)
diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index f7af0286341c..265746e72942 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -484,6 +484,14 @@ 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 any
+ return value outside the table above falls back to it. Connections
+ established via syncookies are not seen by the callback and use the
+ sysctl and route settings instead.
+
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..4d57b4c75846 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)
{
@@ -596,10 +608,10 @@ static inline void tcp_ecn_send_synack(struct sock *sk, struct sk_buff *skb)
/* Packet ECN state for a SYN. */
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);
+ u8 tcp_ecn = tcp_ecn_init_bpf(sk);
+ struct tcp_sock *tp = tcp_sk(sk);
bool use_ecn, use_accecn;
- u8 tcp_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn);
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..4554429f5bde 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -7500,13 +7500,18 @@ 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;
+ u8 tcp_ecn;
+
+ if (!tcp_accecn_syn_requested(th) && !th_ecn)
+ return;
+
+ tcp_ecn = tcp_ecn_init_bpf((struct sock *)req);
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 +7525,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) ||
--
2.34.1