Netdev List
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@google.com>
To: Eric Dumazet <edumazet@kernel.org>,
	Neal Cardwell <ncardwell@google.com>,
	 "David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	 Willem de Bruijn <willemb@google.com>
Cc: Simon Horman <horms@kernel.org>,
	Yuchung Cheng <ycheng@google.com>,
	 Kuniyuki Iwashima <kuniyu@google.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>,
	netdev@vger.kernel.org
Subject: [PATCH v1 net-next 08/10] tcp: Introduce Generic PLB.
Date: Fri,  4 Dec 2026 17:54:38 +0900	[thread overview]
Message-ID: <20261204085551.1518105-9-kuniyu@google.com> (raw)
In-Reply-To: <20261204085551.1518105-1-kuniyu@google.com>

Currently, PLB (Protective Load Balancing) relies on ECN signals
and is only hooked into DCTCP.  However, ECN is often unavailable
in cloud VM and virtualised network environments, and widely deployed
congestion control algorithms such as CUBIC and BBR cannot benefit
from PLB.

This commit introduces Generic PLB, a congestion-control-agnostic
PLB mechanism triggered by a combination of packet loss and low
delivery rate.

Generic PLB adds one per-netns sysctl knob:

  net.ipv4.tcp_plb_loss_rehash_prob (default: 128, range: 0 - 255)

Every 5th Fast Recovery, tcp_plb_check_rehash_loss() computes a
rehash probability in [0, 255] from rate_prob and floor, and
rehashes the flow via tcp_plb_rehash() if a random number in
[0, 254] is smaller than the probability:

  prob = max(rate_prob, floor)
  if (get_random_u32_below(255) < prob)
      tcp_plb_rehash(sk);

1. Rehash probability based on delivery rate regression (rate_prob)

   rate_prob is a repath probability calculation that reflects
   how much the current delivery rate (cur_rate) has regressed
   relative to tp->plb_max_rate, the highest delivery rate recorded
   in tcp_rate_gen():

     deficit = (plb_max_rate - cur_rate) / plb_max_rate
     rate_prob = min(255, 4 * base_prob * deficit^2)

   Squaring deficit suppresses noise churn under small jitter
   while quickly evacuating severely degraded paths.

   To filter burst noise and avoid 64-bit division on the ACK
   fast path, tcp_rate_gen() updates tp->plb_max_rate when a
   sample exceeds tp->plb_max_rate by more than 1/8, applying
   1/8 upward EWMA after the first sample.

2. Rehash probability floor (floor):

   rate_prob cannot trigger when a flow initially lands on a bad
   path and has not yet observed a healthy rate.  Thus, floor is
   used to explore alternative paths.

   For the first two rehashes (tp->plb_rehash < 2), floor is set
   to base_prob >> 1 (25% at default 128).  The cumulative
   rehash probability across n evaluations is:

     1 - (1 - p)^n

   With tcp_plb_loss_rehash_prob = 128, a flow rehashes by the
   20th Fast Recovery (n = 4) with ~68% probability:

     1 - (1 - 0.25)^4 = 0.6836

   Once the flow has rehashed twice (tp->plb_rehash >= 2), it
   likely already knows a healthy tp->plb_max_rate, where a 25%
   floor is too high and causes unnecessary churn.  Thus, floor
   is lowered to base_prob >> 3 (6.25%) thereafter.

3. Stale-hop peak rate decay (tp->plb_stale_hops):

   After repathing, keeping tp->plb_max_rate at a stale high value
   can inflate rehashes if all paths share lower bandwidth.
   Conversely, decaying tp->plb_max_rate on every single hop erodes
   good-path memory during short exploration on asymmetric ECMP.

   Therefore, tp->plb_stale_hops counts consecutive rehashes
   without recovery within ~15% of the peak (deficit < 38).
   Once three consecutive rehashes stay >= ~15% below
   tp->plb_max_rate, each subsequent stale rehash decays
   tp->plb_max_rate by 1/8 (to 7/8) so the flow adapts to the
   new paths.

