test_tcp.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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. struct tcp_pcb dummy_pcb; /* we need this for tcp_next_iss() only */
  42. old_netif_list = netif_list;
  43. old_netif_default = netif_default;
  44. netif_list = NULL;
  45. netif_default = NULL;
  46. /* reset iss to default (6510) */
  47. tcp_ticks = 0;
  48. tcp_ticks = 0 - (tcp_next_iss(&dummy_pcb) - 6510);
  49. tcp_next_iss(&dummy_pcb);
  50. tcp_ticks = 0;
  51. test_tcp_timer = 0;
  52. tcp_remove_all();
  53. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  54. }
  55. static void
  56. tcp_teardown(void)
  57. {
  58. netif_list = NULL;
  59. netif_default = NULL;
  60. tcp_remove_all();
  61. /* restore netif_list for next tests (e.g. loopif) */
  62. netif_list = old_netif_list;
  63. netif_default = old_netif_default;
  64. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  65. }
  66. /* Test functions */
  67. /** Call tcp_new() and tcp_abort() and test memp stats */
  68. START_TEST(test_tcp_new_abort)
  69. {
  70. struct tcp_pcb* pcb;
  71. LWIP_UNUSED_ARG(_i);
  72. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  73. pcb = tcp_new();
  74. fail_unless(pcb != NULL);
  75. if (pcb != NULL) {
  76. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  77. tcp_abort(pcb);
  78. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  79. }
  80. }
  81. END_TEST
  82. /** Call tcp_new() and tcp_abort() and test memp stats */
  83. START_TEST(test_tcp_listen_passive_open)
  84. {
  85. struct tcp_pcb *pcb, *pcbl;
  86. struct tcp_pcb_listen *lpcb;
  87. struct netif netif;
  88. struct test_tcp_txcounters txcounters;
  89. struct test_tcp_counters counters;
  90. struct pbuf *p;
  91. ip_addr_t src_addr;
  92. err_t err;
  93. LWIP_UNUSED_ARG(_i);
  94. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  95. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  96. /* initialize counter struct */
  97. memset(&counters, 0, sizeof(counters));
  98. pcb = tcp_new();
  99. EXPECT_RET(pcb != NULL);
  100. err = tcp_bind(pcb, &netif.ip_addr, 1234);
  101. EXPECT(err == ERR_OK);
  102. pcbl = tcp_listen(pcb);
  103. EXPECT_RET(pcbl != NULL);
  104. EXPECT_RET(pcbl != pcb);
  105. lpcb = (struct tcp_pcb_listen *)pcbl;
  106. ip_addr_set_ip4_u32_val(src_addr, lwip_htonl(lwip_ntohl(ip_addr_get_ip4_u32(&lpcb->local_ip)) + 1));
  107. /* check correct syn packet */
  108. p = tcp_create_segment(&src_addr, &lpcb->local_ip, 12345,
  109. lpcb->local_port, NULL, 0, 12345, 54321, TCP_SYN);
  110. EXPECT(p != NULL);
  111. if (p != NULL) {
  112. /* pass the segment to tcp_input */
  113. test_tcp_input(p, &netif);
  114. /* check if counters are as expected */
  115. EXPECT(txcounters.num_tx_calls == 1);
  116. }
  117. /* check syn packet with short length */
  118. p = tcp_create_segment(&src_addr, &lpcb->local_ip, 12345,
  119. lpcb->local_port, NULL, 0, 12345, 54321, TCP_SYN);
  120. EXPECT(p != NULL);
  121. EXPECT(p->next == NULL);
  122. if ((p != NULL) && (p->next == NULL)) {
  123. p->len -= 2;
  124. p->tot_len -= 2;
  125. /* pass the segment to tcp_input */
  126. test_tcp_input(p, &netif);
  127. /* check if counters are as expected */
  128. EXPECT(txcounters.num_tx_calls == 1);
  129. }
  130. tcp_close(pcbl);
  131. }
  132. END_TEST
  133. /** Create an ESTABLISHED pcb and check if receive callback is called */
  134. START_TEST(test_tcp_recv_inseq)
  135. {
  136. struct test_tcp_counters counters;
  137. struct tcp_pcb* pcb;
  138. struct pbuf* p;
  139. char data[] = {1, 2, 3, 4};
  140. u16_t data_len;
  141. struct netif netif;
  142. struct test_tcp_txcounters txcounters;
  143. LWIP_UNUSED_ARG(_i);
  144. /* initialize local vars */
  145. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  146. data_len = sizeof(data);
  147. /* initialize counter struct */
  148. memset(&counters, 0, sizeof(counters));
  149. counters.expected_data_len = data_len;
  150. counters.expected_data = data;
  151. /* create and initialize the pcb */
  152. pcb = test_tcp_new_counters_pcb(&counters);
  153. EXPECT_RET(pcb != NULL);
  154. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  155. /* create a segment */
  156. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  157. EXPECT(p != NULL);
  158. if (p != NULL) {
  159. /* pass the segment to tcp_input */
  160. test_tcp_input(p, &netif);
  161. /* check if counters are as expected */
  162. EXPECT(counters.close_calls == 0);
  163. EXPECT(counters.recv_calls == 1);
  164. EXPECT(counters.recved_bytes == data_len);
  165. EXPECT(counters.err_calls == 0);
  166. }
  167. /* make sure the pcb is freed */
  168. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  169. tcp_abort(pcb);
  170. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  171. }
  172. END_TEST
  173. /** Create an ESTABLISHED pcb and check if receive callback is called if a segment
  174. * overlapping rcv_nxt is received */
  175. START_TEST(test_tcp_recv_inseq_trim)
  176. {
  177. struct test_tcp_counters counters;
  178. struct tcp_pcb* pcb;
  179. struct pbuf* p;
  180. char data[PBUF_POOL_BUFSIZE*2];
  181. u16_t data_len;
  182. struct netif netif;
  183. struct test_tcp_txcounters txcounters;
  184. const u32_t new_data_len = 40;
  185. LWIP_UNUSED_ARG(_i);
  186. /* initialize local vars */
  187. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  188. data_len = sizeof(data);
  189. memset(data, 0, sizeof(data));
  190. /* initialize counter struct */
  191. memset(&counters, 0, sizeof(counters));
  192. counters.expected_data_len = data_len;
  193. counters.expected_data = data;
  194. /* create and initialize the pcb */
  195. pcb = test_tcp_new_counters_pcb(&counters);
  196. EXPECT_RET(pcb != NULL);
  197. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  198. /* create a segment (with an overlapping/old seqno so that the new data begins in the 2nd pbuf) */
  199. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, (u32_t)(0-(data_len-new_data_len)), 0, 0);
  200. EXPECT(p != NULL);
  201. if (p != NULL) {
  202. EXPECT(p->next != NULL);
  203. if (p->next != NULL) {
  204. EXPECT(p->next->next != NULL);
  205. }
  206. }
  207. if ((p != NULL) && (p->next != NULL) && (p->next->next != NULL)) {
  208. /* pass the segment to tcp_input */
  209. test_tcp_input(p, &netif);
  210. /* check if counters are as expected */
  211. EXPECT(counters.close_calls == 0);
  212. EXPECT(counters.recv_calls == 1);
  213. EXPECT(counters.recved_bytes == new_data_len);
  214. EXPECT(counters.err_calls == 0);
  215. }
  216. /* make sure the pcb is freed */
  217. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  218. tcp_abort(pcb);
  219. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  220. }
  221. END_TEST
  222. static err_t test_tcp_recv_expect1byte(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
  223. static err_t
  224. test_tcp_recv_expectclose(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
  225. {
  226. EXPECT_RETX(pcb != NULL, ERR_OK);
  227. EXPECT_RETX(err == ERR_OK, ERR_OK);
  228. LWIP_UNUSED_ARG(arg);
  229. if (p != NULL) {
  230. fail();
  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. START_TEST(test_tcp_active_abort)
  289. {
  290. struct test_tcp_counters counters;
  291. struct tcp_pcb* pcb;
  292. char data = 0x0f;
  293. struct netif netif;
  294. struct test_tcp_txcounters txcounters;
  295. LWIP_UNUSED_ARG(_i);
  296. memset(&txcounters, 0, sizeof(txcounters));
  297. /* initialize local vars */
  298. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  299. /* initialize counter struct */
  300. memset(&counters, 0, sizeof(counters));
  301. counters.expected_data_len = 1;
  302. counters.expected_data = &data;
  303. /* create and initialize the pcb */
  304. pcb = test_tcp_new_counters_pcb(&counters);
  305. EXPECT_RET(pcb != NULL);
  306. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  307. /* abort the pcb */
  308. EXPECT_RET(txcounters.num_tx_calls == 0);
  309. txcounters.copy_tx_packets = 1;
  310. tcp_abort(pcb);
  311. txcounters.copy_tx_packets = 0;
  312. EXPECT(txcounters.num_tx_calls == 1);
  313. EXPECT(txcounters.num_tx_bytes == 40U);
  314. EXPECT(txcounters.tx_packets != NULL);
  315. if (txcounters.tx_packets != NULL) {
  316. u16_t ret;
  317. struct tcp_hdr tcphdr;
  318. ret = pbuf_copy_partial(txcounters.tx_packets, &tcphdr, 20, 20);
  319. EXPECT(ret == 20);
  320. EXPECT(tcphdr.dest == PP_HTONS(TEST_REMOTE_PORT));
  321. EXPECT(tcphdr.src == PP_HTONS(TEST_LOCAL_PORT));
  322. pbuf_free(txcounters.tx_packets);
  323. txcounters.tx_packets = NULL;
  324. }
  325. /* don't free the pcb here (part of the test!) */
  326. }
  327. END_TEST
  328. /** Check that we handle malformed tcp headers, and discard the pbuf(s) */
  329. START_TEST(test_tcp_malformed_header)
  330. {
  331. struct test_tcp_counters counters;
  332. struct tcp_pcb* pcb;
  333. struct pbuf* p;
  334. char data[] = {1, 2, 3, 4};
  335. u16_t data_len, chksum;
  336. struct netif netif;
  337. struct test_tcp_txcounters txcounters;
  338. struct tcp_hdr *hdr;
  339. LWIP_UNUSED_ARG(_i);
  340. /* initialize local vars */
  341. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  342. data_len = sizeof(data);
  343. /* initialize counter struct */
  344. memset(&counters, 0, sizeof(counters));
  345. counters.expected_data_len = data_len;
  346. counters.expected_data = data;
  347. /* create and initialize the pcb */
  348. pcb = test_tcp_new_counters_pcb(&counters);
  349. EXPECT_RET(pcb != NULL);
  350. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  351. /* create a segment */
  352. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  353. pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
  354. hdr = (struct tcp_hdr *)p->payload;
  355. TCPH_HDRLEN_FLAGS_SET(hdr, 15, 0x3d1);
  356. hdr->chksum = 0;
  357. chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
  358. &test_remote_ip, &test_local_ip);
  359. hdr->chksum = chksum;
  360. pbuf_header(p, sizeof(struct ip_hdr));
  361. EXPECT(p != NULL);
  362. EXPECT(p->next == NULL);
  363. if (p != NULL) {
  364. /* pass the segment to tcp_input */
  365. test_tcp_input(p, &netif);
  366. /* check if counters are as expected */
  367. EXPECT(counters.close_calls == 0);
  368. EXPECT(counters.recv_calls == 0);
  369. EXPECT(counters.recved_bytes == 0);
  370. EXPECT(counters.err_calls == 0);
  371. }
  372. /* make sure the pcb is freed */
  373. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  374. tcp_abort(pcb);
  375. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  376. }
  377. END_TEST
  378. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  379. * At the end, send more data. */
  380. START_TEST(test_tcp_fast_retx_recover)
  381. {
  382. struct netif netif;
  383. struct test_tcp_txcounters txcounters;
  384. struct test_tcp_counters counters;
  385. struct tcp_pcb* pcb;
  386. struct pbuf* p;
  387. char data1[] = { 1, 2, 3, 4};
  388. char data2[] = { 5, 6, 7, 8};
  389. char data3[] = { 9, 10, 11, 12};
  390. char data4[] = {13, 14, 15, 16};
  391. char data5[] = {17, 18, 19, 20};
  392. char data6[TCP_MSS] = {21, 22, 23, 24};
  393. err_t err;
  394. LWIP_UNUSED_ARG(_i);
  395. /* initialize local vars */
  396. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  397. memset(&counters, 0, sizeof(counters));
  398. /* create and initialize the pcb */
  399. pcb = test_tcp_new_counters_pcb(&counters);
  400. EXPECT_RET(pcb != NULL);
  401. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  402. pcb->mss = TCP_MSS;
  403. /* disable initial congestion window (we don't send a SYN here...) */
  404. pcb->cwnd = pcb->snd_wnd;
  405. /* send data1 */
  406. err = tcp_write(pcb, data1, sizeof(data1), TCP_WRITE_FLAG_COPY);
  407. EXPECT_RET(err == ERR_OK);
  408. err = tcp_output(pcb);
  409. EXPECT_RET(err == ERR_OK);
  410. EXPECT_RET(txcounters.num_tx_calls == 1);
  411. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  412. memset(&txcounters, 0, sizeof(txcounters));
  413. /* "recv" ACK for data1 */
  414. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
  415. EXPECT_RET(p != NULL);
  416. test_tcp_input(p, &netif);
  417. EXPECT_RET(txcounters.num_tx_calls == 0);
  418. EXPECT_RET(pcb->unacked == NULL);
  419. /* send data2 */
  420. err = tcp_write(pcb, data2, sizeof(data2), 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 == 1);
  425. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  426. memset(&txcounters, 0, sizeof(txcounters));
  427. /* duplicate ACK for data1 (data2 is lost) */
  428. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  429. EXPECT_RET(p != NULL);
  430. test_tcp_input(p, &netif);
  431. EXPECT_RET(txcounters.num_tx_calls == 0);
  432. EXPECT_RET(pcb->dupacks == 1);
  433. /* send data3 */
  434. err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
  435. EXPECT_RET(err == ERR_OK);
  436. err = tcp_output(pcb);
  437. EXPECT_RET(err == ERR_OK);
  438. /* nagle enabled, no tx calls */
  439. EXPECT_RET(txcounters.num_tx_calls == 0);
  440. EXPECT_RET(txcounters.num_tx_bytes == 0);
  441. memset(&txcounters, 0, sizeof(txcounters));
  442. /* 2nd duplicate ACK for data1 (data2 and data3 are lost) */
  443. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  444. EXPECT_RET(p != NULL);
  445. test_tcp_input(p, &netif);
  446. EXPECT_RET(txcounters.num_tx_calls == 0);
  447. EXPECT_RET(pcb->dupacks == 2);
  448. /* queue data4, don't send it (unsent-oversize is != 0) */
  449. err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
  450. EXPECT_RET(err == ERR_OK);
  451. /* 3nd duplicate ACK for data1 (data2 and data3 are lost) -> fast retransmission */
  452. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  453. EXPECT_RET(p != NULL);
  454. test_tcp_input(p, &netif);
  455. /*EXPECT_RET(txcounters.num_tx_calls == 1);*/
  456. EXPECT_RET(pcb->dupacks == 3);
  457. memset(&txcounters, 0, sizeof(txcounters));
  458. /* @todo: check expected data?*/
  459. /* send data5, not output yet */
  460. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  461. EXPECT_RET(err == ERR_OK);
  462. /*err = tcp_output(pcb);
  463. EXPECT_RET(err == ERR_OK);*/
  464. EXPECT_RET(txcounters.num_tx_calls == 0);
  465. EXPECT_RET(txcounters.num_tx_bytes == 0);
  466. memset(&txcounters, 0, sizeof(txcounters));
  467. {
  468. int i = 0;
  469. do
  470. {
  471. err = tcp_write(pcb, data6, TCP_MSS, TCP_WRITE_FLAG_COPY);
  472. i++;
  473. }while(err == ERR_OK);
  474. EXPECT_RET(err != ERR_OK);
  475. }
  476. err = tcp_output(pcb);
  477. EXPECT_RET(err == ERR_OK);
  478. /*EXPECT_RET(txcounters.num_tx_calls == 0);
  479. EXPECT_RET(txcounters.num_tx_bytes == 0);*/
  480. memset(&txcounters, 0, sizeof(txcounters));
  481. /* send even more data */
  482. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  483. EXPECT_RET(err == ERR_OK);
  484. err = tcp_output(pcb);
  485. EXPECT_RET(err == ERR_OK);
  486. /* ...and even more data */
  487. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  488. EXPECT_RET(err == ERR_OK);
  489. err = tcp_output(pcb);
  490. EXPECT_RET(err == ERR_OK);
  491. /* ...and even more data */
  492. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  493. EXPECT_RET(err == ERR_OK);
  494. err = tcp_output(pcb);
  495. EXPECT_RET(err == ERR_OK);
  496. /* ...and even more data */
  497. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  498. EXPECT_RET(err == ERR_OK);
  499. err = tcp_output(pcb);
  500. EXPECT_RET(err == ERR_OK);
  501. /* send ACKs for data2 and data3 */
  502. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 12, TCP_ACK);
  503. EXPECT_RET(p != NULL);
  504. test_tcp_input(p, &netif);
  505. /*EXPECT_RET(txcounters.num_tx_calls == 0);*/
  506. /* ...and even more data */
  507. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  508. EXPECT_RET(err == ERR_OK);
  509. err = tcp_output(pcb);
  510. EXPECT_RET(err == ERR_OK);
  511. /* ...and even more data */
  512. err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
  513. EXPECT_RET(err == ERR_OK);
  514. err = tcp_output(pcb);
  515. EXPECT_RET(err == ERR_OK);
  516. #if 0
  517. /* create expected segment */
  518. p1 = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  519. EXPECT_RET(p != NULL);
  520. if (p != NULL) {
  521. /* pass the segment to tcp_input */
  522. test_tcp_input(p, &netif);
  523. /* check if counters are as expected */
  524. EXPECT_RET(counters.close_calls == 0);
  525. EXPECT_RET(counters.recv_calls == 1);
  526. EXPECT_RET(counters.recved_bytes == data_len);
  527. EXPECT_RET(counters.err_calls == 0);
  528. }
  529. #endif
  530. /* make sure the pcb is freed */
  531. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  532. tcp_abort(pcb);
  533. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  534. }
  535. END_TEST
  536. static u8_t tx_data[TCP_WND*2];
  537. static void
  538. check_seqnos(struct tcp_seg *segs, int num_expected, u32_t *seqnos_expected)
  539. {
  540. struct tcp_seg *s = segs;
  541. int i;
  542. for (i = 0; i < num_expected; i++, s = s->next) {
  543. EXPECT_RET(s != NULL);
  544. EXPECT(s->tcphdr->seqno == htonl(seqnos_expected[i]));
  545. }
  546. EXPECT(s == NULL);
  547. }
  548. /** Send data with sequence numbers that wrap around the u32_t range.
  549. * Then, provoke fast retransmission by duplicate ACKs and check that all
  550. * segment lists are still properly sorted. */
  551. START_TEST(test_tcp_fast_rexmit_wraparound)
  552. {
  553. struct netif netif;
  554. struct test_tcp_txcounters txcounters;
  555. struct test_tcp_counters counters;
  556. struct tcp_pcb* pcb;
  557. struct pbuf* p;
  558. err_t err;
  559. size_t i;
  560. u16_t sent_total = 0;
  561. LWIP_UNUSED_ARG(_i);
  562. for (i = 0; i < sizeof(tx_data); i++) {
  563. tx_data[i] = (u8_t)i;
  564. }
  565. /* initialize local vars */
  566. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  567. memset(&counters, 0, sizeof(counters));
  568. /* create and initialize the pcb */
  569. tcp_ticks = SEQNO1 - ISS;
  570. pcb = test_tcp_new_counters_pcb(&counters);
  571. EXPECT_RET(pcb != NULL);
  572. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  573. pcb->mss = TCP_MSS;
  574. /* disable initial congestion window (we don't send a SYN here...) */
  575. pcb->cwnd = 2*TCP_MSS;
  576. /* start in congestion advoidance */
  577. pcb->ssthresh = pcb->cwnd;
  578. /* send 6 mss-sized segments */
  579. for (i = 0; i < 6; i++) {
  580. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  581. EXPECT_RET(err == ERR_OK);
  582. sent_total += TCP_MSS;
  583. }
  584. check_seqnos(pcb->unsent, 6, seqnos);
  585. EXPECT(pcb->unacked == NULL);
  586. err = tcp_output(pcb);
  587. EXPECT(txcounters.num_tx_calls == 2);
  588. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  589. memset(&txcounters, 0, sizeof(txcounters));
  590. check_seqnos(pcb->unacked, 2, seqnos);
  591. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  592. /* ACK the first segment */
  593. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  594. test_tcp_input(p, &netif);
  595. /* ensure this didn't trigger a retransmission. Only one
  596. segment should be transmitted because cwnd opened up by
  597. TCP_MSS and a fraction since we are in congestion avoidance */
  598. EXPECT(txcounters.num_tx_calls == 1);
  599. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  600. memset(&txcounters, 0, sizeof(txcounters));
  601. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  602. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  603. /* 3 dupacks */
  604. EXPECT(pcb->dupacks == 0);
  605. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  606. test_tcp_input(p, &netif);
  607. EXPECT(txcounters.num_tx_calls == 0);
  608. EXPECT(pcb->dupacks == 1);
  609. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  610. test_tcp_input(p, &netif);
  611. EXPECT(txcounters.num_tx_calls == 0);
  612. EXPECT(pcb->dupacks == 2);
  613. /* 3rd dupack -> fast rexmit */
  614. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  615. test_tcp_input(p, &netif);
  616. EXPECT(pcb->dupacks == 3);
  617. EXPECT(txcounters.num_tx_calls == 4);
  618. memset(&txcounters, 0, sizeof(txcounters));
  619. EXPECT(pcb->unsent == NULL);
  620. check_seqnos(pcb->unacked, 5, &seqnos[1]);
  621. /* make sure the pcb is freed */
  622. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  623. tcp_abort(pcb);
  624. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  625. }
  626. END_TEST
  627. /** Send data with sequence numbers that wrap around the u32_t range.
  628. * Then, provoke RTO retransmission and check that all
  629. * segment lists are still properly sorted. */
  630. START_TEST(test_tcp_rto_rexmit_wraparound)
  631. {
  632. struct netif netif;
  633. struct test_tcp_txcounters txcounters;
  634. struct test_tcp_counters counters;
  635. struct tcp_pcb* pcb;
  636. struct tcp_pcb dummy_pcb_for_iss; /* we need this for tcp_next_iss() only */
  637. err_t err;
  638. size_t i;
  639. u16_t sent_total = 0;
  640. LWIP_UNUSED_ARG(_i);
  641. for (i = 0; i < sizeof(tx_data); i++) {
  642. tx_data[i] = (u8_t)i;
  643. }
  644. /* initialize local vars */
  645. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  646. memset(&counters, 0, sizeof(counters));
  647. /* create and initialize the pcb */
  648. tcp_ticks = 0;
  649. tcp_ticks = 0 - tcp_next_iss(&dummy_pcb_for_iss);
  650. tcp_ticks = SEQNO1 - tcp_next_iss(&dummy_pcb_for_iss);
  651. pcb = test_tcp_new_counters_pcb(&counters);
  652. EXPECT_RET(pcb != NULL);
  653. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  654. pcb->mss = TCP_MSS;
  655. /* disable initial congestion window (we don't send a SYN here...) */
  656. pcb->cwnd = 2*TCP_MSS;
  657. /* send 6 mss-sized segments */
  658. for (i = 0; i < 6; i++) {
  659. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  660. EXPECT_RET(err == ERR_OK);
  661. sent_total += TCP_MSS;
  662. }
  663. check_seqnos(pcb->unsent, 6, seqnos);
  664. EXPECT(pcb->unacked == NULL);
  665. err = tcp_output(pcb);
  666. EXPECT(txcounters.num_tx_calls == 2);
  667. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  668. memset(&txcounters, 0, sizeof(txcounters));
  669. check_seqnos(pcb->unacked, 2, seqnos);
  670. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  671. /* call the tcp timer some times */
  672. for (i = 0; i < 10; i++) {
  673. test_tcp_tmr();
  674. EXPECT(txcounters.num_tx_calls == 0);
  675. }
  676. /* 11th call to tcp_tmr: RTO rexmit fires */
  677. test_tcp_tmr();
  678. EXPECT(txcounters.num_tx_calls == 1);
  679. check_seqnos(pcb->unacked, 1, seqnos);
  680. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  681. /* fake greater cwnd */
  682. pcb->cwnd = pcb->snd_wnd;
  683. /* send more data */
  684. err = tcp_output(pcb);
  685. EXPECT(err == ERR_OK);
  686. /* check queues are sorted */
  687. EXPECT(pcb->unsent == NULL);
  688. check_seqnos(pcb->unacked, 6, seqnos);
  689. /* make sure the pcb is freed */
  690. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  691. tcp_abort(pcb);
  692. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  693. }
  694. END_TEST
  695. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  696. * At the end, send more data. */
  697. static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
  698. {
  699. struct netif netif;
  700. struct test_tcp_txcounters txcounters;
  701. struct test_tcp_counters counters;
  702. struct tcp_pcb* pcb;
  703. struct pbuf *p;
  704. err_t err;
  705. size_t i;
  706. u16_t sent_total;
  707. u8_t expected = 0xFE;
  708. for (i = 0; i < sizeof(tx_data); i++) {
  709. u8_t d = (u8_t)i;
  710. if (d == 0xFE) {
  711. d = 0xF0;
  712. }
  713. tx_data[i] = d;
  714. }
  715. if (zero_window_probe_from_unsent) {
  716. tx_data[TCP_WND] = expected;
  717. } else {
  718. tx_data[0] = expected;
  719. }
  720. /* initialize local vars */
  721. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  722. memset(&counters, 0, sizeof(counters));
  723. /* create and initialize the pcb */
  724. pcb = test_tcp_new_counters_pcb(&counters);
  725. EXPECT_RET(pcb != NULL);
  726. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  727. pcb->mss = TCP_MSS;
  728. /* disable initial congestion window (we don't send a SYN here...) */
  729. pcb->cwnd = pcb->snd_wnd;
  730. /* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
  731. sent_total = 0;
  732. if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
  733. u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
  734. err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
  735. EXPECT_RET(err == ERR_OK);
  736. err = tcp_output(pcb);
  737. EXPECT_RET(err == ERR_OK);
  738. EXPECT(txcounters.num_tx_calls == 1);
  739. EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
  740. memset(&txcounters, 0, sizeof(txcounters));
  741. sent_total += initial_data_len;
  742. }
  743. for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
  744. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  745. EXPECT_RET(err == ERR_OK);
  746. err = tcp_output(pcb);
  747. EXPECT_RET(err == ERR_OK);
  748. EXPECT(txcounters.num_tx_calls == 1);
  749. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  750. memset(&txcounters, 0, sizeof(txcounters));
  751. }
  752. EXPECT(sent_total == (TCP_WND - TCP_MSS));
  753. /* now ACK the packet before the first */
  754. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  755. test_tcp_input(p, &netif);
  756. /* ensure this didn't trigger a retransmission */
  757. EXPECT(txcounters.num_tx_calls == 0);
  758. EXPECT(txcounters.num_tx_bytes == 0);
  759. EXPECT(pcb->persist_backoff == 0);
  760. /* send the last packet, now a complete window has been sent */
  761. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  762. sent_total += TCP_MSS;
  763. EXPECT_RET(err == ERR_OK);
  764. err = tcp_output(pcb);
  765. EXPECT_RET(err == ERR_OK);
  766. EXPECT(txcounters.num_tx_calls == 1);
  767. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  768. memset(&txcounters, 0, sizeof(txcounters));
  769. EXPECT(pcb->persist_backoff == 0);
  770. if (zero_window_probe_from_unsent) {
  771. /* ACK all data but close the TX window */
  772. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
  773. test_tcp_input(p, &netif);
  774. /* ensure this didn't trigger any transmission */
  775. EXPECT(txcounters.num_tx_calls == 0);
  776. EXPECT(txcounters.num_tx_bytes == 0);
  777. /* window is completely full, but persist timer is off since send buffer is empty */
  778. EXPECT(pcb->snd_wnd == 0);
  779. EXPECT(pcb->persist_backoff == 0);
  780. }
  781. /* send one byte more (out of window) -> persist timer starts */
  782. err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
  783. EXPECT_RET(err == ERR_OK);
  784. err = tcp_output(pcb);
  785. EXPECT_RET(err == ERR_OK);
  786. EXPECT(txcounters.num_tx_calls == 0);
  787. EXPECT(txcounters.num_tx_bytes == 0);
  788. memset(&txcounters, 0, sizeof(txcounters));
  789. if (!zero_window_probe_from_unsent) {
  790. /* no persist timer unless a zero window announcement has been received */
  791. EXPECT(pcb->persist_backoff == 0);
  792. } else {
  793. EXPECT(pcb->persist_backoff == 1);
  794. /* call tcp_timer some more times to let persist timer count up */
  795. for (i = 0; i < 4; i++) {
  796. test_tcp_tmr();
  797. EXPECT(txcounters.num_tx_calls == 0);
  798. EXPECT(txcounters.num_tx_bytes == 0);
  799. }
  800. /* this should trigger the zero-window-probe */
  801. txcounters.copy_tx_packets = 1;
  802. test_tcp_tmr();
  803. txcounters.copy_tx_packets = 0;
  804. EXPECT(txcounters.num_tx_calls == 1);
  805. EXPECT(txcounters.num_tx_bytes == 1 + 40U);
  806. EXPECT(txcounters.tx_packets != NULL);
  807. if (txcounters.tx_packets != NULL) {
  808. u8_t sent;
  809. u16_t ret;
  810. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
  811. EXPECT(ret == 1);
  812. EXPECT(sent == expected);
  813. }
  814. if (txcounters.tx_packets != NULL) {
  815. pbuf_free(txcounters.tx_packets);
  816. txcounters.tx_packets = NULL;
  817. }
  818. }
  819. /* make sure the pcb is freed */
  820. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  821. tcp_abort(pcb);
  822. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  823. }
  824. START_TEST(test_tcp_tx_full_window_lost_from_unsent)
  825. {
  826. LWIP_UNUSED_ARG(_i);
  827. test_tcp_tx_full_window_lost(1);
  828. }
  829. END_TEST
  830. START_TEST(test_tcp_tx_full_window_lost_from_unacked)
  831. {
  832. LWIP_UNUSED_ARG(_i);
  833. test_tcp_tx_full_window_lost(0);
  834. }
  835. END_TEST
  836. /** Send data, provoke retransmission and then add data to a segment
  837. * that already has been sent before. */
  838. START_TEST(test_tcp_retx_add_to_sent)
  839. {
  840. struct netif netif;
  841. struct test_tcp_txcounters txcounters;
  842. struct test_tcp_counters counters;
  843. struct tcp_pcb* pcb;
  844. struct pbuf* p;
  845. char data1a[] = { 1, 2, 3};
  846. char data1b[] = { 4};
  847. char data2a[] = { 5, 6, 7, 8};
  848. char data2b[] = { 5, 6, 7};
  849. char data3[] = { 9, 10, 11, 12, 12};
  850. char data4[] = { 13, 14, 15, 16,17};
  851. err_t err;
  852. int i;
  853. LWIP_UNUSED_ARG(_i);
  854. /* initialize local vars */
  855. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  856. memset(&counters, 0, sizeof(counters));
  857. /* create and initialize the pcb */
  858. pcb = test_tcp_new_counters_pcb(&counters);
  859. EXPECT_RET(pcb != NULL);
  860. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  861. pcb->mss = TCP_MSS;
  862. /* disable initial congestion window (we don't send a SYN here...) */
  863. pcb->cwnd = pcb->snd_wnd;
  864. /* send data1 */
  865. err = tcp_write(pcb, data1a, sizeof(data1a), TCP_WRITE_FLAG_COPY);
  866. EXPECT_RET(err == ERR_OK);
  867. err = tcp_write(pcb, data1b, sizeof(data1b), TCP_WRITE_FLAG_COPY);
  868. EXPECT_RET(err == ERR_OK);
  869. err = tcp_output(pcb);
  870. EXPECT_RET(err == ERR_OK);
  871. EXPECT_RET(txcounters.num_tx_calls == 1);
  872. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1a) + sizeof(data1b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  873. memset(&txcounters, 0, sizeof(txcounters));
  874. /* "recv" ACK for data1 */
  875. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
  876. EXPECT_RET(p != NULL);
  877. test_tcp_input(p, &netif);
  878. EXPECT_RET(txcounters.num_tx_calls == 0);
  879. EXPECT_RET(pcb->unacked == NULL);
  880. /* send data2 */
  881. err = tcp_write(pcb, data2a, sizeof(data2a), TCP_WRITE_FLAG_COPY);
  882. EXPECT_RET(err == ERR_OK);
  883. err = tcp_write(pcb, data2b, sizeof(data2b), TCP_WRITE_FLAG_COPY);
  884. EXPECT_RET(err == ERR_OK);
  885. err = tcp_output(pcb);
  886. EXPECT_RET(err == ERR_OK);
  887. EXPECT_RET(txcounters.num_tx_calls == 1);
  888. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2a) + sizeof(data2b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  889. memset(&txcounters, 0, sizeof(txcounters));
  890. /* send data3 */
  891. err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
  892. EXPECT_RET(err == ERR_OK);
  893. err = tcp_output(pcb);
  894. EXPECT_RET(err == ERR_OK);
  895. EXPECT_RET(txcounters.num_tx_calls == 0);
  896. EXPECT_RET(txcounters.num_tx_bytes == 0);
  897. memset(&txcounters, 0, sizeof(txcounters));
  898. /* data3 not sent yet (nagle) */
  899. EXPECT_RET(pcb->unacked != NULL);
  900. EXPECT_RET(pcb->unsent != NULL);
  901. /* disable nagle for this test so data to sent segment can be added below... */
  902. tcp_nagle_disable(pcb);
  903. /* call the tcp timer some times */
  904. for (i = 0; i < 20; i++) {
  905. test_tcp_tmr();
  906. if (txcounters.num_tx_calls != 0) {
  907. break;
  908. }
  909. }
  910. /* data3 sent */
  911. EXPECT_RET(txcounters.num_tx_calls == 1);
  912. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data3) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  913. EXPECT_RET(pcb->unacked != NULL);
  914. EXPECT_RET(pcb->unsent == NULL);
  915. memset(&txcounters, 0, sizeof(txcounters));
  916. tcp_nagle_enable(pcb);
  917. /* call the tcp timer some times */
  918. for (i = 0; i < 20; i++) {
  919. test_tcp_tmr();
  920. if (txcounters.num_tx_calls != 0) {
  921. break;
  922. }
  923. }
  924. /* RTO: rexmit of data2 */
  925. EXPECT_RET(txcounters.num_tx_calls == 1);
  926. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2a) + sizeof(data2b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  927. EXPECT_RET(pcb->unacked != NULL);
  928. EXPECT_RET(pcb->unsent != NULL);
  929. memset(&txcounters, 0, sizeof(txcounters));
  930. /* send data4 */
  931. err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
  932. EXPECT_RET(err == ERR_OK);
  933. /* disable nagle for this test so data to transmit without further ACKs... */
  934. tcp_nagle_disable(pcb);
  935. err = tcp_output(pcb);
  936. EXPECT_RET(err == ERR_OK);
  937. /* nagle enabled, no tx calls */
  938. EXPECT_RET(txcounters.num_tx_calls == 1);
  939. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data3) + sizeof(data4) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  940. memset(&txcounters, 0, sizeof(txcounters));
  941. /* make sure the pcb is freed */
  942. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  943. tcp_abort(pcb);
  944. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  945. }
  946. END_TEST
  947. START_TEST(test_tcp_rto_tracking)
  948. {
  949. struct netif netif;
  950. struct test_tcp_txcounters txcounters;
  951. struct test_tcp_counters counters;
  952. struct tcp_pcb* pcb;
  953. struct pbuf* p;
  954. err_t err;
  955. size_t i;
  956. u16_t sent_total = 0;
  957. LWIP_UNUSED_ARG(_i);
  958. for (i = 0; i < sizeof(tx_data); i++) {
  959. tx_data[i] = (u8_t)i;
  960. }
  961. /* initialize local vars */
  962. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  963. memset(&counters, 0, sizeof(counters));
  964. /* create and initialize the pcb */
  965. tcp_ticks = SEQNO1 - ISS;
  966. pcb = test_tcp_new_counters_pcb(&counters);
  967. EXPECT_RET(pcb != NULL);
  968. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  969. pcb->mss = TCP_MSS;
  970. /* Set congestion window large enough to send all our segments */
  971. pcb->cwnd = 5*TCP_MSS;
  972. /* send 5 mss-sized segments */
  973. for (i = 0; i < 5; i++) {
  974. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  975. EXPECT_RET(err == ERR_OK);
  976. sent_total += TCP_MSS;
  977. }
  978. check_seqnos(pcb->unsent, 5, seqnos);
  979. EXPECT(pcb->unacked == NULL);
  980. err = tcp_output(pcb);
  981. EXPECT(txcounters.num_tx_calls == 5);
  982. EXPECT(txcounters.num_tx_bytes == 5 * (TCP_MSS + 40U));
  983. memset(&txcounters, 0, sizeof(txcounters));
  984. /* Check all 5 are in-flight */
  985. EXPECT(pcb->unsent == NULL);
  986. check_seqnos(pcb->unacked, 5, seqnos);
  987. /* Force us into retransmisson timeout */
  988. while (!(pcb->flags & TF_RTO)) {
  989. test_tcp_tmr();
  990. }
  991. /* Ensure 4 remaining segments are back on unsent, ready for retransmission */
  992. check_seqnos(pcb->unsent, 4, &seqnos[1]);
  993. /* Ensure 1st segment is on unacked (already retransmitted) */
  994. check_seqnos(pcb->unacked, 1, seqnos);
  995. EXPECT(txcounters.num_tx_calls == 1);
  996. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  997. memset(&txcounters, 0, sizeof(txcounters));
  998. /* Ensure rto_end points to next byte */
  999. EXPECT(pcb->rto_end == seqnos[5]);
  1000. EXPECT(pcb->rto_end == pcb->snd_nxt);
  1001. /* Check cwnd was reset */
  1002. EXPECT(pcb->cwnd == pcb->mss);
  1003. /* Add another segment to send buffer which is outside of RTO */
  1004. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1005. EXPECT_RET(err == ERR_OK);
  1006. sent_total += TCP_MSS;
  1007. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  1008. /* Ensure no new data was sent */
  1009. EXPECT(txcounters.num_tx_calls == 0);
  1010. EXPECT(txcounters.num_tx_bytes == 0);
  1011. EXPECT(pcb->rto_end == pcb->snd_nxt);
  1012. /* ACK first segment */
  1013. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  1014. test_tcp_input(p, &netif);
  1015. /* Next two retranmissions should go out, due to cwnd in slow start */
  1016. EXPECT(txcounters.num_tx_calls == 2);
  1017. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  1018. memset(&txcounters, 0, sizeof(txcounters));
  1019. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  1020. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  1021. /* RTO should still be marked */
  1022. EXPECT(pcb->flags & TF_RTO);
  1023. /* cwnd should have only grown by 1 MSS */
  1024. EXPECT(pcb->cwnd == (tcpwnd_size_t)(2 * pcb->mss));
  1025. /* Ensure no new data was sent */
  1026. EXPECT(pcb->rto_end == pcb->snd_nxt);
  1027. /* ACK the next two segments */
  1028. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
  1029. test_tcp_input(p, &netif);
  1030. /* Final 2 retransmissions and 1 new data should go out */
  1031. EXPECT(txcounters.num_tx_calls == 3);
  1032. EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
  1033. memset(&txcounters, 0, sizeof(txcounters));
  1034. check_seqnos(pcb->unacked, 3, &seqnos[3]);
  1035. EXPECT(pcb->unsent == NULL);
  1036. /* RTO should still be marked */
  1037. EXPECT(pcb->flags & TF_RTO);
  1038. /* cwnd should have only grown by 1 MSS */
  1039. EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
  1040. /* snd_nxt should have been advanced past rto_end */
  1041. EXPECT(TCP_SEQ_GT(pcb->snd_nxt, pcb->rto_end));
  1042. /* ACK the next two segments, finishing our RTO, leaving new segment unacked */
  1043. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
  1044. test_tcp_input(p, &netif);
  1045. EXPECT(!(pcb->flags & TF_RTO));
  1046. check_seqnos(pcb->unacked, 1, &seqnos[5]);
  1047. /* We should be in ABC congestion avoidance, so no change in cwnd */
  1048. EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
  1049. EXPECT(pcb->cwnd >= pcb->ssthresh);
  1050. /* Ensure ABC congestion avoidance is tracking bytes acked */
  1051. EXPECT(pcb->bytes_acked == (tcpwnd_size_t)(2 * pcb->mss));
  1052. /* make sure the pcb is freed */
  1053. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  1054. tcp_abort(pcb);
  1055. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1056. }
  1057. END_TEST
  1058. static void test_tcp_rto_timeout_impl(int link_down)
  1059. {
  1060. struct netif netif;
  1061. struct test_tcp_txcounters txcounters;
  1062. struct test_tcp_counters counters;
  1063. struct tcp_pcb *pcb, *cur;
  1064. err_t err;
  1065. size_t i;
  1066. const size_t max_wait_ctr = 1024 * 1024;
  1067. /* Setup data for a single segment */
  1068. for (i = 0; i < TCP_MSS; i++) {
  1069. tx_data[i] = (u8_t)i;
  1070. }
  1071. /* initialize local vars */
  1072. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1073. memset(&counters, 0, sizeof(counters));
  1074. /* create and initialize the pcb */
  1075. tcp_ticks = SEQNO1 - ISS;
  1076. pcb = test_tcp_new_counters_pcb(&counters);
  1077. EXPECT_RET(pcb != NULL);
  1078. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1079. pcb->mss = TCP_MSS;
  1080. pcb->cwnd = TCP_MSS;
  1081. /* send our segment */
  1082. err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1083. EXPECT_RET(err == ERR_OK);
  1084. err = tcp_output(pcb);
  1085. EXPECT(txcounters.num_tx_calls == 1);
  1086. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1087. memset(&txcounters, 0, sizeof(txcounters));
  1088. /* ensure no errors have been recorded */
  1089. EXPECT(counters.err_calls == 0);
  1090. EXPECT(counters.last_err == ERR_OK);
  1091. /* Force us into retransmisson timeout */
  1092. for (i = 0; !(pcb->flags & TF_RTO) && i < max_wait_ctr; i++) {
  1093. test_tcp_tmr();
  1094. }
  1095. EXPECT(i < max_wait_ctr);
  1096. /* check first rexmit */
  1097. EXPECT(pcb->nrtx == 1);
  1098. EXPECT(txcounters.num_tx_calls == 1);
  1099. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1100. /* still no error expected */
  1101. EXPECT(counters.err_calls == 0);
  1102. EXPECT(counters.last_err == ERR_OK);
  1103. if (link_down) {
  1104. netif_set_link_down(&netif);
  1105. }
  1106. /* keep running the timer till we hit our maximum RTO */
  1107. for (i = 0; counters.last_err == ERR_OK && i < max_wait_ctr; i++) {
  1108. test_tcp_tmr();
  1109. }
  1110. EXPECT(i < max_wait_ctr);
  1111. /* check number of retransmissions */
  1112. if (link_down) {
  1113. EXPECT(txcounters.num_tx_calls == 1);
  1114. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1115. } else {
  1116. EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
  1117. EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (TCP_MSS + 40U));
  1118. }
  1119. /* check the connection (pcb) has been aborted */
  1120. EXPECT(counters.err_calls == 1);
  1121. EXPECT(counters.last_err == ERR_ABRT);
  1122. /* check our pcb is no longer active */
  1123. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1124. EXPECT(cur != pcb);
  1125. }
  1126. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1127. }
  1128. START_TEST(test_tcp_rto_timeout)
  1129. {
  1130. LWIP_UNUSED_ARG(_i);
  1131. test_tcp_rto_timeout_impl(0);
  1132. }
  1133. END_TEST
  1134. START_TEST(test_tcp_rto_timeout_link_down)
  1135. {
  1136. LWIP_UNUSED_ARG(_i);
  1137. test_tcp_rto_timeout_impl(1);
  1138. }
  1139. END_TEST
  1140. static void test_tcp_rto_timeout_syn_sent_impl(int link_down)
  1141. {
  1142. struct netif netif;
  1143. struct test_tcp_txcounters txcounters;
  1144. struct test_tcp_counters counters;
  1145. struct tcp_pcb *pcb, *cur;
  1146. err_t err;
  1147. size_t i;
  1148. const size_t max_wait_ctr = 1024 * 1024;
  1149. const u16_t tcp_syn_opts_len = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_MSS|TF_SEG_OPTS_WND_SCALE|TF_SEG_OPTS_SACK_PERM|TF_SEG_OPTS_TS);
  1150. /* Setup data for a single segment */
  1151. for (i = 0; i < TCP_MSS; i++) {
  1152. tx_data[i] = (u8_t)i;
  1153. }
  1154. /* initialize local vars */
  1155. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1156. memset(&counters, 0, sizeof(counters));
  1157. /* create and initialize the pcb */
  1158. tcp_ticks = SEQNO1 - ISS;
  1159. pcb = test_tcp_new_counters_pcb(&counters);
  1160. EXPECT_RET(pcb != NULL);
  1161. err = tcp_connect(pcb, &netif.gw, 123, NULL);
  1162. EXPECT_RET(err == ERR_OK);
  1163. EXPECT_RET(pcb->state == SYN_SENT);
  1164. EXPECT(txcounters.num_tx_calls == 1);
  1165. EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len);
  1166. /* ensure no errors have been recorded */
  1167. EXPECT(counters.err_calls == 0);
  1168. EXPECT(counters.last_err == ERR_OK);
  1169. txcounters.num_tx_calls = 0;
  1170. txcounters.num_tx_bytes = 0;
  1171. /* Force us into retransmisson timeout */
  1172. for (i = 0; !(pcb->flags & TF_RTO) && i < max_wait_ctr; i++) {
  1173. test_tcp_tmr();
  1174. }
  1175. EXPECT(i < max_wait_ctr);
  1176. /* check first rexmit */
  1177. EXPECT(pcb->nrtx == 1);
  1178. EXPECT(txcounters.num_tx_calls == 1);
  1179. EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len); /* 40: headers; >=: options */
  1180. /* still no error expected */
  1181. EXPECT(counters.err_calls == 0);
  1182. EXPECT(counters.last_err == ERR_OK);
  1183. if (link_down) {
  1184. /* set link down and check what happens to the RTO counter */
  1185. netif_set_link_down(&netif);
  1186. }
  1187. /* keep running the timer till we hit our maximum RTO */
  1188. for (i = 0; counters.last_err == ERR_OK && i < max_wait_ctr; i++) {
  1189. test_tcp_tmr();
  1190. }
  1191. EXPECT(i < max_wait_ctr);
  1192. /* check number of retransmissions */
  1193. if (link_down) {
  1194. EXPECT(txcounters.num_tx_calls == 1);
  1195. EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len);
  1196. } else {
  1197. EXPECT(txcounters.num_tx_calls == TCP_SYNMAXRTX);
  1198. EXPECT(txcounters.num_tx_bytes == TCP_SYNMAXRTX * (tcp_syn_opts_len + 40U));
  1199. }
  1200. /* check the connection (pcb) has been aborted */
  1201. EXPECT(counters.err_calls == 1);
  1202. EXPECT(counters.last_err == ERR_ABRT);
  1203. /* check our pcb is no longer active */
  1204. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1205. EXPECT(cur != pcb);
  1206. }
  1207. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1208. }
  1209. START_TEST(test_tcp_rto_timeout_syn_sent)
  1210. {
  1211. LWIP_UNUSED_ARG(_i);
  1212. test_tcp_rto_timeout_syn_sent_impl(0);
  1213. }
  1214. END_TEST
  1215. START_TEST(test_tcp_rto_timeout_syn_sent_link_down)
  1216. {
  1217. LWIP_UNUSED_ARG(_i);
  1218. test_tcp_rto_timeout_syn_sent_impl(1);
  1219. }
  1220. END_TEST
  1221. static void test_tcp_zwp_timeout_impl(int link_down)
  1222. {
  1223. struct netif netif;
  1224. struct test_tcp_txcounters txcounters;
  1225. struct test_tcp_counters counters;
  1226. struct tcp_pcb *pcb, *cur;
  1227. struct pbuf* p;
  1228. err_t err;
  1229. size_t i;
  1230. /* Setup data for two segments */
  1231. for (i = 0; i < 2*TCP_MSS; i++) {
  1232. tx_data[i] = (u8_t)i;
  1233. }
  1234. /* initialize local vars */
  1235. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1236. memset(&counters, 0, sizeof(counters));
  1237. /* create and initialize the pcb */
  1238. tcp_ticks = SEQNO1 - ISS;
  1239. pcb = test_tcp_new_counters_pcb(&counters);
  1240. EXPECT_RET(pcb != NULL);
  1241. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1242. pcb->mss = TCP_MSS;
  1243. pcb->cwnd = TCP_MSS;
  1244. /* send first segment */
  1245. err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1246. EXPECT(err == ERR_OK);
  1247. err = tcp_output(pcb);
  1248. EXPECT(err == ERR_OK);
  1249. /* verify segment is in-flight */
  1250. EXPECT(pcb->unsent == NULL);
  1251. check_seqnos(pcb->unacked, 1, seqnos);
  1252. EXPECT(txcounters.num_tx_calls == 1);
  1253. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1254. memset(&txcounters, 0, sizeof(txcounters));
  1255. /* ACK the segment and close the TX window */
  1256. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK, 0);
  1257. test_tcp_input(p, &netif);
  1258. EXPECT(pcb->unacked == NULL);
  1259. EXPECT(pcb->unsent == NULL);
  1260. /* send buffer empty, persist should be off */
  1261. EXPECT(pcb->persist_backoff == 0);
  1262. EXPECT(pcb->snd_wnd == 0);
  1263. /* send second segment, should be buffered */
  1264. err = tcp_write(pcb, &tx_data[TCP_MSS], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1265. EXPECT(err == ERR_OK);
  1266. err = tcp_output(pcb);
  1267. EXPECT(err == ERR_OK);
  1268. /* ensure it is buffered and persist timer started */
  1269. EXPECT(pcb->unacked == NULL);
  1270. check_seqnos(pcb->unsent, 1, &seqnos[1]);
  1271. EXPECT(txcounters.num_tx_calls == 0);
  1272. EXPECT(txcounters.num_tx_bytes == 0);
  1273. EXPECT(pcb->persist_backoff == 1);
  1274. /* ensure no errors have been recorded */
  1275. EXPECT(counters.err_calls == 0);
  1276. EXPECT(counters.last_err == ERR_OK);
  1277. /* run timer till first probe */
  1278. EXPECT(pcb->persist_probe == 0);
  1279. while (pcb->persist_probe == 0) {
  1280. test_tcp_tmr();
  1281. }
  1282. EXPECT(txcounters.num_tx_calls == 1);
  1283. EXPECT(txcounters.num_tx_bytes == (1 + 40U));
  1284. memset(&txcounters, 0, sizeof(txcounters));
  1285. /* respond to probe with remote's current SEQ, ACK, and zero-window */
  1286. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 0, TCP_ACK, 0);
  1287. test_tcp_input(p, &netif);
  1288. /* ensure zero-window is still active, but probe count reset */
  1289. EXPECT(pcb->persist_backoff > 1);
  1290. EXPECT(pcb->persist_probe == 0);
  1291. EXPECT(pcb->snd_wnd == 0);
  1292. /* ensure no errors have been recorded */
  1293. EXPECT(counters.err_calls == 0);
  1294. EXPECT(counters.last_err == ERR_OK);
  1295. if (link_down) {
  1296. netif_set_link_down(&netif);
  1297. }
  1298. /* now run the timer till we hit our maximum probe count */
  1299. while (counters.last_err == ERR_OK) {
  1300. test_tcp_tmr();
  1301. }
  1302. if (link_down) {
  1303. EXPECT(txcounters.num_tx_calls == 0);
  1304. EXPECT(txcounters.num_tx_bytes == 0);
  1305. } else {
  1306. /* check maximum number of 1 byte probes were sent */
  1307. EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
  1308. EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (1 + 40U));
  1309. }
  1310. /* check the connection (pcb) has been aborted */
  1311. EXPECT(counters.err_calls == 1);
  1312. EXPECT(counters.last_err == ERR_ABRT);
  1313. /* check our pcb is no longer active */
  1314. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1315. EXPECT(cur != pcb);
  1316. }
  1317. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1318. }
  1319. START_TEST(test_tcp_zwp_timeout)
  1320. {
  1321. LWIP_UNUSED_ARG(_i);
  1322. test_tcp_zwp_timeout_impl(0);
  1323. }
  1324. END_TEST
  1325. START_TEST(test_tcp_zwp_timeout_link_down)
  1326. {
  1327. LWIP_UNUSED_ARG(_i);
  1328. test_tcp_zwp_timeout_impl(1);
  1329. }
  1330. END_TEST
  1331. START_TEST(test_tcp_persist_split)
  1332. {
  1333. struct netif netif;
  1334. struct test_tcp_txcounters txcounters;
  1335. struct test_tcp_counters counters;
  1336. struct tcp_pcb *pcb;
  1337. struct pbuf* p;
  1338. err_t err;
  1339. size_t i;
  1340. LWIP_UNUSED_ARG(_i);
  1341. /* Setup data for four segments */
  1342. for (i = 0; i < 4 * TCP_MSS; i++) {
  1343. tx_data[i] = (u8_t)i;
  1344. }
  1345. /* initialize local vars */
  1346. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1347. memset(&counters, 0, sizeof(counters));
  1348. /* create and initialize the pcb */
  1349. tcp_ticks = SEQNO1 - ISS;
  1350. pcb = test_tcp_new_counters_pcb(&counters);
  1351. EXPECT_RET(pcb != NULL);
  1352. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1353. pcb->mss = TCP_MSS;
  1354. /* set window to three segments */
  1355. pcb->cwnd = 3 * TCP_MSS;
  1356. pcb->snd_wnd = 3 * TCP_MSS;
  1357. pcb->snd_wnd_max = 3 * TCP_MSS;
  1358. /* send four segments. Fourth should stay buffered and is a 3/4 MSS segment to
  1359. get coverage on the oversized segment case */
  1360. err = tcp_write(pcb, &tx_data[0], (3 * TCP_MSS) + (TCP_MSS - (TCP_MSS / 4)), TCP_WRITE_FLAG_COPY);
  1361. EXPECT(err == ERR_OK);
  1362. err = tcp_output(pcb);
  1363. EXPECT(err == ERR_OK);
  1364. /* verify 3 segments are in-flight */
  1365. EXPECT(pcb->unacked != NULL);
  1366. check_seqnos(pcb->unacked, 3, seqnos);
  1367. EXPECT(txcounters.num_tx_calls == 3);
  1368. EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
  1369. memset(&txcounters, 0, sizeof(txcounters));
  1370. /* verify 4th segment is on unsent */
  1371. EXPECT(pcb->unsent != NULL);
  1372. EXPECT(pcb->unsent->len == TCP_MSS - (TCP_MSS / 4));
  1373. check_seqnos(pcb->unsent, 1, &seqnos[3]);
  1374. #if TCP_OVERSIZE
  1375. EXPECT(pcb->unsent_oversize == TCP_MSS / 4);
  1376. #if TCP_OVERSIZE_DBGCHECK
  1377. EXPECT(pcb->unsent->oversize_left == pcb->unsent_oversize);
  1378. #endif /* TCP_OVERSIZE_DBGCHECK */
  1379. #endif /* TCP_OVERSIZE */
  1380. /* ACK the 3 segments and update the window to only 1/2 TCP_MSS.
  1381. 4th segment should stay on unsent because it's bigger than 1/2 MSS */
  1382. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 3 * TCP_MSS, TCP_ACK, TCP_MSS / 2);
  1383. test_tcp_input(p, &netif);
  1384. EXPECT(pcb->unacked == NULL);
  1385. EXPECT(pcb->snd_wnd == TCP_MSS / 2);
  1386. EXPECT(pcb->unsent != NULL);
  1387. check_seqnos(pcb->unsent, 1, &seqnos[3]);
  1388. EXPECT(txcounters.num_tx_calls == 0);
  1389. EXPECT(txcounters.num_tx_bytes == 0);
  1390. /* persist timer should be started since 4th segment is stuck waiting on snd_wnd */
  1391. EXPECT(pcb->persist_backoff == 1);
  1392. /* ensure no errors have been recorded */
  1393. EXPECT(counters.err_calls == 0);
  1394. EXPECT(counters.last_err == ERR_OK);
  1395. /* call tcp_timer some more times to let persist timer count up */
  1396. for (i = 0; i < 4; i++) {
  1397. test_tcp_tmr();
  1398. EXPECT(txcounters.num_tx_calls == 0);
  1399. EXPECT(txcounters.num_tx_bytes == 0);
  1400. }
  1401. /* this should be the first timer shot, which should split the
  1402. * segment and send a runt (of the remaining window size) */
  1403. txcounters.copy_tx_packets = 1;
  1404. test_tcp_tmr();
  1405. txcounters.copy_tx_packets = 0;
  1406. /* persist will be disabled as RTO timer takes over */
  1407. EXPECT(pcb->persist_backoff == 0);
  1408. EXPECT(txcounters.num_tx_calls == 1);
  1409. EXPECT(txcounters.num_tx_bytes == ((TCP_MSS /2) + 40U));
  1410. /* verify 1/2 MSS segment sent, 1/4 MSS still buffered */
  1411. EXPECT(pcb->unsent != NULL);
  1412. EXPECT(pcb->unsent->len == TCP_MSS / 4);
  1413. EXPECT(pcb->unacked != NULL);
  1414. EXPECT(pcb->unacked->len == TCP_MSS / 2);
  1415. #if TCP_OVERSIZE
  1416. /* verify there is no oversized remaining since during the
  1417. segment split, the remainder pbuf is always the exact length */
  1418. EXPECT(pcb->unsent_oversize == 0);
  1419. #if TCP_OVERSIZE_DBGCHECK
  1420. /* Split segment already transmitted, should be at 0 */
  1421. EXPECT(pcb->unacked->oversize_left == 0);
  1422. /* Remainder segement should match pcb value (which is 0) */
  1423. EXPECT(pcb->unsent->oversize_left == pcb->unsent_oversize);
  1424. #endif /* TCP_OVERSIZE_DBGCHECK */
  1425. #endif /* TCP_OVERSIZE */
  1426. /* verify first half segment */
  1427. EXPECT(txcounters.tx_packets != NULL);
  1428. if (txcounters.tx_packets != NULL) {
  1429. u8_t sent[TCP_MSS / 2];
  1430. u16_t ret;
  1431. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 2, 40U);
  1432. EXPECT(ret == TCP_MSS / 2);
  1433. EXPECT(memcmp(sent, &tx_data[3 * TCP_MSS], TCP_MSS / 2) == 0);
  1434. }
  1435. if (txcounters.tx_packets != NULL) {
  1436. pbuf_free(txcounters.tx_packets);
  1437. txcounters.tx_packets = NULL;
  1438. }
  1439. memset(&txcounters, 0, sizeof(txcounters));
  1440. /* ACK the half segment, leave window at half segment */
  1441. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS / 2, TCP_ACK, TCP_MSS / 2);
  1442. txcounters.copy_tx_packets = 1;
  1443. test_tcp_input(p, &netif);
  1444. txcounters.copy_tx_packets = 0;
  1445. /* ensure remaining segment was sent */
  1446. EXPECT(txcounters.num_tx_calls == 1);
  1447. EXPECT(txcounters.num_tx_bytes == ((TCP_MSS / 4) + 40U));
  1448. EXPECT(pcb->unsent == NULL);
  1449. EXPECT(pcb->unacked != NULL);
  1450. EXPECT(pcb->unacked->len == TCP_MSS / 4);
  1451. EXPECT(pcb->snd_wnd == TCP_MSS / 2);
  1452. /* verify remainder segment */
  1453. EXPECT(txcounters.tx_packets != NULL);
  1454. if (txcounters.tx_packets != NULL) {
  1455. u8_t sent[TCP_MSS / 4];
  1456. u16_t ret;
  1457. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 4, 40U);
  1458. EXPECT(ret == TCP_MSS / 4);
  1459. EXPECT(memcmp(sent, &tx_data[(3 * TCP_MSS) + TCP_MSS / 2], TCP_MSS / 4) == 0);
  1460. }
  1461. if (txcounters.tx_packets != NULL) {
  1462. pbuf_free(txcounters.tx_packets);
  1463. txcounters.tx_packets = NULL;
  1464. }
  1465. /* ensure no errors have been recorded */
  1466. EXPECT(counters.err_calls == 0);
  1467. EXPECT(counters.last_err == ERR_OK);
  1468. /* make sure the pcb is freed */
  1469. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  1470. tcp_abort(pcb);
  1471. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1472. }
  1473. END_TEST
  1474. /** Create the suite including all tests for this module */
  1475. Suite *
  1476. tcp_suite(void)
  1477. {
  1478. testfunc tests[] = {
  1479. TESTFUNC(test_tcp_new_abort),
  1480. TESTFUNC(test_tcp_listen_passive_open),
  1481. TESTFUNC(test_tcp_recv_inseq),
  1482. TESTFUNC(test_tcp_recv_inseq_trim),
  1483. TESTFUNC(test_tcp_passive_close),
  1484. TESTFUNC(test_tcp_active_abort),
  1485. TESTFUNC(test_tcp_malformed_header),
  1486. TESTFUNC(test_tcp_fast_retx_recover),
  1487. TESTFUNC(test_tcp_fast_rexmit_wraparound),
  1488. TESTFUNC(test_tcp_rto_rexmit_wraparound),
  1489. TESTFUNC(test_tcp_tx_full_window_lost_from_unacked),
  1490. TESTFUNC(test_tcp_tx_full_window_lost_from_unsent),
  1491. TESTFUNC(test_tcp_retx_add_to_sent),
  1492. TESTFUNC(test_tcp_rto_tracking),
  1493. TESTFUNC(test_tcp_rto_timeout),
  1494. TESTFUNC(test_tcp_rto_timeout_link_down),
  1495. TESTFUNC(test_tcp_rto_timeout_syn_sent),
  1496. TESTFUNC(test_tcp_rto_timeout_syn_sent_link_down),
  1497. TESTFUNC(test_tcp_zwp_timeout),
  1498. TESTFUNC(test_tcp_zwp_timeout_link_down),
  1499. TESTFUNC(test_tcp_persist_split)
  1500. };
  1501. return create_suite("TCP", tests, sizeof(tests)/sizeof(testfunc), tcp_setup, tcp_teardown);
  1502. }