test_tcp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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() - 6510);
  32. tcp_next_iss();
  33. tcp_ticks = 0;
  34. test_tcp_timer = 0;
  35. tcp_remove_all();
  36. }
  37. static void
  38. tcp_teardown(void)
  39. {
  40. tcp_remove_all();
  41. netif_list = NULL;
  42. netif_default = NULL;
  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. EXPECT(pcb->lastack == SEQNO1);
  375. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  376. pcb->mss = TCP_MSS;
  377. /* disable initial congestion window (we don't send a SYN here...) */
  378. pcb->cwnd = 2*TCP_MSS;
  379. /* send 6 mss-sized segments */
  380. for (i = 0; i < 6; i++) {
  381. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  382. EXPECT_RET(err == ERR_OK);
  383. sent_total += TCP_MSS;
  384. }
  385. check_seqnos(pcb->unsent, 6, seqnos);
  386. EXPECT(pcb->unacked == NULL);
  387. err = tcp_output(pcb);
  388. EXPECT(txcounters.num_tx_calls == 2);
  389. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  390. memset(&txcounters, 0, sizeof(txcounters));
  391. check_seqnos(pcb->unacked, 2, seqnos);
  392. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  393. /* ACK the first segment */
  394. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  395. test_tcp_input(p, &netif);
  396. /* ensure this didn't trigger a retransmission */
  397. EXPECT(txcounters.num_tx_calls == 1);
  398. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  399. memset(&txcounters, 0, sizeof(txcounters));
  400. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  401. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  402. /* 3 dupacks */
  403. EXPECT(pcb->dupacks == 0);
  404. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  405. test_tcp_input(p, &netif);
  406. EXPECT(txcounters.num_tx_calls == 0);
  407. EXPECT(pcb->dupacks == 1);
  408. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  409. test_tcp_input(p, &netif);
  410. EXPECT(txcounters.num_tx_calls == 0);
  411. EXPECT(pcb->dupacks == 2);
  412. /* 3rd dupack -> fast rexmit */
  413. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  414. test_tcp_input(p, &netif);
  415. EXPECT(pcb->dupacks == 3);
  416. EXPECT(txcounters.num_tx_calls == 4);
  417. memset(&txcounters, 0, sizeof(txcounters));
  418. EXPECT(pcb->unsent == NULL);
  419. check_seqnos(pcb->unacked, 5, &seqnos[1]);
  420. /* make sure the pcb is freed */
  421. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  422. tcp_abort(pcb);
  423. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  424. }
  425. END_TEST
  426. /** Send data with sequence numbers that wrap around the u32_t range.
  427. * Then, provoke RTO retransmission and check that all
  428. * segment lists are still properly sorted. */
  429. START_TEST(test_tcp_rto_rexmit_wraparound)
  430. {
  431. struct netif netif;
  432. struct test_tcp_txcounters txcounters;
  433. struct test_tcp_counters counters;
  434. struct tcp_pcb* pcb;
  435. ip_addr_t remote_ip, local_ip, netmask;
  436. u16_t remote_port = 0x100, local_port = 0x101;
  437. err_t err;
  438. #define SEQNO1 (0xFFFFFF00 - TCP_MSS)
  439. #define ISS 6510
  440. u16_t i, sent_total = 0;
  441. u32_t seqnos[] = {
  442. SEQNO1,
  443. SEQNO1 + (1 * TCP_MSS),
  444. SEQNO1 + (2 * TCP_MSS),
  445. SEQNO1 + (3 * TCP_MSS),
  446. SEQNO1 + (4 * TCP_MSS),
  447. SEQNO1 + (5 * TCP_MSS)};
  448. LWIP_UNUSED_ARG(_i);
  449. for (i = 0; i < sizeof(tx_data); i++) {
  450. tx_data[i] = (u8_t)i;
  451. }
  452. /* initialize local vars */
  453. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  454. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  455. IP_ADDR4(&netmask, 255, 255, 255, 0);
  456. test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
  457. memset(&counters, 0, sizeof(counters));
  458. /* create and initialize the pcb */
  459. tcp_ticks = 0;
  460. tcp_ticks = 0 - tcp_next_iss();
  461. tcp_ticks = SEQNO1 - tcp_next_iss();
  462. pcb = test_tcp_new_counters_pcb(&counters);
  463. EXPECT_RET(pcb != NULL);
  464. EXPECT(pcb->lastack == SEQNO1);
  465. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  466. pcb->mss = TCP_MSS;
  467. /* disable initial congestion window (we don't send a SYN here...) */
  468. pcb->cwnd = 2*TCP_MSS;
  469. /* send 6 mss-sized segments */
  470. for (i = 0; i < 6; i++) {
  471. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  472. EXPECT_RET(err == ERR_OK);
  473. sent_total += TCP_MSS;
  474. }
  475. check_seqnos(pcb->unsent, 6, seqnos);
  476. EXPECT(pcb->unacked == NULL);
  477. err = tcp_output(pcb);
  478. EXPECT(txcounters.num_tx_calls == 2);
  479. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  480. memset(&txcounters, 0, sizeof(txcounters));
  481. check_seqnos(pcb->unacked, 2, seqnos);
  482. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  483. /* call the tcp timer some times */
  484. for (i = 0; i < 10; i++) {
  485. test_tcp_tmr();
  486. EXPECT(txcounters.num_tx_calls == 0);
  487. }
  488. /* 11th call to tcp_tmr: RTO rexmit fires */
  489. test_tcp_tmr();
  490. EXPECT(txcounters.num_tx_calls == 1);
  491. check_seqnos(pcb->unacked, 1, seqnos);
  492. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  493. /* fake greater cwnd */
  494. pcb->cwnd = pcb->snd_wnd;
  495. /* send more data */
  496. err = tcp_output(pcb);
  497. EXPECT(err == ERR_OK);
  498. /* check queues are sorted */
  499. EXPECT(pcb->unsent == NULL);
  500. check_seqnos(pcb->unacked, 6, seqnos);
  501. /* make sure the pcb is freed */
  502. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  503. tcp_abort(pcb);
  504. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  505. }
  506. END_TEST
  507. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  508. * At the end, send more data. */
  509. static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
  510. {
  511. struct netif netif;
  512. struct test_tcp_txcounters txcounters;
  513. struct test_tcp_counters counters;
  514. struct tcp_pcb* pcb;
  515. struct pbuf *p;
  516. ip_addr_t remote_ip, local_ip, netmask;
  517. u16_t remote_port = 0x100, local_port = 0x101;
  518. err_t err;
  519. u16_t sent_total, i;
  520. u8_t expected = 0xFE;
  521. for (i = 0; i < sizeof(tx_data); i++) {
  522. u8_t d = (u8_t)i;
  523. if (d == 0xFE) {
  524. d = 0xF0;
  525. }
  526. tx_data[i] = d;
  527. }
  528. if (zero_window_probe_from_unsent) {
  529. tx_data[TCP_WND] = expected;
  530. } else {
  531. tx_data[0] = expected;
  532. }
  533. /* initialize local vars */
  534. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  535. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  536. IP_ADDR4(&netmask, 255, 255, 255, 0);
  537. test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
  538. memset(&counters, 0, sizeof(counters));
  539. memset(&txcounters, 0, sizeof(txcounters));
  540. /* create and initialize the pcb */
  541. pcb = test_tcp_new_counters_pcb(&counters);
  542. EXPECT_RET(pcb != NULL);
  543. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  544. pcb->mss = TCP_MSS;
  545. /* disable initial congestion window (we don't send a SYN here...) */
  546. pcb->cwnd = pcb->snd_wnd;
  547. /* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
  548. sent_total = 0;
  549. if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
  550. u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
  551. err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
  552. EXPECT_RET(err == ERR_OK);
  553. err = tcp_output(pcb);
  554. EXPECT_RET(err == ERR_OK);
  555. EXPECT(txcounters.num_tx_calls == 1);
  556. EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
  557. memset(&txcounters, 0, sizeof(txcounters));
  558. sent_total += initial_data_len;
  559. }
  560. for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
  561. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  562. EXPECT_RET(err == ERR_OK);
  563. err = tcp_output(pcb);
  564. EXPECT_RET(err == ERR_OK);
  565. EXPECT(txcounters.num_tx_calls == 1);
  566. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  567. memset(&txcounters, 0, sizeof(txcounters));
  568. }
  569. EXPECT(sent_total == (TCP_WND - TCP_MSS));
  570. /* now ACK the packet before the first */
  571. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  572. test_tcp_input(p, &netif);
  573. /* ensure this didn't trigger a retransmission */
  574. EXPECT(txcounters.num_tx_calls == 0);
  575. EXPECT(txcounters.num_tx_bytes == 0);
  576. EXPECT(pcb->persist_backoff == 0);
  577. /* send the last packet, now a complete window has been sent */
  578. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  579. sent_total += TCP_MSS;
  580. EXPECT_RET(err == ERR_OK);
  581. err = tcp_output(pcb);
  582. EXPECT_RET(err == ERR_OK);
  583. EXPECT(txcounters.num_tx_calls == 1);
  584. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  585. memset(&txcounters, 0, sizeof(txcounters));
  586. EXPECT(pcb->persist_backoff == 0);
  587. if (zero_window_probe_from_unsent) {
  588. /* ACK all data but close the TX window */
  589. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
  590. test_tcp_input(p, &netif);
  591. /* ensure this didn't trigger any transmission */
  592. EXPECT(txcounters.num_tx_calls == 0);
  593. EXPECT(txcounters.num_tx_bytes == 0);
  594. EXPECT(pcb->persist_backoff == 1);
  595. }
  596. /* send one byte more (out of window) -> persist timer starts */
  597. err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
  598. EXPECT_RET(err == ERR_OK);
  599. err = tcp_output(pcb);
  600. EXPECT_RET(err == ERR_OK);
  601. EXPECT(txcounters.num_tx_calls == 0);
  602. EXPECT(txcounters.num_tx_bytes == 0);
  603. memset(&txcounters, 0, sizeof(txcounters));
  604. if (!zero_window_probe_from_unsent) {
  605. /* no persist timer unless a zero window announcement has been received */
  606. EXPECT(pcb->persist_backoff == 0);
  607. } else {
  608. EXPECT(pcb->persist_backoff == 1);
  609. /* call tcp_timer some more times to let persist timer count up */
  610. for (i = 0; i < 4; i++) {
  611. test_tcp_tmr();
  612. EXPECT(txcounters.num_tx_calls == 0);
  613. EXPECT(txcounters.num_tx_bytes == 0);
  614. }
  615. /* this should trigger the zero-window-probe */
  616. txcounters.copy_tx_packets = 1;
  617. test_tcp_tmr();
  618. txcounters.copy_tx_packets = 0;
  619. EXPECT(txcounters.num_tx_calls == 1);
  620. EXPECT(txcounters.num_tx_bytes == 1 + 40U);
  621. EXPECT(txcounters.tx_packets != NULL);
  622. if (txcounters.tx_packets != NULL) {
  623. u8_t sent;
  624. u16_t ret;
  625. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
  626. EXPECT(ret == 1);
  627. EXPECT(sent == expected);
  628. }
  629. if (txcounters.tx_packets != NULL) {
  630. pbuf_free(txcounters.tx_packets);
  631. txcounters.tx_packets = NULL;
  632. }
  633. }
  634. /* make sure the pcb is freed */
  635. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  636. tcp_abort(pcb);
  637. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  638. }
  639. START_TEST(test_tcp_tx_full_window_lost_from_unsent)
  640. {
  641. LWIP_UNUSED_ARG(_i);
  642. test_tcp_tx_full_window_lost(1);
  643. }
  644. END_TEST
  645. START_TEST(test_tcp_tx_full_window_lost_from_unacked)
  646. {
  647. LWIP_UNUSED_ARG(_i);
  648. test_tcp_tx_full_window_lost(0);
  649. }
  650. END_TEST
  651. /** Create the suite including all tests for this module */
  652. Suite *
  653. tcp_suite(void)
  654. {
  655. testfunc tests[] = {
  656. TESTFUNC(test_tcp_new_abort),
  657. TESTFUNC(test_tcp_recv_inseq),
  658. TESTFUNC(test_tcp_malformed_header),
  659. TESTFUNC(test_tcp_fast_retx_recover),
  660. TESTFUNC(test_tcp_fast_rexmit_wraparound),
  661. TESTFUNC(test_tcp_rto_rexmit_wraparound),
  662. TESTFUNC(test_tcp_tx_full_window_lost_from_unacked),
  663. TESTFUNC(test_tcp_tx_full_window_lost_from_unsent)
  664. };
  665. return create_suite("TCP", tests, sizeof(tests)/sizeof(testfunc), tcp_setup, tcp_teardown);
  666. }