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