test_tcp.c 53 KB

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