Line data Source code
1 : /*
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:
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 :
18 : u8 *
19 16 : format_session_fifos (u8 * s, va_list * args)
20 : {
21 16 : session_t *ss = va_arg (*args, session_t *);
22 16 : int verbose = va_arg (*args, int);
23 16 : session_event_t _e, *e = &_e;
24 : u8 found;
25 :
26 16 : if (!ss->rx_fifo || !ss->tx_fifo)
27 1 : return s;
28 :
29 15 : s = format (s, " Rx fifo: %U", format_svm_fifo, ss->rx_fifo, verbose);
30 15 : if (verbose > 2 && ss->rx_fifo->shr->has_event)
31 : {
32 0 : found = session_node_lookup_fifo_event (ss->rx_fifo, e);
33 0 : s = format (s, " session node event: %s\n",
34 : found ? "found" : "not found");
35 : }
36 15 : s = format (s, " Tx fifo: %U", format_svm_fifo, ss->tx_fifo, verbose);
37 15 : if (verbose > 2 && ss->tx_fifo->shr->has_event)
38 : {
39 0 : found = session_node_lookup_fifo_event (ss->tx_fifo, e);
40 0 : s = format (s, " session node event: %s\n",
41 : found ? "found" : "not found");
42 : }
43 15 : return s;
44 : }
45 :
46 : const char *session_state_str[] = {
47 : #define _(sym, str) str,
48 : foreach_session_state
49 : #undef _
50 : };
51 :
52 : u8 *
53 15 : format_session_state (u8 * s, va_list * args)
54 : {
55 15 : session_t *ss = va_arg (*args, session_t *);
56 :
57 15 : if (ss->session_state < SESSION_N_STATES)
58 15 : s = format (s, "%s", session_state_str[ss->session_state]);
59 : else
60 0 : s = format (s, "UNKNOWN STATE (%d)", ss->session_state);
61 :
62 15 : return s;
63 : }
64 :
65 : const char *session_flags_str[] = {
66 : #define _(sym, str) str,
67 : foreach_session_flag
68 : #undef _
69 : };
70 :
71 : u8 *
72 15 : format_session_flags (u8 * s, va_list * args)
73 : {
74 15 : session_t *ss = va_arg (*args, session_t *);
75 15 : int i, last = -1;
76 :
77 135 : for (i = 0; i < SESSION_N_FLAGS; i++)
78 120 : if (ss->flags & (1 << i))
79 16 : last = i;
80 :
81 113 : for (i = 0; i < last; i++)
82 : {
83 98 : if (ss->flags & (1 << i))
84 2 : s = format (s, "%s, ", session_flags_str[i]);
85 : }
86 15 : if (last >= 0)
87 14 : s = format (s, "%s", session_flags_str[last]);
88 :
89 15 : return s;
90 : }
91 :
92 : /**
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 : */
100 : u8 *
101 18 : format_session (u8 * s, va_list * args)
102 : {
103 18 : session_t *ss = va_arg (*args, session_t *);
104 18 : int verbose = va_arg (*args, int);
105 18 : u32 tp = session_get_transport_proto (ss);
106 18 : u8 *str = 0;
107 :
108 18 : if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
109 : {
110 0 : s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
111 0 : return s;
112 : }
113 :
114 18 : if (verbose == 1)
115 : {
116 : u32 rxf, txf;
117 :
118 2 : rxf = ss->rx_fifo ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
119 2 : txf = ss->tx_fifo ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
120 2 : str = format (0, "%-10u%-10u", rxf, txf);
121 : }
122 :
123 18 : if (ss->session_state >= SESSION_STATE_ACCEPTING
124 1 : || ss->session_state == SESSION_STATE_CREATED)
125 : {
126 17 : s = format (s, "%U", format_transport_connection, tp,
127 17 : ss->connection_index, ss->thread_index, verbose);
128 17 : if (verbose == 1)
129 2 : s = format (s, "%v", str);
130 17 : if (verbose > 1)
131 : {
132 15 : s = format (s, "%U", format_session_fifos, ss, verbose);
133 15 : s = format (s, " session: state: %U opaque: 0x%x flags: %U\n",
134 : format_session_state, ss, ss->opaque,
135 : format_session_flags, ss);
136 : }
137 : }
138 1 : else if (ss->session_state == SESSION_STATE_LISTENING)
139 : {
140 1 : s = format (s, "%U%v", format_transport_listen_connection,
141 1 : tp, ss->connection_index, ss->thread_index, verbose, str);
142 1 : if (verbose > 1)
143 1 : s = format (s, "\n%U", format_session_fifos, ss, verbose);
144 : }
145 0 : else if (ss->session_state == SESSION_STATE_CONNECTING)
146 : {
147 0 : if (ss->flags & SESSION_F_HALF_OPEN)
148 0 : s = format (s, "%U%v", format_transport_half_open_connection, tp,
149 0 : ss->connection_index, ss->thread_index, verbose, str);
150 : else
151 0 : s = format (s, "%U", format_transport_connection, tp,
152 0 : ss->connection_index, ss->thread_index, verbose);
153 : }
154 : else
155 : {
156 0 : clib_warning ("Session in state: %d!", ss->session_state);
157 : }
158 18 : vec_free (str);
159 :
160 18 : return s;
161 : }
162 :
163 : uword
164 0 : unformat_stream_session_id (unformat_input_t * input, va_list * args)
165 : {
166 0 : u8 *proto = va_arg (*args, u8 *);
167 0 : u32 *fib_index = va_arg (*args, u32 *);
168 0 : ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
169 0 : ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
170 0 : u16 *lcl_port = va_arg (*args, u16 *);
171 0 : u16 *rmt_port = va_arg (*args, u16 *);
172 0 : u8 *is_ip4 = va_arg (*args, u8 *);
173 0 : u8 tuple_is_set = 0;
174 0 : u32 vrf = ~0;
175 :
176 0 : clib_memset (lcl, 0, sizeof (*lcl));
177 0 : clib_memset (rmt, 0, sizeof (*rmt));
178 :
179 0 : if (unformat (input, "tcp"))
180 : {
181 0 : *proto = TRANSPORT_PROTO_TCP;
182 : }
183 0 : else if (unformat (input, "udp"))
184 : {
185 0 : *proto = TRANSPORT_PROTO_UDP;
186 : }
187 : else
188 0 : return 0;
189 :
190 0 : if (unformat (input, "vrf %u", &vrf))
191 : ;
192 :
193 0 : if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
194 : lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
195 : {
196 0 : *is_ip4 = 1;
197 0 : tuple_is_set = 1;
198 : }
199 0 : 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 0 : *is_ip4 = 0;
203 0 : tuple_is_set = 1;
204 : }
205 :
206 0 : if (vrf != ~0)
207 : {
208 : fib_protocol_t fib_proto;
209 0 : fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
210 0 : *fib_index = fib_table_find (fib_proto, vrf);
211 : }
212 :
213 0 : return tuple_is_set;
214 : }
215 :
216 : uword
217 0 : unformat_session_state (unformat_input_t * input, va_list * args)
218 : {
219 0 : session_state_t *state = va_arg (*args, session_state_t *);
220 0 : u8 *state_vec = 0;
221 0 : 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 0 : foreach_session_state
231 : #undef _
232 0 : done:
233 0 : vec_free (state_vec);
234 0 : return rv;
235 : }
236 :
237 : uword
238 0 : unformat_session (unformat_input_t * input, va_list * args)
239 : {
240 0 : session_t **result = va_arg (*args, session_t **);
241 0 : u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
242 : ip46_address_t lcl, rmt;
243 : session_t *s;
244 0 : u8 proto = ~0;
245 0 : u8 is_ip4 = 0;
246 :
247 0 : if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
248 : &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
249 0 : return 0;
250 :
251 0 : if (is_ip4)
252 0 : s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
253 0 : clib_host_to_net_u16 (lcl_port),
254 0 : clib_host_to_net_u16 (rmt_port), proto);
255 : else
256 0 : s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
257 0 : clib_host_to_net_u16 (lcl_port),
258 0 : clib_host_to_net_u16 (rmt_port), proto);
259 0 : if (s)
260 : {
261 0 : *result = s;
262 0 : return 1;
263 : }
264 0 : return 0;
265 : }
266 :
267 : uword
268 0 : unformat_transport_connection (unformat_input_t * input, va_list * args)
269 : {
270 0 : transport_connection_t **result = va_arg (*args, transport_connection_t **);
271 0 : u32 suggested_proto = va_arg (*args, u32);
272 : transport_connection_t *tc;
273 0 : u8 proto = ~0;
274 : ip46_address_t lcl, rmt;
275 0 : u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
276 0 : u8 is_ip4 = 0;
277 :
278 0 : if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
279 : &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
280 0 : return 0;
281 :
282 0 : proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
283 0 : if (proto == (u8) ~ 0)
284 0 : return 0;
285 0 : if (is_ip4)
286 0 : tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
287 0 : clib_host_to_net_u16 (lcl_port),
288 0 : clib_host_to_net_u16 (rmt_port), proto);
289 : else
290 0 : tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
291 0 : clib_host_to_net_u16 (lcl_port),
292 0 : clib_host_to_net_u16 (rmt_port), proto);
293 :
294 0 : if (tc)
295 : {
296 0 : *result = tc;
297 0 : return 1;
298 : }
299 0 : return 0;
300 : }
301 :
302 : static void
303 19 : session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
304 : {
305 19 : session_main_t *smm = &session_main;
306 : u32 n_closed, thread_index;
307 : session_t *pool, *s;
308 :
309 40 : for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
310 : {
311 21 : pool = smm->wrk[thread_index].sessions;
312 :
313 21 : if (!pool_elts (pool))
314 : {
315 10 : vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
316 10 : continue;
317 : }
318 :
319 11 : if (!verbose)
320 : {
321 0 : vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
322 : pool_elts (pool));
323 0 : continue;
324 : }
325 :
326 11 : if (pool_elts (pool) > 50)
327 : {
328 0 : vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
329 : "suppressed. For more details use filters.",
330 : thread_index, pool_elts (pool));
331 0 : continue;
332 : }
333 :
334 11 : if (verbose == 1)
335 2 : vlib_cli_output (vm, "%s%-" SESSION_CLI_ID_LEN "s%-"
336 : SESSION_CLI_STATE_LEN "s%-10s%-10s",
337 : thread_index ? "\n" : "",
338 : "Connection", "State", "Rx-f", "Tx-f");
339 :
340 11 : n_closed = 0;
341 :
342 : /* *INDENT-OFF* */
343 29 : pool_foreach (s, pool) {
344 18 : if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
345 : {
346 0 : n_closed += 1;
347 0 : continue;
348 : }
349 18 : vlib_cli_output (vm, "%U", format_session, s, verbose);
350 : }
351 : /* *INDENT-ON* */
352 :
353 11 : if (!n_closed)
354 11 : vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
355 11 : pool_elts (pool) - n_closed);
356 : else
357 0 : vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
358 0 : thread_index, pool_elts (pool) - n_closed, n_closed);
359 : }
360 19 : }
361 :
362 : static int
363 0 : session_cli_filter_check (session_t * s, session_state_t * states,
364 : transport_proto_t tp)
365 : {
366 0 : if (states)
367 : {
368 : session_state_t *state;
369 0 : vec_foreach (state, states) if (s->session_state == *state)
370 0 : goto check_transport;
371 0 : return 0;
372 : }
373 :
374 0 : check_transport:
375 :
376 0 : if (tp != TRANSPORT_PROTO_INVALID && session_get_transport_proto (s) != tp)
377 0 : return 0;
378 :
379 0 : return 1;
380 : }
381 :
382 : static void
383 0 : session_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 0 : u8 output_suppressed = 0;
388 : session_worker_t *wrk;
389 : session_t *pool, *s;
390 0 : u32 count = 0, max_index;
391 : int i;
392 :
393 0 : wrk = session_main_get_worker_if_valid (thread_index);
394 0 : if (!wrk)
395 : {
396 0 : vlib_cli_output (vm, "invalid thread index %u", thread_index);
397 0 : return;
398 : }
399 :
400 0 : pool = wrk->sessions;
401 :
402 0 : if (tp == TRANSPORT_PROTO_INVALID && states == 0 && !verbose
403 0 : && (start == 0 && end == ~0))
404 : {
405 0 : vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
406 : pool_elts (pool));
407 0 : return;
408 : }
409 :
410 0 : max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
411 0 : for (i = start; i <= clib_min (end, max_index); i++)
412 : {
413 0 : if (pool_is_free_index (pool, i))
414 0 : continue;
415 :
416 0 : s = pool_elt_at_index (pool, i);
417 :
418 0 : if (session_cli_filter_check (s, states, tp))
419 : {
420 0 : count += 1;
421 0 : if (verbose)
422 : {
423 0 : if (count > 50 || (verbose > 1 && count > 10))
424 : {
425 0 : output_suppressed = 1;
426 0 : continue;
427 : }
428 0 : if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
429 0 : vlib_cli_output (vm, "%U", format_session, s, verbose);
430 : }
431 : }
432 : }
433 :
434 0 : if (!output_suppressed)
435 0 : vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
436 : thread_index, count);
437 : else
438 0 : vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
439 : " shown. Use finer grained filter.", thread_index,
440 : count);
441 : }
442 :
443 : void
444 0 : session_cli_show_events_thread (vlib_main_t * vm, u32 thread_index)
445 : {
446 : session_worker_t *wrk;
447 :
448 0 : wrk = session_main_get_worker_if_valid (thread_index);
449 0 : if (!wrk)
450 : {
451 0 : vlib_cli_output (vm, "invalid thread index %u", thread_index);
452 0 : return;
453 : }
454 :
455 0 : vlib_cli_output (vm, "Thread %d:\n", thread_index);
456 0 : vlib_cli_output (vm, " evt elements alloc: %u",
457 0 : clib_llist_elts (wrk->event_elts));
458 0 : vlib_cli_output (vm, " ctrl evt elt data alloc: %d",
459 0 : clib_llist_elts (wrk->ctrl_evts_data));
460 : }
461 :
462 : static void
463 0 : session_cli_show_events (vlib_main_t * vm, u32 thread_index)
464 : {
465 0 : session_main_t *smm = &session_main;
466 0 : if (!thread_index)
467 : {
468 0 : session_cli_show_events_thread (vm, thread_index);
469 0 : return;
470 : }
471 :
472 0 : for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
473 0 : session_cli_show_events_thread (vm, thread_index);
474 : }
475 :
476 : static void
477 0 : session_cli_print_session_states (vlib_main_t * vm)
478 : {
479 : #define _(sym, str) vlib_cli_output (vm, str);
480 0 : foreach_session_state
481 : #undef _
482 0 : }
483 :
484 : static clib_error_t *
485 19 : show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
486 : vlib_cli_command_t * cmd)
487 : {
488 19 : u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
489 19 : u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
490 19 : transport_proto_t transport_proto = TRANSPORT_PROTO_INVALID;
491 19 : session_state_t state = SESSION_N_STATES, *states = 0;
492 19 : session_main_t *smm = &session_main;
493 19 : clib_error_t *error = 0;
494 : app_worker_t *app_wrk;
495 : u32 transport_index;
496 : const u8 *app_name;
497 19 : u8 do_events = 0;
498 19 : int verbose = 0;
499 : session_t *s;
500 :
501 19 : session_cli_return_if_not_enabled ();
502 :
503 38 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
504 : {
505 19 : if (unformat (input, "verbose %d", &verbose))
506 : ;
507 2 : else if (unformat (input, "verbose"))
508 2 : verbose = 1;
509 0 : else if (unformat (input, "listeners %U", unformat_transport_proto,
510 : &transport_proto))
511 0 : do_listeners = 1;
512 0 : else if (unformat (input, "%U", unformat_session, &s))
513 : {
514 0 : one_session = 1;
515 : }
516 0 : else if (unformat (input, "thread %u index %u", &thread_index,
517 : &session_index))
518 : {
519 0 : s = session_get_if_valid (session_index, thread_index);
520 0 : if (!s)
521 : {
522 0 : vlib_cli_output (vm, "session is not allocated");
523 0 : goto done;
524 : }
525 0 : one_session = 1;
526 : }
527 0 : else if (unformat (input, "thread %u", &thread_index))
528 : {
529 0 : do_filter = 1;
530 : }
531 0 : else if (unformat (input, "state %U", unformat_session_state, &state))
532 : {
533 0 : vec_add1 (states, state);
534 0 : do_filter = 1;
535 : }
536 0 : else if (unformat (input, "proto %U index %u", unformat_transport_proto,
537 : &transport_proto, &transport_index))
538 : {
539 : transport_connection_t *tc;
540 0 : tc = transport_get_connection (transport_proto, transport_index,
541 : thread_index);
542 0 : if (!tc)
543 : {
544 0 : vlib_cli_output (vm, "transport connection %u thread %u is not"
545 : " allocated", transport_index, thread_index);
546 0 : goto done;
547 : }
548 0 : s = session_get_if_valid (tc->s_index, thread_index);
549 0 : if (!s)
550 : {
551 0 : vlib_cli_output (vm, "session for transport connection %u "
552 : "thread %u does not exist", transport_index,
553 : thread_index);
554 0 : goto done;
555 : }
556 0 : one_session = 1;
557 : }
558 0 : else if (unformat (input, "proto %U", unformat_transport_proto,
559 : &transport_proto))
560 0 : do_filter = 1;
561 0 : else if (unformat (input, "range %u %u", &start, &end))
562 0 : do_filter = 1;
563 0 : else if (unformat (input, "range %u", &start))
564 : {
565 0 : end = start + 50;
566 0 : do_filter = 1;
567 : }
568 0 : else if (unformat (input, "elog"))
569 0 : do_elog = 1;
570 0 : else if (unformat (input, "protos"))
571 : {
572 0 : vlib_cli_output (vm, "%U", format_transport_protos);
573 0 : goto done;
574 : }
575 0 : else if (unformat (input, "states"))
576 : {
577 0 : session_cli_print_session_states (vm);
578 0 : goto done;
579 : }
580 0 : else if (unformat (input, "events"))
581 0 : do_events = 1;
582 : else
583 : {
584 0 : error = clib_error_return (0, "unknown input `%U'",
585 : format_unformat_error, input);
586 0 : goto done;
587 : }
588 : }
589 :
590 19 : if (one_session)
591 : {
592 0 : u8 *str = format (0, "%U", format_session, s, 3);
593 0 : if (do_elog && s->session_state != SESSION_STATE_LISTENING)
594 : {
595 0 : elog_main_t *em = &vlib_global_main.elog_main;
596 : transport_connection_t *tc;
597 : f64 dt;
598 :
599 0 : tc = session_get_transport (s);
600 0 : track_index = transport_elog_track_index (tc);
601 0 : dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
602 0 : * vm->clib_time.seconds_per_clock;
603 0 : if (track_index != ~0)
604 0 : str = format (str, " session elog:\n%U", format_elog_track, em,
605 : dt, track_index);
606 : }
607 0 : vlib_cli_output (vm, "%v", str);
608 0 : vec_free (str);
609 0 : goto done;
610 : }
611 :
612 19 : if (do_listeners)
613 : {
614 0 : sst = session_type_from_proto_and_ip (transport_proto, 1);
615 0 : vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-24s", "Listener",
616 : "App");
617 :
618 : /* *INDENT-OFF* */
619 0 : pool_foreach (s, smm->wrk[0].sessions) {
620 0 : if (s->session_state != SESSION_STATE_LISTENING
621 0 : || s->session_type != sst)
622 0 : continue;
623 0 : app_wrk = app_worker_get (s->app_wrk_index);
624 0 : app_name = application_name_from_index (app_wrk->app_index);
625 0 : vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
626 : app_name);
627 : }
628 : /* *INDENT-ON* */
629 0 : goto done;
630 : }
631 :
632 19 : if (do_events)
633 : {
634 0 : session_cli_show_events (vm, thread_index);
635 0 : goto done;
636 : }
637 :
638 19 : if (do_filter)
639 : {
640 0 : if (end < start)
641 : {
642 0 : error = clib_error_return (0, "invalid range start: %u end: %u",
643 : start, end);
644 0 : goto done;
645 : }
646 0 : session_cli_show_session_filter (vm, thread_index, start, end, states,
647 : transport_proto, verbose);
648 0 : goto done;
649 : }
650 :
651 19 : session_cli_show_all_sessions (vm, verbose);
652 :
653 19 : done:
654 19 : vec_free (states);
655 19 : return error;
656 : }
657 :
658 : /* *INDENT-OFF* */
659 285289 : VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
660 : {
661 : .path = "show session",
662 : .short_help = "show session [verbose [n]] [listeners <proto>] "
663 : "[<session-id> [elog]] [thread <n> [index <n>] "
664 : "[proto <proto>] [state <state>] [range <min> [<max>]] "
665 : "[protos] [states] ",
666 : .function = show_session_command_fn,
667 : };
668 : /* *INDENT-ON* */
669 :
670 : static int
671 0 : clear_session (session_t * s)
672 : {
673 0 : app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
674 0 : app_worker_close_notify (server_wrk, s);
675 0 : return 0;
676 : }
677 :
678 : static clib_error_t *
679 0 : clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
680 : vlib_cli_command_t * cmd)
681 : {
682 0 : session_main_t *smm = &session_main;
683 0 : u32 thread_index = 0, clear_all = 0;
684 : session_worker_t *wrk;
685 0 : u32 session_index = ~0;
686 : session_t *session;
687 :
688 0 : if (!smm->is_enabled)
689 : {
690 0 : return clib_error_return (0, "session layer is not enabled");
691 : }
692 :
693 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
694 : {
695 0 : if (unformat (input, "thread %d", &thread_index))
696 : ;
697 0 : else if (unformat (input, "session %d", &session_index))
698 : ;
699 0 : else if (unformat (input, "all"))
700 0 : clear_all = 1;
701 : else
702 0 : return clib_error_return (0, "unknown input `%U'",
703 : format_unformat_error, input);
704 : }
705 :
706 0 : if (!clear_all && session_index == ~0)
707 0 : return clib_error_return (0, "session <nn> required, but not set.");
708 :
709 0 : if (session_index != ~0)
710 : {
711 0 : session = session_get_if_valid (session_index, thread_index);
712 0 : if (!session)
713 0 : return clib_error_return (0, "no session %d on thread %d",
714 : session_index, thread_index);
715 0 : clear_session (session);
716 : }
717 :
718 0 : if (clear_all)
719 : {
720 : /* *INDENT-OFF* */
721 0 : vec_foreach (wrk, smm->wrk)
722 : {
723 0 : pool_foreach (session, wrk->sessions) {
724 0 : clear_session (session);
725 : }
726 : };
727 : /* *INDENT-ON* */
728 : }
729 :
730 0 : return 0;
731 : }
732 :
733 : /* *INDENT-OFF* */
734 285289 : VLIB_CLI_COMMAND (clear_session_command, static) =
735 : {
736 : .path = "clear session",
737 : .short_help = "clear session thread <thread> session <index>",
738 : .function = clear_session_command_fn,
739 : };
740 : /* *INDENT-ON* */
741 :
742 : static clib_error_t *
743 0 : show_session_fifo_trace_command_fn (vlib_main_t * vm,
744 : unformat_input_t * input,
745 : vlib_cli_command_t * cmd)
746 : {
747 0 : session_t *s = 0;
748 0 : u8 is_rx = 0, *str = 0;
749 :
750 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
751 : {
752 0 : if (unformat (input, "%U", unformat_session, &s))
753 : ;
754 0 : else if (unformat (input, "rx"))
755 0 : is_rx = 1;
756 0 : else if (unformat (input, "tx"))
757 0 : is_rx = 0;
758 : else
759 0 : return clib_error_return (0, "unknown input `%U'",
760 : format_unformat_error, input);
761 : }
762 :
763 : if (!SVM_FIFO_TRACE)
764 : {
765 0 : vlib_cli_output (vm, "fifo tracing not enabled");
766 0 : return 0;
767 : }
768 :
769 : if (!s)
770 : {
771 : vlib_cli_output (vm, "could not find session");
772 : return 0;
773 : }
774 :
775 : str = is_rx ?
776 : svm_fifo_dump_trace (str, s->rx_fifo) :
777 : svm_fifo_dump_trace (str, s->tx_fifo);
778 :
779 : vlib_cli_output (vm, "%v", str);
780 : return 0;
781 : }
782 :
783 : /* *INDENT-OFF* */
784 285289 : VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
785 : {
786 : .path = "show session fifo trace",
787 : .short_help = "show session fifo trace <session>",
788 : .function = show_session_fifo_trace_command_fn,
789 : };
790 : /* *INDENT-ON* */
791 :
792 : static clib_error_t *
793 0 : session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
794 : vlib_cli_command_t * cmd)
795 : {
796 0 : session_t *s = 0;
797 0 : u8 is_rx = 0, *str = 0;
798 :
799 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
800 : {
801 0 : if (unformat (input, "%U", unformat_session, &s))
802 : ;
803 0 : else if (unformat (input, "rx"))
804 0 : is_rx = 1;
805 : else
806 0 : return clib_error_return (0, "unknown input `%U'",
807 : format_unformat_error, input);
808 : }
809 :
810 : if (!SVM_FIFO_TRACE)
811 : {
812 0 : vlib_cli_output (vm, "fifo tracing not enabled");
813 0 : return 0;
814 : }
815 :
816 : if (!s)
817 : {
818 : vlib_cli_output (vm, "could not find session");
819 : return 0;
820 : }
821 :
822 : str = is_rx ?
823 : svm_fifo_replay (str, s->rx_fifo, 0, 1) :
824 : svm_fifo_replay (str, s->tx_fifo, 0, 1);
825 :
826 : vlib_cli_output (vm, "%v", str);
827 : return 0;
828 : }
829 :
830 : /* *INDENT-OFF* */
831 285289 : VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
832 : {
833 : .path = "session replay fifo",
834 : .short_help = "session replay fifo <session>",
835 : .function = session_replay_fifo_command_fn,
836 : };
837 : /* *INDENT-ON* */
838 :
839 : static clib_error_t *
840 0 : session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
841 : vlib_cli_command_t * cmd)
842 : {
843 0 : u8 is_en = 2;
844 :
845 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
846 : {
847 0 : if (unformat (input, "enable"))
848 0 : is_en = 1;
849 0 : else if (unformat (input, "disable"))
850 0 : is_en = 0;
851 : else
852 0 : return clib_error_return (0, "unknown input `%U'",
853 : format_unformat_error, input);
854 : }
855 :
856 0 : if (is_en > 1)
857 0 : return clib_error_return (0, "expected enable | disable");
858 :
859 0 : return vnet_session_enable_disable (vm, is_en);
860 : }
861 :
862 : /* *INDENT-OFF* */
863 285289 : VLIB_CLI_COMMAND (session_enable_disable_command, static) =
864 : {
865 : .path = "session",
866 : .short_help = "session [enable|disable]",
867 : .function = session_enable_disable_fn,
868 : };
869 : /* *INDENT-ON* */
870 :
871 : static clib_error_t *
872 0 : show_session_stats_fn (vlib_main_t *vm, unformat_input_t *input,
873 : vlib_cli_command_t *cmd)
874 : {
875 0 : session_main_t *smm = &session_main;
876 : session_worker_t *wrk;
877 : unsigned int *e;
878 :
879 0 : if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
880 0 : return clib_error_return (0, "unknown input `%U'", format_unformat_error,
881 : input);
882 :
883 0 : vec_foreach (wrk, smm->wrk)
884 : {
885 0 : vlib_cli_output (vm, "Thread %u:\n", wrk - smm->wrk);
886 0 : e = wrk->stats.errors;
887 : #define _(name, str) \
888 : if (e[SESSION_EP_##name]) \
889 : vlib_cli_output (vm, " %lu %s", e[SESSION_EP_##name], str);
890 0 : foreach_session_error
891 : #undef _
892 : }
893 0 : return 0;
894 : }
895 :
896 285289 : VLIB_CLI_COMMAND (show_session_stats_command, static) = {
897 : .path = "show session stats",
898 : .short_help = "show session stats",
899 : .function = show_session_stats_fn,
900 : };
901 :
902 : static clib_error_t *
903 0 : clear_session_stats_fn (vlib_main_t *vm, unformat_input_t *input,
904 : vlib_cli_command_t *cmd)
905 : {
906 0 : session_main_t *smm = &session_main;
907 : session_worker_t *wrk;
908 :
909 0 : if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
910 0 : return clib_error_return (0, "unknown input `%U'", format_unformat_error,
911 : input);
912 :
913 0 : vec_foreach (wrk, smm->wrk)
914 : {
915 0 : clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
916 : }
917 :
918 0 : return 0;
919 : }
920 :
921 285289 : VLIB_CLI_COMMAND (clear_session_stats_command, static) = {
922 : .path = "clear session stats",
923 : .short_help = "clear session stats",
924 : .function = clear_session_stats_fn,
925 : };
926 :
927 : /*
928 : * fd.io coding-style-patch-verification: ON
929 : *
930 : * Local Variables:
931 : * eval: (c-set-style "gnu")
932 : * End:
933 : */
|