2 * Copyright (c) 2017-2019 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 session_t *ss = va_arg (*args, session_t *);
22 int verbose = va_arg (*args, int);
23 session_event_t _e, *e = &_e;
26 if (!ss->rx_fifo || !ss->tx_fifo)
29 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->rx_fifo, verbose);
30 if (verbose > 2 && ss->rx_fifo->has_event)
32 found = session_node_lookup_fifo_event (ss->rx_fifo, e);
33 s = format (s, " session node event: %s\n",
34 found ? "found" : "not found");
36 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->tx_fifo, verbose);
37 if (verbose > 2 && ss->tx_fifo->has_event)
39 found = session_node_lookup_fifo_event (ss->tx_fifo, e);
40 s = format (s, " session node event: %s\n",
41 found ? "found" : "not found");
47 * Format stream session as per the following format
50 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
55 format_session (u8 * s, va_list * args)
57 session_t *ss = va_arg (*args, session_t *);
58 int verbose = va_arg (*args, int);
59 u32 tp = session_get_transport_proto (ss);
62 if (ss->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
64 s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
70 u8 post_accept = ss->session_state >= SESSION_STATE_ACCEPTING;
71 u8 hasf = post_accept | session_tx_is_dgram (ss);
74 rxf = hasf ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
75 txf = hasf ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
76 str = format (0, "%-10u%-10u", rxf, txf);
79 if (ss->session_state >= SESSION_STATE_ACCEPTING
80 || ss->session_state == SESSION_STATE_CREATED)
82 s = format (s, "%U", format_transport_connection, tp,
83 ss->connection_index, ss->thread_index, verbose);
85 s = format (s, "%v", str);
87 s = format (s, "%U", format_session_fifos, ss, verbose);
89 else if (ss->session_state == SESSION_STATE_LISTENING)
91 s = format (s, "%U%v", format_transport_listen_connection,
92 tp, ss->connection_index, verbose, str);
94 s = format (s, "\n%U", format_session_fifos, ss, verbose);
96 else if (ss->session_state == SESSION_STATE_CONNECTING)
98 s = format (s, "%-40U%v", format_transport_half_open_connection,
99 tp, ss->connection_index, str);
103 clib_warning ("Session in state: %d!", ss->session_state);
111 unformat_stream_session_id (unformat_input_t * input, va_list * args)
113 u8 *proto = va_arg (*args, u8 *);
114 u32 *fib_index = va_arg (*args, u32 *);
115 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
116 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
117 u16 *lcl_port = va_arg (*args, u16 *);
118 u16 *rmt_port = va_arg (*args, u16 *);
119 u8 *is_ip4 = va_arg (*args, u8 *);
123 clib_memset (lcl, 0, sizeof (*lcl));
124 clib_memset (rmt, 0, sizeof (*rmt));
126 if (unformat (input, "tcp"))
128 *proto = TRANSPORT_PROTO_TCP;
130 else if (unformat (input, "udp"))
132 *proto = TRANSPORT_PROTO_UDP;
137 if (unformat (input, "vrf %u", &vrf))
140 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
141 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
146 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
147 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
155 fib_protocol_t fib_proto;
156 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
157 *fib_index = fib_table_find (fib_proto, vrf);
164 unformat_session (unformat_input_t * input, va_list * args)
166 session_t **result = va_arg (*args, session_t **);
167 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
168 ip46_address_t lcl, rmt;
173 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
174 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
178 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
179 clib_host_to_net_u16 (lcl_port),
180 clib_host_to_net_u16 (rmt_port), proto);
182 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
183 clib_host_to_net_u16 (lcl_port),
184 clib_host_to_net_u16 (rmt_port), proto);
188 session_pool_remove_peeker (s->thread_index);
195 unformat_transport_connection (unformat_input_t * input, va_list * args)
197 transport_connection_t **result = va_arg (*args, transport_connection_t **);
198 u32 suggested_proto = va_arg (*args, u32);
199 transport_connection_t *tc;
201 ip46_address_t lcl, rmt;
202 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
205 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
206 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
209 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
210 if (proto == (u8) ~ 0)
213 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
214 clib_host_to_net_u16 (lcl_port),
215 clib_host_to_net_u16 (rmt_port), proto);
217 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
218 clib_host_to_net_u16 (lcl_port),
219 clib_host_to_net_u16 (rmt_port), proto);
229 static clib_error_t *
230 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
231 vlib_cli_command_t * cmd)
233 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0;
234 session_main_t *smm = &session_main;
235 u32 transport_proto = ~0, track_index;
237 transport_connection_t *tc;
238 app_worker_t *app_wrk;
242 if (!smm->is_enabled)
244 return clib_error_return (0, "session layer is not enabled");
247 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
249 if (unformat (input, "verbose %d", &verbose))
251 else if (unformat (input, "verbose"))
253 else if (unformat (input, "listeners %U", unformat_transport_proto,
256 else if (unformat (input, "%U", unformat_session, &s))
260 else if (unformat (input, "elog"))
263 return clib_error_return (0, "unknown input `%U'",
264 format_unformat_error, input);
269 u8 *str = format (0, "%U", format_session, s, 3);
270 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
272 elog_main_t *em = &vm->elog_main;
275 tc = session_get_transport (s);
276 track_index = transport_elog_track_index (tc);
277 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
278 * vm->clib_time.seconds_per_clock;
279 if (track_index != ~0)
280 str = format (str, " session elog:\n%U", format_elog_track, em,
283 vlib_cli_output (vm, "%v", str);
290 sst = session_type_from_proto_and_ip (transport_proto, 1);
291 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
293 pool_foreach (s, smm->wrk[0].sessions, ({
294 if (s->session_state != SESSION_STATE_LISTENING
295 || s->session_type != sst)
297 app_wrk = app_worker_get (s->app_wrk_index);
298 app_name = application_name_from_index (app_wrk->app_index);
299 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
306 for (i = 0; i < vec_len (smm->wrk); i++)
308 u32 once_per_pool = 1, n_closed = 0;
310 pool = smm->wrk[i].sessions;
311 if (!pool_elts (pool))
313 vlib_cli_output (vm, "Thread %d: no sessions", i);
319 vlib_cli_output (vm, "Thread %d: %d sessions", i, pool_elts (pool));
323 if (once_per_pool && verbose == 1)
325 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s", i ? "\n" : "",
326 "Connection", "State", "Rx-f", "Tx-f");
331 pool_foreach (s, pool, ({
332 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
337 vlib_cli_output (vm, "%U", format_session, s, verbose);
342 vlib_cli_output (vm, "Thread %d: active sessions %u", i,
343 pool_elts (pool) - n_closed);
345 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u", i,
346 pool_elts (pool) - n_closed, n_closed);
353 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
355 .path = "show session",
356 .short_help = "show session [verbose [n]] [listeners <proto>] "
357 "[<session-id> [elog]]",
358 .function = show_session_command_fn,
363 clear_session (session_t * s)
365 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
366 app_worker_close_notify (server_wrk, s);
370 static clib_error_t *
371 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
372 vlib_cli_command_t * cmd)
374 session_main_t *smm = &session_main;
375 u32 thread_index = 0, clear_all = 0;
376 session_worker_t *wrk;
377 u32 session_index = ~0;
380 if (!smm->is_enabled)
382 return clib_error_return (0, "session layer is not enabled");
385 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
387 if (unformat (input, "thread %d", &thread_index))
389 else if (unformat (input, "session %d", &session_index))
391 else if (unformat (input, "all"))
394 return clib_error_return (0, "unknown input `%U'",
395 format_unformat_error, input);
398 if (!clear_all && session_index == ~0)
399 return clib_error_return (0, "session <nn> required, but not set.");
401 if (session_index != ~0)
403 session = session_get_if_valid (session_index, thread_index);
405 return clib_error_return (0, "no session %d on thread %d",
406 session_index, thread_index);
407 clear_session (session);
413 vec_foreach (wrk, smm->wrk)
415 pool_foreach(session, wrk->sessions, ({
416 clear_session (session);
426 VLIB_CLI_COMMAND (clear_session_command, static) =
428 .path = "clear session",
429 .short_help = "clear session thread <thread> session <index>",
430 .function = clear_session_command_fn,
434 static clib_error_t *
435 show_session_fifo_trace_command_fn (vlib_main_t * vm,
436 unformat_input_t * input,
437 vlib_cli_command_t * cmd)
440 u8 is_rx = 0, *str = 0;
442 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
444 if (unformat (input, "%U", unformat_session, &s))
446 else if (unformat (input, "rx"))
448 else if (unformat (input, "tx"))
451 return clib_error_return (0, "unknown input `%U'",
452 format_unformat_error, input);
457 vlib_cli_output (vm, "fifo tracing not enabled");
463 vlib_cli_output (vm, "could not find session");
468 svm_fifo_dump_trace (str, s->rx_fifo) :
469 svm_fifo_dump_trace (str, s->tx_fifo);
471 vlib_cli_output (vm, "%v", str);
476 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
478 .path = "show session fifo trace",
479 .short_help = "show session fifo trace <session>",
480 .function = show_session_fifo_trace_command_fn,
484 static clib_error_t *
485 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
486 vlib_cli_command_t * cmd)
489 u8 is_rx = 0, *str = 0;
491 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
493 if (unformat (input, "%U", unformat_session, &s))
495 else if (unformat (input, "rx"))
498 return clib_error_return (0, "unknown input `%U'",
499 format_unformat_error, input);
504 vlib_cli_output (vm, "fifo tracing not enabled");
510 vlib_cli_output (vm, "could not find session");
515 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
516 svm_fifo_replay (str, s->tx_fifo, 0, 1);
518 vlib_cli_output (vm, "%v", str);
523 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
525 .path = "session replay fifo",
526 .short_help = "session replay fifo <session>",
527 .function = session_replay_fifo_command_fn,
531 static clib_error_t *
532 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
533 vlib_cli_command_t * cmd)
535 unformat_input_t _line_input, *line_input = &_line_input;
539 if (!unformat_user (input, unformat_line_input, line_input))
540 return clib_error_return (0, "expected enable | disable");
542 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
544 if (unformat (line_input, "enable"))
546 else if (unformat (line_input, "disable"))
550 error = clib_error_return (0, "unknown input `%U'",
551 format_unformat_error, line_input);
552 unformat_free (line_input);
557 unformat_free (line_input);
558 return vnet_session_enable_disable (vm, is_en);
562 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
565 .short_help = "session [enable|disable]",
566 .function = session_enable_disable_fn,
571 * fd.io coding-style-patch-verification: ON
574 * eval: (c-set-style "gnu")