File: tcp-validation.cc

package info (click to toggle)
ns3 3.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 106,848 kB
  • sloc: cpp: 633,577; python: 15,491; ansic: 6,773; makefile: 1,959; sh: 1,021; pascal: 632; javascript: 167; perl: 102
file content (1100 lines) | stat: -rw-r--r-- 41,846 bytes parent folder | download
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
/*
 * Copyright (c) 2019 Cable Television Laboratories, Inc.
 * Copyright (c) 2020 Tom Henderson (adapted for DCTCP testing)
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

// This program is designed to observe long-running TCP congestion control
// behavior over a configurable bottleneck link.  The program is also
// instrumented to check program data against validated results, when
// the validation option is enabled.
//
// ---> downstream (primary data transfer from servers to clients)
// <--- upstream (return acks and ICMP echo response)
//
//              ----   bottleneck link    ----
//  servers ---| WR |--------------------| LR |--- clients
//              ----                      ----
//  ns-3 node IDs:
//  nodes 0-2    3                         4        5-7
//
// - The box WR is notionally a WAN router, aggregating all server links
// - The box LR is notionally a LAN router, aggregating all client links
// - Three servers are connected to WR, three clients are connected to LR
//
// clients and servers are configured for ICMP measurements and TCP throughput
// and latency measurements in the downstream direction
//
// All link rates are enforced by a point-to-point (P2P) ns-3 model with full
// duplex operation.  Dynamic queue limits
// (BQL) are enabled to allow for queueing to occur at the priority queue layer;
// the notional P2P hardware device queue is limited to three packets.
//
// One-way link delays and link rates
// -----------------------------------
// (1) server to WR links, 1000 Mbps, 1us delay
// (2) bottleneck link:  configurable rate, configurable delay
// (3) client to LR links, 1000 Mbps, 1us delay
//
// By default, ns-3 FQ-CoDel model is installed on all interfaces, but
// the bottleneck queue uses CoDel by default and is configurable.
//
// The ns-3 FQ-CoDel model uses ns-3 defaults:
// - 100ms interval
// - 5ms target
// - drop batch size of 64 packets
// - minbytes of 1500
//
// Default simulation time is 70 sec.  For single flow experiments, the flow is
// started at simulation time 5 sec; if a second flow is used, it starts
// at 15 sec.
//
// ping frequency is set at 100ms.
//
// A command-line option to enable a step-threshold CE threshold
// from the CoDel queue model is provided.
//
// Measure:
//  - ping RTT
//  - TCP RTT estimate
//  - TCP throughput
//
// IPv4 addressing
// ----------------------------
// pingServer       10.1.1.2 (ping source)
// firstServer      10.1.2.2 (data sender)
// secondServer     10.1.3.2 (data sender)
// pingClient       192.168.1.2
// firstClient      192.168.2.2
// secondClient     192.168.3.2
//
// Program Options:
// ---------------
//    --firstTcpType:   first TCP type (cubic, dctcp, or reno) [cubic]
//    --secondTcpType:  second TCP type (cubic, dctcp, or reno) []
//    --queueType:      bottleneck queue type (fq, codel, pie, or red) [codel]
//    --baseRtt:        base RTT [+80ms]
//    --ceThreshold:    CoDel CE threshold (for DCTCP) [+1ms]
//    --linkRate:       data rate of bottleneck link [50000000bps]
//    --stopTime:       simulation stop time [+1.16667min]
//    --queueUseEcn:    use ECN on queue [false]
//    --enablePcap:     enable Pcap [false]
//    --validate:       validation case to run []
//
// validation cases (and syntax of how to run):
// ------------
// Case 'dctcp-10ms':  DCTCP single flow, 10ms base RTT, 50 Mbps link, ECN enabled, CoDel:
//     ./ns3 run 'tcp-validation --firstTcpType=dctcp --linkRate=50Mbps --baseRtt=10ms
//     --queueUseEcn=1 --stopTime=15s --validate=1 --validation=dctcp-10ms'
//    - Throughput between 48 Mbps and 49 Mbps for time greater than 5.6s
//    - DCTCP alpha below 0.1 for time greater than 5.4s
//    - DCTCP alpha between 0.06 and 0.085 for time greater than 7s
//
// Case 'dctcp-80ms': DCTCP single flow, 80ms base RTT, 50 Mbps link, ECN enabled, CoDel:
//     ./ns3 run 'tcp-validation --firstTcpType=dctcp --linkRate=50Mbps --baseRtt=80ms
//     --queueUseEcn=1 --stopTime=40s --validate=1 --validation=dctcp-80ms'
//    - Throughput less than 20 Mbps for time less than 14s
//    - Throughput less than 48 Mbps for time less than 30s
//    - Throughput between 47.5 Mbps and 48.5 for time greater than 32s
//    - DCTCP alpha above 0.1 for time less than 7.5
//    - DCTCP alpha below 0.01 for time greater than 11 and less than 30
//    - DCTCP alpha between 0.015 and 0.025 for time greater than 34
//
// Case 'cubic-50ms-no-ecn': CUBIC single flow, 50ms base RTT, 50 Mbps link, ECN disabled, CoDel:
//     ./ns3 run 'tcp-validation --firstTcpType=cubic --linkRate=50Mbps --baseRtt=50ms
//     --queueUseEcn=0 --stopTime=20s --validate=1 --validation=cubic-50ms-no-ecn'
//    - Maximum value of cwnd is 511 segments at 5.4593 seconds
//    - cwnd decreases to 173 segments at 5.80304 seconds
//    - cwnd reaches another local maxima around 14.2815 seconds of 236 segments
//    - cwnd reaches a second maximum around 18.048 seconds of 234 segments
//
// Case 'cubic-50ms-ecn': CUBIC single flow, 50ms base RTT, 50 Mbps link, ECN enabled, CoDel:
//     ./ns3 run 'tcp-validation --firstTcpType=cubic --linkRate=50Mbps --baseRtt=50ms
//     --queueUseEcn=0 --stopTime=20s --validate=1 --validation=cubic-50ms-no-ecn'
//    - Maximum value of cwnd is 511 segments at 5.4593 seconds
//    - cwnd decreases to 173 segments at 5.7939 seconds
//    - cwnd reaches another local maxima around 14.3477 seconds of 236 segments
//    - cwnd reaches a second maximum around 18.064 seconds of 234 segments

#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/traffic-control-module.h"

#include <fstream>
#include <iostream>
#include <string>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("TcpValidation");

// These variables are declared outside of main() so that they can
// be used in trace sinks.
uint32_t g_firstBytesReceived = 0;  //!< First received packet size.
uint32_t g_secondBytesReceived = 0; //!< Second received packet size.
uint32_t g_marksObserved = 0;       //!< Number of marked packets observed.
uint32_t g_dropsObserved = 0;       //!< Number of dropped packets observed.
std::string g_validate = "";        //!< Empty string disables validation.
bool g_validationFailed = false;    //!< True if validation failed.

/**
 * Trace first congestion window.
 *
 * @param ofStream Output filestream.
 * @param oldCwnd Old value.
 * @param newCwnd new value.
 */
