]> git.kaiwu.me - nginx.git/commitdiff
Added support for TCP_FASTOPEN supported in Linux >= 3.7.1.
authorMathew Rodley <mathew@rodley.com.au>
Tue, 3 Dec 2013 18:07:03 +0000 (22:07 +0400)
committerMathew Rodley <mathew@rodley.com.au>
Tue, 3 Dec 2013 18:07:03 +0000 (22:07 +0400)
---
 auto/unix                       | 12 ++++++++++++
 src/core/ngx_connection.c       | 32 ++++++++++++++++++++++++++++++++
 src/core/ngx_connection.h       |  4 ++++
 src/http/ngx_http.c             |  4 ++++
 src/http/ngx_http_core_module.c | 21 +++++++++++++++++++++
 src/http/ngx_http_core_module.h |  3 +++
 6 files changed, 76 insertions(+)

auto/unix
src/core/ngx_connection.c
src/core/ngx_connection.h
src/http/ngx_http.c
src/http/ngx_http_core_module.c
src/http/ngx_http_core_module.h

index 4dbdbe0c0539d2ea9aead2b232a47fb217fd0813..10fd3d2933481473e385e84cdf240d4c95b1f037 100755 (executable)
--- a/auto/unix
+++ b/auto/unix
@@ -344,6 +344,18 @@ ngx_feature_test="setsockopt(0, IPPROTO_TCP, TCP_KEEPIDLE, NULL, 0);
 . auto/feature
 
 
+ngx_feature="TCP_FASTOPEN"
+ngx_feature_name="NGX_HAVE_TCP_FASTOPEN"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/socket.h>
+                  #include <netinet/in.h>
+                  #include <netinet/tcp.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="setsockopt(0, IPPROTO_TCP, TCP_FASTOPEN, NULL, 0)"
+. auto/feature
+
+
 ngx_feature="TCP_INFO"
 ngx_feature_name="NGX_HAVE_TCP_INFO"
 ngx_feature_run=no
index fbcd5f719e6651600f5fa0c3bc5a8bbeb5afca78..4544b294a80299d9c6d3899059aa8625ad0fe9dd 100644 (file)
@@ -82,6 +82,10 @@ ngx_create_listening(ngx_conf_t *cf, void *sockaddr, socklen_t socklen)
     ls->setfib = -1;
 #endif
 
+#if (NGX_HAVE_TCP_FASTOPEN)
+    ls->fastopen = -1;
+#endif
+
     return ls;
 }
 
@@ -209,6 +213,21 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
 #endif
 #endif
 
+#if (NGX_HAVE_TCP_FASTOPEN)
+
+        if (getsockopt(ls[i].fastopen, IPPROTO_TCP, TCP_FASTOPEN,
+                       (void *) &ls[i].fastopen, &olen)
+            == -1)
+        {
+            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
+                          "getsockopt(TCP_FASTOPEN) %V failed, ignored",
+                          &ls[i].addr_text);
+
+            ls[i].fastopen = -1;
+        }
+
+#endif
+
 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
 
         ngx_memzero(&af, sizeof(struct accept_filter_arg));
@@ -582,6 +601,19 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
         }
 #endif
 
+#if (NGX_HAVE_TCP_FASTOPEN)
+        if (ls[i].fastopen != -1) {
+            if (setsockopt(ls[i].fd, IPPROTO_TCP, TCP_FASTOPEN,
+                           (const void *) &ls[i].fastopen, sizeof(int))
+                == -1)
+            {
+                ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
+                              "setsockopt(TCP_FASTOPEN, %d) %V failed, ignored",
+                              ls[i].fastopen, &ls[i].addr_text);
+            }
+        }
+#endif
+
 #if 0
         if (1) {
             int tcp_nodelay = 1;
index 3daf2eee4cbb82f9d6cd2b7651cecfc6e00cd9ff..53149676ecc45eaffde06a79accafa5e70f52fd4 100644 (file)
@@ -80,6 +80,10 @@ struct ngx_listening_s {
     int                 setfib;
 #endif
 
+#if (NGX_HAVE_TCP_FASTOPEN)
+    int                 fastopen;
+#endif
+
 };
 
 
index 91bb04f5587b8ec78d2a4f415923a7cdc71330b8..f030baa717d4cbee1e52c929a25bb67ea5ee4116 100644 (file)
@@ -1811,6 +1811,10 @@ ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
     ls->setfib = addr->opt.setfib;
 #endif
 
+#if (NGX_HAVE_TCP_FASTOPEN)
+    ls->fastopen = addr->opt.fastopen;
+#endif
+
     return ls;
 }
 
index 4e5c78e9b1492a50fe60d2fe964770e3ca7aa836..8abf864d535c3aec9c52dc4e3a42f048aa11421b 100644 (file)
@@ -3040,6 +3040,9 @@ ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
         lsopt.sndbuf = -1;
 #if (NGX_HAVE_SETFIB)
         lsopt.setfib = -1;
+#endif
+#if (NGX_HAVE_TCP_FASTOPEN)
+        lsopt.fastopen = -1;
 #endif
         lsopt.wildcard = 1;
 
@@ -3988,6 +3991,9 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
     lsopt.sndbuf = -1;
 #if (NGX_HAVE_SETFIB)
     lsopt.setfib = -1;
+#endif
+#if (NGX_HAVE_TCP_FASTOPEN)
+    lsopt.fastopen = -1;
 #endif
     lsopt.wildcard = u.wildcard;
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
@@ -4027,6 +4033,23 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
             continue;
         }
 #endif
+
+#if (NGX_HAVE_TCP_FASTOPEN)
+        if (ngx_strncmp(value[n].data, "fastopen=", 9) == 0) {
+            lsopt.fastopen = ngx_atoi(value[n].data + 9, value[n].len - 9);
+            lsopt.set = 1;
+            lsopt.bind = 1;
+
+            if (lsopt.fastopen == NGX_ERROR) {
+                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                   "invalid fastopen \"%V\"", &value[n]);
+                return NGX_CONF_ERROR;
+            }
+
+            continue;
+        }
+#endif
+
         if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
             lsopt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
             lsopt.set = 1;
index 5b38000d8db1c55674031acf0ed364b06df8ee04..220c94ee4d129712def0884aa49a8f5492463890 100644 (file)
@@ -89,6 +89,9 @@ typedef struct {
 #if (NGX_HAVE_SETFIB)
     int                        setfib;
 #endif
+#if (NGX_HAVE_TCP_FASTOPEN)
+    int                        fastopen;
+#endif
 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
     int                        tcp_keepidle;
     int                        tcp_keepintvl;