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/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 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 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 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 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 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 return pool_elt_at_index (app->worker_maps, map_index);
101 app_get_name_from_reg_index (application_t * app)
105 vl_api_registration_t *regp;
106 regp = vl_api_client_index_to_registration (app->api_client_index);
108 app_name = format (0, "builtin-%d%c", app->app_index, 0);
110 app_name = format (0, "%s%c", regp->name, 0);
116 app_get_name (application_t * app)
119 return app_get_name_from_reg_index (app);
124 application_session_table (application_t * app, u8 fib_proto)
126 app_namespace_t *app_ns;
127 app_ns = app_namespace_get (app->ns_index);
128 if (!application_has_global_scope (app))
129 return APP_INVALID_INDEX;
130 if (fib_proto == FIB_PROTOCOL_IP4)
131 return session_lookup_get_index_for_fib (fib_proto,
132 app_ns->ip4_fib_index);
134 return session_lookup_get_index_for_fib (fib_proto,
135 app_ns->ip6_fib_index);
139 application_local_session_table (application_t * app)
141 app_namespace_t *app_ns;
142 if (!application_has_local_scope (app))
143 return APP_INVALID_INDEX;
144 app_ns = app_namespace_get (app->ns_index);
145 return app_ns->local_table_index;
149 application_local_listener_session_endpoint (local_session_t * ll,
150 session_endpoint_t * sep)
152 sep->transport_proto =
153 session_type_transport_proto (ll->listener_session_type);
154 sep->port = ll->port;
155 sep->is_ip4 = ll->listener_session_type & 1;
159 application_api_queue_is_full (application_t * app)
163 /* builtin servers are always OK */
164 if (app->api_client_index == ~0)
167 q = vl_api_client_index_to_input_queue (app->api_client_index);
171 if (q->cursize == q->maxsize)
179 * Since the name is not stored per app, we generate it on the fly. It is
180 * the caller's responsibility to free the vector
183 application_name_from_index (u32 app_index)
185 application_t *app = application_get (app_index);
188 return app_get_name_from_reg_index (app);
192 application_table_add (application_t * app)
194 if (app->api_client_index != APP_INVALID_INDEX)
195 hash_set (app_main.app_by_api_client_index, app->api_client_index,
198 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
202 application_table_del (application_t * app)
204 if (app->api_client_index != APP_INVALID_INDEX)
205 hash_unset (app_main.app_by_api_client_index, app->api_client_index);
207 hash_unset_mem (app_main.app_by_name, app->name);
211 application_lookup (u32 api_client_index)
214 p = hash_get (app_main.app_by_api_client_index, api_client_index);
216 return application_get (p[0]);
222 application_lookup_name (const u8 * name)
225 p = hash_get_mem (app_main.app_by_name, name);
227 return application_get (p[0]);
233 application_alloc (void)
236 pool_get (app_main.app_pool, app);
237 memset (app, 0, sizeof (*app));
238 app->app_index = app - app_main.app_pool;
243 application_get (u32 app_index)
245 if (app_index == APP_INVALID_INDEX)
247 return pool_elt_at_index (app_main.app_pool, app_index);
251 application_get_if_valid (u32 app_index)
253 if (pool_is_free_index (app_main.app_pool, app_index))
256 return pool_elt_at_index (app_main.app_pool, app_index);
260 application_index (application_t * app)
262 return app - app_main.app_pool;
266 application_verify_cb_fns (session_cb_vft_t * cb_fns)
268 if (cb_fns->session_accept_callback == 0)
269 clib_warning ("No accept callback function provided");
270 if (cb_fns->session_connected_callback == 0)
271 clib_warning ("No session connected callback function provided");
272 if (cb_fns->session_disconnect_callback == 0)
273 clib_warning ("No session disconnect callback function provided");
274 if (cb_fns->session_reset_callback == 0)
275 clib_warning ("No session reset callback function provided");
279 * Check app config for given segment type
281 * Returns 1 on success and 0 otherwise
284 application_verify_cfg (ssvm_segment_type_t st)
287 if (st == SSVM_SEGMENT_MEMFD)
289 is_valid = (session_manager_get_evt_q_segment () != 0);
291 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
294 else if (st == SSVM_SEGMENT_SHM)
296 is_valid = (session_manager_get_evt_q_segment () == 0);
298 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
306 application_alloc_and_init (app_init_args_t * a)
308 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
309 segment_manager_properties_t *props;
310 vl_api_registration_t *reg;
314 app = application_alloc ();
315 options = a->options;
317 * Make sure we support the requested configuration
319 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
321 reg = vl_api_client_index_to_registration (a->api_client_index);
323 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
324 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
325 seg_type = SSVM_SEGMENT_SHM;
329 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
331 clib_warning ("mq eventfds can only be used if socket transport is "
333 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
335 seg_type = SSVM_SEGMENT_PRIVATE;
338 if (!application_verify_cfg (seg_type))
339 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
341 /* Check that the obvious things are properly set up */
342 application_verify_cb_fns (a->session_cb_vft);
344 app->api_client_index = a->api_client_index;
345 app->flags = options[APP_OPTIONS_FLAGS];
346 app->cb_fns = *a->session_cb_vft;
347 app->ns_index = options[APP_OPTIONS_NAMESPACE];
348 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
349 app->name = vec_dup (a->name);
351 /* If no scope enabled, default to global */
352 if (!application_has_global_scope (app)
353 && !application_has_local_scope (app))
354 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
356 props = application_segment_manager_properties (app);
357 segment_manager_properties_init (props);
358 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
359 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
360 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
362 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
363 props->add_segment = 1;
365 if (options[APP_OPTIONS_RX_FIFO_SIZE])
366 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
367 if (options[APP_OPTIONS_TX_FIFO_SIZE])
368 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
369 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
370 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
371 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
372 props->use_mq_eventfd = 1;
373 if (options[APP_OPTIONS_TLS_ENGINE])
374 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
375 props->segment_type = seg_type;
377 /* Add app to lookup by api_client_index table */
378 application_table_add (app);
379 a->app_index = application_index (app);
381 APP_DBG ("New app name: %v api index: %u index %u", app->name,
382 app->api_client_index, app->app_index);
388 application_free (application_t * app)
390 app_worker_map_t *wrk_map;
391 app_worker_t *app_wrk;
394 session_endpoint_t sep;
397 * The app event queue allocated in first segment is cleared with
398 * the segment manager. No need to explicitly free it.
400 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
401 app->api_client_index, app->app_index);
403 if (application_is_proxy (app))
404 application_remove_proxy (app);
411 pool_flush (wrk_map, app->worker_maps, ({
412 app_wrk = app_worker_get (wrk_map->wrk_index);
413 app_worker_free (app_wrk);
416 pool_free (app->worker_maps);
419 * Free local listeners. Global table unbinds stop local listeners
420 * as well, but if we have only local binds, these won't be cleaned up.
421 * Don't bother with local accepted sessions, we clean them when
422 * cleaning up the worker.
424 table_index = application_local_session_table (app);
426 pool_foreach (ll, app->local_listen_sessions, ({
427 application_local_listener_session_endpoint (ll, &sep);
428 session_lookup_del_session_endpoint (table_index, &sep);
431 pool_free (app->local_listen_sessions);
434 * Cleanup remaining state
436 application_table_del (app);
437 vec_free (app->name);
438 vec_free (app->tls_cert);
439 vec_free (app->tls_key);
440 pool_put (app_main.app_pool, app);
444 application_get_worker (application_t * app, u32 wrk_map_index)
446 app_worker_map_t *map;
447 map = app_worker_map_get (app, wrk_map_index);
450 return app_worker_get (map->wrk_index);
454 application_get_default_worker (application_t * app)
456 return application_get_worker (app, 0);
460 application_listener_select_worker (stream_session_t * ls, u8 is_local)
462 app_listener_t *app_listener;
466 app = application_get (ls->app_index);
468 app_listener = app_listener_get (app, ls->listener_db_index);
470 app_listener = app_local_listener_get (app, ls->listener_db_index);
472 wrk_index = clib_bitmap_next_set (app_listener->workers,
473 app_listener->accept_rotor + 1);
475 wrk_index = clib_bitmap_first_set (app_listener->workers);
477 ASSERT (wrk_index != ~0);
478 app_listener->accept_rotor = wrk_index;
479 return application_get_worker (app, wrk_index);
483 app_worker_alloc (application_t * app)
485 app_worker_t *app_wrk;
486 pool_get (app_main.workers, app_wrk);
487 memset (app_wrk, 0, sizeof (*app_wrk));
488 app_wrk->wrk_index = app_wrk - app_main.workers;
489 app_wrk->app_index = app->app_index;
490 app_wrk->wrk_map_index = ~0;
491 app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
492 app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
493 app_wrk->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
494 APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index);
499 app_worker_get (u32 wrk_index)
501 return pool_elt_at_index (app_main.workers, wrk_index);
505 app_worker_get_if_valid (u32 wrk_index)
507 if (pool_is_free_index (app_main.workers, wrk_index))
509 return pool_elt_at_index (app_main.workers, wrk_index);
513 app_worker_free (app_worker_t * app_wrk)
515 application_t *app = application_get (app_wrk->app_index);
516 vnet_unbind_args_t _a, *a = &_a;
517 u64 handle, *handles = 0;
518 segment_manager_t *sm;
527 hash_foreach (handle, sm_index, app_wrk->listeners_table,
529 vec_add1 (handles, handle);
530 sm = segment_manager_get (sm_index);
531 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
535 for (i = 0; i < vec_len (handles); i++)
537 a->app_index = app->app_index;
538 a->wrk_map_index = app_wrk->wrk_map_index;
539 a->handle = handles[i];
540 /* seg manager is removed when unbind completes */
545 * Connects segment manager cleanup
548 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
550 sm = segment_manager_get (app_wrk->connects_seg_manager);
551 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
552 segment_manager_init_del (sm);
555 /* If first segment manager is used by a listener */
556 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
557 && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
559 sm = segment_manager_get (app_wrk->first_segment_manager);
560 /* .. and has no fifos, e.g. it might be used for redirected sessions,
562 if (!segment_manager_has_fifos (sm))
564 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
565 segment_manager_del (sm);
572 app_worker_local_sessions_free (app_wrk);
574 pool_put (app_main.workers, app_wrk);
576 memset (app_wrk, 0xfe, sizeof (*app_wrk));
580 app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk)
582 app_worker_map_t *wrk_map;
583 app_worker_t *app_wrk;
584 segment_manager_t *sm;
587 app_wrk = app_worker_alloc (app);
588 wrk_map = app_worker_map_alloc (app);
589 wrk_map->wrk_index = app_wrk->wrk_index;
590 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
593 * Setup first segment manager
595 sm = segment_manager_new ();
596 sm->app_wrk_index = app_wrk->wrk_index;
598 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
599 app->sm_properties.prealloc_fifos)))
601 app_worker_free (app_wrk);
604 sm->first_is_protected = 1;
609 app_wrk->first_segment_manager = segment_manager_index (sm);
610 app_wrk->listeners_table = hash_create (0, sizeof (u64));
611 app_wrk->event_queue = segment_manager_event_queue (sm);
612 app_wrk->app_is_builtin = application_is_builtin (app);
615 * Segment manager for local sessions
617 sm = segment_manager_new ();
618 sm->app_wrk_index = app_wrk->wrk_index;
619 app_wrk->local_segment_manager = segment_manager_index (sm);
620 app_wrk->local_connects = hash_create (0, sizeof (u64));
628 app_worker_get_app (u32 wrk_index)
630 app_worker_t *app_wrk;
631 app_wrk = app_worker_get_if_valid (wrk_index);
634 return application_get_if_valid (app_wrk->app_index);
637 static segment_manager_t *
638 app_worker_alloc_segment_manager (app_worker_t * app_wrk)
640 segment_manager_t *sm = 0;
642 /* If the first segment manager is not in use, don't allocate a new one */
643 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
644 && app_wrk->first_segment_manager_in_use == 0)
646 sm = segment_manager_get (app_wrk->first_segment_manager);
647 app_wrk->first_segment_manager_in_use = 1;
651 sm = segment_manager_new ();
652 sm->app_wrk_index = app_wrk->wrk_index;
658 app_worker_start_listen (app_worker_t * app_wrk, stream_session_t * ls)
660 segment_manager_t *sm;
662 /* Allocate segment manager. All sessions derived out of a listen session
663 * have fifos allocated by the same segment manager. */
664 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
667 /* Add to app's listener table. Useful to find all child listeners
668 * when app goes down, although, just for unbinding this is not needed */
669 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
670 segment_manager_index (sm));
672 if (!ls->server_rx_fifo
673 && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
675 if (session_alloc_fifos (sm, ls))
682 app_worker_stop_listen (app_worker_t * app_wrk, session_handle_t handle)
684 segment_manager_t *sm;
687 sm_indexp = hash_get (app_wrk->listeners_table, handle);
688 if (PREDICT_FALSE (!sm_indexp))
690 clib_warning ("listener handle was removed %llu!", handle);
694 sm = segment_manager_get (*sm_indexp);
695 if (app_wrk->first_segment_manager == *sm_indexp)
697 /* Delete sessions but don't remove segment manager */
698 app_wrk->first_segment_manager_in_use = 0;
699 segment_manager_del_sessions (sm);
703 segment_manager_init_del (sm);
705 hash_unset (app_wrk->listeners_table, handle);
711 * Start listening local transport endpoint for requested transport.
713 * Creates a 'dummy' stream session with state LISTENING to be used in session
714 * lookups, prior to establishing connection. Requests transport to build
715 * it's own specific listening connection.
718 application_start_listen (application_t * app,
719 session_endpoint_extended_t * sep_ext,
720 session_handle_t * res)
722 app_listener_t *app_listener;
723 u32 table_index, fib_proto;
724 session_endpoint_t *sep;
725 app_worker_t *app_wrk;
726 stream_session_t *ls;
731 * Check if sep is already listened on
733 sep = (session_endpoint_t *) sep_ext;
734 fib_proto = session_endpoint_fib_proto (sep);
735 table_index = application_session_table (app, fib_proto);
736 lh = session_lookup_endpoint_listener (table_index, sep, 1);
737 if (lh != SESSION_INVALID_HANDLE)
739 ls = listen_session_get_from_handle (lh);
740 if (ls->app_index != app->app_index)
741 return VNET_API_ERROR_ADDRESS_IN_USE;
743 app_wrk = app_worker_get (sep_ext->app_wrk_index);
744 if (ls->app_wrk_index == app_wrk->wrk_index)
745 return VNET_API_ERROR_ADDRESS_IN_USE;
747 if (app_worker_start_listen (app_wrk, ls))
750 app_listener = app_listener_get (app, ls->listener_db_index);
751 app_listener->workers = clib_bitmap_set (app_listener->workers,
752 app_wrk->wrk_map_index, 1);
754 *res = listen_session_get_handle (ls);
759 * Allocate new listener for application
761 sst = session_type_from_proto_and_ip (sep_ext->transport_proto,
763 ls = listen_session_new (0, sst);
764 ls->app_index = app->app_index;
766 if (session_listen (ls, sep_ext))
769 app_listener = app_listener_alloc (app);
770 ls->listener_db_index = app_listener->al_index;
773 * Setup app worker as a listener
775 app_wrk = app_worker_get (sep_ext->app_wrk_index);
776 ls->app_wrk_index = app_wrk->wrk_index;
777 if (app_worker_start_listen (app_wrk, ls))
779 app_listener->workers = clib_bitmap_set (app_listener->workers,
780 app_wrk->wrk_map_index, 1);
782 *res = listen_session_get_handle (ls);
786 listen_session_del (ls);
791 * Stop listening on session associated to handle
793 * @param handle listener handle
794 * @param app_index index of the app owning the handle.
795 * @param app_wrk_index index of the worker requesting the stop
798 application_stop_listen (u32 app_index, u32 app_wrk_index,
799 session_handle_t handle)
801 app_listener_t *app_listener;
802 stream_session_t *listener;
803 app_worker_t *app_wrk;
806 listener = listen_session_get_from_handle (handle);
807 app = application_get (app_index);
808 if (PREDICT_FALSE (!app || app->app_index != listener->app_index))
810 clib_warning ("app doesn't own handle %llu!", handle);
814 app_listener = app_listener_get (app, listener->listener_db_index);
815 if (!clib_bitmap_get (app_listener->workers, app_wrk_index))
817 clib_warning ("worker not listening on handle %lu", handle);
821 app_wrk = application_get_worker (app, app_wrk_index);
822 app_worker_stop_listen (app_wrk, handle);
823 clib_bitmap_set_no_check (app_listener->workers, app_wrk_index, 0);
825 if (clib_bitmap_is_zero (app_listener->workers))
827 session_stop_listen (listener);
828 app_listener_free (app, app_listener);
829 listen_session_del (listener);
836 app_worker_open_session (app_worker_t * app, session_endpoint_t * sep,
841 /* Make sure we have a segment manager for connects */
842 app_worker_alloc_connects_segment_manager (app);
844 if ((rv = session_open (app->wrk_index, sep, api_context)))
851 app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
853 segment_manager_t *sm;
855 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
857 sm = app_worker_alloc_segment_manager (app_wrk);
860 app_wrk->connects_seg_manager = segment_manager_index (sm);
866 app_worker_get_connect_segment_manager (app_worker_t * app)
868 ASSERT (app->connects_seg_manager != (u32) ~ 0);
869 return segment_manager_get (app->connects_seg_manager);
873 app_worker_get_listen_segment_manager (app_worker_t * app,
874 stream_session_t * s)
877 smp = hash_get (app->listeners_table, listen_session_get_handle (s));
879 return segment_manager_get (*smp);
883 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
885 svm_fifo_segment_private_t *fs;
886 app_worker_map_t *wrk_map;
887 app_worker_t *app_wrk;
888 segment_manager_t *sm;
892 app = application_get (a->app_index);
894 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
895 "App %u does not exist", a->app_index);
899 if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
900 return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv);
901 sm = segment_manager_get (app_wrk->first_segment_manager);
902 fs = segment_manager_get_segment_w_lock (sm, 0);
903 a->segment = &fs->ssvm;
904 segment_manager_segment_reader_unlock (sm);
905 a->evt_q = app_wrk->event_queue;
906 a->wrk_index = app_wrk->wrk_map_index;
910 wrk_map = app_worker_map_get (app, a->wrk_index);
912 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
913 "App %u does not have worker %u",
914 app->app_index, a->wrk_index);
915 app_wrk = app_worker_get (wrk_map->wrk_index);
916 app_worker_map_free (app, wrk_map);
918 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
919 "No worker %u", a->wrk_index);
920 app_worker_free (app_wrk);
926 application_get_local_segment_manager (app_worker_t * app)
928 return segment_manager_get (app->local_segment_manager);
932 application_get_local_segment_manager_w_session (app_worker_t * app,
933 local_session_t * ls)
935 stream_session_t *listener;
936 if (application_local_session_listener_has_transport (ls))
938 listener = listen_session_get (ls->listener_index);
939 return app_worker_get_listen_segment_manager (app, listener);
941 return segment_manager_get (app->local_segment_manager);
945 application_is_proxy (application_t * app)
947 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
951 application_is_builtin (application_t * app)
953 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
957 application_is_builtin_proxy (application_t * app)
959 return (application_is_proxy (app) && application_is_builtin (app));
963 application_has_local_scope (application_t * app)
965 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
969 application_has_global_scope (application_t * app)
971 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
975 application_use_mq_for_ctrl (application_t * app)
977 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
981 * Send an API message to the external app, to map new segment
984 app_worker_add_segment_notify (u32 app_wrk_index, ssvm_private_t * fs)
986 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
987 application_t *app = application_get (app_wrk->app_index);
988 return app->cb_fns.add_segment_callback (app->api_client_index, fs);
992 application_n_listeners (app_worker_t * app)
994 return hash_elts (app->listeners_table);
998 app_worker_first_listener (app_worker_t * app, u8 fib_proto,
1001 stream_session_t *listener;
1006 sst = session_type_from_proto_and_ip (transport_proto,
1007 fib_proto == FIB_PROTOCOL_IP4);
1010 hash_foreach (handle, sm_index, app->listeners_table, ({
1011 listener = listen_session_get_from_handle (handle);
1012 if (listener->session_type == sst
1013 && listener->enqueue_epoch != SESSION_PROXY_LISTENER_INDEX)
1022 app_worker_application_is_builtin (app_worker_t * app_wrk)
1024 return app_wrk->app_is_builtin;
1028 application_proxy_listener (app_worker_t * app, u8 fib_proto,
1031 stream_session_t *listener;
1036 sst = session_type_from_proto_and_ip (transport_proto,
1037 fib_proto == FIB_PROTOCOL_IP4);
1040 hash_foreach (handle, sm_index, app->listeners_table, ({
1041 listener = listen_session_get_from_handle (handle);
1042 if (listener->session_type == sst
1043 && listener->enqueue_epoch == SESSION_PROXY_LISTENER_INDEX)
1051 static clib_error_t *
1052 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1053 u8 transport_proto, u8 is_start)
1055 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1056 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1057 session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL;
1058 transport_connection_t *tc;
1059 app_worker_t *app_wrk;
1060 stream_session_t *s;
1063 /* TODO decide if we want proxy to be enabled for all workers */
1064 app_wrk = application_get_default_worker (app);
1067 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1070 sep.is_ip4 = is_ip4;
1071 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1072 sep.sw_if_index = app_ns->sw_if_index;
1073 sep.transport_proto = transport_proto;
1074 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1075 application_start_listen (app, &sep, &handle);
1076 s = listen_session_get_from_handle (handle);
1077 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
1082 s = application_proxy_listener (app_wrk, fib_proto, transport_proto);
1086 tc = listen_session_get_transport (s);
1088 if (!ip_is_zero (&tc->lcl_ip, 1))
1091 sep.is_ip4 = is_ip4;
1092 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1093 sep.transport_proto = transport_proto;
1095 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1097 session_lookup_add_session_endpoint (sti,
1098 (session_endpoint_t *) & sep,
1101 session_lookup_del_session_endpoint (sti,
1102 (session_endpoint_t *) & sep);
1109 application_start_stop_proxy_local_scope (application_t * app,
1110 u8 transport_proto, u8 is_start)
1112 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1113 app_namespace_t *app_ns;
1114 app_ns = app_namespace_get (app->ns_index);
1116 sep.transport_proto = transport_proto;
1121 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1124 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1129 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1131 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1136 application_start_stop_proxy (application_t * app,
1137 transport_proto_t transport_proto, u8 is_start)
1139 if (application_has_local_scope (app))
1140 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1142 if (application_has_global_scope (app))
1144 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1145 transport_proto, is_start);
1146 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1147 transport_proto, is_start);
1152 application_setup_proxy (application_t * app)
1154 u16 transports = app->proxied_transports;
1155 transport_proto_t tp;
1157 ASSERT (application_is_proxy (app));
1160 transport_proto_foreach (tp, ({
1161 if (transports & (1 << tp))
1162 application_start_stop_proxy (app, tp, 1);
1168 application_remove_proxy (application_t * app)
1170 u16 transports = app->proxied_transports;
1171 transport_proto_t tp;
1173 ASSERT (application_is_proxy (app));
1176 transport_proto_foreach (tp, ({
1177 if (transports & (1 << tp))
1178 application_start_stop_proxy (app, tp, 0);
1183 segment_manager_properties_t *
1184 application_segment_manager_properties (application_t * app)
1186 return &app->sm_properties;
1189 segment_manager_properties_t *
1190 application_get_segment_manager_properties (u32 app_index)
1192 application_t *app = application_get (app_index);
1193 return &app->sm_properties;
1197 app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
1199 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
1201 clib_warning ("evt q full");
1202 svm_msg_q_free_msg (mq, msg);
1204 svm_msg_q_unlock (mq);
1210 svm_msg_q_add_and_unlock (mq, msg);
1214 /* Even when not locking the ring, we must wait for queue mutex */
1215 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
1217 clib_warning ("msg q add returned");
1224 app_send_io_evt_rx (app_worker_t * app_wrk, stream_session_t * s, u8 lock)
1226 session_event_t *evt;
1227 svm_msg_q_msg_t msg;
1230 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
1231 && s->session_state != SESSION_STATE_LISTENING))
1233 /* Session is closed so app will never clean up. Flush rx fifo */
1234 if (s->session_state == SESSION_STATE_CLOSED)
1235 svm_fifo_dequeue_drop_all (s->server_rx_fifo);
1239 if (app_worker_application_is_builtin (app_wrk))
1241 application_t *app = application_get (app_wrk->app_index);
1242 return app->cb_fns.builtin_app_rx_callback (s);
1245 if (svm_fifo_has_event (s->server_rx_fifo)
1246 || svm_fifo_is_empty (s->server_rx_fifo))
1249 mq = app_wrk->event_queue;
1251 svm_msg_q_lock (mq);
1253 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1255 clib_warning ("evt q rings full");
1257 svm_msg_q_unlock (mq);
1261 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1262 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1264 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
1265 evt->fifo = s->server_rx_fifo;
1266 evt->event_type = FIFO_EVENT_APP_RX;
1268 if (app_enqueue_evt (mq, &msg, lock))
1270 (void) svm_fifo_set_event (s->server_rx_fifo);
1275 app_send_io_evt_tx (app_worker_t * app_wrk, stream_session_t * s, u8 lock)
1278 session_event_t *evt;
1279 svm_msg_q_msg_t msg;
1281 if (app_worker_application_is_builtin (app_wrk))
1284 mq = app_wrk->event_queue;
1286 svm_msg_q_lock (mq);
1288 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1290 clib_warning ("evt q rings full");
1292 svm_msg_q_unlock (mq);
1296 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1297 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1299 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
1300 evt->event_type = FIFO_EVENT_APP_TX;
1301 evt->fifo = s->server_tx_fifo;
1303 return app_enqueue_evt (mq, &msg, lock);
1307 typedef int (app_send_evt_handler_fn) (app_worker_t *app,
1308 stream_session_t *s,
1310 static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = {
1318 * Send event to application
1320 * Logic from queue perspective is non-blocking. That is, if there's
1321 * not enough space to enqueue a message, we return. However, if the lock
1322 * flag is set, we do wait for queue mutex.
1325 app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type)
1327 ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
1328 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
1332 app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s,
1335 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
1339 application_local_session_alloc (app_worker_t * app_wrk)
1342 pool_get (app_wrk->local_sessions, s);
1343 memset (s, 0, sizeof (*s));
1344 s->app_wrk_index = app_wrk->wrk_index;
1345 s->session_index = s - app_wrk->local_sessions;
1346 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1351 application_local_session_free (app_worker_t * app, local_session_t * s)
1353 pool_put (app->local_sessions, s);
1355 memset (s, 0xfc, sizeof (*s));
1359 application_get_local_session (app_worker_t * app_wrk, u32 session_index)
1361 if (pool_is_free_index (app_wrk->local_sessions, session_index))
1363 return pool_elt_at_index (app_wrk->local_sessions, session_index);
1367 application_get_local_session_from_handle (session_handle_t handle)
1369 app_worker_t *server_wrk;
1370 u32 session_index, server_wrk_index;
1371 local_session_parse_handle (handle, &server_wrk_index, &session_index);
1372 server_wrk = app_worker_get_if_valid (server_wrk_index);
1375 return application_get_local_session (server_wrk, session_index);
1379 application_local_listen_session_alloc (application_t * app)
1381 local_session_t *ll;
1382 pool_get (app->local_listen_sessions, ll);
1383 memset (ll, 0, sizeof (*ll));
1388 application_local_listener_index (application_t * app, local_session_t * ll)
1390 return (ll - app->local_listen_sessions);
1394 application_local_listen_session_free (application_t * app,
1395 local_session_t * ll)
1397 pool_put (app->local_listen_sessions, ll);
1399 memset (ll, 0xfb, sizeof (*ll));
1403 application_start_local_listen (application_t * app,
1404 session_endpoint_extended_t * sep_ext,
1405 session_handle_t * handle)
1407 app_listener_t *app_listener;
1408 session_endpoint_t *sep;
1409 app_worker_t *app_wrk;
1410 session_handle_t lh;
1411 local_session_t *ll;
1414 sep = (session_endpoint_t *) sep_ext;
1415 table_index = application_local_session_table (app);
1416 app_wrk = app_worker_get (sep_ext->app_wrk_index);
1418 /* An exact sep match, as opposed to session_lookup_local_listener */
1419 lh = session_lookup_endpoint_listener (table_index, sep, 1);
1420 if (lh != SESSION_INVALID_HANDLE)
1422 ll = application_get_local_listener_w_handle (lh);
1423 if (ll->app_index != app->app_index)
1424 return VNET_API_ERROR_ADDRESS_IN_USE;
1426 if (ll->app_wrk_index == app_wrk->wrk_index)
1427 return VNET_API_ERROR_ADDRESS_IN_USE;
1429 app_listener = app_local_listener_get (app, ll->listener_db_index);
1430 app_listener->workers = clib_bitmap_set (app_listener->workers,
1431 app_wrk->wrk_map_index, 1);
1432 *handle = application_local_session_handle (ll);
1436 ll = application_local_listen_session_alloc (app);
1437 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1438 ll->app_wrk_index = app_wrk->app_index;
1439 ll->session_index = application_local_listener_index (app, ll);
1440 ll->port = sep_ext->port;
1441 /* Store the original session type for the unbind */
1442 ll->listener_session_type =
1443 session_type_from_proto_and_ip (sep_ext->transport_proto,
1445 ll->transport_listener_index = ~0;
1446 ll->app_index = app->app_index;
1448 app_listener = app_local_listener_alloc (app);
1449 ll->listener_db_index = app_listener->al_index;
1450 app_listener->workers = clib_bitmap_set (app_listener->workers,
1451 app_wrk->wrk_map_index, 1);
1453 *handle = application_local_session_handle (ll);
1454 session_lookup_add_session_endpoint (table_index, sep, *handle);
1460 * Clean up local session table. If we have a listener session use it to
1461 * find the port and proto. If not, the handle must be a local table handle
1465 application_stop_local_listen (u32 app_index, u32 wrk_map_index,
1466 session_handle_t lh)
1468 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1469 u32 table_index, ll_index, server_index;
1470 app_listener_t *app_listener;
1471 app_worker_t *server_wrk;
1472 stream_session_t *sl = 0;
1473 local_session_t *ll, *ls;
1474 application_t *server;
1476 server = application_get (app_index);
1477 table_index = application_local_session_table (server);
1479 /* We have both local and global table binds. Figure from global what
1480 * the sep we should be cleaning up is.
1482 if (!session_handle_is_local (lh))
1484 sl = listen_session_get_from_handle (lh);
1485 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1487 clib_warning ("broken listener");
1490 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1491 if (lh == SESSION_INVALID_HANDLE)
1495 local_session_parse_handle (lh, &server_index, &ll_index);
1496 if (PREDICT_FALSE (server_index != app_index))
1498 clib_warning ("app %u does not own local handle 0x%lx", app_index, lh);
1502 ll = application_get_local_listen_session (server, ll_index);
1503 if (PREDICT_FALSE (!ll))
1505 clib_warning ("no local listener");
1509 app_listener = app_local_listener_get (server, ll->listener_db_index);
1510 if (!clib_bitmap_get (app_listener->workers, wrk_map_index))
1512 clib_warning ("app wrk %u not listening on handle %lu", wrk_map_index,
1517 server_wrk = application_get_worker (server, wrk_map_index);
1519 pool_foreach (ls, server_wrk->local_sessions, ({
1520 if (ls->listener_index == ll->session_index)
1521 application_local_session_disconnect (server_wrk->app_index, ls);
1525 clib_bitmap_set_no_check (app_listener->workers, wrk_map_index, 0);
1526 if (clib_bitmap_is_zero (app_listener->workers))
1528 app_local_listener_free (server, app_listener);
1529 application_local_listener_session_endpoint (ll, &sep);
1530 session_lookup_del_session_endpoint (table_index, &sep);
1531 application_local_listen_session_free (server, ll);
1538 application_local_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
1543 * segment manager initializes only the producer eventds, since vpp is
1544 * typically the producer. But for local sessions, we also pass to the
1545 * apps the mqs they listen on for events from peer apps, so they are also
1548 fd = svm_msg_q_get_producer_eventfd (sq);
1549 svm_msg_q_set_consumer_eventfd (sq, fd);
1550 fd = svm_msg_q_get_producer_eventfd (cq);
1551 svm_msg_q_set_consumer_eventfd (cq, fd);
1555 application_local_session_connect (app_worker_t * client_wrk,
1556 app_worker_t * server_wrk,
1557 local_session_t * ll, u32 opaque)
1559 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1560 segment_manager_properties_t *props, *cprops;
1561 u32 round_rx_fifo_sz, round_tx_fifo_sz;
1562 int rv, has_transport, seg_index;
1563 svm_fifo_segment_private_t *seg;
1564 application_t *server, *client;
1565 segment_manager_t *sm;
1566 local_session_t *ls;
1567 svm_msg_q_t *sq, *cq;
1569 ls = application_local_session_alloc (server_wrk);
1570 server = application_get (server_wrk->app_index);
1571 client = application_get (client_wrk->app_index);
1573 props = application_segment_manager_properties (server);
1574 cprops = application_segment_manager_properties (client);
1575 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
1576 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
1577 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
1578 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
1579 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
1581 has_transport = session_has_transport ((stream_session_t *) ll);
1584 /* Local sessions don't have backing transport */
1585 ls->port = ll->port;
1586 sm = application_get_local_segment_manager (server_wrk);
1590 stream_session_t *sl = (stream_session_t *) ll;
1591 transport_connection_t *tc;
1592 tc = listen_session_get_transport (sl);
1593 ls->port = tc->lcl_port;
1594 sm = app_worker_get_listen_segment_manager (server_wrk, sl);
1597 seg_index = segment_manager_add_segment (sm, seg_size);
1600 clib_warning ("failed to add new cut-through segment");
1603 seg = segment_manager_get_segment_w_lock (sm, seg_index);
1604 sq = segment_manager_alloc_queue (seg, props);
1605 cq = segment_manager_alloc_queue (seg, cprops);
1607 if (props->use_mq_eventfd)
1608 application_local_session_fix_eventds (sq, cq);
1610 ls->server_evt_q = pointer_to_uword (sq);
1611 ls->client_evt_q = pointer_to_uword (cq);
1612 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1613 props->tx_fifo_size,
1614 &ls->server_rx_fifo,
1615 &ls->server_tx_fifo);
1618 clib_warning ("failed to add fifos in cut-through segment");
1619 segment_manager_segment_reader_unlock (sm);
1622 ls->server_rx_fifo->master_session_index = ls->session_index;
1623 ls->server_tx_fifo->master_session_index = ls->session_index;
1624 ls->server_rx_fifo->master_thread_index = ~0;
1625 ls->server_tx_fifo->master_thread_index = ~0;
1626 ls->svm_segment_index = seg_index;
1627 ls->listener_index = ll->session_index;
1628 ls->client_wrk_index = client_wrk->wrk_index;
1629 ls->client_opaque = opaque;
1630 ls->listener_session_type = ll->session_type;
1631 ls->session_state = SESSION_STATE_READY;
1633 if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
1636 clib_warning ("failed to notify server of new segment");
1637 segment_manager_segment_reader_unlock (sm);
1640 segment_manager_segment_reader_unlock (sm);
1641 if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
1643 clib_warning ("failed to send accept cut-through notify to server");
1646 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1647 application_local_session_connect_notify (ls);
1653 segment_manager_del_segment (sm, seg);
1658 application_client_local_connect_key (local_session_t * ls)
1660 return ((uword) ls->app_wrk_index << 32 | (uword) ls->session_index);
1664 application_client_local_connect_key_parse (uword key, u32 * app_wrk_index,
1665 u32 * session_index)
1667 *app_wrk_index = key >> 32;
1668 *session_index = key & 0xFFFFFFFF;
1672 application_local_session_connect_notify (local_session_t * ls)
1674 svm_fifo_segment_private_t *seg;
1675 app_worker_t *client_wrk, *server_wrk;
1676 segment_manager_t *sm;
1677 application_t *client;
1678 int rv, is_fail = 0;
1681 client_wrk = app_worker_get (ls->client_wrk_index);
1682 server_wrk = app_worker_get (ls->app_wrk_index);
1683 client = application_get (client_wrk->app_index);
1685 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
1686 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1687 if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1690 clib_warning ("failed to notify client %u of new segment",
1691 ls->client_wrk_index);
1692 segment_manager_segment_reader_unlock (sm);
1693 application_local_session_disconnect (ls->client_wrk_index, ls);
1698 segment_manager_segment_reader_unlock (sm);
1701 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
1703 (stream_session_t *) ls,
1706 client_key = application_client_local_connect_key (ls);
1707 hash_set (client_wrk->local_connects, client_key, client_key);
1712 application_local_session_cleanup (app_worker_t * client_wrk,
1713 app_worker_t * server_wrk,
1714 local_session_t * ls)
1716 svm_fifo_segment_private_t *seg;
1717 segment_manager_t *sm;
1721 has_transport = session_has_transport ((stream_session_t *) ls);
1722 client_key = application_client_local_connect_key (ls);
1724 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
1726 sm = app_worker_get_listen_segment_manager (server_wrk,
1727 (stream_session_t *) ls);
1729 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1731 hash_unset (client_wrk->local_connects, client_key);
1735 application_t *server = application_get (server_wrk->app_index);
1736 server->cb_fns.del_segment_callback (server->api_client_index,
1740 application_t *client = application_get (client_wrk->app_index);
1741 client->cb_fns.del_segment_callback (client->api_client_index,
1744 segment_manager_del_segment (sm, seg);
1747 application_local_session_free (server_wrk, ls);
1753 application_local_session_disconnect (u32 app_index, local_session_t * ls)
1755 app_worker_t *client_wrk, *server_wrk;
1756 u8 is_server = 0, is_client = 0;
1759 app = application_get_if_valid (app_index);
1763 client_wrk = app_worker_get_if_valid (ls->client_wrk_index);
1764 server_wrk = app_worker_get (ls->app_wrk_index);
1766 if (server_wrk->app_index == app_index)
1768 else if (client_wrk && client_wrk->app_index == app_index)
1771 if (!is_server && !is_client)
1773 clib_warning ("app %u is neither client nor server for session 0x%lx",
1774 app_index, application_local_session_handle (ls));
1775 return VNET_API_ERROR_INVALID_VALUE;
1778 if (ls->session_state == SESSION_STATE_CLOSED)
1779 return application_local_session_cleanup (client_wrk, server_wrk, ls);
1781 if (app_index == ls->client_wrk_index)
1783 mq_send_local_session_disconnected_cb (ls->app_wrk_index, ls);
1789 return application_local_session_cleanup (client_wrk, server_wrk,
1792 else if (ls->session_state < SESSION_STATE_READY)
1794 application_t *client = application_get (client_wrk->app_index);
1795 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
1797 (stream_session_t *) ls,
1799 ls->session_state = SESSION_STATE_CLOSED;
1800 return application_local_session_cleanup (client_wrk, server_wrk,
1805 mq_send_local_session_disconnected_cb (client_wrk->wrk_index, ls);
1809 ls->session_state = SESSION_STATE_CLOSED;
1815 application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index)
1817 app_worker_t *app_wrk;
1818 local_session_t *ls;
1819 app_wrk = app_worker_get (app_wrk_index);
1820 ls = application_get_local_session (app_wrk, ls_index);
1821 return application_local_session_disconnect (app_wrk_index, ls);
1825 app_worker_local_sessions_free (app_worker_t * app_wrk)
1827 u32 index, server_wrk_index, session_index;
1828 u64 handle, *handles = 0;
1829 app_worker_t *server_wrk;
1830 segment_manager_t *sm;
1831 local_session_t *ls;
1837 if (app_wrk->local_sessions)
1840 pool_foreach (ls, app_wrk->local_sessions, ({
1841 application_local_session_disconnect (app_wrk->wrk_index, ls);
1849 vec_reset_length (handles);
1851 hash_foreach (handle, index, app_wrk->local_connects, ({
1852 vec_add1 (handles, handle);
1856 for (i = 0; i < vec_len (handles); i++)
1858 application_client_local_connect_key_parse (handles[i],
1861 server_wrk = app_worker_get_if_valid (server_wrk_index);
1864 ls = application_get_local_session (server_wrk, session_index);
1865 application_local_session_disconnect (app_wrk->wrk_index, ls);
1869 sm = segment_manager_get (app_wrk->local_segment_manager);
1870 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1871 segment_manager_del (sm);
1875 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1878 app = application_get (a->app_index);
1880 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1881 0, "app %u doesn't exist", a->app_index);
1882 app->tls_cert = vec_dup (a->cert);
1887 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1890 app = application_get (a->app_index);
1892 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1893 0, "app %u doesn't exist", a->app_index);
1894 app->tls_key = vec_dup (a->key);
1899 format_app_worker_listener (u8 * s, va_list * args)
1901 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
1902 u64 handle = va_arg (*args, u64);
1903 u32 sm_index = va_arg (*args, u32);
1904 int verbose = va_arg (*args, int);
1905 stream_session_t *listener;
1912 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
1913 "Wrk", "API Client", "ListenerID", "SegManager");
1915 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
1920 app = application_get (app_wrk->app_index);
1921 app_name = app_get_name_from_reg_index (app);
1922 listener = listen_session_get_from_handle (handle);
1923 str = format (0, "%U", format_stream_session, listener, verbose);
1927 s = format (s, "%-40s%-25s%=10u%-15u%-15u%-10u", str, app_name,
1928 app_wrk->wrk_map_index, app->api_client_index, handle,
1932 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
1934 vec_free (app_name);
1939 application_format_listeners (application_t * app, int verbose)
1941 vlib_main_t *vm = vlib_get_main ();
1942 app_worker_map_t *wrk_map;
1943 app_worker_t *app_wrk;
1949 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1955 pool_foreach (wrk_map, app->worker_maps, ({
1956 app_wrk = app_worker_get (wrk_map->wrk_index);
1957 if (hash_elts (app_wrk->listeners_table) == 0)
1959 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1960 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1961 handle, sm_index, verbose);
1968 app_worker_format_connects (app_worker_t * app_wrk, int verbose)
1970 svm_fifo_segment_private_t *fifo_segment;
1971 vlib_main_t *vm = vlib_get_main ();
1972 segment_manager_t *sm;
1973 u8 *app_name, *s = 0;
1980 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1981 "API Client", "SegManager");
1983 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1987 app = application_get (app_wrk->app_index);
1988 if (app_wrk->connects_seg_manager == (u32) ~ 0)
1991 app_name = app_get_name_from_reg_index (app);
1993 /* Across all fifo segments */
1994 sm = segment_manager_get (app_wrk->connects_seg_manager);
1997 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
2001 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
2004 u32 session_index, thread_index;
2005 stream_session_t *session;
2007 session_index = fifo->master_session_index;
2008 thread_index = fifo->master_thread_index;
2010 session = session_get (session_index, thread_index);
2011 str = format (0, "%U", format_stream_session, session, verbose);
2014 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
2015 app->api_client_index, app_wrk->connects_seg_manager);
2017 s = format (s, "%-40s%-20s", str, app_name);
2019 vlib_cli_output (vm, "%v", s);
2020 vec_reset_length (s);
2029 vec_free (app_name);
2033 application_format_connects (application_t * app, int verbose)
2035 app_worker_map_t *wrk_map;
2036 app_worker_t *app_wrk;
2040 app_worker_format_connects (0, verbose);
2045 pool_foreach (wrk_map, app->worker_maps, ({
2046 app_wrk = app_worker_get (wrk_map->wrk_index);
2047 app_worker_format_connects (app_wrk, verbose);
2053 app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose)
2055 vlib_main_t *vm = vlib_get_main ();
2056 local_session_t *ls;
2057 transport_proto_t tp;
2063 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
2068 if (!pool_elts (app_wrk->local_sessions)
2069 && !pool_elts (app_wrk->local_connects))
2073 pool_foreach (ls, app_wrk->local_sessions, ({
2074 tp = session_type_transport_proto(ls->listener_session_type);
2075 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2077 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_wrk_index,
2078 ls->client_wrk_index);
2079 vec_reset_length (conn);
2087 application_format_local_sessions (application_t * app, int verbose)
2089 vlib_main_t *vm = vlib_get_main ();
2090 app_worker_map_t *wrk_map;
2091 app_worker_t *app_wrk;
2092 transport_proto_t tp;
2093 local_session_t *ls;
2098 app_worker_format_local_sessions (0, verbose);
2103 * Format local listeners
2107 pool_foreach (ls, app->local_listen_sessions, ({
2108 tp = session_type_transport_proto (ls->listener_session_type);
2109 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2111 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
2112 vec_reset_length (conn);
2117 * Format local accepted/connected sessions
2120 pool_foreach (wrk_map, app->worker_maps, ({
2121 app_wrk = app_worker_get (wrk_map->wrk_index);
2122 app_worker_format_local_sessions (app_wrk, verbose);
2128 app_worker_format_local_connects (app_worker_t * app, int verbose)
2130 vlib_main_t *vm = vlib_get_main ();
2131 u32 app_wrk_index, session_index;
2132 app_worker_t *server_wrk;
2133 local_session_t *ls;
2141 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
2142 "Peer App", "SegManager");
2144 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
2149 if (!app->local_connects)
2153 hash_foreach (client_key, value, app->local_connects, ({
2154 application_client_local_connect_key_parse (client_key, &app_wrk_index,
2156 server_wrk = app_worker_get (app_wrk_index);
2157 ls = application_get_local_session (server_wrk, session_index);
2158 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_wrk_index,
2159 ls->client_wrk_index);
2165 application_format_local_connects (application_t * app, int verbose)
2167 app_worker_map_t *wrk_map;
2168 app_worker_t *app_wrk;
2172 app_worker_format_local_connects (0, verbose);
2177 pool_foreach (wrk_map, app->worker_maps, ({
2178 app_wrk = app_worker_get (wrk_map->wrk_index);
2179 app_worker_format_local_connects (app_wrk, verbose);
2185 format_application (u8 * s, va_list * args)
2187 application_t *app = va_arg (*args, application_t *);
2188 CLIB_UNUSED (int verbose) = va_arg (*args, int);
2189 segment_manager_properties_t *props;
2190 const u8 *app_ns_name;
2196 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
2197 "API Client", "Namespace", "Add seg size", "Rx-f size",
2200 s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
2205 app_name = app_get_name (app);
2206 app_ns_name = app_namespace_id_from_index (app->ns_index);
2207 props = application_segment_manager_properties (app);
2209 s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->app_index,
2210 app_name, app->api_client_index, app->ns_index,
2211 format_memory_size, props->add_segment_size,
2212 format_memory_size, props->rx_fifo_size, format_memory_size,
2213 props->tx_fifo_size);
2215 s = format (s, "%-10u%-20s%-15d%-40s", app->app_index, app_name,
2216 app->api_client_index, app_ns_name);
2221 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
2225 if (!pool_elts (app_main.app_pool))
2227 vlib_cli_output (vm, "No active server bindings");
2233 application_format_local_sessions (0, verbose);
2235 pool_foreach (app, app_main.app_pool, ({
2236 application_format_local_sessions (app, verbose);
2242 application_format_listeners (0, verbose);
2245 pool_foreach (app, app_main.app_pool, ({
2246 application_format_listeners (app, verbose);
2253 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
2257 if (!pool_elts (app_main.app_pool))
2259 vlib_cli_output (vm, "No active apps");
2265 application_format_local_connects (0, verbose);
2268 pool_foreach (app, app_main.app_pool, ({
2269 application_format_local_connects (app, verbose);
2275 application_format_connects (0, verbose);
2278 pool_foreach (app, app_main.app_pool, ({
2279 application_format_connects (app, verbose);
2285 static clib_error_t *
2286 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
2287 vlib_cli_command_t * cmd)
2289 int do_server = 0, do_client = 0, do_local = 0;
2293 session_cli_return_if_not_enabled ();
2295 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2297 if (unformat (input, "server"))
2299 else if (unformat (input, "client"))
2301 else if (unformat (input, "local"))
2303 else if (unformat (input, "verbose"))
2310 application_format_all_listeners (vm, do_local, verbose);
2313 application_format_all_clients (vm, do_local, verbose);
2315 /* Print app related info */
2316 if (!do_server && !do_client)
2318 vlib_cli_output (vm, "%U", format_application, 0, verbose);
2320 pool_foreach (app, app_main.app_pool, ({
2321 vlib_cli_output (vm, "%U", format_application, app, verbose);
2330 VLIB_CLI_COMMAND (show_app_command, static) =
2333 .short_help = "show app [server|client] [verbose]",
2334 .function = show_app_command_fn,
2339 * fd.io coding-style-patch-verification: ON
2342 * eval: (c-set-style "gnu")