void
TraceFirstCwnd(std::ofstream* ofStream, uint32_t oldCwnd, uint32_t newCwnd)
{
    // TCP segment size is configured below to be 1448 bytes
    // so that we can report cwnd in units of segments
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << static_cast<double>(newCwnd) / 1448
                  << std::endl;
    }
    // Validation checks; both the ECN enabled and disabled cases are similar
    if (g_validate == "cubic-50ms-no-ecn" || g_validate == "cubic-50ms-ecn")
    {
        double now = Simulator::Now().GetSeconds();
        double cwnd = static_cast<double>(newCwnd) / 1448;
        if ((now > 5.43) && (now < 5.465) && (cwnd < 500))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " cwnd " << cwnd << " (expected >= 500)");
            g_validationFailed = true;
        }
        else if ((now > 5.795) && (now < 6) && (cwnd > 190))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " cwnd " << cwnd << " (expected <= 190)");
            g_validationFailed = true;
        }
        else if ((now > 14) && (now < 14.197) && (cwnd < 224))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " cwnd " << cwnd << " (expected >= 224)");
            g_validationFailed = true;
        }
        else if ((now > 17) && (now < 18.026) && (cwnd < 212))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " cwnd " << cwnd << " (expected >= 212)");
            g_validationFailed = true;
        }
    }
}

/**
 * Trace first TcpDctcp.
 *
 * @param ofStream Output filestream.
 * @param bytesMarked Bytes marked.
 * @param bytesAcked Bytes ACKed.
 * @param alpha Alpha.
 */
void
TraceFirstDctcp(std::ofstream* ofStream, uint32_t bytesMarked, uint32_t bytesAcked, double alpha)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << alpha << std::endl;
    }
    // Validation checks
    if (g_validate == "dctcp-80ms")
    {
        double now = Simulator::Now().GetSeconds();
        if ((now < 7.5) && (alpha < 0.1))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " alpha " << alpha << " (expected >= 0.1)");
            g_validationFailed = true;
        }
        else if ((now > 11) && (now < 30) && (alpha > 0.01))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " alpha " << alpha << " (expected <= 0.01)");
            g_validationFailed = true;
        }
        else if ((now > 34) && (alpha < 0.015) && (alpha > 0.025))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " alpha " << alpha
                               << " (expected 0.015 <= alpha <= 0.025)");
            g_validationFailed = true;
        }
    }
    else if (g_validate == "dctcp-10ms")
    {
        double now = Simulator::Now().GetSeconds();
        if ((now > 5.6) && (alpha > 0.1))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " alpha " << alpha << " (expected <= 0.1)");
            g_validationFailed = true;
        }
        if ((now > 7) && ((alpha > 0.09) || (alpha < 0.049)))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " alpha " << alpha
                               << " (expected 0.09 <= alpha <= 0.049)");
            g_validationFailed = true;
        }
    }
}

/**
 * Trace first RTT.
 *
 * @param ofStream Output filestream.
 * @param oldRtt Old value.
 * @param newRtt New value.
 */
void
TraceFirstRtt(std::ofstream* ofStream, Time oldRtt, Time newRtt)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << newRtt.GetSeconds() * 1000
                  << std::endl;
    }
}

/**
 * Trace second congestion window.
 *
 * @param ofStream Output filestream.
 * @param oldCwnd Old value.
 * @param newCwnd new value.
 */
void
TraceSecondCwnd(std::ofstream* ofStream, uint32_t oldCwnd, uint32_t newCwnd)
{
    // TCP segment size is configured below to be 1448 bytes
    // so that we can report cwnd in units of segments
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << static_cast<double>(newCwnd) / 1448
                  << std::endl;
    }
}

/**
 * Trace second RTT.
 *
 * @param ofStream Output filestream.
 * @param oldRtt Old value.
 * @param newRtt New value.
 */
void
TraceSecondRtt(std::ofstream* ofStream, Time oldRtt, Time newRtt)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << newRtt.GetSeconds() * 1000
                  << std::endl;
    }
}

