[PATCH 1 of 4] QUIC: handle datagrams directly in ngx_quic_recvmsg()

Roman Arutyunyan arut at nginx.com
Tue Jan 17 11:23:59 UTC 2023


# HG changeset patch
# User Roman Arutyunyan <arut at nginx.com>
# Date 1672752567 -14400
#      Tue Jan 03 17:29:27 2023 +0400
# Branch quic
# Node ID 7051cace147700a3e5456846dd4eadfdcadcd0c8
# Parent  6bb884dc72916dc675df65d02abee0c9cfabc916
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,
@@ -201,8 +199,7 @@ ngx_quic_apply_transport_params(ngx_conn
 void
 ngx_quic_run(ngx_connection_t *c, ngx_quic_conf_t *conf)
 {
-    ngx_int_t               rc;
-    ngx_quic_connection_t  *qc;
+    ngx_int_t  rc;
 
     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic run");
 
@@ -211,16 +208,6 @@ ngx_quic_run(ngx_connection_t *c, ngx_qu
         ngx_quic_close_connection(c, rc);
         return;
     }
-
-    /* quic connection is now created */
-    qc = ngx_quic_get_connection(c);
-
-    ngx_add_timer(c->read, qc->tp.max_idle_timeout);
-    ngx_quic_connstate_dbg(c);
-
-    c->read->handler = ngx_quic_input_handler;
-
-    return;
 }
 
 
@@ -340,6 +327,8 @@ ngx_quic_new_connection(ngx_connection_t
     c->idle = 1;
     ngx_reusable_connection(c, 1);
 
+    c->read->handler = ngx_quic_input_handler;
+
     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
                    "quic connection created");
 
@@ -397,8 +386,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 +419,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 +618,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 +717,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