]> git.ipfire.org Git - fireperf.git/commitdiff
Initialize the random pool for both, client and server
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Feb 2021 15:56:26 +0000 (15:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Feb 2021 15:56:26 +0000 (15:56 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c
src/main.c
src/main.h
src/random.h

index f848f35a74fe7b0bcc34657f7615ad0d8ee0bd98..a68a46cf4d4e6118318c337ccd48da895c5e71ba 100644 (file)
@@ -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;
 }
index 215c69c3ea67d151d2e1b5bc35fab91a7d09c044..983d5c8bf7b8eaadc9b9e55cd26703bb6b62ca33 100644 (file)
@@ -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;
 }
 
index 8e821179254788c2f3fbe8f590e4e42091d71510..49b05f9ab2986c86718698642cca2a3c358775bf 100644 (file)
@@ -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)
 #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;
index 4e83ff9b9eed2dff1927fe8133f0f0d0c2a12b8d..e82877a579d473d45cc9e74e105f71b92192e3a0 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef FIREPERF_RANDOM_H
 #define FIREPERF_RANDOM_H
 
+#include "main.h"
+
 struct fireperf_random_pool {
        char* data;
        size_t size;