comparison src/event/ngx_event_quic.c @ 7746: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
comparison
equal deleted inserted replaced
7745:5f223cdad40e 7746:b364af7f9f3f
15 NGX_QUIC_ST_APPLICATION /* handshake complete */ 15 NGX_QUIC_ST_APPLICATION /* handshake complete */
16 } ngx_quic_state_t; 16 } ngx_quic_state_t;
17 17
18 18
19 #define NGX_QUIC_STREAM_BUFSIZE 16384 19 #define NGX_QUIC_STREAM_BUFSIZE 16384
20
21
22 typedef struct {
23 ngx_rbtree_node_t node;
24 ngx_buf_t *b;
25 ngx_connection_t *c;
26 ngx_quic_stream_t s;
27 } ngx_quic_stream_node_t;
28 20
29 21
30 typedef struct { 22 typedef struct {
31 ngx_rbtree_t tree; 23 ngx_rbtree_t tree;
32 ngx_rbtree_node_t sentinel; 24 ngx_rbtree_node_t sentinel;
124 ngx_str_t *payload); 116 ngx_str_t *payload);
125 117
126 118
127 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp, 119 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
128 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); 120 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
129 static ngx_quic_stream_node_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree, 121 static ngx_quic_stream_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree,
130 ngx_uint_t key); 122 ngx_uint_t key);
131 static ngx_quic_stream_node_t *ngx_quic_create_stream(ngx_connection_t *c, 123 static ngx_quic_stream_t *ngx_quic_create_stream(ngx_connection_t *c,
132 ngx_uint_t id); 124 ngx_uint_t id);
133 static ssize_t ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, 125 static ssize_t ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf,
134 size_t size); 126 size_t size);
135 static ssize_t ngx_quic_stream_send(ngx_connection_t *c, u_char *buf, 127 static ssize_t ngx_quic_stream_send(ngx_connection_t *c, u_char *buf,
136 size_t size); 128 size_t size);
1049 1041
1050 static ngx_int_t 1042 static ngx_int_t
1051 ngx_quic_handle_stream_frame(ngx_connection_t *c, 1043 ngx_quic_handle_stream_frame(ngx_connection_t *c,
1052 ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *f) 1044 ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *f)
1053 { 1045 {
1054 ngx_buf_t *b; 1046 ngx_buf_t *b;
1055 ngx_event_t *rev; 1047 ngx_event_t *rev;
1056 ngx_quic_connection_t *qc; 1048 ngx_quic_stream_t *sn;
1057 ngx_quic_stream_node_t *sn; 1049 ngx_quic_connection_t *qc;
1058 1050
1059 qc = c->quic; 1051 qc = c->quic;
1060 1052
1061 sn = ngx_quic_find_stream(&qc->streams.tree, f->stream_id); 1053 sn = ngx_quic_find_stream(&qc->streams.tree, f->stream_id);
1062 1054
1137 1129
1138 static ngx_int_t 1130 static ngx_int_t
1139 ngx_quic_handle_stream_data_blocked_frame(ngx_connection_t *c, 1131 ngx_quic_handle_stream_data_blocked_frame(ngx_connection_t *c,
1140 ngx_quic_header_t *pkt, ngx_quic_stream_data_blocked_frame_t *f) 1132 ngx_quic_header_t *pkt, ngx_quic_stream_data_blocked_frame_t *f)
1141 { 1133 {
1142 size_t n; 1134 size_t n;
1143 ngx_buf_t *b; 1135 ngx_buf_t *b;
1144 ngx_quic_frame_t *frame; 1136 ngx_quic_frame_t *frame;
1145 ngx_quic_connection_t *qc; 1137 ngx_quic_stream_t *sn;
1146 ngx_quic_stream_node_t *sn; 1138 ngx_quic_connection_t *qc;
1147 1139
1148 qc = c->quic; 1140 qc = c->quic;
1149 sn = ngx_quic_find_stream(&qc->streams.tree, f->id); 1141 sn = ngx_quic_find_stream(&qc->streams.tree, f->id);
1150 1142
1151 if (sn == NULL) { 1143 if (sn == NULL) {
1355 1347
1356 1348
1357 ngx_connection_t * 1349 ngx_connection_t *
1358 ngx_quic_create_uni_stream(ngx_connection_t *c) 1350 ngx_quic_create_uni_stream(ngx_connection_t *c)
1359 { 1351 {
1360 ngx_uint_t id; 1352 ngx_uint_t id;
1361 ngx_quic_stream_t *qs; 1353 ngx_quic_stream_t *qs, *sn;
1362 ngx_quic_connection_t *qc; 1354 ngx_quic_connection_t *qc;
1363 ngx_quic_stream_node_t *sn;
1364 1355
1365 qs = c->qs; 1356 qs = c->qs;
1366 qc = qs->parent->quic; 1357 qc = qs->parent->quic;
1367 1358
1368 /* 1359 id = (qc->streams.id_counter << 2)
1369 * A stream ID is a 62-bit integer that is unique for all streams 1360 | NGX_QUIC_STREAM_SERVER_INITIATED
1370 * on a connection. 1361 | NGX_QUIC_STREAM_UNIDIRECTIONAL;
1371 *
1372 * 0x3 | Server-Initiated, Unidirectional
1373 */
1374 id = (qc->streams.id_counter << 2) | 0x3;
1375 1362
1376 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, 1363 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1377 "creating server uni stream #%ui id %ui", 1364 "creating server uni stream #%ui id %ui",
1378 qc->streams.id_counter, id); 1365 qc->streams.id_counter, id);
1379 1366
1390 1377
1391 static void 1378 static void
1392 ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp, 1379 ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
1393 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) 1380 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1394 { 1381 {
1395 ngx_rbtree_node_t **p; 1382 ngx_rbtree_node_t **p;
1396 ngx_quic_stream_node_t *qn, *qnt; 1383 ngx_quic_stream_t *qn, *qnt;
1397 1384
1398 for ( ;; ) { 1385 for ( ;; ) {
1399 1386
1400 if (node->key < temp->key) { 1387 if (node->key < temp->key) {
1401 1388
1405 1392
1406 p = &temp->right; 1393 p = &temp->right;
1407 1394
1408 } else { /* node->key == temp->key */ 1395 } else { /* node->key == temp->key */
1409 1396
1410 qn = (ngx_quic_stream_node_t *) &node->color; 1397 qn = (ngx_quic_stream_t *) &node->color;
1411 qnt = (ngx_quic_stream_node_t *) &temp->color; 1398 qnt = (ngx_quic_stream_t *) &temp->color;
1412 1399
1413 if (qn->c < qnt->c) { 1400 if (qn->c < qnt->c) {
1414 p = &temp->left; 1401 p = &temp->left;
1415 } else { 1402 } else {
1416 p = &temp->right; 1403 p = &temp->right;
1430 node->right = sentinel; 1417 node->right = sentinel;
1431 ngx_rbt_red(node); 1418 ngx_rbt_red(node);
1432 } 1419 }
1433 1420
1434 1421
1435 static ngx_quic_stream_node_t * 1422 static ngx_quic_stream_t *
1436 ngx_quic_find_stream(ngx_rbtree_t *rbtree, ngx_uint_t key) 1423 ngx_quic_find_stream(ngx_rbtree_t *rbtree, ngx_uint_t key)
1437 { 1424 {
1438 ngx_rbtree_node_t *node, *sentinel; 1425 ngx_rbtree_node_t *node, *sentinel;
1439 1426
1440 node = rbtree->root; 1427 node = rbtree->root;
1441 sentinel = rbtree->sentinel; 1428 sentinel = rbtree->sentinel;
1442 1429
1443 while (node != sentinel) { 1430 while (node != sentinel) {
1444 1431
1445 if (key == node->key) { 1432 if (key == node->key) {
1446 return (ngx_quic_stream_node_t *) node; 1433 return (ngx_quic_stream_t *) node;
1447 } 1434 }
1448 1435
1449 node = (key < node->key) ? node->left : node->right; 1436 node = (key < node->key) ? node->left : node->right;
1450 } 1437 }
1451 1438
1452 return NULL; 1439 return NULL;
1453 } 1440 }
1454 1441
1455 1442
1456 static ngx_quic_stream_node_t * 1443 static ngx_quic_stream_t *
1457 ngx_quic_create_stream(ngx_connection_t *c, ngx_uint_t id) 1444 ngx_quic_create_stream(ngx_connection_t *c, ngx_uint_t id)
1458 { 1445 {
1459 size_t n; 1446 size_t n;
1460 ngx_log_t *log; 1447 ngx_log_t *log;
1461 ngx_pool_t *pool; 1448 ngx_pool_t *pool;
1462 ngx_event_t *rev, *wev; 1449 ngx_event_t *rev, *wev;
1463 ngx_pool_cleanup_t *cln; 1450 ngx_quic_stream_t *sn;
1464 ngx_quic_connection_t *qc; 1451 ngx_pool_cleanup_t *cln;
1465 ngx_quic_stream_node_t *sn; 1452 ngx_quic_connection_t *qc;
1466 1453
1467 qc = c->quic; 1454 qc = c->quic;
1468 1455
1469 sn = ngx_pcalloc(c->pool, sizeof(ngx_quic_stream_node_t)); 1456 sn = ngx_pcalloc(c->pool, sizeof(ngx_quic_stream_t));
1470 if (sn == NULL) { 1457 if (sn == NULL) {
1471 return NULL; 1458 return NULL;
1472 } 1459 }
1473 1460
1474 sn->c = ngx_get_connection(-1, c->log); // TODO: free on connection termination 1461 sn->c = ngx_get_connection(-1, c->log); // TODO: free on connection termination
1520 return NULL; 1507 return NULL;
1521 } 1508 }
1522 1509
1523 ngx_rbtree_insert(&qc->streams.tree, &sn->node); 1510 ngx_rbtree_insert(&qc->streams.tree, &sn->node);
1524 1511
1525 sn->s.id = id; 1512 sn->id = id;
1526 sn->s.unidirectional = (sn->s.id & 0x02) ? 1 : 0; 1513 sn->parent = c;
1527 sn->s.parent = c; 1514 sn->c->qs = sn;
1528 sn->c->qs = &sn->s;
1529 1515
1530 sn->c->recv = ngx_quic_stream_recv; 1516 sn->c->recv = ngx_quic_stream_recv;
1531 sn->c->send = ngx_quic_stream_send; 1517 sn->c->send = ngx_quic_stream_send;
1532 sn->c->send_chain = ngx_quic_stream_send_chain; 1518 sn->c->send_chain = ngx_quic_stream_send_chain;
1533 1519
1546 1532
1547 1533
1548 static ssize_t 1534 static ssize_t
1549 ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, size_t size) 1535 ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, size_t size)
1550 { 1536 {
1551 ssize_t len; 1537 ssize_t len;
1552 ngx_buf_t *b; 1538 ngx_buf_t *b;
1553 ngx_event_t *rev; 1539 ngx_event_t *rev;
1554 ngx_quic_stream_t *qs; 1540 ngx_quic_stream_t *qs;
1555 ngx_quic_connection_t *qc;
1556 ngx_quic_stream_node_t *sn;
1557 1541
1558 qs = c->qs; 1542 qs = c->qs;
1559 qc = qs->parent->quic; 1543 b = qs->b;
1560
1561 // XXX: get direct pointer from stream structure?
1562 sn = ngx_quic_find_stream(&qc->streams.tree, qs->id);
1563
1564 if (sn == NULL) {
1565 return NGX_ERROR;
1566 }
1567
1568 rev = c->read; 1544 rev = c->read;
1569
1570 b = sn->b;
1571 1545
1572 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, 1546 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1573 "quic recv: eof:%d, avail:%z", 1547 "quic recv: eof:%d, avail:%z",
1574 rev->pending_eof, b->last - b->pos); 1548 rev->pending_eof, b->last - b->pos);
1575 1549
1605 1579
1606 1580
1607 static ssize_t 1581 static ssize_t
1608 ngx_quic_stream_send(ngx_connection_t *c, u_char *buf, size_t size) 1582 ngx_quic_stream_send(ngx_connection_t *c, u_char *buf, size_t size)
1609 { 1583 {
1610 u_char *p; 1584 u_char *p;
1611 ngx_connection_t *pc; 1585 ngx_connection_t *pc;
1612 ngx_quic_frame_t *frame; 1586 ngx_quic_frame_t *frame;
1613 ngx_quic_stream_t *qs; 1587 ngx_quic_stream_t *qs;
1614 ngx_quic_connection_t *qc; 1588 ngx_quic_connection_t *qc;
1615 ngx_quic_stream_node_t *sn;
1616 1589
1617 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send: %uz", size); 1590 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send: %uz", size);
1618 1591
1619 qs = c->qs; 1592 qs = c->qs;
1620 pc = qs->parent; 1593 pc = qs->parent;
1621 qc = pc->quic; 1594 qc = pc->quic;
1622
1623 // XXX: get direct pointer from stream structure?
1624 sn = ngx_quic_find_stream(&qc->streams.tree, qs->id);
1625
1626 if (sn == NULL) {
1627 return NGX_ERROR;
1628 }
1629 1595
1630 frame = ngx_pcalloc(pc->pool, sizeof(ngx_quic_frame_t)); 1596 frame = ngx_pcalloc(pc->pool, sizeof(ngx_quic_frame_t));
1631 if (frame == NULL) { 1597 if (frame == NULL) {
1632 return 0; 1598 return 0;
1633 } 1599 }
1665 static void 1631 static void
1666 ngx_quic_stream_cleanup_handler(void *data) 1632 ngx_quic_stream_cleanup_handler(void *data)
1667 { 1633 {
1668 ngx_connection_t *c = data; 1634 ngx_connection_t *c = data;
1669 1635
1670 ngx_connection_t *pc; 1636 ngx_connection_t *pc;
1671 ngx_quic_frame_t *frame; 1637 ngx_quic_frame_t *frame;
1672 ngx_quic_stream_t *qs; 1638 ngx_quic_stream_t *qs;
1673 ngx_quic_connection_t *qc; 1639 ngx_quic_connection_t *qc;
1674 ngx_quic_stream_node_t *sn;
1675
1676 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send fin");
1677 1640
1678 qs = c->qs; 1641 qs = c->qs;
1679 pc = qs->parent; 1642 pc = qs->parent;
1680 qc = pc->quic; 1643 qc = pc->quic;
1681 1644
1682 if ((qs->id & 0x03) == 0x02) { 1645 if ((qs->id & 0x03) == NGX_QUIC_STREAM_UNIDIRECTIONAL) {
1683 /* do not send fin for client unidirectional streams */ 1646 /* do not send fin for client unidirectional streams */
1684 return; 1647 return;
1685 } 1648 }
1686 1649
1687 // XXX: get direct pointer from stream structure? 1650 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "quic send fin");
1688 sn = ngx_quic_find_stream(&qc->streams.tree, qs->id);
1689
1690 if (sn == NULL) {
1691 return;
1692 }
1693 1651
1694 frame = ngx_pcalloc(pc->pool, sizeof(ngx_quic_frame_t)); 1652 frame = ngx_pcalloc(pc->pool, sizeof(ngx_quic_frame_t));
1695 if (frame == NULL) { 1653 if (frame == NULL) {
1696 return; 1654 return;
1697 } 1655 }