/**
 * Trace second TcpDctcp.
 *
 * @param ofStream Output filestream.
 * @param bytesMarked Bytes marked.
 * @param bytesAcked Bytes ACKed.
 * @param alpha Alpha.
 */
void
TraceSecondDctcp(std::ofstream* ofStream, uint32_t bytesMarked, uint32_t bytesAcked, double alpha)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << alpha << std::endl;
    }
}

/**
 * Trace ping RTT.
 *
 * @param ofStream Output filestream.
 * @param rtt RTT value.
 */
void
TracePingRtt(std::ofstream* ofStream, uint16_t, Time rtt)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << rtt.GetSeconds() * 1000 << std::endl;
    }
}

/**
 * Trace first Rx.
 *
 * @param packet The packet.
 * @param address The sender address.
 */
void
TraceFirstRx(Ptr<const Packet> packet, const Address& address)
{
    g_firstBytesReceived += packet->GetSize();
}

/**
 * Trace second Rx.
 *
 * @param packet The packet.
 * @param address The sender address.
 */
void
TraceSecondRx(Ptr<const Packet> packet, const Address& address)
{
    g_secondBytesReceived += packet->GetSize();
}

/**
 * Trace queue drop.
 *
 * @param ofStream Output filestream.
 * @param item The dropped QueueDiscItem.
 */
void
TraceQueueDrop(std::ofstream* ofStream, Ptr<const QueueDiscItem> item)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << std::hex << item->Hash() << std::endl;
    }
    g_dropsObserved++;
}

/**
 * Trace queue marks.
 *
 * @param ofStream Output filestream.
 * @param item The marked QueueDiscItem.
 * @param reason The reason.
 */
void
TraceQueueMark(std::ofstream* ofStream, Ptr<const QueueDiscItem> item, const char* reason)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << std::hex << item->Hash() << std::endl;
    }
    g_marksObserved++;
}

/**
 * Trace queue length.
 *
 * @param ofStream Output filestream.
 * @param queueLinkRate Queue link rate.
 * @param oldVal Old value.
 * @param newVal New value.
 */
void
TraceQueueLength(std::ofstream* ofStream, DataRate queueLinkRate, uint32_t oldVal, uint32_t newVal)
{
    // output in units of ms
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << std::fixed
                  << static_cast<double>(newVal * 8) / (queueLinkRate.GetBitRate() / 1000)
                  << std::endl;
    }
}

/**
 * Trace marks frequency.
 *
 * @param ofStream Output filestream.
 * @param marksSamplingInterval The mark sampling interval.
 */
void
TraceMarksFrequency(std::ofstream* ofStream, Time marksSamplingInterval)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << g_marksObserved << std::endl;
    }
    g_marksObserved = 0;
    Simulator::Schedule(marksSamplingInterval,
                        &TraceMarksFrequency,
                        ofStream,
                        marksSamplingInterval);
}

/**
 * Trace the first throughput.
 *
 * @param ofStream Output filestream.
 * @param throughputInterval The throughput interval.
 */
void
TraceFirstThroughput(std::ofstream* ofStream, Time throughputInterval)
{
    double throughput = g_firstBytesReceived * 8 / throughputInterval.GetSeconds() / 1e6;
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " " << throughput << std::endl;
    }
    g_firstBytesReceived = 0;
    Simulator::Schedule(throughputInterval, &TraceFirstThroughput, ofStream, throughputInterval);
    if (g_validate == "dctcp-80ms")
    {
        double now = Simulator::Now().GetSeconds();
        if ((now < 14) && (throughput > 20))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " throughput " << throughput
                               << " (expected <= 20)");
            g_validationFailed = true;
        }
        if ((now < 30) && (throughput > 48))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " throughput " << throughput
                               << " (expected <= 48)");
            g_validationFailed = true;
        }
        if ((now > 32) && ((throughput < 47.5) || (throughput > 48.5)))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " throughput " << throughput
                               << " (expected 47.5 <= throughput <= 48.5)");
            g_validationFailed = true;
        }
    }
    else if (g_validate == "dctcp-10ms")
    {
        double now = Simulator::Now().GetSeconds();
        if ((now > 5.6) && ((throughput < 48) || (throughput > 49)))
        {
            NS_LOG_WARN("now " << Now().As(Time::S) << " throughput " << throughput
                               << " (expected 48 <= throughput <= 49)");
            g_validationFailed = true;
        }
    }
}

/**
 * Trace the second throughput.
 *
 * @param ofStream Output filestream.
 * @param throughputInterval The throughput interval.
 */
void
TraceSecondThroughput(std::ofstream* ofStream, Time throughputInterval)
{
    if (g_validate.empty())
    {
        *ofStream << Simulator::Now().GetSeconds() << " "
                  << g_secondBytesReceived * 8 / throughputInterval.GetSeconds() / 1e6 << std::endl;
    }
    g_secondBytesReceived = 0;
    Simulator::Schedule(throughputInterval, &TraceSecondThroughput, ofStream, throughputInterval);
}

/**
 * Schedule trace connection.
 *
 * @param ofStream Output filestream.
 */
void
ScheduleFirstTcpCwndTraceConnection(std::ofstream* ofStream)
{
    Config::ConnectWithoutContextFailSafe(
        "/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",
        MakeBoundCallback(&TraceFirstCwnd, ofStream));
}

/**
 * Schedule trace connection.
 *
 * @param ofStream Output filestream.
 */
void
ScheduleFirstTcpRttTraceConnection(std::ofstream* ofStream)
{
    Config::ConnectWithoutContextFailSafe("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/RTT",
                                          MakeBoundCallback(&TraceFirstRtt, ofStream));
}

