Skip to content

Commit 472d575

Browse files
committed
MINOR: quic: implement delivery rate sampling algorithm
This patch implements an algorithm which may be used by congestion algorithms for QUIC to estimate the current delivery rate of a sender. It is at least used by BBR and could be used by others congestion algorithms as cubic. This algorithm was specified by an RFC draft here: https://datatracker.ietf.org/doc/html/draft-cheng-iccrg-delivery-rate-estimation before being merged into BBR v3 here: https://datatracker.ietf.org/doc/html/draft-cardwell-ccwg-bbr#section-4.5.2.2
1 parent c08b877 commit 472d575

File tree

6 files changed

+209
-1
lines changed

6 files changed

+209
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ OPTIONS_OBJS += src/quic_rx.o src/mux_quic.o src/h3.o src/quic_tx.o \
654654
src/cfgparse-quic.o src/qmux_trace.o src/qpack-enc.o \
655655
src/qpack-tbl.o src/h3_stats.o src/quic_stats.o \
656656
src/quic_fctl.o src/cbuf.o src/quic_rules.o \
657-
src/quic_token.o src/quic_pacing.o
657+
src/quic_token.o src/quic_pacing.o src/quic_cc_drs.o
658658
endif
659659
660660
ifneq ($(USE_QUIC_OPENSSL_COMPAT:0=),)

include/haproxy/quic_cc-t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ struct quic_cc_path {
117117
uint64_t ifae_pkts;
118118
/* Burst size if pacing is used. Not used if congestion algo handle pacing itself. */
119119
uint32_t pacing_burst;
120+
uint64_t delivery_rate; /* bytes per second */
120121
};
121122

