Variables: Socket cookie variable for debugging purposes

Donatas Abraitis donatas.abraitis at gmail.com
Fri Jul 7 19:28:42 UTC 2017


Hi,

this looks promising when new 4.12 kernel introduced SO_COOKIE socket
option which is able to generate a cookie for the socket.

More information and implementation is here:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5daab9db7b65df87da26fd8cfa695fb9546a1ddb

Waiting for comments.

# HG changeset patch
# User Donatas Abraitis <donatas.abraitis at gmail.com>
# Date 1499424023 -10800
#      Fri Jul 07 13:40:23 2017 +0300
# Node ID 55b401978df27f1a1ab4eee4e50615b7551e3c0b
# Parent  70e65bf8dfd7a8d39aae8ac3a209d426e6947735
Add new `socket_cookie` variable

This would be useful for tracking connections by cookie, not only by
request_id as we have it right now. Related:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5daab9db7b65df87da26fd8cfa695fb9546a1ddb

diff -r 70e65bf8dfd7 -r 55b401978df2 auto/unix
--- a/auto/unix    Tue Jul 04 18:50:41 2017 +0300
+++ b/auto/unix    Fri Jul 07 13:40:23 2017 +0300
@@ -331,6 +331,16 @@
 ngx_feature_test="setsockopt(0, SOL_SOCKET, SO_REUSEPORT, NULL, 0)"
 . auto/feature

+ngx_feature="SO_COOKIE"
+ngx_feature_name="NGX_HAVE_SOCKET_COOKIE"
+ngx_feature_run=no
+ngx_feature_incs="#include <sys/socket.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="uint64_t cookie;
+                  socklen_t cookie_len = sizeof(cookie);
+                  getsockopt(0, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len)"
+. auto/feature

 ngx_feature="SO_ACCEPTFILTER"
 ngx_feature_name="NGX_HAVE_DEFERRED_ACCEPT"
diff -r 70e65bf8dfd7 -r 55b401978df2 src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c    Tue Jul 04 18:50:41 2017 +0300
+++ b/src/http/ngx_http_variables.c    Fri Jul 07 13:40:23 2017 +0300
@@ -46,6 +46,10 @@
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+#if (NGX_HAVE_SOCKET_COOKIE)
+static ngx_int_t ngx_http_variable_socket_cookie(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
+#endif
 #if (NGX_HAVE_TCP_INFO)
 static ngx_int_t ngx_http_variable_tcpinfo(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
@@ -346,7 +350,10 @@

     { ngx_string("time_local"), NULL, ngx_http_variable_time_local,
       0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
-
+#if (NGX_HAVE_SOCKET_COOKIE)
+    { ngx_string("socket_cookie"), NULL, ngx_http_variable_socket_cookie,
+      0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+#endif
 #if (NGX_HAVE_TCP_INFO)
     { ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,
       0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -1098,6 +1105,28 @@
     return NGX_OK;
 }

+#if (NGX_HAVE_SOCKET_COOKIE)
+static ngx_int_t
+ngx_http_variable_socket_cookie(ngx_http_request_t *r,
ngx_http_variable_value_t *v,
+    uintptr_t data)
+{
+    uint64_t cookie;
+    socklen_t cookie_len = sizeof(cookie);;
+
+    if (getsockopt(0, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) == -1) {
+        v->not_found = 1;
+        return NGX_OK;
+    }
+
+    v->data = cookie;
+    v->len = cookie_len;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
+    return NGX_OK;
+}
+#endif

 #if (NGX_HAVE_TCP_INFO)

-- 
Donatas


More information about the nginx-devel mailing list