vcl: use events for epoll/select/read/write
[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_event_t _e, *e = &_e;
24   u8 found;
25
26   if (!ss->server_rx_fifo || !ss->server_tx_fifo)
27     return s;
28
29   s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo,
30               verbose);
31   if (verbose > 2 && ss->server_rx_fifo->has_event)
32     {
33       found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
34       s = format (s, " session node event: %s\n",
35                   found ? "found" : "not found");
36     }
37   s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo,
38               verbose);
39   if (verbose > 2 && ss->server_tx_fifo->has_event)
40     {
41       found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
42       s = format (s, " session node event: %s\n",
43                   found ? "found" : "not found");
44     }
45   return s;
46 }
47
48 /**
49  * Format stream session as per the following format
50  *
51  * verbose:
52  *   "Connection", "Rx fifo", "Tx fifo", "Session Index"
53  * non-verbose:
54  *   "Connection"
55  */
56 u8 *
57 format_stream_session (u8 * s, va_list * args)
58 {
59   stream_session_t *ss = va_arg (*args, stream_session_t *);
60   int verbose = va_arg (*args, int);
61   transport_proto_vft_t *tp_vft;
62   u8 *str = 0;
63   tp_vft = transport_protocol_get_vft (session_get_transport_proto (ss));
64
65   if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
66     str = format (0, "%-10u%-10u%-10lld",
67                   svm_fifo_max_dequeue (ss->server_rx_fifo),
68                   svm_fifo_max_enqueue (ss->server_tx_fifo),
69                   session_get_index (ss));
70
71   if (ss->session_state >= SESSION_STATE_ACCEPTING)
72     {
73       s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
74                   ss->thread_index, verbose);
75       if (verbose == 1)
76         s = format (s, "%v", str);
77       if (verbose > 1)
78         s = format (s, "%U", format_stream_session_fifos, ss, verbose);
79     }
80   else if (ss->session_state == SESSION_STATE_LISTENING)
81     {
82       s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index,
83                   str);
84       if (verbose > 1)
85         s = format (s, "\n%U", format_stream_session_fifos, ss, verbose);
86     }
87   else if (ss->session_state == SESSION_STATE_CONNECTING)
88     {
89       s = format (s, "%-40U%v", tp_vft->format_half_open,
90                   ss->connection_index, str);
91     }
92   else
93     {
94       clib_warning ("Session in state: %d!", ss->session_state);
95     }
96   vec_free (str);
97
98   return s;
99 }
100
101 uword
102 unformat_stream_session_id (unformat_input_t * input, va_list * args)
103 {
104   u8 *proto = va_arg (*args, u8 *);
105   ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
106   ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
107   u16 *lcl_port = va_arg (*args, u16 *);
108   u16 *rmt_port = va_arg (*args, u16 *);
109   u8 *is_ip4 = va_arg (*args, u8 *);
110   u8 tuple_is_set = 0;
111
112   memset (lcl, 0, sizeof (*lcl));
113   memset (rmt, 0, sizeof (*rmt));
114
115   if (unformat (input, "tcp"))
116     {
117       *proto = TRANSPORT_PROTO_TCP;
118     }
119   if (unformat (input, "udp"))
120     {
121       *proto = TRANSPORT_PROTO_UDP;
122     }
123   if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
124                 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
125     {
126       *is_ip4 = 1;
127       tuple_is_set = 1;
128     }
129   else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
130                      lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
131     {
132       *is_ip4 = 0;
133       tuple_is_set = 1;
134     }
135
136   return tuple_is_set;
137 }
138
139 uword
140 unformat_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, fib_index = 0;
147   u8 is_ip4 = 0;
148
149   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
150                  &lcl_port, &rmt_port, &is_ip4))
151     return 0;
152
153   if (is_ip4)
154     s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
155                               clib_host_to_net_u16 (lcl_port),
156                               clib_host_to_net_u16 (rmt_port), proto);
157   else
158     s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
159                               clib_host_to_net_u16 (lcl_port),
160                               clib_host_to_net_u16 (rmt_port), proto);
161   if (s)
162     {
163       *result = s;
164       session_pool_remove_peeker (s->thread_index);
165       return 1;
166     }
167   return 0;
168 }
169
170 uword
171 unformat_transport_connection (unformat_input_t * input, va_list * args)
172 {
173   transport_connection_t **result = va_arg (*args, transport_connection_t **);
174   u32 suggested_proto = va_arg (*args, u32);
175   transport_connection_t *tc;
176   u8 proto = ~0;
177   ip46_address_t lcl, rmt;
178   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
179   u8 is_ip4 = 0;
180
181   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
182                  &lcl_port, &rmt_port, &is_ip4))
183     return 0;
184
185   proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
186   if (proto == (u8) ~ 0)
187     return 0;
188   if (is_ip4)
189     tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
190                                      clib_host_to_net_u16 (lcl_port),
191                                      clib_host_to_net_u16 (rmt_port), proto);
192   else
193     tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
194                                      clib_host_to_net_u16 (lcl_port),
195                                      clib_host_to_net_u16 (rmt_port), proto);
196
197   if (tc)
198     {
199       *result = tc;
200       return 1;
201     }
202   return 0;
203 }
204
205 static clib_error_t *
206 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
207                          vlib_cli_command_t * cmd)
208 {
209   session_manager_main_t *smm = &session_manager_main;
210   u8 *str = 0, one_session = 0, do_listeners = 0, sst, *app_name;
211   int verbose = 0, i;
212   stream_session_t *pool, *s;
213   u32 transport_proto = ~0;
214
215   if (!smm->is_enabled)
216     {
217       return clib_error_return (0, "session layer is not enabled");
218     }
219
220   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
221     {
222       if (unformat (input, "verbose %d", &verbose))
223         ;
224       else if (unformat (input, "verbose"))
225         verbose = 1;
226       else if (unformat (input, "listeners %U", unformat_transport_proto,
227                          &transport_proto))
228         do_listeners = 1;
229       else if (unformat (input, "%U", unformat_stream_session, &s))
230         {
231           one_session = 1;
232         }
233       else
234         return clib_error_return (0, "unknown input `%U'",
235                                   format_unformat_error, input);
236     }
237
238   if (one_session)
239     {
240       vlib_cli_output (vm, "%U", format_stream_session, s, 3);
241       return 0;
242     }
243
244   if (do_listeners)
245     {
246       sst = session_type_from_proto_and_ip (transport_proto, 1);
247       vlib_cli_output (vm, "%-40s%-24s%-10s", "Listener", "App", "S-idx");
248       /* *INDENT-OFF* */
249       pool_foreach (s, smm->sessions[0], ({
250         if (s->session_state != SESSION_STATE_LISTENING
251             || s->session_type != sst)
252           continue;
253         app_name = application_name_from_index (s->app_index);
254         vlib_cli_output (vm, "%U%-25v%-10u", format_stream_session, s, 1,
255                          app_name, s->session_index);
256         vec_free (app_name);
257       }));
258       /* *INDENT-ON* */
259       return 0;
260     }
261
262   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             {
276               if (once_per_pool && verbose == 1)
277                 {
278                   str = format (str, "%-50s%-15s%-10s%-10s%-10s",
279                                 "Connection", "State", "Rx-f", "Tx-f",
280                                 "S-idx");
281                   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               ({
289                 vec_reset_length (str);
290                 str = format (str, "%U", format_stream_session, s, verbose);
291                 vlib_cli_output (vm, "%v", str);
292               }));
293               /* *INDENT-ON* */
294             }
295         }
296       else
297         vlib_cli_output (vm, "Thread %d: no active sessions", i);
298       vec_reset_length (str);
299     }
300   vec_free (str);
301
302   return 0;
303 }
304
305 /* *INDENT-OFF* */
306 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
307 {
308   .path = "show session",
309   .short_help = "show session [verbose [nnn]]",
310   .function = show_session_command_fn,
311 };
312 /* *INDENT-ON* */
313
314 static int
315 clear_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
322 static clib_error_t *
323 clear_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;
327   u32 thread_index = 0, clear_all = 0;
328   u32 session_index = ~0;
329   stream_session_t **pool, *session;
330
331   if (!smm->is_enabled)
332     {
333       return clib_error_return (0, "session layer is not enabled");
334     }
335
336   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         ;
342       else if (unformat (input, "all"))
343         clear_all = 1;
344       else
345         return clib_error_return (0, "unknown input `%U'",
346                                   format_unformat_error, input);
347     }
348
349   if (!clear_all && session_index == ~0)
350     return clib_error_return (0, "session <nn> required, but not set.");
351
352   if (session_index != ~0)
353     {
354       session = 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     }
360
361   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     }
372
373   return 0;
374 }
375
376 /* *INDENT-OFF* */
377 VLIB_CLI_COMMAND (clear_session_command, static) =
378 {
379   .path = "clear session",
380   .short_help = "clear session thread <thread> session <index>",
381   .function = clear_session_command_fn,
382 };
383 /* *INDENT-ON* */
384
385 static clib_error_t *
386 show_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* */
427 VLIB_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
435 static clib_error_t *
436 session_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* */
474 VLIB_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
482 static clib_error_t *
483 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
484                            vlib_cli_command_t * cmd)
485 {
486   unformat_input_t _line_input, *line_input = &_line_input;
487   u8 is_en = 1;
488   clib_error_t *error;
489
490   if (!unformat_user (input, unformat_line_input, line_input))
491     return clib_error_return (0, "expected enable | disable");
492
493   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
494     {
495       if (unformat (line_input, "enable"))
496         is_en = 1;
497       else if (unformat (line_input, "disable"))
498         is_en = 0;
499       else
500         {
501           error = clib_error_return (0, "unknown input `%U'",
502                                      format_unformat_error, line_input);
503           unformat_free (line_input);
504           return error;
505         }
506     }
507
508   unformat_free (line_input);
509   return vnet_session_enable_disable (vm, is_en);
510 }
511
512 /* *INDENT-OFF* */
513 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
514 {
515   .path = "session",
516   .short_help = "session [enable|disable]",
517   .function = session_enable_disable_fn,
518 };
519 /* *INDENT-ON* */
520
521 /*
522  * fd.io coding-style-patch-verification: ON
523  *
524  * Local Variables:
525  * eval: (c-set-style "gnu")
526  * End:
527  */