[PATCH net-next v5 3/4] tcp: add TCP_ECN_SEND_ECT_1 flag for congestion control

From: chia-yu . chang

Date: Mon Nov 30 2026 - 07:57:19 EST


From: Chia-Yu Chang <chia-yu.chang@xxxxxxxxxxxxxxxxxxx>

This patch introduces the new TCP_ECN_SEND_ECT_1 bit, which enables
a congestion control module to control whether the connection
transmits using ECT(1) (if the bit is set) or ECT(0) (otherwise),
when congestion control is initialized after ECN negotiation.

A congestion control module, such as the upcoming TCP Prague
using L4S ECN, can set this new TCP_ECN_SEND_ECT_1 bit if it
wants to send the ECT(1) code point in outgoing IP headers.

Signed-off-by: Chia-Yu Chang <chia-yu.chang@xxxxxxxxxxxxxxxxxxx>

---
v5:
- Document detailed comments for INET_ECN_xmit_established_ect()
- Clear ECT(1) selection from prior CC in tcp_reinit_congestion_control()

v3:
- Update commit message
- Update flag name and helper function name to eliminate confusion
---
include/net/tcp.h | 1 +
include/net/tcp_ecn.h | 22 ++++++++++++++++++++++
net/ipv4/tcp_cong.c | 12 +++++++++++-
net/ipv4/tcp_output.c | 2 +-
4 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 12129ae8b06b..1cc3f60da8d6 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -435,6 +435,7 @@ static inline void tcp_dec_quickack_mode(struct sock *sk)
#define TCP_ECN_DEMAND_CWR BIT(2)
#define TCP_ECN_SEEN BIT(3)
#define TCP_ECN_MODE_ACCECN BIT(4)
+#define TCP_ECN_SEND_ECT_1 BIT(5)

#define TCP_ECN_DISABLED 0
#define TCP_ECN_MODE_PENDING (TCP_ECN_MODE_RFC3168 | TCP_ECN_MODE_ACCECN)
diff --git a/include/net/tcp_ecn.h b/include/net/tcp_ecn.h
index 3bb0b2f692f4..a8a64be25c1e 100644
--- a/include/net/tcp_ecn.h
+++ b/include/net/tcp_ecn.h
@@ -55,6 +55,28 @@ static inline void INET_ECN_xmit_negotiation_ect(struct sock *sk)
__INET_ECN_xmit(sk, tcp_ca_ect_1_negotiation(sk));
}

+/**
+ * INET_ECN_xmit_established_ect - set the ECT codepoint after CA init
+ * @sk: TCP socket
+ *
+ * Unlike INET_ECN_xmit_negotiation_ect(), which selects the codepoint
+ * used during ECN negotiation (SYN/SYN-ACK), this helper selects the
+ * ECN codepoint used by the established flow.
+ *
+ * The codepoint is chosen according to TCP_ECN_SEND_ECT_1:
+ * ECT(1) when the bit is set, ECT(0) otherwise.
+ *
+ * This helper does not determine whether ECN should be enabled.
+ * Callers invoke it only after congestion control initialization,
+ * with the congestion control module selecting the codepoint to use.
+ *
+ * This changes the socket TOS/tclass rather than a single packet.
+ */
+static inline void INET_ECN_xmit_established_ect(struct sock *sk)
+{
+ __INET_ECN_xmit(sk, tcp_sk(sk)->ecn_flags & TCP_ECN_SEND_ECT_1);
+}
+
static inline void tcp_ecn_queue_cwr(struct tcp_sock *tp)
{
/* Do not set CWR if in AccECN mode! */
diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
index 3331679c81d7..34617106f5e5 100644
--- a/net/ipv4/tcp_cong.c
+++ b/net/ipv4/tcp_cong.c
@@ -240,7 +240,14 @@ void tcp_init_congestion_control(struct sock *sk)
tcp_sk(sk)->prior_ssthresh = 0;
if (icsk->icsk_ca_ops->init)
icsk->icsk_ca_ops->init(sk);
- if (tcp_ca_needs_ecn(sk))
+ /*
+ * Select the initial ECN codepoint based on the congestion
+ * control module flag, while each module is responsible for
+ * handling fallback cases when negotiation did not succeed.
+ */
+ if (tcp_ca_needs_accecn(sk))
+ INET_ECN_xmit_established_ect(sk);
+ else if (tcp_ca_needs_ecn(sk))
INET_ECN_xmit(sk);
else
INET_ECN_dontxmit(sk);
@@ -251,12 +258,15 @@ static void tcp_reinit_congestion_control(struct sock *sk,
const struct tcp_congestion_ops *ca)
{
struct inet_connection_sock *icsk = inet_csk(sk);
+ struct tcp_sock *tp = tcp_sk(sk);

tcp_cleanup_congestion_control(sk);
WRITE_ONCE(icsk->icsk_ca_ops, ca);
icsk->icsk_ca_setsockopt = 1;
memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));

+ /* Reset ECT(1) selection before initializing a new CC */
+ tp->ecn_flags &= ~TCP_ECN_SEND_ECT_1;
if (ca->flags & TCP_CONG_NEEDS_ECN)
INET_ECN_xmit_negotiation_ect(sk);
else
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 08d0d4a639fe..7b694479095e 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -350,7 +350,7 @@ static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,
if (tcp_ecn_mode_accecn(tp)) {
if (!tcp_accecn_ace_fail_recv(tp) &&
!tcp_accecn_ace_fail_send(tp))
- INET_ECN_xmit(sk);
+ INET_ECN_xmit_established_ect(sk);
else
INET_ECN_dontxmit(sk);
tcp_accecn_set_ace(tp, skb, th);
--
2.34.1