2 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/session/application.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/application_namespace.h>
19 #include <vnet/session/application_local.h>
20 #include <vnet/session/session.h>
22 static app_main_t app_main;
24 #define app_interface_check_thread_and_barrier(_fn, _arg) \
25 if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \
27 vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \
31 static app_listener_t *
32 app_listener_alloc (application_t * app)
34 app_main_t *am = &app_main;
35 app_listener_t *app_listener;
37 pool_get (am->listeners, app_listener);
38 clib_memset (app_listener, 0, sizeof (*app_listener));
39 app_listener->al_index = app_listener - am->listeners;
40 app_listener->app_index = app->app_index;
41 app_listener->session_index = SESSION_INVALID_INDEX;
42 app_listener->local_index = SESSION_INVALID_INDEX;
43 app_listener->ls_handle = SESSION_INVALID_HANDLE;
48 app_listener_get (u32 app_listener_index)
50 app_main_t *am = &app_main;
52 return pool_elt_at_index (am->listeners, app_listener_index);
56 app_listener_free (application_t * app, app_listener_t * app_listener)
58 app_main_t *am = &app_main;
60 clib_bitmap_free (app_listener->workers);
61 vec_free (app_listener->cl_listeners);
63 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
64 pool_put (am->listeners, app_listener);
68 app_listener_handle (app_listener_t * al)
74 app_listen_session_handle (session_t * ls)
77 /* TODO(fcoras): quic session handles */
78 if (ls->al_index == SESSION_INVALID_INDEX)
79 return listen_session_get_handle (ls);
80 al = app_listener_get (ls->al_index);
85 app_listener_get_w_handle (session_handle_t handle)
88 ls = session_get_from_handle_if_valid (handle);
91 return app_listener_get (ls->al_index);
95 app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
97 u32 table_index, fib_proto;
98 session_endpoint_t *sep;
99 session_handle_t handle;
102 ip46_address_t original_ip;
104 sep = (session_endpoint_t *) sep_ext;
105 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
107 table_index = application_local_session_table (app);
108 handle = session_lookup_endpoint_listener (table_index, sep, 1);
109 if (handle != SESSION_INVALID_HANDLE)
111 ls = listen_session_get_from_handle (handle);
112 return app_listener_get (ls->al_index);
116 fib_proto = session_endpoint_fib_proto (sep);
117 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
118 handle = session_lookup_endpoint_listener (table_index, sep, 1);
119 if (handle != SESSION_INVALID_HANDLE)
121 ls = listen_session_get_from_handle (handle);
122 return app_listener_get (ls->al_index);
126 * When binds to "inaddr_any", we add zero address in the local lookup table
127 * and interface address in the global lookup table. If local scope disable,
128 * the latter is the only clue to find the listener.
130 if (!application_has_local_scope (app) &&
131 ip_is_zero (&sep_ext->ip, sep_ext->is_ip4) &&
132 sep_ext->sw_if_index != ENDPOINT_INVALID_INDEX)
134 if ((iface_ip = ip_interface_get_first_ip (sep_ext->sw_if_index,
137 ip_copy (&original_ip, &sep_ext->ip, sep_ext->is_ip4);
138 ip_set (&sep_ext->ip, iface_ip, sep_ext->is_ip4);
139 handle = session_lookup_endpoint_listener (table_index, sep, 1);
140 ip_copy (&sep_ext->ip, &original_ip, sep_ext->is_ip4);
141 if (handle != SESSION_INVALID_HANDLE)
143 ls = listen_session_get_from_handle (handle);
144 return app_listener_get (ls->al_index);
153 app_listener_alloc_and_init (application_t * app,
154 session_endpoint_cfg_t * sep,
155 app_listener_t ** listener)
157 app_listener_t *app_listener;
158 transport_connection_t *tc;
159 u32 al_index, table_index;
165 app_listener = app_listener_alloc (app);
166 al_index = app_listener->al_index;
167 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
170 * Add session endpoint to local session table. Only binds to "inaddr_any"
171 * (i.e., zero address) are added to local scope table.
173 if (application_has_local_scope (app)
174 && session_endpoint_is_local ((session_endpoint_t *) sep))
176 session_type_t local_st;
178 local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
180 ls = listen_session_alloc (0, local_st);
181 ls->app_wrk_index = sep->app_wrk_index;
182 lh = session_handle (ls);
184 if ((rv = session_listen (ls, sep)))
186 ls = session_get_from_handle (lh);
188 app_listener_free (app, app_listener);
192 ls = session_get_from_handle (lh);
193 app_listener = app_listener_get (al_index);
194 app_listener->local_index = ls->session_index;
195 app_listener->ls_handle = lh;
196 ls->al_index = al_index;
198 table_index = application_local_session_table (app);
199 session_lookup_add_session_endpoint (table_index,
200 (session_endpoint_t *) sep, lh);
203 if (application_has_global_scope (app))
206 * Start listening on local endpoint for requested transport and scope.
207 * Creates a stream session with state LISTENING to be used in session
208 * lookups, prior to establishing connection. Requests transport to
209 * build it's own specific listening connection.
211 ls = listen_session_alloc (0, st);
212 ls->app_wrk_index = sep->app_wrk_index;
214 /* Listen pool can be reallocated if the transport is
216 lh = listen_session_get_handle (ls);
218 if ((rv = session_listen (ls, sep)))
220 ls = listen_session_get_from_handle (lh);
222 app_listener_free (app, app_listener);
225 ls = listen_session_get_from_handle (lh);
226 app_listener = app_listener_get (al_index);
227 app_listener->session_index = ls->session_index;
228 app_listener->ls_handle = lh;
229 ls->al_index = al_index;
231 /* Add to the global lookup table after transport was initialized.
232 * Lookup table needs to be populated only now because sessions
233 * with cut-through transport are are added to app local tables that
234 * are not related to network fibs, i.e., cannot be added as
236 tc = session_get_transport (ls);
237 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
239 fib_protocol_t fib_proto;
240 fib_proto = session_endpoint_fib_proto ((session_endpoint_t *) sep);
241 /* Assume namespace vetted previously so make sure table exists */
242 table_index = session_lookup_get_or_alloc_index_for_fib (
243 fib_proto, sep->fib_index);
244 session_lookup_add_session_endpoint (table_index,
245 (session_endpoint_t *) sep,
252 app_listener_free (app, app_listener);
256 *listener = app_listener;
261 app_listener_cleanup (app_listener_t * al)
263 application_t *app = application_get (al->app_index);
266 if (al->session_index != SESSION_INVALID_INDEX)
268 ls = session_get (al->session_index, 0);
269 session_stop_listen (ls);
270 listen_session_free (ls);
272 if (al->local_index != SESSION_INVALID_INDEX)
274 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
277 table_index = application_local_session_table (app);
278 ls = listen_session_get (al->local_index);
279 ct_session_endpoint (ls, &sep);
280 session_lookup_del_session_endpoint (table_index, &sep);
281 session_stop_listen (ls);
282 listen_session_free (ls);
284 app_listener_free (app, al);
287 static app_worker_t *
288 app_listener_select_worker (app_listener_t *al)
293 app = application_get (al->app_index);
294 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
296 wrk_index = clib_bitmap_first_set (al->workers);
298 ASSERT (wrk_index != ~0);
299 al->accept_rotor = wrk_index;
300 return application_get_worker (app, wrk_index);
304 app_listener_get_session (app_listener_t * al)
306 if (al->session_index == SESSION_INVALID_INDEX)
309 return listen_session_get (al->session_index);
313 app_listener_get_local_session (app_listener_t * al)
315 if (al->local_index == SESSION_INVALID_INDEX)
317 return listen_session_get (al->local_index);
321 app_listener_get_wrk_cl_session (app_listener_t *al, u32 wrk_map_index)
323 u32 si = vec_elt (al->cl_listeners, wrk_map_index);
324 return session_get (si, 0 /* listener thread */);
327 static app_worker_map_t *
328 app_worker_map_alloc (application_t * app)
330 app_worker_map_t *map;
331 pool_get (app->worker_maps, map);
332 clib_memset (map, 0, sizeof (*map));
337 app_worker_map_index (application_t * app, app_worker_map_t * map)
339 return (map - app->worker_maps);
343 app_worker_map_free (application_t * app, app_worker_map_t * map)
345 pool_put (app->worker_maps, map);
348 static app_worker_map_t *
349 app_worker_map_get (application_t * app, u32 map_index)
351 if (pool_is_free_index (app->worker_maps, map_index))
353 return pool_elt_at_index (app->worker_maps, map_index);
357 app_get_name (application_t * app)
363 application_session_table (application_t * app, u8 fib_proto)
365 app_namespace_t *app_ns;
366 app_ns = app_namespace_get (app->ns_index);
367 if (!application_has_global_scope (app))
368 return APP_INVALID_INDEX;
369 if (fib_proto == FIB_PROTOCOL_IP4)
370 return session_lookup_get_index_for_fib (fib_proto,
371 app_ns->ip4_fib_index);
373 return session_lookup_get_index_for_fib (fib_proto,
374 app_ns->ip6_fib_index);
378 application_local_session_table (application_t * app)
380 app_namespace_t *app_ns;
381 if (!application_has_local_scope (app))
382 return APP_INVALID_INDEX;
383 app_ns = app_namespace_get (app->ns_index);
384 return app_ns->local_table_index;
388 * Returns app name for app-index
391 application_name_from_index (u32 app_index)
393 application_t *app = application_get (app_index);
396 return app_get_name (app);
400 application_api_table_add (u32 app_index, u32 api_client_index)
402 if (api_client_index != APP_INVALID_INDEX)
403 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
407 application_api_table_del (u32 api_client_index)
409 hash_unset (app_main.app_by_api_client_index, api_client_index);
413 application_name_table_add (application_t * app)
415 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
419 application_name_table_del (application_t * app)
421 hash_unset_mem (app_main.app_by_name, app->name);
425 application_lookup (u32 api_client_index)
428 p = hash_get (app_main.app_by_api_client_index, api_client_index);
430 return application_get_if_valid (p[0]);
436 application_lookup_name (const u8 * name)
439 p = hash_get_mem (app_main.app_by_name, name);
441 return application_get (p[0]);
447 appsl_pending_rx_mqs_add_tail (appsl_wrk_t *aw, app_rx_mq_elt_t *elt)
449 app_rx_mq_elt_t *head;
451 if (!aw->pending_rx_mqs)
453 elt->next = elt->prev = elt;
454 aw->pending_rx_mqs = elt;
458 head = aw->pending_rx_mqs;
460 ASSERT (head != elt);
462 elt->prev = head->prev;
465 head->prev->next = elt;
470 appsl_pending_rx_mqs_del (appsl_wrk_t *aw, app_rx_mq_elt_t *elt)
472 if (elt->next == elt)
474 elt->next = elt->prev = 0;
475 aw->pending_rx_mqs = 0;
479 if (elt == aw->pending_rx_mqs)
480 aw->pending_rx_mqs = elt->next;
482 elt->next->prev = elt->prev;
483 elt->prev->next = elt->next;
484 elt->next = elt->prev = 0;
487 vlib_node_registration_t appsl_rx_mqs_input_node;
489 VLIB_NODE_FN (appsl_rx_mqs_input_node)
490 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
492 u32 thread_index = vm->thread_index, n_msgs = 0;
493 app_rx_mq_elt_t *elt, *next;
494 app_main_t *am = &app_main;
495 session_worker_t *wrk;
496 int __clib_unused rv;
500 aw = &am->wrk[thread_index];
501 elt = aw->pending_rx_mqs;
505 wrk = session_main_get_worker (thread_index);
509 if (!(elt->flags & APP_RX_MQ_F_POSTPONED))
510 rv = read (svm_msg_q_get_eventfd (elt->mq), &buf, sizeof (buf));
511 n_msgs += session_wrk_handle_mq (wrk, elt->mq);
514 appsl_pending_rx_mqs_del (aw, elt);
515 if (!svm_msg_q_is_empty (elt->mq))
517 elt->flags |= APP_RX_MQ_F_POSTPONED;
518 appsl_pending_rx_mqs_add_tail (aw, elt);
526 while (aw->pending_rx_mqs && elt != aw->pending_rx_mqs);
528 if (aw->pending_rx_mqs)
529 vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index);
531 if (n_msgs && wrk->state == SESSION_WRK_INTERRUPT)
532 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
537 VLIB_REGISTER_NODE (appsl_rx_mqs_input_node) = {
538 .name = "appsl-rx-mqs-input",
539 .type = VLIB_NODE_TYPE_INPUT,
540 .state = VLIB_NODE_STATE_DISABLED,
543 static clib_error_t *
544 app_rx_mq_fd_read_ready (clib_file_t *cf)
546 app_rx_mq_handle_t *handle = (app_rx_mq_handle_t *) &cf->private_data;
547 vlib_main_t *vm = vlib_get_main ();
548 app_main_t *am = &app_main;
549 app_rx_mq_elt_t *mqe;
553 ASSERT (vlib_get_thread_index () == handle->thread_index);
554 app = application_get_if_valid (handle->app_index);
558 mqe = &app->rx_mqs[handle->thread_index];
559 if ((mqe->flags & APP_RX_MQ_F_PENDING) || svm_msg_q_is_empty (mqe->mq))
562 aw = &am->wrk[handle->thread_index];
563 appsl_pending_rx_mqs_add_tail (aw, mqe);
564 mqe->flags |= APP_RX_MQ_F_PENDING;
566 vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index);
571 static clib_error_t *
572 app_rx_mq_fd_write_ready (clib_file_t *cf)
574 clib_warning ("should not be called");
579 app_rx_mqs_epoll_add (application_t *app, app_rx_mq_elt_t *mqe)
581 clib_file_t template = { 0 };
582 app_rx_mq_handle_t handle;
586 thread_index = mqe - app->rx_mqs;
587 fd = svm_msg_q_get_eventfd (mqe->mq);
589 handle.app_index = app->app_index;
590 handle.thread_index = thread_index;
592 template.read_function = app_rx_mq_fd_read_ready;
593 template.write_function = app_rx_mq_fd_write_ready;
594 template.file_descriptor = fd;
595 template.private_data = handle.as_u64;
596 template.polling_thread_index = thread_index;
597 template.description =
598 format (0, "app-%u-rx-mq-%u", app->app_index, thread_index);
599 mqe->file_index = clib_file_add (&file_main, &template);
603 app_rx_mqs_epoll_del (application_t *app, app_rx_mq_elt_t *mqe)
605 u32 thread_index = mqe - app->rx_mqs;
606 app_main_t *am = &app_main;
609 aw = &am->wrk[thread_index];
611 session_wrk_handle_mq (session_main_get_worker (thread_index), mqe->mq);
613 if (mqe->flags & APP_RX_MQ_F_PENDING)
614 appsl_pending_rx_mqs_del (aw, mqe);
616 clib_file_del_by_index (&file_main, mqe->file_index);
620 application_rx_mq_get (application_t *app, u32 mq_index)
625 return app->rx_mqs[mq_index].mq;
629 app_rx_mqs_alloc (application_t *app)
631 u32 evt_q_length, evt_size = sizeof (session_event_t);
632 fifo_segment_t *eqs = &app->rx_mqs_segment;
633 u32 n_mqs = vlib_num_workers () + 1;
634 segment_manager_props_t *props;
637 props = application_segment_manager_properties (app);
638 evt_q_length = clib_max (props->evt_q_size, 128);
640 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
641 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
642 { evt_q_length, evt_size, 0 }, { evt_q_length >> 1, 256, 0 }
644 cfg->consumer_pid = 0;
646 cfg->q_nitems = evt_q_length;
649 eqs->ssvm.ssvm_size = svm_msg_q_size_to_alloc (cfg) * n_mqs + (1 << 20);
650 eqs->ssvm.name = format (0, "%v-rx-mqs-seg%c", app->name, 0);
652 if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
654 clib_warning ("failed to initialize queue segment");
655 return SESSION_E_SEG_CREATE;
658 fifo_segment_init (eqs);
660 /* Fifo segment filled only with mqs */
661 eqs->h->n_mqs = n_mqs;
662 vec_validate (app->rx_mqs, n_mqs - 1);
664 for (i = 0; i < n_mqs; i++)
666 app->rx_mqs[i].mq = fifo_segment_msg_q_alloc (eqs, i, cfg);
667 if (svm_msg_q_alloc_eventfd (app->rx_mqs[i].mq))
669 clib_warning ("eventfd returned");
670 fifo_segment_cleanup (eqs);
671 ssvm_delete (&eqs->ssvm);
672 return SESSION_E_EVENTFD_ALLOC;
674 app_rx_mqs_epoll_add (app, &app->rx_mqs[i]);
675 app->rx_mqs[i].app_index = app->app_index;
682 application_use_private_rx_mqs (void)
684 return session_main.use_private_rx_mqs;
688 application_get_rx_mqs_segment (application_t *app)
690 if (application_use_private_rx_mqs ())
691 return &app->rx_mqs_segment;
692 return session_main_get_wrk_mqs_segment ();
696 application_enable_rx_mqs_nodes (u8 is_en)
698 u8 state = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
701 vlib_node_set_state (this_vlib_main, appsl_rx_mqs_input_node.index, state);
704 static application_t *
705 application_alloc (void)
708 pool_get (app_main.app_pool, app);
709 clib_memset (app, 0, sizeof (*app));
710 app->app_index = app - app_main.app_pool;
715 application_get (u32 app_index)
717 if (app_index == APP_INVALID_INDEX)
719 return pool_elt_at_index (app_main.app_pool, app_index);
723 application_get_if_valid (u32 app_index)
725 if (pool_is_free_index (app_main.app_pool, app_index))
728 return pool_elt_at_index (app_main.app_pool, app_index);
732 _null_app_tx_callback (session_t *s)
738 application_verify_cb_fns (session_cb_vft_t * cb_fns)
740 if (cb_fns->session_accept_callback == 0)
741 clib_warning ("No accept callback function provided");
742 if (cb_fns->session_connected_callback == 0)
743 clib_warning ("No session connected callback function provided");
744 if (cb_fns->session_disconnect_callback == 0)
745 clib_warning ("No session disconnect callback function provided");
746 if (cb_fns->session_reset_callback == 0)
747 clib_warning ("No session reset callback function provided");
748 if (!cb_fns->builtin_app_tx_callback)
749 cb_fns->builtin_app_tx_callback = _null_app_tx_callback;
753 * Check app config for given segment type
755 * Returns 1 on success and 0 otherwise
758 application_verify_cfg (ssvm_segment_type_t st)
761 if (st == SSVM_SEGMENT_MEMFD)
763 is_valid = (session_main_get_wrk_mqs_segment () != 0);
765 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
768 else if (st == SSVM_SEGMENT_SHM)
770 is_valid = (session_main_get_wrk_mqs_segment () == 0);
772 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
779 static session_error_t
780 application_alloc_and_init (app_init_args_t *a)
782 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
783 segment_manager_props_t *props;
787 app = application_alloc ();
790 * Make sure we support the requested configuration
792 if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN) &&
793 !(opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_MEMFD_FOR_BUILTIN))
794 seg_type = SSVM_SEGMENT_PRIVATE;
796 if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) &&
797 seg_type != SSVM_SEGMENT_MEMFD)
799 clib_warning ("mq eventfds can only be used if socket transport is "
800 "used for binary api");
801 return SESSION_E_NOSUPPORT;
804 if (!application_verify_cfg (seg_type))
805 return SESSION_E_NOSUPPORT;
807 if (opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS] &&
808 opts[APP_OPTIONS_PREALLOC_FIFO_HDRS])
809 return SESSION_E_NOSUPPORT;
811 /* Check that the obvious things are properly set up */
812 application_verify_cb_fns (a->session_cb_vft);
814 app->flags = opts[APP_OPTIONS_FLAGS];
815 app->cb_fns = *a->session_cb_vft;
816 app->ns_index = opts[APP_OPTIONS_NAMESPACE];
817 app->proxied_transports = opts[APP_OPTIONS_PROXY_TRANSPORT];
818 app->name = vec_dup (a->name);
820 /* If no scope enabled, default to global */
821 if (!application_has_global_scope (app)
822 && !application_has_local_scope (app))
823 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
825 props = application_segment_manager_properties (app);
826 segment_manager_props_init (props);
827 props->segment_size = opts[APP_OPTIONS_SEGMENT_SIZE];
828 props->prealloc_fifos = opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
829 props->prealloc_fifo_hdrs = opts[APP_OPTIONS_PREALLOC_FIFO_HDRS];
830 if (opts[APP_OPTIONS_ADD_SEGMENT_SIZE])
832 props->add_segment_size = opts[APP_OPTIONS_ADD_SEGMENT_SIZE];
833 props->add_segment = 1;
835 if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_HUGE_PAGE)
836 props->huge_page = 1;
837 if (opts[APP_OPTIONS_RX_FIFO_SIZE])
838 props->rx_fifo_size = opts[APP_OPTIONS_RX_FIFO_SIZE];
839 if (opts[APP_OPTIONS_TX_FIFO_SIZE])
840 props->tx_fifo_size = opts[APP_OPTIONS_TX_FIFO_SIZE];
841 if (opts[APP_OPTIONS_EVT_QUEUE_SIZE])
842 props->evt_q_size = opts[APP_OPTIONS_EVT_QUEUE_SIZE];
843 if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
844 props->use_mq_eventfd = 1;
845 if (opts[APP_OPTIONS_TLS_ENGINE])
846 app->tls_engine = opts[APP_OPTIONS_TLS_ENGINE];
847 if (opts[APP_OPTIONS_MAX_FIFO_SIZE])
848 props->max_fifo_size = opts[APP_OPTIONS_MAX_FIFO_SIZE];
849 if (opts[APP_OPTIONS_HIGH_WATERMARK])
850 props->high_watermark = opts[APP_OPTIONS_HIGH_WATERMARK];
851 if (opts[APP_OPTIONS_LOW_WATERMARK])
852 props->low_watermark = opts[APP_OPTIONS_LOW_WATERMARK];
853 if (opts[APP_OPTIONS_PCT_FIRST_ALLOC])
854 props->pct_first_alloc = opts[APP_OPTIONS_PCT_FIRST_ALLOC];
855 props->segment_type = seg_type;
857 /* Add app to lookup by api_client_index table */
858 if (!application_is_builtin (app))
859 application_api_table_add (app->app_index, a->api_client_index);
861 application_name_table_add (app);
863 a->app_index = app->app_index;
865 APP_DBG ("New app name: %v api index: %u index %u", app->name,
866 a->api_client_index, app->app_index);
872 application_free (application_t * app)
874 app_worker_map_t *wrk_map;
875 app_worker_t *app_wrk;
878 * The app event queue allocated in first segment is cleared with
879 * the segment manager. No need to explicitly free it.
881 APP_DBG ("Delete app name %v index: %d", app->name, app->app_index);
883 if (application_is_proxy (app))
884 application_remove_proxy (app);
890 pool_flush (wrk_map, app->worker_maps, ({
891 app_wrk = app_worker_get (wrk_map->wrk_index);
892 app_worker_free (app_wrk);
894 pool_free (app->worker_maps);
897 * Free rx mqs if allocated
902 for (i = 0; i < vec_len (app->rx_mqs); i++)
903 app_rx_mqs_epoll_del (app, &app->rx_mqs[i]);
905 fifo_segment_cleanup (&app->rx_mqs_segment);
906 ssvm_delete (&app->rx_mqs_segment.ssvm);
907 vec_free (app->rx_mqs);
911 * Cleanup remaining state
913 if (application_is_builtin (app))
914 application_name_table_del (app);
915 vec_free (app->name);
916 pool_put (app_main.app_pool, app);
920 application_detach_process (application_t * app, u32 api_client_index)
922 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
923 app_worker_map_t *wrk_map;
924 u32 *wrks = 0, *wrk_index;
925 app_worker_t *app_wrk;
927 if (api_client_index == ~0)
929 application_free (app);
933 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
934 app->app_index, api_client_index);
936 pool_foreach (wrk_map, app->worker_maps) {
937 app_wrk = app_worker_get (wrk_map->wrk_index);
938 if (app_wrk->api_client_index == api_client_index)
939 vec_add1 (wrks, app_wrk->wrk_index);
944 clib_warning ("no workers for app %u api_index %u", app->app_index,
949 args->app_index = app->app_index;
950 args->api_client_index = api_client_index;
951 vec_foreach (wrk_index, wrks)
953 app_wrk = app_worker_get (wrk_index[0]);
954 args->wrk_map_index = app_wrk->wrk_map_index;
956 vnet_app_worker_add_del (args);
962 application_namespace_cleanup (app_namespace_t *app_ns)
964 u32 *app_indices = 0, *app_index;
968 ns_index = app_namespace_index (app_ns);
969 pool_foreach (app, app_main.app_pool)
970 if (app->ns_index == ns_index)
971 vec_add1 (app_indices, app->ns_index);
973 vec_foreach (app_index, app_indices)
975 app = application_get (*app_index);
977 if (application_is_proxy (app))
978 application_remove_proxy (app);
979 app->flags &= ~APP_OPTIONS_FLAGS_IS_PROXY;
981 application_free (app);
983 vec_free (app_indices);
987 application_get_worker (application_t * app, u32 wrk_map_index)
989 app_worker_map_t *map;
990 map = app_worker_map_get (app, wrk_map_index);
993 return app_worker_get (map->wrk_index);
997 application_get_default_worker (application_t * app)
999 return application_get_worker (app, 0);
1003 application_n_workers (application_t * app)
1005 return pool_elts (app->worker_maps);
1009 application_listener_select_worker (session_t * ls)
1013 al = app_listener_get (ls->al_index);
1014 return app_listener_select_worker (al);
1018 app_listener_cl_flow_hash (session_dgram_hdr_t *hdr)
1024 hash = clib_crc32c_u32 (hash, hdr->rmt_ip.ip4.as_u32);
1025 hash = clib_crc32c_u32 (hash, hdr->lcl_ip.ip4.as_u32);
1026 hash = clib_crc32c_u16 (hash, hdr->rmt_port);
1027 hash = clib_crc32c_u16 (hash, hdr->lcl_port);
1031 hash = clib_crc32c_u64 (hash, hdr->rmt_ip.ip6.as_u64[0]);
1032 hash = clib_crc32c_u64 (hash, hdr->rmt_ip.ip6.as_u64[1]);
1033 hash = clib_crc32c_u64 (hash, hdr->lcl_ip.ip6.as_u64[0]);
1034 hash = clib_crc32c_u64 (hash, hdr->lcl_ip.ip6.as_u64[1]);
1035 hash = clib_crc32c_u16 (hash, hdr->rmt_port);
1036 hash = clib_crc32c_u16 (hash, hdr->lcl_port);
1043 app_listener_select_wrk_cl_session (session_t *ls, session_dgram_hdr_t *hdr)
1045 u32 wrk_map_index = 0;
1048 al = app_listener_get (ls->al_index);
1049 /* Crude test to check if only worker 0 is set */
1050 if (al->workers[0] != 1)
1052 u32 hash = app_listener_cl_flow_hash (hdr);
1053 hash %= vec_len (al->workers) * sizeof (uword);
1054 wrk_map_index = clib_bitmap_next_set (al->workers, hash);
1055 if (wrk_map_index == ~0)
1056 wrk_map_index = clib_bitmap_first_set (al->workers);
1059 return app_listener_get_wrk_cl_session (al, wrk_map_index);
1063 application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
1065 app_worker_map_t *wrk_map;
1066 app_worker_t *app_wrk;
1067 segment_manager_t *sm;
1070 app_wrk = app_worker_alloc (app);
1071 wrk_map = app_worker_map_alloc (app);
1072 wrk_map->wrk_index = app_wrk->wrk_index;
1073 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
1076 * Setup first segment manager
1078 sm = segment_manager_alloc ();
1079 sm->app_wrk_index = app_wrk->wrk_index;
1081 if ((rv = segment_manager_init_first (sm)))
1083 app_worker_free (app_wrk);
1086 sm->first_is_protected = 1;
1091 app_wrk->connects_seg_manager = segment_manager_index (sm);
1092 app_wrk->listeners_table = hash_create (0, sizeof (u64));
1093 app_wrk->event_queue = segment_manager_event_queue (sm);
1094 app_wrk->app_is_builtin = application_is_builtin (app);
1102 vnet_app_worker_add_del (vnet_app_worker_add_del_args_t *a)
1105 app_worker_map_t *wrk_map;
1106 app_worker_t *app_wrk;
1107 segment_manager_t *sm;
1111 app = application_get (a->app_index);
1113 return SESSION_E_INVALID;
1117 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
1120 /* Map worker api index to the app */
1121 app_wrk->api_client_index = a->api_client_index;
1122 application_api_table_add (app->app_index, a->api_client_index);
1124 sm = segment_manager_get (app_wrk->connects_seg_manager);
1125 fs = segment_manager_get_segment_w_lock (sm, 0);
1126 a->segment = &fs->ssvm;
1127 a->segment_handle = segment_manager_segment_handle (sm, fs);
1128 segment_manager_segment_reader_unlock (sm);
1129 a->evt_q = app_wrk->event_queue;
1130 a->wrk_map_index = app_wrk->wrk_map_index;
1134 wrk_map = app_worker_map_get (app, a->wrk_map_index);
1136 return SESSION_E_INVALID;
1138 app_wrk = app_worker_get (wrk_map->wrk_index);
1140 return SESSION_E_INVALID;
1142 application_api_table_del (app_wrk->api_client_index);
1143 if (appns_sapi_enabled ())
1144 sapi_socket_close_w_handle (app_wrk->api_client_index);
1145 app_worker_free (app_wrk);
1146 app_worker_map_free (app, wrk_map);
1147 if (application_n_workers (app) == 0)
1148 application_free (app);
1153 static session_error_t
1154 app_validate_namespace (u8 *namespace_id, u64 secret, u32 *app_ns_index)
1156 app_namespace_t *app_ns;
1157 if (vec_len (namespace_id) == 0)
1159 /* Use default namespace */
1164 *app_ns_index = app_namespace_index_from_id (namespace_id);
1165 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
1166 return SESSION_E_INVALID_NS;
1167 app_ns = app_namespace_get (*app_ns_index);
1169 return SESSION_E_INVALID_NS;
1170 if (app_ns->ns_secret != secret)
1171 return SESSION_E_WRONG_NS_SECRET;
1176 app_name_from_api_index (u32 api_client_index)
1178 vl_api_registration_t *regp;
1179 regp = vl_api_client_index_to_registration (api_client_index);
1181 return format (0, "%s", regp->name);
1183 clib_warning ("api client index %u does not have an api registration!",
1185 return format (0, "unknown");
1189 * Attach application to vpp
1191 * Allocates a vpp app, i.e., a structure that keeps back pointers
1192 * to external app and a segment manager for shared memory fifo based
1193 * communication with the external app.
1196 vnet_application_attach (vnet_app_attach_args_t *a)
1199 application_t *app = 0;
1200 app_worker_t *app_wrk;
1201 segment_manager_t *sm;
1202 u32 app_ns_index = 0;
1207 if (a->api_client_index != APP_INVALID_INDEX)
1208 app = application_lookup (a->api_client_index);
1210 app = application_lookup_name (a->name);
1212 return SESSION_E_INVALID;
1215 return SESSION_E_APP_ATTACHED;
1217 /* Socket api sets the name and validates namespace prior to attach */
1218 if (!a->use_sock_api)
1220 if (a->api_client_index != APP_INVALID_INDEX)
1222 app_name = app_name_from_api_index (a->api_client_index);
1226 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
1227 if ((rv = app_validate_namespace (a->namespace_id, secret,
1230 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
1233 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
1236 app = application_get (a->app_index);
1237 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
1240 a->app_evt_q = app_wrk->event_queue;
1241 app_wrk->api_client_index = a->api_client_index;
1242 sm = segment_manager_get (app_wrk->connects_seg_manager);
1243 fs = segment_manager_get_segment_w_lock (sm, 0);
1245 if (application_is_proxy (app))
1247 application_setup_proxy (app);
1248 /* The segment manager pool is reallocated because a new listener
1249 * is added. Re-grab segment manager to avoid dangling reference */
1250 sm = segment_manager_get (app_wrk->connects_seg_manager);
1253 ASSERT (vec_len (fs->ssvm.name) <= 128);
1254 a->segment = &fs->ssvm;
1255 a->segment_handle = segment_manager_segment_handle (sm, fs);
1257 segment_manager_segment_reader_unlock (sm);
1259 if (!application_is_builtin (app) && application_use_private_rx_mqs ())
1260 rv = app_rx_mqs_alloc (app);
1262 vec_free (app_name);
1267 * Detach application from vpp
1270 vnet_application_detach (vnet_app_detach_args_t *a)
1274 app = application_get_if_valid (a->app_index);
1277 clib_warning ("app not attached");
1278 return SESSION_E_NOAPP;
1281 app_interface_check_thread_and_barrier (vnet_application_detach, a);
1282 application_detach_process (app, a->api_client_index);
1287 session_endpoint_in_ns (session_endpoint_cfg_t *sep)
1291 if (sep->flags & SESSION_ENDPT_CFG_F_PROXY_LISTEN)
1294 is_lep = session_endpoint_is_local ((session_endpoint_t *) sep);
1295 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
1296 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
1298 clib_warning ("sw_if_index %u not configured with ip %U",
1299 sep->sw_if_index, format_ip46_address, &sep->ip,
1304 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
1308 session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
1309 application_t * app, u8 is_connect)
1311 app_namespace_t *app_ns;
1312 u32 ns_index, fib_index;
1314 ns_index = app->ns_index;
1316 /* App is a transport proto, so fetch the calling app's ns */
1317 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
1318 ns_index = sep->ns_index;
1320 app_ns = app_namespace_get (ns_index);
1324 /* Ask transport and network to bind to/connect using local interface
1325 * that "supports" app's namespace. This will fix our local connection
1329 /* If in default namespace and user requested a fib index use it */
1330 if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
1331 fib_index = sep->fib_index;
1333 fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1334 sep->peer.fib_index = fib_index;
1335 sep->fib_index = fib_index;
1339 sep->sw_if_index = app_ns->sw_if_index;
1343 if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
1344 && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
1345 && sep->peer.sw_if_index != app_ns->sw_if_index)
1346 clib_warning ("Local sw_if_index different from app ns sw_if_index");
1348 sep->peer.sw_if_index = app_ns->sw_if_index;
1353 vnet_listen (vnet_listen_args_t *a)
1355 app_listener_t *app_listener;
1356 app_worker_t *app_wrk;
1360 ASSERT (vlib_thread_is_main_w_barrier ());
1362 app = application_get_if_valid (a->app_index);
1364 return SESSION_E_NOAPP;
1366 app_wrk = application_get_worker (app, a->wrk_map_index);
1368 return SESSION_E_INVALID_APPWRK;
1370 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
1372 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
1373 if (!session_endpoint_in_ns (&a->sep_ext))
1374 return SESSION_E_INVALID_NS;
1377 * Check if we already have an app listener
1379 app_listener = app_listener_lookup (app, &a->sep_ext);
1382 if (app_listener->app_index != app->app_index)
1383 return SESSION_E_ALREADY_LISTENING;
1384 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1386 a->handle = app_listener_handle (app_listener);
1391 * Create new app listener
1393 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1396 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1398 app_listener_cleanup (app_listener);
1402 a->handle = app_listener_handle (app_listener);
1407 vnet_connect (vnet_connect_args_t *a)
1409 app_worker_t *client_wrk;
1410 application_t *client;
1412 ASSERT (session_vlib_thread_is_cl_thread ());
1414 if (session_endpoint_is_zero (&a->sep))
1415 return SESSION_E_INVALID_RMT_IP;
1417 client = application_get (a->app_index);
1418 session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1419 client_wrk = application_get_worker (client, a->wrk_map_index);
1421 a->sep_ext.opaque = a->api_context;
1424 * First check the local scope for locally attached destinations.
1425 * If we have local scope, we pass *all* connects through it since we may
1426 * have special policy rules even for non-local destinations, think proxy.
1428 if (application_has_local_scope (client))
1432 a->sep_ext.original_tp = a->sep_ext.transport_proto;
1433 a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE;
1434 rv = app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh);
1435 a->sep_ext.transport_proto = a->sep_ext.original_tp;
1436 if (!rv || rv != SESSION_E_LOCAL_CONNECT)
1440 * Not connecting to a local server, propagate to transport
1442 return app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh);
1446 vnet_unlisten (vnet_unlisten_args_t *a)
1448 app_worker_t *app_wrk;
1452 ASSERT (vlib_thread_is_main_w_barrier ());
1454 if (!(app = application_get_if_valid (a->app_index)))
1455 return SESSION_E_NOAPP;
1457 if (!(al = app_listener_get_w_handle (a->handle)))
1458 return SESSION_E_NOLISTEN;
1460 if (al->app_index != app->app_index)
1462 clib_warning ("app doesn't own handle %llu!", a->handle);
1463 return SESSION_E_OWNER;
1466 app_wrk = application_get_worker (app, a->wrk_map_index);
1469 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
1470 return SESSION_E_INVALID_APPWRK;
1473 return app_worker_stop_listen (app_wrk, al);
1477 vnet_shutdown_session (vnet_shutdown_args_t *a)
1479 app_worker_t *app_wrk;
1482 s = session_get_from_handle_if_valid (a->handle);
1484 return SESSION_E_NOSESSION;
1486 app_wrk = app_worker_get (s->app_wrk_index);
1487 if (app_wrk->app_index != a->app_index)
1488 return SESSION_E_OWNER;
1490 /* We're peeking into another's thread pool. Make sure */
1491 ASSERT (s->session_index == session_index_from_handle (a->handle));
1493 session_half_close (s);
1498 vnet_disconnect_session (vnet_disconnect_args_t *a)
1500 app_worker_t *app_wrk;
1503 s = session_get_from_handle_if_valid (a->handle);
1505 return SESSION_E_NOSESSION;
1507 app_wrk = app_worker_get (s->app_wrk_index);
1508 if (app_wrk->app_index != a->app_index)
1509 return SESSION_E_OWNER;
1511 /* We're peeking into another's thread pool. Make sure */
1512 ASSERT (s->session_index == session_index_from_handle (a->handle));
1519 application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1521 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1522 app_listener_t *app_listener;
1527 return SESSION_E_INVALID_APPWRK;
1529 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1530 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1532 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1534 app = application_get (old_wrk->app_index);
1536 return SESSION_E_NOAPP;
1538 app_listener = app_listener_get (s->al_index);
1540 /* Only remove from lb for now */
1541 app_listener->workers = clib_bitmap_set (app_listener->workers,
1542 old_wrk->wrk_map_index, 0);
1544 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1547 s->app_wrk_index = app_wrk->wrk_index;
1553 application_is_proxy (application_t * app)
1555 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1559 application_is_builtin (application_t * app)
1561 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1565 application_is_builtin_proxy (application_t * app)
1567 return (application_is_proxy (app) && application_is_builtin (app));
1571 application_has_local_scope (application_t * app)
1573 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1577 application_has_global_scope (application_t * app)
1579 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1583 application_original_dst_is_enabled (application_t *app)
1585 return app->flags & APP_OPTIONS_FLAGS_GET_ORIGINAL_DST;
1588 static clib_error_t *
1589 application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1590 u8 transport_proto, u8 is_start)
1592 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1593 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
1594 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
1595 transport_connection_t *tc;
1596 app_worker_t *app_wrk;
1601 /* TODO decide if we want proxy to be enabled for all workers */
1602 app_wrk = application_get_default_worker (app);
1605 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
1608 sep.is_ip4 = is_ip4;
1609 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1610 sep.sw_if_index = app_ns->sw_if_index;
1611 sep.transport_proto = transport_proto;
1612 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1614 /* force global scope listener */
1616 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1617 app_listener_alloc_and_init (app, &sep, &al);
1620 app_worker_start_listen (app_wrk, al);
1621 s = listen_session_get (al->session_index);
1622 s->flags |= SESSION_F_PROXY;
1627 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
1631 tc = listen_session_get_transport (s);
1633 if (!ip_is_zero (&tc->lcl_ip, 1))
1636 sep.is_ip4 = is_ip4;
1637 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1638 sep.transport_proto = transport_proto;
1640 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
1642 session_lookup_add_session_endpoint (sti,
1643 (session_endpoint_t *) & sep,
1646 session_lookup_del_session_endpoint (sti,
1647 (session_endpoint_t *) & sep);
1654 application_start_stop_proxy_local_scope (application_t * app,
1655 u8 transport_proto, u8 is_start)
1657 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1658 app_namespace_t *app_ns;
1659 app_ns = app_namespace_get (app->ns_index);
1661 sep.transport_proto = transport_proto;
1666 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1669 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
1674 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1676 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1681 application_start_stop_proxy (application_t * app,
1682 transport_proto_t transport_proto, u8 is_start)
1684 if (application_has_local_scope (app))
1685 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
1687 if (application_has_global_scope (app))
1689 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1690 transport_proto, is_start);
1691 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1692 transport_proto, is_start);
1697 application_setup_proxy (application_t * app)
1699 u16 transports = app->proxied_transports;
1700 transport_proto_t tp;
1702 ASSERT (application_is_proxy (app));
1704 transport_proto_foreach (tp, transports)
1705 application_start_stop_proxy (app, tp, 1);
1709 application_remove_proxy (application_t * app)
1711 u16 transports = app->proxied_transports;
1712 transport_proto_t tp;
1714 ASSERT (application_is_proxy (app));
1716 transport_proto_foreach (tp, transports)
1717 application_start_stop_proxy (app, tp, 0);
1720 segment_manager_props_t *
1721 application_segment_manager_properties (application_t * app)
1723 return &app->sm_properties;
1726 segment_manager_props_t *
1727 application_get_segment_manager_properties (u32 app_index)
1729 application_t *app = application_get (app_index);
1730 return &app->sm_properties;
1734 application_format_listeners (application_t * app, int verbose)
1736 vlib_main_t *vm = vlib_get_main ();
1737 app_worker_map_t *wrk_map;
1738 app_worker_t *app_wrk;
1744 vlib_cli_output (vm, "%U", format_app_worker_listener, NULL /* header */,
1749 pool_foreach (wrk_map, app->worker_maps) {
1750 app_wrk = app_worker_get (wrk_map->wrk_index);
1751 if (hash_elts (app_wrk->listeners_table) == 0)
1753 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1754 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1755 handle, sm_index, verbose);
1761 application_format_connects (application_t * app, int verbose)
1763 app_worker_map_t *wrk_map;
1764 app_worker_t *app_wrk;
1768 app_worker_format_connects (0, verbose);
1772 pool_foreach (wrk_map, app->worker_maps) {
1773 app_wrk = app_worker_get (wrk_map->wrk_index);
1774 app_worker_format_connects (app_wrk, verbose);
1779 format_cert_key_pair (u8 * s, va_list * args)
1781 app_cert_key_pair_t *ckpair = va_arg (*args, app_cert_key_pair_t *);
1782 int key_len = 0, cert_len = 0;
1783 cert_len = vec_len (ckpair->cert);
1784 key_len = vec_len (ckpair->key);
1785 if (ckpair->cert_key_index == 0)
1786 s = format (s, "DEFAULT (cert:%d, key:%d)", cert_len, key_len);
1788 s = format (s, "%d (cert:%d, key:%d)", ckpair->cert_key_index,
1794 format_crypto_engine (u8 * s, va_list * args)
1796 u32 engine = va_arg (*args, u32);
1799 case CRYPTO_ENGINE_NONE:
1800 return format (s, "none");
1801 case CRYPTO_ENGINE_MBEDTLS:
1802 return format (s, "mbedtls");
1803 case CRYPTO_ENGINE_OPENSSL:
1804 return format (s, "openssl");
1805 case CRYPTO_ENGINE_PICOTLS:
1806 return format (s, "picotls");
1807 case CRYPTO_ENGINE_VPP:
1808 return format (s, "vpp");
1810 return format (s, "unknown engine");
1816 unformat_crypto_engine (unformat_input_t * input, va_list * args)
1818 u8 *a = va_arg (*args, u8 *);
1819 if (unformat (input, "mbedtls"))
1820 *a = CRYPTO_ENGINE_MBEDTLS;
1821 else if (unformat (input, "openssl"))
1822 *a = CRYPTO_ENGINE_OPENSSL;
1823 else if (unformat (input, "picotls"))
1824 *a = CRYPTO_ENGINE_PICOTLS;
1825 else if (unformat (input, "vpp"))
1826 *a = CRYPTO_ENGINE_VPP;
1833 format_crypto_context (u8 * s, va_list * args)
1835 crypto_context_t *crctx = va_arg (*args, crypto_context_t *);
1836 s = format (s, "[0x%x][sub%d,ckpair%x]", crctx->ctx_index,
1837 crctx->n_subscribers, crctx->ckpair_index);
1838 s = format (s, "[%U]", format_crypto_engine, crctx->crypto_engine);
1843 format_application (u8 * s, va_list * args)
1845 application_t *app = va_arg (*args, application_t *);
1846 CLIB_UNUSED (int verbose) = va_arg (*args, int);
1847 segment_manager_props_t *props;
1848 const u8 *app_ns_name, *app_name;
1849 app_worker_map_t *wrk_map;
1850 app_worker_t *app_wrk;
1855 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
1859 app_name = app_get_name (app);
1860 app_ns_name = app_namespace_id_from_index (app->ns_index);
1861 props = application_segment_manager_properties (app);
1864 s = format (s, "%-10u%-20v%-40v", app->app_index, app_name,
1869 s = format (s, "app-name %v app-index %u ns-index %u seg-size %U\n",
1870 app_name, app->app_index, app->ns_index,
1871 format_memory_size, props->add_segment_size);
1872 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1873 format_memory_size, props->rx_fifo_size,
1874 format_memory_size, props->tx_fifo_size);
1876 pool_foreach (wrk_map, app->worker_maps) {
1877 app_wrk = app_worker_get (wrk_map->wrk_index);
1878 s = format (s, "%U", format_app_worker, app_wrk);
1885 application_format_all_listeners (vlib_main_t * vm, int verbose)
1889 if (!pool_elts (app_main.app_pool))
1891 vlib_cli_output (vm, "No active server bindings");
1895 application_format_listeners (0, verbose);
1897 pool_foreach (app, app_main.app_pool) {
1898 application_format_listeners (app, verbose);
1903 application_format_all_clients (vlib_main_t * vm, int verbose)
1907 if (!pool_elts (app_main.app_pool))
1909 vlib_cli_output (vm, "No active apps");
1913 application_format_connects (0, verbose);
1915 pool_foreach (app, app_main.app_pool) {
1916 application_format_connects (app, verbose);
1920 static clib_error_t *
1921 show_certificate_command_fn (vlib_main_t * vm, unformat_input_t * input,
1922 vlib_cli_command_t * cmd)
1924 app_cert_key_pair_t *ckpair;
1925 session_cli_return_if_not_enabled ();
1927 pool_foreach (ckpair, app_main.cert_key_pair_store) {
1928 vlib_cli_output (vm, "%U", format_cert_key_pair, ckpair);
1934 appliction_format_app_mq (vlib_main_t * vm, application_t * app)
1936 app_worker_map_t *map;
1940 pool_foreach (map, app->worker_maps) {
1941 wrk = app_worker_get (map->wrk_index);
1942 vlib_cli_output (vm, "[A%d][%d]%U", app->app_index,
1943 map->wrk_index, format_svm_msg_q,
1947 for (i = 0; i < vec_len (app->rx_mqs); i++)
1948 vlib_cli_output (vm, "[A%d][R%d]%U", app->app_index, i, format_svm_msg_q,
1952 static clib_error_t *
1953 appliction_format_all_app_mq (vlib_main_t * vm)
1958 n_threads = vlib_get_n_threads ();
1960 for (i = 0; i < n_threads; i++)
1962 vlib_cli_output (vm, "[Ctrl%d]%U", i, format_svm_msg_q,
1963 session_main_get_vpp_event_queue (i));
1966 pool_foreach (app, app_main.app_pool) {
1967 appliction_format_app_mq (vm, app);
1972 static clib_error_t *
1973 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1974 vlib_cli_command_t * cmd)
1976 int do_server = 0, do_client = 0, do_mq = 0, do_transports = 0;
1982 session_cli_return_if_not_enabled ();
1984 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1986 if (unformat (input, "server"))
1988 else if (unformat (input, "client"))
1990 else if (unformat (input, "transports"))
1992 else if (unformat (input, "mq"))
1994 else if (unformat (input, "%u", &app_index))
1996 else if (unformat (input, "verbose"))
1999 return clib_error_return (0, "unknown input `%U'",
2000 format_unformat_error, input);
2003 if (do_mq && app_index != ~0)
2005 app = application_get_if_valid (app_index);
2007 return clib_error_return (0, "No app with index %u", app_index);
2009 appliction_format_app_mq (vm, app);
2015 appliction_format_all_app_mq (vm);
2021 application_format_all_listeners (vm, verbose);
2027 application_format_all_clients (vm, verbose);
2031 if (app_index != ~0)
2033 app = application_get_if_valid (app_index);
2035 return clib_error_return (0, "No app with index %u", app_index);
2037 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
2041 /* Print app related info */
2042 if (!do_server && !do_client)
2044 vlib_cli_output (vm, "%U", format_application, 0, 0);
2045 pool_foreach (app, app_main.app_pool) {
2046 is_ta = app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2047 if ((!do_transports && !is_ta) || (do_transports && is_ta))
2048 vlib_cli_output (vm, "%U", format_application, app, 0);
2055 /* Certificate store */
2057 static app_cert_key_pair_t *
2058 app_cert_key_pair_alloc ()
2060 app_cert_key_pair_t *ckpair;
2061 pool_get (app_main.cert_key_pair_store, ckpair);
2062 clib_memset (ckpair, 0, sizeof (*ckpair));
2063 ckpair->cert_key_index = ckpair - app_main.cert_key_pair_store;
2067 app_cert_key_pair_t *
2068 app_cert_key_pair_get_if_valid (u32 index)
2070 if (pool_is_free_index (app_main.cert_key_pair_store, index))
2072 return app_cert_key_pair_get (index);
2075 app_cert_key_pair_t *
2076 app_cert_key_pair_get (u32 index)
2078 return pool_elt_at_index (app_main.cert_key_pair_store, index);
2081 app_cert_key_pair_t *
2082 app_cert_key_pair_get_default ()
2084 /* To maintain legacy bapi */
2085 return app_cert_key_pair_get (0);
2089 vnet_app_add_cert_key_pair (vnet_app_add_cert_key_pair_args_t * a)
2091 app_cert_key_pair_t *ckpair = app_cert_key_pair_alloc ();
2092 vec_validate (ckpair->cert, a->cert_len - 1);
2093 clib_memcpy_fast (ckpair->cert, a->cert, a->cert_len);
2094 vec_validate (ckpair->key, a->key_len - 1);
2095 clib_memcpy_fast (ckpair->key, a->key, a->key_len);
2096 a->index = ckpair->cert_key_index;
2101 vnet_app_add_cert_key_interest (u32 index, u32 app_index)
2103 app_cert_key_pair_t *ckpair;
2104 if (!(ckpair = app_cert_key_pair_get_if_valid (index)))
2106 if (vec_search (ckpair->app_interests, app_index) != ~0)
2107 vec_add1 (ckpair->app_interests, app_index);
2112 vnet_app_del_cert_key_pair (u32 index)
2114 app_cert_key_pair_t *ckpair;
2118 if (!(ckpair = app_cert_key_pair_get_if_valid (index)))
2119 return SESSION_E_INVALID;
2121 vec_foreach (app_index, ckpair->app_interests)
2123 if ((app = application_get_if_valid (*app_index))
2124 && app->cb_fns.app_cert_key_pair_delete_callback)
2125 app->cb_fns.app_cert_key_pair_delete_callback (ckpair);
2128 vec_free (ckpair->cert);
2129 vec_free (ckpair->key);
2130 pool_put (app_main.cert_key_pair_store, ckpair);
2135 application_init (vlib_main_t * vm)
2137 app_main_t *am = &app_main;
2140 n_workers = vlib_num_workers ();
2142 /* Index 0 was originally used by legacy apis, maintain as invalid */
2143 (void) app_cert_key_pair_alloc ();
2144 am->last_crypto_engine = CRYPTO_ENGINE_LAST;
2145 am->app_by_name = hash_create_vec (0, sizeof (u8), sizeof (uword));
2147 vec_validate (am->wrk, n_workers);
2152 VLIB_INIT_FUNCTION (application_init);
2154 VLIB_CLI_COMMAND (show_app_command, static) = {
2156 .short_help = "show app [index] [server|client] [mq] [verbose] "
2158 .function = show_app_command_fn,
2161 VLIB_CLI_COMMAND (show_certificate_command, static) = {
2162 .path = "show app certificate",
2163 .short_help = "list app certs and keys present in store",
2164 .function = show_certificate_command_fn,
2167 crypto_engine_type_t
2168 app_crypto_engine_type_add (void)
2170 return (++app_main.last_crypto_engine);
2174 app_crypto_engine_n_types (void)
2176 return (app_main.last_crypto_engine + 1);
2180 * fd.io coding-style-patch-verification: ON
2183 * eval: (c-set-style "gnu")