122123
struct quic_cc_algo {

include/haproxy/quic_cc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static inline void quic_cc_path_init(struct quic_cc_path *path, int ipv4, unsign
100100
path->ifae_pkts = 0;
101101
path->pacing_burst = burst;
102102
quic_cc_init(&path->cc, algo, qc);
103+
path->delivery_rate = 0;
103104
}
104105

105106
/* Return the remaining <room> available on <path> QUIC path for prepared data

include/haproxy/quic_cc_drs.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <inttypes.h>
2+
3+
#include <haproxy/window_filter.h>
4+
5+
/* Per-ACK Rate Sample State */
6+
struct quic_cc_rs {
7+
uint64_t delivered;
8+
uint64_t prior_delivered;
9+
uint64_t tx_in_flight;
10+
uint64_t lost;
11+
uint64_t prior_lost;
12+
int64_t last_end_seq;
13+
uint32_t interval;
14+
uint32_t prior_time;
15+
uint32_t send_elapsed;
16+
uint32_t ack_elapsed;
17+
uint32_t is_app_limited;
18+
};
19+
20+
/* Delivery rate sampling */
21+
struct quic_cc_drs {
22+
struct quic_cc_rs rs;
23+
struct wf wf;
24+
uint64_t round_count;
25+
uint64_t next_round_delivered;
26+
uint64_t delivered;
27+
uint64_t lost;
28+
int64_t last_seq;
29+
uint32_t delivered_time;
30+
uint32_t first_sent_time;
31+
int is_cwnd_limited; /* boolean */
32+
int app_limited; /* boolean */
33+
};
34+
35+
void quic_cc_drs_init(struct quic_cc_drs *drs);
36+
void quic_cc_drs_on_pkt_sent(struct quic_cc_path *path,
37+
struct quic_tx_packet *pkt, struct quic_cc_drs *drs);
38+
void quic_cc_drs_update_rate_sample(struct quic_cc_drs *drs,
39+
struct quic_tx_packet *pkt);
40+
void quic_cc_drs_on_ack_recv(struct quic_cc_drs *drs, struct quic_cc_path *path,
41+
uint64_t pkt_delivered);

include/haproxy/quic_tx-t.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ struct quic_tx_packet {
5353
struct quic_tx_packet *prev;
5454
/* Largest acknowledged packet number if this packet contains an ACK frame */
5555
int64_t largest_acked_pn;
56+
/* Delivery rate sampling information */
57+
struct {
58+
uint64_t delivered;
59+
uint64_t tx_in_flight;
60+
uint64_t lost;
61+
int64_t end_seq;
62+
uint32_t delivered_time;
63+
uint32_t first_sent_time;
64+
int is_app_limited;
65+
} rs;
5666
unsigned char type;
5767
};
5868

src/quic_cc_drs.c

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/* Delivery Rate Sampling */
2+
3+
#include <haproxy/pool.h>
4+
#include <haproxy/quic_cc-t.h>
5+
#include <haproxy/quic_cc_drs.h>
6+
#include <haproxy/quic_tx-t.h>
7+
#include <haproxy/ticks.h>
8+
#include <haproxy/window_filter.h>
9+
10+
static void quic_cc_rs_init(struct quic_cc_rs *rs)
11+
{
12+
rs->interval = UINT32_MAX;
13+
rs->delivered = 0;
14+
rs->prior_delivered = 0;
15+
rs->prior_time = TICK_ETERNITY;
16+
rs->tx_in_flight = 0;
17+
rs->lost = 0;
18+
rs->prior_lost = 0;
19+
rs->send_elapsed = 0;
20+
rs->ack_elapsed = 0;
21+
rs->last_end_seq = -1;
22+
rs->is_app_limited = 0;
23+
}
24+
25+
void quic_cc_drs_init(struct quic_cc_drs *drs)
26+
{
27+
quic_cc_rs_init(&drs->rs);
28+
wf_init(&drs->wf, 12, 0, ~0U);
29+
drs->round_count = 0;
30+
drs->next_round_delivered = 0;
31+
drs->delivered = 0;
32+
drs->lost = 0;
33+
drs->last_seq = -1;
34+
drs->delivered_time = TICK_ETERNITY;
35+
drs->first_sent_time = TICK_ETERNITY;
36+
drs->app_limited = 0;
37+
drs->is_cwnd_limited = 0;
38+
}
39+
40+
/* Update <pkt> TX packet rate sampling information.
41+
* Must be called after <pkt> has just been sent.
42+
*/
43+
void quic_cc_drs_on_pkt_sent(struct quic_cc_path *path,
44+
struct quic_tx_packet *pkt, struct quic_cc_drs *drs)
45+
{
46+
if (!path->in_flight)
47+
drs->first_sent_time = drs->delivered_time = pkt->time_sent;
48+
49+
pkt->rs.first_sent_time = drs->first_sent_time;
50+
pkt->rs.delivered_time = drs->delivered_time;
51+
pkt->rs.delivered = drs->delivered;
52+
pkt->rs.is_app_limited = drs->app_limited != 0;
53+
54+
pkt->rs.tx_in_flight = path->in_flight + pkt->len;
55+
pkt->rs.lost = drs->lost;
56+
pkt->rs.end_seq = ++drs->last_seq;
57+
}
58+
59+
/* Return 1 if <pkt> TX packet is the most recently sent packet
60+
* that has been delivered, 0 if not.
61+
*/
62+
static inline int quic_cc_drs_is_newest_packet(struct quic_cc_drs *drs,
63+
struct quic_tx_packet *pkt)
64+
{
65+
return tick_is_lt(drs->first_sent_time, pkt->time_sent) ||
66+
(pkt->time_sent == drs->first_sent_time &&
67+
pkt->rs.end_seq > drs->rs.last_end_seq);
68+
}
69+
70+
/* RFC https://datatracker.ietf.org/doc/draft-ietf-ccwg-bbr/
71+
* 4.5.2.3.3. Upon receiving an ACK
72+
*
73+
* When an ACK arrives, the sender invokes GenerateRateSample() to fill
74+
* in a rate sample. For each packet that was newly SACKed or ACKed,
75+
* UpdateRateSample() updates the rate sample based on a snapshot of
76+
* connection delivery information from the time at which the packet was
77+
* last transmitted. UpdateRateSample() is invoked multiple times when
78+
* a stretched ACK acknowledges multiple data packets. In this case we
79+
* use the information from the most recently sent packet, i.e., the
80+
* packet with the highest "P.delivered" value.
81+
*
82+
* haproxy implementation: quic_cc_drs_update_rate_sample() matches with
83+
* RFC UpdateRateSample() called from first part of GenerateRateSample().
84+
*/
85+
void quic_cc_drs_update_rate_sample(struct quic_cc_drs *drs,
86+
struct quic_tx_packet *pkt)
87+
{
88+
struct quic_cc_rs *rs = &drs->rs;
89+
90+
if (!tick_isset(pkt->rs.delivered_time))
91+
return;
92+
93+
drs->delivered += pkt->len;
94+
drs->delivered_time = now_ms;
95+
/* Update info using the newest packet. */
96+
if (tick_isset(rs->prior_time) && !quic_cc_drs_is_newest_packet(drs, pkt))
97+
return;
98+
99+
rs->prior_delivered = pkt->rs.delivered;
100+
rs->prior_time = pkt->rs.delivered_time;
101+
rs->is_app_limited = pkt->rs.is_app_limited;
102+
rs->send_elapsed = pkt->time_sent - pkt->rs.first_sent_time;
103+
rs->ack_elapsed = drs->delivered_time - pkt->rs.delivered_time;
104+
rs->tx_in_flight = pkt->rs.tx_in_flight;
105+
rs->prior_lost = pkt->rs.lost;
106+
rs->last_end_seq = pkt->rs.end_seq;
107+
drs->first_sent_time = pkt->time_sent;
108+
/* Mark the packet as delivered once it's SACKed to
109+
* avoid being used again when it's cumulatively acked.
110+
*/
111+
pkt->rs.delivered_time = TICK_ETERNITY;
112+
}
113+
114+
/* RFC https://datatracker.ietf.org/doc/draft-ietf-ccwg-bbr/
115+
* 4.5.2.3.3. Upon receiving an ACK
116+
*
117+
* haproxy implementation: second part of GenerateRateSample(). Follows the
118+
* first one above.
119+
*/
120+
void quic_cc_drs_on_ack_recv(struct quic_cc_drs *drs, struct quic_cc_path *path,
121+
uint64_t pkt_delivered)
122+
{
123+
struct quic_cc_rs *rs = &drs->rs;
124+
uint64_t rate;
125+
126+
if (drs->app_limited && drs->delivered > drs->app_limited)
127+
drs->app_limited = 0;
128+
129+
if (pkt_delivered >= drs->next_round_delivered) {
130+
drs->next_round_delivered = pkt_delivered;
131+
++drs->round_count;
132+
}
133+
134+
if (!tick_isset(rs->prior_time))
135+
return;
136+
137+
rs->interval = MAX(rs->send_elapsed, rs->ack_elapsed);
138+
139+
BUG_ON(drs->delivered <= rs->prior_delivered);
140+
rs->delivered = drs->delivered - rs->prior_delivered;
141+
BUG_ON(drs->lost < rs->prior_lost);
142+
rs->lost = drs->lost - rs->prior_lost;
143+
144+
if (rs->interval < path->loss.rtt_min) {
145+
rs->interval = UINT32_MAX;
146+
return;
147+
}
148+
149+
if (!rs->interval)
150+
return;
151+
152+
rate = rs->delivered * 1000 / rs->interval;
153+
if (rate >= wf_get_max(&drs->wf) || !drs->app_limited)
154+
path->delivery_rate = wf_max_update(&drs->wf, rate, drs->round_count);
155+
}

0 commit comments

Comments
 (0)