session: refactor local connects
[vpp.git] / src / vnet / session / application.c
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/application_namespace.h>
19 #include <vnet/session/application_local.h>
20 #include <vnet/session/session.h>
21
22 static app_main_t app_main;
23
24 #define app_interface_check_thread_and_barrier(_fn, _arg)               \
25   if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ()))                \
26     {                                                                   \
27       vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg));      \
28       return 0;                                                         \
29     }
30
31 static app_listener_t *
32 app_listener_alloc (application_t * app)
33 {
34   app_listener_t *app_listener;
35   pool_get (app->listeners, app_listener);
36   clib_memset (app_listener, 0, sizeof (*app_listener));
37   app_listener->al_index = app_listener - app->listeners;
38   app_listener->app_index = app->app_index;
39   app_listener->session_index = SESSION_INVALID_INDEX;
40   app_listener->local_index = SESSION_INVALID_INDEX;
41   return app_listener;
42 }
43
44 app_listener_t *
45 app_listener_get (application_t * app, u32 app_listener_index)
46 {
47   return pool_elt_at_index (app->listeners, app_listener_index);
48 }
49
50 static void
51 app_listener_free (application_t * app, app_listener_t * app_listener)
52 {
53   clib_bitmap_free (app_listener->workers);
54   pool_put (app->listeners, app_listener);
55   if (CLIB_DEBUG)
56     clib_memset (app_listener, 0xfa, sizeof (*app_listener));
57 }
58
59 static u32
60 app_listener_id (app_listener_t * al)
61 {
62   ASSERT (al->app_index < 1 << 16 && al->al_index < 1 << 16);
63   return (al->app_index << 16 | al->al_index);
64 }
65
66 session_handle_t
67 app_listener_handle (app_listener_t * al)
68 {
69   return ((u64) SESSION_LISTENER_PREFIX << 32 | (u64) app_listener_id (al));
70 }
71
72 static void
73 app_listener_id_parse (u32 listener_id, u32 * app_index,
74                        u32 * app_listener_index)
75 {
76   *app_index = listener_id >> 16;
77   *app_listener_index = listener_id & 0xFFFF;
78 }
79
80 void
81 app_listener_handle_parse (session_handle_t handle, u32 * app_index,
82                            u32 * app_listener_index)
83 {
84   app_listener_id_parse (handle & 0xFFFFFFFF, app_index, app_listener_index);
85 }
86
87 static app_listener_t *
88 app_listener_get_w_id (u32 listener_id)
89 {
90   u32 app_index, app_listener_index;
91   application_t *app;
92
93   app_listener_id_parse (listener_id, &app_index, &app_listener_index);
94   app = application_get_if_valid (app_index);
95   if (!app)
96     return 0;
97   return app_listener_get (app, app_listener_index);
98 }
99
100 app_listener_t *
101 app_listener_get_w_session (session_t * ls)
102 {
103   application_t *app;
104
105   app = application_get_if_valid (ls->app_index);
106   if (!app)
107     return 0;
108   return app_listener_get (app, ls->al_index);
109 }
110
111 app_listener_t *
112 app_listener_get_w_handle (session_handle_t handle)
113 {
114
115   if (handle >> 32 != SESSION_LISTENER_PREFIX)
116     return 0;
117
118   return app_listener_get_w_id (handle & 0xFFFFFFFF);
119 }
120
121 app_listener_t *
122 app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
123 {
124   u32 table_index, fib_proto;
125   session_endpoint_t *sep;
126   session_handle_t handle;
127   session_t *ls;
128
129   sep = (session_endpoint_t *) sep_ext;
130   if (application_has_local_scope (app) && session_endpoint_is_local (sep))
131     {
132       table_index = application_local_session_table (app);
133       handle = session_lookup_endpoint_listener (table_index, sep, 1);
134       if (handle != SESSION_INVALID_HANDLE)
135         {
136           ls = listen_session_get_from_handle (handle);
137           return app_listener_get_w_session (ls);
138         }
139     }
140
141   fib_proto = session_endpoint_fib_proto (sep);
142   table_index = application_session_table (app, fib_proto);
143   handle = session_lookup_endpoint_listener (table_index, sep, 1);
144   if (handle != SESSION_INVALID_HANDLE)
145     {
146       ls = listen_session_get_from_handle (handle);
147       return app_listener_get_w_session ((session_t *) ls);
148     }
149
150   return 0;
151 }
152
153 int
154 app_listener_alloc_and_init (application_t * app,
155                              session_endpoint_cfg_t * sep,
156                              app_listener_t ** listener)
157 {
158   app_listener_t *app_listener;
159   transport_connection_t *tc;
160   session_handle_t lh;
161   session_type_t st;
162   session_t *ls = 0;
163   u32 al_index;
164   int rv;
165
166   app_listener = app_listener_alloc (app);
167   al_index = app_listener->al_index;
168   st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
169
170   /*
171    * Add session endpoint to local session table. Only binds to "inaddr_any"
172    * (i.e., zero address) are added to local scope table.
173    */
174   if (application_has_local_scope (app)
175       && session_endpoint_is_local ((session_endpoint_t *) sep))
176     {
177       session_type_t local_st;
178       u32 table_index;
179
180       local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
181                                                  sep->is_ip4);
182       ls = listen_session_alloc (0, local_st);
183       ls->app_index = app->app_index;
184       ls->app_wrk_index = sep->app_wrk_index;
185       lh = session_handle (ls);
186
187       if ((rv = session_listen (ls, sep)))
188         {
189           ls = session_get_from_handle (lh);
190           session_free (ls);
191           return rv;
192         }
193
194       ls = session_get_from_handle (lh);
195       app_listener = app_listener_get (app, al_index);
196       app_listener->local_index = ls->session_index;
197       ls->al_index = al_index;
198
199       table_index = application_local_session_table (app);
200       session_lookup_add_session_endpoint (table_index,
201                                            (session_endpoint_t *) sep, lh);
202     }
203
204   if (application_has_global_scope (app))
205     {
206       /*
207        * Start listening on local endpoint for requested transport and scope.
208        * Creates a stream session with state LISTENING to be used in session
209        * lookups, prior to establishing connection. Requests transport to
210        * build it's own specific listening connection.
211        */
212       ls = listen_session_alloc (0, st);
213       ls->app_index = app->app_index;
214       ls->app_wrk_index = sep->app_wrk_index;
215
216       /* Listen pool can be reallocated if the transport is
217        * recursive (tls) */
218       lh = listen_session_get_handle (ls);
219
220       if ((rv = session_listen (ls, sep)))
221         {
222           ls = listen_session_get_from_handle (lh);
223           session_free (ls);
224           return rv;
225         }
226       ls = listen_session_get_from_handle (lh);
227       app_listener = app_listener_get (app, al_index);
228       app_listener->session_index = ls->session_index;
229       ls->al_index = al_index;
230
231       /* Add to the global lookup table after transport was initialized.
232        * Lookup table needs to be populated only now because sessions
233        * with cut-through transport are are added to app local tables that
234        * are not related to network fibs, i.e., cannot be added as
235        * connections */
236       tc = session_get_transport (ls);
237       session_lookup_add_connection (tc, lh);
238     }
239
240   if (!ls)
241     {
242       app_listener_free (app, app_listener);
243       return -1;
244     }
245
246   *listener = app_listener;
247   return 0;
248 }
249
250 void
251 app_listener_cleanup (app_listener_t * al)
252 {
253   application_t *app = application_get (al->app_index);
254   session_t *ls;
255
256   if (al->session_index != SESSION_INVALID_INDEX)
257     {
258       ls = session_get (al->session_index, 0);
259       session_stop_listen (ls);
260       listen_session_free (ls);
261     }
262   if (al->local_index != SESSION_INVALID_INDEX)
263     {
264       session_endpoint_t sep = SESSION_ENDPOINT_NULL;
265       u32 table_index;
266
267       table_index = application_local_session_table (app);
268       ls = listen_session_get (al->local_index);
269       ct_session_endpoint (ls, &sep);
270       session_lookup_del_session_endpoint (table_index, &sep);
271       session_stop_listen (ls);
272       listen_session_free (ls);
273     }
274   app_listener_free (app, al);
275 }
276
277 app_worker_t *
278 app_listener_select_worker (app_listener_t * al)
279 {
280   application_t *app;
281   u32 wrk_index;
282
283   app = application_get (al->app_index);
284   wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
285   if (wrk_index == ~0)
286     wrk_index = clib_bitmap_first_set (al->workers);
287
288   ASSERT (wrk_index != ~0);
289   al->accept_rotor = wrk_index;
290   return application_get_worker (app, wrk_index);
291 }
292
293 session_t *
294 app_listener_get_session (app_listener_t * al)
295 {
296   if (al->session_index == SESSION_INVALID_INDEX)
297     return 0;
298
299   return listen_session_get (al->session_index);
300 }
301
302 session_t *
303 app_listener_get_local_session (app_listener_t * al)
304 {
305   if (al->local_index == SESSION_INVALID_INDEX)
306     return 0;
307   return listen_session_get (al->local_index);
308 }
309
310 static app_worker_map_t *
311 app_worker_map_alloc (application_t * app)
312 {
313   app_worker_map_t *map;
314   pool_get (app->worker_maps, map);
315   clib_memset (map, 0, sizeof (*map));
316   return map;
317 }
318
319 static u32
320 app_worker_map_index (application_t * app, app_worker_map_t * map)
321 {
322   return (map - app->worker_maps);
323 }
324
325 static void
326 app_worker_map_free (application_t * app, app_worker_map_t * map)
327 {
328   pool_put (app->worker_maps, map);
329 }
330
331 static app_worker_map_t *
332 app_worker_map_get (application_t * app, u32 map_index)
333 {
334   if (pool_is_free_index (app->worker_maps, map_index))
335     return 0;
336   return pool_elt_at_index (app->worker_maps, map_index);
337 }
338
339 static const u8 *
340 app_get_name (application_t * app)
341 {
342   return app->name;
343 }
344
345 u32
346 application_session_table (application_t * app, u8 fib_proto)
347 {
348   app_namespace_t *app_ns;
349   app_ns = app_namespace_get (app->ns_index);
350   if (!application_has_global_scope (app))
351     return APP_INVALID_INDEX;
352   if (fib_proto == FIB_PROTOCOL_IP4)
353     return session_lookup_get_index_for_fib (fib_proto,
354                                              app_ns->ip4_fib_index);
355   else
356     return session_lookup_get_index_for_fib (fib_proto,
357                                              app_ns->ip6_fib_index);
358 }
359
360 u32
361 application_local_session_table (application_t * app)
362 {
363   app_namespace_t *app_ns;
364   if (!application_has_local_scope (app))
365     return APP_INVALID_INDEX;
366   app_ns = app_namespace_get (app->ns_index);
367   return app_ns->local_table_index;
368 }
369
370 /**
371  * Returns app name for app-index
372  */
373 const u8 *
374 application_name_from_index (u32 app_index)
375 {
376   application_t *app = application_get (app_index);
377   if (!app)
378     return 0;
379   return app_get_name (app);
380 }
381
382 static void
383 application_api_table_add (u32 app_index, u32 api_client_index)
384 {
385   if (api_client_index != APP_INVALID_INDEX)
386     hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
387 }
388
389 static void
390 application_api_table_del (u32 api_client_index)
391 {
392   hash_unset (app_main.app_by_api_client_index, api_client_index);
393 }
394
395 static void
396 application_name_table_add (application_t * app)
397 {
398   hash_set_mem (app_main.app_by_name, app->name, app->app_index);
399 }
400
401 static void
402 application_name_table_del (application_t * app)
403 {
404   hash_unset_mem (app_main.app_by_name, app->name);
405 }
406
407 application_t *
408 application_lookup (u32 api_client_index)
409 {
410   uword *p;
411   p = hash_get (app_main.app_by_api_client_index, api_client_index);
412   if (p)
413     return application_get_if_valid (p[0]);
414
415   return 0;
416 }
417
418 application_t *
419 application_lookup_name (const u8 * name)
420 {
421   uword *p;
422   p = hash_get_mem (app_main.app_by_name, name);
423   if (p)
424     return application_get (p[0]);
425
426   return 0;
427 }
428
429 static application_t *
430 application_alloc (void)
431 {
432   application_t *app;
433   pool_get (app_main.app_pool, app);
434   clib_memset (app, 0, sizeof (*app));
435   app->app_index = app - app_main.app_pool;
436   return app;
437 }
438
439 application_t *
440 application_get (u32 app_index)
441 {
442   if (app_index == APP_INVALID_INDEX)
443     return 0;
444   return pool_elt_at_index (app_main.app_pool, app_index);
445 }
446
447 application_t *
448 application_get_if_valid (u32 app_index)
449 {
450   if (pool_is_free_index (app_main.app_pool, app_index))
451     return 0;
452
453   return pool_elt_at_index (app_main.app_pool, app_index);
454 }
455
456 static void
457 application_verify_cb_fns (session_cb_vft_t * cb_fns)
458 {
459   if (cb_fns->session_accept_callback == 0)
460     clib_warning ("No accept callback function provided");
461   if (cb_fns->session_connected_callback == 0)
462     clib_warning ("No session connected callback function provided");
463   if (cb_fns->session_disconnect_callback == 0)
464     clib_warning ("No session disconnect callback function provided");
465   if (cb_fns->session_reset_callback == 0)
466     clib_warning ("No session reset callback function provided");
467 }
468
469 /**
470  * Check app config for given segment type
471  *
472  * Returns 1 on success and 0 otherwise
473  */
474 static u8
475 application_verify_cfg (ssvm_segment_type_t st)
476 {
477   u8 is_valid;
478   if (st == SSVM_SEGMENT_MEMFD)
479     {
480       is_valid = (session_manager_get_evt_q_segment () != 0);
481       if (!is_valid)
482         clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
483       return is_valid;
484     }
485   else if (st == SSVM_SEGMENT_SHM)
486     {
487       is_valid = (session_manager_get_evt_q_segment () == 0);
488       if (!is_valid)
489         clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
490       return is_valid;
491     }
492   else
493     return 1;
494 }
495
496 static int
497 application_alloc_and_init (app_init_args_t * a)
498 {
499   ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
500   segment_manager_properties_t *props;
501   vl_api_registration_t *reg;
502   application_t *app;
503   u64 *options;
504
505   app = application_alloc ();
506   options = a->options;
507   /*
508    * Make sure we support the requested configuration
509    */
510   if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
511     {
512       reg = vl_api_client_index_to_registration (a->api_client_index);
513       if (!reg)
514         return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
515       if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
516         seg_type = SSVM_SEGMENT_SHM;
517     }
518   else
519     {
520       if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
521         {
522           clib_warning ("mq eventfds can only be used if socket transport is "
523                         "used for api");
524           return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
525         }
526       seg_type = SSVM_SEGMENT_PRIVATE;
527     }
528
529   if (!application_verify_cfg (seg_type))
530     return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
531
532   /* Check that the obvious things are properly set up */
533   application_verify_cb_fns (a->session_cb_vft);
534
535   app->flags = options[APP_OPTIONS_FLAGS];
536   app->cb_fns = *a->session_cb_vft;
537   app->ns_index = options[APP_OPTIONS_NAMESPACE];
538   app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
539   app->name = vec_dup (a->name);
540
541   /* If no scope enabled, default to global */
542   if (!application_has_global_scope (app)
543       && !application_has_local_scope (app))
544     app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
545
546   props = application_segment_manager_properties (app);
547   segment_manager_properties_init (props);
548   props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
549   props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
550   if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
551     {
552       props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
553       props->add_segment = 1;
554     }
555   if (options[APP_OPTIONS_RX_FIFO_SIZE])
556     props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
557   if (options[APP_OPTIONS_TX_FIFO_SIZE])
558     props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
559   if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
560     props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
561   if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
562     props->use_mq_eventfd = 1;
563   if (options[APP_OPTIONS_TLS_ENGINE])
564     app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
565   props->segment_type = seg_type;
566
567   /* Add app to lookup by api_client_index table */
568   if (!application_is_builtin (app))
569     application_api_table_add (app->app_index, a->api_client_index);
570   else
571     application_name_table_add (app);
572
573   a->app_index = app->app_index;
574
575   APP_DBG ("New app name: %v api index: %u index %u", app->name,
576            app->api_client_index, app->app_index);
577
578   return 0;
579 }
580
581 static void
582 application_free (application_t * app)
583 {
584   app_worker_map_t *wrk_map;
585   app_worker_t *app_wrk;
586
587   /*
588    * The app event queue allocated in first segment is cleared with
589    * the segment manager. No need to explicitly free it.
590    */
591   APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
592            app->api_client_index, app->app_index);
593
594   if (application_is_proxy (app))
595     application_remove_proxy (app);
596
597   /*
598    * Free workers
599    */
600
601   /* *INDENT-OFF* */
602   pool_flush (wrk_map, app->worker_maps, ({
603     app_wrk = app_worker_get (wrk_map->wrk_index);
604     app_worker_free (app_wrk);
605   }));
606   /* *INDENT-ON* */
607   pool_free (app->worker_maps);
608
609   /*
610    * Cleanup remaining state
611    */
612   if (application_is_builtin (app))
613     application_name_table_del (app);
614   vec_free (app->name);
615   vec_free (app->tls_cert);
616   vec_free (app->tls_key);
617   pool_put (app_main.app_pool, app);
618 }
619
620 static void
621 application_detach_process (application_t * app, u32 api_client_index)
622 {
623   vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
624   app_worker_map_t *wrk_map;
625   u32 *wrks = 0, *wrk_index;
626   app_worker_t *app_wrk;
627
628   if (api_client_index == ~0)
629     {
630       application_free (app);
631       return;
632     }
633
634   APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
635            app->app_index, app->api_client_index);
636
637   /* *INDENT-OFF* */
638   pool_foreach (wrk_map, app->worker_maps, ({
639     app_wrk = app_worker_get (wrk_map->wrk_index);
640     if (app_wrk->api_client_index == api_client_index)
641       vec_add1 (wrks, app_wrk->wrk_index);
642   }));
643   /* *INDENT-ON* */
644
645   if (!vec_len (wrks))
646     {
647       clib_warning ("no workers for app %u api_index %u", app->app_index,
648                     api_client_index);
649       return;
650     }
651
652   args->app_index = app->app_index;
653   args->api_client_index = api_client_index;
654   vec_foreach (wrk_index, wrks)
655   {
656     app_wrk = app_worker_get (wrk_index[0]);
657     args->wrk_map_index = app_wrk->wrk_map_index;
658     args->is_add = 0;
659     vnet_app_worker_add_del (args);
660   }
661   vec_free (wrks);
662 }
663
664 app_worker_t *
665 application_get_worker (application_t * app, u32 wrk_map_index)
666 {
667   app_worker_map_t *map;
668   map = app_worker_map_get (app, wrk_map_index);
669   if (!map)
670     return 0;
671   return app_worker_get (map->wrk_index);
672 }
673
674 app_worker_t *
675 application_get_default_worker (application_t * app)
676 {
677   return application_get_worker (app, 0);
678 }
679
680 u32
681 application_n_workers (application_t * app)
682 {
683   return pool_elts (app->worker_maps);
684 }
685
686 app_worker_t *
687 application_listener_select_worker (session_t * ls)
688 {
689   app_listener_t *al;
690
691   al = app_listener_get_w_session (ls);
692   return app_listener_select_worker (al);
693 }
694
695 int
696 application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
697 {
698   app_worker_map_t *wrk_map;
699   app_worker_t *app_wrk;
700   segment_manager_t *sm;
701   int rv;
702
703   app_wrk = app_worker_alloc (app);
704   wrk_map = app_worker_map_alloc (app);
705   wrk_map->wrk_index = app_wrk->wrk_index;
706   app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
707
708   /*
709    * Setup first segment manager
710    */
711   sm = segment_manager_new ();
712   sm->app_wrk_index = app_wrk->wrk_index;
713
714   if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
715                                   app->sm_properties.prealloc_fifos)))
716     {
717       app_worker_free (app_wrk);
718       return rv;
719     }
720   sm->first_is_protected = 1;
721
722   /*
723    * Setup app worker
724    */
725   app_wrk->first_segment_manager = segment_manager_index (sm);
726   app_wrk->listeners_table = hash_create (0, sizeof (u64));
727   app_wrk->event_queue = segment_manager_event_queue (sm);
728   app_wrk->app_is_builtin = application_is_builtin (app);
729
730   *wrk = app_wrk;
731
732   return 0;
733 }
734
735 int
736 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
737 {
738   svm_fifo_segment_private_t *fs;
739   app_worker_map_t *wrk_map;
740   app_worker_t *app_wrk;
741   segment_manager_t *sm;
742   application_t *app;
743   int rv;
744
745   app = application_get (a->app_index);
746   if (!app)
747     return VNET_API_ERROR_INVALID_VALUE;
748
749   if (a->is_add)
750     {
751       if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
752         return rv;
753
754       /* Map worker api index to the app */
755       app_wrk->api_client_index = a->api_client_index;
756       application_api_table_add (app->app_index, a->api_client_index);
757
758       sm = segment_manager_get (app_wrk->first_segment_manager);
759       fs = segment_manager_get_segment_w_lock (sm, 0);
760       a->segment = &fs->ssvm;
761       a->segment_handle = segment_manager_segment_handle (sm, fs);
762       segment_manager_segment_reader_unlock (sm);
763       a->evt_q = app_wrk->event_queue;
764       a->wrk_map_index = app_wrk->wrk_map_index;
765     }
766   else
767     {
768       wrk_map = app_worker_map_get (app, a->wrk_map_index);
769       if (!wrk_map)
770         return VNET_API_ERROR_INVALID_VALUE;
771
772       app_wrk = app_worker_get (wrk_map->wrk_index);
773       if (!app_wrk)
774         return VNET_API_ERROR_INVALID_VALUE;
775
776       application_api_table_del (app_wrk->api_client_index);
777       app_worker_free (app_wrk);
778       app_worker_map_free (app, wrk_map);
779       if (application_n_workers (app) == 0)
780         application_free (app);
781     }
782   return 0;
783 }
784
785 static int
786 app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
787 {
788   app_namespace_t *app_ns;
789   if (vec_len (namespace_id) == 0)
790     {
791       /* Use default namespace */
792       *app_ns_index = 0;
793       return 0;
794     }
795
796   *app_ns_index = app_namespace_index_from_id (namespace_id);
797   if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
798     return VNET_API_ERROR_APP_INVALID_NS;
799   app_ns = app_namespace_get (*app_ns_index);
800   if (!app_ns)
801     return VNET_API_ERROR_APP_INVALID_NS;
802   if (app_ns->ns_secret != secret)
803     return VNET_API_ERROR_APP_WRONG_NS_SECRET;
804   return 0;
805 }
806
807 static u8 *
808 app_name_from_api_index (u32 api_client_index)
809 {
810   vl_api_registration_t *regp;
811   regp = vl_api_client_index_to_registration (api_client_index);
812   if (regp)
813     return format (0, "%s%c", regp->name, 0);
814
815   clib_warning ("api client index %u does not have an api registration!",
816                 api_client_index);
817   return format (0, "unknown%c", 0);
818 }
819
820 /**
821  * Attach application to vpp
822  *
823  * Allocates a vpp app, i.e., a structure that keeps back pointers
824  * to external app and a segment manager for shared memory fifo based
825  * communication with the external app.
826  */
827 int
828 vnet_application_attach (vnet_app_attach_args_t * a)
829 {
830   svm_fifo_segment_private_t *fs;
831   application_t *app = 0;
832   app_worker_t *app_wrk;
833   segment_manager_t *sm;
834   u32 app_ns_index = 0;
835   u8 *app_name = 0;
836   u64 secret;
837   int rv;
838
839   if (a->api_client_index != APP_INVALID_INDEX)
840     app = application_lookup (a->api_client_index);
841   else if (a->name)
842     app = application_lookup_name (a->name);
843   else
844     return VNET_API_ERROR_INVALID_VALUE;
845
846   if (app)
847     return VNET_API_ERROR_APP_ALREADY_ATTACHED;
848
849   if (a->api_client_index != APP_INVALID_INDEX)
850     {
851       app_name = app_name_from_api_index (a->api_client_index);
852       a->name = app_name;
853     }
854
855   secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
856   if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
857     return rv;
858   a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
859
860   if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
861     return rv;
862
863   app = application_get (a->app_index);
864   if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
865     return rv;
866
867   a->app_evt_q = app_wrk->event_queue;
868   app_wrk->api_client_index = a->api_client_index;
869   sm = segment_manager_get (app_wrk->first_segment_manager);
870   fs = segment_manager_get_segment_w_lock (sm, 0);
871
872   if (application_is_proxy (app))
873     application_setup_proxy (app);
874
875   ASSERT (vec_len (fs->ssvm.name) <= 128);
876   a->segment = &fs->ssvm;
877   a->segment_handle = segment_manager_segment_handle (sm, fs);
878
879   segment_manager_segment_reader_unlock (sm);
880   vec_free (app_name);
881   return 0;
882 }
883
884 /**
885  * Detach application from vpp
886  */
887 int
888 vnet_application_detach (vnet_app_detach_args_t * a)
889 {
890   application_t *app;
891
892   app = application_get_if_valid (a->app_index);
893   if (!app)
894     {
895       clib_warning ("app not attached");
896       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
897     }
898
899   app_interface_check_thread_and_barrier (vnet_application_detach, a);
900   application_detach_process (app, a->api_client_index);
901   return 0;
902 }
903
904
905 static u8
906 session_endpoint_in_ns (session_endpoint_t * sep)
907 {
908   u8 is_lep = session_endpoint_is_local (sep);
909   if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
910       && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
911     {
912       clib_warning ("sw_if_index %u not configured with ip %U",
913                     sep->sw_if_index, format_ip46_address, &sep->ip,
914                     sep->is_ip4);
915       return 0;
916     }
917   return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
918 }
919
920 static void
921 session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
922                                  application_t * app, u8 is_connect)
923 {
924   app_namespace_t *app_ns;
925   u32 ns_index, fib_index;
926
927   ns_index = app->ns_index;
928
929   /* App is a transport proto, so fetch the calling app's ns */
930   if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
931     ns_index = sep->ns_index;
932
933   app_ns = app_namespace_get (ns_index);
934   if (!app_ns)
935     return;
936
937   /* Ask transport and network to bind to/connect using local interface
938    * that "supports" app's namespace. This will fix our local connection
939    * endpoint.
940    */
941
942   /* If in default namespace and user requested a fib index use it */
943   if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
944     fib_index = sep->fib_index;
945   else
946     fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
947   sep->peer.fib_index = fib_index;
948   sep->fib_index = fib_index;
949
950   if (!is_connect)
951     {
952       sep->sw_if_index = app_ns->sw_if_index;
953     }
954   else
955     {
956       if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
957           && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
958           && sep->peer.sw_if_index != app_ns->sw_if_index)
959         clib_warning ("Local sw_if_index different from app ns sw_if_index");
960
961       sep->peer.sw_if_index = app_ns->sw_if_index;
962     }
963 }
964
965 int
966 vnet_listen (vnet_listen_args_t * a)
967 {
968   app_listener_t *app_listener;
969   app_worker_t *app_wrk;
970   application_t *app;
971   int rv;
972
973   app = application_get_if_valid (a->app_index);
974   if (!app)
975     return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
976
977   app_wrk = application_get_worker (app, a->wrk_map_index);
978   if (!app_wrk)
979     return VNET_API_ERROR_INVALID_VALUE;
980
981   a->sep_ext.app_wrk_index = app_wrk->wrk_index;
982
983   session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
984   if (!session_endpoint_in_ns (&a->sep))
985     return VNET_API_ERROR_INVALID_VALUE_2;
986
987   /*
988    * Check if we already have an app listener
989    */
990   app_listener = app_listener_lookup (app, &a->sep_ext);
991   if (app_listener)
992     {
993       if (app_listener->app_index != app->app_index)
994         return VNET_API_ERROR_ADDRESS_IN_USE;
995       if (app_worker_start_listen (app_wrk, app_listener))
996         return -1;
997       a->handle = app_listener_handle (app_listener);
998       return 0;
999     }
1000
1001   /*
1002    * Create new app listener
1003    */
1004   if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1005     return rv;
1006
1007   if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1008     {
1009       app_listener_cleanup (app_listener);
1010       return rv;
1011     }
1012
1013   a->handle = app_listener_handle (app_listener);
1014   return 0;
1015 }
1016
1017 int
1018 vnet_connect (vnet_connect_args_t * a)
1019 {
1020   app_worker_t *client_wrk;
1021   application_t *client;
1022
1023   if (session_endpoint_is_zero (&a->sep))
1024     return VNET_API_ERROR_INVALID_VALUE;
1025
1026   client = application_get (a->app_index);
1027   session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1028   client_wrk = application_get_worker (client, a->wrk_map_index);
1029
1030   /*
1031    * First check the local scope for locally attached destinations.
1032    * If we have local scope, we pass *all* connects through it since we may
1033    * have special policy rules even for non-local destinations, think proxy.
1034    */
1035   if (application_has_local_scope (client))
1036     {
1037       int rv;
1038
1039       a->sep_ext.original_tp = a->sep_ext.transport_proto;
1040       a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE;
1041       rv = app_worker_connect_session (client_wrk, &a->sep, a->api_context);
1042       if (rv <= 0)
1043         return rv;
1044     }
1045   /*
1046    * Not connecting to a local server, propagate to transport
1047    */
1048   if (app_worker_connect_session (client_wrk, &a->sep, a->api_context))
1049     return VNET_API_ERROR_SESSION_CONNECT;
1050   return 0;
1051 }
1052
1053 int
1054 vnet_unlisten (vnet_unlisten_args_t * a)
1055 {
1056   app_worker_t *app_wrk;
1057   app_listener_t *al;
1058   application_t *app;
1059
1060   if (!(app = application_get_if_valid (a->app_index)))
1061     return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1062
1063   al = app_listener_get_w_handle (a->handle);
1064   if (al->app_index != app->app_index)
1065     {
1066       clib_warning ("app doesn't own handle %llu!", a->handle);
1067       return -1;
1068     }
1069
1070   app_wrk = application_get_worker (app, a->wrk_map_index);
1071   if (!app_wrk)
1072     {
1073       clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
1074       return -1;
1075     }
1076
1077   return app_worker_stop_listen (app_wrk, al);
1078 }
1079
1080 int
1081 vnet_disconnect_session (vnet_disconnect_args_t * a)
1082 {
1083   app_worker_t *app_wrk;
1084   session_t *s;
1085
1086   s = session_get_from_handle_if_valid (a->handle);
1087   if (!s)
1088     return VNET_API_ERROR_INVALID_VALUE;
1089   app_wrk = app_worker_get (s->app_wrk_index);
1090   if (app_wrk->app_index != a->app_index)
1091     return VNET_API_ERROR_INVALID_VALUE;
1092
1093   /* We're peeking into another's thread pool. Make sure */
1094   ASSERT (s->session_index == session_index_from_handle (a->handle));
1095
1096   session_close (s);
1097   return 0;
1098 }
1099
1100 int
1101 application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1102 {
1103   app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1104   app_listener_t *app_listener;
1105   application_t *app;
1106
1107   if (!old_wrk)
1108     return -1;
1109
1110   hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1111   if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1112       && s->rx_fifo)
1113     segment_manager_dealloc_fifos (s->rx_fifo->segment_index, s->rx_fifo,
1114                                    s->tx_fifo);
1115
1116   app = application_get (old_wrk->app_index);
1117   if (!app)
1118     return -1;
1119
1120   app_listener = app_listener_get (app, s->al_index);
1121
1122   /* Only remove from lb for now */
1123   app_listener->workers = clib_bitmap_set (app_listener->workers,
1124                                            old_wrk->wrk_map_index, 0);
1125
1126   if (app_worker_start_listen (app_wrk, app_listener))
1127     return -1;
1128
1129   s->app_wrk_index = app_wrk->wrk_index;
1130
1131   return 0;
1132 }
1133
1134 int
1135 application_is_proxy (application_t * app)
1136 {
1137   return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1138 }
1139
1140 int
1141 application_is_builtin (application_t * app)
1142 {
1143   return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1144 }
1145
1146 int
1147 application_is_builtin_proxy (application_t * app)
1148 {
1149   return (application_is_proxy (app) && application_is_builtin (app));
1150 }
1151
1152 u8
1153 application_has_local_scope (application_t * app)
1154 {
1155   return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1156 }
1157
1158 u8
1159 application_has_global_scope (application_t * app)
1160 {
1161   return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1162 }
1163
1164 u8
1165 application_use_mq_for_ctrl (application_t * app)
1166 {
1167   return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
1168 }
1169
1170 static clib_error_t *
1171 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1172                                         u8 transport_proto, u8 is_start)
1173 {
1174   app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1175   u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1176   session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
1177   transport_connection_t *tc;
1178   app_worker_t *app_wrk;
1179   app_listener_t *al;
1180   session_t *s;
1181   u32 flags;
1182
1183   /* TODO decide if we want proxy to be enabled for all workers */
1184   app_wrk = application_get_default_worker (app);
1185   if (is_start)
1186     {
1187       s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1188       if (!s)
1189         {
1190           sep.is_ip4 = is_ip4;
1191           sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1192           sep.sw_if_index = app_ns->sw_if_index;
1193           sep.transport_proto = transport_proto;
1194           sep.app_wrk_index = app_wrk->wrk_index;       /* only default */
1195
1196           /* force global scope listener */
1197           flags = app->flags;
1198           app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1199           app_listener_alloc_and_init (app, &sep, &al);
1200           app->flags = flags;
1201
1202           app_worker_start_listen (app_wrk, al);
1203           s = listen_session_get (al->session_index);
1204           s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
1205         }
1206     }
1207   else
1208     {
1209       s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
1210       ASSERT (s);
1211     }
1212
1213   tc = listen_session_get_transport (s);
1214
1215   if (!ip_is_zero (&tc->lcl_ip, 1))
1216     {
1217       u32 sti;
1218       sep.is_ip4 = is_ip4;
1219       sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1220       sep.transport_proto = transport_proto;
1221       sep.port = 0;
1222       sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1223       if (is_start)
1224         session_lookup_add_session_endpoint (sti,
1225                                              (session_endpoint_t *) & sep,
1226                                              s->session_index);
1227       else
1228         session_lookup_del_session_endpoint (sti,
1229                                              (session_endpoint_t *) & sep);
1230     }
1231
1232   return 0;
1233 }
1234
1235 static void
1236 application_start_stop_proxy_local_scope (application_t * app,
1237                                           u8 transport_proto, u8 is_start)
1238 {
1239   session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1240   app_namespace_t *app_ns;
1241   app_ns = app_namespace_get (app->ns_index);
1242   sep.is_ip4 = 1;
1243   sep.transport_proto = transport_proto;
1244   sep.port = 0;
1245
1246   if (is_start)
1247     {
1248       session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1249                                            app->app_index);
1250       sep.is_ip4 = 0;
1251       session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1252                                            app->app_index);
1253     }
1254   else
1255     {
1256       session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1257       sep.is_ip4 = 0;
1258       session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1259     }
1260 }
1261
1262 void
1263 application_start_stop_proxy (application_t * app,
1264                               transport_proto_t transport_proto, u8 is_start)
1265 {
1266   if (application_has_local_scope (app))
1267     application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1268
1269   if (application_has_global_scope (app))
1270     {
1271       application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1272                                               transport_proto, is_start);
1273       application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1274                                               transport_proto, is_start);
1275     }
1276 }
1277
1278 void
1279 application_setup_proxy (application_t * app)
1280 {
1281   u16 transports = app->proxied_transports;
1282   transport_proto_t tp;
1283
1284   ASSERT (application_is_proxy (app));
1285
1286   /* *INDENT-OFF* */
1287   transport_proto_foreach (tp, ({
1288     if (transports & (1 << tp))
1289       application_start_stop_proxy (app, tp, 1);
1290   }));
1291   /* *INDENT-ON* */
1292 }
1293
1294 void
1295 application_remove_proxy (application_t * app)
1296 {
1297   u16 transports = app->proxied_transports;
1298   transport_proto_t tp;
1299
1300   ASSERT (application_is_proxy (app));
1301
1302   /* *INDENT-OFF* */
1303   transport_proto_foreach (tp, ({
1304     if (transports & (1 << tp))
1305       application_start_stop_proxy (app, tp, 0);
1306   }));
1307   /* *INDENT-ON* */
1308 }
1309
1310 segment_manager_properties_t *
1311 application_segment_manager_properties (application_t * app)
1312 {
1313   return &app->sm_properties;
1314 }
1315
1316 segment_manager_properties_t *
1317 application_get_segment_manager_properties (u32 app_index)
1318 {
1319   application_t *app = application_get (app_index);
1320   return &app->sm_properties;
1321 }
1322
1323 clib_error_t *
1324 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1325 {
1326   application_t *app;
1327   app = application_get (a->app_index);
1328   if (!app)
1329     return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1330                                    0, "app %u doesn't exist", a->app_index);
1331   app->tls_cert = vec_dup (a->cert);
1332   return 0;
1333 }
1334
1335 clib_error_t *
1336 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1337 {
1338   application_t *app;
1339   app = application_get (a->app_index);
1340   if (!app)
1341     return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1342                                    0, "app %u doesn't exist", a->app_index);
1343   app->tls_key = vec_dup (a->key);
1344   return 0;
1345 }
1346
1347 static void
1348 application_format_listeners (application_t * app, int verbose)
1349 {
1350   vlib_main_t *vm = vlib_get_main ();
1351   app_worker_map_t *wrk_map;
1352   app_worker_t *app_wrk;
1353   u32 sm_index;
1354   u64 handle;
1355
1356   if (!app)
1357     {
1358       vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1359                        0, 0, verbose);
1360       return;
1361     }
1362
1363   /* *INDENT-OFF* */
1364   pool_foreach (wrk_map, app->worker_maps, ({
1365     app_wrk = app_worker_get (wrk_map->wrk_index);
1366     if (hash_elts (app_wrk->listeners_table) == 0)
1367       continue;
1368     hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1369       vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1370                        handle, sm_index, verbose);
1371     }));
1372   }));
1373   /* *INDENT-ON* */
1374 }
1375
1376 static void
1377 application_format_connects (application_t * app, int verbose)
1378 {
1379   app_worker_map_t *wrk_map;
1380   app_worker_t *app_wrk;
1381
1382   if (!app)
1383     {
1384       app_worker_format_connects (0, verbose);
1385       return;
1386     }
1387
1388   /* *INDENT-OFF* */
1389   pool_foreach (wrk_map, app->worker_maps, ({
1390     app_wrk = app_worker_get (wrk_map->wrk_index);
1391     app_worker_format_connects (app_wrk, verbose);
1392   }));
1393   /* *INDENT-ON* */
1394 }
1395
1396 u8 *
1397 format_application (u8 * s, va_list * args)
1398 {
1399   application_t *app = va_arg (*args, application_t *);
1400   CLIB_UNUSED (int verbose) = va_arg (*args, int);
1401   segment_manager_properties_t *props;
1402   const u8 *app_ns_name, *app_name;
1403   app_worker_map_t *wrk_map;
1404   app_worker_t *app_wrk;
1405
1406   if (app == 0)
1407     {
1408       if (!verbose)
1409         s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
1410       return s;
1411     }
1412
1413   app_name = app_get_name (app);
1414   app_ns_name = app_namespace_id_from_index (app->ns_index);
1415   props = application_segment_manager_properties (app);
1416   if (!verbose)
1417     {
1418       s = format (s, "%-10u%-20s%-40s", app->app_index, app_name,
1419                   app_ns_name);
1420       return s;
1421     }
1422
1423   s = format (s, "app-name %s app-index %u ns-index %u seg-size %U\n",
1424               app_name, app->app_index, app->ns_index,
1425               format_memory_size, props->add_segment_size);
1426   s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1427               format_memory_size, props->rx_fifo_size,
1428               format_memory_size, props->tx_fifo_size);
1429
1430   /* *INDENT-OFF* */
1431   pool_foreach (wrk_map, app->worker_maps, ({
1432       app_wrk = app_worker_get (wrk_map->wrk_index);
1433       s = format (s, "%U", format_app_worker, app_wrk);
1434   }));
1435   /* *INDENT-ON* */
1436
1437   return s;
1438 }
1439
1440 void
1441 application_format_all_listeners (vlib_main_t * vm, int verbose)
1442 {
1443   application_t *app;
1444
1445   if (!pool_elts (app_main.app_pool))
1446     {
1447       vlib_cli_output (vm, "No active server bindings");
1448       return;
1449     }
1450
1451   application_format_listeners (0, verbose);
1452
1453   /* *INDENT-OFF* */
1454   pool_foreach (app, app_main.app_pool, ({
1455     application_format_listeners (app, verbose);
1456   }));
1457   /* *INDENT-ON* */
1458 }
1459
1460 void
1461 application_format_all_clients (vlib_main_t * vm, int verbose)
1462 {
1463   application_t *app;
1464
1465   if (!pool_elts (app_main.app_pool))
1466     {
1467       vlib_cli_output (vm, "No active apps");
1468       return;
1469     }
1470
1471   application_format_connects (0, verbose);
1472
1473   /* *INDENT-OFF* */
1474   pool_foreach (app, app_main.app_pool, ({
1475     application_format_connects (app, verbose);
1476   }));
1477   /* *INDENT-ON* */
1478 }
1479
1480 static clib_error_t *
1481 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1482                      vlib_cli_command_t * cmd)
1483 {
1484   int do_server = 0, do_client = 0;
1485   application_t *app;
1486   u32 app_index = ~0;
1487   int verbose = 0;
1488
1489   session_cli_return_if_not_enabled ();
1490
1491   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1492     {
1493       if (unformat (input, "server"))
1494         do_server = 1;
1495       else if (unformat (input, "client"))
1496         do_client = 1;
1497       else if (unformat (input, "%u", &app_index))
1498         ;
1499       else if (unformat (input, "verbose"))
1500         verbose = 1;
1501       else
1502         return clib_error_return (0, "unknown input `%U'",
1503                                   format_unformat_error, input);
1504     }
1505
1506   if (do_server)
1507     {
1508       application_format_all_listeners (vm, verbose);
1509       return 0;
1510     }
1511
1512   if (do_client)
1513     {
1514       application_format_all_clients (vm, verbose);
1515       return 0;
1516     }
1517
1518   if (app_index != ~0)
1519     {
1520       app = application_get_if_valid (app_index);
1521       if (!app)
1522         return clib_error_return (0, "No app with index %u", app_index);
1523
1524       vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
1525       return 0;
1526     }
1527
1528   /* Print app related info */
1529   if (!do_server && !do_client)
1530     {
1531       vlib_cli_output (vm, "%U", format_application, 0, 0);
1532       /* *INDENT-OFF* */
1533       pool_foreach (app, app_main.app_pool, ({
1534         vlib_cli_output (vm, "%U", format_application, app, 0);
1535       }));
1536       /* *INDENT-ON* */
1537     }
1538
1539   return 0;
1540 }
1541
1542 /* *INDENT-OFF* */
1543 VLIB_CLI_COMMAND (show_app_command, static) =
1544 {
1545   .path = "show app",
1546   .short_help = "show app [server|client] [verbose]",
1547   .function = show_app_command_fn,
1548 };
1549 /* *INDENT-ON* */
1550
1551 /*
1552  * fd.io coding-style-patch-verification: ON
1553  *
1554  * Local Variables:
1555  * eval: (c-set-style "gnu")
1556  * End:
1557  */