tcp_plb.c 3.7 KB

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