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 int verbose = va_arg (*args, int);
23 session_fifo_event_t _e, *e = &_e;
26 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
27 if (verbose > 2 && ss->server_rx_fifo->has_event)
29 found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
30 s = format (s, " session node event: %s\n",
31 found ? "found" : "not found");
33 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
34 if (verbose > 2 && ss->server_tx_fifo->has_event)
36 found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
37 s = format (s, " session node event: %s\n",
38 found ? "found" : "not found");
44 * Format stream session as per the following format
47 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
52 format_stream_session (u8 * s, va_list * args)
54 stream_session_t *ss = va_arg (*args, stream_session_t *);
55 int verbose = va_arg (*args, int);
56 transport_proto_vft_t *tp_vft;
58 tp_vft = session_get_transport_vft (ss->session_type);
60 if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
61 str = format (0, "%-10u%-10u%-10lld",
62 svm_fifo_max_dequeue (ss->server_rx_fifo),
63 svm_fifo_max_enqueue (ss->server_tx_fifo),
64 stream_session_get_index (ss));
66 if (ss->session_state == SESSION_STATE_READY
67 || ss->session_state == SESSION_STATE_ACCEPTING)
69 s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
70 ss->thread_index, verbose);
72 s = format (s, "%v", str);
74 s = format (s, "%U", format_stream_session_fifos, ss, verbose);
76 else if (ss->session_state == SESSION_STATE_LISTENING)
78 s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
81 else if (ss->session_state == SESSION_STATE_CONNECTING)
83 s = format (s, "%-40U%v", tp_vft->format_half_open,
84 ss->connection_index, str);
86 else if (ss->session_state == SESSION_STATE_CLOSED)
89 format (s, "[CL] %U", tp_vft->format_connection, ss->connection_index,
90 ss->thread_index, verbose);
92 s = format (s, "%v", str);
94 s = format (s, "%U", format_stream_session_fifos, ss, verbose);
98 clib_warning ("Session in state: %d!", ss->session_state);
106 unformat_stream_session_id (unformat_input_t * input, va_list * args)
108 u8 *proto = va_arg (*args, u8 *);
109 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
110 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
111 u16 *lcl_port = va_arg (*args, u16 *);
112 u16 *rmt_port = va_arg (*args, u16 *);
113 u8 *is_ip4 = va_arg (*args, u8 *);
116 memset (lcl, 0, sizeof (*lcl));
117 memset (rmt, 0, sizeof (*rmt));
119 if (unformat (input, "tcp"))
121 *proto = TRANSPORT_PROTO_TCP;
123 if (unformat (input, "udp"))
125 *proto = TRANSPORT_PROTO_UDP;
127 else if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
128 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
133 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
134 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
149 unformat_stream_session (unformat_input_t * input, va_list * args)
151 stream_session_t **result = va_arg (*args, stream_session_t **);
154 ip46_address_t lcl, rmt;
155 u32 lcl_port = 0, rmt_port = 0;
156 u8 is_ip4 = 0, s_type = ~0, id_is_set = 0;
158 if (unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
159 &lcl_port, &rmt_port, &is_ip4))
171 s_type = session_type_from_proto_and_ip (proto, is_ip4);
173 s = stream_session_lookup4 (&lcl.ip4, &rmt.ip4,
174 clib_host_to_net_u16 (lcl_port),
175 clib_host_to_net_u16 (rmt_port), s_type);
177 s = stream_session_lookup6 (&lcl.ip6, &rmt.ip6,
178 clib_host_to_net_u16 (lcl_port),
179 clib_host_to_net_u16 (rmt_port), s_type);
189 unformat_transport_connection (unformat_input_t * input, va_list * args)
191 transport_connection_t **result = va_arg (*args, transport_connection_t **);
192 u32 suggested_proto = va_arg (*args, u32);
193 transport_connection_t *tc;
195 ip46_address_t lcl, rmt;
196 u32 lcl_port = 0, rmt_port = 0;
197 u8 is_ip4 = 0, s_type = ~0, id_is_set = 0;
199 if (unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
200 &lcl_port, &rmt_port, &is_ip4))
212 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
213 if (proto == (u8) ~ 0)
215 s_type = session_type_from_proto_and_ip (proto, is_ip4);
217 tc = stream_session_lookup_transport4 (&lcl.ip4, &rmt.ip4,
218 clib_host_to_net_u16 (lcl_port),
219 clib_host_to_net_u16 (rmt_port),
222 tc = stream_session_lookup_transport6 (&lcl.ip6, &rmt.ip6,
223 clib_host_to_net_u16 (lcl_port),
224 clib_host_to_net_u16 (rmt_port),
235 static clib_error_t *
236 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
237 vlib_cli_command_t * cmd)
239 session_manager_main_t *smm = &session_manager_main;
241 stream_session_t *pool;
243 u8 *str = 0, one_session = 0;
245 if (!smm->is_enabled)
247 return clib_error_return (0, "session layer is not enabled");
250 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
252 if (unformat (input, "verbose %d", &verbose))
254 else if (unformat (input, "verbose"))
256 else if (unformat (input, "%U", unformat_stream_session, &s))
261 return clib_error_return (0, "unknown input `%U'",
262 format_unformat_error, input);
267 vlib_cli_output (vm, "%U", format_stream_session, s, 3);
271 for (i = 0; i < vec_len (smm->sessions); i++)
274 pool = smm->sessions[i];
278 if (pool_elts (pool))
281 vlib_cli_output (vm, "Thread %d: %d active sessions",
282 i, pool_elts (pool));
285 if (once_per_pool && verbose == 1)
288 format (str, "%-50s%-15s%-10s%-10s%-10s", "Connection",
289 "State", "Rx-f", "Tx-f", "S-idx");
290 vlib_cli_output (vm, "%v", str);
291 vec_reset_length (str);
296 pool_foreach (s, pool,
298 vec_reset_length (str);
299 str = format (str, "%U", format_stream_session, s, verbose);
300 vlib_cli_output (vm, "%v", str);
306 vlib_cli_output (vm, "Thread %d: no active sessions", i);
307 vec_reset_length (str);
315 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
317 .path = "show session",
318 .short_help = "show session [verbose [nnn]]",
319 .function = show_session_command_fn,
324 clear_session (stream_session_t * s)
326 application_t *server = application_get (s->app_index);
327 server->cb_fns.session_disconnect_callback (s);
331 static clib_error_t *
332 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
333 vlib_cli_command_t * cmd)
335 session_manager_main_t *smm = &session_manager_main;
336 u32 thread_index = 0, clear_all = 0;
337 u32 session_index = ~0;
338 stream_session_t **pool, *session;
340 if (!smm->is_enabled)
342 return clib_error_return (0, "session layer is not enabled");
345 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
347 if (unformat (input, "thread %d", &thread_index))
349 else if (unformat (input, "session %d", &session_index))
351 else if (unformat (input, "all"))
354 return clib_error_return (0, "unknown input `%U'",
355 format_unformat_error, input);
358 if (!clear_all && session_index == ~0)
359 return clib_error_return (0, "session <nn> required, but not set.");
361 if (session_index != ~0)
363 session = stream_session_get_if_valid (session_index, thread_index);
365 return clib_error_return (0, "no session %d on thread %d",
366 session_index, thread_index);
367 clear_session (session);
373 vec_foreach (pool, smm->sessions)
375 pool_foreach(session, *pool, ({
376 clear_session (session);
386 VLIB_CLI_COMMAND (clear_session_command, static) =
388 .path = "clear session",
389 .short_help = "clear session thread <thread> session <index>",
390 .function = clear_session_command_fn,
394 static clib_error_t *
395 show_session_fifo_trace_command_fn (vlib_main_t * vm,
396 unformat_input_t * input,
397 vlib_cli_command_t * cmd)
399 stream_session_t *s = 0;
400 u8 is_rx = 0, *str = 0;
402 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
404 if (unformat (input, "%U", unformat_stream_session, &s))
406 else if (unformat (input, "rx"))
408 else if (unformat (input, "tx"))
411 return clib_error_return (0, "unknown input `%U'",
412 format_unformat_error, input);
417 vlib_cli_output (vm, "fifo tracing not enabled");
423 vlib_cli_output (vm, "could not find session");
428 svm_fifo_dump_trace (str, s->server_rx_fifo) :
429 svm_fifo_dump_trace (str, s->server_tx_fifo);
431 vlib_cli_output (vm, "%v", str);
436 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
438 .path = "show session fifo trace",
439 .short_help = "show session fifo trace <session>",
440 .function = show_session_fifo_trace_command_fn,
444 static clib_error_t *
445 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
446 vlib_cli_command_t * cmd)
448 stream_session_t *s = 0;
449 u8 is_rx = 0, *str = 0;
451 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
453 if (unformat (input, "%U", unformat_stream_session, &s))
455 else if (unformat (input, "rx"))
458 return clib_error_return (0, "unknown input `%U'",
459 format_unformat_error, input);
464 vlib_cli_output (vm, "fifo tracing not enabled");
470 vlib_cli_output (vm, "could not find session");
475 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
476 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
478 vlib_cli_output (vm, "%v", str);
483 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
485 .path = "session replay fifo",
486 .short_help = "session replay fifo <session>",
487 .function = session_replay_fifo_command_fn,
491 static clib_error_t *
492 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
493 vlib_cli_command_t * cmd)
497 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
499 if (unformat (input, "enable"))
501 else if (unformat (input, "disable"))
504 return clib_error_return (0, "unknown input `%U'",
505 format_unformat_error, input);
508 return vnet_session_enable_disable (vm, is_en);
512 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
515 .short_help = "session [enable|disable]",
516 .function = session_enable_disable_fn,
521 * fd.io coding-style-patch-verification: ON
524 * eval: (c-set-style "gnu")