Below are benchmark results with the script below [0] across a 4-way
IPv6 ECMP router topology (r0..r3, net.ipv6.fib_multipath_hash_policy
= 0) under three failure and weight-mismatch scenarios:

a. Single-link degradation with 3% random packet loss (base RTT 4 ms;
   r0..r2: 4 Gbps healthy, r3: 800 Mbps with 3% loss; 20 flows paced
   at 440 Mbps = 8.8 Gbps total demand; mean of 3 runs):

   Scenario                      Total(M)  Min(M)  Jain    Flows(r0..r3)    Retrans
   ----------------------------  --------  ------  ------  ---------------  -------
   20x CUBIC (PLB OFF)            7699.77   16.10  0.8925  5.7/5.3/6.7/2.7     1088
   20x CUBIC (Generic-PLB ON)     8578.08  313.25  0.9917  6.3/7.0/6.3/0.3      390
   20x BBR (PLB OFF)              8116.67  228.09  0.9704  4.7/6.0/6.3/3.0    18552
   20x BBR (Generic-PLB ON)       8643.48  424.69  0.9997  8.0/5.3/6.7/0.0      908
   20x DCTCP (PLB OFF)            7160.94   27.71  0.8308  4.7/5.3/6.0/4.0     1951
   20x DCTCP (Both PLB ON)        8700.88  415.98  0.9998  6.3/6.7/7.0/0.3      303
   10x CUBIC + 10x BBR (OFF)      7333.90    8.02  0.8873  6.0/5.0/5.0/4.0    16615
   10x CUBIC + 10x BBR (ON)       8563.66  364.49  0.9980  6.0/8.0/5.7/1.0     2816
   10x CUBIC + 10x DCTCP (OFF)    7036.39   24.59  0.8314  5.7/6.3/4.3/3.7     2023
   10x CUBIC + 10x DCTCP (ON)     8380.88  172.64  0.9745  5.7/6.7/7.0/0.7      656

   Generic PLB evacuates flows from the lossy path r3, recovering
   per-flow minimum goodput and reducing retransmissions by 64% - 95%.

b. Spine switch failure / ECMP hash-collapse (base RTT 4 ms; 8 switches
   in 4 pairs of 2.8 Gbps; one switch in pair r3 fails so r3 has
   1.4 Gbps capacity and half buffer with 0% random loss while still
   receiving 1/4 of ECMP hashes; 56 flows paced at 160 Mbps = 8.96 Gbps
   demand, 96% of surviving capacity; mean of 3 runs):

   Scenario                      Total(M)  Min(M)  Jain    Flows(r0..r3)        Retrans
   ----------------------------  --------  ------  ------  -------------------  -------
   56x CUBIC (PLB OFF)            7894.50   80.31  0.9584  14.0/14.3/12.3/12.3     5148
   56x CUBIC (Generic-PLB ON)     8604.32  143.26  0.9994  16.7/15.7/15.7/7.7      4041
   56x BBR (PLB OFF)              8576.75  146.80  0.9997  16.0/15.3/16.0/7.3     27681
   56x BBR (Generic-PLB ON)       8641.33  149.46  0.9998  15.3/16.3/16.3/7.7     14884
   56x DCTCP (PLB OFF)            7982.80   95.07  0.9642  14.7/14.7/15.0/11.7     6083
   56x DCTCP (Both PLB ON)        8632.26  144.06  0.9995  15.7/16.0/16.0/8.3      3502
   28x CUBIC + 28x BBR (OFF)      8149.27   94.77  0.9811  15.3/16.0/13.7/9.7      8480
   28x CUBIC + 28x BBR (ON)       8585.09  140.94  0.9993  16.7/15.0/15.7/7.3      5143
   28x CUBIC + 28x DCTCP (OFF)    7826.17   78.05  0.9643  11.7/18.0/14.0/12.3     6329
   28x CUBIC + 28x DCTCP (ON)     8623.33  147.09  0.9996  15.7/16.0/15.3/8.7      3857

   Generic PLB spreads flows in proportion to path capacity (converging
   towards the ideal 16/16/16/8 flow distribution across [2.8G, 2.8G,
   2.8G, 1.4G]), recovering minimum flow goodput and cutting
   retransmissions by 22% - 46%.

