blob: 95fc48d75df8dd434ce1267edf62081f0765dd11 [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;
Florin Coras3cbc04b2017-10-02 00:18:51 -070058 tp_vft = transport_protocol_get_vft (ss->session_type);
Dave Barach68b0fb02017-02-28 15:15:56 -050059
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
Florin Coras3cbc04b2017-10-02 00:18:51 -070066 if (ss->session_state >= SESSION_STATE_ACCEPTING)
Dave Barach68b0fb02017-02-28 15:15:56 -050067 {
Florin Corasbb292f42017-05-19 09:49:19 -070068 s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
69 ss->thread_index, verbose);
70 if (verbose == 1)
71 s = format (s, "%v", str);
Florin Coras93992a92017-05-24 18:03:56 -070072 if (verbose > 1)
Florin Coras6534b7a2017-07-18 05:38:03 -040073 s = format (s, "%U", format_stream_session_fifos, ss, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -050074 }
75 else if (ss->session_state == SESSION_STATE_LISTENING)
76 {
77 s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
78 str);
79 }
Florin Corasbb292f42017-05-19 09:49:19 -070080 else if (ss->session_state == SESSION_STATE_CONNECTING)
Dave Barach68b0fb02017-02-28 15:15:56 -050081 {
Florin Corasbb292f42017-05-19 09:49:19 -070082 s = format (s, "%-40U%v", tp_vft->format_half_open,
83 ss->connection_index, str);
Dave Barach68b0fb02017-02-28 15:15:56 -050084 }
Dave Barach68b0fb02017-02-28 15:15:56 -050085 else
86 {
Florin Corase04c2992017-03-01 08:17:34 -080087 clib_warning ("Session in state: %d!", ss->session_state);
Dave Barach68b0fb02017-02-28 15:15:56 -050088 }
Dave Barach68b0fb02017-02-28 15:15:56 -050089 vec_free (str);
90
91 return s;
92}
93
Florin Coras3eb50622017-07-13 01:24:57 -040094uword
95unformat_stream_session_id (unformat_input_t * input, va_list * args)
96{
97 u8 *proto = va_arg (*args, u8 *);
98 ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
99 ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
100 u16 *lcl_port = va_arg (*args, u16 *);
101 u16 *rmt_port = va_arg (*args, u16 *);
102 u8 *is_ip4 = va_arg (*args, u8 *);
103 u8 tuple_is_set = 0;
104
105 memset (lcl, 0, sizeof (*lcl));
106 memset (rmt, 0, sizeof (*rmt));
107
108 if (unformat (input, "tcp"))
109 {
110 *proto = TRANSPORT_PROTO_TCP;
111 }
112 if (unformat (input, "udp"))
113 {
114 *proto = TRANSPORT_PROTO_UDP;
115 }
Dave Barachb7f1faa2017-08-29 11:43:37 -0400116 if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
117 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
Florin Coras3eb50622017-07-13 01:24:57 -0400118 {
119 *is_ip4 = 1;
120 tuple_is_set = 1;
121 }
122 else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
123 lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
124 {
125 *is_ip4 = 0;
126 tuple_is_set = 1;
127 }
Florin Coras3eb50622017-07-13 01:24:57 -0400128
Chris Lukeb2bcad62017-09-18 08:51:22 -0400129 return tuple_is_set;
Florin Coras3eb50622017-07-13 01:24:57 -0400130}
131
132uword
133unformat_stream_session (unformat_input_t * input, va_list * args)
134{
135 stream_session_t **result = va_arg (*args, stream_session_t **);
136 stream_session_t *s;
137 u8 proto = ~0;
138 ip46_address_t lcl, rmt;
Florin Corascea194d2017-10-02 00:18:51 -0700139 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
140 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400141
Chris Lukeb2bcad62017-09-18 08:51:22 -0400142 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
143 &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400144 return 0;
145
Florin Coras3eb50622017-07-13 01:24:57 -0400146 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700147 s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
148 clib_host_to_net_u16 (lcl_port),
149 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400150 else
Florin Coras3cbc04b2017-10-02 00:18:51 -0700151 s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
152 clib_host_to_net_u16 (lcl_port),
153 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400154 if (s)
155 {
156 *result = s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700157 session_pool_remove_peeker (s->thread_index);
Florin Coras3eb50622017-07-13 01:24:57 -0400158 return 1;
159 }
160 return 0;
161}
162
163uword
164unformat_transport_connection (unformat_input_t * input, va_list * args)
165{
166 transport_connection_t **result = va_arg (*args, transport_connection_t **);
167 u32 suggested_proto = va_arg (*args, u32);
168 transport_connection_t *tc;
169 u8 proto = ~0;
170 ip46_address_t lcl, rmt;
Florin Corascea194d2017-10-02 00:18:51 -0700171 u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
172 u8 is_ip4 = 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400173
Chris Lukeb2bcad62017-09-18 08:51:22 -0400174 if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
175 &lcl_port, &rmt_port, &is_ip4))
Florin Coras3eb50622017-07-13 01:24:57 -0400176 return 0;
177
Florin Coras3eb50622017-07-13 01:24:57 -0400178 proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
179 if (proto == (u8) ~ 0)
180 return 0;
Florin Coras3eb50622017-07-13 01:24:57 -0400181 if (is_ip4)
Florin Corascea194d2017-10-02 00:18:51 -0700182 tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
183 clib_host_to_net_u16 (lcl_port),
184 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400185 else
Florin Corascea194d2017-10-02 00:18:51 -0700186 tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
187 clib_host_to_net_u16 (lcl_port),
188 clib_host_to_net_u16 (rmt_port), proto);
Florin Coras3eb50622017-07-13 01:24:57 -0400189
190 if (tc)
191 {
192 *result = tc;
193 return 1;
194 }
195 return 0;
196}
197
Dave Barach68b0fb02017-02-28 15:15:56 -0500198static clib_error_t *
199show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
200 vlib_cli_command_t * cmd)
201{
202 session_manager_main_t *smm = &session_manager_main;
203 int verbose = 0, i;
204 stream_session_t *pool;
205 stream_session_t *s;
Florin Coras3eb50622017-07-13 01:24:57 -0400206 u8 *str = 0, one_session = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500207
Florin Corase04c2992017-03-01 08:17:34 -0800208 if (!smm->is_enabled)
209 {
flyingeagle23e2125062017-04-20 20:01:14 +0800210 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800211 }
212
Dave Barach68b0fb02017-02-28 15:15:56 -0500213 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
214 {
Florin Corasbb292f42017-05-19 09:49:19 -0700215 if (unformat (input, "verbose %d", &verbose))
216 ;
217 else if (unformat (input, "verbose"))
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 verbose = 1;
Florin Coras3eb50622017-07-13 01:24:57 -0400219 else if (unformat (input, "%U", unformat_stream_session, &s))
Dave Barach2c25a622017-06-26 11:35:07 -0400220 {
221 one_session = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400222 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 else
Florin Coras3eb50622017-07-13 01:24:57 -0400224 return clib_error_return (0, "unknown input `%U'",
225 format_unformat_error, input);
Dave Barach68b0fb02017-02-28 15:15:56 -0500226 }
227
Dave Barach2c25a622017-06-26 11:35:07 -0400228 if (one_session)
229 {
Florin Coras6534b7a2017-07-18 05:38:03 -0400230 vlib_cli_output (vm, "%U", format_stream_session, s, 3);
Dave Barach2c25a622017-06-26 11:35:07 -0400231 return 0;
232 }
233
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 for (i = 0; i < vec_len (smm->sessions); i++)
235 {
236 u32 once_per_pool;
237 pool = smm->sessions[i];
238
239 once_per_pool = 1;
240
241 if (pool_elts (pool))
242 {
243
244 vlib_cli_output (vm, "Thread %d: %d active sessions",
245 i, pool_elts (pool));
246 if (verbose)
247 {
Florin Corasbb292f42017-05-19 09:49:19 -0700248 if (once_per_pool && verbose == 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500249 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700250 str = format (str, "%-50s%-15s%-10s%-10s%-10s",
251 "Connection", "State", "Rx-f", "Tx-f",
252 "S-idx");
Dave Barach68b0fb02017-02-28 15:15:56 -0500253 vlib_cli_output (vm, "%v", str);
254 vec_reset_length (str);
255 once_per_pool = 0;
256 }
257
258 /* *INDENT-OFF* */
259 pool_foreach (s, pool,
260 ({
Florin Corasbb292f42017-05-19 09:49:19 -0700261 vec_reset_length (str);
262 str = format (str, "%U", format_stream_session, s, verbose);
Florin Corasbb292f42017-05-19 09:49:19 -0700263 vlib_cli_output (vm, "%v", str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500264 }));
265 /* *INDENT-ON* */
266 }
267 }
268 else
269 vlib_cli_output (vm, "Thread %d: no active sessions", i);
Dave Barach2c25a622017-06-26 11:35:07 -0400270 vec_reset_length (str);
Dave Barach68b0fb02017-02-28 15:15:56 -0500271 }
272 vec_free (str);
273
274 return 0;
275}
276
Florin Corase04c2992017-03-01 08:17:34 -0800277/* *INDENT-OFF* */
Florin Coras66b11312017-07-31 17:18:03 -0700278VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500279{
Florin Corase04c2992017-03-01 08:17:34 -0800280 .path = "show session",
rootc9d1c5b2017-08-15 12:58:31 -0400281 .short_help = "show session [verbose [nnn]]",
Florin Corase04c2992017-03-01 08:17:34 -0800282 .function = show_session_command_fn,
283};
284/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500285
Dave Barach2c25a622017-06-26 11:35:07 -0400286static int
287clear_session (stream_session_t * s)
288{
289 application_t *server = application_get (s->app_index);
290 server->cb_fns.session_disconnect_callback (s);
291 return 0;
292}
293
Dave Barach68b0fb02017-02-28 15:15:56 -0500294static clib_error_t *
295clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
296 vlib_cli_command_t * cmd)
297{
298 session_manager_main_t *smm = &session_manager_main;
Dave Barach2c25a622017-06-26 11:35:07 -0400299 u32 thread_index = 0, clear_all = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500300 u32 session_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -0400301 stream_session_t **pool, *session;
Dave Barach68b0fb02017-02-28 15:15:56 -0500302
Florin Corase04c2992017-03-01 08:17:34 -0800303 if (!smm->is_enabled)
304 {
flyingeagle23e2125062017-04-20 20:01:14 +0800305 return clib_error_return (0, "session layer is not enabled");
Florin Corase04c2992017-03-01 08:17:34 -0800306 }
307
Dave Barach68b0fb02017-02-28 15:15:56 -0500308 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
309 {
310 if (unformat (input, "thread %d", &thread_index))
311 ;
312 else if (unformat (input, "session %d", &session_index))
313 ;
Dave Barach2c25a622017-06-26 11:35:07 -0400314 else if (unformat (input, "all"))
315 clear_all = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500316 else
317 return clib_error_return (0, "unknown input `%U'",
318 format_unformat_error, input);
319 }
320
Dave Barach2c25a622017-06-26 11:35:07 -0400321 if (!clear_all && session_index == ~0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500322 return clib_error_return (0, "session <nn> required, but not set.");
323
Dave Barach2c25a622017-06-26 11:35:07 -0400324 if (session_index != ~0)
325 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700326 session = session_get_if_valid (session_index, thread_index);
Dave Barach2c25a622017-06-26 11:35:07 -0400327 if (!session)
328 return clib_error_return (0, "no session %d on thread %d",
329 session_index, thread_index);
330 clear_session (session);
331 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500332
Dave Barach2c25a622017-06-26 11:35:07 -0400333 if (clear_all)
334 {
335 /* *INDENT-OFF* */
336 vec_foreach (pool, smm->sessions)
337 {
338 pool_foreach(session, *pool, ({
339 clear_session (session);
340 }));
341 };
342 /* *INDENT-ON* */
343 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500344
345 return 0;
346}
347
Florin Corase04c2992017-03-01 08:17:34 -0800348/* *INDENT-OFF* */
349VLIB_CLI_COMMAND (clear_session_command, static) =
Dave Barach68b0fb02017-02-28 15:15:56 -0500350{
Florin Corase04c2992017-03-01 08:17:34 -0800351 .path = "clear session",
352 .short_help = "clear session thread <thread> session <index>",
353 .function = clear_session_command_fn,
354};
355/* *INDENT-ON* */
356
357static clib_error_t *
Florin Coras3eb50622017-07-13 01:24:57 -0400358show_session_fifo_trace_command_fn (vlib_main_t * vm,
359 unformat_input_t * input,
360 vlib_cli_command_t * cmd)
361{
362 stream_session_t *s = 0;
363 u8 is_rx = 0, *str = 0;
364
365 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
366 {
367 if (unformat (input, "%U", unformat_stream_session, &s))
368 ;
369 else if (unformat (input, "rx"))
370 is_rx = 1;
371 else if (unformat (input, "tx"))
372 is_rx = 0;
373 else
374 return clib_error_return (0, "unknown input `%U'",
375 format_unformat_error, input);
376 }
377
378 if (!SVM_FIFO_TRACE)
379 {
380 vlib_cli_output (vm, "fifo tracing not enabled");
381 return 0;
382 }
383
384 if (!s)
385 {
386 vlib_cli_output (vm, "could not find session");
387 return 0;
388 }
389
390 str = is_rx ?
391 svm_fifo_dump_trace (str, s->server_rx_fifo) :
392 svm_fifo_dump_trace (str, s->server_tx_fifo);
393
394 vlib_cli_output (vm, "%v", str);
395 return 0;
396}
397
398/* *INDENT-OFF* */
399VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
400{
401 .path = "show session fifo trace",
402 .short_help = "show session fifo trace <session>",
403 .function = show_session_fifo_trace_command_fn,
404};
405/* *INDENT-ON* */
406
407static clib_error_t *
408session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
409 vlib_cli_command_t * cmd)
410{
411 stream_session_t *s = 0;
412 u8 is_rx = 0, *str = 0;
413
414 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
415 {
416 if (unformat (input, "%U", unformat_stream_session, &s))
417 ;
418 else if (unformat (input, "rx"))
419 is_rx = 1;
420 else
421 return clib_error_return (0, "unknown input `%U'",
422 format_unformat_error, input);
423 }
424
425 if (!SVM_FIFO_TRACE)
426 {
427 vlib_cli_output (vm, "fifo tracing not enabled");
428 return 0;
429 }
430
431 if (!s)
432 {
433 vlib_cli_output (vm, "could not find session");
434 return 0;
435 }
436
437 str = is_rx ?
438 svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
439 svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
440
441 vlib_cli_output (vm, "%v", str);
442 return 0;
443}
444
445/* *INDENT-OFF* */
446VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
447{
448 .path = "session replay fifo",
449 .short_help = "session replay fifo <session>",
450 .function = session_replay_fifo_command_fn,
451};
452/* *INDENT-ON* */
453
454static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -0800455session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
456 vlib_cli_command_t * cmd)
457{
Dave Wallace8af20542017-10-26 03:29:30 -0400458 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase04c2992017-03-01 08:17:34 -0800459 u8 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400460 clib_error_t *error;
Florin Corase04c2992017-03-01 08:17:34 -0800461
Dave Wallace8af20542017-10-26 03:29:30 -0400462 if (!unformat_user (input, unformat_line_input, line_input))
463 return 0;
464
465 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corase04c2992017-03-01 08:17:34 -0800466 {
Dave Wallace8af20542017-10-26 03:29:30 -0400467 if (unformat (line_input, "enable"))
Florin Corase04c2992017-03-01 08:17:34 -0800468 is_en = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400469 else if (unformat (line_input, "disable"))
Florin Corase04c2992017-03-01 08:17:34 -0800470 is_en = 0;
471 else
Dave Wallace8af20542017-10-26 03:29:30 -0400472 {
473 error = clib_error_return (0, "unknown input `%U'",
474 format_unformat_error, line_input);
475 unformat_free (line_input);
476 return error;
477 }
Florin Corase04c2992017-03-01 08:17:34 -0800478 }
479
Dave Wallace8af20542017-10-26 03:29:30 -0400480 unformat_free (line_input);
Florin Corase04c2992017-03-01 08:17:34 -0800481 return vnet_session_enable_disable (vm, is_en);
482}
483
484/* *INDENT-OFF* */
485VLIB_CLI_COMMAND (session_enable_disable_command, static) =
486{
487 .path = "session",
488 .short_help = "session [enable|disable]",
489 .function = session_enable_disable_fn,
490};
491/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500492
493/*
494 * fd.io coding-style-patch-verification: ON
495 *
496 * Local Variables:
497 * eval: (c-set-style "gnu")
498 * End:
499 */