session: adding debug events
[vpp.git] / src / vnet / session / session_cli.c
1 /*
2  * Copyright (c) 2017-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 #include <vnet/session/application.h>
16 #include <vnet/session/session.h>
17 #include <vnet/session/session_debug.h>
18
19 u8 *
20 format_session_fifos (u8 * s, va_list * args)
21 {
22   session_t *ss = va_arg (*args, session_t *);
23   int verbose = va_arg (*args, int);
24   session_event_t _e, *e = &_e;
25   u8 found;
26
27   if (!ss->rx_fifo || !ss->tx_fifo)
28     return s;
29
30   s = format (s, " Rx fifo: %U", format_svm_fifo, ss->rx_fifo, verbose);
31   if (verbose > 2 && ss->rx_fifo->has_event)
32     {
33       found = session_node_lookup_fifo_event (ss->rx_fifo, e);
34       s = format (s, " session node event: %s\n",
35                   found ? "found" : "not found");
36     }
37   s = format (s, " Tx fifo: %U", format_svm_fifo, ss->tx_fifo, verbose);
38   if (verbose > 2 && ss->tx_fifo->has_event)
39     {
40       found = session_node_lookup_fifo_event (ss->tx_fifo, e);
41       s = format (s, " session node event: %s\n",
42                   found ? "found" : "not found");
43     }
44   return s;
45 }
46
47 const char *session_state_str[] = {
48 #define _(sym, str) str,
49   foreach_session_state
50 #undef _
51 };
52
53 u8 *
54 format_session_state (u8 * s, va_list * args)
55 {
56   session_t *ss = va_arg (*args, session_t *);
57
58   if (ss->session_state < SESSION_N_STATES)
59     s = format (s, "%s", session_state_str[ss->session_state]);
60   else
61     s = format (s, "UNKNOWN STATE (%d)", ss->session_state);
62
63   return s;
64 }
65
66 const char *session_flags_str[] = {
67 #define _(sym, str) str,
68   foreach_session_flag
69 #undef _
70 };
71
72 u8 *
73 format_session_flags (u8 * s, va_list * args)
74 {
75   session_t *ss = va_arg (*args, session_t *);
76   int i, last = -1;
77
78   for (i = 0; i < SESSION_N_FLAGS; i++)
79     if (ss->flags & (1 << i))
80       last = i;
81
82   for (i = 0; i < last; i++)
83     {
84       if (ss->flags & (1 << i))
85         s = format (s, "%s, ", session_flags_str[i]);
86     }
87   if (last >= 0)
88     s = format (s, "%s", session_flags_str[last]);
89
90   return s;
91 }
92
93 /**
94  * Format stream session as per the following format
95  *
96  * verbose:
97  *   "Connection", "Rx fifo", "Tx fifo", "Session Index"
98  * non-verbose:
99  *   "Connection"
100  */
101 u8 *
102 format_session (u8 * s, va_list * args)
103 {
104   session_t *ss = va_arg (*args, session_t *);
105   int verbose = va_arg (*args, int);
106   u32 tp = session_get_transport_proto (ss);
107   u8 *str = 0;
108
109   if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
110     {
111       s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
112       return s;
113     }
114
115   if (verbose == 1)
116     {
117       u32 rxf, txf;
118
119       rxf = ss->rx_fifo ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
120       txf = ss->tx_fifo ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
121       str = format (0, "%-10u%-10u", rxf, txf);
122     }
123
124   if (ss->session_state >= SESSION_STATE_ACCEPTING
125       || ss->session_state == SESSION_STATE_CREATED)
126     {
127       s = format (s, "%U", format_transport_connection, tp,
128                   ss->connection_index, ss->thread_index, verbose);
129       if (verbose == 1)
130         s = format (s, "%v", str);
131       if (verbose > 1)
132         {
133           s = format (s, "%U", format_session_fifos, ss, verbose);
134           s = format (s, " session: state: %U opaque: 0x%x flags: %U\n",
135                       format_session_state, ss, ss->opaque,
136                       format_session_flags, ss);
137         }
138     }
139   else if (ss->session_state == SESSION_STATE_LISTENING)
140     {
141       s = format (s, "%U%v", format_transport_listen_connection,
142                   tp, ss->connection_index, ss->thread_index, verbose, str);
143       if (verbose > 1)
144         s = format (s, "\n%U", format_session_fifos, ss, verbose);
145     }
146   else if (ss->session_state == SESSION_STATE_CONNECTING)
147     {
148       s = format (s, "%-40U%v", format_transport_half_open_connection,
149                   tp, ss->connection_index, ss->thread_index, str);
150     }
151   else
152     {
153       clib_warning ("Session in state: %d!", ss->session_state);
154     }
155   vec_free (str);
156
157   return s;
158 }
159
160 uword
161 unformat_stream_session_id (unformat_input_t * input, va_list * args)
162 {
163   u8 *proto = va_arg (*args, u8 *);
164   u32 *fib_index = va_arg (*args, u32 *);
165   ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
166   ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
167   u16 *lcl_port = va_arg (*args, u16 *);
168   u16 *rmt_port = va_arg (*args, u16 *);
169   u8 *is_ip4 = va_arg (*args, u8 *);
170   u8 tuple_is_set = 0;
171   u32 vrf = ~0;
172
173   clib_memset (lcl, 0, sizeof (*lcl));
174   clib_memset (rmt, 0, sizeof (*rmt));
175
176   if (unformat (input, "tcp"))
177     {
178       *proto = TRANSPORT_PROTO_TCP;
179     }
180   else if (unformat (input, "udp"))
181     {
182       *proto = TRANSPORT_PROTO_UDP;
183     }
184   else
185     return 0;
186
187   if (unformat (input, "vrf %u", &vrf))
188     ;
189
190   if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
191                 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
192     {
193       *is_ip4 = 1;
194       tuple_is_set = 1;
195     }
196   else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
197                      lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
198     {
199       *is_ip4 = 0;
200       tuple_is_set = 1;
201     }
202
203   if (vrf != ~0)
204     {
205       fib_protocol_t fib_proto;
206       fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
207       *fib_index = fib_table_find (fib_proto, vrf);
208     }
209
210   return tuple_is_set;
211 }
212
213 uword
214 unformat_session_state (unformat_input_t * input, va_list * args)
215 {
216   session_state_t *state = va_arg (*args, session_state_t *);
217   u8 *state_vec = 0;
218   int rv = 0;
219
220 #define _(sym, str)                                     \
221   if (unformat (input, str))                            \
222     {                                                   \
223       *state = SESSION_STATE_ ## sym;                   \
224       rv = 1;                                           \
225       goto done;                                        \
226     }
227   foreach_session_state
228 #undef _
229 done:
230   vec_free (state_vec);
231   return rv;
232 }
233
234 uword
235 unformat_session (unformat_input_t * input, va_list * args)
236 {
237   session_t **result = va_arg (*args, session_t **);
238   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
239   ip46_address_t lcl, rmt;
240   session_t *s;
241   u8 proto = ~0;
242   u8 is_ip4 = 0;
243
244   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
245                  &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
246     return 0;
247
248   if (is_ip4)
249     s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
250                               clib_host_to_net_u16 (lcl_port),
251                               clib_host_to_net_u16 (rmt_port), proto);
252   else
253     s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
254                               clib_host_to_net_u16 (lcl_port),
255                               clib_host_to_net_u16 (rmt_port), proto);
256   if (s)
257     {
258       *result = s;
259       session_pool_remove_peeker (s->thread_index);
260       return 1;
261     }
262   return 0;
263 }
264
265 uword
266 unformat_transport_connection (unformat_input_t * input, va_list * args)
267 {
268   transport_connection_t **result = va_arg (*args, transport_connection_t **);
269   u32 suggested_proto = va_arg (*args, u32);
270   transport_connection_t *tc;
271   u8 proto = ~0;
272   ip46_address_t lcl, rmt;
273   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
274   u8 is_ip4 = 0;
275
276   if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
277                  &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
278     return 0;
279
280   proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
281   if (proto == (u8) ~ 0)
282     return 0;
283   if (is_ip4)
284     tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
285                                      clib_host_to_net_u16 (lcl_port),
286                                      clib_host_to_net_u16 (rmt_port), proto);
287   else
288     tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
289                                      clib_host_to_net_u16 (lcl_port),
290                                      clib_host_to_net_u16 (rmt_port), proto);
291
292   if (tc)
293     {
294       *result = tc;
295       return 1;
296     }
297   return 0;
298 }
299
300 static void
301 session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
302 {
303   session_main_t *smm = &session_main;
304   u32 n_closed, thread_index;
305   session_t *pool, *s;
306
307   for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
308     {
309       pool = smm->wrk[thread_index].sessions;
310
311       if (!pool_elts (pool))
312         {
313           vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
314           continue;
315         }
316
317       if (!verbose)
318         {
319           vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
320                            pool_elts (pool));
321           continue;
322         }
323
324       if (pool_elts (pool) > 50)
325         {
326           vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
327                            "suppressed. For more details use filters.",
328                            thread_index, pool_elts (pool));
329           continue;
330         }
331
332       if (verbose == 1)
333         vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
334                          thread_index ? "\n" : "",
335                          "Connection", "State", "Rx-f", "Tx-f");
336
337       n_closed = 0;
338
339       /* *INDENT-OFF* */
340       pool_foreach(s, pool, ({
341         if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
342           {
343             n_closed += 1;
344             continue;
345           }
346         vlib_cli_output (vm, "%U", format_session, s, verbose);
347       }));
348       /* *INDENT-ON* */
349
350       if (!n_closed)
351         vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
352                          pool_elts (pool) - n_closed);
353       else
354         vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
355                          thread_index, pool_elts (pool) - n_closed, n_closed);
356     }
357 }
358
359 static int
360 session_cli_filter_check (session_t * s, session_state_t * states,
361                           transport_proto_t tp)
362 {
363   if (states)
364     {
365       session_state_t *state;
366       vec_foreach (state, states) if (s->session_state == *state)
367         goto check_transport;
368       return 0;
369     }
370
371 check_transport:
372
373   if (tp != TRANSPORT_PROTO_INVALID && session_get_transport_proto (s) != tp)
374     return 0;
375
376   return 1;
377 }
378
379 static void
380 session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
381                                  u32 start, u32 end, session_state_t * states,
382                                  transport_proto_t tp, int verbose)
383 {
384   u8 output_suppressed = 0;
385   session_worker_t *wrk;
386   session_t *pool, *s;
387   u32 count = 0, max_index;
388   int i;
389
390   wrk = session_main_get_worker_if_valid (thread_index);
391   if (!wrk)
392     {
393       vlib_cli_output (vm, "invalid thread index %u", thread_index);
394       return;
395     }
396
397   pool = wrk->sessions;
398
399   if (tp == TRANSPORT_PROTO_INVALID && states == 0 && !verbose
400       && (start == 0 && end == ~0))
401     {
402       vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
403                        pool_elts (pool));
404       return;
405     }
406
407   max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
408   for (i = start; i <= clib_min (end, max_index); i++)
409     {
410       if (pool_is_free_index (pool, i))
411         continue;
412
413       s = pool_elt_at_index (pool, i);
414
415       if (session_cli_filter_check (s, states, tp))
416         {
417           count += 1;
418           if (verbose)
419             {
420               if (count > 50 || (verbose > 1 && count > 10))
421                 {
422                   output_suppressed = 1;
423                   continue;
424                 }
425               if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
426                 vlib_cli_output (vm, "%U", format_session, s, verbose);
427             }
428         }
429     }
430
431   if (!output_suppressed)
432     vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
433                      thread_index, count);
434   else
435     vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
436                      " shown. Use finer grained filter.", thread_index,
437                      count);
438 }
439
440 void
441 session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
442 {
443   session_worker_t *wrk;
444
445   wrk = session_main_get_worker_if_valid (thread_index);
446   if (!wrk)
447     {
448       vlib_cli_output (vm, "invalid thread index %u", thread_index);
449       return;
450     }
451
452   vlib_cli_output (vm, "Thread %d:\n", thread_index);
453   vlib_cli_output (vm, " evt elements alloc: %u",
454                    pool_elts (wrk->event_elts));
455   vlib_cli_output (vm, " ctrl evt elt data alloc: %d",
456                    pool_elts (wrk->ctrl_evts_data));
457 }
458
459 static void
460 session_cli_show_events (vlib_main_t * vm, u32 thread_index)
461 {
462   session_main_t *smm = &session_main;
463   if (!thread_index)
464     {
465       session_cli_show_events_thread (vm, thread_index);
466       return;
467     }
468
469   for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
470     session_cli_show_events_thread (vm, thread_index);
471 }
472
473 static void
474 session_cli_print_session_states (vlib_main_t * vm)
475 {
476 #define _(sym, str) vlib_cli_output (vm, str);
477   foreach_session_state
478 #undef _
479 }
480
481 static clib_error_t *
482 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
483                          vlib_cli_command_t * cmd)
484 {
485   u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
486   u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
487   unformat_input_t _line_input, *line_input = &_line_input;
488   transport_proto_t transport_proto = TRANSPORT_PROTO_INVALID;
489   session_state_t state = SESSION_N_STATES, *states = 0;
490   session_main_t *smm = &session_main;
491   clib_error_t *error = 0;
492   app_worker_t *app_wrk;
493   u32 transport_index;
494   const u8 *app_name;
495   u8 do_events = 0;
496   int verbose = 0;
497   session_t *s;
498
499   session_cli_return_if_not_enabled ();
500
501   if (!unformat_user (input, unformat_line_input, line_input))
502     {
503       session_cli_show_all_sessions (vm, 0);
504       return 0;
505     }
506
507   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
508     {
509       if (unformat (line_input, "verbose %d", &verbose))
510         ;
511       else if (unformat (line_input, "verbose"))
512         verbose = 1;
513       else if (unformat (line_input, "listeners %U", unformat_transport_proto,
514                          &transport_proto))
515         do_listeners = 1;
516       else if (unformat (line_input, "%U", unformat_session, &s))
517         {
518           one_session = 1;
519         }
520       else if (unformat (line_input, "thread %u index %u", &thread_index,
521                          &session_index))
522         {
523           s = session_get_if_valid (session_index, thread_index);
524           if (!s)
525             {
526               vlib_cli_output (vm, "session is not allocated");
527               goto done;
528             }
529           one_session = 1;
530         }
531       else if (unformat (line_input, "thread %u", &thread_index))
532         {
533           do_filter = 1;
534         }
535       else
536         if (unformat (line_input, "state %U", unformat_session_state, &state))
537         {
538           vec_add1 (states, state);
539           do_filter = 1;
540         }
541       else if (unformat (line_input, "proto %U index %u",
542                          unformat_transport_proto, &transport_proto,
543                          &transport_index))
544         {
545           transport_connection_t *tc;
546           tc = transport_get_connection (transport_proto, transport_index,
547                                          thread_index);
548           if (!tc)
549             {
550               vlib_cli_output (vm, "transport connection %u thread %u is not"
551                                " allocated", transport_index, thread_index);
552               goto done;
553             }
554           s = session_get_if_valid (tc->s_index, thread_index);
555           if (!s)
556             {
557               vlib_cli_output (vm, "session for transport connection %u "
558                                "thread %u does not exist", transport_index,
559                                thread_index);
560               goto done;
561             }
562           one_session = 1;
563         }
564       else if (unformat (line_input, "proto %U", unformat_transport_proto,
565                          &transport_proto))
566         do_filter = 1;
567       else if (unformat (line_input, "range %u %u", &start, &end))
568         do_filter = 1;
569       else if (unformat (line_input, "range %u", &start))
570         {
571           end = start + 50;
572           do_filter = 1;
573         }
574       else if (unformat (line_input, "elog"))
575         do_elog = 1;
576       else if (unformat (line_input, "protos"))
577         {
578           vlib_cli_output (vm, "%U", format_transport_protos);
579           goto done;
580         }
581       else if (unformat (line_input, "states"))
582         {
583           session_cli_print_session_states (vm);
584           goto done;
585         }
586       else if (unformat (line_input, "events"))
587         do_events = 1;
588       else
589         {
590           error = clib_error_return (0, "unknown input `%U'",
591                                      format_unformat_error, line_input);
592           goto done;
593         }
594     }
595
596   if (one_session)
597     {
598       u8 *str = format (0, "%U", format_session, s, 3);
599       if (do_elog && s->session_state != SESSION_STATE_LISTENING)
600         {
601           elog_main_t *em = &vm->elog_main;
602           transport_connection_t *tc;
603           f64 dt;
604
605           tc = session_get_transport (s);
606           track_index = transport_elog_track_index (tc);
607           dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
608             * vm->clib_time.seconds_per_clock;
609           if (track_index != ~0)
610             str = format (str, " session elog:\n%U", format_elog_track, em,
611                           dt, track_index);
612         }
613       vlib_cli_output (vm, "%v", str);
614       vec_free (str);
615       goto done;
616     }
617
618   if (do_listeners)
619     {
620       sst = session_type_from_proto_and_ip (transport_proto, 1);
621       vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
622       /* *INDENT-OFF* */
623       pool_foreach (s, smm->wrk[0].sessions, ({
624         if (s->session_state != SESSION_STATE_LISTENING
625             || s->session_type != sst)
626           continue;
627         app_wrk = app_worker_get (s->app_wrk_index);
628         app_name = application_name_from_index (app_wrk->app_index);
629         vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
630                          app_name);
631       }));
632       /* *INDENT-ON* */
633       goto done;
634     }
635
636   if (do_events)
637     {
638       session_cli_show_events (vm, thread_index);
639       goto done;
640     }
641
642   if (do_filter)
643     {
644       if (end < start)
645         {
646           error = clib_error_return (0, "invalid range start: %u end: %u",
647                                      start, end);
648           goto done;
649         }
650       session_cli_show_session_filter (vm, thread_index, start, end, states,
651                                        transport_proto, verbose);
652       goto done;
653     }
654
655   session_cli_show_all_sessions (vm, verbose);
656
657 done:
658   unformat_free (line_input);
659   vec_free (states);
660   return error;
661 }
662
663 /* *INDENT-OFF* */
664 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
665 {
666   .path = "show session",
667   .short_help = "show session [verbose [n]] [listeners <proto>] "
668                 "[<session-id> [elog]] [thread <n> [index <n>] "
669                 "[proto <proto>] [state <state>] [range <min> [<max>]] "
670                 "[protos] [states] ",
671   .function = show_session_command_fn,
672 };
673 /* *INDENT-ON* */
674
675 static int
676 clear_session (session_t * s)
677 {
678   app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
679   app_worker_close_notify (server_wrk, s);
680   return 0;
681 }
682
683 static clib_error_t *
684 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
685                           vlib_cli_command_t * cmd)
686 {
687   session_main_t *smm = &session_main;
688   u32 thread_index = 0, clear_all = 0;
689   session_worker_t *wrk;
690   u32 session_index = ~0;
691   session_t *session;
692
693   if (!smm->is_enabled)
694     {
695       return clib_error_return (0, "session layer is not enabled");
696     }
697
698   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
699     {
700       if (unformat (input, "thread %d", &thread_index))
701         ;
702       else if (unformat (input, "session %d", &session_index))
703         ;
704       else if (unformat (input, "all"))
705         clear_all = 1;
706       else
707         return clib_error_return (0, "unknown input `%U'",
708                                   format_unformat_error, input);
709     }
710
711   if (!clear_all && session_index == ~0)
712     return clib_error_return (0, "session <nn> required, but not set.");
713
714   if (session_index != ~0)
715     {
716       session = session_get_if_valid (session_index, thread_index);
717       if (!session)
718         return clib_error_return (0, "no session %d on thread %d",
719                                   session_index, thread_index);
720       clear_session (session);
721     }
722
723   if (clear_all)
724     {
725       /* *INDENT-OFF* */
726       vec_foreach (wrk, smm->wrk)
727         {
728           pool_foreach(session, wrk->sessions, ({
729             clear_session (session);
730           }));
731         };
732       /* *INDENT-ON* */
733     }
734
735   return 0;
736 }
737
738 /* *INDENT-OFF* */
739 VLIB_CLI_COMMAND (clear_session_command, static) =
740 {
741   .path = "clear session",
742   .short_help = "clear session thread <thread> session <index>",
743   .function = clear_session_command_fn,
744 };
745 /* *INDENT-ON* */
746
747 static clib_error_t *
748 show_session_fifo_trace_command_fn (vlib_main_t * vm,
749                                     unformat_input_t * input,
750                                     vlib_cli_command_t * cmd)
751 {
752   session_t *s = 0;
753   u8 is_rx = 0, *str = 0;
754
755   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
756     {
757       if (unformat (input, "%U", unformat_session, &s))
758         ;
759       else if (unformat (input, "rx"))
760         is_rx = 1;
761       else if (unformat (input, "tx"))
762         is_rx = 0;
763       else
764         return clib_error_return (0, "unknown input `%U'",
765                                   format_unformat_error, input);
766     }
767
768   if (!SVM_FIFO_TRACE)
769     {
770       vlib_cli_output (vm, "fifo tracing not enabled");
771       return 0;
772     }
773
774   if (!s)
775     {
776       vlib_cli_output (vm, "could not find session");
777       return 0;
778     }
779
780   str = is_rx ?
781     svm_fifo_dump_trace (str, s->rx_fifo) :
782     svm_fifo_dump_trace (str, s->tx_fifo);
783
784   vlib_cli_output (vm, "%v", str);
785   return 0;
786 }
787
788 /* *INDENT-OFF* */
789 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
790 {
791   .path = "show session fifo trace",
792   .short_help = "show session fifo trace <session>",
793   .function = show_session_fifo_trace_command_fn,
794 };
795 /* *INDENT-ON* */
796
797 static clib_error_t *
798 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
799                                 vlib_cli_command_t * cmd)
800 {
801   session_t *s = 0;
802   u8 is_rx = 0, *str = 0;
803
804   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
805     {
806       if (unformat (input, "%U", unformat_session, &s))
807         ;
808       else if (unformat (input, "rx"))
809         is_rx = 1;
810       else
811         return clib_error_return (0, "unknown input `%U'",
812                                   format_unformat_error, input);
813     }
814
815   if (!SVM_FIFO_TRACE)
816     {
817       vlib_cli_output (vm, "fifo tracing not enabled");
818       return 0;
819     }
820
821   if (!s)
822     {
823       vlib_cli_output (vm, "could not find session");
824       return 0;
825     }
826
827   str = is_rx ?
828     svm_fifo_replay (str, s->rx_fifo, 0, 1) :
829     svm_fifo_replay (str, s->tx_fifo, 0, 1);
830
831   vlib_cli_output (vm, "%v", str);
832   return 0;
833 }
834
835 /* *INDENT-OFF* */
836 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
837 {
838   .path = "session replay fifo",
839   .short_help = "session replay fifo <session>",
840   .function = session_replay_fifo_command_fn,
841 };
842 /* *INDENT-ON* */
843
844 static clib_error_t *
845 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
846                            vlib_cli_command_t * cmd)
847 {
848   unformat_input_t _line_input, *line_input = &_line_input;
849   u8 is_en = 1;
850   clib_error_t *error;
851
852   if (!unformat_user (input, unformat_line_input, line_input))
853     return clib_error_return (0, "expected enable | disable");
854
855   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
856     {
857       if (unformat (line_input, "enable"))
858         is_en = 1;
859       else if (unformat (line_input, "disable"))
860         is_en = 0;
861       else
862         {
863           error = clib_error_return (0, "unknown input `%U'",
864                                      format_unformat_error, line_input);
865           unformat_free (line_input);
866           return error;
867         }
868     }
869
870   unformat_free (line_input);
871   return vnet_session_enable_disable (vm, is_en);
872 }
873
874 /* *INDENT-OFF* */
875 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
876 {
877   .path = "session",
878   .short_help = "session [enable|disable]",
879   .function = session_enable_disable_fn,
880 };
881 /* *INDENT-ON* */
882
883 #if SESSION_DEBUG
884
885 static clib_error_t *
886 show_session_dbg_clock_cycles_fn (vlib_main_t * vm, unformat_input_t * input,
887                                   vlib_cli_command_t * cmd)
888 {
889   u32 thread;
890
891   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
892     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
893                               input);
894
895   for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
896     {
897       vlib_cli_output (vm, "Threads %u:\n", thread);
898       session_dbg_evts_t *sdm = &session_dbg_main.wrk[thread];
899
900 #define _(sym, disp, type, str)                                                                          \
901   if(disp)                                                              \
902     {                                                                   \
903       if (!type)                                                        \
904         vlib_cli_output (vm, "\t %25s : %12lu ",                        \
905                          str, sdm->sess_dbg_evt_type[SESS_Q_##sym].u64);\
906       else                                                              \
907         vlib_cli_output (vm, "\t %25s : %12.3f ",                       \
908                          str, sdm->sess_dbg_evt_type[SESS_Q_##sym].f64);\
909     }
910
911       foreach_session_events
912 #undef _
913     }
914   return 0;
915 }
916
917
918 /* *INDENT-OFF* */
919 VLIB_CLI_COMMAND (show_session_dbg_clock_cycles_command, static) =
920 {
921   .path = "show session dbg clock_cycles",
922   .short_help = "show session dbg clock_cycles",
923   .function = show_session_dbg_clock_cycles_fn,
924 };
925 /* *INDENT-ON* */
926
927
928 static clib_error_t *
929 clear_session_dbg_clock_cycles_fn (vlib_main_t * vm, unformat_input_t * input,
930                                    vlib_cli_command_t * cmd)
931 {
932   session_dbg_evts_t *sdb;
933   u32 thread;
934
935   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
936     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
937                               input);
938
939   for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
940     {
941       sdb = &session_dbg_main.wrk[thread];
942       clib_memset (sdb, 0, sizeof (session_dbg_evts_t));
943       sdb->last_time = vlib_time_now (vlib_mains[thread]);
944     }
945
946   return 0;
947 }
948
949
950 /* *INDENT-OFF* */
951 VLIB_CLI_COMMAND (clear_session_clock_cycles_command, static) =
952 {
953   .path = "clear session dbg clock_cycles",
954   .short_help = "clear session dbg clock_cycles",
955   .function = clear_session_dbg_clock_cycles_fn,
956 };
957 #endif /* #if SESSION_DEBUG */
958
959 /* *INDENT-ON* */
960
961 /*
962  * fd.io coding-style-patch-verification: ON
963  *
964  * Local Variables:
965  * eval: (c-set-style "gnu")
966  * End:
967  */