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