diff options
author | Florin Coras <fcoras@cisco.com> | 2018-08-12 23:50:53 -0700 |
---|---|---|
committer | Damjan Marion <dmarion@me.com> | 2018-08-24 19:05:25 +0000 |
commit | 1553197f9a1a3258b6954adeb9536bbe0191683d (patch) | |
tree | 91f26e2e23da3971741238194474d5e5ab0ae792 /src/vnet/session | |
parent | f8b8586b699bae9e786726f2697c3e642d904c61 (diff) |
session: add support for multiple app workers
Refactor session layer to support multiple workers per application.
Change-Id: Ie67354688d396449d14bbbb8c56050206e307cd8
Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/vnet/session')
-rw-r--r-- | src/vnet/session/application.c | 1002 | ||||
-rw-r--r-- | src/vnet/session/application.h | 243 | ||||
-rw-r--r-- | src/vnet/session/application_interface.c | 129 | ||||
-rw-r--r-- | src/vnet/session/application_interface.h | 37 | ||||
-rw-r--r-- | src/vnet/session/segment_manager.c | 16 | ||||
-rw-r--r-- | src/vnet/session/segment_manager.h | 31 | ||||
-rw-r--r-- | src/vnet/session/session.api | 47 | ||||
-rw-r--r-- | src/vnet/session/session.c | 92 | ||||
-rwxr-xr-x | src/vnet/session/session_api.c | 198 | ||||
-rwxr-xr-x | src/vnet/session/session_cli.c | 5 | ||||
-rw-r--r-- | src/vnet/session/session_lookup.c | 16 | ||||
-rw-r--r-- | src/vnet/session/session_node.c | 46 | ||||
-rw-r--r-- | src/vnet/session/session_test.c | 27 | ||||
-rw-r--r-- | src/vnet/session/stream_session.h | 52 |
14 files changed, 1238 insertions, 703 deletions
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c index 99f3ec89cde..1ad94cc2778 100644 --- a/src/vnet/session/application.c +++ b/src/vnet/session/application.c @@ -18,20 +18,34 @@ #include <vnet/session/application_namespace.h> #include <vnet/session/session.h> -/** - * Pool from which we allocate all applications - */ -static application_t *app_pool; +static app_main_t app_main; -/** - * Hash table of apps by api client index - */ -static uword *app_by_api_client_index; +static app_worker_map_t * +app_worker_map_alloc (application_t * app) +{ + app_worker_map_t *map; + pool_get (app->worker_maps, map); + memset (map, 0, sizeof (*map)); + return map; +} -/** - * Hash table of builtin apps by name - */ -static uword *app_by_name; +static u32 +app_worker_map_index (application_t * app, app_worker_map_t * map) +{ + return (map - app->worker_maps); +} + +static void +app_worker_map_free (application_t * app, app_worker_map_t * map) +{ + pool_put (app->worker_maps, map); +} + +static app_worker_map_t * +app_worker_map_get (application_t * app, u32 map_index) +{ + return pool_elt_at_index (app->worker_maps, map_index); +} static u8 * app_get_name_from_reg_index (application_t * app) @@ -41,7 +55,7 @@ app_get_name_from_reg_index (application_t * app) vl_api_registration_t *regp; regp = vl_api_client_index_to_registration (app->api_client_index); if (!regp) - app_name = format (0, "builtin-%d%c", app->index, 0); + app_name = format (0, "builtin-%d%c", app->app_index, 0); else app_name = format (0, "%s%c", regp->name, 0); @@ -118,25 +132,26 @@ static void application_table_add (application_t * app) { if (app->api_client_index != APP_INVALID_INDEX) - hash_set (app_by_api_client_index, app->api_client_index, app->index); + hash_set (app_main.app_by_api_client_index, app->api_client_index, + app->app_index); else if (app->name) - hash_set_mem (app_by_name, app->name, app->index); + hash_set_mem (app_main.app_by_name, app->name, app->app_index); } static void application_table_del (application_t * app) { if (app->api_client_index != APP_INVALID_INDEX) - hash_unset (app_by_api_client_index, app->api_client_index); + hash_unset (app_main.app_by_api_client_index, app->api_client_index); else if (app->name) - hash_unset_mem (app_by_name, app->name); + hash_unset_mem (app_main.app_by_name, app->name); } application_t * application_lookup (u32 api_client_index) { uword *p; - p = hash_get (app_by_api_client_index, api_client_index); + p = hash_get (app_main.app_by_api_client_index, api_client_index); if (p) return application_get (p[0]); @@ -147,7 +162,7 @@ application_t * application_lookup_name (const u8 * name) { uword *p; - p = hash_get_mem (app_by_name, name); + p = hash_get_mem (app_main.app_by_name, name); if (p) return application_get (p[0]); @@ -155,96 +170,36 @@ application_lookup_name (const u8 * name) } application_t * -application_new () +application_alloc (void) { application_t *app; - pool_get (app_pool, app); + pool_get (app_main.app_pool, app); memset (app, 0, sizeof (*app)); - app->index = application_get_index (app); - app->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; - app->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; - app->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; - if (CLIB_DEBUG > 1) - clib_warning ("[%d] New app (%d)", getpid (), app->index); + app->app_index = app - app_main.app_pool; return app; } -void -application_del (application_t * app) +application_t * +application_get (u32 app_index) { - vnet_unbind_args_t _a, *a = &_a; - u64 handle, *handles = 0; - segment_manager_t *sm; - u32 index; - int i; - - /* - * The app event queue allocated in first segment is cleared with - * the segment manager. No need to explicitly free it. - */ - if (CLIB_DEBUG > 1) - clib_warning ("[%d] Delete app (%d)", getpid (), app->index); - - if (application_is_proxy (app)) - application_remove_proxy (app); - - /* - * Listener cleanup - */ - - /* *INDENT-OFF* */ - hash_foreach (handle, index, app->listeners_table, - ({ - vec_add1 (handles, handle); - sm = segment_manager_get (index); - sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX; - })); - /* *INDENT-ON* */ - - for (i = 0; i < vec_len (handles); i++) - { - a->app_index = app->index; - a->handle = handles[i]; - /* seg manager is removed when unbind completes */ - vnet_unbind (a); - } - - /* - * Connects segment manager cleanup - */ - - if (app->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX) - { - sm = segment_manager_get (app->connects_seg_manager); - sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX; - segment_manager_init_del (sm); - } - - /* If first segment manager is used by a listener */ - if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX - && app->first_segment_manager != app->connects_seg_manager) - { - sm = segment_manager_get (app->first_segment_manager); - /* .. and has no fifos, e.g. it might be used for redirected sessions, - * remove it */ - if (!segment_manager_has_fifos (sm)) - { - sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX; - segment_manager_del (sm); - } - } + if (app_index == APP_INVALID_INDEX) + return 0; + return pool_elt_at_index (app_main.app_pool, app_index); +} - /* - * Local connections cleanup - */ - application_local_sessions_del (app); +application_t * +application_get_if_valid (u32 app_index) +{ + if (pool_is_free_index (app_main.app_pool, app_index)) + return 0; - vec_free (app->tls_cert); - vec_free (app->tls_key); + return pool_elt_at_index (app_main.app_pool, app_index); +} - application_table_del (app); - vec_free (app->name); - pool_put (app_pool, app); +u32 +application_index (application_t * app) +{ + return app - app_main.app_pool; } static void @@ -288,23 +243,22 @@ application_verify_cfg (ssvm_segment_type_t st) } int -application_init (application_t * app, u32 api_client_index, u8 * app_name, - u64 * options, session_cb_vft_t * cb_fns) +application_alloc_and_init (app_init_args_t * a) { ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD; - u32 first_seg_size, prealloc_fifo_pairs; segment_manager_properties_t *props; vl_api_registration_t *reg; - segment_manager_t *sm; - int rv; + application_t *app; + u64 *options; + app = application_alloc (); + options = a->options; /* * Make sure we support the requested configuration */ - if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN)) { - reg = vl_api_client_index_to_registration (api_client_index); + reg = vl_api_client_index_to_registration (a->api_client_index); if (!reg) return VNET_API_ERROR_APP_UNSUPPORTED_CFG; if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) @@ -324,13 +278,25 @@ application_init (application_t * app, u32 api_client_index, u8 * app_name, if (!application_verify_cfg (seg_type)) return VNET_API_ERROR_APP_UNSUPPORTED_CFG; - /* - * Setup segment manager - */ - sm = segment_manager_new (); - sm->app_index = app->index; + /* Check that the obvious things are properly set up */ + application_verify_cb_fns (a->session_cb_vft); + + app->api_client_index = a->api_client_index; + app->flags = options[APP_OPTIONS_FLAGS]; + app->cb_fns = *a->session_cb_vft; + app->ns_index = options[APP_OPTIONS_NAMESPACE]; + app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT]; + app->name = vec_dup (a->name); + + /* If no scope enabled, default to global */ + if (!application_has_global_scope (app) + && !application_has_local_scope (app)) + app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; + props = application_segment_manager_properties (app); segment_manager_properties_init (props); + props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE]; + props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS]; if (options[APP_OPTIONS_ADD_SEGMENT_SIZE]) { props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE]; @@ -348,87 +314,224 @@ application_init (application_t * app, u32 api_client_index, u8 * app_name, app->tls_engine = options[APP_OPTIONS_TLS_ENGINE]; props->segment_type = seg_type; - first_seg_size = options[APP_OPTIONS_SEGMENT_SIZE]; - prealloc_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS]; + /* Add app to lookup by api_client_index table */ + application_table_add (app); + a->app_index = application_index (app); - if ((rv = segment_manager_init (sm, first_seg_size, prealloc_fifo_pairs))) - return rv; - sm->first_is_protected = 1; + APP_DBG ("New app name: %v api index: %u index %u", app->name, + app->api_client_index, app->app_index); + + return 0; +} + +void +application_free (application_t * app) +{ + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; /* - * Setup application + * The app event queue allocated in first segment is cleared with + * the segment manager. No need to explicitly free it. */ - app->first_segment_manager = segment_manager_index (sm); - app->api_client_index = api_client_index; - app->flags = options[APP_OPTIONS_FLAGS]; - app->cb_fns = *cb_fns; - app->ns_index = options[APP_OPTIONS_NAMESPACE]; - app->listeners_table = hash_create (0, sizeof (u64)); - app->local_connects = hash_create (0, sizeof (u64)); - app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT]; - app->event_queue = segment_manager_event_queue (sm); - app->name = vec_dup (app_name); + APP_DBG ("Delete app name %v api index: %d index: %d", app->name, + app->api_client_index, app->app_index); - /* If no scope enabled, default to global */ - if (!application_has_global_scope (app) - && !application_has_local_scope (app)) - app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; + if (application_is_proxy (app)) + application_remove_proxy (app); - /* Check that the obvious things are properly set up */ - application_verify_cb_fns (cb_fns); + /* *INDENT-OFF* */ + pool_flush (wrk_map, app->worker_maps, ({ + app_wrk = app_worker_get (wrk_map->wrk_index); + app_worker_free (app_wrk); + })); + /* *INDENT-ON* */ + pool_free (app->worker_maps); - /* Add app to lookup by api_client_index table */ - application_table_add (app); + application_table_del (app); + vec_free (app->name); + vec_free (app->tls_cert); + vec_free (app->tls_key); + pool_put (app_main.app_pool, app); +} - /* - * Segment manager for local sessions - */ - sm = segment_manager_new (); - sm->app_index = app->index; - app->local_segment_manager = segment_manager_index (sm); +app_worker_t * +application_get_worker (application_t * app, u32 wrk_map_index) +{ + app_worker_map_t *map; + map = app_worker_map_get (app, wrk_map_index); + if (!map) + return 0; + return app_worker_get (map->wrk_index); +} - return 0; +app_worker_t * +application_get_default_worker (application_t * app) +{ + return application_get_worker (app, 0); } -application_t * -application_get (u32 index) +app_worker_t * +app_worker_alloc (application_t * app) { - if (index == APP_INVALID_INDEX) - return 0; - return pool_elt_at_index (app_pool, index); + app_worker_t *app_wrk; + pool_get (app_main.workers, app_wrk); + memset (app_wrk, 0, sizeof (*app_wrk)); + app_wrk->wrk_index = app_wrk - app_main.workers; + app_wrk->app_index = app->app_index; + app_wrk->wrk_map_index = ~0; + app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; + app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; + app_wrk->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; + APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index); + return app_wrk; } -application_t * -application_get_if_valid (u32 index) +app_worker_t * +app_worker_get (u32 wrk_index) +{ + return pool_elt_at_index (app_main.workers, wrk_index); +} + +app_worker_t * +app_worker_get_if_valid (u32 wrk_index) { - if (pool_is_free_index (app_pool, index)) + if (pool_is_free_index (app_main.workers, wrk_index)) return 0; + return pool_elt_at_index (app_main.workers, wrk_index); +} + +void +app_worker_free (app_worker_t * app_wrk) +{ + application_t *app = application_get (app_wrk->app_index); + vnet_unbind_args_t _a, *a = &_a; + u64 handle, *handles = 0; + segment_manager_t *sm; + u32 sm_index; + int i; + + /* + * Listener cleanup + */ + + /* *INDENT-OFF* */ + hash_foreach (handle, sm_index, app_wrk->listeners_table, + ({ + vec_add1 (handles, handle); + sm = segment_manager_get (sm_index); + sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; + })); + /* *INDENT-ON* */ + + for (i = 0; i < vec_len (handles); i++) + { + a->app_index = app->app_index; + a->app_wrk_index = app_wrk->wrk_map_index; + a->handle = handles[i]; + /* seg manager is removed when unbind completes */ + vnet_unbind (a); + } + + /* + * Connects segment manager cleanup + */ + + if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX) + { + sm = segment_manager_get (app_wrk->connects_seg_manager); + sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; + segment_manager_init_del (sm); + } + + /* If first segment manager is used by a listener */ + if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX + && app_wrk->first_segment_manager != app_wrk->connects_seg_manager) + { + sm = segment_manager_get (app_wrk->first_segment_manager); + /* .. and has no fifos, e.g. it might be used for redirected sessions, + * remove it */ + if (!segment_manager_has_fifos (sm)) + { + sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; + segment_manager_del (sm); + } + } - return pool_elt_at_index (app_pool, index); + /* + * Local sessions + */ + application_local_sessions_free (app_wrk); + + pool_put (app_main.workers, app_wrk); + if (CLIB_DEBUG) + memset (app_wrk, 0xfe, sizeof (*app_wrk)); } -u32 -application_get_index (application_t * app) +int +app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk) { - return app - app_pool; + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; + segment_manager_t *sm; + int rv; + + app_wrk = app_worker_alloc (app); + wrk_map = app_worker_map_alloc (app); + wrk_map->wrk_index = app_wrk->wrk_index; + app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map); + + /* + * Setup first segment manager + */ + sm = segment_manager_new (); + sm->app_wrk_index = app_wrk->wrk_index; + + if ((rv = segment_manager_init (sm, app->sm_properties.segment_size, + app->sm_properties.prealloc_fifos))) + { + app_worker_free (app_wrk); + return rv; + } + sm->first_is_protected = 1; + + /* + * Setup app worker + */ + app_wrk->first_segment_manager = segment_manager_index (sm); + app_wrk->listeners_table = hash_create (0, sizeof (u64)); + app_wrk->event_queue = segment_manager_event_queue (sm); + app_wrk->app_is_builtin = application_is_builtin (app); + + /* + * Segment manager for local sessions + */ + sm = segment_manager_new (); + sm->app_wrk_index = app_wrk->wrk_index; + app_wrk->local_segment_manager = segment_manager_index (sm); + app_wrk->local_connects = hash_create (0, sizeof (u64)); + + *wrk = app_wrk; + + return 0; } static segment_manager_t * -application_alloc_segment_manager (application_t * app) +application_alloc_segment_manager (app_worker_t * app_wrk) { segment_manager_t *sm = 0; /* If the first segment manager is not in use, don't allocate a new one */ - if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX - && app->first_segment_manager_in_use == 0) + if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX + && app_wrk->first_segment_manager_in_use == 0) { - sm = segment_manager_get (app->first_segment_manager); - app->first_segment_manager_in_use = 1; + sm = segment_manager_get (app_wrk->first_segment_manager); + app_wrk->first_segment_manager_in_use = 1; return sm; } sm = segment_manager_new (); - sm->app_index = app->index; + sm->app_wrk_index = app_wrk->wrk_index; return sm; } @@ -441,8 +544,8 @@ application_alloc_segment_manager (application_t * app) * it's own specific listening connection. */ int -application_start_listen (application_t * srv, session_endpoint_t * sep, - session_handle_t * res) +app_worker_start_listen (app_worker_t * app_wrk, session_endpoint_t * sep, + session_handle_t * res) { segment_manager_t *sm; stream_session_t *s; @@ -451,22 +554,22 @@ application_start_listen (application_t * srv, session_endpoint_t * sep, sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4); s = listen_session_new (0, sst); - s->app_index = srv->index; + s->app_wrk_index = app_wrk->wrk_index; /* Allocate segment manager. All sessions derived out of a listen session * have fifos allocated by the same segment manager. */ - if (!(sm = application_alloc_segment_manager (srv))) + if (!(sm = application_alloc_segment_manager (app_wrk))) goto err; /* Add to app's listener table. Useful to find all child listeners * when app goes down, although, just for unbinding this is not needed */ handle = listen_session_get_handle (s); - hash_set (srv->listeners_table, handle, segment_manager_index (sm)); + hash_set (app_wrk->listeners_table, handle, segment_manager_index (sm)); if (stream_session_listen (s, sep)) { segment_manager_del (sm); - hash_unset (srv->listeners_table, handle); + hash_unset (app_wrk->listeners_table, handle); goto err; } @@ -480,83 +583,94 @@ err: /** * Stop listening on session associated to handle + * + * @param handle listener handle + * @param app_index index of the app owning the handle. This is used + * only for validating ownership */ int -application_stop_listen (application_t * srv, session_handle_t handle) +app_worker_stop_listen (session_handle_t handle, u32 app_index) { stream_session_t *listener; - uword *indexp; segment_manager_t *sm; + app_worker_t *app_wrk; + uword *indexp; - if (srv && hash_get (srv->listeners_table, handle) == 0) + listener = listen_session_get_from_handle (handle); + app_wrk = app_worker_get (listener->app_wrk_index); + if (PREDICT_FALSE (!app_wrk || app_wrk->app_index != app_index)) { clib_warning ("app doesn't own handle %llu!", handle); return -1; } + if (PREDICT_FALSE (hash_get (app_wrk->listeners_table, handle) == 0)) + { + clib_warning ("listener handle was removed %llu!", handle); + return -1; + } - listener = listen_session_get_from_handle (handle); stream_session_stop_listen (listener); - indexp = hash_get (srv->listeners_table, handle); + indexp = hash_get (app_wrk->listeners_table, handle); ASSERT (indexp); sm = segment_manager_get (*indexp); - if (srv->first_segment_manager == *indexp) + if (app_wrk->first_segment_manager == *indexp) { /* Delete sessions but don't remove segment manager */ - srv->first_segment_manager_in_use = 0; + app_wrk->first_segment_manager_in_use = 0; segment_manager_del_sessions (sm); } else { segment_manager_init_del (sm); } - hash_unset (srv->listeners_table, handle); + hash_unset (app_wrk->listeners_table, handle); listen_session_del (listener); return 0; } int -application_open_session (application_t * app, session_endpoint_t * sep, - u32 api_context) +app_worker_open_session (app_worker_t * app, session_endpoint_t * sep, + u32 api_context) { int rv; /* Make sure we have a segment manager for connects */ - application_alloc_connects_segment_manager (app); + app_worker_alloc_connects_segment_manager (app); - if ((rv = session_open (app->index, sep, api_context))) + if ((rv = session_open (app->wrk_index, sep, api_context))) return rv; return 0; } int -application_alloc_connects_segment_manager (application_t * app) +app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk) { segment_manager_t *sm; - if (app->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX) + if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX) { - sm = application_alloc_segment_manager (app); + sm = application_alloc_segment_manager (app_wrk); if (sm == 0) return -1; - app->connects_seg_manager = segment_manager_index (sm); + app_wrk->connects_seg_manager = segment_manager_index (sm); } return 0; } segment_manager_t * -application_get_connect_segment_manager (application_t * app) +app_worker_get_connect_segment_manager (app_worker_t * app) { ASSERT (app->connects_seg_manager != (u32) ~ 0); return segment_manager_get (app->connects_seg_manager); } segment_manager_t * -application_get_listen_segment_manager (application_t * app, - stream_session_t * s) +app_worker_get_listen_segment_manager (app_worker_t * app, + stream_session_t * s) { uword *smp; smp = hash_get (app->listeners_table, listen_session_get_handle (s)); @@ -564,21 +678,63 @@ application_get_listen_segment_manager (application_t * app, return segment_manager_get (*smp); } +clib_error_t * +vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a) +{ + svm_fifo_segment_private_t *fs; + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; + segment_manager_t *sm; + application_t *app; + int rv; + + app = application_get (a->app_index); + if (!app) + return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, + "App %u does not exist", a->app_index); + + if (a->is_add) + { + if ((rv = app_worker_alloc_and_init (app, &app_wrk))) + return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv); + sm = segment_manager_get (app_wrk->first_segment_manager); + fs = segment_manager_get_segment_w_lock (sm, 0); + a->segment = &fs->ssvm; + segment_manager_segment_reader_unlock (sm); + a->evt_q = app_wrk->event_queue; + } + else + { + wrk_map = app_worker_map_get (app, a->wrk_index); + if (!wrk_map) + return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, + "App %u does not have worker %u", + app->app_index, a->wrk_index); + app_wrk = app_worker_get (wrk_map->wrk_index); + app_worker_map_free (app, wrk_map); + if (!app_wrk) + return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, + "No worker %u", a->wrk_index); + app_worker_free (app_wrk); + } + return 0; +} + segment_manager_t * -application_get_local_segment_manager (application_t * app) +application_get_local_segment_manager (app_worker_t * app) { return segment_manager_get (app->local_segment_manager); } segment_manager_t * -application_get_local_segment_manager_w_session (application_t * app, +application_get_local_segment_manager_w_session (app_worker_t * app, local_session_t * ls) { stream_session_t *listener; if (application_local_session_listener_has_transport (ls)) { listener = listen_session_get (ls->listener_index); - return application_get_listen_segment_manager (app, listener); + return app_worker_get_listen_segment_manager (app, listener); } return segment_manager_get (app->local_segment_manager); } @@ -601,16 +757,6 @@ application_is_builtin_proxy (application_t * app) return (application_is_proxy (app) && application_is_builtin (app)); } -/** - * Send an API message to the external app, to map new segment - */ -int -application_add_segment_notify (u32 app_index, ssvm_private_t * fs) -{ - application_t *app = application_get (app_index); - return app->cb_fns.add_segment_callback (app->api_client_index, fs); -} - u8 application_has_local_scope (application_t * app) { @@ -623,15 +769,26 @@ application_has_global_scope (application_t * app) return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; } +/** + * Send an API message to the external app, to map new segment + */ +int +app_worker_add_segment_notify (u32 app_wrk_index, ssvm_private_t * fs) +{ + app_worker_t *app_wrk = app_worker_get (app_wrk_index); + application_t *app = application_get (app_wrk->app_index); + return app->cb_fns.add_segment_callback (app->api_client_index, fs); +} + u32 -application_n_listeners (application_t * app) +application_n_listeners (app_worker_t * app) { return hash_elts (app->listeners_table); } stream_session_t * -application_first_listener (application_t * app, u8 fib_proto, - u8 transport_proto) +app_worker_first_listener (app_worker_t * app, u8 fib_proto, + u8 transport_proto) { stream_session_t *listener; u64 handle; @@ -653,8 +810,14 @@ application_first_listener (application_t * app, u8 fib_proto, return 0; } +u8 +app_worker_application_is_builtin (app_worker_t * app_wrk) +{ + return app_wrk->app_is_builtin; +} + stream_session_t * -application_proxy_listener (application_t * app, u8 fib_proto, +application_proxy_listener (app_worker_t * app, u8 fib_proto, u8 transport_proto) { stream_session_t *listener; @@ -685,26 +848,29 @@ application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto, u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4); session_endpoint_t sep = SESSION_ENDPOINT_NULL; transport_connection_t *tc; + app_worker_t *app_wrk; stream_session_t *s; u64 handle; + /* TODO decide if we want proxy to be enabled for all workers */ + app_wrk = application_get_default_worker (app); if (is_start) { - s = application_first_listener (app, fib_proto, transport_proto); + s = app_worker_first_listener (app_wrk, fib_proto, transport_proto); if (!s) { sep.is_ip4 = is_ip4; sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); sep.sw_if_index = app_ns->sw_if_index; sep.transport_proto = transport_proto; - application_start_listen (app, &sep, &handle); + app_worker_start_listen (app_wrk, &sep, &handle); s = listen_session_get_from_handle (handle); s->listener_index = SESSION_PROXY_LISTENER_INDEX; } } else { - s = application_proxy_listener (app, fib_proto, transport_proto); + s = application_proxy_listener (app_wrk, fib_proto, transport_proto); ASSERT (s); } @@ -741,10 +907,10 @@ application_start_stop_proxy_local_scope (application_t * app, if (is_start) { session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, - app->index); + app->app_index); sep.is_ip4 = 0; session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, - app->index); + app->app_index); } else { @@ -843,7 +1009,7 @@ app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock) } static inline int -app_send_io_evt_rx (application_t * app, stream_session_t * s, u8 lock) +app_send_io_evt_rx (app_worker_t * app_wrk, stream_session_t * s, u8 lock) { session_event_t *evt; svm_msg_q_msg_t msg; @@ -858,14 +1024,17 @@ app_send_io_evt_rx (application_t * app, stream_session_t * s, u8 lock) return 0; } - if (app->cb_fns.builtin_app_rx_callback) - return app->cb_fns.builtin_app_rx_callback (s); + if (app_worker_application_is_builtin (app_wrk)) + { + application_t *app = application_get (app_wrk->app_index); + return app->cb_fns.builtin_app_rx_callback (s); + } if (svm_fifo_has_event (s->server_rx_fifo) || svm_fifo_is_empty (s->server_rx_fifo)) return 0; - mq = app->event_queue; + mq = app_wrk->event_queue; if (lock) svm_msg_q_lock (mq); @@ -891,16 +1060,16 @@ app_send_io_evt_rx (application_t * app, stream_session_t * s, u8 lock) } static inline int -app_send_io_evt_tx (application_t * app, stream_session_t * s, u8 lock) +app_send_io_evt_tx (app_worker_t * app_wrk, stream_session_t * s, u8 lock) { svm_msg_q_t *mq; session_event_t *evt; svm_msg_q_msg_t msg; - if (application_is_builtin (app)) + if (app_worker_application_is_builtin (app_wrk)) return 0; - mq = app->event_queue; + mq = app_wrk->event_queue; if (lock) svm_msg_q_lock (mq); @@ -923,7 +1092,7 @@ app_send_io_evt_tx (application_t * app, stream_session_t * s, u8 lock) } /* *INDENT-OFF* */ -typedef int (app_send_evt_handler_fn) (application_t *app, +typedef int (app_send_evt_handler_fn) (app_worker_t *app, stream_session_t *s, u8 lock); static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = { @@ -941,34 +1110,33 @@ static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = { * flag is set, we do wait for queue mutex. */ int -application_send_event (application_t * app, stream_session_t * s, - u8 evt_type) +application_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type) { ASSERT (app && evt_type <= FIFO_EVENT_APP_TX); return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ ); } int -application_lock_and_send_event (application_t * app, stream_session_t * s, +application_lock_and_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type) { return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ ); } local_session_t * -application_alloc_local_session (application_t * app) +application_alloc_local_session (app_worker_t * app) { local_session_t *s; pool_get (app->local_sessions, s); memset (s, 0, sizeof (*s)); - s->app_index = app->index; + s->app_wrk_index = app->app_index; s->session_index = s - app->local_sessions; s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0); return s; } void -application_free_local_session (application_t * app, local_session_t * s) +application_free_local_session (app_worker_t * app, local_session_t * s) { pool_put (app->local_sessions, s); if (CLIB_DEBUG) @@ -976,23 +1144,23 @@ application_free_local_session (application_t * app, local_session_t * s) } local_session_t * -application_get_local_session (application_t * app, u32 session_index) +application_get_local_session (app_worker_t * app_wrk, u32 session_index) { - if (pool_is_free_index (app->local_sessions, session_index)) + if (pool_is_free_index (app_wrk->local_sessions, session_index)) return 0; - return pool_elt_at_index (app->local_sessions, session_index); + return pool_elt_at_index (app_wrk->local_sessions, session_index); } local_session_t * application_get_local_session_from_handle (session_handle_t handle) { - application_t *server; - u32 session_index, server_index; - local_session_parse_handle (handle, &server_index, &session_index); - server = application_get_if_valid (server_index); - if (!server) + app_worker_t *server_wrk; + u32 session_index, server_wrk_index; + local_session_parse_handle (handle, &server_wrk_index, &session_index); + server_wrk = app_worker_get_if_valid (server_wrk_index); + if (!server_wrk) return 0; - return application_get_local_session (server, session_index); + return application_get_local_session (server_wrk, session_index); } always_inline void @@ -1006,26 +1174,28 @@ application_local_listener_session_endpoint (local_session_t * ll, } int -application_start_local_listen (application_t * server, +application_start_local_listen (app_worker_t * app_wrk, session_endpoint_t * sep, session_handle_t * handle) { session_handle_t lh; local_session_t *ll; + application_t *app; u32 table_index; - table_index = application_local_session_table (server); + app = application_get (app_wrk->app_index); + table_index = application_local_session_table (app); /* An exact sep match, as opposed to session_lookup_local_listener */ lh = session_lookup_endpoint_listener (table_index, sep, 1); if (lh != SESSION_INVALID_HANDLE) return VNET_API_ERROR_ADDRESS_IN_USE; - pool_get (server->local_listen_sessions, ll); + pool_get (app_wrk->local_listen_sessions, ll); memset (ll, 0, sizeof (*ll)); ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0); - ll->app_index = server->index; - ll->session_index = ll - server->local_listen_sessions; + ll->app_wrk_index = app_wrk->app_index; + ll->session_index = ll - app_wrk->local_listen_sessions; ll->port = sep->port; /* Store the original session type for the unbind */ ll->listener_session_type = @@ -1044,13 +1214,16 @@ application_start_local_listen (application_t * server, * so parse it. */ int -application_stop_local_listen (application_t * server, session_handle_t lh) +application_stop_local_listen (session_handle_t lh, u32 app_index) { session_endpoint_t sep = SESSION_ENDPOINT_NULL; - u32 table_index, ll_index, server_index; + u32 table_index, ll_index, server_wrk_index; + app_worker_t *server_wrk; stream_session_t *sl = 0; local_session_t *ll, *ls; + application_t *server; + server = application_get (app_index); table_index = application_local_session_table (server); /* We have both local and global table binds. Figure from global what @@ -1069,9 +1242,14 @@ application_stop_local_listen (application_t * server, session_handle_t lh) return -1; } - local_session_parse_handle (lh, &server_index, &ll_index); - ASSERT (server->index == server_index); - if (!(ll = application_get_local_listen_session (server, ll_index))) + local_session_parse_handle (lh, &server_wrk_index, &ll_index); + server_wrk = app_worker_get (server_wrk_index); + if (PREDICT_FALSE (server_wrk->app_index != app_index)) + { + clib_warning ("app %u does not own local handle 0x%lx", app_index, lh); + } + ll = application_get_local_listen_session (server_wrk, ll_index); + if (PREDICT_FALSE (!ll)) { clib_warning ("no local listener"); return -1; @@ -1080,12 +1258,12 @@ application_stop_local_listen (application_t * server, session_handle_t lh) session_lookup_del_session_endpoint (table_index, &sep); /* *INDENT-OFF* */ - pool_foreach (ls, server->local_sessions, ({ + pool_foreach (ls, server_wrk->local_sessions, ({ if (ls->listener_index == ll->session_index) - application_local_session_disconnect (server->index, ls); + application_local_session_disconnect (server_wrk->app_index, ls); })); /* *INDENT-ON* */ - pool_put_index (server->local_listen_sessions, ll->session_index); + pool_put_index (server_wrk->local_listen_sessions, ll->session_index); return 0; } @@ -1108,8 +1286,8 @@ application_local_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq) } int -application_local_session_connect (u32 table_index, application_t * client, - application_t * server, +application_local_session_connect (app_worker_t * client_wrk, + app_worker_t * server_wrk, local_session_t * ll, u32 opaque) { u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10; @@ -1117,11 +1295,15 @@ application_local_session_connect (u32 table_index, application_t * client, u32 round_rx_fifo_sz, round_tx_fifo_sz; int rv, has_transport, seg_index; svm_fifo_segment_private_t *seg; + application_t *server, *client; segment_manager_t *sm; local_session_t *ls; svm_msg_q_t *sq, *cq; - ls = application_alloc_local_session (server); + ls = application_alloc_local_session (server_wrk); + + server = application_get (server_wrk->app_index); + client = application_get (client_wrk->app_index); props = application_segment_manager_properties (server); cprops = application_segment_manager_properties (client); @@ -1136,7 +1318,7 @@ application_local_session_connect (u32 table_index, application_t * client, { /* Local sessions don't have backing transport */ ls->port = ll->port; - sm = application_get_local_segment_manager (server); + sm = application_get_local_segment_manager (server_wrk); } else { @@ -1144,7 +1326,7 @@ application_local_session_connect (u32 table_index, application_t * client, transport_connection_t *tc; tc = listen_session_get_transport (sl); ls->port = tc->lcl_port; - sm = application_get_listen_segment_manager (server, sl); + sm = app_worker_get_listen_segment_manager (server_wrk, sl); } seg_index = segment_manager_add_segment (sm, seg_size); @@ -1178,7 +1360,7 @@ application_local_session_connect (u32 table_index, application_t * client, ls->server_tx_fifo->master_thread_index = ~0; ls->svm_segment_index = seg_index; ls->listener_index = ll->session_index; - ls->client_index = client->index; + ls->client_wrk_index = client_wrk->wrk_index; ls->client_opaque = opaque; ls->listener_session_type = ll->session_type; @@ -1209,14 +1391,14 @@ failed: static uword application_client_local_connect_key (local_session_t * ls) { - return ((uword) ls->app_index << 32 | (uword) ls->session_index); + return ((uword) ls->app_wrk_index << 32 | (uword) ls->session_index); } static void -application_client_local_connect_key_parse (uword key, u32 * app_index, +application_client_local_connect_key_parse (uword key, u32 * app_wrk_index, u32 * session_index) { - *app_index = key >> 32; + *app_wrk_index = key >> 32; *session_index = key & 0xFFFFFFFF; } @@ -1224,22 +1406,25 @@ int application_local_session_connect_notify (local_session_t * ls) { svm_fifo_segment_private_t *seg; - application_t *client, *server; + app_worker_t *client_wrk, *server_wrk; segment_manager_t *sm; + application_t *client; int rv, is_fail = 0; uword client_key; - client = application_get (ls->client_index); - server = application_get (ls->app_index); - sm = application_get_local_segment_manager_w_session (server, ls); + client_wrk = app_worker_get (ls->client_wrk_index); + server_wrk = app_worker_get (ls->app_wrk_index); + client = application_get (client_wrk->app_index); + + sm = application_get_local_segment_manager_w_session (server_wrk, ls); seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index); if ((rv = client->cb_fns.add_segment_callback (client->api_client_index, &seg->ssvm))) { clib_warning ("failed to notify client %u of new segment", - ls->client_index); + ls->client_wrk_index); segment_manager_segment_reader_unlock (sm); - application_local_session_disconnect (ls->client_index, ls); + application_local_session_disconnect (ls->client_wrk_index, ls); is_fail = 1; } else @@ -1247,18 +1432,19 @@ application_local_session_connect_notify (local_session_t * ls) segment_manager_segment_reader_unlock (sm); } - client->cb_fns.session_connected_callback (client->index, ls->client_opaque, + client->cb_fns.session_connected_callback (client_wrk->wrk_index, + ls->client_opaque, (stream_session_t *) ls, is_fail); client_key = application_client_local_connect_key (ls); - hash_set (client->local_connects, client_key, client_key); + hash_set (client_wrk->local_connects, client_key, client_key); return 0; } int -application_local_session_cleanup (application_t * client, - application_t * server, +application_local_session_cleanup (app_worker_t * client_wrk, + app_worker_t * server_wrk, local_session_t * ls) { svm_fifo_segment_private_t *seg; @@ -1269,63 +1455,71 @@ application_local_session_cleanup (application_t * client, has_transport = session_has_transport ((stream_session_t *) ls); client_key = application_client_local_connect_key (ls); if (!has_transport) - sm = application_get_local_segment_manager_w_session (server, ls); + sm = application_get_local_segment_manager_w_session (server_wrk, ls); else - sm = application_get_listen_segment_manager (server, - (stream_session_t *) ls); + sm = app_worker_get_listen_segment_manager (server_wrk, + (stream_session_t *) ls); seg = segment_manager_get_segment (sm, ls->svm_segment_index); - if (client) - hash_unset (client->local_connects, client_key); + if (client_wrk) + hash_unset (client_wrk->local_connects, client_key); if (!has_transport) { + application_t *server = application_get (server_wrk->app_index); server->cb_fns.del_segment_callback (server->api_client_index, &seg->ssvm); - if (client) - client->cb_fns.del_segment_callback (client->api_client_index, - &seg->ssvm); + if (client_wrk) + { + application_t *client = application_get (client_wrk->app_index); + client->cb_fns.del_segment_callback (client->api_client_index, + &seg->ssvm); + } segment_manager_del_segment (sm, seg); } - application_free_local_session (server, ls); + application_free_local_session (server_wrk, ls); return 0; } int -application_local_session_disconnect (u32 app_index, local_session_t * ls) +application_local_session_disconnect (u32 app_wrk_index, local_session_t * ls) { - application_t *client, *server; + app_worker_t *client_wrk, *server_wrk; + + client_wrk = app_worker_get_if_valid (ls->client_wrk_index); + server_wrk = app_worker_get (ls->app_wrk_index); - client = application_get_if_valid (ls->client_index); - server = application_get (ls->app_index); if (ls->session_state == SESSION_STATE_CLOSED) - return application_local_session_cleanup (client, server, ls); + return application_local_session_cleanup (client_wrk, server_wrk, ls); - if (app_index == ls->client_index) + if (app_wrk_index == ls->client_wrk_index) { - mq_send_local_session_disconnected_cb (ls->app_index, ls); + mq_send_local_session_disconnected_cb (ls->app_wrk_index, ls); } else { - if (!client) + if (!client_wrk) { - return application_local_session_cleanup (client, server, ls); + return application_local_session_cleanup (client_wrk, server_wrk, + ls); } else if (ls->session_state < SESSION_STATE_READY) { - client->cb_fns.session_connected_callback (client->index, + application_t *client = application_get (client_wrk->app_index); + client->cb_fns.session_connected_callback (client_wrk->wrk_index, ls->client_opaque, (stream_session_t *) ls, 1 /* is_fail */ ); ls->session_state = SESSION_STATE_CLOSED; - return application_local_session_cleanup (client, server, ls); + return application_local_session_cleanup (client_wrk, server_wrk, + ls); } else { - mq_send_local_session_disconnected_cb (client->index, ls); + mq_send_local_session_disconnected_cb (client_wrk->wrk_index, ls); } } @@ -1335,32 +1529,34 @@ application_local_session_disconnect (u32 app_index, local_session_t * ls) } int -application_local_session_disconnect_w_index (u32 app_index, u32 ls_index) +application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index) { - application_t *app; + app_worker_t *app_wrk; local_session_t *ls; - app = application_get (app_index); - ls = application_get_local_session (app, ls_index); - return application_local_session_disconnect (app_index, ls); + app_wrk = app_worker_get (app_wrk_index); + ls = application_get_local_session (app_wrk, ls_index); + return application_local_session_disconnect (app_wrk_index, ls); } void -application_local_sessions_del (application_t * app) +application_local_sessions_free (app_worker_t * app_wrk) { - u32 index, server_index, session_index, table_index; + u32 index, server_wrk_index, session_index, table_index; segment_manager_t *sm; u64 handle, *handles = 0; local_session_t *ls, *ll; - application_t *server; + app_worker_t *server_wrk; session_endpoint_t sep; + application_t *app; int i; /* * Local listens. Don't bother with local sessions, we clean them lower */ + app = application_get (app_wrk->app_index); table_index = application_local_session_table (app); /* *INDENT-OFF* */ - pool_foreach (ll, app->local_listen_sessions, ({ + pool_foreach (ll, app_wrk->local_listen_sessions, ({ application_local_listener_session_endpoint (ll, &sep); session_lookup_del_session_endpoint (table_index, &sep); })); @@ -1369,11 +1565,11 @@ application_local_sessions_del (application_t * app) /* * Local sessions */ - if (app->local_sessions) + if (app_wrk->local_sessions) { /* *INDENT-OFF* */ - pool_foreach (ls, app->local_sessions, ({ - application_local_session_disconnect (app->index, ls); + pool_foreach (ls, app_wrk->local_sessions, ({ + application_local_session_disconnect (app_wrk->wrk_index, ls); })); /* *INDENT-ON* */ } @@ -1383,25 +1579,26 @@ application_local_sessions_del (application_t * app) */ vec_reset_length (handles); /* *INDENT-OFF* */ - hash_foreach (handle, index, app->local_connects, ({ + hash_foreach (handle, index, app_wrk->local_connects, ({ vec_add1 (handles, handle); })); /* *INDENT-ON* */ for (i = 0; i < vec_len (handles); i++) { - application_client_local_connect_key_parse (handles[i], &server_index, + application_client_local_connect_key_parse (handles[i], + &server_wrk_index, &session_index); - server = application_get_if_valid (server_index); - if (server) + server_wrk = app_worker_get_if_valid (server_wrk_index); + if (server_wrk) { - ls = application_get_local_session (server, session_index); - application_local_session_disconnect (app->index, ls); + ls = application_get_local_session (server_wrk, session_index); + application_local_session_disconnect (app_wrk->wrk_index, ls); } } - sm = segment_manager_get (app->local_segment_manager); - sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX; + sm = segment_manager_get (app_wrk->local_segment_manager); + sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; segment_manager_del (sm); } @@ -1430,52 +1627,84 @@ vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a) } u8 * -format_application_listener (u8 * s, va_list * args) +format_app_worker_listener (u8 * s, va_list * args) { - application_t *app = va_arg (*args, application_t *); + app_worker_t *app_wrk = va_arg (*args, app_worker_t *); u64 handle = va_arg (*args, u64); u32 sm_index = va_arg (*args, u32); int verbose = va_arg (*args, int); stream_session_t *listener; + application_t *app; u8 *app_name, *str; - if (app == 0) + if (!app_wrk) { if (verbose) - s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App", + s = format (s, "%-40s%-25s%-15s%-15s%-10s", "Connection", "App", "API Client", "ListenerID", "SegManager"); else - s = format (s, "%-40s%-20s", "Connection", "App"); + s = format (s, "%-40s%-25s", "Connection", "App"); return s; } + app = application_get (app_wrk->app_index); app_name = app_get_name_from_reg_index (app); listener = listen_session_get_from_handle (handle); str = format (0, "%U", format_stream_session, listener, verbose); if (verbose) { - s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name, + s = format (s, "%-40s%-25s%-15u%-15u%-10u", str, app_name, app->api_client_index, handle, sm_index); } else - s = format (s, "%-40s%-20s", str, app_name); + s = format (s, "%-40s%-25s", str, app_name); vec_free (app_name); return s; } -void -application_format_connects (application_t * app, int verbose) +static void +application_format_listeners (application_t * app, int verbose) +{ + vlib_main_t *vm = vlib_get_main (); + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; + u32 sm_index; + u64 handle; + + if (!app) + { + vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ , + 0, 0, verbose); + return; + } + + /* *INDENT-OFF* */ + pool_foreach (wrk_map, app->worker_maps, ({ + app_wrk = app_worker_get (wrk_map->wrk_index); + if (hash_elts (app_wrk->listeners_table) == 0) + continue; + hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ + vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk, + handle, sm_index, verbose); + })); + })); + /* *INDENT-ON* */ +} + +static void +app_worker_format_connects (app_worker_t * app_wrk, int verbose) { svm_fifo_segment_private_t *fifo_segment; vlib_main_t *vm = vlib_get_main (); segment_manager_t *sm; u8 *app_name, *s = 0; + application_t *app; /* Header */ - if (app == 0) + if (!app_wrk) { if (verbose) vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App", @@ -1485,14 +1714,14 @@ application_format_connects (application_t * app, int verbose) return; } - /* make sure */ - if (app->connects_seg_manager == (u32) ~ 0) + app = application_get (app_wrk->app_index); + if (app_wrk->connects_seg_manager == (u32) ~ 0) return; app_name = app_get_name_from_reg_index (app); /* Across all fifo segments */ - sm = segment_manager_get (app->connects_seg_manager); + sm = segment_manager_get (app_wrk->connects_seg_manager); /* *INDENT-OFF* */ segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({ @@ -1513,7 +1742,7 @@ application_format_connects (application_t * app, int verbose) if (verbose) s = format (s, "%-40s%-20s%-15u%-10u", str, app_name, - app->api_client_index, app->connects_seg_manager); + app->api_client_index, app_wrk->connects_seg_manager); else s = format (s, "%-40s%-20s", str, app_name); @@ -1530,8 +1759,28 @@ application_format_connects (application_t * app, int verbose) vec_free (app_name); } -void -application_format_local_sessions (application_t * app, int verbose) +static void +application_format_connects (application_t * app, int verbose) +{ + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; + + if (!app) + { + app_worker_format_connects (0, verbose); + return; + } + + /* *INDENT-OFF* */ + pool_foreach (wrk_map, app->worker_maps, ({ + app_wrk = app_worker_get (wrk_map->wrk_index); + app_worker_format_connects (app_wrk, verbose); + })); + /* *INDENT-ON* */ +} + +static void +app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose) { vlib_main_t *vm = vlib_get_main (); local_session_t *ls; @@ -1539,27 +1788,31 @@ application_format_local_sessions (application_t * app, int verbose) u8 *conn = 0; /* Header */ - if (app == 0) + if (app_wrk == 0) { vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp", "ClientApp"); return; } + if (!pool_elts (app_wrk->local_sessions) + && !pool_elts (app_wrk->local_connects)) + return; + /* *INDENT-OFF* */ - pool_foreach (ls, app->local_listen_sessions, ({ + pool_foreach (ls, app_wrk->local_listen_sessions, ({ tp = session_type_transport_proto(ls->listener_session_type); conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp, ls->port); - vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_index, "*"); + vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*"); vec_reset_length (conn); })); - pool_foreach (ls, app->local_sessions, ({ + pool_foreach (ls, app_wrk->local_sessions, ({ tp = session_type_transport_proto(ls->listener_session_type); conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp, ls->port); - vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_index, - ls->client_index); + vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_wrk_index, + ls->client_wrk_index); vec_reset_length (conn); })); /* *INDENT-ON* */ @@ -1567,12 +1820,32 @@ application_format_local_sessions (application_t * app, int verbose) vec_free (conn); } -void -application_format_local_connects (application_t * app, int verbose) +static void +application_format_local_sessions (application_t * app, int verbose) +{ + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; + + if (!app) + { + app_worker_format_local_sessions (0, verbose); + return; + } + + /* *INDENT-OFF* */ + pool_foreach (wrk_map, app->worker_maps, ({ + app_wrk = app_worker_get (wrk_map->wrk_index); + app_worker_format_local_sessions (app_wrk, verbose); + })); + /* *INDENT-ON* */ +} + +static void +app_worker_format_local_connects (app_worker_t * app, int verbose) { vlib_main_t *vm = vlib_get_main (); - u32 app_index, session_index; - application_t *server; + u32 app_wrk_index, session_index; + app_worker_t *server_wrk; local_session_t *ls; uword client_key; u64 value; @@ -1589,13 +1862,37 @@ application_format_local_connects (application_t * app, int verbose) return; } + if (!app->local_connects) + return; + /* *INDENT-OFF* */ hash_foreach (client_key, value, app->local_connects, ({ - application_client_local_connect_key_parse (client_key, &app_index, + application_client_local_connect_key_parse (client_key, &app_wrk_index, &session_index); - server = application_get (app_index); - ls = application_get_local_session (server, session_index); - vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_index, ls->client_index); + server_wrk = app_worker_get (app_wrk_index); + ls = application_get_local_session (server_wrk, session_index); + vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_wrk_index, + ls->client_wrk_index); + })); + /* *INDENT-ON* */ +} + +static void +application_format_local_connects (application_t * app, int verbose) +{ + app_worker_map_t *wrk_map; + app_worker_t *app_wrk; + + if (!app) + { + app_worker_format_local_connects (0, verbose); + return; + } + + /* *INDENT-OFF* */ + pool_foreach (wrk_map, app->worker_maps, ({ + app_wrk = app_worker_get (wrk_map->wrk_index); + app_worker_format_local_connects (app_wrk, verbose); })); /* *INDENT-ON* */ } @@ -1625,26 +1922,23 @@ format_application (u8 * s, va_list * args) app_ns_name = app_namespace_id_from_index (app->ns_index); props = application_segment_manager_properties (app); if (verbose) - s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->index, + s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->app_index, app_name, app->api_client_index, app->ns_index, format_memory_size, props->add_segment_size, format_memory_size, props->rx_fifo_size, format_memory_size, props->tx_fifo_size); else - s = format (s, "%-10u%-20s%-15d%-40s", app->index, app_name, + s = format (s, "%-10u%-20s%-15d%-40s", app->app_index, app_name, app->api_client_index, app_ns_name); return s; } - void application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose) { application_t *app; - u32 sm_index; - u64 handle; - if (!pool_elts (app_pool)) + if (!pool_elts (app_main.app_pool)) { vlib_cli_output (vm, "No active server bindings"); return; @@ -1654,27 +1948,18 @@ application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose) { application_format_local_sessions (0, verbose); /* *INDENT-OFF* */ - pool_foreach (app, app_pool, ({ - if (!pool_elts (app->local_sessions) - && !pool_elts(app->local_connects)) - continue; + pool_foreach (app, app_main.app_pool, ({ application_format_local_sessions (app, verbose); })); /* *INDENT-ON* */ } else { - vlib_cli_output (vm, "%U", format_application_listener, 0 /* header */ , - 0, 0, verbose); + application_format_listeners (0, verbose); /* *INDENT-OFF* */ - pool_foreach (app, app_pool, ({ - if (hash_elts (app->listeners_table) == 0) - continue; - hash_foreach (handle, sm_index, app->listeners_table, ({ - vlib_cli_output (vm, "%U", format_application_listener, app, - handle, sm_index, verbose); - })); + pool_foreach (app, app_main.app_pool, ({ + application_format_listeners (app, verbose); })); /* *INDENT-ON* */ } @@ -1685,7 +1970,7 @@ application_format_all_clients (vlib_main_t * vm, int do_local, int verbose) { application_t *app; - if (!pool_elts (app_pool)) + if (!pool_elts (app_main.app_pool)) { vlib_cli_output (vm, "No active apps"); return; @@ -1696,9 +1981,8 @@ application_format_all_clients (vlib_main_t * vm, int do_local, int verbose) application_format_local_connects (0, verbose); /* *INDENT-OFF* */ - pool_foreach (app, app_pool, ({ - if (app->local_connects) - application_format_local_connects (app, verbose); + pool_foreach (app, app_main.app_pool, ({ + application_format_local_connects (app, verbose); })); /* *INDENT-ON* */ } @@ -1707,9 +1991,7 @@ application_format_all_clients (vlib_main_t * vm, int do_local, int verbose) application_format_connects (0, verbose); /* *INDENT-OFF* */ - pool_foreach (app, app_pool, ({ - if (app->connects_seg_manager == (u32)~0) - continue; + pool_foreach (app, app_main.app_pool, ({ application_format_connects (app, verbose); })); /* *INDENT-ON* */ @@ -1751,7 +2033,7 @@ show_app_command_fn (vlib_main_t * vm, unformat_input_t * input, { vlib_cli_output (vm, "%U", format_application, 0, verbose); /* *INDENT-OFF* */ - pool_foreach (app, app_pool, ({ + pool_foreach (app, app_main.app_pool, ({ vlib_cli_output (vm, "%U", format_application, app, verbose); })); /* *INDENT-ON* */ diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h index 22df21e6140..cf855660ca5 100644 --- a/src/vnet/session/application.h +++ b/src/vnet/session/application.h @@ -20,6 +20,14 @@ #include <vnet/session/segment_manager.h> #include <vnet/session/application_namespace.h> +#define APP_DEBUG 0 + +#if APP_DEBUG > 0 +#define APP_DBG(_fmt, _args...) clib_warning (_fmt, ##_args) +#else +#define APP_DBG(_fmt, _args...) +#endif + typedef struct _stream_session_cb_vft { /** Notify server of new segment */ @@ -33,7 +41,7 @@ typedef struct _stream_session_cb_vft int (*session_accept_callback) (stream_session_t * new_session); /** Connection request callback */ - int (*session_connected_callback) (u32 app_index, u32 opaque, + int (*session_connected_callback) (u32 app_wrk_index, u32 opaque, stream_session_t * s, u8 code); /** Notify app that session is closing */ @@ -50,39 +58,20 @@ typedef struct _stream_session_cb_vft } session_cb_vft_t; -typedef struct _application +typedef struct app_worker_ { - /** Index in server pool */ - u32 index; - - /** Flags */ - u32 flags; + /** Worker index in global worker pool*/ + u32 wrk_index; - /** Name registered by builtin apps */ - u8 *name; + /** Worker index in app's map pool */ + u32 wrk_map_index; - /* - * Binary API interface to external app - */ - - /** Binary API connection index, ~0 if internal */ - u32 api_client_index; - - /** Namespace the application belongs to */ - u32 ns_index; + /** Index of owning app */ + u32 app_index; /** Application listens for events on this svm queue */ svm_msg_q_t *event_queue; - /* - * Callbacks: shoulder-taps for the server/client - */ - - session_cb_vft_t cb_fns; - - /* - * ssvm (fifo) segment management - */ /** Segment manager used for outgoing connects issued by the app */ u32 connects_seg_manager; @@ -97,11 +86,6 @@ typedef struct _application u32 first_segment_manager; u8 first_segment_manager_in_use; - /** Segment manager properties. Shared by all segment managers */ - segment_manager_properties_t sm_properties; - - u16 proxied_transports; - /* * Local "cut through" connections specific */ @@ -118,6 +102,42 @@ typedef struct _application /** Hash table of the app's local connects */ uword *local_connects; + u8 app_is_builtin; +} app_worker_t; + +typedef struct app_worker_map_ +{ + u32 wrk_index; +} app_worker_map_t; + +typedef struct application_ +{ + /** App index in app pool */ + u32 app_index; + + /** Binary API connection index, ~0 if internal */ + u32 api_client_index; + + /** Flags */ + u32 flags; + + /** Callbacks: shoulder-taps for the server/client */ + session_cb_vft_t cb_fns; + + /** Segment manager properties. Shared by all segment managers */ + segment_manager_properties_t sm_properties; + + /** Pool of mappings that keep track of workers associated to this app */ + app_worker_map_t *worker_maps; + + /** Name registered by builtin apps */ + u8 *name; + + /** Namespace the application belongs to */ + u32 ns_index; + + u16 proxied_transports; + /* * TLS Specific */ @@ -132,97 +152,145 @@ typedef struct _application u8 tls_engine; } application_t; +typedef struct app_main_ +{ + /** + * Pool from which we allocate all applications + */ + application_t *app_pool; + + /** + * Pool of workers associated to apps + */ + app_worker_t *workers; + + /** + * Hash table of apps by api client index + */ + uword *app_by_api_client_index; + + /** + * Hash table of builtin apps by name + */ + uword *app_by_name; +} app_main_t; + +#define foreach_app_init_args \ + _(u32, api_client_index) \ + _(u8 *, name) \ + _(u64 *, options) \ + _(u8 *, namespace_id) \ + _(session_cb_vft_t *, session_cb_vft) \ + _(u32, app_index) \ + +typedef struct app_init_args_ +{ +#define _(_type, _name) _type _name; + foreach_app_init_args +#undef _ +} app_init_args_t; + +typedef struct _vnet_app_worker_add_del_args +{ + u32 app_index; /**< App for which a new worker is requested */ + u32 wrk_index; /**< Index to delete or return value if add */ + ssvm_private_t *segment; /**< First segment in segment manager */ + svm_msg_q_t *evt_q; /**< Worker message queue */ + u8 is_add; /**< Flag set if addition */ +} vnet_app_worker_add_del_args_t; + #define APP_INVALID_INDEX ((u32)~0) #define APP_NS_INVALID_INDEX ((u32)~0) #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0) -application_t *application_new (); -int application_init (application_t * app, u32 api_client_index, - u8 * name, u64 * options, session_cb_vft_t * cb_fns); -void application_del (application_t * app); +app_worker_t *app_worker_alloc (application_t * app); +int app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk); +app_worker_t *app_worker_get (u32 wrk_index); +app_worker_t *app_worker_get_if_valid (u32 wrk_index); +void app_worker_free (app_worker_t * app_wrk); +int app_worker_start_listen (app_worker_t * app, + session_endpoint_t * tep, + session_handle_t * handle); +int app_worker_stop_listen (session_handle_t handle, u32 app_wrk_index); +int app_worker_open_session (app_worker_t * app, session_endpoint_t * tep, + u32 api_context); +segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *, + stream_session_t *); +segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *); +int app_worker_alloc_connects_segment_manager (app_worker_t * app); +int app_worker_add_segment_notify (u32 app_wrk_index, ssvm_private_t * fs); +u32 app_worker_n_listeners (app_worker_t * app); +stream_session_t *app_worker_first_listener (app_worker_t * app, + u8 fib_proto, + u8 transport_proto); +u8 app_worker_application_is_builtin (app_worker_t * app_wrk); +clib_error_t *vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a); + +application_t *application_alloc (void); +int application_alloc_and_init (app_init_args_t * args); +void application_free (application_t * app); application_t *application_get (u32 index); application_t *application_get_if_valid (u32 index); application_t *application_lookup (u32 api_client_index); application_t *application_lookup_name (const u8 * name); -u32 application_get_index (application_t * app); - -int application_start_listen (application_t * app, - session_endpoint_t * tep, - session_handle_t * handle); -int application_start_local_listen (application_t * server, - session_endpoint_t * sep, - session_handle_t * handle); -int application_stop_listen (application_t * srv, session_handle_t handle); -int application_stop_local_listen (application_t * server, - session_handle_t listener_handle); -int application_open_session (application_t * app, session_endpoint_t * tep, - u32 api_context); +u32 application_index (application_t * app); +app_worker_t *application_get_worker (application_t * app, u32 wrk_index); +app_worker_t *application_get_default_worker (application_t * app); int application_api_queue_is_full (application_t * app); -segment_manager_t *application_get_listen_segment_manager (application_t * - app, - stream_session_t * - ls); -segment_manager_t *application_get_connect_segment_manager (application_t * - app); -int application_alloc_connects_segment_manager (application_t * app); int application_is_proxy (application_t * app); int application_is_builtin (application_t * app); int application_is_builtin_proxy (application_t * app); -int application_add_segment_notify (u32 app_index, ssvm_private_t * fs); u32 application_session_table (application_t * app, u8 fib_proto); u32 application_local_session_table (application_t * app); -u8 *application_name_from_index (u32 app_index); - +u8 *application_name_from_index (u32 app_wrk_index); u8 application_has_local_scope (application_t * app); u8 application_has_global_scope (application_t * app); -u32 application_n_listeners (application_t * app); -stream_session_t *application_first_listener (application_t * app, - u8 fib_proto, - u8 transport_proto); void application_setup_proxy (application_t * app); void application_remove_proxy (application_t * app); segment_manager_properties_t *application_get_segment_manager_properties (u32 - app_index); + app_wrk_index); segment_manager_properties_t * application_segment_manager_properties (application_t * app); -local_session_t *application_alloc_local_session (application_t * app); -void application_free_local_session (application_t * app, +local_session_t *application_alloc_local_session (app_worker_t * app); +void application_free_local_session (app_worker_t * app, local_session_t * ls); -local_session_t *application_get_local_session (application_t * app, +local_session_t *application_get_local_session (app_worker_t * app, u32 session_index); local_session_t *application_get_local_session_from_handle (session_handle_t handle); -int application_local_session_connect (u32 table_index, - application_t * client, - application_t * server, +int application_start_local_listen (app_worker_t * server, + session_endpoint_t * sep, + session_handle_t * handle); +int application_stop_local_listen (session_handle_t lh, u32 app_wrk_index); +int application_local_session_connect (app_worker_t * client, + app_worker_t * server, local_session_t * ll, u32 opaque); int application_local_session_connect_notify (local_session_t * ls); -int application_local_session_disconnect (u32 app_index, +int application_local_session_disconnect (u32 app_wrk_index, local_session_t * ls); -int application_local_session_disconnect_w_index (u32 app_index, +int application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index); -void application_local_sessions_del (application_t * app); +void application_local_sessions_free (app_worker_t * app); -int application_send_event (application_t * app, stream_session_t * s, - u8 evt); -int application_lock_and_send_event (application_t * app, +int application_send_event (app_worker_t * app, stream_session_t * s, u8 evt); +int application_lock_and_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type); always_inline u32 local_session_id (local_session_t * ll) { - ASSERT (ll->app_index < (2 << 16) && ll->session_index < (2 << 16)); - return ((u32) ll->app_index << 16 | (u32) ll->session_index); + ASSERT (ll->app_wrk_index < (2 << 16) && ll->session_index < (2 << 16)); + return ((u32) ll->app_wrk_index << 16 | (u32) ll->session_index); } always_inline void -local_session_parse_id (u32 ls_id, u32 * app_index, u32 * session_index) +local_session_parse_id (u32 ls_id, u32 * app_wrk_index, u32 * session_index) { - *app_index = ls_id >> 16; + *app_wrk_index = ls_id >> 16; *session_index = ls_id & 0xFFF; } @@ -244,7 +312,7 @@ application_local_session_handle (local_session_t * ls) } always_inline local_session_t * -application_get_local_listen_session (application_t * app, u32 session_index) +application_get_local_listen_session (app_worker_t * app, u32 session_index) { return pool_elt_at_index (app->local_listen_sessions, session_index); } @@ -252,10 +320,10 @@ application_get_local_listen_session (application_t * app, u32 session_index) always_inline local_session_t * application_get_local_listener_w_handle (session_handle_t handle) { - u32 server_index, session_index; - application_t *app; - local_session_parse_handle (handle, &server_index, &session_index); - app = application_get (server_index); + u32 server_wrk_index, session_index; + app_worker_t *app; + local_session_parse_handle (handle, &server_wrk_index, &session_index); + app = app_worker_get (server_wrk_index); return application_get_local_listen_session (app, session_index); } @@ -267,12 +335,9 @@ application_local_session_listener_has_transport (local_session_t * ls) return (tp != TRANSPORT_PROTO_NONE); } -void mq_send_local_session_disconnected_cb (u32 app_index, +void mq_send_local_session_disconnected_cb (u32 app_wrk_index, local_session_t * ls); -int application_connect (u32 client_index, u32 api_context, - session_endpoint_t * sep); - uword unformat_application_proto (unformat_input_t * input, va_list * args); #endif /* SRC_VNET_SESSION_APPLICATION_H_ */ diff --git a/src/vnet/session/application_interface.c b/src/vnet/session/application_interface.c index 524e7b53663..f8d43d40882 100644 --- a/src/vnet/session/application_interface.c +++ b/src/vnet/session/application_interface.c @@ -148,27 +148,29 @@ session_endpoint_update_for_app (session_endpoint_t * sep, } static int -vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle) +vnet_bind_i (vnet_bind_args_t * a) { u64 lh, ll_handle = SESSION_INVALID_HANDLE; + u32 table_index, fib_proto; + app_worker_t *app_wrk; application_t *app; - u32 table_index; int rv; - app = application_get_if_valid (app_index); + app = application_get_if_valid (a->app_index); if (!app) { SESSION_DBG ("app not attached"); return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } + app_wrk = application_get_worker (app, a->wrk_map_index); - session_endpoint_update_for_app (sep, app); - if (!session_endpoint_in_ns (sep)) + session_endpoint_update_for_app (&a->sep, app); + if (!session_endpoint_in_ns (&a->sep)) return VNET_API_ERROR_INVALID_VALUE_2; - table_index = application_session_table (app, - session_endpoint_fib_proto (sep)); - lh = session_lookup_endpoint_listener (table_index, sep, 1); + fib_proto = session_endpoint_fib_proto (&a->sep); + table_index = application_session_table (app, fib_proto); + lh = session_lookup_endpoint_listener (table_index, &a->sep, 1); if (lh != SESSION_INVALID_HANDLE) return VNET_API_ERROR_ADDRESS_IN_USE; @@ -176,11 +178,13 @@ vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle) * Add session endpoint to local session table. Only binds to "inaddr_any" * (i.e., zero address) are added to local scope table. */ - if (application_has_local_scope (app) && session_endpoint_is_local (sep)) + if (application_has_local_scope (app) + && session_endpoint_is_local (&a->sep)) { - if ((rv = application_start_local_listen (app, sep, handle))) + if ((rv = + application_start_local_listen (app_wrk, &a->sep, &a->handle))) return rv; - ll_handle = *handle; + ll_handle = a->handle; } if (!application_has_global_scope (app)) @@ -191,9 +195,9 @@ vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle) */ /* Setup listen path down to transport */ - rv = application_start_listen (app, sep, handle); + rv = app_worker_start_listen (app_wrk, &a->sep, &a->handle); if (rv && ll_handle != SESSION_INVALID_HANDLE) - session_lookup_del_session_endpoint (table_index, sep); + session_lookup_del_session_endpoint (table_index, &a->sep); /* * Store in local table listener the index of the transport layer @@ -205,7 +209,7 @@ vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle) local_session_t *ll; stream_session_t *tl; ll = application_get_local_listener_w_handle (ll_handle); - tl = listen_session_get_from_handle (*handle); + tl = listen_session_get_from_handle (a->handle); ll->transport_listener_index = tl->session_index; } return rv; @@ -219,13 +223,13 @@ vnet_unbind_i (u32 app_index, session_handle_t handle) if (!(app = application_get_if_valid (app_index))) { - SESSION_DBG ("app (%d) not attached", app_index); + SESSION_DBG ("app (%d) not attached", wrk_map_index); return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } if (application_has_local_scope (app)) { - if ((rv = application_stop_local_listen (app, handle))) + if ((rv = application_stop_local_listen (handle, app_index))) return rv; } @@ -233,25 +237,27 @@ vnet_unbind_i (u32 app_index, session_handle_t handle) * Clear the global scope table of the listener */ if (application_has_global_scope (app)) - return application_stop_listen (app, handle); + return app_worker_stop_listen (handle, app_index); return 0; } -int -application_connect (u32 client_index, u32 api_context, - session_endpoint_t * sep) +static int +application_connect (vnet_connect_args_t * a) { - application_t *server, *client; + app_worker_t *server_wrk, *client_wrk; u32 table_index, server_index, li; stream_session_t *listener; + application_t *client; local_session_t *ll; + u8 fib_proto; u64 lh; - if (session_endpoint_is_zero (sep)) + if (session_endpoint_is_zero (&a->sep)) return VNET_API_ERROR_INVALID_VALUE; - client = application_get (client_index); - session_endpoint_update_for_app (sep, client); + client = application_get (a->app_index); + session_endpoint_update_for_app (&a->sep, client); + client_wrk = application_get_worker (client, a->wrk_map_index); /* * First check the local scope for locally attached destinations. @@ -261,7 +267,7 @@ application_connect (u32 client_index, u32 api_context, if (application_has_local_scope (client)) { table_index = application_local_session_table (client); - lh = session_lookup_local_endpoint (table_index, sep); + lh = session_lookup_local_endpoint (table_index, &a->sep); if (lh == SESSION_DROP_HANDLE) return VNET_API_ERROR_APP_CONNECT_FILTERED; @@ -275,12 +281,13 @@ application_connect (u32 client_index, u32 api_context, * can happen if client is a generic proxy. Route connect through * global table instead. */ - if (server_index != client_index) + if (server_index != a->app_index) { - server = application_get (server_index); - ll = application_get_local_listen_session (server, li); - return application_local_session_connect (table_index, client, - server, ll, api_context); + server_wrk = app_worker_get (server_index); + ll = application_get_local_listen_session (server_wrk, li); + return application_local_session_connect (client_wrk, + server_wrk, ll, + a->api_context); } } @@ -290,28 +297,30 @@ application_connect (u32 client_index, u32 api_context, */ global_scope: - if (session_endpoint_is_local (sep)) + if (session_endpoint_is_local (&a->sep)) return VNET_API_ERROR_SESSION_CONNECT; if (!application_has_global_scope (client)) return VNET_API_ERROR_APP_CONNECT_SCOPE; - table_index = application_session_table (client, - session_endpoint_fib_proto (sep)); - listener = session_lookup_listener (table_index, sep); + fib_proto = session_endpoint_fib_proto (&a->sep); + table_index = application_session_table (client, fib_proto); + listener = session_lookup_listener (table_index, &a->sep); if (listener) { - server = application_get (listener->app_index); - if (server) - return application_local_session_connect (table_index, client, server, - (local_session_t *) - listener, api_context); + server_wrk = app_worker_get (listener->app_wrk_index); + if (server_wrk) + { + ll = (local_session_t *) listener; + return application_local_session_connect (client_wrk, server_wrk, + ll, a->api_context); + } } /* * Not connecting to a local server, propagate to transport */ - if (application_open_session (client, sep, api_context)) + if (app_worker_open_session (client_wrk, &a->sep, a->api_context)) return VNET_API_ERROR_SESSION_CONNECT; return 0; } @@ -415,7 +424,7 @@ parse_uri (char *uri, session_endpoint_extended_t * sep) } static int -session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) +app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) { app_namespace_t *app_ns; if (vec_len (namespace_id) == 0) @@ -448,6 +457,7 @@ vnet_application_attach (vnet_app_attach_args_t * a) { svm_fifo_segment_private_t *fs; application_t *app = 0; + app_worker_t *app_wrk; segment_manager_t *sm; u32 app_ns_index = 0; u64 secret; @@ -466,17 +476,19 @@ vnet_application_attach (vnet_app_attach_args_t * a) "app already attached"); secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; - if ((rv = session_validate_namespace (a->namespace_id, secret, - &app_ns_index))) + if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index))) return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv); a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; - app = application_new (); - if ((rv = application_init (app, a->api_client_index, a->name, a->options, - a->session_cb_vft))) + + if ((rv = application_alloc_and_init ((app_init_args_t *) a))) return clib_error_return_code (0, rv, 0, "app init: %d", rv); - a->app_evt_q = app->event_queue; - sm = segment_manager_get (app->first_segment_manager); + app = application_get (a->app_index); + if ((rv = app_worker_alloc_and_init (app, &app_wrk))) + return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv); + + a->app_evt_q = app_wrk->event_queue; + sm = segment_manager_get (app_wrk->first_segment_manager); fs = segment_manager_get_segment_w_lock (sm, 0); if (application_is_proxy (app)) @@ -484,7 +496,6 @@ vnet_application_attach (vnet_app_attach_args_t * a) ASSERT (vec_len (fs->ssvm.name) <= 128); a->segment = &fs->ssvm; - a->app_index = app->index; segment_manager_segment_reader_unlock (sm); @@ -506,7 +517,7 @@ vnet_application_detach (vnet_app_detach_args_t * a) return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - application_del (app); + application_free (app); return 0; } @@ -519,8 +530,8 @@ vnet_bind_uri (vnet_bind_args_t * a) rv = parse_uri (a->uri, &sep); if (rv) return rv; - - return vnet_bind_i (a->app_index, (session_endpoint_t *) & sep, &a->handle); + clib_memcpy (&a->sep, &sep, sizeof (sep)); + return vnet_bind_i (a); } int @@ -557,8 +568,8 @@ vnet_connect_uri (vnet_connect_args_t * a) if (rv) return clib_error_return_code (0, rv, 0, "app init: %d", rv); - if ((rv = application_connect (a->app_index, a->api_context, - (session_endpoint_t *) & sep))) + clib_memcpy (&a->sep, &sep, sizeof (sep)); + if ((rv = application_connect (a))) return clib_error_return_code (0, rv, 0, "connect failed"); return 0; } @@ -581,7 +592,8 @@ vnet_disconnect_session (vnet_disconnect_args_t * a) if (!(ls = application_get_local_session_from_handle (a->handle))) return 0; - if (ls->app_index != a->app_index && ls->client_index != a->app_index) + if (ls->app_wrk_index != a->app_index + && ls->client_wrk_index != a->app_index) { clib_warning ("app %u is neither client nor server for session %u", a->app_index, a->app_index); @@ -593,7 +605,7 @@ vnet_disconnect_session (vnet_disconnect_args_t * a) { stream_session_t *s; s = session_get_from_handle_if_valid (a->handle); - if (!s || s->app_index != a->app_index) + if (!s || s->app_wrk_index != a->app_index) return VNET_API_ERROR_INVALID_VALUE; /* We're peeking into another's thread pool. Make sure */ @@ -608,7 +620,7 @@ clib_error_t * vnet_bind (vnet_bind_args_t * a) { int rv; - if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle))) + if ((rv = vnet_bind_i (a))) return clib_error_return_code (0, rv, 0, "bind failed: %d", rv); return 0; } @@ -625,10 +637,9 @@ vnet_unbind (vnet_unbind_args_t * a) clib_error_t * vnet_connect (vnet_connect_args_t * a) { - session_endpoint_t *sep = (session_endpoint_t *) & a->sep; int rv; - if ((rv = application_connect (a->app_index, a->api_context, sep))) + if ((rv = application_connect (a))) return clib_error_return_code (0, rv, 0, "connect failed: %d", rv); return 0; } diff --git a/src/vnet/session/application_interface.h b/src/vnet/session/application_interface.h index ba6d2660d7f..13ec73adf0f 100644 --- a/src/vnet/session/application_interface.h +++ b/src/vnet/session/application_interface.h @@ -23,27 +23,11 @@ typedef struct _vnet_app_attach_args_t { - /** Binary API client index */ - u32 api_client_index; - - /** Application name. Used by builtin apps */ - u8 *name; - - /** Application and segment manager options */ - u64 *options; - - /** ID of the namespace the app has access to */ - u8 *namespace_id; - - /** Session to application callback functions */ - session_cb_vft_t *session_cb_vft; - - /* - * Results - */ - ssvm_private_t *segment; +#define _(_type, _name) _type _name; + foreach_app_init_args +#undef _ + ssvm_private_t * segment; svm_msg_q_t *app_evt_q; - u32 app_index; } vnet_app_attach_args_t; typedef struct _vnet_app_detach_args_t @@ -55,11 +39,13 @@ typedef struct _vnet_bind_args_t { union { - char *uri; + session_endpoint_extended_t sep_ext; session_endpoint_t sep; + char *uri; }; u32 app_index; + u32 wrk_map_index; /* * Results @@ -75,19 +61,22 @@ typedef struct _vnet_unbind_args_t union { char *uri; - u64 handle; + u64 handle; /**< Session handle */ }; - u32 app_index; + u32 app_index; /**< Owning application index */ + u32 app_wrk_index; /**< App's local pool worker index */ } vnet_unbind_args_t; typedef struct _vnet_connect_args { union { + session_endpoint_extended_t sep_ext; + session_endpoint_t sep; char *uri; - session_endpoint_extended_t sep; }; u32 app_index; + u32 wrk_map_index; u32 api_context; session_handle_t session_handle; diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c index 6f149ad9412..158a8e8abf3 100644 --- a/src/vnet/session/segment_manager.c +++ b/src/vnet/session/segment_manager.c @@ -34,7 +34,7 @@ static u32 default_app_evt_queue_size = 128; segment_manager_properties_t * segment_manager_properties_get (segment_manager_t * sm) { - return application_get_segment_manager_properties (sm->app_index); + return application_get_segment_manager_properties (sm->app_wrk_index); } segment_manager_properties_t * @@ -50,13 +50,13 @@ segment_manager_properties_init (segment_manager_properties_t * props) static u8 segment_manager_app_detached (segment_manager_t * sm) { - return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX); + return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX); } void segment_manager_app_detach (segment_manager_t * sm) { - sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX; + sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; } always_inline u32 @@ -369,7 +369,7 @@ segment_manager_del_sessions (segment_manager_t * sm) if (fifo->master_thread_index == 255) { svm_fifo_t *next = fifo->next; - application_local_session_disconnect_w_index (sm->app_index, + application_local_session_disconnect_w_index (sm->app_wrk_index, fifo->master_session_index); fifo = next; continue; @@ -518,8 +518,8 @@ alloc_check: *fifo_segment_index = segment_manager_segment_index (sm, fifo_segment); if (added_a_segment) - rv = application_add_segment_notify (sm->app_index, - &fifo_segment->ssvm); + rv = app_worker_add_segment_notify (sm->app_wrk_index, + &fifo_segment->ssvm); /* Drop the lock after app is notified */ segment_manager_segment_reader_unlock (sm); return rv; @@ -721,8 +721,8 @@ segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input, /* *INDENT-OFF* */ pool_foreach (sm, smm->segment_managers, ({ - vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm), - sm->app_index, pool_elts (sm->segments)); + vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm), + sm->app_wrk_index, pool_elts (sm->segments)); })); /* *INDENT-ON* */ diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h index aae4f2e4e51..0e5f62414e5 100644 --- a/src/vnet/session/segment_manager.h +++ b/src/vnet/session/segment_manager.h @@ -24,22 +24,17 @@ typedef struct _segment_manager_properties { - /** Session fifo sizes. */ - u32 rx_fifo_size; - u32 tx_fifo_size; - u32 evt_q_size; - - /** Configured additional segment size */ - u32 add_segment_size; - - /** Flags */ - u8 add_segment:1; /**< can add new segments */ - u8 use_mq_eventfd:1; /**< use eventfds for mqs */ - u8 reserved:6; - - /** Segment type: if set to SSVM_N_TYPES, private segments are used */ - ssvm_segment_type_t segment_type; - + u32 rx_fifo_size; /**< receive fifo size */ + u32 tx_fifo_size; /**< transmit fifo size */ + u32 evt_q_size; /**< event queue length */ + u32 segment_size; /**< first segment size */ + u32 prealloc_fifos; /**< preallocated fifo pairs */ + u32 add_segment_size; /**< additional segment size */ + u8 add_segment:1; /**< can add new segments flag */ + u8 use_mq_eventfd:1; /**< use eventfds for mqs flag */ + u8 reserved:6; /**< reserved flags */ + ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES, + private segments are used */ } segment_manager_properties_t; typedef struct _segment_manager @@ -50,8 +45,8 @@ typedef struct _segment_manager /** rwlock that protects the segments pool */ clib_rwlock_t segments_rwlock; - /** Owner app index */ - u32 app_index; + /** Owner app worker index */ + u32 app_wrk_index; /** * First segment should not be deleted unless segment manger is deleted. diff --git a/src/vnet/session/session.api b/src/vnet/session/session.api index c60e99ea14a..5a38340a450 100644 --- a/src/vnet/session/session.api +++ b/src/vnet/session/session.api @@ -13,7 +13,7 @@ * limitations under the License. */ -option version = "1.1.0"; +option version = "1.2.0"; /** \brief client->vpp, attach application to session layer @param client_index - opaque cookie to identify the sender @@ -271,6 +271,7 @@ define reset_session_reply { /** \brief Bind to an ip:port pair for a given transport protocol @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request + @param wrk_index - index of worker requesting the bind @param vrf - bind namespace @param is_ip4 - flag that is 1 if ip address family is IPv4 @param ip - ip address @@ -281,6 +282,7 @@ define reset_session_reply { define bind_sock { u32 client_index; u32 context; + u32 wrk_index; u32 vrf; u8 is_ip4; u8 ip[16]; @@ -303,6 +305,7 @@ autoreply define unbind_sock { /** \brief Connect to a remote peer @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request + @param wrk_index - worker that requests the connect @param client_queue_address - client's API queue address. Non-zero when used to perform redirects @param options - socket options, fifo sizes, etc. when doing redirects @@ -318,6 +321,7 @@ autoreply define unbind_sock { autoreply define connect_sock { u32 client_index; u32 context; + u32 wrk_index; u64 client_queue_address; u64 options[16]; u32 vrf; @@ -418,6 +422,47 @@ autoreply define app_cut_through_registration_add u8 fd_flags; }; +/** \brief add/del application worker + @param client_index - opaque cookie to identify the sender + client to vpp direction only + @param context - sender context, to match reply w/ request + @param app_api_index - explicit client api index + @param wrk_index - worker index, if a delete + @param is_add - set if an add +*/ +define app_worker_add_del +{ + u32 client_index; + u32 context; + u32 app_api_index; + u32 wrk_index; + u8 is_add; +}; + +/** \brief Reply for app worker add/del + @param context - returned sender context, to match reply w/ request + @param retval - return code + @param wrk_index - worker index, if add + @param app_event_queue_address - vpp event queue address of new worker + @param n_fds - number of fds exchanged + @param fd_flags - set of flags that indicate which fds are to be expected + over the socket (set only if socket transport available) + @param segment_name_length - length of segment name + @param segment_name - name of segment client needs to attach to +*/ +define app_worker_add_del_reply +{ + u32 context; + i32 retval; + u32 wrk_index; + u64 app_event_queue_address; + u8 n_fds; + u8 fd_flags; + u8 segment_name_length; + u8 segment_name[128]; + u8 is_add; +}; + /** \brief enable/disable session layer @param client_index - opaque cookie to identify the sender client to vpp direction only diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c index 9790ec25edd..57ac384a519 100644 --- a/src/vnet/session/session.c +++ b/src/vnet/session/session.c @@ -488,12 +488,12 @@ stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes) static inline int session_enqueue_notify (stream_session_t * s, u8 lock) { - application_t *app; + app_worker_t *app; - app = application_get_if_valid (s->app_index); + app = app_worker_get_if_valid (s->app_wrk_index); if (PREDICT_FALSE (!app)) { - TCP_DBG ("invalid s->app_index = %d", s->app_index); + SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index); return 0; } @@ -513,9 +513,9 @@ session_enqueue_notify (stream_session_t * s, u8 lock) int session_dequeue_notify (stream_session_t * s) { - application_t *app; + app_worker_t *app; - app = application_get_if_valid (s->app_index); + app = app_worker_get_if_valid (s->app_wrk_index); if (PREDICT_FALSE (!app)) return -1; @@ -596,6 +596,7 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) u32 opaque = 0, new_ti, new_si; stream_session_t *new_s = 0; segment_manager_t *sm; + app_worker_t *app_wrk; application_t *app; u8 alloc_fifos; int error = 0; @@ -615,17 +616,18 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) /* Get the app's index from the handle we stored when opening connection * and the opaque (api_context for external apps) from transport session * index */ - app = application_get_if_valid (handle >> 32); - if (!app) + app_wrk = app_worker_get_if_valid (handle >> 32); + if (!app_wrk) return -1; opaque = tc->s_index; + app = application_get (app_wrk->app_index); /* * Allocate new session with fifos (svm segments are allocated if needed) */ if (!is_fail) { - sm = application_get_connect_segment_manager (app); + sm = app_worker_get_connect_segment_manager (app_wrk); alloc_fifos = !application_is_builtin_proxy (app); if (session_alloc_and_init (sm, tc, alloc_fifos, &new_s)) { @@ -634,7 +636,7 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) } else { - new_s->app_index = app->index; + new_s->app_wrk_index = app_wrk->wrk_index; new_si = new_s->session_index; new_ti = new_s->thread_index; } @@ -643,8 +645,8 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) /* * Notify client application */ - if (app->cb_fns.session_connected_callback (app->index, opaque, new_s, - is_fail)) + if (app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque, + new_s, is_fail)) { SESSION_DBG ("failed to notify app"); if (!is_fail) @@ -731,12 +733,16 @@ session_dgram_connect_notify (transport_connection_t * tc, void stream_session_accept_notify (transport_connection_t * tc) { - application_t *server; + app_worker_t *app_wrk; + application_t *app; stream_session_t *s; s = session_get (tc->s_index, tc->thread_index); - server = application_get (s->app_index); - server->cb_fns.session_accept_callback (s); + app_wrk = app_worker_get_if_valid (s->app_wrk_index); + if (!app_wrk) + return; + app = application_get (app_wrk->app_index); + app->cb_fns.session_accept_callback (s); } /** @@ -749,14 +755,17 @@ stream_session_accept_notify (transport_connection_t * tc) void stream_session_disconnect_notify (transport_connection_t * tc) { - application_t *server; + app_worker_t *app_wrk; + application_t *app; stream_session_t *s; s = session_get (tc->s_index, tc->thread_index); s->session_state = SESSION_STATE_CLOSING; - server = application_get_if_valid (s->app_index); - if (server) - server->cb_fns.session_disconnect_callback (s); + app_wrk = app_worker_get_if_valid (s->app_wrk_index); + if (!app_wrk) + return; + app = application_get (app_wrk->app_index); + app->cb_fns.session_disconnect_callback (s); } /** @@ -806,10 +815,12 @@ void stream_session_reset_notify (transport_connection_t * tc) { stream_session_t *s; + app_worker_t *app_wrk; application_t *app; s = session_get (tc->s_index, tc->thread_index); s->session_state = SESSION_STATE_CLOSED; - app = application_get (s->app_index); + app_wrk = app_worker_get (s->app_wrk_index); + app = application_get (app_wrk->app_index); app->cb_fns.session_reset_callback (s); } @@ -820,38 +831,40 @@ int stream_session_accept (transport_connection_t * tc, u32 listener_index, u8 notify) { - application_t *server; stream_session_t *s, *listener; + app_worker_t *app_wrk; segment_manager_t *sm; int rv; /* Find the server */ listener = listen_session_get (listener_index); - server = application_get (listener->app_index); + app_wrk = app_worker_get (listener->app_wrk_index); - sm = application_get_listen_segment_manager (server, listener); + sm = app_worker_get_listen_segment_manager (app_wrk, listener); if ((rv = session_alloc_and_init (sm, tc, 1, &s))) return rv; - s->app_index = server->index; + s->app_wrk_index = app_wrk->wrk_index; s->listener_index = listener_index; s->session_state = SESSION_STATE_ACCEPTING; /* Shoulder-tap the server */ if (notify) { - server->cb_fns.session_accept_callback (s); + application_t *app = application_get (app_wrk->app_index); + app->cb_fns.session_accept_callback (s); } return 0; } int -session_open_cl (u32 app_index, session_endpoint_t * rmt, u32 opaque) +session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) { transport_connection_t *tc; transport_endpoint_t *tep; segment_manager_t *sm; + app_worker_t *app_wrk; stream_session_t *s; application_t *app; int rv; @@ -868,22 +881,23 @@ session_open_cl (u32 app_index, session_endpoint_t * rmt, u32 opaque) /* For dgram type of service, allocate session and fifos now. */ - app = application_get (app_index); - sm = application_get_connect_segment_manager (app); + app_wrk = app_worker_get (app_wrk_index); + sm = app_worker_get_connect_segment_manager (app_wrk); if (session_alloc_and_init (sm, tc, 1, &s)) return -1; - s->app_index = app->index; + s->app_wrk_index = app_wrk->wrk_index; s->session_state = SESSION_STATE_OPENED; /* Tell the app about the new event fifo for this session */ - app->cb_fns.session_connected_callback (app->index, opaque, s, 0); + app = application_get (app_wrk->app_index); + app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque, s, 0); return 0; } int -session_open_vc (u32 app_index, session_endpoint_t * rmt, u32 opaque) +session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) { transport_connection_t *tc; transport_endpoint_t *tep; @@ -907,7 +921,7 @@ session_open_vc (u32 app_index, session_endpoint_t * rmt, u32 opaque) * is needed when the connect notify comes and we have to notify the * external app */ - handle = (((u64) app_index) << 32) | (u64) tc->c_index; + handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index; session_lookup_add_half_open (tc, handle); /* Store api_context (opaque) for when the reply comes. Not the nicest @@ -918,10 +932,10 @@ session_open_vc (u32 app_index, session_endpoint_t * rmt, u32 opaque) } int -session_open_app (u32 app_index, session_endpoint_t * rmt, u32 opaque) +session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) { session_endpoint_extended_t *sep = (session_endpoint_extended_t *) rmt; - sep->app_index = app_index; + sep->app_wrk_index = app_wrk_index; sep->opaque = opaque; return tp_vfts[rmt->transport_proto].open ((transport_endpoint_t *) sep); @@ -951,10 +965,10 @@ static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = { * on open completion. */ int -session_open (u32 app_index, session_endpoint_t * rmt, u32 opaque) +session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) { transport_service_type_t tst = tp_vfts[rmt->transport_proto].service_type; - return session_open_srv_fns[tst] (app_index, rmt, opaque); + return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque); } int @@ -988,7 +1002,7 @@ int session_listen_cl (stream_session_t * s, session_endpoint_t * sep) { transport_connection_t *tc; - application_t *server; + app_worker_t *server; segment_manager_t *sm; u32 tci; @@ -1008,8 +1022,8 @@ session_listen_cl (stream_session_t * s, session_endpoint_t * sep) if (tc == 0) return -1; - server = application_get (s->app_index); - sm = application_get_listen_segment_manager (server, s); + server = app_worker_get (s->app_wrk_index); + sm = app_worker_get_listen_segment_manager (server, s); if (session_alloc_fifos (sm, s)) return -1; @@ -1023,7 +1037,7 @@ session_listen_app (stream_session_t * s, session_endpoint_t * sep) { session_endpoint_extended_t esep; clib_memcpy (&esep, sep, sizeof (*sep)); - esep.app_index = s->app_index; + esep.app_wrk_index = s->app_wrk_index; return tp_vfts[sep->transport_proto].bind (s->session_index, (transport_endpoint_t *) & esep); diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c index dddc1f5f03f..d573261d262 100755 --- a/src/vnet/session/session_api.c +++ b/src/vnet/session/session_api.c @@ -58,6 +58,7 @@ _(SESSION_RULE_ADD_DEL, session_rule_add_del) \ _(SESSION_RULES_DUMP, session_rules_dump) \ _(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \ _(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \ +_(APP_WORKER_ADD_DEL, app_worker_add_del) \ static int session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds) @@ -137,7 +138,8 @@ send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs) mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT); - strcpy ((char *) mp->segment_name, (char *) fs->name); + strncpy ((char *) mp->segment_name, (char *) fs->name, + sizeof (mp->segment_name) - 1); vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); @@ -193,14 +195,16 @@ send_app_cut_through_registration_add (u32 api_client_index, u64 mq_addr, static int send_session_accept_callback (stream_session_t * s) { - application_t *server = application_get (s->app_index); + app_worker_t *server_wrk = app_worker_get (s->app_wrk_index); transport_proto_vft_t *tp_vft; vl_api_accept_session_t *mp; vl_api_registration_t *reg; transport_connection_t *tc; stream_session_t *listener; svm_msg_q_t *vpp_queue; + application_t *server; + server = application_get (server_wrk->app_index); reg = vl_mem_api_client_index_to_registration (server->api_client_index); if (!reg) { @@ -212,7 +216,7 @@ send_session_accept_callback (stream_session_t * s) memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION); - mp->context = server->index; + mp->context = server_wrk->wrk_index; mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); @@ -223,8 +227,8 @@ send_session_accept_callback (stream_session_t * s) if (application_is_proxy (server)) { listener = - application_first_listener (server, session_get_fib_proto (s), - session_get_transport_proto (s)); + app_worker_first_listener (server_wrk, session_get_fib_proto (s), + session_get_transport_proto (s)); if (listener) mp->listener_handle = listen_session_get_handle (listener); } @@ -249,7 +253,7 @@ send_session_accept_callback (stream_session_t * s) } else { - ll = application_get_local_listen_session (server, + ll = application_get_local_listen_session (server_wrk, ls->listener_index); if (ll->transport_listener_index != ~0) { @@ -273,15 +277,18 @@ send_session_accept_callback (stream_session_t * s) } void -mq_send_local_session_disconnected_cb (u32 app_index, local_session_t * ls) +mq_send_local_session_disconnected_cb (u32 app_wrk_index, + local_session_t * ls) { - application_t *app = application_get (app_index); + app_worker_t *app_wrk = app_worker_get (app_wrk_index); svm_msg_q_msg_t _msg, *msg = &_msg; session_disconnected_msg_t *mp; svm_msg_q_t *app_mq; session_event_t *evt; + application_t *app; - app_mq = app->event_queue; + app = application_get (app_wrk->app_index); + app_mq = app_wrk->event_queue; svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg); svm_msg_q_unlock (app_mq); @@ -297,10 +304,12 @@ mq_send_local_session_disconnected_cb (u32 app_index, local_session_t * ls) static void send_session_disconnect_callback (stream_session_t * s) { - application_t *app = application_get (s->app_index); + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); vl_api_disconnect_session_t *mp; vl_api_registration_t *reg; + application_t *app; + app = application_get (app_wrk->app_index); reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { @@ -319,10 +328,12 @@ send_session_disconnect_callback (stream_session_t * s) static void send_session_reset_callback (stream_session_t * s) { - application_t *app = application_get (s->app_index); + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); vl_api_registration_t *reg; vl_api_reset_session_t *mp; + application_t *app; + app = application_get (app_wrk->app_index); reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { @@ -338,16 +349,18 @@ send_session_reset_callback (stream_session_t * s) } int -send_session_connected_callback (u32 app_index, u32 api_context, +send_session_connected_callback (u32 app_wrk_index, u32 api_context, stream_session_t * s, u8 is_fail) { vl_api_connect_session_reply_t *mp; transport_connection_t *tc; vl_api_registration_t *reg; svm_msg_q_t *vpp_queue; + app_worker_t *app_wrk; application_t *app; - app = application_get (app_index); + app_wrk = app_worker_get (app_wrk_index); + app = application_get (app_wrk->app_index); reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { @@ -410,7 +423,7 @@ static session_cb_vft_t session_cb_vft = { static int mq_send_session_accepted_cb (stream_session_t * s) { - application_t *app = application_get (s->app_index); + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); svm_msg_q_msg_t _msg, *msg = &_msg; svm_msg_q_t *vpp_queue, *app_mq; transport_proto_vft_t *tp_vft; @@ -418,8 +431,10 @@ mq_send_session_accepted_cb (stream_session_t * s) stream_session_t *listener; session_accepted_msg_t *mp; session_event_t *evt; + application_t *app; - app_mq = app->event_queue; + app = application_get (app_wrk->app_index); + app_mq = app_wrk->event_queue; svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg); svm_msg_q_unlock (app_mq); @@ -428,7 +443,7 @@ mq_send_session_accepted_cb (stream_session_t * s) memset (evt, 0, sizeof (*evt)); evt->event_type = SESSION_CTRL_EVT_ACCEPTED; mp = (session_accepted_msg_t *) evt->data; - mp->context = app->index; + mp->context = app_wrk->wrk_index; mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); @@ -439,8 +454,8 @@ mq_send_session_accepted_cb (stream_session_t * s) if (application_is_proxy (app)) { listener = - application_first_listener (app, session_get_fib_proto (s), - session_get_transport_proto (s)); + app_worker_first_listener (app_wrk, session_get_fib_proto (s), + session_get_transport_proto (s)); if (listener) mp->listener_handle = listen_session_get_handle (listener); } @@ -471,7 +486,9 @@ mq_send_session_accepted_cb (stream_session_t * s) } else { - ll = application_get_local_listen_session (app, ls->listener_index); + ll = + application_get_local_listen_session (app_wrk, + ls->listener_index); if (ll->transport_listener_index != ~0) { listener = listen_session_get (ll->transport_listener_index); @@ -498,13 +515,15 @@ mq_send_session_accepted_cb (stream_session_t * s) static void mq_send_session_disconnected_cb (stream_session_t * s) { - application_t *app = application_get (s->app_index); + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); svm_msg_q_msg_t _msg, *msg = &_msg; session_disconnected_msg_t *mp; svm_msg_q_t *app_mq; session_event_t *evt; + application_t *app; - app_mq = app->event_queue; + app = application_get (app_wrk->app_index); + app_mq = app_wrk->event_queue; svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg); svm_msg_q_unlock (app_mq); @@ -520,7 +539,7 @@ mq_send_session_disconnected_cb (stream_session_t * s) static void mq_send_session_reset_cb (stream_session_t * s) { - application_t *app = application_get (s->app_index); + app_worker_t *app = app_worker_get (s->app_wrk_index); svm_msg_q_msg_t _msg, *msg = &_msg; session_reset_msg_t *mp; svm_msg_q_t *app_mq; @@ -539,21 +558,23 @@ mq_send_session_reset_cb (stream_session_t * s) } static int -mq_send_session_connected_cb (u32 app_index, u32 api_context, +mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context, stream_session_t * s, u8 is_fail) { svm_msg_q_msg_t _msg, *msg = &_msg; session_connected_msg_t *mp; svm_msg_q_t *vpp_mq, *app_mq; transport_connection_t *tc; + app_worker_t *app_wrk; session_event_t *evt; application_t *app; - app = application_get (app_index); - app_mq = app->event_queue; + app_wrk = app_worker_get (app_wrk_index); + app = application_get (app_wrk->app_index); + app_mq = app_wrk->event_queue; if (!app_mq) { - clib_warning ("app %u with api index: %u not attached", app->index, + clib_warning ("app %u with api index: %u not attached", app->app_index, app->api_client_index); return -1; } @@ -753,7 +774,7 @@ vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) app = application_lookup (mp->client_index); if (app) { - a->app_index = app->index; + a->app_index = app->app_index; rv = vnet_application_detach (a); } @@ -783,7 +804,7 @@ vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) { memset (a, 0, sizeof (*a)); a->uri = (char *) mp->uri; - a->app_index = app->index; + a->app_index = app->app_index; rv = vnet_bind_uri (a); } else @@ -836,7 +857,7 @@ vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) if (app) { a->uri = (char *) mp->uri; - a->app_index = app->index; + a->app_index = app->app_index; rv = vnet_unbind_uri (a); } else @@ -868,7 +889,7 @@ vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) { a->uri = (char *) mp->uri; a->api_context = mp->context; - a->app_index = app->index; + a->app_index = app->app_index; if ((error = vnet_connect_uri (a))) { rv = clib_error_get_code (error); @@ -912,7 +933,7 @@ vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) if (app) { a->handle = mp->handle; - a->app_index = app->index; + a->app_index = app->app_index; rv = vnet_disconnect_session (a); } else @@ -943,7 +964,7 @@ vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * if (app) { a->handle = mp->handle; - a->app_index = app->index; + a->app_index = app->app_index; vnet_disconnect_session (a); } } @@ -951,6 +972,7 @@ vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * static void vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) { + app_worker_t *app_wrk; application_t *app; stream_session_t *s; u32 index, thread_index; @@ -961,12 +983,20 @@ vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) session_parse_handle (mp->handle, &index, &thread_index); s = session_get_if_valid (index, thread_index); - if (s == 0 || app->index != s->app_index) + if (!s) { clib_warning ("Invalid session!"); return; } + app_wrk = app_worker_get (s->app_wrk_index); + if (app_wrk->app_index != app->app_index) + { + clib_warning ("app %u does not own handle 0x%lx", app->app_index, + mp->handle); + return; + } + /* Client objected to resetting the session, log and continue */ if (mp->retval) { @@ -998,7 +1028,7 @@ vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) if (session_handle_is_local (mp->handle)) { ls = application_get_local_session_from_handle (mp->handle); - if (!ls || ls->app_index != mp->context) + if (!ls || ls->app_wrk_index != mp->context) { clib_warning ("server %u doesn't own local handle %llu", mp->context, mp->handle); @@ -1016,7 +1046,7 @@ vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) clib_warning ("session doesn't exist"); return; } - if (s->app_index != mp->context) + if (s->app_wrk_index != mp->context) { clib_warning ("app doesn't own session"); return; @@ -1066,7 +1096,8 @@ vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) a->sep.fib_index = mp->vrf; a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; a->sep.transport_proto = mp->proto; - a->app_index = app->index; + a->app_index = app->app_index; + a->wrk_map_index = mp->wrk_index; if ((error = vnet_bind (a))) { @@ -1118,7 +1149,7 @@ vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) app = application_lookup (mp->client_index); if (app) { - a->app_index = app->index; + a->app_index = app->app_index; a->handle = mp->handle; if ((error = vnet_unbind (a))) { @@ -1163,17 +1194,18 @@ vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; if (mp->hostname_len) { - vec_validate (a->sep.hostname, mp->hostname_len - 1); - clib_memcpy (a->sep.hostname, mp->hostname, mp->hostname_len); + vec_validate (a->sep_ext.hostname, mp->hostname_len - 1); + clib_memcpy (a->sep_ext.hostname, mp->hostname, mp->hostname_len); } a->api_context = mp->context; - a->app_index = app->index; + a->app_index = app->app_index; + a->wrk_map_index = mp->wrk_index; if ((error = vnet_connect (a))) { rv = clib_error_get_code (error); clib_error_report (error); } - vec_free (a->sep.hostname); + vec_free (a->sep_ext.hostname); } else { @@ -1190,6 +1222,84 @@ done: } static void +vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp) +{ + int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; + vl_api_app_worker_add_del_reply_t *rmp; + vl_api_registration_t *reg; + clib_error_t *error = 0; + application_t *app; + u8 fd_flags = 0; + + if (!session_manager_is_enabled ()) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + app = application_lookup (mp->app_api_index); + if (!app) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } + + vnet_app_worker_add_del_args_t args = { + .app_index = app->app_index, + .wrk_index = clib_net_to_host_u32 (mp->wrk_index), + .is_add = mp->is_add + }; + error = vnet_app_worker_add_del (&args); + if (error) + { + rv = clib_error_get_code (error); + clib_error_report (error); + goto done; + } + + /* Send fifo segment fd if needed */ + if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) + { + fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; + fds[n_fds] = args.segment->fd; + n_fds += 1; + } + if (application_segment_manager_properties (app)->use_mq_eventfd) + { + fd_flags |= SESSION_FD_F_MQ_EVENTFD; + fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q); + n_fds += 1; + } + + /* *INDENT-OFF* */ +done: + REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({ + rmp->is_add = mp->is_add; + if (!rv) + { + rmp->wrk_index = clib_host_to_net_u32 (args.wrk_index); + if (vec_len (args.segment->name)) + { + memcpy (rmp->segment_name, args.segment->name, + vec_len (args.segment->name)); + rmp->segment_name_length = vec_len (args.segment->name); + } + rmp->app_event_queue_address = pointer_to_uword (args.evt_q); + rmp->n_fds = n_fds; + rmp->fd_flags = fd_flags; + } + })); + /* *INDENT-ON* */ + + if (n_fds) + session_send_fds (reg, fds, n_fds); +} + +static void vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) { vl_api_app_namespace_add_del_reply_t *rmp; @@ -1444,7 +1554,7 @@ vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t * goto done; } memset (a, 0, sizeof (*a)); - a->app_index = app->index; + a->app_index = app->app_index; cert_len = clib_net_to_host_u16 (mp->cert_len); if (cert_len > 10000) { @@ -1484,7 +1594,7 @@ vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t * goto done; } memset (a, 0, sizeof (*a)); - a->app_index = app->index; + a->app_index = app->app_index; key_len = clib_net_to_host_u16 (mp->key_len); if (key_len > 10000) { @@ -1510,7 +1620,7 @@ application_reaper_cb (u32 client_index) vnet_app_detach_args_t _a, *a = &_a; if (app) { - a->app_index = app->index; + a->app_index = app->app_index; vnet_application_detach (a); } return 0; diff --git a/src/vnet/session/session_cli.c b/src/vnet/session/session_cli.c index 3588bbc2f63..36133523e34 100755 --- a/src/vnet/session/session_cli.c +++ b/src/vnet/session/session_cli.c @@ -250,7 +250,7 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input, if (s->session_state != SESSION_STATE_LISTENING || s->session_type != sst) continue; - app_name = application_name_from_index (s->app_index); + app_name = application_name_from_index (s->app_wrk_index); vlib_cli_output (vm, "%U%-25v%-10u", format_stream_session, s, 1, app_name, s->session_index); vec_free (app_name); @@ -314,7 +314,8 @@ VLIB_CLI_COMMAND (vlib_cli_show_session_command) = static int clear_session (stream_session_t * s) { - application_t *server = application_get (s->app_index); + app_worker_t *server_wrk = app_worker_get (s->app_wrk_index); + application_t *server = application_get (server_wrk->app_index); server->cb_fns.session_disconnect_callback (s); return 0; } diff --git a/src/vnet/session/session_lookup.c b/src/vnet/session/session_lookup.c index 37fccd9d7b7..19aeb0b24e0 100644 --- a/src/vnet/session/session_lookup.c +++ b/src/vnet/session/session_lookup.c @@ -380,7 +380,8 @@ session_lookup_app_listen_session (u32 app_index, u8 fib_proto, if (!app) return 0; - return application_first_listener (app, fib_proto, transport_proto); + return app_worker_first_listener (application_get_default_worker (app), + fib_proto, transport_proto); } static stream_session_t * @@ -1299,15 +1300,17 @@ u8 * format_ip4_session_lookup_kvp (u8 * s, va_list * args) { clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *); - u32 is_local = va_arg (*args, u32), app_index, session_index; + u32 is_local = va_arg (*args, u32), app_wrk_index, session_index; + v4_connection_key_t *key = (v4_connection_key_t *) kvp->key; u8 *app_name, *str = 0; stream_session_t *session; - v4_connection_key_t *key = (v4_connection_key_t *) kvp->key; + app_worker_t *app_wrk; if (!is_local) { session = session_get_from_handle (kvp->value); - app_name = application_name_from_index (session->app_index); + app_wrk = app_worker_get (session->app_wrk_index); + app_name = application_name_from_index (app_wrk->app_index); str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short, key->proto, format_ip4_address, &key->src, clib_net_to_host_u16 (key->src_port), format_ip4_address, @@ -1316,8 +1319,9 @@ format_ip4_session_lookup_kvp (u8 * s, va_list * args) } else { - local_session_parse_handle (kvp->value, &app_index, &session_index); - app_name = application_name_from_index (app_index); + local_session_parse_handle (kvp->value, &app_wrk_index, &session_index); + app_wrk = app_worker_get (app_wrk_index); + app_name = application_name_from_index (app_wrk->app_index); str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto, format_ip4_address, &key->src, clib_net_to_host_u16 (key->src_port)); diff --git a/src/vnet/session/session_node.c b/src/vnet/session/session_node.c index 30cd5ae9a4d..1656e2eed0e 100644 --- a/src/vnet/session/session_node.c +++ b/src/vnet/session/session_node.c @@ -44,7 +44,7 @@ session_mq_accepted_reply_handler (void *data) if (session_handle_is_local (mp->handle)) { ls = application_get_local_session_from_handle (mp->handle); - if (!ls || ls->app_index != mp->context) + if (!ls || ls->app_wrk_index != mp->context) { clib_warning ("server %u doesn't own local handle %llu", mp->context, mp->handle); @@ -62,7 +62,7 @@ session_mq_accepted_reply_handler (void *data) clib_warning ("session doesn't exist"); return; } - if (s->app_index != mp->context) + if (s->app_wrk_index != mp->context) { clib_warning ("app doesn't own session"); return; @@ -70,8 +70,8 @@ session_mq_accepted_reply_handler (void *data) s->session_state = SESSION_STATE_READY; if (!svm_fifo_is_empty (s->server_rx_fifo)) { - application_t *app; - app = application_get (s->app_index); + app_worker_t *app; + app = app_worker_get (s->app_wrk_index); application_send_event (app, s, FIFO_EVENT_APP_RX); } } @@ -81,8 +81,9 @@ static void session_mq_reset_reply_handler (void *data) { session_reset_reply_msg_t *mp; - application_t *app; + app_worker_t *app_wrk; stream_session_t *s; + application_t *app; u32 index, thread_index; mp = (session_reset_reply_msg_t *) data; @@ -92,11 +93,18 @@ session_mq_reset_reply_handler (void *data) session_parse_handle (mp->handle, &index, &thread_index); s = session_get_if_valid (index, thread_index); - if (s == 0 || app->index != s->app_index) + if (!s) { clib_warning ("Invalid session!"); return; } + app_wrk = app_worker_get (s->app_wrk_index); + if (!app_wrk || app_wrk->app_index != app->app_index) + { + clib_warning ("App % does not own handle 0x%lx!", app->app_index, + mp->handle); + return; + } /* Client objected to resetting the session, log and continue */ if (mp->retval) @@ -117,37 +125,39 @@ session_mq_disconnected_handler (void *data) vnet_disconnect_args_t _a, *a = &_a; svm_msg_q_msg_t _msg, *msg = &_msg; session_disconnected_msg_t *mp; + app_worker_t *app_wrk; session_event_t *evt; stream_session_t *s; application_t *app; int rv = 0; mp = (session_disconnected_msg_t *) data; - app = application_lookup (mp->client_index); s = session_get_from_handle_if_valid (mp->handle); - if (!(app && s && s->app_index == app->index)) + app_wrk = app_worker_get (s->app_wrk_index); + app = application_lookup (mp->client_index); + if (!(app_wrk && s && app->app_index == app_wrk->app_index)) { - clib_warning ("could not disconnect session: %llu app: %u", mp->handle, - mp->client_index); + clib_warning ("could not disconnect session: %llu app_wrk: %u", + mp->handle, mp->client_index); return; } a->handle = mp->handle; - a->app_index = app->index; + a->app_index = app_wrk->wrk_index; rv = vnet_disconnect_session (a); - svm_msg_q_lock_and_alloc_msg_w_ring (app->event_queue, + svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg); - svm_msg_q_unlock (app->event_queue); - evt = svm_msg_q_msg_data (app->event_queue, msg); + svm_msg_q_unlock (app_wrk->event_queue); + evt = svm_msg_q_msg_data (app_wrk->event_queue, msg); memset (evt, 0, sizeof (*evt)); evt->event_type = SESSION_CTRL_EVT_DISCONNECTED; rmp = (session_disconnected_reply_msg_t *) evt->data; rmp->handle = mp->handle; rmp->context = mp->context; rmp->retval = rv; - svm_msg_q_add (app->event_queue, msg, SVM_Q_WAIT); + svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT); } static void @@ -171,7 +181,7 @@ session_mq_disconnected_reply_handler (void *data) if (app) { a->handle = mp->handle; - a->app_index = app->index; + a->app_index = app->app_index; vnet_disconnect_session (a); } } @@ -731,6 +741,7 @@ session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, svm_msg_q_msg_t _msg, *msg = &_msg; f64 now = vlib_time_now (vm); int n_tx_packets = 0, i, rv; + app_worker_t *app_wrk; application_t *app; svm_msg_q_t *mq; void (*fp) (void *); @@ -858,7 +869,8 @@ skip_dequeue: if (PREDICT_FALSE (!s)) continue; svm_fifo_unset_event (s->server_rx_fifo); - app = application_get (s->app_index); + app_wrk = app_worker_get (s->app_wrk_index); + app = application_get (app_wrk->app_index); app->cb_fns.builtin_app_rx_callback (s); break; case FIFO_EVENT_RPC: diff --git a/src/vnet/session/session_test.c b/src/vnet/session/session_test.c index c12041a2ee6..6f162a81509 100644 --- a/src/vnet/session/session_test.c +++ b/src/vnet/session/session_test.c @@ -212,7 +212,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) { u64 options[APP_OPTIONS_N_OPTIONS], dummy_secret = 1234; u32 server_index, server_st_index, server_local_st_index; - u32 dummy_port = 1234, client_index; + u32 dummy_port = 1234, client_index, server_wrk_index; u32 dummy_api_context = 4321, dummy_client_api_index = 1234; u32 dummy_server_api_index = ~0, sw_if_index = 0; session_endpoint_t server_sep = SESSION_ENDPOINT_NULL; @@ -318,6 +318,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; server = application_get (server_index); + server_wrk_index = application_get_default_worker (server)->wrk_index; SESSION_TEST ((server->ns_index == 0), "server should be in the default ns"); @@ -328,8 +329,8 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s != 0), "listener should exist in global table"); - SESSION_TEST ((s->app_index == server_index), "app_index should be that of " - "the server"); + SESSION_TEST ((s->app_wrk_index == server_wrk_index), "app_index should be" + " that of the server"); server_local_st_index = application_local_session_table (server); SESSION_TEST ((server_local_st_index == APP_INVALID_INDEX), "server shouldn't have access to local table"); @@ -357,6 +358,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; server = application_get (server_index); + server_wrk_index = application_get_default_worker (server)->wrk_index; SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)), "server should be in the right ns"); @@ -366,8 +368,8 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s != 0), "listener should exist in global table"); - SESSION_TEST ((s->app_index == server_index), "app_index should be that of " - "the server"); + SESSION_TEST ((s->app_wrk_index == server_wrk_index), "app_index should be" + " that of the server"); server_local_st_index = application_local_session_table (server); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle != SESSION_INVALID_HANDLE), @@ -506,6 +508,8 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; + server = application_get (server_index); + server_wrk_index = application_get_default_worker (server)->wrk_index; bind_args.app_index = server_index; error = vnet_bind (&bind_args); @@ -515,8 +519,8 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input) s = session_lookup_listener (server_st_index, &intf_sep); SESSION_TEST ((s != 0), "intf listener should exist in global table"); - SESSION_TEST ((s->app_index == server_index), "app_index should be that of " - "the server"); + SESSION_TEST ((s->app_wrk_index == server_wrk_index), "app_index should be " + "that of the server"); server_local_st_index = application_local_session_table (server); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle != SESSION_INVALID_HANDLE), @@ -1372,7 +1376,7 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input) char *show_listeners = "sh session listeners tcp verbose"; char *show_local_listeners = "sh app ns table default"; unformat_input_t tmp_input; - u32 server_index, app_index; + u32 server_index, app_index, server_wrk_index; u32 dummy_server_api_index = ~0, sw_if_index = 0; clib_error_t *error = 0; u8 is_filtered = 0; @@ -1380,6 +1384,7 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input) transport_connection_t *tc; u16 lcl_port = 1234, rmt_port = 4321; app_namespace_t *app_ns; + application_t *server; int verbose = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) @@ -1439,6 +1444,8 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input) error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; + server = application_get (server_index); + server_wrk_index = application_get_default_worker (server)->wrk_index; if (verbose) { @@ -1455,8 +1462,8 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input) SESSION_TEST ((tc != 0), "lookup 1.2.3.4 1234 5.6.7.8 4321 should be " "successful"); s = listen_session_get (tc->s_index); - SESSION_TEST ((s->app_index == server_index), "lookup should return the" - " server"); + SESSION_TEST ((s->app_wrk_index == server_wrk_index), "lookup should return" + " the server"); tc = session_lookup_connection_wt4 (0, &rmt_ip, &rmt_ip, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); diff --git a/src/vnet/session/stream_session.h b/src/vnet/session/stream_session.h index 34f122f24c3..567962a35db 100644 --- a/src/vnet/session/stream_session.h +++ b/src/vnet/session/stream_session.h @@ -60,8 +60,8 @@ typedef struct _stream_session_t /** Session index in per_thread pool */ u32 session_index; - /** stream server pool index */ - u32 app_index; + /** app worker pool index */ + u32 app_wrk_index; u8 thread_index; @@ -101,7 +101,7 @@ typedef struct local_session_ u32 session_index; /** Server index */ - u32 app_index; + u32 app_wrk_index; /** Segment index where fifos were allocated */ u32 svm_segment_index; @@ -118,7 +118,7 @@ typedef struct local_session_ /** * Client data */ - u32 client_index; + u32 client_wrk_index; u32 client_opaque; u64 server_evt_q; @@ -143,36 +143,36 @@ typedef struct _session_endpoint_extended #define _(type, name) type name; foreach_session_endpoint_fields #undef _ - u32 app_index; + u32 app_wrk_index; u32 opaque; u8 *hostname; } session_endpoint_extended_t; -#define SESSION_IP46_ZERO \ -{ \ - .ip6 = { \ - { 0, 0, }, \ - }, \ +#define SESSION_IP46_ZERO \ +{ \ + .ip6 = { \ + { 0, 0, }, \ + }, \ } -#define SESSION_ENDPOINT_NULL \ -{ \ +#define SESSION_ENDPOINT_NULL \ +{ \ .sw_if_index = ENDPOINT_INVALID_INDEX, \ - .ip = SESSION_IP46_ZERO, \ - .fib_index = ENDPOINT_INVALID_INDEX, \ - .is_ip4 = 0, \ - .port = 0, \ - .transport_proto = 0, \ + .ip = SESSION_IP46_ZERO, \ + .fib_index = ENDPOINT_INVALID_INDEX, \ + .is_ip4 = 0, \ + .port = 0, \ + .transport_proto = 0, \ } -#define SESSION_ENDPOINT_EXT_NULL \ -{ \ +#define SESSION_ENDPOINT_EXT_NULL \ +{ \ .sw_if_index = ENDPOINT_INVALID_INDEX, \ - .ip = SESSION_IP46_ZERO, \ - .fib_index = ENDPOINT_INVALID_INDEX, \ - .is_ip4 = 0, \ - .port = 0, \ - .transport_proto = 0, \ - .app_index = ENDPOINT_INVALID_INDEX, \ - .opaque = ENDPOINT_INVALID_INDEX, \ + .ip = SESSION_IP46_ZERO, \ + .fib_index = ENDPOINT_INVALID_INDEX, \ + .is_ip4 = 0, \ + .port = 0, \ + .transport_proto = 0, \ + .app_wrk_index = ENDPOINT_INVALID_INDEX, \ + .opaque = ENDPOINT_INVALID_INDEX, \ .hostname = 0, \ } |