session: fix listener global endpoint lookup
[vpp.git] / src / plugins / quic / quic.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <sys/socket.h>
17
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
24 #include <vppinfra/lock.h>
25
26 #include <quic/quic.h>
27 #include <quic/certs.h>
28 #include <quic/error.h>
29 #include <quic/quic_crypto.h>
30
31 #include <quicly/constants.h>
32 #include <quicly/defaults.h>
33
34 static char *quic_error_strings[] = {
35 #define quic_error(n,s) s,
36 #include <quic/quic_error.def>
37 #undef quic_error
38 };
39
40 static quic_main_t quic_main;
41 static void quic_update_timer (quic_ctx_t * ctx);
42 static void quic_check_quic_session_connected (quic_ctx_t * ctx);
43 static int quic_reset_connection (u64 udp_session_handle,
44                                   quic_rx_packet_ctx_t * pctx);
45 static void quic_proto_on_close (u32 ctx_index, u32 thread_index);
46
47 static quicly_stream_open_t on_stream_open;
48 static quicly_closed_by_peer_t on_closed_by_peer;
49 static quicly_now_t quicly_vpp_now_cb;
50
51 /* Crypto contexts */
52
53 static inline void
54 quic_crypto_context_make_key_from_ctx (clib_bihash_kv_24_8_t * kv,
55                                        quic_ctx_t * ctx)
56 {
57   application_t *app = application_get (ctx->parent_app_id);
58   kv->key[0] = ((u64) ctx->ckpair_index) << 32 | (u64) ctx->crypto_engine;
59   kv->key[1] = app->sm_properties.rx_fifo_size - 1;
60   kv->key[2] = app->sm_properties.tx_fifo_size - 1;
61 }
62
63 static inline void
64 quic_crypto_context_make_key_from_crctx (clib_bihash_kv_24_8_t * kv,
65                                          crypto_context_t * crctx)
66 {
67   quic_crypto_context_data_t *data =
68     (quic_crypto_context_data_t *) crctx->data;
69   kv->key[0] = ((u64) crctx->ckpair_index) << 32 | (u64) crctx->crypto_engine;
70   kv->key[1] = data->quicly_ctx.transport_params.max_stream_data.bidi_local;
71   kv->key[2] = data->quicly_ctx.transport_params.max_stream_data.bidi_remote;
72 }
73
74 static void
75 quic_crypto_context_free_if_needed (crypto_context_t * crctx, u8 thread_index)
76 {
77   quic_main_t *qm = &quic_main;
78   clib_bihash_kv_24_8_t kv;
79   if (crctx->n_subscribers)
80     return;
81   quic_crypto_context_make_key_from_crctx (&kv, crctx);
82   clib_bihash_add_del_24_8 (&qm->wrk_ctx[thread_index].crypto_context_hash,
83                             &kv, 0 /* is_add */ );
84   clib_mem_free (crctx->data);
85   pool_put (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
86 }
87
88 static int
89 quic_app_cert_key_pair_delete_callback (app_cert_key_pair_t * ckpair)
90 {
91   quic_main_t *qm = &quic_main;
92   crypto_context_t *crctx;
93   clib_bihash_kv_24_8_t kv;
94   vlib_thread_main_t *vtm = vlib_get_thread_main ();
95   int num_threads = 1 /* main thread */  + vtm->n_threads;
96   int i;
97
98   for (i = 0; i < num_threads; i++)
99     {
100       /* *INDENT-OFF* */
101       pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
102         if (crctx->ckpair_index == ckpair->cert_key_index)
103           {
104             quic_crypto_context_make_key_from_crctx (&kv, crctx);
105             clib_bihash_add_del_24_8 (&qm->wrk_ctx[i].crypto_context_hash, &kv, 0 /* is_add */ );
106           }
107       }));
108       /* *INDENT-ON* */
109     }
110   return 0;
111 }
112
113 static crypto_context_t *
114 quic_crypto_context_alloc (u8 thread_index)
115 {
116   quic_main_t *qm = &quic_main;
117   crypto_context_t *crctx;
118   u32 idx;
119
120   pool_get (qm->wrk_ctx[thread_index].crypto_ctx_pool, crctx);
121   clib_memset (crctx, 0, sizeof (*crctx));
122   idx = (crctx - qm->wrk_ctx[thread_index].crypto_ctx_pool);
123   crctx->ctx_index = ((u32) thread_index) << 24 | idx;
124
125   return crctx;
126 }
127
128 static crypto_context_t *
129 quic_crypto_context_get (u32 cr_index, u32 thread_index)
130 {
131   quic_main_t *qm = &quic_main;
132   ASSERT (cr_index >> 24 == thread_index);
133   return pool_elt_at_index (qm->wrk_ctx[thread_index].crypto_ctx_pool,
134                             cr_index & 0x00ffffff);
135 }
136
137 static clib_error_t *
138 quic_list_crypto_context_command_fn (vlib_main_t * vm,
139                                      unformat_input_t * input,
140                                      vlib_cli_command_t * cmd)
141 {
142   quic_main_t *qm = &quic_main;
143   crypto_context_t *crctx;
144   vlib_thread_main_t *vtm = vlib_get_thread_main ();
145   int i, num_threads = 1 /* main thread */  + vtm->n_threads;
146   for (i = 0; i < num_threads; i++)
147     {
148       /* *INDENT-OFF* */
149       pool_foreach (crctx, qm->wrk_ctx[i].crypto_ctx_pool, ({
150         vlib_cli_output (vm, "[%d][Q]%U", i, format_crypto_context, crctx);
151       }));
152       /* *INDENT-ON* */
153     }
154   return 0;
155 }
156
157 static void
158 quic_release_crypto_context (u32 crypto_context_index, u8 thread_index)
159 {
160   crypto_context_t *crctx;
161   crctx = quic_crypto_context_get (crypto_context_index, thread_index);
162   crctx->n_subscribers--;
163   quic_crypto_context_free_if_needed (crctx, thread_index);
164 }
165
166 static int
167 quic_init_crypto_context (crypto_context_t * crctx, quic_ctx_t * ctx)
168 {
169   quic_main_t *qm = &quic_main;
170   quicly_context_t *quicly_ctx;
171   ptls_iovec_t key_vec;
172   app_cert_key_pair_t *ckpair;
173   application_t *app;
174   quic_crypto_context_data_t *data;
175   ptls_context_t *ptls_ctx;
176
177   QUIC_DBG (2, "Init quic crctx %d thread %d", crctx->ctx_index,
178             ctx->c_thread_index);
179
180   data = clib_mem_alloc (sizeof (*data));
181   /* picotls depends on data being zeroed */
182   clib_memset (data, 0, sizeof (*data));
183   crctx->data = (void *) data;
184   quicly_ctx = &data->quicly_ctx;
185   ptls_ctx = &data->ptls_ctx;
186
187   ptls_ctx->random_bytes = ptls_openssl_random_bytes;
188   ptls_ctx->get_time = &ptls_get_time;
189   ptls_ctx->key_exchanges = ptls_openssl_key_exchanges;
190   ptls_ctx->cipher_suites = qm->quic_ciphers[ctx->crypto_engine];
191   ptls_ctx->certificates.list = NULL;
192   ptls_ctx->certificates.count = 0;
193   ptls_ctx->esni = NULL;
194   ptls_ctx->on_client_hello = NULL;
195   ptls_ctx->emit_certificate = NULL;
196   ptls_ctx->sign_certificate = NULL;
197   ptls_ctx->verify_certificate = NULL;
198   ptls_ctx->ticket_lifetime = 86400;
199   ptls_ctx->max_early_data_size = 8192;
200   ptls_ctx->hkdf_label_prefix__obsolete = NULL;
201   ptls_ctx->require_dhe_on_psk = 1;
202   ptls_ctx->encrypt_ticket = &qm->session_cache.super;
203   clib_memcpy (quicly_ctx, &quicly_spec_context, sizeof (quicly_context_t));
204
205   quicly_ctx->max_packet_size = QUIC_MAX_PACKET_SIZE;
206   quicly_ctx->tls = ptls_ctx;
207   quicly_ctx->stream_open = &on_stream_open;
208   quicly_ctx->closed_by_peer = &on_closed_by_peer;
209   quicly_ctx->now = &quicly_vpp_now_cb;
210   quicly_amend_ptls_context (quicly_ctx->tls);
211
212   quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
213   quicly_ctx->transport_params.max_streams_uni = (uint64_t) 1 << 60;
214   quicly_ctx->transport_params.max_streams_bidi = (uint64_t) 1 << 60;
215   quicly_ctx->transport_params.idle_timeout = qm->connection_timeout;
216
217   app = application_get (ctx->parent_app_id);
218   quicly_ctx->transport_params.max_stream_data.bidi_local =
219     app->sm_properties.rx_fifo_size - 1;
220   quicly_ctx->transport_params.max_stream_data.bidi_remote =
221     app->sm_properties.tx_fifo_size - 1;
222   quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
223
224   if (!app->quic_iv_set)
225     {
226       ptls_openssl_random_bytes (app->quic_iv, QUIC_IV_LEN - 1);
227       app->quic_iv[QUIC_IV_LEN - 1] = 0;
228       app->quic_iv_set = 1;
229     }
230
231   clib_memcpy (data->cid_key, app->quic_iv, QUIC_IV_LEN);
232   key_vec = ptls_iovec_init (data->cid_key, QUIC_IV_LEN);
233   quicly_ctx->cid_encryptor =
234     quicly_new_default_cid_encryptor (&ptls_openssl_bfecb,
235                                       &ptls_openssl_aes128ecb,
236                                       &ptls_openssl_sha256, key_vec);
237
238   ckpair = app_cert_key_pair_get_if_valid (crctx->ckpair_index);
239   if (!ckpair || !ckpair->key || !ckpair->cert)
240     {
241       QUIC_DBG (1, "Wrong ckpair id %d\n", crctx->ckpair_index);
242       return -1;
243     }
244   if (load_bio_private_key (quicly_ctx->tls, (char *) ckpair->key))
245     {
246       QUIC_DBG (1, "failed to read private key from app configuration\n");
247       return -1;
248     }
249   if (load_bio_certificate_chain (quicly_ctx->tls, (char *) ckpair->cert))
250     {
251       QUIC_DBG (1, "failed to load certificate\n");
252       return -1;
253     }
254   return 0;
255
256 }
257
258 static int
259 quic_acquire_crypto_context (quic_ctx_t * ctx)
260 {
261   quic_main_t *qm = &quic_main;
262   crypto_context_t *crctx;
263   clib_bihash_kv_24_8_t kv;
264
265   if (ctx->crypto_engine == CRYPTO_ENGINE_NONE)
266     {
267       QUIC_DBG (2, "No crypto engine specified, using %d",
268                 qm->default_crypto_engine);
269       ctx->crypto_engine = qm->default_crypto_engine;
270     }
271   if (!clib_bitmap_get (qm->available_crypto_engines, ctx->crypto_engine))
272     {
273       QUIC_DBG (1, "Quic does not support crypto engine %d",
274                 ctx->crypto_engine);
275       return VNET_API_ERROR_MISSING_CERT_KEY;
276     }
277
278   /* Check for exisiting crypto ctx */
279   quic_crypto_context_make_key_from_ctx (&kv, ctx);
280   if (clib_bihash_search_24_8
281       (&qm->wrk_ctx[ctx->c_thread_index].crypto_context_hash, &kv, &kv) == 0)
282     {
283       crctx = quic_crypto_context_get (kv.value, ctx->c_thread_index);
284       QUIC_DBG (2, "Found exisiting crypto context %d", kv.value);
285       ctx->crypto_context_index = kv.value;
286       crctx->n_subscribers++;
287       return 0;
288     }
289
290   crctx = quic_crypto_context_alloc (ctx->c_thread_index);
291   ctx->crypto_context_index = crctx->ctx_index;
292   kv.value = crctx->ctx_index;
293   crctx->crypto_engine = ctx->crypto_engine;
294   crctx->ckpair_index = ctx->ckpair_index;
295   if (quic_init_crypto_context (crctx, ctx))
296     goto error;
297   if (vnet_app_add_cert_key_interest (ctx->ckpair_index, qm->app_index))
298     goto error;
299   crctx->n_subscribers++;
300   clib_bihash_add_del_24_8 (&qm->
301                             wrk_ctx[ctx->c_thread_index].crypto_context_hash,
302                             &kv, 1 /* is_add */ );
303   return 0;
304
305 error:
306   quic_crypto_context_free_if_needed (crctx, ctx->c_thread_index);
307   return VNET_API_ERROR_MISSING_CERT_KEY;
308 }
309
310 /*  Helper functions */
311
312 static u32
313 quic_ctx_alloc (u32 thread_index)
314 {
315   quic_main_t *qm = &quic_main;
316   quic_ctx_t *ctx;
317
318   pool_get (qm->ctx_pool[thread_index], ctx);
319
320   clib_memset (ctx, 0, sizeof (quic_ctx_t));
321   ctx->c_thread_index = thread_index;
322   ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
323   QUIC_DBG (3, "Allocated quic_ctx %u on thread %u",
324             ctx - qm->ctx_pool[thread_index], thread_index);
325   return ctx - qm->ctx_pool[thread_index];
326 }
327
328 static void
329 quic_ctx_free (quic_ctx_t * ctx)
330 {
331   QUIC_DBG (2, "Free ctx %u %x", ctx->c_thread_index, ctx->c_c_index);
332   u32 thread_index = ctx->c_thread_index;
333   QUIC_ASSERT (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID);
334   if (CLIB_DEBUG)
335     clib_memset (ctx, 0xfb, sizeof (*ctx));
336   pool_put (quic_main.ctx_pool[thread_index], ctx);
337 }
338
339 static quic_ctx_t *
340 quic_ctx_get (u32 ctx_index, u32 thread_index)
341 {
342   return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
343 }
344
345 static quic_ctx_t *
346 quic_ctx_get_if_valid (u32 ctx_index, u32 thread_index)
347 {
348   if (pool_is_free_index (quic_main.ctx_pool[thread_index], ctx_index))
349     return 0;
350   return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
351 }
352
353 static quic_ctx_t *
354 quic_get_conn_ctx (quicly_conn_t * conn)
355 {
356   u64 conn_data;
357   conn_data = (u64) * quicly_get_data (conn);
358   return quic_ctx_get (conn_data & UINT32_MAX, conn_data >> 32);
359 }
360
361 static void
362 quic_store_conn_ctx (quicly_conn_t * conn, quic_ctx_t * ctx)
363 {
364   *quicly_get_data (conn) =
365     (void *) (((u64) ctx->c_thread_index) << 32 | (u64) ctx->c_c_index);
366 }
367
368 static inline int
369 quic_ctx_is_stream (quic_ctx_t * ctx)
370 {
371   return (ctx->flags & QUIC_F_IS_STREAM);
372 }
373
374 static inline int
375 quic_ctx_is_listener (quic_ctx_t * ctx)
376 {
377   return (ctx->flags & QUIC_F_IS_LISTENER);
378 }
379
380 static inline int
381 quic_ctx_is_conn (quic_ctx_t * ctx)
382 {
383   return !(quic_ctx_is_listener (ctx) || quic_ctx_is_stream (ctx));
384 }
385
386 static session_t *
387 get_stream_session_from_stream (quicly_stream_t * stream)
388 {
389   quic_ctx_t *ctx;
390   quic_stream_data_t *stream_data;
391
392   stream_data = (quic_stream_data_t *) stream->data;
393   ctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
394   return session_get (ctx->c_s_index, stream_data->thread_index);
395 }
396
397 static inline void
398 quic_make_connection_key (clib_bihash_kv_16_8_t * kv,
399                           const quicly_cid_plaintext_t * id)
400 {
401   kv->key[0] = ((u64) id->master_id) << 32 | (u64) id->thread_id;
402   kv->key[1] = id->node_id;
403 }
404
405 static int
406 quic_sendable_packet_count (session_t * udp_session)
407 {
408   u32 max_enqueue;
409   u32 packet_size = QUIC_MAX_PACKET_SIZE + SESSION_CONN_HDR_LEN;
410   max_enqueue = svm_fifo_max_enqueue (udp_session->tx_fifo);
411   return clib_min (max_enqueue / packet_size, QUIC_SEND_PACKET_VEC_SIZE);
412 }
413
414 static quicly_context_t *
415 quic_get_quicly_ctx_from_ctx (quic_ctx_t * ctx)
416 {
417   crypto_context_t *crctx =
418     quic_crypto_context_get (ctx->crypto_context_index, ctx->c_thread_index);
419   quic_crypto_context_data_t *data =
420     (quic_crypto_context_data_t *) crctx->data;
421   return &data->quicly_ctx;
422 }
423
424 static quicly_context_t *
425 quic_get_quicly_ctx_from_udp (u64 udp_session_handle)
426 {
427   session_t *udp_session = session_get_from_handle (udp_session_handle);
428   quic_ctx_t *ctx =
429     quic_ctx_get (udp_session->opaque, udp_session->thread_index);
430   return quic_get_quicly_ctx_from_ctx (ctx);
431 }
432
433 static inline void
434 quic_set_udp_tx_evt (session_t * udp_session)
435 {
436   int rv = 0;
437   if (svm_fifo_set_event (udp_session->tx_fifo))
438     rv = session_send_io_evt_to_thread (udp_session->tx_fifo,
439                                         SESSION_IO_EVT_TX);
440   if (PREDICT_FALSE (rv))
441     clib_warning ("Event enqueue errored %d", rv);
442 }
443
444 static inline void
445 quic_stop_ctx_timer (quic_ctx_t * ctx)
446 {
447   tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
448   if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
449     return;
450   tw = &quic_main.wrk_ctx[ctx->c_thread_index].timer_wheel;
451   tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->timer_handle);
452   ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
453   QUIC_DBG (4, "Stopping timer for ctx %u", ctx->c_c_index);
454 }
455
456 /* QUIC protocol actions */
457
458 static void
459 quic_ack_rx_data (session_t * stream_session)
460 {
461   u32 max_deq;
462   quic_ctx_t *sctx;
463   svm_fifo_t *f;
464   quicly_stream_t *stream;
465   quic_stream_data_t *stream_data;
466
467   sctx = quic_ctx_get (stream_session->connection_index,
468                        stream_session->thread_index);
469   QUIC_ASSERT (quic_ctx_is_stream (sctx));
470   stream = sctx->stream;
471   stream_data = (quic_stream_data_t *) stream->data;
472
473   f = stream_session->rx_fifo;
474   max_deq = svm_fifo_max_dequeue (f);
475
476   QUIC_ASSERT (stream_data->app_rx_data_len >= max_deq);
477   quicly_stream_sync_recvbuf (stream, stream_data->app_rx_data_len - max_deq);
478   QUIC_DBG (3, "Acking %u bytes", stream_data->app_rx_data_len - max_deq);
479   stream_data->app_rx_data_len = max_deq;
480 }
481
482 static void
483 quic_disconnect_transport (quic_ctx_t * ctx)
484 {
485   QUIC_DBG (2, "Disconnecting transport 0x%lx", ctx->udp_session_handle);
486   vnet_disconnect_args_t a = {
487     .handle = ctx->udp_session_handle,
488     .app_index = quic_main.app_index,
489   };
490
491   if (vnet_disconnect_session (&a))
492     clib_warning ("UDP session 0x%lx disconnect errored",
493                   ctx->udp_session_handle);
494 }
495
496 static void
497 quic_connection_delete (quic_ctx_t * ctx)
498 {
499   clib_bihash_kv_16_8_t kv;
500   quicly_conn_t *conn;
501
502   QUIC_DBG (2, "Deleting connection %u", ctx->c_c_index);
503
504   QUIC_ASSERT (!quic_ctx_is_stream (ctx));
505   quic_stop_ctx_timer (ctx);
506
507   /*  Delete the connection from the connection map */
508   conn = ctx->conn;
509   ctx->conn = NULL;
510   quic_make_connection_key (&kv, quicly_get_master_id (conn));
511   QUIC_DBG (2, "Deleting conn with id %lu %lu from map", kv.key[0],
512             kv.key[1]);
513   clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 0 /* is_add */ );
514
515   quic_disconnect_transport (ctx);
516
517   if (ctx->conn)
518     quicly_free (ctx->conn);
519   session_transport_delete_notify (&ctx->connection);
520 }
521
522 void
523 quic_increment_counter (u8 evt, u8 val)
524 {
525   vlib_main_t *vm = vlib_get_main ();
526   vlib_node_increment_counter (vm, quic_input_node.index, evt, val);
527 }
528
529 /**
530  * Called when quicly return an error
531  * This function interacts tightly with quic_proto_on_close
532  */
533 static void
534 quic_connection_closed (quic_ctx_t * ctx)
535 {
536   QUIC_DBG (2, "QUIC connection %u/%u closed", ctx->c_thread_index,
537             ctx->c_c_index);
538
539   /* TODO if connection is not established, just delete the session? */
540   /* Actually should send connect or accept error */
541
542   switch (ctx->conn_state)
543     {
544     case QUIC_CONN_STATE_READY:
545       /* Error on an opened connection (timeout...)
546          This puts the session in closing state, we should receive a notification
547          when the app has closed its session */
548       session_transport_reset_notify (&ctx->connection);
549       /* This ensures we delete the connection when the app confirms the close */
550       ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
551       break;
552     case QUIC_CONN_STATE_PASSIVE_CLOSING:
553       ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED;
554       /* quic_proto_on_close will eventually be called when the app confirms the close
555          , we delete the connection at that point */
556       break;
557     case QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED:
558       /* App already confirmed close, we can delete the connection */
559       quic_connection_delete (ctx);
560       break;
561     case QUIC_CONN_STATE_OPENED:
562     case QUIC_CONN_STATE_HANDSHAKE:
563     case QUIC_CONN_STATE_ACTIVE_CLOSING:
564       quic_connection_delete (ctx);
565       break;
566     default:
567       QUIC_DBG (0, "BUG %d", ctx->conn_state);
568       break;
569     }
570 }
571
572 static int
573 quic_send_datagram (session_t * udp_session, quicly_datagram_t * packet)
574 {
575   u32 max_enqueue;
576   session_dgram_hdr_t hdr;
577   u32 len, ret;
578   svm_fifo_t *f;
579   transport_connection_t *tc;
580
581   len = packet->data.len;
582   f = udp_session->tx_fifo;
583   tc = session_get_transport (udp_session);
584   max_enqueue = svm_fifo_max_enqueue (f);
585   if (max_enqueue < SESSION_CONN_HDR_LEN + len)
586     {
587       QUIC_ERR ("Too much data to send, max_enqueue %u, len %u",
588                 max_enqueue, len + SESSION_CONN_HDR_LEN);
589       return QUIC_ERROR_FULL_FIFO;
590     }
591
592   /*  Build packet header for fifo */
593   hdr.data_length = len;
594   hdr.data_offset = 0;
595   hdr.is_ip4 = tc->is_ip4;
596   clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
597   hdr.lcl_port = tc->lcl_port;
598
599   /*  Read dest address from quicly-provided sockaddr */
600   if (hdr.is_ip4)
601     {
602       QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET);
603       struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->dest.sa;
604       hdr.rmt_port = sa4->sin_port;
605       hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
606     }
607   else
608     {
609       QUIC_ASSERT (packet->dest.sa.sa_family == AF_INET6);
610       struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->dest.sa;
611       hdr.rmt_port = sa6->sin6_port;
612       clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
613     }
614
615   ret = svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
616   if (ret != sizeof (hdr))
617     {
618       QUIC_ERR ("Not enough space to enqueue header");
619       return QUIC_ERROR_FULL_FIFO;
620     }
621   ret = svm_fifo_enqueue (f, len, packet->data.base);
622   if (ret != len)
623     {
624       QUIC_ERR ("Not enough space to enqueue payload");
625       return QUIC_ERROR_FULL_FIFO;
626     }
627
628   quic_increment_counter (QUIC_ERROR_TX_PACKETS, 1);
629
630   return 0;
631 }
632
633 static int
634 quic_send_packets (quic_ctx_t * ctx)
635 {
636   quicly_datagram_t *packets[QUIC_SEND_PACKET_VEC_SIZE];
637   session_t *udp_session;
638   quicly_conn_t *conn;
639   size_t num_packets, i, max_packets;
640   quicly_packet_allocator_t *pa;
641   int err = 0;
642
643   /* We have sctx, get qctx */
644   if (quic_ctx_is_stream (ctx))
645     ctx = quic_ctx_get (ctx->quic_connection_ctx_id, ctx->c_thread_index);
646
647   QUIC_ASSERT (!quic_ctx_is_stream (ctx));
648
649   udp_session = session_get_from_handle_if_valid (ctx->udp_session_handle);
650   if (!udp_session)
651     goto quicly_error;
652
653   conn = ctx->conn;
654
655   if (!conn)
656     return 0;
657
658   /* TODO : quicly can assert it can send min_packets up to 2 */
659   if (quic_sendable_packet_count (udp_session) < 2)
660     goto stop_sending;
661
662   pa = quic_get_quicly_ctx_from_ctx (ctx)->packet_allocator;
663   do
664     {
665       max_packets = quic_sendable_packet_count (udp_session);
666       if (max_packets < 2)
667         break;
668       num_packets = max_packets;
669       if ((err = quicly_send (conn, packets, &num_packets)))
670         goto quicly_error;
671
672       for (i = 0; i != num_packets; ++i)
673         {
674           if ((err = quic_send_datagram (udp_session, packets[i])))
675             goto quicly_error;
676
677           pa->free_packet (pa, packets[i]);
678         }
679     }
680   while (num_packets > 0 && num_packets == max_packets);
681
682 stop_sending:
683   quic_set_udp_tx_evt (udp_session);
684
685   QUIC_DBG (3, "%u[TX] %u[RX]", svm_fifo_max_dequeue (udp_session->tx_fifo),
686             svm_fifo_max_dequeue (udp_session->rx_fifo));
687   quic_update_timer (ctx);
688   return 0;
689
690 quicly_error:
691   if (err && err != QUICLY_ERROR_PACKET_IGNORED
692       && err != QUICLY_ERROR_FREE_CONNECTION)
693     clib_warning ("Quic error '%U'.", quic_format_err, err);
694   quic_connection_closed (ctx);
695   return 1;
696 }
697
698 /* Quicly callbacks */
699
700 static void
701 quic_on_stream_destroy (quicly_stream_t * stream, int err)
702 {
703   quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
704   quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
705                                    stream_data->thread_index);
706   session_t *stream_session = session_get (sctx->c_s_index,
707                                            sctx->c_thread_index);
708   QUIC_DBG (2, "DESTROYED_STREAM: session 0x%lx (%U)",
709             session_handle (stream_session), quic_format_err, err);
710
711   stream_session->session_state = SESSION_STATE_CLOSED;
712   session_transport_delete_notify (&sctx->connection);
713
714   quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
715   quic_ctx_free (sctx);
716   clib_mem_free (stream->data);
717 }
718
719 static int
720 quic_on_stop_sending (quicly_stream_t * stream, int err)
721 {
722 #if QUIC_DEBUG >= 2
723   quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
724   quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
725                                    stream_data->thread_index);
726   session_t *stream_session = session_get (sctx->c_s_index,
727                                            sctx->c_thread_index);
728   clib_warning ("(NOT IMPLEMENTD) STOP_SENDING: session 0x%lx (%U)",
729                 session_handle (stream_session), quic_format_err, err);
730 #endif
731   /* TODO : handle STOP_SENDING */
732   return 0;
733 }
734
735 static int
736 quic_on_receive_reset (quicly_stream_t * stream, int err)
737 {
738   quic_stream_data_t *stream_data = (quic_stream_data_t *) stream->data;
739   quic_ctx_t *sctx = quic_ctx_get (stream_data->ctx_id,
740                                    stream_data->thread_index);
741 #if QUIC_DEBUG >= 2
742   session_t *stream_session = session_get (sctx->c_s_index,
743                                            sctx->c_thread_index);
744   clib_warning ("RESET_STREAM: session 0x%lx (%U)",
745                 session_handle (stream_session), quic_format_err, err);
746 #endif
747   session_transport_closing_notify (&sctx->connection);
748   return 0;
749 }
750
751 static int
752 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
753                  size_t len)
754 {
755   QUIC_DBG (3, "received data: %lu bytes, offset %lu", len, off);
756   u32 max_enq, rlen, rv;
757   quic_ctx_t *sctx;
758   session_t *stream_session;
759   app_worker_t *app_wrk;
760   svm_fifo_t *f;
761   quic_stream_data_t *stream_data;
762
763   stream_data = (quic_stream_data_t *) stream->data;
764   sctx = quic_ctx_get (stream_data->ctx_id, stream_data->thread_index);
765   stream_session = session_get (sctx->c_s_index, stream_data->thread_index);
766   f = stream_session->rx_fifo;
767
768   max_enq = svm_fifo_max_enqueue_prod (f);
769   QUIC_DBG (3, "Enqueuing %u at off %u in %u space", len, off, max_enq);
770   /* Handle duplicate packet/chunk from quicly */
771   if (off < stream_data->app_rx_data_len)
772     {
773       QUIC_DBG (3, "Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
774                 "DUPLICATE PACKET (max_enq %u, len %u, "
775                 "app_rx_data_len %u, off %u, ToBeNQ %u)",
776                 stream_session->session_index,
777                 stream_session->app_wrk_index,
778                 stream_session->thread_index, f,
779                 max_enq, len, stream_data->app_rx_data_len, off,
780                 off - stream_data->app_rx_data_len + len);
781       return 0;
782     }
783   if (PREDICT_FALSE ((off - stream_data->app_rx_data_len + len) > max_enq))
784     {
785       QUIC_ERR ("Session [idx %u, app_wrk %u, thread %u, rx-fifo 0x%llx]: "
786                 "RX FIFO IS FULL (max_enq %u, len %u, "
787                 "app_rx_data_len %u, off %u, ToBeNQ %u)",
788                 stream_session->session_index,
789                 stream_session->app_wrk_index,
790                 stream_session->thread_index, f,
791                 max_enq, len, stream_data->app_rx_data_len, off,
792                 off - stream_data->app_rx_data_len + len);
793       return 1;
794     }
795   if (off == stream_data->app_rx_data_len)
796     {
797       /* Streams live on the same thread so (f, stream_data) should stay consistent */
798       rlen = svm_fifo_enqueue (f, len, (u8 *) src);
799       QUIC_DBG (3, "Session [idx %u, app_wrk %u, ti %u, rx-fifo 0x%llx]: "
800                 "Enqueuing %u (rlen %u) at off %u in %u space, ",
801                 stream_session->session_index,
802                 stream_session->app_wrk_index,
803                 stream_session->thread_index, f, len, rlen, off, max_enq);
804       stream_data->app_rx_data_len += rlen;
805       QUIC_ASSERT (rlen >= len);
806       app_wrk = app_worker_get_if_valid (stream_session->app_wrk_index);
807       if (PREDICT_TRUE (app_wrk != 0))
808         {
809           rv = app_worker_lock_and_send_event (app_wrk, stream_session,
810                                                SESSION_IO_EVT_RX);
811           if (rv)
812             QUIC_ERR ("Failed to ping app for RX");
813         }
814       quic_ack_rx_data (stream_session);
815     }
816   else
817     {
818       rlen = svm_fifo_enqueue_with_offset (f,
819                                            off - stream_data->app_rx_data_len,
820                                            len, (u8 *) src);
821       QUIC_ASSERT (rlen == 0);
822     }
823   return 0;
824 }
825
826 void
827 quic_fifo_egress_shift (quicly_stream_t * stream, size_t delta)
828 {
829   quic_stream_data_t *stream_data;
830   session_t *stream_session;
831   svm_fifo_t *f;
832   u32 rv;
833
834   stream_data = (quic_stream_data_t *) stream->data;
835   stream_session = get_stream_session_from_stream (stream);
836   f = stream_session->tx_fifo;
837
838   QUIC_ASSERT (stream_data->app_tx_data_len >= delta);
839   stream_data->app_tx_data_len -= delta;
840   rv = svm_fifo_dequeue_drop (f, delta);
841   QUIC_ASSERT (rv == delta);
842
843   rv = quicly_stream_sync_sendbuf (stream, 0);
844   QUIC_ASSERT (!rv);
845 }
846
847 int
848 quic_fifo_egress_emit (quicly_stream_t * stream, size_t off, void *dst,
849                        size_t * len, int *wrote_all)
850 {
851   u32 deq_max, first_deq, max_rd_chunk, rem_offset;
852   quic_stream_data_t *stream_data;
853   session_t *stream_session;
854   svm_fifo_t *f;
855
856   stream_data = (quic_stream_data_t *) stream->data;
857   stream_session = get_stream_session_from_stream (stream);
858   f = stream_session->tx_fifo;
859
860   QUIC_DBG (3, "Emitting %u, offset %u", *len, off);
861
862   deq_max = svm_fifo_max_dequeue_cons (f);
863   QUIC_ASSERT (off <= deq_max);
864   if (off + *len < deq_max)
865     {
866       *wrote_all = 0;
867     }
868   else
869     {
870       *wrote_all = 1;
871       *len = deq_max - off;
872     }
873   QUIC_ASSERT (*len > 0);
874
875   if (off + *len > stream_data->app_tx_data_len)
876     stream_data->app_tx_data_len = off + *len;
877
878   /* TODO, use something like : return svm_fifo_peek (f, off, *len, dst); */
879   max_rd_chunk = svm_fifo_max_read_chunk (f);
880
881   first_deq = 0;
882   if (off < max_rd_chunk)
883     {
884       first_deq = clib_min (*len, max_rd_chunk - off);
885       clib_memcpy_fast (dst, svm_fifo_head (f) + off, first_deq);
886     }
887
888   if (max_rd_chunk < off + *len)
889     {
890       rem_offset = max_rd_chunk < off ? off - max_rd_chunk : 0;
891       clib_memcpy_fast (dst + first_deq, f->head_chunk->data + rem_offset,
892                         *len - first_deq);
893     }
894
895   return 0;
896 }
897
898 static const quicly_stream_callbacks_t quic_stream_callbacks = {
899   .on_destroy = quic_on_stream_destroy,
900   .on_send_shift = quic_fifo_egress_shift,
901   .on_send_emit = quic_fifo_egress_emit,
902   .on_send_stop = quic_on_stop_sending,
903   .on_receive = quic_on_receive,
904   .on_receive_reset = quic_on_receive_reset
905 };
906
907 static int
908 quic_on_stream_open (quicly_stream_open_t * self, quicly_stream_t * stream)
909 {
910   /* Return code for this function ends either
911    * - in quicly_receive : if not QUICLY_ERROR_PACKET_IGNORED, will close connection
912    * - in quicly_open_stream, returned directly
913    */
914
915   session_t *stream_session, *quic_session;
916   quic_stream_data_t *stream_data;
917   app_worker_t *app_wrk;
918   quic_ctx_t *qctx, *sctx;
919   u32 sctx_id;
920   int rv;
921
922   QUIC_DBG (2, "on_stream_open called");
923   stream->data = clib_mem_alloc (sizeof (quic_stream_data_t));
924   stream->callbacks = &quic_stream_callbacks;
925   /* Notify accept on parent qsession, but only if this is not a locally
926    * initiated stream */
927   if (quicly_stream_is_self_initiated (stream))
928     return 0;
929
930   sctx_id = quic_ctx_alloc (vlib_get_thread_index ());
931   qctx = quic_get_conn_ctx (stream->conn);
932
933   /* Might need to signal that the connection is ready if the first thing the
934    * server does is open a stream */
935   quic_check_quic_session_connected (qctx);
936   /* ctx might be invalidated */
937   qctx = quic_get_conn_ctx (stream->conn);
938
939   stream_session = session_alloc (qctx->c_thread_index);
940   QUIC_DBG (2, "ACCEPTED stream_session 0x%lx ctx %u",
941             session_handle (stream_session), sctx_id);
942   sctx = quic_ctx_get (sctx_id, qctx->c_thread_index);
943   sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
944   sctx->parent_app_id = qctx->parent_app_id;
945   sctx->quic_connection_ctx_id = qctx->c_c_index;
946   sctx->c_c_index = sctx_id;
947   sctx->c_s_index = stream_session->session_index;
948   sctx->stream = stream;
949   sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
950   sctx->flags |= QUIC_F_IS_STREAM;
951   if (quicly_stream_is_unidirectional (stream->stream_id))
952     stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
953
954   stream_data = (quic_stream_data_t *) stream->data;
955   stream_data->ctx_id = sctx_id;
956   stream_data->thread_index = sctx->c_thread_index;
957   stream_data->app_rx_data_len = 0;
958   stream_data->app_tx_data_len = 0;
959
960   sctx->c_s_index = stream_session->session_index;
961   stream_session->session_state = SESSION_STATE_CREATED;
962   stream_session->app_wrk_index = sctx->parent_app_wrk_id;
963   stream_session->connection_index = sctx->c_c_index;
964   stream_session->session_type =
965     session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
966   quic_session = session_get (qctx->c_s_index, qctx->c_thread_index);
967   stream_session->listener_handle = listen_session_get_handle (quic_session);
968
969   app_wrk = app_worker_get (stream_session->app_wrk_index);
970   if ((rv = app_worker_init_connected (app_wrk, stream_session)))
971     {
972       QUIC_ERR ("failed to allocate fifos");
973       quicly_reset_stream (stream, QUIC_APP_ALLOCATION_ERROR);
974       return 0;                 /* Frame is still valid */
975     }
976   svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
977                              SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
978                              SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
979
980   if ((rv = app_worker_accept_notify (app_wrk, stream_session)))
981     {
982       QUIC_ERR ("failed to notify accept worker app");
983       quicly_reset_stream (stream, QUIC_APP_ACCEPT_NOTIFY_ERROR);
984       return 0;                 /* Frame is still valid */
985     }
986
987   return 0;
988 }
989
990 static void
991 quic_on_closed_by_peer (quicly_closed_by_peer_t * self, quicly_conn_t * conn,
992                         int code, uint64_t frame_type,
993                         const char *reason, size_t reason_len)
994 {
995   quic_ctx_t *ctx = quic_get_conn_ctx (conn);
996 #if QUIC_DEBUG >= 2
997   session_t *quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
998   clib_warning ("Session 0x%lx closed by peer (%U) %.*s ",
999                 session_handle (quic_session), quic_format_err, code,
1000                 reason_len, reason);
1001 #endif
1002   ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING;
1003   session_transport_closing_notify (&ctx->connection);
1004 }
1005
1006 /* Timer handling */
1007
1008 static int64_t
1009 quic_get_thread_time (u8 thread_index)
1010 {
1011   return quic_main.wrk_ctx[thread_index].time_now;
1012 }
1013
1014 static int64_t
1015 quic_get_time (quicly_now_t * self)
1016 {
1017   u8 thread_index = vlib_get_thread_index ();
1018   return quic_get_thread_time (thread_index);
1019 }
1020
1021 static u32
1022 quic_set_time_now (u32 thread_index)
1023 {
1024   vlib_main_t *vlib_main = vlib_get_main ();
1025   f64 time = vlib_time_now (vlib_main);
1026   quic_main.wrk_ctx[thread_index].time_now = (int64_t) (time * 1000.f);
1027   return quic_main.wrk_ctx[thread_index].time_now;
1028 }
1029
1030 /* Transport proto callback */
1031 static void
1032 quic_update_time (f64 now, u8 thread_index)
1033 {
1034   tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1035
1036   tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
1037   quic_set_time_now (thread_index);
1038   tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
1039 }
1040
1041 static void
1042 quic_timer_expired (u32 conn_index)
1043 {
1044   quic_ctx_t *ctx;
1045   QUIC_DBG (4, "Timer expired for conn %u at %ld", conn_index,
1046             quic_get_time (NULL));
1047   ctx = quic_ctx_get (conn_index, vlib_get_thread_index ());
1048   ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1049   quic_send_packets (ctx);
1050 }
1051
1052 static void
1053 quic_update_timer (quic_ctx_t * ctx)
1054 {
1055   tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1056   int64_t next_timeout, next_interval;
1057   session_t *quic_session;
1058   int rv;
1059
1060   /*  This timeout is in ms which is the unit of our timer */
1061   next_timeout = quicly_get_first_timeout (ctx->conn);
1062   next_interval = next_timeout - quic_get_time (NULL);
1063
1064   if (next_timeout == 0 || next_interval <= 0)
1065     {
1066       if (ctx->c_s_index == QUIC_SESSION_INVALID)
1067         {
1068           next_interval = 1;
1069         }
1070       else
1071         {
1072           quic_session = session_get (ctx->c_s_index, ctx->c_thread_index);
1073           if (svm_fifo_set_event (quic_session->tx_fifo))
1074             {
1075               rv = session_send_io_evt_to_thread_custom (quic_session,
1076                                                          quic_session->thread_index,
1077                                                          SESSION_IO_EVT_BUILTIN_TX);
1078               if (PREDICT_FALSE (rv))
1079                 QUIC_ERR ("Failed to enqueue builtin_tx %d", rv);
1080             }
1081           return;
1082         }
1083     }
1084
1085   tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
1086
1087   QUIC_DBG (4, "Timer set to %ld (int %ld) for ctx %u", next_timeout,
1088             next_interval, ctx->c_c_index);
1089
1090   if (ctx->timer_handle == QUIC_TIMER_HANDLE_INVALID)
1091     {
1092       if (next_timeout == INT64_MAX)
1093         {
1094           QUIC_DBG (4, "timer for ctx %u already stopped", ctx->c_c_index);
1095           return;
1096         }
1097       ctx->timer_handle = tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index,
1098                                                           0, next_interval);
1099     }
1100   else
1101     {
1102       if (next_timeout == INT64_MAX)
1103         {
1104           quic_stop_ctx_timer (ctx);
1105         }
1106       else
1107         tw_timer_update_1t_3w_1024sl_ov (tw, ctx->timer_handle,
1108                                          next_interval);
1109     }
1110   return;
1111 }
1112
1113 static void
1114 quic_expired_timers_dispatch (u32 * expired_timers)
1115 {
1116   int i;
1117
1118   for (i = 0; i < vec_len (expired_timers); i++)
1119     {
1120       quic_timer_expired (expired_timers[i]);
1121     }
1122 }
1123
1124 /* Transport proto functions */
1125
1126 static int
1127 quic_connect_stream (session_t * quic_session, session_endpoint_cfg_t * sep)
1128 {
1129   uint64_t quic_session_handle;
1130   session_t *stream_session;
1131   quic_stream_data_t *stream_data;
1132   quicly_stream_t *stream;
1133   quicly_conn_t *conn;
1134   app_worker_t *app_wrk;
1135   quic_ctx_t *qctx, *sctx;
1136   u32 sctx_index;
1137   int rv;
1138
1139   /*  Find base session to which the user want to attach a stream */
1140   quic_session_handle = session_handle (quic_session);
1141   QUIC_DBG (2, "Opening new stream (qsession %u)", quic_session_handle);
1142
1143   if (session_type_transport_proto (quic_session->session_type) !=
1144       TRANSPORT_PROTO_QUIC)
1145     {
1146       QUIC_ERR ("received incompatible session");
1147       return -1;
1148     }
1149
1150   app_wrk = app_worker_get_if_valid (quic_session->app_wrk_index);
1151   if (!app_wrk)
1152     {
1153       QUIC_ERR ("Invalid app worker :(");
1154       return -1;
1155     }
1156
1157   sctx_index = quic_ctx_alloc (quic_session->thread_index);     /*  Allocate before we get pointers */
1158   sctx = quic_ctx_get (sctx_index, quic_session->thread_index);
1159   qctx = quic_ctx_get (quic_session->connection_index,
1160                        quic_session->thread_index);
1161   if (quic_ctx_is_stream (qctx))
1162     {
1163       QUIC_ERR ("session is a stream");
1164       quic_ctx_free (sctx);
1165       return -1;
1166     }
1167
1168   sctx->parent_app_wrk_id = qctx->parent_app_wrk_id;
1169   sctx->parent_app_id = qctx->parent_app_id;
1170   sctx->quic_connection_ctx_id = qctx->c_c_index;
1171   sctx->c_c_index = sctx_index;
1172   sctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1173   sctx->flags |= QUIC_F_IS_STREAM;
1174
1175   conn = qctx->conn;
1176
1177   if (!conn || !quicly_connection_is_ready (conn))
1178     return -1;
1179
1180   if ((rv =
1181        quicly_open_stream (conn, &stream,
1182                            sep->flags & SESSION_F_UNIDIRECTIONAL)))
1183     {
1184       QUIC_DBG (2, "Stream open failed with %d", rv);
1185       return -1;
1186     }
1187   quic_increment_counter (QUIC_ERROR_OPENED_STREAM, 1);
1188
1189   sctx->stream = stream;
1190
1191   QUIC_DBG (2, "Opened stream %d, creating session", stream->stream_id);
1192
1193   stream_session = session_alloc (qctx->c_thread_index);
1194   QUIC_DBG (2, "Allocated stream_session 0x%lx ctx %u",
1195             session_handle (stream_session), sctx_index);
1196   stream_session->app_wrk_index = app_wrk->wrk_index;
1197   stream_session->connection_index = sctx_index;
1198   stream_session->listener_handle = quic_session_handle;
1199   stream_session->session_type =
1200     session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, qctx->udp_is_ip4);
1201   if (sep->flags & SESSION_F_UNIDIRECTIONAL)
1202     stream_session->flags |= SESSION_F_UNIDIRECTIONAL;
1203
1204   sctx->c_s_index = stream_session->session_index;
1205   stream_data = (quic_stream_data_t *) stream->data;
1206   stream_data->ctx_id = sctx->c_c_index;
1207   stream_data->thread_index = sctx->c_thread_index;
1208   stream_data->app_rx_data_len = 0;
1209   stream_data->app_tx_data_len = 0;
1210   stream_session->session_state = SESSION_STATE_READY;
1211
1212   /* For now we only reset streams. Cleanup will be triggered by timers */
1213   if (app_worker_init_connected (app_wrk, stream_session))
1214     {
1215       QUIC_ERR ("failed to app_worker_init_connected");
1216       quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1217       return app_worker_connect_notify (app_wrk, NULL, sep->opaque);
1218     }
1219
1220   svm_fifo_add_want_deq_ntf (stream_session->rx_fifo,
1221                              SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL |
1222                              SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY);
1223
1224   if (app_worker_connect_notify (app_wrk, stream_session, sep->opaque))
1225     {
1226       QUIC_ERR ("failed to notify app");
1227       quic_increment_counter (QUIC_ERROR_CLOSED_STREAM, 1);
1228       quicly_reset_stream (stream, QUIC_APP_CONNECT_NOTIFY_ERROR);
1229       return -1;
1230     }
1231
1232   return 0;
1233 }
1234
1235 static int
1236 quic_connect_connection (session_endpoint_cfg_t * sep)
1237 {
1238   vnet_connect_args_t _cargs, *cargs = &_cargs;
1239   quic_main_t *qm = &quic_main;
1240   quic_ctx_t *ctx;
1241   app_worker_t *app_wrk;
1242   application_t *app;
1243   u32 ctx_index;
1244   u32 thread_index = vlib_get_thread_index ();
1245   int error;
1246
1247   clib_memset (cargs, 0, sizeof (*cargs));
1248   ctx_index = quic_ctx_alloc (thread_index);
1249   ctx = quic_ctx_get (ctx_index, thread_index);
1250   ctx->parent_app_wrk_id = sep->app_wrk_index;
1251   ctx->c_s_index = QUIC_SESSION_INVALID;
1252   ctx->c_c_index = ctx_index;
1253   ctx->udp_is_ip4 = sep->is_ip4;
1254   ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1255   ctx->conn_state = QUIC_CONN_STATE_HANDSHAKE;
1256   ctx->client_opaque = sep->opaque;
1257   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1258   if (sep->hostname)
1259     ctx->srv_hostname = format (0, "%v", sep->hostname);
1260   else
1261     /*  needed by quic for crypto + determining client / server */
1262     ctx->srv_hostname = format (0, "%U", format_ip46_address,
1263                                 &sep->ip, sep->is_ip4);
1264   vec_terminate_c_string (ctx->srv_hostname);
1265
1266   clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
1267   cargs->sep.transport_proto = TRANSPORT_PROTO_UDPC;
1268   cargs->app_index = qm->app_index;
1269   cargs->api_context = ctx_index;
1270
1271   app_wrk = app_worker_get (sep->app_wrk_index);
1272   app = application_get (app_wrk->app_index);
1273   ctx->parent_app_id = app_wrk->app_index;
1274   cargs->sep_ext.ns_index = app->ns_index;
1275
1276   ctx->crypto_engine = sep->crypto_engine;
1277   ctx->ckpair_index = sep->ckpair_index;
1278   if ((error = quic_acquire_crypto_context (ctx)))
1279     return error;
1280
1281   if ((error = vnet_connect (cargs)))
1282     return error;
1283
1284   return 0;
1285 }
1286
1287 static int
1288 quic_connect (transport_endpoint_cfg_t * tep)
1289 {
1290   QUIC_DBG (2, "Called quic_connect");
1291   session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
1292   session_t *quic_session;
1293   sep = (session_endpoint_cfg_t *) tep;
1294
1295   quic_session = session_get_from_handle_if_valid (sep->parent_handle);
1296   if (quic_session)
1297     return quic_connect_stream (quic_session, sep);
1298   else
1299     return quic_connect_connection (sep);
1300 }
1301
1302 static void
1303 quic_proto_on_close (u32 ctx_index, u32 thread_index)
1304 {
1305   quic_ctx_t *ctx = quic_ctx_get_if_valid (ctx_index, thread_index);
1306   if (!ctx)
1307     return;
1308 #if QUIC_DEBUG >= 2
1309   session_t *stream_session = session_get (ctx->c_s_index,
1310                                            ctx->c_thread_index);
1311   clib_warning ("Closing session 0x%lx", session_handle (stream_session));
1312 #endif
1313   if (quic_ctx_is_stream (ctx))
1314     {
1315       quicly_stream_t *stream = ctx->stream;
1316       if (!quicly_stream_has_send_side (quicly_is_client (stream->conn),
1317                                         stream->stream_id))
1318         return;
1319       quicly_reset_stream (stream, QUIC_APP_ERROR_CLOSE_NOTIFY);
1320       quic_send_packets (ctx);
1321       return;
1322     }
1323
1324   switch (ctx->conn_state)
1325     {
1326     case QUIC_CONN_STATE_OPENED:
1327     case QUIC_CONN_STATE_HANDSHAKE:
1328     case QUIC_CONN_STATE_READY:
1329       ctx->conn_state = QUIC_CONN_STATE_ACTIVE_CLOSING;
1330       quicly_conn_t *conn = ctx->conn;
1331       /* Start connection closing. Keep sending packets until quicly_send
1332          returns QUICLY_ERROR_FREE_CONNECTION */
1333
1334       quic_increment_counter (QUIC_ERROR_CLOSED_CONNECTION, 1);
1335       quicly_close (conn, QUIC_APP_ERROR_CLOSE_NOTIFY, "Closed by peer");
1336       /* This also causes all streams to be closed (and the cb called) */
1337       quic_send_packets (ctx);
1338       break;
1339     case QUIC_CONN_STATE_PASSIVE_CLOSING:
1340       ctx->conn_state = QUIC_CONN_STATE_PASSIVE_CLOSING_APP_CLOSED;
1341       /* send_packets will eventually return an error, we delete the conn at
1342          that point */
1343       break;
1344     case QUIC_CONN_STATE_PASSIVE_CLOSING_QUIC_CLOSED:
1345       quic_connection_delete (ctx);
1346       break;
1347     case QUIC_CONN_STATE_ACTIVE_CLOSING:
1348       break;
1349     default:
1350       QUIC_ERR ("Trying to close conn in state %d", ctx->conn_state);
1351       break;
1352     }
1353 }
1354
1355 static u32
1356 quic_start_listen (u32 quic_listen_session_index, transport_endpoint_t * tep)
1357 {
1358   vnet_listen_args_t _bargs, *args = &_bargs;
1359   quic_main_t *qm = &quic_main;
1360   session_handle_t udp_handle;
1361   session_endpoint_cfg_t *sep;
1362   session_t *udp_listen_session;
1363   app_worker_t *app_wrk;
1364   application_t *app;
1365   quic_ctx_t *lctx;
1366   u32 lctx_index;
1367   app_listener_t *app_listener;
1368   int rv;
1369
1370   sep = (session_endpoint_cfg_t *) tep;
1371   app_wrk = app_worker_get (sep->app_wrk_index);
1372   /* We need to call this because we call app_worker_init_connected in
1373    * quic_accept_stream, which assumes the connect segment manager exists */
1374   app_worker_alloc_connects_segment_manager (app_wrk);
1375   app = application_get (app_wrk->app_index);
1376   QUIC_DBG (2, "Called quic_start_listen for app %d", app_wrk->app_index);
1377
1378   clib_memset (args, 0, sizeof (*args));
1379   args->app_index = qm->app_index;
1380   args->sep_ext = *sep;
1381   args->sep_ext.ns_index = app->ns_index;
1382   args->sep_ext.transport_proto = TRANSPORT_PROTO_UDPC;
1383   if ((rv = vnet_listen (args)))
1384     return rv;
1385
1386   lctx_index = quic_ctx_alloc (0);
1387   udp_handle = args->handle;
1388   app_listener = app_listener_get_w_handle (udp_handle);
1389   udp_listen_session = app_listener_get_session (app_listener);
1390   udp_listen_session->opaque = lctx_index;
1391
1392   lctx = quic_ctx_get (lctx_index, 0);
1393   lctx->flags |= QUIC_F_IS_LISTENER;
1394
1395   clib_memcpy (&lctx->c_rmt_ip, &args->sep.peer.ip, sizeof (ip46_address_t));
1396   clib_memcpy (&lctx->c_lcl_ip, &args->sep.ip, sizeof (ip46_address_t));
1397   lctx->c_rmt_port = args->sep.peer.port;
1398   lctx->c_lcl_port = args->sep.port;
1399   lctx->c_is_ip4 = args->sep.is_ip4;
1400   lctx->c_fib_index = args->sep.fib_index;
1401   lctx->c_proto = TRANSPORT_PROTO_QUIC;
1402   lctx->parent_app_wrk_id = sep->app_wrk_index;
1403   lctx->parent_app_id = app_wrk->app_index;
1404   lctx->udp_session_handle = udp_handle;
1405   lctx->c_s_index = quic_listen_session_index;
1406   lctx->crypto_engine = sep->crypto_engine;
1407   lctx->ckpair_index = sep->ckpair_index;
1408   if (quic_acquire_crypto_context (lctx))
1409     return -1;
1410
1411   QUIC_DBG (2, "Listening UDP session 0x%lx",
1412             session_handle (udp_listen_session));
1413   QUIC_DBG (2, "Listening QUIC session 0x%lx", quic_listen_session_index);
1414   return lctx_index;
1415 }
1416
1417 static u32
1418 quic_stop_listen (u32 lctx_index)
1419 {
1420   QUIC_DBG (2, "Called quic_stop_listen");
1421   quic_ctx_t *lctx;
1422   lctx = quic_ctx_get (lctx_index, 0);
1423   QUIC_ASSERT (quic_ctx_is_listener (lctx));
1424   vnet_unlisten_args_t a = {
1425     .handle = lctx->udp_session_handle,
1426     .app_index = quic_main.app_index,
1427     .wrk_map_index = 0          /* default wrk */
1428   };
1429   if (vnet_unlisten (&a))
1430     clib_warning ("unlisten errored");
1431
1432   quic_release_crypto_context (lctx->crypto_context_index,
1433                                0 /* thread_index */ );
1434   quic_ctx_free (lctx);
1435   return 0;
1436 }
1437
1438 static transport_connection_t *
1439 quic_connection_get (u32 ctx_index, u32 thread_index)
1440 {
1441   quic_ctx_t *ctx;
1442   ctx = quic_ctx_get (ctx_index, thread_index);
1443   return &ctx->connection;
1444 }
1445
1446 static transport_connection_t *
1447 quic_listener_get (u32 listener_index)
1448 {
1449   QUIC_DBG (2, "Called quic_listener_get");
1450   quic_ctx_t *ctx;
1451   ctx = quic_ctx_get (listener_index, 0);
1452   return &ctx->connection;
1453 }
1454
1455 static u8 *
1456 format_quic_ctx (u8 * s, va_list * args)
1457 {
1458   quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
1459   u32 verbose = va_arg (*args, u32);
1460   u8 *str = 0;
1461
1462   if (!ctx)
1463     return s;
1464   str = format (str, "[#%d][Q] ", ctx->c_thread_index);
1465
1466   if (quic_ctx_is_listener (ctx))
1467     str = format (str, "Listener, UDP %ld", ctx->udp_session_handle);
1468   else if (quic_ctx_is_stream (ctx))
1469     str = format (str, "Stream %ld conn %d",
1470                   ctx->stream->stream_id, ctx->quic_connection_ctx_id);
1471   else                          /* connection */
1472     str = format (str, "Conn %d UDP %d", ctx->c_c_index,
1473                   ctx->udp_session_handle);
1474
1475   str = format (str, " app %d wrk %d", ctx->parent_app_id,
1476                 ctx->parent_app_wrk_id);
1477
1478   if (verbose == 1)
1479     s = format (s, "%-50s%-15d", str, ctx->conn_state);
1480   else
1481     s = format (s, "%s\n", str);
1482   vec_free (str);
1483   return s;
1484 }
1485
1486 static u8 *
1487 format_quic_connection (u8 * s, va_list * args)
1488 {
1489   u32 qc_index = va_arg (*args, u32);
1490   u32 thread_index = va_arg (*args, u32);
1491   u32 verbose = va_arg (*args, u32);
1492   quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1493   s = format (s, "%U", format_quic_ctx, ctx, verbose);
1494   return s;
1495 }
1496
1497 static u8 *
1498 format_quic_half_open (u8 * s, va_list * args)
1499 {
1500   u32 qc_index = va_arg (*args, u32);
1501   u32 thread_index = va_arg (*args, u32);
1502   quic_ctx_t *ctx = quic_ctx_get (qc_index, thread_index);
1503   s = format (s, "[#%d][Q] half-open app %u", thread_index,
1504               ctx->parent_app_id);
1505   return s;
1506 }
1507
1508 /*  TODO improve */
1509 static u8 *
1510 format_quic_listener (u8 * s, va_list * args)
1511 {
1512   u32 tci = va_arg (*args, u32);
1513   u32 thread_index = va_arg (*args, u32);
1514   u32 verbose = va_arg (*args, u32);
1515   quic_ctx_t *ctx = quic_ctx_get (tci, thread_index);
1516   s = format (s, "%U", format_quic_ctx, ctx, verbose);
1517   return s;
1518 }
1519
1520 /* Session layer callbacks */
1521
1522 static inline void
1523 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
1524                      ip46_address_t * addr, u16 port, u8 is_ip4)
1525 {
1526   if (is_ip4)
1527     {
1528       struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
1529       sa4->sin_family = AF_INET;
1530       sa4->sin_port = port;
1531       sa4->sin_addr.s_addr = addr->ip4.as_u32;
1532       *salen = sizeof (struct sockaddr_in);
1533     }
1534   else
1535     {
1536       struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
1537       sa6->sin6_family = AF_INET6;
1538       sa6->sin6_port = port;
1539       clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1540       *salen = sizeof (struct sockaddr_in6);
1541     }
1542 }
1543
1544 static void
1545 quic_on_quic_session_connected (quic_ctx_t * ctx)
1546 {
1547   session_t *quic_session;
1548   app_worker_t *app_wrk;
1549   u32 ctx_id = ctx->c_c_index;
1550   u32 thread_index = ctx->c_thread_index;
1551   int rv;
1552
1553   quic_session = session_alloc (thread_index);
1554
1555   QUIC_DBG (2, "Allocated quic session 0x%lx", session_handle (quic_session));
1556   ctx->c_s_index = quic_session->session_index;
1557   quic_session->app_wrk_index = ctx->parent_app_wrk_id;
1558   quic_session->connection_index = ctx->c_c_index;
1559   quic_session->listener_handle = SESSION_INVALID_HANDLE;
1560   quic_session->session_type =
1561     session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
1562
1563   /* If quic session connected fails, immediatly close connection */
1564   app_wrk = app_worker_get (ctx->parent_app_wrk_id);
1565   if (app_worker_init_connected (app_wrk, quic_session))
1566     {
1567       QUIC_ERR ("failed to app_worker_init_connected");
1568       quic_proto_on_close (ctx_id, thread_index);
1569       app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1570       return;
1571     }
1572
1573   quic_session->session_state = SESSION_STATE_CONNECTING;
1574   if ((rv = app_worker_connect_notify (app_wrk, quic_session,
1575                                        ctx->client_opaque)))
1576     {
1577       QUIC_ERR ("failed to notify app %d", rv);
1578       quic_proto_on_close (ctx_id, thread_index);
1579       return;
1580     }
1581
1582   /*  If the app opens a stream in its callback it may invalidate ctx */
1583   ctx = quic_ctx_get (ctx_id, thread_index);
1584   /*
1585    * app_worker_connect_notify() might have reallocated pool, reload
1586    * quic_session pointer
1587    */
1588   quic_session = session_get (ctx->c_s_index, thread_index);
1589   quic_session->session_state = SESSION_STATE_LISTENING;
1590 }
1591
1592 static void
1593 quic_check_quic_session_connected (quic_ctx_t * ctx)
1594 {
1595   /* Called when we need to trigger quic session connected
1596    * we may call this function on the server side / at
1597    * stream opening */
1598
1599   /* Conn may be set to null if the connection is terminated */
1600   if (!ctx->conn || ctx->conn_state != QUIC_CONN_STATE_HANDSHAKE)
1601     return;
1602   if (!quicly_connection_is_ready (ctx->conn))
1603     return;
1604   ctx->conn_state = QUIC_CONN_STATE_READY;
1605   if (!quicly_is_client (ctx->conn))
1606     return;
1607   quic_on_quic_session_connected (ctx);
1608 }
1609
1610 static inline void
1611 quic_update_conn_ctx (quicly_conn_t * conn, quicly_context_t * quicly_context)
1612 {
1613   /* we need to update the quicly_conn on migrate
1614    * as it contains a pointer to the crypto context */
1615   ptls_context_t **tls;
1616   quicly_context_t **_quicly_context;
1617   _quicly_context = (quicly_context_t **) conn;
1618   *_quicly_context = quicly_context;
1619   tls = (ptls_context_t **) quicly_get_tls (conn);
1620   *tls = quicly_context->tls;
1621 }
1622
1623 static void
1624 quic_receive_connection (void *arg)
1625 {
1626   u32 new_ctx_id, thread_index = vlib_get_thread_index ();
1627   quic_ctx_t *temp_ctx, *new_ctx;
1628   clib_bihash_kv_16_8_t kv;
1629   quicly_conn_t *conn;
1630   quicly_context_t *quicly_context;
1631   session_t *udp_session;
1632
1633   temp_ctx = arg;
1634   new_ctx_id = quic_ctx_alloc (thread_index);
1635   new_ctx = quic_ctx_get (new_ctx_id, thread_index);
1636
1637   QUIC_DBG (2, "Received conn %u (now %u)", temp_ctx->c_thread_index,
1638             new_ctx_id);
1639
1640   clib_memcpy (new_ctx, temp_ctx, sizeof (quic_ctx_t));
1641   clib_mem_free (temp_ctx);
1642
1643   new_ctx->c_thread_index = thread_index;
1644   new_ctx->c_c_index = new_ctx_id;
1645   quic_acquire_crypto_context (new_ctx);
1646
1647   conn = new_ctx->conn;
1648   quicly_context = quic_get_quicly_ctx_from_ctx (new_ctx);
1649   quic_update_conn_ctx (conn, quicly_context);
1650
1651   quic_store_conn_ctx (conn, new_ctx);
1652   quic_make_connection_key (&kv, quicly_get_master_id (conn));
1653   kv.value = ((u64) thread_index) << 32 | (u64) new_ctx_id;
1654   QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1655   clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1656   new_ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1657   quic_update_timer (new_ctx);
1658
1659   /*  Trigger write on this connection if necessary */
1660   udp_session = session_get_from_handle (new_ctx->udp_session_handle);
1661   udp_session->opaque = new_ctx_id;
1662   udp_session->flags &= ~SESSION_F_IS_MIGRATING;
1663   if (svm_fifo_max_dequeue (udp_session->tx_fifo))
1664     quic_set_udp_tx_evt (udp_session);
1665 }
1666
1667 static void
1668 quic_transfer_connection (u32 ctx_index, u32 dest_thread)
1669 {
1670   quic_ctx_t *ctx, *temp_ctx;
1671   u32 thread_index = vlib_get_thread_index ();
1672
1673   QUIC_DBG (2, "Transferring conn %u to thread %u", ctx_index, dest_thread);
1674
1675   temp_ctx = clib_mem_alloc (sizeof (quic_ctx_t));
1676   QUIC_ASSERT (temp_ctx != NULL);
1677   ctx = quic_ctx_get (ctx_index, thread_index);
1678
1679   clib_memcpy (temp_ctx, ctx, sizeof (quic_ctx_t));
1680
1681   quic_stop_ctx_timer (ctx);
1682   quic_release_crypto_context (ctx->crypto_context_index, thread_index);
1683   quic_ctx_free (ctx);
1684
1685   /*  Send connection to destination thread */
1686   session_send_rpc_evt_to_thread (dest_thread, quic_receive_connection,
1687                                   (void *) temp_ctx);
1688 }
1689
1690 static int
1691 quic_udp_session_connected_callback (u32 quic_app_index, u32 ctx_index,
1692                                      session_t * udp_session, u8 is_fail)
1693 {
1694   QUIC_DBG (2, "QSession is now connected (id %u)",
1695             udp_session->session_index);
1696   /* This should always be called before quic_connect returns since UDP always
1697    * connects instantly. */
1698   clib_bihash_kv_16_8_t kv;
1699   struct sockaddr_in6 sa6;
1700   struct sockaddr *sa = (struct sockaddr *) &sa6;
1701   socklen_t salen;
1702   transport_connection_t *tc;
1703   app_worker_t *app_wrk;
1704   quicly_conn_t *conn;
1705   quic_ctx_t *ctx;
1706   u32 thread_index = vlib_get_thread_index ();
1707   int ret;
1708   quicly_context_t *quicly_ctx;
1709
1710
1711   ctx = quic_ctx_get (ctx_index, thread_index);
1712   if (is_fail)
1713     {
1714       u32 api_context;
1715       app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_id);
1716       if (app_wrk)
1717         {
1718           api_context = ctx->c_s_index;
1719           app_worker_connect_notify (app_wrk, 0, api_context);
1720         }
1721       return 0;
1722     }
1723
1724   ctx->c_thread_index = thread_index;
1725   ctx->c_c_index = ctx_index;
1726
1727   QUIC_DBG (2, "Quic connect returned %u. New ctx [%u]%x",
1728             is_fail, thread_index, (ctx) ? ctx_index : ~0);
1729
1730   ctx->udp_session_handle = session_handle (udp_session);
1731   udp_session->opaque = ctx_index;
1732
1733   /* Init QUIC lib connection
1734    * Generate required sockaddr & salen */
1735   tc = session_get_transport (udp_session);
1736   quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1737
1738   quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1739   ret = quicly_connect (&ctx->conn, quicly_ctx, (char *) ctx->srv_hostname,
1740                         sa, NULL, &quic_main.wrk_ctx[thread_index].next_cid,
1741                         ptls_iovec_init (NULL, 0), &quic_main.hs_properties,
1742                         NULL);
1743   ++quic_main.wrk_ctx[thread_index].next_cid.master_id;
1744   /*  Save context handle in quicly connection */
1745   quic_store_conn_ctx (ctx->conn, ctx);
1746   assert (ret == 0);
1747
1748   /*  Register connection in connections map */
1749   conn = ctx->conn;
1750   quic_make_connection_key (&kv, quicly_get_master_id (conn));
1751   kv.value = ((u64) thread_index) << 32 | (u64) ctx_index;
1752   QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
1753   clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
1754
1755   /*  UDP stack quirk? preemptively transfer connection if that happens */
1756   if (udp_session->thread_index != thread_index)
1757     quic_transfer_connection (ctx_index, udp_session->thread_index);
1758   else
1759     quic_send_packets (ctx);
1760
1761   return ret;
1762 }
1763
1764 static void
1765 quic_udp_session_disconnect_callback (session_t * s)
1766 {
1767   clib_warning ("UDP session disconnected???");
1768 }
1769
1770 static void
1771 quic_udp_session_cleanup_callback (session_t * udp_session,
1772                                    session_cleanup_ntf_t ntf)
1773 {
1774   quic_ctx_t *ctx;
1775
1776   if (ntf != SESSION_CLEANUP_SESSION)
1777     return;
1778
1779   ctx = quic_ctx_get (udp_session->opaque, udp_session->thread_index);
1780   quic_stop_ctx_timer (ctx);
1781   quic_release_crypto_context (ctx->crypto_context_index,
1782                                ctx->c_thread_index);
1783   quic_ctx_free (ctx);
1784 }
1785
1786 static void
1787 quic_udp_session_reset_callback (session_t * s)
1788 {
1789   clib_warning ("UDP session reset???");
1790 }
1791
1792 static void
1793 quic_udp_session_migrate_callback (session_t * s, session_handle_t new_sh)
1794 {
1795   u32 new_thread = session_thread_from_handle (new_sh);
1796   quic_ctx_t *ctx;
1797
1798   QUIC_DBG (2, "Session %x migrated to %lx", s->session_index, new_sh);
1799   QUIC_ASSERT (vlib_get_thread_index () == s->thread_index);
1800   ctx = quic_ctx_get (s->opaque, s->thread_index);
1801   QUIC_ASSERT (ctx->udp_session_handle == session_handle (s));
1802
1803   ctx->udp_session_handle = new_sh;
1804 #if QUIC_DEBUG >= 1
1805   s->opaque = 0xfeedface;
1806 #endif
1807   quic_transfer_connection (ctx->c_c_index, new_thread);
1808 }
1809
1810 int
1811 quic_udp_session_accepted_callback (session_t * udp_session)
1812 {
1813   /* New UDP connection, try to accept it */
1814   u32 ctx_index;
1815   quic_ctx_t *ctx, *lctx;
1816   session_t *udp_listen_session;
1817   u32 thread_index = vlib_get_thread_index ();
1818
1819   udp_listen_session =
1820     listen_session_get_from_handle (udp_session->listener_handle);
1821
1822   ctx_index = quic_ctx_alloc (thread_index);
1823   ctx = quic_ctx_get (ctx_index, thread_index);
1824   ctx->c_thread_index = udp_session->thread_index;
1825   ctx->c_c_index = ctx_index;
1826   ctx->c_s_index = QUIC_SESSION_INVALID;
1827   ctx->udp_session_handle = session_handle (udp_session);
1828   QUIC_DBG (2, "ACCEPTED UDP 0x%lx", ctx->udp_session_handle);
1829   ctx->listener_ctx_id = udp_listen_session->opaque;
1830   lctx = quic_ctx_get (udp_listen_session->opaque,
1831                        udp_listen_session->thread_index);
1832   ctx->udp_is_ip4 = lctx->c_is_ip4;
1833   ctx->parent_app_id = lctx->parent_app_id;
1834   ctx->parent_app_wrk_id = lctx->parent_app_wrk_id;
1835   ctx->timer_handle = QUIC_TIMER_HANDLE_INVALID;
1836   ctx->conn_state = QUIC_CONN_STATE_OPENED;
1837   ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
1838
1839   ctx->crypto_engine = lctx->crypto_engine;
1840   ctx->ckpair_index = lctx->ckpair_index;
1841   quic_acquire_crypto_context (ctx);
1842   udp_session->opaque = ctx_index;
1843
1844   /* TODO timeout to delete these if they never connect */
1845   return 0;
1846 }
1847
1848 static int
1849 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1850 {
1851   /* No-op for builtin */
1852   return 0;
1853 }
1854
1855 static int
1856 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1857 {
1858   /* No-op for builtin */
1859   return 0;
1860 }
1861
1862 static int
1863 quic_custom_app_rx_callback (transport_connection_t * tc)
1864 {
1865   quic_ctx_t *ctx;
1866   session_t *stream_session = session_get (tc->s_index, tc->thread_index);
1867   QUIC_DBG (3, "Received app READ notification");
1868   quic_ack_rx_data (stream_session);
1869   svm_fifo_reset_has_deq_ntf (stream_session->rx_fifo);
1870
1871   /* Need to send packets (acks may never be sent otherwise) */
1872   ctx = quic_ctx_get (stream_session->connection_index,
1873                       stream_session->thread_index);
1874   quic_send_packets (ctx);
1875   return 0;
1876 }
1877
1878 static int
1879 quic_custom_tx_callback (void *s, u32 max_burst_size)
1880 {
1881   session_t *stream_session = (session_t *) s;
1882   quic_stream_data_t *stream_data;
1883   quicly_stream_t *stream;
1884   quic_ctx_t *ctx;
1885   u32 max_deq;
1886   int rv;
1887
1888   if (PREDICT_FALSE
1889       (stream_session->session_state >= SESSION_STATE_TRANSPORT_CLOSING))
1890     return 0;
1891   ctx = quic_ctx_get (stream_session->connection_index,
1892                       stream_session->thread_index);
1893   if (PREDICT_FALSE (!quic_ctx_is_stream (ctx)))
1894     {
1895       goto tx_end;              /* Most probably a reschedule */
1896     }
1897
1898   QUIC_DBG (3, "Stream TX event");
1899   quic_ack_rx_data (stream_session);
1900   stream = ctx->stream;
1901   if (!quicly_sendstate_is_open (&stream->sendstate))
1902     {
1903       QUIC_ERR ("Warning: tried to send on closed stream");
1904       return -1;
1905     }
1906
1907   stream_data = (quic_stream_data_t *) stream->data;
1908   max_deq = svm_fifo_max_dequeue (stream_session->tx_fifo);
1909   QUIC_ASSERT (max_deq >= stream_data->app_tx_data_len);
1910   if (max_deq == stream_data->app_tx_data_len)
1911     {
1912       QUIC_DBG (3, "TX but no data %d / %d", max_deq,
1913                 stream_data->app_tx_data_len);
1914       return 0;
1915     }
1916   stream_data->app_tx_data_len = max_deq;
1917   rv = quicly_stream_sync_sendbuf (stream, 1);
1918   QUIC_ASSERT (!rv);
1919
1920 tx_end:
1921   quic_send_packets (ctx);
1922   return 0;
1923 }
1924
1925 /*
1926  * Returns 0 if a matching connection is found and is on the right thread.
1927  * Otherwise returns -1.
1928  * If a connection is found, even on the wrong thread, ctx_thread and ctx_index
1929  * will be set.
1930  */
1931 static inline int
1932 quic_find_packet_ctx (quic_rx_packet_ctx_t * pctx, u32 caller_thread_index)
1933 {
1934   clib_bihash_kv_16_8_t kv;
1935   clib_bihash_16_8_t *h;
1936   quic_ctx_t *ctx;
1937   u32 index, thread_id;
1938
1939   h = &quic_main.connection_hash;
1940   quic_make_connection_key (&kv, &pctx->packet.cid.dest.plaintext);
1941   QUIC_DBG (3, "Searching conn with id %lu %lu", kv.key[0], kv.key[1]);
1942
1943   if (clib_bihash_search_16_8 (h, &kv, &kv))
1944     {
1945       QUIC_DBG (3, "connection not found");
1946       return QUIC_PACKET_TYPE_NONE;
1947     }
1948
1949   index = kv.value & UINT32_MAX;
1950   thread_id = kv.value >> 32;
1951   /* Check if this connection belongs to this thread, otherwise
1952    * ask for it to be moved */
1953   if (thread_id != caller_thread_index)
1954     {
1955       QUIC_DBG (2, "Connection is on wrong thread");
1956       /* Cannot make full check with quicly_is_destination... */
1957       pctx->ctx_index = index;
1958       pctx->thread_index = thread_id;
1959       return QUIC_PACKET_TYPE_MIGRATE;
1960     }
1961   ctx = quic_ctx_get (index, vlib_get_thread_index ());
1962   if (!ctx->conn)
1963     {
1964       QUIC_ERR ("ctx has no conn");
1965       return QUIC_PACKET_TYPE_NONE;
1966     }
1967   if (!quicly_is_destination (ctx->conn, NULL, &pctx->sa, &pctx->packet))
1968     return QUIC_PACKET_TYPE_NONE;
1969
1970   QUIC_DBG (3, "Connection found");
1971   pctx->ctx_index = index;
1972   pctx->thread_index = thread_id;
1973   return QUIC_PACKET_TYPE_RECEIVE;
1974 }
1975
1976 static void
1977 quic_accept_connection (quic_rx_packet_ctx_t * pctx)
1978 {
1979   quicly_context_t *quicly_ctx;
1980   session_t *quic_session;
1981   clib_bihash_kv_16_8_t kv;
1982   app_worker_t *app_wrk;
1983   quicly_conn_t *conn;
1984   quic_ctx_t *ctx;
1985   quic_ctx_t *lctx;
1986   int rv;
1987
1988   /* new connection, accept and create context if packet is valid
1989    * TODO: check if socket is actually listening? */
1990   ctx = quic_ctx_get (pctx->ctx_index, pctx->thread_index);
1991   if (ctx->c_s_index != QUIC_SESSION_INVALID)
1992     {
1993       QUIC_DBG (2, "already accepted ctx 0x%x", ctx_index);
1994       return;
1995     }
1996
1997   quicly_ctx = quic_get_quicly_ctx_from_ctx (ctx);
1998   if ((rv = quicly_accept (&conn, quicly_ctx, NULL, &pctx->sa,
1999                            &pctx->packet, NULL,
2000                            &quic_main.wrk_ctx[pctx->thread_index].next_cid,
2001                            NULL)))
2002     {
2003       /* Invalid packet, pass */
2004       assert (conn == NULL);
2005       QUIC_ERR ("Accept failed with %U", quic_format_err, rv);
2006       /* TODO: cleanup created quic ctx and UDP session */
2007       return;
2008     }
2009   assert (conn != NULL);
2010
2011   ++quic_main.wrk_ctx[pctx->thread_index].next_cid.master_id;
2012   /* Save ctx handle in quicly connection */
2013   quic_store_conn_ctx (conn, ctx);
2014   ctx->conn = conn;
2015
2016   quic_session = session_alloc (ctx->c_thread_index);
2017   QUIC_DBG (2, "Allocated quic_session, 0x%lx ctx %u",
2018             session_handle (quic_session), ctx->c_c_index);
2019   quic_session->session_state = SESSION_STATE_LISTENING;
2020   ctx->c_s_index = quic_session->session_index;
2021
2022   lctx = quic_ctx_get (ctx->listener_ctx_id, 0);
2023
2024   quic_session->app_wrk_index = lctx->parent_app_wrk_id;
2025   quic_session->connection_index = ctx->c_c_index;
2026   quic_session->session_type =
2027     session_type_from_proto_and_ip (TRANSPORT_PROTO_QUIC, ctx->udp_is_ip4);
2028   quic_session->listener_handle = lctx->c_s_index;
2029
2030   /* Register connection in connections map */
2031   quic_make_connection_key (&kv, quicly_get_master_id (conn));
2032   kv.value = ((u64) pctx->thread_index) << 32 | (u64) pctx->ctx_index;
2033   clib_bihash_add_del_16_8 (&quic_main.connection_hash, &kv, 1 /* is_add */ );
2034   QUIC_DBG (2, "Registering conn with id %lu %lu", kv.key[0], kv.key[1]);
2035
2036   /* If notify fails, reset connection immediatly */
2037   if ((rv = app_worker_init_accepted (quic_session)))
2038     {
2039       QUIC_ERR ("failed to allocate fifos");
2040       quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2041       return;
2042     }
2043
2044   app_wrk = app_worker_get (quic_session->app_wrk_index);
2045   if ((rv = app_worker_accept_notify (app_wrk, quic_session)))
2046     {
2047       QUIC_ERR ("failed to notify accept worker app");
2048       quic_proto_on_close (pctx->ctx_index, pctx->thread_index);
2049       return;
2050     }
2051
2052   ctx->conn_state = QUIC_CONN_STATE_READY;
2053 }
2054
2055 static int
2056 quic_reset_connection (u64 udp_session_handle, quic_rx_packet_ctx_t * pctx)
2057 {
2058   /* short header packet; potentially a dead connection. No need to check the
2059    * length of the incoming packet, because loop is prevented by authenticating
2060    * the CID (by checking node_id and thread_id). If the peer is also sending a
2061    * reset, then the next CID is highly likely to contain a non-authenticating
2062    * CID, ... */
2063   QUIC_DBG (2, "Sending stateless reset");
2064   int rv;
2065   quicly_datagram_t *dgram;
2066   session_t *udp_session;
2067   quicly_context_t *quicly_ctx;
2068   if (pctx->packet.cid.dest.plaintext.node_id != 0
2069       || pctx->packet.cid.dest.plaintext.thread_id != 0)
2070     return 0;
2071   quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2072   dgram = quicly_send_stateless_reset (quicly_ctx, &pctx->sa, NULL,
2073                                        &pctx->packet.cid.dest.plaintext);
2074   if (dgram == NULL)
2075     return 1;
2076   udp_session = session_get_from_handle (udp_session_handle);
2077   rv = quic_send_datagram (udp_session, dgram);
2078   quic_set_udp_tx_evt (udp_session);
2079   return rv;
2080 }
2081
2082 static int
2083 quic_process_one_rx_packet (u64 udp_session_handle, svm_fifo_t * f,
2084                             u32 fifo_offset, quic_rx_packet_ctx_t * pctx)
2085 {
2086   size_t plen;
2087   u32 full_len, ret;
2088   u32 thread_index = vlib_get_thread_index ();
2089   u32 cur_deq = svm_fifo_max_dequeue (f) - fifo_offset;
2090   quicly_context_t *quicly_ctx;
2091   session_t *udp_session;
2092   int rv;
2093
2094   ret = svm_fifo_peek (f, fifo_offset,
2095                        SESSION_CONN_HDR_LEN, (u8 *) & pctx->ph);
2096   QUIC_ASSERT (ret == SESSION_CONN_HDR_LEN);
2097   QUIC_ASSERT (pctx->ph.data_offset == 0);
2098   full_len = pctx->ph.data_length + SESSION_CONN_HDR_LEN;
2099   if (full_len > cur_deq)
2100     {
2101       QUIC_ERR ("Not enough data in fifo RX");
2102       return 1;
2103     }
2104
2105   /* Quicly can read len bytes from the fifo at offset:
2106    * ph.data_offset + SESSION_CONN_HDR_LEN */
2107   ret = svm_fifo_peek (f, SESSION_CONN_HDR_LEN + fifo_offset,
2108                        pctx->ph.data_length, pctx->data);
2109   if (ret != pctx->ph.data_length)
2110     {
2111       QUIC_ERR ("Not enough data peeked in RX");
2112       return 1;
2113     }
2114
2115   quic_increment_counter (QUIC_ERROR_RX_PACKETS, 1);
2116   quic_build_sockaddr (&pctx->sa, &pctx->salen, &pctx->ph.rmt_ip,
2117                        pctx->ph.rmt_port, pctx->ph.is_ip4);
2118   quicly_ctx = quic_get_quicly_ctx_from_udp (udp_session_handle);
2119   plen = quicly_decode_packet (quicly_ctx, &pctx->packet,
2120                                pctx->data, pctx->ph.data_length);
2121
2122   if (plen == SIZE_MAX)
2123     {
2124       return 1;
2125     }
2126
2127   rv = quic_find_packet_ctx (pctx, thread_index);
2128   if (rv == QUIC_PACKET_TYPE_RECEIVE)
2129     {
2130       pctx->ptype = QUIC_PACKET_TYPE_RECEIVE;
2131       return 0;
2132     }
2133   else if (rv == QUIC_PACKET_TYPE_MIGRATE)
2134     {
2135       pctx->ptype = QUIC_PACKET_TYPE_MIGRATE;
2136       /*  Connection found but on wrong thread, ask move */
2137     }
2138   else if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
2139     {
2140       pctx->ptype = QUIC_PACKET_TYPE_ACCEPT;
2141       udp_session = session_get_from_handle (udp_session_handle);
2142       pctx->ctx_index = udp_session->opaque;
2143       pctx->thread_index = thread_index;
2144     }
2145   else
2146     {
2147       pctx->ptype = QUIC_PACKET_TYPE_RESET;
2148     }
2149   return 1;
2150 }
2151
2152 static int
2153 quic_udp_session_rx_callback (session_t * udp_session)
2154 {
2155   /*  Read data from UDP rx_fifo and pass it to the quicly conn. */
2156   quic_ctx_t *ctx = NULL;
2157   svm_fifo_t *f = udp_session->rx_fifo;
2158   u32 max_deq;
2159   u64 udp_session_handle = session_handle (udp_session);
2160   int rv = 0;
2161   u32 thread_index = vlib_get_thread_index ();
2162   u32 cur_deq, fifo_offset, max_packets, i;
2163
2164   quic_rx_packet_ctx_t packets_ctx[QUIC_RCV_MAX_BATCH_PACKETS];
2165
2166   if (udp_session->flags & SESSION_F_IS_MIGRATING)
2167     {
2168       QUIC_DBG (3, "RX on migrating udp session");
2169       return 0;
2170     }
2171
2172 rx_start:
2173   max_deq = svm_fifo_max_dequeue (f);
2174   if (max_deq == 0)
2175     return 0;
2176
2177   fifo_offset = 0;
2178   max_packets = QUIC_RCV_MAX_BATCH_PACKETS;
2179
2180 #if CLIB_DEBUG > 0
2181   clib_memset (packets_ctx, 0xfa,
2182                QUIC_RCV_MAX_BATCH_PACKETS * sizeof (quic_rx_packet_ctx_t));
2183 #endif
2184
2185   for (i = 0; i < max_packets; i++)
2186     {
2187       packets_ctx[i].thread_index = UINT32_MAX;
2188       packets_ctx[i].ctx_index = UINT32_MAX;
2189       packets_ctx[i].ptype = QUIC_PACKET_TYPE_DROP;
2190
2191       cur_deq = max_deq - fifo_offset;
2192       if (cur_deq == 0)
2193         {
2194           max_packets = i + 1;
2195           break;
2196         }
2197       if (cur_deq < SESSION_CONN_HDR_LEN)
2198         {
2199           fifo_offset = max_deq;
2200           max_packets = i + 1;
2201           QUIC_ERR ("Fifo %d < header size in RX", cur_deq);
2202           break;
2203         }
2204       rv = quic_process_one_rx_packet (udp_session_handle, f,
2205                                        fifo_offset, &packets_ctx[i]);
2206       if (packets_ctx[i].ptype != QUIC_PACKET_TYPE_MIGRATE)
2207         fifo_offset += SESSION_CONN_HDR_LEN + packets_ctx[i].ph.data_length;
2208       if (rv)
2209         {
2210           max_packets = i + 1;
2211           break;
2212         }
2213     }
2214
2215   for (i = 0; i < max_packets; i++)
2216     {
2217       switch (packets_ctx[i].ptype)
2218         {
2219         case QUIC_PACKET_TYPE_RECEIVE:
2220           ctx = quic_ctx_get (packets_ctx[i].ctx_index, thread_index);
2221           rv = quicly_receive (ctx->conn, NULL, &packets_ctx[i].sa,
2222                                &packets_ctx[i].packet);
2223           if (rv && rv != QUICLY_ERROR_PACKET_IGNORED)
2224             {
2225               QUIC_ERR ("quicly_receive return error %U",
2226                         quic_format_err, rv);
2227             }
2228           break;
2229         case QUIC_PACKET_TYPE_ACCEPT:
2230           quic_accept_connection (&packets_ctx[i]);
2231           break;
2232         case QUIC_PACKET_TYPE_RESET:
2233           quic_reset_connection (udp_session_handle, &packets_ctx[i]);
2234           break;
2235         }
2236     }
2237   for (i = 0; i < max_packets; i++)
2238     {
2239       switch (packets_ctx[i].ptype)
2240         {
2241         case QUIC_PACKET_TYPE_RECEIVE:
2242           ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2243                               packets_ctx[i].thread_index);
2244           quic_check_quic_session_connected (ctx);
2245           ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2246                               packets_ctx[i].thread_index);
2247           break;
2248         case QUIC_PACKET_TYPE_ACCEPT:
2249           ctx = quic_ctx_get (packets_ctx[i].ctx_index,
2250                               packets_ctx[i].thread_index);
2251           break;
2252         default:
2253           continue;
2254         }
2255       quic_send_packets (ctx);
2256     }
2257
2258   udp_session = session_get_from_handle (udp_session_handle);   /*  session alloc might have happened */
2259   f = udp_session->rx_fifo;
2260   svm_fifo_dequeue_drop (f, fifo_offset);
2261
2262   if (svm_fifo_max_dequeue (f))
2263     goto rx_start;
2264
2265   return 0;
2266 }
2267
2268 always_inline void
2269 quic_common_get_transport_endpoint (quic_ctx_t * ctx,
2270                                     transport_endpoint_t * tep, u8 is_lcl)
2271 {
2272   session_t *udp_session;
2273   if (!quic_ctx_is_stream (ctx))
2274     {
2275       udp_session = session_get_from_handle (ctx->udp_session_handle);
2276       session_get_endpoint (udp_session, tep, is_lcl);
2277     }
2278 }
2279
2280 static void
2281 quic_get_transport_listener_endpoint (u32 listener_index,
2282                                       transport_endpoint_t * tep, u8 is_lcl)
2283 {
2284   quic_ctx_t *ctx;
2285   app_listener_t *app_listener;
2286   session_t *udp_listen_session;
2287   ctx = quic_ctx_get (listener_index, vlib_get_thread_index ());
2288   if (quic_ctx_is_listener (ctx))
2289     {
2290       app_listener = app_listener_get_w_handle (ctx->udp_session_handle);
2291       udp_listen_session = app_listener_get_session (app_listener);
2292       return session_get_endpoint (udp_listen_session, tep, is_lcl);
2293     }
2294   quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2295 }
2296
2297 static void
2298 quic_get_transport_endpoint (u32 ctx_index, u32 thread_index,
2299                              transport_endpoint_t * tep, u8 is_lcl)
2300 {
2301   quic_ctx_t *ctx;
2302   ctx = quic_ctx_get (ctx_index, thread_index);
2303   quic_common_get_transport_endpoint (ctx, tep, is_lcl);
2304 }
2305
2306 /* *INDENT-OFF* */
2307 static session_cb_vft_t quic_app_cb_vft = {
2308   .session_accept_callback = quic_udp_session_accepted_callback,
2309   .session_disconnect_callback = quic_udp_session_disconnect_callback,
2310   .session_connected_callback = quic_udp_session_connected_callback,
2311   .session_reset_callback = quic_udp_session_reset_callback,
2312   .session_migrate_callback = quic_udp_session_migrate_callback,
2313   .add_segment_callback = quic_add_segment_callback,
2314   .del_segment_callback = quic_del_segment_callback,
2315   .builtin_app_rx_callback = quic_udp_session_rx_callback,
2316   .session_cleanup_callback = quic_udp_session_cleanup_callback,
2317   .app_cert_key_pair_delete_callback = quic_app_cert_key_pair_delete_callback,
2318 };
2319
2320 static const transport_proto_vft_t quic_proto = {
2321   .connect = quic_connect,
2322   .close = quic_proto_on_close,
2323   .start_listen = quic_start_listen,
2324   .stop_listen = quic_stop_listen,
2325   .get_connection = quic_connection_get,
2326   .get_listener = quic_listener_get,
2327   .update_time = quic_update_time,
2328   .app_rx_evt = quic_custom_app_rx_callback,
2329   .custom_tx = quic_custom_tx_callback,
2330   .format_connection = format_quic_connection,
2331   .format_half_open = format_quic_half_open,
2332   .format_listener = format_quic_listener,
2333   .get_transport_endpoint = quic_get_transport_endpoint,
2334   .get_transport_listener_endpoint = quic_get_transport_listener_endpoint,
2335   .transport_options = {
2336     .tx_type = TRANSPORT_TX_INTERNAL,
2337     .service_type = TRANSPORT_SERVICE_APP,
2338   },
2339 };
2340 /* *INDENT-ON* */
2341
2342 static quicly_stream_open_t on_stream_open = { quic_on_stream_open };
2343 static quicly_closed_by_peer_t on_closed_by_peer = { quic_on_closed_by_peer };
2344 static quicly_now_t quicly_vpp_now_cb = { quic_get_time };
2345
2346 static void
2347 quic_register_cipher_suite (crypto_engine_type_t type,
2348                             ptls_cipher_suite_t ** ciphers)
2349 {
2350   quic_main_t *qm = &quic_main;
2351   vec_validate (qm->quic_ciphers, type);
2352   clib_bitmap_set (qm->available_crypto_engines, type, 1);
2353   qm->quic_ciphers[type] = ciphers;
2354 }
2355
2356 static void
2357 quic_update_fifo_size ()
2358 {
2359   quic_main_t *qm = &quic_main;
2360   segment_manager_props_t *seg_mgr_props =
2361     application_get_segment_manager_properties (qm->app_index);
2362
2363   if (!seg_mgr_props)
2364     {
2365       clib_warning
2366         ("error while getting segment_manager_props_t, can't update fifo-size");
2367       return;
2368     }
2369
2370   seg_mgr_props->tx_fifo_size = qm->udp_fifo_size;
2371   seg_mgr_props->rx_fifo_size = qm->udp_fifo_size;
2372 }
2373
2374 static clib_error_t *
2375 quic_init (vlib_main_t * vm)
2376 {
2377   u32 segment_size = 256 << 20;
2378   vlib_thread_main_t *vtm = vlib_get_thread_main ();
2379   tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
2380   vnet_app_attach_args_t _a, *a = &_a;
2381   u64 options[APP_OPTIONS_N_OPTIONS];
2382   quic_main_t *qm = &quic_main;
2383   u32 num_threads, i;
2384
2385   num_threads = 1 /* main thread */  + vtm->n_threads;
2386
2387   clib_memset (a, 0, sizeof (*a));
2388   clib_memset (options, 0, sizeof (options));
2389
2390   a->session_cb_vft = &quic_app_cb_vft;
2391   a->api_client_index = APP_INVALID_INDEX;
2392   a->options = options;
2393   a->name = format (0, "quic");
2394   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
2395   a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
2396   a->options[APP_OPTIONS_RX_FIFO_SIZE] = qm->udp_fifo_size;
2397   a->options[APP_OPTIONS_TX_FIFO_SIZE] = qm->udp_fifo_size;
2398   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = qm->udp_fifo_prealloc;
2399   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
2400   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
2401   a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2402
2403   if (vnet_application_attach (a))
2404     {
2405       clib_warning ("failed to attach quic app");
2406       return clib_error_return (0, "failed to attach quic app");
2407     }
2408
2409   vec_validate (qm->ctx_pool, num_threads - 1);
2410   vec_validate (qm->wrk_ctx, num_threads - 1);
2411   for (i = 0; i < num_threads; i++)
2412     {
2413       qm->wrk_ctx[i].next_cid.thread_id = i;
2414       tw = &qm->wrk_ctx[i].timer_wheel;
2415       tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
2416                                            1e-3 /* timer period 1ms */ , ~0);
2417       tw->last_run_time = vlib_time_now (vlib_get_main ());
2418       clib_bihash_init_24_8 (&qm->wrk_ctx[i].crypto_context_hash,
2419                              "quic crypto contexts", 64, 128 << 10);
2420     }
2421
2422   clib_bihash_init_16_8 (&qm->connection_hash, "quic connections", 1024,
2423                          4 << 20);
2424
2425   qm->app_index = a->app_index;
2426   qm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
2427     / QUIC_TSTAMP_RESOLUTION;
2428   qm->session_cache.super.cb = quic_encrypt_ticket_cb;
2429
2430   transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2431                                FIB_PROTOCOL_IP4, ~0);
2432   transport_register_protocol (TRANSPORT_PROTO_QUIC, &quic_proto,
2433                                FIB_PROTOCOL_IP6, ~0);
2434
2435   clib_bitmap_alloc (qm->available_crypto_engines,
2436                      app_crypto_engine_n_types ());
2437   quic_register_cipher_suite (CRYPTO_ENGINE_VPP, quic_crypto_cipher_suites);
2438   quic_register_cipher_suite (CRYPTO_ENGINE_PICOTLS,
2439                               ptls_openssl_cipher_suites);
2440   qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2441   vec_free (a->name);
2442   return 0;
2443 }
2444
2445 VLIB_INIT_FUNCTION (quic_init);
2446
2447 static clib_error_t *
2448 quic_plugin_crypto_command_fn (vlib_main_t * vm,
2449                                unformat_input_t * input,
2450                                vlib_cli_command_t * cmd)
2451 {
2452   quic_main_t *qm = &quic_main;
2453   if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
2454     return clib_error_return (0, "unknown input '%U'",
2455                               format_unformat_error, input);
2456   if (unformat (input, "vpp"))
2457     qm->default_crypto_engine = CRYPTO_ENGINE_VPP;
2458   else if (unformat (input, "picotls"))
2459     qm->default_crypto_engine = CRYPTO_ENGINE_PICOTLS;
2460   else
2461     return clib_error_return (0, "unknown input '%U'",
2462                               format_unformat_error, input);
2463   return 0;
2464 }
2465
2466 u64 quic_fifosize = 0;
2467 static clib_error_t *
2468 quic_plugin_set_fifo_size_command_fn (vlib_main_t * vm,
2469                                       unformat_input_t * input,
2470                                       vlib_cli_command_t * cmd)
2471 {
2472   quic_main_t *qm = &quic_main;
2473   unformat_input_t _line_input, *line_input = &_line_input;
2474   uword tmp;
2475
2476   if (!unformat_user (input, unformat_line_input, line_input))
2477     return 0;
2478
2479   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2480     {
2481       if (unformat (line_input, "%U", unformat_memory_size, &tmp))
2482         {
2483           if (tmp >= 0x100000000ULL)
2484             {
2485               return clib_error_return
2486                 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
2487             }
2488           qm->udp_fifo_size = tmp;
2489           quic_update_fifo_size ();
2490         }
2491       else
2492         return clib_error_return (0, "unknown input '%U'",
2493                                   format_unformat_error, line_input);
2494     }
2495
2496   return 0;
2497 }
2498
2499 static inline u64
2500 quic_get_counter_value (u32 event_code)
2501 {
2502   vlib_node_t *n;
2503   vlib_main_t *vm;
2504   vlib_error_main_t *em;
2505
2506   u32 code, i;
2507   u64 c, sum = 0;
2508   int index = 0;
2509
2510   vm = vlib_get_main ();
2511   em = &vm->error_main;
2512   n = vlib_get_node (vm, quic_input_node.index);
2513   code = event_code;
2514   /* *INDENT-OFF* */
2515   foreach_vlib_main(({
2516     em = &this_vlib_main->error_main;
2517     i = n->error_heap_index + code;
2518     c = em->counters[i];
2519
2520     if (i < vec_len (em->counters_last_clear))
2521        c -= em->counters_last_clear[i];
2522     sum += c;
2523     index++;
2524   }));
2525   /* *INDENT-ON* */
2526   return sum;
2527 }
2528
2529 static void
2530 quic_show_aggregated_stats (vlib_main_t * vm)
2531 {
2532   u32 num_workers = vlib_num_workers ();
2533   quic_main_t *qm = &quic_main;
2534   quic_ctx_t *ctx = NULL;
2535   quicly_stats_t st, agg_stats;
2536   u32 i, nconn = 0, nstream = 0;
2537
2538   clib_memset (&agg_stats, 0, sizeof (agg_stats));
2539   for (i = 0; i < num_workers + 1; i++)
2540     {
2541       /* *INDENT-OFF* */
2542       pool_foreach (ctx, qm->ctx_pool[i],
2543       ({
2544         if (quic_ctx_is_conn (ctx) && ctx->conn)
2545           {
2546             quicly_get_stats (ctx->conn, &st);
2547             agg_stats.rtt.smoothed += st.rtt.smoothed;
2548             agg_stats.rtt.minimum += st.rtt.minimum;
2549             agg_stats.rtt.variance += st.rtt.variance;
2550             agg_stats.num_packets.received += st.num_packets.received;
2551             agg_stats.num_packets.sent += st.num_packets.sent;
2552             agg_stats.num_packets.lost += st.num_packets.lost;
2553             agg_stats.num_packets.ack_received += st.num_packets.ack_received;
2554             agg_stats.num_bytes.received += st.num_bytes.received;
2555             agg_stats.num_bytes.sent += st.num_bytes.sent;
2556             nconn++;
2557           }
2558         else if (quic_ctx_is_stream (ctx))
2559           nstream++;
2560       }));
2561       /* *INDENT-ON* */
2562     }
2563   vlib_cli_output (vm, "-------- Connections --------");
2564   vlib_cli_output (vm, "Current:         %u", nconn);
2565   vlib_cli_output (vm, "Opened:          %d",
2566                    quic_get_counter_value (QUIC_ERROR_OPENED_CONNECTION));
2567   vlib_cli_output (vm, "Closed:          %d",
2568                    quic_get_counter_value (QUIC_ERROR_CLOSED_CONNECTION));
2569   vlib_cli_output (vm, "---------- Streams ----------");
2570   vlib_cli_output (vm, "Current:         %u", nstream);
2571   vlib_cli_output (vm, "Opened:          %d",
2572                    quic_get_counter_value (QUIC_ERROR_OPENED_STREAM));
2573   vlib_cli_output (vm, "Closed:          %d",
2574                    quic_get_counter_value (QUIC_ERROR_CLOSED_STREAM));
2575   vlib_cli_output (vm, "---------- Packets ----------");
2576   vlib_cli_output (vm, "RX Total:        %d",
2577                    quic_get_counter_value (QUIC_ERROR_RX_PACKETS));
2578   vlib_cli_output (vm, "RX 0RTT:         %d",
2579                    quic_get_counter_value (QUIC_ERROR_ZERO_RTT_RX_PACKETS));
2580   vlib_cli_output (vm, "RX 1RTT:         %d",
2581                    quic_get_counter_value (QUIC_ERROR_ONE_RTT_RX_PACKETS));
2582   vlib_cli_output (vm, "TX Total:        %d",
2583                    quic_get_counter_value (QUIC_ERROR_TX_PACKETS));
2584   vlib_cli_output (vm, "----------- Stats -----------");
2585   vlib_cli_output (vm, "Min      RTT     %f",
2586                    nconn > 0 ? agg_stats.rtt.minimum / nconn : 0);
2587   vlib_cli_output (vm, "Smoothed RTT     %f",
2588                    nconn > 0 ? agg_stats.rtt.smoothed / nconn : 0);
2589   vlib_cli_output (vm, "Variance on RTT  %f",
2590                    nconn > 0 ? agg_stats.rtt.variance / nconn : 0);
2591   vlib_cli_output (vm, "Packets Received %lu",
2592                    agg_stats.num_packets.received);
2593   vlib_cli_output (vm, "Packets Sent     %lu", agg_stats.num_packets.sent);
2594   vlib_cli_output (vm, "Packets Lost     %lu", agg_stats.num_packets.lost);
2595   vlib_cli_output (vm, "Packets Acks     %lu",
2596                    agg_stats.num_packets.ack_received);
2597   vlib_cli_output (vm, "RX bytes         %lu", agg_stats.num_bytes.received);
2598   vlib_cli_output (vm, "TX bytes         %lu", agg_stats.num_bytes.sent);
2599 }
2600
2601 static u8 *
2602 quic_format_quicly_conn_id (u8 * s, va_list * args)
2603 {
2604   quicly_cid_plaintext_t *mid = va_arg (*args, quicly_cid_plaintext_t *);
2605   s = format (s, "C%x_%x", mid->master_id, mid->thread_id);
2606   return s;
2607 }
2608
2609 static u8 *
2610 quic_format_quicly_stream_id (u8 * s, va_list * args)
2611 {
2612   quicly_stream_t *stream = va_arg (*args, quicly_stream_t *);
2613   s =
2614     format (s, "%U S%lx", quic_format_quicly_conn_id,
2615             quicly_get_master_id (stream->conn), stream->stream_id);
2616   return s;
2617 }
2618
2619 static u8 *
2620 quic_format_listener_ctx (u8 * s, va_list * args)
2621 {
2622   quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2623   s = format (s, "[#%d][%x][Listener]", ctx->c_thread_index, ctx->c_c_index);
2624   return s;
2625 }
2626
2627 static u8 *
2628 quic_format_connection_ctx (u8 * s, va_list * args)
2629 {
2630   quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2631   quicly_stats_t quicly_stats;
2632
2633   s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2634
2635   if (!ctx->conn)
2636     {
2637       s = format (s, "- no conn -\n");
2638       return s;
2639     }
2640   s = format (s, "[%U]",
2641               quic_format_quicly_conn_id, quicly_get_master_id (ctx->conn));
2642   quicly_get_stats (ctx->conn, &quicly_stats);
2643
2644   s = format (s, "[RTT >%3d, ~%3d, V%3d, last %3d]",
2645               quicly_stats.rtt.minimum, quicly_stats.rtt.smoothed,
2646               quicly_stats.rtt.variance, quicly_stats.rtt.latest);
2647   s = format (s, " TX:%d RX:%d loss:%d ack:%d",
2648               quicly_stats.num_packets.sent,
2649               quicly_stats.num_packets.received,
2650               quicly_stats.num_packets.lost,
2651               quicly_stats.num_packets.ack_received);
2652   return s;
2653 }
2654
2655 static u8 *
2656 quic_format_stream_ctx (u8 * s, va_list * args)
2657 {
2658   quic_ctx_t *ctx = va_arg (*args, quic_ctx_t *);
2659   session_t *stream_session;
2660   quicly_stream_t *stream = ctx->stream;
2661   u32 txs, rxs;
2662
2663   s = format (s, "[#%d][%x]", ctx->c_thread_index, ctx->c_c_index);
2664   s = format (s, "[%U]", quic_format_quicly_stream_id, stream);
2665
2666   stream_session = session_get_if_valid (ctx->c_s_index, ctx->c_thread_index);
2667   if (!stream_session)
2668     {
2669       s = format (s, "- no session -\n");
2670       return s;
2671     }
2672   txs = svm_fifo_max_dequeue (stream_session->tx_fifo);
2673   rxs = svm_fifo_max_dequeue (stream_session->rx_fifo);
2674   s = format (s, "[rx %d tx %d]\n", rxs, txs);
2675   return s;
2676 }
2677
2678 static clib_error_t *
2679 quic_show_connections_command_fn (vlib_main_t * vm,
2680                                   unformat_input_t * input,
2681                                   vlib_cli_command_t * cmd)
2682 {
2683   unformat_input_t _line_input, *line_input = &_line_input;
2684   u8 show_listeners = 0, show_conn = 0, show_stream = 0;
2685   u32 num_workers = vlib_num_workers ();
2686   quic_main_t *qm = &quic_main;
2687   clib_error_t *error = 0;
2688   quic_ctx_t *ctx = NULL;
2689
2690   session_cli_return_if_not_enabled ();
2691
2692   if (!unformat_user (input, unformat_line_input, line_input))
2693     {
2694       quic_show_aggregated_stats (vm);
2695       return 0;
2696     }
2697
2698   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2699     {
2700       if (unformat (line_input, "listener"))
2701         show_listeners = 1;
2702       else if (unformat (line_input, "conn"))
2703         show_conn = 1;
2704       else if (unformat (line_input, "stream"))
2705         show_stream = 1;
2706       else
2707         {
2708           error = clib_error_return (0, "unknown input `%U'",
2709                                      format_unformat_error, line_input);
2710           goto done;
2711         }
2712     }
2713
2714   for (int i = 0; i < num_workers + 1; i++)
2715     {
2716       /* *INDENT-OFF* */
2717       pool_foreach (ctx, qm->ctx_pool[i],
2718       ({
2719         if (quic_ctx_is_stream (ctx) && show_stream)
2720           vlib_cli_output (vm, "%U", quic_format_stream_ctx, ctx);
2721         else if (quic_ctx_is_listener (ctx) && show_listeners)
2722           vlib_cli_output (vm, "%U", quic_format_listener_ctx, ctx);
2723         else if (quic_ctx_is_conn (ctx) && show_conn)
2724           vlib_cli_output (vm, "%U", quic_format_connection_ctx, ctx);
2725       }));
2726       /* *INDENT-ON* */
2727     }
2728
2729 done:
2730   unformat_free (line_input);
2731   return error;
2732 }
2733
2734 /* *INDENT-OFF* */
2735 VLIB_CLI_COMMAND (quic_plugin_crypto_command, static) =
2736 {
2737   .path = "quic set crypto api",
2738   .short_help = "quic set crypto api [picotls, vpp]",
2739   .function = quic_plugin_crypto_command_fn,
2740 };
2741 VLIB_CLI_COMMAND(quic_plugin_set_fifo_size_command, static)=
2742 {
2743   .path = "quic set fifo-size",
2744   .short_help = "quic set fifo-size N[K|M|G] (default 64K)",
2745   .function = quic_plugin_set_fifo_size_command_fn,
2746 };
2747 VLIB_CLI_COMMAND(quic_show_ctx_command, static)=
2748 {
2749   .path = "show quic",
2750   .short_help = "show quic",
2751   .function = quic_show_connections_command_fn,
2752 };
2753 VLIB_CLI_COMMAND (quic_list_crypto_context_command, static) =
2754 {
2755   .path = "show quic crypto context",
2756   .short_help = "list quic crypto contextes",
2757   .function = quic_list_crypto_context_command_fn,
2758 };
2759 VLIB_PLUGIN_REGISTER () =
2760 {
2761   .version = VPP_BUILD_VER,
2762   .description = "Quic transport protocol",
2763   .default_disabled = 1,
2764 };
2765 /* *INDENT-ON* */
2766
2767 static clib_error_t *
2768 quic_config_fn (vlib_main_t * vm, unformat_input_t * input)
2769 {
2770   quic_main_t *qm = &quic_main;
2771   uword tmp;
2772   u32 i;
2773
2774   qm->udp_fifo_size = QUIC_DEFAULT_FIFO_SIZE;
2775   qm->udp_fifo_prealloc = 0;
2776   qm->connection_timeout = QUIC_DEFAULT_CONN_TIMEOUT;
2777   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2778     {
2779       if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
2780         {
2781           if (tmp >= 0x100000000ULL)
2782             {
2783               return clib_error_return (0,
2784                                         "fifo-size %llu (0x%llx) too large",
2785                                         tmp, tmp);
2786             }
2787           qm->udp_fifo_size = tmp;
2788         }
2789       else if (unformat (input, "conn-timeout %u", &i))
2790         qm->connection_timeout = i;
2791       else if (unformat (input, "fifo-prealloc %u", &i))
2792         qm->udp_fifo_prealloc = i;
2793       else
2794         return clib_error_return (0, "unknown input '%U'",
2795                                   format_unformat_error, input);
2796     }
2797
2798   return 0;
2799 }
2800
2801 VLIB_EARLY_CONFIG_FUNCTION (quic_config_fn, "quic");
2802
2803 static uword
2804 quic_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
2805               vlib_frame_t * frame)
2806 {
2807   return 0;
2808 }
2809
2810 /* *INDENT-OFF* */
2811 VLIB_REGISTER_NODE (quic_input_node) =
2812 {
2813   .function = quic_node_fn,
2814   .name = "quic-input",
2815   .vector_size = sizeof (u32),
2816   .type = VLIB_NODE_TYPE_INTERNAL,
2817   .n_errors = ARRAY_LEN (quic_error_strings),
2818   .error_strings = quic_error_strings,
2819 };
2820 /* *INDENT-ON* */
2821
2822 /*
2823  * fd.io coding-style-patch-verification: ON
2824  *
2825  * Local Variables:
2826  * eval: (c-set-style "gnu")
2827  * End:
2828  */