-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathselftest.cpp
1503 lines (1267 loc) · 46.3 KB
/
selftest.cpp
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
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
// Comprehensive stress test for socket-like API
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <map>
#include <ctime>
#include <sys/time.h>
#include "libzt.h"
#define EXIT_ON_FAIL false
#define PASSED 1
#define FAILED 0
#define ECHO_INTERVAL 1000000 // us
#define SLAM_INTERVAL 500000
#define WAIT_FOR_SERVER_TO_COME_ONLINE 2
#define WAIT_FOR_TEST_TO_CONCLUDE 15
#define WAIT_FOR_TRANSMISSION_TO_COMPLETE 5
#define STR_SIZE 32
#define TEST_OP_N_BYTES 10
#define TEST_OP_N_SECONDS 11
#define TEST_OP_N_TIMES 12
#define TEST_MODE_CLIENT 20
#define TEST_MODE_SERVER 21
#define TEST_TYPE_SIMPLE 30
#define TEST_TYPE_SUSTAINED 31
#define TEST_TYPE_PERF 32
#define TEST_TYPE_PERF_TO_ECHO 33
#define MIN_PORT 5000
#define MAX_PORT 50000
#define UNIT_TEST_SIG_4 struct sockaddr_in *addr, int operation, int count, int delay, char *details, bool *passed
#define UNIT_TEST_SIG_6 struct sockaddr_in6 *addr, int operation, int count, int delay, char *details, bool *passed
#define ECHOTEST_MODE_RX 333
#define ECHOTEST_MODE_TX 666
#define DATA_BUF_SZ 1024*32
#define MAX_RX_BUF_SZ 2048
#define MAX_TX_BUF_SZ 2048
#define ONE_MEGABYTE 1024 * 1024
#define DETAILS_STR_LEN 128
char str[STR_SIZE];
std::map<std::string, std::string> testConf;
/* Tests in this file:
Basic RX/TX connect()/accept() Functionality:
[ ?] slam - perform thousands of the same call per second
[ ] random - act like a monkey, press all the buttons
[OK] simple client ipv4 - connect, send one message and wait for an echo
[OK] simple server ipv4 - accept, read one message and echo it back
[OK] simple client ipv6 - connect, send one message and wait for an echo
[OK] simple server ipv6 - accept, read one message and echo it back
[OK] sustained client ipv4 - connect and rx/tx many messages, VERIFIES data integrity
[OK] sustained server ipv4 - accept and echo messages, VERIFIES data integrity
[OK] sustained client ipv6 - connect and rx/tx many messages, VERIFIES data integrity
[OK] sustained server ipv6 - accept and echo messages, VERIFIES data integrity
[OK] comprehensive client ipv4 - test all ipv4/6 client simple/sustained modes
[OK] comprehensive server ipv6 - test all ipv4/6 server simple/sustained modes
Performance:
(See libzt.h, compile libzt with appropriate ZT_TCP_TX_BUF_SZ, ZT_TCP_RX_BUF_SZ, ZT_UDP_TX_BUF_SZ, and ZT_UDO_RX_BUF_SZ for your test)
[OK] Throughput - Test maximum RX/TX speeds
[ ] Memory Usage - Test memory consumption profile
[ ] CPU Usage - Test processor usage
[ ]
Correctness:
[ ] Block/Non-block - Test that blocking and non-blocking behaviour is consistent
[ ] Release of resources - Test that all destructor methods/blocks function properly
[ ] Multi-network handling - Test internal Tap multiplexing works for multiple networks
[ ] Address handling - Test that addresses are copied/parsed/returned properly
*/
/****************************************************************************/
/* Helper Functions */
/****************************************************************************/
void displayResults(int *results, int size)
{
int success = 0, failure = 0;
for(int i=0; i<size; i++) {
if(results[i] == 0)
success++;
else
failure++;
}
std::cout << "tials: " << size << std::endl;
std::cout << " - success = " << (float)success / (float)size << std::endl;
std::cout << " - failure = " << (float)failure / (float)size << std::endl;
}
void loadTestConfigFile(std::string filepath)
{
std::string key, value, prefix;
std::ifstream testFile;
testFile.open(filepath.c_str());
while (testFile >> key >> value) {
if(key == "name") {
prefix = value;
}
if(key[0] != '#' && key[0] != ';') {
testConf[prefix + "." + key] = value;
fprintf(stderr, "%s.%s = %s\n", prefix.c_str(), key.c_str(), testConf[prefix + "." + key].c_str());
}
}
testFile.close();
}
long int get_now_ts()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
void generate_random_data(void *buf, size_t n)
{
char *b = (char*)buf;
int min = 0, max = 9;
srand((unsigned)time(0));
for(int i=0; i<n; i++) {
b[i] = min + (rand() % static_cast<int>(max - min + 1));
}
}
void create_addr(std::string ipstr, int port, int ipv, struct sockaddr *saddr)
{
struct hostent *server;
if(ipv == 4) {
struct sockaddr_in *in4 = (struct sockaddr_in*)saddr;
in4->sin_port = htons(port);
in4->sin_addr.s_addr = inet_addr(ipstr.c_str());
in4->sin_family = AF_INET;
}
if(ipv == 6) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)saddr;
server = gethostbyname2(ipstr.c_str(),AF_INET6);
memset((char *) in6, 0, sizeof(struct sockaddr_in6));
in6->sin6_flowinfo = 0;
in6->sin6_family = AF_INET6;
memmove((char *) in6->sin6_addr.s6_addr, (char *) server->h_addr, server->h_length);
in6->sin6_port = htons(port);
}
}
void RECORD_RESULTS(int *test_number, bool passed, char *details, std::vector<std::string> *results)
{
(*test_number) = 0;
char *ok_str = (char*)"[ OK ]";
char *fail_str = (char*)"[ FAIL ]";
if(passed == PASSED) {
DEBUG_TEST("[%d]%s", *test_number, ok_str);
results->push_back(std::string(ok_str) + " " + std::string(details));
}
else {
DEBUG_ERROR("[%d]%s", *test_number, fail_str);
results->push_back(std::string(fail_str) + " " + std::string(details));
}
if(EXIT_ON_FAIL && !passed) {
fprintf(stderr, "%s\n", results->at(results->size()-1).c_str());
exit(0);
}
memset(details, 0, DETAILS_STR_LEN);
}
/****************************************************************************/
/* SIMPLE */
/****************************************************************************/
//
void tcp_client_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_client_4\n");
int r, w, sockfd, err, len = strlen(str);
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0)
DEBUG_ERROR("error connecting to remote host (%d)", err);
w = zts_write(sockfd, str, len);
r = zts_read(sockfd, rbuf, len);
DEBUG_TEST("Sent : %s", str);
DEBUG_TEST("Received : %s", rbuf);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
sprintf(details, "tcp_client_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
}
//
void tcp_server_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_server_4\n");
int w=0, r=0, sockfd, accfd, err, len = strlen(str);
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_bind(sockfd, (struct sockaddr *)addr, sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
if((err = zts_listen(sockfd, 100)) < 0)
printf("error placing socket in LISTENING state (%d)", err);
if((accfd = zts_accept(sockfd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr))) < 0)
DEBUG_ERROR("error accepting connection (%d)", err);
r = zts_read(accfd, rbuf, sizeof rbuf);
w = zts_write(accfd, rbuf, len);
DEBUG_TEST("Received : %s", rbuf);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
err = zts_close(accfd);
sprintf(details, "tcp_server_4, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
}
//
void tcp_client_6(UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_client_6\n");
int r, w, sockfd, err, len = strlen(str);
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0)
DEBUG_ERROR("error connecting to remote host (%d)", err);
w = zts_write(sockfd, str, len);
r = zts_read(sockfd, rbuf, len);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
sprintf(details, "tcp_client_6, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
DEBUG_TEST("Sent : %s", str);
DEBUG_TEST("Received : %s", rbuf);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
}
//
void tcp_server_6(UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_server_6\n");
int w=0, r=0, sockfd, accfd, err, len = strlen(str);
char rbuf[STR_SIZE];
memset(rbuf, 0, sizeof rbuf);
if((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_bind(sockfd, (struct sockaddr *)addr, sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
if((err = zts_listen(sockfd, 100)) < 0)
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
if((accfd = zts_accept(sockfd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr))) < 0)
DEBUG_ERROR("error accepting connection (%d)", err);
r = zts_read(accfd, rbuf, sizeof rbuf);
w = zts_write(accfd, rbuf, len);
DEBUG_TEST("Received : %s", rbuf);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
err = zts_close(accfd);
sprintf(details, "tcp_server_6, n=%d, err=%d, r=%d, w=%d", count, err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, str);
}
/****************************************************************************/
/* SUSTAINED */
/****************************************************************************/
// Maintain transfer for count OR count
void tcp_client_sustained_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_client_sustained_4\n");
int n=0, w=0, r=0, sockfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
char *txbuf = (char*)malloc(count*sizeof(char));
generate_random_data(txbuf, count);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0)
DEBUG_ERROR("error connecting to remote host (%d)", err);
if(operation == TEST_OP_N_BYTES) {
int wrem = count, rrem = count;
// TX
long int tx_ti = get_now_ts();
while(wrem) {
int next_write = std::min(4096, wrem);
n = zts_write(sockfd, &txbuf[w], next_write);
if (n > 0)
{
w += n;
wrem -= n;
err = n;
}
}
long int tx_tf = get_now_ts();
DEBUG_TEST("wrote=%d", w);
// RX
long int rx_ti = 0;
while(rrem) {
n = zts_read(sockfd, &rxbuf[r], rrem);
if(!rx_ti) { // wait for first message
rx_ti = get_now_ts();
}
if (n > 0)
{
r += n;
rrem -= n;
err = n;
}
}
long int rx_tf = get_now_ts();
DEBUG_TEST("read=%d", r);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
// Compare RX and TX buffer and detect mismatches
bool match = true;
for(int i=0; i<count; i++) {
if(rxbuf[i] != txbuf[i]) {
DEBUG_ERROR("buffer mismatch found at idx=%d", i);
match=false;
}
}
// Compute time deltas and transfer rates
float tx_dt = (tx_tf - tx_ti) / (float)1000;
float rx_dt = (rx_tf - rx_ti) / (float)1000;
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_client_sustained_4, match=%d, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
match, count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && match && err>=0);
}
free(rxbuf);
free(txbuf);
}
// Maintain transfer for count OR count
void tcp_client_sustained_6(UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_client_sustained_6\n");
int n=0, w=0, r=0, sockfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
char *txbuf = (char*)malloc(count*sizeof(char));
generate_random_data(txbuf, count);
if((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0)
DEBUG_ERROR("error connecting to remote host (%d)", err);
if(operation == TEST_OP_N_BYTES) {
int wrem = count, rrem = count;
// TX
long int tx_ti = get_now_ts();
while(wrem) {
int next_write = std::min(4096, wrem);
n = zts_write(sockfd, &txbuf[w], next_write);
if (n > 0)
{
w += n;
wrem -= n;
err = n;
}
}
long int tx_tf = get_now_ts();
DEBUG_TEST("wrote=%d", w);
// RX
long int rx_ti = 0;
while(rrem) {
n = zts_read(sockfd, &rxbuf[r], rrem);
if(!rx_ti) { // wait for first message
rx_ti = get_now_ts();
}
if (n > 0)
{
r += n;
rrem -= n;
err = n;
}
}
long int rx_tf = get_now_ts();
DEBUG_TEST("read=%d", r);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
// Compare RX and TX buffer and detect mismatches
bool match = true;
for(int i=0; i<count; i++) {
if(rxbuf[i] != txbuf[i]) {
DEBUG_ERROR("buffer mismatch found at idx=%d", i);
match=false;
}
}
// Compute time deltas and transfer rates
float tx_dt = (tx_tf - tx_ti) / (float)1000;
float rx_dt = (rx_tf - rx_ti) / (float)1000;
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_client_sustained_6, match=%d, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
match, count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && match && err>=0);
}
free(rxbuf);
free(txbuf);
}
// Maintain transfer for count OR count
void tcp_server_sustained_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_server_sustained_4\n");
int n=0, w=0, r=0, sockfd, accfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
memset(rxbuf, 0, count);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_bind(sockfd, (struct sockaddr *)addr, (socklen_t)sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
if((err = zts_listen(sockfd, 1)) < 0)
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
if((accfd = zts_accept(sockfd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr))) < 0)
DEBUG_ERROR("error accepting connection (%d)", err);
if(operation == TEST_OP_N_BYTES) {
int wrem = count, rrem = count;
long int rx_ti = 0;
while(rrem) {
n = zts_read(accfd, &rxbuf[r], rrem);
if (n > 0)
{
if(!rx_ti) { // wait for first message
rx_ti = get_now_ts();
}
r += n;
rrem -= n;
err = n;
}
}
long int rx_tf = get_now_ts();
DEBUG_TEST("read=%d", r);
long int tx_ti = get_now_ts();
while(wrem) {
int next_write = std::min(1024, wrem);
n = zts_write(accfd, &rxbuf[w], next_write);
if (n > 0)
{
w += n;
wrem -= n;
err = n;
}
}
long int tx_tf = get_now_ts();
DEBUG_TEST("wrote=%d", w);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
// Compute time deltas and transfer rates
float tx_dt = (tx_tf - tx_ti) / (float)1000;
float rx_dt = (rx_tf - rx_ti) / (float)1000;
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_server_sustained_4, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && err>=0);
}
free(rxbuf);
}
// Maintain transfer for count OR count
void tcp_server_sustained_6(UNIT_TEST_SIG_6)
{
fprintf(stderr, "\n\n\ntcp_server_sustained_6\n");
int n=0, w=0, r=0, sockfd, accfd, err;
char *rxbuf = (char*)malloc(count*sizeof(char));
memset(rxbuf, 0, count);
if((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_bind(sockfd, (struct sockaddr *)addr, (socklen_t)sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
if((err = zts_listen(sockfd, 1)) < 0)
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
if((accfd = zts_accept(sockfd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr))) < 0)
DEBUG_ERROR("error accepting connection (%d)", err);
if(operation == TEST_OP_N_BYTES) {
int wrem = count, rrem = count;
long int rx_ti = 0;
while(rrem) {
n = zts_read(accfd, &rxbuf[r], rrem);
if (n > 0)
{
if(!rx_ti) { // wait for first message
rx_ti = get_now_ts();
}
r += n;
rrem -= n;
err = n;
}
}
long int rx_tf = get_now_ts();
DEBUG_TEST("read=%d", r);
long int tx_ti = get_now_ts();
while(wrem) {
int next_write = std::min(1024, wrem);
n = zts_write(accfd, &rxbuf[w], next_write);
if (n > 0)
{
w += n;
wrem -= n;
err = n;
}
}
long int tx_tf = get_now_ts();
DEBUG_TEST("wrote=%d", w);
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
// Compute time deltas and transfer rates
float tx_dt = (tx_tf - tx_ti) / (float)1000;
float rx_dt = (rx_tf - rx_ti) / (float)1000;
float tx_rate = (float)count / (float)tx_dt;
float rx_rate = (float)count / (float)rx_dt;
sprintf(details, "tcp_server_sustained_6, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
count, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
*passed = (r == count && w == count && err>=0);
}
free(rxbuf);
}
/****************************************************************************/
/* PERFORMANCE (between library instances) */
/****************************************************************************/
// Maintain transfer for count OR count
void tcp_client_perf_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_client_perf_4\n");
/*
int w=0, sockfd, err;
int total_test_sz = count;
int arbitrary_chunk_sz_max = MAX_RX_BUF_SZ;
int arbitrary_chunk_sz_min = 512;
char rbuf[arbitrary_chunk_sz_max];
for (int i=arbitrary_chunk_sz_min; (i*2) < arbitrary_chunk_sz_max; i*=2) {
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0)
DEBUG_ERROR("error connecting to remote host (%d)", err);
DEBUG_TEST("[TX] Testing (%d) byte chunks: ", i);
int chunk_sz = i;
long int start_time = get_now_ts();
w = 0;
// TX
while(w < total_test_sz)
w += zts_write(sockfd, rbuf, chunk_sz);
long int end_time = get_now_ts();
float ts_delta = (end_time - start_time) / (float)1000;
float rate = (float)total_test_sz / (float)ts_delta;
sprintf(details, "tot=%d, dt=%.2f, rate=%.2f MB/s", w, ts_delta, (rate / float(ONE_MEGABYTE) ));
zts_close(sockfd);
}
*passed = (w == total_test_sz && !err) ? PASSED : FAILED;
*/
}
// Maintain transfer for count OR count
void tcp_server_perf_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_server_perf_4\n");
/*
int r=0, sockfd, accfd, err;
int total_test_sz = count;
int arbitrary_chunk_sz_max = MAX_RX_BUF_SZ;
int arbitrary_chunk_sz_min = 512;
char rbuf[arbitrary_chunk_sz_max];
for (int i=arbitrary_chunk_sz_min; (i*2) < arbitrary_chunk_sz_max; i*=2) {
DEBUG_ERROR("TESTING chunk size = %d", i);
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = zts_bind(sockfd, (struct sockaddr *)addr, (socklen_t)sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (%d)", err);
if((err = zts_listen(sockfd, 1)) < 0)
DEBUG_ERROR("error placing socket in LISTENING state (%d)", err);
if((accfd = zts_accept(sockfd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr))) < 0)
DEBUG_ERROR("error accepting connection (%d)", err);
DEBUG_TEST("[RX] Testing (%d) byte chunks: ", i);
int chunk_sz = i;
long int start_time = get_now_ts();
r = 0;
// RX
while(r < total_test_sz)
r += zts_read(accfd, rbuf, chunk_sz);
long int end_time = get_now_ts();
float ts_delta = (end_time - start_time) / (float)1000;
float rate = (float)total_test_sz / (float)ts_delta;
sprintf(details, "tot=%d, dt=%.2f, rate=%.2f MB/s", r, ts_delta, (rate / float(ONE_MEGABYTE) ));
zts_close(sockfd);
zts_close(accfd);
}
*passed = (r == total_test_sz && !err) ? PASSED : FAILED;
*/
}
/****************************************************************************/
/* PERFORMANCE (between library and native) */
/****************************************************************************/
void tcp_perf_tx_echo_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_perf_tx_echo_4\n");
int err = 0;
int tot = 0;
int w = 0;
int sockfd, mode;
char pbuf[64]; // test parameter buffer
char tbuf[MAX_TX_BUF_SZ];
mode = ECHOTEST_MODE_TX;
// connect to remote echotest host
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
DEBUG_ERROR("error creating ZeroTier socket");
return;
}
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0) {
DEBUG_ERROR("error connecting to remote host (%d)", err);
return;
}
DEBUG_TEST("copying test parameters to buffer");
memset(pbuf, 0, sizeof pbuf);
memcpy(pbuf, &mode, sizeof mode);
memcpy(pbuf + sizeof mode, &count, sizeof count);
DEBUG_TEST("sending test parameters to echotest");
if((w = zts_write(sockfd, pbuf, sizeof pbuf)) < 0) {
DEBUG_ERROR("error while sending test parameters to echotest (err=%d)", w);
return;
}
// begin
DEBUG_TEST("beginning test, sending test byte stream...");
while(tot < count) {
if((w = zts_write(sockfd, tbuf, sizeof tbuf)) < 0) {
DEBUG_ERROR("error while sending test byte stream to echotest (err=%d)", w);
return;
}
tot += w;
DEBUG_TEST("tot=%d, sent=%d", tot, w);
}
// read results
memset(pbuf, 0, sizeof pbuf);
DEBUG_TEST("reading test results from echotest");
if((w = zts_read(sockfd, pbuf, sizeof tbuf)) < 0) {
DEBUG_ERROR("error while reading results from echotest (err=%d)", w);
return;
}
DEBUG_TEST("reading test results");
long int start_time = 0, end_time = 0;
memcpy(&start_time, pbuf, sizeof start_time);
memcpy(&end_time, pbuf + sizeof start_time, sizeof end_time);
float ts_delta = (end_time - start_time) / (float)1000;
float rate = (float)tot / (float)ts_delta;
sprintf(details, "tcp_perf_tx_echo_4, tot=%d, dt=%.2f, rate=%.2f MB/s", tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
*passed = (tot == count && !err) ? PASSED : FAILED;
}
void tcp_perf_rx_echo_4(UNIT_TEST_SIG_4)
{
fprintf(stderr, "\n\n\ntcp_perf_rx_echo_4\n");
int err = 0;
int mode = 0;
int tot = 0;
int r = 0;
char pbuf[64]; // test parameter buffer
char tbuf[MAX_TX_BUF_SZ];
int sockfd;
mode = ECHOTEST_MODE_RX;
// connect to remote echotest host
if((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
DEBUG_ERROR("error creating ZeroTier socket");
return;
}
if((err = zts_connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0) {
DEBUG_ERROR("error connecting to remote host (%d)", err);
return;
}
DEBUG_TEST("copying test parameters to buffer");
memset(pbuf, 0, sizeof pbuf);
memcpy(pbuf, &mode, sizeof mode);
memcpy(pbuf + sizeof mode, &count, sizeof count);
DEBUG_TEST("sending test parameters to echotest");
if((r = zts_write(sockfd, pbuf, sizeof pbuf)) < 0) {
DEBUG_ERROR("error while sending test parameters to echotest (err=%d)", r);
return;
}
// begin
DEBUG_TEST("beginning test, as soon as bytes are read we will start keeping time...");
if((r = read(sockfd, tbuf, sizeof tbuf)) < 0) {
DEBUG_ERROR("there was an error reading the test stream. aborting (err=%d, errno=%s)", r, strerror(errno));
return;
}
tot += r;
long int start_time = get_now_ts();
DEBUG_TEST("Received first set of bytes in test stream. now keeping time");
while(tot < count) {
if((r = read(sockfd, tbuf, sizeof tbuf)) < 0) {
DEBUG_ERROR("there was an error reading the test stream. aborting (err=%d)", r);
return;
}
tot += r;
DEBUG_TEST("r=%d, tot=%d", r, tot);
}
long int end_time = get_now_ts();
float ts_delta = (end_time - start_time) / (float)1000;
float rate = (float)tot / (float)ts_delta;
sprintf(details, "tcp_perf_rx_echo_4, tot=%d, dt=%.2f, rate=%.2f MB/s", tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
err = zts_close(sockfd);
*passed = (tot == count && !err) ? PASSED : FAILED;
}
/****************************************************************************/
/* SLAM API (multiple of each api call and/or plausible call sequence) */
/****************************************************************************/
#define SLAM_NUMBER 16
#define SLAM_REPEAT 1
int slam_api_test()
{
int err = 0;
int results[SLAM_NUMBER*SLAM_REPEAT];
struct hostent *server;
struct sockaddr_in6 addr6;
struct sockaddr_in addr;
// int start_stack_timer_count = pico_ntimers(); // number of picoTCP timers allocated
// TESTS:
// socket()
// close()
if(false)
{
// open and close SLAM_NUMBER*SLAM_REPEAT sockets
for(int j=0; j<SLAM_REPEAT; j++) {
std::cout << "slamming " << j << " time(s)" << std::endl;
usleep(SLAM_INTERVAL);
// create sockets
int fds[SLAM_NUMBER];
for(int i = 0; i<SLAM_NUMBER; i++) {
if((err = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
std::cout << "error creating socket (errno = " << strerror(errno) << ")" << std::endl;
if(errno == EMFILE)
break;
else
return -1;
}
else
fds[i] = err;
std::cout << "\tcreating " << i << " socket(s) fd = " << err << std::endl;
}
// close sockets
for(int i = 0; i<SLAM_NUMBER; i++) {
//std::cout << "\tclosing " << i << " socket(s)" << std::endl;
if((err = zts_close(fds[i])) < 0) {
std::cout << "error closing socket (errno = " << strerror(errno) << ")" << std::endl;
//return -1;
}
else
fds[i] = -1;
}
}
if(zts_nsockets() == 0)
std::cout << "PASSED [slam open and close]" << std::endl;
else
std::cout << "FAILED [slam open and close] - sockets left unclosed" << std::endl;
}
// ---
// TESTS:
// socket()
// bind()
// listen()
// accept()
// close()
if(false)
{
int sock = 0;
std::vector<int> used_ports;
for(int j=0; j<SLAM_REPEAT; j++) {
std::cout << "slamming " << j << " time(s)" << std::endl;
usleep(SLAM_INTERVAL);
for(int i = 0; i<SLAM_NUMBER; i++) {
if((sock = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
std::cout << "error creating socket (errno = " << strerror(errno) << ")" << std::endl;
if(errno == EMFILE)
break;
else
return -1;
}
std::cout << "socket() = " << sock << std::endl;
usleep(SLAM_INTERVAL);
int port;
while(!(std::find(used_ports.begin(),used_ports.end(),port) == used_ports.end())) {
port = MIN_PORT + (rand() % (int)(MAX_PORT - MIN_PORT + 1));
}
used_ports.push_back(port);
std::cout << "port = " << port << std::endl;
if(false) {
server = gethostbyname2("::",AF_INET6);
memset((char *) &addr6, 0, sizeof(addr6));
addr6.sin6_flowinfo = 0;
addr6.sin6_family = AF_INET6;
addr6.sin6_port = htons(port);
addr6.sin6_addr = in6addr_any;
err = zts_bind(sock, (struct sockaddr *)&addr6, (socklen_t)(sizeof addr6));
}
if(true) {
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr("10.9.9.50");
//addr.sin_addr.s_addr = htons(INADDR_ANY);
addr.sin_family = AF_INET;
err = zts_bind(sock, (struct sockaddr *)&addr, (socklen_t)(sizeof addr));
}
if(err < 0) {
std::cout << "error binding socket (errno = " << strerror(errno) << ")" << std::endl;
return -1;
}
if(sock > 0) {
if((err = zts_close(sock)) < 0) {
std::cout << "error closing socket (errno = " << strerror(errno) << ")" << std::endl;
//return -1;
}
}
}
}
used_ports.clear();
if(zts_nsockets() == 0)
std::cout << "PASSED [slam open, bind, listen, accept, close]" << std::endl;
else
std::cout << "FAILED [slam open, bind, listen, accept, close]" << std::endl;
}
// TESTS:
// (1) socket()
// (2) connect()
// (3) close()
int num_times = zts_maxsockets();
std::cout << "socket/connect/close - " << num_times << " times" << std::endl;
for(int i=0;i<(SLAM_NUMBER*SLAM_REPEAT); i++) { results[i] = 0; }
if(true)
{
int port = 4545;