|
|
|
|
@ -315,6 +315,27 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle) |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifdef SO_PROTOCOL |
|
|
|
|
|
|
|
|
|
olen = sizeof(int); |
|
|
|
|
|
|
|
|
|
if (getsockopt(ls[i].fd, SOL_SOCKET, SO_PROTOCOL, |
|
|
|
|
(void *) &ls[i].protocol, &olen) |
|
|
|
|
== -1) |
|
|
|
|
{ |
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno, |
|
|
|
|
"getsockopt(SO_PROTOCOL) %V failed", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
ls[i].ignore = 1; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (ls[i].protocol == IPPROTO_TCP) { |
|
|
|
|
ls[i].protocol = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#if (NGX_HAVE_TCP_FASTOPEN) |
|
|
|
|
|
|
|
|
|
olen = sizeof(int); |
|
|
|
|
@ -436,45 +457,28 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle) |
|
|
|
|
|
|
|
|
|
#if (NGX_HAVE_REUSEPORT) |
|
|
|
|
|
|
|
|
|
if (ls[i].add_reuseport) { |
|
|
|
|
if (ls[i].add_reuseport || ls[i].change_protocol) { |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* to allow transition from a socket without SO_REUSEPORT |
|
|
|
|
* to multiple sockets with SO_REUSEPORT, we have to set |
|
|
|
|
* SO_REUSEPORT on the old socket before opening new ones |
|
|
|
|
* |
|
|
|
|
* to allow socket protocol change (e.g. IPPROTO_MPTCP), |
|
|
|
|
* SO_REUSEPORT is set temporarily on both sockets |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
int reuseport = 1; |
|
|
|
|
|
|
|
|
|
#ifdef SO_REUSEPORT_LB |
|
|
|
|
|
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT_LB, |
|
|
|
|
(const void *) &reuseport, sizeof(int)) |
|
|
|
|
== -1) |
|
|
|
|
{ |
|
|
|
|
if (ngx_reuseport(ls[i].fd) == -1) { |
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, |
|
|
|
|
"setsockopt(SO_REUSEPORT_LB) %V failed, " |
|
|
|
|
"ignored", |
|
|
|
|
ngx_reuseport_n " %V failed, ignored", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
if (setsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT, |
|
|
|
|
(const void *) &reuseport, sizeof(int)) |
|
|
|
|
== -1) |
|
|
|
|
{ |
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, |
|
|
|
|
"setsockopt(SO_REUSEPORT) %V failed, ignored", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
ls[i].add_reuseport = 0; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (ls[i].fd != (ngx_socket_t) -1) { |
|
|
|
|
if (ls[i].fd != (ngx_socket_t) -1 && !ls[i].change_protocol) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -487,7 +491,8 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle) |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, 0); |
|
|
|
|
s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, |
|
|
|
|
ls[i].protocol); |
|
|
|
|
|
|
|
|
|
if (s == (ngx_socket_t) -1) { |
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
|
|
|
|
@ -517,38 +522,12 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle) |
|
|
|
|
|
|
|
|
|
#if (NGX_HAVE_REUSEPORT) |
|
|
|
|
|
|
|
|
|
if (ls[i].reuseport && !ngx_test_config) { |
|
|
|
|
int reuseport; |
|
|
|
|
|
|
|
|
|
reuseport = 1; |
|
|
|
|
|
|
|
|
|
#ifdef SO_REUSEPORT_LB |
|
|
|
|
|
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT_LB, |
|
|
|
|
(const void *) &reuseport, sizeof(int)) |
|
|
|
|
== -1) |
|
|
|
|
{ |
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
|
|
|
|
"setsockopt(SO_REUSEPORT_LB) %V failed", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) { |
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
|
|
|
|
ngx_close_socket_n " %V failed", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return NGX_ERROR; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, |
|
|
|
|
(const void *) &reuseport, sizeof(int)) |
|
|
|
|
== -1) |
|
|
|
|
{ |
|
|
|
|
if ((ls[i].reuseport || ls[i].change_protocol) |
|
|
|
|
&& !ngx_test_config) |
|
|
|
|
{ |
|
|
|
|
if (ngx_reuseport(s) == -1) { |
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
|
|
|
|
"setsockopt(SO_REUSEPORT) %V failed", |
|
|
|
|
ngx_reuseport_n " %V failed", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) { |
|
|
|
|
@ -559,7 +538,6 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle) |
|
|
|
|
|
|
|
|
|
return NGX_ERROR; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
@ -686,6 +664,25 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle) |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if (NGX_HAVE_REUSEPORT) |
|
|
|
|
|
|
|
|
|
if (!ls[i].reuseport && ls[i].change_protocol && !ngx_test_config) { |
|
|
|
|
if (ngx_noreuseport(s) == -1) { |
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
|
|
|
|
ngx_noreuseport_n " %V failed", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) { |
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
|
|
|
|
ngx_close_socket_n " %V failed", |
|
|
|
|
&ls[i].addr_text); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return NGX_ERROR; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
ls[i].listen = 1; |
|
|
|
|
|
|
|
|
|
ls[i].fd = s; |
|
|
|
|
|