c. Inverted WCMP weight mismatch (2 ms return delay, HTB + bfifo with
   TSO/GSO/GRO enabled; 64 flows across r0..r3 with weights
   [1, 1, 2, 2] vs capacities [3.2G, 3.2G, 1.6G, 1.6G] for M=4, and
   [1, 1, 3, 3] vs [3.6G, 3.6G, 1.2G, 1.2G] for M=9, where
   M = (w_b / c_b) / (w_a / c_a) is the worst-to-best ratio of WCMP
   weight fraction w_i = weight_i / sum(weight) to capacity fraction
   c_i = cap_i / sum(cap); CUBIC unpaced, BBR unpaced for M=4 and
   paced at 120 Mbps for M=9; mean of 5 runs):

   Scenario                      Total(M)  Min(M)  Jain    Share(r0..r3 %)      Retrans
   ----------------------------  --------  ------  ------  -------------------  -------
   64x CUBIC M=4 (PLB OFF)        9049.36   57.59  0.6479  34.6/33.4/16.3/15.7    30246
   64x CUBIC M=4 (Generic-PLB)    9053.84   66.40  0.8984  34.5/33.5/16.3/15.7    29921
   64x BBR M=4 (PLB OFF)          9036.99   43.09  0.7037  34.8/33.5/16.2/15.5  1072387
   64x BBR M=4 (Generic-PLB)      9046.21   56.38  0.8062  34.7/33.5/16.2/15.5  1106217
   64x CUBIC M=9 (PLB OFF)        9054.20   36.43  0.4541  38.7/37.4/12.2/11.8    24724
   64x CUBIC M=9 (Generic-PLB)    9053.08   40.46  0.7532  38.7/37.5/12.2/11.7    28021
   64x BBR M=9 120M (PLB OFF)     4908.21   26.49  0.8216  25.9/29.5/22.8/21.8   104658
   64x BBR M=9 120M (Generic-PLB) 5748.63   31.60  0.8796  33.2/29.5/19.2/18.1     8310

   Under extreme M=4 and M=9 weight inversions, Generic PLB migrates
   excess flows off the overloaded r2/r3 paths towards the underutilised
   r0/r1 paths, improving per-flow Jain's index across both CUBIC and
   BBR (0.45 -> 0.75 on unpaced CUBIC M=9) and raising aggregate BBR
   goodput by +17.1% while cutting retransmissions by 92.1% on M=9.

Link: https://gist.github.com/q2ven/c04b56477dea4d1a58f5eadc4b07f9e0 #[0]
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 Documentation/networking/ip-sysctl.rst        | 19 ++++-
 .../net_cachelines/netns_ipv4_sysctl.rst      |  1 +
 .../networking/net_cachelines/tcp_sock.rst    |  3 +
 include/linux/tcp.h                           |  5 ++
 include/net/netns/ipv4.h                      |  3 +-
 include/net/tcp.h                             |  1 +
 net/core/net_namespace.c                      |  2 +
 net/ipv4/sysctl_net_ipv4.c                    |  7 ++
 net/ipv4/tcp.c                                |  4 ++
 net/ipv4/tcp_input.c                          | 15 ++++
 net/ipv4/tcp_ipv4.c                           |  1 +
 net/ipv4/tcp_plb.c                            | 69 +++++++++++++++++--
 12 files changed, 123 insertions(+), 7 deletions(-)

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index dffcec335a77..3046f6725b21 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -1360,8 +1360,8 @@ tcp_child_ehash_entries - INTEGER
 
 tcp_plb_enabled - BOOLEAN
 	If enabled and the underlying congestion control (e.g. DCTCP) supports
