162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * YeAH TCP 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * For further details look at: 762306a36Sopenharmony_ci * https://web.archive.org/web/20080316215752/http://wil.cs.caltech.edu/pfldnet2007/paper/YeAH_TCP.pdf 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci#include <linux/mm.h> 1162306a36Sopenharmony_ci#include <linux/module.h> 1262306a36Sopenharmony_ci#include <linux/skbuff.h> 1362306a36Sopenharmony_ci#include <linux/inet_diag.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <net/tcp.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include "tcp_vegas.h" 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define TCP_YEAH_ALPHA 80 /* number of packets queued at the bottleneck */ 2062306a36Sopenharmony_ci#define TCP_YEAH_GAMMA 1 /* fraction of queue to be removed per rtt */ 2162306a36Sopenharmony_ci#define TCP_YEAH_DELTA 3 /* log minimum fraction of cwnd to be removed on loss */ 2262306a36Sopenharmony_ci#define TCP_YEAH_EPSILON 1 /* log maximum fraction to be removed on early decongestion */ 2362306a36Sopenharmony_ci#define TCP_YEAH_PHY 8 /* maximum delta from base */ 2462306a36Sopenharmony_ci#define TCP_YEAH_RHO 16 /* minimum number of consecutive rtt to consider competition on loss */ 2562306a36Sopenharmony_ci#define TCP_YEAH_ZETA 50 /* minimum number of state switches to reset reno_count */ 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#define TCP_SCALABLE_AI_CNT 100U 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci/* YeAH variables */ 3062306a36Sopenharmony_cistruct yeah { 3162306a36Sopenharmony_ci struct vegas vegas; /* must be first */ 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci /* YeAH */ 3462306a36Sopenharmony_ci u32 lastQ; 3562306a36Sopenharmony_ci u32 doing_reno_now; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci u32 reno_count; 3862306a36Sopenharmony_ci u32 fast_count; 3962306a36Sopenharmony_ci}; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic void tcp_yeah_init(struct sock *sk) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 4462306a36Sopenharmony_ci struct yeah *yeah = inet_csk_ca(sk); 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci tcp_vegas_init(sk); 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci yeah->doing_reno_now = 0; 4962306a36Sopenharmony_ci yeah->lastQ = 0; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci yeah->reno_count = 2; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci /* Ensure the MD arithmetic works. This is somewhat pedantic, 5462306a36Sopenharmony_ci * since I don't think we will see a cwnd this large. :) */ 5562306a36Sopenharmony_ci tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128); 5662306a36Sopenharmony_ci} 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_cistatic void tcp_yeah_cong_avoid(struct sock *sk, u32 ack, u32 acked) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 6162306a36Sopenharmony_ci struct yeah *yeah = inet_csk_ca(sk); 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci if (!tcp_is_cwnd_limited(sk)) 6462306a36Sopenharmony_ci return; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci if (tcp_in_slow_start(tp)) { 6762306a36Sopenharmony_ci acked = tcp_slow_start(tp, acked); 6862306a36Sopenharmony_ci if (!acked) 6962306a36Sopenharmony_ci goto do_vegas; 7062306a36Sopenharmony_ci } 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci if (!yeah->doing_reno_now) { 7362306a36Sopenharmony_ci /* Scalable */ 7462306a36Sopenharmony_ci tcp_cong_avoid_ai(tp, min(tcp_snd_cwnd(tp), TCP_SCALABLE_AI_CNT), 7562306a36Sopenharmony_ci acked); 7662306a36Sopenharmony_ci } else { 7762306a36Sopenharmony_ci /* Reno */ 7862306a36Sopenharmony_ci tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked); 7962306a36Sopenharmony_ci } 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci /* The key players are v_vegas.beg_snd_una and v_beg_snd_nxt. 8262306a36Sopenharmony_ci * 8362306a36Sopenharmony_ci * These are so named because they represent the approximate values 8462306a36Sopenharmony_ci * of snd_una and snd_nxt at the beginning of the current RTT. More 8562306a36Sopenharmony_ci * precisely, they represent the amount of data sent during the RTT. 8662306a36Sopenharmony_ci * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt, 8762306a36Sopenharmony_ci * we will calculate that (v_beg_snd_nxt - v_vegas.beg_snd_una) outstanding 8862306a36Sopenharmony_ci * bytes of data have been ACKed during the course of the RTT, giving 8962306a36Sopenharmony_ci * an "actual" rate of: 9062306a36Sopenharmony_ci * 9162306a36Sopenharmony_ci * (v_beg_snd_nxt - v_vegas.beg_snd_una) / (rtt duration) 9262306a36Sopenharmony_ci * 9362306a36Sopenharmony_ci * Unfortunately, v_vegas.beg_snd_una is not exactly equal to snd_una, 9462306a36Sopenharmony_ci * because delayed ACKs can cover more than one segment, so they 9562306a36Sopenharmony_ci * don't line up yeahly with the boundaries of RTTs. 9662306a36Sopenharmony_ci * 9762306a36Sopenharmony_ci * Another unfortunate fact of life is that delayed ACKs delay the 9862306a36Sopenharmony_ci * advance of the left edge of our send window, so that the number 9962306a36Sopenharmony_ci * of bytes we send in an RTT is often less than our cwnd will allow. 10062306a36Sopenharmony_ci * So we keep track of our cwnd separately, in v_beg_snd_cwnd. 10162306a36Sopenharmony_ci */ 10262306a36Sopenharmony_cido_vegas: 10362306a36Sopenharmony_ci if (after(ack, yeah->vegas.beg_snd_nxt)) { 10462306a36Sopenharmony_ci /* We do the Vegas calculations only if we got enough RTT 10562306a36Sopenharmony_ci * samples that we can be reasonably sure that we got 10662306a36Sopenharmony_ci * at least one RTT sample that wasn't from a delayed ACK. 10762306a36Sopenharmony_ci * If we only had 2 samples total, 10862306a36Sopenharmony_ci * then that means we're getting only 1 ACK per RTT, which 10962306a36Sopenharmony_ci * means they're almost certainly delayed ACKs. 11062306a36Sopenharmony_ci * If we have 3 samples, we should be OK. 11162306a36Sopenharmony_ci */ 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci if (yeah->vegas.cntRTT > 2) { 11462306a36Sopenharmony_ci u32 rtt, queue; 11562306a36Sopenharmony_ci u64 bw; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci /* We have enough RTT samples, so, using the Vegas 11862306a36Sopenharmony_ci * algorithm, we determine if we should increase or 11962306a36Sopenharmony_ci * decrease cwnd, and by how much. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci /* Pluck out the RTT we are using for the Vegas 12362306a36Sopenharmony_ci * calculations. This is the min RTT seen during the 12462306a36Sopenharmony_ci * last RTT. Taking the min filters out the effects 12562306a36Sopenharmony_ci * of delayed ACKs, at the cost of noticing congestion 12662306a36Sopenharmony_ci * a bit later. 12762306a36Sopenharmony_ci */ 12862306a36Sopenharmony_ci rtt = yeah->vegas.minRTT; 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci /* Compute excess number of packets above bandwidth 13162306a36Sopenharmony_ci * Avoid doing full 64 bit divide. 13262306a36Sopenharmony_ci */ 13362306a36Sopenharmony_ci bw = tcp_snd_cwnd(tp); 13462306a36Sopenharmony_ci bw *= rtt - yeah->vegas.baseRTT; 13562306a36Sopenharmony_ci do_div(bw, rtt); 13662306a36Sopenharmony_ci queue = bw; 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci if (queue > TCP_YEAH_ALPHA || 13962306a36Sopenharmony_ci rtt - yeah->vegas.baseRTT > (yeah->vegas.baseRTT / TCP_YEAH_PHY)) { 14062306a36Sopenharmony_ci if (queue > TCP_YEAH_ALPHA && 14162306a36Sopenharmony_ci tcp_snd_cwnd(tp) > yeah->reno_count) { 14262306a36Sopenharmony_ci u32 reduction = min(queue / TCP_YEAH_GAMMA , 14362306a36Sopenharmony_ci tcp_snd_cwnd(tp) >> TCP_YEAH_EPSILON); 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) - reduction); 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp), 14862306a36Sopenharmony_ci yeah->reno_count)); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci tp->snd_ssthresh = tcp_snd_cwnd(tp); 15162306a36Sopenharmony_ci } 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci if (yeah->reno_count <= 2) 15462306a36Sopenharmony_ci yeah->reno_count = max(tcp_snd_cwnd(tp)>>1, 2U); 15562306a36Sopenharmony_ci else 15662306a36Sopenharmony_ci yeah->reno_count++; 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci yeah->doing_reno_now = min(yeah->doing_reno_now + 1, 15962306a36Sopenharmony_ci 0xffffffU); 16062306a36Sopenharmony_ci } else { 16162306a36Sopenharmony_ci yeah->fast_count++; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci if (yeah->fast_count > TCP_YEAH_ZETA) { 16462306a36Sopenharmony_ci yeah->reno_count = 2; 16562306a36Sopenharmony_ci yeah->fast_count = 0; 16662306a36Sopenharmony_ci } 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci yeah->doing_reno_now = 0; 16962306a36Sopenharmony_ci } 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci yeah->lastQ = queue; 17262306a36Sopenharmony_ci } 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci /* Save the extent of the current window so we can use this 17562306a36Sopenharmony_ci * at the end of the next RTT. 17662306a36Sopenharmony_ci */ 17762306a36Sopenharmony_ci yeah->vegas.beg_snd_una = yeah->vegas.beg_snd_nxt; 17862306a36Sopenharmony_ci yeah->vegas.beg_snd_nxt = tp->snd_nxt; 17962306a36Sopenharmony_ci yeah->vegas.beg_snd_cwnd = tcp_snd_cwnd(tp); 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci /* Wipe the slate clean for the next RTT. */ 18262306a36Sopenharmony_ci yeah->vegas.cntRTT = 0; 18362306a36Sopenharmony_ci yeah->vegas.minRTT = 0x7fffffff; 18462306a36Sopenharmony_ci } 18562306a36Sopenharmony_ci} 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_cistatic u32 tcp_yeah_ssthresh(struct sock *sk) 18862306a36Sopenharmony_ci{ 18962306a36Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 19062306a36Sopenharmony_ci struct yeah *yeah = inet_csk_ca(sk); 19162306a36Sopenharmony_ci u32 reduction; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci if (yeah->doing_reno_now < TCP_YEAH_RHO) { 19462306a36Sopenharmony_ci reduction = yeah->lastQ; 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci reduction = min(reduction, max(tcp_snd_cwnd(tp)>>1, 2U)); 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci reduction = max(reduction, tcp_snd_cwnd(tp) >> TCP_YEAH_DELTA); 19962306a36Sopenharmony_ci } else 20062306a36Sopenharmony_ci reduction = max(tcp_snd_cwnd(tp)>>1, 2U); 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci yeah->fast_count = 0; 20362306a36Sopenharmony_ci yeah->reno_count = max(yeah->reno_count>>1, 2U); 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci return max_t(int, tcp_snd_cwnd(tp) - reduction, 2); 20662306a36Sopenharmony_ci} 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_cistatic struct tcp_congestion_ops tcp_yeah __read_mostly = { 20962306a36Sopenharmony_ci .init = tcp_yeah_init, 21062306a36Sopenharmony_ci .ssthresh = tcp_yeah_ssthresh, 21162306a36Sopenharmony_ci .undo_cwnd = tcp_reno_undo_cwnd, 21262306a36Sopenharmony_ci .cong_avoid = tcp_yeah_cong_avoid, 21362306a36Sopenharmony_ci .set_state = tcp_vegas_state, 21462306a36Sopenharmony_ci .cwnd_event = tcp_vegas_cwnd_event, 21562306a36Sopenharmony_ci .get_info = tcp_vegas_get_info, 21662306a36Sopenharmony_ci .pkts_acked = tcp_vegas_pkts_acked, 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci .owner = THIS_MODULE, 21962306a36Sopenharmony_ci .name = "yeah", 22062306a36Sopenharmony_ci}; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_cistatic int __init tcp_yeah_register(void) 22362306a36Sopenharmony_ci{ 22462306a36Sopenharmony_ci BUILD_BUG_ON(sizeof(struct yeah) > ICSK_CA_PRIV_SIZE); 22562306a36Sopenharmony_ci tcp_register_congestion_control(&tcp_yeah); 22662306a36Sopenharmony_ci return 0; 22762306a36Sopenharmony_ci} 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_cistatic void __exit tcp_yeah_unregister(void) 23062306a36Sopenharmony_ci{ 23162306a36Sopenharmony_ci tcp_unregister_congestion_control(&tcp_yeah); 23262306a36Sopenharmony_ci} 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_cimodule_init(tcp_yeah_register); 23562306a36Sopenharmony_cimodule_exit(tcp_yeah_unregister); 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ciMODULE_AUTHOR("Angelo P. Castellani"); 23862306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 23962306a36Sopenharmony_ciMODULE_DESCRIPTION("YeAH TCP"); 240