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 * Hash table of builtin apps by name
34 static uword *app_by_name;
37 app_get_name_from_reg_index (application_t * app)
41 vl_api_registration_t *regp;
42 regp = vl_api_client_index_to_registration (app->api_client_index);
44 app_name = format (0, "builtin-%d%c", app->index, 0);
46 app_name = format (0, "%s%c", regp->name, 0);
52 app_get_name (application_t * app)
55 return app_get_name_from_reg_index (app);
60 application_session_table (application_t * app, u8 fib_proto)
62 app_namespace_t *app_ns;
63 app_ns = app_namespace_get (app->ns_index);
64 if (!application_has_global_scope (app))
65 return APP_INVALID_INDEX;
66 if (fib_proto == FIB_PROTOCOL_IP4)
67 return session_lookup_get_index_for_fib (fib_proto,
68 app_ns->ip4_fib_index);
70 return session_lookup_get_index_for_fib (fib_proto,
71 app_ns->ip6_fib_index);
75 application_local_session_table (application_t * app)
77 app_namespace_t *app_ns;
78 if (!application_has_local_scope (app))
79 return APP_INVALID_INDEX;
80 app_ns = app_namespace_get (app->ns_index);
81 return app_ns->local_table_index;
85 application_api_queue_is_full (application_t * app)
89 /* builtin servers are always OK */
90 if (app->api_client_index == ~0)
93 q = vl_api_client_index_to_input_queue (app->api_client_index);
97 if (q->cursize == q->maxsize)
105 * Since the name is not stored per app, we generate it on the fly. It is
106 * the caller's responsibility to free the vector
109 application_name_from_index (u32 app_index)
111 application_t *app = application_get (app_index);
114 return app_get_name_from_reg_index (app);
118 application_table_add (application_t * app)
120 if (app->api_client_index != APP_INVALID_INDEX)
121 hash_set (app_by_api_client_index, app->api_client_index, app->index);
123 hash_set_mem (app_by_name, app->name, app->index);
127 application_table_del (application_t * app)
129 if (app->api_client_index != APP_INVALID_INDEX)
130 hash_unset (app_by_api_client_index, app->api_client_index);
132 hash_unset_mem (app_by_name, app->name);
136 application_lookup (u32 api_client_index)
139 p = hash_get (app_by_api_client_index, api_client_index);
141 return application_get (p[0]);
147 application_lookup_name (const u8 * name)
150 p = hash_get_mem (app_by_name, name);
152 return application_get (p[0]);
161 pool_get (app_pool, app);
162 memset (app, 0, sizeof (*app));
163 app->index = application_get_index (app);
164 app->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
165 app->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
166 app->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
168 clib_warning ("[%d] New app (%d)", getpid (), app->index);
173 application_del (application_t * app)
175 vnet_unbind_args_t _a, *a = &_a;
176 u64 handle, *handles = 0;
177 segment_manager_t *sm;
182 * The app event queue allocated in first segment is cleared with
183 * the segment manager. No need to explicitly free it.
186 clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
188 if (application_is_proxy (app))
189 application_remove_proxy (app);
196 hash_foreach (handle, index, app->listeners_table,
198 vec_add1 (handles, handle);
199 sm = segment_manager_get (index);
200 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
204 for (i = 0; i < vec_len (handles); i++)
206 a->app_index = app->index;
207 a->handle = handles[i];
208 /* seg manager is removed when unbind completes */
213 * Connects segment manager cleanup
216 if (app->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
218 sm = segment_manager_get (app->connects_seg_manager);
219 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
220 segment_manager_init_del (sm);
223 /* If first segment manager is used by a listener */
224 if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
225 && app->first_segment_manager != app->connects_seg_manager)
227 sm = segment_manager_get (app->first_segment_manager);
228 /* .. and has no fifos, e.g. it might be used for redirected sessions,
230 if (!segment_manager_has_fifos (sm))
232 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
233 segment_manager_del (sm);
238 * Local connections cleanup
240 application_local_sessions_del (app);
242 vec_free (app->tls_cert);
243 vec_free (app->tls_key);
245 application_table_del (app);
246 vec_free (app->name);
247 pool_put (app_pool, app);
251 application_verify_cb_fns (session_cb_vft_t * cb_fns)
253 if (cb_fns->session_accept_callback == 0)
254 clib_warning ("No accept callback function provided");
255 if (cb_fns->session_connected_callback == 0)
256 clib_warning ("No session connected callback function provided");
257 if (cb_fns->session_disconnect_callback == 0)
258 clib_warning ("No session disconnect callback function provided");
259 if (cb_fns->session_reset_callback == 0)
260 clib_warning ("No session reset callback function provided");
264 * Check app config for given segment type
266 * Returns 1 on success and 0 otherwise
269 application_verify_cfg (ssvm_segment_type_t st)
272 if (st == SSVM_SEGMENT_MEMFD)
274 is_valid = (session_manager_get_evt_q_segment () != 0);
276 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
279 else if (st == SSVM_SEGMENT_SHM)
281 is_valid = (session_manager_get_evt_q_segment () == 0);
283 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
291 application_init (application_t * app, u32 api_client_index, u8 * app_name,
292 u64 * options, session_cb_vft_t * cb_fns)
294 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
295 u32 first_seg_size, prealloc_fifo_pairs;
296 segment_manager_properties_t *props;
297 vl_api_registration_t *reg;
298 segment_manager_t *sm;
302 * Make sure we support the requested configuration
305 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
307 reg = vl_api_client_index_to_registration (api_client_index);
309 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
310 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
311 seg_type = SSVM_SEGMENT_SHM;
315 seg_type = SSVM_SEGMENT_PRIVATE;
318 if (!application_verify_cfg (seg_type))
319 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
322 * Setup segment manager
324 sm = segment_manager_new ();
325 sm->app_index = app->index;
326 props = application_segment_manager_properties (app);
327 segment_manager_properties_init (props);
328 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
330 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
331 props->add_segment = 1;
333 if (options[APP_OPTIONS_RX_FIFO_SIZE])
334 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
335 if (options[APP_OPTIONS_TX_FIFO_SIZE])
336 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
337 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
338 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
339 if (options[APP_OPTIONS_TLS_ENGINE])
340 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
341 props->segment_type = seg_type;
343 first_seg_size = options[APP_OPTIONS_SEGMENT_SIZE];
344 prealloc_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
346 if ((rv = segment_manager_init (sm, first_seg_size, prealloc_fifo_pairs)))
348 sm->first_is_protected = 1;
353 app->first_segment_manager = segment_manager_index (sm);
354 app->api_client_index = api_client_index;
355 app->flags = options[APP_OPTIONS_FLAGS];
356 app->cb_fns = *cb_fns;
357 app->ns_index = options[APP_OPTIONS_NAMESPACE];
358 app->listeners_table = hash_create (0, sizeof (u64));
359 app->local_connects = hash_create (0, sizeof (u64));
360 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
361 app->event_queue = segment_manager_event_queue (sm);
362 app->name = vec_dup (app_name);
364 /* If no scope enabled, default to global */
365 if (!application_has_global_scope (app)
366 && !application_has_local_scope (app))
367 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
369 /* Check that the obvious things are properly set up */
370 application_verify_cb_fns (cb_fns);
372 /* Add app to lookup by api_client_index table */
373 application_table_add (app);
376 * Segment manager for local sessions
378 sm = segment_manager_new ();
379 sm->app_index = app->index;
380 app->local_segment_manager = segment_manager_index (sm);
386 application_get (u32 index)
388 if (index == APP_INVALID_INDEX)
390 return pool_elt_at_index (app_pool, index);
394 application_get_if_valid (u32 index)
396 if (pool_is_free_index (app_pool, index))
399 return pool_elt_at_index (app_pool, index);
403 application_get_index (application_t * app)
405 return app - app_pool;
408 static segment_manager_t *
409 application_alloc_segment_manager (application_t * app)
411 segment_manager_t *sm = 0;
413 /* If the first segment manager is not in use, don't allocate a new one */
414 if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
415 && app->first_segment_manager_in_use == 0)
417 sm = segment_manager_get (app->first_segment_manager);
418 app->first_segment_manager_in_use = 1;
422 sm = segment_manager_new ();
423 sm->app_index = app->index;
429 * Start listening local transport endpoint for requested transport.
431 * Creates a 'dummy' stream session with state LISTENING to be used in session
432 * lookups, prior to establishing connection. Requests transport to build
433 * it's own specific listening connection.
436 application_start_listen (application_t * srv, session_endpoint_t * sep,
437 session_handle_t * res)
439 segment_manager_t *sm;
441 session_handle_t handle;
444 sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
445 s = listen_session_new (0, sst);
446 s->app_index = srv->index;
448 /* Allocate segment manager. All sessions derived out of a listen session
449 * have fifos allocated by the same segment manager. */
450 if (!(sm = application_alloc_segment_manager (srv)))
453 /* Add to app's listener table. Useful to find all child listeners
454 * when app goes down, although, just for unbinding this is not needed */
455 handle = listen_session_get_handle (s);
456 hash_set (srv->listeners_table, handle, segment_manager_index (sm));
458 if (stream_session_listen (s, sep))
460 segment_manager_del (sm);
461 hash_unset (srv->listeners_table, handle);
469 listen_session_del (s);
474 * Stop listening on session associated to handle
477 application_stop_listen (application_t * srv, session_handle_t handle)
479 stream_session_t *listener;
481 segment_manager_t *sm;
483 if (srv && hash_get (srv->listeners_table, handle) == 0)
485 clib_warning ("app doesn't own handle %llu!", handle);
489 listener = listen_session_get_from_handle (handle);
490 stream_session_stop_listen (listener);
492 indexp = hash_get (srv->listeners_table, handle);
495 sm = segment_manager_get (*indexp);
496 if (srv->first_segment_manager == *indexp)
498 /* Delete sessions but don't remove segment manager */
499 srv->first_segment_manager_in_use = 0;
500 segment_manager_del_sessions (sm);
504 segment_manager_init_del (sm);
506 hash_unset (srv->listeners_table, handle);
507 listen_session_del (listener);
513 application_open_session (application_t * app, session_endpoint_t * sep,
518 /* Make sure we have a segment manager for connects */
519 application_alloc_connects_segment_manager (app);
521 if ((rv = session_open (app->index, sep, api_context)))
528 application_alloc_connects_segment_manager (application_t * app)
530 segment_manager_t *sm;
532 if (app->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
534 sm = application_alloc_segment_manager (app);
537 app->connects_seg_manager = segment_manager_index (sm);
543 application_get_connect_segment_manager (application_t * app)
545 ASSERT (app->connects_seg_manager != (u32) ~ 0);
546 return segment_manager_get (app->connects_seg_manager);
550 application_get_listen_segment_manager (application_t * app,
551 stream_session_t * s)
554 smp = hash_get (app->listeners_table, listen_session_get_handle (s));
556 return segment_manager_get (*smp);
560 application_get_local_segment_manager (application_t * app)
562 return segment_manager_get (app->local_segment_manager);
566 application_get_local_segment_manager_w_session (application_t * app,
567 local_session_t * ls)
569 stream_session_t *listener;
570 if (application_local_session_listener_has_transport (ls))
572 listener = listen_session_get (ls->listener_index);
573 return application_get_listen_segment_manager (app, listener);
575 return segment_manager_get (app->local_segment_manager);
579 application_is_proxy (application_t * app)
581 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
585 application_is_builtin (application_t * app)
587 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
591 application_is_builtin_proxy (application_t * app)
593 return (application_is_proxy (app) && application_is_builtin (app));
597 * Send an API message to the external app, to map new segment
600 application_add_segment_notify (u32 app_index, ssvm_private_t * fs)
602 application_t *app = application_get (app_index);
603 return app->cb_fns.add_segment_callback (app->api_client_index, fs);
607 application_has_local_scope (application_t * app)
609 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
613 application_has_global_scope (application_t * app)
615 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
619 application_n_listeners (application_t * app)
621 return hash_elts (app->listeners_table);
625 application_first_listener (application_t * app, u8 fib_proto,
628 stream_session_t *listener;
633 sst = session_type_from_proto_and_ip (transport_proto,
634 fib_proto == FIB_PROTOCOL_IP4);
637 hash_foreach (handle, sm_index, app->listeners_table, ({
638 listener = listen_session_get_from_handle (handle);
639 if (listener->session_type == sst
640 && listener->listener_index != SESSION_PROXY_LISTENER_INDEX)
649 application_proxy_listener (application_t * app, u8 fib_proto,
652 stream_session_t *listener;
657 sst = session_type_from_proto_and_ip (transport_proto,
658 fib_proto == FIB_PROTOCOL_IP4);
661 hash_foreach (handle, sm_index, app->listeners_table, ({
662 listener = listen_session_get_from_handle (handle);
663 if (listener->session_type == sst
664 && listener->listener_index == SESSION_PROXY_LISTENER_INDEX)
672 static clib_error_t *
673 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
674 u8 transport_proto, u8 is_start)
676 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
677 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
678 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
679 transport_connection_t *tc;
685 s = application_first_listener (app, fib_proto, transport_proto);
689 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
690 sep.sw_if_index = app_ns->sw_if_index;
691 sep.transport_proto = transport_proto;
692 application_start_listen (app, &sep, &handle);
693 s = listen_session_get_from_handle (handle);
694 s->listener_index = SESSION_PROXY_LISTENER_INDEX;
699 s = application_proxy_listener (app, fib_proto, transport_proto);
703 tc = listen_session_get_transport (s);
705 if (!ip_is_zero (&tc->lcl_ip, 1))
709 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
710 sep.transport_proto = transport_proto;
712 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
714 session_lookup_add_session_endpoint (sti, &sep, s->session_index);
716 session_lookup_del_session_endpoint (sti, &sep);
723 application_start_stop_proxy_local_scope (application_t * app,
724 u8 transport_proto, u8 is_start)
726 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
727 app_namespace_t *app_ns;
728 app_ns = app_namespace_get (app->ns_index);
730 sep.transport_proto = transport_proto;
735 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
738 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
743 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
745 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
750 application_start_stop_proxy (application_t * app,
751 transport_proto_t transport_proto, u8 is_start)
753 if (application_has_local_scope (app))
754 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
756 if (application_has_global_scope (app))
758 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
759 transport_proto, is_start);
760 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
761 transport_proto, is_start);
766 application_setup_proxy (application_t * app)
768 u16 transports = app->proxied_transports;
769 transport_proto_t tp;
771 ASSERT (application_is_proxy (app));
774 transport_proto_foreach (tp, ({
775 if (transports & (1 << tp))
776 application_start_stop_proxy (app, tp, 1);
782 application_remove_proxy (application_t * app)
784 u16 transports = app->proxied_transports;
785 transport_proto_t tp;
787 ASSERT (application_is_proxy (app));
790 transport_proto_foreach (tp, ({
791 if (transports & (1 << tp))
792 application_start_stop_proxy (app, tp, 0);
797 segment_manager_properties_t *
798 application_segment_manager_properties (application_t * app)
800 return &app->sm_properties;
803 segment_manager_properties_t *
804 application_get_segment_manager_properties (u32 app_index)
806 application_t *app = application_get (app_index);
807 return &app->sm_properties;
811 app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
813 if (PREDICT_TRUE (!svm_msg_q_is_full (mq)))
817 svm_msg_q_add_w_lock (mq, msg);
818 svm_msg_q_unlock (mq);
820 else if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
822 clib_warning ("msg q add returned");
824 svm_msg_q_unlock (mq);
830 clib_warning ("evt q full");
831 svm_msg_q_free_msg (mq, msg);
833 svm_msg_q_unlock (mq);
840 app_send_io_evt_rx (application_t * app, stream_session_t * s, u8 lock)
842 session_fifo_event_t *evt;
846 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
848 /* Session is closed so app will never clean up. Flush rx fifo */
849 svm_fifo_dequeue_drop_all (s->server_rx_fifo);
853 /* Built-in app? Hand event to the callback... */
854 if (app->cb_fns.builtin_app_rx_callback)
855 return app->cb_fns.builtin_app_rx_callback (s);
857 /* If no need for event, return */
858 if (!svm_fifo_set_event (s->server_rx_fifo))
861 mq = app->event_queue;
865 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
867 clib_warning ("evt q rings full");
869 svm_msg_q_unlock (mq);
873 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
874 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
876 evt = (session_fifo_event_t *) svm_msg_q_msg_data (mq, &msg);
877 evt->fifo = s->server_rx_fifo;
878 evt->event_type = FIFO_EVENT_APP_RX;
880 return app_enqueue_evt (mq, &msg, lock);
884 app_send_io_evt_tx (application_t * app, stream_session_t * s, u8 lock)
887 session_fifo_event_t *evt;
890 if (application_is_builtin (app))
893 mq = app->event_queue;
897 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
899 clib_warning ("evt q rings full");
901 svm_msg_q_unlock (mq);
905 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
906 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
908 evt = (session_fifo_event_t *) svm_msg_q_msg_data (mq, &msg);
909 evt->event_type = FIFO_EVENT_APP_TX;
910 evt->fifo = s->server_tx_fifo;
912 return app_enqueue_evt (mq, &msg, lock);
916 typedef int (app_send_evt_handler_fn) (application_t *app,
919 static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
926 * Send event to application
928 * Logic from queue perspective is non-blocking. That is, if there's
929 * not enough space to enqueue a message, we return. However, if the lock
930 * flag is set, we do wait for queue mutex.
933 application_send_event (application_t * app, stream_session_t * s,
936 ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
937 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
941 application_lock_and_send_event (application_t * app, stream_session_t * s,
944 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
948 application_alloc_local_session (application_t * app)
951 pool_get (app->local_sessions, s);
952 memset (s, 0, sizeof (*s));
953 s->app_index = app->index;
954 s->session_index = s - app->local_sessions;
955 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
960 application_free_local_session (application_t * app, local_session_t * s)
962 pool_put (app->local_sessions, s);
964 memset (s, 0xfc, sizeof (*s));
968 application_get_local_session (application_t * app, u32 session_index)
970 return pool_elt_at_index (app->local_sessions, session_index);
974 application_get_local_session_from_handle (session_handle_t handle)
976 application_t *server;
977 u32 session_index, server_index;
978 local_session_parse_handle (handle, &server_index, &session_index);
979 server = application_get (server_index);
980 return application_get_local_session (server, session_index);
984 application_local_listener_session_endpoint (local_session_t * ll,
985 session_endpoint_t * sep)
987 sep->transport_proto =
988 session_type_transport_proto (ll->listener_session_type);
989 sep->port = ll->port;
990 sep->is_ip4 = ll->listener_session_type & 1;
994 application_start_local_listen (application_t * server,
995 session_endpoint_t * sep,
996 session_handle_t * handle)
1002 table_index = application_local_session_table (server);
1004 /* An exact sep match, as opposed to session_lookup_local_listener */
1005 lh = session_lookup_endpoint_listener (table_index, sep, 1);
1006 if (lh != SESSION_INVALID_HANDLE)
1007 return VNET_API_ERROR_ADDRESS_IN_USE;
1009 pool_get (server->local_listen_sessions, ll);
1010 memset (ll, 0, sizeof (*ll));
1011 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1012 ll->app_index = server->index;
1013 ll->session_index = ll - server->local_listen_sessions;
1014 ll->port = sep->port;
1015 /* Store the original session type for the unbind */
1016 ll->listener_session_type =
1017 session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
1018 ll->transport_listener_index = ~0;
1020 *handle = application_local_session_handle (ll);
1021 session_lookup_add_session_endpoint (table_index, sep, *handle);
1027 * Clean up local session table. If we have a listener session use it to
1028 * find the port and proto. If not, the handle must be a local table handle
1032 application_stop_local_listen (application_t * server, session_handle_t lh)
1034 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1035 u32 table_index, ll_index, server_index;
1036 stream_session_t *sl = 0;
1037 local_session_t *ll, *ls;
1039 table_index = application_local_session_table (server);
1041 /* We have both local and global table binds. Figure from global what
1042 * the sep we should be cleaning up is.
1044 if (!session_handle_is_local (lh))
1046 sl = listen_session_get_from_handle (lh);
1047 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1049 clib_warning ("broken listener");
1052 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1053 if (lh == SESSION_INVALID_HANDLE)
1057 local_session_parse_handle (lh, &server_index, &ll_index);
1058 ASSERT (server->index == server_index);
1059 if (!(ll = application_get_local_listen_session (server, ll_index)))
1061 clib_warning ("no local listener");
1064 application_local_listener_session_endpoint (ll, &sep);
1065 session_lookup_del_session_endpoint (table_index, &sep);
1068 pool_foreach (ls, server->local_sessions, ({
1069 if (ls->listener_index == ll->session_index)
1070 application_local_session_disconnect (server->index, ls);
1073 pool_put_index (server->local_listen_sessions, ll->session_index);
1079 application_local_session_connect (u32 table_index, application_t * client,
1080 application_t * server,
1081 local_session_t * ll, u32 opaque)
1083 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1084 segment_manager_properties_t *props, *cprops;
1085 int rv, has_transport, seg_index;
1086 svm_fifo_segment_private_t *seg;
1087 segment_manager_t *sm;
1088 local_session_t *ls;
1089 svm_msg_q_t *sq, *cq;
1091 ls = application_alloc_local_session (server);
1093 props = application_segment_manager_properties (server);
1094 cprops = application_segment_manager_properties (client);
1095 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
1096 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
1097 seg_size = props->rx_fifo_size + props->tx_fifo_size + evt_q_sz + margin;
1099 has_transport = session_has_transport ((stream_session_t *) ll);
1102 /* Local sessions don't have backing transport */
1103 ls->port = ll->port;
1104 sm = application_get_local_segment_manager (server);
1108 stream_session_t *sl = (stream_session_t *) ll;
1109 transport_connection_t *tc;
1110 tc = listen_session_get_transport (sl);
1111 ls->port = tc->lcl_port;
1112 sm = application_get_listen_segment_manager (server, sl);
1115 seg_index = segment_manager_add_segment (sm, seg_size);
1118 clib_warning ("failed to add new cut-through segment");
1121 seg = segment_manager_get_segment_w_lock (sm, seg_index);
1122 sq = segment_manager_alloc_queue (seg, props->evt_q_size);
1123 cq = segment_manager_alloc_queue (seg, cprops->evt_q_size);
1124 ls->server_evt_q = pointer_to_uword (sq);
1125 ls->client_evt_q = pointer_to_uword (cq);
1126 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1127 props->tx_fifo_size,
1128 &ls->server_rx_fifo,
1129 &ls->server_tx_fifo);
1132 clib_warning ("failed to add fifos in cut-through segment");
1133 segment_manager_segment_reader_unlock (sm);
1136 ls->server_rx_fifo->master_session_index = ls->session_index;
1137 ls->server_tx_fifo->master_session_index = ls->session_index;
1138 ls->server_rx_fifo->master_thread_index = ~0;
1139 ls->server_tx_fifo->master_thread_index = ~0;
1140 ls->svm_segment_index = seg_index;
1141 ls->listener_index = ll->session_index;
1142 ls->client_index = client->index;
1143 ls->client_opaque = opaque;
1144 ls->listener_session_type = ll->session_type;
1146 if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
1149 clib_warning ("failed to notify server of new segment");
1150 segment_manager_segment_reader_unlock (sm);
1153 segment_manager_segment_reader_unlock (sm);
1154 if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
1156 clib_warning ("failed to send accept cut-through notify to server");
1159 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1160 application_local_session_connect_notify (ls);
1166 segment_manager_del_segment (sm, seg);
1171 application_client_local_connect_key (local_session_t * ls)
1173 return ((uword) ls->app_index << 32 | (uword) ls->session_index);
1177 application_client_local_connect_key_parse (uword key, u32 * app_index,
1178 u32 * session_index)
1180 *app_index = key >> 32;
1181 *session_index = key & 0xFFFFFFFF;
1185 application_local_session_connect_notify (local_session_t * ls)
1187 svm_fifo_segment_private_t *seg;
1188 application_t *client, *server;
1189 segment_manager_t *sm;
1190 int rv, is_fail = 0;
1193 client = application_get (ls->client_index);
1194 server = application_get (ls->app_index);
1195 sm = application_get_local_segment_manager_w_session (server, ls);
1196 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1197 if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1200 clib_warning ("failed to notify client %u of new segment",
1202 segment_manager_segment_reader_unlock (sm);
1203 application_local_session_disconnect (ls->client_index, ls);
1208 segment_manager_segment_reader_unlock (sm);
1211 client->cb_fns.session_connected_callback (client->index, ls->client_opaque,
1212 (stream_session_t *) ls,
1215 client_key = application_client_local_connect_key (ls);
1216 hash_set (client->local_connects, client_key, client_key);
1221 application_local_session_cleanup (application_t * client,
1222 application_t * server,
1223 local_session_t * ls)
1225 svm_fifo_segment_private_t *seg;
1226 segment_manager_t *sm;
1230 has_transport = session_has_transport ((stream_session_t *) ls);
1231 client_key = application_client_local_connect_key (ls);
1233 sm = application_get_local_segment_manager_w_session (server, ls);
1235 sm = application_get_listen_segment_manager (server,
1236 (stream_session_t *) ls);
1238 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1240 hash_unset (client->local_connects, client_key);
1244 server->cb_fns.del_segment_callback (server->api_client_index,
1247 client->cb_fns.del_segment_callback (client->api_client_index,
1249 segment_manager_del_segment (sm, seg);
1252 application_free_local_session (server, ls);
1258 application_local_session_disconnect (u32 app_index, local_session_t * ls)
1260 application_t *client, *server;
1262 client = application_get_if_valid (ls->client_index);
1263 server = application_get (ls->app_index);
1265 if (ls->session_state == SESSION_STATE_CLOSED)
1266 return application_local_session_cleanup (client, server, ls);
1268 if (app_index == ls->client_index)
1270 send_local_session_disconnect_callback (ls->app_index, ls);
1276 return application_local_session_cleanup (client, server, ls);
1278 else if (ls->session_state < SESSION_STATE_READY)
1280 client->cb_fns.session_connected_callback (client->index,
1282 (stream_session_t *) ls,
1284 ls->session_state = SESSION_STATE_CLOSED;
1285 return application_local_session_cleanup (client, server, ls);
1289 send_local_session_disconnect_callback (client->index, ls);
1293 ls->session_state = SESSION_STATE_CLOSED;
1299 application_local_session_disconnect_w_index (u32 app_index, u32 ls_index)
1302 local_session_t *ls;
1303 app = application_get (app_index);
1304 ls = application_get_local_session (app, ls_index);
1305 return application_local_session_disconnect (app_index, ls);
1309 application_local_sessions_del (application_t * app)
1311 u32 index, server_index, session_index, table_index;
1312 segment_manager_t *sm;
1313 u64 handle, *handles = 0;
1314 local_session_t *ls, *ll;
1315 application_t *server;
1316 session_endpoint_t sep;
1320 * Local listens. Don't bother with local sessions, we clean them lower
1322 table_index = application_local_session_table (app);
1324 pool_foreach (ll, app->local_listen_sessions, ({
1325 application_local_listener_session_endpoint (ll, &sep);
1326 session_lookup_del_session_endpoint (table_index, &sep);
1333 if (app->local_sessions)
1336 pool_foreach (ls, app->local_sessions, ({
1337 application_local_session_disconnect (app->index, ls);
1345 vec_reset_length (handles);
1347 hash_foreach (handle, index, app->local_connects, ({
1348 vec_add1 (handles, handle);
1352 for (i = 0; i < vec_len (handles); i++)
1354 application_client_local_connect_key_parse (handles[i], &server_index,
1356 server = application_get_if_valid (server_index);
1359 ls = application_get_local_session (server, session_index);
1360 application_local_session_disconnect (app->index, ls);
1364 sm = segment_manager_get (app->local_segment_manager);
1365 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1366 segment_manager_del (sm);
1370 vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1373 app = application_get (a->app_index);
1375 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1376 0, "app %u doesn't exist", a->app_index);
1377 app->tls_cert = vec_dup (a->cert);
1382 vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1385 app = application_get (a->app_index);
1387 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1388 0, "app %u doesn't exist", a->app_index);
1389 app->tls_key = vec_dup (a->key);
1394 format_application_listener (u8 * s, va_list * args)
1396 application_t *app = va_arg (*args, application_t *);
1397 u64 handle = va_arg (*args, u64);
1398 u32 sm_index = va_arg (*args, u32);
1399 int verbose = va_arg (*args, int);
1400 stream_session_t *listener;
1406 s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
1407 "API Client", "ListenerID", "SegManager");
1409 s = format (s, "%-40s%-20s", "Connection", "App");
1414 app_name = app_get_name_from_reg_index (app);
1415 listener = listen_session_get_from_handle (handle);
1416 str = format (0, "%U", format_stream_session, listener, verbose);
1420 s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
1421 app->api_client_index, handle, sm_index);
1424 s = format (s, "%-40s%-20s", str, app_name);
1426 vec_free (app_name);
1431 application_format_connects (application_t * app, int verbose)
1433 svm_fifo_segment_private_t *fifo_segment;
1434 vlib_main_t *vm = vlib_get_main ();
1435 segment_manager_t *sm;
1436 u8 *app_name, *s = 0;
1442 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1443 "API Client", "SegManager");
1445 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1450 if (app->connects_seg_manager == (u32) ~ 0)
1453 app_name = app_get_name_from_reg_index (app);
1455 /* Across all fifo segments */
1456 sm = segment_manager_get (app->connects_seg_manager);
1459 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
1463 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
1466 u32 session_index, thread_index;
1467 stream_session_t *session;
1469 session_index = fifo->master_session_index;
1470 thread_index = fifo->master_thread_index;
1472 session = session_get (session_index, thread_index);
1473 str = format (0, "%U", format_stream_session, session, verbose);
1476 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
1477 app->api_client_index, app->connects_seg_manager);
1479 s = format (s, "%-40s%-20s", str, app_name);
1481 vlib_cli_output (vm, "%v", s);
1482 vec_reset_length (s);
1491 vec_free (app_name);
1495 application_format_local_sessions (application_t * app, int verbose)
1497 vlib_main_t *vm = vlib_get_main ();
1498 local_session_t *ls;
1499 transport_proto_t tp;
1505 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
1511 pool_foreach (ls, app->local_listen_sessions, ({
1512 tp = session_type_transport_proto(ls->listener_session_type);
1513 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1515 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_index, "*");
1516 vec_reset_length (conn);
1518 pool_foreach (ls, app->local_sessions, ({
1519 tp = session_type_transport_proto(ls->listener_session_type);
1520 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1522 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_index,
1524 vec_reset_length (conn);
1532 application_format_local_connects (application_t * app, int verbose)
1534 vlib_main_t *vm = vlib_get_main ();
1535 u32 app_index, session_index;
1536 application_t *server;
1537 local_session_t *ls;
1545 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
1546 "Peer App", "SegManager");
1548 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
1554 hash_foreach (client_key, value, app->local_connects, ({
1555 application_client_local_connect_key_parse (client_key, &app_index,
1557 server = application_get (app_index);
1558 ls = application_get_local_session (server, session_index);
1559 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_index, ls->client_index);
1565 format_application (u8 * s, va_list * args)
1567 application_t *app = va_arg (*args, application_t *);
1568 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1569 segment_manager_properties_t *props;
1570 const u8 *app_ns_name;
1576 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
1577 "API Client", "Namespace", "Add seg size", "Rx-f size",
1580 s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
1585 app_name = app_get_name (app);
1586 app_ns_name = app_namespace_id_from_index (app->ns_index);
1587 props = application_segment_manager_properties (app);
1589 s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->index,
1590 app_name, app->api_client_index, app->ns_index,
1591 format_memory_size, props->add_segment_size,
1592 format_memory_size, props->rx_fifo_size, format_memory_size,
1593 props->tx_fifo_size);
1595 s = format (s, "%-10u%-20s%-15d%-40s", app->index, app_name,
1596 app->api_client_index, app_ns_name);
1602 application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1608 if (!pool_elts (app_pool))
1610 vlib_cli_output (vm, "No active server bindings");
1616 application_format_local_sessions (0, verbose);
1618 pool_foreach (app, app_pool, ({
1619 if (!pool_elts (app->local_sessions)
1620 && !pool_elts(app->local_connects))
1622 application_format_local_sessions (app, verbose);
1628 vlib_cli_output (vm, "%U", format_application_listener, 0 /* header */ ,
1632 pool_foreach (app, app_pool, ({
1633 if (hash_elts (app->listeners_table) == 0)
1635 hash_foreach (handle, sm_index, app->listeners_table, ({
1636 vlib_cli_output (vm, "%U", format_application_listener, app,
1637 handle, sm_index, verbose);
1645 application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1649 if (!pool_elts (app_pool))
1651 vlib_cli_output (vm, "No active apps");
1657 application_format_local_connects (0, verbose);
1660 pool_foreach (app, app_pool, ({
1661 if (app->local_connects)
1662 application_format_local_connects (app, verbose);
1668 application_format_connects (0, verbose);
1671 pool_foreach (app, app_pool, ({
1672 if (app->connects_seg_manager == (u32)~0)
1674 application_format_connects (app, verbose);
1680 static clib_error_t *
1681 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1682 vlib_cli_command_t * cmd)
1684 int do_server = 0, do_client = 0, do_local = 0;
1688 session_cli_return_if_not_enabled ();
1690 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1692 if (unformat (input, "server"))
1694 else if (unformat (input, "client"))
1696 else if (unformat (input, "local"))
1698 else if (unformat (input, "verbose"))
1705 application_format_all_listeners (vm, do_local, verbose);
1708 application_format_all_clients (vm, do_local, verbose);
1710 /* Print app related info */
1711 if (!do_server && !do_client)
1713 vlib_cli_output (vm, "%U", format_application, 0, verbose);
1715 pool_foreach (app, app_pool, ({
1716 vlib_cli_output (vm, "%U", format_application, app, verbose);
1725 VLIB_CLI_COMMAND (show_app_command, static) =
1728 .short_help = "show app [server|client] [verbose]",
1729 .function = show_app_command_fn,
1734 * fd.io coding-style-patch-verification: ON
1737 * eval: (c-set-style "gnu")