-	and enables PLB feature, TCP PLB (Protective Load Balancing) is
-	enabled. PLB is described in the following paper:
+	ECN-based PLB feature, TCP PLB (Protective Load Balancing) is enabled.
+	PLB is described in the following paper:
 	https://doi.org/10.1145/3544216.3544226. Based on PLB parameters,
 	upon sensing sustained congestion, TCP triggers a change in
 	flow label field for outgoing IPv6 packets. A change in flow label
@@ -1437,6 +1437,21 @@ tcp_plb_cong_thresh - INTEGER
 
 	Default: 128
 
+tcp_plb_loss_rehash_prob - INTEGER
+	Base probability for Generic PLB repathing, mapped from [0, 1]
+	to [0, 255] (e.g., 128 means 50%).  Setting this to 0 disables
+	Generic PLB.
+
+	Unlike tcp_plb_enabled which relies on ECN signals, Generic PLB
+	is congestion-control-agnostic and triggers repathing based on
+	packet loss and delivery rate regression relative to the flow's
+	recent peak delivery rate.  Larger tcp_plb_loss_rehash_prob
+	values make repathing more aggressive.
+
+	Possible Values: 0 - 255
+
+	Default: 128
+
 tcp_pingpong_thresh - INTEGER
 	The number of estimated data replies sent for estimated incoming data
 	requests that must happen before TCP considers that a connection is a
diff --git a/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst b/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
index 2833ce6cba1d..99cccfbb5444 100644
--- a/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
+++ b/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
@@ -136,6 +136,7 @@ u32                             tcp_challenge_count
 u8                              sysctl_tcp_plb_enabled
 u8                              sysctl_tcp_plb_idle_rehash_rounds
 u8                              sysctl_tcp_plb_rehash_rounds
+u8                              sysctl_tcp_plb_loss_rehash_prob                                  read_mostly         tcp_rate_gen
 u8                              sysctl_tcp_plb_suspend_rto_sec
 int                             sysctl_tcp_plb_cong_thresh
 int                             sysctl_udp_wmem_min
diff --git a/Documentation/networking/net_cachelines/tcp_sock.rst b/Documentation/networking/net_cachelines/tcp_sock.rst
index 6eb058478665..43fcfd0356c0 100644
--- a/Documentation/networking/net_cachelines/tcp_sock.rst
+++ b/Documentation/networking/net_cachelines/tcp_sock.rst
@@ -159,11 +159,14 @@ u8                            bpf_sock_ops_cb_flags
 u8:1                          bpf_chg_cc_inprogress
 u16                           timeout_rehash
 u8                            plb_consec_cong_rounds
+u8                            plb_loss_recoveries
+u8                            plb_stale_hops
 u32                           plb_pause_until
 u32                           rcv_ooopack
 u32                           rcv_rtt_last_tsecr
 struct                        rcv_rtt_est                                 read_write          tcp_rcv_space_adjust,tcp_rcv_established
 struct                        rcvq_space                                  read_write          tcp_rcv_space_adjust
+u64                           plb_max_rate                                read_write          tcp_rate_gen
 struct                        mtu_probe
 u32                           plb_rehash
 u32                           mtu_info
diff --git a/include/linux/tcp.h b/include/linux/tcp.h
index 3f275ac8e512..cdbd25e2a26b 100644
--- a/include/linux/tcp.h
+++ b/include/linux/tcp.h
@@ -379,6 +379,9 @@ struct tcp_sock {
 		u32	seq;
 		u64	time;
 	} rcvq_space;
+	u64	plb_max_rate;	/* PLB est. recent max rate
+				 * ((MSS packets / us) << 20)
+				 */
 	__cacheline_group_end(tcp_sock_write_rx);
 	/* End of Hot Path */
 
