blob: 9b132482df3e4c715ba3b20ed80a0b31d553519c [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;
Frédéric Lécaille595251f2023-04-13 10:46:42 +020074 nr->ssthresh = path->cwnd >> 1;
75 path->cwnd = QUIC_MAX(nr->ssthresh, (uint32_t)path->min_cwnd);
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +020076 nr->state = QUIC_CC_ST_RP;
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +010077}
78
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010079/* Slow start callback. */
80static void quic_cc_nr_ss_cb(struct quic_cc *cc, struct quic_cc_event *ev)
81{
82 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020083 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010084
Frédéric Lécaille8f991942023-03-24 15:14:45 +010085 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc);
86 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010087 path = container_of(cc, struct quic_path, cc);
88 switch (ev->type) {
89 case QUIC_CC_EVT_ACK:
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010090 path->cwnd += ev->ack.acked;
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +020091 path->mcwnd = QUIC_MAX(path->cwnd, path->mcwnd);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010092 /* Exit to congestion avoidance if slow start threshold is reached. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020093 if (path->cwnd > nr->ssthresh)
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +020094 nr->state = QUIC_CC_ST_CA;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010095 break;
96
97 case QUIC_CC_EVT_LOSS:
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +010098 quic_cc_nr_enter_recovery(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010099 break;
100
101 case QUIC_CC_EVT_ECN_CE:
102 /* XXX TO DO XXX */
103 break;
104 }
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100105 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, NULL, cc);
106 TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100107}
108
109/* Congestion avoidance callback. */
110static void quic_cc_nr_ca_cb(struct quic_cc *cc, struct quic_cc_event *ev)
111{
112 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200113 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100114
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100115 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc);
116 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100117 path = container_of(cc, struct quic_path, cc);
118 switch (ev->type) {
119 case QUIC_CC_EVT_ACK:
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100120 {
121 uint64_t acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100122
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100123 /* Increasing the congestion window by (acked / cwnd)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100124 */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200125 acked = ev->ack.acked * path->mtu + nr->remain_acked;
126 nr->remain_acked = acked % path->cwnd;
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100127 path->cwnd += acked / path->cwnd;
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +0200128 path->mcwnd = QUIC_MAX(path->cwnd, path->mcwnd);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100129 break;
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100130 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100131
132 case QUIC_CC_EVT_LOSS:
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100133 quic_cc_nr_enter_recovery(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100134 break;
135
136 case QUIC_CC_EVT_ECN_CE:
137 /* XXX TO DO XXX */
138 break;
139 }
140
141 out:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100142 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, NULL, cc);
143 TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144}
145
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100146/* Recovery period callback. */
147static void quic_cc_nr_rp_cb(struct quic_cc *cc, struct quic_cc_event *ev)
148{
149 struct quic_path *path;
150 struct nr *nr = quic_cc_priv(cc);
151
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100152 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc);
153 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100154 path = container_of(cc, struct quic_path, cc);
155 switch (ev->type) {
156 case QUIC_CC_EVT_ACK:
157 /* RFC 9022 7.3.2. Recovery
158 * A recovery period ends and the sender enters congestion avoidance when a
159 * packet sent during the recovery period is acknowledged.
160 */
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100161 if (tick_is_le(ev->ack.time_sent, nr->recovery_start_time)) {
162 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 +0100163 goto leave;
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100164 }
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100165
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +0200166 nr->state = QUIC_CC_ST_CA;
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100167 nr->recovery_start_time = TICK_ETERNITY;
168 path->cwnd = nr->ssthresh;
169 break;
170 case QUIC_CC_EVT_LOSS:
171 /* Do nothing */
172 break;
173 case QUIC_CC_EVT_ECN_CE:
174 /* XXX TO DO XXX */
175 break;
176 }
177
178 leave:
Frédéric Lécaille8f991942023-03-24 15:14:45 +0100179 TRACE_PROTO("CC reno", QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100180 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev);
181}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100182static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc)
183{
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100184 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200185 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100186
187 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +0200188 chunk_appendf(buf, " state=%s cwnd=%llu mcwnd=%llu ssthresh=%ld rpst=%dms pktloss=%llu",
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +0200189 quic_cc_state_str(nr->state),
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100190 (unsigned long long)path->cwnd,
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +0200191 (unsigned long long)path->mcwnd,
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200192 (long)nr->ssthresh,
Frédéric Lécaillefad0e6c2023-04-06 10:19:17 +0200193 !tick_isset(nr->recovery_start_time) ? -1 :
194 TICKS_TO_MS(tick_remain(nr->recovery_start_time, now_ms)),
195 (unsigned long long)path->loss.nb_lost_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100196}
197
198static void (*quic_cc_nr_state_cbs[])(struct quic_cc *cc,
199 struct quic_cc_event *ev) = {
200 [QUIC_CC_ST_SS] = quic_cc_nr_ss_cb,
201 [QUIC_CC_ST_CA] = quic_cc_nr_ca_cb,
Frédéric Lécaille8e6c6612023-03-22 09:13:14 +0100202 [QUIC_CC_ST_RP] = quic_cc_nr_rp_cb,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100203};
204
205static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev)
206{
Frédéric Lécaille7d6270a2023-04-02 12:43:22 +0200207 struct nr *nr = quic_cc_priv(cc);
208
209 return quic_cc_nr_state_cbs[nr->state](cc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100210}
211
212struct quic_cc_algo quic_cc_algo_nr = {
213 .type = QUIC_CC_ALGO_TP_NEWRENO,
214 .init = quic_cc_nr_init,
215 .event = quic_cc_nr_event,
Frédéric Lécaille83bfca62022-03-02 11:18:33 +0100216 .slow_start = quic_cc_nr_slow_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 .state_trace = quic_cc_nr_state_trace,
218};
219