Updated selftest and echotest, also added a few scripts

This commit is contained in:
Joseph Henry
2017-06-22 18:22:39 -07:00
parent e10c621405
commit ab7e9d0091
8 changed files with 783 additions and 405 deletions

View File

@@ -142,7 +142,7 @@ TAP_FILES:=src/SocketTap.cpp \
SDK_OBJS+= SocketTap.o \ SDK_OBJS+= SocketTap.o \
picoTCP.o \ picoTCP.o \
libzt.o \ libzt.o \
Utilities.o Utilities.o
PICO_OBJS+= ext/picotcp/build/lib/pico_device.o \ PICO_OBJS+= ext/picotcp/build/lib/pico_device.o \
ext/picotcp/build/lib/pico_frame.o \ ext/picotcp/build/lib/pico_frame.o \

8
test/alice.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/bash
OSTYPE=$(uname -s | tr '[A-Z]' '[a-z]')
./build/$OSTYPE/selftest test/alice.conf &
echo $! >> "test/selftest.alice"
./build/$OSTYPE/echotest test/alice.conf &
echo $! >> "test/echotest.alice"

8
test/bob.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/bash
OSTYPE=$(uname -s | tr '[A-Z]' '[a-z]')
./build/$OSTYPE/selftest test/bob.conf &
echo $! >> "test/selftest.bob"
./build/$OSTYPE/echotest test/bob.conf &
echo $! >> "test/selftest.bob"

View File

@@ -1,192 +0,0 @@
/*
* 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 <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <map>
#include <ctime>
#include <sys/time.h>
#include "libzt.h"
std::map<std::string, std::string> testConf;
void loadTestConfigFile(std::string filepath)
{
std::string key;
std::string value;
std::ifstream testFile;
testFile.open(filepath.c_str());
while (testFile >> key >> value) {
if(key[0] != '#')
testConf[key] = value;
}
testFile.close();
}
long int get_now_ts()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
/****************************************************************************/
/* PERFORMANCE */
/****************************************************************************/
// Maintain transfer for n_count OR n_count
void start_echo_mode(std::string ipstr, int port)
{
DEBUG_TEST();
/*
int w=0, sockfd, err;
int total_test_sz = 1024*1024;
int arbitrary_chunk_sz_max = 16384;
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 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating ZeroTier socket");
if((err = connect(sockfd, (const struct sockaddr *)addr, sizeof(addr))) < 0)
DEBUG_ERROR("error connecting to remote host (%d)\n", err);
DEBUG_TEST("[TX] Testing (%d) byte chunks: ", i);
int chunk_sz = i;
int iterations = total_test_sz / chunk_sz;
long int start_time = get_now_ts();
w = 0;
while(w < total_test_sz)
w += 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;
DEBUG_TEST("%d total bytes, time = %3f, rate = %3f KB/s", w, ts_delta, (rate / (float)1024) );
close(sockfd);
// let things settle after test
sleep(5);
}
*passed = (w == total_test_sz && !err) ? PASSED : FAILED;
*/
}
/*
Mode 1 (Measure performance of other host's TX):
- Receive incoming TX test config (total bytes intended)
- Prepare receiver
- Record time of first received byte
- Record time of last received byte
- Send results back to other host's selftest instance
Mode 2 (Measure performance of other host's RX):
- Receive incoming RX test config (total bytes requested)
- Prepare transmitter
- Send bytes as fast as possible
*/
int main(int argc , char *argv[])
{
if(argc < 1) {
fprintf(stderr, "usage: echo <alice|bob>.conf\n");
fprintf(stderr, " - Define your test environment in *.conf files.\n");
return 1;
}
int err = 0;
int type = 0;
int protocol = 0;
int mode = 0;
int port = 0;
int start_port = 0;
int port_offset = 0;
int operation = 0;
int n_count = 0;
int delay = 0;
std::string local_echo_ipv4;
std::string nwid, stype, path = argv[1];
std::string ipstr, ipstr6, local_ipstr, local_ipstr6, remote_ipstr, remote_ipstr6;
// if a test config file was specified:
if(path.find(".conf") != std::string::npos) {
//printf("\nTest config file contents:\n");
loadTestConfigFile(path);
nwid = testConf["nwid"];
path = testConf["local_path"];
stype = testConf["test"];
start_port = atoi(testConf["start_port"].c_str());
port_offset = atoi(testConf["port_offset"].c_str());
local_ipstr = testConf["local_ipv4"];
local_ipstr6 = testConf["local_ipv6"];
remote_ipstr = testConf["remote_ipv4"];
remote_ipstr6 = testConf["remote_ipv6"];
std::string smode = testConf["mode"];
/*
if(strcmp(smode.c_str(), "server") == 0)
mode = TEST_MODE_SERVER;
else
mode = TEST_MODE_CLIENT;
*/
fprintf(stderr, "\tlocal_ipstr = %s\n", local_ipstr.c_str());
fprintf(stderr, "\tremote_ipstr = %s\n", remote_ipstr.c_str());
fprintf(stderr, "\tstart_port = %d\n", start_port);
fprintf(stderr, "\tport_offset = %d\n", port_offset);
fprintf(stderr, "\tlocal_echo_ipv4 = %s\n", local_echo_ipv4.c_str());
}
fprintf(stderr, "\tpath = %s\n", path.c_str());
fprintf(stderr, "\tnwid = %s\n", nwid.c_str());
fprintf(stderr, "\ttype = %s\n\n", stype.c_str());
DEBUG_TEST("Starting echo mode...");
start_echo_mode(local_echo_ipv4, start_port+port_offset);
return 1;
}

