blob: 2003f0a77883b5f6a54480217474adf2ff374381 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <vnet/session/application.h>
16#include <vnet/session/session.h>
17
Florin Coras93992a92017-05-24 18:03:56 -070018u8 *
Florin Corasde9a8492018-10-24 22:18:58 -070019format_session_fifos (u8 * s, va_list * args)
Florin Coras93992a92017-05-24 18:03:56 -070020{
Florin Coras288eaab2019-02-03 15:26:14 -080021 session_t *ss = va_arg (*args, session_t *);
Florin Coras6534b7a2017-07-18 05:38:03 -040022 int verbose = va_arg (*args, int);
Florin Coras52207f12018-07-12 14:48:06 -070023 session_event_t _e, *e = &_e;
Florin Coras6534b7a2017-07-18 05:38:03 -040024 u8 found;
25
Florin Coras288eaab2019-02-03 15:26:14 -080026 if (!ss->rx_fifo || !ss->tx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -070027 return s;
28
Florin Coras288eaab2019-02-03 15:26:14 -080029 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->rx_fifo, verbose);
Florin Corasc547e912020-12-08 17:50:45 -080030 if (verbose > 2 && ss->rx_fifo->shr->has_event)
Florin Coras6534b7a2017-07-18 05:38:03 -040031 {
Florin Coras288eaab2019-02-03 15:26:14 -080032 found = session_node_lookup_fifo_event (ss->rx_fifo, e);
Florin Coras6534b7a2017-07-18 05:38:03 -040033 s = format (s, " session node event: %s\n",
34 found ? "found" : "not found");
35 }
Florin Coras288eaab2019-02-03 15:26:14 -080036 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->tx_fifo, verbose);
Florin Corasc547e912020-12-08 17:50:45 -080037 if (verbose > 2 && ss->tx_fifo->shr->has_event)
Florin Coras6534b7a2017-07-18 05:38:03 -040038 {
Florin Coras288eaab2019-02-03 15:26:14 -080039 found = session_node_lookup_fifo_event (ss->tx_fifo, e);
Florin Coras6534b7a2017-07-18 05:38:03 -040040 s = format (s, " session node event: %s\n",
41 found ? "found" : "not found");
42 }
Florin Coras93992a92017-05-24 18:03:56 -070043 return s;
44}
45
Florin Coras91f90d02019-10-23 19:28:08 -070046const char *session_state_str[] = {
47#define _(sym, str) str,
48 foreach_session_state
49#undef _
50};
51
52u8 *
53format_session_state (u8 * s, va_list * args)
54{
55 session_t *ss = va_arg (*args, session_t *);
56
57 if (ss->session_state < SESSION_N_STATES)
58 s = format (s, "%s", session_state_str[ss->session_state]);
59 else
60 s = format (s, "UNKNOWN STATE (%d)", ss->session_state);
61
62 return s;
63}
64
65const char *session_flags_str[] = {
66#define _(sym, str) str,
67 foreach_session_flag
68#undef _
69};
70
71u8 *
72format_session_flags (u8 * s, va_list * args)
73{
74 session_t *ss = va_arg (*args, session_t *);
75 int i, last = -1;
76
77 for (i = 0; i < SESSION_N_FLAGS; i++)
78 if (ss->flags & (1 << i))
79 last = i;
80
81 for (i = 0; i < last; i++)
82 {
83 if (ss->flags & (1 << i))
84 s = format (s, "%s, ", session_flags_str[i]);
85 }
86 if (last >= 0)
87 s = format (s, "%s", session_flags_str[last]);
88
89 return s;
90}
91
Dave Barach68b0fb02017-02-28 15:15:56 -050092/**
93 * Format stream session as per the following format
94 *
95 * verbose:
96 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
97 * non-verbose:
98 * "Connection"
99 */
100u8 *
Florin Coras31c99552019-03-01 13:00:58 -0800101format_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500102{
Florin Coras288eaab2019-02-03 15:26:14 -0800103 session_t *ss = va_arg (*args, session_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -0500104 int verbose = va_arg (*args, int);
Florin Corasde9a8492018-10-24 22:18:58 -0700105 u32 tp = session_get_transport_proto (ss);
Dave Barach68b0fb02017-02-28 15:15:56 -0500106 u8 *str = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500107
Florin Coras5f066322019-07-22 19:03:03 -0700108 if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras3389dd22019-02-01 18:00:05 -0800109 {
110 s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
111 return s;
112 }
113
114 if (verbose == 1)
115 {
Florin Coras3389dd22019-02-01 18:00:05 -0800116 u32 rxf, txf;
117
Aloys Augustin6eef40b2020-04-01 19:50:17 +0200118 rxf = ss->rx_fifo ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
119 txf = ss->tx_fifo ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
Florin Coras3389dd22019-02-01 18:00:05 -0800120 str = format (0, "%-10u%-10u", rxf, txf);
121 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500122
Florin Coras509028a2019-04-02 17:47:03 -0700123 if (ss->session_state >= SESSION_STATE_ACCEPTING
124 || ss->session_state == SESSION_STATE_CREATED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500125 {
Florin Corasde9a8492018-10-24 22:18:58 -0700126 s = format (s, "%U", format_transport_connection, tp,
127 ss->connection_index, ss->thread_index, verbose);
Florin Corasbb292f42017-05-19 09:49:19 -0700128 if (verbose == 1)
129 s = format (s, "%v", str);
Florin Coras93992a92017-05-24 18:03:56 -0700130 if (verbose > 1)
Florin Coras91f90d02019-10-23 19:28:08 -0700131 {
132 s = format (s, "%U", format_session_fifos, ss, verbose);
Florin Corasd9035a42019-11-19 18:22:41 -0800133 s = format (s, " session: state: %U opaque: 0x%x flags: %U\n",
134 format_session_state, ss, ss->opaque,
135 format_session_flags, ss);
Florin Coras91f90d02019-10-23 19:28:08 -0700136 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500137 }
138 else if (ss->session_state == SESSION_STATE_LISTENING)
139 {
Florin Coras3389dd22019-02-01 18:00:05 -0800140 s = format (s, "%U%v", format_transport_listen_connection,
Aloys Augustina0abbff2019-07-12 12:16:16 +0200141 tp, ss->connection_index, ss->thread_index, verbose, str);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700142 if (verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -0700143 s = format (s, "\n%U", format_session_fifos, ss, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -0500144 }
Florin Corasbb292f42017-05-19 09:49:19 -0700145 else if (ss->session_state == SESSION_STATE_CONNECTING)
Dave Barach68b0fb02017-02-28 15:15:56 -0500146 {
Florin Corasea727642021-05-07 19:39:43 -0700147 if (ss->flags & SESSION_F_HALF_OPEN)
148 s = format (s, "%U%v", format_transport_half_open_connection, tp,
149 ss->connection_index, ss->thread_index, verbose, str);
150 else
151 s = format (s, "%U", format_transport_connection, tp,
152 ss->connection_index, ss->thread_index, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -0500153 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500154 else
155 {
Florin Corase04c2992017-03-01 08:17:34 -0800156 clib_warning ("Session in state: %d!", ss->session_state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500157 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500158 vec_free (str);
159
160 return s;
161}
162
Florin Coras3eb50622017-07-13 01:24:57 -0400163uword
164unformat_stream_session_id (unformat_input_t * input, va_list * args)
165{
166 u8 *proto = va_arg (*args, u8 *);
Florin Corasbaee8d42019-01-19 11:29:18 -0800167 u32 *fib_index = va_arg (*args, u32 *);
Florin Coras3eb50622017-07-13 01:24:57 -0400168 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
169 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
170 u16 *lcl_port = va_arg (*args, u16 *);
171 u16 *rmt_port = va_arg (*args, u16 *);
172 u8 *is_ip4 = va_arg (*args, u8 *);
173 u8 tuple_is_set = 0;
Florin Corasbaee8d42019-01-19 11:29:18 -0800174 u32 vrf = ~0;
Florin Coras3eb50622017-07-13 01:24:57 -0400175
Dave Barachb7b92992018-10-17 10:38:51 -0400176 clib_memset (lcl, 0, sizeof (*lcl));
177 clib_memset (rmt, 0, sizeof (*rmt));
Florin Coras3eb50622017-07-13 01:24:57 -0400178
179 if (unformat (input, "tcp"))
180 {
181 *proto = TRANSPORT_PROTO_TCP;
182 }
Florin Corasbaee8d42019-01-19 11:29:18 -0800183 else if (unformat (input, "udp"))
Florin Coras3eb50622017-07-13 01:24:57 -0400184 {
185 *proto = TRANSPORT_PROTO_UDP;
186 }
Florin Corasbaee8d42019-01-19 11:29:18 -0800187 else
188 return 0;
189
190 if (unformat (input, "vrf %u", &vrf))
191 ;
192
Dave Barachb7f1faa2017-08-29 11:43:37 -0400193 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
194 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
Florin Coras3eb50622017-07-13 01:24:57 -0400195 {
196 *is_ip4 = 1;
197 tuple_is_set = 1;
198 }
199 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
200 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
201 {
202 *is_ip4 = 0;
203 tuple_is_set = 1;
204 }
Florin Coras3eb50622017-07-13 01:24:57 -0400205
Florin Corasbaee8d42019-01-19 11:29:18 -0800206 if (vrf != ~0)
207 {
208 fib_protocol_t fib_proto;
209 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
210 *fib_index = fib_table_find (fib_proto, vrf);
211 }
212
Chris Lukeb2bcad62017-09-18 08:51:22 -0400213 return tuple_is_set;
Florin Coras3eb50622017-07-13 01:24:57 -0400214}
215
216uword
Florin Coras5bb23ec2019-08-31 09:45:13 -0700217unformat_session_state (unformat_input_t * input, va_list * args)
218{
219 session_state_t *state = va_arg (*args, session_state_t *);
220 u8 *state_vec = 0;
221 int rv = 0;
222
223#define _(sym, str) \
224 if (unformat (input, str)) \
225 { \
226 *state = SESSION_STATE_ ## sym; \
227 rv = 1; \
228 goto done; \
229 }
230 foreach_session_state
231#undef _
232done:
233 vec_free (state_vec);
234 return rv;
235}
236
237uword
Florin Coras31c99552019-03-01 13:00:58 -0800238unformat_session (unformat_input_t * input, va_list * args)
Florin Coras3eb50622017-07-13 01:24:57 -0400239{
Florin Coras288eaab2019-02-03 15:26:14 -0800240 session_t **result = va_arg (*args, session_t **);
Florin Corasbaee8d42019-01-19 11:29:18 -0800241 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
242 ip46_address_t lcl, rmt;
Florin Coras288eaab2019-02-03 15:26:14 -0800243 session_t *s;
Florin Coras3eb50622017-07-13 01:24:57 -0400244 u8 proto = ~0;
Florin Corascea194d2017-10-02 00:18:51 -0700245 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400246
Florin Corasbaee8d42019-01-19 11:29:18 -0800247 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
248 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400249 return 0;
250
Florin Coras3eb50622017-07-13 01:24:57 -0400251 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700252 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
253 clib_host_to_net_u16 (lcl_port),
254 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400255 else
Florin Coras3cbc04b2017-10-02 00:18:51 -0700256 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
257 clib_host_to_net_u16 (lcl_port),
258 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400259 if (s)
260 {
261 *result = s;
262 return 1;
263 }
264 return 0;
265}
266
267uword
268unformat_transport_connection (unformat_input_t * input, va_list * args)
269{
270 transport_connection_t **result = va_arg (*args, transport_connection_t **);
271 u32 suggested_proto = va_arg (*args, u32);
272 transport_connection_t *tc;
273 u8 proto = ~0;
274 ip46_address_t lcl, rmt;
Florin Corascea194d2017-10-02 00:18:51 -0700275 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
276 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400277
Florin Corasbaee8d42019-01-19 11:29:18 -0800278 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
279 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400280 return 0;
281
Florin Coras3eb50622017-07-13 01:24:57 -0400282 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
283 if (proto == (u8) ~ 0)
284 return 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400285 if (is_ip4)
Florin Corascea194d2017-10-02 00:18:51 -0700286 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
287 clib_host_to_net_u16 (lcl_port),
288 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400289 else
Florin Corascea194d2017-10-02 00:18:51 -0700290 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
291 clib_host_to_net_u16 (lcl_port),
292 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400293
294 if (tc)
295 {
296 *result = tc;
297 return 1;
298 }
299 return 0;
300}
301
Florin Coras5bb23ec2019-08-31 09:45:13 -0700302static void
303session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
304{
305 session_main_t *smm = &session_main;
Florin Coras2d0b2bb2019-11-01 14:03:28 -0700306 u32 n_closed, thread_index;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700307 session_t *pool, *s;
308
309 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
310 {
311 pool = smm->wrk[thread_index].sessions;
312
313 if (!pool_elts (pool))
314 {
315 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
316 continue;
317 }
318
319 if (!verbose)
320 {
321 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
322 pool_elts (pool));
323 continue;
324 }
325
326 if (pool_elts (pool) > 50)
327 {
328 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
329 "suppressed. For more details use filters.",
330 thread_index, pool_elts (pool));
331 continue;
332 }
333
334 if (verbose == 1)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700335 vlib_cli_output (vm, "%s%-" SESSION_CLI_ID_LEN "s%-"
336 SESSION_CLI_STATE_LEN "s%-10s%-10s",
Florin Coras5bb23ec2019-08-31 09:45:13 -0700337 thread_index ? "\n" : "",
338 "Connection", "State", "Rx-f", "Tx-f");
339
Florin Coras2d0b2bb2019-11-01 14:03:28 -0700340 n_closed = 0;
341
Florin Coras5bb23ec2019-08-31 09:45:13 -0700342 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100343 pool_foreach (s, pool) {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700344 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
345 {
346 n_closed += 1;
347 continue;
348 }
349 vlib_cli_output (vm, "%U", format_session, s, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100350 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700351 /* *INDENT-ON* */
352
353 if (!n_closed)
354 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
355 pool_elts (pool) - n_closed);
356 else
357 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
358 thread_index, pool_elts (pool) - n_closed, n_closed);
359 }
360}
361
362static int
363session_cli_filter_check (session_t * s, session_state_t * states,
364 transport_proto_t tp)
365{
366 if (states)
367 {
368 session_state_t *state;
369 vec_foreach (state, states) if (s->session_state == *state)
370 goto check_transport;
371 return 0;
372 }
373
374check_transport:
375
Florin Coras07063b82020-03-13 04:44:51 +0000376 if (tp != TRANSPORT_PROTO_INVALID && session_get_transport_proto (s) != tp)
Florin Coras5bb23ec2019-08-31 09:45:13 -0700377 return 0;
378
379 return 1;
380}
381
382static void
383session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
384 u32 start, u32 end, session_state_t * states,
385 transport_proto_t tp, int verbose)
386{
387 u8 output_suppressed = 0;
388 session_worker_t *wrk;
389 session_t *pool, *s;
390 u32 count = 0, max_index;
391 int i;
392
393 wrk = session_main_get_worker_if_valid (thread_index);
394 if (!wrk)
395 {
396 vlib_cli_output (vm, "invalid thread index %u", thread_index);
397 return;
398 }
399
400 pool = wrk->sessions;
401
Florin Coras07063b82020-03-13 04:44:51 +0000402 if (tp == TRANSPORT_PROTO_INVALID && states == 0 && !verbose
Florin Coras5bb23ec2019-08-31 09:45:13 -0700403 && (start == 0 && end == ~0))
404 {
405 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
406 pool_elts (pool));
407 return;
408 }
409
Florin Coras61a89f22019-09-09 19:22:36 -0700410 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700411 for (i = start; i <= clib_min (end, max_index); i++)
412 {
413 if (pool_is_free_index (pool, i))
414 continue;
415
416 s = pool_elt_at_index (pool, i);
417
418 if (session_cli_filter_check (s, states, tp))
419 {
420 count += 1;
421 if (verbose)
422 {
423 if (count > 50 || (verbose > 1 && count > 10))
424 {
425 output_suppressed = 1;
426 continue;
427 }
428 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
429 vlib_cli_output (vm, "%U", format_session, s, verbose);
430 }
431 }
432 }
433
434 if (!output_suppressed)
435 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
436 thread_index, count);
437 else
438 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
439 " shown. Use finer grained filter.", thread_index,
440 count);
441}
442
Florin Coras16d974e2020-02-09 20:03:12 +0000443void
444session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
445{
446 session_worker_t *wrk;
447
448 wrk = session_main_get_worker_if_valid (thread_index);
449 if (!wrk)
450 {
451 vlib_cli_output (vm, "invalid thread index %u", thread_index);
452 return;
453 }
454
455 vlib_cli_output (vm, "Thread %d:\n", thread_index);
456 vlib_cli_output (vm, " evt elements alloc: %u",
Florin Coras46b8b1a2021-05-17 19:09:33 -0700457 clib_llist_elts (wrk->event_elts));
Florin Coras16d974e2020-02-09 20:03:12 +0000458 vlib_cli_output (vm, " ctrl evt elt data alloc: %d",
Florin Coras46b8b1a2021-05-17 19:09:33 -0700459 clib_llist_elts (wrk->ctrl_evts_data));
Florin Coras16d974e2020-02-09 20:03:12 +0000460}
461
462static void
463session_cli_show_events (vlib_main_t * vm, u32 thread_index)
464{
465 session_main_t *smm = &session_main;
466 if (!thread_index)
467 {
468 session_cli_show_events_thread (vm, thread_index);
469 return;
470 }
471
472 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
473 session_cli_show_events_thread (vm, thread_index);
474}
475
Florin Coras5bb23ec2019-08-31 09:45:13 -0700476static void
Florin Coras5bb23ec2019-08-31 09:45:13 -0700477session_cli_print_session_states (vlib_main_t * vm)
478{
479#define _(sym, str) vlib_cli_output (vm, str);
480 foreach_session_state
481#undef _
482}
483
Dave Barach68b0fb02017-02-28 15:15:56 -0500484static clib_error_t *
485show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
486 vlib_cli_command_t * cmd)
487{
Florin Coras5bb23ec2019-08-31 09:45:13 -0700488 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
489 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
490 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras07063b82020-03-13 04:44:51 +0000491 transport_proto_t transport_proto = TRANSPORT_PROTO_INVALID;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700492 session_state_t state = SESSION_N_STATES, *states = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800493 session_main_t *smm = &session_main;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700494 clib_error_t *error = 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800495 app_worker_t *app_wrk;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700496 u32 transport_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800497 const u8 *app_name;
Florin Coras16d974e2020-02-09 20:03:12 +0000498 u8 do_events = 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700499 int verbose = 0;
500 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500501
Florin Coras5bb23ec2019-08-31 09:45:13 -0700502 session_cli_return_if_not_enabled ();
503
504 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase04c2992017-03-01 08:17:34 -0800505 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700506 session_cli_show_all_sessions (vm, 0);
507 return 0;
Florin Corase04c2992017-03-01 08:17:34 -0800508 }
509
Florin Coras5bb23ec2019-08-31 09:45:13 -0700510 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Dave Barach68b0fb02017-02-28 15:15:56 -0500511 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700512 if (unformat (line_input, "verbose %d", &verbose))
Florin Corasbb292f42017-05-19 09:49:19 -0700513 ;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700514 else if (unformat (line_input, "verbose"))
Dave Barach68b0fb02017-02-28 15:15:56 -0500515 verbose = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700516 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
Florin Corasdbd44562017-11-09 19:30:17 -0800517 &transport_proto))
518 do_listeners = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700519 else if (unformat (line_input, "%U", unformat_session, &s))
Dave Barach2c25a622017-06-26 11:35:07 -0400520 {
521 one_session = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400522 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700523 else if (unformat (line_input, "thread %u index %u", &thread_index,
524 &session_index))
525 {
526 s = session_get_if_valid (session_index, thread_index);
527 if (!s)
528 {
529 vlib_cli_output (vm, "session is not allocated");
530 goto done;
531 }
532 one_session = 1;
533 }
534 else if (unformat (line_input, "thread %u", &thread_index))
535 {
536 do_filter = 1;
537 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500538 else
Florin Coras5bb23ec2019-08-31 09:45:13 -0700539 if (unformat (line_input, "state %U", unformat_session_state, &state))
540 {
541 vec_add1 (states, state);
542 do_filter = 1;
543 }
544 else if (unformat (line_input, "proto %U index %u",
545 unformat_transport_proto, &transport_proto,
546 &transport_index))
547 {
548 transport_connection_t *tc;
549 tc = transport_get_connection (transport_proto, transport_index,
550 thread_index);
551 if (!tc)
552 {
553 vlib_cli_output (vm, "transport connection %u thread %u is not"
554 " allocated", transport_index, thread_index);
555 goto done;
556 }
557 s = session_get_if_valid (tc->s_index, thread_index);
558 if (!s)
559 {
560 vlib_cli_output (vm, "session for transport connection %u "
561 "thread %u does not exist", transport_index,
562 thread_index);
563 goto done;
564 }
565 one_session = 1;
566 }
567 else if (unformat (line_input, "proto %U", unformat_transport_proto,
568 &transport_proto))
569 do_filter = 1;
570 else if (unformat (line_input, "range %u %u", &start, &end))
571 do_filter = 1;
572 else if (unformat (line_input, "range %u", &start))
573 {
574 end = start + 50;
575 do_filter = 1;
576 }
577 else if (unformat (line_input, "elog"))
578 do_elog = 1;
579 else if (unformat (line_input, "protos"))
580 {
Florin Coras07063b82020-03-13 04:44:51 +0000581 vlib_cli_output (vm, "%U", format_transport_protos);
Florin Coras5bb23ec2019-08-31 09:45:13 -0700582 goto done;
583 }
584 else if (unformat (line_input, "states"))
585 {
586 session_cli_print_session_states (vm);
587 goto done;
588 }
Florin Coras16d974e2020-02-09 20:03:12 +0000589 else if (unformat (line_input, "events"))
590 do_events = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700591 else
592 {
593 error = clib_error_return (0, "unknown input `%U'",
594 format_unformat_error, line_input);
595 goto done;
596 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500597 }
598
Dave Barach2c25a622017-06-26 11:35:07 -0400599 if (one_session)
600 {
Florin Coras31c99552019-03-01 13:00:58 -0800601 u8 *str = format (0, "%U", format_session, s, 3);
Florin Coras844a36d2018-12-20 09:50:50 -0800602 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
Florin Corasaefbede2018-12-19 13:07:49 -0800603 {
Damjan Marionf553a2c2021-03-26 13:45:37 +0100604 elog_main_t *em = &vlib_global_main.elog_main;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700605 transport_connection_t *tc;
Florin Corasaefbede2018-12-19 13:07:49 -0800606 f64 dt;
607
608 tc = session_get_transport (s);
609 track_index = transport_elog_track_index (tc);
610 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
611 * vm->clib_time.seconds_per_clock;
612 if (track_index != ~0)
613 str = format (str, " session elog:\n%U", format_elog_track, em,
614 dt, track_index);
615 }
616 vlib_cli_output (vm, "%v", str);
Florin Coras3389dd22019-02-01 18:00:05 -0800617 vec_free (str);
Florin Coras5bb23ec2019-08-31 09:45:13 -0700618 goto done;
Dave Barach2c25a622017-06-26 11:35:07 -0400619 }
620
Florin Corasdbd44562017-11-09 19:30:17 -0800621 if (do_listeners)
622 {
623 sst = session_type_from_proto_and_ip (transport_proto, 1);
Florin Coras7bf6ed62020-09-23 12:02:08 -0700624 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-24s", "Listener",
625 "App");
626
Florin Coras5c9083d2018-04-13 06:39:07 -0700627 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100628 pool_foreach (s, smm->wrk[0].sessions) {
Florin Coras5c9083d2018-04-13 06:39:07 -0700629 if (s->session_state != SESSION_STATE_LISTENING
630 || s->session_type != sst)
631 continue;
Florin Coras053a0e42018-11-13 15:52:38 -0800632 app_wrk = app_worker_get (s->app_wrk_index);
633 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras31c99552019-03-01 13:00:58 -0800634 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
Florin Corasde9a8492018-10-24 22:18:58 -0700635 app_name);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100636 }
Florin Coras5c9083d2018-04-13 06:39:07 -0700637 /* *INDENT-ON* */
Florin Coras5bb23ec2019-08-31 09:45:13 -0700638 goto done;
Florin Corasdbd44562017-11-09 19:30:17 -0800639 }
640
Florin Coras16d974e2020-02-09 20:03:12 +0000641 if (do_events)
642 {
643 session_cli_show_events (vm, thread_index);
644 goto done;
645 }
646
Florin Coras5bb23ec2019-08-31 09:45:13 -0700647 if (do_filter)
Dave Barach68b0fb02017-02-28 15:15:56 -0500648 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700649 if (end < start)
Dave Barach68b0fb02017-02-28 15:15:56 -0500650 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700651 error = clib_error_return (0, "invalid range start: %u end: %u",
652 start, end);
653 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500654 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700655 session_cli_show_session_filter (vm, thread_index, start, end, states,
656 transport_proto, verbose);
657 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500658 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500659
Florin Coras5bb23ec2019-08-31 09:45:13 -0700660 session_cli_show_all_sessions (vm, verbose);
661
662done:
663 unformat_free (line_input);
664 vec_free (states);
665 return error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500666}
667
Florin Corase04c2992017-03-01 08:17:34 -0800668/* *INDENT-OFF* */
Florin Coras66b11312017-07-31 17:18:03 -0700669VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500670{
Florin Corase04c2992017-03-01 08:17:34 -0800671 .path = "show session",
Florin Coras3389dd22019-02-01 18:00:05 -0800672 .short_help = "show session [verbose [n]] [listeners <proto>] "
Florin Coras5bb23ec2019-08-31 09:45:13 -0700673 "[<session-id> [elog]] [thread <n> [index <n>] "
674 "[proto <proto>] [state <state>] [range <min> [<max>]] "
675 "[protos] [states] ",
Florin Corase04c2992017-03-01 08:17:34 -0800676 .function = show_session_command_fn,
677};
678/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500679
Dave Barach2c25a622017-06-26 11:35:07 -0400680static int
Florin Coras288eaab2019-02-03 15:26:14 -0800681clear_session (session_t * s)
Dave Barach2c25a622017-06-26 11:35:07 -0400682{
Florin Coras15531972018-08-12 23:50:53 -0700683 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -0700684 app_worker_close_notify (server_wrk, s);
Dave Barach2c25a622017-06-26 11:35:07 -0400685 return 0;
686}
687
Dave Barach68b0fb02017-02-28 15:15:56 -0500688static clib_error_t *
689clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
690 vlib_cli_command_t * cmd)
691{
Florin Coras31c99552019-03-01 13:00:58 -0800692 session_main_t *smm = &session_main;
Dave Barach2c25a622017-06-26 11:35:07 -0400693 u32 thread_index = 0, clear_all = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800694 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500695 u32 session_index = ~0;
Florin Coras288eaab2019-02-03 15:26:14 -0800696 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500697
Florin Corase04c2992017-03-01 08:17:34 -0800698 if (!smm->is_enabled)
699 {
flyingeagle23e2125062017-04-20 20:01:14 +0800700 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800701 }
702
Dave Barach68b0fb02017-02-28 15:15:56 -0500703 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
704 {
705 if (unformat (input, "thread %d", &thread_index))
706 ;
707 else if (unformat (input, "session %d", &session_index))
708 ;
Dave Barach2c25a622017-06-26 11:35:07 -0400709 else if (unformat (input, "all"))
710 clear_all = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500711 else
712 return clib_error_return (0, "unknown input `%U'",
713 format_unformat_error, input);
714 }
715
Dave Barach2c25a622017-06-26 11:35:07 -0400716 if (!clear_all && session_index == ~0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500717 return clib_error_return (0, "session <nn> required, but not set.");
718
Dave Barach2c25a622017-06-26 11:35:07 -0400719 if (session_index != ~0)
720 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700721 session = session_get_if_valid (session_index, thread_index);
Dave Barach2c25a622017-06-26 11:35:07 -0400722 if (!session)
723 return clib_error_return (0, "no session %d on thread %d",
724 session_index, thread_index);
725 clear_session (session);
726 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500727
Dave Barach2c25a622017-06-26 11:35:07 -0400728 if (clear_all)
729 {
730 /* *INDENT-OFF* */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700731 vec_foreach (wrk, smm->wrk)
Dave Barach2c25a622017-06-26 11:35:07 -0400732 {
Damjan Marionb2c31b62020-12-13 21:47:40 +0100733 pool_foreach (session, wrk->sessions) {
Dave Barach2c25a622017-06-26 11:35:07 -0400734 clear_session (session);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100735 }
Dave Barach2c25a622017-06-26 11:35:07 -0400736 };
737 /* *INDENT-ON* */
738 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500739
740 return 0;
741}
742
Florin Corase04c2992017-03-01 08:17:34 -0800743/* *INDENT-OFF* */
744VLIB_CLI_COMMAND (clear_session_command, static) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500745{
Florin Corase04c2992017-03-01 08:17:34 -0800746 .path = "clear session",
747 .short_help = "clear session thread <thread> session <index>",
748 .function = clear_session_command_fn,
749};
750/* *INDENT-ON* */
751
752static clib_error_t *
Florin Coras3eb50622017-07-13 01:24:57 -0400753show_session_fifo_trace_command_fn (vlib_main_t * vm,
754 unformat_input_t * input,
755 vlib_cli_command_t * cmd)
756{
Florin Coras288eaab2019-02-03 15:26:14 -0800757 session_t *s = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400758 u8 is_rx = 0, *str = 0;
759
760 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
761 {
Florin Coras31c99552019-03-01 13:00:58 -0800762 if (unformat (input, "%U", unformat_session, &s))
Florin Coras3eb50622017-07-13 01:24:57 -0400763 ;
764 else if (unformat (input, "rx"))
765 is_rx = 1;
766 else if (unformat (input, "tx"))
767 is_rx = 0;
768 else
769 return clib_error_return (0, "unknown input `%U'",
770 format_unformat_error, input);
771 }
772
773 if (!SVM_FIFO_TRACE)
774 {
775 vlib_cli_output (vm, "fifo tracing not enabled");
776 return 0;
777 }
778
779 if (!s)
780 {
781 vlib_cli_output (vm, "could not find session");
782 return 0;
783 }
784
785 str = is_rx ?
Florin Coras288eaab2019-02-03 15:26:14 -0800786 svm_fifo_dump_trace (str, s->rx_fifo) :
787 svm_fifo_dump_trace (str, s->tx_fifo);
Florin Coras3eb50622017-07-13 01:24:57 -0400788
789 vlib_cli_output (vm, "%v", str);
790 return 0;
791}
792
793/* *INDENT-OFF* */
794VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
795{
796 .path = "show session fifo trace",
797 .short_help = "show session fifo trace <session>",
798 .function = show_session_fifo_trace_command_fn,
799};
800/* *INDENT-ON* */
801
802static clib_error_t *
803session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
804 vlib_cli_command_t * cmd)
805{
Florin Coras288eaab2019-02-03 15:26:14 -0800806 session_t *s = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400807 u8 is_rx = 0, *str = 0;
808
809 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
810 {
Florin Coras31c99552019-03-01 13:00:58 -0800811 if (unformat (input, "%U", unformat_session, &s))
Florin Coras3eb50622017-07-13 01:24:57 -0400812 ;
813 else if (unformat (input, "rx"))
814 is_rx = 1;
815 else
816 return clib_error_return (0, "unknown input `%U'",
817 format_unformat_error, input);
818 }
819
820 if (!SVM_FIFO_TRACE)
821 {
822 vlib_cli_output (vm, "fifo tracing not enabled");
823 return 0;
824 }
825
826 if (!s)
827 {
828 vlib_cli_output (vm, "could not find session");
829 return 0;
830 }
831
832 str = is_rx ?
Florin Coras288eaab2019-02-03 15:26:14 -0800833 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
834 svm_fifo_replay (str, s->tx_fifo, 0, 1);
Florin Coras3eb50622017-07-13 01:24:57 -0400835
836 vlib_cli_output (vm, "%v", str);
837 return 0;
838}
839
840/* *INDENT-OFF* */
841VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
842{
843 .path = "session replay fifo",
844 .short_help = "session replay fifo <session>",
845 .function = session_replay_fifo_command_fn,
846};
847/* *INDENT-ON* */
848
849static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800850session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
851 vlib_cli_command_t * cmd)
852{
Dave Wallace8af20542017-10-26 03:29:30 -0400853 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase04c2992017-03-01 08:17:34 -0800854 u8 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400855 clib_error_t *error;
Florin Corase04c2992017-03-01 08:17:34 -0800856
Dave Wallace8af20542017-10-26 03:29:30 -0400857 if (!unformat_user (input, unformat_line_input, line_input))
Swarup Nayak82d8ec22017-12-04 11:54:43 +0530858 return clib_error_return (0, "expected enable | disable");
Dave Wallace8af20542017-10-26 03:29:30 -0400859
860 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corase04c2992017-03-01 08:17:34 -0800861 {
Dave Wallace8af20542017-10-26 03:29:30 -0400862 if (unformat (line_input, "enable"))
Florin Corase04c2992017-03-01 08:17:34 -0800863 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400864 else if (unformat (line_input, "disable"))
Florin Corase04c2992017-03-01 08:17:34 -0800865 is_en = 0;
866 else
Dave Wallace8af20542017-10-26 03:29:30 -0400867 {
868 error = clib_error_return (0, "unknown input `%U'",
869 format_unformat_error, line_input);
870 unformat_free (line_input);
871 return error;
872 }
Florin Corase04c2992017-03-01 08:17:34 -0800873 }
874
Dave Wallace8af20542017-10-26 03:29:30 -0400875 unformat_free (line_input);
Florin Corase04c2992017-03-01 08:17:34 -0800876 return vnet_session_enable_disable (vm, is_en);
877}
878
879/* *INDENT-OFF* */
880VLIB_CLI_COMMAND (session_enable_disable_command, static) =
881{
882 .path = "session",
883 .short_help = "session [enable|disable]",
884 .function = session_enable_disable_fn,
885};
886/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500887
Dave Barach68b0fb02017-02-28 15:15:56 -0500888/*
889 * fd.io coding-style-patch-verification: ON
890 *
891 * Local Variables:
892 * eval: (c-set-style "gnu")
893 * End:
894 */