hsa: use crypto_engine_type_t for TLS
[vpp.git] / src / plugins / hs_apps / sapi / vpp_echo_common.c
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 <stdio.h>
17 #include <signal.h>
18
19 #include <hs_apps/sapi/vpp_echo_common.h>
20
21 char *echo_fail_code_str[] = {
22 #define _(sym, str) str,
23   foreach_echo_fail_code
24 #undef _
25 };
26
27 /*
28  *
29  *  Format functions
30  *
31  */
32
33 u8 *
34 format_ip4_address (u8 * s, va_list * args)
35 {
36   u8 *a = va_arg (*args, u8 *);
37   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
38 }
39
40 u8 *
41 format_ip6_address (u8 * s, va_list * args)
42 {
43   ip6_address_t *a = va_arg (*args, ip6_address_t *);
44   u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
45
46   i_max_n_zero = ARRAY_LEN (a->as_u16);
47   max_n_zeros = 0;
48   i_first_zero = i_max_n_zero;
49   n_zeros = 0;
50   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
51     {
52       u32 is_zero = a->as_u16[i] == 0;
53       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
54         {
55           i_first_zero = i;
56           n_zeros = 0;
57         }
58       n_zeros += is_zero;
59       if ((!is_zero && n_zeros > max_n_zeros)
60           || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
61         {
62           i_max_n_zero = i_first_zero;
63           max_n_zeros = n_zeros;
64           i_first_zero = ARRAY_LEN (a->as_u16);
65           n_zeros = 0;
66         }
67     }
68
69   last_double_colon = 0;
70   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
71     {
72       if (i == i_max_n_zero && max_n_zeros > 1)
73         {
74           s = format (s, "::");
75           i += max_n_zeros - 1;
76           last_double_colon = 1;
77         }
78       else
79         {
80           s = format (s, "%s%x",
81                       (last_double_colon || i == 0) ? "" : ":",
82                       clib_net_to_host_u16 (a->as_u16[i]));
83           last_double_colon = 0;
84         }
85     }
86
87   return s;
88 }
89
90 /* Format an IP46 address. */
91 u8 *
92 format_ip46_address (u8 * s, va_list * args)
93 {
94   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
95   ip46_type_t type = va_arg (*args, ip46_type_t);
96   int is_ip4 = 1;
97
98   switch (type)
99     {
100     case IP46_TYPE_ANY:
101       is_ip4 = ip46_address_is_ip4 (ip46);
102       break;
103     case IP46_TYPE_IP4:
104       is_ip4 = 1;
105       break;
106     case IP46_TYPE_IP6:
107       is_ip4 = 0;
108       break;
109     }
110
111   return is_ip4 ?
112     format (s, "%U", format_ip4_address, &ip46->ip4) :
113     format (s, "%U", format_ip6_address, &ip46->ip6);
114 }
115
116 uword
117 unformat_data (unformat_input_t * input, va_list * args)
118 {
119   u64 _a;
120   u64 *a = va_arg (*args, u64 *);
121   if (unformat (input, "%lluGb", &_a))
122     *a = _a << 30;
123   else if (unformat (input, "%lluG", &_a))
124     *a = _a << 30;
125   else if (unformat (input, "%lluMb", &_a))
126     *a = _a << 20;
127   else if (unformat (input, "%lluM", &_a))
128     *a = _a << 20;
129   else if (unformat (input, "%lluKb", &_a))
130     *a = _a << 10;
131   else if (unformat (input, "%lluK", &_a))
132     *a = _a << 10;
133   else if (unformat (input, "%llu", a))
134     ;
135   else
136     return 0;
137   return 1;
138 }
139
140 u8 *
141 format_api_error (u8 * s, va_list * args)
142 {
143   echo_main_t *em = &echo_main;
144   i32 error = va_arg (*args, u32);
145   uword *p;
146
147   p = hash_get (em->error_string_by_error_number, -error);
148
149   if (p)
150     s = format (s, "%s", p[0]);
151   else
152     s = format (s, "%d", error);
153   return s;
154 }
155
156 void
157 init_error_string_table ()
158 {
159   echo_main_t *em = &echo_main;
160   em->error_string_by_error_number = hash_create (0, sizeof (uword));
161
162 #define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
163   foreach_vnet_api_error;
164 #undef _
165
166   hash_set (em->error_string_by_error_number, 99, "Misc");
167 }
168
169 u8 *
170 echo_format_session (u8 * s, va_list * args)
171 {
172   echo_session_t *session = va_arg (*args, echo_session_t *);
173
174   return format (s, "%U 0x%lx S[%u]", echo_format_session_type,
175                  session->session_type, session->vpp_session_handle,
176                  session->session_index);
177 }
178
179 u8 *
180 echo_format_session_type (u8 * s, va_list * args)
181 {
182   u32 session_type = va_arg (*args, u32);
183   switch (session_type)
184     {
185     case ECHO_SESSION_TYPE_QUIC:
186       return format (s, "Qsession");
187     case ECHO_SESSION_TYPE_STREAM:
188       return format (s, "Stream");
189     case ECHO_SESSION_TYPE_LISTEN:
190       return format (s, "Lsession");
191     default:
192       break;
193     }
194   return format (s, "BadSession");
195 }
196
197 u8 *
198 echo_format_session_state (u8 * s, va_list * args)
199 {
200   u32 session_state = va_arg (*args, u32);
201   switch (session_state)
202     {
203     case ECHO_SESSION_STATE_INITIAL:
204       return format (s, "ECHO_SESSION_STATE_INITIAL (%u)", session_state);
205     case ECHO_SESSION_STATE_READY:
206       return format (s, "ECHO_SESSION_STATE_READY (%u)", session_state);
207     case ECHO_SESSION_STATE_AWAIT_CLOSING:
208       return format (s, "ECHO_SESSION_STATE_AWAIT_CLOSING (%u)",
209                      session_state);
210     case ECHO_SESSION_STATE_AWAIT_DATA:
211       return format (s, "ECHO_SESSION_STATE_AWAIT_DATA (%u)", session_state);
212     case ECHO_SESSION_STATE_CLOSING:
213       return format (s, "ECHO_SESSION_STATE_CLOSING (%u)", session_state);
214     case ECHO_SESSION_STATE_CLOSED:
215       return format (s, "ECHO_SESSION_STATE_CLOSED (%u)", session_state);
216     default:
217       break;
218     }
219   return format (s, "unknown session state (%u)", session_state);
220 }
221
222 u8 *
223 echo_format_app_state (u8 * s, va_list * args)
224 {
225   u32 state = va_arg (*args, u32);
226   if (state == STATE_START)
227     return format (s, "STATE_START (%u)", state);
228   if (state == STATE_ATTACHED)
229     return format (s, "STATE_ATTACHED (%u)", state);
230   if (state == STATE_ATTACHED_NO_CERT)
231     return format (s, "STATE_ATTACHED_NO_CERT (%u)", state);
232   if (state == STATE_ATTACHED_ONE_CERT)
233     return format (s, "STATE_ATTACHED_ONE_CERT (%u)", state);
234   if (state == STATE_LISTEN)
235     return format (s, "STATE_LISTEN (%u)", state);
236   if (state == STATE_READY)
237     return format (s, "STATE_READY (%u)", state);
238   if (state == STATE_DATA_DONE)
239     return format (s, "STATE_DATA_DONE (%u)", state);
240   if (state == STATE_DISCONNECTED)
241     return format (s, "STATE_DISCONNECTED (%u)", state);
242   if (state == STATE_DETACHED)
243     return format (s, "STATE_DETACHED (%u)", state);
244   else
245     return format (s, "unknown state (%u)", state);
246 }
247
248 uword
249 echo_unformat_close (unformat_input_t * input, va_list * args)
250 {
251   u8 *a = va_arg (*args, u8 *);
252   if (unformat (input, "Y"))
253     *a = ECHO_CLOSE_F_ACTIVE;
254   else if (unformat (input, "N"))
255     *a = ECHO_CLOSE_F_NONE;
256   else if (unformat (input, "W"))
257     *a = ECHO_CLOSE_F_PASSIVE;
258   else
259     return 0;
260   return 1;
261 }
262
263 uword
264 echo_unformat_timing_event (unformat_input_t * input, va_list * args)
265 {
266   u8 *a = va_arg (*args, u8 *);
267   if (unformat (input, "start"))
268     *a = ECHO_EVT_START;
269   else if (unformat (input, "qconnected"))
270     *a = ECHO_EVT_LAST_QCONNECTED;
271   else if (unformat (input, "qconnect"))
272     *a = ECHO_EVT_FIRST_QCONNECT;
273   else if (unformat (input, "sconnected"))
274     *a = ECHO_EVT_LAST_SCONNECTED;
275   else if (unformat (input, "sconnect"))
276     *a = ECHO_EVT_FIRST_SCONNECT;
277   else if (unformat (input, "lastbyte"))
278     *a = ECHO_EVT_LAST_BYTE;
279   else if (unformat (input, "exit"))
280     *a = ECHO_EVT_EXIT;
281   else
282     return 0;
283   return 1;
284 }
285
286 u8 *
287 echo_format_timing_event (u8 * s, va_list * args)
288 {
289   u32 timing_event = va_arg (*args, u32);
290   if (timing_event == ECHO_EVT_START)
291     return format (s, "start");
292   if (timing_event == ECHO_EVT_FIRST_QCONNECT)
293     return format (s, "qconnect");
294   if (timing_event == ECHO_EVT_LAST_QCONNECTED)
295     return format (s, "qconnected");
296   if (timing_event == ECHO_EVT_FIRST_SCONNECT)
297     return format (s, "sconnect");
298   if (timing_event == ECHO_EVT_LAST_SCONNECTED)
299     return format (s, "sconnected");
300   if (timing_event == ECHO_EVT_LAST_BYTE)
301     return format (s, "lastbyte");
302   if (timing_event == ECHO_EVT_EXIT)
303     return format (s, "exit");
304   else
305     return format (s, "unknown timing event");
306 }
307
308 uword
309 unformat_transport_proto (unformat_input_t * input, va_list * args)
310 {
311   u32 *proto = va_arg (*args, u32 *);
312   if (unformat (input, "tcp"))
313     *proto = TRANSPORT_PROTO_TCP;
314   else if (unformat (input, "TCP"))
315     *proto = TRANSPORT_PROTO_TCP;
316   else if (unformat (input, "udpc"))
317     *proto = TRANSPORT_PROTO_UDPC;
318   else if (unformat (input, "UDPC"))
319     *proto = TRANSPORT_PROTO_UDPC;
320   else if (unformat (input, "udp"))
321     *proto = TRANSPORT_PROTO_UDP;
322   else if (unformat (input, "UDP"))
323     *proto = TRANSPORT_PROTO_UDP;
324   else if (unformat (input, "sctp"))
325     *proto = TRANSPORT_PROTO_SCTP;
326   else if (unformat (input, "SCTP"))
327     *proto = TRANSPORT_PROTO_SCTP;
328   else if (unformat (input, "tls"))
329     *proto = TRANSPORT_PROTO_TLS;
330   else if (unformat (input, "TLS"))
331     *proto = TRANSPORT_PROTO_TLS;
332   else if (unformat (input, "quic"))
333     *proto = TRANSPORT_PROTO_QUIC;
334   else if (unformat (input, "QUIC"))
335     *proto = TRANSPORT_PROTO_QUIC;
336   else
337     return 0;
338   return 1;
339 }
340
341 u8 *
342 format_transport_proto (u8 * s, va_list * args)
343 {
344   u32 transport_proto = va_arg (*args, u32);
345   switch (transport_proto)
346     {
347     case TRANSPORT_PROTO_TCP:
348       s = format (s, "TCP");
349       break;
350     case TRANSPORT_PROTO_UDP:
351       s = format (s, "UDP");
352       break;
353     case TRANSPORT_PROTO_SCTP:
354       s = format (s, "SCTP");
355       break;
356     case TRANSPORT_PROTO_NONE:
357       s = format (s, "NONE");
358       break;
359     case TRANSPORT_PROTO_TLS:
360       s = format (s, "TLS");
361       break;
362     case TRANSPORT_PROTO_UDPC:
363       s = format (s, "UDPC");
364       break;
365     case TRANSPORT_PROTO_QUIC:
366       s = format (s, "QUIC");
367       break;
368     default:
369       s = format (s, "UNKNOWN");
370       break;
371     }
372   return s;
373 }
374
375 uword
376 unformat_ip4_address (unformat_input_t * input, va_list * args)
377 {
378   u8 *result = va_arg (*args, u8 *);
379   unsigned a[4];
380
381   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
382     return 0;
383
384   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
385     return 0;
386
387   result[0] = a[0];
388   result[1] = a[1];
389   result[2] = a[2];
390   result[3] = a[3];
391
392   return 1;
393 }
394
395 uword
396 unformat_ip6_address (unformat_input_t * input, va_list * args)
397 {
398   ip6_address_t *result = va_arg (*args, ip6_address_t *);
399   u16 hex_quads[8];
400   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
401   uword c, n_colon, double_colon_index;
402
403   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
404   double_colon_index = ARRAY_LEN (hex_quads);
405   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
406     {
407       hex_digit = 16;
408       if (c >= '0' && c <= '9')
409         hex_digit = c - '0';
410       else if (c >= 'a' && c <= 'f')
411         hex_digit = c + 10 - 'a';
412       else if (c >= 'A' && c <= 'F')
413         hex_digit = c + 10 - 'A';
414       else if (c == ':' && n_colon < 2)
415         n_colon++;
416       else
417         {
418           unformat_put_input (input);
419           break;
420         }
421
422       /* Too many hex quads. */
423       if (n_hex_quads >= ARRAY_LEN (hex_quads))
424         return 0;
425
426       if (hex_digit < 16)
427         {
428           hex_quad = (hex_quad << 4) | hex_digit;
429
430           /* Hex quad must fit in 16 bits. */
431           if (n_hex_digits >= 4)
432             return 0;
433
434           n_colon = 0;
435           n_hex_digits++;
436         }
437
438       /* Save position of :: */
439       if (n_colon == 2)
440         {
441           /* More than one :: ? */
442           if (double_colon_index < ARRAY_LEN (hex_quads))
443             return 0;
444           double_colon_index = n_hex_quads;
445         }
446
447       if (n_colon > 0 && n_hex_digits > 0)
448         {
449           hex_quads[n_hex_quads++] = hex_quad;
450           hex_quad = 0;
451           n_hex_digits = 0;
452         }
453     }
454
455   if (n_hex_digits > 0)
456     hex_quads[n_hex_quads++] = hex_quad;
457
458   {
459     word i;
460
461     /* Expand :: to appropriate number of zero hex quads. */
462     if (double_colon_index < ARRAY_LEN (hex_quads))
463       {
464         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
465
466         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
467           hex_quads[n_zero + i] = hex_quads[i];
468
469         for (i = 0; i < n_zero; i++)
470           hex_quads[double_colon_index + i] = 0;
471
472         n_hex_quads = ARRAY_LEN (hex_quads);
473       }
474
475     /* Too few hex quads given. */
476     if (n_hex_quads < ARRAY_LEN (hex_quads))
477       return 0;
478
479     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
480       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
481
482     return 1;
483   }
484 }
485
486 u8 *
487 echo_format_crypto_engine (u8 * s, va_list * args)
488 {
489   u32 state = va_arg (*args, u32);
490   if (state == CRYPTO_ENGINE_MBEDTLS)
491     return format (s, "mbedtls");
492   if (state == CRYPTO_ENGINE_OPENSSL)
493     return format (s, "openssl");
494   if (state == CRYPTO_ENGINE_PICOTLS)
495     return format (s, "picotls");
496   if (state == CRYPTO_ENGINE_VPP)
497     return format (s, "vpp");
498   else
499     return format (s, "unknown crypto engine");
500 }
501
502 uword
503 echo_unformat_crypto_engine (unformat_input_t * input, va_list * args)
504 {
505   u8 *a = va_arg (*args, u8 *);
506   if (unformat (input, "mbedtls"))
507     *a = CRYPTO_ENGINE_MBEDTLS;
508   else if (unformat (input, "openssl"))
509     *a = CRYPTO_ENGINE_OPENSSL;
510   else if (unformat (input, "picotls"))
511     *a = CRYPTO_ENGINE_PICOTLS;
512   else if (unformat (input, "vpp"))
513     *a = CRYPTO_ENGINE_VPP;
514   else
515     return 0;
516   return 1;
517 }
518
519
520 /*
521  *
522  *  End of format functions
523  *
524  */
525
526 void
527 echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid)
528 {
529   clib_spinlock_lock (&em->sid_vpp_handles_lock);
530   if (sid == SESSION_INVALID_INDEX)
531     {
532       ECHO_LOG (2, "hash_unset(0x%lx)", handle);
533       hash_unset (em->session_index_by_vpp_handles, handle);
534     }
535   else
536     {
537       ECHO_LOG (2, "hash_set(0x%lx) S[%d]", handle, sid);
538       hash_set (em->session_index_by_vpp_handles, handle, sid);
539     }
540   clib_spinlock_unlock (&em->sid_vpp_handles_lock);
541 }
542
543 echo_session_t *
544 echo_session_new (echo_main_t * em)
545 {
546   /* thread safe new prealloced session */
547   return pool_elt_at_index (em->sessions,
548                             clib_atomic_fetch_add (&em->nxt_available_sidx,
549                                                    1));
550 }
551
552 int
553 echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque)
554 {
555   svm_msg_q_msg_t msg;
556   echo_rpc_msg_t *evt;
557   if (PREDICT_FALSE (svm_msg_q_lock (em->rpc_msq_queue)))
558     {
559       ECHO_LOG (1, "RPC lock failed");
560       return -1;
561     }
562   if (PREDICT_FALSE (svm_msg_q_ring_is_full (em->rpc_msq_queue, 0)))
563     {
564       svm_msg_q_unlock (em->rpc_msq_queue);
565       ECHO_LOG (1, "RPC ring is full");
566       return -2;
567     }
568   msg = svm_msg_q_alloc_msg_w_ring (em->rpc_msq_queue, 0);
569   evt = (echo_rpc_msg_t *) svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
570   evt->arg = arg;
571   evt->opaque = opaque;
572   evt->fp = fp;
573
574   svm_msg_q_add_and_unlock (em->rpc_msq_queue, &msg);
575   return 0;
576 }
577
578 echo_session_t *
579 echo_get_session_from_handle (echo_main_t * em, u64 handle)
580 {
581   uword *p;
582   clib_spinlock_lock (&em->sid_vpp_handles_lock);
583   p = hash_get (em->session_index_by_vpp_handles, handle);
584   clib_spinlock_unlock (&em->sid_vpp_handles_lock);
585   if (!p)
586     {
587       ECHO_LOG (1, "unknown handle 0x%lx", handle);
588       return 0;
589     }
590   return pool_elt_at_index (em->sessions, p[0]);
591 }
592
593 int
594 wait_for_segment_allocation (u64 segment_handle)
595 {
596   echo_main_t *em = &echo_main;
597   f64 timeout;
598   timeout = clib_time_now (&em->clib_time) + TIMEOUT;
599   uword *segment_present;
600   ECHO_LOG (1, "Waiting for segment 0x%lx...", segment_handle);
601   while (clib_time_now (&em->clib_time) < timeout)
602     {
603       clib_spinlock_lock (&em->segment_handles_lock);
604       segment_present = hash_get (em->shared_segment_handles, segment_handle);
605       clib_spinlock_unlock (&em->segment_handles_lock);
606       if (segment_present != 0)
607         return 0;
608       if (em->time_to_stop == 1)
609         return 0;
610     }
611   ECHO_LOG (1, "timeout wait_for_segment_allocation (0x%lx)", segment_handle);
612   return -1;
613 }
614
615 int
616 wait_for_state_change (echo_main_t * em, connection_state_t state,
617                        f64 timeout)
618 {
619   f64 end_time = clib_time_now (&em->clib_time) + timeout;
620   while (!timeout || clib_time_now (&em->clib_time) < end_time)
621     {
622       if (em->state == state)
623         return 0;
624       if (em->time_to_stop)
625         return 1;
626     }
627   ECHO_LOG (1, "timeout waiting for %U", echo_format_app_state, state);
628   return -1;
629 }
630
631 void
632 echo_notify_event (echo_main_t * em, echo_test_evt_t e)
633 {
634   if (em->timing.events_sent & e)
635     return;
636   if (em->timing.start_event == e)
637     em->timing.start_time = clib_time_now (&em->clib_time);
638   else if (em->timing.end_event == e)
639     em->timing.end_time = clib_time_now (&em->clib_time);
640   em->timing.events_sent |= e;
641 }
642
643 void
644 echo_session_print_stats (echo_main_t * em, echo_session_t * session)
645 {
646   f64 deltat = clib_time_now (&em->clib_time) - session->start;
647   ECHO_LOG (0, "Session 0x%x done in %.6fs RX[%.4f] TX[%.4f] Gbit/s\n",
648             session->vpp_session_handle, deltat,
649             (session->bytes_received * 8.0) / deltat / 1e9,
650             (session->bytes_sent * 8.0) / deltat / 1e9);
651 }
652
653 /*
654  * fd.io coding-style-patch-verification: ON
655  *
656  * Local Variables:
657  * eval: (c-set-style "gnu")
658  * End:
659  */