comparison src/event/ngx_event_quic.c @ 8331:bda817d16cc2 quic

Rename types and variables used for packet number space. Quote: Conceptually, a packet number space is the context in which a packet can be processed and acknowledged. ngx_quic_namespace_t => ngx_quic_send_ctx_t qc->ns => qc->send_ctx ns->largest => send_ctx->largest_ack The ngx_quic_ns(level) macro now returns pointer, not just index: ngx_quic_get_send_ctx(c->quic, level) ngx_quic_retransmit_ns() => ngx_quic_retransmit() ngx_quic_output_ns() => ngx_quic_output_frames()
author Vladimir Homutov <vl@nginx.com>
date Tue, 14 Apr 2020 12:06:32 +0300
parents e76be8639621
children 6ad871b63422
comparison
equal deleted inserted replaced
8330:5b7ec588de48 8331:bda817d16cc2
14 * 14 *
15 * 0 - Initial 15 * 0 - Initial
16 * 1 - Handshake 16 * 1 - Handshake
17 * 2 - 0-RTT and 1-RTT 17 * 2 - 0-RTT and 1-RTT
18 */ 18 */
19 #define ngx_quic_ns(level) \ 19 #define ngx_quic_get_send_ctx(qc, level) \
20 ((level) == ssl_encryption_initial) ? 0 \ 20 ((level) == ssl_encryption_initial) ? &((qc)->send_ctx[0]) \
21 : (((level) == ssl_encryption_handshake) ? 1 : 2) 21 : (((level) == ssl_encryption_handshake) ? &((qc)->send_ctx[1]) \
22 22 : &((qc)->send_ctx[2]))
23 #define NGX_QUIC_NAMESPACE_LAST (NGX_QUIC_ENCRYPTION_LAST - 1) 23
24 #define NGX_QUIC_SEND_CTX_LAST (NGX_QUIC_ENCRYPTION_LAST - 1)
24 25
25 #define NGX_QUIC_STREAMS_INC 16 26 #define NGX_QUIC_STREAMS_INC 16
26 #define NGX_QUIC_STREAMS_LIMIT (1ULL < 60) 27 #define NGX_QUIC_STREAMS_LIMIT (1ULL < 60)
27 28
28 29
41 42
42 ngx_uint_t id_counter; 43 ngx_uint_t id_counter;
43 } ngx_quic_streams_t; 44 } ngx_quic_streams_t;
44 45
45 46
47 /*
48 * 12.3. Packet Numbers
49 *
50 * Conceptually, a packet number space is the context in which a packet
51 * can be processed and acknowledged. Initial packets can only be sent
52 * with Initial packet protection keys and acknowledged in packets which
53 * are also Initial packets.
54 */
46 typedef struct { 55 typedef struct {
47 ngx_quic_secret_t client_secret; 56 ngx_quic_secret_t client_secret;
48 ngx_quic_secret_t server_secret; 57 ngx_quic_secret_t server_secret;
49 58
50 uint64_t pnum; 59 uint64_t pnum;
51 uint64_t largest; /* number received from peer */ 60 uint64_t largest_ack; /* number received from peer */
52 61
53 ngx_queue_t frames; 62 ngx_queue_t frames;
54 ngx_queue_t sent; 63 ngx_queue_t sent;
55 } ngx_quic_namespace_t; 64 } ngx_quic_send_ctx_t;
56 65
57 66
58 struct ngx_quic_connection_s { 67 struct ngx_quic_connection_s {
59 ngx_str_t scid; 68 ngx_str_t scid;
60 ngx_str_t dcid; 69 ngx_str_t dcid;
64 ngx_quic_tp_t tp; 73 ngx_quic_tp_t tp;
65 ngx_quic_tp_t ctp; 74 ngx_quic_tp_t ctp;
66 75
67 ngx_quic_state_t state; 76 ngx_quic_state_t state;
68 77
69 ngx_quic_namespace_t ns[NGX_QUIC_NAMESPACE_LAST]; 78 ngx_quic_send_ctx_t send_ctx[NGX_QUIC_SEND_CTX_LAST];
70 ngx_quic_secrets_t keys[NGX_QUIC_ENCRYPTION_LAST]; 79 ngx_quic_secrets_t keys[NGX_QUIC_ENCRYPTION_LAST];
71 ngx_quic_secrets_t next_key; 80 ngx_quic_secrets_t next_key;
72 uint64_t crypto_offset_out[NGX_QUIC_ENCRYPTION_LAST]; 81 uint64_t crypto_offset_out[NGX_QUIC_ENCRYPTION_LAST];
73 uint64_t crypto_offset_in[NGX_QUIC_ENCRYPTION_LAST]; 82 uint64_t crypto_offset_in[NGX_QUIC_ENCRYPTION_LAST];
74 83
134 ngx_quic_header_t *pkt); 143 ngx_quic_header_t *pkt);
135 144
136 static ngx_int_t ngx_quic_handle_ack_frame(ngx_connection_t *c, 145 static ngx_int_t ngx_quic_handle_ack_frame(ngx_connection_t *c,
137 ngx_quic_header_t *pkt, ngx_quic_ack_frame_t *f); 146 ngx_quic_header_t *pkt, ngx_quic_ack_frame_t *f);
138 static ngx_int_t ngx_quic_handle_ack_frame_range(ngx_connection_t *c, 147 static ngx_int_t ngx_quic_handle_ack_frame_range(ngx_connection_t *c,
139 ngx_quic_namespace_t *ns, uint64_t min, uint64_t max); 148 ngx_quic_send_ctx_t *ctx, uint64_t min, uint64_t max);
140 static ngx_int_t ngx_quic_handle_crypto_frame(ngx_connection_t *c, 149 static ngx_int_t ngx_quic_handle_crypto_frame(ngx_connection_t *c,
141 ngx_quic_header_t *pkt, ngx_quic_crypto_frame_t *frame); 150 ngx_quic_header_t *pkt, ngx_quic_crypto_frame_t *frame);
142 static ngx_int_t ngx_quic_handle_stream_frame(ngx_connection_t *c, 151 static ngx_int_t ngx_quic_handle_stream_frame(ngx_connection_t *c,
143 ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *frame); 152 ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *frame);
144 static ngx_int_t ngx_quic_handle_max_streams(ngx_connection_t *c); 153 static ngx_int_t ngx_quic_handle_max_streams(ngx_connection_t *c);
149 158
150 static void ngx_quic_queue_frame(ngx_quic_connection_t *qc, 159 static void ngx_quic_queue_frame(ngx_quic_connection_t *qc,
151 ngx_quic_frame_t *frame); 160 ngx_quic_frame_t *frame);
152 161
153 static ngx_int_t ngx_quic_output(ngx_connection_t *c); 162 static ngx_int_t ngx_quic_output(ngx_connection_t *c);
154 static ngx_int_t ngx_quic_output_ns(ngx_connection_t *c, 163 static ngx_int_t ngx_quic_output_frames(ngx_connection_t *c,
155 ngx_quic_namespace_t *ns, ngx_uint_t nsi); 164 ngx_quic_send_ctx_t *ctx, ngx_uint_t nsi);
156 static void ngx_quic_free_frames(ngx_connection_t *c, ngx_queue_t *frames); 165 static void ngx_quic_free_frames(ngx_connection_t *c, ngx_queue_t *frames);
157 static ngx_int_t ngx_quic_send_frames(ngx_connection_t *c, ngx_queue_t *frames); 166 static ngx_int_t ngx_quic_send_frames(ngx_connection_t *c, ngx_queue_t *frames);
158 167
159 static void ngx_quic_set_packet_number(ngx_quic_header_t *pkt, 168 static void ngx_quic_set_packet_number(ngx_quic_header_t *pkt,
160 ngx_quic_namespace_t *ns); 169 ngx_quic_send_ctx_t *ctx);
161 static void ngx_quic_retransmit_handler(ngx_event_t *ev); 170 static void ngx_quic_retransmit_handler(ngx_event_t *ev);
162 static ngx_int_t ngx_quic_retransmit_ns(ngx_connection_t *c, 171 static ngx_int_t ngx_quic_retransmit(ngx_connection_t *c,
163 ngx_quic_namespace_t *ns, ngx_msec_t *waitp); 172 ngx_quic_send_ctx_t *ctx, ngx_msec_t *waitp);
164 static void ngx_quic_push_handler(ngx_event_t *ev); 173 static void ngx_quic_push_handler(ngx_event_t *ev);
165 174
166 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp, 175 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
167 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); 176 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
168 static ngx_quic_stream_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree, 177 static ngx_quic_stream_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree,
468 477
469 ngx_rbtree_init(&qc->streams.tree, &qc->streams.sentinel, 478 ngx_rbtree_init(&qc->streams.tree, &qc->streams.sentinel,
470 ngx_quic_rbtree_insert_stream); 479 ngx_quic_rbtree_insert_stream);
471 480
472 for (i = 0; i < 3; i++) { 481 for (i = 0; i < 3; i++) {
473 ngx_queue_init(&qc->ns[i].frames); 482 ngx_queue_init(&qc->send_ctx[i].frames);
474 ngx_queue_init(&qc->ns[i].sent); 483 ngx_queue_init(&qc->send_ctx[i].sent);
475 } 484 }
476 485
477 ngx_queue_init(&qc->free_frames); 486 ngx_queue_init(&qc->free_frames);
478 487
479 qc->retry.log = c->log; 488 qc->retry.log = c->log;
1243 1252
1244 static ngx_int_t 1253 static ngx_int_t
1245 ngx_quic_handle_ack_frame(ngx_connection_t *c, ngx_quic_header_t *pkt, 1254 ngx_quic_handle_ack_frame(ngx_connection_t *c, ngx_quic_header_t *pkt,
1246 ngx_quic_ack_frame_t *ack) 1255 ngx_quic_ack_frame_t *ack)
1247 { 1256 {
1248 ssize_t n; 1257 ssize_t n;
1249 u_char *pos, *end; 1258 u_char *pos, *end;
1250 uint64_t gap, range; 1259 uint64_t gap, range;
1251 ngx_uint_t i, min, max; 1260 ngx_uint_t i, min, max;
1252 ngx_quic_namespace_t *ns; 1261 ngx_quic_send_ctx_t *ctx;
1253 1262
1254 ns = &c->quic->ns[ngx_quic_ns(pkt->level)]; 1263 ctx = ngx_quic_get_send_ctx(c->quic, pkt->level);
1255 1264
1256 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, 1265 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
1257 "ngx_quic_handle_ack_frame in namespace %d", 1266 "ngx_quic_handle_ack_frame level %d", pkt->level);
1258 ngx_quic_ns(pkt->level));
1259 1267
1260 /* 1268 /*
1261 * TODO: If any computed packet number is negative, an endpoint MUST 1269 * TODO: If any computed packet number is negative, an endpoint MUST
1262 * generate a connection error of type FRAME_ENCODING_ERROR. 1270 * generate a connection error of type FRAME_ENCODING_ERROR.
1263 * (19.3.1) 1271 * (19.3.1)
1270 } 1278 }
1271 1279
1272 min = ack->largest - ack->first_range; 1280 min = ack->largest - ack->first_range;
1273 max = ack->largest; 1281 max = ack->largest;
1274 1282
1275 if (ngx_quic_handle_ack_frame_range(c, ns, min, max) != NGX_OK) { 1283 if (ngx_quic_handle_ack_frame_range(c, ctx, min, max) != NGX_OK) {
1276 return NGX_ERROR; 1284 return NGX_ERROR;
1277 } 1285 }
1278 1286
1279 /* 13.2.3. Receiver Tracking of ACK Frames */ 1287 /* 13.2.3. Receiver Tracking of ACK Frames */
1280 if (ns->largest < max) { 1288 if (ctx->largest_ack < max) {
1281 ns->largest = max; 1289 ctx->largest_ack = max;
1282 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, 1290 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
1283 "updated largest received: %ui", max); 1291 "updated largest received: %ui", max);
1284 } 1292 }
1285 1293
1286 pos = ack->ranges_start; 1294 pos = ack->ranges_start;
1308 return NGX_ERROR; 1316 return NGX_ERROR;
1309 } 1317 }
1310 1318
1311 min = max - range + 1; 1319 min = max - range + 1;
1312 1320
1313 if (ngx_quic_handle_ack_frame_range(c, ns, min, max) != NGX_OK) { 1321 if (ngx_quic_handle_ack_frame_range(c, ctx, min, max) != NGX_OK) {
1314 return NGX_ERROR; 1322 return NGX_ERROR;
1315 } 1323 }
1316 } 1324 }
1317 1325
1318 return NGX_OK; 1326 return NGX_OK;
1319 } 1327 }
1320 1328
1321 1329
1322 static ngx_int_t 1330 static ngx_int_t
1323 ngx_quic_handle_ack_frame_range(ngx_connection_t *c, ngx_quic_namespace_t *ns, 1331 ngx_quic_handle_ack_frame_range(ngx_connection_t *c, ngx_quic_send_ctx_t *ctx,
1324 uint64_t min, uint64_t max) 1332 uint64_t min, uint64_t max)
1325 { 1333 {
1326 ngx_uint_t found; 1334 ngx_uint_t found;
1327 ngx_queue_t *q, range; 1335 ngx_queue_t *q;
1328 ngx_quic_frame_t *f; 1336 ngx_quic_frame_t *f;
1329 1337
1330 found = 0; 1338 found = 0;
1331 1339
1332 ngx_queue_init(&range); 1340 q = ngx_queue_head(&ctx->sent);
1333 1341
1334 q = ngx_queue_head(&ns->sent); 1342 while (q != ngx_queue_sentinel(&ctx->sent)) {
1335
1336 while (q != ngx_queue_sentinel(&ns->sent)) {
1337 1343
1338 f = ngx_queue_data(q, ngx_quic_frame_t, queue); 1344 f = ngx_queue_data(q, ngx_quic_frame_t, queue);
1339 1345
1340 if (f->pnum >= min && f->pnum <= max) { 1346 if (f->pnum >= min && f->pnum <= max) {
1341 q = ngx_queue_next(q); 1347 q = ngx_queue_next(q);
1348 } 1354 }
1349 } 1355 }
1350 1356
1351 if (!found) { 1357 if (!found) {
1352 1358
1353 if (max <= ns->pnum) { 1359 if (max <= ctx->pnum) {
1354 /* duplicate ACK or ACK for non-ack-eliciting frame */ 1360 /* duplicate ACK or ACK for non-ack-eliciting frame */
1355 return NGX_OK; 1361 return NGX_OK;
1356 } 1362 }
1357 1363
1358 ngx_log_error(NGX_LOG_INFO, c->log, 0, 1364 ngx_log_error(NGX_LOG_INFO, c->log, 0,
1660 1666
1661 1667
1662 static void 1668 static void
1663 ngx_quic_queue_frame(ngx_quic_connection_t *qc, ngx_quic_frame_t *frame) 1669 ngx_quic_queue_frame(ngx_quic_connection_t *qc, ngx_quic_frame_t *frame)
1664 { 1670 {
1665 ngx_quic_namespace_t *ns; 1671 ngx_quic_send_ctx_t *ctx;
1666 1672
1667 ns = &qc->ns[ngx_quic_ns(frame->level)]; 1673 ctx = ngx_quic_get_send_ctx(qc, frame->level);
1668 1674
1669 ngx_queue_insert_tail(&ns->frames, &frame->queue); 1675 ngx_queue_insert_tail(&ctx->frames, &frame->queue);
1670 1676
1671 /* TODO: check PUSH flag on stream and call output */ 1677 /* TODO: check PUSH flag on stream and call output */
1672 1678
1673 if (!qc->push.timer_set && !qc->closing) { 1679 if (!qc->push.timer_set && !qc->closing) {
1674 ngx_add_timer(&qc->push, qc->tp.max_ack_delay); 1680 ngx_add_timer(&qc->push, qc->tp.max_ack_delay);
1678 1684
1679 static ngx_int_t 1685 static ngx_int_t
1680 ngx_quic_output(ngx_connection_t *c) 1686 ngx_quic_output(ngx_connection_t *c)
1681 { 1687 {
1682 ngx_uint_t i; 1688 ngx_uint_t i;
1683 ngx_quic_namespace_t *ns;
1684 ngx_quic_connection_t *qc; 1689 ngx_quic_connection_t *qc;
1685 1690
1686 c->log->action = "sending frames"; 1691 c->log->action = "sending frames";
1687 1692
1688 qc = c->quic; 1693 qc = c->quic;
1689 1694
1690 for (i = 0; i < 3; i++) { 1695 for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) {
1691 ns = &qc->ns[i]; 1696 if (ngx_quic_output_frames(c, &qc->send_ctx[i], i) != NGX_OK) {
1692 if (ngx_quic_output_ns(c, ns, i) != NGX_OK) {
1693 return NGX_ERROR; 1697 return NGX_ERROR;
1694 } 1698 }
1695 } 1699 }
1696 1700
1697 if (!qc->send_timer_set && !qc->closing) { 1701 if (!qc->send_timer_set && !qc->closing) {
1706 return NGX_OK; 1710 return NGX_OK;
1707 } 1711 }
1708 1712
1709 1713
1710 static ngx_int_t 1714 static ngx_int_t
1711 ngx_quic_output_ns(ngx_connection_t *c, ngx_quic_namespace_t *ns, 1715 ngx_quic_output_frames(ngx_connection_t *c, ngx_quic_send_ctx_t *ctx,
1712 ngx_uint_t nsi) 1716 ngx_uint_t nsi)
1713 { 1717 {
1714 size_t len, hlen, n; 1718 size_t len, hlen, n;
1715 ngx_int_t rc; 1719 ngx_int_t rc;
1716 ngx_queue_t *q, range; 1720 ngx_queue_t *q, range;
1717 ngx_quic_frame_t *f; 1721 ngx_quic_frame_t *f;
1718 ngx_quic_connection_t *qc; 1722 ngx_quic_connection_t *qc;
1719 1723
1720 qc = c->quic; 1724 qc = c->quic;
1721 1725
1722 if (ngx_queue_empty(&ns->frames)) { 1726 if (ngx_queue_empty(&ctx->frames)) {
1723 return NGX_OK; 1727 return NGX_OK;
1724 } 1728 }
1725 1729
1726 hlen = (nsi == 2) ? NGX_QUIC_MAX_SHORT_HEADER 1730 hlen = (nsi == 2) ? NGX_QUIC_MAX_SHORT_HEADER
1727 : NGX_QUIC_MAX_LONG_HEADER; 1731 : NGX_QUIC_MAX_LONG_HEADER;
1728 1732
1729 hlen += EVP_GCM_TLS_TAG_LEN; 1733 hlen += EVP_GCM_TLS_TAG_LEN;
1730 1734
1731 q = ngx_queue_head(&ns->frames); 1735 q = ngx_queue_head(&ctx->frames);
1732 1736
1733 do { 1737 do {
1734 len = 0; 1738 len = 0;
1735 ngx_queue_init(&range); 1739 ngx_queue_init(&range);
1736 1740
1751 ngx_queue_remove(&f->queue); 1755 ngx_queue_remove(&f->queue);
1752 ngx_queue_insert_tail(&range, &f->queue); 1756 ngx_queue_insert_tail(&range, &f->queue);
1753 1757
1754 len += n; 1758 len += n;
1755 1759
1756 } while (q != ngx_queue_sentinel(&ns->frames)); 1760 } while (q != ngx_queue_sentinel(&ctx->frames));
1757 1761
1758 rc = ngx_quic_send_frames(c, &range); 1762 rc = ngx_quic_send_frames(c, &range);
1759 1763
1760 if (rc == NGX_OK) { 1764 if (rc == NGX_OK) {
1761 /* 1765 /*
1762 * frames are moved into the sent queue 1766 * frames are moved into the sent queue
1763 * to wait for ack/be retransmitted 1767 * to wait for ack/be retransmitted
1764 */ 1768 */
1765 ngx_queue_add(&ns->sent, &range); 1769 ngx_queue_add(&ctx->sent, &range);
1766 1770
1767 } else if (rc == NGX_DONE) { 1771 } else if (rc == NGX_DONE) {
1768 1772
1769 /* no ack is expected for this frames, can free them */ 1773 /* no ack is expected for this frames, can free them */
1770 ngx_quic_free_frames(c, &range); 1774 ngx_quic_free_frames(c, &range);
1772 } else { 1776 } else {
1773 return NGX_ERROR; 1777 return NGX_ERROR;
1774 } 1778 }
1775 1779
1776 1780
1777 } while (q != ngx_queue_sentinel(&ns->frames)); 1781 } while (q != ngx_queue_sentinel(&ctx->frames));
1778 1782
1779 return NGX_OK; 1783 return NGX_OK;
1780 } 1784 }
1781 1785
1782 1786
1808 ngx_str_t out, res; 1812 ngx_str_t out, res;
1809 ngx_queue_t *q; 1813 ngx_queue_t *q;
1810 ngx_quic_frame_t *f, *start; 1814 ngx_quic_frame_t *f, *start;
1811 ngx_quic_header_t pkt; 1815 ngx_quic_header_t pkt;
1812 ngx_quic_secrets_t *keys; 1816 ngx_quic_secrets_t *keys;
1813 ngx_quic_namespace_t *ns; 1817 ngx_quic_send_ctx_t *ctx;
1814 ngx_quic_connection_t *qc; 1818 ngx_quic_connection_t *qc;
1815 static ngx_str_t initial_token = ngx_null_string; 1819 static ngx_str_t initial_token = ngx_null_string;
1816 static u_char src[NGX_QUIC_DEFAULT_MAX_PACKET_SIZE]; 1820 static u_char src[NGX_QUIC_DEFAULT_MAX_PACKET_SIZE];
1817 static u_char dst[NGX_QUIC_DEFAULT_MAX_PACKET_SIZE]; 1821 static u_char dst[NGX_QUIC_DEFAULT_MAX_PACKET_SIZE];
1818 1822
1819 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_quic_send_frames"); 1823 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_quic_send_frames");
1820 1824
1821 q = ngx_queue_head(frames); 1825 q = ngx_queue_head(frames);
1822 start = ngx_queue_data(q, ngx_quic_frame_t, queue); 1826 start = ngx_queue_data(q, ngx_quic_frame_t, queue);
1823 1827
1824 ns = &c->quic->ns[ngx_quic_ns(start->level)]; 1828 ctx = ngx_quic_get_send_ctx(c->quic, start->level);
1825 1829
1826 ngx_memzero(&pkt, sizeof(ngx_quic_header_t)); 1830 ngx_memzero(&pkt, sizeof(ngx_quic_header_t));
1827 1831
1828 p = src; 1832 p = src;
1829 out.data = src; 1833 out.data = src;
1844 if (f->need_ack) { 1848 if (f->need_ack) {
1845 pkt.need_ack = 1; 1849 pkt.need_ack = 1;
1846 } 1850 }
1847 1851
1848 p += len; 1852 p += len;
1849 f->pnum = ns->pnum; 1853 f->pnum = ctx->pnum;
1850 } 1854 }
1851 1855
1852 if (start->level == ssl_encryption_initial) { 1856 if (start->level == ssl_encryption_initial) {
1853 /* ack will not be sent in initial packets due to initial keys being 1857 /* ack will not be sent in initial packets due to initial keys being
1854 * discarded when handshake start. 1858 * discarded when handshake start.
1884 } else { 1888 } else {
1885 // TODO: macro, set FIXED bit 1889 // TODO: macro, set FIXED bit
1886 pkt.flags = 0x40 | (c->quic->key_phase ? NGX_QUIC_PKT_KPHASE : 0); 1890 pkt.flags = 0x40 | (c->quic->key_phase ? NGX_QUIC_PKT_KPHASE : 0);
1887 } 1891 }
1888 1892
1889 ngx_quic_set_packet_number(&pkt, ns); 1893 ngx_quic_set_packet_number(&pkt, ctx);
1890 1894
1891 pkt.log = c->log; 1895 pkt.log = c->log;
1892 pkt.level = start->level; 1896 pkt.level = start->level;
1893 pkt.dcid = qc->dcid; 1897 pkt.dcid = qc->dcid;
1894 pkt.scid = qc->scid; 1898 pkt.scid = qc->scid;
1906 if (len == NGX_ERROR || (size_t) len != res.len) { 1910 if (len == NGX_ERROR || (size_t) len != res.len) {
1907 return NGX_ERROR; 1911 return NGX_ERROR;
1908 } 1912 }
1909 1913
1910 /* len == NGX_OK || NGX_AGAIN */ 1914 /* len == NGX_OK || NGX_AGAIN */
1911 ns->pnum++; 1915 ctx->pnum++;
1912 1916
1913 now = ngx_current_msec; 1917 now = ngx_current_msec;
1914 start->last = now; 1918 start->last = now;
1915 1919
1916 return pkt.need_ack ? NGX_OK : NGX_DONE; 1920 return pkt.need_ack ? NGX_OK : NGX_DONE;
1917 } 1921 }
1918 1922
1919 1923
1920 static void 1924 static void
1921 ngx_quic_set_packet_number(ngx_quic_header_t *pkt, ngx_quic_namespace_t *ns) 1925 ngx_quic_set_packet_number(ngx_quic_header_t *pkt, ngx_quic_send_ctx_t *ctx)
1922 { 1926 {
1923 uint64_t delta; 1927 uint64_t delta;
1924 1928
1925 delta = ns->pnum - ns->largest; 1929 delta = ctx->pnum - ctx->largest_ack;
1926 pkt->number = ns->pnum; 1930 pkt->number = ctx->pnum;
1927 1931
1928 if (delta <= 0x7F) { 1932 if (delta <= 0x7F) {
1929 pkt->num_len = 1; 1933 pkt->num_len = 1;
1930 pkt->trunc = ns->pnum & 0xff; 1934 pkt->trunc = ctx->pnum & 0xff;
1931 1935
1932 } else if (delta <= 0x7FFF) { 1936 } else if (delta <= 0x7FFF) {
1933 pkt->num_len = 2; 1937 pkt->num_len = 2;
1934 pkt->flags |= 0x1; 1938 pkt->flags |= 0x1;
1935 pkt->trunc = ns->pnum & 0xffff; 1939 pkt->trunc = ctx->pnum & 0xffff;
1936 1940
1937 } else if (delta <= 0x7FFFFF) { 1941 } else if (delta <= 0x7FFFFF) {
1938 pkt->num_len = 3; 1942 pkt->num_len = 3;
1939 pkt->flags |= 0x2; 1943 pkt->flags |= 0x2;
1940 pkt->trunc = ns->pnum & 0xffffff; 1944 pkt->trunc = ctx->pnum & 0xffffff;
1941 1945
1942 } else { 1946 } else {
1943 pkt->num_len = 4; 1947 pkt->num_len = 4;
1944 pkt->flags |= 0x3; 1948 pkt->flags |= 0x3;
1945 pkt->trunc = ns->pnum & 0xffffffff; 1949 pkt->trunc = ctx->pnum & 0xffffffff;
1946 } 1950 }
1947 } 1951 }
1948 1952
1949 1953
1950 static void 1954 static void
1961 c = ev->data; 1965 c = ev->data;
1962 qc = c->quic; 1966 qc = c->quic;
1963 1967
1964 wait = 0; 1968 wait = 0;
1965 1969
1966 for (i = 0; i < NGX_QUIC_NAMESPACE_LAST; i++) { 1970 for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) {
1967 if (ngx_quic_retransmit_ns(c, &qc->ns[i], &nswait) != NGX_OK) { 1971 if (ngx_quic_retransmit(c, &qc->send_ctx[i], &nswait) != NGX_OK) {
1968 ngx_quic_close_connection(c); 1972 ngx_quic_close_connection(c);
1969 return; 1973 return;
1970 } 1974 }
1971 1975
1972 if (i == 0) { 1976 if (i == 0) {
1998 } 2002 }
1999 } 2003 }
2000 2004
2001 2005
2002 static ngx_int_t 2006 static ngx_int_t
2003 ngx_quic_retransmit_ns(ngx_connection_t *c, ngx_quic_namespace_t *ns, 2007 ngx_quic_retransmit(ngx_connection_t *c, ngx_quic_send_ctx_t *ctx,
2004 ngx_msec_t *waitp) 2008 ngx_msec_t *waitp)
2005 { 2009 {
2006 uint64_t pn; 2010 uint64_t pn;
2007 ngx_msec_t now, wait; 2011 ngx_msec_t now, wait;
2008 ngx_queue_t *q, range; 2012 ngx_queue_t *q, range;
2012 qc = c->quic; 2016 qc = c->quic;
2013 2017
2014 now = ngx_current_msec; 2018 now = ngx_current_msec;
2015 wait = 0; 2019 wait = 0;
2016 2020
2017 if (ngx_queue_empty(&ns->sent)) { 2021 if (ngx_queue_empty(&ctx->sent)) {
2018 *waitp = 0; 2022 *waitp = 0;
2019 return NGX_OK; 2023 return NGX_OK;
2020 } 2024 }
2021 2025
2022 q = ngx_queue_head(&ns->sent); 2026 q = ngx_queue_head(&ctx->sent);
2023 start = ngx_queue_data(q, ngx_quic_frame_t, queue); 2027 start = ngx_queue_data(q, ngx_quic_frame_t, queue);
2024 pn = start->pnum; 2028 pn = start->pnum;
2025 f = start; 2029 f = start;
2026 2030
2027 do { 2031 do {
2044 q = ngx_queue_next(q); 2048 q = ngx_queue_next(q);
2045 2049
2046 ngx_queue_remove(&f->queue); 2050 ngx_queue_remove(&f->queue);
2047 ngx_queue_insert_tail(&range, &f->queue); 2051 ngx_queue_insert_tail(&range, &f->queue);
2048 2052
2049 } while (q != ngx_queue_sentinel(&ns->sent)); 2053 } while (q != ngx_queue_sentinel(&ctx->sent));
2050 2054
2051 wait = start->last + qc->tp.max_ack_delay - now; 2055 wait = start->last + qc->tp.max_ack_delay - now;
2052 2056
2053 if ((ngx_msec_int_t) wait > 0) { 2057 if ((ngx_msec_int_t) wait > 0) {
2054 break; 2058 break;
2058 if (ngx_quic_send_frames(c, &range) != NGX_OK) { 2062 if (ngx_quic_send_frames(c, &range) != NGX_OK) {
2059 return NGX_ERROR; 2063 return NGX_ERROR;
2060 } 2064 }
2061 2065
2062 /* move frames group to the end of queue */ 2066 /* move frames group to the end of queue */
2063 ngx_queue_add(&ns->sent, &range); 2067 ngx_queue_add(&ctx->sent, &range);
2064 2068
2065 } while (q != ngx_queue_sentinel(&ns->sent)); 2069 } while (q != ngx_queue_sentinel(&ctx->sent));
2066 2070
2067 *waitp = wait; 2071 *waitp = wait;
2068 2072
2069 return NGX_OK; 2073 return NGX_OK;
2070 } 2074 }