2 * Copyright (c) 2017 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 stream session as per the following format
22 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
27 format_stream_session (u8 * s, va_list * args)
29 stream_session_t *ss = va_arg (*args, stream_session_t *);
30 int verbose = va_arg (*args, int);
31 transport_proto_vft_t *tp_vft;
34 tp_vft = session_get_transport_vft (ss->session_type);
37 str = format (0, "%-20llp%-20llp%-15lld", ss->server_rx_fifo,
38 ss->server_tx_fifo, stream_session_get_index (ss));
40 if (ss->session_state == SESSION_STATE_READY)
42 s = format (s, "%-40U%v", tp_vft->format_connection,
43 ss->connection_index, ss->thread_index, str);
45 else if (ss->session_state == SESSION_STATE_LISTENING)
47 s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
50 else if (ss->session_state == SESSION_STATE_READY)
53 format (s, "%-40U%v", tp_vft->format_half_open, ss->connection_index,
56 else if (ss->session_state == SESSION_STATE_CLOSED)
58 s = format (s, "[CL] %-40U%v", tp_vft->format_connection,
59 ss->connection_index, ss->thread_index, str);
63 clib_warning ("Session in state: %d!", ss->session_state);
72 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
73 vlib_cli_command_t * cmd)
75 session_manager_main_t *smm = &session_manager_main;
77 stream_session_t *pool;
83 clib_error_return (0, "session layer is not enabled");
86 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
88 if (unformat (input, "verbose"))
94 for (i = 0; i < vec_len (smm->sessions); i++)
97 pool = smm->sessions[i];
101 if (pool_elts (pool))
104 vlib_cli_output (vm, "Thread %d: %d active sessions",
105 i, pool_elts (pool));
110 str = format (str, "%-50s%-20s%-20s%-15s",
111 "Connection", "Rx fifo", "Tx fifo",
113 vlib_cli_output (vm, "%v", str);
114 vec_reset_length (str);
119 pool_foreach (s, pool,
121 vlib_cli_output (vm, "%U", format_stream_session, s, verbose);
127 vlib_cli_output (vm, "Thread %d: no active sessions", i);
135 VLIB_CLI_COMMAND (show_session_command, static) =
137 .path = "show session",
138 .short_help = "show session [verbose]",
139 .function = show_session_command_fn,
143 static clib_error_t *
144 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
145 vlib_cli_command_t * cmd)
147 session_manager_main_t *smm = &session_manager_main;
148 u32 thread_index = 0;
149 u32 session_index = ~0;
150 stream_session_t *pool, *session;
151 application_t *server;
153 if (!smm->is_enabled)
155 clib_error_return (0, "session layer is not enabled");
158 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
160 if (unformat (input, "thread %d", &thread_index))
162 else if (unformat (input, "session %d", &session_index))
165 return clib_error_return (0, "unknown input `%U'",
166 format_unformat_error, input);
169 if (session_index == ~0)
170 return clib_error_return (0, "session <nn> required, but not set.");
172 if (thread_index > vec_len (smm->sessions))
173 return clib_error_return (0, "thread %d out of range [0-%d]",
174 thread_index, vec_len (smm->sessions));
176 pool = smm->sessions[thread_index];
178 if (pool_is_free_index (pool, session_index))
179 return clib_error_return (0, "session %d not active", session_index);
181 session = pool_elt_at_index (pool, session_index);
182 server = application_get (session->app_index);
184 /* Disconnect both app and transport */
185 server->cb_fns.session_disconnect_callback (session);
191 VLIB_CLI_COMMAND (clear_session_command, static) =
193 .path = "clear session",
194 .short_help = "clear session thread <thread> session <index>",
195 .function = clear_session_command_fn,
199 static clib_error_t *
200 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
201 vlib_cli_command_t * cmd)
205 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
207 if (unformat (input, "enable"))
209 else if (unformat (input, "disable"))
212 return clib_error_return (0, "unknown input `%U'",
213 format_unformat_error, input);
216 return vnet_session_enable_disable (vm, is_en);
220 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
223 .short_help = "session [enable|disable]",
224 .function = session_enable_disable_fn,
229 * fd.io coding-style-patch-verification: ON
232 * eval: (c-set-style "gnu")