2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <sys/socket.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/transport.h>
20 #include <vnet/session/session.h>
21 #include <vlib/unix/plugin.h>
22 #include <vpp/app/version.h>
23 #include <openssl/pem.h>
25 #include <vppinfra/lock.h>
27 #include <quic/quic.h>
29 #include <quicly/defaults.h>
30 #include <picotls/openssl.h>
31 #include <picotls/pembase64.h>
33 #include <quic/quic_crypto.h>
35 static quic_main_t quic_main;
36 static void quic_update_timer (quic_ctx_t * ctx);
39 quic_format_err (u8 * s, va_list * args)
41 u64 code = va_arg (*args, u64);
45 s = format (s, "no error");
48 case QUIC_ERROR_FULL_FIFO:
49 s = format (s, "full fifo");
51 case QUIC_APP_ERROR_CLOSE_NOTIFY:
52 s = format (s, "QUIC_APP_ERROR_CLOSE_NOTIFY");
54 case QUIC_APP_ALLOCATION_ERROR:
55 s = format (s, "QUIC_APP_ALLOCATION_ERROR");
57 case QUIC_APP_ACCEPT_NOTIFY_ERROR:
58 s = format (s, "QUIC_APP_ACCEPT_NOTIFY_ERROR");
60 case QUIC_APP_CONNECT_NOTIFY_ERROR:
61 s = format (s, "QUIC_APP_CONNECT_NOTIFY_ERROR");
64 case QUICLY_ERROR_PACKET_IGNORED:
65 s = format (s, "QUICLY_ERROR_PACKET_IGNORED");
67 case QUICLY_ERROR_SENDBUF_FULL:
68 s = format (s, "QUICLY_ERROR_SENDBUF_FULL");
70 case QUICLY_ERROR_FREE_CONNECTION:
71 s = format (s, "QUICLY_ERROR_FREE_CONNECTION");
73 case QUICLY_ERROR_RECEIVED_STATELESS_RESET:
74 s = format (s, "QUICLY_ERROR_RECEIVED_STATELESS_RESET");
76 case QUICLY_TRANSPORT_ERROR_NONE:
77 s = format (s, "QUICLY_TRANSPORT_ERROR_NONE");
79 case QUICLY_TRANSPORT_ERROR_INTERNAL:
80 s = format (s, "QUICLY_TRANSPORT_ERROR_INTERNAL");
82 case QUICLY_TRANSPORT_ERROR_SERVER_BUSY:
83 s = format (s, "QUICLY_TRANSPORT_ERROR_SERVER_BUSY");
85 case QUICLY_TRANSPORT_ERROR_FLOW_CONTROL:
86 s = format (s, "QUICLY_TRANSPORT_ERROR_FLOW_CONTROL");
88 case QUICLY_TRANSPORT_ERROR_STREAM_LIMIT:
89 s = format (s, "QUICLY_TRANSPORT_ERROR_STREAM_LIMIT");
91 case QUICLY_TRANSPORT_ERROR_STREAM_STATE:
92 s = format (s, "QUICLY_TRANSPORT_ERROR_STREAM_STATE");
94 case QUICLY_TRANSPORT_ERROR_FINAL_OFFSET:
95 s = format (s, "QUICLY_TRANSPORT_ERROR_FINAL_OFFSET");
97 case QUICLY_TRANSPORT_ERROR_FRAME_ENCODING:
98 s = format (s, "QUICLY_TRANSPORT_ERROR_FRAME_ENCODING");
100 case QUICLY_TRANSPORT_ERROR_TRANSPORT_PARAMETER:
101 s = format (s, "QUICLY_TRANSPORT_ERROR_TRANSPORT_PARAMETER");
103 case QUICLY_TRANSPORT_ERROR_VERSION_NEGOTIATION:
104 s = format (s, "QUICLY_TRANSPORT_ERROR_VERSION_NEGOTIATION");
106 case QUICLY_TRANSPORT_ERROR_PROTOCOL_VIOLATION:
107 s = format (s, "QUICLY_TRANSPORT_ERROR_PROTOCOL_VIOLATION");
109 case QUICLY_TRANSPORT_ERROR_INVALID_MIGRATION:
110 s = format (s, "QUICLY_TRANSPORT_ERROR_INVALID_MIGRATION");
113 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_CLOSE_NOTIFY):
115 format (s, "PTLS_ALERT_CLOSE_NOTIFY");
117 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_UNEXPECTED_MESSAGE):
119 format (s, "PTLS_ALERT_UNEXPECTED_MESSAGE");
121 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_BAD_RECORD_MAC):
123 format (s, "PTLS_ALERT_BAD_RECORD_MAC");
125 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_HANDSHAKE_FAILURE):
127 format (s, "PTLS_ALERT_HANDSHAKE_FAILURE");
129 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_BAD_CERTIFICATE):
131 format (s, "PTLS_ALERT_BAD_CERTIFICATE");
133 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_CERTIFICATE_REVOKED):
135 format (s, "PTLS_ALERT_CERTIFICATE_REVOKED");
137 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_CERTIFICATE_EXPIRED):
139 format (s, "PTLS_ALERT_CERTIFICATE_EXPIRED");
141 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_CERTIFICATE_UNKNOWN):
143 format (s, "PTLS_ALERT_CERTIFICATE_UNKNOWN");
145 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_ILLEGAL_PARAMETER):
147 format (s, "PTLS_ALERT_ILLEGAL_PARAMETER");
149 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_UNKNOWN_CA):
151 format (s, "PTLS_ALERT_UNKNOWN_CA");
153 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_DECODE_ERROR):
155 format (s, "PTLS_ALERT_DECODE_ERROR");
157 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_DECRYPT_ERROR):
159 format (s, "PTLS_ALERT_DECRYPT_ERROR");
161 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_PROTOCOL_VERSION):
163 format (s, "PTLS_ALERT_PROTOCOL_VERSION");
165 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_INTERNAL_ERROR):
167 format (s, "PTLS_ALERT_INTERNAL_ERROR");
169 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_USER_CANCELED):
171 format (s, "PTLS_ALERT_USER_CANCELED");
173 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_MISSING_EXTENSION):
175 format (s, "PTLS_ALERT_MISSING_EXTENSION");
177 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_UNRECOGNIZED_NAME):
179 format (s, "PTLS_ALERT_UNRECOGNIZED_NAME");
181 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_CERTIFICATE_REQUIRED):
183 format (s, "PTLS_ALERT_CERTIFICATE_REQUIRED");
185 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ALERT_NO_APPLICATION_PROTOCOL):
187 format (s, "PTLS_ALERT_NO_APPLICATION_PROTOCOL");
189 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_NO_MEMORY):
191 format (s, "PTLS_ERROR_NO_MEMORY");
193 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_IN_PROGRESS):
195 format (s, "PTLS_ERROR_IN_PROGRESS");
197 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_LIBRARY):
199 format (s, "PTLS_ERROR_LIBRARY");
201 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCOMPATIBLE_KEY):
203 format (s, "PTLS_ERROR_INCOMPATIBLE_KEY");
205 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_SESSION_NOT_FOUND):
207 format (s, "PTLS_ERROR_SESSION_NOT_FOUND");
209 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_STATELESS_RETRY):
211 format (s, "PTLS_ERROR_STATELESS_RETRY");
213 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_NOT_AVAILABLE):
215 format (s, "PTLS_ERROR_NOT_AVAILABLE");
217 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_COMPRESSION_FAILURE):
219 format (s, "PTLS_ERROR_COMPRESSION_FAILURE");
221 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_BER_INCORRECT_ENCODING):
223 format (s, "PTLS_ERROR_BER_INCORRECT_ENCODING");
225 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_BER_MALFORMED_TYPE):
227 format (s, "PTLS_ERROR_BER_MALFORMED_TYPE");
229 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_BER_MALFORMED_LENGTH):
231 format (s, "PTLS_ERROR_BER_MALFORMED_LENGTH");
233 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_BER_EXCESSIVE_LENGTH):
235 format (s, "PTLS_ERROR_BER_EXCESSIVE_LENGTH");
237 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_BER_ELEMENT_TOO_SHORT):
239 format (s, "PTLS_ERROR_BER_ELEMENT_TOO_SHORT");
241 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_BER_UNEXPECTED_EOC):
243 format (s, "PTLS_ERROR_BER_UNEXPECTED_EOC");
245 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_DER_INDEFINITE_LENGTH):
247 format (s, "PTLS_ERROR_DER_INDEFINITE_LENGTH");
249 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCORRECT_ASN1_SYNTAX):
251 format (s, "PTLS_ERROR_INCORRECT_ASN1_SYNTAX");
253 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCORRECT_PEM_KEY_VERSION):
255 format (s, "PTLS_ERROR_INCORRECT_PEM_KEY_VERSION");
257 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCORRECT_PEM_ECDSA_KEY_VERSION):
259 format (s, "PTLS_ERROR_INCORRECT_PEM_ECDSA_KEY_VERSION");
261 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCORRECT_PEM_ECDSA_CURVE):
263 format (s, "PTLS_ERROR_INCORRECT_PEM_ECDSA_CURVE");
265 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCORRECT_PEM_ECDSA_KEYSIZE):
267 format (s, "PTLS_ERROR_INCORRECT_PEM_ECDSA_KEYSIZE");
269 case QUICLY_TRANSPORT_ERROR_TLS_ALERT_BASE + PTLS_ERROR_TO_ALERT (PTLS_ERROR_INCORRECT_ASN1_ECDSA_KEY_SYNTAX):
271 format (s, "PTLS_ERROR_INCORRECT_ASN1_ECDSA_KEY_SYNTAX");
274 s = format (s, "unknown error 0x%lx", code);
281 quic_ctx_alloc (u32 thread_index)
283 quic_main_t *qm = &quic_main;
286 pool_get (qm->ctx_pool[thread_index], ctx);
288 memset (ctx, 0, sizeof (quic_ctx_t));
289 ctx->c_thread_index = thread_index;
290 QUIC_DBG (1, "Allocated quic_ctx %u on thread %u",
291 ctx - qm->ctx_pool[thread_index], thread_index);
292 return ctx - qm->ctx_pool[thread_index];
296 quic_ctx_free (quic_ctx_t * ctx)
298 QUIC_DBG (2, "Free ctx %u", ctx->c_c_index);
299 u32 thread_index = ctx->c_thread_index;
301 memset (ctx, 0xfb, sizeof (*ctx));
302 pool_put (quic_main.ctx_pool[thread_index], ctx);
306 quic_ctx_get (u32 ctx_index, u32 thread_index)
308 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
312 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
314 if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
316 return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
320 quic_get_conn_ctx (quicly_conn_t * conn)
323 conn_data = (u64) * quicly_get_data (conn);
324 return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
328 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
330 *quicly_get_data (conn) =
331 (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
335 quic_ctx_is_stream (quic_ctx_t * ctx)
337 return (ctx->flags & QUIC_F_IS_STREAM);
341 quic_ctx_is_listener (quic_ctx_t * ctx)
343 return (ctx->flags & QUIC_F_IS_LISTENER);
347 get_stream_session_from_stream (quicly_stream_t * stream)
350 quic_stream_data_t *stream_data;
352 stream_data = (quic_stream_data_t *) stream->data;
353 ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
354 return session_get (ctx->c_s_index, stream_data->thread_index);
358 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
359 const quicly_cid_plaintext_t * id)
361 kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
362 kv->key[1] = id->node_id;
366 quic_sendable_packet_count (session_t * udp_session)
369 u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
370 max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
371 return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
376 quic_ack_rx_data (session_t * stream_session)
381 quicly_stream_t *stream;
382 quic_stream_data_t *stream_data;
385 quic_ctx_get (stream_session->connection_index,
386 stream_session->thread_index);
387 ASSERT (quic_ctx_is_stream (sctx));
388 stream = sctx->c_quic_ctx_id.stream;
389 stream_data = (quic_stream_data_t *) stream->data;
391 f = stream_session->rx_fifo;
392 max_deq = svm_fifo_max_dequeue (f);
394 ASSERT (stream_data->app_rx_data_len >= max_deq);
395 quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
396 QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
397 stream_data->app_rx_data_len = max_deq;
401 quic_disconnect_transport (quic_ctx_t * ctx)
403 QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
404 vnet_disconnect_args_t a = {
405 .handle = ctx->udp_session_handle,
406 .app_index = quic_main.app_index,
409 if (vnet_disconnect_session (&a))
410 clib_warning ("UDP session 0x%lx disconnect errored",
411 ctx->udp_session_handle);
415 quic_connection_delete (quic_ctx_t * ctx)
417 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
418 clib_bihash_kv_16_8_t kv;
421 QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
423 ASSERT (!quic_ctx_is_stream (ctx));
426 if (ctx->timer_handle != QUIC_TIMER_HANDLE_INVALID)
428 tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
429 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
432 /* Delete the connection from the connection map */
433 conn = ctx->c_quic_ctx_id.conn;
434 quic_make_connection_key (&kv, quicly_get_master_id (conn));
435 QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
437 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
439 quic_disconnect_transport (ctx);
441 if (ctx->c_quic_ctx_id.conn)
442 quicly_free (ctx->c_quic_ctx_id.conn);
443 ctx->c_quic_ctx_id.conn = NULL;
445 session_transport_delete_notify (&ctx->connection);
450 * Called when quicly return an error
451 * This function interacts tightly with quic_proto_on_close
454 quic_connection_closed (quic_ctx_t * ctx)
456 QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
459 /* TODO if connection is not established, just delete the session? */
460 /* Actually should send connect or accept error */
462 switch (ctx->c_quic_ctx_id.conn_state)
464 case QUIC_CONN_STATE_READY:
465 /* Error on an opened connection (timeout...)
466 This puts the session in closing state, we should receive a notification
467 when the app has closed its session */
468 session_transport_reset_notify (&ctx->connection);
469 /* This ensures we delete the connection when the app confirms the close */
470 ctx->c_quic_ctx_id.conn_state =
471 QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
473 case QUIC_CONN_STATE_PASSIVE_CLOSING:
474 ctx->c_quic_ctx_id.conn_state =
475 QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
476 /* quic_proto_on_close will eventually be called when the app confirms the close
477 , we delete the connection at that point */
479 case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
480 /* App already confirmed close, we can delete the connection */
481 session_transport_delete_notify (&ctx->connection);
482 quic_connection_delete (ctx);
484 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
487 case QUIC_CONN_STATE_ACTIVE_CLOSING:
488 session_transport_delete_notify (&ctx->connection);
489 quic_connection_delete (ctx);
498 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
501 session_dgram_hdr_t hdr;
504 transport_connection_t *tc;
506 len = packet->data.len;
507 f = udp_session->tx_fifo;
508 tc = session_get_transport (udp_session);
509 max_enqueue = svm_fifo_max_enqueue (f);
510 if (max_enqueue < SESSION_CONN_HDR_LEN + len)
512 QUIC_DBG (1, "Too much data to send, max_enqueue %u, len %u",
513 max_enqueue, len + SESSION_CONN_HDR_LEN);
514 return QUIC_ERROR_FULL_FIFO;
517 /* Build packet header for fifo */
518 hdr.data_length = len;
520 hdr.is_ip4 = tc->is_ip4;
521 clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
522 hdr.lcl_port = tc->lcl_port;
524 /* Read dest address from quicly-provided sockaddr */
527 ASSERT (packet->sa.sa_family == AF_INET);
528 struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->sa;
529 hdr.rmt_port = sa4->sin_port;
530 hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
534 ASSERT (packet->sa.sa_family == AF_INET6);
535 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->sa;
536 hdr.rmt_port = sa6->sin6_port;
537 clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
540 ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
541 if (ret != sizeof (hdr))
543 QUIC_DBG (1, "Not enough space to enqueue header");
544 return QUIC_ERROR_FULL_FIFO;
546 ret = svm_fifo_enqueue (f, len, packet->data.base);
549 QUIC_DBG (1, "Not enough space to enqueue payload");
550 return QUIC_ERROR_FULL_FIFO;
556 quic_send_packets (quic_ctx_t * ctx)
558 quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
559 session_t *udp_session;
561 size_t num_packets, i, max_packets;
562 quicly_context_t *quicly_context;
563 app_worker_t *app_wrk;
567 /* We have sctx, get qctx */
568 if (quic_ctx_is_stream (ctx))
570 quic_ctx_get (ctx->c_quic_ctx_id.quic_connection_ctx_id,
571 ctx->c_thread_index);
573 ASSERT (!quic_ctx_is_stream (ctx));
575 udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
579 conn = ctx->c_quic_ctx_id.conn;
584 /* TODO : quicly can assert it can send min_packets up to 2 */
585 if (quic_sendable_packet_count (udp_session) < 2)
588 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
591 clib_warning ("Tried to send packets on non existing app worker %u",
592 ctx->parent_app_wrk_id);
593 quic_connection_delete (ctx);
596 app = application_get (app_wrk->app_index);
598 quicly_context = (quicly_context_t *) app->quicly_ctx;
601 max_packets = quic_sendable_packet_count (udp_session);
604 num_packets = max_packets;
605 if ((err = quicly_send (conn, packets, &num_packets)))
608 for (i = 0; i != num_packets; ++i)
610 if ((err = quic_send_datagram (udp_session, packets[i])))
613 quicly_context->packet_allocator->
614 free_packet (quicly_context->packet_allocator, packets[i]);
617 while (num_packets > 0 && num_packets == max_packets);
620 if (svm_fifo_set_event (udp_session->tx_fifo))
622 session_send_io_evt_to_thread (udp_session->tx_fifo,
624 clib_warning ("Event enqueue errored %d", err);
626 QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
627 svm_fifo_max_dequeue (udp_session->rx_fifo));
628 quic_update_timer (ctx);
632 if (err && err != QUICLY_ERROR_PACKET_IGNORED
633 && err != QUICLY_ERROR_FREE_CONNECTION)
634 clib_warning ("Quic error '%U'.", quic_format_err, err);
635 quic_connection_closed (ctx);
639 /*****************************************************************************
641 * START QUICLY CALLBACKS
642 * Called from QUIC lib
644 *****************************************************************************/
647 quic_on_stream_destroy (quicly_stream_t * stream, int err)
649 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
651 quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
652 session_t *stream_session =
653 session_get (sctx->c_s_index, sctx->c_thread_index);
654 QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
655 session_handle (stream_session), quic_format_err, err);
657 stream_session->session_state = SESSION_STATE_CLOSED;
658 session_transport_delete_notify (&sctx->connection);
660 quic_ctx_free (sctx);
665 quic_on_stop_sending (quicly_stream_t * stream, int err)
668 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
670 quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
671 session_t *stream_session =
672 session_get (sctx->c_s_index, sctx->c_thread_index);
673 clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
674 session_handle (stream_session), quic_format_err, err);
676 /* TODO : handle STOP_SENDING */
681 quic_on_receive_reset (quicly_stream_t * stream, int err)
683 quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
685 quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
687 session_t *stream_session =
688 session_get (sctx->c_s_index, sctx->c_thread_index);
689 clib_warning ("RESET_STREAM: session 0x%lx (%U)",
690 session_handle (stream_session), quic_format_err, err);
692 session_transport_closing_notify (&sctx->connection);
697 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
700 QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
703 session_t *stream_session;
704 app_worker_t *app_wrk;
706 quic_stream_data_t *stream_data;
709 stream_data = (quic_stream_data_t *) stream->data;
710 sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
711 stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
712 f = stream_session->rx_fifo;
714 max_enq = svm_fifo_max_enqueue_prod (f);
715 QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
716 if (off - stream_data->app_rx_data_len + len > max_enq)
718 QUIC_DBG (1, "Error RX fifo is full");
721 if (off == stream_data->app_rx_data_len)
723 /* Streams live on the same thread so (f, stream_data) should stay consistent */
724 rlen = svm_fifo_enqueue (f, len, (u8 *) src);
725 stream_data->app_rx_data_len += rlen;
726 ASSERT (rlen >= len);
727 app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
728 if (PREDICT_TRUE (app_wrk != 0))
729 app_worker_lock_and_send_event (app_wrk, stream_session,
731 quic_ack_rx_data (stream_session);
736 svm_fifo_enqueue_with_offset (f, off - stream_data->app_rx_data_len,
744 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
746 session_t *stream_session;
750 stream_session = get_stream_session_from_stream (stream);
751 f = stream_session->tx_fifo;
753 rv = svm_fifo_dequeue_drop (f, delta);
754 ASSERT (rv == delta);
755 quicly_stream_sync_sendbuf (stream, 0);
759 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
760 size_t * len, int *wrote_all)
762 session_t *stream_session;
764 u32 deq_max, first_deq, max_rd_chunk, rem_offset;
766 stream_session = get_stream_session_from_stream (stream);
767 f = stream_session->tx_fifo;
769 QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
771 deq_max = svm_fifo_max_dequeue_cons (f);
772 ASSERT (off <= deq_max);
773 if (off + *len < deq_max)
780 *len = deq_max - off;
781 QUIC_DBG (3, "Wrote ALL, %u", *len);
784 /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
785 max_rd_chunk = svm_fifo_max_read_chunk (f);
788 if (off < max_rd_chunk)
790 first_deq = clib_min (*len, max_rd_chunk - off);
791 clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
794 if (max_rd_chunk < off + *len)
796 rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
797 clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
804 static const quicly_stream_callbacks_t quic_stream_callbacks = {
805 .on_destroy = quic_on_stream_destroy,
806 .on_send_shift = quic_fifo_egress_shift,
807 .on_send_emit = quic_fifo_egress_emit,
808 .on_send_stop = quic_on_stop_sending,
809 .on_receive = quic_on_receive,
810 .on_receive_reset = quic_on_receive_reset
814 quic_accept_stream (void *s)
816 quicly_stream_t *stream = (quicly_stream_t *) s;
817 session_t *stream_session, *quic_session;
818 quic_stream_data_t *stream_data;
819 app_worker_t *app_wrk;
820 quic_ctx_t *qctx, *sctx;
824 sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
826 qctx = quic_get_conn_ctx (stream->conn);
828 stream_session = session_alloc (qctx->c_thread_index);
829 QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
830 session_handle (stream_session), sctx_id);
831 sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
832 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
833 sctx->parent_app_id = qctx->parent_app_id;
834 sctx->c_quic_ctx_id.quic_connection_ctx_id = qctx->c_c_index;
835 sctx->c_c_index = sctx_id;
836 sctx->c_s_index = stream_session->session_index;
837 sctx->c_quic_ctx_id.stream = stream;
838 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
839 sctx->flags |= QUIC_F_IS_STREAM;
841 stream_data = (quic_stream_data_t *) stream->data;
842 stream_data->ctx_id = sctx_id;
843 stream_data->thread_index = sctx->c_thread_index;
844 stream_data->app_rx_data_len = 0;
846 sctx->c_s_index = stream_session->session_index;
847 stream_session->session_state = SESSION_STATE_CREATED;
848 stream_session->app_wrk_index = sctx->parent_app_wrk_id;
849 stream_session->connection_index = sctx->c_c_index;
850 stream_session->session_type =
851 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
852 qctx->c_quic_ctx_id.udp_is_ip4);
853 quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
854 stream_session->listener_handle = listen_session_get_handle (quic_session);
856 app_wrk = app_worker_get (stream_session->app_wrk_index);
857 if ((rv = app_worker_init_connected (app_wrk, stream_session)))
859 QUIC_DBG (1, "failed to allocate fifos");
860 session_free (stream_session);
861 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
864 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
865 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
866 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
868 rv = app_worker_accept_notify (app_wrk, stream_session);
871 QUIC_DBG (1, "failed to notify accept worker app");
872 session_free_w_fifos (stream_session);
873 quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
879 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
881 QUIC_DBG (2, "on_stream_open called");
882 stream->data = malloc (sizeof (quic_stream_data_t));
883 stream->callbacks = &quic_stream_callbacks;
884 /* Notify accept on parent qsession, but only if this is not a locally
885 * initiated stream */
886 if (!quicly_stream_is_self_initiated (stream))
888 quic_accept_stream (stream);
894 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
895 int code, uint64_t frame_type,
896 const char *reason, size_t reason_len)
898 quic_ctx_t *ctx = quic_get_conn_ctx (conn);
900 session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
901 clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
902 session_handle (quic_session), quic_format_err, code,
905 ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
906 session_transport_closing_notify (&ctx->connection);
909 static quicly_stream_open_t on_stream_open = { &quic_on_stream_open };
910 static quicly_closed_by_peer_t on_closed_by_peer =
911 { &quic_on_closed_by_peer };
914 /*****************************************************************************
916 * END QUICLY CALLBACKS
918 *****************************************************************************/
920 /*****************************************************************************
922 * BEGIN TIMERS HANDLING
924 *****************************************************************************/
927 quic_get_thread_time (u8 thread_index)
929 return quic_main.wrk_ctx[thread_index].time_now;
933 quic_get_time (quicly_now_t * self)
935 u8 thread_index = vlib_get_thread_index ();
936 return quic_get_thread_time (thread_index);
939 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
942 quic_set_time_now (u32 thread_index)
944 vlib_main_t *vlib_main = vlib_get_main ();
945 f64 time = vlib_time_now (vlib_main);
946 quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
947 return quic_main.wrk_ctx[thread_index].time_now;
950 /* Transport proto callback */
952 quic_update_time (f64 now, u8 thread_index)
954 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
956 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
957 quic_set_time_now (thread_index);
958 tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
962 quic_timer_expired (u32 conn_index)
965 QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
966 quic_get_time (NULL));
967 ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
968 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
969 quic_send_packets (ctx);
973 quic_update_timer (quic_ctx_t * ctx)
975 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
976 int64_t next_timeout, next_interval;
977 session_t *quic_session;
979 /* This timeout is in ms which is the unit of our timer */
980 next_timeout = quicly_get_first_timeout (ctx->c_quic_ctx_id.conn);
981 next_interval = next_timeout - quic_get_time (NULL);
983 if (next_timeout == 0 || next_interval <= 0)
985 if (ctx->c_s_index == QUIC_SESSION_INVALID)
991 quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
992 if (svm_fifo_set_event (quic_session->tx_fifo))
993 session_send_io_evt_to_thread_custom (quic_session,
994 quic_session->thread_index,
995 SESSION_IO_EVT_BUILTIN_TX);
1000 tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
1002 QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
1003 next_interval, ctx->c_c_index);
1005 if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
1007 if (next_timeout == INT64_MAX)
1009 QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
1013 tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index, 0, next_interval);
1017 if (next_timeout == INT64_MAX)
1019 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
1020 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1021 QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
1024 tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
1031 quic_expired_timers_dispatch (u32 * expired_timers)
1035 for (i = 0; i < vec_len (expired_timers); i++)
1037 quic_timer_expired (expired_timers[i]);
1041 /*****************************************************************************
1043 * END TIMERS HANDLING
1045 *****************************************************************************/
1047 /* single-entry session cache */
1048 struct st_util_session_cache_t
1050 ptls_encrypt_ticket_t super;
1056 encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
1057 int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
1059 struct st_util_session_cache_t *self = (void *) _self;
1065 /* replace the cached entry along with a newly generated session id */
1066 free (self->data.base);
1067 if ((self->data.base = malloc (src.len)) == NULL)
1068 return PTLS_ERROR_NO_MEMORY;
1070 ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
1071 memcpy (self->data.base, src.base, src.len);
1072 self->data.len = src.len;
1074 /* store the session id in buffer */
1075 if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
1077 memcpy (dst->base + dst->off, self->id, sizeof (self->id));
1078 dst->off += sizeof (self->id);
1084 /* check if session id is the one stored in cache */
1085 if (src.len != sizeof (self->id))
1086 return PTLS_ERROR_SESSION_NOT_FOUND;
1087 if (memcmp (self->id, src.base, sizeof (self->id)) != 0)
1088 return PTLS_ERROR_SESSION_NOT_FOUND;
1090 /* return the cached value */
1091 if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
1093 memcpy (dst->base + dst->off, self->data.base, self->data.len);
1094 dst->off += self->data.len;
1101 static struct st_util_session_cache_t sc = {
1103 .cb = encrypt_ticket_cb,
1107 static ptls_context_t quic_tlsctx = {
1108 .random_bytes = ptls_openssl_random_bytes,
1109 .get_time = &ptls_get_time,
1110 .key_exchanges = ptls_openssl_key_exchanges,
1111 .cipher_suites = ptls_openssl_cipher_suites,
1117 .on_client_hello = NULL,
1118 .emit_certificate = NULL,
1119 .sign_certificate = NULL,
1120 .verify_certificate = NULL,
1121 .ticket_lifetime = 86400,
1122 .max_early_data_size = 8192,
1123 .hkdf_label_prefix__obsolete = NULL,
1124 .require_dhe_on_psk = 1,
1125 .encrypt_ticket = &sc.super,
1130 ptls_compare_separator_line (const char *line, const char *begin_or_end,
1133 int ret = strncmp (line, "-----", 5);
1134 size_t text_index = 5;
1138 size_t begin_or_end_length = strlen (begin_or_end);
1139 ret = strncmp (line + text_index, begin_or_end, begin_or_end_length);
1140 text_index += begin_or_end_length;
1145 ret = line[text_index] - ' ';
1151 size_t label_length = strlen (label);
1152 ret = strncmp (line + text_index, label, label_length);
1153 text_index += label_length;
1158 ret = strncmp (line + text_index, "-----", 5);
1165 ptls_get_bio_pem_object (BIO * bio, const char *label, ptls_buffer_t * buf)
1167 int ret = PTLS_ERROR_PEM_LABEL_NOT_FOUND;
1169 ptls_base64_decode_state_t state;
1171 /* Get the label on a line by itself */
1172 while (BIO_gets (bio, line, 256))
1174 if (ptls_compare_separator_line (line, "BEGIN", label) == 0)
1177 ptls_base64_decode_init (&state);
1181 /* Get the data in the buffer */
1182 while (ret == 0 && BIO_gets (bio, line, 256))
1184 if (ptls_compare_separator_line (line, "END", label) == 0)
1186 if (state.status == PTLS_BASE64_DECODE_DONE
1187 || (state.status == PTLS_BASE64_DECODE_IN_PROGRESS
1194 ret = PTLS_ERROR_INCORRECT_BASE64;
1200 ret = ptls_base64_decode (line, &state, buf);
1208 ptls_load_bio_pem_objects (BIO * bio, const char *label, ptls_iovec_t * list,
1209 size_t list_max, size_t * nb_objects)
1218 while (count < list_max)
1222 ptls_buffer_init (&buf, "", 0);
1224 ret = ptls_get_bio_pem_object (bio, label, &buf);
1228 if (buf.off > 0 && buf.is_allocated)
1230 list[count].base = buf.base;
1231 list[count].len = buf.off;
1236 ptls_buffer_dispose (&buf);
1241 ptls_buffer_dispose (&buf);
1247 if (ret == PTLS_ERROR_PEM_LABEL_NOT_FOUND && count > 0)
1252 *nb_objects = count;
1257 #define PTLS_MAX_CERTS_IN_CONTEXT 16
1260 ptls_load_bio_certificates (ptls_context_t * ctx, BIO * bio)
1264 ctx->certificates.list =
1265 (ptls_iovec_t *) malloc (PTLS_MAX_CERTS_IN_CONTEXT *
1266 sizeof (ptls_iovec_t));
1268 if (ctx->certificates.list == NULL)
1270 ret = PTLS_ERROR_NO_MEMORY;
1275 ptls_load_bio_pem_objects (bio, "CERTIFICATE", ctx->certificates.list,
1276 PTLS_MAX_CERTS_IN_CONTEXT,
1277 &ctx->certificates.count);
1284 load_bio_certificate_chain (ptls_context_t * ctx, const char *cert_data)
1287 cert_bio = BIO_new_mem_buf (cert_data, -1);
1288 if (ptls_load_bio_certificates (ctx, cert_bio) != 0)
1290 BIO_free (cert_bio);
1291 fprintf (stderr, "failed to load certificate:%s\n", strerror (errno));
1294 BIO_free (cert_bio);
1298 load_bio_private_key (ptls_context_t * ctx, const char *pk_data)
1300 static ptls_openssl_sign_certificate_t sc;
1304 key_bio = BIO_new_mem_buf (pk_data, -1);
1305 pkey = PEM_read_bio_PrivateKey (key_bio, NULL, NULL, NULL);
1310 fprintf (stderr, "failed to read private key from app configuration\n");
1314 ptls_openssl_init_sign_certificate (&sc, pkey);
1315 EVP_PKEY_free (pkey);
1317 ctx->sign_certificate = &sc.super;
1321 allocate_quicly_ctx (application_t * app, u8 is_client)
1328 quicly_context_t *quicly_ctx;
1329 ptls_iovec_t key_vec;
1330 QUIC_DBG (2, "Called allocate_quicly_ctx");
1332 if (app->quicly_ctx)
1334 QUIC_DBG (1, "Trying to reallocate quicly_ctx");
1338 ctx_data = malloc (sizeof (*ctx_data));
1339 quicly_ctx = &ctx_data->_;
1340 app->quicly_ctx = (u64 *) quicly_ctx;
1341 memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
1343 quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
1344 quicly_ctx->tls = &quic_tlsctx;
1345 quicly_ctx->stream_open = &on_stream_open;
1346 quicly_ctx->closed_by_peer = &on_closed_by_peer;
1347 quicly_ctx->now = &quicly_vpp_now_cb;
1349 quicly_amend_ptls_context (quicly_ctx->tls);
1351 quicly_ctx->event_log.mask = 0; /* logs */
1352 quicly_ctx->event_log.cb = quicly_new_default_event_logger (stderr);
1354 quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
1355 quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
1356 quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
1357 quicly_ctx->transport_params.max_stream_data.bidi_local = (QUIC_FIFO_SIZE - 1); /* max_enq is SIZE - 1 */
1358 quicly_ctx->transport_params.max_stream_data.bidi_remote = (QUIC_FIFO_SIZE - 1); /* max_enq is SIZE - 1 */
1359 quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
1361 quicly_ctx->tls->random_bytes (ctx_data->cid_key, 16);
1362 ctx_data->cid_key[16] = 0;
1363 key_vec = ptls_iovec_init (ctx_data->cid_key, strlen (ctx_data->cid_key));
1364 quicly_ctx->cid_encryptor =
1365 quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
1366 &ptls_openssl_sha256, key_vec);
1367 if (!is_client && app->tls_key != NULL && app->tls_cert != NULL)
1369 load_bio_private_key (quicly_ctx->tls, (char *) app->tls_key);
1370 load_bio_certificate_chain (quicly_ctx->tls, (char *) app->tls_cert);
1374 /*****************************************************************************
1376 * BEGIN TRANSPORT PROTO FUNCTIONS
1378 *****************************************************************************/
1381 quic_connect_new_stream (session_t * quic_session, u32 opaque)
1383 uint64_t quic_session_handle;
1384 session_t *stream_session;
1385 quic_stream_data_t *stream_data;
1386 quicly_stream_t *stream;
1387 quicly_conn_t *conn;
1388 app_worker_t *app_wrk;
1389 quic_ctx_t *qctx, *sctx;
1393 /* Find base session to which the user want to attach a stream */
1394 quic_session_handle = session_handle (quic_session);
1395 QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
1397 if (session_type_transport_proto (quic_session->session_type) !=
1398 TRANSPORT_PROTO_QUIC)
1400 QUIC_DBG (1, "received incompatible session");
1404 app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1407 QUIC_DBG (1, "Invalid app worker :(");
1411 sctx_index = quic_ctx_alloc (quic_session->thread_index); /* Allocate before we get pointers */
1412 sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1414 quic_ctx_get (quic_session->connection_index, quic_session->thread_index);
1415 if (quic_ctx_is_stream (qctx))
1417 QUIC_DBG (1, "session is a stream");
1418 quic_ctx_free (sctx);
1422 sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1423 sctx->parent_app_id = qctx->parent_app_id;
1424 sctx->c_quic_ctx_id.quic_connection_ctx_id = qctx->c_c_index;
1425 sctx->c_c_index = sctx_index;
1426 sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1427 sctx->flags |= QUIC_F_IS_STREAM;
1429 conn = qctx->c_quic_ctx_id.conn;
1431 if (!conn || !quicly_connection_is_ready (conn))
1434 if ((rv = quicly_open_stream (conn, &stream, 0 /* uni */ )))
1436 QUIC_DBG (2, "Stream open failed with %d", rv);
1439 sctx->c_quic_ctx_id.stream = stream;
1441 QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1443 stream_session = session_alloc (qctx->c_thread_index);
1444 QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1445 session_handle (stream_session), sctx_index);
1446 stream_session->app_wrk_index = app_wrk->wrk_index;
1447 stream_session->connection_index = sctx_index;
1448 stream_session->listener_handle = quic_session_handle;
1449 stream_session->session_type =
1450 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1451 qctx->c_quic_ctx_id.udp_is_ip4);
1453 sctx->c_s_index = stream_session->session_index;
1455 if (app_worker_init_connected (app_wrk, stream_session))
1457 QUIC_DBG (1, "failed to app_worker_init_connected");
1458 quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
1459 session_free_w_fifos (stream_session);
1460 quic_ctx_free (sctx);
1461 return app_worker_connect_notify (app_wrk, NULL, opaque);
1464 svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1465 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1466 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1468 stream_session->session_state = SESSION_STATE_READY;
1469 if (app_worker_connect_notify (app_wrk, stream_session, opaque))
1471 QUIC_DBG (1, "failed to notify app");
1472 quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1473 session_free_w_fifos (stream_session);
1474 quic_ctx_free (sctx);
1477 stream_data = (quic_stream_data_t *) stream->data;
1478 stream_data->ctx_id = sctx->c_c_index;
1479 stream_data->thread_index = sctx->c_thread_index;
1480 stream_data->app_rx_data_len = 0;
1485 quic_connect_new_connection (session_endpoint_cfg_t * sep)
1487 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
1488 quic_main_t *qm = &quic_main;
1490 app_worker_t *app_wrk;
1495 ctx_index = quic_ctx_alloc (vlib_get_thread_index ());
1496 ctx = quic_ctx_get (ctx_index, vlib_get_thread_index ());
1497 ctx->parent_app_wrk_id = sep->app_wrk_index;
1498 ctx->c_s_index = QUIC_SESSION_INVALID;
1499 ctx->c_c_index = ctx_index;
1500 ctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
1501 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1502 ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_HANDSHAKE;
1503 ctx->c_quic_ctx_id.client_opaque = sep->opaque;
1504 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1507 ctx->c_quic_ctx_id.srv_hostname = format (0, "%v", sep->hostname);
1508 vec_terminate_c_string (ctx->c_quic_ctx_id.srv_hostname);
1512 /* needed by quic for crypto + determining client / server */
1513 ctx->c_quic_ctx_id.srv_hostname =
1514 format (0, "%U", format_ip46_address, &sep->ip, sep->is_ip4);
1517 clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1518 cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1519 cargs->app_index = qm->app_index;
1520 cargs->api_context = ctx_index;
1522 app_wrk = app_worker_get (sep->app_wrk_index);
1523 app = application_get (app_wrk->app_index);
1524 ctx->parent_app_id = app_wrk->app_index;
1525 cargs->sep_ext.ns_index = app->ns_index;
1527 allocate_quicly_ctx (app, 1 /* is client */ );
1529 if ((error = vnet_connect (cargs)))
1536 quic_connect (transport_endpoint_cfg_t * tep)
1538 QUIC_DBG (2, "Called quic_connect");
1539 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1540 session_t *quic_session;
1541 sep = (session_endpoint_cfg_t *) tep;
1543 quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1545 return quic_connect_new_stream (quic_session, sep->opaque);
1547 return quic_connect_new_connection (sep);
1551 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1553 quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1557 session_t *stream_session =
1558 session_get (ctx->c_s_index, ctx->c_thread_index);
1559 clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1561 if (quic_ctx_is_stream (ctx))
1563 quicly_stream_t *stream = ctx->c_quic_ctx_id.stream;
1564 quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1565 quic_send_packets (ctx);
1568 switch (ctx->c_quic_ctx_id.conn_state)
1570 case QUIC_CONN_STATE_READY:
1571 ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1572 quicly_conn_t *conn = ctx->c_quic_ctx_id.conn;
1573 /* Start connection closing. Keep sending packets until quicly_send
1574 returns QUICLY_ERROR_FREE_CONNECTION */
1575 quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1576 /* This also causes all streams to be closed (and the cb called) */
1577 quic_send_packets (ctx);
1579 case QUIC_CONN_STATE_PASSIVE_CLOSING:
1580 ctx->c_quic_ctx_id.conn_state =
1581 QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1582 /* send_packets will eventually return an error, we delete the conn at
1585 case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1586 quic_connection_delete (ctx);
1589 QUIC_DBG (0, "BUG");
1595 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1597 vnet_listen_args_t _bargs, *args = &_bargs;
1598 quic_main_t *qm = &quic_main;
1599 session_handle_t udp_handle;
1600 session_endpoint_cfg_t *sep;
1601 session_t *udp_listen_session;
1602 app_worker_t *app_wrk;
1606 app_listener_t *app_listener;
1608 sep = (session_endpoint_cfg_t *) tep;
1609 app_wrk = app_worker_get (sep->app_wrk_index);
1610 /* We need to call this because we call app_worker_init_connected in
1611 * quic_accept_stream, which assumes the connect segment manager exists */
1612 app_worker_alloc_connects_segment_manager (app_wrk);
1613 app = application_get (app_wrk->app_index);
1614 QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1616 allocate_quicly_ctx (app, 0 /* is_client */ );
1618 sep->transport_proto = TRANSPORT_PROTO_UDPC;
1619 memset (args, 0, sizeof (*args));
1620 args->app_index = qm->app_index;
1621 args->sep_ext = *sep;
1622 args->sep_ext.ns_index = app->ns_index;
1623 if (vnet_listen (args))
1626 lctx_index = quic_ctx_alloc (0);
1627 udp_handle = args->handle;
1628 app_listener = app_listener_get_w_handle (udp_handle);
1629 udp_listen_session = app_listener_get_session (app_listener);
1630 udp_listen_session->opaque = lctx_index;
1632 lctx = quic_ctx_get (lctx_index, 0);
1633 lctx->flags |= QUIC_F_IS_LISTENER;
1635 clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1636 clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1637 lctx->c_rmt_port = args->sep.peer.port;
1638 lctx->c_lcl_port = args->sep.port;
1639 lctx->c_is_ip4 = args->sep.is_ip4;
1640 lctx->c_fib_index = args->sep.fib_index;
1641 lctx->c_proto = TRANSPORT_PROTO_QUIC;
1642 lctx->parent_app_wrk_id = sep->app_wrk_index;
1643 lctx->parent_app_id = app_wrk->app_index;
1644 lctx->udp_session_handle = udp_handle;
1645 lctx->c_s_index = quic_listen_session_index;
1647 QUIC_DBG (2, "Listening UDP session 0x%lx",
1648 session_handle (udp_listen_session));
1649 QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1654 quic_stop_listen (u32 lctx_index)
1656 QUIC_DBG (2, "Called quic_stop_listen");
1658 lctx = quic_ctx_get (lctx_index, 0);
1659 ASSERT (quic_ctx_is_listener (lctx));
1660 vnet_unlisten_args_t a = {
1661 .handle = lctx->udp_session_handle,
1662 .app_index = quic_main.app_index,
1663 .wrk_map_index = 0 /* default wrk */
1665 if (vnet_unlisten (&a))
1666 clib_warning ("unlisten errored");
1668 /* TODO: crypto state cleanup */
1670 quic_ctx_free (lctx);
1674 static transport_connection_t *
1675 quic_connection_get (u32 ctx_index, u32 thread_index)
1678 ctx = quic_ctx_get (ctx_index, thread_index);
1679 return &ctx->connection;
1682 static transport_connection_t *
1683 quic_listener_get (u32 listener_index)
1685 QUIC_DBG (2, "Called quic_listener_get");
1687 ctx = quic_ctx_get (listener_index, 0);
1688 return &ctx->connection;
1692 format_quic_ctx (u8 * s, va_list * args)
1694 quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1695 u32 verbose = va_arg (*args, u32);
1700 str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1702 if (quic_ctx_is_listener (ctx))
1703 str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1704 else if (quic_ctx_is_stream (ctx))
1705 str = format (str, "Stream %ld conn %d",
1706 ctx->c_quic_ctx_id.stream->stream_id,
1707 ctx->c_quic_ctx_id.quic_connection_ctx_id);
1708 else /* connection */
1709 str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1710 ctx->udp_session_handle);
1712 str = format (str, " app %d wrk %d", ctx->parent_app_id,
1713 ctx->parent_app_wrk_id);
1716 s = format (s, "%-50s%-15d", str, ctx->c_quic_ctx_id.conn_state);
1718 s = format (s, "%s\n", str);
1724 format_quic_connection (u8 * s, va_list * args)
1726 u32 qc_index = va_arg (*args, u32);
1727 u32 thread_index = va_arg (*args, u32);
1728 u32 verbose = va_arg (*args, u32);
1729 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1730 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1735 format_quic_half_open (u8 * s, va_list * args)
1737 u32 qc_index = va_arg (*args, u32);
1738 u32 thread_index = va_arg (*args, u32);
1739 quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1741 format (s, "[#%d][Q] half-open app %u", thread_index, ctx->parent_app_id);
1747 format_quic_listener (u8 * s, va_list * args)
1749 u32 tci = va_arg (*args, u32);
1750 u32 thread_index = va_arg (*args, u32);
1751 u32 verbose = va_arg (*args, u32);
1752 quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1753 s = format (s, "%U", format_quic_ctx, ctx, verbose);
1757 /*****************************************************************************
1758 * END TRANSPORT PROTO FUNCTIONS
1760 * START SESSION CALLBACKS
1761 * Called from UDP layer
1762 *****************************************************************************/
1765 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1766 ip46_address_t * addr, u16 port, u8 is_ip4)
1770 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1771 sa4->sin_family = AF_INET;
1772 sa4->sin_port = port;
1773 sa4->sin_addr.s_addr = addr->ip4.as_u32;
1774 *salen = sizeof (struct sockaddr_in);
1778 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1779 sa6->sin6_family = AF_INET6;
1780 sa6->sin6_port = port;
1781 clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1782 *salen = sizeof (struct sockaddr_in6);
1787 quic_on_client_connected (quic_ctx_t * ctx)
1789 session_t *quic_session;
1790 app_worker_t *app_wrk;
1791 u32 ctx_id = ctx->c_c_index;
1792 u32 thread_index = ctx->c_thread_index;
1794 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1797 quic_disconnect_transport (ctx);
1801 quic_session = session_alloc (thread_index);
1803 QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1804 ctx->c_s_index = quic_session->session_index;
1805 quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1806 quic_session->connection_index = ctx->c_c_index;
1807 quic_session->listener_handle = SESSION_INVALID_HANDLE;
1808 quic_session->session_type =
1809 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
1810 ctx->c_quic_ctx_id.udp_is_ip4);
1812 if (app_worker_init_connected (app_wrk, quic_session))
1814 QUIC_DBG (1, "failed to app_worker_init_connected");
1815 quic_proto_on_close (ctx_id, thread_index);
1816 return app_worker_connect_notify (app_wrk, NULL,
1817 ctx->c_quic_ctx_id.client_opaque);
1820 quic_session->session_state = SESSION_STATE_CONNECTING;
1821 if (app_worker_connect_notify
1822 (app_wrk, quic_session, ctx->c_quic_ctx_id.client_opaque))
1824 QUIC_DBG (1, "failed to notify app");
1825 quic_proto_on_close (ctx_id, thread_index);
1829 /* If the app opens a stream in its callback it may invalidate ctx */
1830 ctx = quic_ctx_get (ctx_id, thread_index);
1831 quic_session->session_state = SESSION_STATE_LISTENING;
1837 quic_receive_connection (void *arg)
1839 u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1840 quic_ctx_t *temp_ctx, *new_ctx;
1841 clib_bihash_kv_16_8_t kv;
1842 quicly_conn_t *conn;
1845 new_ctx_id = quic_ctx_alloc (thread_index);
1846 new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1848 QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1852 memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1855 new_ctx->c_thread_index = thread_index;
1856 new_ctx->c_c_index = new_ctx_id;
1858 conn = new_ctx->c_quic_ctx_id.conn;
1859 quic_store_conn_ctx (conn, new_ctx);
1860 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1861 kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1862 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1863 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1864 new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1865 quic_update_timer (new_ctx);
1867 /* Trigger read on this connection ? */
1871 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1873 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1874 quic_ctx_t *ctx, *temp_ctx;
1875 clib_bihash_kv_16_8_t kv;
1876 quicly_conn_t *conn;
1877 u32 thread_index = vlib_get_thread_index ();
1879 QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1881 temp_ctx = malloc (sizeof (quic_ctx_t));
1883 ctx = quic_ctx_get (ctx_index, thread_index);
1885 memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1887 /* Remove from lookup hash, timer wheel and thread-local pool */
1888 conn = ctx->c_quic_ctx_id.conn;
1889 quic_make_connection_key (&kv, quicly_get_master_id (conn));
1890 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
1891 if (ctx->timer_handle != QUIC_TIMER_HANDLE_INVALID)
1893 tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1894 tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
1896 quic_ctx_free (ctx);
1898 /* Send connection to destination thread */
1899 session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1904 quic_transfer_connection_rpc (void *arg)
1906 u64 arg_int = (u64) arg;
1907 u32 ctx_index, dest_thread;
1909 ctx_index = (u32) (arg_int >> 32);
1910 dest_thread = (u32) (arg_int & UINT32_MAX);
1911 quic_transfer_connection (ctx_index, dest_thread);
1915 * This assumes that the connection is not yet associated to a session
1916 * So currently it only works on the client side when receiving the first packet
1920 quic_move_connection_to_thread (u32 ctx_index, u32 owner_thread,
1923 QUIC_DBG (2, "Requesting transfer of conn %u from thread %u", ctx_index,
1925 u64 arg = ((u64) ctx_index) << 32 | to_thread;
1926 session_send_rpc_evt_to_thread (owner_thread, quic_transfer_connection_rpc,
1931 quic_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1932 session_t * udp_session, u8 is_fail)
1934 QUIC_DBG (2, "QSession is now connected (id %u)",
1935 udp_session->session_index);
1936 /* This should always be called before quic_connect returns since UDP always
1937 * connects instantly. */
1938 clib_bihash_kv_16_8_t kv;
1939 struct sockaddr_in6 sa6;
1940 struct sockaddr *sa = (struct sockaddr *) &sa6;
1942 transport_connection_t *tc;
1943 app_worker_t *app_wrk;
1944 quicly_conn_t *conn;
1947 u32 thread_index = vlib_get_thread_index ();
1950 ctx = quic_ctx_get (ctx_index, thread_index);
1956 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1959 api_context = ctx->c_s_index;
1960 app_worker_connect_notify (app_wrk, 0, api_context);
1965 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1968 QUIC_DBG (1, "Appwrk not found");
1971 app = application_get (app_wrk->app_index);
1973 ctx->c_thread_index = thread_index;
1974 ctx->c_c_index = ctx_index;
1976 QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1977 is_fail, thread_index, (ctx) ? ctx_index : ~0);
1979 ctx->udp_session_handle = session_handle (udp_session);
1980 udp_session->opaque = ctx->parent_app_id;
1981 udp_session->session_state = SESSION_STATE_READY;
1983 /* Init QUIC lib connection
1984 * Generate required sockaddr & salen */
1985 tc = session_get_transport (udp_session);
1986 quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1989 quicly_connect (&ctx->c_quic_ctx_id.conn,
1990 (quicly_context_t *) app->quicly_ctx,
1991 (char *) ctx->c_quic_ctx_id.srv_hostname, sa, salen,
1992 &quic_main.next_cid, &quic_main.hs_properties, NULL);
1993 ++quic_main.next_cid.master_id;
1994 /* Save context handle in quicly connection */
1995 quic_store_conn_ctx (ctx->c_quic_ctx_id.conn, ctx);
1998 /* Register connection in connections map */
1999 conn = ctx->c_quic_ctx_id.conn;
2000 quic_make_connection_key (&kv, quicly_get_master_id (conn));
2001 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
2002 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
2003 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
2005 quic_send_packets (ctx);
2007 /* UDP stack quirk? preemptively transfer connection if that happens */
2008 if (udp_session->thread_index != thread_index)
2009 quic_transfer_connection (ctx_index, udp_session->thread_index);
2015 quic_session_disconnect_callback (session_t * s)
2017 clib_warning ("UDP session disconnected???");
2021 quic_session_reset_callback (session_t * s)
2023 clib_warning ("UDP session reset???");
2027 quic_session_accepted_callback (session_t * udp_session)
2029 /* New UDP connection, try to accept it */
2032 quic_ctx_t *ctx, *lctx;
2033 session_t *udp_listen_session;
2034 u32 thread_index = vlib_get_thread_index ();
2036 udp_listen_session =
2037 listen_session_get_from_handle (udp_session->listener_handle);
2039 ctx_index = quic_ctx_alloc (thread_index);
2040 ctx = quic_ctx_get (ctx_index, thread_index);
2041 ctx->c_thread_index = udp_session->thread_index;
2042 ctx->c_c_index = ctx_index;
2043 ctx->c_s_index = QUIC_SESSION_INVALID;
2044 ctx->udp_session_handle = session_handle (udp_session);
2045 QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
2046 ctx->c_quic_ctx_id.listener_ctx_id = udp_listen_session->opaque;
2047 lctx = quic_ctx_get (udp_listen_session->opaque,
2048 udp_listen_session->thread_index);
2049 ctx->c_quic_ctx_id.udp_is_ip4 = lctx->c_is_ip4;
2050 ctx->parent_app_id = lctx->parent_app_id;
2051 ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
2052 ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
2053 ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_OPENED;
2054 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
2056 udp_session->opaque = ctx->parent_app_id;
2058 /* Put this ctx in the "opening" pool */
2059 pool_get (quic_main.wrk_ctx[ctx->c_thread_index].opening_ctx_pool,
2061 *pool_index = ctx_index;
2063 /* TODO timeout to delete these if they never connect */
2068 quic_add_segment_callback (u32 client_index, u64 seg_handle)
2070 QUIC_DBG (2, "Called quic_add_segment_callback");
2071 QUIC_DBG (2, "NOT IMPLEMENTED");
2072 /* No-op for builtin */
2077 quic_del_segment_callback (u32 client_index, u64 seg_handle)
2079 QUIC_DBG (2, "Called quic_del_segment_callback");
2080 QUIC_DBG (2, "NOT IMPLEMENTED");
2081 /* No-op for builtin */
2087 quic_custom_app_rx_callback (transport_connection_t * tc)
2090 session_t *stream_session = session_get (tc->s_index, tc->thread_index);
2091 QUIC_DBG (3, "Received app READ notification");
2092 quic_ack_rx_data (stream_session);
2093 svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
2095 /* Need to send packets (acks may never be sent otherwise) */
2096 ctx = quic_ctx_get (stream_session->connection_index,
2097 stream_session->thread_index);
2098 quic_send_packets (ctx);
2103 quic_custom_tx_callback (void *s, u32 max_burst_size)
2105 session_t *stream_session = (session_t *) s;
2106 quicly_stream_t *stream;
2111 (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
2114 quic_ctx_get (stream_session->connection_index,
2115 stream_session->thread_index);
2116 if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
2118 goto tx_end; /* Most probably a reschedule */
2121 QUIC_DBG (3, "Stream TX event");
2122 quic_ack_rx_data (stream_session);
2123 if (!svm_fifo_max_dequeue (stream_session->tx_fifo))
2126 stream = ctx->c_quic_ctx_id.stream;
2127 if (!quicly_sendstate_is_open (&stream->sendstate))
2129 QUIC_DBG (1, "Warning: tried to send on closed stream");
2133 if ((rv = quicly_stream_sync_sendbuf (stream, 1)) != 0)
2137 quic_send_packets (ctx);
2143 * Returns 0 if a matching connection is found and is on the right thread.
2144 * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
2148 quic_find_packet_ctx (u32 * ctx_thread, u32 * ctx_index,
2149 struct sockaddr *sa, socklen_t salen,
2150 quicly_decoded_packet_t * packet,
2151 u32 caller_thread_index)
2154 quicly_conn_t *conn_;
2155 clib_bihash_kv_16_8_t kv;
2156 clib_bihash_16_8_t *h;
2158 h = &quic_main.connection_hash;
2159 quic_make_connection_key (&kv, &packet->cid.dest.plaintext);
2160 QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
2162 if (clib_bihash_search_16_8 (h, &kv, &kv) == 0)
2164 u32 index = kv.value & UINT32_MAX;
2165 u8 thread_id = kv.value >> 32;
2166 /* Check if this connection belongs to this thread, otherwise
2167 * ask for it to be moved */
2168 if (thread_id != caller_thread_index)
2170 QUIC_DBG (2, "Connection is on wrong thread");
2171 /* Cannot make full check with quicly_is_destination... */
2173 *ctx_thread = thread_id;
2176 ctx_ = quic_ctx_get (index, vlib_get_thread_index ());
2177 conn_ = ctx_->c_quic_ctx_id.conn;
2178 if (conn_ && quicly_is_destination (conn_, sa, salen, packet))
2180 QUIC_DBG (3, "Connection found");
2182 *ctx_thread = thread_id;
2186 QUIC_DBG (3, "connection not found");
2191 quic_receive (quic_ctx_t * ctx, quicly_conn_t * conn,
2192 quicly_decoded_packet_t packet)
2195 u32 ctx_id = ctx->c_c_index;
2196 u32 thread_index = ctx->c_thread_index;
2197 /* TODO : QUICLY_ERROR_PACKET_IGNORED sould be handled */
2198 rv = quicly_receive (conn, &packet);
2201 QUIC_DBG (2, "quicly_receive errored %U", quic_format_err, rv);
2204 /* ctx pointer may change if a new stream is opened */
2205 ctx = quic_ctx_get (ctx_id, thread_index);
2206 /* Conn may be set to null if the connection is terminated */
2207 if (ctx->c_quic_ctx_id.conn
2208 && ctx->c_quic_ctx_id.conn_state == QUIC_CONN_STATE_HANDSHAKE)
2210 if (quicly_connection_is_ready (conn))
2212 ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_READY;
2213 if (quicly_is_client (conn))
2215 quic_on_client_connected (ctx);
2216 ctx = quic_ctx_get (ctx_id, thread_index);
2220 return quic_send_packets (ctx);
2224 quic_create_quic_session (quic_ctx_t * ctx)
2226 session_t *quic_session;
2227 app_worker_t *app_wrk;
2231 quic_session = session_alloc (ctx->c_thread_index);
2232 QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
2233 session_handle (quic_session), ctx->c_c_index);
2234 quic_session->session_state = SESSION_STATE_LISTENING;
2235 ctx->c_s_index = quic_session->session_index;
2237 lctx = quic_ctx_get (ctx->c_quic_ctx_id.listener_ctx_id, 0);
2239 quic_session->app_wrk_index = lctx->parent_app_wrk_id;
2240 quic_session->connection_index = ctx->c_c_index;
2241 quic_session->session_type =
2242 session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC,
2243 ctx->c_quic_ctx_id.udp_is_ip4);
2244 quic_session->listener_handle = lctx->c_s_index;
2246 /* TODO: don't alloc fifos when we don't transfer data on this session
2247 * but we still need fifos for the events? */
2248 if ((rv = app_worker_init_accepted (quic_session)))
2250 QUIC_DBG (1, "failed to allocate fifos");
2251 session_free (quic_session);
2254 app_wrk = app_worker_get (quic_session->app_wrk_index);
2255 rv = app_worker_accept_notify (app_wrk, quic_session);
2258 QUIC_DBG (1, "failed to notify accept worker app");
2265 quic_create_connection (quicly_context_t * quicly_ctx,
2266 u32 ctx_index, struct sockaddr *sa,
2267 socklen_t salen, quicly_decoded_packet_t packet)
2269 clib_bihash_kv_16_8_t kv;
2271 quicly_conn_t *conn;
2272 u32 thread_index = vlib_get_thread_index ();
2275 /* new connection, accept and create context if packet is valid
2276 * TODO: check if socket is actually listening? */
2277 if ((rv = quicly_accept (&conn, quicly_ctx, sa, salen,
2278 &packet, ptls_iovec_init (NULL, 0),
2279 &quic_main.next_cid, NULL)))
2281 /* Invalid packet, pass */
2282 assert (conn == NULL);
2283 QUIC_DBG (1, "Accept failed with %d", rv);
2284 /* TODO: cleanup created quic ctx and UDP session */
2287 assert (conn != NULL);
2289 ++quic_main.next_cid.master_id;
2290 ctx = quic_ctx_get (ctx_index, thread_index);
2291 /* Save ctx handle in quicly connection */
2292 quic_store_conn_ctx (conn, ctx);
2293 ctx->c_quic_ctx_id.conn = conn;
2294 ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_HANDSHAKE;
2296 quic_create_quic_session (ctx);
2298 /* Register connection in connections map */
2299 quic_make_connection_key (&kv, quicly_get_master_id (conn));
2300 kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
2301 clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
2302 QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
2304 return quic_send_packets (ctx);
2308 quic_reset_connection (quicly_context_t * quicly_ctx, u64 udp_session_handle,
2309 struct sockaddr *sa, socklen_t salen,
2310 quicly_decoded_packet_t packet)
2312 /* short header packet; potentially a dead connection. No need to check the
2313 * length of the incoming packet, because loop is prevented by authenticating
2314 * the CID (by checking node_id and thread_id). If the peer is also sending a
2315 * reset, then the next CID is highly likely to contain a non-authenticating
2317 QUIC_DBG (2, "Sending stateless reset");
2319 quicly_datagram_t *dgram;
2320 session_t *udp_session;
2321 if (packet.cid.dest.plaintext.node_id == 0
2322 && packet.cid.dest.plaintext.thread_id == 0)
2324 dgram = quicly_send_stateless_reset (quicly_ctx, sa, salen,
2325 &packet.cid.dest.plaintext);
2328 udp_session = session_get_from_handle (udp_session_handle);
2329 rv = quic_send_datagram (udp_session, dgram);
2330 if (svm_fifo_set_event (udp_session->tx_fifo))
2331 session_send_io_evt_to_thread (udp_session->tx_fifo,
2339 quic_app_rx_callback (session_t * udp_session)
2341 /* Read data from UDP rx_fifo and pass it to the quicly conn. */
2342 quicly_decoded_packet_t packet;
2343 session_dgram_hdr_t ph;
2345 quic_ctx_t *ctx = NULL;
2348 struct sockaddr_in6 sa6;
2349 struct sockaddr *sa = (struct sockaddr *) &sa6;
2351 u32 max_deq, full_len, ctx_index = UINT32_MAX, ctx_thread = UINT32_MAX, ret;
2354 u32 *opening_ctx_pool, *ctx_index_ptr;
2355 u32 app_index = udp_session->opaque;
2356 u64 udp_session_handle = session_handle (udp_session);
2358 u32 thread_index = vlib_get_thread_index ();
2359 app = application_get_if_valid (app_index);
2362 QUIC_DBG (1, "Got RX on detached app");
2363 /* TODO: close this session, cleanup state? */
2369 udp_session = session_get_from_handle (udp_session_handle); /* session alloc might have happened */
2370 f = udp_session->rx_fifo;
2371 max_deq = svm_fifo_max_dequeue (f);
2375 if (max_deq < SESSION_CONN_HDR_LEN)
2377 QUIC_DBG (1, "Not enough data for even a header in RX");
2380 ret = svm_fifo_peek (f, 0, SESSION_CONN_HDR_LEN, (u8 *) & ph);
2381 if (ret != SESSION_CONN_HDR_LEN)
2383 QUIC_DBG (1, "Not enough data for header in RX");
2386 ASSERT (ph.data_offset == 0);
2387 full_len = ph.data_length + SESSION_CONN_HDR_LEN;
2388 if (full_len > max_deq)
2390 QUIC_DBG (1, "Not enough data in fifo RX");
2394 /* Quicly can read len bytes from the fifo at offset:
2395 * ph.data_offset + SESSION_CONN_HDR_LEN */
2396 data = malloc (ph.data_length);
2397 ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN, ph.data_length, data);
2398 if (ret != ph.data_length)
2400 QUIC_DBG (1, "Not enough data peeked in RX");
2406 quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
2407 plen = quicly_decode_packet ((quicly_context_t *) app->quicly_ctx,
2408 &packet, data, ph.data_length);
2410 if (plen != SIZE_MAX)
2413 err = quic_find_packet_ctx (&ctx_thread, &ctx_index, sa, salen,
2414 &packet, thread_index);
2417 ctx = quic_ctx_get (ctx_index, thread_index);
2418 quic_receive (ctx, ctx->c_quic_ctx_id.conn, packet);
2420 else if (ctx_thread != UINT32_MAX)
2422 /* Connection found but on wrong thread, ask move */
2423 quic_move_connection_to_thread (ctx_index, ctx_thread,
2426 else if ((packet.octets.base[0] & QUICLY_PACKET_TYPE_BITMASK) ==
2427 QUICLY_PACKET_TYPE_INITIAL)
2429 /* Try to find matching "opening" ctx */
2431 quic_main.wrk_ctx[thread_index].opening_ctx_pool;
2434 pool_foreach (ctx_index_ptr, opening_ctx_pool,
2436 ctx = quic_ctx_get (*ctx_index_ptr, thread_index);
2437 if (ctx->udp_session_handle == udp_session_handle)
2439 /* Right ctx found, create conn & remove from pool */
2440 quic_create_connection ((quicly_context_t *) app->quicly_ctx,
2441 *ctx_index_ptr, sa, salen, packet);
2442 pool_put (opening_ctx_pool, ctx_index_ptr);
2443 goto ctx_search_done;
2451 quic_reset_connection ((quicly_context_t *) app->quicly_ctx,
2452 udp_session_handle, sa, salen, packet);
2456 svm_fifo_dequeue_drop (f, full_len);
2464 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2465 transport_endpoint_t * tep, u8 is_lcl)
2467 session_t *udp_session;
2468 if (!quic_ctx_is_stream (ctx))
2470 udp_session = session_get_from_handle (ctx->udp_session_handle);
2471 session_get_endpoint (udp_session, tep, is_lcl);
2476 quic_get_transport_listener_endpoint (u32 listener_index,
2477 transport_endpoint_t * tep, u8 is_lcl)
2480 app_listener_t *app_listener;
2481 session_t *udp_listen_session;
2482 ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2483 if (quic_ctx_is_listener (ctx))
2485 app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2486 udp_listen_session = app_listener_get_session (app_listener);
2487 return session_get_endpoint (udp_listen_session, tep, is_lcl);
2489 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2493 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2494 transport_endpoint_t * tep, u8 is_lcl)
2497 ctx = quic_ctx_get (ctx_index, thread_index);
2498 quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2501 /*****************************************************************************
2502 * END TRANSPORT PROTO FUNCTIONS
2503 *****************************************************************************/
2506 static session_cb_vft_t quic_app_cb_vft = {
2507 .session_accept_callback = quic_session_accepted_callback,
2508 .session_disconnect_callback = quic_session_disconnect_callback,
2509 .session_connected_callback = quic_session_connected_callback,
2510 .session_reset_callback = quic_session_reset_callback,
2511 .add_segment_callback = quic_add_segment_callback,
2512 .del_segment_callback = quic_del_segment_callback,
2513 .builtin_app_rx_callback = quic_app_rx_callback,
2516 static const transport_proto_vft_t quic_proto = {
2517 .connect = quic_connect,
2518 .close = quic_proto_on_close,
2519 .start_listen = quic_start_listen,
2520 .stop_listen = quic_stop_listen,
2521 .get_connection = quic_connection_get,
2522 .get_listener = quic_listener_get,
2523 .update_time = quic_update_time,
2524 .app_rx_evt = quic_custom_app_rx_callback,
2525 .custom_tx = quic_custom_tx_callback,
2526 .format_connection = format_quic_connection,
2527 .format_half_open = format_quic_half_open,
2528 .format_listener = format_quic_listener,
2529 .get_transport_endpoint = quic_get_transport_endpoint,
2530 .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2531 .transport_options = {
2532 .tx_type = TRANSPORT_TX_INTERNAL,
2533 .service_type = TRANSPORT_SERVICE_APP,
2538 static clib_error_t *
2539 quic_init (vlib_main_t * vm)
2541 u32 segment_size = 256 << 20;
2542 vlib_thread_main_t *vtm = vlib_get_thread_main ();
2543 tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2544 vnet_app_attach_args_t _a, *a = &_a;
2545 u64 options[APP_OPTIONS_N_OPTIONS];
2546 quic_main_t *qm = &quic_main;
2547 u32 fifo_size = QUIC_FIFO_SIZE;
2550 num_threads = 1 /* main thread */ + vtm->n_threads;
2552 memset (a, 0, sizeof (*a));
2553 memset (options, 0, sizeof (options));
2555 a->session_cb_vft = &quic_app_cb_vft;
2556 a->api_client_index = APP_INVALID_INDEX;
2557 a->options = options;
2558 a->name = format (0, "quic");
2559 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2560 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2561 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
2562 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
2563 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2564 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2565 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2567 if (vnet_application_attach (a))
2569 clib_warning ("failed to attach quic app");
2570 return clib_error_return (0, "failed to attach quic app");
2573 vec_validate (qm->ctx_pool, num_threads - 1);
2574 vec_validate (qm->wrk_ctx, num_threads - 1);
2575 /* Timer wheels, one per thread. */
2576 for (i = 0; i < num_threads; i++)
2578 tw = &qm->wrk_ctx[i].timer_wheel;
2579 tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2580 1e-3 /* timer period 1ms */ , ~0);
2581 tw->last_run_time = vlib_time_now (vlib_get_main ());
2584 clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2588 qm->app_index = a->app_index;
2589 qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2590 / QUIC_TSTAMP_RESOLUTION;
2592 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2593 FIB_PROTOCOL_IP4, ~0);
2594 transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2595 FIB_PROTOCOL_IP6, ~0);
2601 VLIB_INIT_FUNCTION (quic_init);
2603 static clib_error_t *
2604 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2605 unformat_input_t * input,
2606 vlib_cli_command_t * cmd)
2608 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2610 if (unformat (input, "vpp"))
2612 quic_tlsctx.cipher_suites = vpp_crypto_cipher_suites;
2615 else if (unformat (input, "picotls"))
2617 quic_tlsctx.cipher_suites = ptls_openssl_cipher_suites;
2621 return clib_error_return (0, "unknown input '%U'",
2622 format_unformat_error, input);
2625 return clib_error_return (0, "unknown input '%U'",
2626 format_unformat_error, input);
2630 VLIB_CLI_COMMAND(quic_plugin_crypto_command, static)=
2632 .path = "quic set crypto api",
2633 .short_help = "quic set crypto api [picotls, vpp]",
2634 .function = quic_plugin_crypto_command_fn,
2637 VLIB_PLUGIN_REGISTER () =
2639 .version = VPP_BUILD_VER,
2640 .description = "Quic transport protocol",
2645 * fd.io coding-style-patch-verification: ON
2648 * eval: (c-set-style "gnu")