tcp: helpers for ECN mode handling
authorIlpo Järvinen <ij@kernel.org>
Wed, 5 Mar 2025 22:38:47 +0000 (23:38 +0100)
committerDavid S. Miller <davem@davemloft.net>
Mon, 17 Mar 2025 13:54:11 +0000 (13:54 +0000)
Create helpers for TCP ECN modes. No functional changes.

Signed-off-by: Ilpo Järvinen <ij@kernel.org>
Signed-off-by: Chia-Yu Chang <chia-yu.chang@nokia-bell-labs.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/tcp.h
net/ipv4/tcp.c
net/ipv4/tcp_dctcp.c
net/ipv4/tcp_input.c
net/ipv4/tcp_minisocks.c
net/ipv4/tcp_output.c

index 4e11bdc864709c17594e9888c666d05448a7e8ff..5520f2d62dc0de182d8f0bb035735ad79f350a13 100644 (file)
@@ -374,10 +374,46 @@ static inline void tcp_dec_quickack_mode(struct sock *sk)
        }
 }
 
-#define        TCP_ECN_OK              1
-#define        TCP_ECN_QUEUE_CWR       2
-#define        TCP_ECN_DEMAND_CWR      4
-#define        TCP_ECN_SEEN            8
+#define        TCP_ECN_MODE_RFC3168    BIT(0)
+#define        TCP_ECN_QUEUE_CWR       BIT(1)
+#define        TCP_ECN_DEMAND_CWR      BIT(2)
+#define        TCP_ECN_SEEN            BIT(3)
+#define        TCP_ECN_MODE_ACCECN     BIT(4)
+
+#define        TCP_ECN_DISABLED        0
+#define        TCP_ECN_MODE_PENDING    (TCP_ECN_MODE_RFC3168 | TCP_ECN_MODE_ACCECN)
+#define        TCP_ECN_MODE_ANY        (TCP_ECN_MODE_RFC3168 | TCP_ECN_MODE_ACCECN)
+
+static inline bool tcp_ecn_mode_any(const struct tcp_sock *tp)
+{
+       return tp->ecn_flags & TCP_ECN_MODE_ANY;
+}
+
+static inline bool tcp_ecn_mode_rfc3168(const struct tcp_sock *tp)
+{
+       return (tp->ecn_flags & TCP_ECN_MODE_ANY) == TCP_ECN_MODE_RFC3168;
+}
+
+static inline bool tcp_ecn_mode_accecn(const struct tcp_sock *tp)
+{
+       return (tp->ecn_flags & TCP_ECN_MODE_ANY) == TCP_ECN_MODE_ACCECN;
+}
+
+static inline bool tcp_ecn_disabled(const struct tcp_sock *tp)
+{
+       return !tcp_ecn_mode_any(tp);
+}
+
+static inline bool tcp_ecn_mode_pending(const struct tcp_sock *tp)
+{
+       return (tp->ecn_flags & TCP_ECN_MODE_PENDING) == TCP_ECN_MODE_PENDING;
+}
+
+static inline void tcp_ecn_mode_set(struct tcp_sock *tp, u8 mode)
+{
+       tp->ecn_flags &= ~TCP_ECN_MODE_ANY;
+       tp->ecn_flags |= mode;
+}
 
 enum tcp_tw_status {
        TCP_TW_SUCCESS = 0,
index 46951e74930844af952dfbc57a107b504d4e296b..989c3c3d8e757361a0ac4a9f039a3cfca10d9612 100644 (file)
@@ -4138,7 +4138,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
                info->tcpi_rcv_wscale = tp->rx_opt.rcv_wscale;
        }
 
-       if (tp->ecn_flags & TCP_ECN_OK)
+       if (tcp_ecn_mode_any(tp))
                info->tcpi_options |= TCPI_OPT_ECN;
        if (tp->ecn_flags & TCP_ECN_SEEN)
                info->tcpi_options |= TCPI_OPT_ECN_SEEN;
