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 { |
| 34 | uint32_t ssthresh; |
| 35 | uint32_t recovery_start_time; |
| 36 | uint32_t remain_acked; |
| 37 | }; |
| 38 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 39 | static int quic_cc_nr_init(struct quic_cc *cc) |
| 40 | { |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 41 | struct nr *nr = quic_cc_priv(cc); |
| 42 | |
| 43 | cc->algo->state = QUIC_CC_ST_SS; |
| 44 | nr->ssthresh = QUIC_CC_INFINITE_SSTHESH; |
| 45 | nr->recovery_start_time = 0; |
| 46 | nr->remain_acked = 0; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 47 | |
| 48 | return 1; |
| 49 | } |
| 50 | |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 51 | /* Re-enter slow start state. */ |
| 52 | static void quic_cc_nr_slow_start(struct quic_cc *cc) |
| 53 | { |
| 54 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 55 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 56 | |
| 57 | path = container_of(cc, struct quic_path, cc); |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 58 | path->cwnd = path->min_cwnd; |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 59 | /* Re-entering slow start state. */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 60 | cc->algo->state = QUIC_CC_ST_SS; |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 61 | /* Recovery start time reset */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 62 | nr->recovery_start_time = 0; |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 65 | /* Slow start callback. */ |
| 66 | static void quic_cc_nr_ss_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 67 | { |
| 68 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 69 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 70 | |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 71 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 72 | path = container_of(cc, struct quic_path, cc); |
| 73 | switch (ev->type) { |
| 74 | case QUIC_CC_EVT_ACK: |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 75 | /* Do not increase the congestion window in recovery period. */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 76 | if (ev->ack.time_sent <= nr->recovery_start_time) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 77 | return; |
| 78 | |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 79 | path->cwnd += ev->ack.acked; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 80 | /* 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] | 81 | if (path->cwnd > nr->ssthresh) |
| 82 | cc->algo->state = QUIC_CC_ST_CA; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 83 | break; |
| 84 | |
| 85 | case QUIC_CC_EVT_LOSS: |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 86 | path->cwnd = QUIC_MAX(path->cwnd >> 1, path->min_cwnd); |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 87 | nr->ssthresh = path->cwnd; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 88 | /* Exit to congestion avoidance. */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 89 | cc->algo->state = QUIC_CC_ST_CA; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 90 | break; |
| 91 | |
| 92 | case QUIC_CC_EVT_ECN_CE: |
| 93 | /* XXX TO DO XXX */ |
| 94 | break; |
| 95 | } |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 96 | TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc,, cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Congestion avoidance callback. */ |
| 100 | static void quic_cc_nr_ca_cb(struct quic_cc *cc, struct quic_cc_event *ev) |
| 101 | { |
| 102 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 103 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 104 | |
Frédéric Lécaille | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 105 | TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 106 | path = container_of(cc, struct quic_path, cc); |
| 107 | switch (ev->type) { |
| 108 | case QUIC_CC_EVT_ACK: |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 109 | { |
| 110 | uint64_t acked; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 111 | /* Do not increase the congestion window in recovery period. */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 112 | if (ev->ack.time_sent <= nr->recovery_start_time) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 113 | goto out; |
| 114 | |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 115 | /* Increasing the congestion window by (acked / cwnd) |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 116 | */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 117 | acked = ev->ack.acked * path->mtu + nr->remain_acked; |
| 118 | nr->remain_acked = acked % path->cwnd; |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 119 | path->cwnd += acked / path->cwnd; |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 120 | break; |
Frédéric Lécaille | 0e7c9a7 | 2022-03-03 07:50:45 +0100 | [diff] [blame] | 121 | } |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 122 | |
| 123 | case QUIC_CC_EVT_LOSS: |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 124 | /* Do not decrease the congestion window when already in recovery period. */ |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 125 | if (ev->loss.time_sent <= nr->recovery_start_time) |
Frédéric Lécaille | a5ee0ae | 2022-03-02 14:52:56 +0100 | [diff] [blame] | 126 | goto out; |
| 127 | |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 128 | nr->recovery_start_time = now_ms; |
| 129 | nr->ssthresh = path->cwnd; |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 130 | path->cwnd = QUIC_MAX(path->cwnd >> 1, path->min_cwnd); |
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 | fde2a98 | 2021-12-27 15:12:09 +0100 | [diff] [blame] | 139 | TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc, NULL, cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc) |
| 143 | { |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 144 | struct quic_path *path; |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 145 | struct nr *nr = quic_cc_priv(cc); |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 146 | |
| 147 | path = container_of(cc, struct quic_path, cc); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 148 | chunk_appendf(buf, " state=%s cwnd=%llu ssthresh=%ld recovery_start_time=%llu", |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 149 | quic_cc_state_str(cc->algo->state), |
Frédéric Lécaille | 9777ead | 2022-03-03 08:24:53 +0100 | [diff] [blame] | 150 | (unsigned long long)path->cwnd, |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 151 | (long)nr->ssthresh, |
| 152 | (unsigned long long)nr->recovery_start_time); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void (*quic_cc_nr_state_cbs[])(struct quic_cc *cc, |
| 156 | struct quic_cc_event *ev) = { |
| 157 | [QUIC_CC_ST_SS] = quic_cc_nr_ss_cb, |
| 158 | [QUIC_CC_ST_CA] = quic_cc_nr_ca_cb, |
| 159 | }; |
| 160 | |
| 161 | static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev) |
| 162 | { |
Frédéric Lécaille | c591459 | 2022-05-31 09:40:44 +0200 | [diff] [blame] | 163 | return quic_cc_nr_state_cbs[cc->algo->state](cc, ev); |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | struct quic_cc_algo quic_cc_algo_nr = { |
| 167 | .type = QUIC_CC_ALGO_TP_NEWRENO, |
| 168 | .init = quic_cc_nr_init, |
| 169 | .event = quic_cc_nr_event, |
Frédéric Lécaille | 83bfca6 | 2022-03-02 11:18:33 +0100 | [diff] [blame] | 170 | .slow_start = quic_cc_nr_slow_start, |
Frédéric Lécaille | a7e7ce9 | 2020-11-23 14:14:04 +0100 | [diff] [blame] | 171 | .state_trace = quic_cc_nr_state_trace, |
| 172 | }; |
| 173 | |