From: Michael Tremer Date: Mon, 1 Feb 2021 18:25:59 +0000 (+0000) Subject: server: Automatically open 10 sockets X-Git-Tag: 0.1.0~22 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=0d5fde264d0d89ad95b59a74ca5cacc1f5303c4f;p=fireperf.git server: Automatically open 10 sockets Signed-off-by: Michael Tremer --- diff --git a/src/main.c b/src/main.c index d0f4ea1..b21eb50 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/main.h b/src/main.h index 98efccc..5d848c0 100644 --- a/src/main.h +++ b/src/main.h @@ -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; diff --git a/src/server.c b/src/server.c index 0424f9b..0243360 100644 --- a/src/server.c +++ b/src/server.c @@ -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);