From: Michael Tremer Date: Thu, 18 Feb 2021 15:56:26 +0000 (+0000) Subject: Initialize the random pool for both, client and server X-Git-Tag: 0.2.0~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=36e66f552937c296060cedcb57f1109094e2936c;p=fireperf.git Initialize the random pool for both, client and server Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index f848f35..a68a46c 100644 --- a/src/client.c +++ b/src/client.c @@ -136,12 +136,12 @@ ERROR: } static int send_data_to_server(struct fireperf_config* conf, - struct fireperf_stats* stats, int fd, struct fireperf_random_pool* pool) { + struct fireperf_stats* stats, int fd) { const char* buffer = ZERO; ssize_t bytes_sent; - if (pool) { - buffer = fireperf_random_pool_get_slice(pool, SOCKET_SEND_BUFFER_SIZE); + if (conf->pool) { + buffer = fireperf_random_pool_get_slice(conf->pool, SOCKET_SEND_BUFFER_SIZE); } do { @@ -158,7 +158,7 @@ static int send_data_to_server(struct fireperf_config* conf, } static int handle_connection_ready(struct fireperf_config* conf, - struct fireperf_stats* stats, int fd, struct fireperf_random_pool* pool) { + struct fireperf_stats* stats, int fd) { // Are we supposed to close this connection straight away? if (conf->close) { DEBUG(conf, "Closing connection %d\n", fd); @@ -169,24 +169,13 @@ static int handle_connection_ready(struct fireperf_config* conf, return 0; } - return send_data_to_server(conf, stats, fd, pool); + return send_data_to_server(conf, stats, fd); } int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, int epollfd, int timerfd) { - struct fireperf_random_pool* pool = NULL; - DEBUG(conf, "Launching " PACKAGE_NAME " in client mode\n"); - // Initialize random pool - if (!conf->zero) { - pool = fireperf_random_pool_create(conf, CLIENT_RANDOM_POOL_SIZE); - if (!pool) { - ERROR(conf, "Could not allocate random data\n"); - return 1; - } - } - int r = 1; struct epoll_event ev = { @@ -274,7 +263,7 @@ int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, stats->open_connections--; } else if (events[i].events & EPOLLOUT) { - r = handle_connection_ready(conf, stats, fd, pool); + r = handle_connection_ready(conf, stats, fd); if (r) goto ERROR; } @@ -286,8 +275,5 @@ int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, r = 0; ERROR: - if (pool) - fireperf_random_pool_free(pool); - return r; } diff --git a/src/main.c b/src/main.c index 215c69c..983d5c8 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,7 @@ #include "client.h" #include "main.h" #include "logging.h" +#include "random.h" #include "server.h" #include "util.h" @@ -268,6 +269,16 @@ int main(int argc, char* argv[]) { if (r) return r; + // Initialize random pool + if (!conf.zero) { + conf.pool = fireperf_random_pool_create(&conf, DEFAULT_RANDOM_POOL_SIZE); + if (!conf.pool) { + ERROR(&conf, "Could not allocate random data\n"); + r = 1; + goto ERROR; + } + } + // Initialize epoll() int epollfd = epoll_create1(0); if (epollfd < 0) { @@ -328,6 +339,9 @@ ERROR: if (timerfd > 0) close(timerfd); + if (conf.pool) + fireperf_random_pool_free(conf.pool); + return r; } diff --git a/src/main.h b/src/main.h index 8e82117..49b05f9 100644 --- a/src/main.h +++ b/src/main.h @@ -30,6 +30,7 @@ #define DEFAULT_PARALLEL 1 #define DEFAULT_PORT 5001 #define DEFAULT_LISTENING_SOCKETS 1 +#define DEFAULT_RANDOM_POOL_SIZE (SOCKET_SEND_BUFFER_SIZE * 512) #define DEFAULT_TIMEOUT 300 #define MAX_PARALLEL (1 << 20) @@ -39,11 +40,11 @@ #define SOCKET_RECV_BUFFER_SIZE SOCKET_BUFFER_SIZE #define SOCKET_SEND_BUFFER_SIZE SOCKET_BUFFER_SIZE -// Random pool size -#define CLIENT_RANDOM_POOL_SIZE (SOCKET_SEND_BUFFER_SIZE * 512) - #define EPOLL_MAX_EVENTS 128 +// Forward declaration +struct fireperf_random_pool; + struct fireperf_config { int terminated; int loglevel; @@ -56,6 +57,7 @@ struct fireperf_config { int keepalive_only; int keepalive_count; int keepalive_interval; + struct fireperf_random_pool* pool; int port; unsigned int listening_sockets; unsigned long parallel; diff --git a/src/random.h b/src/random.h index 4e83ff9..e82877a 100644 --- a/src/random.h +++ b/src/random.h @@ -21,6 +21,8 @@ #ifndef FIREPERF_RANDOM_H #define FIREPERF_RANDOM_H +#include "main.h" + struct fireperf_random_pool { char* data; size_t size;