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