tls: picotls optimize writes
[vpp.git] / src / plugins / tlspicotls / tls_picotls.c
1 #include <math.h>
2
3 #include <tlspicotls/certs.h>
4 #include <tlspicotls/tls_picotls.h>
5 #include <tlspicotls/pico_vpp_crypto.h>
6
7 picotls_main_t picotls_main;
8
9 #define MAX_QUEUE 12000
10 #define PTLS_MAX_PLAINTEXT_RECORD_SIZE 16384
11
12 static ptls_key_exchange_algorithm_t *default_key_exchange[] = {
13 #ifdef PTLS_OPENSSL_HAVE_X25519
14   &ptls_openssl_x25519,
15 #endif
16 #ifdef PTLS_OPENSSL_HAVE_SECP256R1
17   &ptls_openssl_secp256r1,
18 #endif
19 #ifdef PTLS_OPENSSL_HAVE_SECP384R1
20   &ptls_openssl_secp384r1,
21 #endif
22 #ifdef PTLS_OPENSSL_HAVE_SECP521R1
23   &ptls_openssl_secp521r1
24 #endif
25 };
26
27 static u32
28 picotls_ctx_alloc (void)
29 {
30   u8 thread_id = vlib_get_thread_index ();
31   picotls_main_t *pm = &picotls_main;
32   picotls_ctx_t **ctx;
33
34   pool_get (pm->ctx_pool[thread_id], ctx);
35   if (!(*ctx))
36     *ctx = clib_mem_alloc (sizeof (picotls_ctx_t));
37
38   clib_memset (*ctx, 0, sizeof (picotls_ctx_t));
39   (*ctx)->ctx.c_thread_index = thread_id;
40   (*ctx)->ctx.tls_ctx_engine = CRYPTO_ENGINE_PICOTLS;
41   (*ctx)->ctx.app_session_handle = SESSION_INVALID_HANDLE;
42   (*ctx)->ptls_ctx_idx = ctx - pm->ctx_pool[thread_id];
43   return (*ctx)->ptls_ctx_idx;
44 }
45
46 static void
47 picotls_ctx_free (tls_ctx_t * ctx)
48 {
49   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
50   vec_free (ptls_ctx->rx_content);
51   ptls_free (ptls_ctx->tls);
52   pool_put_index (picotls_main.ctx_pool[ctx->c_thread_index],
53                   ptls_ctx->ptls_ctx_idx);
54 }
55
56 static u32
57 picotls_listen_ctx_alloc (void)
58 {
59   picotls_main_t *pm = &picotls_main;
60   picotls_listen_ctx_t *ptls_lctx;
61
62   pool_get (pm->lctx_pool, ptls_lctx);
63
64   clib_memset (ptls_lctx, 0, sizeof (picotls_listen_ctx_t));
65   ptls_lctx->ptls_lctx_index = ptls_lctx - pm->lctx_pool;
66   return ptls_lctx->ptls_lctx_index;
67 }
68
69 static void
70 picotls_listen_ctx_free (picotls_listen_ctx_t * lctx)
71 {
72   pool_put_index (picotls_main.lctx_pool, lctx->ptls_lctx_index);
73 }
74
75 tls_ctx_t *
76 picotls_ctx_get (u32 ctx_index)
77 {
78   picotls_ctx_t **ctx;
79   ctx =
80     pool_elt_at_index (picotls_main.ctx_pool[vlib_get_thread_index ()],
81                        ctx_index);
82   return &(*ctx)->ctx;
83 }
84
85 picotls_listen_ctx_t *
86 picotls_lctx_get (u32 lctx_index)
87 {
88   return pool_elt_at_index (picotls_main.lctx_pool, lctx_index);
89 }
90
91 static u8
92 picotls_handshake_is_over (tls_ctx_t * ctx)
93 {
94   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
95   assert (ptls_ctx->tls);
96   return ptls_handshake_is_complete (ptls_ctx->tls);
97 }
98
99 static int
100 picotls_try_handshake_write (picotls_ctx_t * ptls_ctx,
101                              session_t * tls_session, ptls_buffer_t * buf)
102 {
103   u32 enq_max, enq_now;
104   svm_fifo_t *f;
105   int write, buf_left;
106
107   if (buf->off <= 0)
108     return 0;
109
110   f = tls_session->tx_fifo;
111   buf_left = buf->off;
112   enq_max = svm_fifo_max_enqueue_prod (f);
113   if (!enq_max)
114     return 0;
115
116   enq_now = clib_min (svm_fifo_max_write_chunk (f), enq_max);
117   enq_now = clib_min (enq_now, buf_left);
118   write = svm_fifo_enqueue (f, enq_now, buf->base);
119   buf_left -= write;
120   tls_add_vpp_q_tx_evt (tls_session);
121   return write;
122 }
123
124 static int
125 picotls_start_listen (tls_ctx_t * lctx)
126 {
127   picotls_listen_ctx_t *ptls_lctx;
128   ptls_context_t *ptls_ctx;
129   u32 ptls_lctx_idx;
130   app_cert_key_pair_t *ckpair;
131
132   ckpair = app_cert_key_pair_get_if_valid (lctx->ckpair_index);
133   if (!ckpair || !ckpair->cert || !ckpair->key)
134     {
135       TLS_DBG (1, "tls cert and/or key not configured %d",
136                lctx->parent_app_wrk_index);
137       return -1;
138     }
139
140   ptls_lctx_idx = picotls_listen_ctx_alloc ();
141   ptls_lctx = picotls_lctx_get (ptls_lctx_idx);
142   ptls_ctx = malloc (sizeof (ptls_context_t));
143   ptls_lctx->ptls_ctx = ptls_ctx;
144
145   memset (ptls_ctx, 0, sizeof (ptls_context_t));
146   ptls_ctx->update_open_count = NULL;
147
148   /*
149    * Process certificate & private key.
150    */
151   load_bio_certificate_chain (ptls_ctx, (char *) ckpair->cert);
152   load_bio_private_key (ptls_ctx, (char *) ckpair->key);
153
154   /* setup protocol related functions */
155   ptls_ctx->key_exchanges = default_key_exchange;
156   ptls_ctx->random_bytes = ptls_openssl_random_bytes;
157   ptls_ctx->cipher_suites = ptls_vpp_crypto_cipher_suites;
158   ptls_ctx->get_time = &ptls_get_time;
159
160   lctx->tls_ssl_ctx = ptls_lctx_idx;
161
162   return 0;
163 }
164
165 static int
166 picotls_stop_listen (tls_ctx_t * lctx)
167 {
168   u32 ptls_lctx_index;
169   picotls_listen_ctx_t *ptls_lctx;
170
171   ptls_lctx_index = lctx->tls_ssl_ctx;
172   ptls_lctx = picotls_lctx_get (ptls_lctx_index);
173
174   picotls_listen_ctx_free (ptls_lctx);
175
176   return 0;
177 }
178
179 static void
180 picotls_handle_handshake_failure (tls_ctx_t * ctx)
181 {
182   session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
183   ctx->no_app_session = 1;
184   ctx->c_s_index = SESSION_INVALID_INDEX;
185   tls_disconnect_transport (ctx);
186 }
187
188 static void
189 picotls_confirm_app_close (tls_ctx_t * ctx)
190 {
191   tls_disconnect_transport (ctx);
192   session_transport_closed_notify (&ctx->connection);
193 }
194
195 static int
196 picotls_transport_close (tls_ctx_t * ctx)
197 {
198   if (!picotls_handshake_is_over (ctx))
199     {
200       picotls_handle_handshake_failure (ctx);
201       return 0;
202     }
203   session_transport_closing_notify (&ctx->connection);
204   return 0;
205 }
206
207 static int
208 picotls_app_close (tls_ctx_t * ctx)
209 {
210   session_t *app_session;
211
212   app_session = session_get_from_handle (ctx->app_session_handle);
213   if (!svm_fifo_max_dequeue_cons (app_session->tx_fifo))
214     picotls_confirm_app_close (ctx);
215   else
216     ctx->app_closed = 1;
217
218   return 0;
219 }
220
221 static inline int
222 picotls_do_handshake (picotls_ctx_t *ptls_ctx, session_t *tcp_session)
223 {
224   int rv = PTLS_ERROR_IN_PROGRESS, write = 0, i = 0, read = 0, len;
225   svm_fifo_t *tcp_rx_fifo = tcp_session->rx_fifo;
226   ptls_buffer_t *buf = &ptls_ctx->read_buffer;
227   const int n_segs = 2, max_len = 16384;
228   ptls_t *tls = ptls_ctx->tls;
229   svm_fifo_seg_t fs[n_segs];
230   uword deq_now;
231
232   ptls_buffer_init (buf, "", 0);
233
234   len = svm_fifo_segments (tcp_rx_fifo, 0, fs, n_segs, max_len);
235   if (len <= 0)
236     return 0;
237
238   while (read < len && i < n_segs)
239     {
240       deq_now = fs[i].len;
241       rv = ptls_handshake (tls, buf, fs[i].data, &deq_now, NULL);
242
243       write += picotls_try_handshake_write (ptls_ctx, tcp_session, buf);
244       read += deq_now;
245
246       if (!(rv == 0 || rv == PTLS_ERROR_IN_PROGRESS))
247         {
248           clib_error ("unexpected error %u", rv);
249           break;
250         }
251
252       if (!rv)
253         break;
254
255       if (deq_now < fs[i].len)
256         {
257           fs[i].data += deq_now;
258           fs[i].len -= deq_now;
259         }
260       else
261         i++;
262     }
263
264   if (read)
265     svm_fifo_dequeue_drop (tcp_rx_fifo, read);
266
267   ptls_buffer_dispose (buf);
268
269   return write;
270 }
271
272 static inline int
273 ptls_copy_buf_to_fs (ptls_buffer_t *buf, u32 to_copy, svm_fifo_seg_t *fs,
274                      u32 *fs_idx, u32 max_fs)
275 {
276   u32 idx = *fs_idx;
277
278   while (to_copy)
279     {
280       if (fs[idx].len <= to_copy)
281         {
282           clib_memcpy_fast (fs[idx].data, buf->base + (buf->off - to_copy),
283                             fs[idx].len);
284           to_copy -= fs[idx].len;
285           idx += 1;
286           /* no more space in the app's rx fifo */
287           if (idx == max_fs)
288             break;
289         }
290       else
291         {
292           clib_memcpy_fast (fs[idx].data, buf->base + (buf->off - to_copy),
293                             to_copy);
294           fs[idx].len -= to_copy;
295           fs[idx].data += to_copy;
296           to_copy = 0;
297         }
298     }
299
300   *fs_idx = idx;
301
302   return to_copy;
303 }
304
305 static inline int
306 picotls_ctx_read (tls_ctx_t *ctx, session_t *tcp_session)
307 {
308   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
309   ptls_buffer_t *buf = &ptls_ctx->read_buffer;
310   int off = 0, ret, i = 0, read = 0, len;
311   const int n_segs = 4, max_len = 32768;
312   svm_fifo_t *tcp_rx_fifo, *app_rx_fifo;
313   picotls_main_t *pm = &picotls_main;
314   svm_fifo_seg_t fs[n_segs];
315   session_t *app_session;
316   u32 thread_index;
317   uword deq_now;
318
319   if (PREDICT_FALSE (!ptls_handshake_is_complete (ptls_ctx->tls)))
320     {
321       picotls_do_handshake (ptls_ctx, tcp_session);
322       if (picotls_handshake_is_over (ctx))
323         {
324           if (ptls_is_server (ptls_ctx->tls))
325             {
326               if (tls_notify_app_accept (ctx))
327                 {
328                   ctx->c_s_index = SESSION_INVALID_INDEX;
329                   tls_disconnect_transport (ctx);
330                   return -1;
331                 }
332             }
333           else
334             {
335               tls_notify_app_connected (ctx, SESSION_E_NONE);
336             }
337         }
338
339       if (!svm_fifo_max_dequeue (tcp_session->rx_fifo))
340         return 0;
341     }
342
343   tcp_rx_fifo = tcp_session->rx_fifo;
344   app_session = session_get_from_handle (ctx->app_session_handle);
345   app_rx_fifo = app_session->rx_fifo;
346
347   if (TLS_READ_IS_LEFT (ptls_ctx))
348     goto do_enq;
349
350   len = svm_fifo_segments (tcp_rx_fifo, 0, fs, n_segs, max_len);
351   if (len <= 0)
352     goto final_checks;
353
354   thread_index = ptls_ctx->ctx.c_thread_index;
355   vec_validate (pm->rx_bufs[thread_index], 2 * max_len);
356   ptls_buffer_init (buf, pm->rx_bufs[thread_index], 2 * max_len);
357   ptls_ctx->read_buffer_offset = 0;
358
359   while (read < len && i < n_segs)
360     {
361       deq_now = fs[i].len;
362       ret = ptls_receive (ptls_ctx->tls, buf, fs[i].data, &deq_now);
363       ASSERT (ret == 0 || ret == PTLS_ERROR_IN_PROGRESS);
364       read += deq_now;
365       if (deq_now < fs[i].len)
366         {
367           fs[i].data += deq_now;
368           fs[i].len -= deq_now;
369         }
370       else
371         i++;
372     }
373
374   if (read)
375     svm_fifo_dequeue_drop (tcp_rx_fifo, read);
376
377   if (!TLS_READ_LEFT_LEN (ptls_ctx))
378     {
379       ptls_buffer_dispose (buf);
380       goto final_checks;
381     }
382
383 do_enq:
384
385   len = TLS_READ_LEFT_LEN (ptls_ctx);
386   off = svm_fifo_enqueue (app_rx_fifo, len, TLS_READ_OFFSET (ptls_ctx));
387   if (off != len)
388     {
389       if (off < 0)
390         {
391           off = 0;
392           goto final_checks;
393         }
394       ptls_ctx->read_buffer_offset += off;
395     }
396   else
397     {
398       buf->off = 0;
399       ptls_ctx->read_buffer_offset = 0;
400     }
401
402   if (app_session->session_state >= SESSION_STATE_READY)
403     tls_notify_app_enqueue (ctx, app_session);
404
405 final_checks:
406
407   if (TLS_READ_IS_LEFT (ptls_ctx) || svm_fifo_max_dequeue (tcp_rx_fifo))
408     tls_add_vpp_q_builtin_rx_evt (tcp_session);
409
410   return off;
411 }
412
413 static inline u32
414 ptls_compute_deq_len (picotls_ctx_t *ptls_ctx, u32 dst_chunk, u32 src_chunk,
415                       u32 dst_space, u8 *is_nocopy)
416 {
417   int record_overhead = ptls_get_record_overhead (ptls_ctx->tls);
418   int num_records;
419   u32 deq_len, total_overhead;
420
421   if (dst_chunk >= clib_min (8192, src_chunk + record_overhead))
422     {
423       *is_nocopy = 1;
424       deq_len = clib_min (src_chunk, dst_chunk);
425       num_records = ceil ((f64) deq_len / PTLS_MAX_PLAINTEXT_RECORD_SIZE);
426       total_overhead = num_records * record_overhead;
427       if (deq_len + total_overhead > dst_chunk)
428         deq_len = dst_chunk - total_overhead;
429     }
430   else
431     {
432       deq_len = clib_min (src_chunk, dst_space);
433       num_records = ceil ((f64) deq_len / PTLS_MAX_PLAINTEXT_RECORD_SIZE);
434       total_overhead = num_records * record_overhead;
435       if (deq_len + total_overhead > dst_space)
436         deq_len = dst_space - total_overhead;
437     }
438
439   return deq_len;
440 }
441
442 static u32
443 ptls_app_to_tcp_write (picotls_ctx_t *ptls_ctx, session_t *app_session,
444                        svm_fifo_t *tcp_tx_fifo, u32 max_len)
445 {
446   u32 wrote = 0, max_enq, thread_index, app_buf_len, left, ti = 0;
447   int read = 0, rv, i = 0, len, n_tcp_segs = 4, deq_len;
448   const int n_app_segs = 2, min_chunk = 2048;
449   svm_fifo_seg_t app_fs[n_app_segs], tcp_fs[n_tcp_segs];
450   picotls_main_t *pm = &picotls_main;
451   ptls_buffer_t _buf, *buf = &_buf;
452   svm_fifo_t *app_tx_fifo;
453   u8 is_nocopy, *app_buf;
454   u32 first_chunk_len;
455
456   thread_index = app_session->thread_index;
457   app_tx_fifo = app_session->tx_fifo;
458
459   len = svm_fifo_segments (app_tx_fifo, 0, app_fs, n_app_segs, max_len);
460   if (len <= 0)
461     return 0;
462
463   n_tcp_segs = svm_fifo_provision_chunks (tcp_tx_fifo, tcp_fs, n_tcp_segs,
464                                           1000 + max_len);
465   if (n_tcp_segs <= 0)
466     return 0;
467
468   while ((left = len - read) && ti < n_tcp_segs)
469     {
470       /* If we wrote something and are left with few bytes, postpone write
471        * as we may be able to encrypt a bigger chunk next time */
472       if (wrote && left < min_chunk)
473         break;
474
475       /* Avoid short records if possible */
476       if (app_fs[i].len < min_chunk && min_chunk < left)
477         {
478           app_buf_len = app_fs[i].len + app_fs[i + 1].len;
479           app_buf = pm->rx_bufs[thread_index];
480           vec_validate (pm->rx_bufs[thread_index], app_buf_len);
481           clib_memcpy_fast (pm->rx_bufs[thread_index], app_fs[i].data,
482                             app_fs[i].len);
483           clib_memcpy_fast (pm->rx_bufs[thread_index] + app_fs[i].len,
484                             app_fs[i + 1].data, app_buf_len - app_fs[i].len);
485           first_chunk_len = app_fs[i].len;
486           i += 1;
487         }
488       else
489         {
490           app_buf = app_fs[i].data;
491           app_buf_len = app_fs[i].len;
492           first_chunk_len = 0;
493         }
494
495       is_nocopy = 0;
496       max_enq = tcp_fs[ti].len;
497       max_enq += ti < (n_tcp_segs - 1) ? tcp_fs[ti + 1].len : 0;
498
499       deq_len = ptls_compute_deq_len (ptls_ctx, tcp_fs[ti].len, app_buf_len,
500                                       max_enq, &is_nocopy);
501       if (is_nocopy)
502         {
503           ptls_buffer_init (buf, tcp_fs[ti].data, tcp_fs[ti].len);
504           rv = ptls_send (ptls_ctx->tls, buf, app_buf, deq_len);
505
506           assert (rv == 0);
507           wrote += buf->off;
508
509           tcp_fs[ti].len -= buf->off;
510           tcp_fs[ti].data += buf->off;
511           if (!tcp_fs[ti].len)
512             ti += 1;
513         }
514       else
515         {
516           vec_validate (pm->tx_bufs[thread_index], max_enq);
517           ptls_buffer_init (buf, pm->tx_bufs[thread_index], max_enq);
518           rv = ptls_send (ptls_ctx->tls, buf, app_buf, deq_len);
519
520           assert (rv == 0);
521           wrote += buf->off;
522
523           left = ptls_copy_buf_to_fs (buf, buf->off, tcp_fs, &ti, n_tcp_segs);
524           assert (left == 0);
525         }
526
527       read += deq_len;
528       ASSERT (deq_len >= first_chunk_len);
529
530       if (deq_len == app_buf_len)
531         {
532           i += 1;
533         }
534       else
535         {
536           app_fs[i].len -= deq_len - first_chunk_len;
537           app_fs[i].data += deq_len - first_chunk_len;
538         }
539     }
540
541   if (read)
542     {
543       svm_fifo_dequeue_drop (app_tx_fifo, read);
544       if (svm_fifo_needs_deq_ntf (app_tx_fifo, read))
545         session_dequeue_notify (app_session);
546     }
547
548   if (wrote)
549     {
550       svm_fifo_enqueue_nocopy (tcp_tx_fifo, wrote);
551       if (svm_fifo_set_event (tcp_tx_fifo))
552         session_send_io_evt_to_thread (tcp_tx_fifo, SESSION_IO_EVT_TX);
553     }
554
555   return wrote;
556 }
557
558 static inline int
559 picotls_ctx_write (tls_ctx_t *ctx, session_t *app_session,
560                    transport_send_params_t *sp)
561 {
562   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
563   u32 deq_max, deq_now, enq_max, enq_buf, wrote = 0;
564   svm_fifo_t *tcp_tx_fifo;
565   session_t *tcp_session;
566
567   tcp_session = session_get_from_handle (ctx->tls_session_handle);
568   tcp_tx_fifo = tcp_session->tx_fifo;
569
570   enq_max = svm_fifo_max_enqueue_prod (tcp_tx_fifo);
571   if (enq_max < 2048)
572     goto check_tls_fifo;
573
574   deq_max = svm_fifo_max_dequeue_cons (app_session->tx_fifo);
575   deq_max = clib_min (deq_max, enq_max);
576   if (!deq_max)
577     goto check_tls_fifo;
578
579   deq_now = clib_min (deq_max, sp->max_burst_size);
580   wrote = ptls_app_to_tcp_write (ptls_ctx, app_session, tcp_tx_fifo, deq_now);
581
582 check_tls_fifo:
583
584   if (ctx->app_closed)
585     picotls_app_close (ctx);
586
587   /* Deschedule and wait for deq notification if fifo is almost full */
588   enq_buf = clib_min (svm_fifo_size (tcp_tx_fifo) / 2, TLSP_MIN_ENQ_SPACE);
589   if (enq_max < wrote + enq_buf)
590     {
591       svm_fifo_add_want_deq_ntf (tcp_tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
592       transport_connection_deschedule (&ctx->connection);
593       sp->flags |= TRANSPORT_SND_F_DESCHED;
594     }
595   else
596     /* Request tx reschedule of the app session */
597     app_session->flags |= SESSION_F_CUSTOM_TX;
598
599   return wrote;
600 }
601
602 static int
603 picotls_ctx_init_server (tls_ctx_t * ctx)
604 {
605   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
606   u32 ptls_lctx_idx = ctx->tls_ssl_ctx;
607   picotls_listen_ctx_t *ptls_lctx;
608
609   ptls_lctx = picotls_lctx_get (ptls_lctx_idx);
610   ptls_ctx->tls = ptls_new (ptls_lctx->ptls_ctx, 1);
611   if (ptls_ctx->tls == NULL)
612     {
613       TLS_DBG (1, "Failed to initialize ptls_ssl structure");
614       return -1;
615     }
616
617   ptls_ctx->rx_len = 0;
618   ptls_ctx->rx_offset = 0;
619
620   return 0;
621 }
622
623 static int
624 picotls_ctx_init_client (tls_ctx_t *ctx)
625 {
626   picotls_ctx_t *ptls_ctx = (picotls_ctx_t *) ctx;
627   picotls_main_t *pm = &picotls_main;
628   ptls_context_t *client_ptls_ctx = pm->client_ptls_ctx;
629   ptls_handshake_properties_t hsprop = { { { { NULL } } } };
630
631   session_t *tls_session = session_get_from_handle (ctx->tls_session_handle);
632   ptls_buffer_t hs_buf;
633
634   ptls_ctx->tls = ptls_new (client_ptls_ctx, 0);
635   if (ptls_ctx->tls == NULL)
636     {
637       TLS_DBG (1, "Failed to initialize ptls_ssl structure");
638       return -1;
639     }
640
641   ptls_ctx->rx_len = 0;
642   ptls_ctx->rx_offset = 0;
643
644   ptls_buffer_init (&hs_buf, "", 0);
645   if (ptls_handshake (ptls_ctx->tls, &hs_buf, NULL, NULL, &hsprop) !=
646       PTLS_ERROR_IN_PROGRESS)
647     {
648       TLS_DBG (1, "Failed to initialize tls connection");
649     }
650
651   picotls_try_handshake_write (ptls_ctx, tls_session, &hs_buf);
652
653   ptls_buffer_dispose (&hs_buf);
654
655   return 0;
656 }
657
658 tls_ctx_t *
659 picotls_ctx_get_w_thread (u32 ctx_index, u8 thread_index)
660 {
661   picotls_ctx_t **ctx;
662   ctx = pool_elt_at_index (picotls_main.ctx_pool[thread_index], ctx_index);
663   return &(*ctx)->ctx;
664 }
665
666 int
667 picotls_init_client_ptls_ctx (ptls_context_t **client_ptls_ctx)
668 {
669   *client_ptls_ctx = clib_mem_alloc (sizeof (ptls_context_t));
670   memset (*client_ptls_ctx, 0, sizeof (ptls_context_t));
671
672   (*client_ptls_ctx)->update_open_count = NULL;
673   (*client_ptls_ctx)->key_exchanges = default_key_exchange;
674   (*client_ptls_ctx)->random_bytes = ptls_openssl_random_bytes;
675   (*client_ptls_ctx)->cipher_suites = ptls_vpp_crypto_cipher_suites;
676   (*client_ptls_ctx)->get_time = &ptls_get_time;
677
678   return 0;
679 }
680
681 const static tls_engine_vft_t picotls_engine = {
682   .ctx_alloc = picotls_ctx_alloc,
683   .ctx_free = picotls_ctx_free,
684   .ctx_get = picotls_ctx_get,
685   .ctx_get_w_thread = picotls_ctx_get_w_thread,
686   .ctx_handshake_is_over = picotls_handshake_is_over,
687   .ctx_start_listen = picotls_start_listen,
688   .ctx_stop_listen = picotls_stop_listen,
689   .ctx_init_server = picotls_ctx_init_server,
690   .ctx_init_client = picotls_ctx_init_client,
691   .ctx_read = picotls_ctx_read,
692   .ctx_write = picotls_ctx_write,
693   .ctx_transport_close = picotls_transport_close,
694   .ctx_app_close = picotls_app_close,
695 };
696
697 static clib_error_t *
698 tls_picotls_init (vlib_main_t * vm)
699 {
700   vlib_thread_main_t *vtm = vlib_get_thread_main ();
701   picotls_main_t *pm = &picotls_main;
702   clib_error_t *error = 0;
703   u32 num_threads;
704
705   num_threads = 1 + vtm->n_threads;
706
707   vec_validate (pm->ctx_pool, num_threads - 1);
708   vec_validate (pm->rx_bufs, num_threads - 1);
709   vec_validate (pm->tx_bufs, num_threads - 1);
710
711   clib_rwlock_init (&picotls_main.crypto_keys_rw_lock);
712
713   tls_register_engine (&picotls_engine, CRYPTO_ENGINE_PICOTLS);
714
715   picotls_init_client_ptls_ctx (&pm->client_ptls_ctx);
716
717   return error;
718 }
719
720 /* *INDENT-OFF* */
721 VLIB_INIT_FUNCTION (tls_picotls_init) = {
722   .runs_after = VLIB_INITS ("tls_init"),
723 };
724 /* *INDENT-ON* */
725
726 /* *INDENT-OFF* */
727 VLIB_PLUGIN_REGISTER () = {
728   .version = VPP_BUILD_VER,
729   .description = "Transport Layer Security (TLS) Engine, Picotls Based",
730 };
731 /* *INDENT-ON* */
732
733 /*
734  * fd.io coding-style-patch-verification: ON
735  *
736  * Local Variables:
737  * eval: (c-set-style "gnu")
738  * End:
739  */