Skip to content

Commit

Permalink
daemon/network: enabled Linux-style SO_REUSEPORT if supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Vavruša committed Jun 29, 2015
1 parent ada0fd5 commit 43e351c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions daemon/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
#include "daemon/worker.h"
#include "daemon/io.h"

/* libuv 1.7.0+ is able to support SO_REUSEPORT for loadbalancing */
#define UV_VERSION_NUM UV_VERSION_MAJOR ## UV_VERSION_MINOR ## UV_VERSION_PATCH
#if (defined(ENABLE_REUSEPORT) || UV_VERSION_NUM >= 170) && (__linux__ && SO_REUSEPORT)
#define handle_init(type, loop, handle, family) do { \
uv_ ## type ## _init_ex((loop), (handle), (family)); \
uv_os_fd_t fd = 0; \
if (uv_fileno((uv_handle_t *)(handle), &fd) == 0) { \
int on = 1; \
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)); \
} \
} while (0)
#else
#define handle_init(type, loop, handle, family) \
uv_ ## type ## _init((loop), (handle))
#endif

void network_init(struct network *net, uv_loop_t *loop)
{
if (net != NULL) {
Expand Down Expand Up @@ -96,15 +112,15 @@ static int insert_endpoint(struct network *net, const char *addr, struct endpoin
static int open_endpoint(struct network *net, struct endpoint *ep, struct sockaddr *sa, uint32_t flags)
{
if (flags & NET_UDP) {
uv_udp_init(net->loop, &ep->udp);
handle_init(udp, net->loop, &ep->udp, sa->sa_family);
int ret = udp_bind(ep, sa);
if (ret != 0) {
return ret;
}
ep->flags |= NET_UDP;
}
if (flags & NET_TCP) {
uv_tcp_init(net->loop, &ep->tcp);
handle_init(tcp, net->loop, &ep->tcp, sa->sa_family);
int ret = tcp_bind(ep, sa);
if (ret != 0) {
return ret;
Expand Down
2 changes: 1 addition & 1 deletion scripts/bootstrap-depends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

CMOCKA_TAG="cmocka-0.4.1"
CMOCKA_URL="git://git.cryptomilk.org/projects/cmocka.git"
LIBUV_TAG="v1.5.0"
LIBUV_TAG="v1.x"
LIBUV_URL="https://github.com/libuv/libuv.git"
KNOT_TAG="edc125bc"
KNOT_URL="https://github.com/CZ-NIC/knot.git"
Expand Down

0 comments on commit 43e351c

Please sign in to comment.