session: api to add new transport types
[vpp.git] / src / plugins / hs_apps / sapi / vpp_echo_common.c
index dbe8d92..60ea5a1 100644 (file)
@@ -113,30 +113,6 @@ format_ip46_address (u8 * s, va_list * args)
     format (s, "%U", format_ip6_address, &ip46->ip6);
 }
 
-uword
-unformat_data (unformat_input_t * input, va_list * args)
-{
-  u64 _a;
-  u64 *a = va_arg (*args, u64 *);
-  if (unformat (input, "%lluGb", &_a))
-    *a = _a << 30;
-  else if (unformat (input, "%lluG", &_a))
-    *a = _a << 30;
-  else if (unformat (input, "%lluMb", &_a))
-    *a = _a << 20;
-  else if (unformat (input, "%lluM", &_a))
-    *a = _a << 20;
-  else if (unformat (input, "%lluKb", &_a))
-    *a = _a << 10;
-  else if (unformat (input, "%lluK", &_a))
-    *a = _a << 10;
-  else if (unformat (input, "%llu", a))
-    ;
-  else
-    return 0;
-  return 1;
-}
-
 u8 *
 format_api_error (u8 * s, va_list * args)
 {
@@ -229,8 +205,6 @@ echo_format_app_state (u8 * s, va_list * args)
     return format (s, "STATE_ATTACHED (%u)", state);
   if (state == STATE_ATTACHED_NO_CERT)
     return format (s, "STATE_ATTACHED_NO_CERT (%u)", state);
-  if (state == STATE_ATTACHED_ONE_CERT)
-    return format (s, "STATE_ATTACHED_ONE_CERT (%u)", state);
   if (state == STATE_LISTEN)
     return format (s, "STATE_LISTEN (%u)", state);
   if (state == STATE_READY)
@@ -283,6 +257,20 @@ echo_unformat_timing_event (unformat_input_t * input, va_list * args)
   return 1;
 }
 
+u8 *
+echo_format_bytes_per_sec (u8 * s, va_list * args)
+{
+  f64 bps = va_arg (*args, f64) * 8;
+  if (bps > 1e9)
+    return format (s, "%.3f Gb/s", bps / 1e9);
+  else if (bps > 1e6)
+    return format (s, "%.3f Mb/s", bps / 1e6);
+  else if (bps > 1e3)
+    return format (s, "%.3f Kb/s", bps / 1e3);
+  else
+    return format (s, "%.3f b/s", bps);
+}
+
 u8 *
 echo_format_timing_event (u8 * s, va_list * args)
 {
@@ -321,10 +309,6 @@ unformat_transport_proto (unformat_input_t * input, va_list * args)
     *proto = TRANSPORT_PROTO_UDP;
   else if (unformat (input, "UDP"))
     *proto = TRANSPORT_PROTO_UDP;
-  else if (unformat (input, "sctp"))
-    *proto = TRANSPORT_PROTO_SCTP;
-  else if (unformat (input, "SCTP"))
-    *proto = TRANSPORT_PROTO_SCTP;
   else if (unformat (input, "tls"))
     *proto = TRANSPORT_PROTO_TLS;
   else if (unformat (input, "TLS"))
@@ -350,9 +334,6 @@ format_transport_proto (u8 * s, va_list * args)
     case TRANSPORT_PROTO_UDP:
       s = format (s, "UDP");
       break;
-    case TRANSPORT_PROTO_SCTP:
-      s = format (s, "SCTP");
-      break;
     case TRANSPORT_PROTO_NONE:
       s = format (s, "NONE");
       break;
@@ -483,13 +464,27 @@ unformat_ip6_address (unformat_input_t * input, va_list * args)
   }
 }
 
+uword
+unformat_ip46_address (unformat_input_t * input, va_list * args)
+{
+  ip46_address_t *ip = va_arg (*args, ip46_address_t *);
+
+  if (unformat (input, "%U", unformat_ip4_address, &ip->ip4))
+    ;
+  else if (unformat (input, "%U", unformat_ip6_address, &ip->ip6))
+    ;
+  else
+    return 0;
+  return 1;
+}
+
 u8 *
 echo_format_crypto_engine (u8 * s, va_list * args)
 {
   u32 state = va_arg (*args, u32);
-  if (state == TLS_ENGINE_MBEDTLS)
+  if (state == CRYPTO_ENGINE_MBEDTLS)
     return format (s, "mbedtls");
-  if (state == TLS_ENGINE_OPENSSL)
+  if (state == CRYPTO_ENGINE_OPENSSL)
     return format (s, "openssl");
   if (state == CRYPTO_ENGINE_PICOTLS)
     return format (s, "picotls");
@@ -504,9 +499,9 @@ echo_unformat_crypto_engine (unformat_input_t * input, va_list * args)
 {
   u8 *a = va_arg (*args, u8 *);
   if (unformat (input, "mbedtls"))
-    *a = TLS_ENGINE_MBEDTLS;
+    *a = CRYPTO_ENGINE_MBEDTLS;
   else if (unformat (input, "openssl"))
-    *a = TLS_ENGINE_OPENSSL;
+    *a = CRYPTO_ENGINE_OPENSSL;
   else if (unformat (input, "picotls"))
     *a = CRYPTO_ENGINE_PICOTLS;
   else if (unformat (input, "vpp"))
@@ -529,12 +524,12 @@ echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid)
   clib_spinlock_lock (&em->sid_vpp_handles_lock);
   if (sid == SESSION_INVALID_INDEX)
     {
-      ECHO_LOG (2, "hash_unset(0x%lx)", handle);
+      ECHO_LOG (3, "hash_unset(0x%lx)", handle);
       hash_unset (em->session_index_by_vpp_handles, handle);
     }
   else
     {
-      ECHO_LOG (2, "hash_set(0x%lx) S[%d]", handle, sid);
+      ECHO_LOG (3, "hash_set(0x%lx) S[%d]", handle, sid);
       hash_set (em->session_index_by_vpp_handles, handle, sid);
     }
   clib_spinlock_unlock (&em->sid_vpp_handles_lock);
@@ -543,33 +538,33 @@ echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid)
 echo_session_t *
 echo_session_new (echo_main_t * em)
 {
-  /* thread safe new prealloced session */
+  /* thread safe new prealloced session
+   * see echo_session_prealloc */
   return pool_elt_at_index (em->sessions,
                            clib_atomic_fetch_add (&em->nxt_available_sidx,
                                                   1));
 }
 
 int
-echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque)
+echo_send_rpc (echo_main_t * em, void *fp, echo_rpc_args_t * args)
 {
   svm_msg_q_msg_t msg;
   echo_rpc_msg_t *evt;
   if (PREDICT_FALSE (svm_msg_q_lock (em->rpc_msq_queue)))
     {
-      ECHO_LOG (1, "RPC lock failed");
+      ECHO_FAIL (ECHO_FAIL_RPC_SIZE, "RPC lock failed");
       return -1;
     }
   if (PREDICT_FALSE (svm_msg_q_ring_is_full (em->rpc_msq_queue, 0)))
     {
       svm_msg_q_unlock (em->rpc_msq_queue);
-      ECHO_LOG (1, "RPC ring is full");
+      ECHO_FAIL (ECHO_FAIL_RPC_SIZE, "RPC ring is full");
       return -2;
     }
   msg = svm_msg_q_alloc_msg_w_ring (em->rpc_msq_queue, 0);
   evt = (echo_rpc_msg_t *) svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
-  evt->arg = arg;
-  evt->opaque = opaque;
   evt->fp = fp;
+  clib_memcpy (&evt->args, args, sizeof (evt->args));
 
   svm_msg_q_add_and_unlock (em->rpc_msq_queue, &msg);
   return 0;
@@ -584,34 +579,12 @@ echo_get_session_from_handle (echo_main_t * em, u64 handle)
   clib_spinlock_unlock (&em->sid_vpp_handles_lock);
   if (!p)
     {
-      ECHO_LOG (1, "unknown handle 0x%lx", handle);
+      ECHO_LOG (2, "unknown handle 0x%lx", handle);
       return 0;
     }
   return pool_elt_at_index (em->sessions, p[0]);
 }
 
-int
-wait_for_segment_allocation (u64 segment_handle)
-{
-  echo_main_t *em = &echo_main;
-  f64 timeout;
-  timeout = clib_time_now (&em->clib_time) + TIMEOUT;
-  uword *segment_present;
-  ECHO_LOG (1, "Waiting for segment 0x%lx...", segment_handle);
-  while (clib_time_now (&em->clib_time) < timeout)
-    {
-      clib_spinlock_lock (&em->segment_handles_lock);
-      segment_present = hash_get (em->shared_segment_handles, segment_handle);
-      clib_spinlock_unlock (&em->segment_handles_lock);
-      if (segment_present != 0)
-       return 0;
-      if (em->time_to_stop == 1)
-       return 0;
-    }
-  ECHO_LOG (1, "timeout wait_for_segment_allocation (0x%lx)", segment_handle);
-  return -1;
-}
-
 int
 wait_for_state_change (echo_main_t * em, connection_state_t state,
                       f64 timeout)
@@ -624,7 +597,7 @@ wait_for_state_change (echo_main_t * em, connection_state_t state,
       if (em->time_to_stop)
        return 1;
     }
-  ECHO_LOG (1, "timeout waiting for %U", echo_format_app_state, state);
+  ECHO_LOG (2, "timeout waiting for %U", echo_format_app_state, state);
   return -1;
 }
 
@@ -644,7 +617,7 @@ void
 echo_session_print_stats (echo_main_t * em, echo_session_t * session)
 {
   f64 deltat = clib_time_now (&em->clib_time) - session->start;
-  ECHO_LOG (0, "Session 0x%x done in %.6fs RX[%.4f] TX[%.4f] Gbit/s\n",
+  ECHO_LOG (1, "Session 0x%x done in %.6fs RX[%.4f] TX[%.4f] Gbit/s\n",
            session->vpp_session_handle, deltat,
            (session->bytes_received * 8.0) / deltat / 1e9,
            (session->bytes_sent * 8.0) / deltat / 1e9);