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/application_local.h>
20 #include <vnet/session/session.h>
22 static app_main_t app_main;
24 #define app_interface_check_thread_and_barrier(_fn, _arg) \
25 if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \
27 vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \
31 static app_listener_t *
32 app_listener_alloc (application_t * app)
34 app_listener_t *app_listener;
35 pool_get (app->listeners, app_listener);
36 clib_memset (app_listener, 0, sizeof (*app_listener));
37 app_listener->al_index = app_listener - app->listeners;
38 app_listener->app_index = app->app_index;
39 app_listener->session_index = SESSION_INVALID_INDEX;
40 app_listener->local_index = SESSION_INVALID_INDEX;
45 app_listener_get (application_t * app, u32 app_listener_index)
47 return pool_elt_at_index (app->listeners, app_listener_index);
50 static app_listener_t *
51 app_listener_get_if_valid (application_t * app, u32 app_listener_index)
53 if (pool_is_free_index (app->listeners, app_listener_index))
55 return pool_elt_at_index (app->listeners, app_listener_index);
59 app_listener_free (application_t * app, app_listener_t * app_listener)
61 clib_bitmap_free (app_listener->workers);
62 pool_put (app->listeners, app_listener);
64 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
68 app_listener_id (app_listener_t * al)
70 ASSERT (al->app_index < 1 << 16 && al->al_index < 1 << 16);
71 return (al->app_index << 16 | al->al_index);
75 app_listener_handle (app_listener_t * al)
77 return ((u64) SESSION_LISTENER_PREFIX << 32 | (u64) app_listener_id (al));
81 app_listener_id_parse (u32 listener_id, u32 * app_index,
82 u32 * app_listener_index)
84 *app_index = listener_id >> 16;
85 *app_listener_index = listener_id & 0xFFFF;
89 app_listener_handle_parse (session_handle_t handle, u32 * app_index,
90 u32 * app_listener_index)
92 app_listener_id_parse (handle & 0xFFFFFFFF, app_index, app_listener_index);
95 static app_listener_t *
96 app_listener_get_w_id (u32 listener_id)
98 u32 app_index, app_listener_index;
101 app_listener_id_parse (listener_id, &app_index, &app_listener_index);
102 app = application_get_if_valid (app_index);
105 return app_listener_get_if_valid (app, app_listener_index);
109 app_listener_get_w_session (session_t * ls)
113 app = application_get_if_valid (ls->app_index);
116 return app_listener_get (app, ls->al_index);
120 app_listener_get_w_handle (session_handle_t handle)
123 if (handle >> 32 != SESSION_LISTENER_PREFIX)
126 return app_listener_get_w_id (handle & 0xFFFFFFFF);
130 app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
132 u32 table_index, fib_proto;
133 session_endpoint_t *sep;
134 session_handle_t handle;
137 sep = (session_endpoint_t *) sep_ext;
138 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
140 table_index = application_local_session_table (app);
141 handle = session_lookup_endpoint_listener (table_index, sep, 1);
142 if (handle != SESSION_INVALID_HANDLE)
144 ls = listen_session_get_from_handle (handle);
145 return app_listener_get_w_session (ls);
149 fib_proto = session_endpoint_fib_proto (sep);
150 table_index = application_session_table (app, fib_proto);
151 handle = session_lookup_endpoint_listener (table_index, sep, 1);
152 if (handle != SESSION_INVALID_HANDLE)
154 ls = listen_session_get_from_handle (handle);
155 return app_listener_get_w_session ((session_t *) ls);
162 app_listener_alloc_and_init (application_t * app,
163 session_endpoint_cfg_t * sep,
164 app_listener_t ** listener)
166 app_listener_t *app_listener;
167 transport_connection_t *tc;
174 app_listener = app_listener_alloc (app);
175 al_index = app_listener->al_index;
176 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
179 * Add session endpoint to local session table. Only binds to "inaddr_any"
180 * (i.e., zero address) are added to local scope table.
182 if (application_has_local_scope (app)
183 && session_endpoint_is_local ((session_endpoint_t *) sep))
185 session_type_t local_st;
188 local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
190 ls = listen_session_alloc (0, local_st);
191 ls->app_index = app->app_index;
192 ls->app_wrk_index = sep->app_wrk_index;
193 lh = session_handle (ls);
195 if ((rv = session_listen (ls, sep)))
197 ls = session_get_from_handle (lh);
202 ls = session_get_from_handle (lh);
203 app_listener = app_listener_get (app, al_index);
204 app_listener->local_index = ls->session_index;
205 ls->al_index = al_index;
207 table_index = application_local_session_table (app);
208 session_lookup_add_session_endpoint (table_index,
209 (session_endpoint_t *) sep, lh);
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_alloc (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 = listen_session_get_handle (ls);
228 if ((rv = session_listen (ls, sep)))
230 ls = listen_session_get_from_handle (lh);
234 ls = listen_session_get_from_handle (lh);
235 app_listener = app_listener_get (app, al_index);
236 app_listener->session_index = ls->session_index;
237 ls->al_index = al_index;
239 /* Add to the global lookup table after transport was initialized.
240 * Lookup table needs to be populated only now because sessions
241 * with cut-through transport are are added to app local tables that
242 * are not related to network fibs, i.e., cannot be added as
244 tc = session_get_transport (ls);
245 session_lookup_add_connection (tc, lh);
250 app_listener_free (app, app_listener);
254 *listener = app_listener;
259 app_listener_cleanup (app_listener_t * al)
261 application_t *app = application_get (al->app_index);
264 if (al->session_index != SESSION_INVALID_INDEX)
266 ls = session_get (al->session_index, 0);
267 session_stop_listen (ls);
268 listen_session_free (ls);
270 if (al->local_index != SESSION_INVALID_INDEX)
272 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
275 table_index = application_local_session_table (app);
276 ls = listen_session_get (al->local_index);
277 ct_session_endpoint (ls, &sep);
278 session_lookup_del_session_endpoint (table_index, &sep);
279 session_stop_listen (ls);
280 listen_session_free (ls);
282 app_listener_free (app, al);
285 static app_worker_t *
286 app_listener_select_worker (application_t * app, app_listener_t * al)
290 app = application_get (al->app_index);
291 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
293 wrk_index = clib_bitmap_first_set (al->workers);
295 ASSERT (wrk_index != ~0);
296 al->accept_rotor = wrk_index;
297 return application_get_worker (app, wrk_index);
301 app_listener_get_session (app_listener_t * al)
303 if (al->session_index == SESSION_INVALID_INDEX)
306 return listen_session_get (al->session_index);
310 app_listener_get_local_session (app_listener_t * al)
312 if (al->local_index == SESSION_INVALID_INDEX)
314 return listen_session_get (al->local_index);
317 static app_worker_map_t *
318 app_worker_map_alloc (application_t * app)
320 app_worker_map_t *map;
321 pool_get (app->worker_maps, map);
322 clib_memset (map, 0, sizeof (*map));
327 app_worker_map_index (application_t * app, app_worker_map_t * map)
329 return (map - app->worker_maps);
333 app_worker_map_free (application_t * app, app_worker_map_t * map)
335 pool_put (app->worker_maps, map);
338 static app_worker_map_t *
339 app_worker_map_get (application_t * app, u32 map_index)
341 if (pool_is_free_index (app->worker_maps, map_index))
343 return pool_elt_at_index (app->worker_maps, map_index);
347 app_get_name (application_t * app)
353 application_session_table (application_t * app, u8 fib_proto)
355 app_namespace_t *app_ns;
356 app_ns = app_namespace_get (app->ns_index);
357 if (!application_has_global_scope (app))
358 return APP_INVALID_INDEX;
359 if (fib_proto == FIB_PROTOCOL_IP4)
360 return session_lookup_get_index_for_fib (fib_proto,
361 app_ns->ip4_fib_index);
363 return session_lookup_get_index_for_fib (fib_proto,
364 app_ns->ip6_fib_index);
368 application_local_session_table (application_t * app)
370 app_namespace_t *app_ns;
371 if (!application_has_local_scope (app))
372 return APP_INVALID_INDEX;
373 app_ns = app_namespace_get (app->ns_index);
374 return app_ns->local_table_index;
378 * Returns app name for app-index
381 application_name_from_index (u32 app_index)
383 application_t *app = application_get (app_index);
386 return app_get_name (app);
390 application_api_table_add (u32 app_index, u32 api_client_index)
392 if (api_client_index != APP_INVALID_INDEX)
393 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
397 application_api_table_del (u32 api_client_index)
399 hash_unset (app_main.app_by_api_client_index, api_client_index);
403 application_name_table_add (application_t * app)
405 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
409 application_name_table_del (application_t * app)
411 hash_unset_mem (app_main.app_by_name, app->name);
415 application_lookup (u32 api_client_index)
418 p = hash_get (app_main.app_by_api_client_index, api_client_index);
420 return application_get_if_valid (p[0]);
426 application_lookup_name (const u8 * name)
429 p = hash_get_mem (app_main.app_by_name, name);
431 return application_get (p[0]);
436 static application_t *
437 application_alloc (void)
440 pool_get (app_main.app_pool, app);
441 clib_memset (app, 0, sizeof (*app));
442 app->app_index = app - app_main.app_pool;
447 application_get (u32 app_index)
449 if (app_index == APP_INVALID_INDEX)
451 return pool_elt_at_index (app_main.app_pool, app_index);
455 application_get_if_valid (u32 app_index)
457 if (pool_is_free_index (app_main.app_pool, app_index))
460 return pool_elt_at_index (app_main.app_pool, app_index);
464 application_verify_cb_fns (session_cb_vft_t * cb_fns)
466 if (cb_fns->session_accept_callback == 0)
467 clib_warning ("No accept callback function provided");
468 if (cb_fns->session_connected_callback == 0)
469 clib_warning ("No session connected callback function provided");
470 if (cb_fns->session_disconnect_callback == 0)
471 clib_warning ("No session disconnect callback function provided");
472 if (cb_fns->session_reset_callback == 0)
473 clib_warning ("No session reset callback function provided");
477 * Check app config for given segment type
479 * Returns 1 on success and 0 otherwise
482 application_verify_cfg (ssvm_segment_type_t st)
485 if (st == SSVM_SEGMENT_MEMFD)
487 is_valid = (session_main_get_evt_q_segment () != 0);
489 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
492 else if (st == SSVM_SEGMENT_SHM)
494 is_valid = (session_main_get_evt_q_segment () == 0);
496 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
504 application_alloc_and_init (app_init_args_t * a)
506 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
507 segment_manager_props_t *props;
508 vl_api_registration_t *reg;
512 app = application_alloc ();
513 options = a->options;
515 * Make sure we support the requested configuration
517 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
519 reg = vl_api_client_index_to_registration (a->api_client_index);
521 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
522 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
523 seg_type = SSVM_SEGMENT_SHM;
527 seg_type = SSVM_SEGMENT_PRIVATE;
530 if ((options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
531 && seg_type != SSVM_SEGMENT_MEMFD)
533 clib_warning ("mq eventfds can only be used if socket transport is "
534 "used for binary api");
535 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
538 if (!application_verify_cfg (seg_type))
539 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
541 /* Check that the obvious things are properly set up */
542 application_verify_cb_fns (a->session_cb_vft);
544 app->flags = options[APP_OPTIONS_FLAGS];
545 app->cb_fns = *a->session_cb_vft;
546 app->ns_index = options[APP_OPTIONS_NAMESPACE];
547 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
548 app->name = vec_dup (a->name);
550 /* If no scope enabled, default to global */
551 if (!application_has_global_scope (app)
552 && !application_has_local_scope (app))
553 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
555 props = application_segment_manager_properties (app);
556 segment_manager_props_init (props);
557 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
558 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
559 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
561 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
562 props->add_segment = 1;
564 if (options[APP_OPTIONS_RX_FIFO_SIZE])
565 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
566 if (options[APP_OPTIONS_TX_FIFO_SIZE])
567 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
568 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
569 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
570 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
571 props->use_mq_eventfd = 1;
572 if (options[APP_OPTIONS_TLS_ENGINE])
573 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
574 props->segment_type = seg_type;
576 /* Add app to lookup by api_client_index table */
577 if (!application_is_builtin (app))
578 application_api_table_add (app->app_index, a->api_client_index);
580 application_name_table_add (app);
582 a->app_index = app->app_index;
584 APP_DBG ("New app name: %v api index: %u index %u", app->name,
585 app->api_client_index, app->app_index);
591 application_free (application_t * app)
593 app_worker_map_t *wrk_map;
594 app_worker_t *app_wrk;
597 * The app event queue allocated in first segment is cleared with
598 * the segment manager. No need to explicitly free it.
600 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
601 app->api_client_index, app->app_index);
603 if (application_is_proxy (app))
604 application_remove_proxy (app);
611 pool_flush (wrk_map, app->worker_maps, ({
612 app_wrk = app_worker_get (wrk_map->wrk_index);
613 app_worker_free (app_wrk);
616 pool_free (app->worker_maps);
619 * Cleanup remaining state
621 if (application_is_builtin (app))
622 application_name_table_del (app);
623 vec_free (app->name);
624 vec_free (app->tls_cert);
625 vec_free (app->tls_key);
626 pool_put (app_main.app_pool, app);
630 application_detach_process (application_t * app, u32 api_client_index)
632 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
633 app_worker_map_t *wrk_map;
634 u32 *wrks = 0, *wrk_index;
635 app_worker_t *app_wrk;
637 if (api_client_index == ~0)
639 application_free (app);
643 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
644 app->app_index, app->api_client_index);
647 pool_foreach (wrk_map, app->worker_maps, ({
648 app_wrk = app_worker_get (wrk_map->wrk_index);
649 if (app_wrk->api_client_index == api_client_index)
650 vec_add1 (wrks, app_wrk->wrk_index);
656 clib_warning ("no workers for app %u api_index %u", app->app_index,
661 args->app_index = app->app_index;
662 args->api_client_index = api_client_index;
663 vec_foreach (wrk_index, wrks)
665 app_wrk = app_worker_get (wrk_index[0]);
666 args->wrk_map_index = app_wrk->wrk_map_index;
668 vnet_app_worker_add_del (args);
674 application_get_worker (application_t * app, u32 wrk_map_index)
676 app_worker_map_t *map;
677 map = app_worker_map_get (app, wrk_map_index);
680 return app_worker_get (map->wrk_index);
684 application_get_default_worker (application_t * app)
686 return application_get_worker (app, 0);
690 application_n_workers (application_t * app)
692 return pool_elts (app->worker_maps);
696 application_listener_select_worker (session_t * ls)
701 app = application_get (ls->app_index);
702 al = app_listener_get (app, ls->al_index);
703 return app_listener_select_worker (app, al);
707 application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
709 app_worker_map_t *wrk_map;
710 app_worker_t *app_wrk;
711 segment_manager_t *sm;
714 app_wrk = app_worker_alloc (app);
715 wrk_map = app_worker_map_alloc (app);
716 wrk_map->wrk_index = app_wrk->wrk_index;
717 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
720 * Setup first segment manager
722 sm = segment_manager_alloc ();
723 sm->app_wrk_index = app_wrk->wrk_index;
725 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
726 app->sm_properties.prealloc_fifos)))
728 app_worker_free (app_wrk);
731 sm->first_is_protected = 1;
736 app_wrk->first_segment_manager = segment_manager_index (sm);
737 app_wrk->listeners_table = hash_create (0, sizeof (u64));
738 app_wrk->event_queue = segment_manager_event_queue (sm);
739 app_wrk->app_is_builtin = application_is_builtin (app);
747 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
750 app_worker_map_t *wrk_map;
751 app_worker_t *app_wrk;
752 segment_manager_t *sm;
756 app = application_get (a->app_index);
758 return VNET_API_ERROR_INVALID_VALUE;
762 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
765 /* Map worker api index to the app */
766 app_wrk->api_client_index = a->api_client_index;
767 application_api_table_add (app->app_index, a->api_client_index);
769 sm = segment_manager_get (app_wrk->first_segment_manager);
770 fs = segment_manager_get_segment_w_lock (sm, 0);
771 a->segment = &fs->ssvm;
772 a->segment_handle = segment_manager_segment_handle (sm, fs);
773 segment_manager_segment_reader_unlock (sm);
774 a->evt_q = app_wrk->event_queue;
775 a->wrk_map_index = app_wrk->wrk_map_index;
779 wrk_map = app_worker_map_get (app, a->wrk_map_index);
781 return VNET_API_ERROR_INVALID_VALUE;
783 app_wrk = app_worker_get (wrk_map->wrk_index);
785 return VNET_API_ERROR_INVALID_VALUE;
787 application_api_table_del (app_wrk->api_client_index);
788 app_worker_free (app_wrk);
789 app_worker_map_free (app, wrk_map);
790 if (application_n_workers (app) == 0)
791 application_free (app);
797 app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
799 app_namespace_t *app_ns;
800 if (vec_len (namespace_id) == 0)
802 /* Use default namespace */
807 *app_ns_index = app_namespace_index_from_id (namespace_id);
808 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
809 return VNET_API_ERROR_APP_INVALID_NS;
810 app_ns = app_namespace_get (*app_ns_index);
812 return VNET_API_ERROR_APP_INVALID_NS;
813 if (app_ns->ns_secret != secret)
814 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
819 app_name_from_api_index (u32 api_client_index)
821 vl_api_registration_t *regp;
822 regp = vl_api_client_index_to_registration (api_client_index);
824 return format (0, "%s", regp->name);
826 clib_warning ("api client index %u does not have an api registration!",
828 return format (0, "unknown");
832 * Attach application to vpp
834 * Allocates a vpp app, i.e., a structure that keeps back pointers
835 * to external app and a segment manager for shared memory fifo based
836 * communication with the external app.
839 vnet_application_attach (vnet_app_attach_args_t * a)
842 application_t *app = 0;
843 app_worker_t *app_wrk;
844 segment_manager_t *sm;
845 u32 app_ns_index = 0;
850 if (a->api_client_index != APP_INVALID_INDEX)
851 app = application_lookup (a->api_client_index);
853 app = application_lookup_name (a->name);
855 return VNET_API_ERROR_INVALID_VALUE;
858 return VNET_API_ERROR_APP_ALREADY_ATTACHED;
860 if (a->api_client_index != APP_INVALID_INDEX)
862 app_name = app_name_from_api_index (a->api_client_index);
866 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
867 if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
869 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
871 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
874 app = application_get (a->app_index);
875 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
878 a->app_evt_q = app_wrk->event_queue;
879 app_wrk->api_client_index = a->api_client_index;
880 sm = segment_manager_get (app_wrk->first_segment_manager);
881 fs = segment_manager_get_segment_w_lock (sm, 0);
883 if (application_is_proxy (app))
884 application_setup_proxy (app);
886 ASSERT (vec_len (fs->ssvm.name) <= 128);
887 a->segment = &fs->ssvm;
888 a->segment_handle = segment_manager_segment_handle (sm, fs);
890 segment_manager_segment_reader_unlock (sm);
896 * Detach application from vpp
899 vnet_application_detach (vnet_app_detach_args_t * a)
903 app = application_get_if_valid (a->app_index);
906 clib_warning ("app not attached");
907 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
910 app_interface_check_thread_and_barrier (vnet_application_detach, a);
911 application_detach_process (app, a->api_client_index);
917 session_endpoint_in_ns (session_endpoint_t * sep)
919 u8 is_lep = session_endpoint_is_local (sep);
920 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
921 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
923 clib_warning ("sw_if_index %u not configured with ip %U",
924 sep->sw_if_index, format_ip46_address, &sep->ip,
928 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
932 session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
933 application_t * app, u8 is_connect)
935 app_namespace_t *app_ns;
936 u32 ns_index, fib_index;
938 ns_index = app->ns_index;
940 /* App is a transport proto, so fetch the calling app's ns */
941 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
942 ns_index = sep->ns_index;
944 app_ns = app_namespace_get (ns_index);
948 /* Ask transport and network to bind to/connect using local interface
949 * that "supports" app's namespace. This will fix our local connection
953 /* If in default namespace and user requested a fib index use it */
954 if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
955 fib_index = sep->fib_index;
957 fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
958 sep->peer.fib_index = fib_index;
959 sep->fib_index = fib_index;
963 sep->sw_if_index = app_ns->sw_if_index;
967 if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
968 && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
969 && sep->peer.sw_if_index != app_ns->sw_if_index)
970 clib_warning ("Local sw_if_index different from app ns sw_if_index");
972 sep->peer.sw_if_index = app_ns->sw_if_index;
977 vnet_listen (vnet_listen_args_t * a)
979 app_listener_t *app_listener;
980 app_worker_t *app_wrk;
984 app = application_get_if_valid (a->app_index);
986 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
988 app_wrk = application_get_worker (app, a->wrk_map_index);
990 return VNET_API_ERROR_INVALID_VALUE;
992 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
994 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
995 if (!session_endpoint_in_ns (&a->sep))
996 return VNET_API_ERROR_INVALID_VALUE_2;
999 * Check if we already have an app listener
1001 app_listener = app_listener_lookup (app, &a->sep_ext);
1004 if (app_listener->app_index != app->app_index)
1005 return VNET_API_ERROR_ADDRESS_IN_USE;
1006 if (app_worker_start_listen (app_wrk, app_listener))
1008 a->handle = app_listener_handle (app_listener);
1013 * Create new app listener
1015 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1018 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1020 app_listener_cleanup (app_listener);
1024 a->handle = app_listener_handle (app_listener);
1029 vnet_connect (vnet_connect_args_t * a)
1031 app_worker_t *client_wrk;
1032 application_t *client;
1034 if (session_endpoint_is_zero (&a->sep))
1035 return VNET_API_ERROR_INVALID_VALUE;
1037 client = application_get (a->app_index);
1038 session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1039 client_wrk = application_get_worker (client, a->wrk_map_index);
1042 * First check the local scope for locally attached destinations.
1043 * If we have local scope, we pass *all* connects through it since we may
1044 * have special policy rules even for non-local destinations, think proxy.
1046 if (application_has_local_scope (client))
1050 a->sep_ext.original_tp = a->sep_ext.transport_proto;
1051 a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE;
1052 rv = app_worker_connect_session (client_wrk, &a->sep, a->api_context);
1057 * Not connecting to a local server, propagate to transport
1059 if (app_worker_connect_session (client_wrk, &a->sep, a->api_context))
1060 return VNET_API_ERROR_SESSION_CONNECT;
1065 vnet_unlisten (vnet_unlisten_args_t * a)
1067 app_worker_t *app_wrk;
1071 if (!(app = application_get_if_valid (a->app_index)))
1072 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1074 if (!(al = app_listener_get_w_handle (a->handle)))
1077 if (al->app_index != app->app_index)
1079 clib_warning ("app doesn't own handle %llu!", a->handle);
1083 app_wrk = application_get_worker (app, a->wrk_map_index);
1086 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
1090 return app_worker_stop_listen (app_wrk, al);
1094 vnet_disconnect_session (vnet_disconnect_args_t * a)
1096 app_worker_t *app_wrk;
1099 s = session_get_from_handle_if_valid (a->handle);
1101 return VNET_API_ERROR_INVALID_VALUE;
1102 app_wrk = app_worker_get (s->app_wrk_index);
1103 if (app_wrk->app_index != a->app_index)
1104 return VNET_API_ERROR_INVALID_VALUE;
1106 /* We're peeking into another's thread pool. Make sure */
1107 ASSERT (s->session_index == session_index_from_handle (a->handle));
1114 application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1116 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1117 app_listener_t *app_listener;
1123 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1124 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1126 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1128 app = application_get (old_wrk->app_index);
1132 app_listener = app_listener_get (app, s->al_index);
1134 /* Only remove from lb for now */
1135 app_listener->workers = clib_bitmap_set (app_listener->workers,
1136 old_wrk->wrk_map_index, 0);
1138 if (app_worker_start_listen (app_wrk, app_listener))
1141 s->app_wrk_index = app_wrk->wrk_index;
1147 application_is_proxy (application_t * app)
1149 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1153 application_is_builtin (application_t * app)
1155 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1159 application_is_builtin_proxy (application_t * app)
1161 return (application_is_proxy (app) && application_is_builtin (app));
1165 application_has_local_scope (application_t * app)
1167 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1171 application_has_global_scope (application_t * app)
1173 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1176 static clib_error_t *
1177 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1178 u8 transport_proto, u8 is_start)
1180 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1181 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1182 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
1183 transport_connection_t *tc;
1184 app_worker_t *app_wrk;
1189 /* TODO decide if we want proxy to be enabled for all workers */
1190 app_wrk = application_get_default_worker (app);
1193 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1196 sep.is_ip4 = is_ip4;
1197 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1198 sep.sw_if_index = app_ns->sw_if_index;
1199 sep.transport_proto = transport_proto;
1200 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1202 /* force global scope listener */
1204 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1205 app_listener_alloc_and_init (app, &sep, &al);
1208 app_worker_start_listen (app_wrk, al);
1209 s = listen_session_get (al->session_index);
1210 s->flags |= SESSION_F_PROXY;
1215 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
1219 tc = listen_session_get_transport (s);
1221 if (!ip_is_zero (&tc->lcl_ip, 1))
1224 sep.is_ip4 = is_ip4;
1225 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1226 sep.transport_proto = transport_proto;
1228 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1230 session_lookup_add_session_endpoint (sti,
1231 (session_endpoint_t *) & sep,
1234 session_lookup_del_session_endpoint (sti,
1235 (session_endpoint_t *) & sep);
1242 application_start_stop_proxy_local_scope (application_t * app,
1243 u8 transport_proto, u8 is_start)
1245 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1246 app_namespace_t *app_ns;
1247 app_ns = app_namespace_get (app->ns_index);
1249 sep.transport_proto = transport_proto;
1254 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1257 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1262 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1264 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1269 application_start_stop_proxy (application_t * app,
1270 transport_proto_t transport_proto, u8 is_start)
1272 if (application_has_local_scope (app))
1273 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1275 if (application_has_global_scope (app))
1277 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1278 transport_proto, is_start);
1279 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1280 transport_proto, is_start);
1285 application_setup_proxy (application_t * app)
1287 u16 transports = app->proxied_transports;
1288 transport_proto_t tp;
1290 ASSERT (application_is_proxy (app));
1293 transport_proto_foreach (tp, ({
1294 if (transports & (1 << tp))
1295 application_start_stop_proxy (app, tp, 1);
1301 application_remove_proxy (application_t * app)
1303 u16 transports = app->proxied_transports;
1304 transport_proto_t tp;
1306 ASSERT (application_is_proxy (app));
1309 transport_proto_foreach (tp, ({
1310 if (transports & (1 << tp))
1311 application_start_stop_proxy (app, tp, 0);
1316 segment_manager_props_t *
1317 application_segment_manager_properties (application_t * app)
1319 return &app->sm_properties;
1322 segment_manager_props_t *
1323 application_get_segment_manager_properties (u32 app_index)
1325 application_t *app = application_get (app_index);
1326 return &app->sm_properties;
1330 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1333 app = application_get (a->app_index);
1335 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1336 0, "app %u doesn't exist", a->app_index);
1337 app->tls_cert = vec_dup (a->cert);
1342 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1345 app = application_get (a->app_index);
1347 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1348 0, "app %u doesn't exist", a->app_index);
1349 app->tls_key = vec_dup (a->key);
1354 application_format_listeners (application_t * app, int verbose)
1356 vlib_main_t *vm = vlib_get_main ();
1357 app_worker_map_t *wrk_map;
1358 app_worker_t *app_wrk;
1364 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1370 pool_foreach (wrk_map, app->worker_maps, ({
1371 app_wrk = app_worker_get (wrk_map->wrk_index);
1372 if (hash_elts (app_wrk->listeners_table) == 0)
1374 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1375 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1376 handle, sm_index, verbose);
1383 application_format_connects (application_t * app, int verbose)
1385 app_worker_map_t *wrk_map;
1386 app_worker_t *app_wrk;
1390 app_worker_format_connects (0, verbose);
1395 pool_foreach (wrk_map, app->worker_maps, ({
1396 app_wrk = app_worker_get (wrk_map->wrk_index);
1397 app_worker_format_connects (app_wrk, verbose);
1403 format_application (u8 * s, va_list * args)
1405 application_t *app = va_arg (*args, application_t *);
1406 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1407 segment_manager_props_t *props;
1408 const u8 *app_ns_name, *app_name;
1409 app_worker_map_t *wrk_map;
1410 app_worker_t *app_wrk;
1415 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
1419 app_name = app_get_name (app);
1420 app_ns_name = app_namespace_id_from_index (app->ns_index);
1421 props = application_segment_manager_properties (app);
1424 s = format (s, "%-10u%-20v%-40s", app->app_index, app_name,
1429 s = format (s, "app-name %v app-index %u ns-index %u seg-size %U\n",
1430 app_name, app->app_index, app->ns_index,
1431 format_memory_size, props->add_segment_size);
1432 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1433 format_memory_size, props->rx_fifo_size,
1434 format_memory_size, props->tx_fifo_size);
1437 pool_foreach (wrk_map, app->worker_maps, ({
1438 app_wrk = app_worker_get (wrk_map->wrk_index);
1439 s = format (s, "%U", format_app_worker, app_wrk);
1447 application_format_all_listeners (vlib_main_t * vm, int verbose)
1451 if (!pool_elts (app_main.app_pool))
1453 vlib_cli_output (vm, "No active server bindings");
1457 application_format_listeners (0, verbose);
1460 pool_foreach (app, app_main.app_pool, ({
1461 application_format_listeners (app, verbose);
1467 application_format_all_clients (vlib_main_t * vm, int verbose)
1471 if (!pool_elts (app_main.app_pool))
1473 vlib_cli_output (vm, "No active apps");
1477 application_format_connects (0, verbose);
1480 pool_foreach (app, app_main.app_pool, ({
1481 application_format_connects (app, verbose);
1486 static clib_error_t *
1487 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1488 vlib_cli_command_t * cmd)
1490 int do_server = 0, do_client = 0;
1495 session_cli_return_if_not_enabled ();
1497 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1499 if (unformat (input, "server"))
1501 else if (unformat (input, "client"))
1503 else if (unformat (input, "%u", &app_index))
1505 else if (unformat (input, "verbose"))
1508 return clib_error_return (0, "unknown input `%U'",
1509 format_unformat_error, input);
1514 application_format_all_listeners (vm, verbose);
1520 application_format_all_clients (vm, verbose);
1524 if (app_index != ~0)
1526 app = application_get_if_valid (app_index);
1528 return clib_error_return (0, "No app with index %u", app_index);
1530 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
1534 /* Print app related info */
1535 if (!do_server && !do_client)
1537 vlib_cli_output (vm, "%U", format_application, 0, 0);
1539 pool_foreach (app, app_main.app_pool, ({
1540 vlib_cli_output (vm, "%U", format_application, app, 0);
1549 VLIB_CLI_COMMAND (show_app_command, static) =
1552 .short_help = "show app [server|client] [verbose]",
1553 .function = show_app_command_fn,
1558 * fd.io coding-style-patch-verification: ON
1561 * eval: (c-set-style "gnu")