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