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->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->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);
116 u8 post_accept = ss->session_state >= SESSION_STATE_ACCEPTING;
117 u8 hasf = post_accept
118 || session_transport_service_type (ss) == TRANSPORT_SERVICE_CL;
121 rxf = hasf ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
122 txf = hasf ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
123 str = format (0, "%-10u%-10u", rxf, txf);
126 if (ss->session_state >= SESSION_STATE_ACCEPTING
127 || ss->session_state == SESSION_STATE_CREATED)
129 s = format (s, "%U", format_transport_connection, tp,
130 ss->connection_index, ss->thread_index, verbose);
132 s = format (s, "%v", str);
135 s = format (s, "%U", format_session_fifos, ss, verbose);
136 s = format (s, " session: state: %U flags: %U\n",
137 format_session_state, ss, format_session_flags, ss);
140 else if (ss->session_state == SESSION_STATE_LISTENING)
142 s = format (s, "%U%v", format_transport_listen_connection,
143 tp, ss->connection_index, ss->thread_index, verbose, str);
145 s = format (s, "\n%U", format_session_fifos, ss, verbose);
147 else if (ss->session_state == SESSION_STATE_CONNECTING)
149 s = format (s, "%-40U%v", format_transport_half_open_connection,
150 tp, ss->connection_index, ss->thread_index, str);
154 clib_warning ("Session in state: %d!", ss->session_state);
162 unformat_stream_session_id (unformat_input_t * input, va_list * args)
164 u8 *proto = va_arg (*args, u8 *);
165 u32 *fib_index = va_arg (*args, u32 *);
166 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
167 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
168 u16 *lcl_port = va_arg (*args, u16 *);
169 u16 *rmt_port = va_arg (*args, u16 *);
170 u8 *is_ip4 = va_arg (*args, u8 *);
174 clib_memset (lcl, 0, sizeof (*lcl));
175 clib_memset (rmt, 0, sizeof (*rmt));
177 if (unformat (input, "tcp"))
179 *proto = TRANSPORT_PROTO_TCP;
181 else if (unformat (input, "udp"))
183 *proto = TRANSPORT_PROTO_UDP;
188 if (unformat (input, "vrf %u", &vrf))
191 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
192 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
197 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
198 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
206 fib_protocol_t fib_proto;
207 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
208 *fib_index = fib_table_find (fib_proto, vrf);
215 unformat_session_state (unformat_input_t * input, va_list * args)
217 session_state_t *state = va_arg (*args, session_state_t *);
221 #define _(sym, str) \
222 if (unformat (input, str)) \
224 *state = SESSION_STATE_ ## sym; \
228 foreach_session_state
231 vec_free (state_vec);
236 unformat_session (unformat_input_t * input, va_list * args)
238 session_t **result = va_arg (*args, session_t **);
239 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
240 ip46_address_t lcl, rmt;
245 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
246 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
250 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
251 clib_host_to_net_u16 (lcl_port),
252 clib_host_to_net_u16 (rmt_port), proto);
254 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
255 clib_host_to_net_u16 (lcl_port),
256 clib_host_to_net_u16 (rmt_port), proto);
260 session_pool_remove_peeker (s->thread_index);
267 unformat_transport_connection (unformat_input_t * input, va_list * args)
269 transport_connection_t **result = va_arg (*args, transport_connection_t **);
270 u32 suggested_proto = va_arg (*args, u32);
271 transport_connection_t *tc;
273 ip46_address_t lcl, rmt;
274 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
277 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
278 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
281 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
282 if (proto == (u8) ~ 0)
285 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
286 clib_host_to_net_u16 (lcl_port),
287 clib_host_to_net_u16 (rmt_port), proto);
289 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
290 clib_host_to_net_u16 (lcl_port),
291 clib_host_to_net_u16 (rmt_port), proto);
302 session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
304 session_main_t *smm = &session_main;
305 u32 n_closed, thread_index;
308 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
310 pool = smm->wrk[thread_index].sessions;
312 if (!pool_elts (pool))
314 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
320 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
325 if (pool_elts (pool) > 50)
327 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
328 "suppressed. For more details use filters.",
329 thread_index, pool_elts (pool));
334 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
335 thread_index ? "\n" : "",
336 "Connection", "State", "Rx-f", "Tx-f");
341 pool_foreach(s, pool, ({
342 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
347 vlib_cli_output (vm, "%U", format_session, s, verbose);
352 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
353 pool_elts (pool) - n_closed);
355 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
356 thread_index, pool_elts (pool) - n_closed, n_closed);
361 session_cli_filter_check (session_t * s, session_state_t * states,
362 transport_proto_t tp)
366 session_state_t *state;
367 vec_foreach (state, states) if (s->session_state == *state)
368 goto check_transport;
374 if (tp != TRANSPORT_N_PROTO && session_get_transport_proto (s) != tp)
381 session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
382 u32 start, u32 end, session_state_t * states,
383 transport_proto_t tp, int verbose)
385 u8 output_suppressed = 0;
386 session_worker_t *wrk;
388 u32 count = 0, max_index;
391 wrk = session_main_get_worker_if_valid (thread_index);
394 vlib_cli_output (vm, "invalid thread index %u", thread_index);
398 pool = wrk->sessions;
400 if (tp == TRANSPORT_N_PROTO && states == 0 && !verbose
401 && (start == 0 && end == ~0))
403 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
408 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
409 for (i = start; i <= clib_min (end, max_index); i++)
411 if (pool_is_free_index (pool, i))
414 s = pool_elt_at_index (pool, i);
416 if (session_cli_filter_check (s, states, tp))
421 if (count > 50 || (verbose > 1 && count > 10))
423 output_suppressed = 1;
426 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
427 vlib_cli_output (vm, "%U", format_session, s, verbose);
432 if (!output_suppressed)
433 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
434 thread_index, count);
436 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
437 " shown. Use finer grained filter.", thread_index,
442 session_cli_print_transport_protos (vlib_main_t * vm)
444 #define _(sym, str, sstr) vlib_cli_output (vm, str);
445 foreach_transport_proto
450 session_cli_print_session_states (vlib_main_t * vm)
452 #define _(sym, str) vlib_cli_output (vm, str);
453 foreach_session_state
457 static clib_error_t *
458 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
459 vlib_cli_command_t * cmd)
461 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
462 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
463 unformat_input_t _line_input, *line_input = &_line_input;
464 transport_proto_t transport_proto = TRANSPORT_N_PROTO;
465 session_state_t state = SESSION_N_STATES, *states = 0;
466 session_main_t *smm = &session_main;
467 clib_error_t *error = 0;
468 app_worker_t *app_wrk;
474 session_cli_return_if_not_enabled ();
476 if (!unformat_user (input, unformat_line_input, line_input))
478 session_cli_show_all_sessions (vm, 0);
482 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
484 if (unformat (line_input, "verbose %d", &verbose))
486 else if (unformat (line_input, "verbose"))
488 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
491 else if (unformat (line_input, "%U", unformat_session, &s))
495 else if (unformat (line_input, "thread %u index %u", &thread_index,
498 s = session_get_if_valid (session_index, thread_index);
501 vlib_cli_output (vm, "session is not allocated");
506 else if (unformat (line_input, "thread %u", &thread_index))
511 if (unformat (line_input, "state %U", unformat_session_state, &state))
513 vec_add1 (states, state);
516 else if (unformat (line_input, "proto %U index %u",
517 unformat_transport_proto, &transport_proto,
520 transport_connection_t *tc;
521 tc = transport_get_connection (transport_proto, transport_index,
525 vlib_cli_output (vm, "transport connection %u thread %u is not"
526 " allocated", transport_index, thread_index);
529 s = session_get_if_valid (tc->s_index, thread_index);
532 vlib_cli_output (vm, "session for transport connection %u "
533 "thread %u does not exist", transport_index,
539 else if (unformat (line_input, "proto %U", unformat_transport_proto,
542 else if (unformat (line_input, "range %u %u", &start, &end))
544 else if (unformat (line_input, "range %u", &start))
549 else if (unformat (line_input, "elog"))
551 else if (unformat (line_input, "protos"))
553 session_cli_print_transport_protos (vm);
556 else if (unformat (line_input, "states"))
558 session_cli_print_session_states (vm);
563 error = clib_error_return (0, "unknown input `%U'",
564 format_unformat_error, line_input);
571 u8 *str = format (0, "%U", format_session, s, 3);
572 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
574 elog_main_t *em = &vm->elog_main;
575 transport_connection_t *tc;
578 tc = session_get_transport (s);
579 track_index = transport_elog_track_index (tc);
580 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
581 * vm->clib_time.seconds_per_clock;
582 if (track_index != ~0)
583 str = format (str, " session elog:\n%U", format_elog_track, em,
586 vlib_cli_output (vm, "%v", str);
593 sst = session_type_from_proto_and_ip (transport_proto, 1);
594 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
596 pool_foreach (s, smm->wrk[0].sessions, ({
597 if (s->session_state != SESSION_STATE_LISTENING
598 || s->session_type != sst)
600 app_wrk = app_worker_get (s->app_wrk_index);
601 app_name = application_name_from_index (app_wrk->app_index);
602 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
613 error = clib_error_return (0, "invalid range start: %u end: %u",
617 session_cli_show_session_filter (vm, thread_index, start, end, states,
618 transport_proto, verbose);
622 session_cli_show_all_sessions (vm, verbose);
625 unformat_free (line_input);
631 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
633 .path = "show session",
634 .short_help = "show session [verbose [n]] [listeners <proto>] "
635 "[<session-id> [elog]] [thread <n> [index <n>] "
636 "[proto <proto>] [state <state>] [range <min> [<max>]] "
637 "[protos] [states] ",
638 .function = show_session_command_fn,
643 clear_session (session_t * s)
645 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
646 app_worker_close_notify (server_wrk, s);
650 static clib_error_t *
651 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
652 vlib_cli_command_t * cmd)
654 session_main_t *smm = &session_main;
655 u32 thread_index = 0, clear_all = 0;
656 session_worker_t *wrk;
657 u32 session_index = ~0;
660 if (!smm->is_enabled)
662 return clib_error_return (0, "session layer is not enabled");
665 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
667 if (unformat (input, "thread %d", &thread_index))
669 else if (unformat (input, "session %d", &session_index))
671 else if (unformat (input, "all"))
674 return clib_error_return (0, "unknown input `%U'",
675 format_unformat_error, input);
678 if (!clear_all && session_index == ~0)
679 return clib_error_return (0, "session <nn> required, but not set.");
681 if (session_index != ~0)
683 session = session_get_if_valid (session_index, thread_index);
685 return clib_error_return (0, "no session %d on thread %d",
686 session_index, thread_index);
687 clear_session (session);
693 vec_foreach (wrk, smm->wrk)
695 pool_foreach(session, wrk->sessions, ({
696 clear_session (session);
706 VLIB_CLI_COMMAND (clear_session_command, static) =
708 .path = "clear session",
709 .short_help = "clear session thread <thread> session <index>",
710 .function = clear_session_command_fn,
714 static clib_error_t *
715 show_session_fifo_trace_command_fn (vlib_main_t * vm,
716 unformat_input_t * input,
717 vlib_cli_command_t * cmd)
720 u8 is_rx = 0, *str = 0;
722 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
724 if (unformat (input, "%U", unformat_session, &s))
726 else if (unformat (input, "rx"))
728 else if (unformat (input, "tx"))
731 return clib_error_return (0, "unknown input `%U'",
732 format_unformat_error, input);
737 vlib_cli_output (vm, "fifo tracing not enabled");
743 vlib_cli_output (vm, "could not find session");
748 svm_fifo_dump_trace (str, s->rx_fifo) :
749 svm_fifo_dump_trace (str, s->tx_fifo);
751 vlib_cli_output (vm, "%v", str);
756 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
758 .path = "show session fifo trace",
759 .short_help = "show session fifo trace <session>",
760 .function = show_session_fifo_trace_command_fn,
764 static clib_error_t *
765 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
766 vlib_cli_command_t * cmd)
769 u8 is_rx = 0, *str = 0;
771 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
773 if (unformat (input, "%U", unformat_session, &s))
775 else if (unformat (input, "rx"))
778 return clib_error_return (0, "unknown input `%U'",
779 format_unformat_error, input);
784 vlib_cli_output (vm, "fifo tracing not enabled");
790 vlib_cli_output (vm, "could not find session");
795 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
796 svm_fifo_replay (str, s->tx_fifo, 0, 1);
798 vlib_cli_output (vm, "%v", str);
803 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
805 .path = "session replay fifo",
806 .short_help = "session replay fifo <session>",
807 .function = session_replay_fifo_command_fn,
811 static clib_error_t *
812 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
813 vlib_cli_command_t * cmd)
815 unformat_input_t _line_input, *line_input = &_line_input;
819 if (!unformat_user (input, unformat_line_input, line_input))
820 return clib_error_return (0, "expected enable | disable");
822 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
824 if (unformat (line_input, "enable"))
826 else if (unformat (line_input, "disable"))
830 error = clib_error_return (0, "unknown input `%U'",
831 format_unformat_error, line_input);
832 unformat_free (line_input);
837 unformat_free (line_input);
838 return vnet_session_enable_disable (vm, is_en);
842 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
845 .short_help = "session [enable|disable]",
846 .function = session_enable_disable_fn,
851 * fd.io coding-style-patch-verification: ON
854 * eval: (c-set-style "gnu")