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