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_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_event_t _e, *e = &_e;
26 if (!ss->server_rx_fifo || !ss->server_tx_fifo)
29 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo,
31 if (verbose > 2 && ss->server_rx_fifo->has_event)
33 found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
34 s = format (s, " session node event: %s\n",
35 found ? "found" : "not found");
37 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo,
39 if (verbose > 2 && ss->server_tx_fifo->has_event)
41 found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
42 s = format (s, " session node event: %s\n",
43 found ? "found" : "not found");
49 * Format stream session as per the following format
52 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
57 format_stream_session (u8 * s, va_list * args)
59 stream_session_t *ss = va_arg (*args, stream_session_t *);
60 int verbose = va_arg (*args, int);
61 u32 tp = session_get_transport_proto (ss);
64 if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
65 str = format (0, "%-10u%-10u",
66 svm_fifo_max_dequeue (ss->server_rx_fifo),
67 svm_fifo_max_dequeue (ss->server_tx_fifo));
69 if (ss->session_state >= SESSION_STATE_ACCEPTING)
71 s = format (s, "%U", format_transport_connection, tp,
72 ss->connection_index, ss->thread_index, verbose);
74 s = format (s, "%v", str);
76 s = format (s, "%U", format_session_fifos, ss, verbose);
78 else if (ss->session_state == SESSION_STATE_LISTENING)
80 s = format (s, "%-40U%v", format_transport_listen_connection,
81 tp, ss->connection_index, str);
83 s = format (s, "\n%U", format_session_fifos, ss, verbose);
85 else if (ss->session_state == SESSION_STATE_CONNECTING)
87 s = format (s, "%-40U%v", format_transport_half_open_connection,
88 tp, ss->connection_index, str);
92 clib_warning ("Session in state: %d!", ss->session_state);
100 unformat_stream_session_id (unformat_input_t * input, va_list * args)
102 u8 *proto = va_arg (*args, u8 *);
103 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
104 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
105 u16 *lcl_port = va_arg (*args, u16 *);
106 u16 *rmt_port = va_arg (*args, u16 *);
107 u8 *is_ip4 = va_arg (*args, u8 *);
110 clib_memset (lcl, 0, sizeof (*lcl));
111 clib_memset (rmt, 0, sizeof (*rmt));
113 if (unformat (input, "tcp"))
115 *proto = TRANSPORT_PROTO_TCP;
117 if (unformat (input, "udp"))
119 *proto = TRANSPORT_PROTO_UDP;
121 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
122 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
127 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
128 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
138 unformat_stream_session (unformat_input_t * input, va_list * args)
140 stream_session_t **result = va_arg (*args, stream_session_t **);
143 ip46_address_t lcl, rmt;
144 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
147 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
148 &lcl_port, &rmt_port, &is_ip4))
152 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
153 clib_host_to_net_u16 (lcl_port),
154 clib_host_to_net_u16 (rmt_port), proto);
156 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
157 clib_host_to_net_u16 (lcl_port),
158 clib_host_to_net_u16 (rmt_port), proto);
162 session_pool_remove_peeker (s->thread_index);
169 unformat_transport_connection (unformat_input_t * input, va_list * args)
171 transport_connection_t **result = va_arg (*args, transport_connection_t **);
172 u32 suggested_proto = va_arg (*args, u32);
173 transport_connection_t *tc;
175 ip46_address_t lcl, rmt;
176 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
179 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
180 &lcl_port, &rmt_port, &is_ip4))
183 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
184 if (proto == (u8) ~ 0)
187 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
188 clib_host_to_net_u16 (lcl_port),
189 clib_host_to_net_u16 (rmt_port), proto);
191 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
192 clib_host_to_net_u16 (lcl_port),
193 clib_host_to_net_u16 (rmt_port), proto);
203 static clib_error_t *
204 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
205 vlib_cli_command_t * cmd)
207 session_manager_main_t *smm = &session_manager_main;
208 u8 *str = 0, one_session = 0, do_listeners = 0, sst, *app_name;
210 stream_session_t *pool, *s;
211 u32 transport_proto = ~0;
213 if (!smm->is_enabled)
215 return clib_error_return (0, "session layer is not enabled");
218 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
220 if (unformat (input, "verbose %d", &verbose))
222 else if (unformat (input, "verbose"))
224 else if (unformat (input, "listeners %U", unformat_transport_proto,
227 else if (unformat (input, "%U", unformat_stream_session, &s))
232 return clib_error_return (0, "unknown input `%U'",
233 format_unformat_error, input);
238 vlib_cli_output (vm, "%U", format_stream_session, s, 3);
244 sst = session_type_from_proto_and_ip (transport_proto, 1);
245 vlib_cli_output (vm, "%-40s%-24s", "Listener", "App");
247 pool_foreach (s, smm->wrk[0].sessions, ({
248 if (s->session_state != SESSION_STATE_LISTENING
249 || s->session_type != sst)
251 app_name = application_name_from_index (s->app_wrk_index);
252 vlib_cli_output (vm, "%U%-25v%", format_stream_session, s, 1,
260 for (i = 0; i < vec_len (smm->wrk); i++)
263 pool = smm->wrk[0].sessions;
267 if (pool_elts (pool))
270 vlib_cli_output (vm, "Thread %d: %d active sessions",
271 i, pool_elts (pool));
274 if (once_per_pool && verbose == 1)
276 str = format (str, "%-50s%-15s%-10s%-10s",
277 "Connection", "State", "Rx-f", "Tx-f");
278 vlib_cli_output (vm, "%v", str);
279 vec_reset_length (str);
284 pool_foreach (s, pool,
286 vec_reset_length (str);
287 str = format (str, "%U", format_stream_session, s, verbose);
288 vlib_cli_output (vm, "%v", str);
294 vlib_cli_output (vm, "Thread %d: no active sessions", i);
295 vec_reset_length (str);
303 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
305 .path = "show session",
306 .short_help = "show session [verbose [nnn]]",
307 .function = show_session_command_fn,
312 clear_session (stream_session_t * s)
314 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
315 application_t *server = application_get (server_wrk->app_index);
316 server->cb_fns.session_disconnect_callback (s);
320 static clib_error_t *
321 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
322 vlib_cli_command_t * cmd)
324 session_manager_main_t *smm = &session_manager_main;
325 u32 thread_index = 0, clear_all = 0;
326 session_manager_worker_t *wrk;
327 u32 session_index = ~0;
328 stream_session_t *session;
330 if (!smm->is_enabled)
332 return clib_error_return (0, "session layer is not enabled");
335 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
337 if (unformat (input, "thread %d", &thread_index))
339 else if (unformat (input, "session %d", &session_index))
341 else if (unformat (input, "all"))
344 return clib_error_return (0, "unknown input `%U'",
345 format_unformat_error, input);
348 if (!clear_all && session_index == ~0)
349 return clib_error_return (0, "session <nn> required, but not set.");
351 if (session_index != ~0)
353 session = session_get_if_valid (session_index, thread_index);
355 return clib_error_return (0, "no session %d on thread %d",
356 session_index, thread_index);
357 clear_session (session);
363 vec_foreach (wrk, smm->wrk)
365 pool_foreach(session, wrk->sessions, ({
366 clear_session (session);
376 VLIB_CLI_COMMAND (clear_session_command, static) =
378 .path = "clear session",
379 .short_help = "clear session thread <thread> session <index>",
380 .function = clear_session_command_fn,
384 static clib_error_t *
385 show_session_fifo_trace_command_fn (vlib_main_t * vm,
386 unformat_input_t * input,
387 vlib_cli_command_t * cmd)
389 stream_session_t *s = 0;
390 u8 is_rx = 0, *str = 0;
392 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
394 if (unformat (input, "%U", unformat_stream_session, &s))
396 else if (unformat (input, "rx"))
398 else if (unformat (input, "tx"))
401 return clib_error_return (0, "unknown input `%U'",
402 format_unformat_error, input);
407 vlib_cli_output (vm, "fifo tracing not enabled");
413 vlib_cli_output (vm, "could not find session");
418 svm_fifo_dump_trace (str, s->server_rx_fifo) :
419 svm_fifo_dump_trace (str, s->server_tx_fifo);
421 vlib_cli_output (vm, "%v", str);
426 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
428 .path = "show session fifo trace",
429 .short_help = "show session fifo trace <session>",
430 .function = show_session_fifo_trace_command_fn,
434 static clib_error_t *
435 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
436 vlib_cli_command_t * cmd)
438 stream_session_t *s = 0;
439 u8 is_rx = 0, *str = 0;
441 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
443 if (unformat (input, "%U", unformat_stream_session, &s))
445 else if (unformat (input, "rx"))
448 return clib_error_return (0, "unknown input `%U'",
449 format_unformat_error, input);
454 vlib_cli_output (vm, "fifo tracing not enabled");
460 vlib_cli_output (vm, "could not find session");
465 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
466 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
468 vlib_cli_output (vm, "%v", str);
473 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
475 .path = "session replay fifo",
476 .short_help = "session replay fifo <session>",
477 .function = session_replay_fifo_command_fn,
481 static clib_error_t *
482 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
483 vlib_cli_command_t * cmd)
485 unformat_input_t _line_input, *line_input = &_line_input;
489 if (!unformat_user (input, unformat_line_input, line_input))
490 return clib_error_return (0, "expected enable | disable");
492 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
494 if (unformat (line_input, "enable"))
496 else if (unformat (line_input, "disable"))
500 error = clib_error_return (0, "unknown input `%U'",
501 format_unformat_error, line_input);
502 unformat_free (line_input);
507 unformat_free (line_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")