test_tcp.c 52 KB

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