/**
 * Schedule trace connection.
 *
 * @param ofStream Output filestream.
 */
void
ScheduleFirstDctcpTraceConnection(std::ofstream* ofStream)
{
    Config::ConnectWithoutContextFailSafe("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/"
                                          "CongestionOps/$ns3::TcpDctcp/CongestionEstimate",
                                          MakeBoundCallback(&TraceFirstDctcp, ofStream));
}

/**
 * Schedule trace connection.
 *
 * @param ofStream Output filestream.
 */
void
ScheduleSecondDctcpTraceConnection(std::ofstream* ofStream)
{
    Config::ConnectWithoutContextFailSafe("/NodeList/2/$ns3::TcpL4Protocol/SocketList/0/"
                                          "CongestionOps/$ns3::TcpDctcp/CongestionEstimate",
                                          MakeBoundCallback(&TraceSecondDctcp, ofStream));
}

/**
 * Schedule trace connection.
 */
void
ScheduleFirstPacketSinkConnection()
{
    Config::ConnectWithoutContextFailSafe("/NodeList/6/ApplicationList/*/$ns3::PacketSink/Rx",
                                          MakeCallback(&TraceFirstRx));
}

/**
 * Schedule trace connection.
 *
 * @param ofStream Output filestream.
 */
void
ScheduleSecondTcpCwndTraceConnection(std::ofstream* ofStream)
{
    Config::ConnectWithoutContext("/NodeList/2/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",
                                  MakeBoundCallback(&TraceSecondCwnd, ofStream));
}

/**
 * Schedule trace connection.
 *
 * @param ofStream Output filestream.
 */
void
ScheduleSecondTcpRttTraceConnection(std::ofstream* ofStream)
{
    Config::ConnectWithoutContext("/NodeList/2/$ns3::TcpL4Protocol/SocketList/0/RTT",
                                  MakeBoundCallback(&TraceSecondRtt, ofStream));
}

/**
 * Schedule trace connection.
 */
void
ScheduleSecondPacketSinkConnection()
{
    Config::ConnectWithoutContext("/NodeList/7/ApplicationList/*/$ns3::PacketSink/Rx",
                                  MakeCallback(&TraceSecondRx));
}

