changeset 8280:b364af7f9f3f quic

Removed ngx_quic_stream_node_t. Now ngx_quic_stream_t is directly inserted into the tree.
author Roman Arutyunyan <arut@nginx.com>
date Tue, 24 Mar 2020 16:38:03 +0300
parents 5f223cdad40e
children 618a65de08b3
files src/event/ngx_event_quic.c src/event/ngx_event_quic.h src/http/ngx_http_request.c
diffstat 3 files changed, 59 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/src/event/ngx_event_quic.c
+++ b/src/event/ngx_event_quic.c
@@ -20,14 +20,6 @@ typedef enum {
 
 
 typedef struct {
-    ngx_rbtree_node_t                  node;
-    ngx_buf_t                         *b;
-    ngx_connection_t                  *c;
-    ngx_quic_stream_t                  s;
-} ngx_quic_stream_node_t;
-
-
-typedef struct {
     ngx_rbtree_t                      tree;
     ngx_rbtree_node_t                 sentinel;
     ngx_connection_handler_pt         handler;
@@ -126,9 +118,9 @@ static ngx_int_t ngx_quic_send_packet(ng
 
 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
-static ngx_quic_stream_node_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree,
+static ngx_quic_stream_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree,
     ngx_uint_t key);
-static ngx_quic_stream_node_t *ngx_quic_create_stream(ngx_connection_t *c,
+static ngx_quic_stream_t *ngx_quic_create_stream(ngx_connection_t *c,
     ngx_uint_t id);
 static ssize_t ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf,
     size_t size);
@@ -1051,10 +1043,10 @@ static ngx_int_t
 ngx_quic_handle_stream_frame(ngx_connection_t *c,
     ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *f)
 {
-    ngx_buf_t               *b;
-    ngx_event_t             *rev;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
+    ngx_buf_t              *b;
+    ngx_event_t            *rev;
+    ngx_quic_stream_t      *sn;
+    ngx_quic_connection_t  *qc;
 
     qc = c->quic;
 
@@ -1139,11 +1131,11 @@ static ngx_int_t
 ngx_quic_handle_stream_data_blocked_frame(ngx_connection_t *c,
     ngx_quic_header_t *pkt, ngx_quic_stream_data_blocked_frame_t *f)
 {
-    size_t                   n;
-    ngx_buf_t               *b;
-    ngx_quic_frame_t        *frame;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
+    size_t                  n;
+    ngx_buf_t              *b;
+    ngx_quic_frame_t       *frame;
+    ngx_quic_stream_t      *sn;
+    ngx_quic_connection_t  *qc;
 
     qc = c->quic;
     sn = ngx_quic_find_stream(&qc->streams.tree, f->id);
@@ -1357,21 +1349,16 @@ ngx_quic_send_packet(ngx_connection_t *c
 ngx_connection_t *
 ngx_quic_create_uni_stream(ngx_connection_t *c)
 {
-    ngx_uint_t               id;
-    ngx_quic_stream_t       *qs;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
+    ngx_uint_t              id;
+    ngx_quic_stream_t      *qs, *sn;
+    ngx_quic_connection_t  *qc;
 
     qs = c->qs;
     qc = qs->parent->quic;
 
-    /*
-     * A stream ID is a 62-bit integer that is unique for all streams
-     * on a connection.
-     *
-     * 0x3  | Server-Initiated, Unidirectional
-     */
-    id = (qc->streams.id_counter << 2) | 0x3;
+    id = (qc->streams.id_counter << 2)
+         | NGX_QUIC_STREAM_SERVER_INITIATED
+         | NGX_QUIC_STREAM_UNIDIRECTIONAL;
 
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
                    "creating server uni stream #%ui id %ui",
@@ -1392,8 +1379,8 @@ static void
 ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
 {
-    ngx_rbtree_node_t       **p;
-    ngx_quic_stream_node_t   *qn, *qnt;
+    ngx_rbtree_node_t  **p;
+    ngx_quic_stream_t   *qn, *qnt;
 
     for ( ;; ) {
 
@@ -1407,8 +1394,8 @@ ngx_quic_rbtree_insert_stream(ngx_rbtree
 
         } else { /* node->key == temp->key */
 
-            qn = (ngx_quic_stream_node_t *) &node->color;
-            qnt = (ngx_quic_stream_node_t *) &temp->color;
+            qn = (ngx_quic_stream_t *) &node->color;
+            qnt = (ngx_quic_stream_t *) &temp->color;
 
             if (qn->c < qnt->c) {
                 p = &temp->left;
@@ -1432,7 +1419,7 @@ ngx_quic_rbtree_insert_stream(ngx_rbtree
 }
 
 
-static ngx_quic_stream_node_t *
+static ngx_quic_stream_t *
 ngx_quic_find_stream(ngx_rbtree_t *rbtree, ngx_uint_t key)
 {
     ngx_rbtree_node_t  *node, *sentinel;
@@ -1443,7 +1430,7 @@ ngx_quic_find_stream(ngx_rbtree_t *rbtre
     while (node != sentinel) {
 
         if (key == node->key) {
-            return (ngx_quic_stream_node_t *) node;
+            return (ngx_quic_stream_t *) node;
         }
 
         node = (key < node->key) ? node->left : node->right;
@@ -1453,20 +1440,20 @@ ngx_quic_find_stream(ngx_rbtree_t *rbtre
 }
 
 
-static ngx_quic_stream_node_t *
+static ngx_quic_stream_t *
 ngx_quic_create_stream(ngx_connection_t *c, ngx_uint_t id)
 {
-    size_t                   n;
-    ngx_log_t               *log;
-    ngx_pool_t              *pool;
-    ngx_event_t             *rev, *wev;
-    ngx_pool_cleanup_t      *cln;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
+    size_t                  n;
+    ngx_log_t              *log;
+    ngx_pool_t             *pool;
+    ngx_event_t            *rev, *wev;
+    ngx_quic_stream_t      *sn;
+    ngx_pool_cleanup_t     *cln;
+    ngx_quic_connection_t  *qc;
 
     qc = c->quic;
 
-    sn = ngx_pcalloc(c->pool, sizeof(ngx_quic_stream_node_t));
+    sn = ngx_pcalloc(c->pool, sizeof(ngx_quic_stream_t));
     if (sn == NULL) {
         return NULL;
     }
@@ -1522,10 +1509,9 @@ ngx_quic_create_stream(ngx_connection_t 
 
     ngx_rbtree_insert(&qc->streams.tree, &sn->node);
 
-    sn->s.id = id;
-    sn->s.unidirectional = (sn->s.id & 0x02) ? 1 : 0;
-    sn->s.parent = c;
-    sn->c->qs = &sn->s;
+    sn->id = id;
+    sn->parent = c;
+    sn->c->qs = sn;
 
     sn->c->recv = ngx_quic_stream_recv;
     sn->c->send = ngx_quic_stream_send;
@@ -1548,27 +1534,15 @@ ngx_quic_create_stream(ngx_connection_t 
 static ssize_t
 ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, size_t size)
 {
-    ssize_t                  len;
-    ngx_buf_t               *b;
-    ngx_event_t             *rev;
-    ngx_quic_stream_t       *qs;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
+    ssize_t             len;
+    ngx_buf_t          *b;
+    ngx_event_t        *rev;
+    ngx_quic_stream_t  *qs;
 
     qs = c->qs;
-    qc = qs->parent->quic;
-
-    // XXX: get direct pointer from stream structure?
-    sn = ngx_quic_find_stream(&qc->streams.tree, qs->id);
-
-    if (sn == NULL) {
-        return NGX_ERROR;
-    }
-
+    b = qs->b;
     rev = c->read;
 
-    b = sn->b;
-
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
                    "quic recv: eof:%d, avail:%z",
                    rev->pending_eof, b->last - b->pos);
@@ -1607,12 +1581,11 @@ ngx_quic_stream_recv(ngx_connection_t *c
 static ssize_t
 ngx_quic_stream_send(ngx_connection_t *c, u_char *buf, size_t size)
 {
-    u_char                  *p;
-    ngx_connection_t        *pc;
-    ngx_quic_frame_t        *frame;
-    ngx_quic_stream_t       *qs;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
+    u_char                 *p;
+    ngx_connection_t       *pc;
+    ngx_quic_frame_t       *frame;
+    ngx_quic_stream_t      *qs;
+    ngx_quic_connection_t  *qc;
 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send: %uz", size);
 
@@ -1620,13 +1593,6 @@ ngx_quic_stream_send(ngx_connection_t *c
     pc = qs->parent;
     qc = pc->quic;
 
-    // XXX: get direct pointer from stream structure?
-    sn = ngx_quic_find_stream(&qc->streams.tree, qs->id);
-
-    if (sn == NULL) {
-        return NGX_ERROR;
-    }
-
     frame = ngx_pcalloc(pc->pool, sizeof(ngx_quic_frame_t));
     if (frame == NULL) {
         return 0;
@@ -1667,29 +1633,21 @@ ngx_quic_stream_cleanup_handler(void *da
 {
     ngx_connection_t *c = data;
 
-    ngx_connection_t        *pc;
-    ngx_quic_frame_t        *frame;
-    ngx_quic_stream_t       *qs;
-    ngx_quic_connection_t   *qc;
-    ngx_quic_stream_node_t  *sn;
-
-    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send fin");
+    ngx_connection_t       *pc;
+    ngx_quic_frame_t       *frame;
+    ngx_quic_stream_t      *qs;
+    ngx_quic_connection_t  *qc;
 
     qs = c->qs;
     pc = qs->parent;
     qc = pc->quic;
 
-    if ((qs->id & 0x03) == 0x02) {
+    if ((qs->id & 0x03) == NGX_QUIC_STREAM_UNIDIRECTIONAL) {
         /* do not send fin for client unidirectional streams */
         return;
     }
 
-    // XXX: get direct pointer from stream structure?
-    sn = ngx_quic_find_stream(&qc->streams.tree, qs->id);
-
-    if (sn == NULL) {
-        return;
-    }
+    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send fin");
 
     frame = ngx_pcalloc(pc->pool, sizeof(ngx_quic_frame_t));
     if (frame == NULL) {
--- a/src/event/ngx_event_quic.h
+++ b/src/event/ngx_event_quic.h
@@ -21,6 +21,9 @@
 #define NGX_QUIC_DEFAULT_ACK_DELAY_EXPONENT  3
 #define NGX_QUIC_DEFAULT_MAX_ACK_DELAY       25
 
+#define NGX_QUIC_STREAM_SERVER_INITIATED     0x01
+#define NGX_QUIC_STREAM_UNIDIRECTIONAL       0x02
+
 
 typedef struct {
     /* configurable */
@@ -46,10 +49,11 @@ typedef struct {
 
 
 struct ngx_quic_stream_s {
+    ngx_rbtree_node_t   node;
+    ngx_connection_t   *parent;
+    ngx_connection_t   *c;
     uint64_t            id;
-    ngx_uint_t          unidirectional:1;
-    ngx_connection_t   *parent;
-    void               *data;
+    ngx_buf_t          *b;
 };
 
 
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -414,7 +414,7 @@ ngx_http_quic_stream_handler(ngx_connect
     pc = c->qs->parent;
     h3c = pc->data;
 
-    if (c->qs->unidirectional) {
+    if (c->qs->id & NGX_QUIC_STREAM_UNIDIRECTIONAL) {
         ngx_http_v3_handle_client_uni_stream(c);
         return;
     }