[PATCH 2 of 6] QUIC: handle datagrams directly in ngx_quic_recvmsg()

Roman Arutyunyan arut at nginx.com
Fri Dec 9 09:38:48 UTC 2022


# HG changeset patch
# User Roman Arutyunyan <arut at nginx.com>
# Date 1670428292 0
#      Wed Dec 07 15:51:32 2022 +0000
# Branch quic
# Node ID 8a7f2c71db202141d169f3ab292027bfc16ff8ec
# Parent  1038d7300c29eea02b47eac3f205e293b1e55f5b
QUIC: handle datagrams directly in ngx_quic_recvmsg().

Previously, ngx_quic_recvmsg() called client connection's read event handler
to emulate normal event processing.  Further, the read event handler handled
the datagram by calling ngx_quic_handle_datagram().

Now ngx_quic_handle_datagram() is called directly from ngx_quic_recvmsg(),
which simplifies the code.

diff --git a/src/event/quic/ngx_event_quic.c b/src/event/quic/ngx_event_quic.c
--- a/src/event/quic/ngx_event_quic.c
+++ b/src/event/quic/ngx_event_quic.c
@@ -17,8 +17,6 @@ static ngx_int_t ngx_quic_handle_statele
 static void ngx_quic_input_handler(ngx_event_t *rev);
 static void ngx_quic_close_handler(ngx_event_t *ev);
 
-static ngx_int_t ngx_quic_handle_datagram(ngx_connection_t *c, ngx_buf_t *b,
-    ngx_quic_conf_t *conf);
 static ngx_int_t ngx_quic_handle_packet(ngx_connection_t *c,
     ngx_quic_conf_t *conf, ngx_quic_header_t *pkt);
 static ngx_int_t ngx_quic_handle_payload(ngx_connection_t *c,
@@ -397,8 +395,6 @@ ngx_quic_handle_stateless_reset(ngx_conn
 static void
 ngx_quic_input_handler(ngx_event_t *rev)
 {
-    ngx_int_t               rc;
-    ngx_buf_t              *b;
     ngx_connection_t       *c;
     ngx_quic_connection_t  *qc;
 
@@ -432,29 +428,6 @@ ngx_quic_input_handler(ngx_event_t *rev)
 
         return;
     }
-
-    b = c->udp->buffer;
-    if (b == NULL) {
-        return;
-    }
-
-    rc = ngx_quic_handle_datagram(c, b, NULL);
-
-    if (rc == NGX_ERROR) {
-        ngx_quic_close_connection(c, NGX_ERROR);
-        return;
-    }
-
-    if (rc == NGX_DONE) {
-        return;
-    }
-
-    /* rc == NGX_OK */
-
-    qc->send_timer_set = 0;
-    ngx_add_timer(rev, qc->tp.max_idle_timeout);
-
-    ngx_quic_connstate_dbg(c);
 }
 
 
@@ -654,7 +627,7 @@ ngx_quic_close_handler(ngx_event_t *ev)
 }
 
 
-static ngx_int_t
+ngx_int_t
 ngx_quic_handle_datagram(ngx_connection_t *c, ngx_buf_t *b,
     ngx_quic_conf_t *conf)
 {
@@ -753,6 +726,11 @@ ngx_quic_handle_datagram(ngx_connection_
             qc->error_reason = "QUIC flood detected";
             return NGX_ERROR;
         }
+
+        qc->send_timer_set = 0;
+        ngx_add_timer(c->read, qc->tp.max_idle_timeout);
+
+        ngx_quic_connstate_dbg(c);
     }
 
     return NGX_OK;
diff --git a/src/event/quic/ngx_event_quic_connection.h b/src/event/quic/ngx_event_quic_connection.h
--- a/src/event/quic/ngx_event_quic_connection.h
+++ b/src/event/quic/ngx_event_quic_connection.h
@@ -260,6 +260,8 @@ struct ngx_quic_connection_s {
 };
 
 
+ngx_int_t ngx_quic_handle_datagram(ngx_connection_t *c, ngx_buf_t *b,
+    ngx_quic_conf_t *conf);
 ngx_int_t ngx_quic_apply_transport_params(ngx_connection_t *c,
     ngx_quic_tp_t *ctp);
 void ngx_quic_discard_ctx(ngx_connection_t *c,
diff --git a/src/event/quic/ngx_event_quic_migration.c b/src/event/quic/ngx_event_quic_migration.c
--- a/src/event/quic/ngx_event_quic_migration.c
+++ b/src/event/quic/ngx_event_quic_migration.c
@@ -264,12 +264,6 @@ ngx_quic_set_path(ngx_connection_t *c, n
 
     len = pkt->raw->last - pkt->raw->start;
 
-    if (c->udp->buffer == NULL) {
-        /* first ever packet in connection, path already exists  */
-        path = qc->path;
-        goto update;
-    }
-
     probe = NULL;
 
     for (q = ngx_queue_head(&qc->paths);
diff --git a/src/event/quic/ngx_event_quic_socket.c b/src/event/quic/ngx_event_quic_socket.c
--- a/src/event/quic/ngx_event_quic_socket.c
+++ b/src/event/quic/ngx_event_quic_socket.c
@@ -61,6 +61,9 @@ ngx_quic_open_sockets(ngx_connection_t *
     }
     ngx_memcpy(qc->tp.initial_scid.data, qsock->sid.id, qsock->sid.len);
 
+    ngx_memcpy(&qsock->sockaddr.sockaddr, c->sockaddr, c->socklen);
+    qsock->socklen = c->socklen;
+
     /* for all packets except first, this is set at udp layer */
     c->udp = &qsock->udp;
 
diff --git a/src/event/quic/ngx_event_quic_udp.c b/src/event/quic/ngx_event_quic_udp.c
--- a/src/event/quic/ngx_event_quic_udp.c
+++ b/src/event/quic/ngx_event_quic_udp.c
@@ -186,21 +186,11 @@ ngx_quic_recvmsg(ngx_event_t *ev)
             ngx_memcpy(&qsock->sockaddr.sockaddr, sockaddr, socklen);
             qsock->socklen = socklen;
 
-            c->udp->buffer = &buf;
-
-            rev = c->read;
-            rev->ready = 1;
-            rev->active = 0;
-
-            rev->handler(rev);
-
-            if (c->udp) {
-                c->udp->buffer = NULL;
+            if (ngx_quic_handle_datagram(c, &buf, NULL) == NGX_ERROR) {
+                ngx_quic_close_connection(c, NGX_ERROR);
+                return;
             }
 
-            rev->ready = 0;
-            rev->active = 1;
-
             goto next;
         }
 



More information about the nginx-devel mailing list