quic_cc_test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. /* For generating debug statistics during congestion controller development. */
  10. /*#define GENERATE_LOG*/
  11. #include "testutil.h"
  12. #include <openssl/ssl.h>
  13. #include "internal/quic_cc.h"
  14. #include "internal/priority_queue.h"
  15. /*
  16. * Time Simulation
  17. * ===============
  18. */
  19. static OSSL_TIME fake_time = {0};
  20. #define TIME_BASE (ossl_ticks2time(5 * OSSL_TIME_SECOND))
  21. static OSSL_TIME fake_now(void *arg)
  22. {
  23. return fake_time;
  24. }
  25. static void step_time(uint32_t ms)
  26. {
  27. fake_time = ossl_time_add(fake_time, ossl_ms2time(ms));
  28. }
  29. /*
  30. * Network Simulation
  31. * ==================
  32. *
  33. * This is a simple 'network simulator' which emulates a network with a certain
  34. * bandwidth and latency. Sending a packet into the network causes it to consume
  35. * some capacity of the network until the packet exits the network. Note that
  36. * the capacity is not known to the congestion controller as the entire point of
  37. * a congestion controller is to correctly estimate this capacity and this is
  38. * what we are testing. The network simulator does take care of informing the
  39. * congestion controller of ack/loss events automatically but the caller is
  40. * responsible for querying the congestion controller and choosing the size of
  41. * simulated transmitted packets.
  42. */
  43. typedef struct net_pkt_st {
  44. /*
  45. * The time at which the packet was sent.
  46. */
  47. OSSL_TIME tx_time;
  48. /*
  49. * The time at which the simulated packet arrives at the RX side (success)
  50. * or is dropped (!success).
  51. */
  52. OSSL_TIME arrive_time;
  53. /*
  54. * The time at which the transmitting side makes a determination of
  55. * acknowledgement (if success) or loss (if !success).
  56. */
  57. OSSL_TIME determination_time;
  58. /*
  59. * Current earliest time there is something to be done for this packet.
  60. * min(arrive_time, determination_time).
  61. */
  62. OSSL_TIME next_time;
  63. /* 1 if the packet will be successfully delivered, 0 if it is to be lost. */
  64. int success;
  65. /* 1 if we have already processed packet arrival. */
  66. int arrived;
  67. /* Size of simulated packet in bytes. */
  68. size_t size;
  69. /* pqueue internal index. */
  70. size_t idx;
  71. } NET_PKT;
  72. DEFINE_PRIORITY_QUEUE_OF(NET_PKT);
  73. static int net_pkt_cmp(const NET_PKT *a, const NET_PKT *b)
  74. {
  75. return ossl_time_compare(a->next_time, b->next_time);
  76. }
  77. struct net_sim {
  78. const OSSL_CC_METHOD *ccm;
  79. OSSL_CC_DATA *cc;
  80. uint64_t capacity; /* bytes/s */
  81. uint64_t latency; /* ms */
  82. uint64_t spare_capacity;
  83. PRIORITY_QUEUE_OF(NET_PKT) *pkts;
  84. uint64_t total_acked, total_lost; /* bytes */
  85. };
  86. static int net_sim_init(struct net_sim *s,
  87. const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc,
  88. uint64_t capacity, uint64_t latency)
  89. {
  90. s->ccm = ccm;
  91. s->cc = cc;
  92. s->capacity = capacity;
  93. s->latency = latency;
  94. s->spare_capacity = capacity;
  95. s->total_acked = 0;
  96. s->total_lost = 0;
  97. if (!TEST_ptr(s->pkts = ossl_pqueue_NET_PKT_new(net_pkt_cmp)))
  98. return 0;
  99. return 1;
  100. }
  101. static void do_free(NET_PKT *pkt)
  102. {
  103. OPENSSL_free(pkt);
  104. }
  105. static void net_sim_cleanup(struct net_sim *s)
  106. {
  107. ossl_pqueue_NET_PKT_pop_free(s->pkts, do_free);
  108. }
  109. static int net_sim_process(struct net_sim *s, size_t skip_forward);
  110. static int net_sim_send(struct net_sim *s, size_t sz)
  111. {
  112. NET_PKT *pkt = OPENSSL_zalloc(sizeof(*pkt));
  113. int success;
  114. if (!TEST_ptr(pkt))
  115. return 0;
  116. /*
  117. * Ensure we have processed any events which have come due as these might
  118. * increase our spare capacity.
  119. */
  120. if (!TEST_true(net_sim_process(s, 0)))
  121. goto err;
  122. /* Do we have room for the packet in the network? */
  123. success = (sz <= s->spare_capacity);
  124. pkt->tx_time = fake_time;
  125. pkt->success = success;
  126. if (success) {
  127. /* This packet will arrive successfully after |latency| time. */
  128. pkt->arrive_time = ossl_time_add(pkt->tx_time,
  129. ossl_ms2time(s->latency));
  130. /* Assume all received packets are acknowledged immediately. */
  131. pkt->determination_time = ossl_time_add(pkt->arrive_time,
  132. ossl_ms2time(s->latency));
  133. pkt->next_time = pkt->arrive_time;
  134. s->spare_capacity -= sz;
  135. } else {
  136. /*
  137. * In our network model, assume all packets are dropped due to a
  138. * bottleneck at the peer's NIC RX queue; thus dropping occurs after
  139. * |latency|.
  140. */
  141. pkt->arrive_time = ossl_time_add(pkt->tx_time,
  142. ossl_ms2time(s->latency));
  143. /*
  144. * It will take longer to detect loss than to detect acknowledgement.
  145. */
  146. pkt->determination_time = ossl_time_add(pkt->tx_time,
  147. ossl_ms2time(3 * s->latency));
  148. pkt->next_time = pkt->determination_time;
  149. }
  150. pkt->size = sz;
  151. if (!TEST_true(s->ccm->on_data_sent(s->cc, sz)))
  152. goto err;
  153. if (!TEST_true(ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx)))
  154. goto err;
  155. return 1;
  156. err:
  157. OPENSSL_free(pkt);
  158. return 0;
  159. }
  160. static int net_sim_process_one(struct net_sim *s, int skip_forward)
  161. {
  162. NET_PKT *pkt = ossl_pqueue_NET_PKT_peek(s->pkts);
  163. if (pkt == NULL)
  164. return 3;
  165. /* Jump forward to the next significant point in time. */
  166. if (skip_forward && ossl_time_compare(pkt->next_time, fake_time) > 0)
  167. fake_time = pkt->next_time;
  168. if (pkt->success && !pkt->arrived
  169. && ossl_time_compare(fake_time, pkt->arrive_time) >= 0) {
  170. /* Packet arrives */
  171. s->spare_capacity += pkt->size;
  172. pkt->arrived = 1;
  173. ossl_pqueue_NET_PKT_pop(s->pkts);
  174. pkt->next_time = pkt->determination_time;
  175. if (!ossl_pqueue_NET_PKT_push(s->pkts, pkt, &pkt->idx))
  176. return 0;
  177. return 1;
  178. }
  179. if (ossl_time_compare(fake_time, pkt->determination_time) < 0)
  180. return 2;
  181. if (!TEST_true(!pkt->success || pkt->arrived))
  182. return 0;
  183. if (!pkt->success) {
  184. OSSL_CC_LOSS_INFO loss_info = {0};
  185. loss_info.tx_time = pkt->tx_time;
  186. loss_info.tx_size = pkt->size;
  187. if (!TEST_true(s->ccm->on_data_lost(s->cc, &loss_info)))
  188. return 0;
  189. if (!TEST_true(s->ccm->on_data_lost_finished(s->cc, 0)))
  190. return 0;
  191. s->total_lost += pkt->size;
  192. ossl_pqueue_NET_PKT_pop(s->pkts);
  193. OPENSSL_free(pkt);
  194. } else {
  195. OSSL_CC_ACK_INFO ack_info = {0};
  196. ack_info.tx_time = pkt->tx_time;
  197. ack_info.tx_size = pkt->size;
  198. if (!TEST_true(s->ccm->on_data_acked(s->cc, &ack_info)))
  199. return 0;
  200. s->total_acked += pkt->size;
  201. ossl_pqueue_NET_PKT_pop(s->pkts);
  202. OPENSSL_free(pkt);
  203. }
  204. return 1;
  205. }
  206. static int net_sim_process(struct net_sim *s, size_t skip_forward)
  207. {
  208. int rc;
  209. while ((rc = net_sim_process_one(s, skip_forward > 0 ? 1 : 0)) == 1)
  210. if (skip_forward > 0)
  211. --skip_forward;
  212. return rc;
  213. }
  214. /*
  215. * State Dumping Utilities
  216. * =======================
  217. *
  218. * Utilities for outputting CC state information.
  219. */
  220. #ifdef GENERATE_LOG
  221. static FILE *logfile;
  222. #endif
  223. static int dump_state(const OSSL_CC_METHOD *ccm, OSSL_CC_DATA *cc,
  224. struct net_sim *s)
  225. {
  226. #ifdef GENERATE_LOG
  227. uint64_t cwnd_size, cur_bytes, state;
  228. if (logfile == NULL)
  229. return 1;
  230. if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_CWND_SIZE,
  231. &cwnd_size)))
  232. return 0;
  233. if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
  234. &cur_bytes)))
  235. return 0;
  236. if (!TEST_true(ccm->get_option_uint(cc, OSSL_CC_OPTION_CUR_STATE,
  237. &state)))
  238. return 0;
  239. fprintf(logfile, "%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,%10lu,\"%c\"\n",
  240. ossl_time2ms(fake_time),
  241. ccm->get_tx_allowance(cc),
  242. cwnd_size,
  243. cur_bytes,
  244. s->total_acked,
  245. s->total_lost,
  246. s->capacity,
  247. s->spare_capacity,
  248. (char)state);
  249. #endif
  250. return 1;
  251. }
  252. /*
  253. * Simulation Test
  254. * ===============
  255. *
  256. * Simulator-based unit test in which we simulate a network with a certain
  257. * capacity. The average estimated channel capacity should not be too far from
  258. * the actual channel capacity.
  259. */
  260. static int test_simulate(void)
  261. {
  262. int testresult = 0;
  263. int rc;
  264. int have_sim = 0;
  265. const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method;
  266. OSSL_CC_DATA *cc = NULL;
  267. size_t mdpl = 1472;
  268. uint64_t total_sent = 0, total_to_send, allowance;
  269. uint64_t actual_capacity = 16000; /* B/s - 128kb/s */
  270. uint64_t cwnd_sample_sum = 0, cwnd_sample_count = 0;
  271. uint64_t diag_cur_bytes_in_flight = UINT64_MAX;
  272. uint64_t diag_cur_cwnd_size = UINT64_MAX;
  273. struct net_sim sim;
  274. OSSL_PARAM params[3], *p = params;
  275. fake_time = TIME_BASE;
  276. if (!TEST_ptr(cc = ccm->new(fake_now, NULL)))
  277. goto err;
  278. if (!TEST_true(net_sim_init(&sim, ccm, cc, actual_capacity, 100)))
  279. goto err;
  280. have_sim = 1;
  281. *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
  282. &mdpl);
  283. *p++ = OSSL_PARAM_construct_end();
  284. if (!TEST_true(ccm->set_input_params(cc, params)))
  285. goto err;
  286. p = params;
  287. *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
  288. &diag_cur_bytes_in_flight);
  289. *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_CWND_SIZE,
  290. &diag_cur_cwnd_size);
  291. *p++ = OSSL_PARAM_construct_end();
  292. if (!TEST_true(ccm->bind_diagnostics(cc, params)))
  293. goto err;
  294. ccm->reset(cc);
  295. if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), mdpl))
  296. goto err;
  297. /*
  298. * Start generating traffic. Stop when we've sent 30 MiB.
  299. */
  300. total_to_send = 30 * 1024 * 1024;
  301. while (total_sent < total_to_send) {
  302. /*
  303. * Assume we are bottlenecked by the network (which is the interesting
  304. * case for testing a congestion controller) and always fill our entire
  305. * TX allowance as and when it becomes available.
  306. */
  307. for (;;) {
  308. uint64_t sz;
  309. dump_state(ccm, cc, &sim);
  310. allowance = ccm->get_tx_allowance(cc);
  311. sz = allowance > mdpl ? mdpl : allowance;
  312. if (sz > SIZE_MAX)
  313. sz = SIZE_MAX;
  314. /*
  315. * QUIC minimum packet sizes, etc. mean that in practice we will not
  316. * consume the allowance exactly, so only send above a certain size.
  317. */
  318. if (sz < 30)
  319. break;
  320. step_time(7);
  321. if (!TEST_true(net_sim_send(&sim, (size_t)sz)))
  322. goto err;
  323. total_sent += sz;
  324. }
  325. /* Skip to next event. */
  326. rc = net_sim_process(&sim, 1);
  327. if (!TEST_int_gt(rc, 0))
  328. goto err;
  329. /*
  330. * If we are out of any events to handle at all we definitely should
  331. * have at least one MDPL's worth of allowance as nothing is in flight.
  332. */
  333. if (rc == 3) {
  334. if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0))
  335. goto err;
  336. if (!TEST_uint64_t_ge(ccm->get_tx_allowance(cc), mdpl))
  337. goto err;
  338. }
  339. /* Update our average of the estimated channel capacity. */
  340. {
  341. uint64_t v = 1;
  342. if (!TEST_uint64_t_ne(diag_cur_bytes_in_flight, UINT64_MAX)
  343. || !TEST_uint64_t_ne(diag_cur_cwnd_size, UINT64_MAX))
  344. goto err;
  345. cwnd_sample_sum += v;
  346. ++cwnd_sample_count;
  347. }
  348. }
  349. /*
  350. * Ensure estimated channel capacity is not too far off from actual channel
  351. * capacity.
  352. */
  353. {
  354. uint64_t estimated_capacity = cwnd_sample_sum / cwnd_sample_count;
  355. double error = ((double)estimated_capacity / (double)actual_capacity) - 1.0;
  356. TEST_info("est = %6llu kB/s, act=%6llu kB/s (error=%.02f%%)\n",
  357. (unsigned long long)estimated_capacity,
  358. (unsigned long long)actual_capacity,
  359. error * 100.0);
  360. /* Max 5% error */
  361. if (!TEST_double_le(error, 0.05))
  362. goto err;
  363. }
  364. testresult = 1;
  365. err:
  366. if (have_sim)
  367. net_sim_cleanup(&sim);
  368. if (cc != NULL)
  369. ccm->free(cc);
  370. #ifdef GENERATE_LOG
  371. if (logfile != NULL)
  372. fflush(logfile);
  373. #endif
  374. return testresult;
  375. }
  376. /*
  377. * Sanity Test
  378. * ===========
  379. *
  380. * Basic test of the congestion control APIs.
  381. */
  382. static int test_sanity(void)
  383. {
  384. int testresult = 0;
  385. OSSL_CC_DATA *cc = NULL;
  386. const OSSL_CC_METHOD *ccm = &ossl_cc_newreno_method;
  387. OSSL_CC_LOSS_INFO loss_info = {0};
  388. OSSL_CC_ACK_INFO ack_info = {0};
  389. uint64_t allowance, allowance2;
  390. OSSL_PARAM params[3], *p = params;
  391. size_t mdpl = 1472, diag_mdpl = SIZE_MAX;
  392. uint64_t diag_cur_bytes_in_flight = UINT64_MAX;
  393. fake_time = TIME_BASE;
  394. if (!TEST_ptr(cc = ccm->new(fake_now, NULL)))
  395. goto err;
  396. /* Test configuration of options. */
  397. *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
  398. &mdpl);
  399. *p++ = OSSL_PARAM_construct_end();
  400. if (!TEST_true(ccm->set_input_params(cc, params)))
  401. goto err;
  402. ccm->reset(cc);
  403. p = params;
  404. *p++ = OSSL_PARAM_construct_size_t(OSSL_CC_OPTION_MAX_DGRAM_PAYLOAD_LEN,
  405. &diag_mdpl);
  406. *p++ = OSSL_PARAM_construct_uint64(OSSL_CC_OPTION_CUR_BYTES_IN_FLIGHT,
  407. &diag_cur_bytes_in_flight);
  408. *p++ = OSSL_PARAM_construct_end();
  409. if (!TEST_true(ccm->bind_diagnostics(cc, params))
  410. || !TEST_size_t_eq(diag_mdpl, 1472))
  411. goto err;
  412. if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1472))
  413. goto err;
  414. /* There is TX allowance so wakeup should be immediate */
  415. if (!TEST_true(ossl_time_is_zero(ccm->get_wakeup_deadline(cc))))
  416. goto err;
  417. /* No bytes should currently be in flight. */
  418. if (!TEST_uint64_t_eq(diag_cur_bytes_in_flight, 0))
  419. goto err;
  420. /* Tell the CC we have sent some data. */
  421. if (!TEST_true(ccm->on_data_sent(cc, 1200)))
  422. goto err;
  423. /* Allowance should have decreased. */
  424. if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200))
  425. goto err;
  426. /* Acknowledge the data. */
  427. ack_info.tx_time = fake_time;
  428. ack_info.tx_size = 1200;
  429. step_time(100);
  430. if (!TEST_true(ccm->on_data_acked(cc, &ack_info)))
  431. goto err;
  432. /* Allowance should have returned. */
  433. if (!TEST_uint64_t_ge(allowance2 = ccm->get_tx_allowance(cc), allowance))
  434. goto err;
  435. /* Test invalidation. */
  436. if (!TEST_true(ccm->on_data_sent(cc, 1200)))
  437. goto err;
  438. /* Allowance should have decreased. */
  439. if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance - 1200))
  440. goto err;
  441. if (!TEST_true(ccm->on_data_invalidated(cc, 1200)))
  442. goto err;
  443. /* Allowance should have returned. */
  444. if (!TEST_uint64_t_eq(ccm->get_tx_allowance(cc), allowance2))
  445. goto err;
  446. /* Test loss. */
  447. if (!TEST_uint64_t_ge(allowance = ccm->get_tx_allowance(cc), 1200 + 1300))
  448. goto err;
  449. if (!TEST_true(ccm->on_data_sent(cc, 1200)))
  450. goto err;
  451. if (!TEST_true(ccm->on_data_sent(cc, 1300)))
  452. goto err;
  453. if (!TEST_uint64_t_eq(allowance2 = ccm->get_tx_allowance(cc),
  454. allowance - 1200 - 1300))
  455. goto err;
  456. loss_info.tx_time = fake_time;
  457. loss_info.tx_size = 1200;
  458. step_time(100);
  459. if (!TEST_true(ccm->on_data_lost(cc, &loss_info)))
  460. goto err;
  461. loss_info.tx_size = 1300;
  462. if (!TEST_true(ccm->on_data_lost(cc, &loss_info)))
  463. goto err;
  464. if (!TEST_true(ccm->on_data_lost_finished(cc, 0)))
  465. goto err;
  466. /* Allowance should have changed due to the lost calls */
  467. if (!TEST_uint64_t_ne(ccm->get_tx_allowance(cc), allowance2))
  468. goto err;
  469. /* But it should not be as high as the original value */
  470. if (!TEST_uint64_t_lt(ccm->get_tx_allowance(cc), allowance))
  471. goto err;
  472. testresult = 1;
  473. err:
  474. if (cc != NULL)
  475. ccm->free(cc);
  476. return testresult;
  477. }
  478. int setup_tests(void)
  479. {
  480. #ifdef GENERATE_LOG
  481. logfile = fopen("quic_cc_stats.csv", "w");
  482. fprintf(logfile,
  483. "\"Time\","
  484. "\"TX Allowance\","
  485. "\"CWND Size\","
  486. "\"Bytes in Flight\","
  487. "\"Total Acked\",\"Total Lost\","
  488. "\"Capacity\",\"Spare Capacity\","
  489. "\"State\"\n");
  490. #endif
  491. ADD_TEST(test_simulate);
  492. ADD_TEST(test_sanity);
  493. return 1;
  494. }