int
main(int argc, char* argv[])
{
    ////////////////////////////////////////////////////////////
    // variables not configured at command line               //
    ////////////////////////////////////////////////////////////
    uint32_t pingSize = 100; // bytes
    bool enableSecondTcp = false;
    bool enableLogging = false;
    Time pingInterval = MilliSeconds(100);
    Time marksSamplingInterval = MilliSeconds(100);
    Time throughputSamplingInterval = MilliSeconds(200);
    std::string pingTraceFile = "tcp-validation-ping.dat";
    std::string firstTcpRttTraceFile = "tcp-validation-first-tcp-rtt.dat";
    std::string firstTcpCwndTraceFile = "tcp-validation-first-tcp-cwnd.dat";
    std::string firstDctcpTraceFile = "tcp-validation-first-dctcp-alpha.dat";
    std::string firstTcpThroughputTraceFile = "tcp-validation-first-tcp-throughput.dat";
    std::string secondTcpRttTraceFile = "tcp-validation-second-tcp-rtt.dat";
    std::string secondTcpCwndTraceFile = "tcp-validation-second-tcp-cwnd.dat";
    std::string secondTcpThroughputTraceFile = "tcp-validation-second-tcp-throughput.dat";
    std::string secondDctcpTraceFile = "tcp-validation-second-dctcp-alpha.dat";
    std::string queueMarkTraceFile = "tcp-validation-queue-mark.dat";
    std::string queueDropTraceFile = "tcp-validation-queue-drop.dat";
    std::string queueMarksFrequencyTraceFile = "tcp-validation-queue-marks-frequency.dat";
    std::string queueLengthTraceFile = "tcp-validation-queue-length.dat";

    ////////////////////////////////////////////////////////////
    // variables configured at command line                   //
    ////////////////////////////////////////////////////////////
    std::string firstTcpType = "cubic";
    std::string secondTcpType = "";
    std::string queueType = "codel";
    Time stopTime = Seconds(70);
    Time baseRtt = MilliSeconds(80);
    DataRate linkRate("50Mbps");
    bool queueUseEcn = false;
    Time ceThreshold = MilliSeconds(1);
    bool enablePcap = false;

    ////////////////////////////////////////////////////////////
    // Override ns-3 defaults                                 //
    ////////////////////////////////////////////////////////////
    Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(1448));
    // Increase default buffer sizes to improve throughput over long delay paths
    // Config::SetDefault ("ns3::TcpSocket::SndBufSize",UintegerValue (8192000));
    // Config::SetDefault ("ns3::TcpSocket::RcvBufSize",UintegerValue (8192000));
    Config::SetDefault("ns3::TcpSocket::SndBufSize", UintegerValue(32768000));
    Config::SetDefault("ns3::TcpSocket::RcvBufSize", UintegerValue(32768000));
    Config::SetDefault("ns3::TcpSocket::InitialCwnd", UintegerValue(10));
    Config::SetDefault("ns3::TcpL4Protocol::RecoveryType",
                       TypeIdValue(TcpPrrRecovery::GetTypeId()));
    // Validation criteria were written for TCP Cubic without Reno-friendly behavior, so disable it
    // for these tests
    Config::SetDefault("ns3::TcpCubic::TcpFriendliness", BooleanValue(false));

    ////////////////////////////////////////////////////////////
    // command-line argument parsing                          //
    ////////////////////////////////////////////////////////////
    CommandLine cmd(__FILE__);
    cmd.AddValue("firstTcpType", "first TCP type (cubic, dctcp, or reno)", firstTcpType);
    cmd.AddValue("secondTcpType", "second TCP type (cubic, dctcp, or reno)", secondTcpType);
    cmd.AddValue("queueType", "bottleneck queue type (fq, codel, pie, or red)", queueType);
    cmd.AddValue("baseRtt", "base RTT", baseRtt);
    cmd.AddValue("ceThreshold", "CoDel CE threshold (for DCTCP)", ceThreshold);
    cmd.AddValue("linkRate", "data rate of bottleneck link", linkRate);
    cmd.AddValue("stopTime", "simulation stop time", stopTime);
    cmd.AddValue("queueUseEcn", "use ECN on queue", queueUseEcn);
    cmd.AddValue("enablePcap", "enable Pcap", enablePcap);
    cmd.AddValue("validate", "validation case to run", g_validate);
    cmd.Parse(argc, argv);

    // If validation is selected, perform some configuration checks
    if (!g_validate.empty())
    {
        NS_ABORT_MSG_UNLESS(g_validate == "dctcp-10ms" || g_validate == "dctcp-80ms" ||
                                g_validate == "cubic-50ms-no-ecn" || g_validate == "cubic-50ms-ecn",
                            "Unknown test");
        if (g_validate == "dctcp-10ms" || g_validate == "dctcp-80ms")
        {
            NS_ABORT_MSG_UNLESS(firstTcpType == "dctcp", "Incorrect TCP");
            NS_ABORT_MSG_UNLESS(secondTcpType.empty(), "Incorrect TCP");
            NS_ABORT_MSG_UNLESS(linkRate == DataRate("50Mbps"), "Incorrect data rate");
            NS_ABORT_MSG_UNLESS(queueUseEcn == true, "Incorrect ECN configuration");
            NS_ABORT_MSG_UNLESS(stopTime >= Seconds(15), "Incorrect stopTime");
            if (g_validate == "dctcp-10ms")
            {
                NS_ABORT_MSG_UNLESS(baseRtt == MilliSeconds(10), "Incorrect RTT");
            }
            else if (g_validate == "dctcp-80ms")
            {
                NS_ABORT_MSG_UNLESS(baseRtt == MilliSeconds(80), "Incorrect RTT");
            }
        }
        else if (g_validate == "cubic-50ms-no-ecn" || g_validate == "cubic-50ms-ecn")
        {
            NS_ABORT_MSG_UNLESS(firstTcpType == "cubic", "Incorrect TCP");
            NS_ABORT_MSG_UNLESS(secondTcpType.empty(), "Incorrect TCP");
            NS_ABORT_MSG_UNLESS(baseRtt == MilliSeconds(50), "Incorrect RTT");
            NS_ABORT_MSG_UNLESS(linkRate == DataRate("50Mbps"), "Incorrect data rate");
            NS_ABORT_MSG_UNLESS(stopTime >= Seconds(20), "Incorrect stopTime");
            if (g_validate == "cubic-50ms-no-ecn")
            {
                NS_ABORT_MSG_UNLESS(queueUseEcn == false, "Incorrect ECN configuration");
            }
            else if (g_validate == "cubic-50ms-ecn")
            {
                NS_ABORT_MSG_UNLESS(queueUseEcn == true, "Incorrect ECN configuration");
            }
        }
    }

    if (enableLogging)
    {
        LogComponentEnable(
            "TcpSocketBase",
            (LogLevel)(LOG_PREFIX_FUNC | LOG_PREFIX_NODE | LOG_PREFIX_TIME | LOG_LEVEL_ALL));
        LogComponentEnable(
            "TcpDctcp",
            (LogLevel)(LOG_PREFIX_FUNC | LOG_PREFIX_NODE | LOG_PREFIX_TIME | LOG_LEVEL_ALL));
    }

    Time oneWayDelay = baseRtt / 2;

    TypeId firstTcpTypeId;
    if (firstTcpType == "reno")
    {
        firstTcpTypeId = TcpLinuxReno::GetTypeId();
    }
    else if (firstTcpType == "cubic")
    {
        firstTcpTypeId = TcpCubic::GetTypeId();
    }
    else if (firstTcpType == "dctcp")
    {
        firstTcpTypeId = TcpDctcp::GetTypeId();
        Config::SetDefault("ns3::CoDelQueueDisc::CeThreshold", TimeValue(ceThreshold));
        Config::SetDefault("ns3::FqCoDelQueueDisc::CeThreshold", TimeValue(ceThreshold));
        if (!queueUseEcn)
        {
            std::cout << "Warning: using DCTCP with queue ECN disabled" << std::endl;
        }
    }
    else
    {
        NS_FATAL_ERROR("Fatal error:  tcp unsupported");
    }
    TypeId secondTcpTypeId;
    if (secondTcpType == "reno")
    {
        enableSecondTcp = true;
        secondTcpTypeId = TcpLinuxReno::GetTypeId();
    }
    else if (secondTcpType == "cubic")
    {
        enableSecondTcp = true;
        secondTcpTypeId = TcpCubic::GetTypeId();
    }
    else if (secondTcpType == "dctcp")
    {
        enableSecondTcp = true;
        secondTcpTypeId = TcpDctcp::GetTypeId();
    }
    else if (secondTcpType.empty())
    {
        enableSecondTcp = false;
        NS_LOG_DEBUG("No second TCP selected");
    }
    else
    {
        NS_FATAL_ERROR("Fatal error:  tcp unsupported");
    }
    TypeId queueTypeId;
    if (queueType == "fq")
    {
        queueTypeId = FqCoDelQueueDisc::GetTypeId();
    }
    else if (queueType == "codel")
    {
        queueTypeId = CoDelQueueDisc::GetTypeId();
    }
    else if (queueType == "pie")
    {
        queueTypeId = PieQueueDisc::GetTypeId();
    }
    else if (queueType == "red")
    {
        queueTypeId = RedQueueDisc::GetTypeId();
    }
    else
    {
        NS_FATAL_ERROR("Fatal error:  queueType unsupported");
    }

    if (queueUseEcn)
    {
        Config::SetDefault("ns3::CoDelQueueDisc::UseEcn", BooleanValue(true));
        Config::SetDefault("ns3::FqCoDelQueueDisc::UseEcn", BooleanValue(true));
        Config::SetDefault("ns3::PieQueueDisc::UseEcn", BooleanValue(true));
        Config::SetDefault("ns3::RedQueueDisc::UseEcn", BooleanValue(true));
    }
    // Enable TCP to use ECN regardless
    Config::SetDefault("ns3::TcpSocketBase::UseEcn", StringValue("On"));

    // Report on configuration
    if (enableSecondTcp)
    {
        NS_LOG_DEBUG("first TCP: " << firstTcpTypeId.GetName()
                                   << "; second TCP: " << secondTcpTypeId.GetName()
                                   << "; queue: " << queueTypeId.GetName()
                                   << "; ceThreshold: " << ceThreshold.GetSeconds() * 1000 << "ms");
    }
    else
    {
        NS_LOG_DEBUG("first TCP: " << firstTcpTypeId.GetName()
                                   << "; queue: " << queueTypeId.GetName()
                                   << "; ceThreshold: " << ceThreshold.GetSeconds() * 1000 << "ms");
    }

    // Write traces only if we are not in validation mode (g_validate == "")
    std::ofstream pingOfStream;
    std::ofstream firstTcpRttOfStream;
    std::ofstream firstTcpCwndOfStream;
    std::ofstream firstTcpThroughputOfStream;
    std::ofstream firstTcpDctcpOfStream;
    std::ofstream secondTcpRttOfStream;
    std::ofstream secondTcpCwndOfStream;
    std::ofstream secondTcpThroughputOfStream;
    std::ofstream secondTcpDctcpOfStream;
    std::ofstream queueDropOfStream;
    std::ofstream queueMarkOfStream;
    std::ofstream queueMarksFrequencyOfStream;
    std::ofstream queueLengthOfStream;
    if (g_validate.empty())
    {
        pingOfStream.open(pingTraceFile, std::ofstream::out);
        firstTcpRttOfStream.open(firstTcpRttTraceFile, std::ofstream::out);
        firstTcpCwndOfStream.open(firstTcpCwndTraceFile, std::ofstream::out);
        firstTcpThroughputOfStream.open(firstTcpThroughputTraceFile, std::ofstream::out);
        if (firstTcpType == "dctcp")
        {
            firstTcpDctcpOfStream.open(firstDctcpTraceFile, std::ofstream::out);
        }
        if (enableSecondTcp)
        {
            secondTcpRttOfStream.open(secondTcpRttTraceFile, std::ofstream::out);
            secondTcpCwndOfStream.open(secondTcpCwndTraceFile, std::ofstream::out);
            secondTcpThroughputOfStream.open(secondTcpThroughputTraceFile, std::ofstream::out);
            if (secondTcpType == "dctcp")
            {
                secondTcpDctcpOfStream.open(secondDctcpTraceFile, std::ofstream::out);
            }
        }
        queueDropOfStream.open(queueDropTraceFile, std::ofstream::out);
        queueMarkOfStream.open(queueMarkTraceFile, std::ofstream::out);
        queueMarksFrequencyOfStream.open(queueMarksFrequencyTraceFile, std::ofstream::out);
        queueLengthOfStream.open(queueLengthTraceFile, std::ofstream::out);
    }

    ////////////////////////////////////////////////////////////
    // scenario setup                                         //
    ////////////////////////////////////////////////////////////
    Ptr<Node> pingServer = CreateObject<Node>();
    Ptr<Node> firstServer = CreateObject<Node>();
    Ptr<Node> secondServer = CreateObject<Node>();
    Ptr<Node> wanRouter = CreateObject<Node>();
    Ptr<Node> lanRouter = CreateObject<Node>();
    Ptr<Node> pingClient = CreateObject<Node>();
    Ptr<Node> firstClient = CreateObject<Node>();
    Ptr<Node> secondClient = CreateObject<Node>();

    // Device containers
    NetDeviceContainer pingServerDevices;
    NetDeviceContainer firstServerDevices;
    NetDeviceContainer secondServerDevices;
    NetDeviceContainer wanLanDevices;
    NetDeviceContainer pingClientDevices;
    NetDeviceContainer firstClientDevices;
    NetDeviceContainer secondClientDevices;

    PointToPointHelper p2p;
    p2p.SetQueue("ns3::DropTailQueue", "MaxSize", QueueSizeValue(QueueSize("3p")));
    p2p.SetDeviceAttribute("DataRate", DataRateValue(DataRate("1000Mbps")));
    // Add delay only on the WAN links
    p2p.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
    pingServerDevices = p2p.Install(wanRouter, pingServer);
    firstServerDevices = p2p.Install(wanRouter, firstServer);
    secondServerDevices = p2p.Install(wanRouter, secondServer);
    p2p.SetChannelAttribute("Delay", TimeValue(oneWayDelay));
    wanLanDevices = p2p.Install(wanRouter, lanRouter);
    p2p.SetQueue("ns3::DropTailQueue", "MaxSize", QueueSizeValue(QueueSize("3p")));
    p2p.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
    pingClientDevices = p2p.Install(lanRouter, pingClient);
    firstClientDevices = p2p.Install(lanRouter, firstClient);
    secondClientDevices = p2p.Install(lanRouter, secondClient);

    // Limit the bandwidth on the wanRouter->lanRouter interface
    Ptr<PointToPointNetDevice> p = wanLanDevices.Get(0)->GetObject<PointToPointNetDevice>();
    p->SetAttribute("DataRate", DataRateValue(linkRate));

    InternetStackHelper stackHelper;
    stackHelper.Install(pingServer);
    Ptr<TcpL4Protocol> proto;
    stackHelper.Install(firstServer);
    proto = firstServer->GetObject<TcpL4Protocol>();
    proto->SetAttribute("SocketType", TypeIdValue(firstTcpTypeId));
    stackHelper.Install(secondServer);
    stackHelper.Install(wanRouter);
    stackHelper.Install(lanRouter);
    stackHelper.Install(pingClient);

    stackHelper.Install(firstClient);
    // Set the per-node TCP type here
    proto = firstClient->GetObject<TcpL4Protocol>();
    proto->SetAttribute("SocketType", TypeIdValue(firstTcpTypeId));
    stackHelper.Install(secondClient);

    if (enableSecondTcp)
    {
        proto = secondClient->GetObject<TcpL4Protocol>();
        proto->SetAttribute("SocketType", TypeIdValue(secondTcpTypeId));
        proto = secondServer->GetObject<TcpL4Protocol>();
        proto->SetAttribute("SocketType", TypeIdValue(secondTcpTypeId));
    }

    // InternetStackHelper will install a base TrafficControlLayer on the node,
    // but the Ipv4AddressHelper below will install the default FqCoDelQueueDisc
    // on all single device nodes.  The below code overrides the configuration
    // that is normally done by the Ipv4AddressHelper::Install() method by
    // instead explicitly configuring the queue discs we want on each device.
    TrafficControlHelper tchFq;
    tchFq.SetRootQueueDisc("ns3::FqCoDelQueueDisc");
    tchFq.SetQueueLimits("ns3::DynamicQueueLimits", "HoldTime", StringValue("1ms"));
    tchFq.Install(pingServerDevices);
    tchFq.Install(firstServerDevices);
    tchFq.Install(secondServerDevices);
    tchFq.Install(wanLanDevices.Get(1));
    tchFq.Install(pingClientDevices);
    tchFq.Install(firstClientDevices);
    tchFq.Install(secondClientDevices);
    // Install queue for bottleneck link
    TrafficControlHelper tchBottleneck;
    tchBottleneck.SetRootQueueDisc(queueTypeId.GetName());
    tchBottleneck.SetQueueLimits("ns3::DynamicQueueLimits", "HoldTime", StringValue("1ms"));
    tchBottleneck.Install(wanLanDevices.Get(0));

    Ipv4AddressHelper ipv4;
    ipv4.SetBase("10.1.1.0", "255.255.255.0");
    Ipv4InterfaceContainer pingServerIfaces = ipv4.Assign(pingServerDevices);
    ipv4.SetBase("10.1.2.0", "255.255.255.0");
    Ipv4InterfaceContainer firstServerIfaces = ipv4.Assign(firstServerDevices);
    ipv4.SetBase("10.1.3.0", "255.255.255.0");
    Ipv4InterfaceContainer secondServerIfaces = ipv4.Assign(secondServerDevices);
    ipv4.SetBase("172.16.1.0", "255.255.255.0");
    Ipv4InterfaceContainer wanLanIfaces = ipv4.Assign(wanLanDevices);
    ipv4.SetBase("192.168.1.0", "255.255.255.0");
    Ipv4InterfaceContainer pingClientIfaces = ipv4.Assign(pingClientDevices);
    ipv4.SetBase("192.168.2.0", "255.255.255.0");
    Ipv4InterfaceContainer firstClientIfaces = ipv4.Assign(firstClientDevices);
    ipv4.SetBase("192.168.3.0", "255.255.255.0");
    Ipv4InterfaceContainer secondClientIfaces = ipv4.Assign(secondClientDevices);

    Ipv4GlobalRoutingHelper::PopulateRoutingTables();

    ////////////////////////////////////////////////////////////
    // application setup                                      //
    ////////////////////////////////////////////////////////////
    PingHelper pingHelper(Ipv4Address("192.168.1.2"));
    pingHelper.SetAttribute("Interval", TimeValue(pingInterval));
    pingHelper.SetAttribute("Size", UintegerValue(pingSize));
    pingHelper.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::SILENT));
    ApplicationContainer pingContainer = pingHelper.Install(pingServer);
    Ptr<Ping> ping = pingContainer.Get(0)->GetObject<Ping>();
    ping->TraceConnectWithoutContext("Rtt", MakeBoundCallback(&TracePingRtt, &pingOfStream));
    pingContainer.Start(Seconds(1));
    pingContainer.Stop(stopTime - Seconds(1));

    ApplicationContainer firstApp;
    uint16_t firstPort = 5000;
    BulkSendHelper tcp("ns3::TcpSocketFactory", Address());
    // set to large value:  e.g. 1000 Mb/s for 60 seconds = 7500000000 bytes
    tcp.SetAttribute("MaxBytes", UintegerValue(7500000000));
    // Configure first TCP client/server pair
    InetSocketAddress firstDestAddress(firstClientIfaces.GetAddress(1), firstPort);
    tcp.SetAttribute("Remote", AddressValue(firstDestAddress));
    firstApp = tcp.Install(firstServer);
    firstApp.Start(Seconds(5));
    firstApp.Stop(stopTime - Seconds(1));

    Address firstSinkAddress(InetSocketAddress(Ipv4Address::GetAny(), firstPort));
    ApplicationContainer firstSinkApp;
    PacketSinkHelper firstSinkHelper("ns3::TcpSocketFactory", firstSinkAddress);
    firstSinkApp = firstSinkHelper.Install(firstClient);
    firstSinkApp.Start(Seconds(5));
    firstSinkApp.Stop(stopTime - MilliSeconds(500));

    // Configure second TCP client/server pair
    if (enableSecondTcp)
    {
        BulkSendHelper tcp("ns3::TcpSocketFactory", Address());
        uint16_t secondPort = 5000;
        ApplicationContainer secondApp;
        InetSocketAddress secondDestAddress(secondClientIfaces.GetAddress(1), secondPort);
        tcp.SetAttribute("Remote", AddressValue(secondDestAddress));
        secondApp = tcp.Install(secondServer);
        secondApp.Start(Seconds(15));
        secondApp.Stop(stopTime - Seconds(1));

        Address secondSinkAddress(InetSocketAddress(Ipv4Address::GetAny(), secondPort));
        PacketSinkHelper secondSinkHelper("ns3::TcpSocketFactory", secondSinkAddress);
        ApplicationContainer secondSinkApp;
        secondSinkApp = secondSinkHelper.Install(secondClient);
        secondSinkApp.Start(Seconds(15));
        secondSinkApp.Stop(stopTime - MilliSeconds(500));
    }

    // Setup traces that can be hooked now
    Ptr<TrafficControlLayer> tc;
    Ptr<QueueDisc> qd;
    // Trace drops and marks for bottleneck
    tc = wanLanDevices.Get(0)->GetNode()->GetObject<TrafficControlLayer>();
    qd = tc->GetRootQueueDiscOnDevice(wanLanDevices.Get(0));
    qd->TraceConnectWithoutContext("Drop", MakeBoundCallback(&TraceQueueDrop, &queueDropOfStream));
    qd->TraceConnectWithoutContext("Mark", MakeBoundCallback(&TraceQueueMark, &queueMarkOfStream));
    qd->TraceConnectWithoutContext(
        "BytesInQueue",
        MakeBoundCallback(&TraceQueueLength, &queueLengthOfStream, linkRate));

    // Setup scheduled traces; TCP traces must be hooked after socket creation
    Simulator::Schedule(Seconds(5) + MilliSeconds(100),
                        &ScheduleFirstTcpRttTraceConnection,
                        &firstTcpRttOfStream);
    Simulator::Schedule(Seconds(5) + MilliSeconds(100),
                        &ScheduleFirstTcpCwndTraceConnection,
                        &firstTcpCwndOfStream);
    Simulator::Schedule(Seconds(5) + MilliSeconds(100), &ScheduleFirstPacketSinkConnection);
    if (firstTcpType == "dctcp")
    {
        Simulator::Schedule(Seconds(5) + MilliSeconds(100),
                            &ScheduleFirstDctcpTraceConnection,
                            &firstTcpDctcpOfStream);
    }
    Simulator::Schedule(throughputSamplingInterval,
                        &TraceFirstThroughput,
                        &firstTcpThroughputOfStream,
                        throughputSamplingInterval);
    if (enableSecondTcp)
    {
        // Setup scheduled traces; TCP traces must be hooked after socket creation
        Simulator::Schedule(Seconds(15) + MilliSeconds(100),
                            &ScheduleSecondTcpRttTraceConnection,
                            &secondTcpRttOfStream);
        Simulator::Schedule(Seconds(15) + MilliSeconds(100),
                            &ScheduleSecondTcpCwndTraceConnection,
                            &secondTcpCwndOfStream);
        Simulator::Schedule(Seconds(15) + MilliSeconds(100), &ScheduleSecondPacketSinkConnection);
        Simulator::Schedule(throughputSamplingInterval,
                            &TraceSecondThroughput,
                            &secondTcpThroughputOfStream,
                            throughputSamplingInterval);
        if (secondTcpType == "dctcp")
        {
            Simulator::Schedule(Seconds(15) + MilliSeconds(100),
                                &ScheduleSecondDctcpTraceConnection,
                                &secondTcpDctcpOfStream);
        }
    }
    Simulator::Schedule(marksSamplingInterval,
                        &TraceMarksFrequency,
                        &queueMarksFrequencyOfStream,
                        marksSamplingInterval);

    if (enablePcap)
    {
        p2p.EnablePcapAll("tcp-validation", false);
    }

    Simulator::Stop(stopTime);
    Simulator::Run();
    Simulator::Destroy();

    if (g_validate.empty())
    {
        pingOfStream.close();
        firstTcpCwndOfStream.close();
        firstTcpRttOfStream.close();
        if (firstTcpType == "dctcp")
        {
            firstTcpDctcpOfStream.close();
        }
        firstTcpThroughputOfStream.close();
        if (enableSecondTcp)
        {
            secondTcpCwndOfStream.close();
            secondTcpRttOfStream.close();
            secondTcpThroughputOfStream.close();
            if (secondTcpType == "dctcp")
            {
                secondTcpDctcpOfStream.close();
            }
        }
        queueDropOfStream.close();
        queueMarkOfStream.close();
        queueMarksFrequencyOfStream.close();
        queueLengthOfStream.close();
    }

    if (g_validationFailed)
    {
        NS_FATAL_ERROR("Validation failed");
    }

    return 0;
}