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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/session/application.h>
16 #include <vnet/session/session.h>
17 #include <vnet/session/session_debug.h>
20 format_session_fifos (u8 * s, va_list * args)
22 session_t *ss = va_arg (*args, session_t *);
23 int verbose = va_arg (*args, int);
24 session_event_t _e, *e = &_e;
27 if (!ss->rx_fifo || !ss->tx_fifo)
30 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->rx_fifo, verbose);
31 if (verbose > 2 && ss->rx_fifo->has_event)
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");
37 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->tx_fifo, verbose);
38 if (verbose > 2 && ss->tx_fifo->has_event)
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");
47 const char *session_state_str[] = {
48 #define _(sym, str) str,
54 format_session_state (u8 * s, va_list * args)
56 session_t *ss = va_arg (*args, session_t *);
58 if (ss->session_state < SESSION_N_STATES)
59 s = format (s, "%s", session_state_str[ss->session_state]);
61 s = format (s, "UNKNOWN STATE (%d)", ss->session_state);
66 const char *session_flags_str[] = {
67 #define _(sym, str) str,
73 format_session_flags (u8 * s, va_list * args)
75 session_t *ss = va_arg (*args, session_t *);
78 for (i = 0; i < SESSION_N_FLAGS; i++)
79 if (ss->flags & (1 << i))
82 for (i = 0; i < last; i++)
84 if (ss->flags & (1 << i))
85 s = format (s, "%s, ", session_flags_str[i]);
88 s = format (s, "%s", session_flags_str[last]);
94 * Format stream session as per the following format
97 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
102 format_session (u8 * s, va_list * args)
104 session_t *ss = va_arg (*args, session_t *);
105 int verbose = va_arg (*args, int);
106 u32 tp = session_get_transport_proto (ss);
109 if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
111 s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
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);
124 if (ss->session_state >= SESSION_STATE_ACCEPTING
125 || ss->session_state == SESSION_STATE_CREATED)
127 s = format (s, "%U", format_transport_connection, tp,
128 ss->connection_index, ss->thread_index, verbose);
130 s = format (s, "%v", str);
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);
139 else if (ss->session_state == SESSION_STATE_LISTENING)
141 s = format (s, "%U%v", format_transport_listen_connection,
142 tp, ss->connection_index, ss->thread_index, verbose, str);
144 s = format (s, "\n%U", format_session_fifos, ss, verbose);
146 else if (ss->session_state == SESSION_STATE_CONNECTING)
148 s = format (s, "%-40U%v", format_transport_half_open_connection,
149 tp, ss->connection_index, ss->thread_index, str);
153 clib_warning ("Session in state: %d!", ss->session_state);
161 unformat_stream_session_id (unformat_input_t * input, va_list * args)
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 *);
173 clib_memset (lcl, 0, sizeof (*lcl));
174 clib_memset (rmt, 0, sizeof (*rmt));
176 if (unformat (input, "tcp"))
178 *proto = TRANSPORT_PROTO_TCP;
180 else if (unformat (input, "udp"))
182 *proto = TRANSPORT_PROTO_UDP;
187 if (unformat (input, "vrf %u", &vrf))
190 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
191 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
196 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
197 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
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);
214 unformat_session_state (unformat_input_t * input, va_list * args)
216 session_state_t *state = va_arg (*args, session_state_t *);
220 #define _(sym, str) \
221 if (unformat (input, str)) \
223 *state = SESSION_STATE_ ## sym; \
227 foreach_session_state
230 vec_free (state_vec);
235 unformat_session (unformat_input_t * input, va_list * args)
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;
244 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
245 &lcl, &rmt, &lcl_port, &rmt_port, &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);
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);
259 session_pool_remove_peeker (s->thread_index);
266 unformat_transport_connection (unformat_input_t * input, va_list * args)
268 transport_connection_t **result = va_arg (*args, transport_connection_t **);
269 u32 suggested_proto = va_arg (*args, u32);
270 transport_connection_t *tc;
272 ip46_address_t lcl, rmt;
273 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
276 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
277 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
280 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
281 if (proto == (u8) ~ 0)
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);
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);
301 session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
303 session_main_t *smm = &session_main;
304 u32 n_closed, thread_index;
307 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
309 pool = smm->wrk[thread_index].sessions;
311 if (!pool_elts (pool))
313 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
319 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
324 if (pool_elts (pool) > 50)
326 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
327 "suppressed. For more details use filters.",
328 thread_index, pool_elts (pool));
333 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
334 thread_index ? "\n" : "",
335 "Connection", "State", "Rx-f", "Tx-f");
340 pool_foreach(s, pool, ({
341 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
346 vlib_cli_output (vm, "%U", format_session, s, verbose);
351 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
352 pool_elts (pool) - n_closed);
354 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
355 thread_index, pool_elts (pool) - n_closed, n_closed);
360 session_cli_filter_check (session_t * s, session_state_t * states,
361 transport_proto_t tp)
365 session_state_t *state;
366 vec_foreach (state, states) if (s->session_state == *state)
367 goto check_transport;
373 if (tp != TRANSPORT_PROTO_INVALID && session_get_transport_proto (s) != tp)
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)
384 u8 output_suppressed = 0;
385 session_worker_t *wrk;
387 u32 count = 0, max_index;
390 wrk = session_main_get_worker_if_valid (thread_index);
393 vlib_cli_output (vm, "invalid thread index %u", thread_index);
397 pool = wrk->sessions;
399 if (tp == TRANSPORT_PROTO_INVALID && states == 0 && !verbose
400 && (start == 0 && end == ~0))
402 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
407 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
408 for (i = start; i <= clib_min (end, max_index); i++)
410 if (pool_is_free_index (pool, i))
413 s = pool_elt_at_index (pool, i);
415 if (session_cli_filter_check (s, states, tp))
420 if (count > 50 || (verbose > 1 && count > 10))
422 output_suppressed = 1;
425 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
426 vlib_cli_output (vm, "%U", format_session, s, verbose);
431 if (!output_suppressed)
432 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
433 thread_index, count);
435 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
436 " shown. Use finer grained filter.", thread_index,
441 session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
443 session_worker_t *wrk;
445 wrk = session_main_get_worker_if_valid (thread_index);
448 vlib_cli_output (vm, "invalid thread index %u", thread_index);
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));
460 session_cli_show_events (vlib_main_t * vm, u32 thread_index)
462 session_main_t *smm = &session_main;
465 session_cli_show_events_thread (vm, thread_index);
469 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
470 session_cli_show_events_thread (vm, thread_index);
474 session_cli_print_session_states (vlib_main_t * vm)
476 #define _(sym, str) vlib_cli_output (vm, str);
477 foreach_session_state
481 static clib_error_t *
482 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
483 vlib_cli_command_t * cmd)
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;
499 session_cli_return_if_not_enabled ();
501 if (!unformat_user (input, unformat_line_input, line_input))
503 session_cli_show_all_sessions (vm, 0);
507 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
509 if (unformat (line_input, "verbose %d", &verbose))
511 else if (unformat (line_input, "verbose"))
513 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
516 else if (unformat (line_input, "%U", unformat_session, &s))
520 else if (unformat (line_input, "thread %u index %u", &thread_index,
523 s = session_get_if_valid (session_index, thread_index);
526 vlib_cli_output (vm, "session is not allocated");
531 else if (unformat (line_input, "thread %u", &thread_index))
536 if (unformat (line_input, "state %U", unformat_session_state, &state))
538 vec_add1 (states, state);
541 else if (unformat (line_input, "proto %U index %u",
542 unformat_transport_proto, &transport_proto,
545 transport_connection_t *tc;
546 tc = transport_get_connection (transport_proto, transport_index,
550 vlib_cli_output (vm, "transport connection %u thread %u is not"
551 " allocated", transport_index, thread_index);
554 s = session_get_if_valid (tc->s_index, thread_index);
557 vlib_cli_output (vm, "session for transport connection %u "
558 "thread %u does not exist", transport_index,
564 else if (unformat (line_input, "proto %U", unformat_transport_proto,
567 else if (unformat (line_input, "range %u %u", &start, &end))
569 else if (unformat (line_input, "range %u", &start))
574 else if (unformat (line_input, "elog"))
576 else if (unformat (line_input, "protos"))
578 vlib_cli_output (vm, "%U", format_transport_protos);
581 else if (unformat (line_input, "states"))
583 session_cli_print_session_states (vm);
586 else if (unformat (line_input, "events"))
590 error = clib_error_return (0, "unknown input `%U'",
591 format_unformat_error, line_input);
598 u8 *str = format (0, "%U", format_session, s, 3);
599 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
601 elog_main_t *em = &vm->elog_main;
602 transport_connection_t *tc;
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,
613 vlib_cli_output (vm, "%v", str);
620 sst = session_type_from_proto_and_ip (transport_proto, 1);
621 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
623 pool_foreach (s, smm->wrk[0].sessions, ({
624 if (s->session_state != SESSION_STATE_LISTENING
625 || s->session_type != sst)
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,
638 session_cli_show_events (vm, thread_index);
646 error = clib_error_return (0, "invalid range start: %u end: %u",
650 session_cli_show_session_filter (vm, thread_index, start, end, states,
651 transport_proto, verbose);
655 session_cli_show_all_sessions (vm, verbose);
658 unformat_free (line_input);
664 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
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,
676 clear_session (session_t * s)
678 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
679 app_worker_close_notify (server_wrk, s);
683 static clib_error_t *
684 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
685 vlib_cli_command_t * cmd)
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;
693 if (!smm->is_enabled)
695 return clib_error_return (0, "session layer is not enabled");
698 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
700 if (unformat (input, "thread %d", &thread_index))
702 else if (unformat (input, "session %d", &session_index))
704 else if (unformat (input, "all"))
707 return clib_error_return (0, "unknown input `%U'",
708 format_unformat_error, input);
711 if (!clear_all && session_index == ~0)
712 return clib_error_return (0, "session <nn> required, but not set.");
714 if (session_index != ~0)
716 session = session_get_if_valid (session_index, thread_index);
718 return clib_error_return (0, "no session %d on thread %d",
719 session_index, thread_index);
720 clear_session (session);
726 vec_foreach (wrk, smm->wrk)
728 pool_foreach(session, wrk->sessions, ({
729 clear_session (session);
739 VLIB_CLI_COMMAND (clear_session_command, static) =
741 .path = "clear session",
742 .short_help = "clear session thread <thread> session <index>",
743 .function = clear_session_command_fn,
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)
753 u8 is_rx = 0, *str = 0;
755 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
757 if (unformat (input, "%U", unformat_session, &s))
759 else if (unformat (input, "rx"))
761 else if (unformat (input, "tx"))
764 return clib_error_return (0, "unknown input `%U'",
765 format_unformat_error, input);
770 vlib_cli_output (vm, "fifo tracing not enabled");
776 vlib_cli_output (vm, "could not find session");
781 svm_fifo_dump_trace (str, s->rx_fifo) :
782 svm_fifo_dump_trace (str, s->tx_fifo);
784 vlib_cli_output (vm, "%v", str);
789 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
791 .path = "show session fifo trace",
792 .short_help = "show session fifo trace <session>",
793 .function = show_session_fifo_trace_command_fn,
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)
802 u8 is_rx = 0, *str = 0;
804 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
806 if (unformat (input, "%U", unformat_session, &s))
808 else if (unformat (input, "rx"))
811 return clib_error_return (0, "unknown input `%U'",
812 format_unformat_error, input);
817 vlib_cli_output (vm, "fifo tracing not enabled");
823 vlib_cli_output (vm, "could not find session");
828 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
829 svm_fifo_replay (str, s->tx_fifo, 0, 1);
831 vlib_cli_output (vm, "%v", str);
836 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
838 .path = "session replay fifo",
839 .short_help = "session replay fifo <session>",
840 .function = session_replay_fifo_command_fn,
844 static clib_error_t *
845 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
846 vlib_cli_command_t * cmd)
848 unformat_input_t _line_input, *line_input = &_line_input;
852 if (!unformat_user (input, unformat_line_input, line_input))
853 return clib_error_return (0, "expected enable | disable");
855 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
857 if (unformat (line_input, "enable"))
859 else if (unformat (line_input, "disable"))
863 error = clib_error_return (0, "unknown input `%U'",
864 format_unformat_error, line_input);
865 unformat_free (line_input);
870 unformat_free (line_input);
871 return vnet_session_enable_disable (vm, is_en);
875 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
878 .short_help = "session [enable|disable]",
879 .function = session_enable_disable_fn,
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)
891 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
892 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
895 for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
897 vlib_cli_output (vm, "Threads %u:\n", thread);
898 session_dbg_evts_t *sdm = &session_dbg_main.wrk[thread];
900 #define _(sym, disp, type, str) \
904 vlib_cli_output (vm, "\t %25s : %12lu ", \
905 str, sdm->sess_dbg_evt_type[SESS_Q_##sym].u64);\
907 vlib_cli_output (vm, "\t %25s : %12.3f ", \
908 str, sdm->sess_dbg_evt_type[SESS_Q_##sym].f64);\
911 foreach_session_events
919 VLIB_CLI_COMMAND (show_session_dbg_clock_cycles_command, static) =
921 .path = "show session dbg clock_cycles",
922 .short_help = "show session dbg clock_cycles",
923 .function = show_session_dbg_clock_cycles_fn,
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)
932 session_dbg_evts_t *sdb;
935 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
936 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
939 for (thread = 0; thread < vec_len (session_dbg_main.wrk); thread++)
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]);
951 VLIB_CLI_COMMAND (clear_session_clock_cycles_command, static) =
953 .path = "clear session dbg clock_cycles",
954 .short_help = "clear session dbg clock_cycles",
955 .function = clear_session_dbg_clock_cycles_fn,
957 #endif /* #if SESSION_DEBUG */
962 * fd.io coding-style-patch-verification: ON
965 * eval: (c-set-style "gnu")