quic_ackm_test.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * Copyright 2022 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. #include "testutil.h"
  10. #include <openssl/ssl.h>
  11. #include "internal/quic_ackm.h"
  12. #include "internal/quic_cc.h"
  13. static OSSL_TIME fake_time = {0};
  14. #define TIME_BASE (ossl_ticks2time(123 * OSSL_TIME_SECOND))
  15. static OSSL_TIME fake_now(void *arg)
  16. {
  17. return fake_time;
  18. }
  19. struct pkt_info {
  20. OSSL_ACKM_TX_PKT *pkt;
  21. int lost, acked, discarded;
  22. };
  23. static void on_lost(void *arg)
  24. {
  25. struct pkt_info *info = arg;
  26. ++info->lost;
  27. }
  28. static void on_acked(void *arg)
  29. {
  30. struct pkt_info *info = arg;
  31. ++info->acked;
  32. }
  33. static void on_discarded(void *arg)
  34. {
  35. struct pkt_info *info = arg;
  36. ++info->discarded;
  37. }
  38. struct helper {
  39. OSSL_ACKM *ackm;
  40. struct pkt_info *pkts;
  41. size_t num_pkts;
  42. OSSL_CC_DATA *ccdata;
  43. OSSL_STATM statm;
  44. int have_statm;
  45. };
  46. static void helper_destroy(struct helper *h)
  47. {
  48. size_t i;
  49. if (h->ackm != NULL) {
  50. ossl_ackm_free(h->ackm);
  51. h->ackm = NULL;
  52. }
  53. if (h->ccdata != NULL) {
  54. ossl_cc_dummy_method.free(h->ccdata);
  55. h->ccdata = NULL;
  56. }
  57. if (h->have_statm) {
  58. ossl_statm_destroy(&h->statm);
  59. h->have_statm = 0;
  60. }
  61. if (h->pkts != NULL) {
  62. for (i = 0; i < h->num_pkts; ++i) {
  63. OPENSSL_free(h->pkts[i].pkt);
  64. h->pkts[i].pkt = NULL;
  65. }
  66. OPENSSL_free(h->pkts);
  67. h->pkts = NULL;
  68. }
  69. }
  70. static int helper_init(struct helper *h, size_t num_pkts)
  71. {
  72. int rc = 0;
  73. memset(h, 0, sizeof(*h));
  74. fake_time = TIME_BASE;
  75. /* Initialise statistics tracker. */
  76. if (!TEST_int_eq(ossl_statm_init(&h->statm), 1))
  77. goto err;
  78. h->have_statm = 1;
  79. /* Initialise congestion controller. */
  80. h->ccdata = ossl_cc_dummy_method.new(NULL, NULL, NULL);
  81. if (!TEST_ptr(h->ccdata))
  82. goto err;
  83. /* Initialise ACK manager. */
  84. h->ackm = ossl_ackm_new(fake_now, NULL, &h->statm,
  85. &ossl_cc_dummy_method, h->ccdata);
  86. if (!TEST_ptr(h->ackm))
  87. goto err;
  88. /* Allocate our array of packet information. */
  89. h->num_pkts = num_pkts;
  90. if (num_pkts > 0) {
  91. h->pkts = OPENSSL_zalloc(sizeof(struct pkt_info) * num_pkts);
  92. if (!TEST_ptr(h->pkts))
  93. goto err;
  94. } else {
  95. h->pkts = NULL;
  96. }
  97. rc = 1;
  98. err:
  99. if (rc == 0)
  100. helper_destroy(h);
  101. return rc;
  102. }
  103. static const QUIC_PN linear_20[] = {
  104. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
  105. };
  106. static const QUIC_PN high_linear_20[] = {
  107. 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008,
  108. 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017,
  109. 1018, 1019
  110. };
  111. /*
  112. * TX ACK (Packet Threshold) Test Cases
  113. * ******************************************************************
  114. */
  115. struct tx_ack_test_case {
  116. const QUIC_PN *pn_table;
  117. size_t pn_table_len;
  118. const OSSL_QUIC_ACK_RANGE *ack_ranges;
  119. size_t num_ack_ranges;
  120. const char *expect_ack; /* 1=ack, 2=lost, 4=discarded */
  121. };
  122. #define DEFINE_TX_ACK_CASE(n, pntable) \
  123. static const struct tx_ack_test_case tx_ack_case_##n = { \
  124. (pntable), OSSL_NELEM(pntable), \
  125. tx_ack_range_##n, OSSL_NELEM(tx_ack_range_##n), \
  126. tx_ack_expect_##n \
  127. }
  128. /* One range, partial coverage of space */
  129. static const OSSL_QUIC_ACK_RANGE tx_ack_range_1[] = {
  130. { 0, 10 },
  131. };
  132. static const char tx_ack_expect_1[] = {
  133. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  134. };
  135. DEFINE_TX_ACK_CASE(1, linear_20);
  136. /* Two ranges, partial coverage of space, overlapping by 1 */
  137. static const OSSL_QUIC_ACK_RANGE tx_ack_range_2[] = {
  138. { 5, 10 }, { 0, 5 }
  139. };
  140. static const char tx_ack_expect_2[] = {
  141. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  142. };
  143. DEFINE_TX_ACK_CASE(2, linear_20);
  144. /* Two ranges, partial coverage of space, together contiguous */
  145. static const OSSL_QUIC_ACK_RANGE tx_ack_range_3[] = {
  146. { 6, 10 }, { 0, 5 }
  147. };
  148. static const char tx_ack_expect_3[] = {
  149. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  150. };
  151. DEFINE_TX_ACK_CASE(3, linear_20);
  152. /*
  153. * Two ranges, partial coverage of space, non-contiguous by 1
  154. * Causes inferred loss due to packet threshold being exceeded.
  155. */
  156. static const OSSL_QUIC_ACK_RANGE tx_ack_range_4[] = {
  157. { 7, 10 }, { 0, 5 }
  158. };
  159. static const char tx_ack_expect_4[] = {
  160. 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  161. };
  162. DEFINE_TX_ACK_CASE(4, linear_20);
  163. /*
  164. * Two ranges, partial coverage of space, non-contiguous by 2
  165. * Causes inferred loss due to packet threshold being exceeded.
  166. */
  167. static const OSSL_QUIC_ACK_RANGE tx_ack_range_5[] = {
  168. { 7, 10 }, { 0, 4 }
  169. };
  170. static const char tx_ack_expect_5[] = {
  171. 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  172. };
  173. DEFINE_TX_ACK_CASE(5, linear_20);
  174. /* One range, covering entire space */
  175. static const OSSL_QUIC_ACK_RANGE tx_ack_range_6[] = {
  176. { 0, 20 },
  177. };
  178. static const char tx_ack_expect_6[] = {
  179. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  180. };
  181. DEFINE_TX_ACK_CASE(6, linear_20);
  182. /* One range, covering more space than exists */
  183. static const OSSL_QUIC_ACK_RANGE tx_ack_range_7[] = {
  184. { 0, 30 },
  185. };
  186. static const char tx_ack_expect_7[] = {
  187. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  188. };
  189. DEFINE_TX_ACK_CASE(7, linear_20);
  190. /* One range, covering nothing (too high) */
  191. static const OSSL_QUIC_ACK_RANGE tx_ack_range_8[] = {
  192. { 21, 30 },
  193. };
  194. static const char tx_ack_expect_8[] = {
  195. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  196. };
  197. DEFINE_TX_ACK_CASE(8, linear_20);
  198. /* One range, covering nothing (too low) */
  199. static const OSSL_QUIC_ACK_RANGE tx_ack_range_9[] = {
  200. { 0, 999 },
  201. };
  202. static const char tx_ack_expect_9[] = {
  203. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  204. };
  205. DEFINE_TX_ACK_CASE(9, high_linear_20);
  206. /* One single packet at start of PN set */
  207. static const OSSL_QUIC_ACK_RANGE tx_ack_range_10[] = {
  208. { 0, 0 },
  209. };
  210. static const char tx_ack_expect_10[] = {
  211. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  212. };
  213. DEFINE_TX_ACK_CASE(10, linear_20);
  214. /*
  215. * One single packet in middle of PN set
  216. * Causes inferred loss of one packet due to packet threshold being exceeded,
  217. * but several other previous packets survive as they are under the threshold.
  218. */
  219. static const OSSL_QUIC_ACK_RANGE tx_ack_range_11[] = {
  220. { 3, 3 },
  221. };
  222. static const char tx_ack_expect_11[] = {
  223. 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  224. };
  225. DEFINE_TX_ACK_CASE(11, linear_20);
  226. /*
  227. * One single packet at end of PN set
  228. * Causes inferred loss due to packet threshold being exceeded.
  229. */
  230. static const OSSL_QUIC_ACK_RANGE tx_ack_range_12[] = {
  231. { 19, 19 },
  232. };
  233. static const char tx_ack_expect_12[] = {
  234. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1
  235. };
  236. DEFINE_TX_ACK_CASE(12, linear_20);
  237. /*
  238. * Mixed straddling
  239. * Causes inferred loss due to packet threshold being exceeded.
  240. */
  241. static const OSSL_QUIC_ACK_RANGE tx_ack_range_13[] = {
  242. { 1008, 1008 }, { 1004, 1005 }, { 1001, 1002 }
  243. };
  244. static const char tx_ack_expect_13[] = {
  245. 2, 1, 1, 2, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  246. };
  247. DEFINE_TX_ACK_CASE(13, high_linear_20);
  248. static const struct tx_ack_test_case *const tx_ack_cases[] = {
  249. &tx_ack_case_1,
  250. &tx_ack_case_2,
  251. &tx_ack_case_3,
  252. &tx_ack_case_4,
  253. &tx_ack_case_5,
  254. &tx_ack_case_6,
  255. &tx_ack_case_7,
  256. &tx_ack_case_8,
  257. &tx_ack_case_9,
  258. &tx_ack_case_10,
  259. &tx_ack_case_11,
  260. &tx_ack_case_12,
  261. &tx_ack_case_13,
  262. };
  263. enum {
  264. MODE_ACK, MODE_DISCARD, MODE_PTO, MODE_NUM
  265. };
  266. static int test_probe_counts(const OSSL_ACKM_PROBE_INFO *p,
  267. uint32_t handshake,
  268. uint32_t padded_initial,
  269. uint32_t pto_initial,
  270. uint32_t pto_handshake,
  271. uint32_t pto_app)
  272. {
  273. if (!TEST_uint_eq(p->handshake, handshake))
  274. return 0;
  275. if (!TEST_uint_eq(p->padded_initial, padded_initial))
  276. return 0;
  277. if (!TEST_uint_eq(p->pto[QUIC_PN_SPACE_INITIAL], pto_initial))
  278. return 0;
  279. if (!TEST_uint_eq(p->pto[QUIC_PN_SPACE_HANDSHAKE], pto_handshake))
  280. return 0;
  281. if (!TEST_uint_eq(p->pto[QUIC_PN_SPACE_APP], pto_app))
  282. return 0;
  283. return 1;
  284. }
  285. static void on_loss_detection_deadline_callback(OSSL_TIME deadline, void *arg)
  286. {
  287. *(OSSL_TIME *)arg = deadline;
  288. }
  289. static int test_tx_ack_case_actual(int tidx, int space, int mode)
  290. {
  291. int testresult = 0;
  292. struct helper h;
  293. size_t i;
  294. OSSL_ACKM_TX_PKT *tx;
  295. const struct tx_ack_test_case *c = tx_ack_cases[tidx];
  296. OSSL_QUIC_FRAME_ACK ack = {0};
  297. OSSL_TIME loss_detection_deadline = ossl_time_zero();
  298. /* Cannot discard app space, so skip this */
  299. if (mode == MODE_DISCARD && space == QUIC_PN_SPACE_APP) {
  300. TEST_skip("skipping test for app space");
  301. return 1;
  302. }
  303. if (!TEST_int_eq(helper_init(&h, c->pn_table_len), 1))
  304. goto err;
  305. /* Arm callback. */
  306. ossl_ackm_set_loss_detection_deadline_callback(h.ackm,
  307. on_loss_detection_deadline_callback,
  308. &loss_detection_deadline);
  309. /* Allocate TX packet structures. */
  310. for (i = 0; i < c->pn_table_len; ++i) {
  311. h.pkts[i].pkt = tx = OPENSSL_zalloc(sizeof(*tx));
  312. if (!TEST_ptr(tx))
  313. goto err;
  314. tx->pkt_num = c->pn_table[i];
  315. tx->pkt_space = space;
  316. tx->is_inflight = 1;
  317. tx->is_ack_eliciting = 1;
  318. tx->num_bytes = 123;
  319. tx->largest_acked = QUIC_PN_INVALID;
  320. tx->on_lost = on_lost;
  321. tx->on_acked = on_acked;
  322. tx->on_discarded = on_discarded;
  323. tx->cb_arg = &h.pkts[i];
  324. tx->time = fake_time;
  325. if (!TEST_int_eq(ossl_ackm_on_tx_packet(h.ackm, tx), 1))
  326. goto err;
  327. }
  328. if (mode == MODE_DISCARD) {
  329. /* Try discarding. */
  330. if (!TEST_int_eq(ossl_ackm_on_pkt_space_discarded(h.ackm, space), 1))
  331. goto err;
  332. /* Check all discard callbacks were called. */
  333. for (i = 0; i < c->pn_table_len; ++i) {
  334. if (!TEST_int_eq(h.pkts[i].acked, 0))
  335. goto err;
  336. if (!TEST_int_eq(h.pkts[i].lost, 0))
  337. goto err;
  338. if (!TEST_int_eq(h.pkts[i].discarded, 1))
  339. goto err;
  340. }
  341. } else if (mode == MODE_ACK) {
  342. /* Try acknowledging. */
  343. ack.ack_ranges = (OSSL_QUIC_ACK_RANGE *)c->ack_ranges;
  344. ack.num_ack_ranges = c->num_ack_ranges;
  345. if (!TEST_int_eq(ossl_ackm_on_rx_ack_frame(h.ackm, &ack, space, fake_time), 1))
  346. goto err;
  347. /* Check correct ranges were acknowledged. */
  348. for (i = 0; i < c->pn_table_len; ++i) {
  349. if (!TEST_int_eq(h.pkts[i].acked,
  350. (c->expect_ack[i] & 1) != 0 ? 1 : 0))
  351. goto err;
  352. if (!TEST_int_eq(h.pkts[i].lost,
  353. (c->expect_ack[i] & 2) != 0 ? 1 : 0))
  354. goto err;
  355. if (!TEST_int_eq(h.pkts[i].discarded,
  356. (c->expect_ack[i] & 4) != 0 ? 1 : 0))
  357. goto err;
  358. }
  359. } else if (mode == MODE_PTO) {
  360. OSSL_TIME deadline = ossl_ackm_get_loss_detection_deadline(h.ackm);
  361. OSSL_ACKM_PROBE_INFO probe = {0};
  362. if (!TEST_int_eq(ossl_time_compare(deadline, loss_detection_deadline), 0))
  363. goto err;
  364. /* We should have a PTO deadline. */
  365. if (!TEST_int_gt(ossl_time_compare(deadline, fake_time), 0))
  366. goto err;
  367. /* Should not have any probe requests yet. */
  368. if (!TEST_int_eq(ossl_ackm_get_probe_request(h.ackm, 0, &probe), 1))
  369. goto err;
  370. if (!TEST_int_eq(test_probe_counts(&probe, 0, 0, 0, 0, 0), 1))
  371. goto err;
  372. /*
  373. * If in app space, confirm handshake, as this is necessary to enable
  374. * app space PTO probe requests.
  375. */
  376. if (space == QUIC_PN_SPACE_APP)
  377. if (!TEST_int_eq(ossl_ackm_on_handshake_confirmed(h.ackm), 1))
  378. goto err;
  379. /* Advance to the PTO deadline. */
  380. fake_time = ossl_time_add(deadline, ossl_ticks2time(1));
  381. if (!TEST_int_eq(ossl_ackm_on_timeout(h.ackm), 1))
  382. goto err;
  383. /* Should have a probe request. Not cleared by first call. */
  384. for (i = 0; i < 3; ++i) {
  385. if (!TEST_int_eq(ossl_ackm_get_probe_request(h.ackm, i > 0, &probe), 1))
  386. goto err;
  387. if (i == 2) {
  388. if (!TEST_int_eq(test_probe_counts(&probe, 0, 0, 0, 0, 0), 1))
  389. goto err;
  390. } else {
  391. if (!TEST_int_eq(test_probe_counts(&probe, 0, 0,
  392. space == QUIC_PN_SPACE_INITIAL,
  393. space == QUIC_PN_SPACE_HANDSHAKE,
  394. space == QUIC_PN_SPACE_APP), 1))
  395. goto err;
  396. }
  397. }
  398. } else
  399. goto err;
  400. testresult = 1;
  401. err:
  402. helper_destroy(&h);
  403. return testresult;
  404. }
  405. /*
  406. * TX ACK (Time Threshold) Test
  407. * ******************************************************************
  408. */
  409. enum {
  410. TX_ACK_TIME_OP_END,
  411. TX_ACK_TIME_OP_PKT, /* TX packets */
  412. TX_ACK_TIME_OP_ACK, /* Synthesise incoming ACK of single PN range */
  413. TX_ACK_TIME_OP_EXPECT /* Ack/loss assertion */
  414. };
  415. struct tx_ack_time_op {
  416. int kind;
  417. uint64_t time_advance; /* all ops */
  418. QUIC_PN pn; /* PKT, ACK */
  419. size_t num_pn; /* PKT, ACK */
  420. const char *expect; /* 1=ack, 2=lost, 4=discarded */
  421. };
  422. #define TX_OP_PKT(advance, pn, num_pn) \
  423. { TX_ACK_TIME_OP_PKT, (advance) * OSSL_TIME_MS, (pn), (num_pn), NULL },
  424. #define TX_OP_ACK(advance, pn, num_pn) \
  425. { TX_ACK_TIME_OP_ACK, (advance) * OSSL_TIME_MS, (pn), (num_pn), NULL },
  426. #define TX_OP_EXPECT(expect) \
  427. { TX_ACK_TIME_OP_EXPECT, 0, 0, 0, (expect) },
  428. #define TX_OP_END { TX_ACK_TIME_OP_END }
  429. static const char tx_ack_time_script_1_expect[] = {
  430. 2, 1
  431. };
  432. static const struct tx_ack_time_op tx_ack_time_script_1[] = {
  433. TX_OP_PKT ( 0, 0, 1)
  434. TX_OP_PKT (3600000, 1, 1)
  435. TX_OP_ACK ( 1000, 1, 1)
  436. TX_OP_EXPECT(tx_ack_time_script_1_expect)
  437. TX_OP_END
  438. };
  439. static const struct tx_ack_time_op *const tx_ack_time_scripts[] = {
  440. tx_ack_time_script_1,
  441. };
  442. static int test_tx_ack_time_script(int tidx)
  443. {
  444. int testresult = 0;
  445. struct helper h;
  446. OSSL_ACKM_TX_PKT *tx = NULL;
  447. OSSL_QUIC_FRAME_ACK ack = {0};
  448. OSSL_QUIC_ACK_RANGE ack_range = {0};
  449. size_t i, num_pkts = 0, pkt_idx = 0;
  450. const struct tx_ack_time_op *script = tx_ack_time_scripts[tidx], *s;
  451. /* Calculate number of packets. */
  452. for (s = script; s->kind != TX_ACK_TIME_OP_END; ++s)
  453. if (s->kind == TX_ACK_TIME_OP_PKT)
  454. num_pkts += s->num_pn;
  455. /* Initialise ACK manager and packet structures. */
  456. if (!TEST_int_eq(helper_init(&h, num_pkts), 1))
  457. goto err;
  458. for (i = 0; i < num_pkts; ++i) {
  459. h.pkts[i].pkt = tx = OPENSSL_zalloc(sizeof(*tx));
  460. if (!TEST_ptr(tx))
  461. goto err;
  462. }
  463. /* Run script. */
  464. for (s = script; s->kind != TX_ACK_TIME_OP_END; ++s)
  465. switch (s->kind) {
  466. case TX_ACK_TIME_OP_PKT:
  467. for (i = 0; i < s->num_pn; ++i) {
  468. tx = h.pkts[pkt_idx + i].pkt;
  469. tx->pkt_num = s->pn + i;
  470. tx->pkt_space = QUIC_PN_SPACE_INITIAL;
  471. tx->num_bytes = 123;
  472. tx->largest_acked = QUIC_PN_INVALID;
  473. tx->is_inflight = 1;
  474. tx->is_ack_eliciting = 1;
  475. tx->on_lost = on_lost;
  476. tx->on_acked = on_acked;
  477. tx->on_discarded = on_discarded;
  478. tx->cb_arg = &h.pkts[pkt_idx + i];
  479. fake_time = ossl_time_add(fake_time,
  480. ossl_ticks2time(s->time_advance));
  481. tx->time = fake_time;
  482. if (!TEST_int_eq(ossl_ackm_on_tx_packet(h.ackm, tx), 1))
  483. goto err;
  484. }
  485. pkt_idx += s->num_pn;
  486. break;
  487. case TX_ACK_TIME_OP_ACK:
  488. ack.ack_ranges = &ack_range;
  489. ack.num_ack_ranges = 1;
  490. ack_range.start = s->pn;
  491. ack_range.end = s->pn + s->num_pn;
  492. fake_time = ossl_time_add(fake_time,
  493. ossl_ticks2time(s->time_advance));
  494. if (!TEST_int_eq(ossl_ackm_on_rx_ack_frame(h.ackm, &ack,
  495. QUIC_PN_SPACE_INITIAL,
  496. fake_time), 1))
  497. goto err;
  498. break;
  499. case TX_ACK_TIME_OP_EXPECT:
  500. for (i = 0; i < num_pkts; ++i) {
  501. if (!TEST_int_eq(h.pkts[i].acked,
  502. (s->expect[i] & 1) != 0 ? 1 : 0))
  503. goto err;
  504. if (!TEST_int_eq(h.pkts[i].lost,
  505. (s->expect[i] & 2) != 0 ? 1 : 0))
  506. goto err;
  507. if (!TEST_int_eq(h.pkts[i].discarded,
  508. (s->expect[i] & 4) != 0 ? 1 : 0))
  509. goto err;
  510. }
  511. break;
  512. }
  513. testresult = 1;
  514. err:
  515. helper_destroy(&h);
  516. return testresult;
  517. }
  518. /*
  519. * RX ACK Test
  520. * ******************************************************************
  521. */
  522. enum {
  523. RX_OPK_END,
  524. RX_OPK_PKT, /* RX packet */
  525. RX_OPK_CHECK_UNPROC, /* check PNs unprocessable */
  526. RX_OPK_CHECK_PROC, /* check PNs processable */
  527. RX_OPK_CHECK_STATE, /* check is_desired/deadline */
  528. RX_OPK_CHECK_ACKS, /* check ACK ranges */
  529. RX_OPK_TX, /* TX packet */
  530. RX_OPK_RX_ACK /* RX ACK frame */
  531. };
  532. struct rx_test_op {
  533. int kind;
  534. uint64_t time_advance;
  535. QUIC_PN pn; /* PKT, CHECK_(UN)PROC, TX, RX_ACK */
  536. size_t num_pn; /* PKT, CHECK_(UN)PROC, TX, RX_ACK */
  537. char expect_desired; /* CHECK_STATE */
  538. char expect_deadline; /* CHECK_STATE */
  539. const OSSL_QUIC_ACK_RANGE *ack_ranges; /* CHECK_ACKS */
  540. size_t num_ack_ranges; /* CHECK_ACKS */
  541. QUIC_PN largest_acked; /* TX */
  542. };
  543. #define RX_OP_PKT(advance, pn, num_pn) \
  544. { \
  545. RX_OPK_PKT, (advance) * OSSL_TIME_MS, (pn), (num_pn), \
  546. 0, 0, NULL, 0, 0 \
  547. },
  548. #define RX_OP_CHECK_UNPROC(advance, pn, num_pn) \
  549. { \
  550. RX_OPK_CHECK_UNPROC, (advance) * OSSL_TIME_MS, (pn), (num_pn),\
  551. 0, 0, NULL, 0, 0 \
  552. },
  553. #define RX_OP_CHECK_PROC(advance, pn, num_pn) \
  554. { \
  555. RX_OPK_CHECK_PROC, (advance) * OSSL_TIME_MS, (pn), (num_pn), \
  556. 0, 0, NULL, 0, 0 \
  557. },
  558. #define RX_OP_CHECK_STATE(advance, expect_desired, expect_deadline) \
  559. { \
  560. RX_OPK_CHECK_STATE, (advance) * OSSL_TIME_MS, 0, 0, \
  561. (expect_desired), (expect_deadline), NULL, 0, 0 \
  562. },
  563. #define RX_OP_CHECK_ACKS(advance, ack_ranges) \
  564. { \
  565. RX_OPK_CHECK_ACKS, (advance) * OSSL_TIME_MS, 0, 0, \
  566. 0, 0, (ack_ranges), OSSL_NELEM(ack_ranges), 0 \
  567. },
  568. #define RX_OP_CHECK_NO_ACKS(advance) \
  569. { \
  570. RX_OPK_CHECK_ACKS, (advance) * OSSL_TIME_MS, 0, 0, \
  571. 0, 0, NULL, 0, 0 \
  572. },
  573. #define RX_OP_TX(advance, pn, largest_acked) \
  574. { \
  575. RX_OPK_TX, (advance) * OSSL_TIME_MS, (pn), 1, \
  576. 0, 0, NULL, 0, (largest_acked) \
  577. },
  578. #define RX_OP_RX_ACK(advance, pn, num_pn) \
  579. { \
  580. RX_OPK_RX_ACK, (advance) * OSSL_TIME_MS, (pn), (num_pn), \
  581. 0, 0, NULL, 0, 0 \
  582. },
  583. #define RX_OP_END \
  584. { RX_OPK_END }
  585. /* RX 1. Simple Test with ACK Desired (Packet Threshold, Exactly) */
  586. static const OSSL_QUIC_ACK_RANGE rx_ack_ranges_1a[] = {
  587. { 0, 1 }
  588. };
  589. static const struct rx_test_op rx_script_1[] = {
  590. RX_OP_CHECK_STATE (0, 0, 0) /* no threshold yet */
  591. RX_OP_CHECK_PROC (0, 0, 3)
  592. RX_OP_PKT (0, 0, 2) /* two packets, threshold */
  593. RX_OP_CHECK_UNPROC (0, 0, 2)
  594. RX_OP_CHECK_PROC (0, 2, 1)
  595. RX_OP_CHECK_STATE (0, 1, 0) /* threshold met, immediate */
  596. RX_OP_CHECK_ACKS (0, rx_ack_ranges_1a)
  597. /* At this point we would generate e.g. a packet with an ACK. */
  598. RX_OP_TX (0, 0, 1) /* ACKs both */
  599. RX_OP_CHECK_ACKS (0, rx_ack_ranges_1a) /* not provably ACKed yet */
  600. RX_OP_RX_ACK (0, 0, 1) /* TX'd packet is ACK'd */
  601. RX_OP_CHECK_NO_ACKS (0) /* nothing more to ACK */
  602. RX_OP_CHECK_UNPROC (0, 0, 2) /* still unprocessable */
  603. RX_OP_CHECK_PROC (0, 2, 1) /* still processable */
  604. RX_OP_END
  605. };
  606. /* RX 2. Simple Test with ACK Not Yet Desired (Packet Threshold) */
  607. static const OSSL_QUIC_ACK_RANGE rx_ack_ranges_2a[] = {
  608. { 0, 0 }
  609. };
  610. static const OSSL_QUIC_ACK_RANGE rx_ack_ranges_2b[] = {
  611. { 0, 2 }
  612. };
  613. static const struct rx_test_op rx_script_2[] = {
  614. RX_OP_CHECK_STATE (0, 0, 0) /* no threshold yet */
  615. RX_OP_CHECK_PROC (0, 0, 3)
  616. /* First packet always generates an ACK so get it out of the way. */
  617. RX_OP_PKT (0, 0, 1)
  618. RX_OP_CHECK_UNPROC (0, 0, 1)
  619. RX_OP_CHECK_PROC (0, 1, 1)
  620. RX_OP_CHECK_STATE (0, 1, 0) /* first packet always causes ACK */
  621. RX_OP_CHECK_ACKS (0, rx_ack_ranges_2a) /* clears packet counter */
  622. RX_OP_CHECK_STATE (0, 0, 0) /* desired state should have been cleared */
  623. /* Second packet should not cause ACK-desired state */
  624. RX_OP_PKT (0, 1, 1) /* just one packet, threshold is 2 */
  625. RX_OP_CHECK_UNPROC (0, 0, 2)
  626. RX_OP_CHECK_PROC (0, 2, 1)
  627. RX_OP_CHECK_STATE (0, 0, 1) /* threshold not yet met, so deadline */
  628. /* Don't check ACKs here, as it would reset our threshold counter. */
  629. /* Now receive a second packet, triggering the threshold */
  630. RX_OP_PKT (0, 2, 1) /* second packet meets threshold */
  631. RX_OP_CHECK_UNPROC (0, 0, 3)
  632. RX_OP_CHECK_PROC (0, 3, 1)
  633. RX_OP_CHECK_STATE (0, 1, 0) /* desired immediately */
  634. RX_OP_CHECK_ACKS (0, rx_ack_ranges_2b)
  635. /* At this point we would generate e.g. a packet with an ACK. */
  636. RX_OP_TX (0, 0, 2) /* ACKs all */
  637. RX_OP_CHECK_ACKS (0, rx_ack_ranges_2b) /* not provably ACKed yet */
  638. RX_OP_RX_ACK (0, 0, 1) /* TX'd packet is ACK'd */
  639. RX_OP_CHECK_NO_ACKS (0) /* nothing more to ACK */
  640. RX_OP_CHECK_UNPROC (0, 0, 3) /* still unprocessable */
  641. RX_OP_CHECK_PROC (0, 3, 1) /* still processable */
  642. RX_OP_END
  643. };
  644. /* RX 3. Simple Test with ACK Desired (Packet Threshold, Multiple Watermarks) */
  645. static const OSSL_QUIC_ACK_RANGE rx_ack_ranges_3a[] = {
  646. { 0, 0 }
  647. };
  648. static const OSSL_QUIC_ACK_RANGE rx_ack_ranges_3b[] = {
  649. { 0, 10 }
  650. };
  651. static const OSSL_QUIC_ACK_RANGE rx_ack_ranges_3c[] = {
  652. { 6, 10 }
  653. };
  654. static const struct rx_test_op rx_script_3[] = {
  655. RX_OP_CHECK_STATE (0, 0, 0) /* no threshold yet */
  656. RX_OP_CHECK_PROC (0, 0, 11)
  657. /* First packet always generates an ACK so get it out of the way. */
  658. RX_OP_PKT (0, 0, 1)
  659. RX_OP_CHECK_UNPROC (0, 0, 1)
  660. RX_OP_CHECK_PROC (0, 1, 1)
  661. RX_OP_CHECK_STATE (0, 1, 0) /* first packet always causes ACK */
  662. RX_OP_CHECK_ACKS (0, rx_ack_ranges_3a) /* clears packet counter */
  663. RX_OP_CHECK_STATE (0, 0, 0) /* desired state should have been cleared */
  664. /* Generate ten packets, exceeding the threshold. */
  665. RX_OP_PKT (0, 1, 10) /* ten packets, threshold is 2 */
  666. RX_OP_CHECK_UNPROC (0, 0, 11)
  667. RX_OP_CHECK_PROC (0, 11, 1)
  668. RX_OP_CHECK_STATE (0, 1, 0) /* threshold met, immediate */
  669. RX_OP_CHECK_ACKS (0, rx_ack_ranges_3b)
  670. /*
  671. * Test TX'ing a packet which doesn't ACK anything.
  672. */
  673. RX_OP_TX (0, 0, QUIC_PN_INVALID)
  674. RX_OP_RX_ACK (0, 0, 1)
  675. /*
  676. * At this point we would generate a packet with an ACK immediately.
  677. * TX a packet which when ACKed makes [0,5] provably ACKed.
  678. */
  679. RX_OP_TX (0, 1, 5)
  680. RX_OP_CHECK_ACKS (0, rx_ack_ranges_3b) /* not provably ACKed yet */
  681. RX_OP_RX_ACK (0, 1, 1)
  682. RX_OP_CHECK_ACKS (0, rx_ack_ranges_3c) /* provably ACKed now gone */
  683. RX_OP_CHECK_UNPROC (0, 0, 11) /* still unprocessable */
  684. RX_OP_CHECK_PROC (0, 11, 1) /* still processable */
  685. /*
  686. * Now TX another packet which provably ACKs the rest when ACKed.
  687. */
  688. RX_OP_TX (0, 2, 10)
  689. RX_OP_CHECK_ACKS (0, rx_ack_ranges_3c) /* not provably ACKed yet */
  690. RX_OP_RX_ACK (0, 2, 1)
  691. RX_OP_CHECK_NO_ACKS (0) /* provably ACKed now gone */
  692. RX_OP_CHECK_UNPROC (0, 0, 11) /* still unprocessable */
  693. RX_OP_CHECK_PROC (0, 11, 1) /* still processable */
  694. RX_OP_END
  695. };
  696. static const struct rx_test_op *const rx_test_scripts[] = {
  697. rx_script_1,
  698. rx_script_2,
  699. rx_script_3
  700. };
  701. static void on_ack_deadline_callback(OSSL_TIME deadline,
  702. int pkt_space, void *arg)
  703. {
  704. ((OSSL_TIME *)arg)[pkt_space] = deadline;
  705. }
  706. static int test_rx_ack_actual(int tidx, int space)
  707. {
  708. int testresult = 0;
  709. struct helper h;
  710. const struct rx_test_op *script = rx_test_scripts[tidx], *s;
  711. size_t i, num_tx = 0, txi = 0;
  712. const OSSL_QUIC_FRAME_ACK *ack;
  713. OSSL_QUIC_FRAME_ACK rx_ack = {0};
  714. OSSL_QUIC_ACK_RANGE rx_ack_range = {0};
  715. struct pkt_info *pkts = NULL;
  716. OSSL_ACKM_TX_PKT *txs = NULL, *tx;
  717. OSSL_TIME ack_deadline[QUIC_PN_SPACE_NUM];
  718. for (i = 0; i < QUIC_PN_SPACE_NUM; ++i)
  719. ack_deadline[i] = ossl_time_infinite();
  720. /* Initialise ACK manager. */
  721. if (!TEST_int_eq(helper_init(&h, 0), 1))
  722. goto err;
  723. /* Arm callback for testing. */
  724. ossl_ackm_set_ack_deadline_callback(h.ackm, on_ack_deadline_callback,
  725. ack_deadline);
  726. /*
  727. * Determine how many packets we are TXing, and therefore how many packet
  728. * structures we need.
  729. */
  730. for (s = script; s->kind != RX_OPK_END; ++s)
  731. if (s->kind == RX_OPK_TX)
  732. num_tx += s->num_pn;
  733. /* Allocate packet information structures. */
  734. txs = OPENSSL_zalloc(sizeof(*txs) * num_tx);
  735. if (!TEST_ptr(txs))
  736. goto err;
  737. pkts = OPENSSL_zalloc(sizeof(*pkts) * num_tx);
  738. if (!TEST_ptr(pkts))
  739. goto err;
  740. /* Run script. */
  741. for (s = script; s->kind != RX_OPK_END; ++s) {
  742. fake_time = ossl_time_add(fake_time,
  743. ossl_ticks2time(s->time_advance));
  744. switch (s->kind) {
  745. case RX_OPK_PKT:
  746. for (i = 0; i < s->num_pn; ++i) {
  747. OSSL_ACKM_RX_PKT pkt = {0};
  748. pkt.pkt_num = s->pn + i;
  749. pkt.time = fake_time;
  750. pkt.pkt_space = space;
  751. pkt.is_ack_eliciting = 1;
  752. /* The packet should be processable before we feed it. */
  753. if (!TEST_int_eq(ossl_ackm_is_rx_pn_processable(h.ackm,
  754. pkt.pkt_num,
  755. pkt.pkt_space), 1))
  756. goto err;
  757. if (!TEST_int_eq(ossl_ackm_on_rx_packet(h.ackm, &pkt), 1))
  758. goto err;
  759. }
  760. break;
  761. case RX_OPK_CHECK_UNPROC:
  762. case RX_OPK_CHECK_PROC:
  763. for (i = 0; i < s->num_pn; ++i)
  764. if (!TEST_int_eq(ossl_ackm_is_rx_pn_processable(h.ackm,
  765. s->pn + i, space),
  766. (s->kind == RX_OPK_CHECK_PROC)))
  767. goto err;
  768. break;
  769. case RX_OPK_CHECK_STATE:
  770. if (!TEST_int_eq(ossl_ackm_is_ack_desired(h.ackm, space),
  771. s->expect_desired))
  772. goto err;
  773. if (!TEST_int_eq(!ossl_time_is_infinite(ossl_ackm_get_ack_deadline(h.ackm, space))
  774. && !ossl_time_is_zero(ossl_ackm_get_ack_deadline(h.ackm, space)),
  775. s->expect_deadline))
  776. goto err;
  777. for (i = 0; i < QUIC_PN_SPACE_NUM; ++i) {
  778. if (i != (size_t)space
  779. && !TEST_true(ossl_time_is_infinite(ossl_ackm_get_ack_deadline(h.ackm, i))))
  780. goto err;
  781. if (!TEST_int_eq(ossl_time_compare(ossl_ackm_get_ack_deadline(h.ackm, i),
  782. ack_deadline[i]), 0))
  783. goto err;
  784. }
  785. break;
  786. case RX_OPK_CHECK_ACKS:
  787. ack = ossl_ackm_get_ack_frame(h.ackm, space);
  788. /* Should always be able to get an ACK frame. */
  789. if (!TEST_ptr(ack))
  790. goto err;
  791. if (!TEST_size_t_eq(ack->num_ack_ranges, s->num_ack_ranges))
  792. goto err;
  793. for (i = 0; i < ack->num_ack_ranges; ++i) {
  794. if (!TEST_uint64_t_eq(ack->ack_ranges[i].start,
  795. s->ack_ranges[i].start))
  796. goto err;
  797. if (!TEST_uint64_t_eq(ack->ack_ranges[i].end,
  798. s->ack_ranges[i].end))
  799. goto err;
  800. }
  801. break;
  802. case RX_OPK_TX:
  803. pkts[txi].pkt = tx = &txs[txi];
  804. tx->pkt_num = s->pn;
  805. tx->pkt_space = space;
  806. tx->num_bytes = 123;
  807. tx->largest_acked = s->largest_acked;
  808. tx->is_inflight = 1;
  809. tx->is_ack_eliciting = 1;
  810. tx->on_lost = on_lost;
  811. tx->on_acked = on_acked;
  812. tx->on_discarded = on_discarded;
  813. tx->cb_arg = &pkts[txi];
  814. tx->time = fake_time;
  815. if (!TEST_int_eq(ossl_ackm_on_tx_packet(h.ackm, tx), 1))
  816. goto err;
  817. ++txi;
  818. break;
  819. case RX_OPK_RX_ACK:
  820. rx_ack.ack_ranges = &rx_ack_range;
  821. rx_ack.num_ack_ranges = 1;
  822. rx_ack_range.start = s->pn;
  823. rx_ack_range.end = s->pn + s->num_pn - 1;
  824. if (!TEST_int_eq(ossl_ackm_on_rx_ack_frame(h.ackm, &rx_ack,
  825. space, fake_time), 1))
  826. goto err;
  827. break;
  828. default:
  829. goto err;
  830. }
  831. }
  832. testresult = 1;
  833. err:
  834. helper_destroy(&h);
  835. OPENSSL_free(pkts);
  836. OPENSSL_free(txs);
  837. return testresult;
  838. }
  839. /*
  840. * Driver
  841. * ******************************************************************
  842. */
  843. static int test_tx_ack_case(int idx)
  844. {
  845. int tidx, space;
  846. tidx = idx % OSSL_NELEM(tx_ack_cases);
  847. idx /= OSSL_NELEM(tx_ack_cases);
  848. space = idx % QUIC_PN_SPACE_NUM;
  849. idx /= QUIC_PN_SPACE_NUM;
  850. return test_tx_ack_case_actual(tidx, space, idx);
  851. }
  852. static int test_rx_ack(int idx)
  853. {
  854. int tidx;
  855. tidx = idx % OSSL_NELEM(rx_test_scripts);
  856. idx /= OSSL_NELEM(rx_test_scripts);
  857. return test_rx_ack_actual(tidx, idx);
  858. }
  859. int setup_tests(void)
  860. {
  861. ADD_ALL_TESTS(test_tx_ack_case,
  862. OSSL_NELEM(tx_ack_cases) * MODE_NUM * QUIC_PN_SPACE_NUM);
  863. ADD_ALL_TESTS(test_tx_ack_time_script, OSSL_NELEM(tx_ack_time_scripts));
  864. ADD_ALL_TESTS(test_rx_ack, OSSL_NELEM(rx_test_scripts) * QUIC_PN_SPACE_NUM);
  865. return 1;
  866. }