test_tcp.c
20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#include "test_tcp.h"
#include "lwip/tcp_impl.h"
#include "lwip/stats.h"
#include "tcp_helper.h"
#ifdef _MSC_VER
#pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
#endif
#if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
#error "This tests needs TCP- and MEMP-statistics enabled"
#endif
#if TCP_SND_BUF <= TCP_WND
#error "This tests needs TCP_SND_BUF to be > TCP_WND"
#endif
static u8_t test_tcp_timer;
/* our own version of tcp_tmr so we can reset fast/slow timer state */
static void
test_tcp_tmr(void)
{
tcp_fasttmr();
if (++test_tcp_timer & 1) {
tcp_slowtmr();
}
}
/* Setups/teardown functions */
static void
tcp_setup(void)
{
/* reset iss to default (6510) */
tcp_ticks = 0;
tcp_ticks = 0 - (tcp_next_iss() - 6510);
tcp_next_iss();
tcp_ticks = 0;
test_tcp_timer = 0;
tcp_remove_all();
}
static void
tcp_teardown(void)
{
netif_list = NULL;
tcp_remove_all();
}
/* Test functions */
/** Call tcp_new() and tcp_abort() and test memp stats */
START_TEST(test_tcp_new_abort)
{
struct tcp_pcb* pcb;
LWIP_UNUSED_ARG(_i);
fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
pcb = tcp_new();
fail_unless(pcb != NULL);
if (pcb != NULL) {
fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
tcp_abort(pcb);
fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
}
}
END_TEST
/** Create an ESTABLISHED pcb and check if receive callback is called */
START_TEST(test_tcp_recv_inseq)
{
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf* p;
char data[] = {1, 2, 3, 4};
ip_addr_t remote_ip, local_ip;
u16_t data_len;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
LWIP_UNUSED_ARG(_i);
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
data_len = sizeof(data);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = data_len;
counters.expected_data = data;
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
/* create a segment */
p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
EXPECT(p != NULL);
if (p != NULL) {
/* pass the segment to tcp_input */
test_tcp_input(p, &netif);
/* check if counters are as expected */
EXPECT(counters.close_calls == 0);
EXPECT(counters.recv_calls == 1);
EXPECT(counters.recved_bytes == data_len);
EXPECT(counters.err_calls == 0);
}
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
}
END_TEST
/** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
* At the end, send more data. */
START_TEST(test_tcp_fast_retx_recover)
{
struct netif netif;
struct test_tcp_txcounters txcounters;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf* p;
char data1[] = { 1, 2, 3, 4};
char data2[] = { 5, 6, 7, 8};
char data3[] = { 9, 10, 11, 12};
char data4[] = {13, 14, 15, 16};
char data5[] = {17, 18, 19, 20};
char data6[] = {21, 22, 23, 24};
ip_addr_t remote_ip, local_ip, netmask;
u16_t remote_port = 0x100, local_port = 0x101;
err_t err;
LWIP_UNUSED_ARG(_i);
/* initialize local vars */
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
IP4_ADDR(&netmask, 255, 255, 255, 0);
test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
memset(&counters, 0, sizeof(counters));
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
pcb->mss = TCP_MSS;
/* disable initial congestion window (we don't send a SYN here...) */
pcb->cwnd = pcb->snd_wnd;
/* send data1 */
err = tcp_write(pcb, data1, sizeof(data1), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(txcounters.num_tx_calls == 1);
EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
memset(&txcounters, 0, sizeof(txcounters));
/* "recv" ACK for data1 */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &netif);
EXPECT_RET(txcounters.num_tx_calls == 0);
EXPECT_RET(pcb->unacked == NULL);
/* send data2 */
err = tcp_write(pcb, data2, sizeof(data2), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(txcounters.num_tx_calls == 1);
EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
memset(&txcounters, 0, sizeof(txcounters));
/* duplicate ACK for data1 (data2 is lost) */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &netif);
EXPECT_RET(txcounters.num_tx_calls == 0);
EXPECT_RET(pcb->dupacks == 1);
/* send data3 */
err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/* nagle enabled, no tx calls */
EXPECT_RET(txcounters.num_tx_calls == 0);
EXPECT_RET(txcounters.num_tx_bytes == 0);
memset(&txcounters, 0, sizeof(txcounters));
/* 2nd duplicate ACK for data1 (data2 and data3 are lost) */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &netif);
EXPECT_RET(txcounters.num_tx_calls == 0);
EXPECT_RET(pcb->dupacks == 2);
/* queue data4, don't send it (unsent-oversize is != 0) */
err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
/* 3nd duplicate ACK for data1 (data2 and data3 are lost) -> fast retransmission */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &netif);
/*EXPECT_RET(txcounters.num_tx_calls == 1);*/
EXPECT_RET(pcb->dupacks == 3);
memset(&txcounters, 0, sizeof(txcounters));
/* TODO: check expected data?*/
/* send data5, not output yet */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
/*err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);*/
EXPECT_RET(txcounters.num_tx_calls == 0);
EXPECT_RET(txcounters.num_tx_bytes == 0);
memset(&txcounters, 0, sizeof(txcounters));
{
int i = 0;
do
{
err = tcp_write(pcb, data6, TCP_MSS, TCP_WRITE_FLAG_COPY);
i++;
}while(err == ERR_OK);
EXPECT_RET(err != ERR_OK);
}
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/*EXPECT_RET(txcounters.num_tx_calls == 0);
EXPECT_RET(txcounters.num_tx_bytes == 0);*/
memset(&txcounters, 0, sizeof(txcounters));
/* send even more data */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/* ...and even more data */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/* ...and even more data */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/* ...and even more data */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/* send ACKs for data2 and data3 */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 12, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &netif);
/*EXPECT_RET(txcounters.num_tx_calls == 0);*/
/* ...and even more data */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
/* ...and even more data */
err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
#if 0
/* create expected segment */
p1 = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
EXPECT_RET(p != NULL);
if (p != NULL) {
/* pass the segment to tcp_input */
test_tcp_input(p, &netif);
/* check if counters are as expected */
EXPECT_RET(counters.close_calls == 0);
EXPECT_RET(counters.recv_calls == 1);
EXPECT_RET(counters.recved_bytes == data_len);
EXPECT_RET(counters.err_calls == 0);
}
#endif
/* make sure the pcb is freed */
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
tcp_abort(pcb);
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
}
END_TEST
static u8_t tx_data[TCP_WND*2];
static void
check_seqnos(struct tcp_seg *segs, int num_expected, u32_t *seqnos_expected)
{
struct tcp_seg *s = segs;
int i;
for (i = 0; i < num_expected; i++, s = s->next) {
EXPECT_RET(s != NULL);
EXPECT(s->tcphdr->seqno == htonl(seqnos_expected[i]));
}
EXPECT(s == NULL);
}
/** Send data with sequence numbers that wrap around the u32_t range.
* Then, provoke fast retransmission by duplicate ACKs and check that all
* segment lists are still properly sorted. */
START_TEST(test_tcp_fast_rexmit_wraparound)
{
struct netif netif;
struct test_tcp_txcounters txcounters;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf* p;
ip_addr_t remote_ip, local_ip, netmask;
u16_t remote_port = 0x100, local_port = 0x101;
err_t err;
#define SEQNO1 (0xFFFFFF00 - TCP_MSS)
#define ISS 6510
u16_t i, sent_total = 0;
u32_t seqnos[] = {
SEQNO1,
SEQNO1 + (1 * TCP_MSS),
SEQNO1 + (2 * TCP_MSS),
SEQNO1 + (3 * TCP_MSS),
SEQNO1 + (4 * TCP_MSS),
SEQNO1 + (5 * TCP_MSS)};
LWIP_UNUSED_ARG(_i);
for (i = 0; i < sizeof(tx_data); i++) {
tx_data[i] = (u8_t)i;
}
/* initialize local vars */
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
IP4_ADDR(&netmask, 255, 255, 255, 0);
test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
memset(&counters, 0, sizeof(counters));
/* create and initialize the pcb */
tcp_ticks = SEQNO1 - ISS;
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
EXPECT(pcb->lastack == SEQNO1);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
pcb->mss = TCP_MSS;
/* disable initial congestion window (we don't send a SYN here...) */
pcb->cwnd = 2*TCP_MSS;
/* send 6 mss-sized segments */
for (i = 0; i < 6; i++) {
err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
sent_total += TCP_MSS;
}
check_seqnos(pcb->unsent, 6, seqnos);
EXPECT(pcb->unacked == NULL);
err = tcp_output(pcb);
EXPECT(txcounters.num_tx_calls == 2);
EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
memset(&txcounters, 0, sizeof(txcounters));
check_seqnos(pcb->unacked, 2, seqnos);
check_seqnos(pcb->unsent, 4, &seqnos[2]);
/* ACK the first segment */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
test_tcp_input(p, &netif);
/* ensure this didn't trigger a retransmission */
EXPECT(txcounters.num_tx_calls == 1);
EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
memset(&txcounters, 0, sizeof(txcounters));
check_seqnos(pcb->unacked, 2, &seqnos[1]);
check_seqnos(pcb->unsent, 3, &seqnos[3]);
/* 3 dupacks */
EXPECT(pcb->dupacks == 0);
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
test_tcp_input(p, &netif);
EXPECT(txcounters.num_tx_calls == 0);
EXPECT(pcb->dupacks == 1);
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
test_tcp_input(p, &netif);
EXPECT(txcounters.num_tx_calls == 0);
EXPECT(pcb->dupacks == 2);
/* 3rd dupack -> fast rexmit */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
test_tcp_input(p, &netif);
EXPECT(pcb->dupacks == 3);
EXPECT(txcounters.num_tx_calls == 4);
memset(&txcounters, 0, sizeof(txcounters));
EXPECT(pcb->unsent == NULL);
check_seqnos(pcb->unacked, 5, &seqnos[1]);
/* make sure the pcb is freed */
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
tcp_abort(pcb);
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
}
END_TEST
/** Send data with sequence numbers that wrap around the u32_t range.
* Then, provoke RTO retransmission and check that all
* segment lists are still properly sorted. */
START_TEST(test_tcp_rto_rexmit_wraparound)
{
struct netif netif;
struct test_tcp_txcounters txcounters;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
ip_addr_t remote_ip, local_ip, netmask;
u16_t remote_port = 0x100, local_port = 0x101;
err_t err;
#define SEQNO1 (0xFFFFFF00 - TCP_MSS)
#define ISS 6510
u16_t i, sent_total = 0;
u32_t seqnos[] = {
SEQNO1,
SEQNO1 + (1 * TCP_MSS),
SEQNO1 + (2 * TCP_MSS),
SEQNO1 + (3 * TCP_MSS),
SEQNO1 + (4 * TCP_MSS),
SEQNO1 + (5 * TCP_MSS)};
LWIP_UNUSED_ARG(_i);
for (i = 0; i < sizeof(tx_data); i++) {
tx_data[i] = (u8_t)i;
}
/* initialize local vars */
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
IP4_ADDR(&netmask, 255, 255, 255, 0);
test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
memset(&counters, 0, sizeof(counters));
/* create and initialize the pcb */
tcp_ticks = 0;
tcp_ticks = 0 - tcp_next_iss();
tcp_ticks = SEQNO1 - tcp_next_iss();
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
EXPECT(pcb->lastack == SEQNO1);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
pcb->mss = TCP_MSS;
/* disable initial congestion window (we don't send a SYN here...) */
pcb->cwnd = 2*TCP_MSS;
/* send 6 mss-sized segments */
for (i = 0; i < 6; i++) {
err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
sent_total += TCP_MSS;
}
check_seqnos(pcb->unsent, 6, seqnos);
EXPECT(pcb->unacked == NULL);
err = tcp_output(pcb);
EXPECT(txcounters.num_tx_calls == 2);
EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
memset(&txcounters, 0, sizeof(txcounters));
check_seqnos(pcb->unacked, 2, seqnos);
check_seqnos(pcb->unsent, 4, &seqnos[2]);
/* call the tcp timer some times */
for (i = 0; i < 10; i++) {
test_tcp_tmr();
EXPECT(txcounters.num_tx_calls == 0);
}
/* 11th call to tcp_tmr: RTO rexmit fires */
test_tcp_tmr();
EXPECT(txcounters.num_tx_calls == 1);
check_seqnos(pcb->unacked, 1, seqnos);
check_seqnos(pcb->unsent, 5, &seqnos[1]);
/* fake greater cwnd */
pcb->cwnd = pcb->snd_wnd;
/* send more data */
err = tcp_output(pcb);
EXPECT(err == ERR_OK);
/* check queues are sorted */
EXPECT(pcb->unsent == NULL);
check_seqnos(pcb->unacked, 6, seqnos);
/* make sure the pcb is freed */
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
tcp_abort(pcb);
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
}
END_TEST
/** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
* At the end, send more data. */
static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
{
struct netif netif;
struct test_tcp_txcounters txcounters;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf *p;
ip_addr_t remote_ip, local_ip, netmask;
u16_t remote_port = 0x100, local_port = 0x101;
err_t err;
u16_t sent_total, i;
u8_t expected = 0xFE;
for (i = 0; i < sizeof(tx_data); i++) {
u8_t d = (u8_t)i;
if (d == 0xFE) {
d = 0xF0;
}
tx_data[i] = d;
}
if (zero_window_probe_from_unsent) {
tx_data[TCP_WND] = expected;
} else {
tx_data[0] = expected;
}
/* initialize local vars */
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
IP4_ADDR(&netmask, 255, 255, 255, 0);
test_tcp_init_netif(&netif, &txcounters, &local_ip, &netmask);
memset(&counters, 0, sizeof(counters));
memset(&txcounters, 0, sizeof(txcounters));
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
pcb->mss = TCP_MSS;
/* disable initial congestion window (we don't send a SYN here...) */
pcb->cwnd = pcb->snd_wnd;
/* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
sent_total = 0;
if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT(txcounters.num_tx_calls == 1);
EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
memset(&txcounters, 0, sizeof(txcounters));
sent_total += initial_data_len;
}
for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT(txcounters.num_tx_calls == 1);
EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
memset(&txcounters, 0, sizeof(txcounters));
}
EXPECT(sent_total == (TCP_WND - TCP_MSS));
/* now ACK the packet before the first */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
test_tcp_input(p, &netif);
/* ensure this didn't trigger a retransmission */
EXPECT(txcounters.num_tx_calls == 0);
EXPECT(txcounters.num_tx_bytes == 0);
EXPECT(pcb->persist_backoff == 0);
/* send the last packet, now a complete window has been sent */
err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
sent_total += TCP_MSS;
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT(txcounters.num_tx_calls == 1);
EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
memset(&txcounters, 0, sizeof(txcounters));
EXPECT(pcb->persist_backoff == 0);
if (zero_window_probe_from_unsent) {
/* ACK all data but close the TX window */
p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
test_tcp_input(p, &netif);
/* ensure this didn't trigger any transmission */
EXPECT(txcounters.num_tx_calls == 0);
EXPECT(txcounters.num_tx_bytes == 0);
EXPECT(pcb->persist_backoff == 1);
}
/* send one byte more (out of window) -> persist timer starts */
err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
EXPECT_RET(err == ERR_OK);
err = tcp_output(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT(txcounters.num_tx_calls == 0);
EXPECT(txcounters.num_tx_bytes == 0);
memset(&txcounters, 0, sizeof(txcounters));
if (!zero_window_probe_from_unsent) {
/* no persist timer unless a zero window announcement has been received */
EXPECT(pcb->persist_backoff == 0);
} else {
EXPECT(pcb->persist_backoff == 1);
/* call tcp_timer some more times to let persist timer count up */
for (i = 0; i < 4; i++) {
test_tcp_tmr();
EXPECT(txcounters.num_tx_calls == 0);
EXPECT(txcounters.num_tx_bytes == 0);
}
/* this should trigger the zero-window-probe */
txcounters.copy_tx_packets = 1;
test_tcp_tmr();
txcounters.copy_tx_packets = 0;
EXPECT(txcounters.num_tx_calls == 1);
EXPECT(txcounters.num_tx_bytes == 1 + 40U);
EXPECT(txcounters.tx_packets != NULL);
if (txcounters.tx_packets != NULL) {
u8_t sent;
u16_t ret;
ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
EXPECT(ret == 1);
EXPECT(sent == expected);
}
if (txcounters.tx_packets != NULL) {
pbuf_free(txcounters.tx_packets);
txcounters.tx_packets = NULL;
}
}
/* make sure the pcb is freed */
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
tcp_abort(pcb);
EXPECT_RET(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
}
START_TEST(test_tcp_tx_full_window_lost_from_unsent)
{
LWIP_UNUSED_ARG(_i);
test_tcp_tx_full_window_lost(1);
}
END_TEST
START_TEST(test_tcp_tx_full_window_lost_from_unacked)
{
LWIP_UNUSED_ARG(_i);
test_tcp_tx_full_window_lost(0);
}
END_TEST
/** Create the suite including all tests for this module */
Suite *
tcp_suite(void)
{
TFun tests[] = {
test_tcp_new_abort,
test_tcp_recv_inseq,
test_tcp_fast_retx_recover,
test_tcp_fast_rexmit_wraparound,
test_tcp_rto_rexmit_wraparound,
test_tcp_tx_full_window_lost_from_unacked,
test_tcp_tx_full_window_lost_from_unsent
};
return create_suite("TCP", tests, sizeof(tests)/sizeof(TFun), tcp_setup, tcp_teardown);
}