test_tcp.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. /* 3rd 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_RET(err == ERR_OK);
  589. EXPECT(txcounters.num_tx_calls == 2);
  590. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  591. memset(&txcounters, 0, sizeof(txcounters));
  592. check_seqnos(pcb->unacked, 2, seqnos);
  593. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  594. /* ACK the first segment */
  595. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  596. test_tcp_input(p, &netif);
  597. /* ensure this didn't trigger a retransmission. Only one
  598. segment should be transmitted because cwnd opened up by
  599. TCP_MSS and a fraction since we are in congestion avoidance */
  600. EXPECT(txcounters.num_tx_calls == 1);
  601. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  602. memset(&txcounters, 0, sizeof(txcounters));
  603. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  604. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  605. /* 3 dupacks */
  606. EXPECT(pcb->dupacks == 0);
  607. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  608. test_tcp_input(p, &netif);
  609. EXPECT(txcounters.num_tx_calls == 0);
  610. EXPECT(pcb->dupacks == 1);
  611. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  612. test_tcp_input(p, &netif);
  613. EXPECT(txcounters.num_tx_calls == 0);
  614. EXPECT(pcb->dupacks == 2);
  615. /* 3rd dupack -> fast rexmit */
  616. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  617. test_tcp_input(p, &netif);
  618. EXPECT(pcb->dupacks == 3);
  619. EXPECT(txcounters.num_tx_calls == 4);
  620. memset(&txcounters, 0, sizeof(txcounters));
  621. EXPECT(pcb->unsent == NULL);
  622. check_seqnos(pcb->unacked, 5, &seqnos[1]);
  623. /* make sure the pcb is freed */
  624. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  625. tcp_abort(pcb);
  626. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  627. }
  628. END_TEST
  629. /** Send data with sequence numbers that wrap around the u32_t range.
  630. * Then, provoke RTO retransmission and check that all
  631. * segment lists are still properly sorted. */
  632. START_TEST(test_tcp_rto_rexmit_wraparound)
  633. {
  634. struct netif netif;
  635. struct test_tcp_txcounters txcounters;
  636. struct test_tcp_counters counters;
  637. struct tcp_pcb* pcb;
  638. struct tcp_pcb dummy_pcb_for_iss; /* we need this for tcp_next_iss() only */
  639. err_t err;
  640. size_t i;
  641. u16_t sent_total = 0;
  642. LWIP_UNUSED_ARG(_i);
  643. for (i = 0; i < sizeof(tx_data); i++) {
  644. tx_data[i] = (u8_t)i;
  645. }
  646. /* initialize local vars */
  647. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  648. memset(&counters, 0, sizeof(counters));
  649. /* create and initialize the pcb */
  650. tcp_ticks = 0;
  651. tcp_ticks = 0 - tcp_next_iss(&dummy_pcb_for_iss);
  652. tcp_ticks = SEQNO1 - tcp_next_iss(&dummy_pcb_for_iss);
  653. pcb = test_tcp_new_counters_pcb(&counters);
  654. EXPECT_RET(pcb != NULL);
  655. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  656. pcb->mss = TCP_MSS;
  657. /* disable initial congestion window (we don't send a SYN here...) */
  658. pcb->cwnd = 2*TCP_MSS;
  659. /* send 6 mss-sized segments */
  660. for (i = 0; i < 6; i++) {
  661. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  662. EXPECT_RET(err == ERR_OK);
  663. sent_total += TCP_MSS;
  664. }
  665. check_seqnos(pcb->unsent, 6, seqnos);
  666. EXPECT(pcb->unacked == NULL);
  667. err = tcp_output(pcb);
  668. EXPECT_RET(err == ERR_OK);
  669. EXPECT(txcounters.num_tx_calls == 2);
  670. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  671. memset(&txcounters, 0, sizeof(txcounters));
  672. check_seqnos(pcb->unacked, 2, seqnos);
  673. check_seqnos(pcb->unsent, 4, &seqnos[2]);
  674. /* call the tcp timer some times */
  675. for (i = 0; i < 10; i++) {
  676. test_tcp_tmr();
  677. EXPECT(txcounters.num_tx_calls == 0);
  678. }
  679. /* 11th call to tcp_tmr: RTO rexmit fires */
  680. test_tcp_tmr();
  681. EXPECT(txcounters.num_tx_calls == 1);
  682. check_seqnos(pcb->unacked, 1, seqnos);
  683. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  684. /* fake greater cwnd */
  685. pcb->cwnd = pcb->snd_wnd;
  686. /* send more data */
  687. err = tcp_output(pcb);
  688. EXPECT(err == ERR_OK);
  689. /* check queues are sorted */
  690. EXPECT(pcb->unsent == NULL);
  691. check_seqnos(pcb->unacked, 6, seqnos);
  692. /* make sure the pcb is freed */
  693. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  694. tcp_abort(pcb);
  695. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  696. }
  697. END_TEST
  698. /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
  699. * At the end, send more data. */
  700. static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
  701. {
  702. struct netif netif;
  703. struct test_tcp_txcounters txcounters;
  704. struct test_tcp_counters counters;
  705. struct tcp_pcb* pcb;
  706. struct pbuf *p;
  707. err_t err;
  708. size_t i;
  709. u16_t sent_total;
  710. u8_t expected = 0xFE;
  711. for (i = 0; i < sizeof(tx_data); i++) {
  712. u8_t d = (u8_t)i;
  713. if (d == 0xFE) {
  714. d = 0xF0;
  715. }
  716. tx_data[i] = d;
  717. }
  718. if (zero_window_probe_from_unsent) {
  719. tx_data[TCP_WND] = expected;
  720. } else {
  721. tx_data[0] = expected;
  722. }
  723. /* initialize local vars */
  724. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  725. memset(&counters, 0, sizeof(counters));
  726. /* create and initialize the pcb */
  727. pcb = test_tcp_new_counters_pcb(&counters);
  728. EXPECT_RET(pcb != NULL);
  729. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  730. pcb->mss = TCP_MSS;
  731. /* disable initial congestion window (we don't send a SYN here...) */
  732. pcb->cwnd = pcb->snd_wnd;
  733. /* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
  734. sent_total = 0;
  735. if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
  736. u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
  737. err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
  738. EXPECT_RET(err == ERR_OK);
  739. err = tcp_output(pcb);
  740. EXPECT_RET(err == ERR_OK);
  741. EXPECT(txcounters.num_tx_calls == 1);
  742. EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
  743. memset(&txcounters, 0, sizeof(txcounters));
  744. sent_total += initial_data_len;
  745. }
  746. for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
  747. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  748. EXPECT_RET(err == ERR_OK);
  749. err = tcp_output(pcb);
  750. EXPECT_RET(err == ERR_OK);
  751. EXPECT(txcounters.num_tx_calls == 1);
  752. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  753. memset(&txcounters, 0, sizeof(txcounters));
  754. }
  755. EXPECT(sent_total == (TCP_WND - TCP_MSS));
  756. /* now ACK the packet before the first */
  757. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
  758. test_tcp_input(p, &netif);
  759. /* ensure this didn't trigger a retransmission */
  760. EXPECT(txcounters.num_tx_calls == 0);
  761. EXPECT(txcounters.num_tx_bytes == 0);
  762. EXPECT(pcb->persist_backoff == 0);
  763. /* send the last packet, now a complete window has been sent */
  764. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  765. sent_total += TCP_MSS;
  766. EXPECT_RET(err == ERR_OK);
  767. err = tcp_output(pcb);
  768. EXPECT_RET(err == ERR_OK);
  769. EXPECT(txcounters.num_tx_calls == 1);
  770. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  771. memset(&txcounters, 0, sizeof(txcounters));
  772. EXPECT(pcb->persist_backoff == 0);
  773. if (zero_window_probe_from_unsent) {
  774. /* ACK all data but close the TX window */
  775. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
  776. test_tcp_input(p, &netif);
  777. /* ensure this didn't trigger any transmission */
  778. EXPECT(txcounters.num_tx_calls == 0);
  779. EXPECT(txcounters.num_tx_bytes == 0);
  780. /* window is completely full, but persist timer is off since send buffer is empty */
  781. EXPECT(pcb->snd_wnd == 0);
  782. EXPECT(pcb->persist_backoff == 0);
  783. }
  784. /* send one byte more (out of window) -> persist timer starts */
  785. err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
  786. EXPECT_RET(err == ERR_OK);
  787. err = tcp_output(pcb);
  788. EXPECT_RET(err == ERR_OK);
  789. EXPECT(txcounters.num_tx_calls == 0);
  790. EXPECT(txcounters.num_tx_bytes == 0);
  791. memset(&txcounters, 0, sizeof(txcounters));
  792. if (!zero_window_probe_from_unsent) {
  793. /* no persist timer unless a zero window announcement has been received */
  794. EXPECT(pcb->persist_backoff == 0);
  795. } else {
  796. EXPECT(pcb->persist_backoff == 1);
  797. /* call tcp_timer some more times to let persist timer count up */
  798. for (i = 0; i < 4; i++) {
  799. test_tcp_tmr();
  800. EXPECT(txcounters.num_tx_calls == 0);
  801. EXPECT(txcounters.num_tx_bytes == 0);
  802. }
  803. /* this should trigger the zero-window-probe */
  804. txcounters.copy_tx_packets = 1;
  805. test_tcp_tmr();
  806. txcounters.copy_tx_packets = 0;
  807. EXPECT(txcounters.num_tx_calls == 1);
  808. EXPECT(txcounters.num_tx_bytes == 1 + 40U);
  809. EXPECT(txcounters.tx_packets != NULL);
  810. if (txcounters.tx_packets != NULL) {
  811. u8_t sent;
  812. u16_t ret;
  813. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
  814. EXPECT(ret == 1);
  815. EXPECT(sent == expected);
  816. }
  817. if (txcounters.tx_packets != NULL) {
  818. pbuf_free(txcounters.tx_packets);
  819. txcounters.tx_packets = NULL;
  820. }
  821. }
  822. /* make sure the pcb is freed */
  823. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  824. tcp_abort(pcb);
  825. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  826. }
  827. START_TEST(test_tcp_tx_full_window_lost_from_unsent)
  828. {
  829. LWIP_UNUSED_ARG(_i);
  830. test_tcp_tx_full_window_lost(1);
  831. }
  832. END_TEST
  833. START_TEST(test_tcp_tx_full_window_lost_from_unacked)
  834. {
  835. LWIP_UNUSED_ARG(_i);
  836. test_tcp_tx_full_window_lost(0);
  837. }
  838. END_TEST
  839. /** Send data, provoke retransmission and then add data to a segment
  840. * that already has been sent before. */
  841. START_TEST(test_tcp_retx_add_to_sent)
  842. {
  843. struct netif netif;
  844. struct test_tcp_txcounters txcounters;
  845. struct test_tcp_counters counters;
  846. struct tcp_pcb* pcb;
  847. struct pbuf* p;
  848. char data1a[] = { 1, 2, 3};
  849. char data1b[] = { 4};
  850. char data2a[] = { 5, 6, 7, 8};
  851. char data2b[] = { 5, 6, 7};
  852. char data3[] = { 9, 10, 11, 12, 12};
  853. char data4[] = { 13, 14, 15, 16,17};
  854. err_t err;
  855. int i;
  856. LWIP_UNUSED_ARG(_i);
  857. /* initialize local vars */
  858. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  859. memset(&counters, 0, sizeof(counters));
  860. /* create and initialize the pcb */
  861. pcb = test_tcp_new_counters_pcb(&counters);
  862. EXPECT_RET(pcb != NULL);
  863. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  864. pcb->mss = TCP_MSS;
  865. /* disable initial congestion window (we don't send a SYN here...) */
  866. pcb->cwnd = pcb->snd_wnd;
  867. /* send data1 */
  868. err = tcp_write(pcb, data1a, sizeof(data1a), TCP_WRITE_FLAG_COPY);
  869. EXPECT_RET(err == ERR_OK);
  870. err = tcp_write(pcb, data1b, sizeof(data1b), TCP_WRITE_FLAG_COPY);
  871. EXPECT_RET(err == ERR_OK);
  872. err = tcp_output(pcb);
  873. EXPECT_RET(err == ERR_OK);
  874. EXPECT_RET(txcounters.num_tx_calls == 1);
  875. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1a) + sizeof(data1b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  876. memset(&txcounters, 0, sizeof(txcounters));
  877. /* "recv" ACK for data1 */
  878. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
  879. EXPECT_RET(p != NULL);
  880. test_tcp_input(p, &netif);
  881. EXPECT_RET(txcounters.num_tx_calls == 0);
  882. EXPECT_RET(pcb->unacked == NULL);
  883. /* send data2 */
  884. err = tcp_write(pcb, data2a, sizeof(data2a), TCP_WRITE_FLAG_COPY);
  885. EXPECT_RET(err == ERR_OK);
  886. err = tcp_write(pcb, data2b, sizeof(data2b), TCP_WRITE_FLAG_COPY);
  887. EXPECT_RET(err == ERR_OK);
  888. err = tcp_output(pcb);
  889. EXPECT_RET(err == ERR_OK);
  890. EXPECT_RET(txcounters.num_tx_calls == 1);
  891. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2a) + sizeof(data2b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  892. memset(&txcounters, 0, sizeof(txcounters));
  893. /* send data3 */
  894. err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
  895. EXPECT_RET(err == ERR_OK);
  896. err = tcp_output(pcb);
  897. EXPECT_RET(err == ERR_OK);
  898. EXPECT_RET(txcounters.num_tx_calls == 0);
  899. EXPECT_RET(txcounters.num_tx_bytes == 0);
  900. memset(&txcounters, 0, sizeof(txcounters));
  901. /* data3 not sent yet (nagle) */
  902. EXPECT_RET(pcb->unacked != NULL);
  903. EXPECT_RET(pcb->unsent != NULL);
  904. /* disable nagle for this test so data to sent segment can be added below... */
  905. tcp_nagle_disable(pcb);
  906. /* call the tcp timer some times */
  907. for (i = 0; i < 20; i++) {
  908. test_tcp_tmr();
  909. if (txcounters.num_tx_calls != 0) {
  910. break;
  911. }
  912. }
  913. /* data3 sent */
  914. EXPECT_RET(txcounters.num_tx_calls == 1);
  915. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data3) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  916. EXPECT_RET(pcb->unacked != NULL);
  917. EXPECT_RET(pcb->unsent == NULL);
  918. memset(&txcounters, 0, sizeof(txcounters));
  919. tcp_nagle_enable(pcb);
  920. /* call the tcp timer some times */
  921. for (i = 0; i < 20; i++) {
  922. test_tcp_tmr();
  923. if (txcounters.num_tx_calls != 0) {
  924. break;
  925. }
  926. }
  927. /* RTO: rexmit of data2 */
  928. EXPECT_RET(txcounters.num_tx_calls == 1);
  929. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2a) + sizeof(data2b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  930. EXPECT_RET(pcb->unacked != NULL);
  931. EXPECT_RET(pcb->unsent != NULL);
  932. memset(&txcounters, 0, sizeof(txcounters));
  933. /* send data4 */
  934. err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
  935. EXPECT_RET(err == ERR_OK);
  936. /* disable nagle for this test so data to transmit without further ACKs... */
  937. tcp_nagle_disable(pcb);
  938. err = tcp_output(pcb);
  939. EXPECT_RET(err == ERR_OK);
  940. /* nagle enabled, no tx calls */
  941. EXPECT_RET(txcounters.num_tx_calls == 1);
  942. EXPECT_RET(txcounters.num_tx_bytes == sizeof(data3) + sizeof(data4) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
  943. memset(&txcounters, 0, sizeof(txcounters));
  944. /* make sure the pcb is freed */
  945. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  946. tcp_abort(pcb);
  947. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  948. }
  949. END_TEST
  950. START_TEST(test_tcp_rto_tracking)
  951. {
  952. struct netif netif;
  953. struct test_tcp_txcounters txcounters;
  954. struct test_tcp_counters counters;
  955. struct tcp_pcb* pcb;
  956. struct pbuf* p;
  957. err_t err;
  958. size_t i;
  959. u16_t sent_total = 0;
  960. LWIP_UNUSED_ARG(_i);
  961. for (i = 0; i < sizeof(tx_data); i++) {
  962. tx_data[i] = (u8_t)i;
  963. }
  964. /* initialize local vars */
  965. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  966. memset(&counters, 0, sizeof(counters));
  967. /* create and initialize the pcb */
  968. tcp_ticks = SEQNO1 - ISS;
  969. pcb = test_tcp_new_counters_pcb(&counters);
  970. EXPECT_RET(pcb != NULL);
  971. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  972. pcb->mss = TCP_MSS;
  973. /* Set congestion window large enough to send all our segments */
  974. pcb->cwnd = 5*TCP_MSS;
  975. /* send 5 mss-sized segments */
  976. for (i = 0; i < 5; i++) {
  977. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  978. EXPECT_RET(err == ERR_OK);
  979. sent_total += TCP_MSS;
  980. }
  981. check_seqnos(pcb->unsent, 5, seqnos);
  982. EXPECT(pcb->unacked == NULL);
  983. err = tcp_output(pcb);
  984. EXPECT_RET(err == ERR_OK);
  985. EXPECT(txcounters.num_tx_calls == 5);
  986. EXPECT(txcounters.num_tx_bytes == 5 * (TCP_MSS + 40U));
  987. memset(&txcounters, 0, sizeof(txcounters));
  988. /* Check all 5 are in-flight */
  989. EXPECT(pcb->unsent == NULL);
  990. check_seqnos(pcb->unacked, 5, seqnos);
  991. /* Force us into retransmisson timeout */
  992. while (!(pcb->flags & TF_RTO)) {
  993. test_tcp_tmr();
  994. }
  995. /* Ensure 4 remaining segments are back on unsent, ready for retransmission */
  996. check_seqnos(pcb->unsent, 4, &seqnos[1]);
  997. /* Ensure 1st segment is on unacked (already retransmitted) */
  998. check_seqnos(pcb->unacked, 1, seqnos);
  999. EXPECT(txcounters.num_tx_calls == 1);
  1000. EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
  1001. memset(&txcounters, 0, sizeof(txcounters));
  1002. /* Ensure rto_end points to next byte */
  1003. EXPECT(pcb->rto_end == seqnos[5]);
  1004. EXPECT(pcb->rto_end == pcb->snd_nxt);
  1005. /* Check cwnd was reset */
  1006. EXPECT(pcb->cwnd == pcb->mss);
  1007. /* Add another segment to send buffer which is outside of RTO */
  1008. err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1009. EXPECT_RET(err == ERR_OK);
  1010. sent_total += TCP_MSS;
  1011. check_seqnos(pcb->unsent, 5, &seqnos[1]);
  1012. /* Ensure no new data was sent */
  1013. EXPECT(txcounters.num_tx_calls == 0);
  1014. EXPECT(txcounters.num_tx_bytes == 0);
  1015. EXPECT(pcb->rto_end == pcb->snd_nxt);
  1016. /* ACK first segment */
  1017. p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
  1018. test_tcp_input(p, &netif);
  1019. /* Next two retranmissions should go out, due to cwnd in slow start */
  1020. EXPECT(txcounters.num_tx_calls == 2);
  1021. EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
  1022. memset(&txcounters, 0, sizeof(txcounters));
  1023. check_seqnos(pcb->unacked, 2, &seqnos[1]);
  1024. check_seqnos(pcb->unsent, 3, &seqnos[3]);
  1025. /* RTO should still be marked */
  1026. EXPECT(pcb->flags & TF_RTO);
  1027. /* cwnd should have only grown by 1 MSS */
  1028. EXPECT(pcb->cwnd == (tcpwnd_size_t)(2 * pcb->mss));
  1029. /* Ensure no new data was sent */
  1030. EXPECT(pcb->rto_end == pcb->snd_nxt);
  1031. /* ACK the next two segments */
  1032. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
  1033. test_tcp_input(p, &netif);
  1034. /* Final 2 retransmissions and 1 new data should go out */
  1035. EXPECT(txcounters.num_tx_calls == 3);
  1036. EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
  1037. memset(&txcounters, 0, sizeof(txcounters));
  1038. check_seqnos(pcb->unacked, 3, &seqnos[3]);
  1039. EXPECT(pcb->unsent == NULL);
  1040. /* RTO should still be marked */
  1041. EXPECT(pcb->flags & TF_RTO);
  1042. /* cwnd should have only grown by 1 MSS */
  1043. EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
  1044. /* snd_nxt should have been advanced past rto_end */
  1045. EXPECT(TCP_SEQ_GT(pcb->snd_nxt, pcb->rto_end));
  1046. /* ACK the next two segments, finishing our RTO, leaving new segment unacked */
  1047. p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
  1048. test_tcp_input(p, &netif);
  1049. EXPECT(!(pcb->flags & TF_RTO));
  1050. check_seqnos(pcb->unacked, 1, &seqnos[5]);
  1051. /* We should be in ABC congestion avoidance, so no change in cwnd */
  1052. EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
  1053. EXPECT(pcb->cwnd >= pcb->ssthresh);
  1054. /* Ensure ABC congestion avoidance is tracking bytes acked */
  1055. EXPECT(pcb->bytes_acked == (tcpwnd_size_t)(2 * pcb->mss));
  1056. /* make sure the pcb is freed */
  1057. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  1058. tcp_abort(pcb);
  1059. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1060. }
  1061. END_TEST
  1062. static void test_tcp_rto_timeout_impl(int link_down)
  1063. {
  1064. struct netif netif;
  1065. struct test_tcp_txcounters txcounters;
  1066. struct test_tcp_counters counters;
  1067. struct tcp_pcb *pcb, *cur;
  1068. err_t err;
  1069. size_t i;
  1070. const size_t max_wait_ctr = 1024 * 1024;
  1071. /* Setup data for a single segment */
  1072. for (i = 0; i < TCP_MSS; i++) {
  1073. tx_data[i] = (u8_t)i;
  1074. }
  1075. /* initialize local vars */
  1076. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1077. memset(&counters, 0, sizeof(counters));
  1078. /* create and initialize the pcb */
  1079. tcp_ticks = SEQNO1 - ISS;
  1080. pcb = test_tcp_new_counters_pcb(&counters);
  1081. EXPECT_RET(pcb != NULL);
  1082. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1083. pcb->mss = TCP_MSS;
  1084. pcb->cwnd = TCP_MSS;
  1085. /* send our segment */
  1086. err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1087. EXPECT_RET(err == ERR_OK);
  1088. err = tcp_output(pcb);
  1089. EXPECT_RET(err == ERR_OK);
  1090. EXPECT(txcounters.num_tx_calls == 1);
  1091. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1092. memset(&txcounters, 0, sizeof(txcounters));
  1093. /* ensure no errors have been recorded */
  1094. EXPECT(counters.err_calls == 0);
  1095. EXPECT(counters.last_err == ERR_OK);
  1096. /* Force us into retransmisson timeout */
  1097. for (i = 0; !(pcb->flags & TF_RTO) && i < max_wait_ctr; i++) {
  1098. test_tcp_tmr();
  1099. }
  1100. EXPECT(i < max_wait_ctr);
  1101. /* check first rexmit */
  1102. EXPECT(pcb->nrtx == 1);
  1103. EXPECT(txcounters.num_tx_calls == 1);
  1104. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1105. /* still no error expected */
  1106. EXPECT(counters.err_calls == 0);
  1107. EXPECT(counters.last_err == ERR_OK);
  1108. if (link_down) {
  1109. netif_set_link_down(&netif);
  1110. }
  1111. /* keep running the timer till we hit our maximum RTO */
  1112. for (i = 0; counters.last_err == ERR_OK && i < max_wait_ctr; i++) {
  1113. test_tcp_tmr();
  1114. }
  1115. EXPECT(i < max_wait_ctr);
  1116. /* check number of retransmissions */
  1117. if (link_down) {
  1118. EXPECT(txcounters.num_tx_calls == 1);
  1119. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1120. } else {
  1121. EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
  1122. EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (TCP_MSS + 40U));
  1123. }
  1124. /* check the connection (pcb) has been aborted */
  1125. EXPECT(counters.err_calls == 1);
  1126. EXPECT(counters.last_err == ERR_ABRT);
  1127. /* check our pcb is no longer active */
  1128. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1129. EXPECT(cur != pcb);
  1130. }
  1131. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1132. }
  1133. START_TEST(test_tcp_rto_timeout)
  1134. {
  1135. LWIP_UNUSED_ARG(_i);
  1136. test_tcp_rto_timeout_impl(0);
  1137. }
  1138. END_TEST
  1139. START_TEST(test_tcp_rto_timeout_link_down)
  1140. {
  1141. LWIP_UNUSED_ARG(_i);
  1142. test_tcp_rto_timeout_impl(1);
  1143. }
  1144. END_TEST
  1145. static void test_tcp_rto_timeout_syn_sent_impl(int link_down)
  1146. {
  1147. struct netif netif;
  1148. struct test_tcp_txcounters txcounters;
  1149. struct test_tcp_counters counters;
  1150. struct tcp_pcb *pcb, *cur;
  1151. err_t err;
  1152. size_t i;
  1153. const size_t max_wait_ctr = 1024 * 1024;
  1154. 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);
  1155. /* Setup data for a single segment */
  1156. for (i = 0; i < TCP_MSS; i++) {
  1157. tx_data[i] = (u8_t)i;
  1158. }
  1159. /* initialize local vars */
  1160. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1161. memset(&counters, 0, sizeof(counters));
  1162. /* create and initialize the pcb */
  1163. tcp_ticks = SEQNO1 - ISS;
  1164. pcb = test_tcp_new_counters_pcb(&counters);
  1165. EXPECT_RET(pcb != NULL);
  1166. err = tcp_connect(pcb, &netif.gw, 123, NULL);
  1167. EXPECT_RET(err == ERR_OK);
  1168. EXPECT_RET(pcb->state == SYN_SENT);
  1169. EXPECT(txcounters.num_tx_calls == 1);
  1170. EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len);
  1171. /* ensure no errors have been recorded */
  1172. EXPECT(counters.err_calls == 0);
  1173. EXPECT(counters.last_err == ERR_OK);
  1174. txcounters.num_tx_calls = 0;
  1175. txcounters.num_tx_bytes = 0;
  1176. /* Force us into retransmisson timeout */
  1177. for (i = 0; !(pcb->flags & TF_RTO) && i < max_wait_ctr; i++) {
  1178. test_tcp_tmr();
  1179. }
  1180. EXPECT(i < max_wait_ctr);
  1181. /* check first rexmit */
  1182. EXPECT(pcb->nrtx == 1);
  1183. EXPECT(txcounters.num_tx_calls == 1);
  1184. EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len); /* 40: headers; >=: options */
  1185. /* still no error expected */
  1186. EXPECT(counters.err_calls == 0);
  1187. EXPECT(counters.last_err == ERR_OK);
  1188. if (link_down) {
  1189. /* set link down and check what happens to the RTO counter */
  1190. netif_set_link_down(&netif);
  1191. }
  1192. /* keep running the timer till we hit our maximum RTO */
  1193. for (i = 0; counters.last_err == ERR_OK && i < max_wait_ctr; i++) {
  1194. test_tcp_tmr();
  1195. }
  1196. EXPECT(i < max_wait_ctr);
  1197. /* check number of retransmissions */
  1198. if (link_down) {
  1199. EXPECT(txcounters.num_tx_calls == 1);
  1200. EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len);
  1201. } else {
  1202. EXPECT(txcounters.num_tx_calls == TCP_SYNMAXRTX);
  1203. EXPECT(txcounters.num_tx_bytes == TCP_SYNMAXRTX * (tcp_syn_opts_len + 40U));
  1204. }
  1205. /* check the connection (pcb) has been aborted */
  1206. EXPECT(counters.err_calls == 1);
  1207. EXPECT(counters.last_err == ERR_ABRT);
  1208. /* check our pcb is no longer active */
  1209. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1210. EXPECT(cur != pcb);
  1211. }
  1212. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1213. }
  1214. START_TEST(test_tcp_rto_timeout_syn_sent)
  1215. {
  1216. LWIP_UNUSED_ARG(_i);
  1217. test_tcp_rto_timeout_syn_sent_impl(0);
  1218. }
  1219. END_TEST
  1220. START_TEST(test_tcp_rto_timeout_syn_sent_link_down)
  1221. {
  1222. LWIP_UNUSED_ARG(_i);
  1223. test_tcp_rto_timeout_syn_sent_impl(1);
  1224. }
  1225. END_TEST
  1226. static void test_tcp_zwp_timeout_impl(int link_down)
  1227. {
  1228. struct netif netif;
  1229. struct test_tcp_txcounters txcounters;
  1230. struct test_tcp_counters counters;
  1231. struct tcp_pcb *pcb, *cur;
  1232. struct pbuf* p;
  1233. err_t err;
  1234. size_t i;
  1235. /* Setup data for two segments */
  1236. for (i = 0; i < 2*TCP_MSS; i++) {
  1237. tx_data[i] = (u8_t)i;
  1238. }
  1239. /* initialize local vars */
  1240. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1241. memset(&counters, 0, sizeof(counters));
  1242. /* create and initialize the pcb */
  1243. tcp_ticks = SEQNO1 - ISS;
  1244. pcb = test_tcp_new_counters_pcb(&counters);
  1245. EXPECT_RET(pcb != NULL);
  1246. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1247. pcb->mss = TCP_MSS;
  1248. pcb->cwnd = TCP_MSS;
  1249. /* send first segment */
  1250. err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1251. EXPECT(err == ERR_OK);
  1252. err = tcp_output(pcb);
  1253. EXPECT(err == ERR_OK);
  1254. /* verify segment is in-flight */
  1255. EXPECT(pcb->unsent == NULL);
  1256. check_seqnos(pcb->unacked, 1, seqnos);
  1257. EXPECT(txcounters.num_tx_calls == 1);
  1258. EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
  1259. memset(&txcounters, 0, sizeof(txcounters));
  1260. /* ACK the segment and close the TX window */
  1261. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK, 0);
  1262. test_tcp_input(p, &netif);
  1263. EXPECT(pcb->unacked == NULL);
  1264. EXPECT(pcb->unsent == NULL);
  1265. /* send buffer empty, persist should be off */
  1266. EXPECT(pcb->persist_backoff == 0);
  1267. EXPECT(pcb->snd_wnd == 0);
  1268. /* send second segment, should be buffered */
  1269. err = tcp_write(pcb, &tx_data[TCP_MSS], TCP_MSS, TCP_WRITE_FLAG_COPY);
  1270. EXPECT(err == ERR_OK);
  1271. err = tcp_output(pcb);
  1272. EXPECT(err == ERR_OK);
  1273. /* ensure it is buffered and persist timer started */
  1274. EXPECT(pcb->unacked == NULL);
  1275. check_seqnos(pcb->unsent, 1, &seqnos[1]);
  1276. EXPECT(txcounters.num_tx_calls == 0);
  1277. EXPECT(txcounters.num_tx_bytes == 0);
  1278. EXPECT(pcb->persist_backoff == 1);
  1279. /* ensure no errors have been recorded */
  1280. EXPECT(counters.err_calls == 0);
  1281. EXPECT(counters.last_err == ERR_OK);
  1282. /* run timer till first probe */
  1283. EXPECT(pcb->persist_probe == 0);
  1284. while (pcb->persist_probe == 0) {
  1285. test_tcp_tmr();
  1286. }
  1287. EXPECT(txcounters.num_tx_calls == 1);
  1288. EXPECT(txcounters.num_tx_bytes == (1 + 40U));
  1289. memset(&txcounters, 0, sizeof(txcounters));
  1290. /* respond to probe with remote's current SEQ, ACK, and zero-window */
  1291. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 0, TCP_ACK, 0);
  1292. test_tcp_input(p, &netif);
  1293. /* ensure zero-window is still active, but probe count reset */
  1294. EXPECT(pcb->persist_backoff > 1);
  1295. EXPECT(pcb->persist_probe == 0);
  1296. EXPECT(pcb->snd_wnd == 0);
  1297. /* ensure no errors have been recorded */
  1298. EXPECT(counters.err_calls == 0);
  1299. EXPECT(counters.last_err == ERR_OK);
  1300. if (link_down) {
  1301. netif_set_link_down(&netif);
  1302. }
  1303. /* now run the timer till we hit our maximum probe count */
  1304. while (counters.last_err == ERR_OK) {
  1305. test_tcp_tmr();
  1306. }
  1307. if (link_down) {
  1308. EXPECT(txcounters.num_tx_calls == 0);
  1309. EXPECT(txcounters.num_tx_bytes == 0);
  1310. } else {
  1311. /* check maximum number of 1 byte probes were sent */
  1312. EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
  1313. EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (1 + 40U));
  1314. }
  1315. /* check the connection (pcb) has been aborted */
  1316. EXPECT(counters.err_calls == 1);
  1317. EXPECT(counters.last_err == ERR_ABRT);
  1318. /* check our pcb is no longer active */
  1319. for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
  1320. EXPECT(cur != pcb);
  1321. }
  1322. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1323. }
  1324. START_TEST(test_tcp_zwp_timeout)
  1325. {
  1326. LWIP_UNUSED_ARG(_i);
  1327. test_tcp_zwp_timeout_impl(0);
  1328. }
  1329. END_TEST
  1330. START_TEST(test_tcp_zwp_timeout_link_down)
  1331. {
  1332. LWIP_UNUSED_ARG(_i);
  1333. test_tcp_zwp_timeout_impl(1);
  1334. }
  1335. END_TEST
  1336. START_TEST(test_tcp_persist_split)
  1337. {
  1338. struct netif netif;
  1339. struct test_tcp_txcounters txcounters;
  1340. struct test_tcp_counters counters;
  1341. struct tcp_pcb *pcb;
  1342. struct pbuf* p;
  1343. err_t err;
  1344. size_t i;
  1345. LWIP_UNUSED_ARG(_i);
  1346. /* Setup data for four segments */
  1347. for (i = 0; i < 4 * TCP_MSS; i++) {
  1348. tx_data[i] = (u8_t)i;
  1349. }
  1350. /* initialize local vars */
  1351. test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
  1352. memset(&counters, 0, sizeof(counters));
  1353. /* create and initialize the pcb */
  1354. tcp_ticks = SEQNO1 - ISS;
  1355. pcb = test_tcp_new_counters_pcb(&counters);
  1356. EXPECT_RET(pcb != NULL);
  1357. tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
  1358. pcb->mss = TCP_MSS;
  1359. /* set window to three segments */
  1360. pcb->cwnd = 3 * TCP_MSS;
  1361. pcb->snd_wnd = 3 * TCP_MSS;
  1362. pcb->snd_wnd_max = 3 * TCP_MSS;
  1363. /* send four segments. Fourth should stay buffered and is a 3/4 MSS segment to
  1364. get coverage on the oversized segment case */
  1365. err = tcp_write(pcb, &tx_data[0], (3 * TCP_MSS) + (TCP_MSS - (TCP_MSS / 4)), TCP_WRITE_FLAG_COPY);
  1366. EXPECT(err == ERR_OK);
  1367. err = tcp_output(pcb);
  1368. EXPECT(err == ERR_OK);
  1369. /* verify 3 segments are in-flight */
  1370. EXPECT(pcb->unacked != NULL);
  1371. check_seqnos(pcb->unacked, 3, seqnos);
  1372. EXPECT(txcounters.num_tx_calls == 3);
  1373. EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
  1374. memset(&txcounters, 0, sizeof(txcounters));
  1375. /* verify 4th segment is on unsent */
  1376. EXPECT(pcb->unsent != NULL);
  1377. EXPECT(pcb->unsent->len == TCP_MSS - (TCP_MSS / 4));
  1378. check_seqnos(pcb->unsent, 1, &seqnos[3]);
  1379. #if TCP_OVERSIZE
  1380. EXPECT(pcb->unsent_oversize == TCP_MSS / 4);
  1381. #if TCP_OVERSIZE_DBGCHECK
  1382. EXPECT(pcb->unsent->oversize_left == pcb->unsent_oversize);
  1383. #endif /* TCP_OVERSIZE_DBGCHECK */
  1384. #endif /* TCP_OVERSIZE */
  1385. /* ACK the 3 segments and update the window to only 1/2 TCP_MSS.
  1386. 4th segment should stay on unsent because it's bigger than 1/2 MSS */
  1387. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 3 * TCP_MSS, TCP_ACK, TCP_MSS / 2);
  1388. test_tcp_input(p, &netif);
  1389. EXPECT(pcb->unacked == NULL);
  1390. EXPECT(pcb->snd_wnd == TCP_MSS / 2);
  1391. EXPECT(pcb->unsent != NULL);
  1392. check_seqnos(pcb->unsent, 1, &seqnos[3]);
  1393. EXPECT(txcounters.num_tx_calls == 0);
  1394. EXPECT(txcounters.num_tx_bytes == 0);
  1395. /* persist timer should be started since 4th segment is stuck waiting on snd_wnd */
  1396. EXPECT(pcb->persist_backoff == 1);
  1397. /* ensure no errors have been recorded */
  1398. EXPECT(counters.err_calls == 0);
  1399. EXPECT(counters.last_err == ERR_OK);
  1400. /* call tcp_timer some more times to let persist timer count up */
  1401. for (i = 0; i < 4; i++) {
  1402. test_tcp_tmr();
  1403. EXPECT(txcounters.num_tx_calls == 0);
  1404. EXPECT(txcounters.num_tx_bytes == 0);
  1405. }
  1406. /* this should be the first timer shot, which should split the
  1407. * segment and send a runt (of the remaining window size) */
  1408. txcounters.copy_tx_packets = 1;
  1409. test_tcp_tmr();
  1410. txcounters.copy_tx_packets = 0;
  1411. /* persist will be disabled as RTO timer takes over */
  1412. EXPECT(pcb->persist_backoff == 0);
  1413. EXPECT(txcounters.num_tx_calls == 1);
  1414. EXPECT(txcounters.num_tx_bytes == ((TCP_MSS /2) + 40U));
  1415. /* verify 1/2 MSS segment sent, 1/4 MSS still buffered */
  1416. EXPECT(pcb->unsent != NULL);
  1417. EXPECT(pcb->unsent->len == TCP_MSS / 4);
  1418. EXPECT(pcb->unacked != NULL);
  1419. EXPECT(pcb->unacked->len == TCP_MSS / 2);
  1420. #if TCP_OVERSIZE
  1421. /* verify there is no oversized remaining since during the
  1422. segment split, the remainder pbuf is always the exact length */
  1423. EXPECT(pcb->unsent_oversize == 0);
  1424. #if TCP_OVERSIZE_DBGCHECK
  1425. /* Split segment already transmitted, should be at 0 */
  1426. EXPECT(pcb->unacked->oversize_left == 0);
  1427. /* Remainder segment should match pcb value (which is 0) */
  1428. EXPECT(pcb->unsent->oversize_left == pcb->unsent_oversize);
  1429. #endif /* TCP_OVERSIZE_DBGCHECK */
  1430. #endif /* TCP_OVERSIZE */
  1431. /* verify first half segment */
  1432. EXPECT(txcounters.tx_packets != NULL);
  1433. if (txcounters.tx_packets != NULL) {
  1434. u8_t sent[TCP_MSS / 2];
  1435. u16_t ret;
  1436. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 2, 40U);
  1437. EXPECT(ret == TCP_MSS / 2);
  1438. EXPECT(memcmp(sent, &tx_data[3 * TCP_MSS], TCP_MSS / 2) == 0);
  1439. }
  1440. if (txcounters.tx_packets != NULL) {
  1441. pbuf_free(txcounters.tx_packets);
  1442. txcounters.tx_packets = NULL;
  1443. }
  1444. memset(&txcounters, 0, sizeof(txcounters));
  1445. /* ACK the half segment, leave window at half segment */
  1446. p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS / 2, TCP_ACK, TCP_MSS / 2);
  1447. txcounters.copy_tx_packets = 1;
  1448. test_tcp_input(p, &netif);
  1449. txcounters.copy_tx_packets = 0;
  1450. /* ensure remaining segment was sent */
  1451. EXPECT(txcounters.num_tx_calls == 1);
  1452. EXPECT(txcounters.num_tx_bytes == ((TCP_MSS / 4) + 40U));
  1453. EXPECT(pcb->unsent == NULL);
  1454. EXPECT(pcb->unacked != NULL);
  1455. EXPECT(pcb->unacked->len == TCP_MSS / 4);
  1456. EXPECT(pcb->snd_wnd == TCP_MSS / 2);
  1457. /* verify remainder segment */
  1458. EXPECT(txcounters.tx_packets != NULL);
  1459. if (txcounters.tx_packets != NULL) {
  1460. u8_t sent[TCP_MSS / 4];
  1461. u16_t ret;
  1462. ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 4, 40U);
  1463. EXPECT(ret == TCP_MSS / 4);
  1464. EXPECT(memcmp(sent, &tx_data[(3 * TCP_MSS) + TCP_MSS / 2], TCP_MSS / 4) == 0);
  1465. }
  1466. if (txcounters.tx_packets != NULL) {
  1467. pbuf_free(txcounters.tx_packets);
  1468. txcounters.tx_packets = NULL;
  1469. }
  1470. /* ensure no errors have been recorded */
  1471. EXPECT(counters.err_calls == 0);
  1472. EXPECT(counters.last_err == ERR_OK);
  1473. /* make sure the pcb is freed */
  1474. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  1475. tcp_abort(pcb);
  1476. EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  1477. }
  1478. END_TEST
  1479. /** Create the suite including all tests for this module */
  1480. Suite *
  1481. tcp_suite(void)
  1482. {
  1483. testfunc tests[] = {
  1484. TESTFUNC(test_tcp_new_abort),
  1485. TESTFUNC(test_tcp_listen_passive_open),
  1486. TESTFUNC(test_tcp_recv_inseq),
  1487. TESTFUNC(test_tcp_recv_inseq_trim),
  1488. TESTFUNC(test_tcp_passive_close),
  1489. TESTFUNC(test_tcp_active_abort),
  1490. TESTFUNC(test_tcp_malformed_header),
  1491. TESTFUNC(test_tcp_fast_retx_recover),
  1492. TESTFUNC(test_tcp_fast_rexmit_wraparound),
  1493. TESTFUNC(test_tcp_rto_rexmit_wraparound),
  1494. TESTFUNC(test_tcp_tx_full_window_lost_from_unacked),
  1495. TESTFUNC(test_tcp_tx_full_window_lost_from_unsent),
  1496. TESTFUNC(test_tcp_retx_add_to_sent),
  1497. TESTFUNC(test_tcp_rto_tracking),
  1498. TESTFUNC(test_tcp_rto_timeout),
  1499. TESTFUNC(test_tcp_rto_timeout_link_down),
  1500. TESTFUNC(test_tcp_rto_timeout_syn_sent),
  1501. TESTFUNC(test_tcp_rto_timeout_syn_sent_link_down),
  1502. TESTFUNC(test_tcp_zwp_timeout),
  1503. TESTFUNC(test_tcp_zwp_timeout_link_down),
  1504. TESTFUNC(test_tcp_persist_split)
  1505. };
  1506. return create_suite("TCP", tests, sizeof(tests)/sizeof(testfunc), tcp_setup, tcp_teardown);
  1507. }