session: add support for vrf in session unformat (VPP-1546)
[vpp.git] / src / vnet / session / session_cli.c
index 6ca090a..9da7f5b 100755 (executable)
@@ -100,12 +100,14 @@ uword
 unformat_stream_session_id (unformat_input_t * input, va_list * args)
 {
   u8 *proto = va_arg (*args, u8 *);
+  u32 *fib_index = va_arg (*args, u32 *);
   ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
   ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
   u16 *lcl_port = va_arg (*args, u16 *);
   u16 *rmt_port = va_arg (*args, u16 *);
   u8 *is_ip4 = va_arg (*args, u8 *);
   u8 tuple_is_set = 0;
+  u32 vrf = ~0;
 
   clib_memset (lcl, 0, sizeof (*lcl));
   clib_memset (rmt, 0, sizeof (*rmt));
@@ -114,10 +116,16 @@ unformat_stream_session_id (unformat_input_t * input, va_list * args)
     {
       *proto = TRANSPORT_PROTO_TCP;
     }
-  if (unformat (input, "udp"))
+  else if (unformat (input, "udp"))
     {
       *proto = TRANSPORT_PROTO_UDP;
     }
+  else
+    return 0;
+
+  if (unformat (input, "vrf %u", &vrf))
+    ;
+
   if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
                lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
     {
@@ -131,6 +139,13 @@ unformat_stream_session_id (unformat_input_t * input, va_list * args)
       tuple_is_set = 1;
     }
 
+  if (vrf != ~0)
+    {
+      fib_protocol_t fib_proto;
+      fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
+      *fib_index = fib_table_find (fib_proto, vrf);
+    }
+
   return tuple_is_set;
 }
 
@@ -138,14 +153,14 @@ uword
 unformat_stream_session (unformat_input_t * input, va_list * args)
 {
   stream_session_t **result = va_arg (*args, stream_session_t **);
+  u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
+  ip46_address_t lcl, rmt;
   stream_session_t *s;
   u8 proto = ~0;
-  ip46_address_t lcl, rmt;
-  u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
   u8 is_ip4 = 0;
 
-  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
-                &lcl_port, &rmt_port, &is_ip4))
+  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
+                &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
     return 0;
 
   if (is_ip4)
@@ -176,8 +191,8 @@ unformat_transport_connection (unformat_input_t * input, va_list * args)
   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
   u8 is_ip4 = 0;
 
-  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
-                &lcl_port, &rmt_port, &is_ip4))
+  if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
+                &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
     return 0;
 
   proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
@@ -204,11 +219,14 @@ static clib_error_t *
 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
                         vlib_cli_command_t * cmd)
 {
+  u8 *str = 0, one_session = 0, do_listeners = 0, sst, do_elog = 0;
   session_manager_main_t *smm = &session_manager_main;
-  u8 *str = 0, one_session = 0, do_listeners = 0, sst, *app_name;
-  int verbose = 0, i;
+  u32 transport_proto = ~0, track_index;
   stream_session_t *pool, *s;
-  u32 transport_proto = ~0;
+  transport_connection_t *tc;
+  app_worker_t *app_wrk;
+  int verbose = 0, i;
+  const u8 *app_name;
 
   if (!smm->is_enabled)
     {
@@ -228,6 +246,8 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
        {
          one_session = 1;
        }
+      else if (unformat (input, "elog"))
+       do_elog = 1;
       else
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);
@@ -235,7 +255,21 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 
   if (one_session)
     {
-      vlib_cli_output (vm, "%U", format_stream_session, s, 3);
+      str = format (0, "%U", format_stream_session, s, 3);
+      if (do_elog && s->session_state != SESSION_STATE_LISTENING)
+       {
+         elog_main_t *em = &vm->elog_main;
+         f64 dt;
+
+         tc = session_get_transport (s);
+         track_index = transport_elog_track_index (tc);
+         dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
+           * vm->clib_time.seconds_per_clock;
+         if (track_index != ~0)
+           str = format (str, " session elog:\n%U", format_elog_track, em,
+                         dt, track_index);
+       }
+      vlib_cli_output (vm, "%v", str);
       return 0;
     }
 
@@ -244,23 +278,23 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
       sst = session_type_from_proto_and_ip (transport_proto, 1);
       vlib_cli_output (vm, "%-40s%-24s", "Listener", "App");
       /* *INDENT-OFF* */
-      pool_foreach (s, smm->sessions[0], ({
+      pool_foreach (s, smm->wrk[0].sessions, ({
        if (s->session_state != SESSION_STATE_LISTENING
            || s->session_type != sst)
          continue;
-       app_name = application_name_from_index (s->app_wrk_index);
+       app_wrk = app_worker_get (s->app_wrk_index);
+       app_name = application_name_from_index (app_wrk->app_index);
        vlib_cli_output (vm, "%U%-25v%", format_stream_session, s, 1,
                         app_name);
-       vec_free (app_name);
       }));
       /* *INDENT-ON* */
       return 0;
     }
 
-  for (i = 0; i < vec_len (smm->sessions); i++)
+  for (i = 0; i < vec_len (smm->wrk); i++)
     {
       u32 once_per_pool;
-      pool = smm->sessions[i];
+      pool = smm->wrk[i].sessions;
 
       once_per_pool = 1;
 
@@ -323,8 +357,9 @@ clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 {
   session_manager_main_t *smm = &session_manager_main;
   u32 thread_index = 0, clear_all = 0;
+  session_manager_worker_t *wrk;
   u32 session_index = ~0;
-  stream_session_t **pool, *session;
+  stream_session_t *session;
 
   if (!smm->is_enabled)
     {
@@ -359,9 +394,9 @@ clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
   if (clear_all)
     {
       /* *INDENT-OFF* */
-      vec_foreach (pool, smm->sessions)
+      vec_foreach (wrk, smm->wrk)
        {
-         pool_foreach(session, *pool, ({
+         pool_foreach(session, wrk->sessions, ({
            clear_session (session);
          }));
        };