Introduced ngx_reuseport() and ngx_noreuseport() helpers.

This commit is contained in:
Sergey Kandaurov 2025-10-16 15:22:56 +00:00
parent 78d1ab5a2c
commit 0f1a1275b0
3 changed files with 57 additions and 53 deletions

View File

@ -444,32 +444,12 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
* SO_REUSEPORT on the old socket before opening new ones
*/
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
@ -518,18 +498,9 @@ 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)
{
if (ngx_reuseport(s) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
"setsockopt(SO_REUSEPORT_LB) %V failed",
ngx_reuseport_n " %V failed",
&ls[i].addr_text);
if (ngx_close_socket(s) == -1) {
@ -540,26 +511,6 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
return NGX_ERROR;
}
#else
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
(const void *) &reuseport, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
"setsockopt(SO_REUSEPORT) %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
}
#endif

View File

@ -114,3 +114,37 @@ ngx_tcp_push(ngx_socket_t s)
}
#endif
#if (NGX_HAVE_REUSEPORT)
int
ngx_reuseport(ngx_socket_t s)
{
int reuseport = 1;
#ifdef SO_REUSEPORT_LB
return setsockopt(s, SOL_SOCKET, SO_REUSEPORT_LB,
(const void *) &reuseport, sizeof(int));
#else
return setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
(const void *) &reuseport, sizeof(int));
#endif
}
int
ngx_noreuseport(ngx_socket_t s)
{
int reuseport = 0;
#ifdef SO_REUSEPORT_LB
return setsockopt(s, SOL_SOCKET, SO_REUSEPORT_LB,
(const void *) &reuseport, sizeof(int));
#else
return setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
(const void *) &reuseport, sizeof(int));
#endif
}
#endif

View File

@ -63,6 +63,25 @@ int ngx_tcp_push(ngx_socket_t s);
#endif
#if (NGX_HAVE_REUSEPORT)
int ngx_reuseport(ngx_socket_t s);
int ngx_noreuseport(ngx_socket_t s);
#ifdef SO_REUSEPORT_LB
#define ngx_reuseport_n "setsockopt(SO_REUSEPORT_LB)"
#define ngx_noreuseport_n "setsockopt(!SO_REUSEPORT_LB)"
#else
#define ngx_reuseport_n "setsockopt(SO_REUSEPORT)"
#define ngx_noreuseport_n "setsockopt(!SO_REUSEPORT)"
#endif
#endif
#define ngx_shutdown_socket shutdown
#define ngx_shutdown_socket_n "shutdown()"