blob: e18ba6fa30a4b7b2d5cf1b8260041a7856cc7e12 [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 {
34 uint32_t ssthresh;
35 uint32_t recovery_start_time;
36 uint32_t remain_acked;
37};
38
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010039static int quic_cc_nr_init(struct quic_cc *cc)
40{
Frédéric Lécaillec5914592022-05-31 09:40:44 +020041 struct nr *nr = quic_cc_priv(cc);
42
43 cc->algo->state = QUIC_CC_ST_SS;
44 nr->ssthresh = QUIC_CC_INFINITE_SSTHESH;
45 nr->recovery_start_time = 0;
46 nr->remain_acked = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010047
48 return 1;
49}
50
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010051/* Re-enter slow start state. */
52static void quic_cc_nr_slow_start(struct quic_cc *cc)
53{
54 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020055 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010056
57 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010058 path->cwnd = path->min_cwnd;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010059 /* Re-entering slow start state. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020060 cc->algo->state = QUIC_CC_ST_SS;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010061 /* Recovery start time reset */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020062 nr->recovery_start_time = 0;
Frédéric Lécaille83bfca62022-03-02 11:18:33 +010063}
64
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010065/* Slow start callback. */
66static void quic_cc_nr_ss_cb(struct quic_cc *cc, struct quic_cc_event *ev)
67{
68 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +020069 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010070
Frédéric Lécaillefde2a982021-12-27 15:12:09 +010071 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010072 path = container_of(cc, struct quic_path, cc);
73 switch (ev->type) {
74 case QUIC_CC_EVT_ACK:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010075 /* Do not increase the congestion window in recovery period. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020076 if (ev->ack.time_sent <= nr->recovery_start_time)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010077 return;
78
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010079 path->cwnd += ev->ack.acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010080 /* Exit to congestion avoidance if slow start threshold is reached. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020081 if (path->cwnd > nr->ssthresh)
82 cc->algo->state = QUIC_CC_ST_CA;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010083 break;
84
85 case QUIC_CC_EVT_LOSS:
Frédéric Lécaille9777ead2022-03-03 08:24:53 +010086 path->cwnd = QUIC_MAX(path->cwnd >> 1, path->min_cwnd);
Frédéric Lécaillec5914592022-05-31 09:40:44 +020087 nr->ssthresh = path->cwnd;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010088 /* Exit to congestion avoidance. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +020089 cc->algo->state = QUIC_CC_ST_CA;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010090 break;
91
92 case QUIC_CC_EVT_ECN_CE:
93 /* XXX TO DO XXX */
94 break;
95 }
Frédéric Lécaillefde2a982021-12-27 15:12:09 +010096 TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc,, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010097}
98
99/* Congestion avoidance callback. */
100static void quic_cc_nr_ca_cb(struct quic_cc *cc, struct quic_cc_event *ev)
101{
102 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200103 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100104
Frédéric Lécaillefde2a982021-12-27 15:12:09 +0100105 TRACE_ENTER(QUIC_EV_CONN_CC, cc->qc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100106 path = container_of(cc, struct quic_path, cc);
107 switch (ev->type) {
108 case QUIC_CC_EVT_ACK:
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100109 {
110 uint64_t acked;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100111 /* Do not increase the congestion window in recovery period. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200112 if (ev->ack.time_sent <= nr->recovery_start_time)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100113 goto out;
114
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100115 /* Increasing the congestion window by (acked / cwnd)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100116 */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200117 acked = ev->ack.acked * path->mtu + nr->remain_acked;
118 nr->remain_acked = acked % path->cwnd;
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100119 path->cwnd += acked / path->cwnd;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100120 break;
Frédéric Lécaille0e7c9a72022-03-03 07:50:45 +0100121 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100122
123 case QUIC_CC_EVT_LOSS:
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +0100124 /* Do not decrease the congestion window when already in recovery period. */
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200125 if (ev->loss.time_sent <= nr->recovery_start_time)
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +0100126 goto out;
127
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200128 nr->recovery_start_time = now_ms;
129 nr->ssthresh = path->cwnd;
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100130 path->cwnd = QUIC_MAX(path->cwnd >> 1, path->min_cwnd);
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écaillefde2a982021-12-27 15:12:09 +0100139 TRACE_LEAVE(QUIC_EV_CONN_CC, cc->qc, NULL, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140}
141
142static void quic_cc_nr_state_trace(struct buffer *buf, const struct quic_cc *cc)
143{
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100144 struct quic_path *path;
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200145 struct nr *nr = quic_cc_priv(cc);
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100146
147 path = container_of(cc, struct quic_path, cc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100148 chunk_appendf(buf, " state=%s cwnd=%llu ssthresh=%ld recovery_start_time=%llu",
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200149 quic_cc_state_str(cc->algo->state),
Frédéric Lécaille9777ead2022-03-03 08:24:53 +0100150 (unsigned long long)path->cwnd,
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200151 (long)nr->ssthresh,
152 (unsigned long long)nr->recovery_start_time);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100153}
154
155static void (*quic_cc_nr_state_cbs[])(struct quic_cc *cc,
156 struct quic_cc_event *ev) = {
157 [QUIC_CC_ST_SS] = quic_cc_nr_ss_cb,
158 [QUIC_CC_ST_CA] = quic_cc_nr_ca_cb,
159};
160
161static void quic_cc_nr_event(struct quic_cc *cc, struct quic_cc_event *ev)
162{
Frédéric Lécaillec5914592022-05-31 09:40:44 +0200163 return quic_cc_nr_state_cbs[cc->algo->state](cc, ev);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164}
165
166struct quic_cc_algo quic_cc_algo_nr = {
167 .type = QUIC_CC_ALGO_TP_NEWRENO,
168 .init = quic_cc_nr_init,
169 .event = quic_cc_nr_event,
Frédéric Lécaille83bfca62022-03-02 11:18:33 +0100170 .slow_start = quic_cc_nr_slow_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100171 .state_trace = quic_cc_nr_state_trace,
172};
173