session: use generic session pool for listeners
[vpp.git] / src / vnet / session / session_cli.c
1 /*
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
18 u8 *
19 format_stream_session_fifos (u8 * s, va_list * args)
20 {
21   stream_session_t *ss = va_arg (*args, stream_session_t *);
22   int verbose = va_arg (*args, int);
23   session_fifo_event_t _e, *e = &_e;
24   u8 found;
25
26   s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
27   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     }
33   s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
34   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     }
40   return s;
41 }
42
43 /**
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  */
51 u8 *
52 format_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;
58   tp_vft = transport_protocol_get_vft (session_get_transport_proto (ss));
59
60   if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
61     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                   session_get_index (ss));
65
66   if (ss->session_state >= SESSION_STATE_ACCEPTING)
67     {
68       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);
72       if (verbose > 1)
73         s = format (s, "%U", format_stream_session_fifos, ss, verbose);
74     }
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     }
80   else if (ss->session_state == SESSION_STATE_CONNECTING)
81     {
82       s = format (s, "%-40U%v", tp_vft->format_half_open,
83                   ss->connection_index, str);
84     }
85   else
86     {
87       clib_warning ("Session in state: %d!", ss->session_state);
88     }
89   vec_free (str);
90
91   return s;
92 }
93
94 uword
95 unformat_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     }
116   if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
117                 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
118     {
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     }
128
129   return tuple_is_set;
130 }
131
132 uword
133 unformat_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;
139   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
140   u8 is_ip4 = 0;
141
142   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
143                  &lcl_port, &rmt_port, &is_ip4))
144     return 0;
145
146   if (is_ip4)
147     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);
150   else
151     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);
154   if (s)
155     {
156       *result = s;
157       session_pool_remove_peeker (s->thread_index);
158       return 1;
159     }
160   return 0;
161 }
162
163 uword
164 unformat_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;
171   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
172   u8 is_ip4 = 0;
173
174   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
175                  &lcl_port, &rmt_port, &is_ip4))
176     return 0;
177
178   proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
179   if (proto == (u8) ~ 0)
180     return 0;
181   if (is_ip4)
182     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);
185   else
186     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);
189
190   if (tc)
191     {
192       *result = tc;
193       return 1;
194     }
195   return 0;
196 }
197
198 static clib_error_t *
199 show_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   u8 *str = 0, one_session = 0, do_listeners = 0, sst, *app_name;
204   int verbose = 0, i;
205   stream_session_t *pool, *s;
206   u32 transport_proto = ~0;
207
208   if (!smm->is_enabled)
209     {
210       return clib_error_return (0, "session layer is not enabled");
211     }
212
213   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
214     {
215       if (unformat (input, "verbose %d", &verbose))
216         ;
217       else if (unformat (input, "verbose"))
218         verbose = 1;
219       else if (unformat (input, "listeners %U", unformat_transport_proto,
220                          &transport_proto))
221         do_listeners = 1;
222       else if (unformat (input, "%U", unformat_stream_session, &s))
223         {
224           one_session = 1;
225         }
226       else
227         return clib_error_return (0, "unknown input `%U'",
228                                   format_unformat_error, input);
229     }
230
231   if (one_session)
232     {
233       vlib_cli_output (vm, "%U", format_stream_session, s, 3);
234       return 0;
235     }
236
237   if (do_listeners)
238     {
239       sst = session_type_from_proto_and_ip (transport_proto, 1);
240       vlib_cli_output (vm, "%-40s%-24s%-10s", "Listener", "App", "S-idx");
241       /* *INDENT-OFF* */
242       pool_foreach (s, smm->sessions[0], ({
243         if (s->session_state != SESSION_STATE_LISTENING
244             || s->session_type != sst)
245           continue;
246         app_name = application_name_from_index (s->app_index);
247         vlib_cli_output (vm, "%U%-25v%-10u", format_stream_session, s, 1,
248                          app_name, s->session_index);
249         vec_free (app_name);
250       }));
251       /* *INDENT-ON* */
252       return 0;
253     }
254
255   for (i = 0; i < vec_len (smm->sessions); i++)
256     {
257       u32 once_per_pool;
258       pool = smm->sessions[i];
259
260       once_per_pool = 1;
261
262       if (pool_elts (pool))
263         {
264
265           vlib_cli_output (vm, "Thread %d: %d active sessions",
266                            i, pool_elts (pool));
267           if (verbose)
268             {
269               if (once_per_pool && verbose == 1)
270                 {
271                   str = format (str, "%-50s%-15s%-10s%-10s%-10s",
272                                 "Connection", "State", "Rx-f", "Tx-f",
273                                 "S-idx");
274                   vlib_cli_output (vm, "%v", str);
275                   vec_reset_length (str);
276                   once_per_pool = 0;
277                 }
278
279               /* *INDENT-OFF* */
280               pool_foreach (s, pool,
281               ({
282                 vec_reset_length (str);
283                 str = format (str, "%U", format_stream_session, s, verbose);
284                 vlib_cli_output (vm, "%v", str);
285               }));
286               /* *INDENT-ON* */
287             }
288         }
289       else
290         vlib_cli_output (vm, "Thread %d: no active sessions", i);
291       vec_reset_length (str);
292     }
293   vec_free (str);
294
295   return 0;
296 }
297
298 /* *INDENT-OFF* */
299 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
300 {
301   .path = "show session",
302   .short_help = "show session [verbose [nnn]]",
303   .function = show_session_command_fn,
304 };
305 /* *INDENT-ON* */
306
307 static int
308 clear_session (stream_session_t * s)
309 {
310   application_t *server = application_get (s->app_index);
311   server->cb_fns.session_disconnect_callback (s);
312   return 0;
313 }
314
315 static clib_error_t *
316 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
317                           vlib_cli_command_t * cmd)
318 {
319   session_manager_main_t *smm = &session_manager_main;
320   u32 thread_index = 0, clear_all = 0;
321   u32 session_index = ~0;
322   stream_session_t **pool, *session;
323
324   if (!smm->is_enabled)
325     {
326       return clib_error_return (0, "session layer is not enabled");
327     }
328
329   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
330     {
331       if (unformat (input, "thread %d", &thread_index))
332         ;
333       else if (unformat (input, "session %d", &session_index))
334         ;
335       else if (unformat (input, "all"))
336         clear_all = 1;
337       else
338         return clib_error_return (0, "unknown input `%U'",
339                                   format_unformat_error, input);
340     }
341
342   if (!clear_all && session_index == ~0)
343     return clib_error_return (0, "session <nn> required, but not set.");
344
345   if (session_index != ~0)
346     {
347       session = session_get_if_valid (session_index, thread_index);
348       if (!session)
349         return clib_error_return (0, "no session %d on thread %d",
350                                   session_index, thread_index);
351       clear_session (session);
352     }
353
354   if (clear_all)
355     {
356       /* *INDENT-OFF* */
357       vec_foreach (pool, smm->sessions)
358         {
359           pool_foreach(session, *pool, ({
360             clear_session (session);
361           }));
362         };
363       /* *INDENT-ON* */
364     }
365
366   return 0;
367 }
368
369 /* *INDENT-OFF* */
370 VLIB_CLI_COMMAND (clear_session_command, static) =
371 {
372   .path = "clear session",
373   .short_help = "clear session thread <thread> session <index>",
374   .function = clear_session_command_fn,
375 };
376 /* *INDENT-ON* */
377
378 static clib_error_t *
379 show_session_fifo_trace_command_fn (vlib_main_t * vm,
380                                     unformat_input_t * input,
381                                     vlib_cli_command_t * cmd)
382 {
383   stream_session_t *s = 0;
384   u8 is_rx = 0, *str = 0;
385
386   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
387     {
388       if (unformat (input, "%U", unformat_stream_session, &s))
389         ;
390       else if (unformat (input, "rx"))
391         is_rx = 1;
392       else if (unformat (input, "tx"))
393         is_rx = 0;
394       else
395         return clib_error_return (0, "unknown input `%U'",
396                                   format_unformat_error, input);
397     }
398
399   if (!SVM_FIFO_TRACE)
400     {
401       vlib_cli_output (vm, "fifo tracing not enabled");
402       return 0;
403     }
404
405   if (!s)
406     {
407       vlib_cli_output (vm, "could not find session");
408       return 0;
409     }
410
411   str = is_rx ?
412     svm_fifo_dump_trace (str, s->server_rx_fifo) :
413     svm_fifo_dump_trace (str, s->server_tx_fifo);
414
415   vlib_cli_output (vm, "%v", str);
416   return 0;
417 }
418
419 /* *INDENT-OFF* */
420 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
421 {
422   .path = "show session fifo trace",
423   .short_help = "show session fifo trace <session>",
424   .function = show_session_fifo_trace_command_fn,
425 };
426 /* *INDENT-ON* */
427
428 static clib_error_t *
429 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
430                                 vlib_cli_command_t * cmd)
431 {
432   stream_session_t *s = 0;
433   u8 is_rx = 0, *str = 0;
434
435   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
436     {
437       if (unformat (input, "%U", unformat_stream_session, &s))
438         ;
439       else if (unformat (input, "rx"))
440         is_rx = 1;
441       else
442         return clib_error_return (0, "unknown input `%U'",
443                                   format_unformat_error, input);
444     }
445
446   if (!SVM_FIFO_TRACE)
447     {
448       vlib_cli_output (vm, "fifo tracing not enabled");
449       return 0;
450     }
451
452   if (!s)
453     {
454       vlib_cli_output (vm, "could not find session");
455       return 0;
456     }
457
458   str = is_rx ?
459     svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
460     svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
461
462   vlib_cli_output (vm, "%v", str);
463   return 0;
464 }
465
466 /* *INDENT-OFF* */
467 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
468 {
469   .path = "session replay fifo",
470   .short_help = "session replay fifo <session>",
471   .function = session_replay_fifo_command_fn,
472 };
473 /* *INDENT-ON* */
474
475 static clib_error_t *
476 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
477                            vlib_cli_command_t * cmd)
478 {
479   unformat_input_t _line_input, *line_input = &_line_input;
480   u8 is_en = 1;
481   clib_error_t *error;
482
483   if (!unformat_user (input, unformat_line_input, line_input))
484     return clib_error_return (0, "expected enable | disable");
485
486   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
487     {
488       if (unformat (line_input, "enable"))
489         is_en = 1;
490       else if (unformat (line_input, "disable"))
491         is_en = 0;
492       else
493         {
494           error = clib_error_return (0, "unknown input `%U'",
495                                      format_unformat_error, line_input);
496           unformat_free (line_input);
497           return error;
498         }
499     }
500
501   unformat_free (line_input);
502   return vnet_session_enable_disable (vm, is_en);
503 }
504
505 /* *INDENT-OFF* */
506 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
507 {
508   .path = "session",
509   .short_help = "session [enable|disable]",
510   .function = session_enable_disable_fn,
511 };
512 /* *INDENT-ON* */
513
514 /*
515  * fd.io coding-style-patch-verification: ON
516  *
517  * Local Variables:
518  * eval: (c-set-style "gnu")
519  * End:
520  */