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");
47 * Format stream session as per the following format
50 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
55 format_session (u8 * s, va_list * args)
57 session_t *ss = va_arg (*args, session_t *);
58 int verbose = va_arg (*args, int);
59 u32 tp = session_get_transport_proto (ss);
62 if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
64 s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
70 u8 post_accept = ss->session_state >= SESSION_STATE_ACCEPTING;
72 || session_transport_service_type (ss) == TRANSPORT_SERVICE_CL;
75 rxf = hasf ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
76 txf = hasf ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
77 str = format (0, "%-10u%-10u", rxf, txf);
80 if (ss->session_state >= SESSION_STATE_ACCEPTING
81 || ss->session_state == SESSION_STATE_CREATED)
83 s = format (s, "%U", format_transport_connection, tp,
84 ss->connection_index, ss->thread_index, verbose);
86 s = format (s, "%v", str);
88 s = format (s, "%U", format_session_fifos, ss, verbose);
90 else if (ss->session_state == SESSION_STATE_LISTENING)
92 s = format (s, "%U%v", format_transport_listen_connection,
93 tp, ss->connection_index, ss->thread_index, verbose, str);
95 s = format (s, "\n%U", format_session_fifos, ss, verbose);
97 else if (ss->session_state == SESSION_STATE_CONNECTING)
99 s = format (s, "%-40U%v", format_transport_half_open_connection,
100 tp, ss->connection_index, ss->thread_index, str);
104 clib_warning ("Session in state: %d!", ss->session_state);
112 unformat_stream_session_id (unformat_input_t * input, va_list * args)
114 u8 *proto = va_arg (*args, u8 *);
115 u32 *fib_index = va_arg (*args, u32 *);
116 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
117 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
118 u16 *lcl_port = va_arg (*args, u16 *);
119 u16 *rmt_port = va_arg (*args, u16 *);
120 u8 *is_ip4 = va_arg (*args, u8 *);
124 clib_memset (lcl, 0, sizeof (*lcl));
125 clib_memset (rmt, 0, sizeof (*rmt));
127 if (unformat (input, "tcp"))
129 *proto = TRANSPORT_PROTO_TCP;
131 else if (unformat (input, "udp"))
133 *proto = TRANSPORT_PROTO_UDP;
138 if (unformat (input, "vrf %u", &vrf))
141 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
142 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
147 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
148 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
156 fib_protocol_t fib_proto;
157 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
158 *fib_index = fib_table_find (fib_proto, vrf);
165 unformat_session_state (unformat_input_t * input, va_list * args)
167 session_state_t *state = va_arg (*args, session_state_t *);
171 #define _(sym, str) \
172 if (unformat (input, str)) \
174 *state = SESSION_STATE_ ## sym; \
178 foreach_session_state
181 vec_free (state_vec);
186 unformat_session (unformat_input_t * input, va_list * args)
188 session_t **result = va_arg (*args, session_t **);
189 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
190 ip46_address_t lcl, rmt;
195 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
196 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
200 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
201 clib_host_to_net_u16 (lcl_port),
202 clib_host_to_net_u16 (rmt_port), proto);
204 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
205 clib_host_to_net_u16 (lcl_port),
206 clib_host_to_net_u16 (rmt_port), proto);
210 session_pool_remove_peeker (s->thread_index);
217 unformat_transport_connection (unformat_input_t * input, va_list * args)
219 transport_connection_t **result = va_arg (*args, transport_connection_t **);
220 u32 suggested_proto = va_arg (*args, u32);
221 transport_connection_t *tc;
223 ip46_address_t lcl, rmt;
224 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
227 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
228 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
231 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
232 if (proto == (u8) ~ 0)
235 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
236 clib_host_to_net_u16 (lcl_port),
237 clib_host_to_net_u16 (rmt_port), proto);
239 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
240 clib_host_to_net_u16 (lcl_port),
241 clib_host_to_net_u16 (rmt_port), proto);
252 session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
254 session_main_t *smm = &session_main;
255 u32 n_closed = 0, thread_index;
258 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
260 pool = smm->wrk[thread_index].sessions;
262 if (!pool_elts (pool))
264 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
270 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
275 if (pool_elts (pool) > 50)
277 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
278 "suppressed. For more details use filters.",
279 thread_index, pool_elts (pool));
284 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
285 thread_index ? "\n" : "",
286 "Connection", "State", "Rx-f", "Tx-f");
289 pool_foreach(s, pool, ({
290 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
295 vlib_cli_output (vm, "%U", format_session, s, verbose);
300 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
301 pool_elts (pool) - n_closed);
303 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
304 thread_index, pool_elts (pool) - n_closed, n_closed);
309 session_cli_filter_check (session_t * s, session_state_t * states,
310 transport_proto_t tp)
314 session_state_t *state;
315 vec_foreach (state, states) if (s->session_state == *state)
316 goto check_transport;
322 if (tp != TRANSPORT_N_PROTO && session_get_transport_proto (s) != tp)
329 session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
330 u32 start, u32 end, session_state_t * states,
331 transport_proto_t tp, int verbose)
333 u8 output_suppressed = 0;
334 session_worker_t *wrk;
336 u32 count = 0, max_index;
339 wrk = session_main_get_worker_if_valid (thread_index);
342 vlib_cli_output (vm, "invalid thread index %u", thread_index);
346 pool = wrk->sessions;
348 if (tp == TRANSPORT_N_PROTO && states == 0 && !verbose
349 && (start == 0 && end == ~0))
351 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
356 max_index = pool_elts (pool) ? pool_elts (pool) - 1 : 0;
357 for (i = start; i <= clib_min (end, max_index); i++)
359 if (pool_is_free_index (pool, i))
362 s = pool_elt_at_index (pool, i);
364 if (session_cli_filter_check (s, states, tp))
369 if (count > 50 || (verbose > 1 && count > 10))
371 output_suppressed = 1;
374 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
375 vlib_cli_output (vm, "%U", format_session, s, verbose);
380 if (!output_suppressed)
381 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
382 thread_index, count);
384 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
385 " shown. Use finer grained filter.", thread_index,
390 session_cli_print_transport_protos (vlib_main_t * vm)
392 #define _(sym, str, sstr) vlib_cli_output (vm, str);
393 foreach_transport_proto
398 session_cli_print_session_states (vlib_main_t * vm)
400 #define _(sym, str) vlib_cli_output (vm, str);
401 foreach_session_state
405 static clib_error_t *
406 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
407 vlib_cli_command_t * cmd)
409 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
410 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
411 unformat_input_t _line_input, *line_input = &_line_input;
412 transport_proto_t transport_proto = TRANSPORT_N_PROTO;
413 session_state_t state = SESSION_N_STATES, *states = 0;
414 session_main_t *smm = &session_main;
415 clib_error_t *error = 0;
416 app_worker_t *app_wrk;
422 session_cli_return_if_not_enabled ();
424 if (!unformat_user (input, unformat_line_input, line_input))
426 session_cli_show_all_sessions (vm, 0);
430 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
432 if (unformat (line_input, "verbose %d", &verbose))
434 else if (unformat (line_input, "verbose"))
436 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
439 else if (unformat (line_input, "%U", unformat_session, &s))
443 else if (unformat (line_input, "thread %u index %u", &thread_index,
446 s = session_get_if_valid (session_index, thread_index);
449 vlib_cli_output (vm, "session is not allocated");
454 else if (unformat (line_input, "thread %u", &thread_index))
459 if (unformat (line_input, "state %U", unformat_session_state, &state))
461 vec_add1 (states, state);
464 else if (unformat (line_input, "proto %U index %u",
465 unformat_transport_proto, &transport_proto,
468 transport_connection_t *tc;
469 tc = transport_get_connection (transport_proto, transport_index,
473 vlib_cli_output (vm, "transport connection %u thread %u is not"
474 " allocated", transport_index, thread_index);
477 s = session_get_if_valid (tc->s_index, thread_index);
480 vlib_cli_output (vm, "session for transport connection %u "
481 "thread %u does not exist", transport_index,
487 else if (unformat (line_input, "proto %U", unformat_transport_proto,
490 else if (unformat (line_input, "range %u %u", &start, &end))
492 else if (unformat (line_input, "range %u", &start))
497 else if (unformat (line_input, "elog"))
499 else if (unformat (line_input, "protos"))
501 session_cli_print_transport_protos (vm);
504 else if (unformat (line_input, "states"))
506 session_cli_print_session_states (vm);
511 error = clib_error_return (0, "unknown input `%U'",
512 format_unformat_error, line_input);
519 u8 *str = format (0, "%U", format_session, s, 3);
520 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
522 elog_main_t *em = &vm->elog_main;
523 transport_connection_t *tc;
526 tc = session_get_transport (s);
527 track_index = transport_elog_track_index (tc);
528 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
529 * vm->clib_time.seconds_per_clock;
530 if (track_index != ~0)
531 str = format (str, " session elog:\n%U", format_elog_track, em,
534 vlib_cli_output (vm, "%v", str);
541 sst = session_type_from_proto_and_ip (transport_proto, 1);
542 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
544 pool_foreach (s, smm->wrk[0].sessions, ({
545 if (s->session_state != SESSION_STATE_LISTENING
546 || s->session_type != sst)
548 app_wrk = app_worker_get (s->app_wrk_index);
549 app_name = application_name_from_index (app_wrk->app_index);
550 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
561 error = clib_error_return (0, "invalid range start: %u end: %u",
565 session_cli_show_session_filter (vm, thread_index, start, end, states,
566 transport_proto, verbose);
570 session_cli_show_all_sessions (vm, verbose);
573 unformat_free (line_input);
579 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
581 .path = "show session",
582 .short_help = "show session [verbose [n]] [listeners <proto>] "
583 "[<session-id> [elog]] [thread <n> [index <n>] "
584 "[proto <proto>] [state <state>] [range <min> [<max>]] "
585 "[protos] [states] ",
586 .function = show_session_command_fn,
591 clear_session (session_t * s)
593 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
594 app_worker_close_notify (server_wrk, s);
598 static clib_error_t *
599 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
600 vlib_cli_command_t * cmd)
602 session_main_t *smm = &session_main;
603 u32 thread_index = 0, clear_all = 0;
604 session_worker_t *wrk;
605 u32 session_index = ~0;
608 if (!smm->is_enabled)
610 return clib_error_return (0, "session layer is not enabled");
613 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
615 if (unformat (input, "thread %d", &thread_index))
617 else if (unformat (input, "session %d", &session_index))
619 else if (unformat (input, "all"))
622 return clib_error_return (0, "unknown input `%U'",
623 format_unformat_error, input);
626 if (!clear_all && session_index == ~0)
627 return clib_error_return (0, "session <nn> required, but not set.");
629 if (session_index != ~0)
631 session = session_get_if_valid (session_index, thread_index);
633 return clib_error_return (0, "no session %d on thread %d",
634 session_index, thread_index);
635 clear_session (session);
641 vec_foreach (wrk, smm->wrk)
643 pool_foreach(session, wrk->sessions, ({
644 clear_session (session);
654 VLIB_CLI_COMMAND (clear_session_command, static) =
656 .path = "clear session",
657 .short_help = "clear session thread <thread> session <index>",
658 .function = clear_session_command_fn,
662 static clib_error_t *
663 show_session_fifo_trace_command_fn (vlib_main_t * vm,
664 unformat_input_t * input,
665 vlib_cli_command_t * cmd)
668 u8 is_rx = 0, *str = 0;
670 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
672 if (unformat (input, "%U", unformat_session, &s))
674 else if (unformat (input, "rx"))
676 else if (unformat (input, "tx"))
679 return clib_error_return (0, "unknown input `%U'",
680 format_unformat_error, input);
685 vlib_cli_output (vm, "fifo tracing not enabled");
691 vlib_cli_output (vm, "could not find session");
696 svm_fifo_dump_trace (str, s->rx_fifo) :
697 svm_fifo_dump_trace (str, s->tx_fifo);
699 vlib_cli_output (vm, "%v", str);
704 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
706 .path = "show session fifo trace",
707 .short_help = "show session fifo trace <session>",
708 .function = show_session_fifo_trace_command_fn,
712 static clib_error_t *
713 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
714 vlib_cli_command_t * cmd)
717 u8 is_rx = 0, *str = 0;
719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
721 if (unformat (input, "%U", unformat_session, &s))
723 else if (unformat (input, "rx"))
726 return clib_error_return (0, "unknown input `%U'",
727 format_unformat_error, input);
732 vlib_cli_output (vm, "fifo tracing not enabled");
738 vlib_cli_output (vm, "could not find session");
743 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
744 svm_fifo_replay (str, s->tx_fifo, 0, 1);
746 vlib_cli_output (vm, "%v", str);
751 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
753 .path = "session replay fifo",
754 .short_help = "session replay fifo <session>",
755 .function = session_replay_fifo_command_fn,
759 static clib_error_t *
760 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
761 vlib_cli_command_t * cmd)
763 unformat_input_t _line_input, *line_input = &_line_input;
767 if (!unformat_user (input, unformat_line_input, line_input))
768 return clib_error_return (0, "expected enable | disable");
770 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
772 if (unformat (line_input, "enable"))
774 else if (unformat (line_input, "disable"))
778 error = clib_error_return (0, "unknown input `%U'",
779 format_unformat_error, line_input);
780 unformat_free (line_input);
785 unformat_free (line_input);
786 return vnet_session_enable_disable (vm, is_en);
790 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
793 .short_help = "session [enable|disable]",
794 .function = session_enable_disable_fn,
799 * fd.io coding-style-patch-verification: ON
802 * eval: (c-set-style "gnu")