blob: 770ae086cc06afea18638c84e562c986c967748d [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
Dave Barach68b0fb02017-02-28 15:15:56 -050046/**
47 * Format stream session as per the following format
48 *
49 * verbose:
50 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
51 * non-verbose:
52 * "Connection"
53 */
54u8 *
Florin Coras31c99552019-03-01 13:00:58 -080055format_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -050056{
Florin Coras288eaab2019-02-03 15:26:14 -080057 session_t *ss = va_arg (*args, session_t *);
Dave Barach68b0fb02017-02-28 15:15:56 -050058 int verbose = va_arg (*args, int);
Florin Corasde9a8492018-10-24 22:18:58 -070059 u32 tp = session_get_transport_proto (ss);
Dave Barach68b0fb02017-02-28 15:15:56 -050060 u8 *str = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050061
Florin Coras5f066322019-07-22 19:03:03 -070062 if (ss->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras3389dd22019-02-01 18:00:05 -080063 {
64 s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
65 return s;
66 }
67
68 if (verbose == 1)
69 {
70 u8 post_accept = ss->session_state >= SESSION_STATE_ACCEPTING;
Aloys Augustin95dd1fe2019-07-12 17:11:04 +020071 u8 hasf = post_accept
72 || session_transport_service_type (ss) == TRANSPORT_SERVICE_CL;
Florin Coras3389dd22019-02-01 18:00:05 -080073 u32 rxf, txf;
74
Florin Coras288eaab2019-02-03 15:26:14 -080075 rxf = hasf ? svm_fifo_max_dequeue (ss->rx_fifo) : 0;
76 txf = hasf ? svm_fifo_max_dequeue (ss->tx_fifo) : 0;
Florin Coras3389dd22019-02-01 18:00:05 -080077 str = format (0, "%-10u%-10u", rxf, txf);
78 }
Dave Barach68b0fb02017-02-28 15:15:56 -050079
Florin Coras509028a2019-04-02 17:47:03 -070080 if (ss->session_state >= SESSION_STATE_ACCEPTING
81 || ss->session_state == SESSION_STATE_CREATED)
Dave Barach68b0fb02017-02-28 15:15:56 -050082 {
Florin Corasde9a8492018-10-24 22:18:58 -070083 s = format (s, "%U", format_transport_connection, tp,
84 ss->connection_index, ss->thread_index, verbose);
Florin Corasbb292f42017-05-19 09:49:19 -070085 if (verbose == 1)
86 s = format (s, "%v", str);
Florin Coras93992a92017-05-24 18:03:56 -070087 if (verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -070088 s = format (s, "%U", format_session_fifos, ss, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -050089 }
90 else if (ss->session_state == SESSION_STATE_LISTENING)
91 {
Florin Coras3389dd22019-02-01 18:00:05 -080092 s = format (s, "%U%v", format_transport_listen_connection,
Aloys Augustina0abbff2019-07-12 12:16:16 +020093 tp, ss->connection_index, ss->thread_index, verbose, str);
Florin Coras7fb0fe12018-04-09 09:24:52 -070094 if (verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -070095 s = format (s, "\n%U", format_session_fifos, ss, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -050096 }
Florin Corasbb292f42017-05-19 09:49:19 -070097 else if (ss->session_state == SESSION_STATE_CONNECTING)
Dave Barach68b0fb02017-02-28 15:15:56 -050098 {
Florin Corasde9a8492018-10-24 22:18:58 -070099 s = format (s, "%-40U%v", format_transport_half_open_connection,
Aloys Augustina0abbff2019-07-12 12:16:16 +0200100 tp, ss->connection_index, ss->thread_index, str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500101 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500102 else
103 {
Florin Corase04c2992017-03-01 08:17:34 -0800104 clib_warning ("Session in state: %d!", ss->session_state);
Dave Barach68b0fb02017-02-28 15:15:56 -0500105 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500106 vec_free (str);
107
108 return s;
109}
110
Florin Coras3eb50622017-07-13 01:24:57 -0400111uword
112unformat_stream_session_id (unformat_input_t * input, va_list * args)
113{
114 u8 *proto = va_arg (*args, u8 *);
Florin Corasbaee8d42019-01-19 11:29:18 -0800115 u32 *fib_index = va_arg (*args, u32 *);
Florin Coras3eb50622017-07-13 01:24:57 -0400116 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
117 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
118 u16 *lcl_port = va_arg (*args, u16 *);
119 u16 *rmt_port = va_arg (*args, u16 *);
120 u8 *is_ip4 = va_arg (*args, u8 *);
121 u8 tuple_is_set = 0;
Florin Corasbaee8d42019-01-19 11:29:18 -0800122 u32 vrf = ~0;
Florin Coras3eb50622017-07-13 01:24:57 -0400123
Dave Barachb7b92992018-10-17 10:38:51 -0400124 clib_memset (lcl, 0, sizeof (*lcl));
125 clib_memset (rmt, 0, sizeof (*rmt));
Florin Coras3eb50622017-07-13 01:24:57 -0400126
127 if (unformat (input, "tcp"))
128 {
129 *proto = TRANSPORT_PROTO_TCP;
130 }
Florin Corasbaee8d42019-01-19 11:29:18 -0800131 else if (unformat (input, "udp"))
Florin Coras3eb50622017-07-13 01:24:57 -0400132 {
133 *proto = TRANSPORT_PROTO_UDP;
134 }
Florin Corasbaee8d42019-01-19 11:29:18 -0800135 else
136 return 0;
137
138 if (unformat (input, "vrf %u", &vrf))
139 ;
140
Dave Barachb7f1faa2017-08-29 11:43:37 -0400141 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
142 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
Florin Coras3eb50622017-07-13 01:24:57 -0400143 {
144 *is_ip4 = 1;
145 tuple_is_set = 1;
146 }
147 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
148 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
149 {
150 *is_ip4 = 0;
151 tuple_is_set = 1;
152 }
Florin Coras3eb50622017-07-13 01:24:57 -0400153
Florin Corasbaee8d42019-01-19 11:29:18 -0800154 if (vrf != ~0)
155 {
156 fib_protocol_t fib_proto;
157 fib_proto = *is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
158 *fib_index = fib_table_find (fib_proto, vrf);
159 }
160
Chris Lukeb2bcad62017-09-18 08:51:22 -0400161 return tuple_is_set;
Florin Coras3eb50622017-07-13 01:24:57 -0400162}
163
164uword
Florin Coras5bb23ec2019-08-31 09:45:13 -0700165unformat_session_state (unformat_input_t * input, va_list * args)
166{
167 session_state_t *state = va_arg (*args, session_state_t *);
168 u8 *state_vec = 0;
169 int rv = 0;
170
171#define _(sym, str) \
172 if (unformat (input, str)) \
173 { \
174 *state = SESSION_STATE_ ## sym; \
175 rv = 1; \
176 goto done; \
177 }
178 foreach_session_state
179#undef _
180done:
181 vec_free (state_vec);
182 return rv;
183}
184
185uword
Florin Coras31c99552019-03-01 13:00:58 -0800186unformat_session (unformat_input_t * input, va_list * args)
Florin Coras3eb50622017-07-13 01:24:57 -0400187{
Florin Coras288eaab2019-02-03 15:26:14 -0800188 session_t **result = va_arg (*args, session_t **);
Florin Corasbaee8d42019-01-19 11:29:18 -0800189 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
190 ip46_address_t lcl, rmt;
Florin Coras288eaab2019-02-03 15:26:14 -0800191 session_t *s;
Florin Coras3eb50622017-07-13 01:24:57 -0400192 u8 proto = ~0;
Florin Corascea194d2017-10-02 00:18:51 -0700193 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400194
Florin Corasbaee8d42019-01-19 11:29:18 -0800195 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &fib_index,
196 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400197 return 0;
198
Florin Coras3eb50622017-07-13 01:24:57 -0400199 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700200 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
201 clib_host_to_net_u16 (lcl_port),
202 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400203 else
Florin Coras3cbc04b2017-10-02 00:18:51 -0700204 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
205 clib_host_to_net_u16 (lcl_port),
206 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400207 if (s)
208 {
209 *result = s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210 session_pool_remove_peeker (s->thread_index);
Florin Coras3eb50622017-07-13 01:24:57 -0400211 return 1;
212 }
213 return 0;
214}
215
216uword
217unformat_transport_connection (unformat_input_t * input, va_list * args)
218{
219 transport_connection_t **result = va_arg (*args, transport_connection_t **);
220 u32 suggested_proto = va_arg (*args, u32);
221 transport_connection_t *tc;
222 u8 proto = ~0;
223 ip46_address_t lcl, rmt;
Florin Corascea194d2017-10-02 00:18:51 -0700224 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
225 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400226
Florin Corasbaee8d42019-01-19 11:29:18 -0800227 if (!unformat (input, "%U", unformat_stream_session_id, &fib_index, &proto,
228 &lcl, &rmt, &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400229 return 0;
230
Florin Coras3eb50622017-07-13 01:24:57 -0400231 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
232 if (proto == (u8) ~ 0)
233 return 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400234 if (is_ip4)
Florin Corascea194d2017-10-02 00:18:51 -0700235 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
236 clib_host_to_net_u16 (lcl_port),
237 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400238 else
Florin Corascea194d2017-10-02 00:18:51 -0700239 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
240 clib_host_to_net_u16 (lcl_port),
241 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400242
243 if (tc)
244 {
245 *result = tc;
246 return 1;
247 }
248 return 0;
249}
250
Florin Coras5bb23ec2019-08-31 09:45:13 -0700251static void
252session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
253{
254 session_main_t *smm = &session_main;
255 u32 n_closed = 0, thread_index;
256 session_t *pool, *s;
257
258 for (thread_index = 0; thread_index < vec_len (smm->wrk); thread_index++)
259 {
260 pool = smm->wrk[thread_index].sessions;
261
262 if (!pool_elts (pool))
263 {
264 vlib_cli_output (vm, "Thread %d: no sessions", thread_index);
265 continue;
266 }
267
268 if (!verbose)
269 {
270 vlib_cli_output (vm, "Thread %d: %d sessions", thread_index,
271 pool_elts (pool));
272 continue;
273 }
274
275 if (pool_elts (pool) > 50)
276 {
277 vlib_cli_output (vm, "Thread %u: %d sessions. Verbose output "
278 "suppressed. For more details use filters.",
279 thread_index, pool_elts (pool));
280 continue;
281 }
282
283 if (verbose == 1)
284 vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s",
285 thread_index ? "\n" : "",
286 "Connection", "State", "Rx-f", "Tx-f");
287
288 /* *INDENT-OFF* */
289 pool_foreach(s, pool, ({
290 if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
291 {
292 n_closed += 1;
293 continue;
294 }
295 vlib_cli_output (vm, "%U", format_session, s, verbose);
296 }));
297 /* *INDENT-ON* */
298
299 if (!n_closed)
300 vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
301 pool_elts (pool) - n_closed);
302 else
303 vlib_cli_output (vm, "Thread %d: active sessions %u closed %u",
304 thread_index, pool_elts (pool) - n_closed, n_closed);
305 }
306}
307
308static int
309session_cli_filter_check (session_t * s, session_state_t * states,
310 transport_proto_t tp)
311{
312 if (states)
313 {
314 session_state_t *state;
315 vec_foreach (state, states) if (s->session_state == *state)
316 goto check_transport;
317 return 0;
318 }
319
320check_transport:
321
322 if (tp != TRANSPORT_N_PROTO && session_get_transport_proto (s) != tp)
323 return 0;
324
325 return 1;
326}
327
328static void
329session_cli_show_session_filter (vlib_main_t * vm, u32 thread_index,
330 u32 start, u32 end, session_state_t * states,
331 transport_proto_t tp, int verbose)
332{
333 u8 output_suppressed = 0;
334 session_worker_t *wrk;
335 session_t *pool, *s;
336 u32 count = 0, max_index;
337 int i;
338
339 wrk = session_main_get_worker_if_valid (thread_index);
340 if (!wrk)
341 {
342 vlib_cli_output (vm, "invalid thread index %u", thread_index);
343 return;
344 }
345
346 pool = wrk->sessions;
347
348 if (tp == TRANSPORT_N_PROTO && states == 0 && !verbose
349 && (start == 0 && end == ~0))
350 {
351 vlib_cli_output (vm, "Thread %d: %u sessions", thread_index,
352 pool_elts (pool));
353 return;
354 }
355
Florin Coras61a89f22019-09-09 19:22:36 -0700356 max_index = pool_len (pool) ? pool_len (pool) - 1 : 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700357 for (i = start; i <= clib_min (end, max_index); i++)
358 {
359 if (pool_is_free_index (pool, i))
360 continue;
361
362 s = pool_elt_at_index (pool, i);
363
364 if (session_cli_filter_check (s, states, tp))
365 {
366 count += 1;
367 if (verbose)
368 {
369 if (count > 50 || (verbose > 1 && count > 10))
370 {
371 output_suppressed = 1;
372 continue;
373 }
374 if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
375 vlib_cli_output (vm, "%U", format_session, s, verbose);
376 }
377 }
378 }
379
380 if (!output_suppressed)
381 vlib_cli_output (vm, "Thread %d: %u sessions matched filter",
382 thread_index, count);
383 else
384 vlib_cli_output (vm, "Thread %d: %u sessions matched filter. Not all"
385 " shown. Use finer grained filter.", thread_index,
386 count);
387}
388
389static void
390session_cli_print_transport_protos (vlib_main_t * vm)
391{
392#define _(sym, str, sstr) vlib_cli_output (vm, str);
393 foreach_transport_proto
394#undef _
395}
396
397static void
398session_cli_print_session_states (vlib_main_t * vm)
399{
400#define _(sym, str) vlib_cli_output (vm, str);
401 foreach_session_state
402#undef _
403}
404
Dave Barach68b0fb02017-02-28 15:15:56 -0500405static clib_error_t *
406show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
407 vlib_cli_command_t * cmd)
408{
Florin Coras5bb23ec2019-08-31 09:45:13 -0700409 u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
410 u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
411 unformat_input_t _line_input, *line_input = &_line_input;
412 transport_proto_t transport_proto = TRANSPORT_N_PROTO;
413 session_state_t state = SESSION_N_STATES, *states = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800414 session_main_t *smm = &session_main;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700415 clib_error_t *error = 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800416 app_worker_t *app_wrk;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700417 u32 transport_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800418 const u8 *app_name;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700419 int verbose = 0;
420 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500421
Florin Coras5bb23ec2019-08-31 09:45:13 -0700422 session_cli_return_if_not_enabled ();
423
424 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase04c2992017-03-01 08:17:34 -0800425 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700426 session_cli_show_all_sessions (vm, 0);
427 return 0;
Florin Corase04c2992017-03-01 08:17:34 -0800428 }
429
Florin Coras5bb23ec2019-08-31 09:45:13 -0700430 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Dave Barach68b0fb02017-02-28 15:15:56 -0500431 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700432 if (unformat (line_input, "verbose %d", &verbose))
Florin Corasbb292f42017-05-19 09:49:19 -0700433 ;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700434 else if (unformat (line_input, "verbose"))
Dave Barach68b0fb02017-02-28 15:15:56 -0500435 verbose = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700436 else if (unformat (line_input, "listeners %U", unformat_transport_proto,
Florin Corasdbd44562017-11-09 19:30:17 -0800437 &transport_proto))
438 do_listeners = 1;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700439 else if (unformat (line_input, "%U", unformat_session, &s))
Dave Barach2c25a622017-06-26 11:35:07 -0400440 {
441 one_session = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400442 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700443 else if (unformat (line_input, "thread %u index %u", &thread_index,
444 &session_index))
445 {
446 s = session_get_if_valid (session_index, thread_index);
447 if (!s)
448 {
449 vlib_cli_output (vm, "session is not allocated");
450 goto done;
451 }
452 one_session = 1;
453 }
454 else if (unformat (line_input, "thread %u", &thread_index))
455 {
456 do_filter = 1;
457 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500458 else
Florin Coras5bb23ec2019-08-31 09:45:13 -0700459 if (unformat (line_input, "state %U", unformat_session_state, &state))
460 {
461 vec_add1 (states, state);
462 do_filter = 1;
463 }
464 else if (unformat (line_input, "proto %U index %u",
465 unformat_transport_proto, &transport_proto,
466 &transport_index))
467 {
468 transport_connection_t *tc;
469 tc = transport_get_connection (transport_proto, transport_index,
470 thread_index);
471 if (!tc)
472 {
473 vlib_cli_output (vm, "transport connection %u thread %u is not"
474 " allocated", transport_index, thread_index);
475 goto done;
476 }
477 s = session_get_if_valid (tc->s_index, thread_index);
478 if (!s)
479 {
480 vlib_cli_output (vm, "session for transport connection %u "
481 "thread %u does not exist", transport_index,
482 thread_index);
483 goto done;
484 }
485 one_session = 1;
486 }
487 else if (unformat (line_input, "proto %U", unformat_transport_proto,
488 &transport_proto))
489 do_filter = 1;
490 else if (unformat (line_input, "range %u %u", &start, &end))
491 do_filter = 1;
492 else if (unformat (line_input, "range %u", &start))
493 {
494 end = start + 50;
495 do_filter = 1;
496 }
497 else if (unformat (line_input, "elog"))
498 do_elog = 1;
499 else if (unformat (line_input, "protos"))
500 {
501 session_cli_print_transport_protos (vm);
502 goto done;
503 }
504 else if (unformat (line_input, "states"))
505 {
506 session_cli_print_session_states (vm);
507 goto done;
508 }
509 else
510 {
511 error = clib_error_return (0, "unknown input `%U'",
512 format_unformat_error, line_input);
513 goto done;
514 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500515 }
516
Dave Barach2c25a622017-06-26 11:35:07 -0400517 if (one_session)
518 {
Florin Coras31c99552019-03-01 13:00:58 -0800519 u8 *str = format (0, "%U", format_session, s, 3);
Florin Coras844a36d2018-12-20 09:50:50 -0800520 if (do_elog && s->session_state != SESSION_STATE_LISTENING)
Florin Corasaefbede2018-12-19 13:07:49 -0800521 {
522 elog_main_t *em = &vm->elog_main;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700523 transport_connection_t *tc;
Florin Corasaefbede2018-12-19 13:07:49 -0800524 f64 dt;
525
526 tc = session_get_transport (s);
527 track_index = transport_elog_track_index (tc);
528 dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
529 * vm->clib_time.seconds_per_clock;
530 if (track_index != ~0)
531 str = format (str, " session elog:\n%U", format_elog_track, em,
532 dt, track_index);
533 }
534 vlib_cli_output (vm, "%v", str);
Florin Coras3389dd22019-02-01 18:00:05 -0800535 vec_free (str);
Florin Coras5bb23ec2019-08-31 09:45:13 -0700536 goto done;
Dave Barach2c25a622017-06-26 11:35:07 -0400537 }
538
Florin Corasdbd44562017-11-09 19:30:17 -0800539 if (do_listeners)
540 {
541 sst = session_type_from_proto_and_ip (transport_proto, 1);
Florin Coras3389dd22019-02-01 18:00:05 -0800542 vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
Florin Coras5c9083d2018-04-13 06:39:07 -0700543 /* *INDENT-OFF* */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700544 pool_foreach (s, smm->wrk[0].sessions, ({
Florin Coras5c9083d2018-04-13 06:39:07 -0700545 if (s->session_state != SESSION_STATE_LISTENING
546 || s->session_type != sst)
547 continue;
Florin Coras053a0e42018-11-13 15:52:38 -0800548 app_wrk = app_worker_get (s->app_wrk_index);
549 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras31c99552019-03-01 13:00:58 -0800550 vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
Florin Corasde9a8492018-10-24 22:18:58 -0700551 app_name);
Florin Coras5c9083d2018-04-13 06:39:07 -0700552 }));
553 /* *INDENT-ON* */
Florin Coras5bb23ec2019-08-31 09:45:13 -0700554 goto done;
Florin Corasdbd44562017-11-09 19:30:17 -0800555 }
556
Florin Coras5bb23ec2019-08-31 09:45:13 -0700557 if (do_filter)
Dave Barach68b0fb02017-02-28 15:15:56 -0500558 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700559 if (end < start)
Dave Barach68b0fb02017-02-28 15:15:56 -0500560 {
Florin Coras5bb23ec2019-08-31 09:45:13 -0700561 error = clib_error_return (0, "invalid range start: %u end: %u",
562 start, end);
563 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700565 session_cli_show_session_filter (vm, thread_index, start, end, states,
566 transport_proto, verbose);
567 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -0500568 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500569
Florin Coras5bb23ec2019-08-31 09:45:13 -0700570 session_cli_show_all_sessions (vm, verbose);
571
572done:
573 unformat_free (line_input);
574 vec_free (states);
575 return error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500576}
577
Florin Corase04c2992017-03-01 08:17:34 -0800578/* *INDENT-OFF* */
Florin Coras66b11312017-07-31 17:18:03 -0700579VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500580{
Florin Corase04c2992017-03-01 08:17:34 -0800581 .path = "show session",
Florin Coras3389dd22019-02-01 18:00:05 -0800582 .short_help = "show session [verbose [n]] [listeners <proto>] "
Florin Coras5bb23ec2019-08-31 09:45:13 -0700583 "[<session-id> [elog]] [thread <n> [index <n>] "
584 "[proto <proto>] [state <state>] [range <min> [<max>]] "
585 "[protos] [states] ",
Florin Corase04c2992017-03-01 08:17:34 -0800586 .function = show_session_command_fn,
587};
588/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500589
Dave Barach2c25a622017-06-26 11:35:07 -0400590static int
Florin Coras288eaab2019-02-03 15:26:14 -0800591clear_session (session_t * s)
Dave Barach2c25a622017-06-26 11:35:07 -0400592{
Florin Coras15531972018-08-12 23:50:53 -0700593 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -0700594 app_worker_close_notify (server_wrk, s);
Dave Barach2c25a622017-06-26 11:35:07 -0400595 return 0;
596}
597
Dave Barach68b0fb02017-02-28 15:15:56 -0500598static clib_error_t *
599clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
600 vlib_cli_command_t * cmd)
601{
Florin Coras31c99552019-03-01 13:00:58 -0800602 session_main_t *smm = &session_main;
Dave Barach2c25a622017-06-26 11:35:07 -0400603 u32 thread_index = 0, clear_all = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800604 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500605 u32 session_index = ~0;
Florin Coras288eaab2019-02-03 15:26:14 -0800606 session_t *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500607
Florin Corase04c2992017-03-01 08:17:34 -0800608 if (!smm->is_enabled)
609 {
flyingeagle23e2125062017-04-20 20:01:14 +0800610 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800611 }
612
Dave Barach68b0fb02017-02-28 15:15:56 -0500613 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
614 {
615 if (unformat (input, "thread %d", &thread_index))
616 ;
617 else if (unformat (input, "session %d", &session_index))
618 ;
Dave Barach2c25a622017-06-26 11:35:07 -0400619 else if (unformat (input, "all"))
620 clear_all = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500621 else
622 return clib_error_return (0, "unknown input `%U'",
623 format_unformat_error, input);
624 }
625
Dave Barach2c25a622017-06-26 11:35:07 -0400626 if (!clear_all && session_index == ~0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500627 return clib_error_return (0, "session <nn> required, but not set.");
628
Dave Barach2c25a622017-06-26 11:35:07 -0400629 if (session_index != ~0)
630 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700631 session = session_get_if_valid (session_index, thread_index);
Dave Barach2c25a622017-06-26 11:35:07 -0400632 if (!session)
633 return clib_error_return (0, "no session %d on thread %d",
634 session_index, thread_index);
635 clear_session (session);
636 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500637
Dave Barach2c25a622017-06-26 11:35:07 -0400638 if (clear_all)
639 {
640 /* *INDENT-OFF* */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700641 vec_foreach (wrk, smm->wrk)
Dave Barach2c25a622017-06-26 11:35:07 -0400642 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700643 pool_foreach(session, wrk->sessions, ({
Dave Barach2c25a622017-06-26 11:35:07 -0400644 clear_session (session);
645 }));
646 };
647 /* *INDENT-ON* */
648 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500649
650 return 0;
651}
652
Florin Corase04c2992017-03-01 08:17:34 -0800653/* *INDENT-OFF* */
654VLIB_CLI_COMMAND (clear_session_command, static) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500655{
Florin Corase04c2992017-03-01 08:17:34 -0800656 .path = "clear session",
657 .short_help = "clear session thread <thread> session <index>",
658 .function = clear_session_command_fn,
659};
660/* *INDENT-ON* */
661
662static clib_error_t *
Florin Coras3eb50622017-07-13 01:24:57 -0400663show_session_fifo_trace_command_fn (vlib_main_t * vm,
664 unformat_input_t * input,
665 vlib_cli_command_t * cmd)
666{
Florin Coras288eaab2019-02-03 15:26:14 -0800667 session_t *s = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400668 u8 is_rx = 0, *str = 0;
669
670 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
671 {
Florin Coras31c99552019-03-01 13:00:58 -0800672 if (unformat (input, "%U", unformat_session, &s))
Florin Coras3eb50622017-07-13 01:24:57 -0400673 ;
674 else if (unformat (input, "rx"))
675 is_rx = 1;
676 else if (unformat (input, "tx"))
677 is_rx = 0;
678 else
679 return clib_error_return (0, "unknown input `%U'",
680 format_unformat_error, input);
681 }
682
683 if (!SVM_FIFO_TRACE)
684 {
685 vlib_cli_output (vm, "fifo tracing not enabled");
686 return 0;
687 }
688
689 if (!s)
690 {
691 vlib_cli_output (vm, "could not find session");
692 return 0;
693 }
694
695 str = is_rx ?
Florin Coras288eaab2019-02-03 15:26:14 -0800696 svm_fifo_dump_trace (str, s->rx_fifo) :
697 svm_fifo_dump_trace (str, s->tx_fifo);
Florin Coras3eb50622017-07-13 01:24:57 -0400698
699 vlib_cli_output (vm, "%v", str);
700 return 0;
701}
702
703/* *INDENT-OFF* */
704VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
705{
706 .path = "show session fifo trace",
707 .short_help = "show session fifo trace <session>",
708 .function = show_session_fifo_trace_command_fn,
709};
710/* *INDENT-ON* */
711
712static clib_error_t *
713session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
714 vlib_cli_command_t * cmd)
715{
Florin Coras288eaab2019-02-03 15:26:14 -0800716 session_t *s = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400717 u8 is_rx = 0, *str = 0;
718
719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
720 {
Florin Coras31c99552019-03-01 13:00:58 -0800721 if (unformat (input, "%U", unformat_session, &s))
Florin Coras3eb50622017-07-13 01:24:57 -0400722 ;
723 else if (unformat (input, "rx"))
724 is_rx = 1;
725 else
726 return clib_error_return (0, "unknown input `%U'",
727 format_unformat_error, input);
728 }
729
730 if (!SVM_FIFO_TRACE)
731 {
732 vlib_cli_output (vm, "fifo tracing not enabled");
733 return 0;
734 }
735
736 if (!s)
737 {
738 vlib_cli_output (vm, "could not find session");
739 return 0;
740 }
741
742 str = is_rx ?
Florin Coras288eaab2019-02-03 15:26:14 -0800743 svm_fifo_replay (str, s->rx_fifo, 0, 1) :
744 svm_fifo_replay (str, s->tx_fifo, 0, 1);
Florin Coras3eb50622017-07-13 01:24:57 -0400745
746 vlib_cli_output (vm, "%v", str);
747 return 0;
748}
749
750/* *INDENT-OFF* */
751VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
752{
753 .path = "session replay fifo",
754 .short_help = "session replay fifo <session>",
755 .function = session_replay_fifo_command_fn,
756};
757/* *INDENT-ON* */
758
759static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800760session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
761 vlib_cli_command_t * cmd)
762{
Dave Wallace8af20542017-10-26 03:29:30 -0400763 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase04c2992017-03-01 08:17:34 -0800764 u8 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400765 clib_error_t *error;
Florin Corase04c2992017-03-01 08:17:34 -0800766
Dave Wallace8af20542017-10-26 03:29:30 -0400767 if (!unformat_user (input, unformat_line_input, line_input))
Swarup Nayak82d8ec22017-12-04 11:54:43 +0530768 return clib_error_return (0, "expected enable | disable");
Dave Wallace8af20542017-10-26 03:29:30 -0400769
770 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corase04c2992017-03-01 08:17:34 -0800771 {
Dave Wallace8af20542017-10-26 03:29:30 -0400772 if (unformat (line_input, "enable"))
Florin Corase04c2992017-03-01 08:17:34 -0800773 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400774 else if (unformat (line_input, "disable"))
Florin Corase04c2992017-03-01 08:17:34 -0800775 is_en = 0;
776 else
Dave Wallace8af20542017-10-26 03:29:30 -0400777 {
778 error = clib_error_return (0, "unknown input `%U'",
779 format_unformat_error, line_input);
780 unformat_free (line_input);
781 return error;
782 }
Florin Corase04c2992017-03-01 08:17:34 -0800783 }
784
Dave Wallace8af20542017-10-26 03:29:30 -0400785 unformat_free (line_input);
Florin Corase04c2992017-03-01 08:17:34 -0800786 return vnet_session_enable_disable (vm, is_en);
787}
788
789/* *INDENT-OFF* */
790VLIB_CLI_COMMAND (session_enable_disable_command, static) =
791{
792 .path = "session",
793 .short_help = "session [enable|disable]",
794 .function = session_enable_disable_fn,
795};
796/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500797
798/*
799 * fd.io coding-style-patch-verification: ON
800 *
801 * Local Variables:
802 * eval: (c-set-style "gnu")
803 * End:
804 */