[nginx] Added FreeBSD support for "listen ... reuseport".

Ruslan Ermilov ru at nginx.com
Mon Jul 2 12:45:18 UTC 2018


details:   http://hg.nginx.org/nginx/rev/5c2ac36fcf56
branches:  
changeset: 7304:5c2ac36fcf56
user:      Ruslan Ermilov <ru at nginx.com>
date:      Mon Jul 02 13:54:33 2018 +0300
description:
Added FreeBSD support for "listen ... reuseport".

diffstat:

 src/core/ngx_connection.c |  54 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)

diffs (99 lines):

diff -r 118885f7a577 -r 5c2ac36fcf56 src/core/ngx_connection.c
--- a/src/core/ngx_connection.c	Fri Jun 15 17:29:55 2018 +0300
+++ b/src/core/ngx_connection.c	Mon Jul 02 13:54:33 2018 +0300
@@ -281,6 +281,22 @@ ngx_set_inherited_sockets(ngx_cycle_t *c
         reuseport = 0;
         olen = sizeof(int);
 
+#ifdef SO_REUSEPORT_LB
+
+        if (getsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT_LB,
+                       (void *) &reuseport, &olen)
+            == -1)
+        {
+            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
+                          "getsockopt(SO_REUSEPORT_LB) %V failed, ignored",
+                          &ls[i].addr_text);
+
+        } else {
+            ls[i].reuseport = reuseport ? 1 : 0;
+        }
+
+#else
+
         if (getsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT,
                        (void *) &reuseport, &olen)
             == -1)
@@ -292,6 +308,7 @@ ngx_set_inherited_sockets(ngx_cycle_t *c
         } else {
             ls[i].reuseport = reuseport ? 1 : 0;
         }
+#endif
 
 #endif
 
@@ -430,6 +447,20 @@ ngx_open_listening_sockets(ngx_cycle_t *
 
                 int  reuseport = 1;
 
+#ifdef SO_REUSEPORT_LB
+
+                if (setsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT_LB,
+                               (const void *) &reuseport, sizeof(int))
+                    == -1)
+                {
+                    ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
+                                  "setsockopt(SO_REUSEPORT_LB) %V failed, "
+                                  "ignored",
+                                  &ls[i].addr_text);
+                }
+
+#else
+
                 if (setsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT,
                                (const void *) &reuseport, sizeof(int))
                     == -1)
@@ -438,6 +469,7 @@ ngx_open_listening_sockets(ngx_cycle_t *
                                   "setsockopt(SO_REUSEPORT) %V failed, ignored",
                                   &ls[i].addr_text);
                 }
+#endif
 
                 ls[i].add_reuseport = 0;
             }
@@ -488,6 +520,27 @@ ngx_open_listening_sockets(ngx_cycle_t *
 
                 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)
@@ -504,6 +557,7 @@ ngx_open_listening_sockets(ngx_cycle_t *
 
                     return NGX_ERROR;
                 }
+#endif
             }
 #endif
 


More information about the nginx-devel mailing list