session/dlmalloc: coverity fixes
[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   /* Allocate segment manager. All sessions derived out of a listen session
449    * have fifos allocated by the same segment manager. */
450   if (!(sm = application_alloc_segment_manager (srv)))
451     goto err;
452
453   /* Add to app's listener table. Useful to find all child listeners
454    * when app goes down, although, just for unbinding this is not needed */
455   handle = listen_session_get_handle (s);
456   hash_set (srv->listeners_table, handle, segment_manager_index (sm));
457
458   if (stream_session_listen (s, sep))
459     {
460       segment_manager_del (sm);
461       hash_unset (srv->listeners_table, handle);
462       goto err;
463     }
464
465   *res = handle;
466   return 0;
467
468 err:
469   listen_session_del (s);
470   return -1;
471 }
472
473 /**
474  * Stop listening on session associated to handle
475  */
476 int
477 application_stop_listen (application_t * srv, session_handle_t handle)
478 {
479   stream_session_t *listener;
480   uword *indexp;
481   segment_manager_t *sm;
482
483   if (srv && hash_get (srv->listeners_table, handle) == 0)
484     {
485       clib_warning ("app doesn't own handle %llu!", handle);
486       return -1;
487     }
488
489   listener = listen_session_get_from_handle (handle);
490   stream_session_stop_listen (listener);
491
492   indexp = hash_get (srv->listeners_table, handle);
493   ASSERT (indexp);
494
495   sm = segment_manager_get (*indexp);
496   if (srv->first_segment_manager == *indexp)
497     {
498       /* Delete sessions but don't remove segment manager */
499       srv->first_segment_manager_in_use = 0;
500       segment_manager_del_sessions (sm);
501     }
502   else
503     {
504       segment_manager_init_del (sm);
505     }
506   hash_unset (srv->listeners_table, handle);
507   listen_session_del (listener);
508
509   return 0;
510 }
511
512 int
513 application_open_session (application_t * app, session_endpoint_t * sep,
514                           u32 api_context)
515 {
516   int rv;
517
518   /* Make sure we have a segment manager for connects */
519   application_alloc_connects_segment_manager (app);
520
521   if ((rv = session_open (app->index, sep, api_context)))
522     return rv;
523
524   return 0;
525 }
526
527 int
528 application_alloc_connects_segment_manager (application_t * app)
529 {
530   segment_manager_t *sm;
531
532   if (app->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
533     {
534       sm = application_alloc_segment_manager (app);
535       if (sm == 0)
536         return -1;
537       app->connects_seg_manager = segment_manager_index (sm);
538     }
539   return 0;
540 }
541
542 segment_manager_t *
543 application_get_connect_segment_manager (application_t * app)
544 {
545   ASSERT (app->connects_seg_manager != (u32) ~ 0);
546   return segment_manager_get (app->connects_seg_manager);
547 }
548
549 segment_manager_t *
550 application_get_listen_segment_manager (application_t * app,
551                                         stream_session_t * s)
552 {
553   uword *smp;
554   smp = hash_get (app->listeners_table, listen_session_get_handle (s));
555   ASSERT (smp != 0);
556   return segment_manager_get (*smp);
557 }
558
559 segment_manager_t *
560 application_get_local_segment_manager (application_t * app)
561 {
562   return segment_manager_get (app->local_segment_manager);
563 }
564
565 segment_manager_t *
566 application_get_local_segment_manager_w_session (application_t * app,
567                                                  local_session_t * ls)
568 {
569   stream_session_t *listener;
570   if (application_local_session_listener_has_transport (ls))
571     {
572       listener = listen_session_get (ls->listener_index);
573       return application_get_listen_segment_manager (app, listener);
574     }
575   return segment_manager_get (app->local_segment_manager);
576 }
577
578 int
579 application_is_proxy (application_t * app)
580 {
581   return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
582 }
583
584 int
585 application_is_builtin (application_t * app)
586 {
587   return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
588 }
589
590 int
591 application_is_builtin_proxy (application_t * app)
592 {
593   return (application_is_proxy (app) && application_is_builtin (app));
594 }
595
596 /**
597  * Send an API message to the external app, to map new segment
598  */
599 int
600 application_add_segment_notify (u32 app_index, ssvm_private_t * fs)
601 {
602   application_t *app = application_get (app_index);
603   return app->cb_fns.add_segment_callback (app->api_client_index, fs);
604 }
605
606 u8
607 application_has_local_scope (application_t * app)
608 {
609   return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
610 }
611
612 u8
613 application_has_global_scope (application_t * app)
614 {
615   return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
616 }
617
618 u32
619 application_n_listeners (application_t * app)
620 {
621   return hash_elts (app->listeners_table);
622 }
623
624 stream_session_t *
625 application_first_listener (application_t * app, u8 fib_proto,
626                             u8 transport_proto)
627 {
628   stream_session_t *listener;
629   u64 handle;
630   u32 sm_index;
631   u8 sst;
632
633   sst = session_type_from_proto_and_ip (transport_proto,
634                                         fib_proto == FIB_PROTOCOL_IP4);
635
636   /* *INDENT-OFF* */
637    hash_foreach (handle, sm_index, app->listeners_table, ({
638      listener = listen_session_get_from_handle (handle);
639      if (listener->session_type == sst
640          && listener->listener_index != SESSION_PROXY_LISTENER_INDEX)
641        return listener;
642    }));
643   /* *INDENT-ON* */
644
645   return 0;
646 }
647
648 stream_session_t *
649 application_proxy_listener (application_t * app, u8 fib_proto,
650                             u8 transport_proto)
651 {
652   stream_session_t *listener;
653   u64 handle;
654   u32 sm_index;
655   u8 sst;
656
657   sst = session_type_from_proto_and_ip (transport_proto,
658                                         fib_proto == FIB_PROTOCOL_IP4);
659
660   /* *INDENT-OFF* */
661    hash_foreach (handle, sm_index, app->listeners_table, ({
662      listener = listen_session_get_from_handle (handle);
663      if (listener->session_type == sst
664          && listener->listener_index == SESSION_PROXY_LISTENER_INDEX)
665        return listener;
666    }));
667   /* *INDENT-ON* */
668
669   return 0;
670 }
671
672 static clib_error_t *
673 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
674                                         u8 transport_proto, u8 is_start)
675 {
676   app_namespace_t *app_ns = app_namespace_get (app->ns_index);
677   u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
678   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
679   transport_connection_t *tc;
680   stream_session_t *s;
681   u64 handle;
682
683   if (is_start)
684     {
685       s = application_first_listener (app, fib_proto, transport_proto);
686       if (!s)
687         {
688           sep.is_ip4 = is_ip4;
689           sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
690           sep.sw_if_index = app_ns->sw_if_index;
691           sep.transport_proto = transport_proto;
692           application_start_listen (app, &sep, &handle);
693           s = listen_session_get_from_handle (handle);
694           s->listener_index = SESSION_PROXY_LISTENER_INDEX;
695         }
696     }
697   else
698     {
699       s = application_proxy_listener (app, fib_proto, transport_proto);
700       ASSERT (s);
701     }
702
703   tc = listen_session_get_transport (s);
704
705   if (!ip_is_zero (&tc->lcl_ip, 1))
706     {
707       u32 sti;
708       sep.is_ip4 = is_ip4;
709       sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
710       sep.transport_proto = transport_proto;
711       sep.port = 0;
712       sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
713       if (is_start)
714         session_lookup_add_session_endpoint (sti, &sep, s->session_index);
715       else
716         session_lookup_del_session_endpoint (sti, &sep);
717     }
718
719   return 0;
720 }
721
722 static void
723 application_start_stop_proxy_local_scope (application_t * app,
724                                           u8 transport_proto, u8 is_start)
725 {
726   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
727   app_namespace_t *app_ns;
728   app_ns = app_namespace_get (app->ns_index);
729   sep.is_ip4 = 1;
730   sep.transport_proto = transport_proto;
731   sep.port = 0;
732
733   if (is_start)
734     {
735       session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
736                                            app->index);
737       sep.is_ip4 = 0;
738       session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
739                                            app->index);
740     }
741   else
742     {
743       session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
744       sep.is_ip4 = 0;
745       session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
746     }
747 }
748
749 void
750 application_start_stop_proxy (application_t * app,
751                               transport_proto_t transport_proto, u8 is_start)
752 {
753   if (application_has_local_scope (app))
754     application_start_stop_proxy_local_scope (app, transport_proto, is_start);
755
756   if (application_has_global_scope (app))
757     {
758       application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
759                                               transport_proto, is_start);
760       application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
761                                               transport_proto, is_start);
762     }
763 }
764
765 void
766 application_setup_proxy (application_t * app)
767 {
768   u16 transports = app->proxied_transports;
769   transport_proto_t tp;
770
771   ASSERT (application_is_proxy (app));
772
773   /* *INDENT-OFF* */
774   transport_proto_foreach (tp, ({
775     if (transports & (1 << tp))
776       application_start_stop_proxy (app, tp, 1);
777   }));
778   /* *INDENT-ON* */
779 }
780
781 void
782 application_remove_proxy (application_t * app)
783 {
784   u16 transports = app->proxied_transports;
785   transport_proto_t tp;
786
787   ASSERT (application_is_proxy (app));
788
789   /* *INDENT-OFF* */
790   transport_proto_foreach (tp, ({
791     if (transports & (1 << tp))
792       application_start_stop_proxy (app, tp, 0);
793   }));
794   /* *INDENT-ON* */
795 }
796
797 segment_manager_properties_t *
798 application_segment_manager_properties (application_t * app)
799 {
800   return &app->sm_properties;
801 }
802
803 segment_manager_properties_t *
804 application_get_segment_manager_properties (u32 app_index)
805 {
806   application_t *app = application_get (app_index);
807   return &app->sm_properties;
808 }
809
810 static inline int
811 app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
812 {
813   if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
814     {
815       clib_warning ("evt q full");
816       svm_msg_q_free_msg (mq, msg);
817       if (lock)
818         svm_msg_q_unlock (mq);
819       return -1;
820     }
821
822   if (lock)
823     {
824       svm_msg_q_add_and_unlock (mq, msg);
825       return 0;
826     }
827
828   /* Even when not locking the ring, we must wait for queue mutex */
829   if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
830     {
831       clib_warning ("msg q add returned");
832       return -1;
833     }
834   return 0;
835 }
836
837 static inline int
838 app_send_io_evt_rx (application_t * app, stream_session_t * s, u8 lock)
839 {
840   session_event_t *evt;
841   svm_msg_q_msg_t msg;
842   svm_msg_q_t *mq;
843
844   if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
845     {
846       /* Session is closed so app will never clean up. Flush rx fifo */
847       if (s->session_state == SESSION_STATE_CLOSED)
848         svm_fifo_dequeue_drop_all (s->server_rx_fifo);
849       return 0;
850     }
851
852   if (app->cb_fns.builtin_app_rx_callback)
853     return app->cb_fns.builtin_app_rx_callback (s);
854
855   if (svm_fifo_has_event (s->server_rx_fifo)
856       || svm_fifo_is_empty (s->server_rx_fifo))
857     return 0;
858
859   mq = app->event_queue;
860   if (lock)
861     svm_msg_q_lock (mq);
862
863   if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
864     {
865       clib_warning ("evt q rings full");
866       if (lock)
867         svm_msg_q_unlock (mq);
868       return -1;
869     }
870
871   msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
872   ASSERT (!svm_msg_q_msg_is_invalid (&msg));
873
874   evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
875   evt->fifo = s->server_rx_fifo;
876   evt->event_type = FIFO_EVENT_APP_RX;
877
878   if (app_enqueue_evt (mq, &msg, lock))
879     return -1;
880   (void) svm_fifo_set_event (s->server_rx_fifo);
881   return 0;
882 }
883
884 static inline int
885 app_send_io_evt_tx (application_t * app, stream_session_t * s, u8 lock)
886 {
887   svm_msg_q_t *mq;
888   session_event_t *evt;
889   svm_msg_q_msg_t msg;
890
891   if (application_is_builtin (app))
892     return 0;
893
894   mq = app->event_queue;
895   if (lock)
896     svm_msg_q_lock (mq);
897
898   if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
899     {
900       clib_warning ("evt q rings full");
901       if (lock)
902         svm_msg_q_unlock (mq);
903       return -1;
904     }
905
906   msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
907   ASSERT (!svm_msg_q_msg_is_invalid (&msg));
908
909   evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
910   evt->event_type = FIFO_EVENT_APP_TX;
911   evt->fifo = s->server_tx_fifo;
912
913   return app_enqueue_evt (mq, &msg, lock);
914 }
915
916 /* *INDENT-OFF* */
917 typedef int (app_send_evt_handler_fn) (application_t *app,
918                                        stream_session_t *s,
919                                        u8 lock);
920 static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
921     app_send_io_evt_rx,
922     app_send_io_evt_tx,
923 };
924 /* *INDENT-ON* */
925
926 /**
927  * Send event to application
928  *
929  * Logic from queue perspective is non-blocking. That is, if there's
930  * not enough space to enqueue a message, we return. However, if the lock
931  * flag is set, we do wait for queue mutex.
932  */
933 int
934 application_send_event (application_t * app, stream_session_t * s,
935                         u8 evt_type)
936 {
937   ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
938   return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
939 }
940
941 int
942 application_lock_and_send_event (application_t * app, stream_session_t * s,
943                                  u8 evt_type)
944 {
945   return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
946 }
947
948 local_session_t *
949 application_alloc_local_session (application_t * app)
950 {
951   local_session_t *s;
952   pool_get (app->local_sessions, s);
953   memset (s, 0, sizeof (*s));
954   s->app_index = app->index;
955   s->session_index = s - app->local_sessions;
956   s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
957   return s;
958 }
959
960 void
961 application_free_local_session (application_t * app, local_session_t * s)
962 {
963   pool_put (app->local_sessions, s);
964   if (CLIB_DEBUG)
965     memset (s, 0xfc, sizeof (*s));
966 }
967
968 local_session_t *
969 application_get_local_session (application_t * app, u32 session_index)
970 {
971   return pool_elt_at_index (app->local_sessions, session_index);
972 }
973
974 local_session_t *
975 application_get_local_session_from_handle (session_handle_t handle)
976 {
977   application_t *server;
978   u32 session_index, server_index;
979   local_session_parse_handle (handle, &server_index, &session_index);
980   server = application_get (server_index);
981   return application_get_local_session (server, session_index);
982 }
983
984 always_inline void
985 application_local_listener_session_endpoint (local_session_t * ll,
986                                              session_endpoint_t * sep)
987 {
988   sep->transport_proto =
989     session_type_transport_proto (ll->listener_session_type);
990   sep->port = ll->port;
991   sep->is_ip4 = ll->listener_session_type & 1;
992 }
993
994 int
995 application_start_local_listen (application_t * server,
996                                 session_endpoint_t * sep,
997                                 session_handle_t * handle)
998 {
999   session_handle_t lh;
1000   local_session_t *ll;
1001   u32 table_index;
1002
1003   table_index = application_local_session_table (server);
1004
1005   /* An exact sep match, as opposed to session_lookup_local_listener */
1006   lh = session_lookup_endpoint_listener (table_index, sep, 1);
1007   if (lh != SESSION_INVALID_HANDLE)
1008     return VNET_API_ERROR_ADDRESS_IN_USE;
1009
1010   pool_get (server->local_listen_sessions, ll);
1011   memset (ll, 0, sizeof (*ll));
1012   ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1013   ll->app_index = server->index;
1014   ll->session_index = ll - server->local_listen_sessions;
1015   ll->port = sep->port;
1016   /* Store the original session type for the unbind */
1017   ll->listener_session_type =
1018     session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
1019   ll->transport_listener_index = ~0;
1020
1021   *handle = application_local_session_handle (ll);
1022   session_lookup_add_session_endpoint (table_index, sep, *handle);
1023
1024   return 0;
1025 }
1026
1027 /**
1028  * Clean up local session table. If we have a listener session use it to
1029  * find the port and proto. If not, the handle must be a local table handle
1030  * so parse it.
1031  */
1032 int
1033 application_stop_local_listen (application_t * server, session_handle_t lh)
1034 {
1035   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1036   u32 table_index, ll_index, server_index;
1037   stream_session_t *sl = 0;
1038   local_session_t *ll, *ls;
1039
1040   table_index = application_local_session_table (server);
1041
1042   /* We have both local and global table binds. Figure from global what
1043    * the sep we should be cleaning up is.
1044    */
1045   if (!session_handle_is_local (lh))
1046     {
1047       sl = listen_session_get_from_handle (lh);
1048       if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1049         {
1050           clib_warning ("broken listener");
1051           return -1;
1052         }
1053       lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1054       if (lh == SESSION_INVALID_HANDLE)
1055         return -1;
1056     }
1057
1058   local_session_parse_handle (lh, &server_index, &ll_index);
1059   ASSERT (server->index == server_index);
1060   if (!(ll = application_get_local_listen_session (server, ll_index)))
1061     {
1062       clib_warning ("no local listener");
1063       return -1;
1064     }
1065   application_local_listener_session_endpoint (ll, &sep);
1066   session_lookup_del_session_endpoint (table_index, &sep);
1067
1068   /* *INDENT-OFF* */
1069   pool_foreach (ls, server->local_sessions, ({
1070     if (ls->listener_index == ll->session_index)
1071       application_local_session_disconnect (server->index, ls);
1072   }));
1073   /* *INDENT-ON* */
1074   pool_put_index (server->local_listen_sessions, ll->session_index);
1075
1076   return 0;
1077 }
1078
1079 int
1080 application_local_session_connect (u32 table_index, application_t * client,
1081                                    application_t * server,
1082                                    local_session_t * ll, u32 opaque)
1083 {
1084   u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1085   segment_manager_properties_t *props, *cprops;
1086   u32 round_rx_fifo_sz, round_tx_fifo_sz;
1087   int rv, has_transport, seg_index;
1088   svm_fifo_segment_private_t *seg;
1089   segment_manager_t *sm;
1090   local_session_t *ls;
1091   svm_msg_q_t *sq, *cq;
1092
1093   ls = application_alloc_local_session (server);
1094
1095   props = application_segment_manager_properties (server);
1096   cprops = application_segment_manager_properties (client);
1097   evt_q_elts = props->evt_q_size + cprops->evt_q_size;
1098   evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
1099   round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
1100   round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
1101   seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
1102
1103   has_transport = session_has_transport ((stream_session_t *) ll);
1104   if (!has_transport)
1105     {
1106       /* Local sessions don't have backing transport */
1107       ls->port = ll->port;
1108       sm = application_get_local_segment_manager (server);
1109     }
1110   else
1111     {
1112       stream_session_t *sl = (stream_session_t *) ll;
1113       transport_connection_t *tc;
1114       tc = listen_session_get_transport (sl);
1115       ls->port = tc->lcl_port;
1116       sm = application_get_listen_segment_manager (server, sl);
1117     }
1118
1119   seg_index = segment_manager_add_segment (sm, seg_size);
1120   if (seg_index < 0)
1121     {
1122       clib_warning ("failed to add new cut-through segment");
1123       return seg_index;
1124     }
1125   seg = segment_manager_get_segment_w_lock (sm, seg_index);
1126   sq = segment_manager_alloc_queue (seg, props->evt_q_size);
1127   cq = segment_manager_alloc_queue (seg, cprops->evt_q_size);
1128   ls->server_evt_q = pointer_to_uword (sq);
1129   ls->client_evt_q = pointer_to_uword (cq);
1130   rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1131                                         props->tx_fifo_size,
1132                                         &ls->server_rx_fifo,
1133                                         &ls->server_tx_fifo);
1134   if (rv)
1135     {
1136       clib_warning ("failed to add fifos in cut-through segment");
1137       segment_manager_segment_reader_unlock (sm);
1138       goto failed;
1139     }
1140   ls->server_rx_fifo->master_session_index = ls->session_index;
1141   ls->server_tx_fifo->master_session_index = ls->session_index;
1142   ls->server_rx_fifo->master_thread_index = ~0;
1143   ls->server_tx_fifo->master_thread_index = ~0;
1144   ls->svm_segment_index = seg_index;
1145   ls->listener_index = ll->session_index;
1146   ls->client_index = client->index;
1147   ls->client_opaque = opaque;
1148   ls->listener_session_type = ll->session_type;
1149
1150   if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
1151                                                  &seg->ssvm)))
1152     {
1153       clib_warning ("failed to notify server of new segment");
1154       segment_manager_segment_reader_unlock (sm);
1155       goto failed;
1156     }
1157   segment_manager_segment_reader_unlock (sm);
1158   if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
1159     {
1160       clib_warning ("failed to send accept cut-through notify to server");
1161       goto failed;
1162     }
1163   if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1164     application_local_session_connect_notify (ls);
1165
1166   return 0;
1167
1168 failed:
1169   if (!has_transport)
1170     segment_manager_del_segment (sm, seg);
1171   return rv;
1172 }
1173
1174 static uword
1175 application_client_local_connect_key (local_session_t * ls)
1176 {
1177   return ((uword) ls->app_index << 32 | (uword) ls->session_index);
1178 }
1179
1180 static void
1181 application_client_local_connect_key_parse (uword key, u32 * app_index,
1182                                             u32 * session_index)
1183 {
1184   *app_index = key >> 32;
1185   *session_index = key & 0xFFFFFFFF;
1186 }
1187
1188 int
1189 application_local_session_connect_notify (local_session_t * ls)
1190 {
1191   svm_fifo_segment_private_t *seg;
1192   application_t *client, *server;
1193   segment_manager_t *sm;
1194   int rv, is_fail = 0;
1195   uword client_key;
1196
1197   client = application_get (ls->client_index);
1198   server = application_get (ls->app_index);
1199   sm = application_get_local_segment_manager_w_session (server, ls);
1200   seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1201   if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1202                                                  &seg->ssvm)))
1203     {
1204       clib_warning ("failed to notify client %u of new segment",
1205                     ls->client_index);
1206       segment_manager_segment_reader_unlock (sm);
1207       application_local_session_disconnect (ls->client_index, ls);
1208       is_fail = 1;
1209     }
1210   else
1211     {
1212       segment_manager_segment_reader_unlock (sm);
1213     }
1214
1215   client->cb_fns.session_connected_callback (client->index, ls->client_opaque,
1216                                              (stream_session_t *) ls,
1217                                              is_fail);
1218
1219   client_key = application_client_local_connect_key (ls);
1220   hash_set (client->local_connects, client_key, client_key);
1221   return 0;
1222 }
1223
1224 int
1225 application_local_session_cleanup (application_t * client,
1226                                    application_t * server,
1227                                    local_session_t * ls)
1228 {
1229   svm_fifo_segment_private_t *seg;
1230   segment_manager_t *sm;
1231   uword client_key;
1232   u8 has_transport;
1233
1234   has_transport = session_has_transport ((stream_session_t *) ls);
1235   client_key = application_client_local_connect_key (ls);
1236   if (!has_transport)
1237     sm = application_get_local_segment_manager_w_session (server, ls);
1238   else
1239     sm = application_get_listen_segment_manager (server,
1240                                                  (stream_session_t *) ls);
1241
1242   seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1243   if (client)
1244     hash_unset (client->local_connects, client_key);
1245
1246   if (!has_transport)
1247     {
1248       server->cb_fns.del_segment_callback (server->api_client_index,
1249                                            &seg->ssvm);
1250       if (client)
1251         client->cb_fns.del_segment_callback (client->api_client_index,
1252                                              &seg->ssvm);
1253       segment_manager_del_segment (sm, seg);
1254     }
1255
1256   application_free_local_session (server, ls);
1257
1258   return 0;
1259 }
1260
1261 int
1262 application_local_session_disconnect (u32 app_index, local_session_t * ls)
1263 {
1264   application_t *client, *server;
1265
1266   client = application_get_if_valid (ls->client_index);
1267   server = application_get (ls->app_index);
1268
1269   if (ls->session_state == SESSION_STATE_CLOSED)
1270     return application_local_session_cleanup (client, server, ls);
1271
1272   if (app_index == ls->client_index)
1273     {
1274       send_local_session_disconnect_callback (ls->app_index, ls);
1275     }
1276   else
1277     {
1278       if (!client)
1279         {
1280           return application_local_session_cleanup (client, server, ls);
1281         }
1282       else if (ls->session_state < SESSION_STATE_READY)
1283         {
1284           client->cb_fns.session_connected_callback (client->index,
1285                                                      ls->client_opaque,
1286                                                      (stream_session_t *) ls,
1287                                                      1 /* is_fail */ );
1288           ls->session_state = SESSION_STATE_CLOSED;
1289           return application_local_session_cleanup (client, server, ls);
1290         }
1291       else
1292         {
1293           send_local_session_disconnect_callback (client->index, ls);
1294         }
1295     }
1296
1297   ls->session_state = SESSION_STATE_CLOSED;
1298
1299   return 0;
1300 }
1301
1302 int
1303 application_local_session_disconnect_w_index (u32 app_index, u32 ls_index)
1304 {
1305   application_t *app;
1306   local_session_t *ls;
1307   app = application_get (app_index);
1308   ls = application_get_local_session (app, ls_index);
1309   return application_local_session_disconnect (app_index, ls);
1310 }
1311
1312 void
1313 application_local_sessions_del (application_t * app)
1314 {
1315   u32 index, server_index, session_index, table_index;
1316   segment_manager_t *sm;
1317   u64 handle, *handles = 0;
1318   local_session_t *ls, *ll;
1319   application_t *server;
1320   session_endpoint_t sep;
1321   int i;
1322
1323   /*
1324    * Local listens. Don't bother with local sessions, we clean them lower
1325    */
1326   table_index = application_local_session_table (app);
1327   /* *INDENT-OFF* */
1328   pool_foreach (ll, app->local_listen_sessions, ({
1329     application_local_listener_session_endpoint (ll, &sep);
1330     session_lookup_del_session_endpoint (table_index, &sep);
1331   }));
1332   /* *INDENT-ON* */
1333
1334   /*
1335    * Local sessions
1336    */
1337   if (app->local_sessions)
1338     {
1339       /* *INDENT-OFF* */
1340       pool_foreach (ls, app->local_sessions, ({
1341         application_local_session_disconnect (app->index, ls);
1342       }));
1343       /* *INDENT-ON* */
1344     }
1345
1346   /*
1347    * Local connects
1348    */
1349   vec_reset_length (handles);
1350   /* *INDENT-OFF* */
1351   hash_foreach (handle, index, app->local_connects, ({
1352     vec_add1 (handles, handle);
1353   }));
1354   /* *INDENT-ON* */
1355
1356   for (i = 0; i < vec_len (handles); i++)
1357     {
1358       application_client_local_connect_key_parse (handles[i], &server_index,
1359                                                   &session_index);
1360       server = application_get_if_valid (server_index);
1361       if (server)
1362         {
1363           ls = application_get_local_session (server, session_index);
1364           application_local_session_disconnect (app->index, ls);
1365         }
1366     }
1367
1368   sm = segment_manager_get (app->local_segment_manager);
1369   sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1370   segment_manager_del (sm);
1371 }
1372
1373 clib_error_t *
1374 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1375 {
1376   application_t *app;
1377   app = application_get (a->app_index);
1378   if (!app)
1379     return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1380                                    0, "app %u doesn't exist", a->app_index);
1381   app->tls_cert = vec_dup (a->cert);
1382   return 0;
1383 }
1384
1385 clib_error_t *
1386 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1387 {
1388   application_t *app;
1389   app = application_get (a->app_index);
1390   if (!app)
1391     return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1392                                    0, "app %u doesn't exist", a->app_index);
1393   app->tls_key = vec_dup (a->key);
1394   return 0;
1395 }
1396
1397 u8 *
1398 format_application_listener (u8 * s, va_list * args)
1399 {
1400   application_t *app = va_arg (*args, application_t *);
1401   u64 handle = va_arg (*args, u64);
1402   u32 sm_index = va_arg (*args, u32);
1403   int verbose = va_arg (*args, int);
1404   stream_session_t *listener;
1405   u8 *app_name, *str;
1406
1407   if (app == 0)
1408     {
1409       if (verbose)
1410         s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
1411                     "API Client", "ListenerID", "SegManager");
1412       else
1413         s = format (s, "%-40s%-20s", "Connection", "App");
1414
1415       return s;
1416     }
1417
1418   app_name = app_get_name_from_reg_index (app);
1419   listener = listen_session_get_from_handle (handle);
1420   str = format (0, "%U", format_stream_session, listener, verbose);
1421
1422   if (verbose)
1423     {
1424       s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
1425                   app->api_client_index, handle, sm_index);
1426     }
1427   else
1428     s = format (s, "%-40s%-20s", str, app_name);
1429
1430   vec_free (app_name);
1431   return s;
1432 }
1433
1434 void
1435 application_format_connects (application_t * app, int verbose)
1436 {
1437   svm_fifo_segment_private_t *fifo_segment;
1438   vlib_main_t *vm = vlib_get_main ();
1439   segment_manager_t *sm;
1440   u8 *app_name, *s = 0;
1441
1442   /* Header */
1443   if (app == 0)
1444     {
1445       if (verbose)
1446         vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1447                          "API Client", "SegManager");
1448       else
1449         vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1450       return;
1451     }
1452
1453   /* make sure */
1454   if (app->connects_seg_manager == (u32) ~ 0)
1455     return;
1456
1457   app_name = app_get_name_from_reg_index (app);
1458
1459   /* Across all fifo segments */
1460   sm = segment_manager_get (app->connects_seg_manager);
1461
1462   /* *INDENT-OFF* */
1463   segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
1464     svm_fifo_t *fifo;
1465     u8 *str;
1466
1467     fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
1468     while (fifo)
1469         {
1470           u32 session_index, thread_index;
1471           stream_session_t *session;
1472
1473           session_index = fifo->master_session_index;
1474           thread_index = fifo->master_thread_index;
1475
1476           session = session_get (session_index, thread_index);
1477           str = format (0, "%U", format_stream_session, session, verbose);
1478
1479           if (verbose)
1480             s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
1481                         app->api_client_index, app->connects_seg_manager);
1482           else
1483             s = format (s, "%-40s%-20s", str, app_name);
1484
1485           vlib_cli_output (vm, "%v", s);
1486           vec_reset_length (s);
1487           vec_free (str);
1488
1489           fifo = fifo->next;
1490         }
1491     vec_free (s);
1492   }));
1493   /* *INDENT-ON* */
1494
1495   vec_free (app_name);
1496 }
1497
1498 void
1499 application_format_local_sessions (application_t * app, int verbose)
1500 {
1501   vlib_main_t *vm = vlib_get_main ();
1502   local_session_t *ls;
1503   transport_proto_t tp;
1504   u8 *conn = 0;
1505
1506   /* Header */
1507   if (app == 0)
1508     {
1509       vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
1510                        "ClientApp");
1511       return;
1512     }
1513
1514   /* *INDENT-OFF* */
1515   pool_foreach (ls, app->local_listen_sessions, ({
1516     tp = session_type_transport_proto(ls->listener_session_type);
1517     conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1518                    ls->port);
1519     vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_index, "*");
1520     vec_reset_length (conn);
1521   }));
1522   pool_foreach (ls, app->local_sessions, ({
1523     tp = session_type_transport_proto(ls->listener_session_type);
1524     conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1525                    ls->port);
1526     vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_index,
1527                      ls->client_index);
1528     vec_reset_length (conn);
1529   }));
1530   /* *INDENT-ON* */
1531
1532   vec_free (conn);
1533 }
1534
1535 void
1536 application_format_local_connects (application_t * app, int verbose)
1537 {
1538   vlib_main_t *vm = vlib_get_main ();
1539   u32 app_index, session_index;
1540   application_t *server;
1541   local_session_t *ls;
1542   uword client_key;
1543   u64 value;
1544
1545   /* Header */
1546   if (app == 0)
1547     {
1548       if (verbose)
1549         vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
1550                          "Peer App", "SegManager");
1551       else
1552         vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
1553                          "Peer App");
1554       return;
1555     }
1556
1557   /* *INDENT-OFF* */
1558   hash_foreach (client_key, value, app->local_connects, ({
1559     application_client_local_connect_key_parse (client_key, &app_index,
1560                                                 &session_index);
1561     server = application_get (app_index);
1562     ls = application_get_local_session (server, session_index);
1563     vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_index, ls->client_index);
1564   }));
1565   /* *INDENT-ON* */
1566 }
1567
1568 u8 *
1569 format_application (u8 * s, va_list * args)
1570 {
1571   application_t *app = va_arg (*args, application_t *);
1572   CLIB_UNUSED (int verbose) = va_arg (*args, int);
1573   segment_manager_properties_t *props;
1574   const u8 *app_ns_name;
1575   u8 *app_name;
1576
1577   if (app == 0)
1578     {
1579       if (verbose)
1580         s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
1581                     "API Client", "Namespace", "Add seg size", "Rx-f size",
1582                     "Tx-f size");
1583       else
1584         s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
1585                     "Namespace");
1586       return s;
1587     }
1588
1589   app_name = app_get_name (app);
1590   app_ns_name = app_namespace_id_from_index (app->ns_index);
1591   props = application_segment_manager_properties (app);
1592   if (verbose)
1593     s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->index,
1594                 app_name, app->api_client_index, app->ns_index,
1595                 format_memory_size, props->add_segment_size,
1596                 format_memory_size, props->rx_fifo_size, format_memory_size,
1597                 props->tx_fifo_size);
1598   else
1599     s = format (s, "%-10u%-20s%-15d%-40s", app->index, app_name,
1600                 app->api_client_index, app_ns_name);
1601   return s;
1602 }
1603
1604
1605 void
1606 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1607 {
1608   application_t *app;
1609   u32 sm_index;
1610   u64 handle;
1611
1612   if (!pool_elts (app_pool))
1613     {
1614       vlib_cli_output (vm, "No active server bindings");
1615       return;
1616     }
1617
1618   if (do_local)
1619     {
1620       application_format_local_sessions (0, verbose);
1621       /* *INDENT-OFF* */
1622       pool_foreach (app, app_pool, ({
1623         if (!pool_elts (app->local_sessions)
1624             && !pool_elts(app->local_connects))
1625           continue;
1626         application_format_local_sessions (app, verbose);
1627       }));
1628       /* *INDENT-ON* */
1629     }
1630   else
1631     {
1632       vlib_cli_output (vm, "%U", format_application_listener, 0 /* header */ ,
1633                        0, 0, verbose);
1634
1635       /* *INDENT-OFF* */
1636       pool_foreach (app, app_pool, ({
1637         if (hash_elts (app->listeners_table) == 0)
1638           continue;
1639         hash_foreach (handle, sm_index, app->listeners_table, ({
1640           vlib_cli_output (vm, "%U", format_application_listener, app,
1641                            handle, sm_index, verbose);
1642         }));
1643       }));
1644       /* *INDENT-ON* */
1645     }
1646 }
1647
1648 void
1649 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1650 {
1651   application_t *app;
1652
1653   if (!pool_elts (app_pool))
1654     {
1655       vlib_cli_output (vm, "No active apps");
1656       return;
1657     }
1658
1659   if (do_local)
1660     {
1661       application_format_local_connects (0, verbose);
1662
1663       /* *INDENT-OFF* */
1664       pool_foreach (app, app_pool, ({
1665         if (app->local_connects)
1666           application_format_local_connects (app, verbose);
1667       }));
1668       /* *INDENT-ON* */
1669     }
1670   else
1671     {
1672       application_format_connects (0, verbose);
1673
1674       /* *INDENT-OFF* */
1675       pool_foreach (app, app_pool, ({
1676         if (app->connects_seg_manager == (u32)~0)
1677           continue;
1678         application_format_connects (app, verbose);
1679       }));
1680       /* *INDENT-ON* */
1681     }
1682 }
1683
1684 static clib_error_t *
1685 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1686                      vlib_cli_command_t * cmd)
1687 {
1688   int do_server = 0, do_client = 0, do_local = 0;
1689   application_t *app;
1690   int verbose = 0;
1691
1692   session_cli_return_if_not_enabled ();
1693
1694   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1695     {
1696       if (unformat (input, "server"))
1697         do_server = 1;
1698       else if (unformat (input, "client"))
1699         do_client = 1;
1700       else if (unformat (input, "local"))
1701         do_local = 1;
1702       else if (unformat (input, "verbose"))
1703         verbose = 1;
1704       else
1705         break;
1706     }
1707
1708   if (do_server)
1709     application_format_all_listeners (vm, do_local, verbose);
1710
1711   if (do_client)
1712     application_format_all_clients (vm, do_local, verbose);
1713
1714   /* Print app related info */
1715   if (!do_server && !do_client)
1716     {
1717       vlib_cli_output (vm, "%U", format_application, 0, verbose);
1718       /* *INDENT-OFF* */
1719       pool_foreach (app, app_pool, ({
1720         vlib_cli_output (vm, "%U", format_application, app, verbose);
1721       }));
1722       /* *INDENT-ON* */
1723     }
1724
1725   return 0;
1726 }
1727
1728 /* *INDENT-OFF* */
1729 VLIB_CLI_COMMAND (show_app_command, static) =
1730 {
1731   .path = "show app",
1732   .short_help = "show app [server|client] [verbose]",
1733   .function = show_app_command_fn,
1734 };
1735 /* *INDENT-ON* */
1736
1737 /*
1738  * fd.io coding-style-patch-verification: ON
1739  *
1740  * Local Variables:
1741  * eval: (c-set-style "gnu")
1742  * End:
1743  */