2 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/application_namespace.h>
19 #include <vnet/session/session.h>
21 static app_main_t app_main;
23 static app_listener_t *
24 app_listener_alloc (application_t * app)
26 app_listener_t *app_listener;
27 pool_get (app->listeners, app_listener);
28 clib_memset (app_listener, 0, sizeof (*app_listener));
29 app_listener->al_index = app_listener - app->listeners;
33 static app_listener_t *
34 app_listener_get (application_t * app, u32 app_listener_index)
36 return pool_elt_at_index (app->listeners, app_listener_index);
40 app_listener_free (application_t * app, app_listener_t * app_listener)
42 clib_bitmap_free (app_listener->workers);
43 pool_put (app->listeners, app_listener);
45 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
48 static app_listener_t *
49 app_local_listener_alloc (application_t * app)
51 app_listener_t *app_listener;
52 pool_get (app->local_listeners, app_listener);
53 clib_memset (app_listener, 0, sizeof (*app_listener));
54 app_listener->al_index = app_listener - app->local_listeners;
58 static app_listener_t *
59 app_local_listener_get (application_t * app, u32 app_listener_index)
61 return pool_elt_at_index (app->local_listeners, app_listener_index);
65 app_local_listener_free (application_t * app, app_listener_t * app_listener)
67 clib_bitmap_free (app_listener->workers);
68 pool_put (app->local_listeners, app_listener);
70 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
73 static app_worker_map_t *
74 app_worker_map_alloc (application_t * app)
76 app_worker_map_t *map;
77 pool_get (app->worker_maps, map);
78 clib_memset (map, 0, sizeof (*map));
83 app_worker_map_index (application_t * app, app_worker_map_t * map)
85 return (map - app->worker_maps);
89 app_worker_map_free (application_t * app, app_worker_map_t * map)
91 pool_put (app->worker_maps, map);
94 static app_worker_map_t *
95 app_worker_map_get (application_t * app, u32 map_index)
97 if (pool_is_free_index (app->worker_maps, map_index))
99 return pool_elt_at_index (app->worker_maps, map_index);
103 app_get_name (application_t * app)
109 application_session_table (application_t * app, u8 fib_proto)
111 app_namespace_t *app_ns;
112 app_ns = app_namespace_get (app->ns_index);
113 if (!application_has_global_scope (app))
114 return APP_INVALID_INDEX;
115 if (fib_proto == FIB_PROTOCOL_IP4)
116 return session_lookup_get_index_for_fib (fib_proto,
117 app_ns->ip4_fib_index);
119 return session_lookup_get_index_for_fib (fib_proto,
120 app_ns->ip6_fib_index);
124 application_local_session_table (application_t * app)
126 app_namespace_t *app_ns;
127 if (!application_has_local_scope (app))
128 return APP_INVALID_INDEX;
129 app_ns = app_namespace_get (app->ns_index);
130 return app_ns->local_table_index;
134 application_local_listener_session_endpoint (local_session_t * ll,
135 session_endpoint_t * sep)
137 sep->transport_proto =
138 session_type_transport_proto (ll->listener_session_type);
139 sep->port = ll->port;
140 sep->is_ip4 = ll->listener_session_type & 1;
144 * Returns app name for app-index
147 application_name_from_index (u32 app_index)
149 application_t *app = application_get (app_index);
152 return app_get_name (app);
156 application_api_table_add (u32 app_index, u32 api_client_index)
158 if (api_client_index != APP_INVALID_INDEX)
159 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
163 application_api_table_del (u32 api_client_index)
165 hash_unset (app_main.app_by_api_client_index, api_client_index);
169 application_name_table_add (application_t * app)
171 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
175 application_name_table_del (application_t * app)
177 hash_unset_mem (app_main.app_by_name, app->name);
181 application_lookup (u32 api_client_index)
184 p = hash_get (app_main.app_by_api_client_index, api_client_index);
186 return application_get_if_valid (p[0]);
192 application_lookup_name (const u8 * name)
195 p = hash_get_mem (app_main.app_by_name, name);
197 return application_get (p[0]);
203 application_alloc (void)
206 pool_get (app_main.app_pool, app);
207 clib_memset (app, 0, sizeof (*app));
208 app->app_index = app - app_main.app_pool;
213 application_get (u32 app_index)
215 if (app_index == APP_INVALID_INDEX)
217 return pool_elt_at_index (app_main.app_pool, app_index);
221 application_get_if_valid (u32 app_index)
223 if (pool_is_free_index (app_main.app_pool, app_index))
226 return pool_elt_at_index (app_main.app_pool, app_index);
230 application_verify_cb_fns (session_cb_vft_t * cb_fns)
232 if (cb_fns->session_accept_callback == 0)
233 clib_warning ("No accept callback function provided");
234 if (cb_fns->session_connected_callback == 0)
235 clib_warning ("No session connected callback function provided");
236 if (cb_fns->session_disconnect_callback == 0)
237 clib_warning ("No session disconnect callback function provided");
238 if (cb_fns->session_reset_callback == 0)
239 clib_warning ("No session reset callback function provided");
243 * Check app config for given segment type
245 * Returns 1 on success and 0 otherwise
248 application_verify_cfg (ssvm_segment_type_t st)
251 if (st == SSVM_SEGMENT_MEMFD)
253 is_valid = (session_manager_get_evt_q_segment () != 0);
255 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
258 else if (st == SSVM_SEGMENT_SHM)
260 is_valid = (session_manager_get_evt_q_segment () == 0);
262 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
270 application_alloc_and_init (app_init_args_t * a)
272 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
273 segment_manager_properties_t *props;
274 vl_api_registration_t *reg;
278 app = application_alloc ();
279 options = a->options;
281 * Make sure we support the requested configuration
283 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
285 reg = vl_api_client_index_to_registration (a->api_client_index);
287 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
288 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
289 seg_type = SSVM_SEGMENT_SHM;
293 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
295 clib_warning ("mq eventfds can only be used if socket transport is "
297 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
299 seg_type = SSVM_SEGMENT_PRIVATE;
302 if (!application_verify_cfg (seg_type))
303 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
305 /* Check that the obvious things are properly set up */
306 application_verify_cb_fns (a->session_cb_vft);
308 app->flags = options[APP_OPTIONS_FLAGS];
309 app->cb_fns = *a->session_cb_vft;
310 app->ns_index = options[APP_OPTIONS_NAMESPACE];
311 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
312 app->name = vec_dup (a->name);
314 /* If no scope enabled, default to global */
315 if (!application_has_global_scope (app)
316 && !application_has_local_scope (app))
317 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
319 props = application_segment_manager_properties (app);
320 segment_manager_properties_init (props);
321 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
322 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
323 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
325 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
326 props->add_segment = 1;
328 if (options[APP_OPTIONS_RX_FIFO_SIZE])
329 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
330 if (options[APP_OPTIONS_TX_FIFO_SIZE])
331 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
332 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
333 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
334 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
335 props->use_mq_eventfd = 1;
336 if (options[APP_OPTIONS_TLS_ENGINE])
337 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
338 props->segment_type = seg_type;
340 /* Add app to lookup by api_client_index table */
341 if (!application_is_builtin (app))
342 application_api_table_add (app->app_index, a->api_client_index);
344 application_name_table_add (app);
346 a->app_index = app->app_index;
348 APP_DBG ("New app name: %v api index: %u index %u", app->name,
349 app->api_client_index, app->app_index);
355 application_free (application_t * app)
357 app_worker_map_t *wrk_map;
358 app_worker_t *app_wrk;
361 session_endpoint_t sep;
364 * The app event queue allocated in first segment is cleared with
365 * the segment manager. No need to explicitly free it.
367 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
368 app->api_client_index, app->app_index);
370 if (application_is_proxy (app))
371 application_remove_proxy (app);
378 pool_flush (wrk_map, app->worker_maps, ({
379 app_wrk = app_worker_get (wrk_map->wrk_index);
380 app_worker_free (app_wrk);
383 pool_free (app->worker_maps);
386 * Free local listeners. Global table unbinds stop local listeners
387 * as well, but if we have only local binds, these won't be cleaned up.
388 * Don't bother with local accepted sessions, we clean them when
389 * cleaning up the worker.
391 table_index = application_local_session_table (app);
393 pool_foreach (ll, app->local_listen_sessions, ({
394 application_local_listener_session_endpoint (ll, &sep);
395 session_lookup_del_session_endpoint (table_index, &sep);
398 pool_free (app->local_listen_sessions);
401 * Cleanup remaining state
403 if (application_is_builtin (app))
404 application_name_table_del (app);
405 vec_free (app->name);
406 vec_free (app->tls_cert);
407 vec_free (app->tls_key);
408 pool_put (app_main.app_pool, app);
412 application_detach_process (application_t * app, u32 api_client_index)
414 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
415 app_worker_map_t *wrk_map;
416 u32 *wrks = 0, *wrk_index;
417 app_worker_t *app_wrk;
419 if (api_client_index == ~0)
421 application_free (app);
425 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
426 app->app_index, app->api_client_index);
429 pool_foreach (wrk_map, app->worker_maps, ({
430 app_wrk = app_worker_get (wrk_map->wrk_index);
431 if (app_wrk->api_client_index == api_client_index)
432 vec_add1 (wrks, app_wrk->wrk_index);
438 clib_warning ("no workers for app %u api_index %u", app->app_index,
443 args->app_index = app->app_index;
444 args->api_client_index = api_client_index;
445 vec_foreach (wrk_index, wrks)
447 app_wrk = app_worker_get (wrk_index[0]);
448 args->wrk_map_index = app_wrk->wrk_map_index;
450 vnet_app_worker_add_del (args);
456 application_get_worker (application_t * app, u32 wrk_map_index)
458 app_worker_map_t *map;
459 map = app_worker_map_get (app, wrk_map_index);
462 return app_worker_get (map->wrk_index);
466 application_get_default_worker (application_t * app)
468 return application_get_worker (app, 0);
472 application_n_workers (application_t * app)
474 return pool_elts (app->worker_maps);
478 application_listener_select_worker (session_t * ls, u8 is_local)
480 app_listener_t *app_listener;
484 app = application_get (ls->app_index);
486 app_listener = app_listener_get (app, ls->listener_db_index);
488 app_listener = app_local_listener_get (app, ls->listener_db_index);
490 wrk_index = clib_bitmap_next_set (app_listener->workers,
491 app_listener->accept_rotor + 1);
493 wrk_index = clib_bitmap_first_set (app_listener->workers);
495 ASSERT (wrk_index != ~0);
496 app_listener->accept_rotor = wrk_index;
497 return application_get_worker (app, wrk_index);
501 app_worker_alloc (application_t * app)
503 app_worker_t *app_wrk;
504 pool_get (app_main.workers, app_wrk);
505 clib_memset (app_wrk, 0, sizeof (*app_wrk));
506 app_wrk->wrk_index = app_wrk - app_main.workers;
507 app_wrk->app_index = app->app_index;
508 app_wrk->wrk_map_index = ~0;
509 app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
510 app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
511 app_wrk->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
512 APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index);
517 app_worker_get (u32 wrk_index)
519 return pool_elt_at_index (app_main.workers, wrk_index);
523 app_worker_get_if_valid (u32 wrk_index)
525 if (pool_is_free_index (app_main.workers, wrk_index))
527 return pool_elt_at_index (app_main.workers, wrk_index);
531 app_worker_free (app_worker_t * app_wrk)
533 application_t *app = application_get (app_wrk->app_index);
534 vnet_unbind_args_t _a, *a = &_a;
535 u64 handle, *handles = 0;
536 segment_manager_t *sm;
545 hash_foreach (handle, sm_index, app_wrk->listeners_table,
547 vec_add1 (handles, handle);
548 sm = segment_manager_get (sm_index);
549 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
553 for (i = 0; i < vec_len (handles); i++)
555 a->app_index = app->app_index;
556 a->wrk_map_index = app_wrk->wrk_map_index;
557 a->handle = handles[i];
558 /* seg manager is removed when unbind completes */
563 * Connects segment manager cleanup
566 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
568 sm = segment_manager_get (app_wrk->connects_seg_manager);
569 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
570 segment_manager_init_del (sm);
573 /* If first segment manager is used by a listener */
574 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
575 && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
577 sm = segment_manager_get (app_wrk->first_segment_manager);
578 sm->first_is_protected = 0;
579 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
580 /* .. and has no fifos, e.g. it might be used for redirected sessions,
582 if (!segment_manager_has_fifos (sm))
583 segment_manager_del (sm);
589 app_worker_local_sessions_free (app_wrk);
591 pool_put (app_main.workers, app_wrk);
593 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
597 app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk)
599 app_worker_map_t *wrk_map;
600 app_worker_t *app_wrk;
601 segment_manager_t *sm;
604 app_wrk = app_worker_alloc (app);
605 wrk_map = app_worker_map_alloc (app);
606 wrk_map->wrk_index = app_wrk->wrk_index;
607 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
610 * Setup first segment manager
612 sm = segment_manager_new ();
613 sm->app_wrk_index = app_wrk->wrk_index;
615 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
616 app->sm_properties.prealloc_fifos)))
618 app_worker_free (app_wrk);
621 sm->first_is_protected = 1;
626 app_wrk->first_segment_manager = segment_manager_index (sm);
627 app_wrk->listeners_table = hash_create (0, sizeof (u64));
628 app_wrk->event_queue = segment_manager_event_queue (sm);
629 app_wrk->app_is_builtin = application_is_builtin (app);
632 * Segment manager for local sessions
634 sm = segment_manager_new ();
635 sm->app_wrk_index = app_wrk->wrk_index;
636 app_wrk->local_segment_manager = segment_manager_index (sm);
637 app_wrk->local_connects = hash_create (0, sizeof (u64));
645 app_worker_get_app (u32 wrk_index)
647 app_worker_t *app_wrk;
648 app_wrk = app_worker_get_if_valid (wrk_index);
651 return application_get_if_valid (app_wrk->app_index);
654 static segment_manager_t *
655 app_worker_alloc_segment_manager (app_worker_t * app_wrk)
657 segment_manager_t *sm = 0;
659 /* If the first segment manager is not in use, don't allocate a new one */
660 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
661 && app_wrk->first_segment_manager_in_use == 0)
663 sm = segment_manager_get (app_wrk->first_segment_manager);
664 app_wrk->first_segment_manager_in_use = 1;
668 sm = segment_manager_new ();
669 sm->app_wrk_index = app_wrk->wrk_index;
675 app_worker_start_listen (app_worker_t * app_wrk, session_t * ls)
677 segment_manager_t *sm;
679 /* Allocate segment manager. All sessions derived out of a listen session
680 * have fifos allocated by the same segment manager. */
681 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
684 /* Add to app's listener table. Useful to find all child listeners
685 * when app goes down, although, just for unbinding this is not needed */
686 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
687 segment_manager_index (sm));
690 && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
692 if (session_alloc_fifos (sm, ls))
699 app_worker_stop_listen (app_worker_t * app_wrk, session_handle_t handle)
701 segment_manager_t *sm;
704 sm_indexp = hash_get (app_wrk->listeners_table, handle);
705 if (PREDICT_FALSE (!sm_indexp))
707 clib_warning ("listener handle was removed %llu!", handle);
711 sm = segment_manager_get (*sm_indexp);
712 if (app_wrk->first_segment_manager == *sm_indexp)
714 /* Delete sessions but don't remove segment manager */
715 app_wrk->first_segment_manager_in_use = 0;
716 segment_manager_del_sessions (sm);
720 segment_manager_init_del (sm);
722 hash_unset (app_wrk->listeners_table, handle);
728 app_worker_own_session (app_worker_t * app_wrk, session_t * s)
730 segment_manager_t *sm;
731 svm_fifo_t *rxf, *txf;
733 if (s->session_state == SESSION_STATE_LISTENING)
735 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
736 u64 lsh = listen_session_get_handle (s);
737 app_listener_t *app_listener;
743 hash_unset (old_wrk->listeners_table, lsh);
744 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
747 hash_set (app_wrk->listeners_table, lsh, segment_manager_index (sm));
748 s->app_wrk_index = app_wrk->wrk_index;
750 app = application_get (old_wrk->app_index);
754 app_listener = app_listener_get (app, s->listener_db_index);
755 app_listener->workers = clib_bitmap_set (app_listener->workers,
756 app_wrk->wrk_map_index, 1);
757 app_listener->workers = clib_bitmap_set (app_listener->workers,
758 old_wrk->wrk_map_index, 0);
762 s->app_wrk_index = app_wrk->wrk_index;
773 sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk);
774 if (session_alloc_fifos (sm, s))
777 if (!svm_fifo_is_empty (rxf))
779 clib_memcpy_fast (s->rx_fifo->data, rxf->data, rxf->nitems);
780 s->rx_fifo->head = rxf->head;
781 s->rx_fifo->tail = rxf->tail;
782 s->rx_fifo->cursize = rxf->cursize;
785 if (!svm_fifo_is_empty (txf))
787 clib_memcpy_fast (s->tx_fifo->data, txf->data, txf->nitems);
788 s->tx_fifo->head = txf->head;
789 s->tx_fifo->tail = txf->tail;
790 s->tx_fifo->cursize = txf->cursize;
793 segment_manager_dealloc_fifos (rxf->segment_index, rxf, txf);
799 * Start listening local transport endpoint for requested transport.
801 * Creates a 'dummy' stream session with state LISTENING to be used in session
802 * lookups, prior to establishing connection. Requests transport to build
803 * it's own specific listening connection.
806 application_start_listen (application_t * app,
807 session_endpoint_cfg_t * sep_ext,
808 session_handle_t * res)
810 app_listener_t *app_listener;
811 u32 table_index, fib_proto;
812 session_endpoint_t *sep;
813 app_worker_t *app_wrk;
819 * Check if sep is already listened on
821 sep = (session_endpoint_t *) sep_ext;
822 fib_proto = session_endpoint_fib_proto (sep);
823 table_index = application_session_table (app, fib_proto);
824 lh = session_lookup_endpoint_listener (table_index, sep, 1);
825 if (lh != SESSION_INVALID_HANDLE)
827 ls = listen_session_get_from_handle (lh);
828 if (ls->app_index != app->app_index)
829 return VNET_API_ERROR_ADDRESS_IN_USE;
831 app_wrk = app_worker_get (sep_ext->app_wrk_index);
832 if (ls->app_wrk_index == app_wrk->wrk_index)
833 return VNET_API_ERROR_ADDRESS_IN_USE;
835 if (app_worker_start_listen (app_wrk, ls))
838 app_listener = app_listener_get (app, ls->listener_db_index);
839 app_listener->workers = clib_bitmap_set (app_listener->workers,
840 app_wrk->wrk_map_index, 1);
842 *res = listen_session_get_handle (ls);
847 * Allocate new listener for application
849 sst = session_type_from_proto_and_ip (sep_ext->transport_proto,
851 ls = listen_session_new (0, sst);
852 ls->app_index = app->app_index;
853 lh = listen_session_get_handle (ls);
854 if (session_listen (ls, sep_ext))
858 ls = listen_session_get_from_handle (lh);
859 app_listener = app_listener_alloc (app);
860 ls->listener_db_index = app_listener->al_index;
863 * Setup app worker as a listener
865 app_wrk = app_worker_get (sep_ext->app_wrk_index);
866 ls->app_wrk_index = app_wrk->wrk_index;
867 if (app_worker_start_listen (app_wrk, ls))
869 app_listener->workers = clib_bitmap_set (app_listener->workers,
870 app_wrk->wrk_map_index, 1);
876 listen_session_del (ls);
881 * Stop listening on session associated to handle
883 * @param handle listener handle
884 * @param app_index index of the app owning the handle.
885 * @param app_wrk_index index of the worker requesting the stop
888 application_stop_listen (u32 app_index, u32 app_wrk_index,
889 session_handle_t handle)
891 app_listener_t *app_listener;
893 app_worker_t *app_wrk;
896 listener = listen_session_get_from_handle (handle);
897 app = application_get (app_index);
898 if (PREDICT_FALSE (!app || app->app_index != listener->app_index))
900 clib_warning ("app doesn't own handle %llu!", handle);
904 app_listener = app_listener_get (app, listener->listener_db_index);
905 if (!clib_bitmap_get (app_listener->workers, app_wrk_index))
907 clib_warning ("worker %u not listening on handle %lu", app_wrk_index,
912 app_wrk = application_get_worker (app, app_wrk_index);
913 app_worker_stop_listen (app_wrk, handle);
914 clib_bitmap_set_no_check (app_listener->workers, app_wrk_index, 0);
916 if (clib_bitmap_is_zero (app_listener->workers))
918 session_stop_listen (listener);
919 app_listener_free (app, app_listener);
920 listen_session_del (listener);
927 app_worker_open_session (app_worker_t * app, session_endpoint_t * sep,
932 /* Make sure we have a segment manager for connects */
933 app_worker_alloc_connects_segment_manager (app);
935 if ((rv = session_open (app->wrk_index, sep, api_context)))
942 app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
944 segment_manager_t *sm;
946 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
948 sm = app_worker_alloc_segment_manager (app_wrk);
951 app_wrk->connects_seg_manager = segment_manager_index (sm);
957 app_worker_get_connect_segment_manager (app_worker_t * app)
959 ASSERT (app->connects_seg_manager != (u32) ~ 0);
960 return segment_manager_get (app->connects_seg_manager);
964 app_worker_get_or_alloc_connect_segment_manager (app_worker_t * app_wrk)
966 if (app_wrk->connects_seg_manager == (u32) ~ 0)
967 app_worker_alloc_connects_segment_manager (app_wrk);
968 return segment_manager_get (app_wrk->connects_seg_manager);
972 app_worker_get_listen_segment_manager (app_worker_t * app,
973 session_t * listener)
976 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
978 return segment_manager_get (*smp);
982 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
984 svm_fifo_segment_private_t *fs;
985 app_worker_map_t *wrk_map;
986 app_worker_t *app_wrk;
987 segment_manager_t *sm;
991 app = application_get (a->app_index);
993 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
994 "App %u does not exist", a->app_index);
998 if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
999 return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv);
1001 /* Map worker api index to the app */
1002 app_wrk->api_client_index = a->api_client_index;
1003 application_api_table_add (app->app_index, a->api_client_index);
1005 sm = segment_manager_get (app_wrk->first_segment_manager);
1006 fs = segment_manager_get_segment_w_lock (sm, 0);
1007 a->segment = &fs->ssvm;
1008 a->segment_handle = segment_manager_segment_handle (sm, fs);
1009 segment_manager_segment_reader_unlock (sm);
1010 a->evt_q = app_wrk->event_queue;
1011 a->wrk_map_index = app_wrk->wrk_map_index;
1015 wrk_map = app_worker_map_get (app, a->wrk_map_index);
1017 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1018 "App %u does not have worker %u",
1019 app->app_index, a->wrk_map_index);
1020 app_wrk = app_worker_get (wrk_map->wrk_index);
1022 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1023 "No worker %u", a->wrk_map_index);
1024 application_api_table_del (app_wrk->api_client_index);
1025 app_worker_free (app_wrk);
1026 app_worker_map_free (app, wrk_map);
1027 if (application_n_workers (app) == 0)
1028 application_free (app);
1034 application_get_local_segment_manager (app_worker_t * app)
1036 return segment_manager_get (app->local_segment_manager);
1040 application_get_local_segment_manager_w_session (app_worker_t * app,
1041 local_session_t * ls)
1043 session_t *listener;
1044 if (application_local_session_listener_has_transport (ls))
1046 listener = listen_session_get (ls->listener_index);
1047 return app_worker_get_listen_segment_manager (app, listener);
1049 return segment_manager_get (app->local_segment_manager);
1053 application_is_proxy (application_t * app)
1055 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1059 application_is_builtin (application_t * app)
1061 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1065 application_is_builtin_proxy (application_t * app)
1067 return (application_is_proxy (app) && application_is_builtin (app));
1071 application_has_local_scope (application_t * app)
1073 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1077 application_has_global_scope (application_t * app)
1079 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1083 application_use_mq_for_ctrl (application_t * app)
1085 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
1089 * Send an API message to the external app, to map new segment
1092 app_worker_add_segment_notify (u32 app_wrk_index, u64 segment_handle)
1094 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
1095 application_t *app = application_get (app_wrk->app_index);
1096 return app->cb_fns.add_segment_callback (app_wrk->api_client_index,
1101 application_n_listeners (app_worker_t * app)
1103 return hash_elts (app->listeners_table);
1107 app_worker_first_listener (app_worker_t * app, u8 fib_proto,
1110 session_t *listener;
1115 sst = session_type_from_proto_and_ip (transport_proto,
1116 fib_proto == FIB_PROTOCOL_IP4);
1119 hash_foreach (handle, sm_index, app->listeners_table, ({
1120 listener = listen_session_get_from_handle (handle);
1121 if (listener->session_type == sst
1122 && listener->enqueue_epoch != SESSION_PROXY_LISTENER_INDEX)
1131 app_worker_application_is_builtin (app_worker_t * app_wrk)
1133 return app_wrk->app_is_builtin;
1137 application_proxy_listener (app_worker_t * app, u8 fib_proto,
1140 session_t *listener;
1145 sst = session_type_from_proto_and_ip (transport_proto,
1146 fib_proto == FIB_PROTOCOL_IP4);
1149 hash_foreach (handle, sm_index, app->listeners_table, ({
1150 listener = listen_session_get_from_handle (handle);
1151 if (listener->session_type == sst
1152 && listener->enqueue_epoch == SESSION_PROXY_LISTENER_INDEX)
1160 static clib_error_t *
1161 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1162 u8 transport_proto, u8 is_start)
1164 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1165 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1166 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
1167 transport_connection_t *tc;
1168 app_worker_t *app_wrk;
1172 /* TODO decide if we want proxy to be enabled for all workers */
1173 app_wrk = application_get_default_worker (app);
1176 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1179 sep.is_ip4 = is_ip4;
1180 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1181 sep.sw_if_index = app_ns->sw_if_index;
1182 sep.transport_proto = transport_proto;
1183 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1184 application_start_listen (app, &sep, &handle);
1185 s = listen_session_get_from_handle (handle);
1186 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
1191 s = application_proxy_listener (app_wrk, fib_proto, transport_proto);
1195 tc = listen_session_get_transport (s);
1197 if (!ip_is_zero (&tc->lcl_ip, 1))
1200 sep.is_ip4 = is_ip4;
1201 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1202 sep.transport_proto = transport_proto;
1204 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1206 session_lookup_add_session_endpoint (sti,
1207 (session_endpoint_t *) & sep,
1210 session_lookup_del_session_endpoint (sti,
1211 (session_endpoint_t *) & sep);
1218 application_start_stop_proxy_local_scope (application_t * app,
1219 u8 transport_proto, u8 is_start)
1221 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1222 app_namespace_t *app_ns;
1223 app_ns = app_namespace_get (app->ns_index);
1225 sep.transport_proto = transport_proto;
1230 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1233 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1238 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1240 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1245 application_start_stop_proxy (application_t * app,
1246 transport_proto_t transport_proto, u8 is_start)
1248 if (application_has_local_scope (app))
1249 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1251 if (application_has_global_scope (app))
1253 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1254 transport_proto, is_start);
1255 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1256 transport_proto, is_start);
1261 application_setup_proxy (application_t * app)
1263 u16 transports = app->proxied_transports;
1264 transport_proto_t tp;
1266 ASSERT (application_is_proxy (app));
1269 transport_proto_foreach (tp, ({
1270 if (transports & (1 << tp))
1271 application_start_stop_proxy (app, tp, 1);
1277 application_remove_proxy (application_t * app)
1279 u16 transports = app->proxied_transports;
1280 transport_proto_t tp;
1282 ASSERT (application_is_proxy (app));
1285 transport_proto_foreach (tp, ({
1286 if (transports & (1 << tp))
1287 application_start_stop_proxy (app, tp, 0);
1292 segment_manager_properties_t *
1293 application_segment_manager_properties (application_t * app)
1295 return &app->sm_properties;
1298 segment_manager_properties_t *
1299 application_get_segment_manager_properties (u32 app_index)
1301 application_t *app = application_get (app_index);
1302 return &app->sm_properties;
1306 app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
1308 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
1310 clib_warning ("evt q full");
1311 svm_msg_q_free_msg (mq, msg);
1313 svm_msg_q_unlock (mq);
1319 svm_msg_q_add_and_unlock (mq, msg);
1323 /* Even when not locking the ring, we must wait for queue mutex */
1324 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
1326 clib_warning ("msg q add returned");
1333 app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s, u8 lock)
1335 session_event_t *evt;
1336 svm_msg_q_msg_t msg;
1339 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
1340 && s->session_state != SESSION_STATE_LISTENING))
1342 /* Session is closed so app will never clean up. Flush rx fifo */
1343 if (s->session_state == SESSION_STATE_CLOSED)
1344 svm_fifo_dequeue_drop_all (s->rx_fifo);
1348 if (app_worker_application_is_builtin (app_wrk))
1350 application_t *app = application_get (app_wrk->app_index);
1351 return app->cb_fns.builtin_app_rx_callback (s);
1354 if (svm_fifo_has_event (s->rx_fifo) || svm_fifo_is_empty (s->rx_fifo))
1357 mq = app_wrk->event_queue;
1359 svm_msg_q_lock (mq);
1361 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1363 clib_warning ("evt q rings full");
1365 svm_msg_q_unlock (mq);
1369 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1370 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1372 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
1373 evt->fifo = s->rx_fifo;
1374 evt->event_type = FIFO_EVENT_APP_RX;
1376 (void) svm_fifo_set_event (s->rx_fifo);
1378 if (app_enqueue_evt (mq, &msg, lock))
1384 app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s, u8 lock)
1387 session_event_t *evt;
1388 svm_msg_q_msg_t msg;
1390 if (app_worker_application_is_builtin (app_wrk))
1393 mq = app_wrk->event_queue;
1395 svm_msg_q_lock (mq);
1397 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1399 clib_warning ("evt q rings full");
1401 svm_msg_q_unlock (mq);
1405 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1406 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1408 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
1409 evt->event_type = FIFO_EVENT_APP_TX;
1410 evt->fifo = s->tx_fifo;
1412 return app_enqueue_evt (mq, &msg, lock);
1416 typedef int (app_send_evt_handler_fn) (app_worker_t *app,
1419 static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = {
1427 * Send event to application
1429 * Logic from queue perspective is non-blocking. If there's
1430 * not enough space to enqueue a message, we return.
1433 app_worker_send_event (app_worker_t * app, session_t * s, u8 evt_type)
1435 ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
1436 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
1440 * Send event to application
1442 * Logic from queue perspective is blocking. However, if queue is full,
1446 app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
1449 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
1453 application_local_session_alloc (app_worker_t * app_wrk)
1456 pool_get (app_wrk->local_sessions, s);
1457 clib_memset (s, 0, sizeof (*s));
1458 s->app_wrk_index = app_wrk->wrk_index;
1459 s->session_index = s - app_wrk->local_sessions;
1460 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1465 application_local_session_free (app_worker_t * app, local_session_t * s)
1467 pool_put (app->local_sessions, s);
1469 clib_memset (s, 0xfc, sizeof (*s));
1473 application_get_local_session (app_worker_t * app_wrk, u32 session_index)
1475 if (pool_is_free_index (app_wrk->local_sessions, session_index))
1477 return pool_elt_at_index (app_wrk->local_sessions, session_index);
1481 application_get_local_session_from_handle (session_handle_t handle)
1483 app_worker_t *server_wrk;
1484 u32 session_index, server_wrk_index;
1485 local_session_parse_handle (handle, &server_wrk_index, &session_index);
1486 server_wrk = app_worker_get_if_valid (server_wrk_index);
1489 return application_get_local_session (server_wrk, session_index);
1493 application_local_listen_session_alloc (application_t * app)
1495 local_session_t *ll;
1496 pool_get (app->local_listen_sessions, ll);
1497 clib_memset (ll, 0, sizeof (*ll));
1502 application_local_listener_index (application_t * app, local_session_t * ll)
1504 return (ll - app->local_listen_sessions);
1508 application_local_listen_session_free (application_t * app,
1509 local_session_t * ll)
1511 pool_put (app->local_listen_sessions, ll);
1513 clib_memset (ll, 0xfb, sizeof (*ll));
1517 application_start_local_listen (application_t * app,
1518 session_endpoint_cfg_t * sep_ext,
1519 session_handle_t * handle)
1521 app_listener_t *app_listener;
1522 session_endpoint_t *sep;
1523 app_worker_t *app_wrk;
1524 session_handle_t lh;
1525 local_session_t *ll;
1528 sep = (session_endpoint_t *) sep_ext;
1529 table_index = application_local_session_table (app);
1530 app_wrk = app_worker_get (sep_ext->app_wrk_index);
1532 /* An exact sep match, as opposed to session_lookup_local_listener */
1533 lh = session_lookup_endpoint_listener (table_index, sep, 1);
1534 if (lh != SESSION_INVALID_HANDLE)
1536 ll = application_get_local_listener_w_handle (lh);
1537 if (ll->app_index != app->app_index)
1538 return VNET_API_ERROR_ADDRESS_IN_USE;
1540 if (ll->app_wrk_index == app_wrk->wrk_index)
1541 return VNET_API_ERROR_ADDRESS_IN_USE;
1543 app_listener = app_local_listener_get (app, ll->listener_db_index);
1544 app_listener->workers = clib_bitmap_set (app_listener->workers,
1545 app_wrk->wrk_map_index, 1);
1546 *handle = application_local_session_handle (ll);
1550 ll = application_local_listen_session_alloc (app);
1551 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1552 ll->app_wrk_index = app_wrk->app_index;
1553 ll->session_index = application_local_listener_index (app, ll);
1554 ll->port = sep_ext->port;
1555 /* Store the original session type for the unbind */
1556 ll->listener_session_type =
1557 session_type_from_proto_and_ip (sep_ext->transport_proto,
1559 ll->transport_listener_index = ~0;
1560 ll->app_index = app->app_index;
1562 app_listener = app_local_listener_alloc (app);
1563 ll->listener_db_index = app_listener->al_index;
1564 app_listener->workers = clib_bitmap_set (app_listener->workers,
1565 app_wrk->wrk_map_index, 1);
1567 *handle = application_local_session_handle (ll);
1568 session_lookup_add_session_endpoint (table_index, sep, *handle);
1574 * Clean up local session table. If we have a listener session use it to
1575 * find the port and proto. If not, the handle must be a local table handle
1579 application_stop_local_listen (u32 app_index, u32 wrk_map_index,
1580 session_handle_t lh)
1582 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1583 u32 table_index, ll_index, server_index;
1584 app_listener_t *app_listener;
1585 app_worker_t *server_wrk;
1587 local_session_t *ll, *ls;
1588 application_t *server;
1590 server = application_get (app_index);
1591 table_index = application_local_session_table (server);
1593 /* We have both local and global table binds. Figure from global what
1594 * the sep we should be cleaning up is.
1596 if (!session_handle_is_local (lh))
1598 sl = listen_session_get_from_handle (lh);
1599 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1601 clib_warning ("broken listener");
1604 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1605 if (lh == SESSION_INVALID_HANDLE)
1609 local_session_parse_handle (lh, &server_index, &ll_index);
1610 if (PREDICT_FALSE (server_index != app_index))
1612 clib_warning ("app %u does not own local handle 0x%lx", app_index, lh);
1616 ll = application_get_local_listen_session (server, ll_index);
1617 if (PREDICT_FALSE (!ll))
1619 clib_warning ("no local listener");
1623 app_listener = app_local_listener_get (server, ll->listener_db_index);
1624 if (!clib_bitmap_get (app_listener->workers, wrk_map_index))
1626 clib_warning ("app wrk %u not listening on handle %lu", wrk_map_index,
1631 server_wrk = application_get_worker (server, wrk_map_index);
1633 pool_foreach (ls, server_wrk->local_sessions, ({
1634 if (ls->listener_index == ll->session_index)
1635 application_local_session_disconnect (server_wrk->app_index, ls);
1639 clib_bitmap_set_no_check (app_listener->workers, wrk_map_index, 0);
1640 if (clib_bitmap_is_zero (app_listener->workers))
1642 app_local_listener_free (server, app_listener);
1643 application_local_listener_session_endpoint (ll, &sep);
1644 session_lookup_del_session_endpoint (table_index, &sep);
1645 application_local_listen_session_free (server, ll);
1652 application_local_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
1657 * segment manager initializes only the producer eventds, since vpp is
1658 * typically the producer. But for local sessions, we also pass to the
1659 * apps the mqs they listen on for events from peer apps, so they are also
1662 fd = svm_msg_q_get_producer_eventfd (sq);
1663 svm_msg_q_set_consumer_eventfd (sq, fd);
1664 fd = svm_msg_q_get_producer_eventfd (cq);
1665 svm_msg_q_set_consumer_eventfd (cq, fd);
1669 application_local_session_connect (app_worker_t * client_wrk,
1670 app_worker_t * server_wrk,
1671 local_session_t * ll, u32 opaque)
1673 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1674 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index;
1675 segment_manager_properties_t *props, *cprops;
1676 int rv, has_transport, seg_index;
1677 svm_fifo_segment_private_t *seg;
1678 application_t *server, *client;
1679 segment_manager_t *sm;
1680 local_session_t *ls;
1681 svm_msg_q_t *sq, *cq;
1684 ls = application_local_session_alloc (server_wrk);
1685 server = application_get (server_wrk->app_index);
1686 client = application_get (client_wrk->app_index);
1688 props = application_segment_manager_properties (server);
1689 cprops = application_segment_manager_properties (client);
1690 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
1691 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
1692 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
1693 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
1694 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
1696 has_transport = session_has_transport ((session_t *) ll);
1699 /* Local sessions don't have backing transport */
1700 ls->port = ll->port;
1701 sm = application_get_local_segment_manager (server_wrk);
1705 session_t *sl = (session_t *) ll;
1706 transport_connection_t *tc;
1707 tc = listen_session_get_transport (sl);
1708 ls->port = tc->lcl_port;
1709 sm = app_worker_get_listen_segment_manager (server_wrk, sl);
1712 seg_index = segment_manager_add_segment (sm, seg_size);
1715 clib_warning ("failed to add new cut-through segment");
1718 seg = segment_manager_get_segment_w_lock (sm, seg_index);
1719 sq = segment_manager_alloc_queue (seg, props);
1720 cq = segment_manager_alloc_queue (seg, cprops);
1722 if (props->use_mq_eventfd)
1723 application_local_session_fix_eventds (sq, cq);
1725 ls->server_evt_q = pointer_to_uword (sq);
1726 ls->client_evt_q = pointer_to_uword (cq);
1727 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1728 props->tx_fifo_size,
1729 &ls->rx_fifo, &ls->tx_fifo);
1732 clib_warning ("failed to add fifos in cut-through segment");
1733 segment_manager_segment_reader_unlock (sm);
1736 sm_index = segment_manager_index (sm);
1737 ls->rx_fifo->ct_session_index = ls->session_index;
1738 ls->tx_fifo->ct_session_index = ls->session_index;
1739 ls->rx_fifo->segment_manager = sm_index;
1740 ls->tx_fifo->segment_manager = sm_index;
1741 ls->rx_fifo->segment_index = seg_index;
1742 ls->tx_fifo->segment_index = seg_index;
1743 ls->svm_segment_index = seg_index;
1744 ls->listener_index = ll->session_index;
1745 ls->client_wrk_index = client_wrk->wrk_index;
1746 ls->client_opaque = opaque;
1747 ls->listener_session_type = ll->session_type;
1748 ls->session_state = SESSION_STATE_READY;
1750 segment_handle = segment_manager_segment_handle (sm, seg);
1751 if ((rv = server->cb_fns.add_segment_callback (server_wrk->api_client_index,
1754 clib_warning ("failed to notify server of new segment");
1755 segment_manager_segment_reader_unlock (sm);
1758 segment_manager_segment_reader_unlock (sm);
1759 if ((rv = server->cb_fns.session_accept_callback ((session_t *) ls)))
1761 clib_warning ("failed to send accept cut-through notify to server");
1764 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1765 application_local_session_connect_notify (ls);
1771 segment_manager_del_segment (sm, seg);
1776 application_client_local_connect_key (local_session_t * ls)
1778 return (((u64) ls->app_wrk_index) << 32 | (u64) ls->session_index);
1782 application_client_local_connect_key_parse (u64 key, u32 * app_wrk_index,
1783 u32 * session_index)
1785 *app_wrk_index = key >> 32;
1786 *session_index = key & 0xFFFFFFFF;
1790 application_local_session_connect_notify (local_session_t * ls)
1792 svm_fifo_segment_private_t *seg;
1793 app_worker_t *client_wrk, *server_wrk;
1794 segment_manager_t *sm;
1795 application_t *client;
1796 int rv, is_fail = 0;
1800 client_wrk = app_worker_get (ls->client_wrk_index);
1801 server_wrk = app_worker_get (ls->app_wrk_index);
1802 client = application_get (client_wrk->app_index);
1804 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
1805 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1806 segment_handle = segment_manager_segment_handle (sm, seg);
1807 if ((rv = client->cb_fns.add_segment_callback (client_wrk->api_client_index,
1810 clib_warning ("failed to notify client %u of new segment",
1811 ls->client_wrk_index);
1812 segment_manager_segment_reader_unlock (sm);
1813 application_local_session_disconnect (ls->client_wrk_index, ls);
1818 segment_manager_segment_reader_unlock (sm);
1821 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
1823 (session_t *) ls, is_fail);
1825 client_key = application_client_local_connect_key (ls);
1826 hash_set (client_wrk->local_connects, client_key, client_key);
1831 application_local_session_cleanup (app_worker_t * client_wrk,
1832 app_worker_t * server_wrk,
1833 local_session_t * ls)
1835 svm_fifo_segment_private_t *seg;
1836 session_t *listener;
1837 segment_manager_t *sm;
1841 /* Retrieve listener transport type as it is the one that decides where
1842 * the fifos are allocated */
1843 has_transport = application_local_session_listener_has_transport (ls);
1845 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
1848 listener = listen_session_get (ls->listener_index);
1849 sm = app_worker_get_listen_segment_manager (server_wrk, listener);
1852 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1855 client_key = application_client_local_connect_key (ls);
1856 hash_unset (client_wrk->local_connects, client_key);
1861 application_t *server = application_get (server_wrk->app_index);
1862 u64 segment_handle = segment_manager_segment_handle (sm, seg);
1863 server->cb_fns.del_segment_callback (server_wrk->api_client_index,
1867 application_t *client = application_get (client_wrk->app_index);
1868 client->cb_fns.del_segment_callback (client_wrk->api_client_index,
1871 segment_manager_del_segment (sm, seg);
1874 application_local_session_free (server_wrk, ls);
1880 application_local_session_disconnect (u32 app_index, local_session_t * ls)
1882 app_worker_t *client_wrk, *server_wrk;
1883 u8 is_server = 0, is_client = 0;
1886 app = application_get_if_valid (app_index);
1890 client_wrk = app_worker_get_if_valid (ls->client_wrk_index);
1891 server_wrk = app_worker_get (ls->app_wrk_index);
1893 if (server_wrk->app_index == app_index)
1895 else if (client_wrk && client_wrk->app_index == app_index)
1898 if (!is_server && !is_client)
1900 clib_warning ("app %u is neither client nor server for session 0x%lx",
1901 app_index, application_local_session_handle (ls));
1902 return VNET_API_ERROR_INVALID_VALUE;
1905 if (ls->session_state == SESSION_STATE_CLOSED)
1906 return application_local_session_cleanup (client_wrk, server_wrk, ls);
1908 if (app_index == ls->client_wrk_index)
1910 mq_send_local_session_disconnected_cb (ls->app_wrk_index, ls);
1916 return application_local_session_cleanup (client_wrk, server_wrk,
1919 else if (ls->session_state < SESSION_STATE_READY)
1921 application_t *client = application_get (client_wrk->app_index);
1922 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
1926 ls->session_state = SESSION_STATE_CLOSED;
1927 return application_local_session_cleanup (client_wrk, server_wrk,
1932 mq_send_local_session_disconnected_cb (client_wrk->wrk_index, ls);
1936 ls->session_state = SESSION_STATE_CLOSED;
1942 application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index)
1944 app_worker_t *app_wrk;
1945 local_session_t *ls;
1946 app_wrk = app_worker_get (app_wrk_index);
1947 ls = application_get_local_session (app_wrk, ls_index);
1948 return application_local_session_disconnect (app_wrk_index, ls);
1952 app_worker_local_sessions_free (app_worker_t * app_wrk)
1954 u32 index, server_wrk_index, session_index;
1955 u64 handle, *handles = 0;
1956 app_worker_t *server_wrk;
1957 segment_manager_t *sm;
1958 local_session_t *ls;
1964 if (app_wrk->local_sessions)
1967 pool_foreach (ls, app_wrk->local_sessions, ({
1968 application_local_session_disconnect (app_wrk->wrk_index, ls);
1976 vec_reset_length (handles);
1978 hash_foreach (handle, index, app_wrk->local_connects, ({
1979 vec_add1 (handles, handle);
1983 for (i = 0; i < vec_len (handles); i++)
1985 application_client_local_connect_key_parse (handles[i],
1988 server_wrk = app_worker_get_if_valid (server_wrk_index);
1991 ls = application_get_local_session (server_wrk, session_index);
1992 application_local_session_disconnect (app_wrk->wrk_index, ls);
1996 sm = segment_manager_get (app_wrk->local_segment_manager);
1997 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1998 segment_manager_del (sm);
2002 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
2005 app = application_get (a->app_index);
2007 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
2008 0, "app %u doesn't exist", a->app_index);
2009 app->tls_cert = vec_dup (a->cert);
2014 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
2017 app = application_get (a->app_index);
2019 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
2020 0, "app %u doesn't exist", a->app_index);
2021 app->tls_key = vec_dup (a->key);
2026 format_app_worker_listener (u8 * s, va_list * args)
2028 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
2029 u64 handle = va_arg (*args, u64);
2030 u32 sm_index = va_arg (*args, u32);
2031 int verbose = va_arg (*args, int);
2032 session_t *listener;
2039 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
2040 "Wrk", "API Client", "ListenerID", "SegManager");
2042 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
2047 app_name = application_name_from_index (app_wrk->app_index);
2048 listener = listen_session_get_from_handle (handle);
2049 str = format (0, "%U", format_stream_session, listener, verbose);
2054 sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
2055 s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name,
2056 buf, app_wrk->api_client_index, handle, sm_index);
2059 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
2065 application_format_listeners (application_t * app, int verbose)
2067 vlib_main_t *vm = vlib_get_main ();
2068 app_worker_map_t *wrk_map;
2069 app_worker_t *app_wrk;
2075 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
2081 pool_foreach (wrk_map, app->worker_maps, ({
2082 app_wrk = app_worker_get (wrk_map->wrk_index);
2083 if (hash_elts (app_wrk->listeners_table) == 0)
2085 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
2086 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
2087 handle, sm_index, verbose);
2094 app_worker_format_connects (app_worker_t * app_wrk, int verbose)
2096 svm_fifo_segment_private_t *fifo_segment;
2097 vlib_main_t *vm = vlib_get_main ();
2098 segment_manager_t *sm;
2106 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
2107 "API Client", "SegManager");
2109 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
2113 if (app_wrk->connects_seg_manager == (u32) ~ 0)
2116 app_name = application_name_from_index (app_wrk->app_index);
2118 /* Across all fifo segments */
2119 sm = segment_manager_get (app_wrk->connects_seg_manager);
2122 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
2126 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
2129 u32 session_index, thread_index;
2132 session_index = fifo->master_session_index;
2133 thread_index = fifo->master_thread_index;
2135 session = session_get (session_index, thread_index);
2136 str = format (0, "%U", format_stream_session, session, verbose);
2139 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
2140 app_wrk->api_client_index, app_wrk->connects_seg_manager);
2142 s = format (s, "%-40s%-20s", str, app_name);
2144 vlib_cli_output (vm, "%v", s);
2145 vec_reset_length (s);
2157 application_format_connects (application_t * app, int verbose)
2159 app_worker_map_t *wrk_map;
2160 app_worker_t *app_wrk;
2164 app_worker_format_connects (0, verbose);
2169 pool_foreach (wrk_map, app->worker_maps, ({
2170 app_wrk = app_worker_get (wrk_map->wrk_index);
2171 app_worker_format_connects (app_wrk, verbose);
2177 app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose)
2179 vlib_main_t *vm = vlib_get_main ();
2180 local_session_t *ls;
2181 transport_proto_t tp;
2187 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
2192 if (!pool_elts (app_wrk->local_sessions)
2193 && !pool_elts (app_wrk->local_connects))
2197 pool_foreach (ls, app_wrk->local_sessions, ({
2198 tp = session_type_transport_proto(ls->listener_session_type);
2199 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2201 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_wrk_index,
2202 ls->client_wrk_index);
2203 vec_reset_length (conn);
2211 application_format_local_sessions (application_t * app, int verbose)
2213 vlib_main_t *vm = vlib_get_main ();
2214 app_worker_map_t *wrk_map;
2215 app_worker_t *app_wrk;
2216 transport_proto_t tp;
2217 local_session_t *ls;
2222 app_worker_format_local_sessions (0, verbose);
2227 * Format local listeners
2231 pool_foreach (ls, app->local_listen_sessions, ({
2232 tp = session_type_transport_proto (ls->listener_session_type);
2233 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2235 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
2236 vec_reset_length (conn);
2241 * Format local accepted/connected sessions
2244 pool_foreach (wrk_map, app->worker_maps, ({
2245 app_wrk = app_worker_get (wrk_map->wrk_index);
2246 app_worker_format_local_sessions (app_wrk, verbose);
2252 app_worker_format_local_connects (app_worker_t * app, int verbose)
2254 vlib_main_t *vm = vlib_get_main ();
2255 u32 app_wrk_index, session_index;
2256 app_worker_t *server_wrk;
2257 local_session_t *ls;
2265 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
2266 "Peer App", "SegManager");
2268 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
2273 if (!app->local_connects)
2277 hash_foreach (client_key, value, app->local_connects, ({
2278 application_client_local_connect_key_parse (client_key, &app_wrk_index,
2280 server_wrk = app_worker_get (app_wrk_index);
2281 ls = application_get_local_session (server_wrk, session_index);
2282 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_wrk_index,
2283 ls->client_wrk_index);
2289 application_format_local_connects (application_t * app, int verbose)
2291 app_worker_map_t *wrk_map;
2292 app_worker_t *app_wrk;
2296 app_worker_format_local_connects (0, verbose);
2301 pool_foreach (wrk_map, app->worker_maps, ({
2302 app_wrk = app_worker_get (wrk_map->wrk_index);
2303 app_worker_format_local_connects (app_wrk, verbose);
2309 format_application_worker (u8 * s, va_list * args)
2311 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
2314 s = format (s, "%U wrk-index %u app-index %u map-index %u "
2315 "api-client-index %d\n", format_white_space, indent,
2316 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
2317 app_wrk->api_client_index);
2322 format_application (u8 * s, va_list * args)
2324 application_t *app = va_arg (*args, application_t *);
2325 CLIB_UNUSED (int verbose) = va_arg (*args, int);
2326 segment_manager_properties_t *props;
2327 const u8 *app_ns_name, *app_name;
2328 app_worker_map_t *wrk_map;
2329 app_worker_t *app_wrk;
2334 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
2338 app_name = app_get_name (app);
2339 app_ns_name = app_namespace_id_from_index (app->ns_index);
2340 props = application_segment_manager_properties (app);
2343 s = format (s, "%-10u%-20s%-40s", app->app_index, app_name,
2348 s = format (s, "app-name %s app-index %u ns-index %u seg-size %U\n",
2349 app_name, app->app_index, app->ns_index,
2350 format_memory_size, props->add_segment_size);
2351 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
2352 format_memory_size, props->rx_fifo_size,
2353 format_memory_size, props->tx_fifo_size);
2356 pool_foreach (wrk_map, app->worker_maps, ({
2357 app_wrk = app_worker_get (wrk_map->wrk_index);
2358 s = format (s, "%U", format_application_worker, app_wrk);
2366 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
2370 if (!pool_elts (app_main.app_pool))
2372 vlib_cli_output (vm, "No active server bindings");
2378 application_format_local_sessions (0, verbose);
2380 pool_foreach (app, app_main.app_pool, ({
2381 application_format_local_sessions (app, verbose);
2387 application_format_listeners (0, verbose);
2390 pool_foreach (app, app_main.app_pool, ({
2391 application_format_listeners (app, verbose);
2398 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
2402 if (!pool_elts (app_main.app_pool))
2404 vlib_cli_output (vm, "No active apps");
2410 application_format_local_connects (0, verbose);
2413 pool_foreach (app, app_main.app_pool, ({
2414 application_format_local_connects (app, verbose);
2420 application_format_connects (0, verbose);
2423 pool_foreach (app, app_main.app_pool, ({
2424 application_format_connects (app, verbose);
2430 static clib_error_t *
2431 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
2432 vlib_cli_command_t * cmd)
2434 int do_server = 0, do_client = 0, do_local = 0;
2439 session_cli_return_if_not_enabled ();
2441 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2443 if (unformat (input, "server"))
2445 else if (unformat (input, "client"))
2447 else if (unformat (input, "local"))
2449 else if (unformat (input, "%u", &app_index))
2451 else if (unformat (input, "verbose"))
2454 return clib_error_return (0, "unknown input `%U'",
2455 format_unformat_error, input);
2460 application_format_all_listeners (vm, do_local, verbose);
2466 application_format_all_clients (vm, do_local, verbose);
2470 if (app_index != ~0)
2472 app = application_get_if_valid (app_index);
2474 return clib_error_return (0, "No app with index %u", app_index);
2476 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
2480 /* Print app related info */
2481 if (!do_server && !do_client)
2483 vlib_cli_output (vm, "%U", format_application, 0, 0);
2485 pool_foreach (app, app_main.app_pool, ({
2486 vlib_cli_output (vm, "%U", format_application, app, 0);
2495 VLIB_CLI_COMMAND (show_app_command, static) =
2498 .short_help = "show app [server|client] [verbose]",
2499 .function = show_app_command_fn,
2504 * fd.io coding-style-patch-verification: ON
2507 * eval: (c-set-style "gnu")