comparison src/event/ngx_event_quic.c @ 8229:cfc429911c0d quic

Implemented creation of server unidirectional streams. The ngx_quic_create_stream() function is a generic function extracted from the ngx_quic_handle_stream_frame() function.
author Vladimir Homutov <vl@nginx.com>
date Wed, 18 Mar 2020 13:49:39 +0300
parents 714a19dba6af
children 78540e2160d0
comparison
equal deleted inserted replaced
8228:ac41c53e446d 8229:cfc429911c0d
20 typedef struct { 20 typedef struct {
21 ngx_rbtree_t tree; 21 ngx_rbtree_t tree;
22 ngx_rbtree_node_t sentinel; 22 ngx_rbtree_node_t sentinel;
23 ngx_msec_t timeout; 23 ngx_msec_t timeout;
24 ngx_connection_handler_pt handler; 24 ngx_connection_handler_pt handler;
25
26 ngx_uint_t id_counter;
25 } ngx_quic_streams_t; 27 } ngx_quic_streams_t;
26 28
27 29
28 struct ngx_quic_connection_s { 30 struct ngx_quic_connection_s {
29 ngx_str_t scid; 31 ngx_str_t scid;
99 101
100 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp, 102 static void ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
101 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); 103 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
102 static ngx_quic_stream_node_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree, 104 static ngx_quic_stream_node_t *ngx_quic_find_stream(ngx_rbtree_t *rbtree,
103 ngx_uint_t key); 105 ngx_uint_t key);
106 static ngx_quic_stream_node_t *ngx_quic_create_stream(ngx_connection_t *c,
107 ngx_uint_t id);
104 static ssize_t ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, 108 static ssize_t ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf,
105 size_t size); 109 size_t size);
106 static ssize_t ngx_quic_stream_send(ngx_connection_t *c, u_char *buf, 110 static ssize_t ngx_quic_stream_send(ngx_connection_t *c, u_char *buf,
107 size_t size); 111 size_t size);
108 static ngx_chain_t *ngx_quic_stream_send_chain(ngx_connection_t *c, 112 static ngx_chain_t *ngx_quic_stream_send_chain(ngx_connection_t *c,
914 static ngx_int_t 918 static ngx_int_t
915 ngx_quic_handle_stream_frame(ngx_connection_t *c, 919 ngx_quic_handle_stream_frame(ngx_connection_t *c,
916 ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *f) 920 ngx_quic_header_t *pkt, ngx_quic_stream_frame_t *f)
917 { 921 {
918 ngx_buf_t *b; 922 ngx_buf_t *b;
923 ngx_quic_connection_t *qc;
924 ngx_quic_stream_node_t *sn;
925
926 qc = c->quic;
927
928 sn = ngx_quic_find_stream(&qc->streams.tree, f->stream_id);
929
930 if (sn) {
931 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "existing stream");
932 b = sn->b;
933
934 if ((size_t) (b->end - b->pos) < f->length) {
935 ngx_log_error(NGX_LOG_INFO, c->log, 0, "no space in stream buffer");
936 return NGX_ERROR;
937 }
938
939 ngx_memcpy(b->pos, f->data, f->length);
940 b->pos += f->length;
941
942 // TODO: notify
943
944 return NGX_OK;
945 }
946
947 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "stream is new");
948
949 sn = ngx_quic_create_stream(c, f->stream_id);
950 if (sn == NULL) {
951 return NGX_ERROR;
952 }
953
954 b = sn->b;
955
956 ngx_memcpy(b->start, f->data, f->length);
957 b->last = b->start + f->length;
958
959 qc->streams.handler(sn->c);
960
961 return NGX_OK;
962 }
963
964
965 static void
966 ngx_quic_queue_frame(ngx_quic_connection_t *qc, ngx_quic_frame_t *frame)
967 {
968 ngx_quic_frame_t *f;
969
970 if (qc->frames == NULL) {
971 qc->frames = frame;
972 return;
973 }
974
975 for (f = qc->frames; f->next; f = f->next) {
976 if (f->next->level > frame->level) {
977 break;
978 }
979 }
980
981 frame->next = f->next;
982 f->next = frame;
983 }
984
985
986 static ngx_int_t
987 ngx_quic_output(ngx_connection_t *c)
988 {
989 size_t len;
990 ngx_uint_t lvl;
991 ngx_quic_frame_t *f, *start;
992 ngx_quic_connection_t *qc;
993
994 qc = c->quic;
995
996 if (qc->frames == NULL) {
997 return NGX_OK;
998 }
999
1000 lvl = qc->frames->level;
1001 start = qc->frames;
1002 f = start;
1003
1004 do {
1005 len = 0;
1006
1007 do {
1008 /* process same-level group of frames */
1009
1010 len += ngx_quic_frame_len(f);// TODO: handle overflow, max size
1011
1012 f = f->next;
1013 } while (f && f->level == lvl);
1014
1015
1016 if (ngx_quic_frames_send(c, start, f, len) != NGX_OK) {
1017 return NGX_ERROR;
1018 }
1019
1020 if (f == NULL) {
1021 break;
1022 }
1023
1024 lvl = f->level; // TODO: must not decrease (ever, also between calls)
1025 start = f;
1026
1027 } while (1);
1028
1029 qc->frames = NULL;
1030
1031 return NGX_OK;
1032 }
1033
1034
1035 /* pack a group of frames [start; end) into memory p and send as single packet */
1036 ngx_int_t
1037 ngx_quic_frames_send(ngx_connection_t *c, ngx_quic_frame_t *start,
1038 ngx_quic_frame_t *end, size_t total)
1039 {
1040 ssize_t len;
1041 u_char *p;
1042 ngx_str_t out;
1043 ngx_quic_frame_t *f;
1044
1045 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1046 "sending frames %p...%p", start, end);
1047
1048 p = ngx_pnalloc(c->pool, total);
1049 if (p == NULL) {
1050 return NGX_ERROR;
1051 }
1052
1053 out.data = p;
1054
1055 for (f = start; f != end; f = f->next) {
1056
1057 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "frame: %s", f->info);
1058
1059 len = ngx_quic_create_frame(p, p + total, f);
1060 if (len == -1) {
1061 return NGX_ERROR;
1062 }
1063
1064 p += len;
1065 }
1066
1067 out.len = p - out.data;
1068
1069 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1070 "packet ready: %ui bytes at level %d",
1071 out.len, start->level);
1072
1073 // IOVEC/sendmsg_chain ?
1074 if (ngx_quic_send_packet(c, c->quic, start->level, &out) != NGX_OK) {
1075 return NGX_ERROR;
1076 }
1077
1078 return NGX_OK;
1079 }
1080
1081
1082 static ngx_int_t
1083 ngx_quic_send_packet(ngx_connection_t *c, ngx_quic_connection_t *qc,
1084 enum ssl_encryption_level_t level, ngx_str_t *payload)
1085 {
1086 ngx_str_t res;
1087 ngx_quic_header_t pkt;
1088
1089 pkt.log = c->log;
1090
1091 static ngx_str_t initial_token = ngx_null_string;
1092
1093 ngx_memzero(&pkt, sizeof(ngx_quic_header_t));
1094 ngx_quic_hexdump0(c->log, "payload", payload->data, payload->len);
1095
1096 pkt.level = level;
1097 pkt.dcid = qc->dcid;
1098 pkt.scid = qc->scid;
1099
1100 if (level == ssl_encryption_initial) {
1101 pkt.number = &qc->initial_pn;
1102 pkt.flags = NGX_QUIC_PKT_INITIAL;
1103 pkt.secret = &qc->secrets.server.in;
1104 pkt.token = initial_token;
1105
1106 } else if (level == ssl_encryption_handshake) {
1107 pkt.number = &qc->handshake_pn;
1108 pkt.flags = NGX_QUIC_PKT_HANDSHAKE;
1109 pkt.secret = &qc->secrets.server.hs;
1110
1111 } else {
1112 pkt.number = &qc->appdata_pn;
1113 pkt.secret = &qc->secrets.server.ad;
1114 }
1115
1116 if (ngx_quic_encrypt(c->pool, c->ssl->connection, &pkt, payload, &res)
1117 != NGX_OK)
1118 {
1119 return NGX_ERROR;
1120 }
1121
1122 ngx_quic_hexdump0(c->log, "packet to send", res.data, res.len);
1123
1124 c->send(c, res.data, res.len); // TODO: err handling
1125
1126 (*pkt.number)++;
1127
1128 return NGX_OK;
1129 }
1130
1131
1132 ngx_connection_t *
1133 ngx_quic_create_uni_stream(ngx_connection_t *c)
1134 {
1135 ngx_uint_t id;
1136 ngx_quic_stream_t *qs;
1137 ngx_quic_connection_t *qc;
1138 ngx_quic_stream_node_t *sn;
1139
1140 qs = c->qs;
1141 qc = qs->parent->quic;
1142
1143 /*
1144 * A stream ID is a 62-bit integer that is unique for all streams
1145 * on a connection.
1146 *
1147 * 0x3 | Server-Initiated, Unidirectional
1148 */
1149 id = (qc->streams.id_counter << 2) | 0x3;
1150
1151 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1152 "creating server uni stream #%ui id %ui",
1153 qc->streams.id_counter, id);
1154
1155 qc->streams.id_counter++;
1156
1157 sn = ngx_quic_create_stream(qs->parent, id);
1158 if (sn == NULL) {
1159 return NULL;
1160 }
1161
1162 return sn->c;
1163 }
1164
1165
1166 static void
1167 ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
1168 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1169 {
1170 ngx_rbtree_node_t **p;
1171 ngx_quic_stream_node_t *qn, *qnt;
1172
1173 for ( ;; ) {
1174
1175 if (node->key < temp->key) {
1176
1177 p = &temp->left;
1178
1179 } else if (node->key > temp->key) {
1180
1181 p = &temp->right;
1182
1183 } else { /* node->key == temp->key */
1184
1185 qn = (ngx_quic_stream_node_t *) &node->color;
1186 qnt = (ngx_quic_stream_node_t *) &temp->color;
1187
1188 if (qn->c < qnt->c) {
1189 p = &temp->left;
1190 } else {
1191 p = &temp->right;
1192 }
1193 }
1194
1195 if (*p == sentinel) {
1196 break;
1197 }
1198
1199 temp = *p;
1200 }
1201
1202 *p = node;
1203 node->parent = temp;
1204 node->left = sentinel;
1205 node->right = sentinel;
1206 ngx_rbt_red(node);
1207 }
1208
1209
1210 static ngx_quic_stream_node_t *
1211 ngx_quic_find_stream(ngx_rbtree_t *rbtree, ngx_uint_t key)
1212 {
1213 ngx_rbtree_node_t *node, *sentinel;
1214
1215 node = rbtree->root;
1216 sentinel = rbtree->sentinel;
1217
1218 while (node != sentinel) {
1219
1220 if (key == node->key) {
1221 return (ngx_quic_stream_node_t *) node;
1222 }
1223
1224 node = (key < node->key) ? node->left : node->right;
1225 }
1226
1227 return NULL;
1228 }
1229
1230
1231 static ngx_quic_stream_node_t *
1232 ngx_quic_create_stream(ngx_connection_t *c, ngx_uint_t id)
1233 {
919 ngx_log_t *log; 1234 ngx_log_t *log;
920 ngx_pool_t *pool; 1235 ngx_pool_t *pool;
921 ngx_event_t *rev, *wev; 1236 ngx_event_t *rev, *wev;
922 ngx_quic_connection_t *qc; 1237 ngx_quic_connection_t *qc;
923 ngx_quic_stream_node_t *sn; 1238 ngx_quic_stream_node_t *sn;
924 1239
925 qc = c->quic; 1240 qc = c->quic;
926 1241
927 sn = ngx_quic_find_stream(&qc->streams.tree, f->stream_id);
928
929 if (sn) {
930 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "existing stream");
931 b = sn->b;
932
933 if ((size_t) (b->end - b->pos) < f->length) {
934 ngx_log_error(NGX_LOG_INFO, c->log, 0, "no space in stream buffer");
935 return NGX_ERROR;
936 }
937
938 ngx_memcpy(b->pos, f->data, f->length);
939 b->pos += f->length;
940
941 // TODO: notify
942
943 return NGX_OK;
944 }
945
946 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "stream is new");
947
948 sn = ngx_pcalloc(c->pool, sizeof(ngx_quic_stream_node_t)); 1242 sn = ngx_pcalloc(c->pool, sizeof(ngx_quic_stream_node_t));
949 if (sn == NULL) { 1243 if (sn == NULL) {
950 return NGX_ERROR; 1244 return NULL;
951 } 1245 }
952 1246
953 sn->c = ngx_get_connection(-1, c->log); // TODO: free on connection termination 1247 sn->c = ngx_get_connection(-1, c->log); // TODO: free on connection termination
954 if (sn->c == NULL) { 1248 if (sn->c == NULL) {
955 return NGX_ERROR; 1249 return NULL;
956 } 1250 }
957 1251
958 pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, c->log); 1252 pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, c->log);
959 if (pool == NULL) { 1253 if (pool == NULL) {
960 /* XXX free connection */ 1254 /* XXX free connection */
961 return NGX_ERROR; 1255 // TODO: add pool cleanup handdler
1256 return NULL;
962 } 1257 }
963 1258
964 log = ngx_palloc(pool, sizeof(ngx_log_t)); 1259 log = ngx_palloc(pool, sizeof(ngx_log_t));
965 if (log == NULL) { 1260 if (log == NULL) {
966 /* XXX free pool and connection */ 1261 /* XXX free pool and connection */
967 return NGX_ERROR; 1262 return NULL;
968 } 1263 }
969 1264
970 *log = *c->log; 1265 *log = *c->log;
971 pool->log = log; 1266 pool->log = log;
972 1267
985 rev->log = c->log; 1280 rev->log = c->log;
986 wev->log = c->log; 1281 wev->log = c->log;
987 1282
988 sn->c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); 1283 sn->c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
989 1284
990 sn->node.key = f->stream_id; 1285 sn->node.key =id;
991 sn->b = ngx_create_temp_buf(pool, 16 * 1024); // XXX enough for everyone 1286 sn->b = ngx_create_temp_buf(pool, 16 * 1024); // XXX enough for everyone
992 if (sn->b == NULL) { 1287 if (sn->b == NULL) {
993 return NGX_ERROR; 1288 return NULL;
994 } 1289 }
995 b = sn->b;
996
997 ngx_memcpy(b->start, f->data, f->length);
998 b->last = b->start + f->length;
999 1290
1000 ngx_rbtree_insert(&qc->streams.tree, &sn->node); 1291 ngx_rbtree_insert(&qc->streams.tree, &sn->node);
1001 1292
1002 sn->s.id = f->stream_id; 1293 sn->s.id = id;
1003 sn->s.unidirectional = (sn->s.id & 0x02) ? 1 : 0; 1294 sn->s.unidirectional = (sn->s.id & 0x02) ? 1 : 0;
1004 sn->s.parent = c; 1295 sn->s.parent = c;
1005 sn->c->qs = &sn->s; 1296 sn->c->qs = &sn->s;
1006 1297
1007 sn->c->recv = ngx_quic_stream_recv; 1298 sn->c->recv = ngx_quic_stream_recv;
1008 sn->c->send = ngx_quic_stream_send; 1299 sn->c->send = ngx_quic_stream_send;
1009 sn->c->send_chain = ngx_quic_stream_send_chain; 1300 sn->c->send_chain = ngx_quic_stream_send_chain;
1010 1301
1011 qc->streams.handler(sn->c); 1302 return sn;
1012
1013 return NGX_OK;
1014 }
1015
1016
1017 static void
1018 ngx_quic_queue_frame(ngx_quic_connection_t *qc, ngx_quic_frame_t *frame)
1019 {
1020 ngx_quic_frame_t *f;
1021
1022 if (qc->frames == NULL) {
1023 qc->frames = frame;
1024 return;
1025 }
1026
1027 for (f = qc->frames; f->next; f = f->next) {
1028 if (f->next->level > frame->level) {
1029 break;
1030 }
1031 }
1032
1033 frame->next = f->next;
1034 f->next = frame;
1035 }
1036
1037
1038 static ngx_int_t
1039 ngx_quic_output(ngx_connection_t *c)
1040 {
1041 size_t len;
1042 ngx_uint_t lvl;
1043 ngx_quic_frame_t *f, *start;
1044 ngx_quic_connection_t *qc;
1045
1046 qc = c->quic;
1047
1048 if (qc->frames == NULL) {
1049 return NGX_OK;
1050 }
1051
1052 lvl = qc->frames->level;
1053 start = qc->frames;
1054 f = start;
1055
1056 do {
1057 len = 0;
1058
1059 do {
1060 /* process same-level group of frames */
1061
1062 len += ngx_quic_frame_len(f);// TODO: handle overflow, max size
1063
1064 f = f->next;
1065 } while (f && f->level == lvl);
1066
1067
1068 if (ngx_quic_frames_send(c, start, f, len) != NGX_OK) {
1069 return NGX_ERROR;
1070 }
1071
1072 if (f == NULL) {
1073 break;
1074 }
1075
1076 lvl = f->level; // TODO: must not decrease (ever, also between calls)
1077 start = f;
1078
1079 } while (1);
1080
1081 qc->frames = NULL;
1082
1083 return NGX_OK;
1084 }
1085
1086
1087 /* pack a group of frames [start; end) into memory p and send as single packet */
1088 ngx_int_t
1089 ngx_quic_frames_send(ngx_connection_t *c, ngx_quic_frame_t *start,
1090 ngx_quic_frame_t *end, size_t total)
1091 {
1092 ssize_t len;
1093 u_char *p;
1094 ngx_str_t out;
1095 ngx_quic_frame_t *f;
1096
1097 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1098 "sending frames %p...%p", start, end);
1099
1100 p = ngx_pnalloc(c->pool, total);
1101 if (p == NULL) {
1102 return NGX_ERROR;
1103 }
1104
1105 out.data = p;
1106
1107 for (f = start; f != end; f = f->next) {
1108
1109 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "frame: %s", f->info);
1110
1111 len = ngx_quic_create_frame(p, p + total, f);
1112 if (len == -1) {
1113 return NGX_ERROR;
1114 }
1115
1116 p += len;
1117 }
1118
1119 out.len = p - out.data;
1120
1121 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1122 "packet ready: %ui bytes at level %d",
1123 out.len, start->level);
1124
1125 // IOVEC/sendmsg_chain ?
1126 if (ngx_quic_send_packet(c, c->quic, start->level, &out) != NGX_OK) {
1127 return NGX_ERROR;
1128 }
1129
1130 return NGX_OK;
1131 }
1132
1133
1134 static ngx_int_t
1135 ngx_quic_send_packet(ngx_connection_t *c, ngx_quic_connection_t *qc,
1136 enum ssl_encryption_level_t level, ngx_str_t *payload)
1137 {
1138 ngx_str_t res;
1139 ngx_quic_header_t pkt;
1140
1141 pkt.log = c->log;
1142
1143 static ngx_str_t initial_token = ngx_null_string;
1144
1145 ngx_memzero(&pkt, sizeof(ngx_quic_header_t));
1146 ngx_quic_hexdump0(c->log, "payload", payload->data, payload->len);
1147
1148 pkt.level = level;
1149 pkt.dcid = qc->dcid;
1150 pkt.scid = qc->scid;
1151
1152 if (level == ssl_encryption_initial) {
1153 pkt.number = &qc->initial_pn;
1154 pkt.flags = NGX_QUIC_PKT_INITIAL;
1155 pkt.secret = &qc->secrets.server.in;
1156 pkt.token = initial_token;
1157
1158 } else if (level == ssl_encryption_handshake) {
1159 pkt.number = &qc->handshake_pn;
1160 pkt.flags = NGX_QUIC_PKT_HANDSHAKE;
1161 pkt.secret = &qc->secrets.server.hs;
1162
1163 } else {
1164 pkt.number = &qc->appdata_pn;
1165 pkt.secret = &qc->secrets.server.ad;
1166 }
1167
1168 if (ngx_quic_encrypt(c->pool, c->ssl->connection, &pkt, payload, &res)
1169 != NGX_OK)
1170 {
1171 return NGX_ERROR;
1172 }
1173
1174 ngx_quic_hexdump0(c->log, "packet to send", res.data, res.len);
1175
1176 c->send(c, res.data, res.len); // TODO: err handling
1177
1178 (*pkt.number)++;
1179
1180 return NGX_OK;
1181 }
1182
1183
1184 ngx_connection_t *
1185 ngx_quic_create_uni_stream(ngx_connection_t *c)
1186 {
1187 /* XXX */
1188 return NULL;
1189 }
1190
1191
1192 static void
1193 ngx_quic_rbtree_insert_stream(ngx_rbtree_node_t *temp,
1194 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1195 {
1196 ngx_rbtree_node_t **p;
1197 ngx_quic_stream_node_t *qn, *qnt;
1198
1199 for ( ;; ) {
1200
1201 if (node->key < temp->key) {
1202
1203 p = &temp->left;
1204
1205 } else if (node->key > temp->key) {
1206
1207 p = &temp->right;
1208
1209 } else { /* node->key == temp->key */
1210
1211 qn = (ngx_quic_stream_node_t *) &node->color;
1212 qnt = (ngx_quic_stream_node_t *) &temp->color;
1213
1214 if (qn->c < qnt->c) {
1215 p = &temp->left;
1216 } else {
1217 p = &temp->right;
1218 }
1219 }
1220
1221 if (*p == sentinel) {
1222 break;
1223 }
1224
1225 temp = *p;
1226 }
1227
1228 *p = node;
1229 node->parent = temp;
1230 node->left = sentinel;
1231 node->right = sentinel;
1232 ngx_rbt_red(node);
1233 }
1234
1235
1236 static ngx_quic_stream_node_t *
1237 ngx_quic_find_stream(ngx_rbtree_t *rbtree, ngx_uint_t key)
1238 {
1239 ngx_rbtree_node_t *node, *sentinel;
1240
1241 node = rbtree->root;
1242 sentinel = rbtree->sentinel;
1243
1244 while (node != sentinel) {
1245
1246 if (key == node->key) {
1247 return (ngx_quic_stream_node_t *) node;
1248 }
1249
1250 node = (key < node->key) ? node->left : node->right;
1251 }
1252
1253 return NULL;
1254 } 1303 }
1255 1304
1256 1305
1257 static ssize_t 1306 static ssize_t
1258 ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, size_t size) 1307 ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, size_t size)