http server: improvements
[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_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   u32 tp = session_get_transport_proto (ss);
62   u8 *str = 0;
63
64   if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
65     str = format (0, "%-10u%-10u",
66                   svm_fifo_max_dequeue (ss->server_rx_fifo),
67                   svm_fifo_max_dequeue (ss->server_tx_fifo));
68
69   if (ss->session_state >= SESSION_STATE_ACCEPTING)
70     {
71       s = format (s, "%U", format_transport_connection, tp,
72                   ss->connection_index, ss->thread_index, verbose);
73       if (verbose == 1)
74         s = format (s, "%v", str);
75       if (verbose > 1)
76         s = format (s, "%U", format_session_fifos, ss, verbose);
77     }
78   else if (ss->session_state == SESSION_STATE_LISTENING)
79     {
80       s = format (s, "%-40U%v", format_transport_listen_connection,
81                   tp, ss->connection_index, str);
82       if (verbose > 1)
83         s = format (s, "\n%U", format_session_fifos, ss, verbose);
84     }
85   else if (ss->session_state == SESSION_STATE_CONNECTING)
86     {
87       s = format (s, "%-40U%v", format_transport_half_open_connection,
88                   tp, ss->connection_index, str);
89     }
90   else
91     {
92       clib_warning ("Session in state: %d!", ss->session_state);
93     }
94   vec_free (str);
95
96   return s;
97 }
98
99 uword
100 unformat_stream_session_id (unformat_input_t * input, va_list * args)
101 {
102   u8 *proto = va_arg (*args, u8 *);
103   ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
104   ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
105   u16 *lcl_port = va_arg (*args, u16 *);
106   u16 *rmt_port = va_arg (*args, u16 *);
107   u8 *is_ip4 = va_arg (*args, u8 *);
108   u8 tuple_is_set = 0;
109
110   clib_memset (lcl, 0, sizeof (*lcl));
111   clib_memset (rmt, 0, sizeof (*rmt));
112
113   if (unformat (input, "tcp"))
114     {
115       *proto = TRANSPORT_PROTO_TCP;
116     }
117   if (unformat (input, "udp"))
118     {
119       *proto = TRANSPORT_PROTO_UDP;
120     }
121   if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
122                 lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
123     {
124       *is_ip4 = 1;
125       tuple_is_set = 1;
126     }
127   else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
128                      lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
129     {
130       *is_ip4 = 0;
131       tuple_is_set = 1;
132     }
133
134   return tuple_is_set;
135 }
136
137 uword
138 unformat_stream_session (unformat_input_t * input, va_list * args)
139 {
140   stream_session_t **result = va_arg (*args, stream_session_t **);
141   stream_session_t *s;
142   u8 proto = ~0;
143   ip46_address_t lcl, rmt;
144   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
145   u8 is_ip4 = 0;
146
147   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
148                  &lcl_port, &rmt_port, &is_ip4))
149     return 0;
150
151   if (is_ip4)
152     s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
153                               clib_host_to_net_u16 (lcl_port),
154                               clib_host_to_net_u16 (rmt_port), proto);
155   else
156     s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
157                               clib_host_to_net_u16 (lcl_port),
158                               clib_host_to_net_u16 (rmt_port), proto);
159   if (s)
160     {
161       *result = s;
162       session_pool_remove_peeker (s->thread_index);
163       return 1;
164     }
165   return 0;
166 }
167
168 uword
169 unformat_transport_connection (unformat_input_t * input, va_list * args)
170 {
171   transport_connection_t **result = va_arg (*args, transport_connection_t **);
172   u32 suggested_proto = va_arg (*args, u32);
173   transport_connection_t *tc;
174   u8 proto = ~0;
175   ip46_address_t lcl, rmt;
176   u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
177   u8 is_ip4 = 0;
178
179   if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
180                  &lcl_port, &rmt_port, &is_ip4))
181     return 0;
182
183   proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
184   if (proto == (u8) ~ 0)
185     return 0;
186   if (is_ip4)
187     tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
188                                      clib_host_to_net_u16 (lcl_port),
189                                      clib_host_to_net_u16 (rmt_port), proto);
190   else
191     tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
192                                      clib_host_to_net_u16 (lcl_port),
193                                      clib_host_to_net_u16 (rmt_port), proto);
194
195   if (tc)
196     {
197       *result = tc;
198       return 1;
199     }
200   return 0;
201 }
202
203 static clib_error_t *
204 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
205                          vlib_cli_command_t * cmd)
206 {
207   u8 *str = 0, one_session = 0, do_listeners = 0, sst, do_elog = 0;
208   session_manager_main_t *smm = &session_manager_main;
209   u32 transport_proto = ~0, track_index;
210   stream_session_t *pool, *s;
211   transport_connection_t *tc;
212   app_worker_t *app_wrk;
213   int verbose = 0, i;
214   const u8 *app_name;
215
216   if (!smm->is_enabled)
217     {
218       return clib_error_return (0, "session layer is not enabled");
219     }
220
221   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
222     {
223       if (unformat (input, "verbose %d", &verbose))
224         ;
225       else if (unformat (input, "verbose"))
226         verbose = 1;
227       else if (unformat (input, "listeners %U", unformat_transport_proto,
228                          &transport_proto))
229         do_listeners = 1;
230       else if (unformat (input, "%U", unformat_stream_session, &s))
231         {
232           one_session = 1;
233         }
234       else if (unformat (input, "elog"))
235         do_elog = 1;
236       else
237         return clib_error_return (0, "unknown input `%U'",
238                                   format_unformat_error, input);
239     }
240
241   if (one_session)
242     {
243       str = format (0, "%U", format_stream_session, s, 3);
244       if (do_elog && s->session_state != SESSION_STATE_LISTENING)
245         {
246           elog_main_t *em = &vm->elog_main;
247           f64 dt;
248
249           tc = session_get_transport (s);
250           track_index = transport_elog_track_index (tc);
251           dt = (em->init_time.cpu - vm->clib_time.init_cpu_time)
252             * vm->clib_time.seconds_per_clock;
253           if (track_index != ~0)
254             str = format (str, " session elog:\n%U", format_elog_track, em,
255                           dt, track_index);
256         }
257       vlib_cli_output (vm, "%v", str);
258       return 0;
259     }
260
261   if (do_listeners)
262     {
263       sst = session_type_from_proto_and_ip (transport_proto, 1);
264       vlib_cli_output (vm, "%-40s%-24s", "Listener", "App");
265       /* *INDENT-OFF* */
266       pool_foreach (s, smm->wrk[0].sessions, ({
267         if (s->session_state != SESSION_STATE_LISTENING
268             || s->session_type != sst)
269           continue;
270         app_wrk = app_worker_get (s->app_wrk_index);
271         app_name = application_name_from_index (app_wrk->app_index);
272         vlib_cli_output (vm, "%U%-25v%", format_stream_session, s, 1,
273                          app_name);
274       }));
275       /* *INDENT-ON* */
276       return 0;
277     }
278
279   for (i = 0; i < vec_len (smm->wrk); i++)
280     {
281       u32 once_per_pool;
282       pool = smm->wrk[i].sessions;
283
284       once_per_pool = 1;
285
286       if (pool_elts (pool))
287         {
288
289           vlib_cli_output (vm, "Thread %d: %d active sessions",
290                            i, pool_elts (pool));
291           if (verbose)
292             {
293               if (once_per_pool && verbose == 1)
294                 {
295                   str = format (str, "%-50s%-15s%-10s%-10s",
296                                 "Connection", "State", "Rx-f", "Tx-f");
297                   vlib_cli_output (vm, "%v", str);
298                   vec_reset_length (str);
299                   once_per_pool = 0;
300                 }
301
302               /* *INDENT-OFF* */
303               pool_foreach (s, pool,
304               ({
305                 vec_reset_length (str);
306                 str = format (str, "%U", format_stream_session, s, verbose);
307                 vlib_cli_output (vm, "%v", str);
308               }));
309               /* *INDENT-ON* */
310             }
311         }
312       else
313         vlib_cli_output (vm, "Thread %d: no active sessions", i);
314       vec_reset_length (str);
315     }
316   vec_free (str);
317
318   return 0;
319 }
320
321 /* *INDENT-OFF* */
322 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
323 {
324   .path = "show session",
325   .short_help = "show session [verbose [nnn]]",
326   .function = show_session_command_fn,
327 };
328 /* *INDENT-ON* */
329
330 static int
331 clear_session (stream_session_t * s)
332 {
333   app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
334   application_t *server = application_get (server_wrk->app_index);
335   server->cb_fns.session_disconnect_callback (s);
336   return 0;
337 }
338
339 static clib_error_t *
340 clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
341                           vlib_cli_command_t * cmd)
342 {
343   session_manager_main_t *smm = &session_manager_main;
344   u32 thread_index = 0, clear_all = 0;
345   session_manager_worker_t *wrk;
346   u32 session_index = ~0;
347   stream_session_t *session;
348
349   if (!smm->is_enabled)
350     {
351       return clib_error_return (0, "session layer is not enabled");
352     }
353
354   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
355     {
356       if (unformat (input, "thread %d", &thread_index))
357         ;
358       else if (unformat (input, "session %d", &session_index))
359         ;
360       else if (unformat (input, "all"))
361         clear_all = 1;
362       else
363         return clib_error_return (0, "unknown input `%U'",
364                                   format_unformat_error, input);
365     }
366
367   if (!clear_all && session_index == ~0)
368     return clib_error_return (0, "session <nn> required, but not set.");
369
370   if (session_index != ~0)
371     {
372       session = session_get_if_valid (session_index, thread_index);
373       if (!session)
374         return clib_error_return (0, "no session %d on thread %d",
375                                   session_index, thread_index);
376       clear_session (session);
377     }
378
379   if (clear_all)
380     {
381       /* *INDENT-OFF* */
382       vec_foreach (wrk, smm->wrk)
383         {
384           pool_foreach(session, wrk->sessions, ({
385             clear_session (session);
386           }));
387         };
388       /* *INDENT-ON* */
389     }
390
391   return 0;
392 }
393
394 /* *INDENT-OFF* */
395 VLIB_CLI_COMMAND (clear_session_command, static) =
396 {
397   .path = "clear session",
398   .short_help = "clear session thread <thread> session <index>",
399   .function = clear_session_command_fn,
400 };
401 /* *INDENT-ON* */
402
403 static clib_error_t *
404 show_session_fifo_trace_command_fn (vlib_main_t * vm,
405                                     unformat_input_t * input,
406                                     vlib_cli_command_t * cmd)
407 {
408   stream_session_t *s = 0;
409   u8 is_rx = 0, *str = 0;
410
411   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
412     {
413       if (unformat (input, "%U", unformat_stream_session, &s))
414         ;
415       else if (unformat (input, "rx"))
416         is_rx = 1;
417       else if (unformat (input, "tx"))
418         is_rx = 0;
419       else
420         return clib_error_return (0, "unknown input `%U'",
421                                   format_unformat_error, input);
422     }
423
424   if (!SVM_FIFO_TRACE)
425     {
426       vlib_cli_output (vm, "fifo tracing not enabled");
427       return 0;
428     }
429
430   if (!s)
431     {
432       vlib_cli_output (vm, "could not find session");
433       return 0;
434     }
435
436   str = is_rx ?
437     svm_fifo_dump_trace (str, s->server_rx_fifo) :
438     svm_fifo_dump_trace (str, s->server_tx_fifo);
439
440   vlib_cli_output (vm, "%v", str);
441   return 0;
442 }
443
444 /* *INDENT-OFF* */
445 VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
446 {
447   .path = "show session fifo trace",
448   .short_help = "show session fifo trace <session>",
449   .function = show_session_fifo_trace_command_fn,
450 };
451 /* *INDENT-ON* */
452
453 static clib_error_t *
454 session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
455                                 vlib_cli_command_t * cmd)
456 {
457   stream_session_t *s = 0;
458   u8 is_rx = 0, *str = 0;
459
460   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
461     {
462       if (unformat (input, "%U", unformat_stream_session, &s))
463         ;
464       else if (unformat (input, "rx"))
465         is_rx = 1;
466       else
467         return clib_error_return (0, "unknown input `%U'",
468                                   format_unformat_error, input);
469     }
470
471   if (!SVM_FIFO_TRACE)
472     {
473       vlib_cli_output (vm, "fifo tracing not enabled");
474       return 0;
475     }
476
477   if (!s)
478     {
479       vlib_cli_output (vm, "could not find session");
480       return 0;
481     }
482
483   str = is_rx ?
484     svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
485     svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
486
487   vlib_cli_output (vm, "%v", str);
488   return 0;
489 }
490
491 /* *INDENT-OFF* */
492 VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
493 {
494   .path = "session replay fifo",
495   .short_help = "session replay fifo <session>",
496   .function = session_replay_fifo_command_fn,
497 };
498 /* *INDENT-ON* */
499
500 static clib_error_t *
501 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
502                            vlib_cli_command_t * cmd)
503 {
504   unformat_input_t _line_input, *line_input = &_line_input;
505   u8 is_en = 1;
506   clib_error_t *error;
507
508   if (!unformat_user (input, unformat_line_input, line_input))
509     return clib_error_return (0, "expected enable | disable");
510
511   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
512     {
513       if (unformat (line_input, "enable"))
514         is_en = 1;
515       else if (unformat (line_input, "disable"))
516         is_en = 0;
517       else
518         {
519           error = clib_error_return (0, "unknown input `%U'",
520                                      format_unformat_error, line_input);
521           unformat_free (line_input);
522           return error;
523         }
524     }
525
526   unformat_free (line_input);
527   return vnet_session_enable_disable (vm, is_en);
528 }
529
530 /* *INDENT-OFF* */
531 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
532 {
533   .path = "session",
534   .short_help = "session [enable|disable]",
535   .function = session_enable_disable_fn,
536 };
537 /* *INDENT-ON* */
538
539 /*
540  * fd.io coding-style-patch-verification: ON
541  *
542  * Local Variables:
543  * eval: (c-set-style "gnu")
544  * End:
545  */