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
68 || ss->session_state == SESSION_STATE_CLOSED)
70 s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
71 ss->thread_index, verbose);
73 s = format (s, "%v", str);
75 s = format (s, "%U", format_stream_session_fifos, ss, verbose);
77 else if (ss->session_state == SESSION_STATE_LISTENING)
79 s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
82 else if (ss->session_state == SESSION_STATE_CONNECTING)
84 s = format (s, "%-40U%v", tp_vft->format_half_open,
85 ss->connection_index, str);
89 clib_warning ("Session in state: %d!", ss->session_state);
97 unformat_stream_session_id (unformat_input_t * input, va_list * args)
99 u8 *proto = va_arg (*args, u8 *);
100 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
101 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
102 u16 *lcl_port = va_arg (*args, u16 *);
103 u16 *rmt_port = va_arg (*args, u16 *);
104 u8 *is_ip4 = va_arg (*args, u8 *);
107 memset (lcl, 0, sizeof (*lcl));
108 memset (rmt, 0, sizeof (*rmt));
110 if (unformat (input, "tcp"))
112 *proto = TRANSPORT_PROTO_TCP;
114 if (unformat (input, "udp"))
116 *proto = TRANSPORT_PROTO_UDP;
118 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
119 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
124 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
125 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
135 unformat_stream_session (unformat_input_t * input, va_list * args)
137 stream_session_t **result = va_arg (*args, stream_session_t **);
140 ip46_address_t lcl, rmt;
141 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
144 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
145 &lcl_port, &rmt_port, &is_ip4))
149 s = session_lookup4 (fib_index, &lcl.ip4, &rmt.ip4,
150 clib_host_to_net_u16 (lcl_port),
151 clib_host_to_net_u16 (rmt_port), proto);
153 s = session_lookup6 (fib_index, &lcl.ip6, &rmt.ip6,
154 clib_host_to_net_u16 (lcl_port),
155 clib_host_to_net_u16 (rmt_port), proto);
165 unformat_transport_connection (unformat_input_t * input, va_list * args)
167 transport_connection_t **result = va_arg (*args, transport_connection_t **);
168 u32 suggested_proto = va_arg (*args, u32);
169 transport_connection_t *tc;
171 ip46_address_t lcl, rmt;
172 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
175 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
176 &lcl_port, &rmt_port, &is_ip4))
179 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
180 if (proto == (u8) ~ 0)
183 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
184 clib_host_to_net_u16 (lcl_port),
185 clib_host_to_net_u16 (rmt_port), proto);
187 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
188 clib_host_to_net_u16 (lcl_port),
189 clib_host_to_net_u16 (rmt_port), proto);
199 static clib_error_t *
200 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
201 vlib_cli_command_t * cmd)
203 session_manager_main_t *smm = &session_manager_main;
205 stream_session_t *pool;
207 u8 *str = 0, one_session = 0;
209 if (!smm->is_enabled)
211 return clib_error_return (0, "session layer is not enabled");
214 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
216 if (unformat (input, "verbose %d", &verbose))
218 else if (unformat (input, "verbose"))
220 else if (unformat (input, "%U", unformat_stream_session, &s))
225 return clib_error_return (0, "unknown input `%U'",
226 format_unformat_error, input);
231 vlib_cli_output (vm, "%U", format_stream_session, s, 3);
235 for (i = 0; i < vec_len (smm->sessions); i++)
238 pool = smm->sessions[i];
242 if (pool_elts (pool))
245 vlib_cli_output (vm, "Thread %d: %d active sessions",
246 i, pool_elts (pool));
249 if (once_per_pool && verbose == 1)
251 str = format (str, "%-50s%-15s%-10s%-10s%-10s",
252 "Connection", "State", "Rx-f", "Tx-f",
254 vlib_cli_output (vm, "%v", str);
255 vec_reset_length (str);
260 pool_foreach (s, pool,
262 vec_reset_length (str);
263 str = format (str, "%U", format_stream_session, s, verbose);
264 vlib_cli_output (vm, "%v", str);
270 vlib_cli_output (vm, "Thread %d: no active sessions", i);
271 vec_reset_length (str);
279 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
281 .path = "show session",
282 .short_help = "show session [verbose [nnn]]",
283 .function = show_session_command_fn,
288 clear_session (stream_session_t * s)
290 application_t *server = application_get (s->app_index);
291 server->cb_fns.session_disconnect_callback (s);
295 static clib_error_t *
296 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
297 vlib_cli_command_t * cmd)
299 session_manager_main_t *smm = &session_manager_main;
300 u32 thread_index = 0, clear_all = 0;
301 u32 session_index = ~0;
302 stream_session_t **pool, *session;
304 if (!smm->is_enabled)
306 return clib_error_return (0, "session layer is not enabled");
309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 if (unformat (input, "thread %d", &thread_index))
313 else if (unformat (input, "session %d", &session_index))
315 else if (unformat (input, "all"))
318 return clib_error_return (0, "unknown input `%U'",
319 format_unformat_error, input);
322 if (!clear_all && session_index == ~0)
323 return clib_error_return (0, "session <nn> required, but not set.");
325 if (session_index != ~0)
327 session = stream_session_get_if_valid (session_index, thread_index);
329 return clib_error_return (0, "no session %d on thread %d",
330 session_index, thread_index);
331 clear_session (session);
337 vec_foreach (pool, smm->sessions)
339 pool_foreach(session, *pool, ({
340 clear_session (session);
350 VLIB_CLI_COMMAND (clear_session_command, static) =
352 .path = "clear session",
353 .short_help = "clear session thread <thread> session <index>",
354 .function = clear_session_command_fn,
358 static clib_error_t *
359 show_session_fifo_trace_command_fn (vlib_main_t * vm,
360 unformat_input_t * input,
361 vlib_cli_command_t * cmd)
363 stream_session_t *s = 0;
364 u8 is_rx = 0, *str = 0;
366 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
368 if (unformat (input, "%U", unformat_stream_session, &s))
370 else if (unformat (input, "rx"))
372 else if (unformat (input, "tx"))
375 return clib_error_return (0, "unknown input `%U'",
376 format_unformat_error, input);
381 vlib_cli_output (vm, "fifo tracing not enabled");
387 vlib_cli_output (vm, "could not find session");
392 svm_fifo_dump_trace (str, s->server_rx_fifo) :
393 svm_fifo_dump_trace (str, s->server_tx_fifo);
395 vlib_cli_output (vm, "%v", str);
400 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
402 .path = "show session fifo trace",
403 .short_help = "show session fifo trace <session>",
404 .function = show_session_fifo_trace_command_fn,
408 static clib_error_t *
409 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
410 vlib_cli_command_t * cmd)
412 stream_session_t *s = 0;
413 u8 is_rx = 0, *str = 0;
415 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
417 if (unformat (input, "%U", unformat_stream_session, &s))
419 else if (unformat (input, "rx"))
422 return clib_error_return (0, "unknown input `%U'",
423 format_unformat_error, input);
428 vlib_cli_output (vm, "fifo tracing not enabled");
434 vlib_cli_output (vm, "could not find session");
439 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
440 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
442 vlib_cli_output (vm, "%v", str);
447 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
449 .path = "session replay fifo",
450 .short_help = "session replay fifo <session>",
451 .function = session_replay_fifo_command_fn,
455 static clib_error_t *
456 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
457 vlib_cli_command_t * cmd)
461 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
463 if (unformat (input, "enable"))
465 else if (unformat (input, "disable"))
468 return clib_error_return (0, "unknown input `%U'",
469 format_unformat_error, input);
472 return vnet_session_enable_disable (vm, is_en);
476 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
479 .short_help = "session [enable|disable]",
480 .function = session_enable_disable_fn,
485 * fd.io coding-style-patch-verification: ON
488 * eval: (c-set-style "gnu")