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