1 // SPDX-License-Identifier: GPL-2.0 2 /* Protective Load Balancing (PLB) 3 * 4 * PLB was designed to reduce link load imbalance across datacenter 5 * switches. PLB is a host-based optimization; it leverages congestion 6 * signals from the transport layer to randomly change the path of the 7 * connection experiencing sustained congestion. PLB prefers to repath 8 * after idle periods to minimize packet reordering. It repaths by 9 * changing the IPv6 Flow Label on the packets of a connection, which 10 * datacenter switches include as part of ECMP/WCMP hashing. 11 * 12 * PLB is described in detail in: 13 * 14 * Mubashir Adnan Qureshi, Yuchung Cheng, Qianwen Yin, Qiaobin Fu, 15 * Gautam Kumar, Masoud Moshref, Junhua Yan, Van Jacobson, 16 * David Wetherall,Abdul Kabbani: 17 * "PLB: Congestion Signals are Simple and Effective for 18 * Network Load Balancing" 19 * In ACM SIGCOMM 2022, Amsterdam Netherlands. 20 * 21 */ 22 23 #include <net/tcp.h> 24 25 /* Called once per round-trip to update PLB state for a connection. */ 26 void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb, 27 const int cong_ratio) 28 { 29 struct net *net = sock_net(sk); 30 31 if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)) 32 return; 33 34 if (cong_ratio >= 0) { 35 if (cong_ratio < READ_ONCE(net->ipv4.sysctl_tcp_plb_cong_thresh)) 36 plb->consec_cong_rounds = 0; 37 else if (plb->consec_cong_rounds < 38 READ_ONCE(net->ipv4.sysctl_tcp_plb_rehash_rounds)) 39 plb->consec_cong_rounds++; 40 } 41 } 42 EXPORT_SYMBOL_GPL(tcp_plb_update_state); 43 44 /* Check whether recent congestion has been persistent enough to warrant 45 * a load balancing decision that switches the connection to another path. 46 */ 47 void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb) 48 { 49 struct net *net = sock_net(sk); 50 u32 max_suspend; 51 bool forced_rehash = false, idle_rehash = false; 52 53 if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)) 54 return; 55 56 forced_rehash = plb->consec_cong_rounds >= 57 READ_ONCE(net->ipv4.sysctl_tcp_plb_rehash_rounds); 58 /* If sender goes idle then we check whether to rehash. */ 59 idle_rehash = READ_ONCE(net->ipv4.sysctl_tcp_plb_idle_rehash_rounds) && 60 !tcp_sk(sk)->packets_out && 61 plb->consec_cong_rounds >= 62 READ_ONCE(net->ipv4.sysctl_tcp_plb_idle_rehash_rounds); 63 64 if (!forced_rehash && !idle_rehash) 65 return; 66 67 /* Note that tcp_jiffies32 can wrap; we detect wraps by checking for 68 * cases where the max suspension end is before the actual suspension 69 * end. We clear pause_until to 0 to indicate there is no recent 70 * RTO event that constrains PLB rehashing. 71 */ 72 max_suspend = 2 * READ_ONCE(net->ipv4.sysctl_tcp_plb_suspend_rto_sec) * HZ; 73 if (plb->pause_until && 74 (!before(tcp_jiffies32, plb->pause_until) || 75 before(tcp_jiffies32 + max_suspend, plb->pause_until))) 76 plb->pause_until = 0; 77 78 if (plb->pause_until) 79 return; 80 81 sk_rethink_txhash(sk); 82 plb->consec_cong_rounds = 0; 83 tcp_sk(sk)->plb_rehash++; 84 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPLBREHASH); 85 } 86 EXPORT_SYMBOL_GPL(tcp_plb_check_rehash); 87 88 /* Upon RTO, disallow load balancing for a while, to avoid having load 89 * balancing decisions switch traffic to a black-holed path that was 90 * previously avoided with a sk_rethink_txhash() call at RTO time. 91 */ 92 void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb) 93 { 94 struct net *net = sock_net(sk); 95 u32 pause; 96 97 if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)) 98 return; 99 100 pause = READ_ONCE(net->ipv4.sysctl_tcp_plb_suspend_rto_sec) * HZ; 101 pause += get_random_u32_below(pause); 102 plb->pause_until = tcp_jiffies32 + pause; 103 104 /* Reset PLB state upon RTO, since an RTO causes a sk_rethink_txhash() call 105 * that may switch this connection to a path with completely different 106 * congestion characteristics. 107 */ 108 plb->consec_cong_rounds = 0; 109 } 110 EXPORT_SYMBOL_GPL(tcp_plb_update_state_upon_rto); 111