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