@@ -508,6 +511,8 @@ struct tcp_sock {
 
 	/* State for PLB (Protective Load Balancing) for a single TCP connection. */
 	u8	plb_consec_cong_rounds; /* consecutive congested rounds */
+	u8	plb_loss_recoveries;	/* fast recoveries since last PLB check */
+	u8	plb_stale_hops;		/* consecutive degraded rehashes */
 	u32	plb_pause_until; /* jiffies32 when PLB can resume rerouting */
 
 	u32 rcv_ooopack; /* Received out-of-order packets, for tcpinfo */
diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
index 3efbc7f4b4f7..b225f30580f9 100644
--- a/include/net/netns/ipv4.h
+++ b/include/net/netns/ipv4.h
@@ -83,7 +83,8 @@ struct netns_ipv4 {
 	u8 sysctl_ip_early_demux;
 	u8 sysctl_tcp_early_demux;
 	u8 sysctl_tcp_l3mdev_accept;
-	/* 3 bytes hole, try to pack */
+	u8 sysctl_tcp_plb_loss_rehash_prob;
+	/* 1 bytes hole, try to pack */
 	int sysctl_tcp_reordering;
 	int sysctl_tcp_rmem[3];
 	int sysctl_tcp_rcvbuf_low_rtt;
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 7e53293ed3c8..a956ed173e70 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2737,6 +2737,7 @@ extern void tcp_rack_reo_timeout(struct sock *sk);
 
 void tcp_plb_update_state(struct sock *sk, const int cong_ratio);
 void tcp_plb_check_rehash_ecn(struct sock *sk);
+void tcp_plb_check_rehash_loss(struct sock *sk);
 void tcp_plb_update_state_upon_rto(struct sock *sk);
 
 static inline void tcp_warn_once(const struct sock *sk, bool cond, const char *str)
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 217021eeab13..e12de8cf7707 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -1252,6 +1252,8 @@ static void __init netns_ipv4_struct_check(void)
 				      sysctl_tcp_early_demux);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
 				      sysctl_tcp_l3mdev_accept);
+	CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
+				      sysctl_tcp_plb_loss_rehash_prob);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
 				      sysctl_tcp_reordering);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
index fb0f3124836d..f99852d3fda4 100644
--- a/net/ipv4/sysctl_net_ipv4.c
+++ b/net/ipv4/sysctl_net_ipv4.c
@@ -1598,6 +1598,13 @@ static const struct ctl_table ipv4_net_table[] = {
 		.proc_handler   = proc_dou8vec_minmax,
 		.extra2         = &tcp_plb_max_rounds,
 	},
+	{
+		.procname	= "tcp_plb_loss_rehash_prob",
+		.data		= &init_net.ipv4.sysctl_tcp_plb_loss_rehash_prob,
+		.maxlen		= sizeof(u8),
+		.mode		= 0644,
+		.proc_handler	= proc_dou8vec_minmax,
+	},
 	{
 		.procname       = "tcp_plb_suspend_rto_sec",
 		.data           = &init_net.ipv4.sysctl_tcp_plb_suspend_rto_sec,
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 002eaca58c46..aa18d018f2eb 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -3487,8 +3487,11 @@ int tcp_disconnect(struct sock *sk, int flags)
 	tp->sacked_out = 0;
 	tp->tlp_high_seq = 0;
 	tp->last_oow_ack_time = 0;
+	tp->plb_max_rate = 0;
 	tp->plb_rehash = 0;
 	tp->plb_consec_cong_rounds = 0;
+	tp->plb_loss_recoveries = 0;
+	tp->plb_stale_hops = 0;
 	tp->plb_pause_until = 0;
 	/* There's a bubble in the pipe until at least the first ACK. */
 	tp->app_limited = ~0U;
@@ -5330,6 +5333,7 @@ static void __init tcp_struct_check(void)
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcv_collapse_credit);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcv_rtt_est);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, rcvq_space);
+	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_rx, plb_max_rate);
 }
 
 void __init tcp_init(void)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 975e6501aef8..8902103ab932 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -1831,6 +1831,18 @@ static void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
 		tp->rate_delivered = rs->delivered;
 		tp->rate_interval_us = rs->interval_us;
 		tp->rate_app_limited = rs->is_app_limited;
