comparison src/event/quic/ngx_event_quic_transport.c @ 8545:4009f120cad4 quic

QUIC: eliminated stream type from ngx_quic_stream_frame_t. The information about the type is contained in off/len/fin bits. Also, where possible, only the first stream type (0x08) is used for simplicity.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 28 Jul 2021 13:21:47 +0300
parents 4715f3e669f1
children 40f2f059145a
comparison
equal deleted inserted replaced
8544:d0ef43a53a51 8545:4009f120cad4
11 11
12 12
13 #define NGX_QUIC_LONG_DCID_LEN_OFFSET 5 13 #define NGX_QUIC_LONG_DCID_LEN_OFFSET 5
14 #define NGX_QUIC_LONG_DCID_OFFSET 6 14 #define NGX_QUIC_LONG_DCID_OFFSET 6
15 #define NGX_QUIC_SHORT_DCID_OFFSET 1 15 #define NGX_QUIC_SHORT_DCID_OFFSET 1
16
17 #define NGX_QUIC_STREAM_FRAME_FIN 0x01
18 #define NGX_QUIC_STREAM_FRAME_LEN 0x02
19 #define NGX_QUIC_STREAM_FRAME_OFF 0x04
16 20
17 21
18 #if (NGX_HAVE_NONALIGNED) 22 #if (NGX_HAVE_NONALIGNED)
19 23
20 #define ngx_quic_parse_uint16(p) ntohs(*(uint16_t *) (p)) 24 #define ngx_quic_parse_uint16(p) ntohs(*(uint16_t *) (p))
734 738
735 return p - out; 739 return p - out;
736 } 740 }
737 741
738 742
739 #define ngx_quic_stream_bit_off(val) (((val) & 0x04) ? 1 : 0)
740 #define ngx_quic_stream_bit_len(val) (((val) & 0x02) ? 1 : 0)
741 #define ngx_quic_stream_bit_fin(val) (((val) & 0x01) ? 1 : 0)
742
743 ssize_t 743 ssize_t
744 ngx_quic_parse_frame(ngx_quic_header_t *pkt, u_char *start, u_char *end, 744 ngx_quic_parse_frame(ngx_quic_header_t *pkt, u_char *start, u_char *end,
745 ngx_quic_frame_t *f) 745 ngx_quic_frame_t *f)
746 { 746 {
747 u_char *p; 747 u_char *p;
929 goto error; 929 goto error;
930 } 930 }
931 931
932 break; 932 break;
933 933
934 case NGX_QUIC_FT_STREAM0: 934 case NGX_QUIC_FT_STREAM:
935 case NGX_QUIC_FT_STREAM1: 935 case NGX_QUIC_FT_STREAM1:
936 case NGX_QUIC_FT_STREAM2: 936 case NGX_QUIC_FT_STREAM2:
937 case NGX_QUIC_FT_STREAM3: 937 case NGX_QUIC_FT_STREAM3:
938 case NGX_QUIC_FT_STREAM4: 938 case NGX_QUIC_FT_STREAM4:
939 case NGX_QUIC_FT_STREAM5: 939 case NGX_QUIC_FT_STREAM5:
940 case NGX_QUIC_FT_STREAM6: 940 case NGX_QUIC_FT_STREAM6:
941 case NGX_QUIC_FT_STREAM7: 941 case NGX_QUIC_FT_STREAM7:
942 942
943 f->u.stream.type = f->type; 943 f->u.stream.fin = (f->type & NGX_QUIC_STREAM_FRAME_FIN) ? 1 : 0;
944
945 f->u.stream.off = ngx_quic_stream_bit_off(f->type);
946 f->u.stream.len = ngx_quic_stream_bit_len(f->type);
947 f->u.stream.fin = ngx_quic_stream_bit_fin(f->type);
948 944
949 p = ngx_quic_parse_int(p, end, &f->u.stream.stream_id); 945 p = ngx_quic_parse_int(p, end, &f->u.stream.stream_id);
950 if (p == NULL) { 946 if (p == NULL) {
951 goto error; 947 goto error;
952 } 948 }
953 949
954 if (f->type & 0x04) { 950 if (f->type & NGX_QUIC_STREAM_FRAME_OFF) {
951 f->u.stream.off = 1;
952
955 p = ngx_quic_parse_int(p, end, &f->u.stream.offset); 953 p = ngx_quic_parse_int(p, end, &f->u.stream.offset);
956 if (p == NULL) { 954 if (p == NULL) {
957 goto error; 955 goto error;
958 } 956 }
959 957
960 } else { 958 } else {
959 f->u.stream.off = 0;
961 f->u.stream.offset = 0; 960 f->u.stream.offset = 0;
962 } 961 }
963 962
964 if (f->type & 0x02) { 963 if (f->type & NGX_QUIC_STREAM_FRAME_LEN) {
964 f->u.stream.len = 1;
965
965 p = ngx_quic_parse_int(p, end, &f->u.stream.length); 966 p = ngx_quic_parse_int(p, end, &f->u.stream.length);
966 if (p == NULL) { 967 if (p == NULL) {
967 goto error; 968 goto error;
968 } 969 }
969 970
970 } else { 971 } else {
972 f->u.stream.len = 0;
971 f->u.stream.length = end - p; /* up to packet end */ 973 f->u.stream.length = end - p; /* up to packet end */
972 } 974 }
973 975
974 p = ngx_quic_read_bytes(p, end, f->u.stream.length, &b->pos); 976 p = ngx_quic_read_bytes(p, end, f->u.stream.length, &b->pos);
975 if (p == NULL) { 977 if (p == NULL) {
976 goto error; 978 goto error;
977 } 979 }
978 980
979 b->last = p; 981 b->last = p;
982
983 f->type = NGX_QUIC_FT_STREAM;
980 break; 984 break;
981 985
982 case NGX_QUIC_FT_MAX_DATA: 986 case NGX_QUIC_FT_MAX_DATA:
983 987
984 p = ngx_quic_parse_int(p, end, &f->u.max_data.max_data); 988 p = ngx_quic_parse_int(p, end, &f->u.max_data.max_data);
1139 /* ACK_ECN */ 0xD, 1143 /* ACK_ECN */ 0xD,
1140 /* RESET_STREAM */ 0x3, 1144 /* RESET_STREAM */ 0x3,
1141 /* STOP_SENDING */ 0x3, 1145 /* STOP_SENDING */ 0x3,
1142 /* CRYPTO */ 0xD, 1146 /* CRYPTO */ 0xD,
1143 /* NEW_TOKEN */ 0x0, /* only sent by server */ 1147 /* NEW_TOKEN */ 0x0, /* only sent by server */
1144 /* STREAM0 */ 0x3, 1148 /* STREAM */ 0x3,
1145 /* STREAM1 */ 0x3, 1149 /* STREAM1 */ 0x3,
1146 /* STREAM2 */ 0x3, 1150 /* STREAM2 */ 0x3,
1147 /* STREAM3 */ 0x3, 1151 /* STREAM3 */ 0x3,
1148 /* STREAM4 */ 0x3, 1152 /* STREAM4 */ 0x3,
1149 /* STREAM5 */ 0x3, 1153 /* STREAM5 */ 0x3,
1274 return ngx_quic_create_hs_done(p); 1278 return ngx_quic_create_hs_done(p);
1275 1279
1276 case NGX_QUIC_FT_NEW_TOKEN: 1280 case NGX_QUIC_FT_NEW_TOKEN:
1277 return ngx_quic_create_new_token(p, &f->u.token); 1281 return ngx_quic_create_new_token(p, &f->u.token);
1278 1282
1279 case NGX_QUIC_FT_STREAM0: 1283 case NGX_QUIC_FT_STREAM:
1280 case NGX_QUIC_FT_STREAM1:
1281 case NGX_QUIC_FT_STREAM2:
1282 case NGX_QUIC_FT_STREAM3:
1283 case NGX_QUIC_FT_STREAM4:
1284 case NGX_QUIC_FT_STREAM5:
1285 case NGX_QUIC_FT_STREAM6:
1286 case NGX_QUIC_FT_STREAM7:
1287 return ngx_quic_create_stream(p, &f->u.stream, f->data); 1284 return ngx_quic_create_stream(p, &f->u.stream, f->data);
1288 1285
1289 case NGX_QUIC_FT_CONNECTION_CLOSE: 1286 case NGX_QUIC_FT_CONNECTION_CLOSE:
1290 case NGX_QUIC_FT_CONNECTION_CLOSE_APP: 1287 case NGX_QUIC_FT_CONNECTION_CLOSE_APP:
1291 f->need_ack = 0; 1288 f->need_ack = 0;
1497 static size_t 1494 static size_t
1498 ngx_quic_create_stream(u_char *p, ngx_quic_stream_frame_t *sf, 1495 ngx_quic_create_stream(u_char *p, ngx_quic_stream_frame_t *sf,
1499 ngx_chain_t *data) 1496 ngx_chain_t *data)
1500 { 1497 {
1501 size_t len; 1498 size_t len;
1502 u_char *start; 1499 u_char *start, type;
1503 ngx_buf_t *b; 1500 ngx_buf_t *b;
1504 1501
1505 if (p == NULL) { 1502 type = NGX_QUIC_FT_STREAM;
1506 len = ngx_quic_varint_len(sf->type); 1503
1504 if (sf->off) {
1505 type |= NGX_QUIC_STREAM_FRAME_OFF;
1506 }
1507
1508 if (sf->len) {
1509 type |= NGX_QUIC_STREAM_FRAME_LEN;
1510 }
1511
1512 if (sf->fin) {
1513 type |= NGX_QUIC_STREAM_FRAME_FIN;
1514 }
1515
1516 if (p == NULL) {
1517 len = ngx_quic_varint_len(type);
1518 len += ngx_quic_varint_len(sf->stream_id);
1507 1519
1508 if (sf->off) { 1520 if (sf->off) {
1509 len += ngx_quic_varint_len(sf->offset); 1521 len += ngx_quic_varint_len(sf->offset);
1510 } 1522 }
1511 1523
1512 len += ngx_quic_varint_len(sf->stream_id); 1524 if (sf->len) {
1513 1525 len += ngx_quic_varint_len(sf->length);
1514 /* length is always present in generated frames */ 1526 }
1515 len += ngx_quic_varint_len(sf->length);
1516 1527
1517 len += sf->length; 1528 len += sf->length;
1518 1529
1519 return len; 1530 return len;
1520 } 1531 }
1521 1532
1522 start = p; 1533 start = p;
1523 1534
1524 ngx_quic_build_int(&p, sf->type); 1535 ngx_quic_build_int(&p, type);
1525 ngx_quic_build_int(&p, sf->stream_id); 1536 ngx_quic_build_int(&p, sf->stream_id);
1526 1537
1527 if (sf->off) { 1538 if (sf->off) {
1528 ngx_quic_build_int(&p, sf->offset); 1539 ngx_quic_build_int(&p, sf->offset);
1529 } 1540 }
1530 1541
1531 /* length is always present in generated frames */ 1542 if (sf->len) {
1532 ngx_quic_build_int(&p, sf->length); 1543 ngx_quic_build_int(&p, sf->length);
1544 }
1533 1545
1534 while (data) { 1546 while (data) {
1535 b = data->buf; 1547 b = data->buf;
1536 p = ngx_cpymem(p, b->pos, b->last - b->pos); 1548 p = ngx_cpymem(p, b->pos, b->last - b->pos);
1537 data = data->next; 1549 data = data->next;