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>
22 * Pool from which we allocate all applications
24 static application_t *app_pool;
27 * Hash table of apps by api client index
29 static uword *app_by_api_client_index;
32 app_get_name_from_reg_index (application_t * app)
36 vl_api_registration_t *regp;
37 regp = vl_api_client_index_to_registration (app->api_client_index);
39 app_name = format (0, "builtin-%d%c", app->index, 0);
41 app_name = format (0, "%s%c", regp->name, 0);
47 application_session_table (application_t * app, u8 fib_proto)
49 app_namespace_t *app_ns;
50 app_ns = app_namespace_get (app->ns_index);
51 if (!application_has_global_scope (app))
52 return APP_INVALID_INDEX;
53 if (fib_proto == FIB_PROTOCOL_IP4)
54 return session_lookup_get_index_for_fib (fib_proto,
55 app_ns->ip4_fib_index);
57 return session_lookup_get_index_for_fib (fib_proto,
58 app_ns->ip6_fib_index);
62 application_local_session_table (application_t * app)
64 app_namespace_t *app_ns;
65 if (!application_has_local_scope (app))
66 return APP_INVALID_INDEX;
67 app_ns = app_namespace_get (app->ns_index);
68 return app_ns->local_table_index;
72 application_api_queue_is_full (application_t * app)
76 /* builtin servers are always OK */
77 if (app->api_client_index == ~0)
80 q = vl_api_client_index_to_input_queue (app->api_client_index);
84 if (q->cursize == q->maxsize)
92 * Since the name is not stored per app, we generate it on the fly. It is
93 * the caller's responsibility to free the vector
96 application_name_from_index (u32 app_index)
98 application_t *app = application_get (app_index);
101 return app_get_name_from_reg_index (app);
105 application_table_add (application_t * app)
107 hash_set (app_by_api_client_index, app->api_client_index, app->index);
111 application_table_del (application_t * app)
113 hash_unset (app_by_api_client_index, app->api_client_index);
117 application_lookup (u32 api_client_index)
120 p = hash_get (app_by_api_client_index, api_client_index);
122 return application_get (p[0]);
131 pool_get (app_pool, app);
132 memset (app, 0, sizeof (*app));
133 app->index = application_get_index (app);
134 app->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
135 app->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
136 app->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
138 clib_warning ("[%d] New app (%d)", getpid (), app->index);
143 application_del (application_t * app)
145 vnet_unbind_args_t _a, *a = &_a;
146 u64 handle, *handles = 0;
147 segment_manager_t *sm;
152 * The app event queue allocated in first segment is cleared with
153 * the segment manager. No need to explicitly free it.
156 clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
158 if (application_is_proxy (app))
159 application_remove_proxy (app);
166 hash_foreach (handle, index, app->listeners_table,
168 vec_add1 (handles, handle);
169 sm = segment_manager_get (index);
170 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
174 for (i = 0; i < vec_len (handles); i++)
176 a->app_index = app->index;
177 a->handle = handles[i];
178 /* seg manager is removed when unbind completes */
183 * Connects segment manager cleanup
186 if (app->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
188 sm = segment_manager_get (app->connects_seg_manager);
189 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
190 segment_manager_init_del (sm);
193 /* If first segment manager is used by a listener */
194 if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
195 && app->first_segment_manager != app->connects_seg_manager)
197 sm = segment_manager_get (app->first_segment_manager);
198 /* .. and has no fifos, e.g. it might be used for redirected sessions,
200 if (!segment_manager_has_fifos (sm))
202 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
203 segment_manager_del (sm);
208 * Local connections cleanup
210 application_local_sessions_del (app);
212 vec_free (app->tls_cert);
213 vec_free (app->tls_key);
215 application_table_del (app);
216 pool_put (app_pool, app);
220 application_verify_cb_fns (session_cb_vft_t * cb_fns)
222 if (cb_fns->session_accept_callback == 0)
223 clib_warning ("No accept callback function provided");
224 if (cb_fns->session_connected_callback == 0)
225 clib_warning ("No session connected callback function provided");
226 if (cb_fns->session_disconnect_callback == 0)
227 clib_warning ("No session disconnect callback function provided");
228 if (cb_fns->session_reset_callback == 0)
229 clib_warning ("No session reset callback function provided");
233 * Check app config for given segment type
235 * Returns 1 on success and 0 otherwise
238 application_verify_cfg (ssvm_segment_type_t st)
241 if (st == SSVM_SEGMENT_MEMFD)
243 is_valid = (session_manager_get_evt_q_segment () != 0);
245 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
248 else if (st == SSVM_SEGMENT_SHM)
250 is_valid = (session_manager_get_evt_q_segment () == 0);
252 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
260 application_init (application_t * app, u32 api_client_index, u64 * options,
261 session_cb_vft_t * cb_fns)
263 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
264 u32 first_seg_size, prealloc_fifo_pairs;
265 segment_manager_properties_t *props;
266 vl_api_registration_t *reg;
267 segment_manager_t *sm;
271 * Make sure we support the requested configuration
274 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
276 reg = vl_api_client_index_to_registration (api_client_index);
278 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
279 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
280 seg_type = SSVM_SEGMENT_SHM;
284 seg_type = SSVM_SEGMENT_PRIVATE;
287 if (!application_verify_cfg (seg_type))
288 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
291 * Setup segment manager
293 sm = segment_manager_new ();
294 sm->app_index = app->index;
295 props = application_segment_manager_properties (app);
296 segment_manager_properties_init (props);
297 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
299 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
300 props->add_segment = 1;
302 if (options[APP_OPTIONS_RX_FIFO_SIZE])
303 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
304 if (options[APP_OPTIONS_TX_FIFO_SIZE])
305 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
306 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
307 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
308 if (options[APP_OPTIONS_TLS_ENGINE])
309 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
310 props->segment_type = seg_type;
312 first_seg_size = options[APP_OPTIONS_SEGMENT_SIZE];
313 prealloc_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
315 if ((rv = segment_manager_init (sm, first_seg_size, prealloc_fifo_pairs)))
317 sm->first_is_protected = 1;
322 app->first_segment_manager = segment_manager_index (sm);
323 app->api_client_index = api_client_index;
324 app->flags = options[APP_OPTIONS_FLAGS];
325 app->cb_fns = *cb_fns;
326 app->ns_index = options[APP_OPTIONS_NAMESPACE];
327 app->listeners_table = hash_create (0, sizeof (u64));
328 app->local_connects = hash_create (0, sizeof (u64));
329 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
330 app->event_queue = segment_manager_event_queue (sm);
332 /* If no scope enabled, default to global */
333 if (!application_has_global_scope (app)
334 && !application_has_local_scope (app))
335 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
337 /* Check that the obvious things are properly set up */
338 application_verify_cb_fns (cb_fns);
340 /* Add app to lookup by api_client_index table */
341 application_table_add (app);
344 * Segment manager for local sessions
346 sm = segment_manager_new ();
347 sm->app_index = app->index;
348 app->local_segment_manager = segment_manager_index (sm);
354 application_get (u32 index)
356 if (index == APP_INVALID_INDEX)
358 return pool_elt_at_index (app_pool, index);
362 application_get_if_valid (u32 index)
364 if (pool_is_free_index (app_pool, index))
367 return pool_elt_at_index (app_pool, index);
371 application_get_index (application_t * app)
373 return app - app_pool;
376 static segment_manager_t *
377 application_alloc_segment_manager (application_t * app)
379 segment_manager_t *sm = 0;
381 /* If the first segment manager is not in use, don't allocate a new one */
382 if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
383 && app->first_segment_manager_in_use == 0)
385 sm = segment_manager_get (app->first_segment_manager);
386 app->first_segment_manager_in_use = 1;
390 sm = segment_manager_new ();
391 sm->app_index = app->index;
397 * Start listening local transport endpoint for requested transport.
399 * Creates a 'dummy' stream session with state LISTENING to be used in session
400 * lookups, prior to establishing connection. Requests transport to build
401 * it's own specific listening connection.
404 application_start_listen (application_t * srv, session_endpoint_t * sep,
405 session_handle_t * res)
407 segment_manager_t *sm;
409 session_handle_t handle;
412 sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
413 s = listen_session_new (sst);
414 s->app_index = srv->index;
416 if (stream_session_listen (s, sep))
419 /* Allocate segment manager. All sessions derived out of a listen session
420 * have fifos allocated by the same segment manager. */
421 sm = application_alloc_segment_manager (srv);
425 /* Add to app's listener table. Useful to find all child listeners
426 * when app goes down, although, just for unbinding this is not needed */
427 handle = listen_session_get_handle (s);
428 hash_set (srv->listeners_table, handle, segment_manager_index (sm));
434 listen_session_del (s);
439 * Stop listening on session associated to handle
442 application_stop_listen (application_t * srv, session_handle_t handle)
444 stream_session_t *listener;
446 segment_manager_t *sm;
448 if (srv && hash_get (srv->listeners_table, handle) == 0)
450 clib_warning ("app doesn't own handle %llu!", handle);
454 listener = listen_session_get_from_handle (handle);
455 stream_session_stop_listen (listener);
457 indexp = hash_get (srv->listeners_table, handle);
460 sm = segment_manager_get (*indexp);
461 if (srv->first_segment_manager == *indexp)
463 /* Delete sessions but don't remove segment manager */
464 srv->first_segment_manager_in_use = 0;
465 segment_manager_del_sessions (sm);
469 segment_manager_init_del (sm);
471 hash_unset (srv->listeners_table, handle);
472 listen_session_del (listener);
478 application_open_session (application_t * app, session_endpoint_t * sep,
483 /* Make sure we have a segment manager for connects */
484 application_alloc_connects_segment_manager (app);
486 if ((rv = session_open (app->index, sep, api_context)))
493 application_alloc_connects_segment_manager (application_t * app)
495 segment_manager_t *sm;
497 if (app->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
499 sm = application_alloc_segment_manager (app);
502 app->connects_seg_manager = segment_manager_index (sm);
508 application_get_connect_segment_manager (application_t * app)
510 ASSERT (app->connects_seg_manager != (u32) ~ 0);
511 return segment_manager_get (app->connects_seg_manager);
515 application_get_listen_segment_manager (application_t * app,
516 stream_session_t * s)
519 smp = hash_get (app->listeners_table, listen_session_get_handle (s));
521 return segment_manager_get (*smp);
525 application_get_local_segment_manager (application_t * app)
527 return segment_manager_get (app->local_segment_manager);
531 application_get_local_segment_manager_w_session (application_t * app,
532 local_session_t * ls)
534 stream_session_t *listener;
535 if (application_local_session_listener_has_transport (ls))
537 listener = listen_session_get (ls->listener_session_type,
539 return application_get_listen_segment_manager (app, listener);
541 return segment_manager_get (app->local_segment_manager);
545 application_is_proxy (application_t * app)
547 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
551 application_is_builtin (application_t * app)
553 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
557 application_is_builtin_proxy (application_t * app)
559 return (application_is_proxy (app) && application_is_builtin (app));
563 * Send an API message to the external app, to map new segment
566 application_add_segment_notify (u32 app_index, ssvm_private_t * fs)
568 application_t *app = application_get (app_index);
569 return app->cb_fns.add_segment_callback (app->api_client_index, fs);
573 application_has_local_scope (application_t * app)
575 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
579 application_has_global_scope (application_t * app)
581 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
585 application_n_listeners (application_t * app)
587 return hash_elts (app->listeners_table);
591 application_first_listener (application_t * app, u8 fib_proto,
594 stream_session_t *listener;
599 sst = session_type_from_proto_and_ip (transport_proto,
600 fib_proto == FIB_PROTOCOL_IP4);
603 hash_foreach (handle, sm_index, app->listeners_table, ({
604 listener = listen_session_get_from_handle (handle);
605 if (listener->session_type == sst
606 && listener->listener_index != SESSION_PROXY_LISTENER_INDEX)
615 application_proxy_listener (application_t * app, u8 fib_proto,
618 stream_session_t *listener;
623 sst = session_type_from_proto_and_ip (transport_proto,
624 fib_proto == FIB_PROTOCOL_IP4);
627 hash_foreach (handle, sm_index, app->listeners_table, ({
628 listener = listen_session_get_from_handle (handle);
629 if (listener->session_type == sst
630 && listener->listener_index == SESSION_PROXY_LISTENER_INDEX)
638 static clib_error_t *
639 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
640 u8 transport_proto, u8 is_start)
642 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
643 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
644 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
645 transport_connection_t *tc;
651 s = application_first_listener (app, fib_proto, transport_proto);
655 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
656 sep.sw_if_index = app_ns->sw_if_index;
657 sep.transport_proto = transport_proto;
658 application_start_listen (app, &sep, &handle);
659 s = listen_session_get_from_handle (handle);
660 s->listener_index = SESSION_PROXY_LISTENER_INDEX;
665 s = application_proxy_listener (app, fib_proto, transport_proto);
669 tc = listen_session_get_transport (s);
671 if (!ip_is_zero (&tc->lcl_ip, 1))
675 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
676 sep.transport_proto = transport_proto;
678 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
680 session_lookup_add_session_endpoint (sti, &sep, s->session_index);
682 session_lookup_del_session_endpoint (sti, &sep);
689 application_start_stop_proxy_local_scope (application_t * app,
690 u8 transport_proto, u8 is_start)
692 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
693 app_namespace_t *app_ns;
694 app_ns = app_namespace_get (app->ns_index);
696 sep.transport_proto = transport_proto;
701 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
704 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
709 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
711 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
716 application_start_stop_proxy (application_t * app,
717 transport_proto_t transport_proto, u8 is_start)
719 if (application_has_local_scope (app))
720 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
722 if (application_has_global_scope (app))
724 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
725 transport_proto, is_start);
726 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
727 transport_proto, is_start);
732 application_setup_proxy (application_t * app)
734 u16 transports = app->proxied_transports;
735 transport_proto_t tp;
737 ASSERT (application_is_proxy (app));
740 transport_proto_foreach (tp, ({
741 if (transports & (1 << tp))
742 application_start_stop_proxy (app, tp, 1);
748 application_remove_proxy (application_t * app)
750 u16 transports = app->proxied_transports;
751 transport_proto_t tp;
753 ASSERT (application_is_proxy (app));
756 transport_proto_foreach (tp, ({
757 if (transports & (1 << tp))
758 application_start_stop_proxy (app, tp, 0);
763 segment_manager_properties_t *
764 application_segment_manager_properties (application_t * app)
766 return &app->sm_properties;
769 segment_manager_properties_t *
770 application_get_segment_manager_properties (u32 app_index)
772 application_t *app = application_get (app_index);
773 return &app->sm_properties;
777 application_alloc_local_session (application_t * app)
780 pool_get (app->local_sessions, s);
781 memset (s, 0, sizeof (*s));
782 s->app_index = app->index;
783 s->session_index = s - app->local_sessions;
784 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
789 application_free_local_session (application_t * app, local_session_t * s)
791 pool_put (app->local_sessions, s);
793 memset (s, 0xfc, sizeof (*s));
797 application_get_local_session (application_t * app, u32 session_index)
799 return pool_elt_at_index (app->local_sessions, session_index);
803 application_get_local_session_from_handle (session_handle_t handle)
805 application_t *server;
806 u32 session_index, server_index;
807 local_session_parse_handle (handle, &server_index, &session_index);
808 server = application_get (server_index);
809 return application_get_local_session (server, session_index);
813 application_local_listener_session_endpoint (local_session_t * ll,
814 session_endpoint_t * sep)
816 sep->transport_proto =
817 session_type_transport_proto (ll->listener_session_type);
818 sep->port = ll->port;
819 sep->is_ip4 = ll->listener_session_type & 1;
823 application_start_local_listen (application_t * server,
824 session_endpoint_t * sep,
825 session_handle_t * handle)
831 table_index = application_local_session_table (server);
833 /* An exact sep match, as opposed to session_lookup_local_listener */
834 lh = session_lookup_endpoint_listener (table_index, sep, 1);
835 if (lh != SESSION_INVALID_HANDLE)
836 return VNET_API_ERROR_ADDRESS_IN_USE;
838 pool_get (server->local_listen_sessions, ll);
839 memset (ll, 0, sizeof (*ll));
840 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
841 ll->app_index = server->index;
842 ll->session_index = ll - server->local_listen_sessions;
843 ll->port = sep->port;
844 /* Store the original session type for the unbind */
845 ll->listener_session_type =
846 session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
847 ll->transport_listener_index = ~0;
849 *handle = application_local_session_handle (ll);
850 session_lookup_add_session_endpoint (table_index, sep, *handle);
856 * Clean up local session table. If we have a listener session use it to
857 * find the port and proto. If not, the handle must be a local table handle
861 application_stop_local_listen (application_t * server, session_handle_t lh)
863 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
864 u32 table_index, ll_index, server_index;
865 stream_session_t *sl = 0;
866 local_session_t *ll, *ls;
868 table_index = application_local_session_table (server);
870 /* We have both local and global table binds. Figure from global what
871 * the sep we should be cleaning up is.
873 if (!session_handle_is_local (lh))
875 sl = listen_session_get_from_handle (lh);
876 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
878 clib_warning ("broken listener");
881 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
882 if (lh == SESSION_INVALID_HANDLE)
886 local_session_parse_handle (lh, &server_index, &ll_index);
887 ASSERT (server->index == server_index);
888 if (!(ll = application_get_local_listen_session (server, ll_index)))
890 clib_warning ("no local listener");
893 application_local_listener_session_endpoint (ll, &sep);
894 session_lookup_del_session_endpoint (table_index, &sep);
897 pool_foreach (ls, server->local_sessions, ({
898 if (ls->listener_index == ll->session_index)
899 application_local_session_disconnect (server->index, ls);
902 pool_put_index (server->local_listen_sessions, ll->session_index);
908 application_local_session_connect (u32 table_index, application_t * client,
909 application_t * server,
910 local_session_t * ll, u32 opaque)
912 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
913 segment_manager_properties_t *props, *cprops;
914 int rv, has_transport, seg_index;
915 svm_fifo_segment_private_t *seg;
916 segment_manager_t *sm;
918 svm_queue_t *sq, *cq;
920 ls = application_alloc_local_session (server);
922 props = application_segment_manager_properties (server);
923 cprops = application_segment_manager_properties (client);
924 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
925 evt_q_sz = evt_q_elts * sizeof (session_fifo_event_t);
926 seg_size = props->rx_fifo_size + props->tx_fifo_size + evt_q_sz + margin;
928 has_transport = session_has_transport ((stream_session_t *) ll);
931 /* Local sessions don't have backing transport */
933 sm = application_get_local_segment_manager (server);
937 stream_session_t *sl = (stream_session_t *) ll;
938 transport_connection_t *tc;
939 tc = listen_session_get_transport (sl);
940 ls->port = tc->lcl_port;
941 sm = application_get_listen_segment_manager (server, sl);
944 seg_index = segment_manager_add_segment (sm, seg_size);
947 clib_warning ("failed to add new cut-through segment");
950 seg = segment_manager_get_segment_w_lock (sm, seg_index);
951 sq = segment_manager_alloc_queue (seg, props->evt_q_size);
952 cq = segment_manager_alloc_queue (seg, cprops->evt_q_size);
953 ls->server_evt_q = pointer_to_uword (sq);
954 ls->client_evt_q = pointer_to_uword (cq);
955 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
958 &ls->server_tx_fifo);
961 clib_warning ("failed to add fifos in cut-through segment");
962 segment_manager_segment_reader_unlock (sm);
965 ls->server_rx_fifo->master_session_index = ls->session_index;
966 ls->server_tx_fifo->master_session_index = ls->session_index;
967 ls->server_rx_fifo->master_thread_index = ~0;
968 ls->server_tx_fifo->master_thread_index = ~0;
969 ls->svm_segment_index = seg_index;
970 ls->listener_index = ll->session_index;
971 ls->client_index = client->index;
972 ls->client_opaque = opaque;
973 ls->listener_session_type = ll->session_type;
975 if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
978 clib_warning ("failed to notify server of new segment");
979 segment_manager_segment_reader_unlock (sm);
982 segment_manager_segment_reader_unlock (sm);
983 if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
985 clib_warning ("failed to send accept cut-through notify to server");
988 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
989 application_local_session_connect_notify (ls);
995 segment_manager_del_segment (sm, seg);
1000 application_client_local_connect_key (local_session_t * ls)
1002 return ((uword) ls->app_index << 32 | (uword) ls->session_index);
1006 application_client_local_connect_key_parse (uword key, u32 * app_index,
1007 u32 * session_index)
1009 *app_index = key >> 32;
1010 *session_index = key & 0xFFFFFFFF;
1014 application_local_session_connect_notify (local_session_t * ls)
1016 svm_fifo_segment_private_t *seg;
1017 application_t *client, *server;
1018 segment_manager_t *sm;
1019 int rv, is_fail = 0;
1022 client = application_get (ls->client_index);
1023 server = application_get (ls->app_index);
1024 sm = application_get_local_segment_manager_w_session (server, ls);
1025 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1026 if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1029 clib_warning ("failed to notify client %u of new segment",
1031 segment_manager_segment_reader_unlock (sm);
1032 application_local_session_disconnect (ls->client_index, ls);
1037 segment_manager_segment_reader_unlock (sm);
1040 client->cb_fns.session_connected_callback (client->index, ls->client_opaque,
1041 (stream_session_t *) ls,
1044 client_key = application_client_local_connect_key (ls);
1045 hash_set (client->local_connects, client_key, client_key);
1050 application_local_session_disconnect (u32 app_index, local_session_t * ls)
1052 svm_fifo_segment_private_t *seg;
1053 application_t *client, *server;
1054 segment_manager_t *sm;
1057 client = application_get_if_valid (ls->client_index);
1058 server = application_get (ls->app_index);
1060 if (ls->session_state == SESSION_STATE_CLOSED)
1063 client_key = application_client_local_connect_key (ls);
1064 sm = application_get_local_segment_manager_w_session (server, ls);
1065 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1069 hash_unset (client->local_connects, client_key);
1070 client->cb_fns.del_segment_callback (client->api_client_index,
1074 server->cb_fns.del_segment_callback (server->api_client_index,
1076 segment_manager_del_segment (sm, seg);
1077 application_free_local_session (server, ls);
1081 if (app_index == ls->client_index)
1083 send_local_session_disconnect_callback (ls->app_index, ls);
1091 else if (ls->session_state < SESSION_STATE_READY)
1093 client->cb_fns.session_connected_callback (client->index,
1095 (stream_session_t *) ls,
1097 ls->session_state = SESSION_STATE_CLOSED;
1102 send_local_session_disconnect_callback (ls->client_index, ls);
1106 ls->session_state = SESSION_STATE_CLOSED;
1112 application_local_sessions_del (application_t * app)
1114 u32 index, server_index, session_index, table_index;
1115 segment_manager_t *sm;
1116 u64 handle, *handles = 0;
1117 local_session_t *ls, *ll;
1118 application_t *server;
1119 session_endpoint_t sep;
1123 * Local listens. Don't bother with local sessions, we clean them lower
1125 table_index = application_local_session_table (app);
1127 pool_foreach (ll, app->local_listen_sessions, ({
1128 application_local_listener_session_endpoint (ll, &sep);
1129 session_lookup_del_session_endpoint (table_index, &sep);
1136 if (app->local_sessions)
1139 pool_foreach (ls, app->local_sessions, ({
1140 application_local_session_disconnect (app->index, ls);
1148 vec_reset_length (handles);
1150 hash_foreach (handle, index, app->local_connects, ({
1151 vec_add1 (handles, handle);
1155 for (i = 0; i < vec_len (handles); i++)
1157 application_client_local_connect_key_parse (handles[i], &server_index,
1159 server = application_get_if_valid (server_index);
1162 ls = application_get_local_session (server, session_index);
1163 application_local_session_disconnect (app->index, ls);
1167 sm = segment_manager_get (app->local_segment_manager);
1168 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1169 segment_manager_del (sm);
1173 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1176 app = application_get (a->app_index);
1178 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1179 0, "app %u doesn't exist", a->app_index);
1180 app->tls_cert = vec_dup (a->cert);
1185 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1188 app = application_get (a->app_index);
1190 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1191 0, "app %u doesn't exist", a->app_index);
1192 app->tls_key = vec_dup (a->key);
1197 format_application_listener (u8 * s, va_list * args)
1199 application_t *app = va_arg (*args, application_t *);
1200 u64 handle = va_arg (*args, u64);
1201 u32 sm_index = va_arg (*args, u32);
1202 int verbose = va_arg (*args, int);
1203 stream_session_t *listener;
1209 s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
1210 "API Client", "ListenerID", "SegManager");
1212 s = format (s, "%-40s%-20s", "Connection", "App");
1217 app_name = app_get_name_from_reg_index (app);
1218 listener = listen_session_get_from_handle (handle);
1219 str = format (0, "%U", format_stream_session, listener, verbose);
1223 s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
1224 app->api_client_index, handle, sm_index);
1227 s = format (s, "%-40s%-20s", str, app_name);
1229 vec_free (app_name);
1234 application_format_connects (application_t * app, int verbose)
1236 svm_fifo_segment_private_t *fifo_segment;
1237 vlib_main_t *vm = vlib_get_main ();
1238 segment_manager_t *sm;
1239 u8 *app_name, *s = 0;
1245 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1246 "API Client", "SegManager");
1248 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1253 if (app->connects_seg_manager == (u32) ~ 0)
1256 app_name = app_get_name_from_reg_index (app);
1258 /* Across all fifo segments */
1259 sm = segment_manager_get (app->connects_seg_manager);
1262 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
1266 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
1269 u32 session_index, thread_index;
1270 stream_session_t *session;
1272 session_index = fifo->master_session_index;
1273 thread_index = fifo->master_thread_index;
1275 session = session_get (session_index, thread_index);
1276 str = format (0, "%U", format_stream_session, session, verbose);
1279 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
1280 app->api_client_index, app->connects_seg_manager);
1282 s = format (s, "%-40s%-20s", str, app_name);
1284 vlib_cli_output (vm, "%v", s);
1285 vec_reset_length (s);
1294 vec_free (app_name);
1298 application_format_local_sessions (application_t * app, int verbose)
1300 vlib_main_t *vm = vlib_get_main ();
1301 local_session_t *ls;
1302 transport_proto_t tp;
1308 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
1314 pool_foreach (ls, app->local_listen_sessions, ({
1315 tp = session_type_transport_proto(ls->listener_session_type);
1316 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1318 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_index, "*");
1319 vec_reset_length (conn);
1321 pool_foreach (ls, app->local_sessions, ({
1322 tp = session_type_transport_proto(ls->listener_session_type);
1323 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1325 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_index,
1327 vec_reset_length (conn);
1335 application_format_local_connects (application_t * app, int verbose)
1337 vlib_main_t *vm = vlib_get_main ();
1338 u32 app_index, session_index;
1339 application_t *server;
1340 local_session_t *ls;
1348 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
1349 "Peer App", "SegManager");
1351 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
1357 hash_foreach (client_key, value, app->local_connects, ({
1358 application_client_local_connect_key_parse (client_key, &app_index,
1360 server = application_get (app_index);
1361 ls = application_get_local_session (server, session_index);
1362 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_index, ls->client_index);
1368 format_application (u8 * s, va_list * args)
1370 application_t *app = va_arg (*args, application_t *);
1371 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1372 segment_manager_properties_t *props;
1373 const u8 *app_ns_name;
1379 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
1380 "API Client", "Namespace", "Add seg size", "Rx fifo size",
1384 format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
1389 app_name = app_get_name_from_reg_index (app);
1390 app_ns_name = app_namespace_id_from_index (app->ns_index);
1391 props = application_segment_manager_properties (app);
1394 format (s, "%-10d%-20s%-15d%-15d%-15d%-15d%-15d", app->index, app_name,
1395 app->api_client_index, app->ns_index,
1396 props->add_segment_size,
1397 props->rx_fifo_size, props->tx_fifo_size);
1399 s = format (s, "%-10d%-20s%-15d%-40s", app->index, app_name,
1400 app->api_client_index, app_ns_name);
1406 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1412 if (!pool_elts (app_pool))
1414 vlib_cli_output (vm, "No active server bindings");
1420 application_format_local_sessions (0, verbose);
1422 pool_foreach (app, app_pool, ({
1423 if (!pool_elts (app->local_sessions)
1424 && !pool_elts(app->local_connects))
1426 application_format_local_sessions (app, verbose);
1432 vlib_cli_output (vm, "%U", format_application_listener, 0 /* header */ ,
1436 pool_foreach (app, app_pool, ({
1437 if (hash_elts (app->listeners_table) == 0)
1439 hash_foreach (handle, sm_index, app->listeners_table, ({
1440 vlib_cli_output (vm, "%U", format_application_listener, app,
1441 handle, sm_index, verbose);
1449 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1453 if (!pool_elts (app_pool))
1455 vlib_cli_output (vm, "No active apps");
1461 application_format_local_connects (0, verbose);
1464 pool_foreach (app, app_pool, ({
1465 if (app->local_connects)
1466 application_format_local_connects (app, verbose);
1472 application_format_connects (0, verbose);
1475 pool_foreach (app, app_pool, ({
1476 if (app->connects_seg_manager == (u32)~0)
1478 application_format_connects (app, verbose);
1484 static clib_error_t *
1485 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1486 vlib_cli_command_t * cmd)
1488 int do_server = 0, do_client = 0, do_local = 0;
1492 session_cli_return_if_not_enabled ();
1494 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1496 if (unformat (input, "server"))
1498 else if (unformat (input, "client"))
1500 else if (unformat (input, "local"))
1502 else if (unformat (input, "verbose"))
1509 application_format_all_listeners (vm, do_local, verbose);
1512 application_format_all_clients (vm, do_local, verbose);
1514 /* Print app related info */
1515 if (!do_server && !do_client)
1517 vlib_cli_output (vm, "%U", format_application, 0, verbose);
1519 pool_foreach (app, app_pool, ({
1520 vlib_cli_output (vm, "%U", format_application, app, verbose);
1529 VLIB_CLI_COMMAND (show_app_command, static) =
1532 .short_help = "show app [server|client] [verbose]",
1533 .function = show_app_command_fn,
1538 * fd.io coding-style-patch-verification: ON
1541 * eval: (c-set-style "gnu")