Fix a race condition in the benchmark example and all output goes to stderr.

This commit is contained in:
Anthony Hu
2021-08-11 17:07:01 -04:00
parent 9bbb32c352
commit 586317f198

View File

@@ -308,6 +308,9 @@ typedef struct {
/* server messages to client in memory */ /* server messages to client in memory */
memBuf_t to_client; memBuf_t to_client;
/* Indicates that the server is ready for connection */
int serverListening;
#endif #endif
/* server */ /* server */
@@ -345,7 +348,7 @@ static int ServerMemSend(info_t* info, char* buf, int sz)
/* check for overflow */ /* check for overflow */
if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) { if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
pthread_mutex_unlock(&info->to_client.mutex); pthread_mutex_unlock(&info->to_client.mutex);
printf("ServerMemSend overflow\n"); fprintf(stderr, "ServerMemSend overflow\n");
return -1; return -1;
} }
#else #else
@@ -410,7 +413,7 @@ static int ClientMemSend(info_t* info, char* buf, int sz)
#ifndef BENCH_USE_NONBLOCK #ifndef BENCH_USE_NONBLOCK
/* check for overflow */ /* check for overflow */
if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) { if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
printf("ClientMemSend overflow %d %d %d\n", info->to_client.write_idx, sz, MEM_BUFFER_SZ); fprintf(stderr, "ClientMemSend overflow %d %d %d\n", info->to_client.write_idx, sz, MEM_BUFFER_SZ);
pthread_mutex_unlock(&info->to_server.mutex); pthread_mutex_unlock(&info->to_server.mutex);
return -1; return -1;
} }
@@ -536,7 +539,7 @@ static int ReceiveFrom(WOLFSSL *ssl, int sd, char *buf, int sz)
if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
sizeof(timeout)) != 0) { sizeof(timeout)) != 0) {
printf("setsockopt rcvtimeo failed\n"); fprintf(stderr, "setsockopt rcvtimeo failed\n");
} }
} }
@@ -698,12 +701,12 @@ static int SetSocketNonBlocking(int sockFd)
{ {
int flags = fcntl(sockFd, F_GETFL, 0); int flags = fcntl(sockFd, F_GETFL, 0);
if (flags < 0) { if (flags < 0) {
printf("fcntl get failed\n"); fprintf(stderr, "fcntl get failed\n");
return -1; return -1;
} }
flags = fcntl(sockFd, F_SETFL, flags | O_NONBLOCK); flags = fcntl(sockFd, F_SETFL, flags | O_NONBLOCK);
if (flags < 0) { if (flags < 0) {
printf("fcntl set failed\n"); fprintf(stderr, "fcntl set failed\n");
return -1; return -1;
} }
return 0; return 0;
@@ -737,7 +740,7 @@ static int SetupSocketAndConnect(info_t* info, const char* host,
/* Create the SOCK_DGRAM socket type is implemented on the User /* Create the SOCK_DGRAM socket type is implemented on the User
* Datagram Protocol/Internet Protocol(UDP/IP protocol).*/ * Datagram Protocol/Internet Protocol(UDP/IP protocol).*/
if ((info->client.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((info->client.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("ERROR: failed to create the SOCK_DGRAM socket\n"); fprintf(stderr, "ERROR: failed to create the SOCK_DGRAM socket\n");
return -1; return -1;
} }
XMEMCPY(&info->serverAddr, &servAddr, sizeof(servAddr)); XMEMCPY(&info->serverAddr, &servAddr, sizeof(servAddr));
@@ -747,14 +750,19 @@ static int SetupSocketAndConnect(info_t* info, const char* host,
* Sets the socket to be stream based (TCP), * Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */ * 0 means choose the default protocol. */
if ((info->client.sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { if ((info->client.sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("ERROR: failed to create the socket\n"); fprintf(stderr, "ERROR: failed to create the socket\n");
return -1; return -1;
} }
/* Connect to the server */ /* Connect to the server */
while (info->serverListening == 0) {
fprintf(stderr, "Waiting for server to listen...\n");
usleep(1);
}
if (connect(info->client.sockFd, (struct sockaddr*)&servAddr, if (connect(info->client.sockFd, (struct sockaddr*)&servAddr,
sizeof(servAddr)) == -1) { sizeof(servAddr)) == -1) {
printf("ERROR: failed to connect\n"); fprintf(stderr, "ERROR: failed to connect\n");
return -1; return -1;
} }
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
@@ -768,7 +776,7 @@ static int SetupSocketAndConnect(info_t* info, const char* host,
#endif #endif
if (info->showVerbose) { if (info->showVerbose) {
printf("Connected to %s on port %d\n", host, port); fprintf(stderr, "Connected to %s on port %d\n", host, port);
} }
return 0; return 0;
@@ -809,7 +817,7 @@ static int bench_tls_client(info_t* info)
#endif #endif
if (cli_ctx == NULL) { if (cli_ctx == NULL) {
printf("error creating ctx\n"); fprintf(stderr, "error creating ctx\n");
ret = MEMORY_E; goto exit; ret = MEMORY_E; goto exit;
} }
@@ -826,7 +834,7 @@ static int bench_tls_client(info_t* info)
sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1); sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
} }
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error loading CA\n"); fprintf(stderr, "error loading CA\n");
goto exit; goto exit;
} }
#endif #endif
@@ -837,14 +845,14 @@ static int bench_tls_client(info_t* info)
/* set cipher suite */ /* set cipher suite */
ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher); ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error setting cipher suite\n"); fprintf(stderr, "error setting cipher suite\n");
goto exit; goto exit;
} }
#ifndef NO_DH #ifndef NO_DH
ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS); ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("Error setting minimum DH key size\n"); fprintf(stderr, "Error setting minimum DH key size\n");
goto exit; goto exit;
} }
#endif #endif
@@ -853,7 +861,7 @@ static int bench_tls_client(info_t* info)
writeBuf = (unsigned char*)XMALLOC(info->packetSize, NULL, writeBuf = (unsigned char*)XMALLOC(info->packetSize, NULL,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
if (writeBuf == NULL) { if (writeBuf == NULL) {
printf("failed to allocate write memory\n"); fprintf(stderr, "failed to allocate write memory\n");
ret = MEMORY_E; goto exit; ret = MEMORY_E; goto exit;
} }
@@ -861,7 +869,7 @@ static int bench_tls_client(info_t* info)
readBufSz = info->packetSize; readBufSz = info->packetSize;
readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (readBuf == NULL) { if (readBuf == NULL) {
printf("failed to allocate read memory\n"); fprintf(stderr, "failed to allocate read memory\n");
ret = MEMORY_E; goto exit; ret = MEMORY_E; goto exit;
} }
@@ -883,7 +891,7 @@ static int bench_tls_client(info_t* info)
cli_ssl = wolfSSL_new(cli_ctx); cli_ssl = wolfSSL_new(cli_ctx);
if (cli_ssl == NULL) { if (cli_ssl == NULL) {
printf("error creating client object\n"); fprintf(stderr, "error creating client object\n");
goto exit; goto exit;
} }
@@ -892,12 +900,12 @@ static int bench_tls_client(info_t* info)
ret = wolfSSL_dtls_set_peer(cli_ssl, &info->serverAddr, ret = wolfSSL_dtls_set_peer(cli_ssl, &info->serverAddr,
sizeof(info->serverAddr)); sizeof(info->serverAddr));
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error setting dtls peer\n"); fprintf(stderr, "error setting dtls peer\n");
goto exit; goto exit;
} }
ret = wolfSSL_SetHsDoneCb(cli_ssl, myDoneHsCb, NULL); ret = wolfSSL_SetHsDoneCb(cli_ssl, myDoneHsCb, NULL);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error handshake done callback\n"); fprintf(stderr, "error handshake done callback\n");
goto exit; goto exit;
} }
} }
@@ -931,7 +939,7 @@ static int bench_tls_client(info_t* info)
#endif #endif
start = gettime_secs(0) - start; start = gettime_secs(0) - start;
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error connecting client\n"); fprintf(stderr, "error connecting client\n");
ret = wolfSSL_get_error(cli_ssl, ret); ret = wolfSSL_get_error(cli_ssl, ret);
goto exit; goto exit;
} }
@@ -950,12 +958,12 @@ static int bench_tls_client(info_t* info)
writeSz = (int)XSTRLEN(kShutdown) + 1; writeSz = (int)XSTRLEN(kShutdown) + 1;
XMEMCPY(writeBuf, kShutdown, writeSz); /* include null term */ XMEMCPY(writeBuf, kShutdown, writeSz); /* include null term */
if (info->showVerbose) { if (info->showVerbose) {
printf("Sending shutdown\n"); fprintf(stderr, "Sending shutdown\n");
} }
ret = wolfSSL_write(cli_ssl, writeBuf, writeSz); ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
if (ret < 0) { if (ret < 0) {
printf("error on client write\n"); fprintf(stderr, "error on client write\n");
ret = wolfSSL_get_error(cli_ssl, ret); ret = wolfSSL_get_error(cli_ssl, ret);
goto exit; goto exit;
} }
@@ -982,7 +990,7 @@ static int bench_tls_client(info_t* info)
#endif #endif
info->client_stats.txTime += gettime_secs(0) - start; info->client_stats.txTime += gettime_secs(0) - start;
if (ret < 0) { if (ret < 0) {
printf("error on client write\n"); fprintf(stderr, "error on client write\n");
ret = wolfSSL_get_error(cli_ssl, ret); ret = wolfSSL_get_error(cli_ssl, ret);
goto exit; goto exit;
} }
@@ -1003,7 +1011,7 @@ static int bench_tls_client(info_t* info)
#endif #endif
info->client_stats.rxTime += gettime_secs(0) - start; info->client_stats.rxTime += gettime_secs(0) - start;
if (ret < 0) { if (ret < 0) {
printf("error on client read\n"); fprintf(stderr, "error on client read\n");
ret = wolfSSL_get_error(cli_ssl, ret); ret = wolfSSL_get_error(cli_ssl, ret);
goto exit; goto exit;
} }
@@ -1012,7 +1020,7 @@ static int bench_tls_client(info_t* info)
/* validate echo */ /* validate echo */
if (XMEMCMP((char*)writeBuf, (char*)readBuf, writeSz) != 0) { if (XMEMCMP((char*)writeBuf, (char*)readBuf, writeSz) != 0) {
printf("echo check failed!\n"); fprintf(stderr, "echo check failed!\n");
ret = wolfSSL_get_error(cli_ssl, ret); ret = wolfSSL_get_error(cli_ssl, ret);
goto exit; goto exit;
} }
@@ -1027,7 +1035,7 @@ static int bench_tls_client(info_t* info)
exit: exit:
if (ret != 0 && ret != WOLFSSL_SUCCESS) { if (ret != 0 && ret != WOLFSSL_SUCCESS) {
printf("Client Error: %d (%s)\n", ret, fprintf(stderr, "Client Error: %d (%s)\n", ret,
wolfSSL_ERR_reason_error_string(ret)); wolfSSL_ERR_reason_error_string(ret));
} }
@@ -1085,7 +1093,7 @@ static int SetupSocketAndListen(int* listenFd, word32 port, int doDTLS)
/* Create a socket that is implemented on the User Datagram Protocol/ /* Create a socket that is implemented on the User Datagram Protocol/
* Interet Protocol(UDP/IP protocol). */ * Interet Protocol(UDP/IP protocol). */
if((*listenFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { if((*listenFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
printf("ERROR: failed to create the socket\n"); fprintf(stderr, "ERROR: failed to create the socket\n");
return -1; return -1;
} }
} else } else
@@ -1094,28 +1102,28 @@ static int SetupSocketAndListen(int* listenFd, word32 port, int doDTLS)
* Sets the socket to be stream based (TCP), * Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */ * 0 means choose the default protocol. */
if ((*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { if ((*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("ERROR: failed to create the socket\n"); fprintf(stderr, "ERROR: failed to create the socket\n");
return -1; return -1;
} }
/* allow reuse */ /* allow reuse */
if (setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR, if (setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(optval)) == -1) { &optval, sizeof(optval)) == -1) {
printf("setsockopt SO_REUSEADDR failed\n"); fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
return -1; return -1;
} }
/* Connect to the server */ /* Listen for the client. */
if (bind(*listenFd, (struct sockaddr*)&servAddr, if (bind(*listenFd, (struct sockaddr*)&servAddr,
sizeof(servAddr)) == -1) { sizeof(servAddr)) == -1) {
printf("ERROR: failed to bind\n"); fprintf(stderr, "ERROR: failed to bind\n");
return -1; return -1;
} }
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
if (!doDTLS) if (!doDTLS)
#endif #endif
if (listen(*listenFd, 5) != 0) { if (listen(*listenFd, 5) != 0) {
printf("ERROR: failed to listen\n"); fprintf(stderr, "ERROR: failed to listen\n");
return -1; return -1;
} }
@@ -1148,17 +1156,18 @@ static int SocketWaitClient(info_t* info)
connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg), connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
MSG_PEEK, (struct sockaddr*)&clientAddr, &size); MSG_PEEK, (struct sockaddr*)&clientAddr, &size);
if (connd < -1) { if (connd < -1) {
printf("ERROR: failed to accept the connection\n"); fprintf(stderr, "ERROR: failed to accept the connection\n");
return -1; return -1;
} }
XMEMCPY(&info->clientAddr, &clientAddr, sizeof(clientAddr)); XMEMCPY(&info->clientAddr, &clientAddr, sizeof(clientAddr));
info->server.sockFd = info->listenFd; info->server.sockFd = info->listenFd;
} else { } else {
#endif #endif
info->serverListening = 1;
if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr, &size)) == -1) { if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr, &size)) == -1) {
if (errno == SOCKET_EWOULDBLOCK) if (errno == SOCKET_EWOULDBLOCK)
return -2; return -2;
printf("ERROR: failed to accept the connection\n"); fprintf(stderr, "ERROR: failed to accept the connection\n");
return -1; return -1;
} }
info->server.sockFd = connd; info->server.sockFd = connd;
@@ -1167,7 +1176,7 @@ static int SocketWaitClient(info_t* info)
#endif #endif
if (info->showVerbose) { if (info->showVerbose) {
printf("Got client %d\n", connd); fprintf(stderr, "Got client %d\n", connd);
} }
return 0; return 0;
@@ -1207,7 +1216,7 @@ static int bench_tls_server(info_t* info)
} }
#endif #endif
if (srv_ctx == NULL) { if (srv_ctx == NULL) {
printf("error creating server ctx\n"); fprintf(stderr, "error creating server ctx\n");
ret = MEMORY_E; goto exit; ret = MEMORY_E; goto exit;
} }
@@ -1224,7 +1233,7 @@ static int bench_tls_server(info_t* info)
sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1); sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1);
} }
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error loading server key\n"); fprintf(stderr, "error loading server key\n");
goto exit; goto exit;
} }
@@ -1240,7 +1249,7 @@ static int bench_tls_server(info_t* info)
sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1); sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
} }
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error loading server cert\n"); fprintf(stderr, "error loading server cert\n");
goto exit; goto exit;
} }
#endif /* !NO_CERTS */ #endif /* !NO_CERTS */
@@ -1251,14 +1260,14 @@ static int bench_tls_server(info_t* info)
/* set cipher suite */ /* set cipher suite */
ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher); ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error setting cipher suite\n"); fprintf(stderr, "error setting cipher suite\n");
goto exit; goto exit;
} }
#ifndef NO_DH #ifndef NO_DH
ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS); ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS);
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("Error setting minimum DH key size\n"); fprintf(stderr, "Error setting minimum DH key size\n");
goto exit; goto exit;
} }
#endif #endif
@@ -1267,7 +1276,7 @@ static int bench_tls_server(info_t* info)
readBufSz = info->packetSize; readBufSz = info->packetSize;
readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (readBuf == NULL) { if (readBuf == NULL) {
printf("failed to allocate read memory\n"); fprintf(stderr, "failed to allocate read memory\n");
ret = MEMORY_E; goto exit; ret = MEMORY_E; goto exit;
} }
@@ -1296,7 +1305,7 @@ static int bench_tls_server(info_t* info)
srv_ssl = wolfSSL_new(srv_ctx); srv_ssl = wolfSSL_new(srv_ctx);
if (srv_ssl == NULL) { if (srv_ssl == NULL) {
printf("error creating server object\n"); fprintf(stderr, "error creating server object\n");
ret = MEMORY_E; goto exit; ret = MEMORY_E; goto exit;
} }
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
@@ -1304,7 +1313,7 @@ static int bench_tls_server(info_t* info)
ret = wolfSSL_dtls_set_peer(srv_ssl, &info->clientAddr, ret = wolfSSL_dtls_set_peer(srv_ssl, &info->clientAddr,
sizeof(info->clientAddr)); sizeof(info->clientAddr));
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error setting dtls peer\n"); fprintf(stderr, "error setting dtls peer\n");
goto exit; goto exit;
} }
} }
@@ -1329,7 +1338,7 @@ static int bench_tls_server(info_t* info)
#endif #endif
start = gettime_secs(0) - start; start = gettime_secs(0) - start;
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
printf("error on server accept\n"); fprintf(stderr, "error on server accept\n");
ret = wolfSSL_get_error(srv_ssl, ret); ret = wolfSSL_get_error(srv_ssl, ret);
goto exit; goto exit;
} }
@@ -1361,7 +1370,7 @@ static int bench_tls_server(info_t* info)
if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) { if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) {
info->server.shutdown = 1; info->server.shutdown = 1;
if (info->showVerbose) { if (info->showVerbose) {
printf("Server shutdown done\n"); fprintf(stderr, "Server shutdown done\n");
} }
ret = 0; /* success */ ret = 0; /* success */
break; break;
@@ -1369,7 +1378,7 @@ static int bench_tls_server(info_t* info)
info->server_stats.rxTime += rxTime; info->server_stats.rxTime += rxTime;
if (ret < 0) { if (ret < 0) {
printf("error on server read\n"); fprintf(stderr, "error on server read\n");
ret = wolfSSL_get_error(srv_ssl, ret); ret = wolfSSL_get_error(srv_ssl, ret);
goto exit; goto exit;
} }
@@ -1390,7 +1399,7 @@ static int bench_tls_server(info_t* info)
#endif #endif
info->server_stats.txTime += gettime_secs(0) - start; info->server_stats.txTime += gettime_secs(0) - start;
if (ret < 0) { if (ret < 0) {
printf("error on server write\n"); fprintf(stderr, "error on server write\n");
ret = wolfSSL_get_error(srv_ssl, ret); ret = wolfSSL_get_error(srv_ssl, ret);
goto exit; goto exit;
} }
@@ -1413,8 +1422,8 @@ static int bench_tls_server(info_t* info)
exit: exit:
if (ret != 0 && ret != WOLFSSL_SUCCESS) { if (ret != 0 && ret != WOLFSSL_SUCCESS) {
printf("Server Error: %d (%s)\n", ret, fprintf(stderr, "Server Error: %d (%s)\n", ret,
wolfSSL_ERR_reason_error_string(ret)); wolfSSL_ERR_reason_error_string(ret));
} }
/* clean up */ /* clean up */
@@ -1484,47 +1493,47 @@ static void print_stats(stats_t* wcStat, const char* desc, const char* cipher, i
formatStr = "%-6s %-33s %11d %9d %9.3f %9.3f %9.3f %9.3f %17.3f %15.3f\n"; formatStr = "%-6s %-33s %11d %9d %9.3f %9.3f %9.3f %9.3f %17.3f %15.3f\n";
} }
printf(formatStr, fprintf(stderr, formatStr,
desc, desc,
cipher, cipher,
wcStat->txTotal + wcStat->rxTotal, wcStat->txTotal + wcStat->rxTotal,
wcStat->connCount, wcStat->connCount,
wcStat->rxTime * 1000, wcStat->rxTime * 1000,
wcStat->txTime * 1000, wcStat->txTime * 1000,
wcStat->rxTotal / wcStat->rxTime / 1024 / 1024, wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
wcStat->txTotal / wcStat->txTime / 1024 / 1024, wcStat->txTotal / wcStat->txTime / 1024 / 1024,
wcStat->connTime * 1000, wcStat->connTime * 1000,
wcStat->connTime * 1000 / wcStat->connCount); wcStat->connTime * 1000 / wcStat->connCount);
} }
static void Usage(void) static void Usage(void)
{ {
printf("tls_bench " LIBWOLFSSL_VERSION_STRING fprintf(stderr, "tls_bench " LIBWOLFSSL_VERSION_STRING
" NOTE: All files relative to wolfSSL home dir\n"); " NOTE: All files relative to wolfSSL home dir\n");
printf("-? Help, print this usage\n"); fprintf(stderr, "-? Help, print this usage\n");
printf("-c Run as client only, no threading and uses sockets\n"); fprintf(stderr, "-c Run as client only, no threading and uses sockets\n");
printf("-s Run as server only, no threading and uses sockets\n"); fprintf(stderr, "-s Run as server only, no threading and uses sockets\n");
printf("-h Host (default %s)\n", BENCH_DEFAULT_HOST); fprintf(stderr, "-h Host (default %s)\n", BENCH_DEFAULT_HOST);
printf("-P Port (default %d)\n", BENCH_DEFAULT_PORT); fprintf(stderr, "-P Port (default %d)\n", BENCH_DEFAULT_PORT);
printf("-e List Every cipher suite available\n"); fprintf(stderr, "-e List Every cipher suite available\n");
printf("-i Show peer info\n"); fprintf(stderr, "-i Show peer info\n");
printf("-l <str> Cipher suite list (: delimited)\n"); fprintf(stderr, "-l <str> Cipher suite list (: delimited)\n");
printf("-t <num> Time <num> (seconds) to run each test (default %d)\n", BENCH_RUNTIME_SEC); fprintf(stderr, "-t <num> Time <num> (seconds) to run each test (default %d)\n", BENCH_RUNTIME_SEC);
printf("-p <num> The packet size <num> in bytes [1-16kB] (default %d)\n", TEST_PACKET_SIZE); fprintf(stderr, "-p <num> The packet size <num> in bytes [1-16kB] (default %d)\n", TEST_PACKET_SIZE);
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
printf(" In the case of DTLS, [1-8kB] (default %d)\n", TEST_DTLS_PACKET_SIZE); fprintf(stderr, " In the case of DTLS, [1-8kB] (default %d)\n", TEST_DTLS_PACKET_SIZE);
#endif #endif
printf("-S <num> The total size <num> in bytes (default %d)\n", TEST_MAX_SIZE); fprintf(stderr, "-S <num> The total size <num> in bytes (default %d)\n", TEST_MAX_SIZE);
printf("-v Show verbose output\n"); fprintf(stderr, "-v Show verbose output\n");
#ifdef DEBUG_WOLFSSL #ifdef DEBUG_WOLFSSL
printf("-d Enable debug messages\n"); fprintf(stderr, "-d Enable debug messages\n");
#endif #endif
#ifdef HAVE_PTHREAD #ifdef HAVE_PTHREAD
printf("-T <num> Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS); fprintf(stderr, "-T <num> Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS);
printf("-m Use local memory, not socket\n"); fprintf(stderr, "-m Use local memory, not socket\n");
#endif #endif
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
printf("-u Use DTLS\n"); fprintf(stderr, "-u Use DTLS\n");
#endif #endif
} }
@@ -1535,7 +1544,7 @@ static void ShowCiphers(void)
int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers)); int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
if (ret == WOLFSSL_SUCCESS) if (ret == WOLFSSL_SUCCESS)
printf("%s\n", ciphers); fprintf(stderr, "%s\n", ciphers);
} }
#ifdef __GNUC__ #ifdef __GNUC__
@@ -1631,7 +1640,7 @@ int bench_tls(void* args)
case 'p' : case 'p' :
argTestPacketSize = atoi(myoptarg); argTestPacketSize = atoi(myoptarg);
if (argTestPacketSize > (16 * 1024)) { if (argTestPacketSize > (16 * 1024)) {
printf("Invalid packet size %d\n", argTestPacketSize); fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
Usage(); Usage();
ret = MY_EX_USAGE; goto exit; ret = MY_EX_USAGE; goto exit;
} }
@@ -1667,7 +1676,7 @@ int bench_tls(void* args)
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
doDTLS = 1; doDTLS = 1;
#ifdef BENCH_USE_NONBLOCK #ifdef BENCH_USE_NONBLOCK
printf("tls_bench hasn't yet supported DTLS " fprintf(stderr, "tls_bench hasn't yet supported DTLS "
"non-blocking mode.\n"); "non-blocking mode.\n");
Usage(); Usage();
ret = MY_EX_USAGE; goto exit; ret = MY_EX_USAGE; goto exit;
@@ -1703,7 +1712,7 @@ int bench_tls(void* args)
} }
#ifndef HAVE_PTHREAD #ifndef HAVE_PTHREAD
else { else {
printf("Threading is not enabled, so please use -s or -c to indicate side\n"); fprintf(stderr, "Threading is not enabled, so please use -s or -c to indicate side\n");
Usage(); Usage();
ret = MY_EX_USAGE; goto exit; ret = MY_EX_USAGE; goto exit;
} }
@@ -1733,11 +1742,11 @@ int bench_tls(void* args)
#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER) #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
if (doDTLS) { if (doDTLS) {
if (argLocalMem) { if (argLocalMem) {
printf("tls_bench hasn't yet supported DTLS with local memory.\n"); fprintf(stderr, "tls_bench hasn't yet supported DTLS with local memory.\n");
ret = MY_EX_USAGE; goto exit; ret = MY_EX_USAGE; goto exit;
} }
if (option_p && argTestPacketSize > TEST_DTLS_PACKET_SIZE){ if (option_p && argTestPacketSize > TEST_DTLS_PACKET_SIZE){
printf("Invalid packet size %d\n", argTestPacketSize); fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
Usage(); Usage();
ret = MY_EX_USAGE; goto exit; ret = MY_EX_USAGE; goto exit;
} else { } else {
@@ -1747,7 +1756,7 @@ int bench_tls(void* args)
} }
} }
#endif #endif
printf("Running TLS Benchmarks...\n"); fprintf(stderr, "Running TLS Benchmarks...\n");
/* parse by : */ /* parse by : */
while ((cipher != NULL) && (cipher[0] != '\0')) { while ((cipher != NULL) && (cipher[0] != '\0')) {
@@ -1757,7 +1766,7 @@ int bench_tls(void* args)
} }
if (argShowVerbose) { if (argShowVerbose) {
printf("Cipher: %s\n", cipher); fprintf(stderr, "Cipher: %s\n", cipher);
} }
for (i=0; i<argThreadPairs; i++) { for (i=0; i<argThreadPairs; i++) {
@@ -1837,7 +1846,7 @@ int bench_tls(void* args)
} }
} while (!doShutdown); } while (!doShutdown);
if (argShowVerbose) { if (argShowVerbose) {
printf("Shutdown complete\n"); fprintf(stderr, "Shutdown complete\n");
} }
} }
#endif /* HAVE_PTHREAD */ #endif /* HAVE_PTHREAD */
@@ -1847,7 +1856,7 @@ int bench_tls(void* args)
for (i = 0; i < argThreadPairs; ++i) { for (i = 0; i < argThreadPairs; ++i) {
info = &theadInfo[i]; info = &theadInfo[i];
printf("\nThread %d\n", i); fprintf(stderr, "\nThread %d\n", i);
#ifndef NO_WOLFSSL_SERVER #ifndef NO_WOLFSSL_SERVER
if (!argClientOnly) if (!argClientOnly)
print_stats(&info->server_stats, "Server", info->cipher, 1); print_stats(&info->server_stats, "Server", info->cipher, 1);
@@ -1886,12 +1895,12 @@ int bench_tls(void* args)
} }
if (argShowVerbose) { if (argShowVerbose) {
printf("Totals for %d Threads\n", argThreadPairs); fprintf(stderr, "Totals for %d Threads\n", argThreadPairs);
} }
else { else {
printf("%-6s %-33s %11s %9s %9s %9s %9s %9s %17s %15s\n", fprintf(stderr, "%-6s %-33s %11s %9s %9s %9s %9s %9s %17s %15s\n",
"Side", "Cipher", "Total Bytes", "Num Conns", "Rx ms", "Tx ms", "Side", "Cipher", "Total Bytes", "Num Conns", "Rx ms", "Tx ms",
"Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms"); "Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms");
#ifndef NO_WOLFSSL_SERVER #ifndef NO_WOLFSSL_SERVER
if (!argClientOnly) if (!argClientOnly)
print_stats(&srv_comb, "Server", theadInfo[0].cipher, 0); print_stats(&srv_comb, "Server", theadInfo[0].cipher, 0);