+ fifo_segment_init (eqs);
+
+ /* Fifo segment filled only with mqs */
+ eqs->h->n_mqs = n_mqs;
+ vec_validate (app->rx_mqs, n_mqs - 1);
+
+ for (i = 0; i < n_mqs; i++)
+ {
+ app->rx_mqs[i].mq = fifo_segment_msg_q_alloc (eqs, i, cfg);
+ if (svm_msg_q_alloc_eventfd (app->rx_mqs[i].mq))
+ {
+ clib_warning ("eventfd returned");
+ fifo_segment_cleanup (eqs);
+ ssvm_delete (&eqs->ssvm);
+ return SESSION_E_EVENTFD_ALLOC;
+ }
+ app_rx_mqs_epoll_add (app, &app->rx_mqs[i]);
+ app->rx_mqs[i].app_index = app->app_index;
+ }
+
+ return 0;
+}
+
+u8
+application_use_private_rx_mqs (void)
+{
+ return session_main.use_private_rx_mqs;
+}
+
+fifo_segment_t *
+application_get_rx_mqs_segment (application_t *app)
+{
+ if (application_use_private_rx_mqs ())
+ return &app->rx_mqs_segment;
+ return session_main_get_wrk_mqs_segment ();
+}
+
+void
+application_enable_rx_mqs_nodes (u8 is_en)
+{
+ u8 state = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
+
+ foreach_vlib_main ()
+ vlib_node_set_state (this_vlib_main, appsl_rx_mqs_input_node.index, state);
+}
+
+static application_t *
+application_alloc (void)
+{
+ application_t *app;
+ pool_get (app_main.app_pool, app);
+ clib_memset (app, 0, sizeof (*app));
+ app->app_index = app - app_main.app_pool;
+ return app;
+}
+
+application_t *
+application_get (u32 app_index)
+{
+ if (app_index == APP_INVALID_INDEX)
+ return 0;
+ return pool_elt_at_index (app_main.app_pool, app_index);
+}
+
+application_t *
+application_get_if_valid (u32 app_index)
+{
+ if (pool_is_free_index (app_main.app_pool, app_index))
+ return 0;
+
+ return pool_elt_at_index (app_main.app_pool, app_index);
+}
+
+static void
+application_verify_cb_fns (session_cb_vft_t * cb_fns)
+{
+ if (cb_fns->session_accept_callback == 0)
+ clib_warning ("No accept callback function provided");
+ if (cb_fns->session_connected_callback == 0)
+ clib_warning ("No session connected callback function provided");
+ if (cb_fns->session_disconnect_callback == 0)
+ clib_warning ("No session disconnect callback function provided");
+ if (cb_fns->session_reset_callback == 0)
+ clib_warning ("No session reset callback function provided");
+}
+
+/**
+ * Check app config for given segment type
+ *
+ * Returns 1 on success and 0 otherwise
+ */
+static u8
+application_verify_cfg (ssvm_segment_type_t st)
+{
+ u8 is_valid;
+ if (st == SSVM_SEGMENT_MEMFD)
+ {
+ is_valid = (session_main_get_wrk_mqs_segment () != 0);
+ if (!is_valid)
+ clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
+ return is_valid;
+ }
+ else if (st == SSVM_SEGMENT_SHM)
+ {
+ is_valid = (session_main_get_wrk_mqs_segment () == 0);
+ if (!is_valid)
+ clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
+ return is_valid;
+ }
+ else
+ return 1;
+}
+
+static int
+application_alloc_and_init (app_init_args_t * a)
+{
+ ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
+ segment_manager_props_t *props;
+ application_t *app;
+ u64 *opts;
+
+ app = application_alloc ();
+ opts = a->options;
+ /*
+ * Make sure we support the requested configuration
+ */
+ if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN) &&
+ !(opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_MEMFD_FOR_BUILTIN))
+ seg_type = SSVM_SEGMENT_PRIVATE;
+
+ if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) &&
+ seg_type != SSVM_SEGMENT_MEMFD)
+ {
+ clib_warning ("mq eventfds can only be used if socket transport is "
+ "used for binary api");
+ return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
+ }
+
+ if (!application_verify_cfg (seg_type))
+ return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
+
+ if (opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS] &&
+ opts[APP_OPTIONS_PREALLOC_FIFO_HDRS])
+ return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
+
+ /* Check that the obvious things are properly set up */
+ application_verify_cb_fns (a->session_cb_vft);
+
+ app->flags = opts[APP_OPTIONS_FLAGS];
+ app->cb_fns = *a->session_cb_vft;
+ app->ns_index = opts[APP_OPTIONS_NAMESPACE];
+ app->proxied_transports = opts[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_props_init (props);
+ props->segment_size = opts[APP_OPTIONS_SEGMENT_SIZE];
+ props->prealloc_fifos = opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
+ props->prealloc_fifo_hdrs = opts[APP_OPTIONS_PREALLOC_FIFO_HDRS];
+ if (opts[APP_OPTIONS_ADD_SEGMENT_SIZE])
+ {
+ props->add_segment_size = opts[APP_OPTIONS_ADD_SEGMENT_SIZE];
+ props->add_segment = 1;
+ }
+ if (opts[APP_OPTIONS_RX_FIFO_SIZE])
+ props->rx_fifo_size = opts[APP_OPTIONS_RX_FIFO_SIZE];
+ if (opts[APP_OPTIONS_TX_FIFO_SIZE])
+ props->tx_fifo_size = opts[APP_OPTIONS_TX_FIFO_SIZE];
+ if (opts[APP_OPTIONS_EVT_QUEUE_SIZE])
+ props->evt_q_size = opts[APP_OPTIONS_EVT_QUEUE_SIZE];
+ if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
+ props->use_mq_eventfd = 1;
+ if (opts[APP_OPTIONS_TLS_ENGINE])
+ app->tls_engine = opts[APP_OPTIONS_TLS_ENGINE];
+ if (opts[APP_OPTIONS_MAX_FIFO_SIZE])
+ props->max_fifo_size = opts[APP_OPTIONS_MAX_FIFO_SIZE];
+ if (opts[APP_OPTIONS_HIGH_WATERMARK])
+ props->high_watermark = opts[APP_OPTIONS_HIGH_WATERMARK];
+ if (opts[APP_OPTIONS_LOW_WATERMARK])
+ props->low_watermark = opts[APP_OPTIONS_LOW_WATERMARK];
+ if (opts[APP_OPTIONS_PCT_FIRST_ALLOC])
+ props->pct_first_alloc = opts[APP_OPTIONS_PCT_FIRST_ALLOC];
+ props->segment_type = seg_type;
+
+ /* Add app to lookup by api_client_index table */
+ if (!application_is_builtin (app))
+ application_api_table_add (app->app_index, a->api_client_index);
+ else
+ application_name_table_add (app);
+
+ a->app_index = app->app_index;
+
+ APP_DBG ("New app name: %v api index: %u index %u", app->name,
+ a->api_client_index, app->app_index);
+
+ return 0;
+}
+
+static void
+application_free (application_t * app)
+{
+ app_worker_map_t *wrk_map;
+ app_worker_t *app_wrk;
+
+ /*
+ * The app event queue allocated in first segment is cleared with
+ * the segment manager. No need to explicitly free it.
+ */
+ APP_DBG ("Delete app name %v index: %d", app->name, app->app_index);
+
+ if (application_is_proxy (app))
+ application_remove_proxy (app);
+
+ /*
+ * Free workers
+ */
+
+ /* *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);
+
+ /*
+ * Free rx mqs if allocated
+ */
+ if (app->rx_mqs)
+ {
+ int i;
+ for (i = 0; i < vec_len (app->rx_mqs); i++)
+ app_rx_mqs_epoll_del (app, &app->rx_mqs[i]);
+
+ fifo_segment_cleanup (&app->rx_mqs_segment);
+ ssvm_delete (&app->rx_mqs_segment.ssvm);
+ vec_free (app->rx_mqs);
+ }
+
+ /*
+ * Cleanup remaining state
+ */
+ if (application_is_builtin (app))
+ application_name_table_del (app);
+ vec_free (app->name);
+ pool_put (app_main.app_pool, app);
+}
+
+static void
+application_detach_process (application_t * app, u32 api_client_index)
+{
+ vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
+ app_worker_map_t *wrk_map;
+ u32 *wrks = 0, *wrk_index;
+ app_worker_t *app_wrk;
+
+ if (api_client_index == ~0)
+ {
+ application_free (app);
+ return;
+ }
+
+ APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
+ app->app_index, api_client_index);
+
+ /* *INDENT-OFF* */
+ pool_foreach (wrk_map, app->worker_maps) {
+ app_wrk = app_worker_get (wrk_map->wrk_index);
+ if (app_wrk->api_client_index == api_client_index)
+ vec_add1 (wrks, app_wrk->wrk_index);
+ }
+ /* *INDENT-ON* */
+
+ if (!vec_len (wrks))
+ {
+ clib_warning ("no workers for app %u api_index %u", app->app_index,
+ api_client_index);
+ return;
+ }
+
+ args->app_index = app->app_index;
+ args->api_client_index = api_client_index;
+ vec_foreach (wrk_index, wrks)
+ {
+ app_wrk = app_worker_get (wrk_index[0]);
+ args->wrk_map_index = app_wrk->wrk_map_index;
+ args->is_add = 0;
+ vnet_app_worker_add_del (args);
+ }
+ vec_free (wrks);
+}
+
+void
+application_namespace_cleanup (app_namespace_t *app_ns)
+{
+ u32 *app_indices = 0, *app_index;
+ application_t *app;
+ u32 ns_index;
+
+ ns_index = app_namespace_index (app_ns);
+ pool_foreach (app, app_main.app_pool)
+ if (app->ns_index == ns_index)
+ vec_add1 (app_indices, app->ns_index);
+
+ vec_foreach (app_index, app_indices)
+ {
+ app = application_get (*app_index);
+
+ if (application_is_proxy (app))
+ application_remove_proxy (app);
+ app->flags &= ~APP_OPTIONS_FLAGS_IS_PROXY;
+
+ application_free (app);
+ }
+ vec_free (app_indices);
+}
+
+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);
+}
+
+app_worker_t *
+application_get_default_worker (application_t * app)
+{
+ return application_get_worker (app, 0);
+}
+
+u32
+application_n_workers (application_t * app)
+{
+ return pool_elts (app->worker_maps);
+}
+
+app_worker_t *
+application_listener_select_worker (session_t * ls)
+{
+ application_t *app;
+ app_listener_t *al;
+
+ app = application_get (ls->app_index);
+ al = app_listener_get (app, ls->al_index);
+ return app_listener_select_worker (app, al);
+}
+
+int
+application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
+{
+ app_worker_map_t *wrk_map;
+ app_worker_t *app_wrk;