blob: 89a0524a89cf2f478ffedae3aead3873ffa158ef [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
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écaillec5914592022-05-31 09:40:44 +020029/* Newreno state */
30struct nr {
31 uint32_t ssthresh;
32 uint32_t recovery_start_time;
33 uint32_t remain_acked;
34};
35
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010036static int quic_cc_nr_init(struct quic_cc *cc)
37{
Frédéric Lécaillec5914592022-05-31 09:40:44 +020038 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écaillea7e7ce92020-11-23 14:14:04 +010044
45 return 1;
46}
47
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010048/* Re-enter slow start state. */
49static void quic_cc_nr_slow_start(struct quic_cc *cc)
50{
51 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020052 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010053
54 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010055 path->cwnd = path->min_cwnd;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010056 /* Re-entering slow start state. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020057 cc->algo->state = QUIC_CC_ST_SS;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010058 /* Recovery start time reset */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020059 nr->recovery_start_time = 0;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010060}
61
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010062/* Slow start callback. */
63static 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écaillec5914592022-05-31 09:40:44 +020066 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010067
Frédéric Lécaillefde2a982021-12-27 15:12:09 +010068 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010069 path = container_of(cc, struct quic_path, cc);
70 switch (ev->type) {
71 case QUIC_CC_EVT_ACK:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010072 /* Do not increase the congestion window in recovery period. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020073 if (ev->ack.time_sent <= nr->recovery_start_time)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074 return;
75
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010076 path->cwnd += ev->ack.acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010077 /* Exit to congestion avoidance if slow start threshold is reached. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020078 if (path->cwnd > nr->ssthresh)
79 cc->algo->state = QUIC_CC_ST_CA;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010080 break;
81
82 case QUIC_CC_EVT_LOSS:
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010083 path->cwnd = QUIC_MAX(path->cwnd >> 1, path->min_cwnd);
Frédéric Lécaillec5914592022-05-31 09:40:44 +020084 nr->ssthresh = path->cwnd;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010085 /* Exit to congestion avoidance. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020086 cc->algo->state = QUIC_CC_ST_CA;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010087 break;
88
89 case QUIC_CC_EVT_ECN_CE:
90 /* XXX TO DO XXX */
91 break;
92 }
Frédéric Lécaillefde2a982021-12-27 15:12:09 +010093 TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc,, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010094}
95
96/* Congestion avoidance callback. */
97static 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écaillec5914592022-05-31 09:40:44 +0200100 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100101
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100102 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100103 path = container_of(cc, struct quic_path, cc);
104 switch (ev->type) {
105 case QUIC_CC_EVT_ACK:
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100106 {
107 uint64_t acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100108 /* Do not increase the congestion window in recovery period. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200109 if (ev->ack.time_sent <= nr->recovery_start_time)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100110 goto out;
111
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100112 /* Increasing the congestion window by (acked / cwnd)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100113 */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200114 acked = ev->ack.acked * path->mtu + nr->remain_acked;
115 nr->remain_acked = acked % path->cwnd;
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100116 path->cwnd += acked / path->cwnd;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100117 break;
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100118 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100119
120 case QUIC_CC_EVT_LOSS:
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +0100121 /* Do not decrease the congestion window when already in recovery period. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200122 if (ev->loss.time_sent <= nr->recovery_start_time)
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +0100123 goto out;
124
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200125 nr->recovery_start_time = now_ms;
126 nr->ssthresh = path->cwnd;
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100127 path->cwnd = QUIC_MAX(path->cwnd >> 1, path->min_cwnd);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100128 break;
129
130 case QUIC_CC_EVT_ECN_CE:
131 /* XXX TO DO XXX */
132 break;
133 }
134
135 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100136 TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc, NULL, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100137}
138
139static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc)
140{
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100141 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200142 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100143
144 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145 chunk_appendf(buf, " state=%s cwnd=%llu ssthresh=%ld recovery_start_time=%llu",
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200146 quic_cc_state_str(cc->algo->state),
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100147 (unsigned long long)path->cwnd,
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200148 (long)nr->ssthresh,
149 (unsigned long long)nr->recovery_start_time);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100150}
151
152static 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
158static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev)
159{
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200160 return quic_cc_nr_state_cbs[cc->algo->state](cc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161}
162
163struct 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écaille83bfca62022-03-02 11:18:33 +0100167 .slow_start = quic_cc_nr_slow_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100168 .state_trace = quic_cc_nr_state_trace,
169};
170