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;
23 #define app_interface_check_thread_and_barrier(_fn, _arg) \
24 if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \
26 vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \
31 application_local_listener_session_endpoint (local_session_t * ll,
32 session_endpoint_t * sep)
34 sep->transport_proto =
35 session_type_transport_proto (ll->listener_session_type);
37 sep->is_ip4 = ll->listener_session_type & 1;
40 static app_listener_t *
41 app_listener_alloc (application_t * app)
43 app_listener_t *app_listener;
44 pool_get (app->listeners, app_listener);
45 clib_memset (app_listener, 0, sizeof (*app_listener));
46 app_listener->al_index = app_listener - app->listeners;
47 app_listener->app_index = app->app_index;
48 app_listener->session_index = SESSION_INVALID_INDEX;
49 app_listener->local_index = SESSION_INVALID_INDEX;
54 app_listener_get (application_t * app, u32 app_listener_index)
56 return pool_elt_at_index (app->listeners, app_listener_index);
60 app_listener_free (application_t * app, app_listener_t * app_listener)
62 clib_bitmap_free (app_listener->workers);
63 pool_put (app->listeners, app_listener);
65 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
69 application_local_listen_session_alloc (application_t * app)
72 pool_get_zero (app->local_listen_sessions, ll);
73 ll->session_index = ll - app->local_listen_sessions;
74 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
75 ll->app_index = app->app_index;
76 ll->session_state = SESSION_STATE_LISTENING;
81 application_local_listen_session_free (application_t * app,
84 pool_put (app->local_listen_sessions, ll);
86 clib_memset (ll, 0xfb, sizeof (*ll));
90 app_listener_id (app_listener_t * al)
92 ASSERT (al->app_index < 1 << 16 && al->al_index < 1 << 16);
93 return (al->app_index << 16 | al->al_index);
97 app_listener_handle (app_listener_t * al)
99 return ((u64) SESSION_LISTENER_PREFIX << 32 | (u64) app_listener_id (al));
103 app_listener_id_parse (u32 listener_id, u32 * app_index,
104 u32 * app_listener_index)
106 *app_index = listener_id >> 16;
107 *app_listener_index = listener_id & 0xFFFF;
111 app_listener_handle_parse (session_handle_t handle, u32 * app_index,
112 u32 * app_listener_index)
114 app_listener_id_parse (handle & 0xFFFFFFFF, app_index, app_listener_index);
117 static app_listener_t *
118 app_listener_get_w_id (u32 listener_id)
120 u32 app_index, app_listener_index;
123 app_listener_id_parse (listener_id, &app_index, &app_listener_index);
124 app = application_get_if_valid (app_index);
127 return app_listener_get (app, app_listener_index);
131 app_listener_get_w_session (session_t * ls)
135 app = application_get_if_valid (ls->app_index);
138 return app_listener_get (app, ls->al_index);
142 app_listener_get_w_handle (session_handle_t handle)
145 if (handle >> 32 != SESSION_LISTENER_PREFIX)
148 return app_listener_get_w_id (handle & 0xFFFFFFFF);
152 app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
154 u32 table_index, fib_proto;
155 session_endpoint_t *sep;
156 session_handle_t handle;
160 sep = (session_endpoint_t *) sep_ext;
161 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
163 table_index = application_local_session_table (app);
164 handle = session_lookup_endpoint_listener (table_index, sep, 1);
165 if (handle != SESSION_INVALID_HANDLE)
167 ll = application_get_local_listener_w_handle (handle);
168 return app_listener_get_w_session ((session_t *) ll);
172 fib_proto = session_endpoint_fib_proto (sep);
173 table_index = application_session_table (app, fib_proto);
174 handle = session_lookup_endpoint_listener (table_index, sep, 1);
175 if (handle != SESSION_INVALID_HANDLE)
177 ls = listen_session_get_from_handle (handle);
178 return app_listener_get_w_session ((session_t *) ls);
185 app_listener_alloc_and_init (application_t * app,
186 session_endpoint_cfg_t * sep,
187 app_listener_t ** listener)
189 app_listener_t *app_listener;
190 local_session_t *ll = 0;
196 app_listener = app_listener_alloc (app);
197 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
200 * Add session endpoint to local session table. Only binds to "inaddr_any"
201 * (i.e., zero address) are added to local scope table.
203 if (application_has_local_scope (app)
204 && session_endpoint_is_local ((session_endpoint_t *) sep))
208 ll = application_local_listen_session_alloc (app);
209 ll->port = sep->port;
210 /* Store the original session type for the unbind */
211 ll->listener_session_type = st;
212 table_index = application_local_session_table (app);
213 lh = application_local_session_handle (ll);
214 session_lookup_add_session_endpoint (table_index,
215 (session_endpoint_t *) sep, lh);
216 app_listener->local_index = ll->session_index;
217 ll->al_index = app_listener->al_index;
220 if (application_has_global_scope (app))
223 * Start listening on local endpoint for requested transport and scope.
224 * Creates a stream session with state LISTENING to be used in session
225 * lookups, prior to establishing connection. Requests transport to
226 * build it's own specific listening connection.
228 ls = listen_session_new (0, st);
229 ls->app_index = app->app_index;
230 ls->app_wrk_index = sep->app_wrk_index;
232 /* Listen pool can be reallocated if the transport is
234 lh = session_handle (ls);
236 if ((rv = session_listen (ls, sep)))
238 ls = session_get_from_handle (lh);
242 app_listener->session_index = ls->session_index;
243 ls->al_index = app_listener->al_index;
248 app_listener_free (app, app_listener);
252 *listener = app_listener;
257 app_listener_cleanup (app_listener_t * al)
259 application_t *app = application_get (al->app_index);
261 if (al->session_index != SESSION_INVALID_INDEX)
263 session_t *ls = session_get (al->session_index, 0);
264 session_stop_listen (ls);
265 listen_session_del (ls);
267 if (al->local_index != SESSION_INVALID_INDEX)
269 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
273 table_index = application_local_session_table (app);
274 ll = application_get_local_listen_session (app, al->local_index);
275 application_local_listener_session_endpoint (ll, &sep);
276 session_lookup_del_session_endpoint (table_index, &sep);
277 application_local_listen_session_free (app, ll);
279 app_listener_free (app, al);
283 app_listener_select_worker (app_listener_t * al)
288 app = application_get (al->app_index);
289 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
291 wrk_index = clib_bitmap_first_set (al->workers);
293 ASSERT (wrk_index != ~0);
294 al->accept_rotor = wrk_index;
295 return application_get_worker (app, wrk_index);
299 app_listener_get_session (app_listener_t * al)
301 if (al->session_index == SESSION_INVALID_INDEX)
304 return listen_session_get (al->session_index);
307 static app_worker_map_t *
308 app_worker_map_alloc (application_t * app)
310 app_worker_map_t *map;
311 pool_get (app->worker_maps, map);
312 clib_memset (map, 0, sizeof (*map));
317 app_worker_map_index (application_t * app, app_worker_map_t * map)
319 return (map - app->worker_maps);
323 app_worker_map_free (application_t * app, app_worker_map_t * map)
325 pool_put (app->worker_maps, map);
328 static app_worker_map_t *
329 app_worker_map_get (application_t * app, u32 map_index)
331 if (pool_is_free_index (app->worker_maps, map_index))
333 return pool_elt_at_index (app->worker_maps, map_index);
337 app_get_name (application_t * app)
343 application_session_table (application_t * app, u8 fib_proto)
345 app_namespace_t *app_ns;
346 app_ns = app_namespace_get (app->ns_index);
347 if (!application_has_global_scope (app))
348 return APP_INVALID_INDEX;
349 if (fib_proto == FIB_PROTOCOL_IP4)
350 return session_lookup_get_index_for_fib (fib_proto,
351 app_ns->ip4_fib_index);
353 return session_lookup_get_index_for_fib (fib_proto,
354 app_ns->ip6_fib_index);
358 application_local_session_table (application_t * app)
360 app_namespace_t *app_ns;
361 if (!application_has_local_scope (app))
362 return APP_INVALID_INDEX;
363 app_ns = app_namespace_get (app->ns_index);
364 return app_ns->local_table_index;
368 * Returns app name for app-index
371 application_name_from_index (u32 app_index)
373 application_t *app = application_get (app_index);
376 return app_get_name (app);
380 application_api_table_add (u32 app_index, u32 api_client_index)
382 if (api_client_index != APP_INVALID_INDEX)
383 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
387 application_api_table_del (u32 api_client_index)
389 hash_unset (app_main.app_by_api_client_index, api_client_index);
393 application_name_table_add (application_t * app)
395 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
399 application_name_table_del (application_t * app)
401 hash_unset_mem (app_main.app_by_name, app->name);
405 application_lookup (u32 api_client_index)
408 p = hash_get (app_main.app_by_api_client_index, api_client_index);
410 return application_get_if_valid (p[0]);
416 application_lookup_name (const u8 * name)
419 p = hash_get_mem (app_main.app_by_name, name);
421 return application_get (p[0]);
426 static application_t *
427 application_alloc (void)
430 pool_get (app_main.app_pool, app);
431 clib_memset (app, 0, sizeof (*app));
432 app->app_index = app - app_main.app_pool;
437 application_get (u32 app_index)
439 if (app_index == APP_INVALID_INDEX)
441 return pool_elt_at_index (app_main.app_pool, app_index);
445 application_get_if_valid (u32 app_index)
447 if (pool_is_free_index (app_main.app_pool, app_index))
450 return pool_elt_at_index (app_main.app_pool, app_index);
454 application_verify_cb_fns (session_cb_vft_t * cb_fns)
456 if (cb_fns->session_accept_callback == 0)
457 clib_warning ("No accept callback function provided");
458 if (cb_fns->session_connected_callback == 0)
459 clib_warning ("No session connected callback function provided");
460 if (cb_fns->session_disconnect_callback == 0)
461 clib_warning ("No session disconnect callback function provided");
462 if (cb_fns->session_reset_callback == 0)
463 clib_warning ("No session reset callback function provided");
467 * Check app config for given segment type
469 * Returns 1 on success and 0 otherwise
472 application_verify_cfg (ssvm_segment_type_t st)
475 if (st == SSVM_SEGMENT_MEMFD)
477 is_valid = (session_manager_get_evt_q_segment () != 0);
479 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
482 else if (st == SSVM_SEGMENT_SHM)
484 is_valid = (session_manager_get_evt_q_segment () == 0);
486 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
494 application_alloc_and_init (app_init_args_t * a)
496 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
497 segment_manager_properties_t *props;
498 vl_api_registration_t *reg;
502 app = application_alloc ();
503 options = a->options;
505 * Make sure we support the requested configuration
507 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
509 reg = vl_api_client_index_to_registration (a->api_client_index);
511 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
512 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
513 seg_type = SSVM_SEGMENT_SHM;
517 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
519 clib_warning ("mq eventfds can only be used if socket transport is "
521 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
523 seg_type = SSVM_SEGMENT_PRIVATE;
526 if (!application_verify_cfg (seg_type))
527 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
529 /* Check that the obvious things are properly set up */
530 application_verify_cb_fns (a->session_cb_vft);
532 app->flags = options[APP_OPTIONS_FLAGS];
533 app->cb_fns = *a->session_cb_vft;
534 app->ns_index = options[APP_OPTIONS_NAMESPACE];
535 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
536 app->name = vec_dup (a->name);
538 /* If no scope enabled, default to global */
539 if (!application_has_global_scope (app)
540 && !application_has_local_scope (app))
541 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
543 props = application_segment_manager_properties (app);
544 segment_manager_properties_init (props);
545 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
546 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
547 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
549 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
550 props->add_segment = 1;
552 if (options[APP_OPTIONS_RX_FIFO_SIZE])
553 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
554 if (options[APP_OPTIONS_TX_FIFO_SIZE])
555 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
556 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
557 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
558 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
559 props->use_mq_eventfd = 1;
560 if (options[APP_OPTIONS_TLS_ENGINE])
561 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
562 props->segment_type = seg_type;
564 /* Add app to lookup by api_client_index table */
565 if (!application_is_builtin (app))
566 application_api_table_add (app->app_index, a->api_client_index);
568 application_name_table_add (app);
570 a->app_index = app->app_index;
572 APP_DBG ("New app name: %v api index: %u index %u", app->name,
573 app->api_client_index, app->app_index);
579 application_free (application_t * app)
581 app_worker_map_t *wrk_map;
582 app_worker_t *app_wrk;
585 session_endpoint_t sep;
588 * The app event queue allocated in first segment is cleared with
589 * the segment manager. No need to explicitly free it.
591 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
592 app->api_client_index, app->app_index);
594 if (application_is_proxy (app))
595 application_remove_proxy (app);
602 pool_flush (wrk_map, app->worker_maps, ({
603 app_wrk = app_worker_get (wrk_map->wrk_index);
604 app_worker_free (app_wrk);
607 pool_free (app->worker_maps);
610 * Free local listeners. Global table unbinds stop local listeners
611 * as well, but if we have only local binds, these won't be cleaned up.
612 * Don't bother with local accepted sessions, we clean them when
613 * cleaning up the worker.
615 table_index = application_local_session_table (app);
617 pool_foreach (ll, app->local_listen_sessions, ({
618 application_local_listener_session_endpoint (ll, &sep);
619 session_lookup_del_session_endpoint (table_index, &sep);
622 pool_free (app->local_listen_sessions);
625 * Cleanup remaining state
627 if (application_is_builtin (app))
628 application_name_table_del (app);
629 vec_free (app->name);
630 vec_free (app->tls_cert);
631 vec_free (app->tls_key);
632 pool_put (app_main.app_pool, app);
636 application_detach_process (application_t * app, u32 api_client_index)
638 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
639 app_worker_map_t *wrk_map;
640 u32 *wrks = 0, *wrk_index;
641 app_worker_t *app_wrk;
643 if (api_client_index == ~0)
645 application_free (app);
649 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
650 app->app_index, app->api_client_index);
653 pool_foreach (wrk_map, app->worker_maps, ({
654 app_wrk = app_worker_get (wrk_map->wrk_index);
655 if (app_wrk->api_client_index == api_client_index)
656 vec_add1 (wrks, app_wrk->wrk_index);
662 clib_warning ("no workers for app %u api_index %u", app->app_index,
667 args->app_index = app->app_index;
668 args->api_client_index = api_client_index;
669 vec_foreach (wrk_index, wrks)
671 app_wrk = app_worker_get (wrk_index[0]);
672 args->wrk_map_index = app_wrk->wrk_map_index;
674 vnet_app_worker_add_del (args);
680 application_get_worker (application_t * app, u32 wrk_map_index)
682 app_worker_map_t *map;
683 map = app_worker_map_get (app, wrk_map_index);
686 return app_worker_get (map->wrk_index);
690 application_get_default_worker (application_t * app)
692 return application_get_worker (app, 0);
696 application_n_workers (application_t * app)
698 return pool_elts (app->worker_maps);
702 application_listener_select_worker (session_t * ls)
706 al = app_listener_get_w_session (ls);
707 return app_listener_select_worker (al);
711 application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
713 app_worker_map_t *wrk_map;
714 app_worker_t *app_wrk;
715 segment_manager_t *sm;
718 app_wrk = app_worker_alloc (app);
719 wrk_map = app_worker_map_alloc (app);
720 wrk_map->wrk_index = app_wrk->wrk_index;
721 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
724 * Setup first segment manager
726 sm = segment_manager_new ();
727 sm->app_wrk_index = app_wrk->wrk_index;
729 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
730 app->sm_properties.prealloc_fifos)))
732 app_worker_free (app_wrk);
735 sm->first_is_protected = 1;
740 app_wrk->first_segment_manager = segment_manager_index (sm);
741 app_wrk->listeners_table = hash_create (0, sizeof (u64));
742 app_wrk->event_queue = segment_manager_event_queue (sm);
743 app_wrk->app_is_builtin = application_is_builtin (app);
746 * Segment manager for local sessions
748 sm = segment_manager_new ();
749 sm->app_wrk_index = app_wrk->wrk_index;
750 app_wrk->local_segment_manager = segment_manager_index (sm);
751 app_wrk->local_connects = hash_create (0, sizeof (u64));
759 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
761 svm_fifo_segment_private_t *fs;
762 app_worker_map_t *wrk_map;
763 app_worker_t *app_wrk;
764 segment_manager_t *sm;
768 app = application_get (a->app_index);
770 return VNET_API_ERROR_INVALID_VALUE;
774 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
777 /* Map worker api index to the app */
778 app_wrk->api_client_index = a->api_client_index;
779 application_api_table_add (app->app_index, a->api_client_index);
781 sm = segment_manager_get (app_wrk->first_segment_manager);
782 fs = segment_manager_get_segment_w_lock (sm, 0);
783 a->segment = &fs->ssvm;
784 a->segment_handle = segment_manager_segment_handle (sm, fs);
785 segment_manager_segment_reader_unlock (sm);
786 a->evt_q = app_wrk->event_queue;
787 a->wrk_map_index = app_wrk->wrk_map_index;
791 wrk_map = app_worker_map_get (app, a->wrk_map_index);
793 return VNET_API_ERROR_INVALID_VALUE;
795 app_wrk = app_worker_get (wrk_map->wrk_index);
797 return VNET_API_ERROR_INVALID_VALUE;
799 application_api_table_del (app_wrk->api_client_index);
800 app_worker_free (app_wrk);
801 app_worker_map_free (app, wrk_map);
802 if (application_n_workers (app) == 0)
803 application_free (app);
809 app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
811 app_namespace_t *app_ns;
812 if (vec_len (namespace_id) == 0)
814 /* Use default namespace */
819 *app_ns_index = app_namespace_index_from_id (namespace_id);
820 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
821 return VNET_API_ERROR_APP_INVALID_NS;
822 app_ns = app_namespace_get (*app_ns_index);
824 return VNET_API_ERROR_APP_INVALID_NS;
825 if (app_ns->ns_secret != secret)
826 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
831 app_name_from_api_index (u32 api_client_index)
833 vl_api_registration_t *regp;
834 regp = vl_api_client_index_to_registration (api_client_index);
836 return format (0, "%s%c", regp->name, 0);
838 clib_warning ("api client index %u does not have an api registration!",
840 return format (0, "unknown%c", 0);
844 * Attach application to vpp
846 * Allocates a vpp app, i.e., a structure that keeps back pointers
847 * to external app and a segment manager for shared memory fifo based
848 * communication with the external app.
851 vnet_application_attach (vnet_app_attach_args_t * a)
853 svm_fifo_segment_private_t *fs;
854 application_t *app = 0;
855 app_worker_t *app_wrk;
856 segment_manager_t *sm;
857 u32 app_ns_index = 0;
862 if (a->api_client_index != APP_INVALID_INDEX)
863 app = application_lookup (a->api_client_index);
865 app = application_lookup_name (a->name);
867 return VNET_API_ERROR_INVALID_VALUE;
870 return VNET_API_ERROR_APP_ALREADY_ATTACHED;
872 if (a->api_client_index != APP_INVALID_INDEX)
874 app_name = app_name_from_api_index (a->api_client_index);
878 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
879 if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
881 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
883 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
886 app = application_get (a->app_index);
887 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
890 a->app_evt_q = app_wrk->event_queue;
891 app_wrk->api_client_index = a->api_client_index;
892 sm = segment_manager_get (app_wrk->first_segment_manager);
893 fs = segment_manager_get_segment_w_lock (sm, 0);
895 if (application_is_proxy (app))
896 application_setup_proxy (app);
898 ASSERT (vec_len (fs->ssvm.name) <= 128);
899 a->segment = &fs->ssvm;
900 a->segment_handle = segment_manager_segment_handle (sm, fs);
902 segment_manager_segment_reader_unlock (sm);
908 * Detach application from vpp
911 vnet_application_detach (vnet_app_detach_args_t * a)
915 app = application_get_if_valid (a->app_index);
918 clib_warning ("app not attached");
919 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
922 app_interface_check_thread_and_barrier (vnet_application_detach, a);
923 application_detach_process (app, a->api_client_index);
929 session_endpoint_in_ns (session_endpoint_t * sep)
931 u8 is_lep = session_endpoint_is_local (sep);
932 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
933 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
935 clib_warning ("sw_if_index %u not configured with ip %U",
936 sep->sw_if_index, format_ip46_address, &sep->ip,
940 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
944 session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
945 application_t * app, u8 is_connect)
947 app_namespace_t *app_ns;
948 u32 ns_index, fib_index;
950 ns_index = app->ns_index;
952 /* App is a transport proto, so fetch the calling app's ns */
953 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
955 app_worker_t *owner_wrk;
956 application_t *owner_app;
958 owner_wrk = app_worker_get (sep->app_wrk_index);
959 owner_app = application_get (owner_wrk->app_index);
960 ns_index = owner_app->ns_index;
962 app_ns = app_namespace_get (ns_index);
966 /* Ask transport and network to bind to/connect using local interface
967 * that "supports" app's namespace. This will fix our local connection
971 /* If in default namespace and user requested a fib index use it */
972 if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
973 fib_index = sep->fib_index;
975 fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
976 sep->peer.fib_index = fib_index;
977 sep->fib_index = fib_index;
981 sep->sw_if_index = app_ns->sw_if_index;
985 if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
986 && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
987 && sep->peer.sw_if_index != app_ns->sw_if_index)
988 clib_warning ("Local sw_if_index different from app ns sw_if_index");
990 sep->peer.sw_if_index = app_ns->sw_if_index;
995 vnet_listen (vnet_listen_args_t * a)
997 app_listener_t *app_listener;
998 app_worker_t *app_wrk;
1002 app = application_get_if_valid (a->app_index);
1004 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1006 app_wrk = application_get_worker (app, a->wrk_map_index);
1008 return VNET_API_ERROR_INVALID_VALUE;
1010 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
1012 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
1013 if (!session_endpoint_in_ns (&a->sep))
1014 return VNET_API_ERROR_INVALID_VALUE_2;
1017 * Check if we already have an app listener
1019 app_listener = app_listener_lookup (app, &a->sep_ext);
1022 if (app_listener->app_index != app->app_index)
1023 return VNET_API_ERROR_ADDRESS_IN_USE;
1024 if (app_worker_start_listen (app_wrk, app_listener))
1026 a->handle = app_listener_handle (app_listener);
1031 * Create new app listener
1033 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1036 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1038 app_listener_cleanup (app_listener);
1042 a->handle = app_listener_handle (app_listener);
1047 vnet_connect (vnet_connect_args_t * a)
1049 app_worker_t *server_wrk, *client_wrk;
1050 application_t *client;
1051 local_session_t *ll;
1058 if (session_endpoint_is_zero (&a->sep))
1059 return VNET_API_ERROR_INVALID_VALUE;
1061 client = application_get (a->app_index);
1062 session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1063 client_wrk = application_get_worker (client, a->wrk_map_index);
1066 * First check the local scope for locally attached destinations.
1067 * If we have local scope, we pass *all* connects through it since we may
1068 * have special policy rules even for non-local destinations, think proxy.
1070 if (application_has_local_scope (client))
1072 table_index = application_local_session_table (client);
1073 lh = session_lookup_local_endpoint (table_index, &a->sep);
1074 if (lh == SESSION_DROP_HANDLE)
1075 return VNET_API_ERROR_APP_CONNECT_FILTERED;
1077 if (lh == SESSION_INVALID_HANDLE)
1080 ll = application_get_local_listener_w_handle (lh);
1081 al = app_listener_get_w_session ((session_t *) ll);
1084 * Break loop if rule in local table points to connecting app. This
1085 * can happen if client is a generic proxy. Route connect through
1086 * global table instead.
1088 if (al->app_index == a->app_index)
1091 server_wrk = app_listener_select_worker (al);
1092 return app_worker_local_session_connect (client_wrk, server_wrk, ll,
1097 * If nothing found, check the global scope for locally attached
1098 * destinations. Make sure first that we're allowed to.
1102 if (session_endpoint_is_local (&a->sep))
1103 return VNET_API_ERROR_SESSION_CONNECT;
1105 if (!application_has_global_scope (client))
1106 return VNET_API_ERROR_APP_CONNECT_SCOPE;
1108 fib_proto = session_endpoint_fib_proto (&a->sep);
1109 table_index = application_session_table (client, fib_proto);
1110 ls = session_lookup_listener (table_index, &a->sep);
1113 al = app_listener_get_w_session (ls);
1114 server_wrk = app_listener_select_worker (al);
1115 ll = (local_session_t *) ls;
1116 return app_worker_local_session_connect (client_wrk, server_wrk, ll,
1121 * Not connecting to a local server, propagate to transport
1123 if (app_worker_connect_session (client_wrk, &a->sep, a->api_context))
1124 return VNET_API_ERROR_SESSION_CONNECT;
1129 vnet_unlisten (vnet_unlisten_args_t * a)
1131 app_worker_t *app_wrk;
1135 if (!(app = application_get_if_valid (a->app_index)))
1136 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1138 al = app_listener_get_w_handle (a->handle);
1139 if (al->app_index != app->app_index)
1141 clib_warning ("app doesn't own handle %llu!", a->handle);
1145 app_wrk = application_get_worker (app, a->wrk_map_index);
1148 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
1152 return app_worker_stop_listen (app_wrk, al);
1156 vnet_disconnect_session (vnet_disconnect_args_t * a)
1158 if (session_handle_is_local (a->handle))
1160 local_session_t *ls;
1162 /* Disconnect reply came to worker 1 not main thread */
1163 app_interface_check_thread_and_barrier (vnet_disconnect_session, a);
1165 if (!(ls = app_worker_get_local_session_from_handle (a->handle)))
1168 return app_worker_local_session_disconnect (a->app_index, ls);
1172 app_worker_t *app_wrk;
1175 s = session_get_from_handle_if_valid (a->handle);
1177 return VNET_API_ERROR_INVALID_VALUE;
1178 app_wrk = app_worker_get (s->app_wrk_index);
1179 if (app_wrk->app_index != a->app_index)
1180 return VNET_API_ERROR_INVALID_VALUE;
1182 /* We're peeking into another's thread pool. Make sure */
1183 ASSERT (s->session_index == session_index_from_handle (a->handle));
1191 application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1193 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1194 app_listener_t *app_listener;
1200 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1201 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1203 segment_manager_dealloc_fifos (s->rx_fifo->segment_index, s->rx_fifo,
1206 app = application_get (old_wrk->app_index);
1210 app_listener = app_listener_get (app, s->al_index);
1212 /* Only remove from lb for now */
1213 app_listener->workers = clib_bitmap_set (app_listener->workers,
1214 old_wrk->wrk_map_index, 0);
1216 if (app_worker_start_listen (app_wrk, app_listener))
1219 s->app_wrk_index = app_wrk->wrk_index;
1225 application_is_proxy (application_t * app)
1227 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1231 application_is_builtin (application_t * app)
1233 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1237 application_is_builtin_proxy (application_t * app)
1239 return (application_is_proxy (app) && application_is_builtin (app));
1243 application_has_local_scope (application_t * app)
1245 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1249 application_has_global_scope (application_t * app)
1251 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1255 application_use_mq_for_ctrl (application_t * app)
1257 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
1260 static clib_error_t *
1261 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1262 u8 transport_proto, u8 is_start)
1264 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1265 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1266 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
1267 transport_connection_t *tc;
1268 app_worker_t *app_wrk;
1273 /* TODO decide if we want proxy to be enabled for all workers */
1274 app_wrk = application_get_default_worker (app);
1277 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1280 sep.is_ip4 = is_ip4;
1281 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1282 sep.sw_if_index = app_ns->sw_if_index;
1283 sep.transport_proto = transport_proto;
1284 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1286 /* force global scope listener */
1288 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1289 app_listener_alloc_and_init (app, &sep, &al);
1292 app_worker_start_listen (app_wrk, al);
1293 s = listen_session_get (al->session_index);
1294 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
1299 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
1303 tc = listen_session_get_transport (s);
1305 if (!ip_is_zero (&tc->lcl_ip, 1))
1308 sep.is_ip4 = is_ip4;
1309 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1310 sep.transport_proto = transport_proto;
1312 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1314 session_lookup_add_session_endpoint (sti,
1315 (session_endpoint_t *) & sep,
1318 session_lookup_del_session_endpoint (sti,
1319 (session_endpoint_t *) & sep);
1326 application_start_stop_proxy_local_scope (application_t * app,
1327 u8 transport_proto, u8 is_start)
1329 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1330 app_namespace_t *app_ns;
1331 app_ns = app_namespace_get (app->ns_index);
1333 sep.transport_proto = transport_proto;
1338 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1341 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1346 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1348 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1353 application_start_stop_proxy (application_t * app,
1354 transport_proto_t transport_proto, u8 is_start)
1356 if (application_has_local_scope (app))
1357 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1359 if (application_has_global_scope (app))
1361 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1362 transport_proto, is_start);
1363 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1364 transport_proto, is_start);
1369 application_setup_proxy (application_t * app)
1371 u16 transports = app->proxied_transports;
1372 transport_proto_t tp;
1374 ASSERT (application_is_proxy (app));
1377 transport_proto_foreach (tp, ({
1378 if (transports & (1 << tp))
1379 application_start_stop_proxy (app, tp, 1);
1385 application_remove_proxy (application_t * app)
1387 u16 transports = app->proxied_transports;
1388 transport_proto_t tp;
1390 ASSERT (application_is_proxy (app));
1393 transport_proto_foreach (tp, ({
1394 if (transports & (1 << tp))
1395 application_start_stop_proxy (app, tp, 0);
1400 segment_manager_properties_t *
1401 application_segment_manager_properties (application_t * app)
1403 return &app->sm_properties;
1406 segment_manager_properties_t *
1407 application_get_segment_manager_properties (u32 app_index)
1409 application_t *app = application_get (app_index);
1410 return &app->sm_properties;
1414 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1417 app = application_get (a->app_index);
1419 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1420 0, "app %u doesn't exist", a->app_index);
1421 app->tls_cert = vec_dup (a->cert);
1426 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1429 app = application_get (a->app_index);
1431 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1432 0, "app %u doesn't exist", a->app_index);
1433 app->tls_key = vec_dup (a->key);
1438 application_format_listeners (application_t * app, int verbose)
1440 vlib_main_t *vm = vlib_get_main ();
1441 app_worker_map_t *wrk_map;
1442 app_worker_t *app_wrk;
1448 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1454 pool_foreach (wrk_map, app->worker_maps, ({
1455 app_wrk = app_worker_get (wrk_map->wrk_index);
1456 if (hash_elts (app_wrk->listeners_table) == 0)
1458 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1459 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1460 handle, sm_index, verbose);
1467 application_format_connects (application_t * app, int verbose)
1469 app_worker_map_t *wrk_map;
1470 app_worker_t *app_wrk;
1474 app_worker_format_connects (0, verbose);
1479 pool_foreach (wrk_map, app->worker_maps, ({
1480 app_wrk = app_worker_get (wrk_map->wrk_index);
1481 app_worker_format_connects (app_wrk, verbose);
1487 application_format_local_sessions (application_t * app, int verbose)
1489 vlib_main_t *vm = vlib_get_main ();
1490 app_worker_map_t *wrk_map;
1491 app_worker_t *app_wrk;
1492 transport_proto_t tp;
1493 local_session_t *ls;
1498 app_worker_format_local_sessions (0, verbose);
1503 * Format local listeners
1507 pool_foreach (ls, app->local_listen_sessions, ({
1508 tp = session_type_transport_proto (ls->listener_session_type);
1509 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1511 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
1512 vec_reset_length (conn);
1517 * Format local accepted/connected sessions
1520 pool_foreach (wrk_map, app->worker_maps, ({
1521 app_wrk = app_worker_get (wrk_map->wrk_index);
1522 app_worker_format_local_sessions (app_wrk, verbose);
1528 application_format_local_connects (application_t * app, int verbose)
1530 app_worker_map_t *wrk_map;
1531 app_worker_t *app_wrk;
1535 app_worker_format_local_connects (0, verbose);
1540 pool_foreach (wrk_map, app->worker_maps, ({
1541 app_wrk = app_worker_get (wrk_map->wrk_index);
1542 app_worker_format_local_connects (app_wrk, verbose);
1548 format_application (u8 * s, va_list * args)
1550 application_t *app = va_arg (*args, application_t *);
1551 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1552 segment_manager_properties_t *props;
1553 const u8 *app_ns_name, *app_name;
1554 app_worker_map_t *wrk_map;
1555 app_worker_t *app_wrk;
1560 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
1564 app_name = app_get_name (app);
1565 app_ns_name = app_namespace_id_from_index (app->ns_index);
1566 props = application_segment_manager_properties (app);
1569 s = format (s, "%-10u%-20s%-40s", app->app_index, app_name,
1574 s = format (s, "app-name %s app-index %u ns-index %u seg-size %U\n",
1575 app_name, app->app_index, app->ns_index,
1576 format_memory_size, props->add_segment_size);
1577 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1578 format_memory_size, props->rx_fifo_size,
1579 format_memory_size, props->tx_fifo_size);
1582 pool_foreach (wrk_map, app->worker_maps, ({
1583 app_wrk = app_worker_get (wrk_map->wrk_index);
1584 s = format (s, "%U", format_app_worker, app_wrk);
1592 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1596 if (!pool_elts (app_main.app_pool))
1598 vlib_cli_output (vm, "No active server bindings");
1604 application_format_local_sessions (0, verbose);
1606 pool_foreach (app, app_main.app_pool, ({
1607 application_format_local_sessions (app, verbose);
1613 application_format_listeners (0, verbose);
1616 pool_foreach (app, app_main.app_pool, ({
1617 application_format_listeners (app, verbose);
1624 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1628 if (!pool_elts (app_main.app_pool))
1630 vlib_cli_output (vm, "No active apps");
1636 application_format_local_connects (0, verbose);
1639 pool_foreach (app, app_main.app_pool, ({
1640 application_format_local_connects (app, verbose);
1646 application_format_connects (0, verbose);
1649 pool_foreach (app, app_main.app_pool, ({
1650 application_format_connects (app, verbose);
1656 static clib_error_t *
1657 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1658 vlib_cli_command_t * cmd)
1660 int do_server = 0, do_client = 0, do_local = 0;
1665 session_cli_return_if_not_enabled ();
1667 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1669 if (unformat (input, "server"))
1671 else if (unformat (input, "client"))
1673 else if (unformat (input, "local"))
1675 else if (unformat (input, "%u", &app_index))
1677 else if (unformat (input, "verbose"))
1680 return clib_error_return (0, "unknown input `%U'",
1681 format_unformat_error, input);
1686 application_format_all_listeners (vm, do_local, verbose);
1692 application_format_all_clients (vm, do_local, verbose);
1696 if (app_index != ~0)
1698 app = application_get_if_valid (app_index);
1700 return clib_error_return (0, "No app with index %u", app_index);
1702 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
1706 /* Print app related info */
1707 if (!do_server && !do_client)
1709 vlib_cli_output (vm, "%U", format_application, 0, 0);
1711 pool_foreach (app, app_main.app_pool, ({
1712 vlib_cli_output (vm, "%U", format_application, app, 0);
1721 VLIB_CLI_COMMAND (show_app_command, static) =
1724 .short_help = "show app [server|client] [verbose]",
1725 .function = show_app_command_fn,
1730 * fd.io coding-style-patch-verification: ON
1733 * eval: (c-set-style "gnu")