From aedc0b15d1ba1d7b8b1c8ec1f483e0a31c07fe22 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Tue, 19 Nov 2024 22:34:17 +0800 Subject: [PATCH] Core: implement TCP_KEEPALIVE on Darwin In earlier versions, macOS only supported setting TCP_KEEPALIVE in place of TCP_KEEPIDLE, but macOS has supported TCP_KEEPINTVL and TCP_KEEPCNT since 10.8 in 2012. Ref: https://lists.apple.com/archives/macnetworkprog/2012/Jul/msg00005.html https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/netinet/tcp.h#L215-L230 https://github.com/apple-oss-distributions/xnu/blob/8d741a5de7ff4191bf97d57b9f54c2f6d4a15585/bsd/netinet/tcp.h#L226-L241 --- auto/os/darwin | 16 ++++++++++++++++ auto/unix | 26 ++++++++++++++------------ src/core/ngx_connection.c | 12 ++++++++++++ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/auto/os/darwin b/auto/os/darwin index 429468f7f..e61317064 100644 --- a/auto/os/darwin +++ b/auto/os/darwin @@ -118,3 +118,19 @@ ngx_feature_libs= ngx_feature_test="int32_t lock = 0; if (!OSAtomicCompareAndSwap32Barrier(0, 1, &lock)) return 1" . auto/feature + + +# TCP keepalive + +ngx_feature="TCP_KEEPALIVE" +ngx_feature_name="NGX_HAVE_KEEPALIVE_TUNABLE" +ngx_feature_run=no +ngx_feature_incs="#include + #include + #include " +ngx_feature_path= +ngx_feature_libs= +ngx_feature_test="setsockopt(0, IPPROTO_TCP, TCP_KEEPALIVE, NULL, 0); + setsockopt(0, IPPROTO_TCP, TCP_KEEPINTVL, NULL, 0); + setsockopt(0, IPPROTO_TCP, TCP_KEEPCNT, NULL, 0)" +. auto/feature diff --git a/auto/unix b/auto/unix index f29e69c61..2314fb515 100644 --- a/auto/unix +++ b/auto/unix @@ -508,18 +508,20 @@ ngx_feature_test="setsockopt(0, IPPROTO_TCP, TCP_DEFER_ACCEPT, NULL, 0)" . auto/feature -ngx_feature="TCP_KEEPIDLE" -ngx_feature_name="NGX_HAVE_KEEPALIVE_TUNABLE" -ngx_feature_run=no -ngx_feature_incs="#include - #include - #include " -ngx_feature_path= -ngx_feature_libs= -ngx_feature_test="setsockopt(0, IPPROTO_TCP, TCP_KEEPIDLE, NULL, 0); - setsockopt(0, IPPROTO_TCP, TCP_KEEPINTVL, NULL, 0); - setsockopt(0, IPPROTO_TCP, TCP_KEEPCNT, NULL, 0)" -. auto/feature +if [ -z "$NGX_HAVE_KEEPALIVE_TUNABLE" ]; then + ngx_feature="TCP_KEEPIDLE" + ngx_feature_name="NGX_HAVE_KEEPALIVE_TUNABLE" + ngx_feature_run=no + ngx_feature_incs="#include + #include + #include " + ngx_feature_path= + ngx_feature_libs= + ngx_feature_test="setsockopt(0, IPPROTO_TCP, TCP_KEEPIDLE, NULL, 0); + setsockopt(0, IPPROTO_TCP, TCP_KEEPINTVL, NULL, 0); + setsockopt(0, IPPROTO_TCP, TCP_KEEPCNT, NULL, 0)" + . auto/feature +fi ngx_feature="TCP_FASTOPEN" diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c index 75809d9ad..1b971b8b6 100644 --- a/src/core/ngx_connection.c +++ b/src/core/ngx_connection.c @@ -772,6 +772,7 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle) value *= NGX_KEEPALIVE_FACTOR; #endif +#ifdef TCP_KEEPIDLE if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPIDLE, (const void *) &value, sizeof(int)) == -1) @@ -780,6 +781,17 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle) "setsockopt(TCP_KEEPIDLE, %d) %V failed, ignored", value, &ls[i].addr_text); } +#elif defined(TCP_KEEPALIVE) + /* The equivalent of TCP_KEEPIDLE on Darwin is TCP_KEEPALIVE. */ + if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_KEEPALIVE, + (const void *) &value, sizeof(int)) + == -1) + { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno, + "setsockopt(TCP_KEEPALIVE, %d) %V failed, ignored", + value, &ls[i].addr_text); + } +#endif } if (ls[i].keepintvl) {