2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* For generating debug statistics during congestion controller development. */
11 /*#define GENERATE_LOG*/
14 #include <openssl/ssl.h>
15 #include "internal/quic_cc.h"
16 #include "internal/priority_queue.h"
22 static OSSL_TIME fake_time = {0};
24 #define TIME_BASE (ossl_ticks2time(5 * OSSL_TIME_SECOND))
26 static OSSL_TIME fake_now(void *arg)
31 static void step_time(uint32_t ms)
33 fake_time = ossl_time_add(fake_time, ossl_ms2time(ms));
40 * This is a simple 'network simulator' which emulates a network with a certain
41 * bandwidth and latency. Sending a packet into the network causes it to consume
42 * some capacity of the network until the packet exits the network. Note that
43 * the capacity is not known to the congestion controller as the entire point of
44 * a congestion controller is to correctly estimate this capacity and this is
45 * what we are testing. The network simulator does take care of informing the
46 * congestion controller of ack/loss events automatically but the caller is
47 * responsible for querying the congestion controller and choosing the size of
48 * simulated transmitted packets.
50 typedef struct net_pkt_st {
52 * The time at which the packet was sent.
57 * The time at which the simulated packet arrives at the RX side (success)
58 * or is dropped (!success).
60 OSSL_TIME arrive_time;
63 * The time at which the transmitting side makes a determination of
64 * acknowledgement (if success) or loss (if !success).
66 OSSL_TIME determination_time;
69 * Current earliest time there is something to be done for this packet.
70 * min(arrive_time, determination_time).
74 /* 1 if the packet will be successfully delivered, 0 if it is to be lost. */
77 /* 1 if we have already processed packet arrival. */
80 /* Size of simulated packet in bytes. */
83 /* pqueue internal index. */
87 DEFINE_PRIORITY_QUEUE_OF(NET_PKT);
89 static int net_pkt_cmp(const NET_PKT *a, const NET_PKT *b)
91 return ossl_time_compare(a->next_time, b->next_time);
95 const OSSL_CC_METHOD *ccm;
98 uint64_t capacity; /* bytes/s */
99 uint64_t latency; /* ms */
101 uint64_t spare_capacity;
102 PRIORITY_QUEUE_OF(NET_PKT) *pkts;
104 uint64_t total_acked, total_lost; /* bytes */
107 static int net_sim_init(struct net_sim *s,
108 const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc,
109 uint64_t capacity, uint64_t latency)
114 s->capacity = capacity;
115 s->latency = latency;
117 s->spare_capacity = capacity;
122 if (!TEST_ptr(s->pkts = ossl_pqueue_NET_PKT_new(net_pkt_cmp)))
128 static void do_free(NET_PKT *pkt)
133 static void net_sim_cleanup(struct net_sim *s)
135 ossl_pqueue_NET_PKT_pop_free(s->pkts, do_free);
138 static int net_sim_process(struct net_sim *s, size_t skip_forward);
140 static int net_sim_send(struct net_sim *s, size_t sz)
142 NET_PKT *pkt = OPENSSL_zalloc(sizeof(*pkt));
149 * Ensure we have processed any events which have come due as these might
150 * increase our spare capacity.
152 if (!TEST_true(net_sim_process(s, 0)))
155 /* Do we have room for the packet in the network? */
156 success = (sz <= s->spare_capacity);
158 pkt->tx_time = fake_time;
159 pkt->success = success;
161 /* This packet will arrive successfully after |latency| time. */
162 pkt->arrive_time = ossl_time_add(pkt->tx_time,
163 ossl_ms2time(s->latency));
164 /* Assume all received packets are acknowledged immediately. */
165 pkt->determination_time = ossl_time_add(pkt->arrive_time,
166 ossl_ms2time(s->latency));
167 pkt->next_time = pkt->arrive_time;
168 s->spare_capacity -= sz;
171 * In our network model, assume all packets are dropped due to a
172 * bottleneck at the peer's NIC RX queue; thus dropping occurs after
175 pkt->arrive_time = ossl_time_add(pkt->tx_time,
176 ossl_ms2time(s->latency));
178 * It will take longer to detect loss than to detect acknowledgement.
180 pkt->determination_time = ossl_time_add(pkt->tx_time,
181 ossl_ms2time(3 * s->latency));
182 pkt->next_time = pkt->determination_time;
187 if (!TEST_true(s->ccm->on_data_sent(s->cc, sz)))
190 if (!TEST_true(ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx)))
196 static int net_sim_process_one(struct net_sim *s, int skip_forward)
198 NET_PKT *pkt = ossl_pqueue_NET_PKT_peek(s->pkts);
203 /* Jump forward to the next significant point in time. */
204 if (skip_forward && ossl_time_compare(pkt->next_time, fake_time) > 0)
205 fake_time = pkt->next_time;
207 if (pkt->success && !pkt->arrived
208 && ossl_time_compare(fake_time, pkt->arrive_time) >= 0) {
210 s->spare_capacity += pkt->size;
213 ossl_pqueue_NET_PKT_pop(s->pkts);
214 pkt->next_time = pkt->determination_time;
215 if (!ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx))
221 if (ossl_time_compare(fake_time, pkt->determination_time) < 0)
224 if (!TEST_true(!pkt->success || pkt->arrived))
228 OSSL_CC_LOSS_INFO loss_info = {0};
230 loss_info.tx_time = pkt->tx_time;
231 loss_info.tx_size = pkt->size;
233 if (!TEST_true(s->ccm->on_data_lost(s->cc, &loss_info)))
236 if (!TEST_true(s->ccm->on_data_lost_finished(s->cc, 0)))
239 s->total_lost += pkt->size;
240 ossl_pqueue_NET_PKT_pop(s->pkts);
243 OSSL_CC_ACK_INFO ack_info = {0};
245 ack_info.tx_time = pkt->tx_time;
246 ack_info.tx_size = pkt->size;
248 if (!TEST_true(s->ccm->on_data_acked(s->cc, &ack_info)))
251 s->total_acked += pkt->size;
252 ossl_pqueue_NET_PKT_pop(s->pkts);
259 static int net_sim_process(struct net_sim *s, size_t skip_forward)
263 while ((rc = net_sim_process_one(s, skip_forward > 0 ? 1 : 0)) == 1)
264 if (skip_forward > 0)
271 * State Dumping Utilities
272 * =======================
274 * Utilities for outputting CC state information.
277 static FILE *logfile;
280 static int dump_state(const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc,
284 uint64_t cwnd_size, cur_bytes, state;
289 if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_CWND_SIZE,
293 if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
297 if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_STATE,
301 fprintf(logfile, "%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,\"%c\"\n",
302 ossl_time2ms(fake_time),
303 ccm->get_tx_allowance(cc),
320 * Simulator-based unit test in which we simulate a network with a certain
321 * capacity. The average estimated channel capacity should not be too far from
322 * the actual channel capacity.
324 static int test_simulate(void)
329 const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method;
330 OSSL_CC_DATA *cc = NULL;
332 uint64_t total_sent = 0, total_to_send, allowance;
333 uint64_t actual_capacity = 16000; /* B/s - 128kb/s */
334 uint64_t cwnd_sample_sum = 0, cwnd_sample_count = 0;
335 uint64_t diag_cur_bytes_in_flight = UINT64_MAX;
336 uint64_t diag_cur_cwnd_size = UINT64_MAX;
338 OSSL_PARAM params[3], *p = params;
340 fake_time = TIME_BASE;
342 if (!TEST_ptr(cc = ccm->new(fake_now, NULL)))
345 if (!TEST_true(net_sim_init(&sim, ccm, cc, actual_capacity, 100)))
350 *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
352 *p++ = OSSL_PARAM_construct_end();
354 if (!TEST_true(ccm->set_input_params(cc, params)))
358 *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
359 &diag_cur_bytes_in_flight);
360 *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_CWND_SIZE,
361 &diag_cur_cwnd_size);
362 *p++ = OSSL_PARAM_construct_end();
364 if (!TEST_true(ccm->bind_diagnostics(cc, params)))
369 if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), mdpl))
373 * Start generating traffic. Stop when we've sent 30 MiB.
375 total_to_send = 30 * 1024 * 1024;
377 while (total_sent < total_to_send) {
379 * Assume we are bottlenecked by the network (which is the interesting
380 * case for testing a congestion controller) and always fill our entire
381 * TX allowance as and when it becomes available.
386 dump_state(ccm, cc, &sim);
388 allowance = ccm->get_tx_allowance(cc);
389 sz = allowance > mdpl ? mdpl : allowance;
394 * QUIC minimum packet sizes, etc. mean that in practice we will not
395 * consume the allowance exactly, so only send above a certain size.
402 if (!TEST_true(net_sim_send(&sim, (size_t)sz)))
408 /* Skip to next event. */
409 rc = net_sim_process(&sim, 1);
410 if (!TEST_int_gt(rc, 0))
414 * If we are out of any events to handle at all we definitely should
415 * have at least one MDPL's worth of allowance as nothing is in flight.
418 if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0))
421 if (!TEST_uint64_t_ge(ccm->get_tx_allowance(cc), mdpl))
425 /* Update our average of the estimated channel capacity. */
429 if (!TEST_uint64_t_ne(diag_cur_bytes_in_flight, UINT64_MAX)
430 || !TEST_uint64_t_ne(diag_cur_cwnd_size, UINT64_MAX))
433 cwnd_sample_sum += v;
439 * Ensure estimated channel capacity is not too far off from actual channel
443 uint64_t estimated_capacity = cwnd_sample_sum / cwnd_sample_count;
445 double error = ((double)estimated_capacity / (double)actual_capacity) - 1.0;
447 TEST_info("est = %6lu kB/s, act=%6lu kB/s (error=%.02f%%)\n",
448 estimated_capacity, actual_capacity, error * 100.0);
451 if (!TEST_double_le(error, 0.05))
458 net_sim_cleanup(&sim);
475 * Basic test of the congestion control APIs.
477 static int test_sanity(void)
480 OSSL_CC_DATA *cc = NULL;
481 const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method;
482 OSSL_CC_LOSS_INFO loss_info = {0};
483 OSSL_CC_ACK_INFO ack_info = {0};
484 uint64_t allowance, allowance2;
485 OSSL_PARAM params[3], *p = params;
486 size_t mdpl = 1472, diag_mdpl = SIZE_MAX;
487 uint64_t diag_cur_bytes_in_flight = UINT64_MAX;
489 fake_time = TIME_BASE;
491 if (!TEST_ptr(cc = ccm->new(fake_now, NULL)))
494 /* Test configuration of options. */
495 *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
497 *p++ = OSSL_PARAM_construct_end();
499 if (!TEST_true(ccm->set_input_params(cc, params)))
505 *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
507 *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
508 &diag_cur_bytes_in_flight);
509 *p++ = OSSL_PARAM_construct_end();
511 if (!TEST_true(ccm->bind_diagnostics(cc, params))
512 || !TEST_size_t_eq(diag_mdpl, 1472))
515 if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1472))
519 * No wakeups should be scheduled currently as we don't currently implement
522 if (!TEST_true(ossl_time_is_infinite(ccm->get_wakeup_deadline(cc))))
525 /* No bytes should currently be in flight. */
526 if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0))
529 /* Tell the CC we have sent some data. */
530 if (!TEST_true(ccm->on_data_sent(cc, 1200)))
533 /* Allowance should have decreased. */
534 if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200))
537 /* Acknowledge the data. */
538 ack_info.tx_time = fake_time;
539 ack_info.tx_size = 1200;
541 if (!TEST_true(ccm->on_data_acked(cc, &ack_info)))
544 /* Allowance should have returned. */
545 if (!TEST_uint64_t_ge(allowance2 = ccm->get_tx_allowance(cc), allowance))
548 /* Test invalidation. */
549 if (!TEST_true(ccm->on_data_sent(cc, 1200)))
552 /* Allowance should have decreased. */
553 if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200))
556 if (!TEST_true(ccm->on_data_invalidated(cc, 1200)))
559 /* Allowance should have returned. */
560 if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance2))
564 if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1200 + 1300))
567 if (!TEST_true(ccm->on_data_sent(cc, 1200)))
570 if (!TEST_true(ccm->on_data_sent(cc, 1300)))
573 if (!TEST_uint64_t_eq(allowance2 = ccm->get_tx_allowance(cc),
574 allowance - 1200 - 1300))
577 loss_info.tx_time = fake_time;
578 loss_info.tx_size = 1200;
581 if (!TEST_true(ccm->on_data_lost(cc, &loss_info)))
584 loss_info.tx_size = 1300;
585 if (!TEST_true(ccm->on_data_lost(cc, &loss_info)))
588 if (!TEST_true(ccm->on_data_lost_finished(cc, 0)))
591 /* Allowance should have changed due to the lost calls */
592 if (!TEST_uint64_t_ne(ccm->get_tx_allowance(cc), allowance2))
595 /* But it should not be as high as the original value */
596 if (!TEST_uint64_t_lt(ccm->get_tx_allowance(cc), allowance))
608 int setup_tests(void)
612 logfile = fopen("quic_cc_stats.csv", "w");
617 "\"Bytes in Flight\","
618 "\"Total Acked\",\"Total Lost\","
619 "\"Capacity\",\"Spare Capacity\","
623 ADD_TEST(test_simulate);
624 ADD_TEST(test_sanity);