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