2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/session.h>
21 * Pool from which we allocate all applications
23 static application_t *app_pool;
26 * Hash table of apps by api client index
28 static uword *app_by_api_client_index;
31 * Default application event queue size
33 static u32 default_app_evt_queue_size = 128;
36 application_api_queue_is_full (application_t * app)
38 unix_shared_memory_queue_t *q;
40 /* builtin servers are always OK */
41 if (app->api_client_index == ~0)
44 q = vl_api_client_index_to_input_queue (app->api_client_index);
48 if (q->cursize == q->maxsize)
54 application_table_add (application_t * app)
56 hash_set (app_by_api_client_index, app->api_client_index, app->index);
60 application_table_del (application_t * app)
62 hash_unset (app_by_api_client_index, app->api_client_index);
66 application_lookup (u32 api_client_index)
69 p = hash_get (app_by_api_client_index, api_client_index);
71 return application_get (p[0]);
80 pool_get (app_pool, app);
81 memset (app, 0, sizeof (*app));
82 app->index = application_get_index (app);
83 app->connects_seg_manager = ~0;
84 app->first_segment_manager = ~0;
86 clib_warning ("[%d] New app (%d)", getpid (), app->index);
91 application_del (application_t * app)
93 segment_manager_t *sm;
95 u32 index, *handles = 0;
97 vnet_unbind_args_t _a, *a = &_a;
100 * The app event queue allocated in first segment is cleared with
101 * the segment manager. No need to explicitly free it.
104 clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
107 * Cleanup segment managers
109 if ((app->connects_seg_manager != (u32) ~ 0) &&
110 (app->connects_seg_manager != app->first_segment_manager))
112 sm = segment_manager_get (app->connects_seg_manager);
113 segment_manager_del (sm);
117 hash_foreach (handle, index, app->listeners_table,
119 vec_add1 (handles, handle);
123 /* Actual listener cleanup */
124 for (i = 0; i < vec_len (handles); i++)
126 a->app_index = app->index;
127 a->handle = handles[i];
128 /* seg manager is removed when unbind completes */
132 if (app->first_segment_manager != ~0)
134 sm = segment_manager_get (app->first_segment_manager);
135 segment_manager_first_segment_maybe_del (sm);
138 application_table_del (app);
139 pool_put (app_pool, app);
143 application_verify_cb_fns (session_cb_vft_t * cb_fns)
145 if (cb_fns->session_accept_callback == 0)
146 clib_warning ("No accept callback function provided");
147 if (cb_fns->session_connected_callback == 0)
148 clib_warning ("No session connected callback function provided");
149 if (cb_fns->session_disconnect_callback == 0)
150 clib_warning ("No session disconnect callback function provided");
151 if (cb_fns->session_reset_callback == 0)
152 clib_warning ("No session reset callback function provided");
156 application_init (application_t * app, u32 api_client_index, u64 * options,
157 session_cb_vft_t * cb_fns)
159 segment_manager_t *sm;
160 segment_manager_properties_t *props;
161 u32 app_evt_queue_size, first_seg_size;
164 app_evt_queue_size = options[APP_EVT_QUEUE_SIZE] > 0 ?
165 options[APP_EVT_QUEUE_SIZE] : default_app_evt_queue_size;
167 /* Setup segment manager */
168 sm = segment_manager_new ();
169 sm->app_index = app->index;
170 props = &app->sm_properties;
171 props->add_segment_size = options[SESSION_OPTIONS_ADD_SEGMENT_SIZE];
172 props->rx_fifo_size = options[SESSION_OPTIONS_RX_FIFO_SIZE];
173 props->tx_fifo_size = options[SESSION_OPTIONS_TX_FIFO_SIZE];
174 props->add_segment = props->add_segment_size != 0;
175 props->preallocated_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
176 props->use_private_segment = options[APP_OPTIONS_FLAGS]
177 & APP_OPTIONS_FLAGS_BUILTIN_APP;
178 props->private_segment_count = options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT];
179 props->private_segment_size = options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE];
181 first_seg_size = options[SESSION_OPTIONS_SEGMENT_SIZE];
182 if ((rv = segment_manager_init (sm, props, first_seg_size)))
185 app->first_segment_manager = segment_manager_index (sm);
186 app->api_client_index = api_client_index;
187 app->flags = options[APP_OPTIONS_FLAGS];
188 app->cb_fns = *cb_fns;
190 /* Allocate app event queue in the first shared-memory segment */
191 app->event_queue = segment_manager_alloc_queue (sm, app_evt_queue_size);
193 /* Check that the obvious things are properly set up */
194 application_verify_cb_fns (cb_fns);
196 /* Add app to lookup by api_client_index table */
197 application_table_add (app);
203 application_get (u32 index)
205 return pool_elt_at_index (app_pool, index);
209 application_get_if_valid (u32 index)
211 if (pool_is_free_index (app_pool, index))
214 return pool_elt_at_index (app_pool, index);
218 application_get_index (application_t * app)
220 return app - app_pool;
223 static segment_manager_t *
224 application_alloc_segment_manager (application_t * app)
226 segment_manager_t *sm = 0;
228 if (app->first_segment_manager != (u32) ~ 0)
230 sm = segment_manager_get (app->first_segment_manager);
234 sm = segment_manager_new ();
235 if (segment_manager_init (sm, &app->sm_properties, 0))
241 * Start listening local transport endpoint for requested transport.
243 * Creates a 'dummy' stream session with state LISTENING to be used in session
244 * lookups, prior to establishing connection. Requests transport to build
245 * it's own specific listening connection.
248 application_start_listen (application_t * srv, session_type_t session_type,
249 transport_endpoint_t * tep, u64 * res)
251 segment_manager_t *sm;
255 s = listen_session_new (session_type);
256 s->app_index = srv->index;
258 if (stream_session_listen (s, tep))
261 /* Allocate segment manager. All sessions derived out of a listen session
262 * have fifos allocated by the same segment manager. */
263 sm = application_alloc_segment_manager (srv);
267 /* Add to app's listener table. Useful to find all child listeners
268 * when app goes down, although, just for unbinding this is not needed */
269 handle = listen_session_get_handle (s);
270 hash_set (srv->listeners_table, handle, segment_manager_index (sm));
276 listen_session_del (s);
281 * Stop listening on session associated to handle
284 application_stop_listen (application_t * srv, u64 handle)
286 stream_session_t *listener;
288 segment_manager_t *sm;
290 if (srv && hash_get (srv->listeners_table, handle) == 0)
292 clib_warning ("app doesn't own handle %llu!", handle);
296 listener = listen_session_get_from_handle (handle);
297 stream_session_stop_listen (listener);
299 indexp = hash_get (srv->listeners_table, handle);
302 sm = segment_manager_get (*indexp);
303 segment_manager_del (sm);
304 srv->first_segment_manager = ~0;
305 hash_unset (srv->listeners_table, handle);
306 listen_session_del (listener);
312 application_open_session (application_t * app, session_type_t sst,
313 transport_endpoint_t * tep, u32 api_context)
315 segment_manager_t *sm;
316 transport_connection_t *tc = 0;
319 /* Make sure we have a segment manager for connects */
320 if (app->connects_seg_manager == (u32) ~ 0)
322 sm = application_alloc_segment_manager (app);
325 app->connects_seg_manager = segment_manager_index (sm);
328 if ((rv = stream_session_open (app->index, sst, tep, &tc)))
331 /* Store api_context for when the reply comes. Not the nicest thing
332 * but better allocating a separate half-open pool. */
333 tc->s_index = api_context;
339 application_get_connect_segment_manager (application_t * app)
341 ASSERT (app->connects_seg_manager != (u32) ~ 0);
342 return segment_manager_get (app->connects_seg_manager);
346 application_get_listen_segment_manager (application_t * app,
347 stream_session_t * s)
350 smp = hash_get (app->listeners_table, listen_session_get_handle (s));
352 return segment_manager_get (*smp);
356 app_get_name_from_reg_index (application_t * app)
360 vl_api_registration_t *regp;
361 regp = vl_api_client_index_to_registration (app->api_client_index);
363 app_name = format (0, "builtin-%d%c", app->index, 0);
365 app_name = format (0, "%s%c", regp->name, 0);
371 format_application_listener (u8 * s, va_list * args)
373 application_t *app = va_arg (*args, application_t *);
374 u64 handle = va_arg (*args, u64);
375 u32 index = va_arg (*args, u32);
376 int verbose = va_arg (*args, int);
377 stream_session_t *listener;
383 s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
384 "API Client", "ListenerID", "SegManager");
386 s = format (s, "%-40s%-20s", "Connection", "App");
391 app_name = app_get_name_from_reg_index (app);
392 listener = listen_session_get_from_handle (handle);
393 str = format (0, "%U", format_stream_session, listener, verbose);
397 s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
398 app->api_client_index, handle, index);
401 s = format (s, "%-40s%-20s", str, app_name);
408 application_format_connects (application_t * app, int verbose)
410 vlib_main_t *vm = vlib_get_main ();
411 segment_manager_t *sm;
412 u8 *app_name, *s = 0;
419 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
420 "API Client", "SegManager");
422 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
427 if (app->connects_seg_manager == (u32) ~ 0)
430 app_name = app_get_name_from_reg_index (app);
432 /* Across all fifo segments */
433 sm = segment_manager_get (app->connects_seg_manager);
434 for (j = 0; j < vec_len (sm->segment_indices); j++)
436 svm_fifo_segment_private_t *fifo_segment;
440 fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]);
441 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
444 u32 session_index, thread_index;
445 stream_session_t *session;
447 session_index = fifo->master_session_index;
448 thread_index = fifo->master_thread_index;
450 session = stream_session_get (session_index, thread_index);
451 str = format (0, "%U", format_stream_session, session, verbose);
454 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
455 app->api_client_index, app->connects_seg_manager);
457 s = format (s, "%-40s%-20s", str, app_name);
459 vlib_cli_output (vm, "%v", s);
460 vec_reset_length (s);
472 format_application (u8 * s, va_list * args)
474 application_t *app = va_arg (*args, application_t *);
475 CLIB_UNUSED (int verbose) = va_arg (*args, int);
481 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s", "Index", "Name",
482 "API Client", "Add seg size", "Rx fifo size",
485 s = format (s, "%-10s%-20s%-20s", "Index", "Name", "API Client");
489 app_name = app_get_name_from_reg_index (app);
491 s = format (s, "%-10d%-20s%-15d%-15d%-15d%-15d", app->index, app_name,
492 app->api_client_index, app->sm_properties.add_segment_size,
493 app->sm_properties.rx_fifo_size,
494 app->sm_properties.tx_fifo_size);
496 s = format (s, "%-10d%-20s%-20d", app->index, app_name,
497 app->api_client_index);
501 static clib_error_t *
502 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
503 vlib_cli_command_t * cmd)
510 if (!session_manager_is_enabled ())
512 clib_error_return (0, "session layer is not enabled");
515 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
517 if (unformat (input, "server"))
519 else if (unformat (input, "client"))
521 else if (unformat (input, "verbose"))
531 if (pool_elts (app_pool))
533 vlib_cli_output (vm, "%U", format_application_listener,
534 0 /* header */ , 0, 0,
537 pool_foreach (app, app_pool,
539 /* App's listener sessions */
540 if (hash_elts (app->listeners_table) == 0)
542 hash_foreach (handle, index, app->listeners_table,
544 vlib_cli_output (vm, "%U", format_application_listener, app,
545 handle, index, verbose);
551 vlib_cli_output (vm, "No active server bindings");
556 if (pool_elts (app_pool))
558 application_format_connects (0, verbose);
561 pool_foreach (app, app_pool,
563 if (app->connects_seg_manager == (u32)~0)
565 application_format_connects (app, verbose);
570 vlib_cli_output (vm, "No active client bindings");
573 /* Print app related info */
574 if (!do_server && !do_client)
576 vlib_cli_output (vm, "%U", format_application, 0, verbose);
577 pool_foreach (app, app_pool, (
579 vlib_cli_output (vm, "%U",
580 format_application, app,
590 VLIB_CLI_COMMAND (show_app_command, static) =
593 .short_help = "show app [server|client] [verbose]",
594 .function = show_app_command_fn,
599 * fd.io coding-style-patch-verification: ON
602 * eval: (c-set-style "gnu")