test_tcp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "test_tcp.h"
  2. #include "lwip/tcp_impl.h"
  3. #include "lwip/stats.h"
  4. #include "tcp_helper.h"
  5. #ifdef _MSC_VER
  6. #pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
  7. #endif
  8. #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
  9. #error "This tests needs TCP- and MEMP-statistics enabled"
  10. #endif
  11. #if TCP_SND_BUF <= TCP_WND
  12. #error "This tests needs TCP_SND_BUF to be > TCP_WND"
  13. #endif
  14. static u8_t test_tcp_timer;
  15. /* our own version of tcp_tmr so we can reset fast/slow timer state */
  16. static void
  17. test_tcp_tmr(void)
  18. {
  19. tcp_fasttmr();
  20. if (++test_tcp_timer & 1) {
  21. tcp_slowtmr();
  22. }
  23. }
  24. /* Setups/teardown functions */
  25. static void
  26. tcp_setup(void)
  27. {
  28. /* reset iss to default (6510) */
  29. tcp_ticks = 0;
  30. tcp_ticks = 0 - (tcp_next_iss() - 6510);
  31. tcp_next_iss();
  32. tcp_ticks = 0;
  33. test_tcp_timer = 0;
  34. tcp_remove_all();
  35. }
  36. static void
  37. tcp_teardown(void)
  38. {
  39. netif_list = NULL;
  40. tcp_remove_all();
  41. }
  42. /* Test functions */
  43. /** Call tcp_new() and tcp_abort() and test memp stats */
  44. START_TEST(test_tcp_new_abort)
  45. {
  46. struct tcp_pcb* pcb;
  47. LWIP_UNUSED_ARG(_i);
  48. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  49. pcb = tcp_new();
  50. fail_unless(pcb != NULL);
  51. if (pcb != NULL) {
  52. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  53. tcp_abort(pcb);
  54. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  55. }
  56. }
  57. END_TEST
  58. /** Create an ESTABLISHED pcb and check if receive callback is called */
  59. START_TEST(test_tcp_recv_inseq)
  60. {
  61. struct test_tcp_counters counters;
  62. struct tcp_pcb* pcb;
  63. struct pbuf* p;
  64. char data[] = {1, 2, 3, 4};
  65. ip_addr_t remote_ip, local_ip;
  66. u16_t data_len;
  67. u16_t remote_port = 0x100, local_port = 0x101;
  68. struct netif netif;
  69. LWIP_UNUSED_ARG(_i);
  70. /* initialize local vars */
  71. memset(&netif, 0, sizeof(netif));
  72. IP4_ADDR(&local_ip, 192, 168, 1, 1);
  73. IP4_ADDR(&remote_ip, 192, 168, 1, 2);
  74. data_len = sizeof(data);
  75. /* initialize counter struct */
  76. memset(&counters, 0, sizeof(counters));
  77. counters.expected_data_len = data_len;
  78. counters.expected_data = data;
  79. /* create and initialize the pcb */
  80. pcb = test_tcp_new_counters_pcb(&counters);
  81. EXPECT_RET(pcb != NULL);
  82. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  83. /* create a segment */
  84. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  85. EXPECT(p != NULL);
  86. if (p != NULL) {
  87. /* pass the segment to tcp_input */
  88. test_tcp_input(p, &netif);
  89. /* check if counters are as expected */
  90. EXPECT(counters.close_calls == 0);
  91. EXPECT(counters.recv_calls == 1);
  92. EXPECT(counters.recved_bytes == data_len);
  93. EXPECT(counters.err_calls == 0);
  94. }
  95. /* make sure the pcb is freed */
  96. EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  97. tcp_abort(pcb);
  98. EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  99. }
  100. END_TEST
  101. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  102. * At the end, send more data. */
  103. START_TEST(test_tcp_fast_retx_recover)
  104. {
  105. struct netif netif;
  106. struct test_tcp_txcounters txcounters;
  107. struct test_tcp_counters counters;
  108. struct tcp_pcb* pcb;
  109. struct pbuf* p;
  110. char data1[] = { 1, 2, 3, 4};
  111. char data2[] = { 5, 6, 7, 8};
  112. char data3[] = { 9, 10, 11, 12};
  113. char data4[] = {13, 14, 15, 16};
  114. char data5[] = {17, 18, 19, 20};
  115. char data6[] = {21, 22, 23, 24};
  116. ip_addr_t remote_ip, local_ip, netmask;
  117. u16_t remote_port = 0x100, local_port = 0x101;
  118. err_t err;
  119. LWIP_UNUSED_ARG(_i);
  120. /* initialize local vars */
  121. IP4_ADDR(&local_ip, 192, 168, 1, 1);
  122. IP4_ADDR(&remote_ip, 192, 168, 1, 2);
  123. IP4_ADDR(&netmask, 255, 255, 255, 0);
  124. test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
  125. memset(&counters, 0, sizeof(counters));
  126. /* create and initialize the pcb */
  127. pcb = test_tcp_new_counters_pcb(&counters);
  128. EXPECT_RET(pcb != NULL);
  129. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  130. pcb->mss = TCP_MSS;
  131. /* disable initial congestion window (we don't send a SYN here...) */
  132. pcb->cwnd = pcb->snd_wnd;
  133. /* send data1 */
  134. err = tcp_write(pcb, data1, sizeof(data1), TCP_WRITE_FLAG_COPY);
  135. EXPECT_RET(err == ERR_OK);
  136. err = tcp_output(pcb);
  137. EXPECT_RET(err == ERR_OK);
  138. EXPECT_RET(txcounters.num_tx_calls == 1);
  139. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  140. memset(&txcounters, 0, sizeof(txcounters));
  141. /* "recv" ACK for data1 */
  142. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
  143. EXPECT_RET(p != NULL);
  144. test_tcp_input(p, &netif);
  145. EXPECT_RET(txcounters.num_tx_calls == 0);
  146. EXPECT_RET(pcb->unacked == NULL);
  147. /* send data2 */
  148. err = tcp_write(pcb, data2, sizeof(data2), TCP_WRITE_FLAG_COPY);
  149. EXPECT_RET(err == ERR_OK);
  150. err = tcp_output(pcb);
  151. EXPECT_RET(err == ERR_OK);
  152. EXPECT_RET(txcounters.num_tx_calls == 1);
  153. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  154. memset(&txcounters, 0, sizeof(txcounters));
  155. /* duplicate ACK for data1 (data2 is lost) */
  156. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  157. EXPECT_RET(p != NULL);
  158. test_tcp_input(p, &netif);
  159. EXPECT_RET(txcounters.num_tx_calls == 0);
  160. EXPECT_RET(pcb->dupacks == 1);
  161. /* send data3 */
  162. err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
  163. EXPECT_RET(err == ERR_OK);
  164. err = tcp_output(pcb);
  165. EXPECT_RET(err == ERR_OK);
  166. /* nagle enabled, no tx calls */
  167. EXPECT_RET(txcounters.num_tx_calls == 0);
  168. EXPECT_RET(txcounters.num_tx_bytes == 0);
  169. memset(&txcounters, 0, sizeof(txcounters));
  170. /* 2nd duplicate ACK for data1 (data2 and data3 are lost) */
  171. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  172. EXPECT_RET(p != NULL);
  173. test_tcp_input(p, &netif);
  174. EXPECT_RET(txcounters.num_tx_calls == 0);
  175. EXPECT_RET(pcb->dupacks == 2);
  176. /* queue data4, don't send it (unsent-oversize is != 0) */
  177. err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
  178. EXPECT_RET(err == ERR_OK);
  179. /* 3nd duplicate ACK for data1 (data2 and data3 are lost) -> fast retransmission */
  180. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  181. EXPECT_RET(p != NULL);
  182. test_tcp_input(p, &netif);
  183. /*EXPECT_RET(txcounters.num_tx_calls == 1);*/
  184. EXPECT_RET(pcb->dupacks == 3);
  185. memset(&txcounters, 0, sizeof(txcounters));
  186. /* TODO: check expected data?*/
  187. /* send data5, not output yet */
  188. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  189. EXPECT_RET(err == ERR_OK);
  190. /*err = tcp_output(pcb);
  191. EXPECT_RET(err == ERR_OK);*/
  192. EXPECT_RET(txcounters.num_tx_calls == 0);
  193. EXPECT_RET(txcounters.num_tx_bytes == 0);
  194. memset(&txcounters, 0, sizeof(txcounters));
  195. {
  196. int i = 0;
  197. do
  198. {
  199. err = tcp_write(pcb, data6, TCP_MSS, TCP_WRITE_FLAG_COPY);
  200. i++;
  201. }while(err == ERR_OK);
  202. EXPECT_RET(err != ERR_OK);
  203. }
  204. err = tcp_output(pcb);
  205. EXPECT_RET(err == ERR_OK);
  206. /*EXPECT_RET(txcounters.num_tx_calls == 0);
  207. EXPECT_RET(txcounters.num_tx_bytes == 0);*/
  208. memset(&txcounters, 0, sizeof(txcounters));
  209. /* send even more data */
  210. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  211. EXPECT_RET(err == ERR_OK);
  212. err = tcp_output(pcb);
  213. EXPECT_RET(err == ERR_OK);
  214. /* ...and even more data */
  215. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  216. EXPECT_RET(err == ERR_OK);
  217. err = tcp_output(pcb);
  218. EXPECT_RET(err == ERR_OK);
  219. /* ...and even more data */
  220. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  221. EXPECT_RET(err == ERR_OK);
  222. err = tcp_output(pcb);
  223. EXPECT_RET(err == ERR_OK);
  224. /* ...and even more data */
  225. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  226. EXPECT_RET(err == ERR_OK);
  227. err = tcp_output(pcb);
  228. EXPECT_RET(err == ERR_OK);
  229. /* send ACKs for data2 and data3 */
  230. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 12, TCP_ACK);
  231. EXPECT_RET(p != NULL);
  232. test_tcp_input(p, &netif);
  233. /*EXPECT_RET(txcounters.num_tx_calls == 0);*/
  234. /* ...and even more data */
  235. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  236. EXPECT_RET(err == ERR_OK);
  237. err = tcp_output(pcb);
  238. EXPECT_RET(err == ERR_OK);
  239. /* ...and even more data */
  240. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  241. EXPECT_RET(err == ERR_OK);
  242. err = tcp_output(pcb);
  243. EXPECT_RET(err == ERR_OK);
  244. #if 0
  245. /* create expected segment */
  246. p1 = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  247. EXPECT_RET(p != NULL);
  248. if (p != NULL) {
  249. /* pass the segment to tcp_input */
  250. test_tcp_input(p, &netif);
  251. /* check if counters are as expected */
  252. EXPECT_RET(counters.close_calls == 0);
  253. EXPECT_RET(counters.recv_calls == 1);
  254. EXPECT_RET(counters.recved_bytes == data_len);
  255. EXPECT_RET(counters.err_calls == 0);
  256. }
  257. #endif
  258. /* make sure the pcb is freed */
  259. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  260. tcp_abort(pcb);
  261. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  262. }
  263. END_TEST
  264. static u8_t tx_data[TCP_WND*2];
  265. static void
  266. check_seqnos(struct tcp_seg *segs, int num_expected, u32_t *seqnos_expected)
  267. {
  268. struct tcp_seg *s = segs;
  269. int i;
  270. for (i = 0; i < num_expected; i++, s = s->next) {
  271. EXPECT_RET(s != NULL);
  272. EXPECT(s->tcphdr->seqno == htonl(seqnos_expected[i]));
  273. }
  274. EXPECT(s == NULL);
  275. }
  276. /** Send data with sequence numbers that wrap around the u32_t range.
  277. * Then, provoke fast retransmission by duplicate ACKs and check that all
  278. * segment lists are still properly sorted. */
  279. START_TEST(test_tcp_fast_rexmit_wraparound)
  280. {
  281. struct netif netif;
  282. struct test_tcp_txcounters txcounters;
  283. struct test_tcp_counters counters;
  284. struct tcp_pcb* pcb;
  285. struct pbuf* p;
  286. ip_addr_t remote_ip, local_ip, netmask;
  287. u16_t remote_port = 0x100, local_port = 0x101;
  288. err_t err;
  289. #define SEQNO1 (0xFFFFFF00 - TCP_MSS)
  290. #define ISS 6510
  291. u16_t i, sent_total = 0;
  292. u32_t seqnos[] = {
  293. SEQNO1,
  294. SEQNO1 + (1 * TCP_MSS),
  295. SEQNO1 + (2 * TCP_MSS),
  296. SEQNO1 + (3 * TCP_MSS),
  297. SEQNO1 + (4 * TCP_MSS),
  298. SEQNO1 + (5 * TCP_MSS)};
  299. LWIP_UNUSED_ARG(_i);
  300. for (i = 0; i < sizeof(tx_data); i++) {
  301. tx_data[i] = (u8_t)i;
  302. }
  303. /* initialize local vars */
  304. IP4_ADDR(&local_ip, 192, 168, 1, 1);
  305. IP4_ADDR(&remote_ip, 192, 168, 1, 2);
  306. IP4_ADDR(&netmask, 255, 255, 255, 0);
  307. test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
  308. memset(&counters, 0, sizeof(counters));
  309. /* create and initialize the pcb */
  310. tcp_ticks = SEQNO1 - ISS;
  311. pcb = test_tcp_new_counters_pcb(&counters);
  312. EXPECT_RET(pcb != NULL);
  313. EXPECT(pcb->lastack == SEQNO1);
  314. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  315. pcb->mss = TCP_MSS;
  316. /* disable initial congestion window (we don't send a SYN here...) */
  317. pcb->cwnd = 2*TCP_MSS;
  318. /* send 6 mss-sized segments */
  319. for (i = 0; i < 6; i++) {
  320. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  321. EXPECT_RET(err == ERR_OK);
  322. sent_total += TCP_MSS;
  323. }
  324. check_seqnos(pcb->unsent, 6, seqnos);
  325. EXPECT(pcb->unacked == NULL);
  326. err = tcp_output(pcb);
  327. EXPECT(txcounters.num_tx_calls == 2);
  328. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  329. memset(&txcounters, 0, sizeof(txcounters));
  330. check_seqnos(pcb->unacked, 2, seqnos);
  331. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  332. /* ACK the first segment */
  333. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  334. test_tcp_input(p, &netif);
  335. /* ensure this didn't trigger a retransmission */
  336. EXPECT(txcounters.num_tx_calls == 1);
  337. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  338. memset(&txcounters, 0, sizeof(txcounters));
  339. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  340. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  341. /* 3 dupacks */
  342. EXPECT(pcb->dupacks == 0);
  343. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  344. test_tcp_input(p, &netif);
  345. EXPECT(txcounters.num_tx_calls == 0);
  346. EXPECT(pcb->dupacks == 1);
  347. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  348. test_tcp_input(p, &netif);
  349. EXPECT(txcounters.num_tx_calls == 0);
  350. EXPECT(pcb->dupacks == 2);
  351. /* 3rd dupack -> fast rexmit */
  352. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  353. test_tcp_input(p, &netif);
  354. EXPECT(pcb->dupacks == 3);
  355. EXPECT(txcounters.num_tx_calls == 4);
  356. memset(&txcounters, 0, sizeof(txcounters));
  357. EXPECT(pcb->unsent == NULL);
  358. check_seqnos(pcb->unacked, 5, &seqnos[1]);
  359. /* make sure the pcb is freed */
  360. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  361. tcp_abort(pcb);
  362. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  363. }
  364. END_TEST
  365. /** Send data with sequence numbers that wrap around the u32_t range.
  366. * Then, provoke RTO retransmission and check that all
  367. * segment lists are still properly sorted. */
  368. START_TEST(test_tcp_rto_rexmit_wraparound)
  369. {
  370. struct netif netif;
  371. struct test_tcp_txcounters txcounters;
  372. struct test_tcp_counters counters;
  373. struct tcp_pcb* pcb;
  374. ip_addr_t remote_ip, local_ip, netmask;
  375. u16_t remote_port = 0x100, local_port = 0x101;
  376. err_t err;
  377. #define SEQNO1 (0xFFFFFF00 - TCP_MSS)
  378. #define ISS 6510
  379. u16_t i, sent_total = 0;
  380. u32_t seqnos[] = {
  381. SEQNO1,
  382. SEQNO1 + (1 * TCP_MSS),
  383. SEQNO1 + (2 * TCP_MSS),
  384. SEQNO1 + (3 * TCP_MSS),
  385. SEQNO1 + (4 * TCP_MSS),
  386. SEQNO1 + (5 * TCP_MSS)};
  387. LWIP_UNUSED_ARG(_i);
  388. for (i = 0; i < sizeof(tx_data); i++) {
  389. tx_data[i] = (u8_t)i;
  390. }
  391. /* initialize local vars */
  392. IP4_ADDR(&local_ip, 192, 168, 1, 1);
  393. IP4_ADDR(&remote_ip, 192, 168, 1, 2);
  394. IP4_ADDR(&netmask, 255, 255, 255, 0);
  395. test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
  396. memset(&counters, 0, sizeof(counters));
  397. /* create and initialize the pcb */
  398. tcp_ticks = 0;
  399. tcp_ticks = 0 - tcp_next_iss();
  400. tcp_ticks = SEQNO1 - tcp_next_iss();
  401. pcb = test_tcp_new_counters_pcb(&counters);
  402. EXPECT_RET(pcb != NULL);
  403. EXPECT(pcb->lastack == SEQNO1);
  404. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  405. pcb->mss = TCP_MSS;
  406. /* disable initial congestion window (we don't send a SYN here...) */
  407. pcb->cwnd = 2*TCP_MSS;
  408. /* send 6 mss-sized segments */
  409. for (i = 0; i < 6; i++) {
  410. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  411. EXPECT_RET(err == ERR_OK);
  412. sent_total += TCP_MSS;
  413. }
  414. check_seqnos(pcb->unsent, 6, seqnos);
  415. EXPECT(pcb->unacked == NULL);
  416. err = tcp_output(pcb);
  417. EXPECT(txcounters.num_tx_calls == 2);
  418. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  419. memset(&txcounters, 0, sizeof(txcounters));
  420. check_seqnos(pcb->unacked, 2, seqnos);
  421. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  422. /* call the tcp timer some times */
  423. for (i = 0; i < 10; i++) {
  424. test_tcp_tmr();
  425. EXPECT(txcounters.num_tx_calls == 0);
  426. }
  427. /* 11th call to tcp_tmr: RTO rexmit fires */
  428. test_tcp_tmr();
  429. EXPECT(txcounters.num_tx_calls == 1);
  430. check_seqnos(pcb->unacked, 1, seqnos);
  431. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  432. /* fake greater cwnd */
  433. pcb->cwnd = pcb->snd_wnd;
  434. /* send more data */
  435. err = tcp_output(pcb);
  436. EXPECT(err == ERR_OK);
  437. /* check queues are sorted */
  438. EXPECT(pcb->unsent == NULL);
  439. check_seqnos(pcb->unacked, 6, seqnos);
  440. /* make sure the pcb is freed */
  441. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  442. tcp_abort(pcb);
  443. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  444. }
  445. END_TEST
  446. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  447. * At the end, send more data. */
  448. static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
  449. {
  450. struct netif netif;
  451. struct test_tcp_txcounters txcounters;
  452. struct test_tcp_counters counters;
  453. struct tcp_pcb* pcb;
  454. struct pbuf *p;
  455. ip_addr_t remote_ip, local_ip, netmask;
  456. u16_t remote_port = 0x100, local_port = 0x101;
  457. err_t err;
  458. u16_t sent_total, i;
  459. u8_t expected = 0xFE;
  460. for (i = 0; i < sizeof(tx_data); i++) {
  461. u8_t d = (u8_t)i;
  462. if (d == 0xFE) {
  463. d = 0xF0;
  464. }
  465. tx_data[i] = d;
  466. }
  467. if (zero_window_probe_from_unsent) {
  468. tx_data[TCP_WND] = expected;
  469. } else {
  470. tx_data[0] = expected;
  471. }
  472. /* initialize local vars */
  473. IP4_ADDR(&local_ip, 192, 168, 1, 1);
  474. IP4_ADDR(&remote_ip, 192, 168, 1, 2);
  475. IP4_ADDR(&netmask, 255, 255, 255, 0);
  476. test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
  477. memset(&counters, 0, sizeof(counters));
  478. memset(&txcounters, 0, sizeof(txcounters));
  479. /* create and initialize the pcb */
  480. pcb = test_tcp_new_counters_pcb(&counters);
  481. EXPECT_RET(pcb != NULL);
  482. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  483. pcb->mss = TCP_MSS;
  484. /* disable initial congestion window (we don't send a SYN here...) */
  485. pcb->cwnd = pcb->snd_wnd;
  486. /* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
  487. sent_total = 0;
  488. if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
  489. u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
  490. err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
  491. EXPECT_RET(err == ERR_OK);
  492. err = tcp_output(pcb);
  493. EXPECT_RET(err == ERR_OK);
  494. EXPECT(txcounters.num_tx_calls == 1);
  495. EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
  496. memset(&txcounters, 0, sizeof(txcounters));
  497. sent_total += initial_data_len;
  498. }
  499. for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
  500. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  501. EXPECT_RET(err == ERR_OK);
  502. err = tcp_output(pcb);
  503. EXPECT_RET(err == ERR_OK);
  504. EXPECT(txcounters.num_tx_calls == 1);
  505. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  506. memset(&txcounters, 0, sizeof(txcounters));
  507. }
  508. EXPECT(sent_total == (TCP_WND - TCP_MSS));
  509. /* now ACK the packet before the first */
  510. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  511. test_tcp_input(p, &netif);
  512. /* ensure this didn't trigger a retransmission */
  513. EXPECT(txcounters.num_tx_calls == 0);
  514. EXPECT(txcounters.num_tx_bytes == 0);
  515. EXPECT(pcb->persist_backoff == 0);
  516. /* send the last packet, now a complete window has been sent */
  517. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  518. sent_total += TCP_MSS;
  519. EXPECT_RET(err == ERR_OK);
  520. err = tcp_output(pcb);
  521. EXPECT_RET(err == ERR_OK);
  522. EXPECT(txcounters.num_tx_calls == 1);
  523. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  524. memset(&txcounters, 0, sizeof(txcounters));
  525. EXPECT(pcb->persist_backoff == 0);
  526. if (zero_window_probe_from_unsent) {
  527. /* ACK all data but close the TX window */
  528. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
  529. test_tcp_input(p, &netif);
  530. /* ensure this didn't trigger any transmission */
  531. EXPECT(txcounters.num_tx_calls == 0);
  532. EXPECT(txcounters.num_tx_bytes == 0);
  533. EXPECT(pcb->persist_backoff == 1);
  534. }
  535. /* send one byte more (out of window) -> persist timer starts */
  536. err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
  537. EXPECT_RET(err == ERR_OK);
  538. err = tcp_output(pcb);
  539. EXPECT_RET(err == ERR_OK);
  540. EXPECT(txcounters.num_tx_calls == 0);
  541. EXPECT(txcounters.num_tx_bytes == 0);
  542. memset(&txcounters, 0, sizeof(txcounters));
  543. if (!zero_window_probe_from_unsent) {
  544. /* no persist timer unless a zero window announcement has been received */
  545. EXPECT(pcb->persist_backoff == 0);
  546. } else {
  547. EXPECT(pcb->persist_backoff == 1);
  548. /* call tcp_timer some more times to let persist timer count up */
  549. for (i = 0; i < 4; i++) {
  550. test_tcp_tmr();
  551. EXPECT(txcounters.num_tx_calls == 0);
  552. EXPECT(txcounters.num_tx_bytes == 0);
  553. }
  554. /* this should trigger the zero-window-probe */
  555. txcounters.copy_tx_packets = 1;
  556. test_tcp_tmr();
  557. txcounters.copy_tx_packets = 0;
  558. EXPECT(txcounters.num_tx_calls == 1);
  559. EXPECT(txcounters.num_tx_bytes == 1 + 40U);
  560. EXPECT(txcounters.tx_packets != NULL);
  561. if (txcounters.tx_packets != NULL) {
  562. u8_t sent;
  563. u16_t ret;
  564. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
  565. EXPECT(ret == 1);
  566. EXPECT(sent == expected);
  567. }
  568. if (txcounters.tx_packets != NULL) {
  569. pbuf_free(txcounters.tx_packets);
  570. txcounters.tx_packets = NULL;
  571. }
  572. }
  573. /* make sure the pcb is freed */
  574. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  575. tcp_abort(pcb);
  576. EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  577. }
  578. START_TEST(test_tcp_tx_full_window_lost_from_unsent)
  579. {
  580. LWIP_UNUSED_ARG(_i);
  581. test_tcp_tx_full_window_lost(1);
  582. }
  583. END_TEST
  584. START_TEST(test_tcp_tx_full_window_lost_from_unacked)
  585. {
  586. LWIP_UNUSED_ARG(_i);
  587. test_tcp_tx_full_window_lost(0);
  588. }
  589. END_TEST
  590. /** Create the suite including all tests for this module */
  591. Suite *
  592. tcp_suite(void)
  593. {
  594. TFun tests[] = {
  595. test_tcp_new_abort,
  596. test_tcp_recv_inseq,
  597. test_tcp_fast_retx_recover,
  598. test_tcp_fast_rexmit_wraparound,
  599. test_tcp_rto_rexmit_wraparound,
  600. test_tcp_tx_full_window_lost_from_unacked,
  601. test_tcp_tx_full_window_lost_from_unsent
  602. };
  603. return create_suite("TCP", tests, sizeof(tests)/sizeof(TFun), tcp_setup, tcp_teardown);
  604. }