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 opaque: 0x%x flags: %U\n",
137 format_session_state, ss, ss->opaque,
138 format_session_flags, ss);
141 else if (ss->session_state == SESSION_STATE_LISTENING)
143 s = format (s, "%U%v", format_transport_listen_connection,
144 tp, ss->connection_index, ss->thread_index, verbose, str);
146 s = format (s, "\n%U", format_session_fifos, ss, verbose);
148 else if (ss->session_state == SESSION_STATE_CONNECTING)
150 s = format (s, "%-40U%v", format_transport_half_open_connection,
151 tp, ss->connection_index, ss->thread_index, str);
155 clib_warning ("Session in state: %d!", ss->session_state);
163 unformat_stream_session_id (unformat_input_t * input, va_list * args)
165 u8 *proto = va_arg (*args, u8 *);
166 u32 *fib_index = va_arg (*args, u32 *);
167 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
168 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
169 u16 *lcl_port = va_arg (*args, u16 *);
170 u16 *rmt_port = va_arg (*args, u16 *);
171 u8 *is_ip4 = va_arg (*args, u8 *);
175 clib_memset (lcl, 0, sizeof (*lcl));
176 clib_memset (rmt, 0, sizeof (*rmt));
178 if (unformat (input, "tcp"))
180 *proto = TRANSPORT_PROTO_TCP;
182 else if (unformat (input, "udp"))
184 *proto = TRANSPORT_PROTO_UDP;
189 if (unformat (input, "vrf %u", &vrf))
192 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
193 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
198 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
199 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
207 fib_protocol_t fib_proto;
208 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
209 *fib_index = fib_table_find (fib_proto, vrf);
216 unformat_session_state (unformat_input_t * input, va_list * args)
218 session_state_t *state = va_arg (*args, session_state_t *);
222 #define _(sym, str) \
223 if (unformat (input, str)) \
225 *state = SESSION_STATE_ ## sym; \
229 foreach_session_state
232 vec_free (state_vec);
237 unformat_session (unformat_input_t * input, va_list * args)
239 session_t **result = va_arg (*args, session_t **);
240 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
241 ip46_address_t lcl, rmt;
246 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
247 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
251 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
252 clib_host_to_net_u16 (lcl_port),
253 clib_host_to_net_u16 (rmt_port), proto);
255 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
256 clib_host_to_net_u16 (lcl_port),
257 clib_host_to_net_u16 (rmt_port), proto);
261 session_pool_remove_peeker (s->thread_index);
268 unformat_transport_connection (unformat_input_t * input, va_list * args)
270 transport_connection_t **result = va_arg (*args, transport_connection_t **);
271 u32 suggested_proto = va_arg (*args, u32);
272 transport_connection_t *tc;
274 ip46_address_t lcl, rmt;
275 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
278 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
279 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
282 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
283 if (proto == (u8) ~ 0)
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);
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);
303 session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
305 session_main_t *smm = &session_main;
306 u32 n_closed, thread_index;
309 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
311 pool = smm->wrk[thread_index].sessions;
313 if (!pool_elts (pool))
315 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
321 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
326 if (pool_elts (pool) > 50)
328 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
329 "suppressed. For more details use filters.",
330 thread_index, pool_elts (pool));
335 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
336 thread_index ? "\n" : "",
337 "Connection", "State", "Rx-f", "Tx-f");
342 pool_foreach(s, pool, ({
343 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
348 vlib_cli_output (vm, "%U", format_session, s, verbose);
353 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
354 pool_elts (pool) - n_closed);
356 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
357 thread_index, pool_elts (pool) - n_closed, n_closed);
362 session_cli_filter_check (session_t * s, session_state_t * states,
363 transport_proto_t tp)
367 session_state_t *state;
368 vec_foreach (state, states) if (s->session_state == *state)
369 goto check_transport;
375 if (tp != TRANSPORT_PROTO_INVALID && session_get_transport_proto (s) != tp)
382 session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
383 u32 start, u32 end, session_state_t * states,
384 transport_proto_t tp, int verbose)
386 u8 output_suppressed = 0;
387 session_worker_t *wrk;
389 u32 count = 0, max_index;
392 wrk = session_main_get_worker_if_valid (thread_index);
395 vlib_cli_output (vm, "invalid thread index %u", thread_index);
399 pool = wrk->sessions;
401 if (tp == TRANSPORT_PROTO_INVALID && states == 0 && !verbose
402 && (start == 0 && end == ~0))
404 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
409 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
410 for (i = start; i <= clib_min (end, max_index); i++)
412 if (pool_is_free_index (pool, i))
415 s = pool_elt_at_index (pool, i);
417 if (session_cli_filter_check (s, states, tp))
422 if (count > 50 || (verbose > 1 && count > 10))
424 output_suppressed = 1;
427 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
428 vlib_cli_output (vm, "%U", format_session, s, verbose);
433 if (!output_suppressed)
434 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
435 thread_index, count);
437 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
438 " shown. Use finer grained filter.", thread_index,
443 session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
445 session_worker_t *wrk;
447 wrk = session_main_get_worker_if_valid (thread_index);
450 vlib_cli_output (vm, "invalid thread index %u", thread_index);
454 vlib_cli_output (vm, "Thread %d:\n", thread_index);
455 vlib_cli_output (vm, " evt elements alloc: %u",
456 pool_elts (wrk->event_elts));
457 vlib_cli_output (vm, " ctrl evt elt data alloc: %d",
458 pool_elts (wrk->ctrl_evts_data));
462 session_cli_show_events (vlib_main_t * vm, u32 thread_index)
464 session_main_t *smm = &session_main;
467 session_cli_show_events_thread (vm, thread_index);
471 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
472 session_cli_show_events_thread (vm, thread_index);
476 session_cli_print_session_states (vlib_main_t * vm)
478 #define _(sym, str) vlib_cli_output (vm, str);
479 foreach_session_state
483 static clib_error_t *
484 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
485 vlib_cli_command_t * cmd)
487 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
488 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
489 unformat_input_t _line_input, *line_input = &_line_input;
490 transport_proto_t transport_proto = TRANSPORT_PROTO_INVALID;
491 session_state_t state = SESSION_N_STATES, *states = 0;
492 session_main_t *smm = &session_main;
493 clib_error_t *error = 0;
494 app_worker_t *app_wrk;
501 session_cli_return_if_not_enabled ();
503 if (!unformat_user (input, unformat_line_input, line_input))
505 session_cli_show_all_sessions (vm, 0);
509 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
511 if (unformat (line_input, "verbose %d", &verbose))
513 else if (unformat (line_input, "verbose"))
515 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
518 else if (unformat (line_input, "%U", unformat_session, &s))
522 else if (unformat (line_input, "thread %u index %u", &thread_index,
525 s = session_get_if_valid (session_index, thread_index);
528 vlib_cli_output (vm, "session is not allocated");
533 else if (unformat (line_input, "thread %u", &thread_index))
538 if (unformat (line_input, "state %U", unformat_session_state, &state))
540 vec_add1 (states, state);
543 else if (unformat (line_input, "proto %U index %u",
544 unformat_transport_proto, &transport_proto,
547 transport_connection_t *tc;
548 tc = transport_get_connection (transport_proto, transport_index,
552 vlib_cli_output (vm, "transport connection %u thread %u is not"
553 " allocated", transport_index, thread_index);
556 s = session_get_if_valid (tc->s_index, thread_index);
559 vlib_cli_output (vm, "session for transport connection %u "
560 "thread %u does not exist", transport_index,
566 else if (unformat (line_input, "proto %U", unformat_transport_proto,
569 else if (unformat (line_input, "range %u %u", &start, &end))
571 else if (unformat (line_input, "range %u", &start))
576 else if (unformat (line_input, "elog"))
578 else if (unformat (line_input, "protos"))
580 vlib_cli_output (vm, "%U", format_transport_protos);
583 else if (unformat (line_input, "states"))
585 session_cli_print_session_states (vm);
588 else if (unformat (line_input, "events"))
592 error = clib_error_return (0, "unknown input `%U'",
593 format_unformat_error, line_input);
600 u8 *str = format (0, "%U", format_session, s, 3);
601 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
603 elog_main_t *em = &vm->elog_main;
604 transport_connection_t *tc;
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,
615 vlib_cli_output (vm, "%v", str);
622 sst = session_type_from_proto_and_ip (transport_proto, 1);
623 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
625 pool_foreach (s, smm->wrk[0].sessions, ({
626 if (s->session_state != SESSION_STATE_LISTENING
627 || s->session_type != sst)
629 app_wrk = app_worker_get (s->app_wrk_index);
630 app_name = application_name_from_index (app_wrk->app_index);
631 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
640 session_cli_show_events (vm, thread_index);
648 error = clib_error_return (0, "invalid range start: %u end: %u",
652 session_cli_show_session_filter (vm, thread_index, start, end, states,
653 transport_proto, verbose);
657 session_cli_show_all_sessions (vm, verbose);
660 unformat_free (line_input);
666 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
668 .path = "show session",
669 .short_help = "show session [verbose [n]] [listeners <proto>] "
670 "[<session-id> [elog]] [thread <n> [index <n>] "
671 "[proto <proto>] [state <state>] [range <min> [<max>]] "
672 "[protos] [states] ",
673 .function = show_session_command_fn,
678 clear_session (session_t * s)
680 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
681 app_worker_close_notify (server_wrk, s);
685 static clib_error_t *
686 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
687 vlib_cli_command_t * cmd)
689 session_main_t *smm = &session_main;
690 u32 thread_index = 0, clear_all = 0;
691 session_worker_t *wrk;
692 u32 session_index = ~0;
695 if (!smm->is_enabled)
697 return clib_error_return (0, "session layer is not enabled");
700 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
702 if (unformat (input, "thread %d", &thread_index))
704 else if (unformat (input, "session %d", &session_index))
706 else if (unformat (input, "all"))
709 return clib_error_return (0, "unknown input `%U'",
710 format_unformat_error, input);
713 if (!clear_all && session_index == ~0)
714 return clib_error_return (0, "session <nn> required, but not set.");
716 if (session_index != ~0)
718 session = session_get_if_valid (session_index, thread_index);
720 return clib_error_return (0, "no session %d on thread %d",
721 session_index, thread_index);
722 clear_session (session);
728 vec_foreach (wrk, smm->wrk)
730 pool_foreach(session, wrk->sessions, ({
731 clear_session (session);
741 VLIB_CLI_COMMAND (clear_session_command, static) =
743 .path = "clear session",
744 .short_help = "clear session thread <thread> session <index>",
745 .function = clear_session_command_fn,
749 static clib_error_t *
750 show_session_fifo_trace_command_fn (vlib_main_t * vm,
751 unformat_input_t * input,
752 vlib_cli_command_t * cmd)
755 u8 is_rx = 0, *str = 0;
757 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
759 if (unformat (input, "%U", unformat_session, &s))
761 else if (unformat (input, "rx"))
763 else if (unformat (input, "tx"))
766 return clib_error_return (0, "unknown input `%U'",
767 format_unformat_error, input);
772 vlib_cli_output (vm, "fifo tracing not enabled");
778 vlib_cli_output (vm, "could not find session");
783 svm_fifo_dump_trace (str, s->rx_fifo) :
784 svm_fifo_dump_trace (str, s->tx_fifo);
786 vlib_cli_output (vm, "%v", str);
791 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
793 .path = "show session fifo trace",
794 .short_help = "show session fifo trace <session>",
795 .function = show_session_fifo_trace_command_fn,
799 static clib_error_t *
800 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
801 vlib_cli_command_t * cmd)
804 u8 is_rx = 0, *str = 0;
806 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
808 if (unformat (input, "%U", unformat_session, &s))
810 else if (unformat (input, "rx"))
813 return clib_error_return (0, "unknown input `%U'",
814 format_unformat_error, input);
819 vlib_cli_output (vm, "fifo tracing not enabled");
825 vlib_cli_output (vm, "could not find session");
830 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
831 svm_fifo_replay (str, s->tx_fifo, 0, 1);
833 vlib_cli_output (vm, "%v", str);
838 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
840 .path = "session replay fifo",
841 .short_help = "session replay fifo <session>",
842 .function = session_replay_fifo_command_fn,
846 static clib_error_t *
847 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
848 vlib_cli_command_t * cmd)
850 unformat_input_t _line_input, *line_input = &_line_input;
854 if (!unformat_user (input, unformat_line_input, line_input))
855 return clib_error_return (0, "expected enable | disable");
857 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
859 if (unformat (line_input, "enable"))
861 else if (unformat (line_input, "disable"))
865 error = clib_error_return (0, "unknown input `%U'",
866 format_unformat_error, line_input);
867 unformat_free (line_input);
872 unformat_free (line_input);
873 return vnet_session_enable_disable (vm, is_en);
877 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
880 .short_help = "session [enable|disable]",
881 .function = session_enable_disable_fn,
886 * fd.io coding-style-patch-verification: ON
889 * eval: (c-set-style "gnu")