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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/application_namespace.h>
19 #include <vnet/session/session.h>
21 static app_main_t app_main;
24 application_local_listener_session_endpoint (local_session_t * ll,
25 session_endpoint_t * sep)
27 sep->transport_proto =
28 session_type_transport_proto (ll->listener_session_type);
30 sep->is_ip4 = ll->listener_session_type & 1;
33 static app_listener_t *
34 app_listener_alloc (application_t * app)
36 app_listener_t *app_listener;
37 pool_get (app->listeners, app_listener);
38 clib_memset (app_listener, 0, sizeof (*app_listener));
39 app_listener->al_index = app_listener - app->listeners;
40 app_listener->app_index = app->app_index;
41 app_listener->session_index = SESSION_INVALID_INDEX;
42 app_listener->local_index = SESSION_INVALID_INDEX;
47 app_listener_get (application_t * app, u32 app_listener_index)
49 return pool_elt_at_index (app->listeners, app_listener_index);
53 app_listener_free (application_t * app, app_listener_t * app_listener)
55 clib_bitmap_free (app_listener->workers);
56 pool_put (app->listeners, app_listener);
58 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
62 application_local_listen_session_alloc (application_t * app)
65 pool_get_zero (app->local_listen_sessions, ll);
66 ll->session_index = ll - app->local_listen_sessions;
67 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
68 ll->app_index = app->app_index;
73 application_local_listen_session_free (application_t * app,
76 pool_put (app->local_listen_sessions, ll);
78 clib_memset (ll, 0xfb, sizeof (*ll));
82 app_listener_id (app_listener_t * al)
84 ASSERT (al->app_index < 1 << 16 && al->al_index < 1 << 16);
85 return (al->app_index << 16 | al->al_index);
89 app_listener_handle (app_listener_t * al)
91 return ((u64) SESSION_LISTENER_PREFIX << 32 | (u64) app_listener_id (al));
95 app_listener_id_parse (u32 listener_id, u32 * app_index,
96 u32 * app_listener_index)
98 *app_index = listener_id >> 16;
99 *app_listener_index = listener_id & 0xFFFF;
103 app_listener_handle_parse (session_handle_t handle, u32 * app_index,
104 u32 * app_listener_index)
106 app_listener_id_parse (handle & 0xFFFFFFFF, app_index, app_listener_index);
109 static app_listener_t *
110 app_listener_get_w_id (u32 listener_id)
112 u32 app_index, app_listener_index;
115 app_listener_id_parse (listener_id, &app_index, &app_listener_index);
116 app = application_get_if_valid (app_index);
119 return app_listener_get (app, app_listener_index);
123 app_listener_get_w_session (session_t * ls)
127 app = application_get_if_valid (ls->app_index);
130 return app_listener_get (app, ls->al_index);
134 app_listener_get_w_handle (session_handle_t handle)
137 if (handle >> 32 != SESSION_LISTENER_PREFIX)
140 return app_listener_get_w_id (handle & 0xFFFFFFFF);
144 app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
146 u32 table_index, fib_proto;
147 session_endpoint_t *sep;
148 session_handle_t handle;
152 sep = (session_endpoint_t *) sep_ext;
153 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
155 table_index = application_local_session_table (app);
156 handle = session_lookup_endpoint_listener (table_index, sep, 1);
157 if (handle != SESSION_INVALID_HANDLE)
159 ll = application_get_local_listener_w_handle (handle);
160 return app_listener_get_w_session ((session_t *) ll);
164 fib_proto = session_endpoint_fib_proto (sep);
165 table_index = application_session_table (app, fib_proto);
166 handle = session_lookup_endpoint_listener (table_index, sep, 1);
167 if (handle != SESSION_INVALID_HANDLE)
169 ls = listen_session_get_from_handle (handle);
170 return app_listener_get_w_session ((session_t *) ls);
177 app_listener_alloc_and_init (application_t * app,
178 session_endpoint_cfg_t * sep,
179 app_listener_t ** listener)
181 app_listener_t *app_listener;
182 local_session_t *ll = 0;
188 app_listener = app_listener_alloc (app);
189 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
192 * Add session endpoint to local session table. Only binds to "inaddr_any"
193 * (i.e., zero address) are added to local scope table.
195 if (application_has_local_scope (app)
196 && session_endpoint_is_local ((session_endpoint_t *) sep))
200 ll = application_local_listen_session_alloc (app);
201 ll->port = sep->port;
202 /* Store the original session type for the unbind */
203 ll->listener_session_type = st;
204 table_index = application_local_session_table (app);
205 lh = application_local_session_handle (ll);
206 session_lookup_add_session_endpoint (table_index,
207 (session_endpoint_t *) sep, lh);
208 app_listener->local_index = ll->session_index;
209 ll->al_index = app_listener->al_index;
212 if (application_has_global_scope (app))
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.
220 ls = listen_session_new (0, st);
221 ls->app_index = app->app_index;
222 ls->app_wrk_index = sep->app_wrk_index;
224 /* Listen pool can be reallocated if the transport is
226 lh = session_handle (ls);
228 if ((rv = session_listen (ls, sep)))
230 ls = session_get_from_handle (lh);
234 app_listener->session_index = ls->session_index;
235 ls->al_index = app_listener->al_index;
240 app_listener_free (app, app_listener);
244 *listener = app_listener;
249 app_listener_cleanup (app_listener_t * al)
251 application_t *app = application_get (al->app_index);
253 if (al->session_index != SESSION_INVALID_INDEX)
255 session_t *ls = session_get (al->session_index, 0);
256 session_stop_listen (ls);
257 listen_session_del (ls);
259 if (al->local_index != SESSION_INVALID_INDEX)
261 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
265 table_index = application_local_session_table (app);
266 ll = application_get_local_listen_session (app, al->local_index);
267 application_local_listener_session_endpoint (ll, &sep);
268 session_lookup_del_session_endpoint (table_index, &sep);
269 application_local_listen_session_free (app, ll);
271 app_listener_free (app, al);
275 app_listener_select_worker (app_listener_t * al)
280 app = application_get (al->app_index);
281 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
283 wrk_index = clib_bitmap_first_set (al->workers);
285 ASSERT (wrk_index != ~0);
286 al->accept_rotor = wrk_index;
287 return application_get_worker (app, wrk_index);
291 app_listener_get_session (app_listener_t * al)
293 if (al->session_index == SESSION_INVALID_INDEX)
296 return listen_session_get (al->session_index);
299 static app_worker_map_t *
300 app_worker_map_alloc (application_t * app)
302 app_worker_map_t *map;
303 pool_get (app->worker_maps, map);
304 clib_memset (map, 0, sizeof (*map));
309 app_worker_map_index (application_t * app, app_worker_map_t * map)
311 return (map - app->worker_maps);
315 app_worker_map_free (application_t * app, app_worker_map_t * map)
317 pool_put (app->worker_maps, map);
320 static app_worker_map_t *
321 app_worker_map_get (application_t * app, u32 map_index)
323 if (pool_is_free_index (app->worker_maps, map_index))
325 return pool_elt_at_index (app->worker_maps, map_index);
329 app_get_name (application_t * app)
335 application_session_table (application_t * app, u8 fib_proto)
337 app_namespace_t *app_ns;
338 app_ns = app_namespace_get (app->ns_index);
339 if (!application_has_global_scope (app))
340 return APP_INVALID_INDEX;
341 if (fib_proto == FIB_PROTOCOL_IP4)
342 return session_lookup_get_index_for_fib (fib_proto,
343 app_ns->ip4_fib_index);
345 return session_lookup_get_index_for_fib (fib_proto,
346 app_ns->ip6_fib_index);
350 application_local_session_table (application_t * app)
352 app_namespace_t *app_ns;
353 if (!application_has_local_scope (app))
354 return APP_INVALID_INDEX;
355 app_ns = app_namespace_get (app->ns_index);
356 return app_ns->local_table_index;
360 * Returns app name for app-index
363 application_name_from_index (u32 app_index)
365 application_t *app = application_get (app_index);
368 return app_get_name (app);
372 application_api_table_add (u32 app_index, u32 api_client_index)
374 if (api_client_index != APP_INVALID_INDEX)
375 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
379 application_api_table_del (u32 api_client_index)
381 hash_unset (app_main.app_by_api_client_index, api_client_index);
385 application_name_table_add (application_t * app)
387 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
391 application_name_table_del (application_t * app)
393 hash_unset_mem (app_main.app_by_name, app->name);
397 application_lookup (u32 api_client_index)
400 p = hash_get (app_main.app_by_api_client_index, api_client_index);
402 return application_get_if_valid (p[0]);
408 application_lookup_name (const u8 * name)
411 p = hash_get_mem (app_main.app_by_name, name);
413 return application_get (p[0]);
419 application_alloc (void)
422 pool_get (app_main.app_pool, app);
423 clib_memset (app, 0, sizeof (*app));
424 app->app_index = app - app_main.app_pool;
429 application_get (u32 app_index)
431 if (app_index == APP_INVALID_INDEX)
433 return pool_elt_at_index (app_main.app_pool, app_index);
437 application_get_if_valid (u32 app_index)
439 if (pool_is_free_index (app_main.app_pool, app_index))
442 return pool_elt_at_index (app_main.app_pool, app_index);
446 application_verify_cb_fns (session_cb_vft_t * cb_fns)
448 if (cb_fns->session_accept_callback == 0)
449 clib_warning ("No accept callback function provided");
450 if (cb_fns->session_connected_callback == 0)
451 clib_warning ("No session connected callback function provided");
452 if (cb_fns->session_disconnect_callback == 0)
453 clib_warning ("No session disconnect callback function provided");
454 if (cb_fns->session_reset_callback == 0)
455 clib_warning ("No session reset callback function provided");
459 * Check app config for given segment type
461 * Returns 1 on success and 0 otherwise
464 application_verify_cfg (ssvm_segment_type_t st)
467 if (st == SSVM_SEGMENT_MEMFD)
469 is_valid = (session_manager_get_evt_q_segment () != 0);
471 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
474 else if (st == SSVM_SEGMENT_SHM)
476 is_valid = (session_manager_get_evt_q_segment () == 0);
478 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
486 application_alloc_and_init (app_init_args_t * a)
488 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
489 segment_manager_properties_t *props;
490 vl_api_registration_t *reg;
494 app = application_alloc ();
495 options = a->options;
497 * Make sure we support the requested configuration
499 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
501 reg = vl_api_client_index_to_registration (a->api_client_index);
503 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
504 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
505 seg_type = SSVM_SEGMENT_SHM;
509 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
511 clib_warning ("mq eventfds can only be used if socket transport is "
513 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
515 seg_type = SSVM_SEGMENT_PRIVATE;
518 if (!application_verify_cfg (seg_type))
519 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
521 /* Check that the obvious things are properly set up */
522 application_verify_cb_fns (a->session_cb_vft);
524 app->flags = options[APP_OPTIONS_FLAGS];
525 app->cb_fns = *a->session_cb_vft;
526 app->ns_index = options[APP_OPTIONS_NAMESPACE];
527 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
528 app->name = vec_dup (a->name);
530 /* If no scope enabled, default to global */
531 if (!application_has_global_scope (app)
532 && !application_has_local_scope (app))
533 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
535 props = application_segment_manager_properties (app);
536 segment_manager_properties_init (props);
537 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
538 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
539 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
541 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
542 props->add_segment = 1;
544 if (options[APP_OPTIONS_RX_FIFO_SIZE])
545 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
546 if (options[APP_OPTIONS_TX_FIFO_SIZE])
547 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
548 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
549 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
550 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
551 props->use_mq_eventfd = 1;
552 if (options[APP_OPTIONS_TLS_ENGINE])
553 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
554 props->segment_type = seg_type;
556 /* Add app to lookup by api_client_index table */
557 if (!application_is_builtin (app))
558 application_api_table_add (app->app_index, a->api_client_index);
560 application_name_table_add (app);
562 a->app_index = app->app_index;
564 APP_DBG ("New app name: %v api index: %u index %u", app->name,
565 app->api_client_index, app->app_index);
571 application_free (application_t * app)
573 app_worker_map_t *wrk_map;
574 app_worker_t *app_wrk;
577 session_endpoint_t sep;
580 * The app event queue allocated in first segment is cleared with
581 * the segment manager. No need to explicitly free it.
583 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
584 app->api_client_index, app->app_index);
586 if (application_is_proxy (app))
587 application_remove_proxy (app);
594 pool_flush (wrk_map, app->worker_maps, ({
595 app_wrk = app_worker_get (wrk_map->wrk_index);
596 app_worker_free (app_wrk);
599 pool_free (app->worker_maps);
602 * Free local listeners. Global table unbinds stop local listeners
603 * as well, but if we have only local binds, these won't be cleaned up.
604 * Don't bother with local accepted sessions, we clean them when
605 * cleaning up the worker.
607 table_index = application_local_session_table (app);
609 pool_foreach (ll, app->local_listen_sessions, ({
610 application_local_listener_session_endpoint (ll, &sep);
611 session_lookup_del_session_endpoint (table_index, &sep);
614 pool_free (app->local_listen_sessions);
617 * Cleanup remaining state
619 if (application_is_builtin (app))
620 application_name_table_del (app);
621 vec_free (app->name);
622 vec_free (app->tls_cert);
623 vec_free (app->tls_key);
624 pool_put (app_main.app_pool, app);
628 application_detach_process (application_t * app, u32 api_client_index)
630 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
631 app_worker_map_t *wrk_map;
632 u32 *wrks = 0, *wrk_index;
633 app_worker_t *app_wrk;
635 if (api_client_index == ~0)
637 application_free (app);
641 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
642 app->app_index, app->api_client_index);
645 pool_foreach (wrk_map, app->worker_maps, ({
646 app_wrk = app_worker_get (wrk_map->wrk_index);
647 if (app_wrk->api_client_index == api_client_index)
648 vec_add1 (wrks, app_wrk->wrk_index);
654 clib_warning ("no workers for app %u api_index %u", app->app_index,
659 args->app_index = app->app_index;
660 args->api_client_index = api_client_index;
661 vec_foreach (wrk_index, wrks)
663 app_wrk = app_worker_get (wrk_index[0]);
664 args->wrk_map_index = app_wrk->wrk_map_index;
666 vnet_app_worker_add_del (args);
672 application_get_worker (application_t * app, u32 wrk_map_index)
674 app_worker_map_t *map;
675 map = app_worker_map_get (app, wrk_map_index);
678 return app_worker_get (map->wrk_index);
682 application_get_default_worker (application_t * app)
684 return application_get_worker (app, 0);
688 application_n_workers (application_t * app)
690 return pool_elts (app->worker_maps);
694 application_listener_select_worker (session_t * ls)
698 al = app_listener_get_w_session (ls);
699 return app_listener_select_worker (al);
703 application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
705 app_worker_map_t *wrk_map;
706 app_worker_t *app_wrk;
707 segment_manager_t *sm;
710 app_wrk = app_worker_alloc (app);
711 wrk_map = app_worker_map_alloc (app);
712 wrk_map->wrk_index = app_wrk->wrk_index;
713 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
716 * Setup first segment manager
718 sm = segment_manager_new ();
719 sm->app_wrk_index = app_wrk->wrk_index;
721 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
722 app->sm_properties.prealloc_fifos)))
724 app_worker_free (app_wrk);
727 sm->first_is_protected = 1;
732 app_wrk->first_segment_manager = segment_manager_index (sm);
733 app_wrk->listeners_table = hash_create (0, sizeof (u64));
734 app_wrk->event_queue = segment_manager_event_queue (sm);
735 app_wrk->app_is_builtin = application_is_builtin (app);
738 * Segment manager for local sessions
740 sm = segment_manager_new ();
741 sm->app_wrk_index = app_wrk->wrk_index;
742 app_wrk->local_segment_manager = segment_manager_index (sm);
743 app_wrk->local_connects = hash_create (0, sizeof (u64));
751 application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
753 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
754 app_listener_t *app_listener;
760 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
761 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
763 segment_manager_dealloc_fifos (s->rx_fifo->segment_index, s->rx_fifo,
766 app = application_get (old_wrk->app_index);
770 app_listener = app_listener_get (app, s->al_index);
772 /* Only remove from lb for now */
773 app_listener->workers = clib_bitmap_set (app_listener->workers,
774 old_wrk->wrk_map_index, 0);
776 if (app_worker_start_listen (app_wrk, app_listener))
779 s->app_wrk_index = app_wrk->wrk_index;
785 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
787 svm_fifo_segment_private_t *fs;
788 app_worker_map_t *wrk_map;
789 app_worker_t *app_wrk;
790 segment_manager_t *sm;
794 app = application_get (a->app_index);
796 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
797 "App %u does not exist", a->app_index);
801 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
802 return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv);
804 /* Map worker api index to the app */
805 app_wrk->api_client_index = a->api_client_index;
806 application_api_table_add (app->app_index, a->api_client_index);
808 sm = segment_manager_get (app_wrk->first_segment_manager);
809 fs = segment_manager_get_segment_w_lock (sm, 0);
810 a->segment = &fs->ssvm;
811 a->segment_handle = segment_manager_segment_handle (sm, fs);
812 segment_manager_segment_reader_unlock (sm);
813 a->evt_q = app_wrk->event_queue;
814 a->wrk_map_index = app_wrk->wrk_map_index;
818 wrk_map = app_worker_map_get (app, a->wrk_map_index);
820 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
821 "App %u does not have worker %u",
822 app->app_index, a->wrk_map_index);
823 app_wrk = app_worker_get (wrk_map->wrk_index);
825 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
826 "No worker %u", a->wrk_map_index);
827 application_api_table_del (app_wrk->api_client_index);
828 app_worker_free (app_wrk);
829 app_worker_map_free (app, wrk_map);
830 if (application_n_workers (app) == 0)
831 application_free (app);
837 application_is_proxy (application_t * app)
839 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
843 application_is_builtin (application_t * app)
845 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
849 application_is_builtin_proxy (application_t * app)
851 return (application_is_proxy (app) && application_is_builtin (app));
855 application_has_local_scope (application_t * app)
857 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
861 application_has_global_scope (application_t * app)
863 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
867 application_use_mq_for_ctrl (application_t * app)
869 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
872 static clib_error_t *
873 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
874 u8 transport_proto, u8 is_start)
876 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
877 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
878 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
879 transport_connection_t *tc;
880 app_worker_t *app_wrk;
885 /* TODO decide if we want proxy to be enabled for all workers */
886 app_wrk = application_get_default_worker (app);
889 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
893 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
894 sep.sw_if_index = app_ns->sw_if_index;
895 sep.transport_proto = transport_proto;
896 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
898 /* force global scope listener */
900 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
901 app_listener_alloc_and_init (app, &sep, &al);
904 app_worker_start_listen (app_wrk, al);
905 s = listen_session_get (al->session_index);
906 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
911 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
915 tc = listen_session_get_transport (s);
917 if (!ip_is_zero (&tc->lcl_ip, 1))
921 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
922 sep.transport_proto = transport_proto;
924 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
926 session_lookup_add_session_endpoint (sti,
927 (session_endpoint_t *) & sep,
930 session_lookup_del_session_endpoint (sti,
931 (session_endpoint_t *) & sep);
938 application_start_stop_proxy_local_scope (application_t * app,
939 u8 transport_proto, u8 is_start)
941 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
942 app_namespace_t *app_ns;
943 app_ns = app_namespace_get (app->ns_index);
945 sep.transport_proto = transport_proto;
950 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
953 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
958 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
960 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
965 application_start_stop_proxy (application_t * app,
966 transport_proto_t transport_proto, u8 is_start)
968 if (application_has_local_scope (app))
969 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
971 if (application_has_global_scope (app))
973 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
974 transport_proto, is_start);
975 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
976 transport_proto, is_start);
981 application_setup_proxy (application_t * app)
983 u16 transports = app->proxied_transports;
984 transport_proto_t tp;
986 ASSERT (application_is_proxy (app));
989 transport_proto_foreach (tp, ({
990 if (transports & (1 << tp))
991 application_start_stop_proxy (app, tp, 1);
997 application_remove_proxy (application_t * app)
999 u16 transports = app->proxied_transports;
1000 transport_proto_t tp;
1002 ASSERT (application_is_proxy (app));
1005 transport_proto_foreach (tp, ({
1006 if (transports & (1 << tp))
1007 application_start_stop_proxy (app, tp, 0);
1012 segment_manager_properties_t *
1013 application_segment_manager_properties (application_t * app)
1015 return &app->sm_properties;
1018 segment_manager_properties_t *
1019 application_get_segment_manager_properties (u32 app_index)
1021 application_t *app = application_get (app_index);
1022 return &app->sm_properties;
1026 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1029 app = application_get (a->app_index);
1031 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1032 0, "app %u doesn't exist", a->app_index);
1033 app->tls_cert = vec_dup (a->cert);
1038 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1041 app = application_get (a->app_index);
1043 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1044 0, "app %u doesn't exist", a->app_index);
1045 app->tls_key = vec_dup (a->key);
1050 application_format_listeners (application_t * app, int verbose)
1052 vlib_main_t *vm = vlib_get_main ();
1053 app_worker_map_t *wrk_map;
1054 app_worker_t *app_wrk;
1060 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1066 pool_foreach (wrk_map, app->worker_maps, ({
1067 app_wrk = app_worker_get (wrk_map->wrk_index);
1068 if (hash_elts (app_wrk->listeners_table) == 0)
1070 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1071 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1072 handle, sm_index, verbose);
1079 application_format_connects (application_t * app, int verbose)
1081 app_worker_map_t *wrk_map;
1082 app_worker_t *app_wrk;
1086 app_worker_format_connects (0, verbose);
1091 pool_foreach (wrk_map, app->worker_maps, ({
1092 app_wrk = app_worker_get (wrk_map->wrk_index);
1093 app_worker_format_connects (app_wrk, verbose);
1099 application_format_local_sessions (application_t * app, int verbose)
1101 vlib_main_t *vm = vlib_get_main ();
1102 app_worker_map_t *wrk_map;
1103 app_worker_t *app_wrk;
1104 transport_proto_t tp;
1105 local_session_t *ls;
1110 app_worker_format_local_sessions (0, verbose);
1115 * Format local listeners
1119 pool_foreach (ls, app->local_listen_sessions, ({
1120 tp = session_type_transport_proto (ls->listener_session_type);
1121 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1123 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
1124 vec_reset_length (conn);
1129 * Format local accepted/connected sessions
1132 pool_foreach (wrk_map, app->worker_maps, ({
1133 app_wrk = app_worker_get (wrk_map->wrk_index);
1134 app_worker_format_local_sessions (app_wrk, verbose);
1140 application_format_local_connects (application_t * app, int verbose)
1142 app_worker_map_t *wrk_map;
1143 app_worker_t *app_wrk;
1147 app_worker_format_local_connects (0, verbose);
1152 pool_foreach (wrk_map, app->worker_maps, ({
1153 app_wrk = app_worker_get (wrk_map->wrk_index);
1154 app_worker_format_local_connects (app_wrk, verbose);
1160 format_application (u8 * s, va_list * args)
1162 application_t *app = va_arg (*args, application_t *);
1163 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1164 segment_manager_properties_t *props;
1165 const u8 *app_ns_name, *app_name;
1166 app_worker_map_t *wrk_map;
1167 app_worker_t *app_wrk;
1172 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
1176 app_name = app_get_name (app);
1177 app_ns_name = app_namespace_id_from_index (app->ns_index);
1178 props = application_segment_manager_properties (app);
1181 s = format (s, "%-10u%-20s%-40s", app->app_index, app_name,
1186 s = format (s, "app-name %s app-index %u ns-index %u seg-size %U\n",
1187 app_name, app->app_index, app->ns_index,
1188 format_memory_size, props->add_segment_size);
1189 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1190 format_memory_size, props->rx_fifo_size,
1191 format_memory_size, props->tx_fifo_size);
1194 pool_foreach (wrk_map, app->worker_maps, ({
1195 app_wrk = app_worker_get (wrk_map->wrk_index);
1196 s = format (s, "%U", format_app_worker, app_wrk);
1204 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1208 if (!pool_elts (app_main.app_pool))
1210 vlib_cli_output (vm, "No active server bindings");
1216 application_format_local_sessions (0, verbose);
1218 pool_foreach (app, app_main.app_pool, ({
1219 application_format_local_sessions (app, verbose);
1225 application_format_listeners (0, verbose);
1228 pool_foreach (app, app_main.app_pool, ({
1229 application_format_listeners (app, verbose);
1236 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1240 if (!pool_elts (app_main.app_pool))
1242 vlib_cli_output (vm, "No active apps");
1248 application_format_local_connects (0, verbose);
1251 pool_foreach (app, app_main.app_pool, ({
1252 application_format_local_connects (app, verbose);
1258 application_format_connects (0, verbose);
1261 pool_foreach (app, app_main.app_pool, ({
1262 application_format_connects (app, verbose);
1268 static clib_error_t *
1269 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1270 vlib_cli_command_t * cmd)
1272 int do_server = 0, do_client = 0, do_local = 0;
1277 session_cli_return_if_not_enabled ();
1279 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1281 if (unformat (input, "server"))
1283 else if (unformat (input, "client"))
1285 else if (unformat (input, "local"))
1287 else if (unformat (input, "%u", &app_index))
1289 else if (unformat (input, "verbose"))
1292 return clib_error_return (0, "unknown input `%U'",
1293 format_unformat_error, input);
1298 application_format_all_listeners (vm, do_local, verbose);
1304 application_format_all_clients (vm, do_local, verbose);
1308 if (app_index != ~0)
1310 app = application_get_if_valid (app_index);
1312 return clib_error_return (0, "No app with index %u", app_index);
1314 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
1318 /* Print app related info */
1319 if (!do_server && !do_client)
1321 vlib_cli_output (vm, "%U", format_application, 0, 0);
1323 pool_foreach (app, app_main.app_pool, ({
1324 vlib_cli_output (vm, "%U", format_application, app, 0);
1333 VLIB_CLI_COMMAND (show_app_command, static) =
1336 .short_help = "show app [server|client] [verbose]",
1337 .function = show_app_command_fn,
1342 * fd.io coding-style-patch-verification: ON
1345 * eval: (c-set-style "gnu")