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>
19 format_session_fifos (u8 * s, va_list * args)
21 session_t *ss = va_arg (*args, session_t *);
22 int verbose = va_arg (*args, int);
23 session_event_t _e, *e = &_e;
26 if (!ss->rx_fifo || !ss->tx_fifo)
29 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->rx_fifo, verbose);
30 if (verbose > 2 && ss->rx_fifo->shr->has_event)
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");
36 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->tx_fifo, verbose);
37 if (verbose > 2 && ss->tx_fifo->shr->has_event)
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");
46 const char *session_state_str[] = {
47 #define _(sym, str) str,
53 format_session_state (u8 * s, va_list * args)
55 session_t *ss = va_arg (*args, session_t *);
57 if (ss->session_state < SESSION_N_STATES)
58 s = format (s, "%s", session_state_str[ss->session_state]);
60 s = format (s, "UNKNOWN STATE (%d)", ss->session_state);
65 const char *session_flags_str[] = {
66 #define _(sym, str) str,
72 format_session_flags (u8 * s, va_list * args)
74 session_t *ss = va_arg (*args, session_t *);
77 for (i = 0; i < SESSION_N_FLAGS; i++)
78 if (ss->flags & (1 << i))
81 for (i = 0; i < last; i++)
83 if (ss->flags & (1 << i))
84 s = format (s, "%s, ", session_flags_str[i]);
87 s = format (s, "%s", session_flags_str[last]);
93 * Format stream session as per the following format
96 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
101 format_session (u8 * s, va_list * args)
103 session_t *ss = va_arg (*args, session_t *);
104 int verbose = va_arg (*args, int);
105 u32 tp = session_get_transport_proto (ss);
108 if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
110 s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
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);
123 if (ss->session_state >= SESSION_STATE_ACCEPTING
124 || ss->session_state == SESSION_STATE_CREATED)
126 s = format (s, "%U", format_transport_connection, tp,
127 ss->connection_index, ss->thread_index, verbose);
129 s = format (s, "%v", str);
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);
138 else if (ss->session_state == SESSION_STATE_LISTENING)
140 s = format (s, "%U%v", format_transport_listen_connection,
141 tp, ss->connection_index, ss->thread_index, verbose, str);
143 s = format (s, "\n%U", format_session_fifos, ss, verbose);
145 else if (ss->session_state == SESSION_STATE_CONNECTING)
147 if (ss->flags & SESSION_F_HALF_OPEN)
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);
154 s = format (s, "%U", format_transport_connection, tp,
155 ss->connection_index, ss->thread_index, verbose);
159 clib_warning ("Session in state: %d!", ss->session_state);
167 unformat_stream_session_id (unformat_input_t * input, va_list * args)
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 *);
179 clib_memset (lcl, 0, sizeof (*lcl));
180 clib_memset (rmt, 0, sizeof (*rmt));
182 if (unformat (input, "tcp"))
184 *proto = TRANSPORT_PROTO_TCP;
186 else if (unformat (input, "udp"))
188 *proto = TRANSPORT_PROTO_UDP;
193 if (unformat (input, "vrf %u", &vrf))
196 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
197 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
202 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
203 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
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);
220 unformat_session_state (unformat_input_t * input, va_list * args)
222 session_state_t *state = va_arg (*args, session_state_t *);
226 #define _(sym, str) \
227 if (unformat (input, str)) \
229 *state = SESSION_STATE_ ## sym; \
233 foreach_session_state
236 vec_free (state_vec);
241 unformat_session (unformat_input_t * input, va_list * args)
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;
250 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
251 &lcl, &rmt, &lcl_port, &rmt_port, &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);
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);
271 unformat_transport_connection (unformat_input_t * input, va_list * args)
273 transport_connection_t **result = va_arg (*args, transport_connection_t **);
274 u32 suggested_proto = va_arg (*args, u32);
275 transport_connection_t *tc;
277 ip46_address_t lcl, rmt;
278 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
281 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
282 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
285 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
286 if (proto == (u8) ~ 0)
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);
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);
306 session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
308 session_main_t *smm = &session_main;
309 u32 n_closed, thread_index;
312 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
314 pool = smm->wrk[thread_index].sessions;
316 if (!pool_elts (pool))
318 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
324 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
329 if (pool_elts (pool) > 50)
331 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
332 "suppressed. For more details use filters.",
333 thread_index, pool_elts (pool));
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");
345 pool_foreach (s, pool) {
346 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
351 vlib_cli_output (vm, "%U", format_session, s, verbose);
355 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
356 pool_elts (pool) - n_closed);
358 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
359 thread_index, pool_elts (pool) - n_closed, n_closed);
364 session_cli_filter_check (session_t * s, session_state_t * states,
365 transport_proto_t tp)
369 session_state_t *state;
370 vec_foreach (state, states) if (s->session_state == *state)
371 goto check_transport;
377 if (tp != TRANSPORT_PROTO_INVALID && session_get_transport_proto (s) != tp)
384 session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
385 u32 start, u32 end, session_state_t * states,
386 transport_proto_t tp, int verbose)
388 u8 output_suppressed = 0;
389 session_worker_t *wrk;
391 u32 count = 0, max_index;
394 wrk = session_main_get_worker_if_valid (thread_index);
397 vlib_cli_output (vm, "invalid thread index %u", thread_index);
401 pool = wrk->sessions;
403 if (tp == TRANSPORT_PROTO_INVALID && states == 0 && !verbose
404 && (start == 0 && end == ~0))
406 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
411 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
412 for (i = start; i <= clib_min (end, max_index); i++)
414 if (pool_is_free_index (pool, i))
417 s = pool_elt_at_index (pool, i);
419 if (session_cli_filter_check (s, states, tp))
424 if (count > 50 || (verbose > 1 && count > 10))
426 output_suppressed = 1;
429 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
430 vlib_cli_output (vm, "%U", format_session, s, verbose);
435 if (!output_suppressed)
436 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
437 thread_index, count);
439 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
440 " shown. Use finer grained filter.", thread_index,
445 session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
447 session_worker_t *wrk;
449 wrk = session_main_get_worker_if_valid (thread_index);
452 vlib_cli_output (vm, "invalid thread index %u", thread_index);
456 vlib_cli_output (vm, "Thread %d:\n", thread_index);
457 vlib_cli_output (vm, " evt elements alloc: %u",
458 clib_llist_elts (wrk->event_elts));
459 vlib_cli_output (vm, " ctrl evt elt data alloc: %d",
460 clib_llist_elts (wrk->ctrl_evts_data));
464 session_cli_show_events (vlib_main_t * vm, u32 thread_index)
466 session_main_t *smm = &session_main;
469 session_cli_show_events_thread (vm, thread_index);
473 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
474 session_cli_show_events_thread (vm, thread_index);
478 session_cli_print_session_states (vlib_main_t * vm)
480 #define _(sym, str) vlib_cli_output (vm, str);
481 foreach_session_state
485 static clib_error_t *
486 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
487 vlib_cli_command_t * cmd)
489 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
490 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
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;
502 session_cli_return_if_not_enabled ();
504 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
506 if (unformat (input, "verbose %d", &verbose))
508 else if (unformat (input, "verbose"))
510 else if (unformat (input, "listeners %U", unformat_transport_proto,
513 else if (unformat (input, "%U", unformat_session, &s))
517 else if (unformat (input, "thread %u index %u", &thread_index,
520 s = session_get_if_valid (session_index, thread_index);
523 vlib_cli_output (vm, "session is not allocated");
528 else if (unformat (input, "thread %u", &thread_index))
532 else if (unformat (input, "state %U", unformat_session_state, &state))
534 vec_add1 (states, state);
537 else if (unformat (input, "proto %U index %u", unformat_transport_proto,
538 &transport_proto, &transport_index))
540 transport_connection_t *tc;
541 tc = transport_get_connection (transport_proto, transport_index,
545 vlib_cli_output (vm, "transport connection %u thread %u is not"
546 " allocated", transport_index, thread_index);
549 s = session_get_if_valid (tc->s_index, thread_index);
552 vlib_cli_output (vm, "session for transport connection %u "
553 "thread %u does not exist", transport_index,
559 else if (unformat (input, "proto %U", unformat_transport_proto,
562 else if (unformat (input, "range %u %u", &start, &end))
564 else if (unformat (input, "range %u", &start))
569 else if (unformat (input, "elog"))
571 else if (unformat (input, "protos"))
573 vlib_cli_output (vm, "%U", format_transport_protos);
576 else if (unformat (input, "states"))
578 session_cli_print_session_states (vm);
581 else if (unformat (input, "events"))
585 error = clib_error_return (0, "unknown input `%U'",
586 format_unformat_error, input);
593 u8 *str = format (0, "%U", format_session, s, 3);
594 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
596 elog_main_t *em = &vlib_global_main.elog_main;
597 transport_connection_t *tc;
600 tc = session_get_transport (s);
601 track_index = transport_elog_track_index (tc);
602 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
603 * vm->clib_time.seconds_per_clock;
604 if (track_index != ~0)
605 str = format (str, " session elog:\n%U", format_elog_track, em,
608 vlib_cli_output (vm, "%v", str);
615 sst = session_type_from_proto_and_ip (transport_proto, 1);
616 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-24s", "Listener",
619 pool_foreach (s, smm->wrk[0].sessions) {
620 if (s->session_state != SESSION_STATE_LISTENING
621 || s->session_type != sst)
623 app_wrk = app_worker_get (s->app_wrk_index);
624 app_name = application_name_from_index (app_wrk->app_index);
625 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
633 session_cli_show_events (vm, thread_index);
641 error = clib_error_return (0, "invalid range start: %u end: %u",
645 session_cli_show_session_filter (vm, thread_index, start, end, states,
646 transport_proto, verbose);
650 session_cli_show_all_sessions (vm, verbose);
657 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
659 .path = "show session",
660 .short_help = "show session [verbose [n]] [listeners <proto>] "
661 "[<session-id> [elog]] [thread <n> [index <n>] "
662 "[proto <proto>] [state <state>] [range <min> [<max>]] "
663 "[protos] [states] ",
664 .function = show_session_command_fn,
668 clear_session (session_t * s)
670 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
671 app_worker_close_notify (server_wrk, s);
675 static clib_error_t *
676 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
677 vlib_cli_command_t * cmd)
679 session_main_t *smm = &session_main;
680 u32 thread_index = 0, clear_all = 0;
681 session_worker_t *wrk;
682 u32 session_index = ~0;
685 if (!smm->is_enabled)
687 return clib_error_return (0, "session layer is not enabled");
690 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
692 if (unformat (input, "thread %d", &thread_index))
694 else if (unformat (input, "session %d", &session_index))
696 else if (unformat (input, "all"))
699 return clib_error_return (0, "unknown input `%U'",
700 format_unformat_error, input);
703 if (!clear_all && session_index == ~0)
704 return clib_error_return (0, "session <nn> required, but not set.");
706 if (session_index != ~0)
708 session = session_get_if_valid (session_index, thread_index);
710 return clib_error_return (0, "no session %d on thread %d",
711 session_index, thread_index);
712 clear_session (session);
717 vec_foreach (wrk, smm->wrk)
719 pool_foreach (session, wrk->sessions) {
720 clear_session (session);
728 VLIB_CLI_COMMAND (clear_session_command, static) =
730 .path = "clear session",
731 .short_help = "clear session thread <thread> session <index>",
732 .function = clear_session_command_fn,
735 static clib_error_t *
736 show_session_fifo_trace_command_fn (vlib_main_t * vm,
737 unformat_input_t * input,
738 vlib_cli_command_t * cmd)
741 u8 is_rx = 0, *str = 0;
743 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
745 if (unformat (input, "%U", unformat_session, &s))
747 else if (unformat (input, "rx"))
749 else if (unformat (input, "tx"))
752 return clib_error_return (0, "unknown input `%U'",
753 format_unformat_error, input);
758 vlib_cli_output (vm, "fifo tracing not enabled");
764 vlib_cli_output (vm, "could not find session");
769 svm_fifo_dump_trace (str, s->rx_fifo) :
770 svm_fifo_dump_trace (str, s->tx_fifo);
772 vlib_cli_output (vm, "%v", str);
776 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
778 .path = "show session fifo trace",
779 .short_help = "show session fifo trace <session>",
780 .function = show_session_fifo_trace_command_fn,
783 static clib_error_t *
784 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
785 vlib_cli_command_t * cmd)
788 u8 is_rx = 0, *str = 0;
790 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
792 if (unformat (input, "%U", unformat_session, &s))
794 else if (unformat (input, "rx"))
797 return clib_error_return (0, "unknown input `%U'",
798 format_unformat_error, input);
803 vlib_cli_output (vm, "fifo tracing not enabled");
809 vlib_cli_output (vm, "could not find session");
814 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
815 svm_fifo_replay (str, s->tx_fifo, 0, 1);
817 vlib_cli_output (vm, "%v", str);
821 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
823 .path = "session replay fifo",
824 .short_help = "session replay fifo <session>",
825 .function = session_replay_fifo_command_fn,
828 static clib_error_t *
829 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
830 vlib_cli_command_t * cmd)
834 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
836 if (unformat (input, "enable"))
838 else if (unformat (input, "disable"))
841 return clib_error_return (0, "unknown input `%U'",
842 format_unformat_error, input);
846 return clib_error_return (0, "expected enable | disable");
848 return vnet_session_enable_disable (vm, is_en);
851 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
854 .short_help = "session [enable|disable]",
855 .function = session_enable_disable_fn,
858 static clib_error_t *
859 show_session_stats_fn (vlib_main_t *vm, unformat_input_t *input,
860 vlib_cli_command_t *cmd)
862 session_main_t *smm = &session_main;
863 session_worker_t *wrk;
866 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
867 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
870 vec_foreach (wrk, smm->wrk)
872 vlib_cli_output (vm, "Thread %u:\n", wrk - smm->wrk);
873 e = wrk->stats.errors;
874 #define _(name, str) \
875 if (e[SESSION_EP_##name]) \
876 vlib_cli_output (vm, " %lu %s", e[SESSION_EP_##name], str);
877 foreach_session_error
883 VLIB_CLI_COMMAND (show_session_stats_command, static) = {
884 .path = "show session stats",
885 .short_help = "show session stats",
886 .function = show_session_stats_fn,
889 static clib_error_t *
890 clear_session_stats_fn (vlib_main_t *vm, unformat_input_t *input,
891 vlib_cli_command_t *cmd)
893 session_main_t *smm = &session_main;
894 session_worker_t *wrk;
896 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
897 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
900 vec_foreach (wrk, smm->wrk)
902 clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
908 VLIB_CLI_COMMAND (clear_session_stats_command, static) = {
909 .path = "clear session stats",
910 .short_help = "clear session stats",
911 .function = clear_session_stats_fn,
915 * fd.io coding-style-patch-verification: ON
918 * eval: (c-set-style "gnu")