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);
90 unformat_stream_session_id (unformat_input_t * input, va_list * args)
92 u8 *proto = va_arg (*args, u8 *);
93 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
94 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
95 u16 *lcl_port = va_arg (*args, u16 *);
96 u16 *rmt_port = va_arg (*args, u16 *);
97 u8 *is_ip4 = va_arg (*args, u8 *);
100 memset (lcl, 0, sizeof (*lcl));
101 memset (rmt, 0, sizeof (*rmt));
103 if (unformat (input, "tcp"))
105 *proto = TRANSPORT_PROTO_TCP;
107 if (unformat (input, "udp"))
109 *proto = TRANSPORT_PROTO_UDP;
111 else if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
112 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
117 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
118 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
133 unformat_stream_session (unformat_input_t * input, va_list * args)
135 stream_session_t **result = va_arg (*args, stream_session_t **);
138 ip46_address_t lcl, rmt;
139 u32 lcl_port = 0, rmt_port = 0;
140 u8 is_ip4 = 0, s_type = ~0, id_is_set = 0;
142 if (unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
143 &lcl_port, &rmt_port, &is_ip4))
155 s_type = session_type_from_proto_and_ip (proto, is_ip4);
157 s = stream_session_lookup4 (&lcl.ip4, &rmt.ip4,
158 clib_host_to_net_u16 (lcl_port),
159 clib_host_to_net_u16 (rmt_port), s_type);
161 s = stream_session_lookup6 (&lcl.ip6, &rmt.ip6,
162 clib_host_to_net_u16 (lcl_port),
163 clib_host_to_net_u16 (rmt_port), s_type);
173 unformat_transport_connection (unformat_input_t * input, va_list * args)
175 transport_connection_t **result = va_arg (*args, transport_connection_t **);
176 u32 suggested_proto = va_arg (*args, u32);
177 transport_connection_t *tc;
179 ip46_address_t lcl, rmt;
180 u32 lcl_port = 0, rmt_port = 0;
181 u8 is_ip4 = 0, s_type = ~0, id_is_set = 0;
183 if (unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
184 &lcl_port, &rmt_port, &is_ip4))
196 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
197 if (proto == (u8) ~ 0)
199 s_type = session_type_from_proto_and_ip (proto, is_ip4);
201 tc = stream_session_lookup_transport4 (&lcl.ip4, &rmt.ip4,
202 clib_host_to_net_u16 (lcl_port),
203 clib_host_to_net_u16 (rmt_port),
206 tc = stream_session_lookup_transport6 (&lcl.ip6, &rmt.ip6,
207 clib_host_to_net_u16 (lcl_port),
208 clib_host_to_net_u16 (rmt_port),
219 static clib_error_t *
220 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
221 vlib_cli_command_t * cmd)
223 session_manager_main_t *smm = &session_manager_main;
225 stream_session_t *pool;
227 u8 *str = 0, one_session = 0;
229 if (!smm->is_enabled)
231 return clib_error_return (0, "session layer is not enabled");
234 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
236 if (unformat (input, "verbose %d", &verbose))
238 else if (unformat (input, "verbose"))
240 else if (unformat (input, "%U", unformat_stream_session, &s))
245 return clib_error_return (0, "unknown input `%U'",
246 format_unformat_error, input);
251 vlib_cli_output (vm, "%U", format_stream_session, s, 2);
255 for (i = 0; i < vec_len (smm->sessions); i++)
258 pool = smm->sessions[i];
262 if (pool_elts (pool))
265 vlib_cli_output (vm, "Thread %d: %d active sessions",
266 i, pool_elts (pool));
269 if (once_per_pool && verbose == 1)
272 format (str, "%-50s%-15s%-10s%-10s%-10s", "Connection",
273 "State", "Rx-f", "Tx-f", "S-idx");
274 vlib_cli_output (vm, "%v", str);
275 vec_reset_length (str);
280 pool_foreach (s, pool,
282 vec_reset_length (str);
283 str = format (str, "%U", format_stream_session, s, verbose);
284 vlib_cli_output (vm, "%v", str);
290 vlib_cli_output (vm, "Thread %d: no active sessions", i);
291 vec_reset_length (str);
299 VLIB_CLI_COMMAND (show_session_command, static) =
301 .path = "show session",
302 .short_help = "show session [verbose]",
303 .function = show_session_command_fn,
308 clear_session (stream_session_t * s)
310 application_t *server = application_get (s->app_index);
311 server->cb_fns.session_disconnect_callback (s);
315 static clib_error_t *
316 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
317 vlib_cli_command_t * cmd)
319 session_manager_main_t *smm = &session_manager_main;
320 u32 thread_index = 0, clear_all = 0;
321 u32 session_index = ~0;
322 stream_session_t **pool, *session;
324 if (!smm->is_enabled)
326 return clib_error_return (0, "session layer is not enabled");
329 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
331 if (unformat (input, "thread %d", &thread_index))
333 else if (unformat (input, "session %d", &session_index))
335 else if (unformat (input, "all"))
338 return clib_error_return (0, "unknown input `%U'",
339 format_unformat_error, input);
342 if (!clear_all && session_index == ~0)
343 return clib_error_return (0, "session <nn> required, but not set.");
345 if (session_index != ~0)
347 session = stream_session_get_if_valid (session_index, thread_index);
349 return clib_error_return (0, "no session %d on thread %d",
350 session_index, thread_index);
351 clear_session (session);
357 vec_foreach (pool, smm->sessions)
359 pool_foreach(session, *pool, ({
360 clear_session (session);
370 VLIB_CLI_COMMAND (clear_session_command, static) =
372 .path = "clear session",
373 .short_help = "clear session thread <thread> session <index>",
374 .function = clear_session_command_fn,
378 static clib_error_t *
379 show_session_fifo_trace_command_fn (vlib_main_t * vm,
380 unformat_input_t * input,
381 vlib_cli_command_t * cmd)
383 stream_session_t *s = 0;
384 u8 is_rx = 0, *str = 0;
386 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
388 if (unformat (input, "%U", unformat_stream_session, &s))
390 else if (unformat (input, "rx"))
392 else if (unformat (input, "tx"))
395 return clib_error_return (0, "unknown input `%U'",
396 format_unformat_error, input);
401 vlib_cli_output (vm, "fifo tracing not enabled");
407 vlib_cli_output (vm, "could not find session");
412 svm_fifo_dump_trace (str, s->server_rx_fifo) :
413 svm_fifo_dump_trace (str, s->server_tx_fifo);
415 vlib_cli_output (vm, "%v", str);
420 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
422 .path = "show session fifo trace",
423 .short_help = "show session fifo trace <session>",
424 .function = show_session_fifo_trace_command_fn,
428 static clib_error_t *
429 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
430 vlib_cli_command_t * cmd)
432 stream_session_t *s = 0;
433 u8 is_rx = 0, *str = 0;
435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
437 if (unformat (input, "%U", unformat_stream_session, &s))
439 else if (unformat (input, "rx"))
442 return clib_error_return (0, "unknown input `%U'",
443 format_unformat_error, input);
448 vlib_cli_output (vm, "fifo tracing not enabled");
454 vlib_cli_output (vm, "could not find session");
459 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
460 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
462 vlib_cli_output (vm, "%v", str);
467 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
469 .path = "session replay fifo",
470 .short_help = "session replay fifo <session>",
471 .function = session_replay_fifo_command_fn,
475 static clib_error_t *
476 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
477 vlib_cli_command_t * cmd)
481 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
483 if (unformat (input, "enable"))
485 else if (unformat (input, "disable"))
488 return clib_error_return (0, "unknown input `%U'",
489 format_unformat_error, input);
492 return vnet_session_enable_disable (vm, is_en);
496 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
499 .short_help = "session [enable|disable]",
500 .function = session_enable_disable_fn,
505 * fd.io coding-style-patch-verification: ON
508 * eval: (c-set-style "gnu")