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