blob: 8c30a1df7ebba0d17823fccb8ab729e3db056807 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2017 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
Florin Coras93992a92017-05-24 18:03:56 -070018u8 *
19format_stream_session_fifos (u8 * s, va_list * args)
20{
21 stream_session_t *ss = va_arg (*args, stream_session_t *);
Florin Coras6534b7a2017-07-18 05:38:03 -040022 int verbose = va_arg (*args, int);
23 session_fifo_event_t _e, *e = &_e;
24 u8 found;
25
Florin Coras93992a92017-05-24 18:03:56 -070026 s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
Florin Coras6534b7a2017-07-18 05:38:03 -040027 if (verbose > 2 && ss->server_rx_fifo->has_event)
28 {
29 found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
30 s = format (s, " session node event: %s\n",
31 found ? "found" : "not found");
32 }
Florin Coras93992a92017-05-24 18:03:56 -070033 s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
Florin Coras6534b7a2017-07-18 05:38:03 -040034 if (verbose > 2 && ss->server_tx_fifo->has_event)
35 {
36 found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
37 s = format (s, " session node event: %s\n",
38 found ? "found" : "not found");
39 }
Florin Coras93992a92017-05-24 18:03:56 -070040 return s;
41}
42
Dave Barach68b0fb02017-02-28 15:15:56 -050043/**
44 * Format stream session as per the following format
45 *
46 * verbose:
47 * "Connection", "Rx fifo", "Tx fifo", "Session Index"
48 * non-verbose:
49 * "Connection"
50 */
51u8 *
52format_stream_session (u8 * s, va_list * args)
53{
54 stream_session_t *ss = va_arg (*args, stream_session_t *);
55 int verbose = va_arg (*args, int);
56 transport_proto_vft_t *tp_vft;
57 u8 *str = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050058 tp_vft = session_get_transport_vft (ss->session_type);
59
Florin Coras68810622017-07-24 17:40:28 -070060 if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
Florin Corasbb292f42017-05-19 09:49:19 -070061 str = format (0, "%-10u%-10u%-10lld",
62 svm_fifo_max_dequeue (ss->server_rx_fifo),
63 svm_fifo_max_enqueue (ss->server_tx_fifo),
64 stream_session_get_index (ss));
Dave Barach68b0fb02017-02-28 15:15:56 -050065
Dave Barach2c25a622017-06-26 11:35:07 -040066 if (ss->session_state == SESSION_STATE_READY
Florin Corasc87c91d2017-08-16 19:55:49 -070067 || ss->session_state == SESSION_STATE_ACCEPTING
68 || ss->session_state == SESSION_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -050069 {
Florin Corasbb292f42017-05-19 09:49:19 -070070 s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
71 ss->thread_index, verbose);
72 if (verbose == 1)
73 s = format (s, "%v", str);
Florin Coras93992a92017-05-24 18:03:56 -070074 if (verbose > 1)
Florin Coras6534b7a2017-07-18 05:38:03 -040075 s = format (s, "%U", format_stream_session_fifos, ss, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -050076 }
77 else if (ss->session_state == SESSION_STATE_LISTENING)
78 {
79 s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
80 str);
81 }
Florin Corasbb292f42017-05-19 09:49:19 -070082 else if (ss->session_state == SESSION_STATE_CONNECTING)
Dave Barach68b0fb02017-02-28 15:15:56 -050083 {
Florin Corasbb292f42017-05-19 09:49:19 -070084 s = format (s, "%-40U%v", tp_vft->format_half_open,
85 ss->connection_index, str);
Dave Barach68b0fb02017-02-28 15:15:56 -050086 }
Dave Barach68b0fb02017-02-28 15:15:56 -050087 else
88 {
Florin Corase04c2992017-03-01 08:17:34 -080089 clib_warning ("Session in state: %d!", ss->session_state);
Dave Barach68b0fb02017-02-28 15:15:56 -050090 }
Dave Barach68b0fb02017-02-28 15:15:56 -050091 vec_free (str);
92
93 return s;
94}
95
Florin Coras3eb50622017-07-13 01:24:57 -040096uword
97unformat_stream_session_id (unformat_input_t * input, va_list * args)
98{
99 u8 *proto = va_arg (*args, u8 *);
100 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
101 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
102 u16 *lcl_port = va_arg (*args, u16 *);
103 u16 *rmt_port = va_arg (*args, u16 *);
104 u8 *is_ip4 = va_arg (*args, u8 *);
105 u8 tuple_is_set = 0;
106
107 memset (lcl, 0, sizeof (*lcl));
108 memset (rmt, 0, sizeof (*rmt));
109
110 if (unformat (input, "tcp"))
111 {
112 *proto = TRANSPORT_PROTO_TCP;
113 }
114 if (unformat (input, "udp"))
115 {
116 *proto = TRANSPORT_PROTO_UDP;
117 }
Dave Barachb7f1faa2017-08-29 11:43:37 -0400118 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
119 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
Florin Coras3eb50622017-07-13 01:24:57 -0400120 {
121 *is_ip4 = 1;
122 tuple_is_set = 1;
123 }
124 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
125 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
126 {
127 *is_ip4 = 0;
128 tuple_is_set = 1;
129 }
Florin Coras3eb50622017-07-13 01:24:57 -0400130
Chris Lukeb2bcad62017-09-18 08:51:22 -0400131 return tuple_is_set;
Florin Coras3eb50622017-07-13 01:24:57 -0400132}
133
134uword
135unformat_stream_session (unformat_input_t * input, va_list * args)
136{
137 stream_session_t **result = va_arg (*args, stream_session_t **);
138 stream_session_t *s;
139 u8 proto = ~0;
140 ip46_address_t lcl, rmt;
141 u32 lcl_port = 0, rmt_port = 0;
Chris Lukeb2bcad62017-09-18 08:51:22 -0400142 u8 is_ip4 = 0, s_type = ~0;
Florin Coras3eb50622017-07-13 01:24:57 -0400143
Chris Lukeb2bcad62017-09-18 08:51:22 -0400144 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
145 &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400146 return 0;
147
Florin Coras3eb50622017-07-13 01:24:57 -0400148 s_type = session_type_from_proto_and_ip (proto, is_ip4);
149 if (is_ip4)
150 s = stream_session_lookup4 (&lcl.ip4, &rmt.ip4,
151 clib_host_to_net_u16 (lcl_port),
152 clib_host_to_net_u16 (rmt_port), s_type);
153 else
154 s = stream_session_lookup6 (&lcl.ip6, &rmt.ip6,
155 clib_host_to_net_u16 (lcl_port),
156 clib_host_to_net_u16 (rmt_port), s_type);
157 if (s)
158 {
159 *result = s;
160 return 1;
161 }
162 return 0;
163}
164
165uword
166unformat_transport_connection (unformat_input_t * input, va_list * args)
167{
168 transport_connection_t **result = va_arg (*args, transport_connection_t **);
169 u32 suggested_proto = va_arg (*args, u32);
170 transport_connection_t *tc;
171 u8 proto = ~0;
172 ip46_address_t lcl, rmt;
173 u32 lcl_port = 0, rmt_port = 0;
Chris Lukeb2bcad62017-09-18 08:51:22 -0400174 u8 is_ip4 = 0, s_type = ~0;
Florin Coras3eb50622017-07-13 01:24:57 -0400175
Chris Lukeb2bcad62017-09-18 08:51:22 -0400176 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
177 &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400178 return 0;
179
Florin Coras3eb50622017-07-13 01:24:57 -0400180 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
181 if (proto == (u8) ~ 0)
182 return 0;
183 s_type = session_type_from_proto_and_ip (proto, is_ip4);
184 if (is_ip4)
185 tc = stream_session_lookup_transport4 (&lcl.ip4, &rmt.ip4,
186 clib_host_to_net_u16 (lcl_port),
187 clib_host_to_net_u16 (rmt_port),
188 s_type);
189 else
190 tc = stream_session_lookup_transport6 (&lcl.ip6, &rmt.ip6,
191 clib_host_to_net_u16 (lcl_port),
192 clib_host_to_net_u16 (rmt_port),
193 s_type);
194
195 if (tc)
196 {
197 *result = tc;
198 return 1;
199 }
200 return 0;
201}
202
Dave Barach68b0fb02017-02-28 15:15:56 -0500203static clib_error_t *
204show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
205 vlib_cli_command_t * cmd)
206{
207 session_manager_main_t *smm = &session_manager_main;
208 int verbose = 0, i;
209 stream_session_t *pool;
210 stream_session_t *s;
Florin Coras3eb50622017-07-13 01:24:57 -0400211 u8 *str = 0, one_session = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500212
Florin Corase04c2992017-03-01 08:17:34 -0800213 if (!smm->is_enabled)
214 {
flyingeagle23e2125062017-04-20 20:01:14 +0800215 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800216 }
217
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
219 {
Florin Corasbb292f42017-05-19 09:49:19 -0700220 if (unformat (input, "verbose %d", &verbose))
221 ;
222 else if (unformat (input, "verbose"))
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 verbose = 1;
Florin Coras3eb50622017-07-13 01:24:57 -0400224 else if (unformat (input, "%U", unformat_stream_session, &s))
Dave Barach2c25a622017-06-26 11:35:07 -0400225 {
226 one_session = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400227 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500228 else
Florin Coras3eb50622017-07-13 01:24:57 -0400229 return clib_error_return (0, "unknown input `%U'",
230 format_unformat_error, input);
Dave Barach68b0fb02017-02-28 15:15:56 -0500231 }
232
Dave Barach2c25a622017-06-26 11:35:07 -0400233 if (one_session)
234 {
Florin Coras6534b7a2017-07-18 05:38:03 -0400235 vlib_cli_output (vm, "%U", format_stream_session, s, 3);
Dave Barach2c25a622017-06-26 11:35:07 -0400236 return 0;
237 }
238
Dave Barach68b0fb02017-02-28 15:15:56 -0500239 for (i = 0; i < vec_len (smm->sessions); i++)
240 {
241 u32 once_per_pool;
242 pool = smm->sessions[i];
243
244 once_per_pool = 1;
245
246 if (pool_elts (pool))
247 {
248
249 vlib_cli_output (vm, "Thread %d: %d active sessions",
250 i, pool_elts (pool));
251 if (verbose)
252 {
Florin Corasbb292f42017-05-19 09:49:19 -0700253 if (once_per_pool && verbose == 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500254 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700255 str = format (str, "%-50s%-15s%-10s%-10s%-10s",
256 "Connection", "State", "Rx-f", "Tx-f",
257 "S-idx");
Dave Barach68b0fb02017-02-28 15:15:56 -0500258 vlib_cli_output (vm, "%v", str);
259 vec_reset_length (str);
260 once_per_pool = 0;
261 }
262
263 /* *INDENT-OFF* */
264 pool_foreach (s, pool,
265 ({
Florin Corasbb292f42017-05-19 09:49:19 -0700266 vec_reset_length (str);
267 str = format (str, "%U", format_stream_session, s, verbose);
Florin Corasbb292f42017-05-19 09:49:19 -0700268 vlib_cli_output (vm, "%v", str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500269 }));
270 /* *INDENT-ON* */
271 }
272 }
273 else
274 vlib_cli_output (vm, "Thread %d: no active sessions", i);
Dave Barach2c25a622017-06-26 11:35:07 -0400275 vec_reset_length (str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500276 }
277 vec_free (str);
278
279 return 0;
280}
281
Florin Corase04c2992017-03-01 08:17:34 -0800282/* *INDENT-OFF* */
Florin Coras66b11312017-07-31 17:18:03 -0700283VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500284{
Florin Corase04c2992017-03-01 08:17:34 -0800285 .path = "show session",
rootc9d1c5b2017-08-15 12:58:31 -0400286 .short_help = "show session [verbose [nnn]]",
Florin Corase04c2992017-03-01 08:17:34 -0800287 .function = show_session_command_fn,
288};
289/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500290
Dave Barach2c25a622017-06-26 11:35:07 -0400291static int
292clear_session (stream_session_t * s)
293{
294 application_t *server = application_get (s->app_index);
295 server->cb_fns.session_disconnect_callback (s);
296 return 0;
297}
298
Dave Barach68b0fb02017-02-28 15:15:56 -0500299static clib_error_t *
300clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
301 vlib_cli_command_t * cmd)
302{
303 session_manager_main_t *smm = &session_manager_main;
Dave Barach2c25a622017-06-26 11:35:07 -0400304 u32 thread_index = 0, clear_all = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 u32 session_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -0400306 stream_session_t **pool, *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500307
Florin Corase04c2992017-03-01 08:17:34 -0800308 if (!smm->is_enabled)
309 {
flyingeagle23e2125062017-04-20 20:01:14 +0800310 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800311 }
312
Dave Barach68b0fb02017-02-28 15:15:56 -0500313 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
314 {
315 if (unformat (input, "thread %d", &thread_index))
316 ;
317 else if (unformat (input, "session %d", &session_index))
318 ;
Dave Barach2c25a622017-06-26 11:35:07 -0400319 else if (unformat (input, "all"))
320 clear_all = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500321 else
322 return clib_error_return (0, "unknown input `%U'",
323 format_unformat_error, input);
324 }
325
Dave Barach2c25a622017-06-26 11:35:07 -0400326 if (!clear_all && session_index == ~0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 return clib_error_return (0, "session <nn> required, but not set.");
328
Dave Barach2c25a622017-06-26 11:35:07 -0400329 if (session_index != ~0)
330 {
331 session = stream_session_get_if_valid (session_index, thread_index);
332 if (!session)
333 return clib_error_return (0, "no session %d on thread %d",
334 session_index, thread_index);
335 clear_session (session);
336 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500337
Dave Barach2c25a622017-06-26 11:35:07 -0400338 if (clear_all)
339 {
340 /* *INDENT-OFF* */
341 vec_foreach (pool, smm->sessions)
342 {
343 pool_foreach(session, *pool, ({
344 clear_session (session);
345 }));
346 };
347 /* *INDENT-ON* */
348 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500349
350 return 0;
351}
352
Florin Corase04c2992017-03-01 08:17:34 -0800353/* *INDENT-OFF* */
354VLIB_CLI_COMMAND (clear_session_command, static) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500355{
Florin Corase04c2992017-03-01 08:17:34 -0800356 .path = "clear session",
357 .short_help = "clear session thread <thread> session <index>",
358 .function = clear_session_command_fn,
359};
360/* *INDENT-ON* */
361
362static clib_error_t *
Florin Coras3eb50622017-07-13 01:24:57 -0400363show_session_fifo_trace_command_fn (vlib_main_t * vm,
364 unformat_input_t * input,
365 vlib_cli_command_t * cmd)
366{
367 stream_session_t *s = 0;
368 u8 is_rx = 0, *str = 0;
369
370 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
371 {
372 if (unformat (input, "%U", unformat_stream_session, &s))
373 ;
374 else if (unformat (input, "rx"))
375 is_rx = 1;
376 else if (unformat (input, "tx"))
377 is_rx = 0;
378 else
379 return clib_error_return (0, "unknown input `%U'",
380 format_unformat_error, input);
381 }
382
383 if (!SVM_FIFO_TRACE)
384 {
385 vlib_cli_output (vm, "fifo tracing not enabled");
386 return 0;
387 }
388
389 if (!s)
390 {
391 vlib_cli_output (vm, "could not find session");
392 return 0;
393 }
394
395 str = is_rx ?
396 svm_fifo_dump_trace (str, s->server_rx_fifo) :
397 svm_fifo_dump_trace (str, s->server_tx_fifo);
398
399 vlib_cli_output (vm, "%v", str);
400 return 0;
401}
402
403/* *INDENT-OFF* */
404VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
405{
406 .path = "show session fifo trace",
407 .short_help = "show session fifo trace <session>",
408 .function = show_session_fifo_trace_command_fn,
409};
410/* *INDENT-ON* */
411
412static clib_error_t *
413session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
414 vlib_cli_command_t * cmd)
415{
416 stream_session_t *s = 0;
417 u8 is_rx = 0, *str = 0;
418
419 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
420 {
421 if (unformat (input, "%U", unformat_stream_session, &s))
422 ;
423 else if (unformat (input, "rx"))
424 is_rx = 1;
425 else
426 return clib_error_return (0, "unknown input `%U'",
427 format_unformat_error, input);
428 }
429
430 if (!SVM_FIFO_TRACE)
431 {
432 vlib_cli_output (vm, "fifo tracing not enabled");
433 return 0;
434 }
435
436 if (!s)
437 {
438 vlib_cli_output (vm, "could not find session");
439 return 0;
440 }
441
442 str = is_rx ?
443 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
444 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
445
446 vlib_cli_output (vm, "%v", str);
447 return 0;
448}
449
450/* *INDENT-OFF* */
451VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
452{
453 .path = "session replay fifo",
454 .short_help = "session replay fifo <session>",
455 .function = session_replay_fifo_command_fn,
456};
457/* *INDENT-ON* */
458
459static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800460session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
461 vlib_cli_command_t * cmd)
462{
463 u8 is_en = 1;
464
465 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
466 {
467 if (unformat (input, "enable"))
468 is_en = 1;
469 else if (unformat (input, "disable"))
470 is_en = 0;
471 else
472 return clib_error_return (0, "unknown input `%U'",
473 format_unformat_error, input);
474 }
475
476 return vnet_session_enable_disable (vm, is_en);
477}
478
479/* *INDENT-OFF* */
480VLIB_CLI_COMMAND (session_enable_disable_command, static) =
481{
482 .path = "session",
483 .short_help = "session [enable|disable]",
484 .function = session_enable_disable_fn,
485};
486/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500487
488/*
489 * fd.io coding-style-patch-verification: ON
490 *
491 * Local Variables:
492 * eval: (c-set-style "gnu")
493 * End:
494 */