blob: 4d9bf136a4e696d42263ca51c8e151d08d7ef3e6 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * NewReno congestion control algorithm.
3 *
4 * This file contains definitions for QUIC congestion control.
5 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01006 * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01007 *
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 Denoyelle5c25dc52022-09-30 17:44:15 +020023#include <haproxy/api-t.h>
24#include <haproxy/buf.h>
25#include <haproxy/chunk.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010026#include <haproxy/quic_cc.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020027#include <haproxy/quic_conn-t.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010028#include <haproxy/trace.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010029
30#define TRACE_SOURCE &trace_quic
31
Frédéric Lécaillec5914592022-05-31 09:40:44 +020032/* Newreno state */
33struct nr {
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +020034 uint32_t state;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020035 uint32_t ssthresh;
36 uint32_t recovery_start_time;
37 uint32_t remain_acked;
38};
39
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040static int quic_cc_nr_init(struct quic_cc *cc)
41{
Frédéric Lécaillec5914592022-05-31 09:40:44 +020042 struct nr *nr = quic_cc_priv(cc);
43
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +020044 nr->state = QUIC_CC_ST_SS;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020045 nr->ssthresh = QUIC_CC_INFINITE_SSTHESH;
46 nr->recovery_start_time = 0;
47 nr->remain_acked = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010048
49 return 1;
50}
51
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010052/* Re-enter slow start state. */
53static void quic_cc_nr_slow_start(struct quic_cc *cc)
54{
55 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020056 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010057
58 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010059 path->cwnd = path->min_cwnd;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010060 /* Re-entering slow start state. */
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +020061 nr->state = QUIC_CC_ST_SS;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010062 /* Recovery start time reset */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020063 nr->recovery_start_time = 0;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010064}
65
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +010066/* Enter a recovery period. */
67static 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écaille7d6270a2023-04-02 12:43:22 +020075 nr->state = QUIC_CC_ST_RP;
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +010076}
77
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010078/* Slow start callback. */
79static 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écaillec5914592022-05-31 09:40:44 +020082 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010083
Frédéric Lécaille8f991942023-03-24 15:14:45 +010084 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc);
85 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010086 path = container_of(cc, struct quic_path, cc);
87 switch (ev->type) {
88 case QUIC_CC_EVT_ACK:
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010089 path->cwnd += ev->ack.acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010090 /* Exit to congestion avoidance if slow start threshold is reached. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020091 if (path->cwnd > nr->ssthresh)
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +020092 nr->state = QUIC_CC_ST_CA;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 break;
94
95 case QUIC_CC_EVT_LOSS:
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +010096 quic_cc_nr_enter_recovery(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010097 break;
98
99 case QUIC_CC_EVT_ECN_CE:
100 /* XXX TO DO XXX */
101 break;
102 }
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100103 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écaillea7e7ce92020-11-23 14:14:04 +0100105}
106
107/* Congestion avoidance callback. */
108static 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écaillec5914592022-05-31 09:40:44 +0200111 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100112
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100113 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc);
114 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100115 path = container_of(cc, struct quic_path, cc);
116 switch (ev->type) {
117 case QUIC_CC_EVT_ACK:
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100118 {
119 uint64_t acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100120
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100121 /* Increasing the congestion window by (acked / cwnd)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100122 */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200123 acked = ev->ack.acked * path->mtu + nr->remain_acked;
124 nr->remain_acked = acked % path->cwnd;
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100125 path->cwnd += acked / path->cwnd;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100126 break;
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100127 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100128
129 case QUIC_CC_EVT_LOSS:
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100130 quic_cc_nr_enter_recovery(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100131 break;
132
133 case QUIC_CC_EVT_ECN_CE:
134 /* XXX TO DO XXX */
135 break;
136 }
137
138 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100139 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écaillea7e7ce92020-11-23 14:14:04 +0100141}
142
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100143/* Recovery period callback. */
144static 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écaille8f991942023-03-24 15:14:45 +0100149 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc);
150 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100151 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écaille8f991942023-03-24 15:14:45 +0100158 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écaille8e6c6612023-03-22 09:13:14 +0100160 goto leave;
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100161 }
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100162
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +0200163 nr->state = QUIC_CC_ST_CA;
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100164 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écaille8f991942023-03-24 15:14:45 +0100176 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100177 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev);
178}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100179static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc)
180{
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100181 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200182 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100183
184 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100185 chunk_appendf(buf, " state=%s cwnd=%llu ssthresh=%ld recovery_start_time=%llu",
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +0200186 quic_cc_state_str(nr->state),
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100187 (unsigned long long)path->cwnd,
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200188 (long)nr->ssthresh,
189 (unsigned long long)nr->recovery_start_time);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100190}
191
192static 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écaille8e6c6612023-03-22 09:13:14 +0100196 [QUIC_CC_ST_RP] = quic_cc_nr_rp_cb,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100197};
198
199static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev)
200{
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +0200201 struct nr *nr = quic_cc_priv(cc);
202
203 return quic_cc_nr_state_cbs[nr->state](cc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100204}
205
206struct 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écaille83bfca62022-03-02 11:18:33 +0100210 .slow_start = quic_cc_nr_slow_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211 .state_trace = quic_cc_nr_state_trace,
212};
213