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