Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NewReno congestion control algorithm. |
| 3 | * |
| 4 | * This file contains definitions for QUIC congestion control. |
| 5 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 6 | * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation, version 2.1 |
| 11 | * exclusively. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
Amaury Denoyelle | 5c25dc5 | 2022-09-30 17:44:15 +0200 | [diff] [blame] | 23 | #include <haproxy/api-t.h> |
| 24 | #include <haproxy/buf.h> |
| 25 | #include <haproxy/chunk.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 26 | #include <haproxy/quic_cc.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 27 | #include <haproxy/quic_conn-t.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 28 | #include <haproxy/trace.h> |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 29 | |
| 30 | #define TRACE_SOURCE &trace_quic |
| 31 | |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 32 | /* Newreno state */ |
| 33 | struct nr { |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 34 | uint32_t state; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 35 | uint32_t ssthresh; |
| 36 | uint32_t recovery_start_time; |
| 37 | uint32_t remain_acked; |
| 38 | }; |
| 39 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 40 | static int quic_cc_nr_init(struct quic_cc *cc) |
| 41 | { |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 42 | struct nr *nr = quic_cc_priv(cc); |
| 43 | |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 44 | nr->state = QUIC_CC_ST_SS; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 45 | nr->ssthresh = QUIC_CC_INFINITE_SSTHESH; |
| 46 | nr->recovery_start_time = 0; |
| 47 | nr->remain_acked = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 48 | |
| 49 | return 1; |
| 50 | } |
| 51 | |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 52 | /* Re-enter slow start state. */ |
| 53 | static void quic_cc_nr_slow_start(struct quic_cc *cc) |
| 54 | { |
| 55 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 56 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 57 | |
| 58 | path = container_of(cc, struct quic_path, cc); |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 59 | path->cwnd = path->min_cwnd; |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 60 | /* Re-entering slow start state. */ |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 61 | nr->state = QUIC_CC_ST_SS; |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 62 | /* Recovery start time reset */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 63 | nr->recovery_start_time = 0; |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 66 | /* Enter a recovery period. */ |
| 67 | static void quic_cc_nr_enter_recovery(struct quic_cc *cc) |
| 68 | { |
| 69 | struct quic_path *path; |
| 70 | struct nr *nr = quic_cc_priv(cc); |
| 71 | |
| 72 | path = container_of(cc, struct quic_path, cc); |
| 73 | nr->recovery_start_time = now_ms; |
Frédéric Lécaille | 595251f | 2023-04-13 10:46:42 +0200 | [diff] [blame] | 74 | nr->ssthresh = path->cwnd >> 1; |
| 75 | path->cwnd = QUIC_MAX(nr->ssthresh, (uint32_t)path->min_cwnd); |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 76 | nr->state = QUIC_CC_ST_RP; |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 79 | /* Slow start callback. */ |
| 80 | static void quic_cc_nr_ss_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 81 | { |
| 82 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 83 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 84 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 85 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc); |
| 86 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 87 | path = container_of(cc, struct quic_path, cc); |
| 88 | switch (ev->type) { |
| 89 | case QUIC_CC_EVT_ACK: |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 90 | path->cwnd += ev->ack.acked; |
Frédéric Lécaille | fad0e6c | 2023-04-06 10:19:17 +0200 | [diff] [blame] | 91 | path->mcwnd = QUIC_MAX(path->cwnd, path->mcwnd); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 92 | /* Exit to congestion avoidance if slow start threshold is reached. */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 93 | if (path->cwnd > nr->ssthresh) |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 94 | nr->state = QUIC_CC_ST_CA; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | case QUIC_CC_EVT_LOSS: |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 98 | quic_cc_nr_enter_recovery(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 99 | break; |
| 100 | |
| 101 | case QUIC_CC_EVT_ECN_CE: |
| 102 | /* XXX TO DO XXX */ |
| 103 | break; |
| 104 | } |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 105 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, NULL, cc); |
| 106 | TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* Congestion avoidance callback. */ |
| 110 | static void quic_cc_nr_ca_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 111 | { |
| 112 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 113 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 114 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 115 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc); |
| 116 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 117 | path = container_of(cc, struct quic_path, cc); |
| 118 | switch (ev->type) { |
| 119 | case QUIC_CC_EVT_ACK: |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 120 | { |
| 121 | uint64_t acked; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 122 | |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 123 | /* Increasing the congestion window by (acked / cwnd) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 124 | */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 125 | acked = ev->ack.acked * path->mtu + nr->remain_acked; |
| 126 | nr->remain_acked = acked % path->cwnd; |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 127 | path->cwnd += acked / path->cwnd; |
Frédéric Lécaille | fad0e6c | 2023-04-06 10:19:17 +0200 | [diff] [blame] | 128 | path->mcwnd = QUIC_MAX(path->cwnd, path->mcwnd); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 129 | break; |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 130 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 131 | |
| 132 | case QUIC_CC_EVT_LOSS: |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 133 | quic_cc_nr_enter_recovery(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 134 | break; |
| 135 | |
| 136 | case QUIC_CC_EVT_ECN_CE: |
| 137 | /* XXX TO DO XXX */ |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | out: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 142 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, NULL, cc); |
| 143 | TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 146 | /* Recovery period callback. */ |
| 147 | static void quic_cc_nr_rp_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 148 | { |
| 149 | struct quic_path *path; |
| 150 | struct nr *nr = quic_cc_priv(cc); |
| 151 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 152 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc); |
| 153 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 154 | path = container_of(cc, struct quic_path, cc); |
| 155 | switch (ev->type) { |
| 156 | case QUIC_CC_EVT_ACK: |
| 157 | /* RFC 9022 7.3.2. Recovery |
| 158 | * A recovery period ends and the sender enters congestion avoidance when a |
| 159 | * packet sent during the recovery period is acknowledged. |
| 160 | */ |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 161 | if (tick_is_le(ev->ack.time_sent, nr->recovery_start_time)) { |
| 162 | TRACE_PROTO("CC reno (still in recovery period)", QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 163 | goto leave; |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 164 | } |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 165 | |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 166 | nr->state = QUIC_CC_ST_CA; |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 167 | nr->recovery_start_time = TICK_ETERNITY; |
| 168 | path->cwnd = nr->ssthresh; |
| 169 | break; |
| 170 | case QUIC_CC_EVT_LOSS: |
| 171 | /* Do nothing */ |
| 172 | break; |
| 173 | case QUIC_CC_EVT_ECN_CE: |
| 174 | /* XXX TO DO XXX */ |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | leave: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 179 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 180 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev); |
| 181 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 182 | static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc) |
| 183 | { |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 184 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 185 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 186 | |
| 187 | path = container_of(cc, struct quic_path, cc); |
Frédéric Lécaille | fad0e6c | 2023-04-06 10:19:17 +0200 | [diff] [blame] | 188 | chunk_appendf(buf, " state=%s cwnd=%llu mcwnd=%llu ssthresh=%ld rpst=%dms pktloss=%llu", |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 189 | quic_cc_state_str(nr->state), |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 190 | (unsigned long long)path->cwnd, |
Frédéric Lécaille | fad0e6c | 2023-04-06 10:19:17 +0200 | [diff] [blame] | 191 | (unsigned long long)path->mcwnd, |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 192 | (long)nr->ssthresh, |
Frédéric Lécaille | fad0e6c | 2023-04-06 10:19:17 +0200 | [diff] [blame] | 193 | !tick_isset(nr->recovery_start_time) ? -1 : |
| 194 | TICKS_TO_MS(tick_remain(nr->recovery_start_time, now_ms)), |
| 195 | (unsigned long long)path->loss.nb_lost_pkt); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void (*quic_cc_nr_state_cbs[])(struct quic_cc *cc, |
| 199 | struct quic_cc_event *ev) = { |
| 200 | [QUIC_CC_ST_SS] = quic_cc_nr_ss_cb, |
| 201 | [QUIC_CC_ST_CA] = quic_cc_nr_ca_cb, |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 202 | [QUIC_CC_ST_RP] = quic_cc_nr_rp_cb, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev) |
| 206 | { |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 207 | struct nr *nr = quic_cc_priv(cc); |
| 208 | |
| 209 | return quic_cc_nr_state_cbs[nr->state](cc, ev); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | struct quic_cc_algo quic_cc_algo_nr = { |
| 213 | .type = QUIC_CC_ALGO_TP_NEWRENO, |
| 214 | .init = quic_cc_nr_init, |
| 215 | .event = quic_cc_nr_event, |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 216 | .slow_start = quic_cc_nr_slow_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 217 | .state_trace = quic_cc_nr_state_trace, |
| 218 | }; |
| 219 | |