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