hsa: make vpp_echo use mq instead of bapi
[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_app_state (u8 * s, va_list * args)
171 {
172   u32 state = va_arg (*args, u32);
173   if (state == STATE_START)
174     return format (s, "STATE_START");
175   if (state == STATE_ATTACHED)
176     return format (s, "STATE_ATTACHED");
177   if (state == STATE_ATTACHED_NO_CERT)
178     return format (s, "STATE_ATTACHED_NO_CERT");
179   if (state == STATE_ATTACHED_ONE_CERT)
180     return format (s, "STATE_ATTACHED_ONE_CERT");
181   if (state == STATE_LISTEN)
182     return format (s, "STATE_LISTEN");
183   if (state == STATE_READY)
184     return format (s, "STATE_READY");
185   if (state == STATE_DATA_DONE)
186     return format (s, "STATE_DATA_DONE");
187   if (state == STATE_DISCONNECTED)
188     return format (s, "STATE_DISCONNECTED");
189   if (state == STATE_DETACHED)
190     return format (s, "STATE_DETACHED");
191   else
192     return format (s, "unknown state");
193 }
194
195 uword
196 echo_unformat_close (unformat_input_t * input, va_list * args)
197 {
198   u8 *a = va_arg (*args, u8 *);
199   if (unformat (input, "Y"))
200     *a = ECHO_CLOSE_F_ACTIVE;
201   else if (unformat (input, "N"))
202     *a = ECHO_CLOSE_F_NONE;
203   else if (unformat (input, "W"))
204     *a = ECHO_CLOSE_F_PASSIVE;
205   else
206     return 0;
207   return 1;
208 }
209
210 uword
211 echo_unformat_timing_event (unformat_input_t * input, va_list * args)
212 {
213   u8 *a = va_arg (*args, u8 *);
214   if (unformat (input, "start"))
215     *a = ECHO_EVT_START;
216   else if (unformat (input, "qconnected"))
217     *a = ECHO_EVT_LAST_QCONNECTED;
218   else if (unformat (input, "qconnect"))
219     *a = ECHO_EVT_FIRST_QCONNECT;
220   else if (unformat (input, "sconnected"))
221     *a = ECHO_EVT_LAST_SCONNECTED;
222   else if (unformat (input, "sconnect"))
223     *a = ECHO_EVT_FIRST_SCONNECT;
224   else if (unformat (input, "lastbyte"))
225     *a = ECHO_EVT_LAST_BYTE;
226   else if (unformat (input, "exit"))
227     *a = ECHO_EVT_EXIT;
228   else
229     return 0;
230   return 1;
231 }
232
233 u8 *
234 echo_format_timing_event (u8 * s, va_list * args)
235 {
236   u32 timing_event = va_arg (*args, u32);
237   if (timing_event == ECHO_EVT_START)
238     return format (s, "start");
239   if (timing_event == ECHO_EVT_FIRST_QCONNECT)
240     return format (s, "qconnect");
241   if (timing_event == ECHO_EVT_LAST_QCONNECTED)
242     return format (s, "qconnected");
243   if (timing_event == ECHO_EVT_FIRST_SCONNECT)
244     return format (s, "sconnect");
245   if (timing_event == ECHO_EVT_LAST_SCONNECTED)
246     return format (s, "sconnected");
247   if (timing_event == ECHO_EVT_LAST_BYTE)
248     return format (s, "lastbyte");
249   if (timing_event == ECHO_EVT_EXIT)
250     return format (s, "exit");
251   else
252     return format (s, "unknown timing event");
253 }
254
255 uword
256 unformat_transport_proto (unformat_input_t * input, va_list * args)
257 {
258   u32 *proto = va_arg (*args, u32 *);
259   if (unformat (input, "tcp"))
260     *proto = TRANSPORT_PROTO_TCP;
261   else if (unformat (input, "TCP"))
262     *proto = TRANSPORT_PROTO_TCP;
263   else if (unformat (input, "udpc"))
264     *proto = TRANSPORT_PROTO_UDPC;
265   else if (unformat (input, "UDPC"))
266     *proto = TRANSPORT_PROTO_UDPC;
267   else if (unformat (input, "udp"))
268     *proto = TRANSPORT_PROTO_UDP;
269   else if (unformat (input, "UDP"))
270     *proto = TRANSPORT_PROTO_UDP;
271   else if (unformat (input, "sctp"))
272     *proto = TRANSPORT_PROTO_SCTP;
273   else if (unformat (input, "SCTP"))
274     *proto = TRANSPORT_PROTO_SCTP;
275   else if (unformat (input, "tls"))
276     *proto = TRANSPORT_PROTO_TLS;
277   else if (unformat (input, "TLS"))
278     *proto = TRANSPORT_PROTO_TLS;
279   else if (unformat (input, "quic"))
280     *proto = TRANSPORT_PROTO_QUIC;
281   else if (unformat (input, "QUIC"))
282     *proto = TRANSPORT_PROTO_QUIC;
283   else
284     return 0;
285   return 1;
286 }
287
288 u8 *
289 format_transport_proto (u8 * s, va_list * args)
290 {
291   u32 transport_proto = va_arg (*args, u32);
292   switch (transport_proto)
293     {
294     case TRANSPORT_PROTO_TCP:
295       s = format (s, "TCP");
296       break;
297     case TRANSPORT_PROTO_UDP:
298       s = format (s, "UDP");
299       break;
300     case TRANSPORT_PROTO_SCTP:
301       s = format (s, "SCTP");
302       break;
303     case TRANSPORT_PROTO_NONE:
304       s = format (s, "NONE");
305       break;
306     case TRANSPORT_PROTO_TLS:
307       s = format (s, "TLS");
308       break;
309     case TRANSPORT_PROTO_UDPC:
310       s = format (s, "UDPC");
311       break;
312     case TRANSPORT_PROTO_QUIC:
313       s = format (s, "QUIC");
314       break;
315     default:
316       s = format (s, "UNKNOWN");
317       break;
318     }
319   return s;
320 }
321
322 uword
323 unformat_ip4_address (unformat_input_t * input, va_list * args)
324 {
325   u8 *result = va_arg (*args, u8 *);
326   unsigned a[4];
327
328   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
329     return 0;
330
331   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
332     return 0;
333
334   result[0] = a[0];
335   result[1] = a[1];
336   result[2] = a[2];
337   result[3] = a[3];
338
339   return 1;
340 }
341
342 uword
343 unformat_ip6_address (unformat_input_t * input, va_list * args)
344 {
345   ip6_address_t *result = va_arg (*args, ip6_address_t *);
346   u16 hex_quads[8];
347   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
348   uword c, n_colon, double_colon_index;
349
350   n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
351   double_colon_index = ARRAY_LEN (hex_quads);
352   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
353     {
354       hex_digit = 16;
355       if (c >= '0' && c <= '9')
356         hex_digit = c - '0';
357       else if (c >= 'a' && c <= 'f')
358         hex_digit = c + 10 - 'a';
359       else if (c >= 'A' && c <= 'F')
360         hex_digit = c + 10 - 'A';
361       else if (c == ':' && n_colon < 2)
362         n_colon++;
363       else
364         {
365           unformat_put_input (input);
366           break;
367         }
368
369       /* Too many hex quads. */
370       if (n_hex_quads >= ARRAY_LEN (hex_quads))
371         return 0;
372
373       if (hex_digit < 16)
374         {
375           hex_quad = (hex_quad << 4) | hex_digit;
376
377           /* Hex quad must fit in 16 bits. */
378           if (n_hex_digits >= 4)
379             return 0;
380
381           n_colon = 0;
382           n_hex_digits++;
383         }
384
385       /* Save position of :: */
386       if (n_colon == 2)
387         {
388           /* More than one :: ? */
389           if (double_colon_index < ARRAY_LEN (hex_quads))
390             return 0;
391           double_colon_index = n_hex_quads;
392         }
393
394       if (n_colon > 0 && n_hex_digits > 0)
395         {
396           hex_quads[n_hex_quads++] = hex_quad;
397           hex_quad = 0;
398           n_hex_digits = 0;
399         }
400     }
401
402   if (n_hex_digits > 0)
403     hex_quads[n_hex_quads++] = hex_quad;
404
405   {
406     word i;
407
408     /* Expand :: to appropriate number of zero hex quads. */
409     if (double_colon_index < ARRAY_LEN (hex_quads))
410       {
411         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
412
413         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
414           hex_quads[n_zero + i] = hex_quads[i];
415
416         for (i = 0; i < n_zero; i++)
417           hex_quads[double_colon_index + i] = 0;
418
419         n_hex_quads = ARRAY_LEN (hex_quads);
420       }
421
422     /* Too few hex quads given. */
423     if (n_hex_quads < ARRAY_LEN (hex_quads))
424       return 0;
425
426     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
427       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
428
429     return 1;
430   }
431 }
432
433 u8 *
434 echo_format_crypto_engine (u8 * s, va_list * args)
435 {
436   u32 state = va_arg (*args, u32);
437   if (state == TLS_ENGINE_MBEDTLS)
438     return format (s, "mbedtls");
439   if (state == TLS_ENGINE_OPENSSL)
440     return format (s, "openssl");
441   if (state == CRYPTO_ENGINE_PICOTLS)
442     return format (s, "picotls");
443   if (state == CRYPTO_ENGINE_VPP)
444     return format (s, "vpp");
445   else
446     return format (s, "unknown crypto engine");
447 }
448
449 uword
450 echo_unformat_crypto_engine (unformat_input_t * input, va_list * args)
451 {
452   u8 *a = va_arg (*args, u8 *);
453   if (unformat (input, "mbedtls"))
454     *a = TLS_ENGINE_MBEDTLS;
455   else if (unformat (input, "openssl"))
456     *a = TLS_ENGINE_OPENSSL;
457   else if (unformat (input, "picotls"))
458     *a = CRYPTO_ENGINE_PICOTLS;
459   else if (unformat (input, "vpp"))
460     *a = CRYPTO_ENGINE_VPP;
461   else
462     return 0;
463   return 1;
464 }
465
466
467 /*
468  *
469  *  End of format functions
470  *
471  */
472
473 void
474 echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid)
475 {
476   clib_spinlock_lock (&em->sid_vpp_handles_lock);
477   if (sid == SESSION_INVALID_INDEX)
478     hash_unset (em->session_index_by_vpp_handles, handle);
479   else
480     hash_set (em->session_index_by_vpp_handles, handle, sid);
481   clib_spinlock_unlock (&em->sid_vpp_handles_lock);
482 }
483
484 echo_session_t *
485 echo_session_new (echo_main_t * em)
486 {
487   /* thread safe new prealloced session */
488   return pool_elt_at_index (em->sessions,
489                             clib_atomic_fetch_add (&em->nxt_available_sidx,
490                                                    1));
491 }
492
493 int
494 echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque)
495 {
496   svm_msg_q_msg_t msg;
497   echo_rpc_msg_t *evt;
498   if (PREDICT_FALSE (svm_msg_q_lock (em->rpc_msq_queue)))
499     {
500       ECHO_LOG (1, "RPC lock failed");
501       return -1;
502     }
503   if (PREDICT_FALSE (svm_msg_q_ring_is_full (em->rpc_msq_queue, 0)))
504     {
505       svm_msg_q_unlock (em->rpc_msq_queue);
506       ECHO_LOG (1, "RPC ring is full");
507       return -2;
508     }
509   msg = svm_msg_q_alloc_msg_w_ring (em->rpc_msq_queue, 0);
510   evt = (echo_rpc_msg_t *) svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
511   evt->arg = arg;
512   evt->opaque = opaque;
513   evt->fp = fp;
514
515   svm_msg_q_add_and_unlock (em->rpc_msq_queue, &msg);
516   return 0;
517 }
518
519 echo_session_t *
520 echo_get_session_from_handle (echo_main_t * em, u64 handle)
521 {
522   uword *p;
523   clib_spinlock_lock (&em->sid_vpp_handles_lock);
524   p = hash_get (em->session_index_by_vpp_handles, handle);
525   clib_spinlock_unlock (&em->sid_vpp_handles_lock);
526   if (!p)
527     {
528       ECHO_FAIL (ECHO_FAIL_GET_SESSION_FROM_HANDLE,
529                  "unknown handle 0x%lx", handle);
530       return 0;
531     }
532   return pool_elt_at_index (em->sessions, p[0]);
533 }
534
535 int
536 wait_for_segment_allocation (u64 segment_handle)
537 {
538   echo_main_t *em = &echo_main;
539   f64 timeout;
540   timeout = clib_time_now (&em->clib_time) + TIMEOUT;
541   uword *segment_present;
542   ECHO_LOG (1, "Waiting for segment 0x%lx...", segment_handle);
543   while (clib_time_now (&em->clib_time) < timeout)
544     {
545       clib_spinlock_lock (&em->segment_handles_lock);
546       segment_present = hash_get (em->shared_segment_handles, segment_handle);
547       clib_spinlock_unlock (&em->segment_handles_lock);
548       if (segment_present != 0)
549         return 0;
550       if (em->time_to_stop == 1)
551         return 0;
552     }
553   ECHO_LOG (1, "timeout wait_for_segment_allocation (0x%lx)", segment_handle);
554   return -1;
555 }
556
557 int
558 wait_for_state_change (echo_main_t * em, connection_state_t state,
559                        f64 timeout)
560 {
561   f64 end_time = clib_time_now (&em->clib_time) + timeout;
562   while (!timeout || clib_time_now (&em->clib_time) < end_time)
563     {
564       if (em->state == state)
565         return 0;
566       if (em->time_to_stop)
567         return 1;
568     }
569   ECHO_LOG (1, "timeout waiting for %U", echo_format_app_state, state);
570   return -1;
571 }
572
573 void
574 echo_notify_event (echo_main_t * em, echo_test_evt_t e)
575 {
576   if (em->timing.events_sent & e)
577     return;
578   if (em->timing.start_event == e)
579     em->timing.start_time = clib_time_now (&em->clib_time);
580   else if (em->timing.end_event == e)
581     em->timing.end_time = clib_time_now (&em->clib_time);
582   em->timing.events_sent |= e;
583 }
584
585 void
586 echo_session_print_stats (echo_main_t * em, echo_session_t * session)
587 {
588   f64 deltat = clib_time_now (&em->clib_time) - session->start;
589   ECHO_LOG (0, "Session 0x%x done in %.6fs RX[%.4f] TX[%.4f] Gbit/s\n",
590             session->vpp_session_handle, deltat,
591             (session->bytes_received * 8.0) / deltat / 1e9,
592             (session->bytes_sent * 8.0) / deltat / 1e9);
593 }
594
595 /*
596  * fd.io coding-style-patch-verification: ON
597  *
598  * Local Variables:
599  * eval: (c-set-style "gnu")
600  * End:
601  */