+
+		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_plb_loss_rehash_prob) &&
+		    (u64)rs->delivered << 20 >
+		    (u64)(tp->plb_max_rate + (tp->plb_max_rate >> 3)) * rs->interval_us) {
+			u64 new_rate;
+
+			new_rate = div64_ul((u64)rs->delivered << 20, rs->interval_us);
+			if (!tp->plb_max_rate)
+				tp->plb_max_rate = new_rate;
+			else
+				tp->plb_max_rate += (new_rate - tp->plb_max_rate) >> 3;
+		}
 	}
 }
 
@@ -3235,6 +3247,8 @@ void tcp_enter_recovery(struct sock *sk, bool ece_ack)
 		tcp_init_cwnd_reduction(sk);
 	}
 	tcp_set_ca_state(sk, TCP_CA_Recovery);
+
+	tcp_plb_check_rehash_loss(sk);
 }
 
 static void tcp_update_rto_time(struct tcp_sock *tp)
@@ -4201,6 +4215,7 @@ static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
 		tcp_try_keep_open(sk);
 		NET_INC_STATS(sock_net(sk),
 				LINUX_MIB_TCPLOSSPROBERECOVERY);
+		tcp_plb_check_rehash_loss(sk);
 	} else if (!(flag & (FLAG_SND_UNA_ADVANCED |
 			     FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
 		/* Pure dupack: original and TLP probe arrived; no loss */
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 14ba61845f49..05c916a3f34e 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -3531,6 +3531,7 @@ static int __net_init tcp_sk_init(struct net *net)
 	net->ipv4.sysctl_tcp_plb_suspend_rto_sec = 5;
 	/* Default congestion threshold for PLB to mark a round is 50% */
 	net->ipv4.sysctl_tcp_plb_cong_thresh = (1 << TCP_PLB_SCALE) / 2;
+	net->ipv4.sysctl_tcp_plb_loss_rehash_prob = 128;
 
 	/* Reno is always built in */
 	if (!net_eq(net, &init_net) &&
diff --git a/net/ipv4/tcp_plb.c b/net/ipv4/tcp_plb.c
index 794bddc4dc2d..324bcb27f540 100644
--- a/net/ipv4/tcp_plb.c
+++ b/net/ipv4/tcp_plb.c
@@ -41,7 +41,7 @@ void tcp_plb_update_state(struct sock *sk, const int cong_ratio)
 }
 EXPORT_SYMBOL_GPL(tcp_plb_update_state);
 
-static void tcp_plb_rehash(struct sock *sk)
+static bool tcp_plb_rehash(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 	struct net *net = sock_net(sk);
@@ -59,14 +59,16 @@ static void tcp_plb_rehash(struct sock *sk)
 		tp->plb_pause_until = 0;
 
 	if (tp->plb_pause_until)
-		return;
+		return false;
 
 	if (!__sk_rethink_txhash_reset_dst(sk))
-		return;
+		return false;
 
 	tp->plb_consec_cong_rounds = 0;
+	tp->plb_loss_recoveries = 0;
 	WRITE_ONCE(tp->plb_rehash, tp->plb_rehash + 1);
 	NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPLBREHASH);
+	return true;
 }
 
 /* Check whether recent congestion has been persistent enough to warrant
@@ -96,6 +98,63 @@ void tcp_plb_check_rehash_ecn(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(tcp_plb_check_rehash_ecn);
 
+void tcp_plb_check_rehash_loss(struct sock *sk)
+{
+	u32 base_prob, rate_prob, floor, prob;
+	struct tcp_sock *tp = tcp_sk(sk);
+	struct net *net = sock_net(sk);
+	u64 cur_rate, max_rate;
+
+	base_prob = READ_ONCE(net->ipv4.sysctl_tcp_plb_loss_rehash_prob);
+	if (!base_prob)
+		return;
+
+	if (++tp->plb_loss_recoveries < 5)
+		return;
+
+	tp->plb_loss_recoveries = 0;
+
+	/* self-normalise MSS-per-sec rate, using the highest
+	 * observed rate.  More regression, more rehash.
+	 */
+	rate_prob = 0;
+	cur_rate = (u64)tp->rate_delivered << 20;
+	max_rate = tp->plb_max_rate * tp->rate_interval_us;
+	if (max_rate > cur_rate) {
+		u64 deficit;
+
+		deficit = mul_u64_u64_div_u64(max_rate - cur_rate,
+					      1 << 8, max_rate);
+		rate_prob = (4 * base_prob * deficit * deficit) >> 16;
+		rate_prob = min_t(u32, rate_prob, 255);
+
+		/* Don't decay plb_max_rate if the regression is
+		 * under 15% (38/256).
+		 */
+		if (deficit < 38)
+			tp->plb_stale_hops = 0;
+	} else {
+		tp->plb_stale_hops = 0;
+	}
+
+	if (tp->plb_rehash < 2)
+		floor = base_prob >> 1;
+	else
+		floor = base_prob >> 3;
+
+	prob = max(rate_prob, floor);
+	if (get_random_u32_below(255) >= prob)
+		return;
+
+	if (!tcp_plb_rehash(sk))
+		return;
+
+	if (tp->plb_stale_hops < 3)
+		tp->plb_stale_hops++;
+	if (tp->plb_stale_hops == 3)
+		tp->plb_max_rate -= tp->plb_max_rate >> 3;
+}
+
 /* Upon RTO, disallow load balancing for a while, to avoid having load
  * balancing decisions switch traffic to a black-holed path that was
  * previously avoided with a sk_rethink_txhash() call at RTO time.
@@ -106,7 +165,8 @@ void tcp_plb_update_state_upon_rto(struct sock *sk)
 	struct net *net = sock_net(sk);
 	u32 pause;
 
-	if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled))
+	if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled) &&
+	    !READ_ONCE(net->ipv4.sysctl_tcp_plb_loss_rehash_prob))
 		return;
 
 	pause = READ_ONCE(net->ipv4.sysctl_tcp_plb_suspend_rto_sec) * HZ;
@@ -120,4 +180,5 @@ void tcp_plb_update_state_upon_rto(struct sock *sk)
 	 * congestion characteristics.
 	 */
 	tp->plb_consec_cong_rounds = 0;
+	tp->plb_loss_recoveries = 0;
 }
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-04  8:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  8:54 [PATCH v1 net-next 00/10] tcp: Introduce Generic PLB Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 01/10] tcp: Return early when tcp_plb_check_rehash() does not rehash a flow Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 02/10] tcp: Don't call get_random_u32_below(0) in tcp_plb_update_state_upon_rto() Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 03/10] tcp: Move struct tcp_plb_state to struct tcp_sock Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 04/10] tcp: Call tcp_plb_update_state_upon_rto() after rehash in tcp_write_timeout() Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 05/10] tcp: Rename tcp_plb_check_rehash() to tcp_plb_check_rehash_ecn() Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 06/10] tcp: Factorise tcp_plb_rehash() Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 07/10] tcp: Lower tcp_plb_suspend_rto_sec default Kuniyuki Iwashima
2026-12-04  8:54 ` Kuniyuki Iwashima [this message]
2026-12-04  8:54 ` [PATCH v1 net-next 09/10] selftest: packetdrill: Add tests for Generic PLB Kuniyuki Iwashima
2026-12-04  8:54 ` [PATCH v1 net-next 10/10] selftest: net: Disable Generic PLB in ecmp_rehash.sh Kuniyuki Iwashima
2026-12-04  9:03 ` [PATCH v1 net-next 00/10] tcp: Introduce Generic PLB netdev-bot+sinfo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204085551.1518105-9-kuniyu@google.com \
    --to=kuniyu@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    --cc=ycheng@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox