test_tcp.c 23 KB

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