tls: add openssl engine
[vpp.git] / src / vnet / session / application.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
16 #include <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/application_namespace.h>
19 #include <vnet/session/session.h>
20
21 /**
22  * Pool from which we allocate all applications
23  */
24 static application_t *app_pool;
25
26 /**
27  * Hash table of apps by api client index
28  */
29 static uword *app_by_api_client_index;
30
31 static u8 *
32 app_get_name_from_reg_index (application_t * app)
33 {
34   u8 *app_name;
35
36   vl_api_registration_t *regp;
37   regp = vl_api_client_index_to_registration (app->api_client_index);
38   if (!regp)
39     app_name = format (0, "builtin-%d%c", app->index, 0);
40   else
41     app_name = format (0, "%s%c", regp->name, 0);
42
43   return app_name;
44 }
45
46 u32
47 application_session_table (application_t * app, u8 fib_proto)
48 {
49   app_namespace_t *app_ns;
50   app_ns = app_namespace_get (app->ns_index);
51   if (!application_has_global_scope (app))
52     return APP_INVALID_INDEX;
53   if (fib_proto == FIB_PROTOCOL_IP4)
54     return session_lookup_get_index_for_fib (fib_proto,
55                                              app_ns->ip4_fib_index);
56   else
57     return session_lookup_get_index_for_fib (fib_proto,
58                                              app_ns->ip6_fib_index);
59 }
60
61 u32
62 application_local_session_table (application_t * app)
63 {
64   app_namespace_t *app_ns;
65   if (!application_has_local_scope (app))
66     return APP_INVALID_INDEX;
67   app_ns = app_namespace_get (app->ns_index);
68   return app_ns->local_table_index;
69 }
70
71 int
72 application_api_queue_is_full (application_t * app)
73 {
74   svm_queue_t *q;
75
76   /* builtin servers are always OK */
77   if (app->api_client_index == ~0)
78     return 0;
79
80   q = vl_api_client_index_to_input_queue (app->api_client_index);
81   if (!q)
82     return 1;
83
84   if (q->cursize == q->maxsize)
85     return 1;
86   return 0;
87 }
88
89 /**
90  * Returns app name
91  *
92  * Since the name is not stored per app, we generate it on the fly. It is
93  * the caller's responsibility to free the vector
94  */
95 u8 *
96 application_name_from_index (u32 app_index)
97 {
98   application_t *app = application_get (app_index);
99   if (!app)
100     return 0;
101   return app_get_name_from_reg_index (app);
102 }
103
104 static void
105 application_table_add (application_t * app)
106 {
107   hash_set (app_by_api_client_index, app->api_client_index, app->index);
108 }
109
110 static void
111 application_table_del (application_t * app)
112 {
113   hash_unset (app_by_api_client_index, app->api_client_index);
114 }
115
116 application_t *
117 application_lookup (u32 api_client_index)
118 {
119   uword *p;
120   p = hash_get (app_by_api_client_index, api_client_index);
121   if (p)
122     return application_get (p[0]);
123
124   return 0;
125 }
126
127 application_t *
128 application_new ()
129 {
130   application_t *app;
131   pool_get (app_pool, app);
132   memset (app, 0, sizeof (*app));
133   app->index = application_get_index (app);
134   app->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
135   app->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
136   app->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
137   if (CLIB_DEBUG > 1)
138     clib_warning ("[%d] New app (%d)", getpid (), app->index);
139   return app;
140 }
141
142 void
143 application_del (application_t * app)
144 {
145   vnet_unbind_args_t _a, *a = &_a;
146   u64 handle, *handles = 0;
147   segment_manager_t *sm;
148   u32 index;
149   int i;
150
151   /*
152    * The app event queue allocated in first segment is cleared with
153    * the segment manager. No need to explicitly free it.
154    */
155   if (CLIB_DEBUG > 1)
156     clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
157
158   if (application_is_proxy (app))
159     application_remove_proxy (app);
160
161   /*
162    *  Listener cleanup
163    */
164
165   /* *INDENT-OFF* */
166   hash_foreach (handle, index, app->listeners_table,
167   ({
168     vec_add1 (handles, handle);
169     sm = segment_manager_get (index);
170     sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
171   }));
172   /* *INDENT-ON* */
173
174   for (i = 0; i < vec_len (handles); i++)
175     {
176       a->app_index = app->index;
177       a->handle = handles[i];
178       /* seg manager is removed when unbind completes */
179       vnet_unbind (a);
180     }
181
182   /*
183    * Connects segment manager cleanup
184    */
185
186   if (app->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
187     {
188       sm = segment_manager_get (app->connects_seg_manager);
189       sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
190       segment_manager_init_del (sm);
191     }
192
193   /* If first segment manager is used by a listener */
194   if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
195       && app->first_segment_manager != app->connects_seg_manager)
196     {
197       sm = segment_manager_get (app->first_segment_manager);
198       /* .. and has no fifos, e.g. it might be used for redirected sessions,
199        * remove it */
200       if (!segment_manager_has_fifos (sm))
201         {
202           sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
203           segment_manager_del (sm);
204         }
205     }
206
207   /*
208    * Local connections cleanup
209    */
210   application_local_sessions_del (app);
211
212   vec_free (app->tls_cert);
213   vec_free (app->tls_key);
214
215   application_table_del (app);
216   pool_put (app_pool, app);
217 }
218
219 static void
220 application_verify_cb_fns (session_cb_vft_t * cb_fns)
221 {
222   if (cb_fns->session_accept_callback == 0)
223     clib_warning ("No accept callback function provided");
224   if (cb_fns->session_connected_callback == 0)
225     clib_warning ("No session connected callback function provided");
226   if (cb_fns->session_disconnect_callback == 0)
227     clib_warning ("No session disconnect callback function provided");
228   if (cb_fns->session_reset_callback == 0)
229     clib_warning ("No session reset callback function provided");
230 }
231
232 /**
233  * Check app config for given segment type
234  *
235  * Returns 1 on success and 0 otherwise
236  */
237 static u8
238 application_verify_cfg (ssvm_segment_type_t st)
239 {
240   u8 is_valid;
241   if (st == SSVM_SEGMENT_MEMFD)
242     {
243       is_valid = (session_manager_get_evt_q_segment () != 0);
244       if (!is_valid)
245         clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
246       return is_valid;
247     }
248   else if (st == SSVM_SEGMENT_SHM)
249     {
250       is_valid = (session_manager_get_evt_q_segment () == 0);
251       if (!is_valid)
252         clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
253       return is_valid;
254     }
255   else
256     return 1;
257 }
258
259 int
260 application_init (application_t * app, u32 api_client_index, u64 * options,
261                   session_cb_vft_t * cb_fns)
262 {
263   ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
264   u32 first_seg_size, prealloc_fifo_pairs;
265   segment_manager_properties_t *props;
266   vl_api_registration_t *reg;
267   segment_manager_t *sm;
268   int rv;
269
270   /*
271    * Make sure we support the requested configuration
272    */
273
274   if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
275     {
276       reg = vl_api_client_index_to_registration (api_client_index);
277       if (!reg)
278         return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
279       if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
280         seg_type = SSVM_SEGMENT_SHM;
281     }
282   else
283     {
284       seg_type = SSVM_SEGMENT_PRIVATE;
285     }
286
287   if (!application_verify_cfg (seg_type))
288     return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
289
290   /*
291    * Setup segment manager
292    */
293   sm = segment_manager_new ();
294   sm->app_index = app->index;
295   props = application_segment_manager_properties (app);
296   segment_manager_properties_init (props);
297   if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
298     {
299       props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
300       props->add_segment = 1;
301     }
302   if (options[APP_OPTIONS_RX_FIFO_SIZE])
303     props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
304   if (options[APP_OPTIONS_TX_FIFO_SIZE])
305     props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
306   if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
307     props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
308   if (options[APP_OPTIONS_TLS_ENGINE])
309     app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
310   props->segment_type = seg_type;
311
312   first_seg_size = options[APP_OPTIONS_SEGMENT_SIZE];
313   prealloc_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
314
315   if ((rv = segment_manager_init (sm, first_seg_size, prealloc_fifo_pairs)))
316     return rv;
317   sm->first_is_protected = 1;
318
319   /*
320    * Setup application
321    */
322   app->first_segment_manager = segment_manager_index (sm);
323   app->api_client_index = api_client_index;
324   app->flags = options[APP_OPTIONS_FLAGS];
325   app->cb_fns = *cb_fns;
326   app->ns_index = options[APP_OPTIONS_NAMESPACE];
327   app->listeners_table = hash_create (0, sizeof (u64));
328   app->local_connects = hash_create (0, sizeof (u64));
329   app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
330   app->event_queue = segment_manager_event_queue (sm);
331
332   /* If no scope enabled, default to global */
333   if (!application_has_global_scope (app)
334       && !application_has_local_scope (app))
335     app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
336
337   /* Check that the obvious things are properly set up */
338   application_verify_cb_fns (cb_fns);
339
340   /* Add app to lookup by api_client_index table */
341   application_table_add (app);
342
343   /*
344    * Segment manager for local sessions
345    */
346   sm = segment_manager_new ();
347   sm->app_index = app->index;
348   app->local_segment_manager = segment_manager_index (sm);
349
350   return 0;
351 }
352
353 application_t *
354 application_get (u32 index)
355 {
356   if (index == APP_INVALID_INDEX)
357     return 0;
358   return pool_elt_at_index (app_pool, index);
359 }
360
361 application_t *
362 application_get_if_valid (u32 index)
363 {
364   if (pool_is_free_index (app_pool, index))
365     return 0;
366
367   return pool_elt_at_index (app_pool, index);
368 }
369
370 u32
371 application_get_index (application_t * app)
372 {
373   return app - app_pool;
374 }
375
376 static segment_manager_t *
377 application_alloc_segment_manager (application_t * app)
378 {
379   segment_manager_t *sm = 0;
380
381   /* If the first segment manager is not in use, don't allocate a new one */
382   if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
383       && app->first_segment_manager_in_use == 0)
384     {
385       sm = segment_manager_get (app->first_segment_manager);
386       app->first_segment_manager_in_use = 1;
387       return sm;
388     }
389
390   sm = segment_manager_new ();
391   sm->app_index = app->index;
392
393   return sm;
394 }
395
396 /**
397  * Start listening local transport endpoint for requested transport.
398  *
399  * Creates a 'dummy' stream session with state LISTENING to be used in session
400  * lookups, prior to establishing connection. Requests transport to build
401  * it's own specific listening connection.
402  */
403 int
404 application_start_listen (application_t * srv, session_endpoint_t * sep,
405                           session_handle_t * res)
406 {
407   segment_manager_t *sm;
408   stream_session_t *s;
409   session_handle_t handle;
410   session_type_t sst;
411
412   sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
413   s = listen_session_new (sst);
414   s->app_index = srv->index;
415
416   if (stream_session_listen (s, sep))
417     goto err;
418
419   /* Allocate segment manager. All sessions derived out of a listen session
420    * have fifos allocated by the same segment manager. */
421   sm = application_alloc_segment_manager (srv);
422   if (sm == 0)
423     goto err;
424
425   /* Add to app's listener table. Useful to find all child listeners
426    * when app goes down, although, just for unbinding this is not needed */
427   handle = listen_session_get_handle (s);
428   hash_set (srv->listeners_table, handle, segment_manager_index (sm));
429
430   *res = handle;
431   return 0;
432
433 err:
434   listen_session_del (s);
435   return -1;
436 }
437
438 /**
439  * Stop listening on session associated to handle
440  */
441 int
442 application_stop_listen (application_t * srv, session_handle_t handle)
443 {
444   stream_session_t *listener;
445   uword *indexp;
446   segment_manager_t *sm;
447
448   if (srv && hash_get (srv->listeners_table, handle) == 0)
449     {
450       clib_warning ("app doesn't own handle %llu!", handle);
451       return -1;
452     }
453
454   listener = listen_session_get_from_handle (handle);
455   stream_session_stop_listen (listener);
456
457   indexp = hash_get (srv->listeners_table, handle);
458   ASSERT (indexp);
459
460   sm = segment_manager_get (*indexp);
461   if (srv->first_segment_manager == *indexp)
462     {
463       /* Delete sessions but don't remove segment manager */
464       srv->first_segment_manager_in_use = 0;
465       segment_manager_del_sessions (sm);
466     }
467   else
468     {
469       segment_manager_init_del (sm);
470     }
471   hash_unset (srv->listeners_table, handle);
472   listen_session_del (listener);
473
474   return 0;
475 }
476
477 int
478 application_open_session (application_t * app, session_endpoint_t * sep,
479                           u32 api_context)
480 {
481   int rv;
482
483   /* Make sure we have a segment manager for connects */
484   application_alloc_connects_segment_manager (app);
485
486   if ((rv = session_open (app->index, sep, api_context)))
487     return rv;
488
489   return 0;
490 }
491
492 int
493 application_alloc_connects_segment_manager (application_t * app)
494 {
495   segment_manager_t *sm;
496
497   if (app->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
498     {
499       sm = application_alloc_segment_manager (app);
500       if (sm == 0)
501         return -1;
502       app->connects_seg_manager = segment_manager_index (sm);
503     }
504   return 0;
505 }
506
507 segment_manager_t *
508 application_get_connect_segment_manager (application_t * app)
509 {
510   ASSERT (app->connects_seg_manager != (u32) ~ 0);
511   return segment_manager_get (app->connects_seg_manager);
512 }
513
514 segment_manager_t *
515 application_get_listen_segment_manager (application_t * app,
516                                         stream_session_t * s)
517 {
518   uword *smp;
519   smp = hash_get (app->listeners_table, listen_session_get_handle (s));
520   ASSERT (smp != 0);
521   return segment_manager_get (*smp);
522 }
523
524 segment_manager_t *
525 application_get_local_segment_manager (application_t * app)
526 {
527   return segment_manager_get (app->local_segment_manager);
528 }
529
530 segment_manager_t *
531 application_get_local_segment_manager_w_session (application_t * app,
532                                                  local_session_t * ls)
533 {
534   stream_session_t *listener;
535   if (application_local_session_listener_has_transport (ls))
536     {
537       listener = listen_session_get (ls->listener_session_type,
538                                      ls->listener_index);
539       return application_get_listen_segment_manager (app, listener);
540     }
541   return segment_manager_get (app->local_segment_manager);
542 }
543
544 int
545 application_is_proxy (application_t * app)
546 {
547   return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
548 }
549
550 int
551 application_is_builtin (application_t * app)
552 {
553   return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
554 }
555
556 int
557 application_is_builtin_proxy (application_t * app)
558 {
559   return (application_is_proxy (app) && application_is_builtin (app));
560 }
561
562 /**
563  * Send an API message to the external app, to map new segment
564  */
565 int
566 application_add_segment_notify (u32 app_index, ssvm_private_t * fs)
567 {
568   application_t *app = application_get (app_index);
569   return app->cb_fns.add_segment_callback (app->api_client_index, fs);
570 }
571
572 u8
573 application_has_local_scope (application_t * app)
574 {
575   return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
576 }
577
578 u8
579 application_has_global_scope (application_t * app)
580 {
581   return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
582 }
583
584 u32
585 application_n_listeners (application_t * app)
586 {
587   return hash_elts (app->listeners_table);
588 }
589
590 stream_session_t *
591 application_first_listener (application_t * app, u8 fib_proto,
592                             u8 transport_proto)
593 {
594   stream_session_t *listener;
595   u64 handle;
596   u32 sm_index;
597   u8 sst;
598
599   sst = session_type_from_proto_and_ip (transport_proto,
600                                         fib_proto == FIB_PROTOCOL_IP4);
601
602   /* *INDENT-OFF* */
603    hash_foreach (handle, sm_index, app->listeners_table, ({
604      listener = listen_session_get_from_handle (handle);
605      if (listener->session_type == sst
606          && listener->listener_index != SESSION_PROXY_LISTENER_INDEX)
607        return listener;
608    }));
609   /* *INDENT-ON* */
610
611   return 0;
612 }
613
614 stream_session_t *
615 application_proxy_listener (application_t * app, u8 fib_proto,
616                             u8 transport_proto)
617 {
618   stream_session_t *listener;
619   u64 handle;
620   u32 sm_index;
621   u8 sst;
622
623   sst = session_type_from_proto_and_ip (transport_proto,
624                                         fib_proto == FIB_PROTOCOL_IP4);
625
626   /* *INDENT-OFF* */
627    hash_foreach (handle, sm_index, app->listeners_table, ({
628      listener = listen_session_get_from_handle (handle);
629      if (listener->session_type == sst
630          && listener->listener_index == SESSION_PROXY_LISTENER_INDEX)
631        return listener;
632    }));
633   /* *INDENT-ON* */
634
635   return 0;
636 }
637
638 static clib_error_t *
639 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
640                                         u8 transport_proto, u8 is_start)
641 {
642   app_namespace_t *app_ns = app_namespace_get (app->ns_index);
643   u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
644   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
645   transport_connection_t *tc;
646   stream_session_t *s;
647   u64 handle;
648
649   if (is_start)
650     {
651       s = application_first_listener (app, fib_proto, transport_proto);
652       if (!s)
653         {
654           sep.is_ip4 = is_ip4;
655           sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
656           sep.sw_if_index = app_ns->sw_if_index;
657           sep.transport_proto = transport_proto;
658           application_start_listen (app, &sep, &handle);
659           s = listen_session_get_from_handle (handle);
660           s->listener_index = SESSION_PROXY_LISTENER_INDEX;
661         }
662     }
663   else
664     {
665       s = application_proxy_listener (app, fib_proto, transport_proto);
666       ASSERT (s);
667     }
668
669   tc = listen_session_get_transport (s);
670
671   if (!ip_is_zero (&tc->lcl_ip, 1))
672     {
673       u32 sti;
674       sep.is_ip4 = is_ip4;
675       sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
676       sep.transport_proto = transport_proto;
677       sep.port = 0;
678       sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
679       if (is_start)
680         session_lookup_add_session_endpoint (sti, &sep, s->session_index);
681       else
682         session_lookup_del_session_endpoint (sti, &sep);
683     }
684
685   return 0;
686 }
687
688 static void
689 application_start_stop_proxy_local_scope (application_t * app,
690                                           u8 transport_proto, u8 is_start)
691 {
692   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
693   app_namespace_t *app_ns;
694   app_ns = app_namespace_get (app->ns_index);
695   sep.is_ip4 = 1;
696   sep.transport_proto = transport_proto;
697   sep.port = 0;
698
699   if (is_start)
700     {
701       session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
702                                            app->index);
703       sep.is_ip4 = 0;
704       session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
705                                            app->index);
706     }
707   else
708     {
709       session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
710       sep.is_ip4 = 0;
711       session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
712     }
713 }
714
715 void
716 application_start_stop_proxy (application_t * app,
717                               transport_proto_t transport_proto, u8 is_start)
718 {
719   if (application_has_local_scope (app))
720     application_start_stop_proxy_local_scope (app, transport_proto, is_start);
721
722   if (application_has_global_scope (app))
723     {
724       application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
725                                               transport_proto, is_start);
726       application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
727                                               transport_proto, is_start);
728     }
729 }
730
731 void
732 application_setup_proxy (application_t * app)
733 {
734   u16 transports = app->proxied_transports;
735   transport_proto_t tp;
736
737   ASSERT (application_is_proxy (app));
738
739   /* *INDENT-OFF* */
740   transport_proto_foreach (tp, ({
741     if (transports & (1 << tp))
742       application_start_stop_proxy (app, tp, 1);
743   }));
744   /* *INDENT-ON* */
745 }
746
747 void
748 application_remove_proxy (application_t * app)
749 {
750   u16 transports = app->proxied_transports;
751   transport_proto_t tp;
752
753   ASSERT (application_is_proxy (app));
754
755   /* *INDENT-OFF* */
756   transport_proto_foreach (tp, ({
757     if (transports & (1 << tp))
758       application_start_stop_proxy (app, tp, 0);
759   }));
760   /* *INDENT-ON* */
761 }
762
763 segment_manager_properties_t *
764 application_segment_manager_properties (application_t * app)
765 {
766   return &app->sm_properties;
767 }
768
769 segment_manager_properties_t *
770 application_get_segment_manager_properties (u32 app_index)
771 {
772   application_t *app = application_get (app_index);
773   return &app->sm_properties;
774 }
775
776 local_session_t *
777 application_alloc_local_session (application_t * app)
778 {
779   local_session_t *s;
780   pool_get (app->local_sessions, s);
781   memset (s, 0, sizeof (*s));
782   s->app_index = app->index;
783   s->session_index = s - app->local_sessions;
784   s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
785   return s;
786 }
787
788 void
789 application_free_local_session (application_t * app, local_session_t * s)
790 {
791   pool_put (app->local_sessions, s);
792   if (CLIB_DEBUG)
793     memset (s, 0xfc, sizeof (*s));
794 }
795
796 local_session_t *
797 application_get_local_session (application_t * app, u32 session_index)
798 {
799   return pool_elt_at_index (app->local_sessions, session_index);
800 }
801
802 local_session_t *
803 application_get_local_session_from_handle (session_handle_t handle)
804 {
805   application_t *server;
806   u32 session_index, server_index;
807   local_session_parse_handle (handle, &server_index, &session_index);
808   server = application_get (server_index);
809   return application_get_local_session (server, session_index);
810 }
811
812 always_inline void
813 application_local_listener_session_endpoint (local_session_t * ll,
814                                              session_endpoint_t * sep)
815 {
816   sep->transport_proto =
817     session_type_transport_proto (ll->listener_session_type);
818   sep->port = ll->port;
819   sep->is_ip4 = ll->listener_session_type & 1;
820 }
821
822 int
823 application_start_local_listen (application_t * server,
824                                 session_endpoint_t * sep,
825                                 session_handle_t * handle)
826 {
827   session_handle_t lh;
828   local_session_t *ll;
829   u32 table_index;
830
831   table_index = application_local_session_table (server);
832
833   /* An exact sep match, as opposed to session_lookup_local_listener */
834   lh = session_lookup_endpoint_listener (table_index, sep, 1);
835   if (lh != SESSION_INVALID_HANDLE)
836     return VNET_API_ERROR_ADDRESS_IN_USE;
837
838   pool_get (server->local_listen_sessions, ll);
839   memset (ll, 0, sizeof (*ll));
840   ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
841   ll->app_index = server->index;
842   ll->session_index = ll - server->local_listen_sessions;
843   ll->port = sep->port;
844   /* Store the original session type for the unbind */
845   ll->listener_session_type =
846     session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
847   ll->transport_listener_index = ~0;
848
849   *handle = application_local_session_handle (ll);
850   session_lookup_add_session_endpoint (table_index, sep, *handle);
851
852   return 0;
853 }
854
855 /**
856  * Clean up local session table. If we have a listener session use it to
857  * find the port and proto. If not, the handle must be a local table handle
858  * so parse it.
859  */
860 int
861 application_stop_local_listen (application_t * server, session_handle_t lh)
862 {
863   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
864   u32 table_index, ll_index, server_index;
865   stream_session_t *sl = 0;
866   local_session_t *ll, *ls;
867
868   table_index = application_local_session_table (server);
869
870   /* We have both local and global table binds. Figure from global what
871    * the sep we should be cleaning up is.
872    */
873   if (!session_handle_is_local (lh))
874     {
875       sl = listen_session_get_from_handle (lh);
876       if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
877         {
878           clib_warning ("broken listener");
879           return -1;
880         }
881       lh = session_lookup_endpoint_listener (table_index, &sep, 0);
882       if (lh == SESSION_INVALID_HANDLE)
883         return -1;
884     }
885
886   local_session_parse_handle (lh, &server_index, &ll_index);
887   ASSERT (server->index == server_index);
888   if (!(ll = application_get_local_listen_session (server, ll_index)))
889     {
890       clib_warning ("no local listener");
891       return -1;
892     }
893   application_local_listener_session_endpoint (ll, &sep);
894   session_lookup_del_session_endpoint (table_index, &sep);
895
896   /* *INDENT-OFF* */
897   pool_foreach (ls, server->local_sessions, ({
898     if (ls->listener_index == ll->session_index)
899       application_local_session_disconnect (server->index, ls);
900   }));
901   /* *INDENT-ON* */
902   pool_put_index (server->local_listen_sessions, ll->session_index);
903
904   return 0;
905 }
906
907 int
908 application_local_session_connect (u32 table_index, application_t * client,
909                                    application_t * server,
910                                    local_session_t * ll, u32 opaque)
911 {
912   u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
913   segment_manager_properties_t *props, *cprops;
914   int rv, has_transport, seg_index;
915   svm_fifo_segment_private_t *seg;
916   segment_manager_t *sm;
917   local_session_t *ls;
918   svm_queue_t *sq, *cq;
919
920   ls = application_alloc_local_session (server);
921
922   props = application_segment_manager_properties (server);
923   cprops = application_segment_manager_properties (client);
924   evt_q_elts = props->evt_q_size + cprops->evt_q_size;
925   evt_q_sz = evt_q_elts * sizeof (session_fifo_event_t);
926   seg_size = props->rx_fifo_size + props->tx_fifo_size + evt_q_sz + margin;
927
928   has_transport = session_has_transport ((stream_session_t *) ll);
929   if (!has_transport)
930     {
931       /* Local sessions don't have backing transport */
932       ls->port = ll->port;
933       sm = application_get_local_segment_manager (server);
934     }
935   else
936     {
937       stream_session_t *sl = (stream_session_t *) ll;
938       transport_connection_t *tc;
939       tc = listen_session_get_transport (sl);
940       ls->port = tc->lcl_port;
941       sm = application_get_listen_segment_manager (server, sl);
942     }
943
944   seg_index = segment_manager_add_segment (sm, seg_size);
945   if (seg_index < 0)
946     {
947       clib_warning ("failed to add new cut-through segment");
948       return seg_index;
949     }
950   seg = segment_manager_get_segment_w_lock (sm, seg_index);
951   sq = segment_manager_alloc_queue (seg, props->evt_q_size);
952   cq = segment_manager_alloc_queue (seg, cprops->evt_q_size);
953   ls->server_evt_q = pointer_to_uword (sq);
954   ls->client_evt_q = pointer_to_uword (cq);
955   rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
956                                         props->tx_fifo_size,
957                                         &ls->server_rx_fifo,
958                                         &ls->server_tx_fifo);
959   if (rv)
960     {
961       clib_warning ("failed to add fifos in cut-through segment");
962       segment_manager_segment_reader_unlock (sm);
963       goto failed;
964     }
965   ls->server_rx_fifo->master_session_index = ls->session_index;
966   ls->server_tx_fifo->master_session_index = ls->session_index;
967   ls->server_rx_fifo->master_thread_index = ~0;
968   ls->server_tx_fifo->master_thread_index = ~0;
969   ls->svm_segment_index = seg_index;
970   ls->listener_index = ll->session_index;
971   ls->client_index = client->index;
972   ls->client_opaque = opaque;
973   ls->listener_session_type = ll->session_type;
974
975   if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
976                                                  &seg->ssvm)))
977     {
978       clib_warning ("failed to notify server of new segment");
979       segment_manager_segment_reader_unlock (sm);
980       goto failed;
981     }
982   segment_manager_segment_reader_unlock (sm);
983   if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
984     {
985       clib_warning ("failed to send accept cut-through notify to server");
986       goto failed;
987     }
988   if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
989     application_local_session_connect_notify (ls);
990
991   return 0;
992
993 failed:
994   if (!has_transport)
995     segment_manager_del_segment (sm, seg);
996   return rv;
997 }
998
999 static uword
1000 application_client_local_connect_key (local_session_t * ls)
1001 {
1002   return ((uword) ls->app_index << 32 | (uword) ls->session_index);
1003 }
1004
1005 static void
1006 application_client_local_connect_key_parse (uword key, u32 * app_index,
1007                                             u32 * session_index)
1008 {
1009   *app_index = key >> 32;
1010   *session_index = key & 0xFFFFFFFF;
1011 }
1012
1013 int
1014 application_local_session_connect_notify (local_session_t * ls)
1015 {
1016   svm_fifo_segment_private_t *seg;
1017   application_t *client, *server;
1018   segment_manager_t *sm;
1019   int rv, is_fail = 0;
1020   uword client_key;
1021
1022   client = application_get (ls->client_index);
1023   server = application_get (ls->app_index);
1024   sm = application_get_local_segment_manager_w_session (server, ls);
1025   seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1026   if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1027                                                  &seg->ssvm)))
1028     {
1029       clib_warning ("failed to notify client %u of new segment",
1030                     ls->client_index);
1031       segment_manager_segment_reader_unlock (sm);
1032       application_local_session_disconnect (ls->client_index, ls);
1033       is_fail = 1;
1034     }
1035   else
1036     {
1037       segment_manager_segment_reader_unlock (sm);
1038     }
1039
1040   client->cb_fns.session_connected_callback (client->index, ls->client_opaque,
1041                                              (stream_session_t *) ls,
1042                                              is_fail);
1043
1044   client_key = application_client_local_connect_key (ls);
1045   hash_set (client->local_connects, client_key, client_key);
1046   return 0;
1047 }
1048
1049 int
1050 application_local_session_disconnect (u32 app_index, local_session_t * ls)
1051 {
1052   svm_fifo_segment_private_t *seg;
1053   application_t *client, *server;
1054   segment_manager_t *sm;
1055   uword client_key;
1056
1057   client = application_get_if_valid (ls->client_index);
1058   server = application_get (ls->app_index);
1059
1060   if (ls->session_state == SESSION_STATE_CLOSED)
1061     {
1062     cleanup:
1063       client_key = application_client_local_connect_key (ls);
1064       sm = application_get_local_segment_manager_w_session (server, ls);
1065       seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1066
1067       if (client)
1068         {
1069           hash_unset (client->local_connects, client_key);
1070           client->cb_fns.del_segment_callback (client->api_client_index,
1071                                                &seg->ssvm);
1072         }
1073
1074       server->cb_fns.del_segment_callback (server->api_client_index,
1075                                            &seg->ssvm);
1076       segment_manager_del_segment (sm, seg);
1077       application_free_local_session (server, ls);
1078       return 0;
1079     }
1080
1081   if (app_index == ls->client_index)
1082     {
1083       send_local_session_disconnect_callback (ls->app_index, ls);
1084     }
1085   else
1086     {
1087       if (!client)
1088         {
1089           goto cleanup;
1090         }
1091       else if (ls->session_state < SESSION_STATE_READY)
1092         {
1093           client->cb_fns.session_connected_callback (client->index,
1094                                                      ls->client_opaque,
1095                                                      (stream_session_t *) ls,
1096                                                      1 /* is_fail */ );
1097           ls->session_state = SESSION_STATE_CLOSED;
1098           goto cleanup;
1099         }
1100       else
1101         {
1102           send_local_session_disconnect_callback (ls->client_index, ls);
1103         }
1104     }
1105
1106   ls->session_state = SESSION_STATE_CLOSED;
1107
1108   return 0;
1109 }
1110
1111 void
1112 application_local_sessions_del (application_t * app)
1113 {
1114   u32 index, server_index, session_index, table_index;
1115   segment_manager_t *sm;
1116   u64 handle, *handles = 0;
1117   local_session_t *ls, *ll;
1118   application_t *server;
1119   session_endpoint_t sep;
1120   int i;
1121
1122   /*
1123    * Local listens. Don't bother with local sessions, we clean them lower
1124    */
1125   table_index = application_local_session_table (app);
1126   /* *INDENT-OFF* */
1127   pool_foreach (ll, app->local_listen_sessions, ({
1128     application_local_listener_session_endpoint (ll, &sep);
1129     session_lookup_del_session_endpoint (table_index, &sep);
1130   }));
1131   /* *INDENT-ON* */
1132
1133   /*
1134    * Local sessions
1135    */
1136   if (app->local_sessions)
1137     {
1138       /* *INDENT-OFF* */
1139       pool_foreach (ls, app->local_sessions, ({
1140         application_local_session_disconnect (app->index, ls);
1141       }));
1142       /* *INDENT-ON* */
1143     }
1144
1145   /*
1146    * Local connects
1147    */
1148   vec_reset_length (handles);
1149   /* *INDENT-OFF* */
1150   hash_foreach (handle, index, app->local_connects, ({
1151     vec_add1 (handles, handle);
1152   }));
1153   /* *INDENT-ON* */
1154
1155   for (i = 0; i < vec_len (handles); i++)
1156     {
1157       application_client_local_connect_key_parse (handles[i], &server_index,
1158                                                   &session_index);
1159       server = application_get_if_valid (server_index);
1160       if (server)
1161         {
1162           ls = application_get_local_session (server, session_index);
1163           application_local_session_disconnect (app->index, ls);
1164         }
1165     }
1166
1167   sm = segment_manager_get (app->local_segment_manager);
1168   sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1169   segment_manager_del (sm);
1170 }
1171
1172 clib_error_t *
1173 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1174 {
1175   application_t *app;
1176   app = application_get (a->app_index);
1177   if (!app)
1178     return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1179                                    0, "app %u doesn't exist", a->app_index);
1180   app->tls_cert = vec_dup (a->cert);
1181   return 0;
1182 }
1183
1184 clib_error_t *
1185 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1186 {
1187   application_t *app;
1188   app = application_get (a->app_index);
1189   if (!app)
1190     return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1191                                    0, "app %u doesn't exist", a->app_index);
1192   app->tls_key = vec_dup (a->key);
1193   return 0;
1194 }
1195
1196 u8 *
1197 format_application_listener (u8 * s, va_list * args)
1198 {
1199   application_t *app = va_arg (*args, application_t *);
1200   u64 handle = va_arg (*args, u64);
1201   u32 sm_index = va_arg (*args, u32);
1202   int verbose = va_arg (*args, int);
1203   stream_session_t *listener;
1204   u8 *app_name, *str;
1205
1206   if (app == 0)
1207     {
1208       if (verbose)
1209         s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
1210                     "API Client", "ListenerID", "SegManager");
1211       else
1212         s = format (s, "%-40s%-20s", "Connection", "App");
1213
1214       return s;
1215     }
1216
1217   app_name = app_get_name_from_reg_index (app);
1218   listener = listen_session_get_from_handle (handle);
1219   str = format (0, "%U", format_stream_session, listener, verbose);
1220
1221   if (verbose)
1222     {
1223       s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
1224                   app->api_client_index, handle, sm_index);
1225     }
1226   else
1227     s = format (s, "%-40s%-20s", str, app_name);
1228
1229   vec_free (app_name);
1230   return s;
1231 }
1232
1233 void
1234 application_format_connects (application_t * app, int verbose)
1235 {
1236   svm_fifo_segment_private_t *fifo_segment;
1237   vlib_main_t *vm = vlib_get_main ();
1238   segment_manager_t *sm;
1239   u8 *app_name, *s = 0;
1240
1241   /* Header */
1242   if (app == 0)
1243     {
1244       if (verbose)
1245         vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1246                          "API Client", "SegManager");
1247       else
1248         vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1249       return;
1250     }
1251
1252   /* make sure */
1253   if (app->connects_seg_manager == (u32) ~ 0)
1254     return;
1255
1256   app_name = app_get_name_from_reg_index (app);
1257
1258   /* Across all fifo segments */
1259   sm = segment_manager_get (app->connects_seg_manager);
1260
1261   /* *INDENT-OFF* */
1262   segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
1263     svm_fifo_t *fifo;
1264     u8 *str;
1265
1266     fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
1267     while (fifo)
1268         {
1269           u32 session_index, thread_index;
1270           stream_session_t *session;
1271
1272           session_index = fifo->master_session_index;
1273           thread_index = fifo->master_thread_index;
1274
1275           session = session_get (session_index, thread_index);
1276           str = format (0, "%U", format_stream_session, session, verbose);
1277
1278           if (verbose)
1279             s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
1280                         app->api_client_index, app->connects_seg_manager);
1281           else
1282             s = format (s, "%-40s%-20s", str, app_name);
1283
1284           vlib_cli_output (vm, "%v", s);
1285           vec_reset_length (s);
1286           vec_free (str);
1287
1288           fifo = fifo->next;
1289         }
1290     vec_free (s);
1291   }));
1292   /* *INDENT-ON* */
1293
1294   vec_free (app_name);
1295 }
1296
1297 void
1298 application_format_local_sessions (application_t * app, int verbose)
1299 {
1300   vlib_main_t *vm = vlib_get_main ();
1301   local_session_t *ls;
1302   transport_proto_t tp;
1303   u8 *conn = 0;
1304
1305   /* Header */
1306   if (app == 0)
1307     {
1308       vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
1309                        "ClientApp");
1310       return;
1311     }
1312
1313   /* *INDENT-OFF* */
1314   pool_foreach (ls, app->local_listen_sessions, ({
1315     tp = session_type_transport_proto(ls->listener_session_type);
1316     conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1317                    ls->port);
1318     vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_index, "*");
1319     vec_reset_length (conn);
1320   }));
1321   pool_foreach (ls, app->local_sessions, ({
1322     tp = session_type_transport_proto(ls->listener_session_type);
1323     conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1324                    ls->port);
1325     vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_index,
1326                      ls->client_index);
1327     vec_reset_length (conn);
1328   }));
1329   /* *INDENT-ON* */
1330
1331   vec_free (conn);
1332 }
1333
1334 void
1335 application_format_local_connects (application_t * app, int verbose)
1336 {
1337   vlib_main_t *vm = vlib_get_main ();
1338   u32 app_index, session_index;
1339   application_t *server;
1340   local_session_t *ls;
1341   uword client_key;
1342   u64 value;
1343
1344   /* Header */
1345   if (app == 0)
1346     {
1347       if (verbose)
1348         vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
1349                          "Peer App", "SegManager");
1350       else
1351         vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
1352                          "Peer App");
1353       return;
1354     }
1355
1356   /* *INDENT-OFF* */
1357   hash_foreach (client_key, value, app->local_connects, ({
1358     application_client_local_connect_key_parse (client_key, &app_index,
1359                                                 &session_index);
1360     server = application_get (app_index);
1361     ls = application_get_local_session (server, session_index);
1362     vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_index, ls->client_index);
1363   }));
1364   /* *INDENT-ON* */
1365 }
1366
1367 u8 *
1368 format_application (u8 * s, va_list * args)
1369 {
1370   application_t *app = va_arg (*args, application_t *);
1371   CLIB_UNUSED (int verbose) = va_arg (*args, int);
1372   segment_manager_properties_t *props;
1373   const u8 *app_ns_name;
1374   u8 *app_name;
1375
1376   if (app == 0)
1377     {
1378       if (verbose)
1379         s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
1380                     "API Client", "Namespace", "Add seg size", "Rx fifo size",
1381                     "Tx fifo size");
1382       else
1383         s =
1384           format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
1385                   "Namespace");
1386       return s;
1387     }
1388
1389   app_name = app_get_name_from_reg_index (app);
1390   app_ns_name = app_namespace_id_from_index (app->ns_index);
1391   props = application_segment_manager_properties (app);
1392   if (verbose)
1393     s =
1394       format (s, "%-10d%-20s%-15d%-15d%-15d%-15d%-15d", app->index, app_name,
1395               app->api_client_index, app->ns_index,
1396               props->add_segment_size,
1397               props->rx_fifo_size, props->tx_fifo_size);
1398   else
1399     s = format (s, "%-10d%-20s%-15d%-40s", app->index, app_name,
1400                 app->api_client_index, app_ns_name);
1401   return s;
1402 }
1403
1404
1405 void
1406 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1407 {
1408   application_t *app;
1409   u32 sm_index;
1410   u64 handle;
1411
1412   if (!pool_elts (app_pool))
1413     {
1414       vlib_cli_output (vm, "No active server bindings");
1415       return;
1416     }
1417
1418   if (do_local)
1419     {
1420       application_format_local_sessions (0, verbose);
1421       /* *INDENT-OFF* */
1422       pool_foreach (app, app_pool, ({
1423         if (!pool_elts (app->local_sessions)
1424             && !pool_elts(app->local_connects))
1425           continue;
1426         application_format_local_sessions (app, verbose);
1427       }));
1428       /* *INDENT-ON* */
1429     }
1430   else
1431     {
1432       vlib_cli_output (vm, "%U", format_application_listener, 0 /* header */ ,
1433                        0, 0, verbose);
1434
1435       /* *INDENT-OFF* */
1436       pool_foreach (app, app_pool, ({
1437         if (hash_elts (app->listeners_table) == 0)
1438           continue;
1439         hash_foreach (handle, sm_index, app->listeners_table, ({
1440           vlib_cli_output (vm, "%U", format_application_listener, app,
1441                            handle, sm_index, verbose);
1442         }));
1443       }));
1444       /* *INDENT-ON* */
1445     }
1446 }
1447
1448 void
1449 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1450 {
1451   application_t *app;
1452
1453   if (!pool_elts (app_pool))
1454     {
1455       vlib_cli_output (vm, "No active apps");
1456       return;
1457     }
1458
1459   if (do_local)
1460     {
1461       application_format_local_connects (0, verbose);
1462
1463       /* *INDENT-OFF* */
1464       pool_foreach (app, app_pool, ({
1465         if (app->local_connects)
1466           application_format_local_connects (app, verbose);
1467       }));
1468       /* *INDENT-ON* */
1469     }
1470   else
1471     {
1472       application_format_connects (0, verbose);
1473
1474       /* *INDENT-OFF* */
1475       pool_foreach (app, app_pool, ({
1476         if (app->connects_seg_manager == (u32)~0)
1477           continue;
1478         application_format_connects (app, verbose);
1479       }));
1480       /* *INDENT-ON* */
1481     }
1482 }
1483
1484 static clib_error_t *
1485 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1486                      vlib_cli_command_t * cmd)
1487 {
1488   int do_server = 0, do_client = 0, do_local = 0;
1489   application_t *app;
1490   int verbose = 0;
1491
1492   session_cli_return_if_not_enabled ();
1493
1494   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1495     {
1496       if (unformat (input, "server"))
1497         do_server = 1;
1498       else if (unformat (input, "client"))
1499         do_client = 1;
1500       else if (unformat (input, "local"))
1501         do_local = 1;
1502       else if (unformat (input, "verbose"))
1503         verbose = 1;
1504       else
1505         break;
1506     }
1507
1508   if (do_server)
1509     application_format_all_listeners (vm, do_local, verbose);
1510
1511   if (do_client)
1512     application_format_all_clients (vm, do_local, verbose);
1513
1514   /* Print app related info */
1515   if (!do_server && !do_client)
1516     {
1517       vlib_cli_output (vm, "%U", format_application, 0, verbose);
1518       /* *INDENT-OFF* */
1519       pool_foreach (app, app_pool, ({
1520         vlib_cli_output (vm, "%U", format_application, app, verbose);
1521       }));
1522       /* *INDENT-ON* */
1523     }
1524
1525   return 0;
1526 }
1527
1528 /* *INDENT-OFF* */
1529 VLIB_CLI_COMMAND (show_app_command, static) =
1530 {
1531   .path = "show app",
1532   .short_help = "show app [server|client] [verbose]",
1533   .function = show_app_command_fn,
1534 };
1535 /* *INDENT-ON* */
1536
1537 /*
1538  * fd.io coding-style-patch-verification: ON
1539  *
1540  * Local Variables:
1541  * eval: (c-set-style "gnu")
1542  * End:
1543  */