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