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;
197 app_listener = app_listener_alloc (app);
198 al_index = app_listener->al_index;
199 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
202 * Add session endpoint to local session table. Only binds to "inaddr_any"
203 * (i.e., zero address) are added to local scope table.
205 if (application_has_local_scope (app)
206 && session_endpoint_is_local ((session_endpoint_t *) sep))
210 ll = application_local_listen_session_alloc (app);
211 ll->port = sep->port;
212 /* Store the original session type for the unbind */
213 ll->listener_session_type = st;
214 table_index = application_local_session_table (app);
215 lh = application_local_session_handle (ll);
216 session_lookup_add_session_endpoint (table_index,
217 (session_endpoint_t *) sep, lh);
218 app_listener->local_index = ll->session_index;
219 ll->al_index = app_listener->al_index;
222 if (application_has_global_scope (app))
225 * Start listening on local endpoint for requested transport and scope.
226 * Creates a stream session with state LISTENING to be used in session
227 * lookups, prior to establishing connection. Requests transport to
228 * build it's own specific listening connection.
230 ls = listen_session_new (0, st);
231 ls->app_index = app->app_index;
232 ls->app_wrk_index = sep->app_wrk_index;
234 /* Listen pool can be reallocated if the transport is
236 lh = session_handle (ls);
238 if ((rv = session_listen (ls, sep)))
240 ls = session_get_from_handle (lh);
244 ls = session_get_from_handle (lh);
245 app_listener = app_listener_get (app, al_index);
246 app_listener->session_index = ls->session_index;
247 ls->al_index = al_index;
252 app_listener_free (app, app_listener);
256 *listener = app_listener;
261 app_listener_cleanup (app_listener_t * al)
263 application_t *app = application_get (al->app_index);
265 if (al->session_index != SESSION_INVALID_INDEX)
267 session_t *ls = session_get (al->session_index, 0);
268 session_stop_listen (ls);
269 listen_session_del (ls);
271 if (al->local_index != SESSION_INVALID_INDEX)
273 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
277 table_index = application_local_session_table (app);
278 ll = application_get_local_listen_session (app, al->local_index);
279 application_local_listener_session_endpoint (ll, &sep);
280 session_lookup_del_session_endpoint (table_index, &sep);
281 application_local_listen_session_free (app, ll);
283 app_listener_free (app, al);
287 app_listener_select_worker (app_listener_t * al)
292 app = application_get (al->app_index);
293 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
295 wrk_index = clib_bitmap_first_set (al->workers);
297 ASSERT (wrk_index != ~0);
298 al->accept_rotor = wrk_index;
299 return application_get_worker (app, wrk_index);
303 app_listener_get_session (app_listener_t * al)
305 if (al->session_index == SESSION_INVALID_INDEX)
308 return listen_session_get (al->session_index);
311 static app_worker_map_t *
312 app_worker_map_alloc (application_t * app)
314 app_worker_map_t *map;
315 pool_get (app->worker_maps, map);
316 clib_memset (map, 0, sizeof (*map));
321 app_worker_map_index (application_t * app, app_worker_map_t * map)
323 return (map - app->worker_maps);
327 app_worker_map_free (application_t * app, app_worker_map_t * map)
329 pool_put (app->worker_maps, map);
332 static app_worker_map_t *
333 app_worker_map_get (application_t * app, u32 map_index)
335 if (pool_is_free_index (app->worker_maps, map_index))
337 return pool_elt_at_index (app->worker_maps, map_index);
341 app_get_name (application_t * app)
347 application_session_table (application_t * app, u8 fib_proto)
349 app_namespace_t *app_ns;
350 app_ns = app_namespace_get (app->ns_index);
351 if (!application_has_global_scope (app))
352 return APP_INVALID_INDEX;
353 if (fib_proto == FIB_PROTOCOL_IP4)
354 return session_lookup_get_index_for_fib (fib_proto,
355 app_ns->ip4_fib_index);
357 return session_lookup_get_index_for_fib (fib_proto,
358 app_ns->ip6_fib_index);
362 application_local_session_table (application_t * app)
364 app_namespace_t *app_ns;
365 if (!application_has_local_scope (app))
366 return APP_INVALID_INDEX;
367 app_ns = app_namespace_get (app->ns_index);
368 return app_ns->local_table_index;
372 * Returns app name for app-index
375 application_name_from_index (u32 app_index)
377 application_t *app = application_get (app_index);
380 return app_get_name (app);
384 application_api_table_add (u32 app_index, u32 api_client_index)
386 if (api_client_index != APP_INVALID_INDEX)
387 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
391 application_api_table_del (u32 api_client_index)
393 hash_unset (app_main.app_by_api_client_index, api_client_index);
397 application_name_table_add (application_t * app)
399 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
403 application_name_table_del (application_t * app)
405 hash_unset_mem (app_main.app_by_name, app->name);
409 application_lookup (u32 api_client_index)
412 p = hash_get (app_main.app_by_api_client_index, api_client_index);
414 return application_get_if_valid (p[0]);
420 application_lookup_name (const u8 * name)
423 p = hash_get_mem (app_main.app_by_name, name);
425 return application_get (p[0]);
430 static application_t *
431 application_alloc (void)
434 pool_get (app_main.app_pool, app);
435 clib_memset (app, 0, sizeof (*app));
436 app->app_index = app - app_main.app_pool;
441 application_get (u32 app_index)
443 if (app_index == APP_INVALID_INDEX)
445 return pool_elt_at_index (app_main.app_pool, app_index);
449 application_get_if_valid (u32 app_index)
451 if (pool_is_free_index (app_main.app_pool, app_index))
454 return pool_elt_at_index (app_main.app_pool, app_index);
458 application_verify_cb_fns (session_cb_vft_t * cb_fns)
460 if (cb_fns->session_accept_callback == 0)
461 clib_warning ("No accept callback function provided");
462 if (cb_fns->session_connected_callback == 0)
463 clib_warning ("No session connected callback function provided");
464 if (cb_fns->session_disconnect_callback == 0)
465 clib_warning ("No session disconnect callback function provided");
466 if (cb_fns->session_reset_callback == 0)
467 clib_warning ("No session reset callback function provided");
471 * Check app config for given segment type
473 * Returns 1 on success and 0 otherwise
476 application_verify_cfg (ssvm_segment_type_t st)
479 if (st == SSVM_SEGMENT_MEMFD)
481 is_valid = (session_manager_get_evt_q_segment () != 0);
483 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
486 else if (st == SSVM_SEGMENT_SHM)
488 is_valid = (session_manager_get_evt_q_segment () == 0);
490 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
498 application_alloc_and_init (app_init_args_t * a)
500 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
501 segment_manager_properties_t *props;
502 vl_api_registration_t *reg;
506 app = application_alloc ();
507 options = a->options;
509 * Make sure we support the requested configuration
511 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
513 reg = vl_api_client_index_to_registration (a->api_client_index);
515 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
516 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
517 seg_type = SSVM_SEGMENT_SHM;
521 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
523 clib_warning ("mq eventfds can only be used if socket transport is "
525 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
527 seg_type = SSVM_SEGMENT_PRIVATE;
530 if (!application_verify_cfg (seg_type))
531 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
533 /* Check that the obvious things are properly set up */
534 application_verify_cb_fns (a->session_cb_vft);
536 app->flags = options[APP_OPTIONS_FLAGS];
537 app->cb_fns = *a->session_cb_vft;
538 app->ns_index = options[APP_OPTIONS_NAMESPACE];
539 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
540 app->name = vec_dup (a->name);
542 /* If no scope enabled, default to global */
543 if (!application_has_global_scope (app)
544 && !application_has_local_scope (app))
545 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
547 props = application_segment_manager_properties (app);
548 segment_manager_properties_init (props);
549 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
550 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
551 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
553 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
554 props->add_segment = 1;
556 if (options[APP_OPTIONS_RX_FIFO_SIZE])
557 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
558 if (options[APP_OPTIONS_TX_FIFO_SIZE])
559 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
560 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
561 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
562 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
563 props->use_mq_eventfd = 1;
564 if (options[APP_OPTIONS_TLS_ENGINE])
565 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
566 props->segment_type = seg_type;
568 /* Add app to lookup by api_client_index table */
569 if (!application_is_builtin (app))
570 application_api_table_add (app->app_index, a->api_client_index);
572 application_name_table_add (app);
574 a->app_index = app->app_index;
576 APP_DBG ("New app name: %v api index: %u index %u", app->name,
577 app->api_client_index, app->app_index);
583 application_free (application_t * app)
585 app_worker_map_t *wrk_map;
586 app_worker_t *app_wrk;
589 session_endpoint_t sep;
592 * The app event queue allocated in first segment is cleared with
593 * the segment manager. No need to explicitly free it.
595 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
596 app->api_client_index, app->app_index);
598 if (application_is_proxy (app))
599 application_remove_proxy (app);
606 pool_flush (wrk_map, app->worker_maps, ({
607 app_wrk = app_worker_get (wrk_map->wrk_index);
608 app_worker_free (app_wrk);
611 pool_free (app->worker_maps);
614 * Free local listeners. Global table unbinds stop local listeners
615 * as well, but if we have only local binds, these won't be cleaned up.
616 * Don't bother with local accepted sessions, we clean them when
617 * cleaning up the worker.
619 table_index = application_local_session_table (app);
621 pool_foreach (ll, app->local_listen_sessions, ({
622 application_local_listener_session_endpoint (ll, &sep);
623 session_lookup_del_session_endpoint (table_index, &sep);
626 pool_free (app->local_listen_sessions);
629 * Cleanup remaining state
631 if (application_is_builtin (app))
632 application_name_table_del (app);
633 vec_free (app->name);
634 vec_free (app->tls_cert);
635 vec_free (app->tls_key);
636 pool_put (app_main.app_pool, app);
640 application_detach_process (application_t * app, u32 api_client_index)
642 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
643 app_worker_map_t *wrk_map;
644 u32 *wrks = 0, *wrk_index;
645 app_worker_t *app_wrk;
647 if (api_client_index == ~0)
649 application_free (app);
653 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
654 app->app_index, app->api_client_index);
657 pool_foreach (wrk_map, app->worker_maps, ({
658 app_wrk = app_worker_get (wrk_map->wrk_index);
659 if (app_wrk->api_client_index == api_client_index)
660 vec_add1 (wrks, app_wrk->wrk_index);
666 clib_warning ("no workers for app %u api_index %u", app->app_index,
671 args->app_index = app->app_index;
672 args->api_client_index = api_client_index;
673 vec_foreach (wrk_index, wrks)
675 app_wrk = app_worker_get (wrk_index[0]);
676 args->wrk_map_index = app_wrk->wrk_map_index;
678 vnet_app_worker_add_del (args);
684 application_get_worker (application_t * app, u32 wrk_map_index)
686 app_worker_map_t *map;
687 map = app_worker_map_get (app, wrk_map_index);
690 return app_worker_get (map->wrk_index);
694 application_get_default_worker (application_t * app)
696 return application_get_worker (app, 0);
700 application_n_workers (application_t * app)
702 return pool_elts (app->worker_maps);
706 application_listener_select_worker (session_t * ls)
710 al = app_listener_get_w_session (ls);
711 return app_listener_select_worker (al);
715 application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
717 app_worker_map_t *wrk_map;
718 app_worker_t *app_wrk;
719 segment_manager_t *sm;
722 app_wrk = app_worker_alloc (app);
723 wrk_map = app_worker_map_alloc (app);
724 wrk_map->wrk_index = app_wrk->wrk_index;
725 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
728 * Setup first segment manager
730 sm = segment_manager_new ();
731 sm->app_wrk_index = app_wrk->wrk_index;
733 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
734 app->sm_properties.prealloc_fifos)))
736 app_worker_free (app_wrk);
739 sm->first_is_protected = 1;
744 app_wrk->first_segment_manager = segment_manager_index (sm);
745 app_wrk->listeners_table = hash_create (0, sizeof (u64));
746 app_wrk->event_queue = segment_manager_event_queue (sm);
747 app_wrk->app_is_builtin = application_is_builtin (app);
750 * Segment manager for local sessions
752 sm = segment_manager_new ();
753 sm->app_wrk_index = app_wrk->wrk_index;
754 app_wrk->local_segment_manager = segment_manager_index (sm);
755 app_wrk->local_connects = hash_create (0, sizeof (u64));
763 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
765 svm_fifo_segment_private_t *fs;
766 app_worker_map_t *wrk_map;
767 app_worker_t *app_wrk;
768 segment_manager_t *sm;
772 app = application_get (a->app_index);
774 return VNET_API_ERROR_INVALID_VALUE;
778 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
781 /* Map worker api index to the app */
782 app_wrk->api_client_index = a->api_client_index;
783 application_api_table_add (app->app_index, a->api_client_index);
785 sm = segment_manager_get (app_wrk->first_segment_manager);
786 fs = segment_manager_get_segment_w_lock (sm, 0);
787 a->segment = &fs->ssvm;
788 a->segment_handle = segment_manager_segment_handle (sm, fs);
789 segment_manager_segment_reader_unlock (sm);
790 a->evt_q = app_wrk->event_queue;
791 a->wrk_map_index = app_wrk->wrk_map_index;
795 wrk_map = app_worker_map_get (app, a->wrk_map_index);
797 return VNET_API_ERROR_INVALID_VALUE;
799 app_wrk = app_worker_get (wrk_map->wrk_index);
801 return VNET_API_ERROR_INVALID_VALUE;
803 application_api_table_del (app_wrk->api_client_index);
804 app_worker_free (app_wrk);
805 app_worker_map_free (app, wrk_map);
806 if (application_n_workers (app) == 0)
807 application_free (app);
813 app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
815 app_namespace_t *app_ns;
816 if (vec_len (namespace_id) == 0)
818 /* Use default namespace */
823 *app_ns_index = app_namespace_index_from_id (namespace_id);
824 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
825 return VNET_API_ERROR_APP_INVALID_NS;
826 app_ns = app_namespace_get (*app_ns_index);
828 return VNET_API_ERROR_APP_INVALID_NS;
829 if (app_ns->ns_secret != secret)
830 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
835 app_name_from_api_index (u32 api_client_index)
837 vl_api_registration_t *regp;
838 regp = vl_api_client_index_to_registration (api_client_index);
840 return format (0, "%s%c", regp->name, 0);
842 clib_warning ("api client index %u does not have an api registration!",
844 return format (0, "unknown%c", 0);
848 * Attach application to vpp
850 * Allocates a vpp app, i.e., a structure that keeps back pointers
851 * to external app and a segment manager for shared memory fifo based
852 * communication with the external app.
855 vnet_application_attach (vnet_app_attach_args_t * a)
857 svm_fifo_segment_private_t *fs;
858 application_t *app = 0;
859 app_worker_t *app_wrk;
860 segment_manager_t *sm;
861 u32 app_ns_index = 0;
866 if (a->api_client_index != APP_INVALID_INDEX)
867 app = application_lookup (a->api_client_index);
869 app = application_lookup_name (a->name);
871 return VNET_API_ERROR_INVALID_VALUE;
874 return VNET_API_ERROR_APP_ALREADY_ATTACHED;
876 if (a->api_client_index != APP_INVALID_INDEX)
878 app_name = app_name_from_api_index (a->api_client_index);
882 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
883 if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
885 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
887 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
890 app = application_get (a->app_index);
891 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
894 a->app_evt_q = app_wrk->event_queue;
895 app_wrk->api_client_index = a->api_client_index;
896 sm = segment_manager_get (app_wrk->first_segment_manager);
897 fs = segment_manager_get_segment_w_lock (sm, 0);
899 if (application_is_proxy (app))
900 application_setup_proxy (app);
902 ASSERT (vec_len (fs->ssvm.name) <= 128);
903 a->segment = &fs->ssvm;
904 a->segment_handle = segment_manager_segment_handle (sm, fs);
906 segment_manager_segment_reader_unlock (sm);
912 * Detach application from vpp
915 vnet_application_detach (vnet_app_detach_args_t * a)
919 app = application_get_if_valid (a->app_index);
922 clib_warning ("app not attached");
923 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
926 app_interface_check_thread_and_barrier (vnet_application_detach, a);
927 application_detach_process (app, a->api_client_index);
933 session_endpoint_in_ns (session_endpoint_t * sep)
935 u8 is_lep = session_endpoint_is_local (sep);
936 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
937 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
939 clib_warning ("sw_if_index %u not configured with ip %U",
940 sep->sw_if_index, format_ip46_address, &sep->ip,
944 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
948 session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
949 application_t * app, u8 is_connect)
951 app_namespace_t *app_ns;
952 u32 ns_index, fib_index;
954 ns_index = app->ns_index;
956 /* App is a transport proto, so fetch the calling app's ns */
957 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
959 app_worker_t *owner_wrk;
960 application_t *owner_app;
962 owner_wrk = app_worker_get (sep->app_wrk_index);
963 owner_app = application_get (owner_wrk->app_index);
964 ns_index = owner_app->ns_index;
966 app_ns = app_namespace_get (ns_index);
970 /* Ask transport and network to bind to/connect using local interface
971 * that "supports" app's namespace. This will fix our local connection
975 /* If in default namespace and user requested a fib index use it */
976 if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
977 fib_index = sep->fib_index;
979 fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
980 sep->peer.fib_index = fib_index;
981 sep->fib_index = fib_index;
985 sep->sw_if_index = app_ns->sw_if_index;
989 if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
990 && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
991 && sep->peer.sw_if_index != app_ns->sw_if_index)
992 clib_warning ("Local sw_if_index different from app ns sw_if_index");
994 sep->peer.sw_if_index = app_ns->sw_if_index;
999 vnet_listen (vnet_listen_args_t * a)
1001 app_listener_t *app_listener;
1002 app_worker_t *app_wrk;
1006 app = application_get_if_valid (a->app_index);
1008 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1010 app_wrk = application_get_worker (app, a->wrk_map_index);
1012 return VNET_API_ERROR_INVALID_VALUE;
1014 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
1016 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
1017 if (!session_endpoint_in_ns (&a->sep))
1018 return VNET_API_ERROR_INVALID_VALUE_2;
1021 * Check if we already have an app listener
1023 app_listener = app_listener_lookup (app, &a->sep_ext);
1026 if (app_listener->app_index != app->app_index)
1027 return VNET_API_ERROR_ADDRESS_IN_USE;
1028 if (app_worker_start_listen (app_wrk, app_listener))
1030 a->handle = app_listener_handle (app_listener);
1035 * Create new app listener
1037 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1040 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1042 app_listener_cleanup (app_listener);
1046 a->handle = app_listener_handle (app_listener);
1051 vnet_connect (vnet_connect_args_t * a)
1053 app_worker_t *server_wrk, *client_wrk;
1054 application_t *client;
1055 local_session_t *ll;
1062 if (session_endpoint_is_zero (&a->sep))
1063 return VNET_API_ERROR_INVALID_VALUE;
1065 client = application_get (a->app_index);
1066 session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1067 client_wrk = application_get_worker (client, a->wrk_map_index);
1070 * First check the local scope for locally attached destinations.
1071 * If we have local scope, we pass *all* connects through it since we may
1072 * have special policy rules even for non-local destinations, think proxy.
1074 if (application_has_local_scope (client))
1076 table_index = application_local_session_table (client);
1077 lh = session_lookup_local_endpoint (table_index, &a->sep);
1078 if (lh == SESSION_DROP_HANDLE)
1079 return VNET_API_ERROR_APP_CONNECT_FILTERED;
1081 if (lh == SESSION_INVALID_HANDLE)
1084 ll = application_get_local_listener_w_handle (lh);
1085 al = app_listener_get_w_session ((session_t *) ll);
1088 * Break loop if rule in local table points to connecting app. This
1089 * can happen if client is a generic proxy. Route connect through
1090 * global table instead.
1092 if (al->app_index == a->app_index)
1095 server_wrk = app_listener_select_worker (al);
1096 return app_worker_local_session_connect (client_wrk, server_wrk, ll,
1101 * If nothing found, check the global scope for locally attached
1102 * destinations. Make sure first that we're allowed to.
1106 if (session_endpoint_is_local (&a->sep))
1107 return VNET_API_ERROR_SESSION_CONNECT;
1109 if (!application_has_global_scope (client))
1110 return VNET_API_ERROR_APP_CONNECT_SCOPE;
1112 fib_proto = session_endpoint_fib_proto (&a->sep);
1113 table_index = application_session_table (client, fib_proto);
1114 ls = session_lookup_listener (table_index, &a->sep);
1117 al = app_listener_get_w_session (ls);
1118 server_wrk = app_listener_select_worker (al);
1119 ll = (local_session_t *) ls;
1120 return app_worker_local_session_connect (client_wrk, server_wrk, ll,
1125 * Not connecting to a local server, propagate to transport
1127 if (app_worker_connect_session (client_wrk, &a->sep, a->api_context))
1128 return VNET_API_ERROR_SESSION_CONNECT;
1133 vnet_unlisten (vnet_unlisten_args_t * a)
1135 app_worker_t *app_wrk;
1139 if (!(app = application_get_if_valid (a->app_index)))
1140 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1142 al = app_listener_get_w_handle (a->handle);
1143 if (al->app_index != app->app_index)
1145 clib_warning ("app doesn't own handle %llu!", a->handle);
1149 app_wrk = application_get_worker (app, a->wrk_map_index);
1152 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
1156 return app_worker_stop_listen (app_wrk, al);
1160 vnet_disconnect_session (vnet_disconnect_args_t * a)
1162 if (session_handle_is_local (a->handle))
1164 local_session_t *ls;
1166 /* Disconnect reply came to worker 1 not main thread */
1167 app_interface_check_thread_and_barrier (vnet_disconnect_session, a);
1169 if (!(ls = app_worker_get_local_session_from_handle (a->handle)))
1172 return app_worker_local_session_disconnect (a->app_index, ls);
1176 app_worker_t *app_wrk;
1179 s = session_get_from_handle_if_valid (a->handle);
1181 return VNET_API_ERROR_INVALID_VALUE;
1182 app_wrk = app_worker_get (s->app_wrk_index);
1183 if (app_wrk->app_index != a->app_index)
1184 return VNET_API_ERROR_INVALID_VALUE;
1186 /* We're peeking into another's thread pool. Make sure */
1187 ASSERT (s->session_index == session_index_from_handle (a->handle));
1195 application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1197 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1198 app_listener_t *app_listener;
1204 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1205 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1207 segment_manager_dealloc_fifos (s->rx_fifo->segment_index, s->rx_fifo,
1210 app = application_get (old_wrk->app_index);
1214 app_listener = app_listener_get (app, s->al_index);
1216 /* Only remove from lb for now */
1217 app_listener->workers = clib_bitmap_set (app_listener->workers,
1218 old_wrk->wrk_map_index, 0);
1220 if (app_worker_start_listen (app_wrk, app_listener))
1223 s->app_wrk_index = app_wrk->wrk_index;
1229 application_is_proxy (application_t * app)
1231 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1235 application_is_builtin (application_t * app)
1237 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1241 application_is_builtin_proxy (application_t * app)
1243 return (application_is_proxy (app) && application_is_builtin (app));
1247 application_has_local_scope (application_t * app)
1249 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1253 application_has_global_scope (application_t * app)
1255 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1259 application_use_mq_for_ctrl (application_t * app)
1261 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
1264 static clib_error_t *
1265 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1266 u8 transport_proto, u8 is_start)
1268 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1269 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1270 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
1271 transport_connection_t *tc;
1272 app_worker_t *app_wrk;
1277 /* TODO decide if we want proxy to be enabled for all workers */
1278 app_wrk = application_get_default_worker (app);
1281 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1284 sep.is_ip4 = is_ip4;
1285 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1286 sep.sw_if_index = app_ns->sw_if_index;
1287 sep.transport_proto = transport_proto;
1288 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1290 /* force global scope listener */
1292 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1293 app_listener_alloc_and_init (app, &sep, &al);
1296 app_worker_start_listen (app_wrk, al);
1297 s = listen_session_get (al->session_index);
1298 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
1303 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
1307 tc = listen_session_get_transport (s);
1309 if (!ip_is_zero (&tc->lcl_ip, 1))
1312 sep.is_ip4 = is_ip4;
1313 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1314 sep.transport_proto = transport_proto;
1316 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1318 session_lookup_add_session_endpoint (sti,
1319 (session_endpoint_t *) & sep,
1322 session_lookup_del_session_endpoint (sti,
1323 (session_endpoint_t *) & sep);
1330 application_start_stop_proxy_local_scope (application_t * app,
1331 u8 transport_proto, u8 is_start)
1333 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1334 app_namespace_t *app_ns;
1335 app_ns = app_namespace_get (app->ns_index);
1337 sep.transport_proto = transport_proto;
1342 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1345 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1350 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1352 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1357 application_start_stop_proxy (application_t * app,
1358 transport_proto_t transport_proto, u8 is_start)
1360 if (application_has_local_scope (app))
1361 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1363 if (application_has_global_scope (app))
1365 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1366 transport_proto, is_start);
1367 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1368 transport_proto, is_start);
1373 application_setup_proxy (application_t * app)
1375 u16 transports = app->proxied_transports;
1376 transport_proto_t tp;
1378 ASSERT (application_is_proxy (app));
1381 transport_proto_foreach (tp, ({
1382 if (transports & (1 << tp))
1383 application_start_stop_proxy (app, tp, 1);
1389 application_remove_proxy (application_t * app)
1391 u16 transports = app->proxied_transports;
1392 transport_proto_t tp;
1394 ASSERT (application_is_proxy (app));
1397 transport_proto_foreach (tp, ({
1398 if (transports & (1 << tp))
1399 application_start_stop_proxy (app, tp, 0);
1404 segment_manager_properties_t *
1405 application_segment_manager_properties (application_t * app)
1407 return &app->sm_properties;
1410 segment_manager_properties_t *
1411 application_get_segment_manager_properties (u32 app_index)
1413 application_t *app = application_get (app_index);
1414 return &app->sm_properties;
1418 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1421 app = application_get (a->app_index);
1423 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1424 0, "app %u doesn't exist", a->app_index);
1425 app->tls_cert = vec_dup (a->cert);
1430 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1433 app = application_get (a->app_index);
1435 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1436 0, "app %u doesn't exist", a->app_index);
1437 app->tls_key = vec_dup (a->key);
1442 application_format_listeners (application_t * app, int verbose)
1444 vlib_main_t *vm = vlib_get_main ();
1445 app_worker_map_t *wrk_map;
1446 app_worker_t *app_wrk;
1452 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1458 pool_foreach (wrk_map, app->worker_maps, ({
1459 app_wrk = app_worker_get (wrk_map->wrk_index);
1460 if (hash_elts (app_wrk->listeners_table) == 0)
1462 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1463 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1464 handle, sm_index, verbose);
1471 application_format_connects (application_t * app, int verbose)
1473 app_worker_map_t *wrk_map;
1474 app_worker_t *app_wrk;
1478 app_worker_format_connects (0, verbose);
1483 pool_foreach (wrk_map, app->worker_maps, ({
1484 app_wrk = app_worker_get (wrk_map->wrk_index);
1485 app_worker_format_connects (app_wrk, verbose);
1491 application_format_local_sessions (application_t * app, int verbose)
1493 vlib_main_t *vm = vlib_get_main ();
1494 app_worker_map_t *wrk_map;
1495 app_worker_t *app_wrk;
1496 transport_proto_t tp;
1497 local_session_t *ls;
1502 app_worker_format_local_sessions (0, verbose);
1507 * Format local listeners
1511 pool_foreach (ls, app->local_listen_sessions, ({
1512 tp = session_type_transport_proto (ls->listener_session_type);
1513 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1515 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
1516 vec_reset_length (conn);
1521 * Format local accepted/connected sessions
1524 pool_foreach (wrk_map, app->worker_maps, ({
1525 app_wrk = app_worker_get (wrk_map->wrk_index);
1526 app_worker_format_local_sessions (app_wrk, verbose);
1532 application_format_local_connects (application_t * app, int verbose)
1534 app_worker_map_t *wrk_map;
1535 app_worker_t *app_wrk;
1539 app_worker_format_local_connects (0, verbose);
1544 pool_foreach (wrk_map, app->worker_maps, ({
1545 app_wrk = app_worker_get (wrk_map->wrk_index);
1546 app_worker_format_local_connects (app_wrk, verbose);
1552 format_application (u8 * s, va_list * args)
1554 application_t *app = va_arg (*args, application_t *);
1555 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1556 segment_manager_properties_t *props;
1557 const u8 *app_ns_name, *app_name;
1558 app_worker_map_t *wrk_map;
1559 app_worker_t *app_wrk;
1564 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
1568 app_name = app_get_name (app);
1569 app_ns_name = app_namespace_id_from_index (app->ns_index);
1570 props = application_segment_manager_properties (app);
1573 s = format (s, "%-10u%-20s%-40s", app->app_index, app_name,
1578 s = format (s, "app-name %s app-index %u ns-index %u seg-size %U\n",
1579 app_name, app->app_index, app->ns_index,
1580 format_memory_size, props->add_segment_size);
1581 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1582 format_memory_size, props->rx_fifo_size,
1583 format_memory_size, props->tx_fifo_size);
1586 pool_foreach (wrk_map, app->worker_maps, ({
1587 app_wrk = app_worker_get (wrk_map->wrk_index);
1588 s = format (s, "%U", format_app_worker, app_wrk);
1596 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1600 if (!pool_elts (app_main.app_pool))
1602 vlib_cli_output (vm, "No active server bindings");
1608 application_format_local_sessions (0, verbose);
1610 pool_foreach (app, app_main.app_pool, ({
1611 application_format_local_sessions (app, verbose);
1617 application_format_listeners (0, verbose);
1620 pool_foreach (app, app_main.app_pool, ({
1621 application_format_listeners (app, verbose);
1628 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1632 if (!pool_elts (app_main.app_pool))
1634 vlib_cli_output (vm, "No active apps");
1640 application_format_local_connects (0, verbose);
1643 pool_foreach (app, app_main.app_pool, ({
1644 application_format_local_connects (app, verbose);
1650 application_format_connects (0, verbose);
1653 pool_foreach (app, app_main.app_pool, ({
1654 application_format_connects (app, verbose);
1660 static clib_error_t *
1661 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1662 vlib_cli_command_t * cmd)
1664 int do_server = 0, do_client = 0, do_local = 0;
1669 session_cli_return_if_not_enabled ();
1671 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1673 if (unformat (input, "server"))
1675 else if (unformat (input, "client"))
1677 else if (unformat (input, "local"))
1679 else if (unformat (input, "%u", &app_index))
1681 else if (unformat (input, "verbose"))
1684 return clib_error_return (0, "unknown input `%U'",
1685 format_unformat_error, input);
1690 application_format_all_listeners (vm, do_local, verbose);
1696 application_format_all_clients (vm, do_local, verbose);
1700 if (app_index != ~0)
1702 app = application_get_if_valid (app_index);
1704 return clib_error_return (0, "No app with index %u", app_index);
1706 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
1710 /* Print app related info */
1711 if (!do_server && !do_client)
1713 vlib_cli_output (vm, "%U", format_application, 0, 0);
1715 pool_foreach (app, app_main.app_pool, ({
1716 vlib_cli_output (vm, "%U", format_application, app, 0);
1725 VLIB_CLI_COMMAND (show_app_command, static) =
1728 .short_help = "show app [server|client] [verbose]",
1729 .function = show_app_command_fn,
1734 * fd.io coding-style-patch-verification: ON
1737 * eval: (c-set-style "gnu")