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