162306a36Sopenharmony_ci/* Protective Load Balancing (PLB) 262306a36Sopenharmony_ci * 362306a36Sopenharmony_ci * PLB was designed to reduce link load imbalance across datacenter 462306a36Sopenharmony_ci * switches. PLB is a host-based optimization; it leverages congestion 562306a36Sopenharmony_ci * signals from the transport layer to randomly change the path of the 662306a36Sopenharmony_ci * connection experiencing sustained congestion. PLB prefers to repath 762306a36Sopenharmony_ci * after idle periods to minimize packet reordering. It repaths by 862306a36Sopenharmony_ci * changing the IPv6 Flow Label on the packets of a connection, which 962306a36Sopenharmony_ci * datacenter switches include as part of ECMP/WCMP hashing. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * PLB is described in detail in: 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * Mubashir Adnan Qureshi, Yuchung Cheng, Qianwen Yin, Qiaobin Fu, 1462306a36Sopenharmony_ci * Gautam Kumar, Masoud Moshref, Junhua Yan, Van Jacobson, 1562306a36Sopenharmony_ci * David Wetherall,Abdul Kabbani: 1662306a36Sopenharmony_ci * "PLB: Congestion Signals are Simple and Effective for 1762306a36Sopenharmony_ci * Network Load Balancing" 1862306a36Sopenharmony_ci * In ACM SIGCOMM 2022, Amsterdam Netherlands. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci */ 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#include <net/tcp.h> 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci/* Called once per round-trip to update PLB state for a connection. */ 2562306a36Sopenharmony_civoid tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb, 2662306a36Sopenharmony_ci const int cong_ratio) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci struct net *net = sock_net(sk); 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)) 3162306a36Sopenharmony_ci return; 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci if (cong_ratio >= 0) { 3462306a36Sopenharmony_ci if (cong_ratio < READ_ONCE(net->ipv4.sysctl_tcp_plb_cong_thresh)) 3562306a36Sopenharmony_ci plb->consec_cong_rounds = 0; 3662306a36Sopenharmony_ci else if (plb->consec_cong_rounds < 3762306a36Sopenharmony_ci READ_ONCE(net->ipv4.sysctl_tcp_plb_rehash_rounds)) 3862306a36Sopenharmony_ci plb->consec_cong_rounds++; 3962306a36Sopenharmony_ci } 4062306a36Sopenharmony_ci} 4162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_plb_update_state); 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci/* Check whether recent congestion has been persistent enough to warrant 4462306a36Sopenharmony_ci * a load balancing decision that switches the connection to another path. 4562306a36Sopenharmony_ci */ 4662306a36Sopenharmony_civoid tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb) 4762306a36Sopenharmony_ci{ 4862306a36Sopenharmony_ci struct net *net = sock_net(sk); 4962306a36Sopenharmony_ci u32 max_suspend; 5062306a36Sopenharmony_ci bool forced_rehash = false, idle_rehash = false; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)) 5362306a36Sopenharmony_ci return; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci forced_rehash = plb->consec_cong_rounds >= 5662306a36Sopenharmony_ci READ_ONCE(net->ipv4.sysctl_tcp_plb_rehash_rounds); 5762306a36Sopenharmony_ci /* If sender goes idle then we check whether to rehash. */ 5862306a36Sopenharmony_ci idle_rehash = READ_ONCE(net->ipv4.sysctl_tcp_plb_idle_rehash_rounds) && 5962306a36Sopenharmony_ci !tcp_sk(sk)->packets_out && 6062306a36Sopenharmony_ci plb->consec_cong_rounds >= 6162306a36Sopenharmony_ci READ_ONCE(net->ipv4.sysctl_tcp_plb_idle_rehash_rounds); 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci if (!forced_rehash && !idle_rehash) 6462306a36Sopenharmony_ci return; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci /* Note that tcp_jiffies32 can wrap; we detect wraps by checking for 6762306a36Sopenharmony_ci * cases where the max suspension end is before the actual suspension 6862306a36Sopenharmony_ci * end. We clear pause_until to 0 to indicate there is no recent 6962306a36Sopenharmony_ci * RTO event that constrains PLB rehashing. 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_ci max_suspend = 2 * READ_ONCE(net->ipv4.sysctl_tcp_plb_suspend_rto_sec) * HZ; 7262306a36Sopenharmony_ci if (plb->pause_until && 7362306a36Sopenharmony_ci (!before(tcp_jiffies32, plb->pause_until) || 7462306a36Sopenharmony_ci before(tcp_jiffies32 + max_suspend, plb->pause_until))) 7562306a36Sopenharmony_ci plb->pause_until = 0; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci if (plb->pause_until) 7862306a36Sopenharmony_ci return; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci sk_rethink_txhash(sk); 8162306a36Sopenharmony_ci plb->consec_cong_rounds = 0; 8262306a36Sopenharmony_ci tcp_sk(sk)->plb_rehash++; 8362306a36Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPLBREHASH); 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_plb_check_rehash); 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci/* Upon RTO, disallow load balancing for a while, to avoid having load 8862306a36Sopenharmony_ci * balancing decisions switch traffic to a black-holed path that was 8962306a36Sopenharmony_ci * previously avoided with a sk_rethink_txhash() call at RTO time. 9062306a36Sopenharmony_ci */ 9162306a36Sopenharmony_civoid tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb) 9262306a36Sopenharmony_ci{ 9362306a36Sopenharmony_ci struct net *net = sock_net(sk); 9462306a36Sopenharmony_ci u32 pause; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci if (!READ_ONCE(net->ipv4.sysctl_tcp_plb_enabled)) 9762306a36Sopenharmony_ci return; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci pause = READ_ONCE(net->ipv4.sysctl_tcp_plb_suspend_rto_sec) * HZ; 10062306a36Sopenharmony_ci pause += get_random_u32_below(pause); 10162306a36Sopenharmony_ci plb->pause_until = tcp_jiffies32 + pause; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci /* Reset PLB state upon RTO, since an RTO causes a sk_rethink_txhash() call 10462306a36Sopenharmony_ci * that may switch this connection to a path with completely different 10562306a36Sopenharmony_ci * congestion characteristics. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci plb->consec_cong_rounds = 0; 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_plb_update_state_upon_rto); 110