blob: d9f516beab919324ee6359412ca5aa0432f5cc3e [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 }
130 else
131 return 0;
132
133 if (tuple_is_set)
134 return 1;
135
136 return 0;
137}
138
139uword
140unformat_stream_session (unformat_input_t * input, va_list * args)
141{
142 stream_session_t **result = va_arg (*args, stream_session_t **);
143 stream_session_t *s;
144 u8 proto = ~0;
145 ip46_address_t lcl, rmt;
146 u32 lcl_port = 0, rmt_port = 0;
147 u8 is_ip4 = 0, s_type = ~0, id_is_set = 0;
148
149 if (unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
150 &lcl_port, &rmt_port, &is_ip4))
151 {
152 id_is_set = 1;
153 }
154 else
155 return 0;
156
157 if (!id_is_set)
158 {
159 return 0;
160 }
161
162 s_type = session_type_from_proto_and_ip (proto, is_ip4);
163 if (is_ip4)
164 s = stream_session_lookup4 (&lcl.ip4, &rmt.ip4,
165 clib_host_to_net_u16 (lcl_port),
166 clib_host_to_net_u16 (rmt_port), s_type);
167 else
168 s = stream_session_lookup6 (&lcl.ip6, &rmt.ip6,
169 clib_host_to_net_u16 (lcl_port),
170 clib_host_to_net_u16 (rmt_port), s_type);
171 if (s)
172 {
173 *result = s;
174 return 1;
175 }
176 return 0;
177}
178
179uword
180unformat_transport_connection (unformat_input_t * input, va_list * args)
181{
182 transport_connection_t **result = va_arg (*args, transport_connection_t **);
183 u32 suggested_proto = va_arg (*args, u32);
184 transport_connection_t *tc;
185 u8 proto = ~0;
186 ip46_address_t lcl, rmt;
187 u32 lcl_port = 0, rmt_port = 0;
188 u8 is_ip4 = 0, s_type = ~0, id_is_set = 0;
189
190 if (unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
191 &lcl_port, &rmt_port, &is_ip4))
192 {
193 id_is_set = 1;
194 }
195 else
196 return 0;
197
198 if (!id_is_set)
199 {
200 return 0;
201 }
202
203 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
204 if (proto == (u8) ~ 0)
205 return 0;
206 s_type = session_type_from_proto_and_ip (proto, is_ip4);
207 if (is_ip4)
208 tc = stream_session_lookup_transport4 (&lcl.ip4, &rmt.ip4,
209 clib_host_to_net_u16 (lcl_port),
210 clib_host_to_net_u16 (rmt_port),
211 s_type);
212 else
213 tc = stream_session_lookup_transport6 (&lcl.ip6, &rmt.ip6,
214 clib_host_to_net_u16 (lcl_port),
215 clib_host_to_net_u16 (rmt_port),
216 s_type);
217
218 if (tc)
219 {
220 *result = tc;
221 return 1;
222 }
223 return 0;
224}
225
Dave Barach68b0fb02017-02-28 15:15:56 -0500226static clib_error_t *
227show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
228 vlib_cli_command_t * cmd)
229{
230 session_manager_main_t *smm = &session_manager_main;
231 int verbose = 0, i;
232 stream_session_t *pool;
233 stream_session_t *s;
Florin Coras3eb50622017-07-13 01:24:57 -0400234 u8 *str = 0, one_session = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500235
Florin Corase04c2992017-03-01 08:17:34 -0800236 if (!smm->is_enabled)
237 {
flyingeagle23e2125062017-04-20 20:01:14 +0800238 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800239 }
240
Dave Barach68b0fb02017-02-28 15:15:56 -0500241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
242 {
Florin Corasbb292f42017-05-19 09:49:19 -0700243 if (unformat (input, "verbose %d", &verbose))
244 ;
245 else if (unformat (input, "verbose"))
Dave Barach68b0fb02017-02-28 15:15:56 -0500246 verbose = 1;
Florin Coras3eb50622017-07-13 01:24:57 -0400247 else if (unformat (input, "%U", unformat_stream_session, &s))
Dave Barach2c25a622017-06-26 11:35:07 -0400248 {
249 one_session = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400250 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500251 else
Florin Coras3eb50622017-07-13 01:24:57 -0400252 return clib_error_return (0, "unknown input `%U'",
253 format_unformat_error, input);
Dave Barach68b0fb02017-02-28 15:15:56 -0500254 }
255
Dave Barach2c25a622017-06-26 11:35:07 -0400256 if (one_session)
257 {
Florin Coras6534b7a2017-07-18 05:38:03 -0400258 vlib_cli_output (vm, "%U", format_stream_session, s, 3);
Dave Barach2c25a622017-06-26 11:35:07 -0400259 return 0;
260 }
261
Dave Barach68b0fb02017-02-28 15:15:56 -0500262 for (i = 0; i < vec_len (smm->sessions); i++)
263 {
264 u32 once_per_pool;
265 pool = smm->sessions[i];
266
267 once_per_pool = 1;
268
269 if (pool_elts (pool))
270 {
271
272 vlib_cli_output (vm, "Thread %d: %d active sessions",
273 i, pool_elts (pool));
274 if (verbose)
275 {
Florin Corasbb292f42017-05-19 09:49:19 -0700276 if (once_per_pool && verbose == 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500277 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700278 str = format (str, "%-50s%-15s%-10s%-10s%-10s",
279 "Connection", "State", "Rx-f", "Tx-f",
280 "S-idx");
Dave Barach68b0fb02017-02-28 15:15:56 -0500281 vlib_cli_output (vm, "%v", str);
282 vec_reset_length (str);
283 once_per_pool = 0;
284 }
285
286 /* *INDENT-OFF* */
287 pool_foreach (s, pool,
288 ({
Florin Corasbb292f42017-05-19 09:49:19 -0700289 vec_reset_length (str);
290 str = format (str, "%U", format_stream_session, s, verbose);
Florin Corasbb292f42017-05-19 09:49:19 -0700291 vlib_cli_output (vm, "%v", str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500292 }));
293 /* *INDENT-ON* */
294 }
295 }
296 else
297 vlib_cli_output (vm, "Thread %d: no active sessions", i);
Dave Barach2c25a622017-06-26 11:35:07 -0400298 vec_reset_length (str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500299 }
300 vec_free (str);
301
302 return 0;
303}
304
Florin Corase04c2992017-03-01 08:17:34 -0800305/* *INDENT-OFF* */
Florin Coras66b11312017-07-31 17:18:03 -0700306VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500307{
Florin Corase04c2992017-03-01 08:17:34 -0800308 .path = "show session",
rootc9d1c5b2017-08-15 12:58:31 -0400309 .short_help = "show session [verbose [nnn]]",
Florin Corase04c2992017-03-01 08:17:34 -0800310 .function = show_session_command_fn,
311};
312/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500313
Dave Barach2c25a622017-06-26 11:35:07 -0400314static int
315clear_session (stream_session_t * s)
316{
317 application_t *server = application_get (s->app_index);
318 server->cb_fns.session_disconnect_callback (s);
319 return 0;
320}
321
Dave Barach68b0fb02017-02-28 15:15:56 -0500322static clib_error_t *
323clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
324 vlib_cli_command_t * cmd)
325{
326 session_manager_main_t *smm = &session_manager_main;
Dave Barach2c25a622017-06-26 11:35:07 -0400327 u32 thread_index = 0, clear_all = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500328 u32 session_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -0400329 stream_session_t **pool, *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500330
Florin Corase04c2992017-03-01 08:17:34 -0800331 if (!smm->is_enabled)
332 {
flyingeagle23e2125062017-04-20 20:01:14 +0800333 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800334 }
335
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
337 {
338 if (unformat (input, "thread %d", &thread_index))
339 ;
340 else if (unformat (input, "session %d", &session_index))
341 ;
Dave Barach2c25a622017-06-26 11:35:07 -0400342 else if (unformat (input, "all"))
343 clear_all = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500344 else
345 return clib_error_return (0, "unknown input `%U'",
346 format_unformat_error, input);
347 }
348
Dave Barach2c25a622017-06-26 11:35:07 -0400349 if (!clear_all && session_index == ~0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500350 return clib_error_return (0, "session <nn> required, but not set.");
351
Dave Barach2c25a622017-06-26 11:35:07 -0400352 if (session_index != ~0)
353 {
354 session = stream_session_get_if_valid (session_index, thread_index);
355 if (!session)
356 return clib_error_return (0, "no session %d on thread %d",
357 session_index, thread_index);
358 clear_session (session);
359 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500360
Dave Barach2c25a622017-06-26 11:35:07 -0400361 if (clear_all)
362 {
363 /* *INDENT-OFF* */
364 vec_foreach (pool, smm->sessions)
365 {
366 pool_foreach(session, *pool, ({
367 clear_session (session);
368 }));
369 };
370 /* *INDENT-ON* */
371 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500372
373 return 0;
374}
375
Florin Corase04c2992017-03-01 08:17:34 -0800376/* *INDENT-OFF* */
377VLIB_CLI_COMMAND (clear_session_command, static) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500378{
Florin Corase04c2992017-03-01 08:17:34 -0800379 .path = "clear session",
380 .short_help = "clear session thread <thread> session <index>",
381 .function = clear_session_command_fn,
382};
383/* *INDENT-ON* */
384
385static clib_error_t *
Florin Coras3eb50622017-07-13 01:24:57 -0400386show_session_fifo_trace_command_fn (vlib_main_t * vm,
387 unformat_input_t * input,
388 vlib_cli_command_t * cmd)
389{
390 stream_session_t *s = 0;
391 u8 is_rx = 0, *str = 0;
392
393 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
394 {
395 if (unformat (input, "%U", unformat_stream_session, &s))
396 ;
397 else if (unformat (input, "rx"))
398 is_rx = 1;
399 else if (unformat (input, "tx"))
400 is_rx = 0;
401 else
402 return clib_error_return (0, "unknown input `%U'",
403 format_unformat_error, input);
404 }
405
406 if (!SVM_FIFO_TRACE)
407 {
408 vlib_cli_output (vm, "fifo tracing not enabled");
409 return 0;
410 }
411
412 if (!s)
413 {
414 vlib_cli_output (vm, "could not find session");
415 return 0;
416 }
417
418 str = is_rx ?
419 svm_fifo_dump_trace (str, s->server_rx_fifo) :
420 svm_fifo_dump_trace (str, s->server_tx_fifo);
421
422 vlib_cli_output (vm, "%v", str);
423 return 0;
424}
425
426/* *INDENT-OFF* */
427VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
428{
429 .path = "show session fifo trace",
430 .short_help = "show session fifo trace <session>",
431 .function = show_session_fifo_trace_command_fn,
432};
433/* *INDENT-ON* */
434
435static clib_error_t *
436session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
437 vlib_cli_command_t * cmd)
438{
439 stream_session_t *s = 0;
440 u8 is_rx = 0, *str = 0;
441
442 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
443 {
444 if (unformat (input, "%U", unformat_stream_session, &s))
445 ;
446 else if (unformat (input, "rx"))
447 is_rx = 1;
448 else
449 return clib_error_return (0, "unknown input `%U'",
450 format_unformat_error, input);
451 }
452
453 if (!SVM_FIFO_TRACE)
454 {
455 vlib_cli_output (vm, "fifo tracing not enabled");
456 return 0;
457 }
458
459 if (!s)
460 {
461 vlib_cli_output (vm, "could not find session");
462 return 0;
463 }
464
465 str = is_rx ?
466 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
467 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
468
469 vlib_cli_output (vm, "%v", str);
470 return 0;
471}
472
473/* *INDENT-OFF* */
474VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
475{
476 .path = "session replay fifo",
477 .short_help = "session replay fifo <session>",
478 .function = session_replay_fifo_command_fn,
479};
480/* *INDENT-ON* */
481
482static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800483session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
484 vlib_cli_command_t * cmd)
485{
486 u8 is_en = 1;
487
488 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
489 {
490 if (unformat (input, "enable"))
491 is_en = 1;
492 else if (unformat (input, "disable"))
493 is_en = 0;
494 else
495 return clib_error_return (0, "unknown input `%U'",
496 format_unformat_error, input);
497 }
498
499 return vnet_session_enable_disable (vm, is_en);
500}
501
502/* *INDENT-OFF* */
503VLIB_CLI_COMMAND (session_enable_disable_command, static) =
504{
505 .path = "session",
506 .short_help = "session [enable|disable]",
507 .function = session_enable_disable_fn,
508};
509/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500510
511/*
512 * fd.io coding-style-patch-verification: ON
513 *
514 * Local Variables:
515 * eval: (c-set-style "gnu")
516 * End:
517 */