2 * Copyright 2022-2023 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)))
200 static int net_sim_process_one(struct net_sim *s, int skip_forward)
202 NET_PKT *pkt = ossl_pqueue_NET_PKT_peek(s->pkts);
207 /* Jump forward to the next significant point in time. */
208 if (skip_forward && ossl_time_compare(pkt->next_time, fake_time) > 0)
209 fake_time = pkt->next_time;
211 if (pkt->success && !pkt->arrived
212 && ossl_time_compare(fake_time, pkt->arrive_time) >= 0) {
214 s->spare_capacity += pkt->size;
217 ossl_pqueue_NET_PKT_pop(s->pkts);
218 pkt->next_time = pkt->determination_time;
219 if (!ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx))
225 if (ossl_time_compare(fake_time, pkt->determination_time) < 0)
228 if (!TEST_true(!pkt->success || pkt->arrived))
232 OSSL_CC_LOSS_INFO loss_info = {0};
234 loss_info.tx_time = pkt->tx_time;
235 loss_info.tx_size = pkt->size;
237 if (!TEST_true(s->ccm->on_data_lost(s->cc, &loss_info)))
240 if (!TEST_true(s->ccm->on_data_lost_finished(s->cc, 0)))
243 s->total_lost += pkt->size;
244 ossl_pqueue_NET_PKT_pop(s->pkts);
247 OSSL_CC_ACK_INFO ack_info = {0};
249 ack_info.tx_time = pkt->tx_time;
250 ack_info.tx_size = pkt->size;
252 if (!TEST_true(s->ccm->on_data_acked(s->cc, &ack_info)))
255 s->total_acked += pkt->size;
256 ossl_pqueue_NET_PKT_pop(s->pkts);
263 static int net_sim_process(struct net_sim *s, size_t skip_forward)
267 while ((rc = net_sim_process_one(s, skip_forward > 0 ? 1 : 0)) == 1)
268 if (skip_forward > 0)
275 * State Dumping Utilities
276 * =======================
278 * Utilities for outputting CC state information.
281 static FILE *logfile;
284 static int dump_state(const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc,
288 uint64_t cwnd_size, cur_bytes, state;
293 if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_CWND_SIZE,
297 if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
301 if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_STATE,
305 fprintf(logfile, "%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,\"%c\"\n",
306 ossl_time2ms(fake_time),
307 ccm->get_tx_allowance(cc),
324 * Simulator-based unit test in which we simulate a network with a certain
325 * capacity. The average estimated channel capacity should not be too far from
326 * the actual channel capacity.
328 static int test_simulate(void)
333 const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method;
334 OSSL_CC_DATA *cc = NULL;
336 uint64_t total_sent = 0, total_to_send, allowance;
337 uint64_t actual_capacity = 16000; /* B/s - 128kb/s */
338 uint64_t cwnd_sample_sum = 0, cwnd_sample_count = 0;
339 uint64_t diag_cur_bytes_in_flight = UINT64_MAX;
340 uint64_t diag_cur_cwnd_size = UINT64_MAX;
342 OSSL_PARAM params[3], *p = params;
344 fake_time = TIME_BASE;
346 if (!TEST_ptr(cc = ccm->new(fake_now, NULL)))
349 if (!TEST_true(net_sim_init(&sim, ccm, cc, actual_capacity, 100)))
354 *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
356 *p++ = OSSL_PARAM_construct_end();
358 if (!TEST_true(ccm->set_input_params(cc, params)))
362 *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
363 &diag_cur_bytes_in_flight);
364 *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_CWND_SIZE,
365 &diag_cur_cwnd_size);
366 *p++ = OSSL_PARAM_construct_end();
368 if (!TEST_true(ccm->bind_diagnostics(cc, params)))
373 if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), mdpl))
377 * Start generating traffic. Stop when we've sent 30 MiB.
379 total_to_send = 30 * 1024 * 1024;
381 while (total_sent < total_to_send) {
383 * Assume we are bottlenecked by the network (which is the interesting
384 * case for testing a congestion controller) and always fill our entire
385 * TX allowance as and when it becomes available.
390 dump_state(ccm, cc, &sim);
392 allowance = ccm->get_tx_allowance(cc);
393 sz = allowance > mdpl ? mdpl : allowance;
398 * QUIC minimum packet sizes, etc. mean that in practice we will not
399 * consume the allowance exactly, so only send above a certain size.
406 if (!TEST_true(net_sim_send(&sim, (size_t)sz)))
412 /* Skip to next event. */
413 rc = net_sim_process(&sim, 1);
414 if (!TEST_int_gt(rc, 0))
418 * If we are out of any events to handle at all we definitely should
419 * have at least one MDPL's worth of allowance as nothing is in flight.
422 if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0))
425 if (!TEST_uint64_t_ge(ccm->get_tx_allowance(cc), mdpl))
429 /* Update our average of the estimated channel capacity. */
433 if (!TEST_uint64_t_ne(diag_cur_bytes_in_flight, UINT64_MAX)
434 || !TEST_uint64_t_ne(diag_cur_cwnd_size, UINT64_MAX))
437 cwnd_sample_sum += v;
443 * Ensure estimated channel capacity is not too far off from actual channel
447 uint64_t estimated_capacity = cwnd_sample_sum / cwnd_sample_count;
449 double error = ((double)estimated_capacity / (double)actual_capacity) - 1.0;
451 TEST_info("est = %6llu kB/s, act=%6llu kB/s (error=%.02f%%)\n",
452 (unsigned long long)estimated_capacity,
453 (unsigned long long)actual_capacity,
457 if (!TEST_double_le(error, 0.05))
464 net_sim_cleanup(&sim);
481 * Basic test of the congestion control APIs.
483 static int test_sanity(void)
486 OSSL_CC_DATA *cc = NULL;
487 const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method;
488 OSSL_CC_LOSS_INFO loss_info = {0};
489 OSSL_CC_ACK_INFO ack_info = {0};
490 uint64_t allowance, allowance2;
491 OSSL_PARAM params[3], *p = params;
492 size_t mdpl = 1472, diag_mdpl = SIZE_MAX;
493 uint64_t diag_cur_bytes_in_flight = UINT64_MAX;
495 fake_time = TIME_BASE;
497 if (!TEST_ptr(cc = ccm->new(fake_now, NULL)))
500 /* Test configuration of options. */
501 *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
503 *p++ = OSSL_PARAM_construct_end();
505 if (!TEST_true(ccm->set_input_params(cc, params)))
511 *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
513 *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
514 &diag_cur_bytes_in_flight);
515 *p++ = OSSL_PARAM_construct_end();
517 if (!TEST_true(ccm->bind_diagnostics(cc, params))
518 || !TEST_size_t_eq(diag_mdpl, 1472))
521 if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1472))
524 /* There is TX allowance so wakeup should be immediate */
525 if (!TEST_true(ossl_time_is_zero(ccm->get_wakeup_deadline(cc))))
528 /* No bytes should currently be in flight. */
529 if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0))
532 /* Tell the CC we have sent some data. */
533 if (!TEST_true(ccm->on_data_sent(cc, 1200)))
536 /* Allowance should have decreased. */
537 if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200))
540 /* Acknowledge the data. */
541 ack_info.tx_time = fake_time;
542 ack_info.tx_size = 1200;
544 if (!TEST_true(ccm->on_data_acked(cc, &ack_info)))
547 /* Allowance should have returned. */
548 if (!TEST_uint64_t_ge(allowance2 = ccm->get_tx_allowance(cc), allowance))
551 /* Test invalidation. */
552 if (!TEST_true(ccm->on_data_sent(cc, 1200)))
555 /* Allowance should have decreased. */
556 if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200))
559 if (!TEST_true(ccm->on_data_invalidated(cc, 1200)))
562 /* Allowance should have returned. */
563 if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance2))
567 if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1200 + 1300))
570 if (!TEST_true(ccm->on_data_sent(cc, 1200)))
573 if (!TEST_true(ccm->on_data_sent(cc, 1300)))
576 if (!TEST_uint64_t_eq(allowance2 = ccm->get_tx_allowance(cc),
577 allowance - 1200 - 1300))
580 loss_info.tx_time = fake_time;
581 loss_info.tx_size = 1200;
584 if (!TEST_true(ccm->on_data_lost(cc, &loss_info)))
587 loss_info.tx_size = 1300;
588 if (!TEST_true(ccm->on_data_lost(cc, &loss_info)))
591 if (!TEST_true(ccm->on_data_lost_finished(cc, 0)))
594 /* Allowance should have changed due to the lost calls */
595 if (!TEST_uint64_t_ne(ccm->get_tx_allowance(cc), allowance2))
598 /* But it should not be as high as the original value */
599 if (!TEST_uint64_t_lt(ccm->get_tx_allowance(cc), allowance))
611 int setup_tests(void)
615 logfile = fopen("quic_cc_stats.csv", "w");
620 "\"Bytes in Flight\","
621 "\"Total Acked\",\"Total Lost\","
622 "\"Capacity\",\"Spare Capacity\","
626 ADD_TEST(test_simulate);
627 ADD_TEST(test_sanity);