FD.io VPP  v19.04.4-rc0-5-ge88582fac
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 #include <openssl/pem.h>
24 
25 #include <vppinfra/lock.h>
26 
27 #include <quic/quic.h>
28 
29 #include <quicly/streambuf.h>
30 #include <picotls/openssl.h>
31 #include <picotls/pembase64.h>
32 
34 
35 static void quic_update_timer (quic_ctx_t * ctx);
36 static int64_t quic_get_time (quicly_now_cb * self);
37 static void quic_connection_closed (u32 conn_index);
38 static void quic_disconnect (u32 ctx_index, u32 thread_index);
39 
40 #define QUIC_INT_MAX 0x3FFFFFFFFFFFFFFF
41 
42 u32
44 {
45  quic_main_t *qm = &quic_main;
46  u8 will_expand = 0;
47  quic_ctx_t *ctx;
48  u32 ctx_index;
49 
51  if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
52  {
54  pool_get (qm->half_open_ctx_pool, ctx);
55  ctx_index = ctx - qm->half_open_ctx_pool;
57  }
58  else
59  {
60  /* reader lock assumption: only main thread will call pool_get */
62  pool_get (qm->half_open_ctx_pool, ctx);
63  ctx_index = ctx - qm->half_open_ctx_pool;
65  }
66  memset (ctx, 0, sizeof (*ctx));
67  return ctx_index;
68 }
69 
70 void
72 {
73  quic_main_t *qm = &quic_main;
75  pool_put_index (qm->half_open_ctx_pool, ho_index);
77 }
78 
79 quic_ctx_t *
81 {
82  quic_main_t *qm = &quic_main;
84  return pool_elt_at_index (qm->half_open_ctx_pool, ctx_index);
85 }
86 
87 void
89 {
91 }
92 
93 u32
95 {
96  return (ctx - quic_main.half_open_ctx_pool);
97 }
98 
99 u32
101 {
102  u8 thread_index = vlib_get_thread_index ();
103  quic_main_t *qm = &quic_main;
104  quic_ctx_t *ctx;
105 
106  pool_get (qm->ctx_pool[thread_index], ctx);
107 
108  memset (ctx, 0, sizeof (quic_ctx_t));
109  ctx->c_thread_index = thread_index;
110  return ctx - qm->ctx_pool[thread_index];
111 }
112 
113 static void
115 {
116  QUIC_DBG (2, "Free ctx %u", ctx->c_c_index);
117  u32 thread_index = ctx->c_thread_index;
118  if (CLIB_DEBUG)
119  memset (ctx, 0xfb, sizeof (*ctx));
120  pool_put (quic_main.ctx_pool[thread_index], ctx);
121 }
122 
123 static quic_ctx_t *
124 quic_ctx_get (u32 ctx_index)
125 {
126  return pool_elt_at_index (quic_main.ctx_pool[vlib_get_thread_index ()],
127  ctx_index);
128 }
129 
130 static quic_ctx_t *
131 quic_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
132 {
133  return pool_elt_at_index (quic_main.ctx_pool[thread_index], ctx_index);
134 }
135 
136 static void
138 {
139  QUIC_DBG (2, "Called quic_disconnect_transport");
141  .handle = ctx->c_quic_ctx_id.quic_session,
142  .app_index = quic_main.app_index,
143  };
144 
145  if (vnet_disconnect_session (&a))
146  clib_warning ("UDP session disconnect errored");
147 }
148 
149 static int
150 quic_send_datagram (session_t * session, quicly_datagram_t * packet)
151 {
152  QUIC_DBG (2, "Called quic_send_datagram at %ld", quic_get_time (NULL));
153  u32 max_enqueue;
155  int rv;
156  u32 len;
157  svm_fifo_t *f;
159 
160  len = packet->data.len;
161  f = session->tx_fifo;
162  tc = session_get_transport (session);
163 
164  max_enqueue = svm_fifo_max_enqueue (f);
165  if (max_enqueue <= sizeof (session_dgram_hdr_t))
166  return 1;
167 
168  max_enqueue -= sizeof (session_dgram_hdr_t);
169 
170  if (max_enqueue < len)
171  return 1;
172 
173  // Build packet header for fifo
174  hdr.data_length = len;
175  hdr.data_offset = 0;
176  hdr.is_ip4 = tc->is_ip4;
177  clib_memcpy (&hdr.lcl_ip, &tc->lcl_ip, sizeof (ip46_address_t));
178  hdr.lcl_port = tc->lcl_port;
179 
180  // Read dest address from quicly-provided sockaddr
181  if (hdr.is_ip4)
182  {
183  ASSERT (packet->sa.sa_family == AF_INET);
184  struct sockaddr_in *sa4 = (struct sockaddr_in *) &packet->sa;
185  hdr.rmt_port = sa4->sin_port;
186  hdr.rmt_ip.ip4.as_u32 = sa4->sin_addr.s_addr;
187  }
188  else
189  {
190  ASSERT (packet->sa.sa_family == AF_INET6);
191  struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &packet->sa;
192  hdr.rmt_port = sa6->sin6_port;
193  clib_memcpy (&hdr.rmt_ip.ip6, &sa6->sin6_addr, 16);
194  }
195 
196  rv = svm_fifo_enqueue_nowait (f, sizeof (hdr), (u8 *) & hdr);
197  ASSERT (rv == sizeof (hdr));
198  if (svm_fifo_enqueue_nowait (f, len, packet->data.base) != len)
199  return 1;
200  return 0;
201 }
202 
203 static int
205 {
206  //QUIC_DBG (2, "Called quic_send_packets");
207  quicly_datagram_t *packets[16];
208  session_t *quic_session;
209  quicly_conn_t *conn;
210  size_t num_packets, i;
211  int ret;
212 
213  quic_session = session_get_from_handle (ctx->c_quic_ctx_id.quic_session);
214  conn = ctx->c_quic_ctx_id.conn;
215 
216  if (!conn)
217  return 0;
218 
219  do
220  {
221  num_packets = sizeof (packets) / sizeof (packets[0]);
222  if ((ret = quicly_send (conn, packets, &num_packets)) == 0)
223  {
224  for (i = 0; i != num_packets; ++i)
225  {
226  if (quic_send_datagram (quic_session, packets[i]))
227  {
228  QUIC_DBG (2, "quic_send_datagram failed");
229  goto stop_sending;
230  }
231  ret = 0;
232  quicly_default_free_packet_cb.cb
233  (&quicly_default_free_packet_cb, packets[i]);
234  }
235  }
236  else
237  {
238  QUIC_DBG (2, "quicly_send returned %d, closing connection\n", ret);
239  return ret;
240  }
241  }
242  while (ret == 0 && num_packets == sizeof (packets) / sizeof (packets[0]));
243 
244 stop_sending:
245  if (svm_fifo_set_event (quic_session->tx_fifo))
247 
248  quic_update_timer (ctx);
249  return 0;
250 }
251 
252 /*****************************************************************************
253  * START QUICLY CALLBACKS
254  * Called from QUIC lib
255  *****************************************************************************/
256 
257 static int
258 quic_on_stop_sending (quicly_stream_t * stream, int error_code)
259 {
260  QUIC_DBG (2, "received STOP_SENDING: %d", error_code);
261  return 0;
262 }
263 
264 static int
265 quic_on_receive_reset (quicly_stream_t * stream, int error_code)
266 {
267  QUIC_DBG (2, "received RESET_STREAM: %d", error_code);
268  return 0;
269 }
270 
271 static int
272 quic_on_receive (quicly_stream_t * stream, size_t off, const void *src,
273  size_t len)
274 {
275  QUIC_DBG (2, "received data: %lu bytes", len);
276  u32 to_enqueue, ctx_id;
277  quic_ctx_t *ctx;
278  session_t *app_session;
279  svm_fifo_t *rx_fifo;
280  app_worker_t *app_wrk;
281 
282  ctx_id = (u64) * quicly_get_data (stream->conn);
283  ctx = quic_ctx_get (ctx_id);
284  app_session = session_get_from_handle (ctx->c_quic_ctx_id.app_session);
285  rx_fifo = app_session->rx_fifo;
286  to_enqueue = svm_fifo_max_enqueue (rx_fifo);
287  if (to_enqueue > len)
288  to_enqueue = len;
289  // TODO what happens to the excess bytes?
290 
291  svm_fifo_enqueue_nowait (rx_fifo, to_enqueue, src);
292 
293  // Notify app
294  app_wrk = app_worker_get_if_valid (app_session->app_wrk_index);
295  if (PREDICT_TRUE (app_wrk != 0))
296  app_worker_lock_and_send_event (app_wrk, app_session, SESSION_IO_EVT_RX);
297  return 0;
298 }
299 
300 static const quicly_stream_callbacks_t quic_stream_callbacks = {
301  .on_destroy = quicly_streambuf_destroy,
302  .on_send_shift = quicly_streambuf_egress_shift,
303  .on_send_emit = quicly_streambuf_egress_emit,
304  .on_send_stop = quic_on_stop_sending,
305  .on_receive = quic_on_receive,
306  .on_receive_reset = quic_on_receive_reset
307 };
308 
309 static int
310 quic_on_stream_open (quicly_stream_open_cb * self, quicly_stream_t * stream)
311 {
312  QUIC_DBG (2, "on_stream_open called");
313  int ret;
314  if ((ret =
315  quicly_streambuf_create (stream, sizeof (quicly_streambuf_t))) != 0)
316  {
317  return ret;
318  }
319  stream->callbacks = &quic_stream_callbacks;
320  return 0;
321 }
322 
323 static quicly_stream_open_cb on_stream_open = { &quic_on_stream_open };
324 
325 static void
326 quic_on_conn_close (quicly_closed_by_peer_cb * self, quicly_conn_t * conn,
327  int code, uint64_t frame_type,
328  const char *reason, size_t reason_len)
329 {
330  QUIC_DBG (2, "connection closed, reason: %s", reason);
331  u32 ctx_index = (u64) * quicly_get_data (conn);
332  quic_connection_closed (ctx_index);
333 }
334 
335 static quicly_closed_by_peer_cb on_closed_by_peer = { &quic_on_conn_close };
336 
337 
338 /*****************************************************************************
339  * END QUICLY CALLBACKS
340  *****************************************************************************/
341 
342 /* single-entry session cache */
344 {
345  ptls_encrypt_ticket_t super;
346  uint8_t id[32];
347  ptls_iovec_t data;
348 };
349 
350 static int
351 encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
352  int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
353 {
354  struct st_util_session_cache_t *self = (void *) _self;
355  int ret;
356 
357  if (is_encrypt)
358  {
359 
360  /* replace the cached entry along with a newly generated session id */
361  free (self->data.base);
362  if ((self->data.base = malloc (src.len)) == NULL)
363  return PTLS_ERROR_NO_MEMORY;
364 
365  ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
366  memcpy (self->data.base, src.base, src.len);
367  self->data.len = src.len;
368 
369  /* store the session id in buffer */
370  if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
371  return ret;
372  memcpy (dst->base + dst->off, self->id, sizeof (self->id));
373  dst->off += sizeof (self->id);
374 
375  }
376  else
377  {
378 
379  /* check if session id is the one stored in cache */
380  if (src.len != sizeof (self->id))
381  return PTLS_ERROR_SESSION_NOT_FOUND;
382  if (memcmp (self->id, src.base, sizeof (self->id)) != 0)
383  return PTLS_ERROR_SESSION_NOT_FOUND;
384 
385  /* return the cached value */
386  if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
387  return ret;
388  memcpy (dst->base + dst->off, self->data.base, self->data.len);
389  dst->off += self->data.len;
390  }
391 
392  return 0;
393 }
394 
395 static struct st_util_session_cache_t sc = {
396  .super = {
397  .cb = encrypt_ticket_cb,
398  },
399 };
400 
401 /* *INDENT-OFF* */
402 static ptls_context_t quic_tlsctx = {
403  .random_bytes = ptls_openssl_random_bytes,
404  .get_time = &ptls_get_time,
405  .key_exchanges = ptls_openssl_key_exchanges,
406  .cipher_suites = ptls_openssl_cipher_suites,
407  .certificates = {
408  .list = NULL,
409  .count = 0
410  },
411  .esni = NULL,
412  .on_client_hello = NULL,
413  .emit_certificate = NULL,
414  .sign_certificate = NULL,
415  .verify_certificate = NULL,
416  .ticket_lifetime = 86400,
417  .max_early_data_size = 8192,
418  .hkdf_label_prefix__obsolete = NULL,
419  .require_dhe_on_psk = 1,
420  .encrypt_ticket = &sc.super,
421 };
422 /* *INDENT-ON* */
423 
424 static int
425 ptls_compare_separator_line (const char *line, const char *begin_or_end,
426  const char *label)
427 {
428  int ret = strncmp (line, "-----", 5);
429  size_t text_index = 5;
430 
431  if (ret == 0)
432  {
433  size_t begin_or_end_length = strlen (begin_or_end);
434  ret = strncmp (line + text_index, begin_or_end, begin_or_end_length);
435  text_index += begin_or_end_length;
436  }
437 
438  if (ret == 0)
439  {
440  ret = line[text_index] - ' ';
441  text_index++;
442  }
443 
444  if (ret == 0)
445  {
446  size_t label_length = strlen (label);
447  ret = strncmp (line + text_index, label, label_length);
448  text_index += label_length;
449  }
450 
451  if (ret == 0)
452  {
453  ret = strncmp (line + text_index, "-----", 5);
454  }
455 
456  return ret;
457 }
458 
459 static int
460 ptls_get_bio_pem_object (BIO * bio, const char *label, ptls_buffer_t * buf)
461 {
462  int ret = PTLS_ERROR_PEM_LABEL_NOT_FOUND;
463  char line[256];
464  ptls_base64_decode_state_t state;
465 
466  /* Get the label on a line by itself */
467  while (BIO_gets (bio, line, 256))
468  {
469  if (ptls_compare_separator_line (line, "BEGIN", label) == 0)
470  {
471  ret = 0;
472  ptls_base64_decode_init (&state);
473  break;
474  }
475  }
476  /* Get the data in the buffer */
477  while (ret == 0 && BIO_gets (bio, line, 256))
478  {
479  if (ptls_compare_separator_line (line, "END", label) == 0)
480  {
481  if (state.status == PTLS_BASE64_DECODE_DONE
482  || (state.status == PTLS_BASE64_DECODE_IN_PROGRESS
483  && state.nbc == 0))
484  {
485  ret = 0;
486  }
487  else
488  {
489  ret = PTLS_ERROR_INCORRECT_BASE64;
490  }
491  break;
492  }
493  else
494  {
495  ret = ptls_base64_decode (line, &state, buf);
496  }
497  }
498 
499  return ret;
500 }
501 
502 int
503 ptls_load_bio_pem_objects (BIO * bio, const char *label, ptls_iovec_t * list,
504  size_t list_max, size_t * nb_objects)
505 {
506  int ret = 0;
507  size_t count = 0;
508 
509  *nb_objects = 0;
510 
511  if (ret == 0)
512  {
513  while (count < list_max)
514  {
515  ptls_buffer_t buf;
516 
517  ptls_buffer_init (&buf, "", 0);
518 
519  ret = ptls_get_bio_pem_object (bio, label, &buf);
520 
521  if (ret == 0)
522  {
523  if (buf.off > 0 && buf.is_allocated)
524  {
525  list[count].base = buf.base;
526  list[count].len = buf.off;
527  count++;
528  }
529  else
530  {
531  ptls_buffer_dispose (&buf);
532  }
533  }
534  else
535  {
536  ptls_buffer_dispose (&buf);
537  break;
538  }
539  }
540  }
541 
542  if (ret == PTLS_ERROR_PEM_LABEL_NOT_FOUND && count > 0)
543  {
544  ret = 0;
545  }
546 
547  *nb_objects = count;
548 
549  return ret;
550 }
551 
552 #define PTLS_MAX_CERTS_IN_CONTEXT 16
553 
554 int
555 ptls_load_bio_certificates (ptls_context_t * ctx, BIO * bio)
556 {
557  int ret = 0;
558 
559  ctx->certificates.list =
560  (ptls_iovec_t *) malloc (PTLS_MAX_CERTS_IN_CONTEXT *
561  sizeof (ptls_iovec_t));
562 
563  if (ctx->certificates.list == NULL)
564  {
565  ret = PTLS_ERROR_NO_MEMORY;
566  }
567  else
568  {
569  ret =
570  ptls_load_bio_pem_objects (bio, "CERTIFICATE", ctx->certificates.list,
572  &ctx->certificates.count);
573  }
574 
575  return ret;
576 }
577 
578 static inline void
579 load_bio_certificate_chain (ptls_context_t * ctx, const char *cert_data)
580 {
581  BIO *cert_bio;
582  cert_bio = BIO_new_mem_buf (cert_data, -1);
583  if (ptls_load_bio_certificates (ctx, cert_bio) != 0)
584  {
585  BIO_free (cert_bio);
586  fprintf (stderr, "failed to load certificate:%s\n", strerror (errno));
587  exit (1);
588  }
589  BIO_free (cert_bio);
590 }
591 
592 static inline void
593 load_bio_private_key (ptls_context_t * ctx, const char *pk_data)
594 {
595  static ptls_openssl_sign_certificate_t sc;
596  EVP_PKEY *pkey;
597  BIO *key_bio;
598 
599  key_bio = BIO_new_mem_buf (pk_data, -1);
600  pkey = PEM_read_bio_PrivateKey (key_bio, NULL, NULL, NULL);
601  BIO_free (key_bio);
602 
603  if (pkey == NULL)
604  {
605  fprintf (stderr, "failed to read private key from app configuration\n");
606  exit (1);
607  }
608 
609  ptls_openssl_init_sign_certificate (&sc, pkey);
610  EVP_PKEY_free (pkey);
611 
612  ctx->sign_certificate = &sc.super;
613 }
614 
615 static void
617 {
618  QUIC_DBG (2, "QUIC connection closed");
619  quic_ctx_t *ctx;
620 
621  ctx = quic_ctx_get (ctx_index);
622  // TODO if connection is not established, just delete the session
623 
624  // Do not try to send anything anymore
625  ctx->stream = NULL;
626  quicly_free (ctx->c_quic_ctx_id.conn);
627  ctx->c_quic_ctx_id.conn = NULL;
629 }
630 
631 static int64_t
632 quic_get_time (quicly_now_cb * self)
633 {
634  // TODO read value set by set_time_now?
635  // (needs to change it not to call this function)
637  f64 time = vlib_time_now (vlib_main);
638  return (int64_t) (time * 1000.f);
639 }
640 quicly_now_cb quicly_vpp_now_cb = { quic_get_time };
641 
642 static void
644 {
645  QUIC_DBG (2, "Called allocate_quicly_ctx");
646  struct
647  {
648  quicly_context_t _;
649  char cid_key[17];
650  } *ctx_data;
651  quicly_context_t *quicly_ctx;
652  char *cid_key;
653 
654  ctx_data = malloc (sizeof (*ctx_data));
655  quicly_ctx = &ctx_data->_;
656  app->quicly_ctx = (u64 *) quicly_ctx;
657  memcpy (quicly_ctx, &quicly_default_context, sizeof (quicly_context_t));
658 
659  quicly_ctx->tls = &quic_tlsctx;
660  quicly_ctx->stream_open = &on_stream_open;
661  quicly_ctx->closed_by_peer = &on_closed_by_peer;
662  quicly_ctx->now = &quicly_vpp_now_cb;
663 
664  quicly_amend_ptls_context (quicly_ctx->tls);
665 
666  quicly_ctx->event_log.mask = INT64_MAX;
667  quicly_ctx->event_log.cb = quicly_new_default_event_log_cb (stderr);
668 
669  quicly_ctx->transport_params.max_data = QUIC_INT_MAX;
670  quicly_ctx->transport_params.max_streams_uni = QUIC_INT_MAX;
671  quicly_ctx->transport_params.max_streams_bidi = QUIC_INT_MAX;
672  quicly_ctx->transport_params.max_stream_data.bidi_local = QUIC_INT_MAX;
673  quicly_ctx->transport_params.max_stream_data.bidi_remote = QUIC_INT_MAX;
674  quicly_ctx->transport_params.max_stream_data.uni = QUIC_INT_MAX;
675 
676  if (!is_client)
677  {
678  load_bio_private_key (quicly_ctx->tls, (char *) app->tls_key);
679  load_bio_certificate_chain (quicly_ctx->tls, (char *) app->tls_cert);
680  cid_key = ctx_data->cid_key;
681  quicly_ctx->tls->random_bytes (cid_key, 16);
682  cid_key[16] = 0;
683  quicly_ctx->encrypt_cid =
684  quicly_new_default_encrypt_cid_cb (&ptls_openssl_bfecb,
685  &ptls_openssl_sha256,
686  ptls_iovec_init (cid_key,
687  strlen
688  (cid_key)));
689  quicly_ctx->decrypt_cid =
690  quicly_new_default_decrypt_cid_cb (&ptls_openssl_bfecb,
691  &ptls_openssl_sha256,
692  ptls_iovec_init (cid_key,
693  strlen
694  (cid_key)));
695  }
696 }
697 
698 
699 /*****************************************************************************
700  * BEGIN TIMERS HANDLING
701  *****************************************************************************/
702 
703 static u32
704 quic_set_time_now (u32 thread_index)
705 {
706  quic_main.wrk_ctx[thread_index].time_now = quic_get_time (NULL);
707  return quic_main.wrk_ctx[thread_index].time_now;
708 }
709 
710 static void
712 {
713  quic_ctx_t *ctx;
714  QUIC_DBG (2, "Timer expired for conn %u at %ld", conn_index,
715  quic_get_time (NULL));
716  ctx = quic_ctx_get (conn_index);
717  ctx->c_quic_ctx_id.timer_handle = QUIC_TIMER_HANDLE_INVALID;
718  if (quic_send_packets (ctx))
719  {
720  quic_connection_closed (conn_index);
721  }
722 }
723 
724 static void
726 {
727  tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
728  int64_t next_timeout;
729 
730  // This timeout is in ms which is the unit of our timer
731  next_timeout = quicly_get_first_timeout (ctx->c_quic_ctx_id.conn);
732  tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
733  f64 next_timeout_f = ((f64) next_timeout) / 1000.f;
734 
735  clib_warning ("Timer set to %ld (%lf)", next_timeout, next_timeout_f);
736 
737  if (ctx->c_quic_ctx_id.timer_handle == QUIC_TIMER_HANDLE_INVALID)
738  {
739  if (next_timeout == INT64_MAX)
740  return;
741  ctx->c_quic_ctx_id.timer_handle =
742  tw_timer_start_1t_3w_1024sl_ov (tw, ctx->c_c_index, 0,
743  next_timeout_f);
744  }
745  else
746  {
747  if (next_timeout == INT64_MAX)
748  {
749  tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->c_quic_ctx_id.timer_handle);
750  ctx->c_quic_ctx_id.timer_handle = QUIC_TIMER_HANDLE_INVALID;
751  }
752  else
753  tw_timer_update_1t_3w_1024sl_ov (tw, ctx->c_quic_ctx_id.timer_handle,
754  next_timeout_f);
755  }
756 }
757 
758 static void
760 {
761  int i;
762 
763  for (i = 0; i < vec_len (expired_timers); i++)
764  {
765  quic_timer_expired (expired_timers[i]);
766  }
767 }
768 
769 
770 /*****************************************************************************
771  * END TIMERS HANDLING
772  *
773  * BEGIN TRANSPORT PROTO FUNCTIONS
774  *****************************************************************************/
775 
776 int
778 {
779  QUIC_DBG (2, "Called quic_connect");
780  vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
782  quic_main_t *qm = &quic_main;
783  quic_ctx_t *ctx;
784  app_worker_t *app_wrk;
785  application_t *app;
786  u32 ctx_index;
787  int error;
788 
789  sep = (session_endpoint_cfg_t *) tep;
790  ctx_index = quic_ctx_half_open_alloc ();
791  ctx = quic_ctx_half_open_get (ctx_index);
792  ctx->c_quic_ctx_id.parent_app_wrk_idx = sep->app_wrk_index;
793  ctx->c_s_index = 0xFAFAFAFA;
794  ctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
795  ctx->c_quic_ctx_id.timer_handle = QUIC_TIMER_HANDLE_INVALID;
796  ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_HANDSHAKE;
797  ctx->client_opaque = sep->opaque;
798  if (sep->hostname)
799  {
800  ctx->srv_hostname = format (0, "%v", sep->hostname);
802  }
803  else
804  {
805  // needed by quic for crypto + determining client / server
806  ctx->srv_hostname =
807  format (0, "%U", format_ip46_address, &sep->ip, sep->is_ip4);
808  }
809 
811 
812  clib_memcpy (&cargs->sep, sep, sizeof (session_endpoint_cfg_t));
813  cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
814  cargs->app_index = qm->app_index;
815  cargs->api_context = ctx_index;
816 
817  app_wrk = app_worker_get (sep->app_wrk_index);
818  app = application_get (app_wrk->app_index);
819  ctx->c_quic_ctx_id.parent_app_id = app_wrk->app_index;
820  cargs->sep_ext.ns_index = app->ns_index;
821 
822  allocate_quicly_ctx (app, 1 /* is client */ );
823 
824  if ((error = vnet_connect (cargs)))
825  return error;
826 
827  QUIC_DBG (1, "New connect request %u", ctx_index);
828  return 0;
829 }
830 
831 static void
832 quic_disconnect (u32 ctx_index, u32 thread_index)
833 {
834  QUIC_DBG (2, "Called quic_disconnect");
835  tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
836  quic_ctx_t *ctx;
837 
838  QUIC_DBG (1, "Disconnecting %x", ctx_index);
839 
840  ctx = quic_ctx_get (ctx_index);
841  if (ctx->c_quic_ctx_id.timer_handle != QUIC_TIMER_HANDLE_INVALID)
842  {
843  tw = &quic_main.wrk_ctx[vlib_get_thread_index ()].timer_wheel;
844  tw_timer_stop_1t_3w_1024sl_ov (tw, ctx->c_quic_ctx_id.timer_handle);
845  }
847  // This removes the session from the lookup table and frees it.
849  quic_ctx_free (ctx);
850 }
851 
852 u32
853 quic_start_listen (u32 app_listen_session_index, transport_endpoint_t * tep)
854 {
855  QUIC_DBG (2, "Called quic_start_listen");
856  vnet_listen_args_t _bargs, *args = &_bargs;
857  quic_main_t *qm = &quic_main;
858  session_handle_t udp_handle;
860  session_t *quic_listen_session, *app_listen_session;
861  app_worker_t *app_wrk;
862  application_t *app;
863  quic_ctx_t *lctx;
864  u32 lctx_index;
865  app_listener_t *app_listener;
866 
867  sep = (session_endpoint_cfg_t *) tep;
868  app_wrk = app_worker_get (sep->app_wrk_index);
869  app = application_get (app_wrk->app_index);
870 
871  allocate_quicly_ctx (app, 0 /* is_client */ );
872 
873  sep->transport_proto = TRANSPORT_PROTO_UDP;
874  memset (args, 0, sizeof (*args));
875  args->app_index = qm->app_index;
876  args->sep_ext = *sep;
877  args->sep_ext.ns_index = app->ns_index;
878  if (vnet_listen (args))
879  return -1;
880 
881  lctx_index = quic_ctx_alloc (); // listener
882  udp_handle = args->handle;
883  app_listener = app_listener_get_w_handle (udp_handle);
884  quic_listen_session = app_listener_get_session (app_listener);
885  quic_listen_session->opaque = lctx_index;
886 
887  app_listen_session = listen_session_get (app_listen_session_index);
888 
889  lctx = quic_ctx_get (lctx_index); // listener
890  lctx->is_listener = 1;
891  lctx->c_quic_ctx_id.parent_app_wrk_idx = sep->app_wrk_index;
892  lctx->c_quic_ctx_id.parent_app_id = app_wrk->app_index;
893  lctx->c_quic_ctx_id.quic_session = udp_handle;
894  lctx->c_quic_ctx_id.app_session =
895  listen_session_get_handle (app_listen_session);
896  lctx->c_quic_ctx_id.udp_is_ip4 = sep->is_ip4;
897 
898  QUIC_DBG (1, "Started listening %d", lctx_index);
899  return lctx_index;
900 }
901 
902 u32
903 quic_stop_listen (u32 lctx_index)
904 {
905  QUIC_DBG (2, "Called quic_stop_listen");
906  quic_ctx_t *lctx;
907 
908  lctx = quic_ctx_get (lctx_index); // listener
910  .handle = lctx->c_quic_ctx_id.quic_session,
911  .app_index = quic_main.app_index,
912  .wrk_map_index = 0 /* default wrk */
913  };
914  if (vnet_unlisten (&a))
915  clib_warning ("unlisten errored");
916 
917  // TODO: crypto state cleanup
918 
919  quic_ctx_free (lctx); // listener
920  return 0;
921 }
922 
924 quic_connection_get (u32 ctx_index, u32 thread_index)
925 {
926  QUIC_DBG (2, "Called quic_connection_get");
927  quic_ctx_t *ctx;
928  ctx = quic_ctx_get_w_thread (ctx_index, thread_index);
929  return &ctx->connection;
930 }
931 
933 quic_listener_get (u32 listener_index)
934 {
935  QUIC_DBG (2, "Called quic_listener_get");
936  quic_ctx_t *ctx;
937  ctx = quic_ctx_get (listener_index);
938  return &ctx->connection;
939 }
940 
941 static void
942 quic_update_time (f64 now, u8 thread_index)
943 {
944  tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
945 
946  tw = &quic_main.wrk_ctx[thread_index].timer_wheel;
947  quic_set_time_now (thread_index);
948  tw_timer_expire_timers_1t_3w_1024sl_ov (tw, now);
949 }
950 
951 static u8 *
952 format_quic_connection (u8 * s, va_list * args)
953 {
954  s = format (s, "[QUIC] connection");
955  return s;
956 }
957 
958 static u8 *
959 format_quic_half_open (u8 * s, va_list * args)
960 {
961  u32 qc_index = va_arg (*args, u32);
962  quic_ctx_t *ctx = quic_ctx_half_open_get (qc_index);
963  s = format (s, "[QUIC] half-open app %u", ctx->c_quic_ctx_id.parent_app_id);
965  return s;
966 }
967 
968 // TODO improve
969 static u8 *
970 format_quic_listener (u8 * s, va_list * args)
971 {
972  s = format (s, "[QUIC] listener");
973  return s;
974 }
975 
976 /*****************************************************************************
977  * END TRANSPORT PROTO FUNCTIONS
978  *
979  * START SESSION CALLBACKS
980  * Called from UDP layer
981  *****************************************************************************/
982 
983 static inline void
984 quic_build_sockaddr (struct sockaddr *sa, socklen_t * salen,
985  ip46_address_t * addr, u16 port, u8 is_ip4)
986 {
987  if (is_ip4)
988  {
989  struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
990  sa4->sin_family = AF_INET;
991  sa4->sin_port = port;
992  sa4->sin_addr.s_addr = addr->ip4.as_u32;
993  *salen = sizeof (struct sockaddr_in);
994  }
995  else
996  {
997  struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
998  sa6->sin6_family = AF_INET6;
999  sa6->sin6_port = port;
1000  clib_memcpy (&sa6->sin6_addr, &addr->ip6, 16);
1001  *salen = sizeof (struct sockaddr_in6);
1002  }
1003 }
1004 
1005 static int
1007 {
1008  QUIC_DBG (1, "quic_notify_app_connected");
1009  session_t *app_session;
1010  app_worker_t *app_wrk;
1011  quic_ctx_t *ctx;
1012  ctx = quic_ctx_get ((u32) (u64) ctx_index);
1013 
1014  app_wrk = app_worker_get_if_valid (ctx->c_quic_ctx_id.parent_app_wrk_idx);
1015  if (!app_wrk)
1016  {
1018  return -1;
1019  }
1020 
1021  app_session = session_alloc (ctx->c_thread_index);
1022  QUIC_DBG (1, "Created app_session, id %u", app_session->session_index);
1023  ctx->c_s_index = app_session->session_index;
1024  app_session->app_wrk_index = ctx->c_quic_ctx_id.parent_app_wrk_idx;
1025  app_session->connection_index = ctx->c_c_index;
1026  app_session->session_type =
1028  ctx->c_quic_ctx_id.udp_is_ip4);
1029 
1030  if (app_worker_init_connected (app_wrk, app_session)) // TODO dont allocate fifos
1031  {
1032  quic_disconnect (ctx->c_c_index, vlib_get_thread_index ());
1033  return app_worker_connect_notify (app_wrk, NULL, ctx->client_opaque);
1034  }
1035 
1036  app_session->session_state = SESSION_STATE_CONNECTING;
1037  if (app_worker_connect_notify (app_wrk, app_session, ctx->client_opaque))
1038  {
1039  QUIC_DBG (1, "failed to notify app");
1040  quic_disconnect (ctx->c_c_index, vlib_get_thread_index ());
1041  return -1;
1042  }
1043 
1044  ctx->c_quic_ctx_id.app_session = session_handle (app_session);
1045  app_session->session_state = SESSION_STATE_LISTENING;
1047  session_handle (app_session));
1048 
1049  return 0;
1050 }
1051 
1052 int
1053 quic_session_connected_callback (u32 quic_app_index, u32 ho_ctx_idx,
1054  session_t * s, u8 is_fail)
1055 {
1056  QUIC_DBG (2, "Called quic_session_connected_callback");
1057  // This should always be called before quic_connect returns since UDP always
1058  // connects instantly.
1059  struct sockaddr_in6 sa6;
1060  struct sockaddr *sa = (struct sockaddr *) &sa6;
1061  socklen_t salen;
1063  quic_ctx_t *ho_ctx, *ctx;
1064  u32 ctx_index;
1065  int ret;
1066  application_t *app;
1067  app_worker_t *app_wrk;
1068 
1069  ho_ctx = quic_ctx_half_open_get (ho_ctx_idx);
1070  if (is_fail)
1071  {
1072  u32 api_context;
1073  int rv = 0;
1074 
1075  app_wrk =
1076  app_worker_get_if_valid (ho_ctx->c_quic_ctx_id.parent_app_wrk_idx);
1077  if (app_wrk)
1078  {
1079  api_context = ho_ctx->c_s_index;
1080  app_worker_connect_notify (app_wrk, 0, api_context);
1081  }
1083  quic_ctx_half_open_free (ho_ctx_idx);
1084  return rv;
1085  }
1086 
1087  app_wrk =
1088  app_worker_get_if_valid (ho_ctx->c_quic_ctx_id.parent_app_wrk_idx);
1089  if (!app_wrk)
1090  {
1091  QUIC_DBG (1, "Appwrk not found");
1092  return -1;
1093  }
1094  app = application_get (app_wrk->app_index);
1095 
1096  ctx_index = quic_ctx_alloc ();
1097  ctx = quic_ctx_get (ctx_index);
1098  clib_memcpy (ctx, ho_ctx, sizeof (*ctx));
1099  quic_ctx_half_open_reader_unlock (); // TODO: this is a race
1100  quic_ctx_half_open_free (ho_ctx_idx);
1101 
1102  ctx->c_thread_index = vlib_get_thread_index ();
1103  ctx->c_c_index = ctx_index;
1104 
1105  QUIC_DBG (1, "Quic connect for returned %u. New connection [%u]%x",
1106  is_fail, vlib_get_thread_index (), (ctx) ? ctx_index : ~0);
1107 
1108  ctx->c_quic_ctx_id.quic_session = session_handle (s);
1109  s->opaque = ctx_index;
1111 
1112  // Init QUIC lib connection
1113  // Generate required sockaddr & salen
1114  tc = session_get_transport (s);
1115  quic_build_sockaddr (sa, &salen, &tc->rmt_ip, tc->rmt_port, tc->is_ip4);
1116 
1117  ret =
1118  quicly_connect (&ctx->c_quic_ctx_id.conn,
1119  (quicly_context_t *) app->quicly_ctx,
1120  (char *) ctx->srv_hostname, sa, salen,
1121  &quic_main.next_cid, &quic_main.hs_properties, NULL);
1122  ++quic_main.next_cid.master_id;
1123  // Save context handle in quicly connection
1124  *quicly_get_data (ctx->c_quic_ctx_id.conn) = (void *) (u64) ctx_index;
1125  assert (ret == 0);
1126 
1127  if (quic_send_packets (ctx))
1128  {
1129  quic_connection_closed (ctx_index);
1130  }
1131  return ret;
1132 }
1133 
1134 void
1136 {
1137  clib_warning ("UDP session disconnected???");
1138 }
1139 
1140 void
1142 {
1143  clib_warning ("UDP session reset???");
1144 }
1145 
1146 int
1148 {
1149  /* never called */
1150  return 0;
1151 }
1152 
1153 static int
1154 quic_add_segment_callback (u32 client_index, u64 seg_handle)
1155 {
1156  QUIC_DBG (2, "Called quic_add_segment_callback");
1157  QUIC_DBG (2, "NOT IMPLEMENTED");
1158  /* No-op for builtin */
1159  return 0;
1160 }
1161 
1162 static int
1163 quic_del_segment_callback (u32 client_index, u64 seg_handle)
1164 {
1165  QUIC_DBG (2, "Called quic_del_segment_callback");
1166  QUIC_DBG (2, "NOT IMPLEMENTED");
1167  /* No-op for builtin */
1168  return 0;
1169 }
1170 
1171 int
1173 {
1174  if (svm_fifo_set_event (s->tx_fifo))
1177  return 0;
1178 }
1179 
1180 void
1182 {
1183  quicly_conn_t *conn = ctx->c_quic_ctx_id.conn;
1184  if (ctx->stream)
1185  {
1186  QUIC_DBG (2, "----------- > FOUND Stream id %d",
1187  ctx->stream->stream_id);
1188  QUIC_DBG (2, "----------- > FOUND Stream is_open %d",
1189  ctx->stream->sendstate.is_open);
1190  return;
1191  }
1192  if (quicly_connection_is_ready (conn))
1193  assert (!quicly_open_stream (conn, &ctx->stream, 0));
1194  QUIC_DBG (2, "Stream id %d", ctx->stream->stream_id);
1195  QUIC_DBG (2, "Stream is_open %d", ctx->stream->sendstate.is_open);
1196 }
1197 
1198 int
1200 {
1201  QUIC_DBG (2, "Called quic_custom_tx_callback");
1202  session_t *app_session = (session_t *) session;
1203  quic_ctx_t *ctx;
1204  svm_fifo_t *f;
1205  u32 deq_max;
1206  u8 *data;
1207 
1208  if (PREDICT_FALSE
1210  return 0;
1211  ctx = quic_ctx_get (app_session->connection_index);
1213  if (!ctx->stream)
1214  {
1215  quic_add_vpp_q_builtin_tx_evt (app_session);
1216  return 0;
1217  }
1218 
1219  f = app_session->tx_fifo;
1220  deq_max = svm_fifo_max_dequeue (f);
1221  if (!deq_max)
1222  return 0;
1223 
1224  data = svm_fifo_head (f);
1225  if (quicly_streambuf_egress_write (ctx->stream, data, deq_max))
1226  {
1227  assert (0);
1228  return 0;
1229  }
1230  QUIC_DBG (2, "Sent %u bytes", deq_max);
1231  svm_fifo_dequeue_drop (f, deq_max);
1232  if (quic_send_packets (ctx))
1233  {
1234  quic_connection_closed (ctx->c_c_index);
1235  }
1236  return 0;
1237 }
1238 
1239 int
1240 quic_find_packet_ctx (quic_ctx_t ** ctx, quicly_conn_t ** conn,
1241  struct sockaddr *sa, socklen_t salen,
1242  quicly_decoded_packet_t packet)
1243 {
1244  quic_ctx_t *ctx_;
1245  quicly_conn_t *conn_;
1246  /* *INDENT-OFF* */
1247  pool_foreach (ctx_, quic_main.ctx_pool[vlib_get_thread_index()],
1248  ({
1249  conn_ = ctx_->c_quic_ctx_id.conn;
1250  if (conn_ && !ctx_->is_listener)
1251  {
1252  if (quicly_is_destination(conn_, sa, salen, &packet))
1253  {
1254  *conn = conn_;
1255  *ctx = ctx_;
1256  QUIC_DBG (2, "connection_found");
1257  return 0;
1258  }
1259  }
1260  }));
1261  /* *INDENT-ON* */
1262  return 0;
1263 }
1264 
1265 static int
1266 quic_receive (quic_ctx_t * ctx, quicly_conn_t * conn,
1267  quicly_decoded_packet_t packet)
1268 {
1269  quicly_receive (conn, &packet);
1270  // Conn may be set to null if the connection is terminated
1271  if (ctx->c_quic_ctx_id.conn
1272  && ctx->c_quic_ctx_id.conn_state == QUIC_CONN_STATE_HANDSHAKE)
1273  {
1274  if (quicly_connection_is_ready (conn))
1275  {
1276  ctx->c_quic_ctx_id.conn_state = QUIC_CONN_STATE_READY;
1277  if (quicly_is_client (conn))
1280  (void *) (u64)
1281  ctx->c_c_index);
1282  }
1283  }
1284  if (quic_send_packets (ctx))
1285  {
1286  quic_connection_closed (ctx->c_c_index);
1287  }
1288  return 0;
1289 }
1290 
1291 static int
1293 {
1294  quic_ctx_t *lctx, *ctx;
1295  session_t *app_session, *app_listen_session;
1296  app_worker_t *app_wrk;
1297  int rv;
1298 
1299  ctx = quic_ctx_get ((u32) (u64) ctx_index);
1300  app_session = session_alloc (ctx->c_thread_index);
1301  app_session->session_state = SESSION_STATE_LISTENING;
1302  ctx->c_s_index = app_session->session_index;
1303 
1304  lctx = quic_ctx_get (ctx->c_quic_ctx_id.listener_ctx_id);
1305 
1306  app_listen_session =
1307  listen_session_get_from_handle (lctx->c_quic_ctx_id.app_session);
1308  app_session->app_wrk_index = lctx->c_quic_ctx_id.parent_app_wrk_idx;
1309  app_session->connection_index = ctx->c_c_index;
1310  app_session->session_type = app_listen_session->session_type;
1311  app_session->listener_index = app_listen_session->session_index;
1312  app_session->app_index = quic_main.app_index;
1313 
1314  // TODO: don't alloc fifos when we don't transfer data on this session
1315  if ((rv = app_worker_init_accepted (app_session)))
1316  {
1317  QUIC_DBG (1, "failed to allocate fifos");
1318  session_free (app_session);
1319  return rv;
1320  }
1321  ctx->c_quic_ctx_id.app_session = session_handle (app_session);
1322  ctx->c_quic_ctx_id.parent_app_id = lctx->c_quic_ctx_id.parent_app_id;
1323  ctx->c_quic_ctx_id.udp_is_ip4 = lctx->c_quic_ctx_id.udp_is_ip4;
1324  ctx->c_quic_ctx_id.parent_app_wrk_idx = app_session->app_wrk_index;
1326  session_handle (app_session));
1327  app_wrk = app_worker_get (app_session->app_wrk_index);
1328  rv = app_worker_accept_notify (app_wrk, app_session);
1329  if (rv)
1330  {
1331  QUIC_DBG (1, "failed to notify accept worker app");
1332  return rv;
1333  }
1334  return 0;
1335 }
1336 
1337 static int
1338 quic_create_connection (quicly_context_t * quicly_ctx,
1339  u64 quic_session_handle, u32 lctx_index,
1340  quicly_conn_t * conn, struct sockaddr *sa,
1341  socklen_t salen, quicly_decoded_packet_t packet)
1342 {
1343  quic_ctx_t *ctx;
1344  u32 ctx_index;
1345 
1346  /* new connection, accept and create context if packet is valid */
1347  // TODO: check if socket is actually listening?
1348  QUIC_DBG (2, "New connection created");
1349  if (quicly_accept (&conn, quicly_ctx, sa, salen,
1350  &packet, ptls_iovec_init (NULL, 0),
1351  &quic_main.next_cid, NULL) != 0)
1352  {
1353  // Invalid packet, pass
1354  assert (conn == NULL);
1355  QUIC_DBG (2, "Accept failed");
1356  return 0;
1357  }
1358  assert (conn != NULL);
1359 
1360  ++quic_main.next_cid.master_id;
1361  // Create context
1362  ctx_index = quic_ctx_alloc ();
1363  ctx = quic_ctx_get (ctx_index);
1364  // Save ctx handle in quicly connection
1365  *quicly_get_data (conn) = (void *) (u64) ctx_index;
1366 
1367  ctx->c_thread_index = vlib_get_thread_index ();
1368  ctx->c_c_index = ctx_index;
1369  ctx->c_quic_ctx_id.quic_session = quic_session_handle;
1370  ctx->c_quic_ctx_id.listener_ctx_id = lctx_index;
1371  ctx->c_quic_ctx_id.timer_handle = QUIC_TIMER_HANDLE_INVALID;
1372  ctx->c_quic_ctx_id.conn = conn;
1373 
1376  (void *) (u64) ctx_index);
1377  if (quic_send_packets (ctx))
1378  {
1379  quic_connection_closed (ctx_index);
1380  }
1381  return 0;
1382 }
1383 
1384 static int
1385 quic_reset_connection (quicly_context_t * quicly_ctx, u64 quic_session_handle,
1386  struct sockaddr *sa, socklen_t salen,
1387  quicly_decoded_packet_t packet)
1388 {
1389  /* short header packet; potentially a dead connection. No need to check the length of the incoming packet,
1390  * because loop is prevented by authenticating the CID (by checking node_id and thread_id). If the peer is also
1391  * sending a reset, then the next CID is highly likely to contain a non-authenticating CID, ... */
1392  QUIC_DBG (2, "Sending stateless reset");
1393  quicly_datagram_t *dgram;
1394  session_t *quic_session;
1395  if (packet.cid.dest.plaintext.node_id == 0
1396  && packet.cid.dest.plaintext.thread_id == 0)
1397  {
1398  dgram = quicly_send_stateless_reset (quicly_ctx, sa, salen,
1399  &packet.cid.dest.plaintext);
1400  quic_session = session_get_from_handle (quic_session_handle);
1401  if (quic_send_datagram (quic_session, dgram)) // TODO : missing event on fifo
1402  QUIC_DBG (2, "Send reset failed");
1403  }
1404  return 0;
1405 }
1406 
1407 int
1409 {
1410  // Read data from UDP rx_fifo and pass it to the quicly conn.
1411  QUIC_DBG (2, "Called quic_app_rx_callback");
1412 
1413  quicly_decoded_packet_t packet;
1415  application_t *app;
1416  quicly_conn_t *conn = NULL;
1417  quic_ctx_t *lctx, *ctx = NULL;
1418  svm_fifo_t *f;
1419  size_t plen;
1420  struct sockaddr_in6 sa6;
1421  struct sockaddr *sa = (struct sockaddr *) &sa6;
1422  socklen_t salen;
1423  u32 max_deq, len;
1424  u8 *data;
1425  u32 lctx_index = quic_session->opaque;
1426  u64 quic_session_handle = session_handle (quic_session);
1427 
1428  f = quic_session->rx_fifo;
1429 
1430  do
1431  {
1432  conn = NULL;
1433  max_deq = svm_fifo_max_dequeue (f);
1434  if (max_deq < sizeof (session_dgram_hdr_t))
1435  {
1437  return 0;
1438  }
1439  QUIC_DBG (2, "Processing one packet at %ld", quic_get_time (NULL));
1440 
1442  svm_fifo_peek (f, 0, sizeof (ph), (u8 *) & ph);
1443  ASSERT (ph.data_length >= ph.data_offset);
1444  len = ph.data_length - ph.data_offset;
1445 
1446  quic_build_sockaddr (sa, &salen, &ph.rmt_ip, ph.rmt_port, ph.is_ip4);
1447 
1448  // Quicly can read len bytes from the fifo at offset:
1449  // ph.data_offset + SESSION_CONN_HDR_LEN
1450  data = svm_fifo_head (f) + ph.data_offset + SESSION_CONN_HDR_LEN;
1451 
1452  lctx = quic_ctx_get (lctx_index);
1453  app = application_get (lctx->c_quic_ctx_id.parent_app_id);
1454 
1455  plen =
1456  quicly_decode_packet ((quicly_context_t *) app->quicly_ctx, &packet,
1457  data, len);
1458  if (plen != SIZE_MAX)
1459  {
1460  quic_find_packet_ctx (&ctx, &conn, sa, salen, packet);
1461  if (conn != NULL)
1462  quic_receive (ctx, conn, packet);
1463  else if (QUICLY_PACKET_IS_LONG_HEADER (packet.octets.base[0]))
1464  quic_create_connection ((quicly_context_t *) app->quicly_ctx,
1465  quic_session_handle, lctx_index, conn,
1466  sa, salen, packet);
1467  else if (((quicly_context_t *) app->quicly_ctx)->encrypt_cid)
1468  quic_reset_connection ((quicly_context_t *) app->quicly_ctx,
1469  quic_session_handle, sa, salen, packet);
1470  }
1472  ph.data_length + ph.data_offset +
1474  }
1475  while (1);
1476  return 0;
1477 }
1478 
1479 /*****************************************************************************
1480  * END TRANSPORT PROTO FUNCTIONS
1481 *****************************************************************************/
1482 
1483 /* *INDENT-OFF* */
1485  .session_accept_callback = quic_session_accepted_callback,
1486  .session_disconnect_callback = quic_session_disconnect_callback,
1487  .session_connected_callback = quic_session_connected_callback,
1488  .session_reset_callback = quic_session_reset_callback,
1489  .add_segment_callback = quic_add_segment_callback,
1490  .del_segment_callback = quic_del_segment_callback,
1491  .builtin_app_rx_callback = quic_app_rx_callback,
1492 };
1493 
1495  .connect = quic_connect,
1496  .close = quic_disconnect,
1497  .start_listen = quic_start_listen,
1498  .stop_listen = quic_stop_listen,
1499  .get_connection = quic_connection_get,
1500  .get_listener = quic_listener_get,
1501  .update_time = quic_update_time,
1502  .custom_tx = quic_custom_tx_callback,
1503  .tx_type = TRANSPORT_TX_INTERNAL,
1504  .service_type = TRANSPORT_SERVICE_APP,
1505  .format_connection = format_quic_connection,
1506  .format_half_open = format_quic_half_open,
1507  .format_listener = format_quic_listener,
1508 };
1509 /* *INDENT-ON* */
1510 
1511 static clib_error_t *
1513 {
1514  QUIC_DBG (2, "Called quic_init");
1515  u32 add_segment_size = (4096ULL << 20) - 1, segment_size = 512 << 20;
1517  vnet_app_attach_args_t _a, *a = &_a;
1518  u64 options[APP_OPTIONS_N_OPTIONS];
1519  quic_main_t *qm = &quic_main;
1520  u32 fifo_size = 64 << 10;
1521  u32 num_threads;
1522 
1523  num_threads = 1 /* main thread */ + vtm->n_threads;
1524 
1525  memset (a, 0, sizeof (*a));
1526  memset (options, 0, sizeof (options));
1527 
1528  a->session_cb_vft = &quic_app_cb_vft;
1529  a->api_client_index = APP_INVALID_INDEX;
1530  a->options = options;
1531  a->name = format (0, "quic");
1532  a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
1533  a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = add_segment_size;
1534  a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
1535  a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
1536  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1537  a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1538  a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1539 
1540  if (vnet_application_attach (a))
1541  {
1542  clib_warning ("failed to attach quic app");
1543  return clib_error_return (0, "failed to attach quic app");
1544  }
1545 
1546  vec_validate (qm->ctx_pool, num_threads - 1);
1547  vec_validate (qm->wrk_ctx, num_threads - 1);
1548  // Timers, one per thread.
1549  tw_timer_wheel_1t_3w_1024sl_ov_t *tw;
1550  /* *INDENT-OFF* */
1551  foreach_vlib_main (({
1552  tw = &qm->wrk_ctx[ii].timer_wheel;
1553  tw_timer_wheel_init_1t_3w_1024sl_ov (tw, quic_expired_timers_dispatch,
1554  10e-3 /* timer period 1ms */ , ~0);
1555  tw->last_run_time = vlib_time_now (this_vlib_main);
1556  }));
1557  /* *INDENT-ON* */
1558 
1559  if (!qm->ca_cert_path)
1561 
1562  qm->app_index = a->app_index;
1566 
1568  FIB_PROTOCOL_IP4, ~0);
1570  FIB_PROTOCOL_IP6, ~0);
1571 
1572  vec_free (a->name);
1573  return 0;
1574 }
1575 
1576 quic_main_t *
1578 {
1579  return &quic_main;
1580 }
1581 
1583 
1584 /* *INDENT-OFF* */
1586 {
1587  .version = VPP_BUILD_VER,
1588  .description = "Quic Transport Protocol",
1589 };
1590 /* *INDENT-ON* */
1591 
1592 /*
1593  * fd.io coding-style-patch-verification: ON
1594  *
1595  * Local Variables:
1596  * eval: (c-set-style "gnu")
1597  * End:
1598  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:139
static int quic_delayed_create_app_session(void *ctx_index)
Definition: quic.c:1292
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.
int app_worker_init_accepted(session_t *s)
int quic_find_packet_ctx(quic_ctx_t **ctx, quicly_conn_t **conn, struct sockaddr *sa, socklen_t salen, quicly_decoded_packet_t packet)
Definition: quic.c:1240
session_type_t session_type
Type built from transport and network protocol types.
quic_worker_ctx_t * wrk_ctx
Definition: quic.h:94
a
Definition: bitmap.h:538
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:177
int ptls_load_bio_certificates(ptls_context_t *ctx, BIO *bio)
Definition: quic.c:555
quicly_stream_t * stream
Definition: quic.h:74
svm_fifo_t * tx_fifo
u32 ns_index
Namespace the application belongs to.
Definition: application.h:104
clib_rwlock_t half_open_rwlock
Definition: quic.h:93
quicly_cid_plaintext_t next_cid
Definition: quic.h:103
struct _vnet_connect_args vnet_connect_args_t
int quic_add_vpp_q_builtin_tx_evt(session_t *s)
Definition: quic.c:1172
static void load_bio_certificate_chain(ptls_context_t *ctx, const char *cert_data)
Definition: quic.c:579
int quic_session_accepted_callback(session_t *s)
Definition: quic.c:1147
struct _vnet_unlisten_args_t vnet_unlisten_args_t
#define QUIC_TSTAMP_RESOLUTION
Definition: quic.h:34
#define PREDICT_TRUE(x)
Definition: clib.h:112
u32 session_index
Index in thread pool where session was allocated.
#define QUIC_CONN_STATE_READY
Definition: quic.h:46
ptls_encrypt_ticket_t super
Definition: quic.c:345
unsigned long u64
Definition: types.h:89
#define NULL
Definition: clib.h:58
void session_transport_delete_notify(transport_connection_t *tc)
Notification from transport that connection is being deleted.
Definition: session.c:746
transport_connection_t * session_get_transport(session_t *s)
Definition: session.c:1283
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
char * ca_cert_path
Definition: quic.h:105
svm_fifo_t * rx_fifo
Pointers to rx/tx buffers.
u32 quic_ctx_half_open_index(quic_ctx_t *ctx)
Definition: quic.c:94
VLIB_PLUGIN_REGISTER()
static int quic_on_receive(quicly_stream_t *stream, size_t off, const void *src, size_t len)
Definition: quic.c:272
static void load_bio_private_key(ptls_context_t *ctx, const char *pk_data)
Definition: quic.c:593
static session_t * listen_session_get_from_handle(session_handle_t handle)
Definition: session.h:452
#define pool_get_aligned_will_expand(P, YESNO, A)
See if pool_get will expand the pool or not.
Definition: pool.h:242
#define vec_terminate_c_string(V)
(If necessary) NULL terminate a vector containing a c-string.
Definition: vec.h:1014
static int quic_send_datagram(session_t *session, quicly_datagram_t *packet)
Definition: quic.c:150
int i
u32 client_opaque
Definition: quic.h:76
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:984
static quic_ctx_t * quic_ctx_get_w_thread(u32 ctx_index, u8 thread_index)
Definition: quic.c:131
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:147
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
int vnet_unlisten(vnet_unlisten_args_t *a)
Definition: application.c:1065
clib_time_t clib_time
Definition: main.h:72
static void quic_timer_expired(u32 conn_index)
Definition: quic.c:711
vl_api_ip4_address_t dst
Definition: ipsec_gre.api:39
f64 tstamp_ticks_per_clock
Definition: quic.h:95
static int ptls_compare_separator_line(const char *line, const char *begin_or_end, const char *label)
Definition: quic.c:425
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
transport_connection_t * quic_listener_get(u32 listener_index)
Definition: quic.c:933
static void quic_expired_timers_dispatch(u32 *expired_timers)
Definition: quic.c:759
vhost_vring_addr_t addr
Definition: vhost_user.h:121
unsigned char u8
Definition: types.h:56
int quic_connect(transport_endpoint_cfg_t *tep)
Definition: quic.c:777
#define QUIC_DBG(_lvl, _fmt, _args...)
Definition: quic.h:38
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
#define clib_memcpy(d, s, n)
Definition: string.h:180
static int quic_on_stream_open(quicly_stream_open_cb *self, quicly_stream_t *stream)
Definition: quic.c:310
#define assert(x)
Definition: dlmalloc.c:30
int quic_session_connected_callback(u32 quic_app_index, u32 ho_ctx_idx, session_t *s, u8 is_fail)
Definition: quic.c:1053
int quic_app_rx_callback(session_t *quic_session)
Definition: quic.c:1408
void session_transport_closing_notify(transport_connection_t *tc)
Notification from transport that connection is being closed.
Definition: session.c:724
static int quic_reset_connection(quicly_context_t *quicly_ctx, u64 quic_session_handle, struct sockaddr *sa, socklen_t salen, quicly_decoded_packet_t packet)
Definition: quic.c:1385
static int quic_send_packets(quic_ctx_t *ctx)
Definition: quic.c:204
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
struct _vnet_disconnect_args_t vnet_disconnect_args_t
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:129
#define clib_error_return(e, args...)
Definition: error.h:99
static const transport_proto_vft_t quic_proto
Definition: quic.c:1494
int svm_fifo_enqueue_nowait(svm_fifo_t *f, u32 max_bytes, const u8 *copy_from_here)
Definition: svm_fifo.c:530
vhost_vring_state_t state
Definition: vhost_user.h:120
ip46_address_t lcl_ip
unsigned int u32
Definition: types.h:88
static int 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:351
u32 time_now
worker time
Definition: quic.h:83
int session_send_io_evt_to_thread(svm_fifo_t *f, session_evt_type_t evt_type)
Definition: session.c:86
ptls_handshake_properties_t hs_properties
Definition: quic.h:102
static u8 * svm_fifo_head(svm_fifo_t *f)
Definition: svm_fifo.h:242
quicly_now_cb quicly_vpp_now_cb
Definition: quic.c:640
#define FIFO_EVENT_APP_TX
struct _vnet_app_attach_args_t vnet_app_attach_args_t
struct _transport_proto_vft transport_proto_vft_t
struct _session_endpoint_cfg session_endpoint_cfg_t
#define QUIC_TIMER_HANDLE_INVALID
Definition: quic.h:32
#define QUIC_DEFAULT_CA_CERT_PATH
Definition: quic.h:30
static session_type_t session_type_from_proto_and_ip(transport_proto_t proto, u8 is_ip4)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:122
void session_send_rpc_evt_to_thread_force(u32 thread_index, void *fp, void *rpc_args)
Definition: session.c:109
quic_ctx_id_t c_quic_ctx_id
Definition: quic.h:71
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
Definition: lock.h:157
int app_worker_accept_notify(app_worker_t *app_wrk, session_t *s)
static session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:227
session_t * app_listener_get_session(app_listener_t *al)
Definition: application.c:301
static u32 quic_set_time_now(u32 thread_index)
Definition: quic.c:704
u64 * quicly_ctx
Definition: application.h:124
static void allocate_quicly_ctx(application_t *app, u8 is_client)
Definition: quic.c:643
long ctx[MAX_CONNS]
Definition: main.c:144
u32 app_index
Index of application that owns the listener.
unsigned short u16
Definition: types.h:57
u32 app_index
Definition: quic.h:90
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
#define APP_INVALID_INDEX
Definition: application.h:164
#define PREDICT_FALSE(x)
Definition: clib.h:111
static u8 * format_quic_connection(u8 *s, va_list *args)
Definition: quic.c:952
Definition: quic.h:66
int ptls_load_bio_pem_objects(BIO *bio, const char *label, ptls_iovec_t *list, size_t list_max, size_t *nb_objects)
Definition: quic.c:503
static void svm_fifo_unset_event(svm_fifo_t *f)
Unsets fifo event flag.
Definition: svm_fifo.h:185
static void quic_disconnect(u32 ctx_index, u32 thread_index)
Definition: quic.c:832
f64 seconds_per_clock
Definition: time.h:57
app_worker_t * app_worker_get_if_valid(u32 wrk_index)
vl_api_ip4_address_t src
Definition: ipsec_gre.api:38
static quicly_stream_open_cb on_stream_open
Definition: quic.c:323
#define foreach_vlib_main(body)
Definition: threads.h:235
u32 quic_start_listen(u32 app_listen_session_index, transport_endpoint_t *tep)
Definition: quic.c:853
static u64 listen_session_get_handle(session_t *s)
Definition: session.h:445
u8 len
Definition: ip_types.api:49
int vnet_application_attach(vnet_app_attach_args_t *a)
Attach application to vpp.
Definition: application.c:839
u32 quic_ctx_alloc()
Definition: quic.c:100
static u8 svm_fifo_set_event(svm_fifo_t *f)
Sets fifo event flag.
Definition: svm_fifo.h:172
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:185
transport_connection_t connection
Definition: quic.h:70
#define QUIC_CONN_STATE_HANDSHAKE
Definition: quic.h:45
app transport service
transport_connection_t * quic_connection_get(u32 ctx_index, u32 thread_index)
Definition: quic.c:924
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
vlib_main_t * vm
Definition: buffer.c:312
int quic_custom_tx_callback(void *session)
Definition: quic.c:1199
quic_main_t * vnet_quic_get_main(void)
Definition: quic.c:1577
#define PTLS_MAX_CERTS_IN_CONTEXT
Definition: quic.c:552
static clib_error_t * quic_init(vlib_main_t *vm)
Definition: quic.c:1512
#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:175
#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:33
struct _transport_connection transport_connection_t
static quic_ctx_t * quic_ctx_get(u32 ctx_index)
Definition: quic.c:124
static int quic_add_segment_callback(u32 client_index, u64 seg_handle)
Definition: quic.c:1154
int app_worker_init_connected(app_worker_t *app_wrk, session_t *s)
void quic_ctx_half_open_free(u32 ho_index)
Definition: quic.c:71
static int quic_create_connection(quicly_context_t *quicly_ctx, u64 quic_session_handle, u32 lctx_index, quicly_conn_t *conn, struct sockaddr *sa, socklen_t salen, quicly_decoded_packet_t packet)
Definition: quic.c:1338
static void quic_update_time(f64 now, u8 thread_index)
Definition: quic.c:942
tw_timer_wheel_1t_3w_1024sl_ov_t timer_wheel
worker timer wheel
Definition: quic.h:84
application_t * application_get(u32 app_index)
Definition: application.c:447
static void quic_update_timer(quic_ctx_t *ctx)
Definition: quic.c:725
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:248
#define FIFO_EVENT_BUILTIN_TX
apps acting as transports
app_listener_t * app_listener_get_w_handle(session_handle_t handle)
Definition: application.c:120
static int quic_del_segment_callback(u32 client_index, u64 seg_handle)
Definition: quic.c:1163
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:311
#define ASSERT(truth)
static quicly_closed_by_peer_cb on_closed_by_peer
Definition: quic.c:335
void quic_open_stream_if_ready(quic_ctx_t *ctx)
Definition: quic.c:1181
int vnet_listen(vnet_listen_args_t *a)
Definition: application.c:977
static u8 * format_quic_listener(u8 *s, va_list *args)
Definition: quic.c:970
static void quic_connection_closed(u32 conn_index)
Definition: quic.c:616
u8 * tls_key
PEM encoded key.
Definition: application.h:119
size_t count
Definition: vapi.c:47
ip46_address_t rmt_ip
static int64_t quic_get_time(quicly_now_cb *self)
Definition: quic.c:632
int vnet_connect(vnet_connect_args_t *a)
Definition: application.c:1029
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 thread_index
Index of the thread that allocated the session.
static int quic_on_stop_sending(quicly_stream_t *stream, int error_code)
Definition: quic.c:258
session_t * session_alloc(u32 thread_index)
Definition: session.c:150
int vlib_main(vlib_main_t *volatile vm, unformat_input_t *input)
Definition: main.c:1975
static void quic_disconnect_transport(quic_ctx_t *ctx)
Definition: quic.c:137
static ptls_context_t quic_tlsctx
Definition: quic.c:402
#define QUIC_INT_MAX
Definition: quic.c:40
static session_cb_vft_t quic_app_cb_vft
Definition: quic.c:1484
u8 * srv_hostname
Definition: quic.h:75
void quic_ctx_half_open_reader_unlock()
Definition: quic.c:88
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 max_bytes)
Definition: svm_fifo.c:734
void quic_session_disconnect_callback(session_t *s)
Definition: quic.c:1135
static int quic_on_receive_reset(quicly_stream_t *stream, int error_code)
Definition: quic.c:265
app_worker_t * app_worker_get(u32 wrk_index)
u64 session_handle_t
ptls_iovec_t data
Definition: quic.c:347
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
volatile u8 session_state
State in session layer state machine.
u32 quic_stop_listen(u32 lctx_index)
Definition: quic.c:903
u32 opaque
Opaque, for general use.
void quic_session_reset_callback(session_t *s)
Definition: quic.c:1141
int vnet_disconnect_session(vnet_disconnect_args_t *a)
Definition: application.c:1094
static int quic_receive(quic_ctx_t *ctx, quicly_conn_t *conn, quicly_decoded_packet_t packet)
Definition: quic.c:1266
int session_send_io_evt_to_thread_custom(void *data, u32 thread_index, session_evt_type_t evt_type)
Definition: session.c:93
u8 is_listener
Definition: quic.h:77
u32 quic_ctx_half_open_alloc(void)
Definition: quic.c:43
u32 app_index
Index of owning app.
Definition: application.h:43
int session_lookup_add_connection(transport_connection_t *tc, u64 value)
Add transport connection to a session table.
static void quic_ctx_free(quic_ctx_t *ctx)
Definition: quic.c:114
static struct st_util_session_cache_t sc
Definition: quic.c:395
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static u32 vlib_num_workers()
Definition: threads.h:366
u8 * tls_cert
Certificate to be used for listen sessions.
Definition: application.h:116
u32 app_wrk_index
Index of the app worker that owns the session.
quic_ctx_t ** ctx_pool
Definition: quic.h:92
static int quic_delayed_notify_app_connected(void *ctx_index)
Definition: quic.c:1006
int svm_fifo_peek(svm_fifo_t *f, u32 relative_offset, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:726
int app_worker_connect_notify(app_worker_t *app_wrk, session_t *s, u32 opaque)
struct session_dgram_header_ session_dgram_hdr_t
static int ptls_get_bio_pem_object(BIO *bio, const char *label, ptls_buffer_t *buf)
Definition: quic.c:460
static const quicly_stream_callbacks_t quic_stream_callbacks
Definition: quic.c:300
static u8 * format_quic_half_open(u8 *s, va_list *args)
Definition: quic.c:959
static session_t * listen_session_get(u32 ls_index)
Definition: session.h:475
quic_ctx_t * quic_ctx_half_open_get(u32 ctx_index)
Definition: quic.c:80
u32 listener_index
Parent listener session index if the result of an accept.
quic_ctx_t * half_open_ctx_pool
Definition: quic.h:91
static void quic_on_conn_close(quicly_closed_by_peer_cb *self, quicly_conn_t *conn, int code, uint64_t frame_type, const char *reason, size_t reason_len)
Definition: quic.c:326