262
test/echotest.cpp Normal file
View File

@@ -0,0 +1,262 @@
/*
* 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.
*/
// Echo program to aid in the operation of selftest
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <map>
#include <ctime>
#include <sys/time.h>
#include "libzt.h"
#define ECHOTEST_MODE_RX 333
#define ECHOTEST_MODE_TX 666
#define MAX_RX_BUF_SZ 16384
#define MAX_TX_BUF_SZ 16384
std::map<std::string, std::string> testConf;
void loadTestConfigFile(std::string filepath)
{
std::string key;
std::string value;
std::ifstream testFile;
testFile.open(filepath.c_str());
while (testFile >> key >> value) {
if(key[0] != '#')
testConf[key] = value;
}
testFile.close();
}
long int get_now_ts()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
void start_echo_mode(std::string ipstr, int listen_port)
{
DEBUG_TEST();
DEBUG_TEST("listening for connections on port (%d)", listen_port);
int backlog = 128;
int err = 0;
int sockfd, accfd;
struct sockaddr_in addr;
struct sockaddr_in client;
socklen_t clen = sizeof client;
addr.sin_port = htons(listen_port);
addr.sin_addr.s_addr = inet_addr(ipstr.c_str());
addr.sin_family = AF_INET;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
DEBUG_ERROR("error creating socket (err=%d, errno=%s)", err, strerror(errno));
if((err = bind(sockfd, (struct sockaddr *)&addr, (socklen_t)sizeof(struct sockaddr_in)) < 0))
DEBUG_ERROR("error binding to interface (err=%d, errno=%s)\n", err, strerror(errno));
if((err = listen(sockfd, backlog)) < 0)
DEBUG_ERROR("error placing socket in LISTENING state (err=%d, errno=%s)\n", err, strerror(errno));
DEBUG_TEST("accepting test connections...");
while(true)
{
if((accfd = accept(sockfd, (struct sockaddr *)&client, &clen)) < 0) {
DEBUG_ERROR("error accepting connection (err=%d, errno=%s)", accfd, strerror(errno));
return;
}
DEBUG_TEST("connection accepted! (fd=%d)", accfd);
// Read initial test parameters from other host
int err = 0;
int mode = 0; // rx/tx
int count = 0; // of incoming byte stream, or requested outgoing
char pbuf[64]; // test parameter buffer
char rbuf[MAX_RX_BUF_SZ];
int len = sizeof mode + sizeof count;
int tot = 0; // total bytes read from remote test stream (selftest)
memset(pbuf, 0, sizeof pbuf);
DEBUG_TEST("reading %d bytes (test parameters)", len);
if((err = read(accfd, pbuf, len)) < 0) {
DEBUG_ERROR("error while reading test parameters from remote selftest host (err=%d, errno=%s)", err, strerror(errno));
return;
}
memcpy(&mode, pbuf, sizeof mode);
memcpy(&count, pbuf + sizeof mode, sizeof count);
DEBUG_TEST("mode = %d, count = %d", mode, count);
float totKB=0, totMB=0;
/*
Mode 1 (Measure performance of other host's TX):
- Receive incoming TX test config (total bytes intended)
- Prepare receiver
- Record time of first received byte
- Record time of last received byte
- Send results back to other host's selftest instance
*/
// read 'count' bytes and send back before/after timestamps
if(mode == ECHOTEST_MODE_TX)
{
DEBUG_TEST("entering READ mode, as soon as bytes are read we will start keeping time...");
if((err = read(accfd, rbuf, sizeof rbuf)) < 0) {
DEBUG_ERROR("there was an error reading the test stream. aborting (err=%d, errno=%s)", err, errno);
return;
}
tot += err;
long int start_time = get_now_ts();
totKB=0;
totMB=0;
DEBUG_TEST("Received first set of bytes in test stream. now keeping time");
while(tot < count) {
if((err = read(accfd, rbuf, sizeof rbuf)) < 0) {
DEBUG_ERROR("there was an error reading the test stream. aborting");
return;
}
tot += err;
totKB = (float)tot / (float)1024;
totMB = (float)tot / (float)(1024*1024);
//DEBUG_TEST("read = %d, totB = %d, totKB = %3f, totMB = %3f", err, tot, totKB, totMB);
}
//DEBUG_TEST("total received = %d (%d MB)", tot);
long int end_time = get_now_ts();
DEBUG_TEST("read last byte (tot=%d). stopping timer. sending test data back to remote selftest", tot);
memset(pbuf, 0, sizeof pbuf);
memcpy(pbuf, &start_time, sizeof start_time);
memcpy(pbuf + sizeof start_time, &end_time, sizeof end_time);
DEBUG_TEST("copied test data, sending...");
if((err = write(accfd, pbuf, sizeof start_time + sizeof end_time)) < 0) {
DEBUG_ERROR("error while sending test data to remote selftest host (err=%d, errno=%s)", err, strerror(errno));
return;
}
DEBUG_TEST("sleeping before closing socket and accepting further selftest connections\n\n");
sleep(3);
}
/*
Mode 2 (Measure performance of other host's RX):
- Receive incoming RX test config (total bytes requested)
- Prepare transmitter
- Send bytes as fast as possible
*/
// send 'count' bytes as quickly as possible
if(mode == ECHOTEST_MODE_RX)
{
totKB=0;
totMB=0;
while(tot < count) {
if((err = write(accfd, rbuf, sizeof rbuf)) < 0) {
DEBUG_ERROR("error while sending test byte stream to echotest");
return;
}
tot += err;
totKB = (float)tot / (float)1024;
totMB = (float)tot / (float)(1024*1024);
//DEBUG_TEST("read = %d, totB = %d, totKB = %3f, totMB = %3f", err, tot, totKB, totMB);
}
DEBUG_TEST("sleeping before closing socket and accepting further selftest connections");
sleep(3);
}
close(accfd);
}
close(sockfd);
}
int main(int argc , char *argv[])
{
if(argc < 1) {
fprintf(stderr, "usage: echo <alice|bob>.conf\n");
fprintf(stderr, " - Define your test environment in *.conf files.\n");
return 1;
}
int start_port = 0;
int port_offset = 0;
int echo_listen_port = 0;
std::string local_echo_ipv4;
std::string nwid, stype, path = argv[1];
std::string ipstr, ipstr6, local_ipstr, local_ipstr6, remote_ipstr, remote_ipstr6;
// if a test config file was specified:
if(path.find(".conf") != std::string::npos) {
loadTestConfigFile(path);
start_port = atoi(testConf["start_port"].c_str());
port_offset = atoi(testConf["port_offset"].c_str());
local_ipstr = testConf["local_ipv4"];
local_ipstr6 = testConf["local_ipv6"];
local_echo_ipv4 = testConf["local_echo_ipv4"];
remote_ipstr = testConf["remote_ipv4"];
remote_ipstr6 = testConf["remote_ipv6"];
if(strcmp(testConf["name"].c_str(), "alice") == 0)
echo_listen_port = start_port+port_offset;
else if(strcmp(testConf["name"].c_str(), "bob") == 0)
echo_listen_port = start_port+port_offset+1;
fprintf(stderr, "\tlocal_ipstr = %s\n", local_ipstr.c_str());
fprintf(stderr, "\tremote_ipstr = %s\n", remote_ipstr.c_str());
fprintf(stderr, "\tstart_port = %d\n", start_port);
fprintf(stderr, "\tport_offset = %d\n", port_offset);
fprintf(stderr, "\tlocal_echo_ipv4 = %s\n", local_echo_ipv4.c_str());
}
DEBUG_TEST("Starting echo mode... %s : %d", local_echo_ipv4.c_str(), echo_listen_port);
start_echo_mode(local_echo_ipv4, echo_listen_port);
return 1;
}

23
test/generate_test_env.sh Executable file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
# create dirs to house the identities/config
mkdir -p test_identities
mkdir test_identities/alice
mkdir test_identities/bob
# generate identities
zerotier-one test_identities/alice -d
echo $! >> "test_identities/zto.alice"
zerotier-one test_identities/bob -d
echo $! >> "test_identities/zto.bob"
# should be done by now
sleep(30)
# kill daemons
echo "killing daemons"
pid=$(cat test_identities/alice/zto.alice)
kill -9 $pid
pid=$(cat test_identities/bob/zto.bob)
kill -9 $pid

File diff suppressed because it is too large Load Diff

16
test/stop.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
if [[ $1 == "" ]]
then
echo "Usage: stop <alice|bob>."
exit
fi
echo "killing $1"
for f in "test/*.$1";
do
pid=$(cat $f)
kill -9 $pid
rm -rf $f
done