blob: aba1c610e183e9416702eeec293171c08b36d3ca [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);
30 if (verbose > 2 && ss->rx_fifo->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);
37 if (verbose > 2 && ss->tx_fifo->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 {
116 u8 post_accept = ss->session_state >= SESSION_STATE_ACCEPTING;
Aloys Augustin95dd1fe2019-07-12 17:11:04 +0200117 u8 hasf = post_accept
118 || session_transport_service_type (ss) == TRANSPORT_SERVICE_CL;
Florin Coras3389dd22019-02-01 18:00:05 -0800119 u32 rxf, txf;
120
Florin Coras288eaab2019-02-03 15:26:14 -0800121 rxf = hasf ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
122 txf = hasf ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
Florin Coras3389dd22019-02-01 18:00:05 -0800123 str = format (0, "%-10u%-10u", rxf, txf);
124 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500125
Florin Coras509028a2019-04-02 17:47:03 -0700126 if (ss->session_state >= SESSION_STATE_ACCEPTING
127 || ss->session_state == SESSION_STATE_CREATED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500128 {
Florin Corasde9a8492018-10-24 22:18:58 -0700129 s = format (s, "%U", format_transport_connection, tp,
130 ss->connection_index, ss->thread_index, verbose);
Florin Corasbb292f42017-05-19 09:49:19 -0700131 if (verbose == 1)
132 s = format (s, "%v", str);
Florin Coras93992a92017-05-24 18:03:56 -0700133 if (verbose > 1)
Florin Coras91f90d02019-10-23 19:28:08 -0700134 {
135 s = format (s, "%U", format_session_fifos, ss, verbose);
Florin Corasd9035a42019-11-19 18:22:41 -0800136 s = format (s, " session: state: %U opaque: 0x%x flags: %U\n",
137 format_session_state, ss, ss->opaque,
138 format_session_flags, ss);
Florin Coras91f90d02019-10-23 19:28:08 -0700139 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500140 }
141 else if (ss->session_state == SESSION_STATE_LISTENING)
142 {
Florin Coras3389dd22019-02-01 18:00:05 -0800143 s = format (s, "%U%v", format_transport_listen_connection,
Aloys Augustina0abbff2019-07-12 12:16:16 +0200144 tp, ss->connection_index, ss->thread_index, verbose, str);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700145 if (verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -0700146 s = format (s, "\n%U", format_session_fifos, ss, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -0500147 }
Florin Corasbb292f42017-05-19 09:49:19 -0700148 else if (ss->session_state == SESSION_STATE_CONNECTING)
Dave Barach68b0fb02017-02-28 15:15:56 -0500149 {
Florin Corasde9a8492018-10-24 22:18:58 -0700150 s = format (s, "%-40U%v", format_transport_half_open_connection,
Aloys Augustina0abbff2019-07-12 12:16:16 +0200151 tp, ss->connection_index, ss->thread_index, str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500152 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500153 else
154 {
Florin Corase04c2992017-03-01 08:17:34 -0800155 clib_warning ("Session in state: %d!", ss->session_state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500156 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500157 vec_free (str);
158
159 return s;
160}
161
Florin Coras3eb50622017-07-13 01:24:57 -0400162uword
163unformat_stream_session_id (unformat_input_t * input, va_list * args)
164{
165 u8 *proto = va_arg (*args, u8 *);
Florin Corasbaee8d42019-01-19 11:29:18 -0800166 u32 *fib_index = va_arg (*args, u32 *);
Florin Coras3eb50622017-07-13 01:24:57 -0400167 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
168 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
169 u16 *lcl_port = va_arg (*args, u16 *);
170 u16 *rmt_port = va_arg (*args, u16 *);
171 u8 *is_ip4 = va_arg (*args, u8 *);
172 u8 tuple_is_set = 0;
Florin Corasbaee8d42019-01-19 11:29:18 -0800173 u32 vrf = ~0;
Florin Coras3eb50622017-07-13 01:24:57 -0400174
Dave Barachb7b92992018-10-17 10:38:51 -0400175 clib_memset (lcl, 0, sizeof (*lcl));
176 clib_memset (rmt, 0, sizeof (*rmt));
Florin Coras3eb50622017-07-13 01:24:57 -0400177
178 if (unformat (input, "tcp"))
179 {
180 *proto = TRANSPORT_PROTO_TCP;
181 }
Florin Corasbaee8d42019-01-19 11:29:18 -0800182 else if (unformat (input, "udp"))
Florin Coras3eb50622017-07-13 01:24:57 -0400183 {
184 *proto = TRANSPORT_PROTO_UDP;
185 }
Florin Corasbaee8d42019-01-19 11:29:18 -0800186 else
187 return 0;
188
189 if (unformat (input, "vrf %u", &vrf))
190 ;
191
Dave Barachb7f1faa2017-08-29 11:43:37 -0400192 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
193 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
Florin Coras3eb50622017-07-13 01:24:57 -0400194 {
195 *is_ip4 = 1;
196 tuple_is_set = 1;
197 }
198 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
199 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
200 {
201 *is_ip4 = 0;
202 tuple_is_set = 1;
203 }
Florin Coras3eb50622017-07-13 01:24:57 -0400204
Florin Corasbaee8d42019-01-19 11:29:18 -0800205 if (vrf != ~0)
206 {
207 fib_protocol_t fib_proto;
208 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
209 *fib_index = fib_table_find (fib_proto, vrf);
210 }
211
Chris Lukeb2bcad62017-09-18 08:51:22 -0400212 return tuple_is_set;
Florin Coras3eb50622017-07-13 01:24:57 -0400213}
214
215uword
Florin Coras5bb23ec2019-08-31 09:45:13 -0700216unformat_session_state (unformat_input_t * input, va_list * args)
217{
218 session_state_t *state = va_arg (*args, session_state_t *);
219 u8 *state_vec = 0;
220 int rv = 0;
221
222#define _(sym, str) \
223 if (unformat (input, str)) \
224 { \
225 *state = SESSION_STATE_ ## sym; \
226 rv = 1; \
227 goto done; \
228 }
229 foreach_session_state
230#undef _
231done:
232 vec_free (state_vec);
233 return rv;
234}
235
236uword
Florin Coras31c99552019-03-01 13:00:58 -0800237unformat_session (unformat_input_t * input, va_list * args)
Florin Coras3eb50622017-07-13 01:24:57 -0400238{
Florin Coras288eaab2019-02-03 15:26:14 -0800239 session_t **result = va_arg (*args, session_t **);
Florin Corasbaee8d42019-01-19 11:29:18 -0800240 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
241 ip46_address_t lcl, rmt;
Florin Coras288eaab2019-02-03 15:26:14 -0800242 session_t *s;
Florin Coras3eb50622017-07-13 01:24:57 -0400243 u8 proto = ~0;
Florin Corascea194d2017-10-02 00:18:51 -0700244 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400245
Florin Corasbaee8d42019-01-19 11:29:18 -0800246 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
247 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400248 return 0;
249
Florin Coras3eb50622017-07-13 01:24:57 -0400250 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700251 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
252 clib_host_to_net_u16 (lcl_port),
253 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400254 else
Florin Coras3cbc04b2017-10-02 00:18:51 -0700255 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
256 clib_host_to_net_u16 (lcl_port),
257 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400258 if (s)
259 {
260 *result = s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700261 session_pool_remove_peeker (s->thread_index);
Florin Coras3eb50622017-07-13 01:24:57 -0400262 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)
335 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
336 thread_index ? "\n" : "",
337 "Connection", "State", "Rx-f", "Tx-f");
338
Florin Coras2d0b2bb2019-11-01 14:03:28 -0700339 n_closed = 0;
340
Florin Coras5bb23ec2019-08-31 09:45:13 -0700341 /* *INDENT-OFF* */
342 pool_foreach(s, pool, ({
343 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
344 {
345 n_closed += 1;
346 continue;
347 }
348 vlib_cli_output (vm, "%U", format_session, s, verbose);
349 }));
350 /* *INDENT-ON* */
351
352 if (!n_closed)
353 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
354 pool_elts (pool) - n_closed);
355 else
356 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
357 thread_index, pool_elts (pool) - n_closed, n_closed);
358 }
359}
360
361static int
362session_cli_filter_check (session_t * s, session_state_t * states,
363 transport_proto_t tp)
364{
365 if (states)
366 {
367 session_state_t *state;
368 vec_foreach (state, states) if (s->session_state == *state)
369 goto check_transport;
370 return 0;
371 }
372
373check_transport:
374
375 if (tp != TRANSPORT_N_PROTO && session_get_transport_proto (s) != tp)
376 return 0;
377
378 return 1;
379}
380
381static void
382session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
383 u32 start, u32 end, session_state_t * states,
384 transport_proto_t tp, int verbose)
385{
386 u8 output_suppressed = 0;
387 session_worker_t *wrk;
388 session_t *pool, *s;
389 u32 count = 0, max_index;
390 int i;
391
392 wrk = session_main_get_worker_if_valid (thread_index);
393 if (!wrk)
394 {
395 vlib_cli_output (vm, "invalid thread index %u", thread_index);
396 return;
397 }
398
399 pool = wrk->sessions;
400
401 if (tp == TRANSPORT_N_PROTO && states == 0 && !verbose
402 && (start == 0 && end == ~0))
403 {
404 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
405 pool_elts (pool));
406 return;
407 }
408
Florin Coras61a89f22019-09-09 19:22:36 -0700409 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700410 for (i = start; i <= clib_min (end, max_index); i++)
411 {
412 if (pool_is_free_index (pool, i))
413 continue;
414
415 s = pool_elt_at_index (pool, i);
416
417 if (session_cli_filter_check (s, states, tp))
418 {
419 count += 1;
420 if (verbose)
421 {
422 if (count > 50 || (verbose > 1 && count > 10))
423 {
424 output_suppressed = 1;
425 continue;
426 }
427 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
428 vlib_cli_output (vm, "%U", format_session, s, verbose);
429 }
430 }
431 }
432
433 if (!output_suppressed)
434 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
435 thread_index, count);
436 else
437 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
438 " shown. Use finer grained filter.", thread_index,
439 count);
440}
441
Florin Coras16d974e2020-02-09 20:03:12 +0000442void
443session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
444{
445 session_worker_t *wrk;
446
447 wrk = session_main_get_worker_if_valid (thread_index);
448 if (!wrk)
449 {
450 vlib_cli_output (vm, "invalid thread index %u", thread_index);
451 return;
452 }
453
454 vlib_cli_output (vm, "Thread %d:\n", thread_index);
455 vlib_cli_output (vm, " evt elements alloc: %u",
456 pool_elts (wrk->event_elts));
457 vlib_cli_output (vm, " ctrl evt elt data alloc: %d",
458 pool_elts (wrk->ctrl_evts_data));
459}
460
461static void
462session_cli_show_events (vlib_main_t * vm, u32 thread_index)
463{
464 session_main_t *smm = &session_main;
465 if (!thread_index)
466 {
467 session_cli_show_events_thread (vm, thread_index);
468 return;
469 }
470
471 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
472 session_cli_show_events_thread (vm, thread_index);
473}
474
Florin Coras5bb23ec2019-08-31 09:45:13 -0700475static void
476session_cli_print_transport_protos (vlib_main_t * vm)
477{
478#define _(sym, str, sstr) vlib_cli_output (vm, str);
479 foreach_transport_proto
480#undef _
481}
482
483static void
484session_cli_print_session_states (vlib_main_t * vm)
485{
486#define _(sym, str) vlib_cli_output (vm, str);
487 foreach_session_state
488#undef _
489}
490
Dave Barach68b0fb02017-02-28 15:15:56 -0500491static clib_error_t *
492show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
493 vlib_cli_command_t * cmd)
494{
Florin Coras5bb23ec2019-08-31 09:45:13 -0700495 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
496 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
497 unformat_input_t _line_input, *line_input = &_line_input;
498 transport_proto_t transport_proto = TRANSPORT_N_PROTO;
499 session_state_t state = SESSION_N_STATES, *states = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800500 session_main_t *smm = &session_main;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700501 clib_error_t *error = 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800502 app_worker_t *app_wrk;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700503 u32 transport_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800504 const u8 *app_name;
Florin Coras16d974e2020-02-09 20:03:12 +0000505 u8 do_events = 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700506 int verbose = 0;
507 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500508
Florin Coras5bb23ec2019-08-31 09:45:13 -0700509 session_cli_return_if_not_enabled ();
510
511 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase04c2992017-03-01 08:17:34 -0800512 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700513 session_cli_show_all_sessions (vm, 0);
514 return 0;
Florin Corase04c2992017-03-01 08:17:34 -0800515 }
516
Florin Coras5bb23ec2019-08-31 09:45:13 -0700517 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Dave Barach68b0fb02017-02-28 15:15:56 -0500518 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700519 if (unformat (line_input, "verbose %d", &verbose))
Florin Corasbb292f42017-05-19 09:49:19 -0700520 ;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700521 else if (unformat (line_input, "verbose"))
Dave Barach68b0fb02017-02-28 15:15:56 -0500522 verbose = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700523 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
Florin Corasdbd44562017-11-09 19:30:17 -0800524 &transport_proto))
525 do_listeners = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700526 else if (unformat (line_input, "%U", unformat_session, &s))
Dave Barach2c25a622017-06-26 11:35:07 -0400527 {
528 one_session = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400529 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700530 else if (unformat (line_input, "thread %u index %u", &thread_index,
531 &session_index))
532 {
533 s = session_get_if_valid (session_index, thread_index);
534 if (!s)
535 {
536 vlib_cli_output (vm, "session is not allocated");
537 goto done;
538 }
539 one_session = 1;
540 }
541 else if (unformat (line_input, "thread %u", &thread_index))
542 {
543 do_filter = 1;
544 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 else
Florin Coras5bb23ec2019-08-31 09:45:13 -0700546 if (unformat (line_input, "state %U", unformat_session_state, &state))
547 {
548 vec_add1 (states, state);
549 do_filter = 1;
550 }
551 else if (unformat (line_input, "proto %U index %u",
552 unformat_transport_proto, &transport_proto,
553 &transport_index))
554 {
555 transport_connection_t *tc;
556 tc = transport_get_connection (transport_proto, transport_index,
557 thread_index);
558 if (!tc)
559 {
560 vlib_cli_output (vm, "transport connection %u thread %u is not"
561 " allocated", transport_index, thread_index);
562 goto done;
563 }
564 s = session_get_if_valid (tc->s_index, thread_index);
565 if (!s)
566 {
567 vlib_cli_output (vm, "session for transport connection %u "
568 "thread %u does not exist", transport_index,
569 thread_index);
570 goto done;
571 }
572 one_session = 1;
573 }
574 else if (unformat (line_input, "proto %U", unformat_transport_proto,
575 &transport_proto))
576 do_filter = 1;
577 else if (unformat (line_input, "range %u %u", &start, &end))
578 do_filter = 1;
579 else if (unformat (line_input, "range %u", &start))
580 {
581 end = start + 50;
582 do_filter = 1;
583 }
584 else if (unformat (line_input, "elog"))
585 do_elog = 1;
586 else if (unformat (line_input, "protos"))
587 {
588 session_cli_print_transport_protos (vm);
589 goto done;
590 }
591 else if (unformat (line_input, "states"))
592 {
593 session_cli_print_session_states (vm);
594 goto done;
595 }
Florin Coras16d974e2020-02-09 20:03:12 +0000596 else if (unformat (line_input, "events"))
597 do_events = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700598 else
599 {
600 error = clib_error_return (0, "unknown input `%U'",
601 format_unformat_error, line_input);
602 goto done;
603 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500604 }
605
Dave Barach2c25a622017-06-26 11:35:07 -0400606 if (one_session)
607 {
Florin Coras31c99552019-03-01 13:00:58 -0800608 u8 *str = format (0, "%U", format_session, s, 3);
Florin Coras844a36d2018-12-20 09:50:50 -0800609 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
Florin Corasaefbede2018-12-19 13:07:49 -0800610 {
611 elog_main_t *em = &vm->elog_main;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700612 transport_connection_t *tc;
Florin Corasaefbede2018-12-19 13:07:49 -0800613 f64 dt;
614
615 tc = session_get_transport (s);
616 track_index = transport_elog_track_index (tc);
617 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
618 * vm->clib_time.seconds_per_clock;
619 if (track_index != ~0)
620 str = format (str, " session elog:\n%U", format_elog_track, em,
621 dt, track_index);
622 }
623 vlib_cli_output (vm, "%v", str);
Florin Coras3389dd22019-02-01 18:00:05 -0800624 vec_free (str);
Florin Coras5bb23ec2019-08-31 09:45:13 -0700625 goto done;
Dave Barach2c25a622017-06-26 11:35:07 -0400626 }
627
Florin Corasdbd44562017-11-09 19:30:17 -0800628 if (do_listeners)
629 {
630 sst = session_type_from_proto_and_ip (transport_proto, 1);
Florin Coras3389dd22019-02-01 18:00:05 -0800631 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
Florin Coras5c9083d2018-04-13 06:39:07 -0700632 /* *INDENT-OFF* */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700633 pool_foreach (s, smm->wrk[0].sessions, ({
Florin Coras5c9083d2018-04-13 06:39:07 -0700634 if (s->session_state != SESSION_STATE_LISTENING
635 || s->session_type != sst)
636 continue;
Florin Coras053a0e42018-11-13 15:52:38 -0800637 app_wrk = app_worker_get (s->app_wrk_index);
638 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras31c99552019-03-01 13:00:58 -0800639 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
Florin Corasde9a8492018-10-24 22:18:58 -0700640 app_name);
Florin Coras5c9083d2018-04-13 06:39:07 -0700641 }));
642 /* *INDENT-ON* */
Florin Coras5bb23ec2019-08-31 09:45:13 -0700643 goto done;
Florin Corasdbd44562017-11-09 19:30:17 -0800644 }
645
Florin Coras16d974e2020-02-09 20:03:12 +0000646 if (do_events)
647 {
648 session_cli_show_events (vm, thread_index);
649 goto done;
650 }
651
Florin Coras5bb23ec2019-08-31 09:45:13 -0700652 if (do_filter)
Dave Barach68b0fb02017-02-28 15:15:56 -0500653 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700654 if (end < start)
Dave Barach68b0fb02017-02-28 15:15:56 -0500655 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700656 error = clib_error_return (0, "invalid range start: %u end: %u",
657 start, end);
658 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700660 session_cli_show_session_filter (vm, thread_index, start, end, states,
661 transport_proto, verbose);
662 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500663 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500664
Florin Coras5bb23ec2019-08-31 09:45:13 -0700665 session_cli_show_all_sessions (vm, verbose);
666
667done:
668 unformat_free (line_input);
669 vec_free (states);
670 return error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500671}
672
Florin Corase04c2992017-03-01 08:17:34 -0800673/* *INDENT-OFF* */
Florin Coras66b11312017-07-31 17:18:03 -0700674VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500675{
Florin Corase04c2992017-03-01 08:17:34 -0800676 .path = "show session",
Florin Coras3389dd22019-02-01 18:00:05 -0800677 .short_help = "show session [verbose [n]] [listeners <proto>] "
Florin Coras5bb23ec2019-08-31 09:45:13 -0700678 "[<session-id> [elog]] [thread <n> [index <n>] "
679 "[proto <proto>] [state <state>] [range <min> [<max>]] "
680 "[protos] [states] ",
Florin Corase04c2992017-03-01 08:17:34 -0800681 .function = show_session_command_fn,
682};
683/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500684
Dave Barach2c25a622017-06-26 11:35:07 -0400685static int
Florin Coras288eaab2019-02-03 15:26:14 -0800686clear_session (session_t * s)
Dave Barach2c25a622017-06-26 11:35:07 -0400687{
Florin Coras15531972018-08-12 23:50:53 -0700688 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -0700689 app_worker_close_notify (server_wrk, s);
Dave Barach2c25a622017-06-26 11:35:07 -0400690 return 0;
691}
692
Dave Barach68b0fb02017-02-28 15:15:56 -0500693static clib_error_t *
694clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
695 vlib_cli_command_t * cmd)
696{
Florin Coras31c99552019-03-01 13:00:58 -0800697 session_main_t *smm = &session_main;
Dave Barach2c25a622017-06-26 11:35:07 -0400698 u32 thread_index = 0, clear_all = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800699 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500700 u32 session_index = ~0;
Florin Coras288eaab2019-02-03 15:26:14 -0800701 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500702
Florin Corase04c2992017-03-01 08:17:34 -0800703 if (!smm->is_enabled)
704 {
flyingeagle23e2125062017-04-20 20:01:14 +0800705 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800706 }
707
Dave Barach68b0fb02017-02-28 15:15:56 -0500708 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
709 {
710 if (unformat (input, "thread %d", &thread_index))
711 ;
712 else if (unformat (input, "session %d", &session_index))
713 ;
Dave Barach2c25a622017-06-26 11:35:07 -0400714 else if (unformat (input, "all"))
715 clear_all = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500716 else
717 return clib_error_return (0, "unknown input `%U'",
718 format_unformat_error, input);
719 }
720
Dave Barach2c25a622017-06-26 11:35:07 -0400721 if (!clear_all && session_index == ~0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500722 return clib_error_return (0, "session <nn> required, but not set.");
723
Dave Barach2c25a622017-06-26 11:35:07 -0400724 if (session_index != ~0)
725 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700726 session = session_get_if_valid (session_index, thread_index);
Dave Barach2c25a622017-06-26 11:35:07 -0400727 if (!session)
728 return clib_error_return (0, "no session %d on thread %d",
729 session_index, thread_index);
730 clear_session (session);
731 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500732
Dave Barach2c25a622017-06-26 11:35:07 -0400733 if (clear_all)
734 {
735 /* *INDENT-OFF* */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700736 vec_foreach (wrk, smm->wrk)
Dave Barach2c25a622017-06-26 11:35:07 -0400737 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700738 pool_foreach(session, wrk->sessions, ({
Dave Barach2c25a622017-06-26 11:35:07 -0400739 clear_session (session);
740 }));
741 };
742 /* *INDENT-ON* */
743 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500744
745 return 0;
746}
747
Florin Corase04c2992017-03-01 08:17:34 -0800748/* *INDENT-OFF* */
749VLIB_CLI_COMMAND (clear_session_command, static) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500750{
Florin Corase04c2992017-03-01 08:17:34 -0800751 .path = "clear session",
752 .short_help = "clear session thread <thread> session <index>",
753 .function = clear_session_command_fn,
754};
755/* *INDENT-ON* */
756
757static clib_error_t *
Florin Coras3eb50622017-07-13 01:24:57 -0400758show_session_fifo_trace_command_fn (vlib_main_t * vm,
759 unformat_input_t * input,
760 vlib_cli_command_t * cmd)
761{
Florin Coras288eaab2019-02-03 15:26:14 -0800762 session_t *s = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400763 u8 is_rx = 0, *str = 0;
764
765 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
766 {
Florin Coras31c99552019-03-01 13:00:58 -0800767 if (unformat (input, "%U", unformat_session, &s))
Florin Coras3eb50622017-07-13 01:24:57 -0400768 ;
769 else if (unformat (input, "rx"))
770 is_rx = 1;
771 else if (unformat (input, "tx"))
772 is_rx = 0;
773 else
774 return clib_error_return (0, "unknown input `%U'",
775 format_unformat_error, input);
776 }
777
778 if (!SVM_FIFO_TRACE)
779 {
780 vlib_cli_output (vm, "fifo tracing not enabled");
781 return 0;
782 }
783
784 if (!s)
785 {
786 vlib_cli_output (vm, "could not find session");
787 return 0;
788 }
789
790 str = is_rx ?
Florin Coras288eaab2019-02-03 15:26:14 -0800791 svm_fifo_dump_trace (str, s->rx_fifo) :
792 svm_fifo_dump_trace (str, s->tx_fifo);
Florin Coras3eb50622017-07-13 01:24:57 -0400793
794 vlib_cli_output (vm, "%v", str);
795 return 0;
796}
797
798/* *INDENT-OFF* */
799VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
800{
801 .path = "show session fifo trace",
802 .short_help = "show session fifo trace <session>",
803 .function = show_session_fifo_trace_command_fn,
804};
805/* *INDENT-ON* */
806
807static clib_error_t *
808session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
809 vlib_cli_command_t * cmd)
810{
Florin Coras288eaab2019-02-03 15:26:14 -0800811 session_t *s = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400812 u8 is_rx = 0, *str = 0;
813
814 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
815 {
Florin Coras31c99552019-03-01 13:00:58 -0800816 if (unformat (input, "%U", unformat_session, &s))
Florin Coras3eb50622017-07-13 01:24:57 -0400817 ;
818 else if (unformat (input, "rx"))
819 is_rx = 1;
820 else
821 return clib_error_return (0, "unknown input `%U'",
822 format_unformat_error, input);
823 }
824
825 if (!SVM_FIFO_TRACE)
826 {
827 vlib_cli_output (vm, "fifo tracing not enabled");
828 return 0;
829 }
830
831 if (!s)
832 {
833 vlib_cli_output (vm, "could not find session");
834 return 0;
835 }
836
837 str = is_rx ?
Florin Coras288eaab2019-02-03 15:26:14 -0800838 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
839 svm_fifo_replay (str, s->tx_fifo, 0, 1);
Florin Coras3eb50622017-07-13 01:24:57 -0400840
841 vlib_cli_output (vm, "%v", str);
842 return 0;
843}
844
845/* *INDENT-OFF* */
846VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
847{
848 .path = "session replay fifo",
849 .short_help = "session replay fifo <session>",
850 .function = session_replay_fifo_command_fn,
851};
852/* *INDENT-ON* */
853
854static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800855session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
856 vlib_cli_command_t * cmd)
857{
Dave Wallace8af20542017-10-26 03:29:30 -0400858 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase04c2992017-03-01 08:17:34 -0800859 u8 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400860 clib_error_t *error;
Florin Corase04c2992017-03-01 08:17:34 -0800861
Dave Wallace8af20542017-10-26 03:29:30 -0400862 if (!unformat_user (input, unformat_line_input, line_input))
Swarup Nayak82d8ec22017-12-04 11:54:43 +0530863 return clib_error_return (0, "expected enable | disable");
Dave Wallace8af20542017-10-26 03:29:30 -0400864
865 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corase04c2992017-03-01 08:17:34 -0800866 {
Dave Wallace8af20542017-10-26 03:29:30 -0400867 if (unformat (line_input, "enable"))
Florin Corase04c2992017-03-01 08:17:34 -0800868 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400869 else if (unformat (line_input, "disable"))
Florin Corase04c2992017-03-01 08:17:34 -0800870 is_en = 0;
871 else
Dave Wallace8af20542017-10-26 03:29:30 -0400872 {
873 error = clib_error_return (0, "unknown input `%U'",
874 format_unformat_error, line_input);
875 unformat_free (line_input);
876 return error;
877 }
Florin Corase04c2992017-03-01 08:17:34 -0800878 }
879
Dave Wallace8af20542017-10-26 03:29:30 -0400880 unformat_free (line_input);
Florin Corase04c2992017-03-01 08:17:34 -0800881 return vnet_session_enable_disable (vm, is_en);
882}
883
884/* *INDENT-OFF* */
885VLIB_CLI_COMMAND (session_enable_disable_command, static) =
886{
887 .path = "session",
888 .short_help = "session [enable|disable]",
889 .function = session_enable_disable_fn,
890};
891/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500892
893/*
894 * fd.io coding-style-patch-verification: ON
895 *
896 * Local Variables:
897 * eval: (c-set-style "gnu")
898 * End:
899 */