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