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