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; |
| 74 | nr->ssthresh = QUIC_MAX(path->cwnd >> 1, path->min_cwnd); |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 75 | nr->state = QUIC_CC_ST_RP; |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 78 | /* Slow start callback. */ |
| 79 | static void quic_cc_nr_ss_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 80 | { |
| 81 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 82 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 83 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 84 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc); |
| 85 | 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] | 86 | path = container_of(cc, struct quic_path, cc); |
| 87 | switch (ev->type) { |
| 88 | case QUIC_CC_EVT_ACK: |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 89 | path->cwnd += ev->ack.acked; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 90 | /* 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] | 91 | if (path->cwnd > nr->ssthresh) |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 92 | nr->state = QUIC_CC_ST_CA; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 93 | break; |
| 94 | |
| 95 | case QUIC_CC_EVT_LOSS: |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 96 | quic_cc_nr_enter_recovery(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 97 | break; |
| 98 | |
| 99 | case QUIC_CC_EVT_ECN_CE: |
| 100 | /* XXX TO DO XXX */ |
| 101 | break; |
| 102 | } |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 103 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, NULL, cc); |
| 104 | TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Congestion avoidance callback. */ |
| 108 | static void quic_cc_nr_ca_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 109 | { |
| 110 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 111 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 112 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 113 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc); |
| 114 | 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] | 115 | path = container_of(cc, struct quic_path, cc); |
| 116 | switch (ev->type) { |
| 117 | case QUIC_CC_EVT_ACK: |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 118 | { |
| 119 | uint64_t acked; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 120 | |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 121 | /* Increasing the congestion window by (acked / cwnd) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 122 | */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 123 | acked = ev->ack.acked * path->mtu + nr->remain_acked; |
| 124 | nr->remain_acked = acked % path->cwnd; |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 125 | path->cwnd += acked / path->cwnd; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 126 | break; |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 127 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 128 | |
| 129 | case QUIC_CC_EVT_LOSS: |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 130 | quic_cc_nr_enter_recovery(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case QUIC_CC_EVT_ECN_CE: |
| 134 | /* XXX TO DO XXX */ |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | out: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 139 | TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, NULL, cc); |
| 140 | TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 143 | /* Recovery period callback. */ |
| 144 | static void quic_cc_nr_rp_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 145 | { |
| 146 | struct quic_path *path; |
| 147 | struct nr *nr = quic_cc_priv(cc); |
| 148 | |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 149 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc); |
| 150 | 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] | 151 | path = container_of(cc, struct quic_path, cc); |
| 152 | switch (ev->type) { |
| 153 | case QUIC_CC_EVT_ACK: |
| 154 | /* RFC 9022 7.3.2. Recovery |
| 155 | * A recovery period ends and the sender enters congestion avoidance when a |
| 156 | * packet sent during the recovery period is acknowledged. |
| 157 | */ |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 158 | if (tick_is_le(ev->ack.time_sent, nr->recovery_start_time)) { |
| 159 | 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] | 160 | goto leave; |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 161 | } |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 162 | |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 163 | nr->state = QUIC_CC_ST_CA; |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 164 | nr->recovery_start_time = TICK_ETERNITY; |
| 165 | path->cwnd = nr->ssthresh; |
| 166 | break; |
| 167 | case QUIC_CC_EVT_LOSS: |
| 168 | /* Do nothing */ |
| 169 | break; |
| 170 | case QUIC_CC_EVT_ECN_CE: |
| 171 | /* XXX TO DO XXX */ |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | leave: |
Frédéric Lécaille | 8f99194 | 2023-03-24 15:14:45 +0100 | [diff] [blame] | 176 | 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] | 177 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev); |
| 178 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 179 | static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc) |
| 180 | { |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 181 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 182 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 183 | |
| 184 | path = container_of(cc, struct quic_path, cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 185 | chunk_appendf(buf, " state=%s cwnd=%llu ssthresh=%ld recovery_start_time=%llu", |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 186 | quic_cc_state_str(nr->state), |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 187 | (unsigned long long)path->cwnd, |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 188 | (long)nr->ssthresh, |
| 189 | (unsigned long long)nr->recovery_start_time); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void (*quic_cc_nr_state_cbs[])(struct quic_cc *cc, |
| 193 | struct quic_cc_event *ev) = { |
| 194 | [QUIC_CC_ST_SS] = quic_cc_nr_ss_cb, |
| 195 | [QUIC_CC_ST_CA] = quic_cc_nr_ca_cb, |
Frédéric Lécaille | 8e6c661 | 2023-03-22 09:13:14 +0100 | [diff] [blame] | 196 | [QUIC_CC_ST_RP] = quic_cc_nr_rp_cb, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev) |
| 200 | { |
Frédéric Lécaille | 7d6270a | 2023-04-02 12:43:22 +0200 | [diff] [blame] | 201 | struct nr *nr = quic_cc_priv(cc); |
| 202 | |
| 203 | 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] | 204 | } |
| 205 | |
| 206 | struct quic_cc_algo quic_cc_algo_nr = { |
| 207 | .type = QUIC_CC_ALGO_TP_NEWRENO, |
| 208 | .init = quic_cc_nr_init, |
| 209 | .event = quic_cc_nr_event, |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 210 | .slow_start = quic_cc_nr_slow_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 211 | .state_trace = quic_cc_nr_state_trace, |
| 212 | }; |
| 213 | |