index 8a45a4aea9335916121ec533f6d4399ee019208b..03abe0848420d7fb7b514e6ad24f4c702954ab14 100644 (file)
@@ -90,7 +90,7 @@ __bpf_kfunc static void dctcp_init(struct sock *sk)
 {
        const struct tcp_sock *tp = tcp_sk(sk);
 
-       if ((tp->ecn_flags & TCP_ECN_OK) ||
+       if (tcp_ecn_mode_any(tp) ||
            (sk->sk_state == TCP_LISTEN ||
             sk->sk_state == TCP_CLOSE)) {
                struct dctcp *ca = inet_csk_ca(sk);
index 769048b559e5955c03e6cdd0cdc69b915413d6e0..5c270cf96678440df98be52e0f116f61ce0ef09d 100644 (file)
@@ -342,7 +342,7 @@ static bool tcp_in_quickack_mode(struct sock *sk)
 
 static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
 {
-       if (tp->ecn_flags & TCP_ECN_OK)
+       if (tcp_ecn_mode_rfc3168(tp))
                tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
 }
 
@@ -369,7 +369,7 @@ static void tcp_data_ecn_check(struct sock *sk, const struct sk_buff *skb)
 {
        struct tcp_sock *tp = tcp_sk(sk);
 
-       if (!(tcp_sk(sk)->ecn_flags & TCP_ECN_OK))
+       if (tcp_ecn_disabled(tp))
                return;
 
        switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
@@ -402,19 +402,19 @@ static void tcp_data_ecn_check(struct sock *sk, const struct sk_buff *skb)
 
 static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
 {
-       if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
-               tp->ecn_flags &= ~TCP_ECN_OK;
+       if (tcp_ecn_mode_rfc3168(tp) && (!th->ece || th->cwr))
+               tcp_ecn_mode_set(tp, TCP_ECN_DISABLED);
 }
 
 static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
 {
-       if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
-               tp->ecn_flags &= ~TCP_ECN_OK;
+       if (tcp_ecn_mode_rfc3168(tp) && (!th->ece || !th->cwr))
+               tcp_ecn_mode_set(tp, TCP_ECN_DISABLED);
 }
 
 static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
 {
-       if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
+       if (th->ece && !th->syn && tcp_ecn_mode_rfc3168(tp))
                return true;
        return false;
 }
index 3cb8f281186b205e2b03d1b78e1750a024b94f6a..0ae24add155b1222b827f5f10e120b06a2e471cd 100644 (file)
@@ -461,7 +461,9 @@ void tcp_openreq_init_rwin(struct request_sock *req,
 static void tcp_ecn_openreq_child(struct tcp_sock *tp,
                                  const struct request_sock *req)
 {
-       tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0;
+       tcp_ecn_mode_set(tp, inet_rsk(req)->ecn_ok ?
+                            TCP_ECN_MODE_RFC3168 :
+                            TCP_ECN_DISABLED);
 }
 
 void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst)
index efd3cb5e1ded06d1a55c8fc0b230fab2e703b563..fffbd3ee44edc5991a0db02dfca1a9d4c73b8216 100644 (file)
@@ -325,7 +325,7 @@ static void tcp_ecn_send_synack(struct sock *sk, struct sk_buff *skb)
        const struct tcp_sock *tp = tcp_sk(sk);
 
        TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR;
-       if (!(tp->ecn_flags & TCP_ECN_OK))
+       if (tcp_ecn_disabled(tp))
                TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE;
        else if (tcp_ca_needs_ecn(sk) ||
                 tcp_bpf_ca_needs_ecn(sk))
@@ -351,7 +351,7 @@ static void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
 
        if (use_ecn) {
                TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR;
-               tp->ecn_flags = TCP_ECN_OK;
+               tcp_ecn_mode_set(tp, TCP_ECN_MODE_RFC3168);
                if (tcp_ca_needs_ecn(sk) || bpf_needs_ecn)
                        INET_ECN_xmit(sk);
        }
@@ -381,7 +381,7 @@ static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,
 {
        struct tcp_sock *tp = tcp_sk(sk);
 
-       if (tp->ecn_flags & TCP_ECN_OK) {
+       if (tcp_ecn_mode_rfc3168(tp)) {
                /* Not-retransmitted data segment: set ECT and inject CWR. */
                if (skb->len != tcp_header_len &&
                    !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {