^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * CAIA Delay-Gradient (CDG) congestion control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This implementation is based on the paper:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * D.A. Hayes and G. Armitage. "Revisiting TCP congestion control using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * delay gradients." In IFIP Networking, pages 328-341. Springer, 2011.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Scavenger traffic (Less-than-Best-Effort) should disable coexistence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * heuristics using parameters use_shadow=0 and use_ineff=0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Parameters window, backoff_beta, and backoff_factor are crucial for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * throughput and delay. Future work is needed to determine better defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * and to provide guidelines for use in different environments/contexts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Except for window, knobs are configured via /sys/module/tcp_cdg/parameters/.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Parameter window is only configurable when loading tcp_cdg as a module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Notable differences from paper/FreeBSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * o Using Hybrid Slow start and Proportional Rate Reduction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * o Add toggle for shadow window mechanism. Suggested by David Hayes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * o Add toggle for non-congestion loss tolerance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * o Scaling parameter G is changed to a backoff factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * conversion is given by: backoff_factor = 1000/(G * window).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * o Limit shadow window to 2 * cwnd, or to cwnd when application limited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * o More accurate e^-x.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define HYSTART_ACK_TRAIN 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define HYSTART_DELAY 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int window __read_mostly = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static unsigned int backoff_beta __read_mostly = 0.7071 * 1024; /* sqrt 0.5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static unsigned int backoff_factor __read_mostly = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static unsigned int hystart_detect __read_mostly = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static unsigned int use_ineff __read_mostly = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static bool use_shadow __read_mostly = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static bool use_tolerance __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) module_param(window, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) MODULE_PARM_DESC(window, "gradient window size (power of two <= 256)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) module_param(backoff_beta, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) MODULE_PARM_DESC(backoff_beta, "backoff beta (0-1024)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) module_param(backoff_factor, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) MODULE_PARM_DESC(backoff_factor, "backoff probability scale factor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) module_param(hystart_detect, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) MODULE_PARM_DESC(hystart_detect, "use Hybrid Slow start "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) "(0: disabled, 1: ACK train, 2: delay threshold, 3: both)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) module_param(use_ineff, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) MODULE_PARM_DESC(use_ineff, "use ineffectual backoff detection (threshold)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) module_param(use_shadow, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) MODULE_PARM_DESC(use_shadow, "use shadow window heuristic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) module_param(use_tolerance, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) MODULE_PARM_DESC(use_tolerance, "use loss tolerance heuristic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct cdg_minmax {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) s32 min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) s32 max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u64 v64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) enum cdg_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) CDG_UNKNOWN = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) CDG_NONFULL = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) CDG_FULL = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) CDG_BACKOFF = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct cdg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct cdg_minmax rtt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct cdg_minmax rtt_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct cdg_minmax *gradients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct cdg_minmax gsum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bool gfilled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u8 tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) u8 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u8 delack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u32 rtt_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) u32 shadow_wnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u16 backoff_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) u16 sample_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) s32 delay_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) u32 last_ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) u32 round_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * nexp_u32 - negative base-e exponential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @ux: x in units of micro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Returns exp(ux * -1e-6) * U32_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static u32 __pure nexp_u32(u32 ux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const u16 v[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* exp(-x)*65536-1 for x = 0, 0.000256, 0.000512, ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 65535,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 65518, 65501, 65468, 65401, 65267, 65001, 64470, 63422,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 61378, 57484, 50423, 38795, 22965, 8047, 987, 14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) u32 msb = ux >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) u32 res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Cut off when ux >= 2^24 (actual result is <= 222/U32_MAX). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (msb > U16_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Scale first eight bits linearly: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) res = U32_MAX - (ux & 0xff) * (U32_MAX / 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Obtain e^(x + y + ...) by computing e^x * e^y * ...: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) for (i = 1; msb; i++, msb >>= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u32 y = v[i & -(msb & 1)] + U32_C(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) res = ((u64)res * y) >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Based on the HyStart algorithm (by Ha et al.) that is implemented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * tcp_cubic. Differences/experimental changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * o Using Hayes' delayed ACK filter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * o Using a usec clock for the ACK train.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * o Reset ACK train when application limited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * o Invoked at any cwnd (i.e. also when cwnd < 16).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * o Invoked only when cwnd < ssthresh (i.e. not when cwnd == ssthresh).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void tcp_cdg_hystart_update(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ca->delay_min = min_not_zero(ca->delay_min, ca->rtt.min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ca->delay_min == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (hystart_detect & HYSTART_ACK_TRAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) u32 now_us = tp->tcp_mstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ca->last_ack == 0 || !tcp_is_cwnd_limited(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ca->last_ack = now_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ca->round_start = now_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) } else if (before(now_us, ca->last_ack + 3000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u32 base_owd = max(ca->delay_min / 2U, 125U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ca->last_ack = now_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (after(now_us, ca->round_start + base_owd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) NET_INC_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) LINUX_MIB_TCPHYSTARTTRAINDETECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) NET_ADD_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) LINUX_MIB_TCPHYSTARTTRAINCWND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) tp->snd_ssthresh = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (hystart_detect & HYSTART_DELAY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ca->sample_cnt < 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ca->sample_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) s32 thresh = max(ca->delay_min + ca->delay_min / 8U,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 125U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (ca->rtt.min > thresh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) NET_INC_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) LINUX_MIB_TCPHYSTARTDELAYDETECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) NET_ADD_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) LINUX_MIB_TCPHYSTARTDELAYCWND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) tp->snd_ssthresh = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static s32 tcp_cdg_grad(struct cdg *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) s32 gmin = ca->rtt.min - ca->rtt_prev.min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) s32 gmax = ca->rtt.max - ca->rtt_prev.max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) s32 grad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ca->gradients) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ca->gsum.min += gmin - ca->gradients[ca->tail].min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ca->gsum.max += gmax - ca->gradients[ca->tail].max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ca->gradients[ca->tail].min = gmin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ca->gradients[ca->tail].max = gmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ca->tail = (ca->tail + 1) & (window - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) gmin = ca->gsum.min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) gmax = ca->gsum.max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* We keep sums to ignore gradients during cwnd reductions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * the paper's smoothed gradients otherwise simplify to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * (rtt_latest - rtt_oldest) / window.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * We also drop division by window here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) grad = gmin > 0 ? gmin : gmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* Extrapolate missing values in gradient window: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!ca->gfilled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!ca->gradients && window > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) grad *= window; /* Memory allocation failed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) else if (ca->tail == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ca->gfilled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) grad = (grad * window) / (int)ca->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* Backoff was effectual: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (gmin <= -32 || gmax <= -32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) ca->backoff_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (use_tolerance) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* Reduce small variations to zero: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) gmin = DIV_ROUND_CLOSEST(gmin, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) gmax = DIV_ROUND_CLOSEST(gmax, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (gmin > 0 && gmax <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ca->state = CDG_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) else if ((gmin > 0 && gmax > 0) || gmax < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ca->state = CDG_NONFULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return grad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static bool tcp_cdg_backoff(struct sock *sk, u32 grad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (prandom_u32() <= nexp_u32(grad * backoff_factor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (use_ineff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ca->backoff_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (ca->backoff_cnt > use_ineff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) ca->shadow_wnd = max(ca->shadow_wnd, tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ca->state = CDG_BACKOFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) tcp_enter_cwr(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* Not called in CWR or Recovery state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void tcp_cdg_cong_avoid(struct sock *sk, u32 ack, u32 acked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) u32 prior_snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) u32 incr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (tcp_in_slow_start(tp) && hystart_detect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) tcp_cdg_hystart_update(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (after(ack, ca->rtt_seq) && ca->rtt.v64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) s32 grad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ca->rtt_prev.v64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) grad = tcp_cdg_grad(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) ca->rtt_seq = tp->snd_nxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ca->rtt_prev = ca->rtt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ca->rtt.v64 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) ca->last_ack = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ca->sample_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (grad > 0 && tcp_cdg_backoff(sk, grad))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!tcp_is_cwnd_limited(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ca->shadow_wnd = min(ca->shadow_wnd, tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) prior_snd_cwnd = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) tcp_reno_cong_avoid(sk, ack, acked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) incr = tp->snd_cwnd - prior_snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ca->shadow_wnd = max(ca->shadow_wnd, ca->shadow_wnd + incr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static void tcp_cdg_acked(struct sock *sk, const struct ack_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (sample->rtt_us <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /* A heuristic for filtering delayed ACKs, adapted from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * D.A. Hayes. "Timing enhancements to the FreeBSD kernel to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * delay and rate based TCP mechanisms." TR 100219A. CAIA, 2010.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (tp->sacked_out == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (sample->pkts_acked == 1 && ca->delack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* A delayed ACK is only used for the minimum if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * provenly lower than an existing non-zero minimum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ca->rtt.min = min(ca->rtt.min, sample->rtt_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ca->delack--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) } else if (sample->pkts_acked > 1 && ca->delack < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ca->delack++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ca->rtt.min = min_not_zero(ca->rtt.min, sample->rtt_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ca->rtt.max = max(ca->rtt.max, sample->rtt_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static u32 tcp_cdg_ssthresh(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (ca->state == CDG_BACKOFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return max(2U, (tp->snd_cwnd * min(1024U, backoff_beta)) >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (ca->state == CDG_NONFULL && use_tolerance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ca->shadow_wnd = min(ca->shadow_wnd >> 1, tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (use_shadow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return max3(2U, ca->shadow_wnd, tp->snd_cwnd >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return max(2U, tp->snd_cwnd >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static void tcp_cdg_cwnd_event(struct sock *sk, const enum tcp_ca_event ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct cdg_minmax *gradients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) switch (ev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case CA_EVENT_CWND_RESTART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) gradients = ca->gradients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (gradients)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) memset(gradients, 0, window * sizeof(gradients[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) memset(ca, 0, sizeof(*ca));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ca->gradients = gradients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ca->rtt_seq = tp->snd_nxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ca->shadow_wnd = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) case CA_EVENT_COMPLETE_CWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ca->state = CDG_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ca->rtt_seq = tp->snd_nxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ca->rtt_prev = ca->rtt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ca->rtt.v64 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static void tcp_cdg_init(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* We silently fall back to window = 1 if allocation fails. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (window > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ca->gradients = kcalloc(window, sizeof(ca->gradients[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) GFP_NOWAIT | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ca->rtt_seq = tp->snd_nxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ca->shadow_wnd = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void tcp_cdg_release(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct cdg *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) kfree(ca->gradients);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static struct tcp_congestion_ops tcp_cdg __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) .cong_avoid = tcp_cdg_cong_avoid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .cwnd_event = tcp_cdg_cwnd_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .pkts_acked = tcp_cdg_acked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) .undo_cwnd = tcp_reno_undo_cwnd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .ssthresh = tcp_cdg_ssthresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .release = tcp_cdg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .init = tcp_cdg_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .name = "cdg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static int __init tcp_cdg_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (backoff_beta > 1024 || window < 1 || window > 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (!is_power_of_2(window))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) BUILD_BUG_ON(sizeof(struct cdg) > ICSK_CA_PRIV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) tcp_register_congestion_control(&tcp_cdg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static void __exit tcp_cdg_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) tcp_unregister_congestion_control(&tcp_cdg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) module_init(tcp_cdg_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) module_exit(tcp_cdg_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_AUTHOR("Kenneth Klette Jonassen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) MODULE_DESCRIPTION("TCP CDG");