test_tcp.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. #include "test_tcp.h"
  2. #include "lwip/priv/tcp_priv.h"
  3. #include "lwip/stats.h"
  4. #include "tcp_helper.h"
  5. #include "lwip/inet_chksum.h"
  6. #ifdef _MSC_VER
  7. #pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
  8. #endif
  9. #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
  10. #error "This tests needs TCP- and MEMP-statistics enabled"
  11. #endif
  12. #if TCP_SND_BUF <= TCP_WND
  13. #error "This tests needs TCP_SND_BUF to be > TCP_WND"
  14. #endif
  15. /* used with check_seqnos() */
  16. #define SEQNO1 (0xFFFFFF00 - TCP_MSS)
  17. #define ISS 6510
  18. static u32_t seqnos[] = {
  19. SEQNO1,
  20. SEQNO1 + (1 * TCP_MSS),
  21. SEQNO1 + (2 * TCP_MSS),
  22. SEQNO1 + (3 * TCP_MSS),
  23. SEQNO1 + (4 * TCP_MSS),
  24. SEQNO1 + (5 * TCP_MSS) };
  25. static u8_t test_tcp_timer;
  26. /* our own version of tcp_tmr so we can reset fast/slow timer state */
  27. static void
  28. test_tcp_tmr(void)
  29. {
  30. tcp_fasttmr();
  31. if (++test_tcp_timer & 1) {
  32. tcp_slowtmr();
  33. }
  34. }
  35. /* Setups/teardown functions */
  36. static struct netif *old_netif_list;
  37. static struct netif *old_netif_default;
  38. static void
  39. tcp_setup(void)
  40. {
  41. old_netif_list = netif_list;
  42. old_netif_default = netif_default;
  43. netif_list = NULL;
  44. netif_default = NULL;
  45. /* reset iss to default (6510) */
  46. tcp_ticks = 0;
  47. tcp_ticks = 0 - (tcp_next_iss(NULL) - 6510);
  48. tcp_next_iss(NULL);
  49. tcp_ticks = 0;
  50. test_tcp_timer = 0;
  51. tcp_remove_all();
  52. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  53. }
  54. static void
  55. tcp_teardown(void)
  56. {
  57. netif_list = NULL;
  58. netif_default = NULL;
  59. tcp_remove_all();
  60. /* restore netif_list for next tests (e.g. loopif) */
  61. netif_list = old_netif_list;
  62. netif_default = old_netif_default;
  63. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  64. }
  65. /* Test functions */
  66. /** Call tcp_new() and tcp_abort() and test memp stats */
  67. START_TEST(test_tcp_new_abort)
  68. {
  69. struct tcp_pcb* pcb;
  70. LWIP_UNUSED_ARG(_i);
  71. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  72. pcb = tcp_new();
  73. fail_unless(pcb != NULL);
  74. if (pcb != NULL) {
  75. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  76. tcp_abort(pcb);
  77. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  78. }
  79. }
  80. END_TEST
  81. /** Call tcp_new() and tcp_abort() and test memp stats */
  82. START_TEST(test_tcp_listen_passive_open)
  83. {
  84. struct tcp_pcb *pcb, *pcbl;
  85. struct tcp_pcb_listen *lpcb;
  86. struct netif netif;
  87. struct test_tcp_txcounters txcounters;
  88. struct test_tcp_counters counters;
  89. struct pbuf *p;
  90. ip_addr_t src_addr;
  91. err_t err;
  92. LWIP_UNUSED_ARG(_i);
  93. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  94. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  95. /* initialize counter struct */
  96. memset(&counters, 0, sizeof(counters));
  97. pcb = tcp_new();
  98. EXPECT_RET(pcb != NULL);
  99. err = tcp_bind(pcb, &netif.ip_addr, 1234);
  100. EXPECT(err == ERR_OK);
  101. pcbl = tcp_listen(pcb);
  102. EXPECT_RET(pcbl != NULL);
  103. EXPECT_RET(pcbl != pcb);
  104. lpcb = (struct tcp_pcb_listen *)pcbl;
  105. ip_addr_set_ip4_u32_val(src_addr, lwip_htonl(lwip_ntohl(ip_addr_get_ip4_u32(&lpcb->local_ip)) + 1));
  106. /* check correct syn packet */
  107. p = tcp_create_segment(&src_addr, &lpcb->local_ip, 12345,
  108. lpcb->local_port, NULL, 0, 12345, 54321, TCP_SYN);
  109. EXPECT(p != NULL);
  110. if (p != NULL) {
  111. /* pass the segment to tcp_input */
  112. test_tcp_input(p, &netif);
  113. /* check if counters are as expected */
  114. EXPECT(txcounters.num_tx_calls == 1);
  115. }
  116. /* chekc syn packet with short length */
  117. p = tcp_create_segment(&src_addr, &lpcb->local_ip, 12345,
  118. lpcb->local_port, NULL, 0, 12345, 54321, TCP_SYN);
  119. EXPECT(p != NULL);
  120. EXPECT(p->next == NULL);
  121. if ((p != NULL) && (p->next == NULL)) {
  122. p->len -= 2;
  123. p->tot_len -= 2;
  124. /* pass the segment to tcp_input */
  125. test_tcp_input(p, &netif);
  126. /* check if counters are as expected */
  127. EXPECT(txcounters.num_tx_calls == 1);
  128. }
  129. tcp_close(pcbl);
  130. }
  131. END_TEST
  132. /** Create an ESTABLISHED pcb and check if receive callback is called */
  133. START_TEST(test_tcp_recv_inseq)
  134. {
  135. struct test_tcp_counters counters;
  136. struct tcp_pcb* pcb;
  137. struct pbuf* p;
  138. char data[] = {1, 2, 3, 4};
  139. u16_t data_len;
  140. struct netif netif;
  141. struct test_tcp_txcounters txcounters;
  142. LWIP_UNUSED_ARG(_i);
  143. /* initialize local vars */
  144. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  145. data_len = sizeof(data);
  146. /* initialize counter struct */
  147. memset(&counters, 0, sizeof(counters));
  148. counters.expected_data_len = data_len;
  149. counters.expected_data = data;
  150. /* create and initialize the pcb */
  151. pcb = test_tcp_new_counters_pcb(&counters);
  152. EXPECT_RET(pcb != NULL);
  153. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  154. /* create a segment */
  155. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  156. EXPECT(p != NULL);
  157. if (p != NULL) {
  158. /* pass the segment to tcp_input */
  159. test_tcp_input(p, &netif);
  160. /* check if counters are as expected */
  161. EXPECT(counters.close_calls == 0);
  162. EXPECT(counters.recv_calls == 1);
  163. EXPECT(counters.recved_bytes == data_len);
  164. EXPECT(counters.err_calls == 0);
  165. }
  166. /* make sure the pcb is freed */
  167. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  168. tcp_abort(pcb);
  169. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  170. }
  171. END_TEST
  172. /** Create an ESTABLISHED pcb and check if receive callback is called if a segment
  173. * overlapping rcv_nxt is received */
  174. START_TEST(test_tcp_recv_inseq_trim)
  175. {
  176. struct test_tcp_counters counters;
  177. struct tcp_pcb* pcb;
  178. struct pbuf* p;
  179. char data[PBUF_POOL_BUFSIZE*2];
  180. u16_t data_len;
  181. struct netif netif;
  182. struct test_tcp_txcounters txcounters;
  183. const u32_t new_data_len = 40;
  184. LWIP_UNUSED_ARG(_i);
  185. /* initialize local vars */
  186. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  187. data_len = sizeof(data);
  188. memset(data, 0, sizeof(data));
  189. /* initialize counter struct */
  190. memset(&counters, 0, sizeof(counters));
  191. counters.expected_data_len = data_len;
  192. counters.expected_data = data;
  193. /* create and initialize the pcb */
  194. pcb = test_tcp_new_counters_pcb(&counters);
  195. EXPECT_RET(pcb != NULL);
  196. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  197. /* create a segment (with an overlapping/old seqno so that the new data begins in the 2nd pbuf) */
  198. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, (u32_t)(0-(data_len-new_data_len)), 0, 0);
  199. EXPECT(p != NULL);
  200. if (p != NULL) {
  201. EXPECT(p->next != NULL);
  202. if (p->next != NULL) {
  203. EXPECT(p->next->next != NULL);
  204. }
  205. }
  206. if ((p != NULL) && (p->next != NULL) && (p->next->next != NULL)) {
  207. /* pass the segment to tcp_input */
  208. test_tcp_input(p, &netif);
  209. /* check if counters are as expected */
  210. EXPECT(counters.close_calls == 0);
  211. EXPECT(counters.recv_calls == 1);
  212. EXPECT(counters.recved_bytes == new_data_len);
  213. EXPECT(counters.err_calls == 0);
  214. }
  215. /* make sure the pcb is freed */
  216. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  217. tcp_abort(pcb);
  218. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  219. }
  220. END_TEST
  221. static err_t test_tcp_recv_expect1byte(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
  222. static err_t
  223. test_tcp_recv_expectclose(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
  224. {
  225. EXPECT_RETX(pcb != NULL, ERR_OK);
  226. EXPECT_RETX(err == ERR_OK, ERR_OK);
  227. LWIP_UNUSED_ARG(arg);
  228. if (p != NULL) {
  229. fail();
  230. pbuf_free(p);
  231. } else {
  232. /* correct: FIN received; close our end, too */
  233. err_t err2 = tcp_close(pcb);
  234. fail_unless(err2 == ERR_OK);
  235. /* set back to some other rx function, just to not get here again */
  236. tcp_recv(pcb, test_tcp_recv_expect1byte);
  237. }
  238. return ERR_OK;
  239. }
  240. static err_t
  241. test_tcp_recv_expect1byte(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
  242. {
  243. EXPECT_RETX(pcb != NULL, ERR_OK);
  244. EXPECT_RETX(err == ERR_OK, ERR_OK);
  245. LWIP_UNUSED_ARG(arg);
  246. if (p != NULL) {
  247. if ((p->len == 1) && (p->tot_len == 1)) {
  248. tcp_recv(pcb, test_tcp_recv_expectclose);
  249. } else {
  250. fail();
  251. }
  252. pbuf_free(p);
  253. } else {
  254. fail();
  255. }
  256. return ERR_OK;
  257. }
  258. START_TEST(test_tcp_passive_close)
  259. {
  260. struct test_tcp_counters counters;
  261. struct tcp_pcb* pcb;
  262. struct pbuf* p;
  263. char data = 0x0f;
  264. struct netif netif;
  265. struct test_tcp_txcounters txcounters;
  266. LWIP_UNUSED_ARG(_i);
  267. /* initialize local vars */
  268. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  269. /* initialize counter struct */
  270. memset(&counters, 0, sizeof(counters));
  271. counters.expected_data_len = 1;
  272. counters.expected_data = &data;
  273. /* create and initialize the pcb */
  274. pcb = test_tcp_new_counters_pcb(&counters);
  275. EXPECT_RET(pcb != NULL);
  276. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  277. /* create a segment without data */
  278. p = tcp_create_rx_segment(pcb, &data, 1, 0, 0, TCP_FIN);
  279. EXPECT(p != NULL);
  280. if (p != NULL) {
  281. tcp_recv(pcb, test_tcp_recv_expect1byte);
  282. /* pass the segment to tcp_input */
  283. test_tcp_input(p, &netif);
  284. }
  285. /* don't free the pcb here (part of the test!) */
  286. }
  287. END_TEST
  288. /** Check that we handle malformed tcp headers, and discard the pbuf(s) */
  289. START_TEST(test_tcp_malformed_header)
  290. {
  291. struct test_tcp_counters counters;
  292. struct tcp_pcb* pcb;
  293. struct pbuf* p;
  294. char data[] = {1, 2, 3, 4};
  295. u16_t data_len, chksum;
  296. struct netif netif;
  297. struct test_tcp_txcounters txcounters;
  298. struct tcp_hdr *hdr;
  299. LWIP_UNUSED_ARG(_i);
  300. /* initialize local vars */
  301. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  302. data_len = sizeof(data);
  303. /* initialize counter struct */
  304. memset(&counters, 0, sizeof(counters));
  305. counters.expected_data_len = data_len;
  306. counters.expected_data = data;
  307. /* create and initialize the pcb */
  308. pcb = test_tcp_new_counters_pcb(&counters);
  309. EXPECT_RET(pcb != NULL);
  310. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  311. /* create a segment */
  312. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  313. pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
  314. hdr = (struct tcp_hdr *)p->payload;
  315. TCPH_HDRLEN_FLAGS_SET(hdr, 15, 0x3d1);
  316. hdr->chksum = 0;
  317. chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
  318. &test_remote_ip, &test_local_ip);
  319. hdr->chksum = chksum;
  320. pbuf_header(p, sizeof(struct ip_hdr));
  321. EXPECT(p != NULL);
  322. EXPECT(p->next == NULL);
  323. if (p != NULL) {
  324. /* pass the segment to tcp_input */
  325. test_tcp_input(p, &netif);
  326. /* check if counters are as expected */
  327. EXPECT(counters.close_calls == 0);
  328. EXPECT(counters.recv_calls == 0);
  329. EXPECT(counters.recved_bytes == 0);
  330. EXPECT(counters.err_calls == 0);
  331. }
  332. /* make sure the pcb is freed */
  333. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  334. tcp_abort(pcb);
  335. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  336. }
  337. END_TEST
  338. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  339. * At the end, send more data. */
  340. START_TEST(test_tcp_fast_retx_recover)
  341. {
  342. struct netif netif;
  343. struct test_tcp_txcounters txcounters;
  344. struct test_tcp_counters counters;
  345. struct tcp_pcb* pcb;
  346. struct pbuf* p;
  347. char data1[] = { 1, 2, 3, 4};
  348. char data2[] = { 5, 6, 7, 8};
  349. char data3[] = { 9, 10, 11, 12};
  350. char data4[] = {13, 14, 15, 16};
  351. char data5[] = {17, 18, 19, 20};
  352. char data6[TCP_MSS] = {21, 22, 23, 24};
  353. err_t err;
  354. LWIP_UNUSED_ARG(_i);
  355. /* initialize local vars */
  356. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  357. memset(&counters, 0, sizeof(counters));
  358. /* create and initialize the pcb */
  359. pcb = test_tcp_new_counters_pcb(&counters);
  360. EXPECT_RET(pcb != NULL);
  361. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  362. pcb->mss = TCP_MSS;
  363. /* disable initial congestion window (we don't send a SYN here...) */
  364. pcb->cwnd = pcb->snd_wnd;
  365. /* send data1 */
  366. err = tcp_write(pcb, data1, sizeof(data1), TCP_WRITE_FLAG_COPY);
  367. EXPECT_RET(err == ERR_OK);
  368. err = tcp_output(pcb);
  369. EXPECT_RET(err == ERR_OK);
  370. EXPECT_RET(txcounters.num_tx_calls == 1);
  371. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  372. memset(&txcounters, 0, sizeof(txcounters));
  373. /* "recv" ACK for data1 */
  374. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
  375. EXPECT_RET(p != NULL);
  376. test_tcp_input(p, &netif);
  377. EXPECT_RET(txcounters.num_tx_calls == 0);
  378. EXPECT_RET(pcb->unacked == NULL);
  379. /* send data2 */
  380. err = tcp_write(pcb, data2, sizeof(data2), TCP_WRITE_FLAG_COPY);
  381. EXPECT_RET(err == ERR_OK);
  382. err = tcp_output(pcb);
  383. EXPECT_RET(err == ERR_OK);
  384. EXPECT_RET(txcounters.num_tx_calls == 1);
  385. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  386. memset(&txcounters, 0, sizeof(txcounters));
  387. /* duplicate ACK for data1 (data2 is lost) */
  388. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  389. EXPECT_RET(p != NULL);
  390. test_tcp_input(p, &netif);
  391. EXPECT_RET(txcounters.num_tx_calls == 0);
  392. EXPECT_RET(pcb->dupacks == 1);
  393. /* send data3 */
  394. err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
  395. EXPECT_RET(err == ERR_OK);
  396. err = tcp_output(pcb);
  397. EXPECT_RET(err == ERR_OK);
  398. /* nagle enabled, no tx calls */
  399. EXPECT_RET(txcounters.num_tx_calls == 0);
  400. EXPECT_RET(txcounters.num_tx_bytes == 0);
  401. memset(&txcounters, 0, sizeof(txcounters));
  402. /* 2nd duplicate ACK for data1 (data2 and data3 are lost) */
  403. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  404. EXPECT_RET(p != NULL);
  405. test_tcp_input(p, &netif);
  406. EXPECT_RET(txcounters.num_tx_calls == 0);
  407. EXPECT_RET(pcb->dupacks == 2);
  408. /* queue data4, don't send it (unsent-oversize is != 0) */
  409. err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
  410. EXPECT_RET(err == ERR_OK);
  411. /* 3nd duplicate ACK for data1 (data2 and data3 are lost) -> fast retransmission */
  412. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  413. EXPECT_RET(p != NULL);
  414. test_tcp_input(p, &netif);
  415. /*EXPECT_RET(txcounters.num_tx_calls == 1);*/
  416. EXPECT_RET(pcb->dupacks == 3);
  417. memset(&txcounters, 0, sizeof(txcounters));
  418. /* @todo: check expected data?*/
  419. /* send data5, not output yet */
  420. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  421. EXPECT_RET(err == ERR_OK);
  422. /*err = tcp_output(pcb);
  423. EXPECT_RET(err == ERR_OK);*/
  424. EXPECT_RET(txcounters.num_tx_calls == 0);
  425. EXPECT_RET(txcounters.num_tx_bytes == 0);
  426. memset(&txcounters, 0, sizeof(txcounters));
  427. {
  428. int i = 0;
  429. do
  430. {
  431. err = tcp_write(pcb, data6, TCP_MSS, TCP_WRITE_FLAG_COPY);
  432. i++;
  433. }while(err == ERR_OK);
  434. EXPECT_RET(err != ERR_OK);
  435. }
  436. err = tcp_output(pcb);
  437. EXPECT_RET(err == ERR_OK);
  438. /*EXPECT_RET(txcounters.num_tx_calls == 0);
  439. EXPECT_RET(txcounters.num_tx_bytes == 0);*/
  440. memset(&txcounters, 0, sizeof(txcounters));
  441. /* send even more data */
  442. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  443. EXPECT_RET(err == ERR_OK);
  444. err = tcp_output(pcb);
  445. EXPECT_RET(err == ERR_OK);
  446. /* ...and even more data */
  447. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  448. EXPECT_RET(err == ERR_OK);
  449. err = tcp_output(pcb);
  450. EXPECT_RET(err == ERR_OK);
  451. /* ...and even more data */
  452. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  453. EXPECT_RET(err == ERR_OK);
  454. err = tcp_output(pcb);
  455. EXPECT_RET(err == ERR_OK);
  456. /* ...and even more data */
  457. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  458. EXPECT_RET(err == ERR_OK);
  459. err = tcp_output(pcb);
  460. EXPECT_RET(err == ERR_OK);
  461. /* send ACKs for data2 and data3 */
  462. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 12, TCP_ACK);
  463. EXPECT_RET(p != NULL);
  464. test_tcp_input(p, &netif);
  465. /*EXPECT_RET(txcounters.num_tx_calls == 0);*/
  466. /* ...and even more data */
  467. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  468. EXPECT_RET(err == ERR_OK);
  469. err = tcp_output(pcb);
  470. EXPECT_RET(err == ERR_OK);
  471. /* ...and even more data */
  472. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  473. EXPECT_RET(err == ERR_OK);
  474. err = tcp_output(pcb);
  475. EXPECT_RET(err == ERR_OK);
  476. #if 0
  477. /* create expected segment */
  478. p1 = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  479. EXPECT_RET(p != NULL);
  480. if (p != NULL) {
  481. /* pass the segment to tcp_input */
  482. test_tcp_input(p, &netif);
  483. /* check if counters are as expected */
  484. EXPECT_RET(counters.close_calls == 0);
  485. EXPECT_RET(counters.recv_calls == 1);
  486. EXPECT_RET(counters.recved_bytes == data_len);
  487. EXPECT_RET(counters.err_calls == 0);
  488. }
  489. #endif
  490. /* make sure the pcb is freed */
  491. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  492. tcp_abort(pcb);
  493. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  494. }
  495. END_TEST
  496. static u8_t tx_data[TCP_WND*2];
  497. static void
  498. check_seqnos(struct tcp_seg *segs, int num_expected, u32_t *seqnos_expected)
  499. {
  500. struct tcp_seg *s = segs;
  501. int i;
  502. for (i = 0; i < num_expected; i++, s = s->next) {
  503. EXPECT_RET(s != NULL);
  504. EXPECT(s->tcphdr->seqno == htonl(seqnos_expected[i]));
  505. }
  506. EXPECT(s == NULL);
  507. }
  508. /** Send data with sequence numbers that wrap around the u32_t range.
  509. * Then, provoke fast retransmission by duplicate ACKs and check that all
  510. * segment lists are still properly sorted. */
  511. START_TEST(test_tcp_fast_rexmit_wraparound)
  512. {
  513. struct netif netif;
  514. struct test_tcp_txcounters txcounters;
  515. struct test_tcp_counters counters;
  516. struct tcp_pcb* pcb;
  517. struct pbuf* p;
  518. err_t err;
  519. u16_t i, sent_total = 0;
  520. LWIP_UNUSED_ARG(_i);
  521. for (i = 0; i < sizeof(tx_data); i++) {
  522. tx_data[i] = (u8_t)i;
  523. }
  524. /* initialize local vars */
  525. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  526. memset(&counters, 0, sizeof(counters));
  527. /* create and initialize the pcb */
  528. tcp_ticks = SEQNO1 - ISS;
  529. pcb = test_tcp_new_counters_pcb(&counters);
  530. EXPECT_RET(pcb != NULL);
  531. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  532. pcb->mss = TCP_MSS;
  533. /* disable initial congestion window (we don't send a SYN here...) */
  534. pcb->cwnd = 2*TCP_MSS;
  535. /* start in congestion advoidance */
  536. pcb->ssthresh = pcb->cwnd;
  537. /* send 6 mss-sized segments */
  538. for (i = 0; i < 6; i++) {
  539. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  540. EXPECT_RET(err == ERR_OK);
  541. sent_total += TCP_MSS;
  542. }
  543. check_seqnos(pcb->unsent, 6, seqnos);
  544. EXPECT(pcb->unacked == NULL);
  545. err = tcp_output(pcb);
  546. EXPECT(txcounters.num_tx_calls == 2);
  547. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  548. memset(&txcounters, 0, sizeof(txcounters));
  549. check_seqnos(pcb->unacked, 2, seqnos);
  550. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  551. /* ACK the first segment */
  552. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  553. test_tcp_input(p, &netif);
  554. /* ensure this didn't trigger a retransmission. Only one
  555. segment should be transmitted because cwnd opened up by
  556. TCP_MSS and a fraction since we are in congestion avoidance */
  557. EXPECT(txcounters.num_tx_calls == 1);
  558. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  559. memset(&txcounters, 0, sizeof(txcounters));
  560. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  561. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  562. /* 3 dupacks */
  563. EXPECT(pcb->dupacks == 0);
  564. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  565. test_tcp_input(p, &netif);
  566. EXPECT(txcounters.num_tx_calls == 0);
  567. EXPECT(pcb->dupacks == 1);
  568. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  569. test_tcp_input(p, &netif);
  570. EXPECT(txcounters.num_tx_calls == 0);
  571. EXPECT(pcb->dupacks == 2);
  572. /* 3rd dupack -> fast rexmit */
  573. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  574. test_tcp_input(p, &netif);
  575. EXPECT(pcb->dupacks == 3);
  576. EXPECT(txcounters.num_tx_calls == 4);
  577. memset(&txcounters, 0, sizeof(txcounters));
  578. EXPECT(pcb->unsent == NULL);
  579. check_seqnos(pcb->unacked, 5, &seqnos[1]);
  580. /* make sure the pcb is freed */
  581. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  582. tcp_abort(pcb);
  583. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  584. }
  585. END_TEST
  586. /** Send data with sequence numbers that wrap around the u32_t range.
  587. * Then, provoke RTO retransmission and check that all
  588. * segment lists are still properly sorted. */
  589. START_TEST(test_tcp_rto_rexmit_wraparound)
  590. {
  591. struct netif netif;
  592. struct test_tcp_txcounters txcounters;
  593. struct test_tcp_counters counters;
  594. struct tcp_pcb* pcb;
  595. err_t err;
  596. u16_t i, sent_total = 0;
  597. LWIP_UNUSED_ARG(_i);
  598. for (i = 0; i < sizeof(tx_data); i++) {
  599. tx_data[i] = (u8_t)i;
  600. }
  601. /* initialize local vars */
  602. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  603. memset(&counters, 0, sizeof(counters));
  604. /* create and initialize the pcb */
  605. tcp_ticks = 0;
  606. tcp_ticks = 0 - tcp_next_iss(NULL);
  607. tcp_ticks = SEQNO1 - tcp_next_iss(NULL);
  608. pcb = test_tcp_new_counters_pcb(&counters);
  609. EXPECT_RET(pcb != NULL);
  610. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  611. pcb->mss = TCP_MSS;
  612. /* disable initial congestion window (we don't send a SYN here...) */
  613. pcb->cwnd = 2*TCP_MSS;
  614. /* send 6 mss-sized segments */
  615. for (i = 0; i < 6; i++) {
  616. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  617. EXPECT_RET(err == ERR_OK);
  618. sent_total += TCP_MSS;
  619. }
  620. check_seqnos(pcb->unsent, 6, seqnos);
  621. EXPECT(pcb->unacked == NULL);
  622. err = tcp_output(pcb);
  623. EXPECT(txcounters.num_tx_calls == 2);
  624. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  625. memset(&txcounters, 0, sizeof(txcounters));
  626. check_seqnos(pcb->unacked, 2, seqnos);
  627. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  628. /* call the tcp timer some times */
  629. for (i = 0; i < 10; i++) {
  630. test_tcp_tmr();
  631. EXPECT(txcounters.num_tx_calls == 0);
  632. }
  633. /* 11th call to tcp_tmr: RTO rexmit fires */
  634. test_tcp_tmr();
  635. EXPECT(txcounters.num_tx_calls == 1);
  636. check_seqnos(pcb->unacked, 1, seqnos);
  637. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  638. /* fake greater cwnd */
  639. pcb->cwnd = pcb->snd_wnd;
  640. /* send more data */
  641. err = tcp_output(pcb);
  642. EXPECT(err == ERR_OK);
  643. /* check queues are sorted */
  644. EXPECT(pcb->unsent == NULL);
  645. check_seqnos(pcb->unacked, 6, seqnos);
  646. /* make sure the pcb is freed */
  647. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  648. tcp_abort(pcb);
  649. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  650. }
  651. END_TEST
  652. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  653. * At the end, send more data. */
  654. static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
  655. {
  656. struct netif netif;
  657. struct test_tcp_txcounters txcounters;
  658. struct test_tcp_counters counters;
  659. struct tcp_pcb* pcb;
  660. struct pbuf *p;
  661. err_t err;
  662. u16_t sent_total, i;
  663. u8_t expected = 0xFE;
  664. for (i = 0; i < sizeof(tx_data); i++) {
  665. u8_t d = (u8_t)i;
  666. if (d == 0xFE) {
  667. d = 0xF0;
  668. }
  669. tx_data[i] = d;
  670. }
  671. if (zero_window_probe_from_unsent) {
  672. tx_data[TCP_WND] = expected;
  673. } else {
  674. tx_data[0] = expected;
  675. }
  676. /* initialize local vars */
  677. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  678. memset(&counters, 0, sizeof(counters));
  679. /* create and initialize the pcb */
  680. pcb = test_tcp_new_counters_pcb(&counters);
  681. EXPECT_RET(pcb != NULL);
  682. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  683. pcb->mss = TCP_MSS;
  684. /* disable initial congestion window (we don't send a SYN here...) */
  685. pcb->cwnd = pcb->snd_wnd;
  686. /* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
  687. sent_total = 0;
  688. if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
  689. u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
  690. err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
  691. EXPECT_RET(err == ERR_OK);
  692. err = tcp_output(pcb);
  693. EXPECT_RET(err == ERR_OK);
  694. EXPECT(txcounters.num_tx_calls == 1);
  695. EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
  696. memset(&txcounters, 0, sizeof(txcounters));
  697. sent_total += initial_data_len;
  698. }
  699. for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
  700. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  701. EXPECT_RET(err == ERR_OK);
  702. err = tcp_output(pcb);
  703. EXPECT_RET(err == ERR_OK);
  704. EXPECT(txcounters.num_tx_calls == 1);
  705. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  706. memset(&txcounters, 0, sizeof(txcounters));
  707. }
  708. EXPECT(sent_total == (TCP_WND - TCP_MSS));
  709. /* now ACK the packet before the first */
  710. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  711. test_tcp_input(p, &netif);
  712. /* ensure this didn't trigger a retransmission */
  713. EXPECT(txcounters.num_tx_calls == 0);
  714. EXPECT(txcounters.num_tx_bytes == 0);
  715. EXPECT(pcb->persist_backoff == 0);
  716. /* send the last packet, now a complete window has been sent */
  717. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  718. sent_total += TCP_MSS;
  719. EXPECT_RET(err == ERR_OK);
  720. err = tcp_output(pcb);
  721. EXPECT_RET(err == ERR_OK);
  722. EXPECT(txcounters.num_tx_calls == 1);
  723. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  724. memset(&txcounters, 0, sizeof(txcounters));
  725. EXPECT(pcb->persist_backoff == 0);
  726. if (zero_window_probe_from_unsent) {
  727. /* ACK all data but close the TX window */
  728. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
  729. test_tcp_input(p, &netif);
  730. /* ensure this didn't trigger any transmission */
  731. EXPECT(txcounters.num_tx_calls == 0);
  732. EXPECT(txcounters.num_tx_bytes == 0);
  733. /* window is completely full, but persist timer is off since send buffer is empty */
  734. EXPECT(pcb->snd_wnd == 0);
  735. EXPECT(pcb->persist_backoff == 0);
  736. }
  737. /* send one byte more (out of window) -> persist timer starts */
  738. err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
  739. EXPECT_RET(err == ERR_OK);
  740. err = tcp_output(pcb);
  741. EXPECT_RET(err == ERR_OK);
  742. EXPECT(txcounters.num_tx_calls == 0);
  743. EXPECT(txcounters.num_tx_bytes == 0);
  744. memset(&txcounters, 0, sizeof(txcounters));
  745. if (!zero_window_probe_from_unsent) {
  746. /* no persist timer unless a zero window announcement has been received */
  747. EXPECT(pcb->persist_backoff == 0);
  748. } else {
  749. EXPECT(pcb->persist_backoff == 1);
  750. /* call tcp_timer some more times to let persist timer count up */
  751. for (i = 0; i < 4; i++) {
  752. test_tcp_tmr();
  753. EXPECT(txcounters.num_tx_calls == 0);
  754. EXPECT(txcounters.num_tx_bytes == 0);
  755. }
  756. /* this should trigger the zero-window-probe */
  757. txcounters.copy_tx_packets = 1;
  758. test_tcp_tmr();
  759. txcounters.copy_tx_packets = 0;
  760. EXPECT(txcounters.num_tx_calls == 1);
  761. EXPECT(txcounters.num_tx_bytes == 1 + 40U);
  762. EXPECT(txcounters.tx_packets != NULL);
  763. if (txcounters.tx_packets != NULL) {
  764. u8_t sent;
  765. u16_t ret;
  766. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
  767. EXPECT(ret == 1);
  768. EXPECT(sent == expected);
  769. }
  770. if (txcounters.tx_packets != NULL) {
  771. pbuf_free(txcounters.tx_packets);
  772. txcounters.tx_packets = NULL;
  773. }
  774. }
  775. /* make sure the pcb is freed */
  776. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  777. tcp_abort(pcb);
  778. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  779. }
  780. START_TEST(test_tcp_tx_full_window_lost_from_unsent)
  781. {
  782. LWIP_UNUSED_ARG(_i);
  783. test_tcp_tx_full_window_lost(1);
  784. }
  785. END_TEST
  786. START_TEST(test_tcp_tx_full_window_lost_from_unacked)
  787. {
  788. LWIP_UNUSED_ARG(_i);
  789. test_tcp_tx_full_window_lost(0);
  790. }
  791. END_TEST
  792. START_TEST(test_tcp_rto_tracking)
  793. {
  794. struct netif netif;
  795. struct test_tcp_txcounters txcounters;
  796. struct test_tcp_counters counters;
  797. struct tcp_pcb* pcb;
  798. struct pbuf* p;
  799. err_t err;
  800. u16_t i, sent_total = 0;
  801. LWIP_UNUSED_ARG(_i);
  802. for (i = 0; i < sizeof(tx_data); i++) {
  803. tx_data[i] = (u8_t)i;
  804. }
  805. /* initialize local vars */
  806. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  807. memset(&counters, 0, sizeof(counters));
  808. /* create and initialize the pcb */
  809. tcp_ticks = SEQNO1 - ISS;
  810. pcb = test_tcp_new_counters_pcb(&counters);
  811. EXPECT_RET(pcb != NULL);
  812. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  813. pcb->mss = TCP_MSS;
  814. /* Set congestion window large enough to send all our segments */
  815. pcb->cwnd = 5*TCP_MSS;
  816. /* send 5 mss-sized segments */
  817. for (i = 0; i < 5; i++) {
  818. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  819. EXPECT_RET(err == ERR_OK);
  820. sent_total += TCP_MSS;
  821. }
  822. check_seqnos(pcb->unsent, 5, seqnos);
  823. EXPECT(pcb->unacked == NULL);
  824. err = tcp_output(pcb);
  825. EXPECT(txcounters.num_tx_calls == 5);
  826. EXPECT(txcounters.num_tx_bytes == 5 * (TCP_MSS + 40U));
  827. memset(&txcounters, 0, sizeof(txcounters));
  828. /* Check all 5 are in-flight */
  829. EXPECT(pcb->unsent == NULL);
  830. check_seqnos(pcb->unacked, 5, seqnos);
  831. /* Force us into retransmisson timeout */
  832. while (!(pcb->flags & TF_RTO)) {
  833. test_tcp_tmr();
  834. }
  835. /* Ensure 4 remaining segments are back on unsent, ready for retransmission */
  836. check_seqnos(pcb->unsent, 4, &seqnos[1]);
  837. /* Ensure 1st segment is on unacked (already retransmitted) */
  838. check_seqnos(pcb->unacked, 1, seqnos);
  839. EXPECT(txcounters.num_tx_calls == 1);
  840. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  841. memset(&txcounters, 0, sizeof(txcounters));
  842. /* Ensure rto_end points to next byte */
  843. EXPECT(pcb->rto_end == seqnos[5]);
  844. EXPECT(pcb->rto_end == pcb->snd_nxt);
  845. /* Check cwnd was reset */
  846. EXPECT(pcb->cwnd == pcb->mss);
  847. /* Add another segment to send buffer which is outside of RTO */
  848. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  849. EXPECT_RET(err == ERR_OK);
  850. sent_total += TCP_MSS;
  851. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  852. /* Ensure no new data was sent */
  853. EXPECT(txcounters.num_tx_calls == 0);
  854. EXPECT(txcounters.num_tx_bytes == 0);
  855. EXPECT(pcb->rto_end == pcb->snd_nxt);
  856. /* ACK first segment */
  857. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  858. test_tcp_input(p, &netif);
  859. /* Next two retranmissions should go out, due to cwnd in slow start */
  860. EXPECT(txcounters.num_tx_calls == 2);
  861. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  862. memset(&txcounters, 0, sizeof(txcounters));
  863. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  864. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  865. /* RTO should still be marked */
  866. EXPECT(pcb->flags & TF_RTO);
  867. /* cwnd should have only grown by 1 MSS */
  868. EXPECT(pcb->cwnd == (tcpwnd_size_t)(2 * pcb->mss));
  869. /* Ensure no new data was sent */
  870. EXPECT(pcb->rto_end == pcb->snd_nxt);
  871. /* ACK the next two segments */
  872. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
  873. test_tcp_input(p, &netif);
  874. /* Final 2 retransmissions and 1 new data should go out */
  875. EXPECT(txcounters.num_tx_calls == 3);
  876. EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
  877. memset(&txcounters, 0, sizeof(txcounters));
  878. check_seqnos(pcb->unacked, 3, &seqnos[3]);
  879. EXPECT(pcb->unsent == NULL);
  880. /* RTO should still be marked */
  881. EXPECT(pcb->flags & TF_RTO);
  882. /* cwnd should have only grown by 1 MSS */
  883. EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
  884. /* snd_nxt should have been advanced past rto_end */
  885. EXPECT(TCP_SEQ_GT(pcb->snd_nxt, pcb->rto_end));
  886. /* ACK the next two segments, finishing our RTO, leaving new segment unacked */
  887. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
  888. test_tcp_input(p, &netif);
  889. EXPECT(!(pcb->flags & TF_RTO));
  890. check_seqnos(pcb->unacked, 1, &seqnos[5]);
  891. /* We should be in ABC congestion avoidance, so no change in cwnd */
  892. EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
  893. EXPECT(pcb->cwnd >= pcb->ssthresh);
  894. /* Ensure ABC congestion avoidance is tracking bytes acked */
  895. EXPECT(pcb->bytes_acked == (tcpwnd_size_t)(2 * pcb->mss));
  896. /* make sure the pcb is freed */
  897. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  898. tcp_abort(pcb);
  899. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  900. }
  901. END_TEST
  902. START_TEST(test_tcp_rto_timeout)
  903. {
  904. struct netif netif;
  905. struct test_tcp_txcounters txcounters;
  906. struct test_tcp_counters counters;
  907. struct tcp_pcb *pcb, *cur;
  908. err_t err;
  909. u16_t i;
  910. LWIP_UNUSED_ARG(_i);
  911. /* Setup data for a single segment */
  912. for (i = 0; i < TCP_MSS; i++) {
  913. tx_data[i] = (u8_t)i;
  914. }
  915. /* initialize local vars */
  916. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  917. memset(&counters, 0, sizeof(counters));
  918. /* create and initialize the pcb */
  919. tcp_ticks = SEQNO1 - ISS;
  920. pcb = test_tcp_new_counters_pcb(&counters);
  921. EXPECT_RET(pcb != NULL);
  922. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  923. pcb->mss = TCP_MSS;
  924. pcb->cwnd = TCP_MSS;
  925. /* send our segment */
  926. err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
  927. EXPECT_RET(err == ERR_OK);
  928. err = tcp_output(pcb);
  929. EXPECT(txcounters.num_tx_calls == 1);
  930. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  931. memset(&txcounters, 0, sizeof(txcounters));
  932. /* ensure no errors have been recorded */
  933. EXPECT(counters.err_calls == 0);
  934. EXPECT(counters.last_err == ERR_OK);
  935. /* Force us into retransmisson timeout */
  936. while (!(pcb->flags & TF_RTO)) {
  937. test_tcp_tmr();
  938. }
  939. /* check first rexmit */
  940. EXPECT(pcb->nrtx == 1);
  941. EXPECT(txcounters.num_tx_calls == 1);
  942. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  943. /* still no error expected */
  944. EXPECT(counters.err_calls == 0);
  945. EXPECT(counters.last_err == ERR_OK);
  946. /* keep running the timer till we hit our maximum RTO */
  947. while (counters.last_err == ERR_OK) {
  948. test_tcp_tmr();
  949. }
  950. /* check number of retransmissions */
  951. EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
  952. EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (TCP_MSS + 40U));
  953. /* check the connection (pcb) has been aborted */
  954. EXPECT(counters.err_calls == 1);
  955. EXPECT(counters.last_err == ERR_ABRT);
  956. /* check our pcb is no longer active */
  957. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  958. EXPECT(cur != pcb);
  959. }
  960. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  961. }
  962. END_TEST
  963. START_TEST(test_tcp_zwp_timeout)
  964. {
  965. struct netif netif;
  966. struct test_tcp_txcounters txcounters;
  967. struct test_tcp_counters counters;
  968. struct tcp_pcb *pcb, *cur;
  969. struct pbuf* p;
  970. err_t err;
  971. u16_t i;
  972. LWIP_UNUSED_ARG(_i);
  973. /* Setup data for two segments */
  974. for (i = 0; i < 2*TCP_MSS; i++) {
  975. tx_data[i] = (u8_t)i;
  976. }
  977. /* initialize local vars */
  978. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  979. memset(&counters, 0, sizeof(counters));
  980. /* create and initialize the pcb */
  981. tcp_ticks = SEQNO1 - ISS;
  982. pcb = test_tcp_new_counters_pcb(&counters);
  983. EXPECT_RET(pcb != NULL);
  984. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  985. pcb->mss = TCP_MSS;
  986. pcb->cwnd = TCP_MSS;
  987. /* send first segment */
  988. err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
  989. EXPECT(err == ERR_OK);
  990. err = tcp_output(pcb);
  991. EXPECT(err == ERR_OK);
  992. /* verify segment is in-flight */
  993. EXPECT(pcb->unsent == NULL);
  994. check_seqnos(pcb->unacked, 1, seqnos);
  995. EXPECT(txcounters.num_tx_calls == 1);
  996. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  997. memset(&txcounters, 0, sizeof(txcounters));
  998. /* ACK the segment and close the TX window */
  999. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK, 0);
  1000. test_tcp_input(p, &netif);
  1001. EXPECT(pcb->unacked == NULL);
  1002. EXPECT(pcb->unsent == NULL);
  1003. /* send buffer empty, persist should be off */
  1004. EXPECT(pcb->persist_backoff == 0);
  1005. EXPECT(pcb->snd_wnd == 0);
  1006. /* send second segment, should be buffered */
  1007. err = tcp_write(pcb, &tx_data[TCP_MSS], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1008. EXPECT(err == ERR_OK);
  1009. err = tcp_output(pcb);
  1010. EXPECT(err == ERR_OK);
  1011. /* ensure it is buffered and persist timer started */
  1012. EXPECT(pcb->unacked == NULL);
  1013. check_seqnos(pcb->unsent, 1, &seqnos[1]);
  1014. EXPECT(txcounters.num_tx_calls == 0);
  1015. EXPECT(txcounters.num_tx_bytes == 0);
  1016. EXPECT(pcb->persist_backoff == 1);
  1017. /* ensure no errors have been recorded */
  1018. EXPECT(counters.err_calls == 0);
  1019. EXPECT(counters.last_err == ERR_OK);
  1020. /* run timer till first probe */
  1021. EXPECT(pcb->persist_probe == 0);
  1022. while (pcb->persist_probe == 0) {
  1023. test_tcp_tmr();
  1024. }
  1025. EXPECT(txcounters.num_tx_calls == 1);
  1026. EXPECT(txcounters.num_tx_bytes == (1 + 40U));
  1027. memset(&txcounters, 0, sizeof(txcounters));
  1028. /* respond to probe with remote's current SEQ, ACK, and zero-window */
  1029. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 0, TCP_ACK, 0);
  1030. test_tcp_input(p, &netif);
  1031. /* ensure zero-window is still active, but probe count reset */
  1032. EXPECT(pcb->persist_backoff > 1);
  1033. EXPECT(pcb->persist_probe == 0);
  1034. EXPECT(pcb->snd_wnd == 0);
  1035. /* ensure no errors have been recorded */
  1036. EXPECT(counters.err_calls == 0);
  1037. EXPECT(counters.last_err == ERR_OK);
  1038. /* now run the timer till we hit our maximum probe count */
  1039. while (counters.last_err == ERR_OK) {
  1040. test_tcp_tmr();
  1041. }
  1042. /* check maximum number of 1 byte probes were sent */
  1043. EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
  1044. EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (1 + 40U));
  1045. /* check the connection (pcb) has been aborted */
  1046. EXPECT(counters.err_calls == 1);
  1047. EXPECT(counters.last_err == ERR_ABRT);
  1048. /* check our pcb is no longer active */
  1049. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1050. EXPECT(cur != pcb);
  1051. }
  1052. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1053. }
  1054. END_TEST
  1055. START_TEST(test_tcp_persist_split)
  1056. {
  1057. struct netif netif;
  1058. struct test_tcp_txcounters txcounters;
  1059. struct test_tcp_counters counters;
  1060. struct tcp_pcb *pcb;
  1061. struct pbuf* p;
  1062. err_t err;
  1063. u16_t i;
  1064. LWIP_UNUSED_ARG(_i);
  1065. /* Setup data for four segments */
  1066. for (i = 0; i < 4 * TCP_MSS; i++) {
  1067. tx_data[i] = (u8_t)i;
  1068. }
  1069. /* initialize local vars */
  1070. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1071. memset(&counters, 0, sizeof(counters));
  1072. /* create and initialize the pcb */
  1073. tcp_ticks = SEQNO1 - ISS;
  1074. pcb = test_tcp_new_counters_pcb(&counters);
  1075. EXPECT_RET(pcb != NULL);
  1076. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1077. pcb->mss = TCP_MSS;
  1078. /* set window to three segments */
  1079. pcb->cwnd = 3 * TCP_MSS;
  1080. pcb->snd_wnd = 3 * TCP_MSS;
  1081. pcb->snd_wnd_max = 3 * TCP_MSS;
  1082. /* send three segments */
  1083. err = tcp_write(pcb, &tx_data[0], 3 * TCP_MSS, TCP_WRITE_FLAG_COPY);
  1084. EXPECT(err == ERR_OK);
  1085. err = tcp_output(pcb);
  1086. EXPECT(err == ERR_OK);
  1087. /* verify segments are in-flight */
  1088. EXPECT(pcb->unsent == NULL);
  1089. EXPECT(pcb->unacked != NULL);
  1090. check_seqnos(pcb->unacked, 3, seqnos);
  1091. EXPECT(txcounters.num_tx_calls == 3);
  1092. EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
  1093. memset(&txcounters, 0, sizeof(txcounters));
  1094. /* ACK the segments and update the window to only 1/2 TCP_MSS */
  1095. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 3 * TCP_MSS, TCP_ACK, TCP_MSS / 2);
  1096. test_tcp_input(p, &netif);
  1097. EXPECT(pcb->unacked == NULL);
  1098. EXPECT(pcb->unsent == NULL);
  1099. EXPECT(pcb->persist_backoff == 0);
  1100. EXPECT(pcb->snd_wnd == TCP_MSS / 2);
  1101. /* send fourth segment, which is larger than snd_wnd */
  1102. err = tcp_write(pcb, &tx_data[3 * TCP_MSS], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1103. EXPECT(err == ERR_OK);
  1104. err = tcp_output(pcb);
  1105. EXPECT(err == ERR_OK);
  1106. /* ensure it is buffered and persist timer started */
  1107. EXPECT(pcb->unacked == NULL);
  1108. EXPECT(pcb->unsent != NULL);
  1109. check_seqnos(pcb->unsent, 1, &seqnos[3]);
  1110. EXPECT(txcounters.num_tx_calls == 0);
  1111. EXPECT(txcounters.num_tx_bytes == 0);
  1112. EXPECT(pcb->persist_backoff == 1);
  1113. /* ensure no errors have been recorded */
  1114. EXPECT(counters.err_calls == 0);
  1115. EXPECT(counters.last_err == ERR_OK);
  1116. /* call tcp_timer some more times to let persist timer count up */
  1117. for (i = 0; i < 4; i++) {
  1118. test_tcp_tmr();
  1119. EXPECT(txcounters.num_tx_calls == 0);
  1120. EXPECT(txcounters.num_tx_bytes == 0);
  1121. }
  1122. /* this should be the first timer shot, which should split the
  1123. * segment and send a runt (of the remaining window size) */
  1124. txcounters.copy_tx_packets = 1;
  1125. test_tcp_tmr();
  1126. txcounters.copy_tx_packets = 0;
  1127. /* persist will be disabled as RTO timer takes over */
  1128. EXPECT(pcb->persist_backoff == 0);
  1129. EXPECT(txcounters.num_tx_calls == 1);
  1130. EXPECT(txcounters.num_tx_bytes == ((TCP_MSS /2) + 40U));
  1131. /* verify half segment sent, half still buffered */
  1132. EXPECT(pcb->unsent != NULL);
  1133. EXPECT(pcb->unsent->len == TCP_MSS / 2);
  1134. EXPECT(pcb->unacked != NULL);
  1135. EXPECT(pcb->unacked->len == TCP_MSS / 2);
  1136. /* verify first half segment */
  1137. EXPECT(txcounters.tx_packets != NULL);
  1138. if (txcounters.tx_packets != NULL) {
  1139. u8_t sent[TCP_MSS / 2];
  1140. u16_t ret;
  1141. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 2, 40U);
  1142. EXPECT(ret == TCP_MSS / 2);
  1143. EXPECT(memcmp(sent, &tx_data[3 * TCP_MSS], TCP_MSS / 2) == 0);
  1144. }
  1145. if (txcounters.tx_packets != NULL) {
  1146. pbuf_free(txcounters.tx_packets);
  1147. txcounters.tx_packets = NULL;
  1148. }
  1149. memset(&txcounters, 0, sizeof(txcounters));
  1150. /* ACK the half segment, leave window at half segment */
  1151. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS / 2, TCP_ACK, TCP_MSS / 2);
  1152. txcounters.copy_tx_packets = 1;
  1153. test_tcp_input(p, &netif);
  1154. txcounters.copy_tx_packets = 0;
  1155. /* ensure remaining half segment was sent */
  1156. EXPECT(txcounters.num_tx_calls == 1);
  1157. EXPECT(txcounters.num_tx_bytes == ((TCP_MSS /2 ) + 40U));
  1158. EXPECT(pcb->unsent == NULL);
  1159. EXPECT(pcb->unacked != NULL);
  1160. EXPECT(pcb->unacked->len == TCP_MSS / 2);
  1161. EXPECT(pcb->snd_wnd == TCP_MSS / 2);
  1162. /* verify second half segment */
  1163. EXPECT(txcounters.tx_packets != NULL);
  1164. if (txcounters.tx_packets != NULL) {
  1165. u8_t sent[TCP_MSS / 2];
  1166. u16_t ret;
  1167. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 2, 40U);
  1168. EXPECT(ret == TCP_MSS / 2);
  1169. EXPECT(memcmp(sent, &tx_data[(3 * TCP_MSS) + TCP_MSS / 2], TCP_MSS / 2) == 0);
  1170. }
  1171. if (txcounters.tx_packets != NULL) {
  1172. pbuf_free(txcounters.tx_packets);
  1173. txcounters.tx_packets = NULL;
  1174. }
  1175. /* ensure no errors have been recorded */
  1176. EXPECT(counters.err_calls == 0);
  1177. EXPECT(counters.last_err == ERR_OK);
  1178. /* make sure the pcb is freed */
  1179. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  1180. tcp_abort(pcb);
  1181. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1182. }
  1183. END_TEST
  1184. /** Create the suite including all tests for this module */
  1185. Suite *
  1186. tcp_suite(void)
  1187. {
  1188. testfunc tests[] = {
  1189. TESTFUNC(test_tcp_new_abort),
  1190. TESTFUNC(test_tcp_listen_passive_open),
  1191. TESTFUNC(test_tcp_recv_inseq),
  1192. TESTFUNC(test_tcp_recv_inseq_trim),
  1193. TESTFUNC(test_tcp_passive_close),
  1194. TESTFUNC(test_tcp_malformed_header),
  1195. TESTFUNC(test_tcp_fast_retx_recover),
  1196. TESTFUNC(test_tcp_fast_rexmit_wraparound),
  1197. TESTFUNC(test_tcp_rto_rexmit_wraparound),
  1198. TESTFUNC(test_tcp_tx_full_window_lost_from_unacked),
  1199. TESTFUNC(test_tcp_tx_full_window_lost_from_unsent),
  1200. TESTFUNC(test_tcp_rto_tracking),
  1201. TESTFUNC(test_tcp_rto_timeout),
  1202. TESTFUNC(test_tcp_zwp_timeout),
  1203. TESTFUNC(test_tcp_persist_split)
  1204. };
  1205. return create_suite("TCP", tests, sizeof(tests)/sizeof(testfunc), tcp_setup, tcp_teardown);
  1206. }