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_fifos (u8 * s, va_list * args)
21 stream_session_t *ss = va_arg (*args, stream_session_t *);
22 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
23 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
28 * Format stream session as per the following format
31 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
36 format_stream_session (u8 * s, va_list * args)
38 stream_session_t *ss = va_arg (*args, stream_session_t *);
39 int verbose = va_arg (*args, int);
40 transport_proto_vft_t *tp_vft;
42 tp_vft = session_get_transport_vft (ss->session_type);
45 str = format (0, "%-10u%-10u%-10lld",
46 svm_fifo_max_dequeue (ss->server_rx_fifo),
47 svm_fifo_max_enqueue (ss->server_tx_fifo),
48 stream_session_get_index (ss));
50 if (ss->session_state == SESSION_STATE_READY
51 || ss->session_state == SESSION_STATE_ACCEPTING)
53 s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
54 ss->thread_index, verbose);
56 s = format (s, "%v", str);
58 s = format (s, "%U", format_stream_session_fifos, ss);
60 else if (ss->session_state == SESSION_STATE_LISTENING)
62 s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
65 else if (ss->session_state == SESSION_STATE_CONNECTING)
67 s = format (s, "%-40U%v", tp_vft->format_half_open,
68 ss->connection_index, str);
70 else if (ss->session_state == SESSION_STATE_CLOSED)
73 format (s, "[CL] %U", tp_vft->format_connection, ss->connection_index,
74 ss->thread_index, verbose);
76 s = format (s, "%v", str);
78 s = format (s, "%U", format_stream_session_fifos, ss);
82 clib_warning ("Session in state: %d!", ss->session_state);
91 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
92 vlib_cli_command_t * cmd)
94 session_manager_main_t *smm = &session_manager_main;
96 stream_session_t *pool;
98 u8 *str = 0, one_session = 0, proto_set = 0, proto = 0;
99 u8 is_ip4 = 0, s_type = 0;
100 ip4_address_t lcl_ip4, rmt_ip4;
101 u32 lcl_port = 0, rmt_port = 0;
103 memset (&lcl_ip4, 0, sizeof (lcl_ip4));
104 memset (&rmt_ip4, 0, sizeof (rmt_ip4));
106 if (!smm->is_enabled)
108 return clib_error_return (0, "session layer is not enabled");
111 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
113 if (unformat (input, "verbose %d", &verbose))
115 else if (unformat (input, "verbose"))
117 else if (unformat (input, "tcp"))
120 proto = TRANSPORT_PROTO_TCP;
122 else if (unformat (input, "%U:%d->%U:%d",
123 unformat_ip4_address, &lcl_ip4, &lcl_port,
124 unformat_ip4_address, &rmt_ip4, &rmt_port))
138 vlib_cli_output (vm, "proto not set");
139 return clib_error_return (0, "proto not set");
142 s_type = session_type_from_proto_and_ip (proto, is_ip4);
143 s = stream_session_lookup4 (&lcl_ip4, &rmt_ip4,
144 clib_host_to_net_u16 (lcl_port),
145 clib_host_to_net_u16 (rmt_port), s_type);
147 vlib_cli_output (vm, "%U", format_stream_session, s, 2);
149 vlib_cli_output (vm, "session does not exist");
154 for (i = 0; i < vec_len (smm->sessions); i++)
157 pool = smm->sessions[i];
161 if (pool_elts (pool))
164 vlib_cli_output (vm, "Thread %d: %d active sessions",
165 i, pool_elts (pool));
168 if (once_per_pool && verbose == 1)
171 format (str, "%-50s%-15s%-10s%-10s%-10s", "Connection",
172 "State", "Rx-f", "Tx-f", "S-idx");
173 vlib_cli_output (vm, "%v", str);
174 vec_reset_length (str);
179 pool_foreach (s, pool,
181 vec_reset_length (str);
182 str = format (str, "%U", format_stream_session, s, verbose);
183 vlib_cli_output (vm, "%v", str);
189 vlib_cli_output (vm, "Thread %d: no active sessions", i);
190 vec_reset_length (str);
198 VLIB_CLI_COMMAND (show_session_command, static) =
200 .path = "show session",
201 .short_help = "show session [verbose]",
202 .function = show_session_command_fn,
207 clear_session (stream_session_t * s)
209 application_t *server = application_get (s->app_index);
210 server->cb_fns.session_disconnect_callback (s);
214 static clib_error_t *
215 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
216 vlib_cli_command_t * cmd)
218 session_manager_main_t *smm = &session_manager_main;
219 u32 thread_index = 0, clear_all = 0;
220 u32 session_index = ~0;
221 stream_session_t **pool, *session;
223 if (!smm->is_enabled)
225 return clib_error_return (0, "session layer is not enabled");
228 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
230 if (unformat (input, "thread %d", &thread_index))
232 else if (unformat (input, "session %d", &session_index))
234 else if (unformat (input, "all"))
237 return clib_error_return (0, "unknown input `%U'",
238 format_unformat_error, input);
241 if (!clear_all && session_index == ~0)
242 return clib_error_return (0, "session <nn> required, but not set.");
244 if (session_index != ~0)
246 session = stream_session_get_if_valid (session_index, thread_index);
248 return clib_error_return (0, "no session %d on thread %d",
249 session_index, thread_index);
250 clear_session (session);
256 vec_foreach (pool, smm->sessions)
258 pool_foreach(session, *pool, ({
259 clear_session (session);
269 VLIB_CLI_COMMAND (clear_session_command, static) =
271 .path = "clear session",
272 .short_help = "clear session thread <thread> session <index>",
273 .function = clear_session_command_fn,
277 static clib_error_t *
278 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
279 vlib_cli_command_t * cmd)
283 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
285 if (unformat (input, "enable"))
287 else if (unformat (input, "disable"))
290 return clib_error_return (0, "unknown input `%U'",
291 format_unformat_error, input);
294 return vnet_session_enable_disable (vm, is_en);
298 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
301 .short_help = "session [enable|disable]",
302 .function = session_enable_disable_fn,
307 * fd.io coding-style-patch-verification: ON
310 * eval: (c-set-style "gnu")