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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
|
/* $Id: NetPerf.cpp $ */
/** @file
* NetPerf - Network Performance Benchmark.
*/
/*
* Copyright (C) 2010-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/getopt.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/path.h>
#include <iprt/param.h>
#include <iprt/process.h>
#include <iprt/rand.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/tcp.h>
#include <iprt/thread.h>
#include <iprt/test.h>
#include <iprt/time.h>
#include <iprt/timer.h>
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** Default TCP port (update help text if you change this) */
#define NETPERF_DEFAULT_PORT 5002
/** Default TCP packet size (bytes) */
#define NETPERF_DEFAULT_PKT_SIZE_THROUGHPUT 8192
/** Default TCP packet size (bytes) */
#define NETPERF_DEFAULT_PKT_SIZE_LATENCY 1024
/** Maximum packet size possible (bytes). */
#define NETPERF_MAX_PKT_SIZE _1M
/** Minimum packet size possible (bytes). */
#define NETPERF_MIN_PKT_SIZE sizeof(NETPERFHDR)
/** Default timeout in (seconds) */
#define NETPERF_DEFAULT_TIMEOUT 10
/** Maximum timeout possible (seconds). */
#define NETPERF_MAX_TIMEOUT 3600 /* 1h */
/** Minimum timeout possible (seconds). */
#define NETPERF_MIN_TIMEOUT 1
/** The default warmup time (ms). */
#define NETPERF_DEFAULT_WARMUP 1000 /* 1s */
/** The maxium warmup time (ms). */
#define NETPERF_MAX_WARMUP 60000 /* 60s */
/** The minimum warmup time (ms). */
#define NETPERF_MIN_WARMUP 1000 /* 1s */
/** The default cool down time (ms). */
#define NETPERF_DEFAULT_COOL_DOWN 1000 /* 1s */
/** The maxium cool down time (ms). */
#define NETPERF_MAX_COOL_DOWN 60000 /* 60s */
/** The minimum cool down time (ms). */
#define NETPERF_MIN_COOL_DOWN 1000 /* 1s */
/** Maximum socket buffer size possible (bytes). */
#define NETPERF_MAX_BUF_SIZE _128M
/** Minimum socket buffer size possible (bytes). */
#define NETPERF_MIN_BUF_SIZE 256
/** The length of the length prefix used when submitting parameters and
* results. */
#define NETPERF_LEN_PREFIX 4
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
typedef enum NETPERFPROTO
{
NETPERFPROTO_INVALID = 0,
NETPERFPROTO_TCP
//NETPERFPROTO_UDP
} NETPERFPROTO;
/**
* What kind of test we're performing.
*/
typedef enum NETPERFMODE
{
NETPERFMODE_INVALID = 0,
/** Latency of a symmetric packet exchange. */
NETPERFMODE_LATENCY,
/** Separate throughput measurements for each direction. */
NETPERFMODE_THROUGHPUT,
/** Transmit throughput. */
NETPERFMODE_THROUGHPUT_XMIT,
/** Transmit throughput. */
NETPERFMODE_THROUGHPUT_RECV
} NETPERFMODE;
/**
* Statistics.
*/
typedef struct NETPERFSTATS
{
uint64_t cTx;
uint64_t cRx;
uint64_t cEchos;
uint64_t cErrors;
uint64_t cNsElapsed;
} NETPERFSTATS;
/**
* Settings & a little bit of state.
*/
typedef struct NETPERFPARAMS
{
/** @name Static settings
* @{ */
/** The TCP port number. */
uint32_t uPort;
/** Client: Use server statistcs. */
bool fServerStats;
/** Server: Quit after the first client. */
bool fSingleClient;
/** Send and receive buffer sizes for TCP sockets, zero if to use defaults. */
uint32_t cbBufferSize;
/** @} */
/** @name Dynamic settings
* @{ */
/** Disable send packet coalescing. */
bool fNoDelay;
/** Detect broken payloads. */
bool fCheckData;
/** The test mode. */
NETPERFMODE enmMode;
/** The number of seconds to run each of the test steps. */
uint32_t cSecTimeout;
/** Number of millisecond to spend warning up before testing. */
uint32_t cMsWarmup;
/** Number of millisecond to spend cooling down after the testing. */
uint32_t cMsCoolDown;
/** The packet size. */
uint32_t cbPacket;
/** @} */
/** @name State
* @{ */
RTSOCKET hSocket;
/** @} */
} NETPERFPARAMS;
/**
* Packet header used in tests.
*
* Need to indicate when we've timed out and it's time to reverse the roles or
* stop testing.
*/
typedef struct NETPERFHDR
{
/** Magic value (little endian). */
uint32_t u32Magic;
/** State value. */
uint32_t u32State;
/** Sequence number (little endian). */
uint32_t u32Seq;
/** Reserved, must be zero. */
uint32_t u32Reserved;
} NETPERFHDR;
/** Magic value for NETPERFHDR::u32Magic. */
#define NETPERFHDR_MAGIC UINT32_C(0xfeedf00d)
/** @name Packet State (NETPERF::u32Magic)
* @{ */
/** Warm up. */
#define NETPERFHDR_WARMUP UINT32_C(0x0c0ffe01)
/** The clock is running. */
#define NETPERFHDR_TESTING UINT32_C(0x0c0ffe02)
/** Stop the clock but continue the package flow. */
#define NETPERFHDR_COOL_DOWN UINT32_C(0x0c0ffe03)
/** Done, stop the clock if not done already and reply with results. */
#define NETPERFHDR_DONE UINT32_C(0x0c0ffe04)
/** @} */
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Connection start/identifier to make sure other end is NetPerf. */
static const char g_ConnectStart[] = "yo! waaazzzzzaaaaup dude?";
/** Start of parameters proposal made by the client. */
static const char g_szStartParams[] = "deal?";
/** All okay to start test */
static const char g_szAck[] = "okay!";
/** Negative. */
static const char g_szNegative[] = "nope!";
AssertCompile(sizeof(g_szAck) == sizeof(g_szNegative));
/** Start of statistics. */
static const char g_szStartStats[] = "dude, stats";
/** Command line parameters */
static const RTGETOPTDEF g_aCmdOptions[] =
{
{ "--server", 's', RTGETOPT_REQ_NOTHING },
{ "--client", 'c', RTGETOPT_REQ_STRING },
{ "--interval", 'i', RTGETOPT_REQ_UINT32 },
{ "--port", 'p', RTGETOPT_REQ_UINT32 },
{ "--len", 'l', RTGETOPT_REQ_UINT32 },
{ "--nodelay", 'N', RTGETOPT_REQ_NOTHING },
{ "--mode", 'm', RTGETOPT_REQ_STRING },
{ "--warmup", 'w', RTGETOPT_REQ_UINT32 },
{ "--cool-down", 'W', RTGETOPT_REQ_UINT32 },
{ "--server-stats", 'S', RTGETOPT_REQ_NOTHING },
{ "--single-client", '1', RTGETOPT_REQ_NOTHING },
{ "--daemonize", 'd', RTGETOPT_REQ_NOTHING },
{ "--daemonized", 'D', RTGETOPT_REQ_NOTHING },
{ "--check-data", 'C', RTGETOPT_REQ_NOTHING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING },
{ "--buffer-size", 'b', RTGETOPT_REQ_UINT32 },
{ "--help", 'h', RTGETOPT_REQ_NOTHING } /* for Usage() */
};
/** The test handle. */
static RTTEST g_hTest;
/** Verbosity level. */
static uint32_t g_uVerbosity = 0;
static void Usage(PRTSTREAM pStrm)
{
char szExec[RTPATH_MAX];
RTStrmPrintf(pStrm, "usage: %s <-s|-c <host>> [options]\n",
RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
RTStrmPrintf(pStrm, "\n");
RTStrmPrintf(pStrm, "options: \n");
for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
{
const char *pszHelp;
switch (g_aCmdOptions[i].iShort)
{
case 'h':
pszHelp = "Displays this help and exit";
break;
case 's':
pszHelp = "Run in server mode, waiting for clients (default)";
break;
case 'c':
pszHelp = "Run in client mode, connecting to <host>";
break;
case 'i':
pszHelp = "Interval in seconds to run the test (default " RT_XSTR(NETPERF_DEFAULT_TIMEOUT) " s)";
break;
case 'p':
pszHelp = "Server port to listen/connect to (default " RT_XSTR(NETPERF_DEFAULT_PORT) ")";
break;
case 'l':
pszHelp = "Packet size in bytes (defaults to " RT_XSTR(NETPERF_DEFAULT_PKT_SIZE_LATENCY)
" for latency and " RT_XSTR(NETPERF_DEFAULT_PKT_SIZE_THROUGHPUT) " for throughput)";
break;
case 'm':
pszHelp = "Test mode: latency (default), throughput, throughput-xmit or throughput-recv";
break;
case 'N':
pszHelp = "Set TCP no delay, disabling Nagle's algorithm";
break;
case 'S':
pszHelp = "Report server stats, ignored if server";
break;
case '1':
pszHelp = "Stop the server after the first client";
break;
case 'd':
pszHelp = "Daemonize if server, ignored if client";
break;
case 'D':
continue; /* internal */
case 'w':
pszHelp = "Warmup time, in milliseconds (default " RT_XSTR(NETPERF_DEFAULT_WARMUP) " ms)";
break;
case 'W':
pszHelp = "Cool down time, in milliseconds (default " RT_XSTR(NETPERF_DEFAULT_COOL_DOWN) " ms)";
break;
case 'C':
pszHelp = "Check payload data at the receiving end";
break;
case 'b':
pszHelp = "Send and receive buffer sizes for TCP";
break;
case 'v':
pszHelp = "Verbose execution.";
break;
default:
pszHelp = "Option undocumented";
break;
}
char szOpt[256];
RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
RTStrmPrintf(pStrm, " %-20s%s\n", szOpt, pszHelp);
}
}
/**
* Timer callback employed to set the stop indicator.
*
* This is used both by the client and server side.
*
* @param hTimer The timer, ignored.
* @param pvUser Pointer to the stop variable.
* @param iTick The tick, ignored.
*/
static DECLCALLBACK(void) netperfStopTimerCallback(RTTIMERLR hTimer, void *pvUser, uint64_t iTick)
{
bool volatile *pfStop = (bool volatile *)pvUser;
if (g_uVerbosity > 0)
RTPrintf("Time's Up!\n");
ASMAtomicWriteBool(pfStop, true);
NOREF(hTimer); NOREF(iTick);
}
/**
* Sends a statistics packet to our peer.
*
* @returns IPRT status code.
* @param pStats The stats to send.
* @param hSocket The TCP socket to send them to.
*/
static int netperfSendStats(NETPERFSTATS const *pStats, RTSOCKET hSocket)
{
char szBuf[256 + NETPERF_LEN_PREFIX];
size_t cch = RTStrPrintf(&szBuf[NETPERF_LEN_PREFIX], sizeof(szBuf) - NETPERF_LEN_PREFIX,
"%s:%llu:%llu:%llu:%llu:%llu",
g_szStartStats,
pStats->cTx,
pStats->cRx,
pStats->cEchos,
pStats->cErrors,
pStats->cNsElapsed);
RTStrPrintf(szBuf, NETPERF_LEN_PREFIX + 1, "%0*u", NETPERF_LEN_PREFIX, cch);
szBuf[NETPERF_LEN_PREFIX] = g_szStartStats[0];
Assert(strlen(szBuf) == cch + NETPERF_LEN_PREFIX);
int rc = RTTcpWrite(hSocket, szBuf, cch + NETPERF_LEN_PREFIX);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "stats: Failed to send stats: %Rrc\n", rc);
/*
* Wait for ACK.
*/
rc = RTTcpRead(hSocket, szBuf, sizeof(g_szAck) - 1, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "stats: failed to write stats: %Rrc\n", rc);
szBuf[sizeof(g_szAck) - 1] = '\0';
if (!strcmp(szBuf, g_szNegative))
return RTTestIFailedRc(rc, "stats: client failed to parse them\n");
if (strcmp(szBuf, g_szAck))
return RTTestIFailedRc(rc, "stats: got '%s' in instead of ack/nack\n", szBuf);
return VINF_SUCCESS;
}
/**
* Receives a statistics packet from our peer.
*
* @returns IPRT status code. Error signalled.
* @param pStats Where to receive the stats.
* @param hSocket The TCP socket to recevie them from.
*/
static int netperfRecvStats(NETPERFSTATS *pStats, RTSOCKET hSocket)
{
/*
* Read the stats message.
*/
/* the length prefix */
char szBuf[256 + NETPERF_LEN_PREFIX];
int rc = RTTcpRead(hSocket, szBuf, NETPERF_LEN_PREFIX, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "stats: failed to read stats prefix: %Rrc\n", rc);
szBuf[NETPERF_LEN_PREFIX] = '\0';
uint32_t cch;
rc = RTStrToUInt32Full(szBuf, 10, &cch);
if (rc != VINF_SUCCESS)
return RTTestIFailedRc(RT_SUCCESS(rc) ? -rc : rc, "stats: bad stat length prefix: '%s' - %Rrc\n", szBuf, rc);
if (cch >= sizeof(szBuf))
return RTTestIFailedRc(VERR_BUFFER_OVERFLOW, "stats: too large: %u bytes\n", cch);
/* the actual message */
rc = RTTcpRead(hSocket, szBuf, cch, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "failed to read stats: %Rrc\n", rc);
szBuf[cch] = '\0';
/*
* Validate the message header.
*/
if ( strncmp(szBuf, g_szStartStats, sizeof(g_szStartStats) - 1)
|| szBuf[sizeof(g_szStartStats) - 1] != ':')
return RTTestIFailedRc(VERR_NET_PROTOCOL_ERROR, "stats: invalid packet start: '%s'\n", szBuf);
char *pszCur = &szBuf[sizeof(g_szStartStats)];
/*
* Parse it.
*/
static const char * const s_apszNames[] =
{
"cTx", "cRx", "cEchos", "cErrors", "cNsElapsed"
};
uint64_t *apu64[RT_ELEMENTS(s_apszNames)] =
{
&pStats->cTx,
&pStats->cRx,
&pStats->cEchos,
&pStats->cErrors,
&pStats->cNsElapsed
};
for (unsigned i = 0; i < RT_ELEMENTS(apu64); i++)
{
if (!pszCur)
return RTTestIFailedRc(VERR_PARSE_ERROR, "stats: missing %s\n", s_apszNames[i]);
char *pszNext = strchr(pszCur, ':');
if (pszNext)
*pszNext++ = '\0';
rc = RTStrToUInt64Full(pszCur, 10, apu64[i]);
if (rc != VINF_SUCCESS)
return RTTestIFailedRc(RT_SUCCESS(rc) ? -rc : rc, "stats: bad value for %s: '%s' - %Rrc\n",
s_apszNames[i], pszCur, rc);
pszCur = pszNext;
}
if (pszCur)
return RTTestIFailedRc(VERR_PARSE_ERROR, "stats: Unparsed data: '%s'\n", pszCur);
/*
* Send ACK.
*/
rc = RTTcpWrite(hSocket, g_szAck, sizeof(g_szAck) - 1);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "stats: failed to write ack: %Rrc\n", rc);
return VINF_SUCCESS;
}
/**
* TCP Throughput: Print the statistics.
*
* @param pSendStats Send stats.
* @param pRecvStats Receive stats.
* @param cbPacket Packet size.
*/
static void netperfPrintThroughputStats(NETPERFSTATS const *pSendStats, NETPERFSTATS const *pRecvStats, uint32_t cbPacket)
{
RTTestIValue("Packet size", cbPacket, RTTESTUNIT_BYTES);
if (pSendStats)
{
double rdSecElapsed = (double)pSendStats->cNsElapsed / 1000000000.0;
RTTestIValue("Sends", pSendStats->cTx, RTTESTUNIT_PACKETS);
RTTestIValue("Send Interval", pSendStats->cNsElapsed, RTTESTUNIT_NS);
RTTestIValue("Send Throughput", (uint64_t)((double)(cbPacket * pSendStats->cTx) / rdSecElapsed), RTTESTUNIT_BYTES_PER_SEC);
RTTestIValue("Send Rate", (uint64_t)((double)pSendStats->cTx / rdSecElapsed), RTTESTUNIT_PACKETS_PER_SEC);
RTTestIValue("Send Latency", (uint64_t)(rdSecElapsed / (double)pSendStats->cTx * 1000000000.0), RTTESTUNIT_NS_PER_PACKET);
}
if (pRecvStats)
{
double rdSecElapsed = (double)pRecvStats->cNsElapsed / 1000000000.0;
RTTestIValue("Receives", pRecvStats->cRx, RTTESTUNIT_PACKETS);
RTTestIValue("Receive Interval", pRecvStats->cNsElapsed, RTTESTUNIT_NS);
RTTestIValue("Receive Throughput", (uint64_t)((double)(cbPacket * pRecvStats->cRx) / rdSecElapsed), RTTESTUNIT_BYTES_PER_SEC);
RTTestIValue("Receive Rate", (uint64_t)((double)pRecvStats->cRx / rdSecElapsed), RTTESTUNIT_PACKETS_PER_SEC);
RTTestIValue("Receive Latency", (uint64_t)(rdSecElapsed / (double)pRecvStats->cRx * 1000000000.0), RTTESTUNIT_NS_PER_PACKET);
}
}
/**
* TCP Throughput: Send data to the other party.
*
* @returns IPRT status code.
* @param pParams The TCP parameters block.
* @param pBuf The buffer we're using when sending.
* @param pSendStats Where to return the statistics.
*/
static int netperfTCPThroughputSend(NETPERFPARAMS const *pParams, NETPERFHDR *pBuf, NETPERFSTATS *pSendStats)
{
RT_ZERO(*pSendStats);
/*
* Create the timer
*/
RTTIMERLR hTimer;
bool volatile fStop = false;
int rc = RTTimerLRCreateEx(&hTimer, 0 /* nsec */, RTTIMER_FLAGS_CPU_ANY, netperfStopTimerCallback, (void *)&fStop);
if (RT_SUCCESS(rc))
{
uint32_t u32Seq = 0;
RT_BZERO(pBuf, pParams->cbPacket);
pBuf->u32Magic = RT_H2LE_U32_C(NETPERFHDR_MAGIC);
pBuf->u32State = 0;
pBuf->u32Seq = 0;
pBuf->u32Reserved = 0;
/*
* Warm up.
*/
if (g_uVerbosity > 0)
RTPrintf("Warmup...\n");
pBuf->u32State = RT_H2LE_U32_C(NETPERFHDR_WARMUP);
rc = RTTimerLRStart(hTimer, pParams->cMsWarmup * UINT64_C(1000000) /* nsec */);
if (RT_SUCCESS(rc))
{
while (!fStop)
{
u32Seq++;
pBuf->u32Seq = RT_H2LE_U32(u32Seq);
rc = RTTcpWrite(pParams->hSocket, pBuf, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpWrite/warmup: %Rrc\n", rc);
break;
}
}
}
else
RTTestIFailed("RTTimerLRStart/warmup: %Rrc\n", rc);
/*
* The real thing.
*/
if (RT_SUCCESS(rc))
{
if (g_uVerbosity > 0)
RTPrintf("The real thing...\n");
pBuf->u32State = RT_H2LE_U32_C(NETPERFHDR_TESTING);
fStop = false;
rc = RTTimerLRStart(hTimer, pParams->cSecTimeout * UINT64_C(1000000000) /* nsec */);
if (RT_SUCCESS(rc))
{
uint64_t u64StartTS = RTTimeNanoTS();
while (!fStop)
{
u32Seq++;
pBuf->u32Seq = RT_H2LE_U32(u32Seq);
rc = RTTcpWrite(pParams->hSocket, pBuf, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpWrite/testing: %Rrc\n", rc);
break;
}
pSendStats->cTx++;
}
pSendStats->cNsElapsed = RTTimeNanoTS() - u64StartTS;
}
else
RTTestIFailed("RTTimerLRStart/testing: %Rrc\n", rc);
}
/*
* Cool down.
*/
if (RT_SUCCESS(rc))
{
if (g_uVerbosity > 0)
RTPrintf("Cool down...\n");
pBuf->u32State = RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN);
fStop = false;
rc = RTTimerLRStart(hTimer, pParams->cMsCoolDown * UINT64_C(1000000) /* nsec */);
if (RT_SUCCESS(rc))
{
while (!fStop)
{
u32Seq++;
pBuf->u32Seq = RT_H2LE_U32(u32Seq);
rc = RTTcpWrite(pParams->hSocket, pBuf, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpWrite/cool down: %Rrc\n", rc);
break;
}
}
}
else
RTTestIFailed("RTTimerLRStart/testing: %Rrc\n", rc);
}
/*
* Send DONE packet.
*/
if (g_uVerbosity > 0)
RTPrintf("Done\n");
if (RT_SUCCESS(rc))
{
u32Seq++;
pBuf->u32Seq = RT_H2LE_U32(u32Seq);
pBuf->u32State = RT_H2LE_U32_C(NETPERFHDR_DONE);
rc = RTTcpWrite(pParams->hSocket, pBuf, pParams->cbPacket);
if (RT_FAILURE(rc))
RTTestIFailed("RTTcpWrite/done: %Rrc\n", rc);
}
RTTimerLRDestroy(hTimer);
}
else
RTTestIFailed("Failed to create timer object: %Rrc\n", rc);
return rc;
}
/**
* TCP Throughput: Receive data from the other party.
*
* @returns IPRT status code.
* @param pParams The TCP parameters block.
* @param pBuf The buffer we're using when sending.
* @param pStats Where to return the statistics.
*/
static int netperfTCPThroughputRecv(NETPERFPARAMS const *pParams, NETPERFHDR *pBuf, NETPERFSTATS *pStats)
{
RT_ZERO(*pStats);
int rc;
uint32_t u32Seq = 0;
uint64_t cRx = 0;
uint64_t u64StartTS = 0;
uint32_t uState = RT_H2LE_U32_C(NETPERFHDR_WARMUP);
for (;;)
{
rc = RTTcpRead(pParams->hSocket, pBuf, pParams->cbPacket, NULL);
if (RT_FAILURE(rc))
{
pStats->cErrors++;
RTTestIFailed("RTTcpRead failed: %Rrc\n", rc);
break;
}
if (RT_UNLIKELY( pBuf->u32Magic != RT_H2LE_U32_C(NETPERFHDR_MAGIC)
|| pBuf->u32Reserved != 0))
{
pStats->cErrors++;
RTTestIFailed("Invalid magic or reserved field value: %#x %#x\n", RT_H2LE_U32(pBuf->u32Magic), RT_H2LE_U32(pBuf->u32Reserved));
rc = VERR_INVALID_MAGIC;
break;
}
u32Seq += 1;
if (RT_UNLIKELY(pBuf->u32Seq != RT_H2LE_U32(u32Seq)))
{
pStats->cErrors++;
RTTestIFailed("Out of sequence: got %#x, expected %#x\n", RT_H2LE_U32(pBuf->u32Seq), u32Seq);
rc = VERR_WRONG_ORDER;
break;
}
if (pParams->fCheckData && uState == RT_H2LE_U32_C(NETPERFHDR_TESTING))
{
unsigned i = sizeof(NETPERFHDR);
for (;i < pParams->cbPacket; ++i)
if (((unsigned char *)pBuf)[i])
break;
if (i != pParams->cbPacket)
{
pStats->cErrors++;
RTTestIFailed("Broken payload: at %#x got %#x, expected %#x\n", i, ((unsigned char *)pBuf)[i], 0);
rc = VERR_NOT_EQUAL;
break;
}
}
if (RT_LIKELY(pBuf->u32State == uState))
cRx++;
/*
* Validate and act on switch state.
*/
else if ( uState == RT_H2LE_U32_C(NETPERFHDR_WARMUP)
&& pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_TESTING))
{
cRx = 0;
u64StartTS = RTTimeNanoTS();
uState = pBuf->u32State;
}
else if ( uState == RT_H2LE_U32_C(NETPERFHDR_TESTING)
&& ( pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN)
|| pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_DONE)) )
{
pStats->cNsElapsed = RTTimeNanoTS() - u64StartTS;
pStats->cRx = cRx + 1;
uState = pBuf->u32State;
if (uState == RT_H2LE_U32_C(NETPERFHDR_DONE))
break;
}
else if ( uState == RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN)
&& pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_DONE))
{
uState = pBuf->u32State;
break;
}
else
{
pStats->cErrors++;
RTTestIFailed("Protocol error: invalid state transition %#x -> %#x\n",
RT_LE2H_U32(uState), RT_LE2H_U32(pBuf->u32State));
rc = VERR_INVALID_MAGIC;
break;
}
}
AssertReturn(uState == RT_H2LE_U32_C(NETPERFHDR_DONE) || RT_FAILURE(rc), VERR_INVALID_STATE);
return rc;
}
/**
* Prints the statistics for the latency test.
*
* @param pStats The statistics.
* @param cbPacket The packet size in bytes.
*/
static void netperfPrintLatencyStats(NETPERFSTATS const *pStats, uint32_t cbPacket)
{
double rdSecElapsed = (double)pStats->cNsElapsed / 1000000000.0;
RTTestIValue("Transmitted", pStats->cTx, RTTESTUNIT_PACKETS);
RTTestIValue("Successful echos", pStats->cEchos, RTTESTUNIT_PACKETS);
RTTestIValue("Errors", pStats->cErrors, RTTESTUNIT_PACKETS);
RTTestIValue("Interval", pStats->cNsElapsed, RTTESTUNIT_NS);
RTTestIValue("Packet size", cbPacket, RTTESTUNIT_BYTES);
RTTestIValue("Average rate", (uint64_t)((double)pStats->cEchos / rdSecElapsed), RTTESTUNIT_PACKETS_PER_SEC);
RTTestIValue("Average throughput", (uint64_t)((double)(cbPacket * pStats->cEchos) / rdSecElapsed), RTTESTUNIT_BYTES_PER_SEC);
RTTestIValue("Average latency", (uint64_t)(rdSecElapsed / (double)pStats->cEchos * 1000000000.0), RTTESTUNIT_NS_PER_ROUND_TRIP);
RTTestISubDone();
}
/**
* NETPERFMODE -> string.
*
* @returns readonly string.
* @param enmMode The mode.
*/
static const char *netperfModeToString(NETPERFMODE enmMode)
{
switch (enmMode)
{
case NETPERFMODE_LATENCY: return "latency";
case NETPERFMODE_THROUGHPUT: return "throughput";
case NETPERFMODE_THROUGHPUT_XMIT: return "throughput-xmit";
case NETPERFMODE_THROUGHPUT_RECV: return "throughput-recv";
default: AssertFailed(); return "internal-error";
}
}
/**
* String -> NETPERFMODE.
*
* @returns The corresponding NETPERFMODE, NETPERFMODE_INVALID on failure.
* @param pszMode The mode string.
*/
static NETPERFMODE netperfModeFromString(const char *pszMode)
{
if (!strcmp(pszMode, "latency"))
return NETPERFMODE_LATENCY;
if ( !strcmp(pszMode, "throughput")
|| !strcmp(pszMode, "thruput") )
return NETPERFMODE_THROUGHPUT;
if ( !strcmp(pszMode, "throughput-xmit")
|| !strcmp(pszMode, "thruput-xmit")
|| !strcmp(pszMode, "xmit") )
return NETPERFMODE_THROUGHPUT_XMIT;
if ( !strcmp(pszMode, "throughput-recv")
|| !strcmp(pszMode, "thruput-recv")
|| !strcmp(pszMode, "recv") )
return NETPERFMODE_THROUGHPUT_RECV;
return NETPERFMODE_INVALID;
}
/**
* TCP Server: Throughput test.
*
* @returns IPRT status code.
* @param pParams The parameters to use for this test.
*/
static int netperfTCPServerDoThroughput(NETPERFPARAMS const *pParams)
{
/*
* Allocate the buffer.
*/
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
/*
* Receive first, then Send. The reverse of the client.
*/
NETPERFSTATS RecvStats;
int rc = netperfTCPThroughputRecv(pParams, pBuf, &RecvStats);
if (RT_SUCCESS(rc))
{
rc = netperfSendStats(&RecvStats, pParams->hSocket);
if (RT_SUCCESS(rc))
{
NETPERFSTATS SendStats;
rc = netperfTCPThroughputSend(pParams, pBuf, &SendStats);
if (RT_SUCCESS(rc))
{
rc = netperfSendStats(&SendStats, pParams->hSocket);
netperfPrintThroughputStats(&SendStats, &RecvStats, pParams->cbPacket);
}
}
}
return rc;
}
/**
* TCP Server: Throughput xmit test (receive from client).
*
* @returns IPRT status code.
* @param pParams The parameters to use for this test.
*/
static int netperfTCPServerDoThroughputXmit(NETPERFPARAMS const *pParams)
{
/*
* Allocate the buffer.
*/
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
/*
* Receive the transmitted data (reverse of client).
*/
NETPERFSTATS RecvStats;
int rc = netperfTCPThroughputRecv(pParams, pBuf, &RecvStats);
if (RT_SUCCESS(rc))
{
rc = netperfSendStats(&RecvStats, pParams->hSocket);
if (RT_SUCCESS(rc))
netperfPrintThroughputStats(NULL, &RecvStats, pParams->cbPacket);
}
return rc;
}
/**
* TCP Server: Throughput recv test (transmit to client).
*
* @returns IPRT status code.
* @param pParams The parameters to use for this test.
*/
static int netperfTCPServerDoThroughputRecv(NETPERFPARAMS const *pParams)
{
/*
* Allocate the buffer.
*/
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
/*
* Send data to the client (reverse of client).
*/
NETPERFSTATS SendStats;
int rc = netperfTCPThroughputSend(pParams, pBuf, &SendStats);
if (RT_SUCCESS(rc))
{
rc = netperfSendStats(&SendStats, pParams->hSocket);
if (RT_SUCCESS(rc))
netperfPrintThroughputStats(&SendStats, NULL, pParams->cbPacket);
}
return rc;
}
/**
* TCP Server: Latency test.
*
* @returns IPRT status code.
* @param pParams The parameters to use for this test.
*/
static int netperfTCPServerDoLatency(NETPERFPARAMS const *pParams)
{
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Failed to allocated packet buffer of %u bytes.\n", pParams->cbPacket);
/*
* Ping pong with client.
*/
int rc;
uint32_t uState = RT_H2LE_U32_C(NETPERFHDR_WARMUP);
uint32_t u32Seq = 0;
uint64_t cTx = 0;
uint64_t cRx = 0;
uint64_t u64StartTS = 0;
NETPERFSTATS Stats;
RT_ZERO(Stats);
for (;;)
{
rc = RTTcpRead(pParams->hSocket, pBuf, pParams->cbPacket, NULL);
if (RT_FAILURE(rc))
{
RTTestIFailed("Failed to read data from client: %Rrc\n", rc);
break;
}
/*
* Validate the packet
*/
if (RT_UNLIKELY( pBuf->u32Magic != RT_H2LE_U32_C(NETPERFHDR_MAGIC)
|| pBuf->u32Reserved != 0))
{
RTTestIFailed("Invalid magic or reserved field value: %#x %#x\n", RT_H2LE_U32(pBuf->u32Magic), RT_H2LE_U32(pBuf->u32Reserved));
rc = VERR_INVALID_MAGIC;
break;
}
u32Seq += 1;
if (RT_UNLIKELY(pBuf->u32Seq != RT_H2LE_U32(u32Seq)))
{
RTTestIFailed("Out of sequence: got %#x, expected %#x\n", RT_H2LE_U32(pBuf->u32Seq), u32Seq);
rc = VERR_WRONG_ORDER;
break;
}
/*
* Count the packet if the state remains unchanged.
*/
if (RT_LIKELY(pBuf->u32State == uState))
cRx++;
/*
* Validate and act on the state transition.
*/
else if ( uState == RT_H2LE_U32_C(NETPERFHDR_WARMUP)
&& pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_TESTING))
{
cRx = cTx = 0;
u64StartTS = RTTimeNanoTS();
uState = pBuf->u32State;
}
else if ( uState == RT_H2LE_U32_C(NETPERFHDR_TESTING)
&& ( pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN)
|| pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_DONE)) )
{
Stats.cNsElapsed = RTTimeNanoTS() - u64StartTS;
Stats.cEchos = cTx;
Stats.cTx = cTx;
Stats.cRx = cRx;
uState = pBuf->u32State;
if (uState == RT_H2LE_U32_C(NETPERFHDR_DONE))
break;
}
else if ( uState == RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN)
&& pBuf->u32State == RT_H2LE_U32_C(NETPERFHDR_DONE))
{
uState = pBuf->u32State;
break;
}
else
{
RTTestIFailed("Protocol error: invalid state transition %#x -> %#x\n",
RT_LE2H_U32(uState), RT_LE2H_U32(pBuf->u32State));
break;
}
/*
* Write same data back to client.
*/
rc = RTTcpWrite(pParams->hSocket, pBuf, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("Failed to write data to client: %Rrc\n", rc);
break;
}
cTx++;
}
/*
* Send stats to client and print them.
*/
if (uState == RT_H2LE_U32_C(NETPERFHDR_DONE))
netperfSendStats(&Stats, pParams->hSocket);
if ( uState == RT_H2LE_U32_C(NETPERFHDR_DONE)
|| uState == RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN))
netperfPrintLatencyStats(&Stats, pParams->cbPacket);
RTMemFree(pBuf);
return rc;
}
/**
* Parses the parameters the client has sent us.
*
* @returns IPRT status code. Message has been shown on failure.
* @param pParams The parameter structure to store the parameters
* in.
* @param pszParams The parameter string sent by the client.
*/
static int netperfTCPServerParseParams(NETPERFPARAMS *pParams, char *pszParams)
{
/*
* Set defaults for the dynamic settings.
*/
pParams->fNoDelay = false;
pParams->enmMode = NETPERFMODE_LATENCY;
pParams->cSecTimeout = NETPERF_DEFAULT_WARMUP;
pParams->cMsCoolDown = NETPERF_DEFAULT_COOL_DOWN;
pParams->cMsWarmup = NETPERF_DEFAULT_WARMUP;
pParams->cbPacket = NETPERF_DEFAULT_PKT_SIZE_LATENCY;
/*
* Parse the client parameters.
*/
/* first arg: transport type. [mandatory] */
char *pszCur = strchr(pszParams, ':');
if (!pszCur)
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: No colon\n");
char *pszNext = strchr(++pszCur, ':');
if (pszNext)
*pszNext++ = '\0';
if (strcmp(pszCur, "TCP"))
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: Invalid transport type: \"%s\"\n", pszCur);
pszCur = pszNext;
/* second arg: mode. [mandatory] */
if (!pszCur)
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: Missing test mode\n");
pszNext = strchr(pszCur, ':');
if (pszNext)
*pszNext++ = '\0';
pParams->enmMode = netperfModeFromString(pszCur);
if (pParams->enmMode == NETPERFMODE_INVALID)
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: Invalid test mode: \"%s\"\n", pszCur);
pszCur = pszNext;
/*
* The remainder are uint32_t or bool.
*/
struct
{
bool fBool;
bool fMandatory;
void *pvValue;
uint32_t uMin;
uint32_t uMax;
const char *pszName;
} aElements[] =
{
{ false, true, &pParams->cSecTimeout, NETPERF_MIN_TIMEOUT, NETPERF_MAX_TIMEOUT, "timeout" },
{ false, true, &pParams->cbPacket, NETPERF_MIN_PKT_SIZE, NETPERF_MAX_PKT_SIZE, "packet size" },
{ false, true, &pParams->cMsWarmup, NETPERF_MIN_WARMUP, NETPERF_MAX_WARMUP, "warmup period" },
{ false, true, &pParams->cMsCoolDown, NETPERF_MIN_COOL_DOWN, NETPERF_MAX_COOL_DOWN, "cool down period" },
{ true, true, &pParams->fNoDelay, false, true, "no delay" },
};
for (unsigned i = 0; i < RT_ELEMENTS(aElements); i++)
{
if (!pszCur)
return aElements[i].fMandatory
? RTTestIFailedRc(VERR_PARSE_ERROR, "client params: missing %s\n", aElements[i].pszName)
: VINF_SUCCESS;
pszNext = strchr(pszCur, ':');
if (pszNext)
*pszNext++ = '\0';
uint32_t u32;
int rc = RTStrToUInt32Full(pszCur, 10, &u32);
if (rc != VINF_SUCCESS)
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: bad %s value \"%s\": %Rrc\n",
aElements[i].pszName, pszCur, rc);
if ( u32 < aElements[i].uMin
|| u32 > aElements[i].uMax)
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: %s %u s is out of range (%u..%u)\n",
aElements[i].pszName, u32, aElements[i].uMin, aElements[i].uMax);
if (aElements[i].fBool)
*(bool *)aElements[i].pvValue = u32 ? true : false;
else
*(uint32_t *)aElements[i].pvValue = u32;
pszCur = pszNext;
}
/* Fail if too many elements. */
if (pszCur)
return RTTestIFailedRc(VERR_PARSE_ERROR, "client params: too many elements: \"%s\"\n",
pszCur);
return VINF_SUCCESS;
}
/**
* TCP server callback that handles one client connection.
*
* @returns IPRT status code. VERR_TCP_SERVER_STOP is special.
* @param hSocket The client socket.
* @param pvUser Our parameters.
*/
static DECLCALLBACK(int) netperfTCPServerWorker(RTSOCKET hSocket, void *pvUser)
{
NETPERFPARAMS *pParams = (NETPERFPARAMS *)pvUser;
AssertReturn(pParams, VERR_INVALID_POINTER);
pParams->hSocket = hSocket;
RTNETADDR Addr;
int rc = RTTcpGetPeerAddress(hSocket, &Addr);
if (RT_SUCCESS(rc))
RTTestIPrintf(RTTESTLVL_ALWAYS, "Client connected from %RTnaddr\n", &Addr);
else
{
RTTestIPrintf(RTTESTLVL_ALWAYS, "Failed to get client details: %Rrc\n", rc);
Addr.enmType = RTNETADDRTYPE_INVALID;
}
/*
* Adjust send and receive buffer sizes if necessary.
*/
if (pParams->cbBufferSize)
{
rc = RTTcpSetBufferSize(hSocket, pParams->cbBufferSize);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to set socket buffer sizes to %#x: %Rrc\n", pParams->cbBufferSize, rc);
}
/*
* Greet the other dude.
*/
rc = RTTcpWrite(hSocket, g_ConnectStart, sizeof(g_ConnectStart) - 1);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to send connection start Id: %Rrc\n", rc);
/*
* Read connection parameters.
*/
char szBuf[256];
rc = RTTcpRead(hSocket, szBuf, NETPERF_LEN_PREFIX, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to read connection parameters: %Rrc\n", rc);
szBuf[NETPERF_LEN_PREFIX] = '\0';
uint32_t cchParams;
rc = RTStrToUInt32Full(szBuf, 10, &cchParams);
if (rc != VINF_SUCCESS)
return RTTestIFailedRc(RT_SUCCESS(rc) ? VERR_INTERNAL_ERROR : rc,
"Failed to read connection parameters: %Rrc\n", rc);
if (cchParams >= sizeof(szBuf))
return RTTestIFailedRc(VERR_TOO_MUCH_DATA, "parameter packet is too big (%u bytes)\n", cchParams);
rc = RTTcpRead(hSocket, szBuf, cchParams, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to read connection parameters: %Rrc\n", rc);
szBuf[cchParams] = '\0';
if (strncmp(szBuf, g_szStartParams, sizeof(g_szStartParams) - 1))
return RTTestIFailedRc(VERR_NET_PROTOCOL_ERROR, "Invalid connection parameters '%s'\n", szBuf);
/*
* Parse the parameters and signal whether we've got a deal or not.
*/
rc = netperfTCPServerParseParams(pParams, szBuf);
if (RT_FAILURE(rc))
{
int rc2 = RTTcpWrite(hSocket, g_szNegative, sizeof(g_szNegative) - 1);
if (RT_FAILURE(rc2))
RTTestIFailed("Failed to send negative ack: %Rrc\n", rc2);
return rc;
}
if (Addr.enmType != RTNETADDRTYPE_INVALID)
RTTestISubF("%RTnaddr - %s, %u s, %u bytes", &Addr,
netperfModeToString(pParams->enmMode), pParams->cSecTimeout, pParams->cbPacket);
else
RTTestISubF("Unknown - %s, %u s, %u bytes",
netperfModeToString(pParams->enmMode), pParams->cSecTimeout, pParams->cbPacket);
rc = RTTcpSetSendCoalescing(hSocket, !pParams->fNoDelay);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to apply no-delay option (%RTbool): %Rrc\n", pParams->fNoDelay, rc);
rc = RTTcpWrite(hSocket, g_szAck, sizeof(g_szAck) - 1);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to send start test commend to client: %Rrc\n", rc);
/*
* Take action according to our mode.
*/
switch (pParams->enmMode)
{
case NETPERFMODE_LATENCY:
rc = netperfTCPServerDoLatency(pParams);
break;
case NETPERFMODE_THROUGHPUT:
rc = netperfTCPServerDoThroughput(pParams);
break;
case NETPERFMODE_THROUGHPUT_XMIT:
rc = netperfTCPServerDoThroughputXmit(pParams);
break;
case NETPERFMODE_THROUGHPUT_RECV:
rc = netperfTCPServerDoThroughputRecv(pParams);
break;
case NETPERFMODE_INVALID:
rc = VERR_INTERNAL_ERROR;
break;
/* no default! */
}
if (rc == VERR_NO_MEMORY)
return VERR_TCP_SERVER_STOP;
/*
* Wait for other clients or quit.
*/
if (pParams->fSingleClient)
return VERR_TCP_SERVER_STOP;
return VINF_SUCCESS;
}
/**
* TCP server.
*
* @returns IPRT status code.
* @param pParams The TCP parameter block.
*/
static int netperfTCPServer(NETPERFPARAMS *pParams)
{
/*
* Spawn the TCP server thread & listen.
*/
PRTTCPSERVER pServer;
int rc = RTTcpServerCreateEx(NULL, pParams->uPort, &pServer);
if (RT_SUCCESS(rc))
{
RTPrintf("Server listening on TCP port %d\n", pParams->uPort);
rc = RTTcpServerListen(pServer, netperfTCPServerWorker, pParams);
RTTcpServerDestroy(pServer);
}
else
RTPrintf("Failed to create TCP server thread: %Rrc\n", rc);
return rc;
}
/**
* The server part.
*
* @returns Exit code.
* @param enmProto The protocol.
* @param pParams The parameter block.
*/
static RTEXITCODE netperfServer(NETPERFPROTO enmProto, NETPERFPARAMS *pParams)
{
switch (enmProto)
{
case NETPERFPROTO_TCP:
{
int rc = netperfTCPServer(pParams);
return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
}
default:
RTTestIFailed("Protocol not supported.\n");
return RTEXITCODE_FAILURE;
}
}
/**
* TCP client: Do the throughput test.
*
* @returns IPRT status code
* @param pParams The parameters.
*/
static int netperfTCPClientDoThroughput(NETPERFPARAMS *pParams)
{
/*
* Allocate the buffer.
*/
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
/*
* Send first, then Receive.
*/
NETPERFSTATS SendStats;
int rc = netperfTCPThroughputSend(pParams, pBuf, &SendStats);
if (RT_SUCCESS(rc))
{
NETPERFSTATS SrvSendStats;
rc = netperfRecvStats(&SrvSendStats, pParams->hSocket);
if (RT_SUCCESS(rc))
{
NETPERFSTATS RecvStats;
rc = netperfTCPThroughputRecv(pParams, pBuf, &RecvStats);
if (RT_SUCCESS(rc))
{
NETPERFSTATS SrvRecvStats;
rc = netperfRecvStats(&SrvRecvStats, pParams->hSocket);
if (RT_SUCCESS(rc))
{
if (pParams->fServerStats)
netperfPrintThroughputStats(&SrvSendStats, &SrvRecvStats, pParams->cbPacket);
else
netperfPrintThroughputStats(&SendStats, &RecvStats, pParams->cbPacket);
}
}
}
}
RTTestISubDone();
return rc;
}
/**
* TCP client: Do the throughput xmit test.
*
* @returns IPRT status code
* @param pParams The parameters.
*/
static int netperfTCPClientDoThroughputXmit(NETPERFPARAMS *pParams)
{
/*
* Allocate the buffer.
*/
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
/*
* Do the job.
*/
NETPERFSTATS SendStats;
int rc = netperfTCPThroughputSend(pParams, pBuf, &SendStats);
if (RT_SUCCESS(rc))
{
NETPERFSTATS SrvSendStats;
rc = netperfRecvStats(&SrvSendStats, pParams->hSocket);
if (RT_SUCCESS(rc))
{
if (pParams->fServerStats)
netperfPrintThroughputStats(&SrvSendStats, NULL, pParams->cbPacket);
else
netperfPrintThroughputStats(&SendStats, NULL, pParams->cbPacket);
}
}
RTTestISubDone();
return rc;
}
/**
* TCP client: Do the throughput recv test.
*
* @returns IPRT status code
* @param pParams The parameters.
*/
static int netperfTCPClientDoThroughputRecv(NETPERFPARAMS *pParams)
{
/*
* Allocate the buffer.
*/
NETPERFHDR *pBuf = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!pBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
/*
* Do the job.
*/
NETPERFSTATS RecvStats;
int rc = netperfTCPThroughputRecv(pParams, pBuf, &RecvStats);
if (RT_SUCCESS(rc))
{
NETPERFSTATS SrvRecvStats;
rc = netperfRecvStats(&SrvRecvStats, pParams->hSocket);
if (RT_SUCCESS(rc))
{
if (pParams->fServerStats)
netperfPrintThroughputStats(NULL, &SrvRecvStats, pParams->cbPacket);
else
netperfPrintThroughputStats(NULL, &RecvStats, pParams->cbPacket);
}
}
RTTestISubDone();
return rc;
}
/**
* TCP client: Do the latency test.
*
* @returns IPRT status code
* @param pParams The parameters.
*/
static int netperfTCPClientDoLatency(NETPERFPARAMS *pParams)
{
/*
* Generate a selection of packages before we start, after all we're not
* benchmarking the random number generator, are we. :-)
*/
void *pvReadBuf = RTMemAllocZ(pParams->cbPacket);
if (!pvReadBuf)
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
size_t i;
NETPERFHDR *apPackets[256];
for (i = 0; i < RT_ELEMENTS(apPackets); i++)
{
apPackets[i] = (NETPERFHDR *)RTMemAllocZ(pParams->cbPacket);
if (!apPackets[i])
{
while (i-- > 0)
RTMemFree(apPackets[i]);
RTMemFree(pvReadBuf);
return RTTestIFailedRc(VERR_NO_MEMORY, "Out of memory");
}
RTRandBytes(apPackets[i], pParams->cbPacket);
apPackets[i]->u32Magic = RT_H2LE_U32_C(NETPERFHDR_MAGIC);
apPackets[i]->u32State = 0;
apPackets[i]->u32Seq = 0;
apPackets[i]->u32Reserved = 0;
}
/*
* Create & start a timer to eventually disconnect.
*/
bool volatile fStop = false;
RTTIMERLR hTimer;
int rc = RTTimerLRCreateEx(&hTimer, 0 /* nsec */, RTTIMER_FLAGS_CPU_ANY, netperfStopTimerCallback, (void *)&fStop);
if (RT_SUCCESS(rc))
{
uint32_t u32Seq = 0;
NETPERFSTATS Stats;
RT_ZERO(Stats);
/*
* Warm up.
*/
if (g_uVerbosity > 0)
RTPrintf("Warmup...\n");
rc = RTTimerLRStart(hTimer, pParams->cMsWarmup * UINT64_C(1000000) /* nsec */);
if (RT_SUCCESS(rc))
{
while (!fStop)
{
NETPERFHDR *pPacket = apPackets[u32Seq % RT_ELEMENTS(apPackets)];
u32Seq++;
pPacket->u32Seq = RT_H2LE_U32(u32Seq);
pPacket->u32State = RT_H2LE_U32_C(NETPERFHDR_WARMUP);
rc = RTTcpWrite(pParams->hSocket, pPacket, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpWrite/warmup: %Rrc\n", rc);
break;
}
rc = RTTcpRead(pParams->hSocket, pvReadBuf, pParams->cbPacket, NULL);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpRead/warmup: %Rrc\n", rc);
break;
}
}
}
else
RTTestIFailed("RTTimerLRStart/warmup: %Rrc\n", rc);
/*
* The real thing.
*/
if (RT_SUCCESS(rc))
{
if (g_uVerbosity > 0)
RTPrintf("The real thing...\n");
fStop = false;
rc = RTTimerLRStart(hTimer, pParams->cSecTimeout * UINT64_C(1000000000) /* nsec */);
if (RT_SUCCESS(rc))
{
uint64_t u64StartTS = RTTimeNanoTS();
while (!fStop)
{
NETPERFHDR *pPacket = apPackets[u32Seq % RT_ELEMENTS(apPackets)];
u32Seq++;
pPacket->u32Seq = RT_H2LE_U32(u32Seq);
pPacket->u32State = RT_H2LE_U32_C(NETPERFHDR_TESTING);
rc = RTTcpWrite(pParams->hSocket, pPacket, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpWrite/testing: %Rrc\n", rc);
break;
}
Stats.cTx++;
rc = RTTcpRead(pParams->hSocket, pvReadBuf, pParams->cbPacket, NULL);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpRead/testing: %Rrc\n", rc);
break;
}
Stats.cRx++;
if (!memcmp(pvReadBuf, pPacket, pParams->cbPacket))
Stats.cEchos++;
else
Stats.cErrors++;
}
Stats.cNsElapsed = RTTimeNanoTS() - u64StartTS;
}
else
RTTestIFailed("RTTimerLRStart/testing: %Rrc\n", rc);
}
/*
* Cool down.
*/
if (RT_SUCCESS(rc))
{
if (g_uVerbosity > 0)
RTPrintf("Cool down...\n");
fStop = false;
rc = RTTimerLRStart(hTimer, pParams->cMsCoolDown * UINT64_C(1000000) /* nsec */);
if (RT_SUCCESS(rc))
{
while (!fStop)
{
NETPERFHDR *pPacket = apPackets[u32Seq % RT_ELEMENTS(apPackets)];
u32Seq++;
pPacket->u32Seq = RT_H2LE_U32(u32Seq);
pPacket->u32State = RT_H2LE_U32_C(NETPERFHDR_COOL_DOWN);
rc = RTTcpWrite(pParams->hSocket, pPacket, pParams->cbPacket);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpWrite/warmup: %Rrc\n", rc);
break;
}
rc = RTTcpRead(pParams->hSocket, pvReadBuf, pParams->cbPacket, NULL);
if (RT_FAILURE(rc))
{
RTTestIFailed("RTTcpRead/warmup: %Rrc\n", rc);
break;
}
}
}
else
RTTestIFailed("RTTimerLRStart/testing: %Rrc\n", rc);
}
/*
* Send DONE packet.
*/
if (g_uVerbosity > 0)
RTPrintf("Done\n");
if (RT_SUCCESS(rc))
{
u32Seq++;
NETPERFHDR *pPacket = apPackets[u32Seq % RT_ELEMENTS(apPackets)];
pPacket->u32Seq = RT_H2LE_U32(u32Seq);
pPacket->u32State = RT_H2LE_U32_C(NETPERFHDR_DONE);
rc = RTTcpWrite(pParams->hSocket, pPacket, pParams->cbPacket);
if (RT_FAILURE(rc))
RTTestIFailed("RTTcpWrite/done: %Rrc\n", rc);
}
/*
* Get and print stats.
*/
NETPERFSTATS SrvStats;
if (RT_SUCCESS(rc))
{
rc = netperfRecvStats(&SrvStats, pParams->hSocket);
if (RT_SUCCESS(rc) && pParams->fServerStats)
netperfPrintLatencyStats(&SrvStats, pParams->cbPacket);
else if (!pParams->fServerStats)
netperfPrintLatencyStats(&Stats, pParams->cbPacket);
}
/* clean up*/
RTTimerLRDestroy(hTimer);
}
else
RTTestIFailed("Failed to create timer object: %Rrc\n", rc);
for (i = 0; i < RT_ELEMENTS(apPackets); i++)
RTMemFree(apPackets[i]);
RTMemFree(pvReadBuf);
return rc;
}
/**
* TCP client test driver.
*
* @returns IPRT status code
* @param pszServer The server name.
* @param pParams The parameter structure.
*/
static int netperfTCPClient(const char *pszServer, NETPERFPARAMS *pParams)
{
AssertReturn(pParams, VERR_INVALID_POINTER);
RTTestISubF("TCP - %u s, %u bytes%s", pParams->cSecTimeout,
pParams->cbPacket, pParams->fNoDelay ? ", no delay" : "");
RTSOCKET hSocket = NIL_RTSOCKET;
int rc = RTTcpClientConnect(pszServer, pParams->uPort, &hSocket);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to connect to %s on port %u: %Rrc\n", pszServer, pParams->uPort, rc);
pParams->hSocket = hSocket;
/*
* Disable send coalescing (no-delay).
*/
if (pParams->fNoDelay)
{
rc = RTTcpSetSendCoalescing(hSocket, false /*fEnable*/);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to set no-delay option: %Rrc\n", rc);
}
/*
* Adjust send and receive buffer sizes if necessary.
*/
if (pParams->cbBufferSize)
{
rc = RTTcpSetBufferSize(hSocket, pParams->cbBufferSize);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to set socket buffer sizes to %#x: %Rrc\n", pParams->cbBufferSize, rc);
}
/*
* Verify the super secret Start Connect Id to start the connection.
*/
char szBuf[256 + NETPERF_LEN_PREFIX];
RT_ZERO(szBuf);
rc = RTTcpRead(hSocket, szBuf, sizeof(g_ConnectStart) - 1, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to read connection initializer: %Rrc\n", rc);
if (strcmp(szBuf, g_ConnectStart))
return RTTestIFailedRc(VERR_INVALID_MAGIC, "Invalid connection initializer '%s'\n", szBuf);
/*
* Send all the dynamic parameters to the server.
* (If the server is newer than the client, it will select default for any
* missing parameters.)
*/
size_t cchParams = RTStrPrintf(&szBuf[NETPERF_LEN_PREFIX], sizeof(szBuf) - NETPERF_LEN_PREFIX,
"%s:%s:%s:%u:%u:%u:%u:%u",
g_szStartParams,
"TCP",
netperfModeToString(pParams->enmMode),
pParams->cSecTimeout,
pParams->cbPacket,
pParams->cMsWarmup,
pParams->cMsCoolDown,
pParams->fNoDelay);
RTStrPrintf(szBuf, NETPERF_LEN_PREFIX + 1, "%0*u", NETPERF_LEN_PREFIX, cchParams);
szBuf[NETPERF_LEN_PREFIX] = g_szStartParams[0];
Assert(strlen(szBuf) == NETPERF_LEN_PREFIX + cchParams);
rc = RTTcpWrite(hSocket, szBuf, NETPERF_LEN_PREFIX + cchParams);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to send connection parameters: %Rrc\n", rc);
/*
* Wait for acknowledgment.
*/
rc = RTTcpRead(hSocket, szBuf, sizeof(g_szAck) - 1, NULL);
if (RT_FAILURE(rc))
return RTTestIFailedRc(rc, "Failed to send parameters: %Rrc\n", rc);
szBuf[sizeof(g_szAck) - 1] = '\0';
if (!strcmp(szBuf, g_szNegative))
return RTTestIFailedRc(VERR_NET_PROTOCOL_ERROR, "Server failed to accept packet size of %u bytes.\n", pParams->cbPacket);
if (strcmp(szBuf, g_szAck))
return RTTestIFailedRc(VERR_NET_PROTOCOL_ERROR, "Invalid response from server '%s'\n", szBuf);
/*
* Take action according to our mode.
*/
switch (pParams->enmMode)
{
case NETPERFMODE_LATENCY:
RTTestIPrintf(RTTESTLVL_ALWAYS, "Connected to %s port %u, running the latency test for %u seconds.\n",
pszServer, pParams->uPort, pParams->cSecTimeout);
rc = netperfTCPClientDoLatency(pParams);
break;
case NETPERFMODE_THROUGHPUT:
RTTestIPrintf(RTTESTLVL_ALWAYS, "Connected to %s port %u, running the throughput test for %u seconds in each direction.\n",
pszServer, pParams->uPort, pParams->cSecTimeout);
rc = netperfTCPClientDoThroughput(pParams);
break;
case NETPERFMODE_THROUGHPUT_XMIT:
RTTestIPrintf(RTTESTLVL_ALWAYS, "Connected to %s port %u, running the throughput-xmit test for %u seconds.\n",
pszServer, pParams->uPort, pParams->cSecTimeout);
rc = netperfTCPClientDoThroughputXmit(pParams);
break;
case NETPERFMODE_THROUGHPUT_RECV:
RTTestIPrintf(RTTESTLVL_ALWAYS, "Connected to %s port %u, running the throughput-recv test for %u seconds.\n",
pszServer, pParams->uPort, pParams->cSecTimeout);
rc = netperfTCPClientDoThroughputRecv(pParams);
break;
case NETPERFMODE_INVALID:
rc = VERR_INTERNAL_ERROR;
break;
/* no default! */
}
return rc;
}
/**
* The client part.
*
* @returns Exit code.
* @param enmProto The protocol.
* @param pszServer The server name.
* @param pvUser The parameter block as opaque user data.
*/
static RTEXITCODE netperfClient(NETPERFPROTO enmProto, const char *pszServer, void *pvUser)
{
switch (enmProto)
{
case NETPERFPROTO_TCP:
{
NETPERFPARAMS *pParams = (NETPERFPARAMS *)pvUser;
int rc = netperfTCPClient(pszServer, pParams);
if (pParams->hSocket != NIL_RTSOCKET)
{
RTTcpClientClose(pParams->hSocket);
pParams->hSocket = NIL_RTSOCKET;
}
return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
}
default:
RTTestIFailed("Protocol not supported.\n");
return RTEXITCODE_FAILURE;
}
}
int main(int argc, char *argv[])
{
/*
* Init IPRT and globals.
*/
int rc = RTTestInitAndCreate("NetPerf", &g_hTest);
if (rc)
return rc;
/*
* Special case.
*/
if (argc < 2)
{
RTTestFailed(g_hTest, "No arguments given.");
return RTTestSummaryAndDestroy(g_hTest);
}
/*
* Default values.
*/
NETPERFPROTO enmProtocol = NETPERFPROTO_TCP;
bool fServer = true;
bool fDaemonize = false;
bool fDaemonized = false;
bool fPacketSizeSet = false;
const char *pszServerAddress= NULL;
NETPERFPARAMS Params;
Params.uPort = NETPERF_DEFAULT_PORT;
Params.fServerStats = false;
Params.fSingleClient = false;
Params.fNoDelay = false;
Params.fCheckData = false;
Params.enmMode = NETPERFMODE_LATENCY;
Params.cSecTimeout = NETPERF_DEFAULT_TIMEOUT;
Params.cMsWarmup = NETPERF_DEFAULT_WARMUP;
Params.cMsCoolDown = NETPERF_DEFAULT_COOL_DOWN;
Params.cbPacket = NETPERF_DEFAULT_PKT_SIZE_LATENCY;
Params.cbBufferSize = 0;
Params.hSocket = NIL_RTSOCKET;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
while ((rc = RTGetOpt(&GetState, &ValueUnion)))
{
switch (rc)
{
case 's':
fServer = true;
break;
case 'c':
fServer = false;
pszServerAddress = ValueUnion.psz;
break;
case 'd':
fDaemonize = true;
break;
case 'D':
fDaemonized = true;
break;
case 'i':
Params.cSecTimeout = ValueUnion.u32;
if ( Params.cSecTimeout < NETPERF_MIN_TIMEOUT
|| Params.cSecTimeout > NETPERF_MAX_TIMEOUT)
{
RTTestFailed(g_hTest, "Invalid interval %u s, valid range: %u-%u\n",
Params.cbPacket, NETPERF_MIN_TIMEOUT, NETPERF_MAX_TIMEOUT);
return RTTestSummaryAndDestroy(g_hTest);
}
break;
case 'l':
Params.cbPacket = ValueUnion.u32;
if ( Params.cbPacket < NETPERF_MIN_PKT_SIZE
|| Params.cbPacket > NETPERF_MAX_PKT_SIZE)
{
RTTestFailed(g_hTest, "Invalid packet size %u bytes, valid range: %u-%u\n",
Params.cbPacket, NETPERF_MIN_PKT_SIZE, NETPERF_MAX_PKT_SIZE);
return RTTestSummaryAndDestroy(g_hTest);
}
fPacketSizeSet = true;
break;
case 'm':
Params.enmMode = netperfModeFromString(ValueUnion.psz);
if (Params.enmMode == NETPERFMODE_INVALID)
{
RTTestFailed(g_hTest, "Invalid test mode: \"%s\"\n", ValueUnion.psz);
return RTTestSummaryAndDestroy(g_hTest);
}
if (!fPacketSizeSet)
switch (Params.enmMode)
{
case NETPERFMODE_LATENCY:
Params.cbPacket = NETPERF_DEFAULT_PKT_SIZE_LATENCY;
break;
case NETPERFMODE_THROUGHPUT:
case NETPERFMODE_THROUGHPUT_XMIT:
case NETPERFMODE_THROUGHPUT_RECV:
Params.cbPacket = NETPERF_DEFAULT_PKT_SIZE_THROUGHPUT;
break;
case NETPERFMODE_INVALID:
break;
/* no default! */
}
break;
case 'p':
Params.uPort = ValueUnion.u32;
break;
case 'N':
Params.fNoDelay = true;
break;
case 'S':
Params.fServerStats = true;
break;
case '1':
Params.fSingleClient = true;
break;
case 'v':
g_uVerbosity++;
break;
case 'h':
Usage(g_pStdOut);
return RTEXITCODE_SUCCESS;
case 'V':
RTPrintf("$Revision: 155244 $\n");
return RTEXITCODE_SUCCESS;
case 'w':
Params.cMsWarmup = ValueUnion.u32;
if ( Params.cMsWarmup < NETPERF_MIN_WARMUP
|| Params.cMsWarmup > NETPERF_MAX_WARMUP)
{
RTTestFailed(g_hTest, "invalid warmup time %u ms, valid range: %u-%u\n",
Params.cMsWarmup, NETPERF_MIN_WARMUP, NETPERF_MAX_WARMUP);
return RTTestSummaryAndDestroy(g_hTest);
}
break;
case 'W':
Params.cMsCoolDown = ValueUnion.u32;
if ( Params.cMsCoolDown < NETPERF_MIN_COOL_DOWN
|| Params.cMsCoolDown > NETPERF_MAX_COOL_DOWN)
{
RTTestFailed(g_hTest, "invalid cool down time %u ms, valid range: %u-%u\n",
Params.cMsCoolDown, NETPERF_MIN_COOL_DOWN, NETPERF_MAX_COOL_DOWN);
return RTTestSummaryAndDestroy(g_hTest);
}
break;
case 'C':
Params.fCheckData = true;
break;
case 'b':
Params.cbBufferSize = ValueUnion.u32;
if ( ( Params.cbBufferSize < NETPERF_MIN_BUF_SIZE
|| Params.cbBufferSize > NETPERF_MAX_BUF_SIZE)
&& Params.cbBufferSize != 0)
{
RTTestFailed(g_hTest, "Invalid packet size %u bytes, valid range: %u-%u or 0\n",
Params.cbBufferSize, NETPERF_MIN_BUF_SIZE, NETPERF_MAX_BUF_SIZE);
return RTTestSummaryAndDestroy(g_hTest);
}
break;
default:
return RTGetOptPrintError(rc, &ValueUnion);
}
}
/*
* Handle the server process daemoniziation.
*/
if (fDaemonize && !fDaemonized && fServer)
{
rc = RTProcDaemonize(argv, "--daemonized");
if (RT_FAILURE(rc))
return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize failed: %Rrc\n", rc);
return RTEXITCODE_SUCCESS;
}
/*
* Get down to business.
*/
RTTestBanner(g_hTest);
if (fServer)
rc = netperfServer(enmProtocol, &Params);
else if (pszServerAddress)
rc = netperfClient(enmProtocol, pszServerAddress, &Params);
else
RTTestFailed(g_hTest, "missing server address to connect to\n");
RTEXITCODE rc2 = RTTestSummaryAndDestroy(g_hTest);
return rc2 != RTEXITCODE_FAILURE ? (RTEXITCODE)rc2 : rc;
}
|