]> git.kaiwu.me - nginx.git/commitdiff
Common tree insert function for QUIC and UDP connections.
authorRoman Arutyunyan <arut@nginx.com>
Sun, 14 May 2023 08:30:11 +0000 (12:30 +0400)
committerRoman Arutyunyan <arut@nginx.com>
Sun, 14 May 2023 08:30:11 +0000 (12:30 +0400)
Previously, ngx_udp_rbtree_insert_value() was used for plain UDP and
ngx_quic_rbtree_insert_value() was used for QUIC.  Because of this it was
impossible to initialize connection tree in ngx_create_listening() since
this function is not aware what kind of listening it creates.

Now ngx_udp_rbtree_insert_value() is used for both QUIC and UDP.  To make
is possible, a generic key field is added to ngx_udp_connection_t.  It keeps
client address for UDP and connection ID for QUIC.

src/core/ngx_connection.c
src/event/ngx_event_udp.c
src/event/ngx_event_udp.h
src/event/quic/ngx_event_quic.h
src/event/quic/ngx_event_quic_socket.c
src/event/quic/ngx_event_quic_udp.c
src/http/ngx_http.c
src/stream/ngx_stream.c

index 5e56839280cab9d032703bf14e52bdb248a5a911..10f4d9b9136d565b831be4cfa6c2e6b20c4225f3 100644 (file)
@@ -72,6 +72,10 @@ ngx_create_listening(ngx_conf_t *cf, struct sockaddr *sockaddr,
 
     ngx_memcpy(ls->addr_text.data, text, len);
 
+#if !(NGX_WIN32)
+    ngx_rbtree_init(&ls->rbtree, &ls->sentinel, ngx_udp_rbtree_insert_value);
+#endif
+
     ls->fd = (ngx_socket_t) -1;
     ls->type = SOCK_STREAM;
 
index ec86fdfbc41d44fe00a025c0f2d07006f0b6a914..43fa621b0c8aedb2fbb4a8405531ba7b73b81f95 100644 (file)
@@ -417,8 +417,8 @@ ngx_udp_rbtree_insert_value(ngx_rbtree_node_t *temp,
             udpt = (ngx_udp_connection_t *) temp;
             ct = udpt->connection;
 
-            rc = ngx_cmp_sockaddr(c->sockaddr, c->socklen,
-                                  ct->sockaddr, ct->socklen, 1);
+            rc = ngx_memn2cmp(udp->key.data, udpt->key.data,
+                              udp->key.len, udpt->key.len);
 
             if (rc == 0 && c->listening->wildcard) {
                 rc = ngx_cmp_sockaddr(c->local_sockaddr, c->local_socklen,
@@ -471,6 +471,8 @@ ngx_insert_udp_connection(ngx_connection_t *c)
     ngx_crc32_final(hash);
 
     udp->node.key = hash;
+    udp->key.data = (u_char *) c->sockaddr;
+    udp->key.len = c->socklen;
 
     cln = ngx_pool_cleanup_add(c->pool, 0);
     if (cln == NULL) {
index d7a64ee03dace46a708a6c73dbe64dfd05b97a05..e5ddf1b31783d98c4443b8bfea0aa7a2b924240c 100644 (file)
@@ -27,6 +27,7 @@ struct ngx_udp_connection_s {
     ngx_rbtree_node_t   node;
     ngx_connection_t   *connection;
     ngx_buf_t          *buffer;
+    ngx_str_t           key;
 };
 
 
index 0c68d68f0da09411f8981b17e8e4b6c434fd7509..ca15200b43ff64f24fe637cc57c6616759815e9d 100644 (file)
@@ -111,8 +111,6 @@ struct ngx_quic_stream_s {
 
 
 void ngx_quic_recvmsg(ngx_event_t *ev);
-void ngx_quic_rbtree_insert_value(ngx_rbtree_node_t *temp,
-    ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
 void ngx_quic_run(ngx_connection_t *c, ngx_quic_conf_t *conf);
 ngx_connection_t *ngx_quic_open_stream(ngx_connection_t *c, ngx_uint_t bidi);
 void ngx_quic_finalize_connection(ngx_connection_t *c, ngx_uint_t err,
index 6813fcd0a0adfa578abfa0a33bde4b398b2efabd..6652523b7124444ea450965066802c739b75b471 100644 (file)
@@ -179,6 +179,7 @@ ngx_quic_listen(ngx_connection_t *c, ngx_quic_connection_t *qc,
 
     qsock->udp.connection = c;
     qsock->udp.node.key = ngx_crc32_long(id.data, id.len);
+    qsock->udp.key = id;
 
     ngx_rbtree_insert(&c->listening->rbtree, &qsock->udp.node);
 
index 0721ca8b663b920fb9a78f43e5ddc1655566c284..71754a48b2e9d0a87660ea09204cb8dbe2b14f3b 100644 (file)
@@ -365,59 +365,6 @@ ngx_quic_close_accepted_connection(ngx_connection_t *c)
 }
 
 
-void
-ngx_quic_rbtree_insert_value(ngx_rbtree_node_t *temp,
-    ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
-{
-    ngx_int_t            rc;
-    ngx_connection_t    *c, *ct;
-    ngx_rbtree_node_t  **p;
-    ngx_quic_socket_t   *qsock, *qsockt;
-
-    for ( ;; ) {
-
-        if (node->key < temp->key) {
-
-            p = &temp->left;
-
-        } else if (node->key > temp->key) {
-
-            p = &temp->right;
-
-        } else { /* node->key == temp->key */
-
-            qsock = (ngx_quic_socket_t *) node;
-            c = qsock->udp.connection;
-
-            qsockt = (ngx_quic_socket_t *) temp;
-            ct = qsockt->udp.connection;
-
-            rc = ngx_memn2cmp(qsock->sid.id, qsockt->sid.id,
-                              qsock->sid.len, qsockt->sid.len);
-
-            if (rc == 0 && c->listening->wildcard) {
-                rc = ngx_cmp_sockaddr(c->local_sockaddr, c->local_socklen,
-                                      ct->local_sockaddr, ct->local_socklen, 1);
-            }
-
-            p = (rc < 0) ? &temp->left : &temp->right;
-        }
-
-        if (*p == sentinel) {
-            break;
-        }
-
-        temp = *p;
-    }
-
-    *p = node;
-    node->parent = temp;
-    node->left = sentinel;
-    node->right = sentinel;
-    ngx_rbt_red(node);
-}
-
-
 static ngx_connection_t *
 ngx_quic_lookup_connection(ngx_listening_t *ls, ngx_str_t *key,
     struct sockaddr *local_sockaddr, socklen_t local_socklen)
index 34bfc539e40dcb6a77f40852548ee63410b80542..d835f896e94852094798bda0ba7b298e97c6e306 100644 (file)
@@ -1883,14 +1883,7 @@ ngx_http_add_listening(ngx_conf_t *cf, ngx_http_conf_addr_t *addr)
     ls->wildcard = addr->opt.wildcard;
 
 #if (NGX_HTTP_V3)
-
     ls->quic = addr->opt.quic;
-
-    if (ls->quic) {
-        ngx_rbtree_init(&ls->rbtree, &ls->sentinel,
-                        ngx_quic_rbtree_insert_value);
-    }
-
 #endif
 
     return ls;
index 2762786e265b53321b450c6c561576cd305d8309..3304c843cef91a3da02225586d5c0168470fd89e 100644 (file)
@@ -518,11 +518,6 @@ ngx_stream_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports)
             ls->reuseport = addr[i].opt.reuseport;
 #endif
 
-#if !(NGX_WIN32)
-            ngx_rbtree_init(&ls->rbtree, &ls->sentinel,
-                            ngx_udp_rbtree_insert_value);
-#endif
-
             stport = ngx_palloc(cf->pool, sizeof(ngx_stream_port_t));
             if (stport == NULL) {
                 return NGX_CONF_ERROR;