]> git.ipfire.org Git - fireperf.git/commitdiff
server: Automatically open 10 sockets
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 1 Feb 2021 18:25:59 +0000 (18:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 1 Feb 2021 18:25:59 +0000 (18:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/main.c
src/main.h
src/server.c

index d0f4ea153fed5a98d25765bf7a151ff2ded2530b..b21eb504ee462e344a8a4c73755bba573270f57e 100644 (file)
@@ -183,6 +183,7 @@ int main(int argc, char* argv[]) {
                .mode = FIREPERF_MODE_NONE,
                .port = DEFAULT_PORT,
                .parallel = DEFAULT_PARALLEL,
+               .sockets = DEFAULT_SOCKETS,
                .timeout = DEFAULT_TIMEOUT,
        };
        int r;
index 98efccc4d62e9bcba45632cd5fb8ffbd1f2111c2..5d848c0be97d6aa44a5bcc528be376a6386ff3b8 100644 (file)
@@ -28,6 +28,7 @@
 #define DEFAULT_LOG_LEVEL LOG_INFO
 #define DEFAULT_PARALLEL 1
 #define DEFAULT_PORT 5001
+#define DEFAULT_SOCKETS 10
 #define DEFAULT_TIMEOUT 300
 
 #define MAX_PARALLEL (1 << 20)
@@ -55,6 +56,7 @@ struct fireperf_config {
        int keepalive_count;
        int keepalive_interval;
        int port;
+       unsigned int sockets;
        unsigned long parallel;
        unsigned int timeout;
        int zero;
index 0424f9ba6e9de4551c65a2b0b602c4a9ae648619..0243360dc4a67c63f59a2c49ad400aa04350aac2 100644 (file)
@@ -166,7 +166,7 @@ static int dump_stats(struct fireperf_config* conf, struct fireperf_server_stats
        return 0;
 }
 
-static int create_socket(struct fireperf_config* conf) {
+static int create_socket(struct fireperf_config* conf, int i) {
        int r;
 
        // Open a new socket
@@ -187,7 +187,7 @@ static int create_socket(struct fireperf_config* conf) {
 
        struct sockaddr_in6 addr = {
                .sin6_family = AF_INET6,
-               .sin6_port = htons(conf->port),
+               .sin6_port = htons(conf->port + i),
        };
 
        // Bind it to the selected port
@@ -260,23 +260,27 @@ static int handle_io_on_connection(struct fireperf_config* conf,
        return 0;
 }
 
+static int is_listening_socket(struct fireperf_config* conf, int* sockets, int fd) {
+       for (unsigned int i = 0; i < conf->sockets; i++) {
+               if (sockets[i] == fd)
+                       return 1;
+       }
+
+       return 0;
+}
+
 int fireperf_server(struct fireperf_config* conf) {
        struct fireperf_server_stats stats = { 0 };
 
        DEBUG(conf, "Launching " PACKAGE_NAME " in server mode\n");
 
-       int sockfd = -1;
+       int sockets[conf->sockets];
 
        int epollfd = -1;
        struct epoll_event events[EPOLL_MAX_EVENTS];
 
        int r = 1;
 
-       // Create listening socket
-       sockfd = create_socket(conf);
-       if (sockfd < 0)
-               return 1;
-
        // Initialize epoll()
        epollfd = epoll_create1(0);
        if (epollfd < 0) {
@@ -284,16 +288,25 @@ int fireperf_server(struct fireperf_config* conf) {
                return 1;
        }
 
-       // Add listening socket
-       struct epoll_event ev = {
-               .events  = EPOLLIN,
-               .data.fd = sockfd,
-       };
+       // Create listening sockets
+       for (unsigned int i = 0; i < conf->sockets; i++) {
+               int sockfd = create_socket(conf, i);
+               if (sockfd < 0)
+                       return 1;
 
-       if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev)) {
-               ERROR(conf, "Could not add socket file descriptor to epoll(): %s\n",
-                       strerror(errno));
-               goto ERROR;
+               sockets[i] = sockfd;
+
+               // Add listening socket to epoll
+               struct epoll_event ev = {
+                       .events  = EPOLLIN,
+                       .data.fd = sockfd,
+               };
+
+               if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev)) {
+                       ERROR(conf, "Could not add socket file descriptor to epoll(): %s\n",
+                               strerror(errno));
+                       goto ERROR;
+               }
        }
 
        // Create timerfd() to print statistics
@@ -303,8 +316,10 @@ int fireperf_server(struct fireperf_config* conf) {
                goto ERROR;
        }
 
-       ev.events = EPOLLIN;
-       ev.data.fd = timerfd;
+       struct epoll_event ev = {
+               .events  = EPOLLIN,
+               .data.fd = timerfd,
+       };
 
        if (epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &ev)) {
                ERROR(conf, "Could not add timerfd to epoll(): %s\n",
@@ -341,8 +356,8 @@ int fireperf_server(struct fireperf_config* conf) {
                        int fd = events[i].data.fd;
 
                        // The listening socket
-                       if (fd == sockfd) {
-                               int connfd = accept_connection(conf, sockfd);
+                       if (is_listening_socket(conf, sockets, fd)) {
+                               int connfd = accept_connection(conf, fd);
                                if (connfd < 0)
                                        goto ERROR;
 
@@ -406,8 +421,10 @@ int fireperf_server(struct fireperf_config* conf) {
        }
 
 ERROR:
-       if (sockfd > 0)
-               close(sockfd);
+       for (unsigned int i = 0; i < conf->sockets; i++) {
+               if (sockets[i] > 0)
+                       close(sockets[i]);
+       }
 
        if (epollfd > 0)
                close(epollfd);