From ce13ba62cc70fd6861ffc5e18f822cc3fb127841 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 5 Nov 2014 16:59:39 +0100 Subject: socket-default: Use poll(2) instead of select It is not only simpler, but also allows the use of arbitrary high fd numbers, which silently fails with select(). --- .../plugins/socket_default/socket_default_socket.c | 66 +++++++--------------- 1 file changed, 20 insertions(+), 46 deletions(-) (limited to 'src/libcharon/plugins/socket_default/socket_default_socket.c') diff --git a/src/libcharon/plugins/socket_default/socket_default_socket.c b/src/libcharon/plugins/socket_default/socket_default_socket.c index 9cc39955b..a0e4a54c5 100644 --- a/src/libcharon/plugins/socket_default/socket_default_socket.c +++ b/src/libcharon/plugins/socket_default/socket_default_socket.c @@ -150,66 +150,40 @@ METHOD(socket_t, receiver, status_t, chunk_t data; packet_t *pkt; host_t *source = NULL, *dest = NULL; - int bytes_read = 0; + int i, bytes_read = 0, selected = -1; bool oldstate; - - fd_set rfds; - int max_fd = 0, selected = 0; u_int16_t port = 0; + struct pollfd pfd[] = { + { .fd = this->ipv4, .events = POLLIN }, + { .fd = this->ipv4_natt, .events = POLLIN }, + { .fd = this->ipv6, .events = POLLIN }, + { .fd = this->ipv6_natt, .events = POLLIN }, + }; + int ports[] = { + /* port numbers assocaited to pollfds */ + this->port, this->natt, this->port, this->natt, + }; - FD_ZERO(&rfds); - - if (this->ipv4 != -1) - { - FD_SET(this->ipv4, &rfds); - max_fd = max(max_fd, this->ipv4); - } - if (this->ipv4_natt != -1) - { - FD_SET(this->ipv4_natt, &rfds); - max_fd = max(max_fd, this->ipv4_natt); - } - if (this->ipv6 != -1) - { - FD_SET(this->ipv6, &rfds); - max_fd = max(max_fd, this->ipv6); - } - if (this->ipv6_natt != -1) - { - FD_SET(this->ipv6_natt, &rfds); - max_fd = max(max_fd, this->ipv6_natt); - } DBG2(DBG_NET, "waiting for data on sockets"); oldstate = thread_cancelability(TRUE); - if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0) + if (poll(pfd, countof(pfd), -1) <= 0) { thread_cancelability(oldstate); return FAILED; } thread_cancelability(oldstate); - if (this->ipv4 != -1 && FD_ISSET(this->ipv4, &rfds)) - { - port = this->port; - selected = this->ipv4; - } - if (this->ipv4_natt != -1 && FD_ISSET(this->ipv4_natt, &rfds)) - { - port = this->natt; - selected = this->ipv4_natt; - } - if (this->ipv6 != -1 && FD_ISSET(this->ipv6, &rfds)) + for (i = 0; i < countof(pfd); i++) { - port = this->port; - selected = this->ipv6; - } - if (this->ipv6_natt != -1 && FD_ISSET(this->ipv6_natt, &rfds)) - { - port = this->natt; - selected = this->ipv6_natt; + if (pfd[i].revents & POLLIN) + { + selected = pfd[i].fd; + port = ports[i]; + break; + } } - if (selected) + if (selected != -1) { struct msghdr msg; struct cmsghdr *cmsgptr; -- cgit v1.2.3 From ed247660e81557261cd954e8b3ff19e410a347be Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Fri, 21 Nov 2014 11:43:20 +0100 Subject: socket-default: Use round-robin selection of sockets to read from If multiple sockets are ready, we previously preferred the IPv4 non-NAT socket over others. To handle all with equal priority, use a round-robin selection. --- .../plugins/socket_default/socket_default_socket.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/libcharon/plugins/socket_default/socket_default_socket.c') diff --git a/src/libcharon/plugins/socket_default/socket_default_socket.c b/src/libcharon/plugins/socket_default/socket_default_socket.c index a0e4a54c5..30e50d2d2 100644 --- a/src/libcharon/plugins/socket_default/socket_default_socket.c +++ b/src/libcharon/plugins/socket_default/socket_default_socket.c @@ -141,6 +141,11 @@ struct private_socket_default_socket_t { * TRUE if the source address should be set on outbound packets */ bool set_source; + + /** + * A counter to implement round-robin selection of read sockets + */ + u_int rr_counter; }; METHOD(socket_t, receiver, status_t, @@ -150,7 +155,7 @@ METHOD(socket_t, receiver, status_t, chunk_t data; packet_t *pkt; host_t *source = NULL, *dest = NULL; - int i, bytes_read = 0, selected = -1; + int i, rr, index, bytes_read = 0, selected = -1; bool oldstate; u_int16_t port = 0; struct pollfd pfd[] = { @@ -164,7 +169,6 @@ METHOD(socket_t, receiver, status_t, this->port, this->natt, this->port, this->natt, }; - DBG2(DBG_NET, "waiting for data on sockets"); oldstate = thread_cancelability(TRUE); if (poll(pfd, countof(pfd), -1) <= 0) @@ -174,12 +178,16 @@ METHOD(socket_t, receiver, status_t, } thread_cancelability(oldstate); + rr = this->rr_counter++; for (i = 0; i < countof(pfd); i++) { - if (pfd[i].revents & POLLIN) + /* To serve all ports with equal priority, we use a round-robin + * scheme to choose the one to process in this invocation */ + index = (rr + i) % countof(pfd); + if (pfd[index].revents & POLLIN) { - selected = pfd[i].fd; - port = ports[i]; + selected = pfd[index].fd; + port = ports[index]; break; } } -- cgit v1.2.3