blob: c66548507e51b965a0cc7b21a21d490e5f9a0d5a [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/session/application.h>
Florin Coras6cf30ad2017-04-04 23:08:23 -070017#include <vnet/session/application_interface.h>
Florin Corascea194d2017-10-02 00:18:51 -070018#include <vnet/session/application_namespace.h>
Florin Corasba7d8f52019-02-22 13:11:38 -080019#include <vnet/session/application_local.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <vnet/session/session.h>
21
Florin Coras15531972018-08-12 23:50:53 -070022static app_main_t app_main;
Dave Barach68b0fb02017-02-28 15:15:56 -050023
Florin Corasc1a42652019-02-08 18:27:29 -080024#define app_interface_check_thread_and_barrier(_fn, _arg) \
25 if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \
26 { \
27 vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \
28 return 0; \
29 }
30
Florin Corasab2f6db2018-08-31 14:31:41 -070031static app_listener_t *
32app_listener_alloc (application_t * app)
33{
Florin Coras97fef282023-12-21 19:41:12 -080034 app_main_t *am = &app_main;
Florin Corasab2f6db2018-08-31 14:31:41 -070035 app_listener_t *app_listener;
Florin Coras97fef282023-12-21 19:41:12 -080036
37 pool_get (am->listeners, app_listener);
Dave Barachb7b92992018-10-17 10:38:51 -040038 clib_memset (app_listener, 0, sizeof (*app_listener));
Florin Coras97fef282023-12-21 19:41:12 -080039 app_listener->al_index = app_listener - am->listeners;
Florin Corasc9940fc2019-02-05 20:55:11 -080040 app_listener->app_index = app->app_index;
41 app_listener->session_index = SESSION_INVALID_INDEX;
42 app_listener->local_index = SESSION_INVALID_INDEX;
Florin Coras87d66332019-06-11 12:31:31 -070043 app_listener->ls_handle = SESSION_INVALID_HANDLE;
Florin Corasab2f6db2018-08-31 14:31:41 -070044 return app_listener;
45}
46
Florin Corasc9940fc2019-02-05 20:55:11 -080047app_listener_t *
Florin Coras97fef282023-12-21 19:41:12 -080048app_listener_get (u32 app_listener_index)
Florin Corasab2f6db2018-08-31 14:31:41 -070049{
Florin Coras97fef282023-12-21 19:41:12 -080050 app_main_t *am = &app_main;
51
52 return pool_elt_at_index (am->listeners, app_listener_index);
Florin Corasab2f6db2018-08-31 14:31:41 -070053}
54
55static void
56app_listener_free (application_t * app, app_listener_t * app_listener)
57{
Florin Coras97fef282023-12-21 19:41:12 -080058 app_main_t *am = &app_main;
59
Florin Corasab2f6db2018-08-31 14:31:41 -070060 clib_bitmap_free (app_listener->workers);
Florin Coras7428eaa2023-12-11 16:04:57 -080061 vec_free (app_listener->cl_listeners);
Florin Corasab2f6db2018-08-31 14:31:41 -070062 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040063 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
Florin Coras97fef282023-12-21 19:41:12 -080064 pool_put (am->listeners, app_listener);
Florin Corasab2f6db2018-08-31 14:31:41 -070065}
66
Florin Corasc9940fc2019-02-05 20:55:11 -080067session_handle_t
68app_listener_handle (app_listener_t * al)
69{
Florin Coras87d66332019-06-11 12:31:31 -070070 return al->ls_handle;
Florin Corasc9940fc2019-02-05 20:55:11 -080071}
72
Florin Coras87d66332019-06-11 12:31:31 -070073session_handle_t
74app_listen_session_handle (session_t * ls)
75{
76 app_listener_t *al;
Florin Coras97fef282023-12-21 19:41:12 -080077 /* TODO(fcoras): quic session handles */
78 if (ls->al_index == SESSION_INVALID_INDEX)
Florin Coras87d66332019-06-11 12:31:31 -070079 return listen_session_get_handle (ls);
Florin Coras97fef282023-12-21 19:41:12 -080080 al = app_listener_get (ls->al_index);
Florin Coras87d66332019-06-11 12:31:31 -070081 return al->ls_handle;
82}
83
Florin Corasc9940fc2019-02-05 20:55:11 -080084app_listener_t *
85app_listener_get_w_handle (session_handle_t handle)
86{
Florin Coras87d66332019-06-11 12:31:31 -070087 session_t *ls;
88 ls = session_get_from_handle_if_valid (handle);
89 if (!ls)
Florin Corasc9940fc2019-02-05 20:55:11 -080090 return 0;
Florin Coras97fef282023-12-21 19:41:12 -080091 return app_listener_get (ls->al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -080092}
93
94app_listener_t *
95app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
96{
97 u32 table_index, fib_proto;
98 session_endpoint_t *sep;
99 session_handle_t handle;
Florin Corasc9940fc2019-02-05 20:55:11 -0800100 session_t *ls;
liuyacan694046c2021-04-30 08:57:37 +0000101 void *iface_ip;
102 ip46_address_t original_ip;
Florin Corasc9940fc2019-02-05 20:55:11 -0800103
104 sep = (session_endpoint_t *) sep_ext;
105 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
106 {
107 table_index = application_local_session_table (app);
108 handle = session_lookup_endpoint_listener (table_index, sep, 1);
109 if (handle != SESSION_INVALID_HANDLE)
110 {
Florin Corasd4295e62019-02-22 13:11:38 -0800111 ls = listen_session_get_from_handle (handle);
Florin Coras97fef282023-12-21 19:41:12 -0800112 return app_listener_get (ls->al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800113 }
114 }
115
116 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800117 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800118 handle = session_lookup_endpoint_listener (table_index, sep, 1);
119 if (handle != SESSION_INVALID_HANDLE)
120 {
121 ls = listen_session_get_from_handle (handle);
Florin Coras97fef282023-12-21 19:41:12 -0800122 return app_listener_get (ls->al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800123 }
124
liuyacan694046c2021-04-30 08:57:37 +0000125 /*
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.
129 */
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)
133 {
134 if ((iface_ip = ip_interface_get_first_ip (sep_ext->sw_if_index,
135 sep_ext->is_ip4)))
136 {
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)
142 {
143 ls = listen_session_get_from_handle (handle);
Florin Coras97fef282023-12-21 19:41:12 -0800144 return app_listener_get (ls->al_index);
liuyacan694046c2021-04-30 08:57:37 +0000145 }
146 }
147 }
148
Florin Corasc9940fc2019-02-05 20:55:11 -0800149 return 0;
150}
151
152int
153app_listener_alloc_and_init (application_t * app,
154 session_endpoint_cfg_t * sep,
155 app_listener_t ** listener)
156{
157 app_listener_t *app_listener;
Florin Corasd4295e62019-02-22 13:11:38 -0800158 transport_connection_t *tc;
Florin Coras95cd8642019-12-30 21:53:19 -0800159 u32 al_index, table_index;
Florin Corasc9940fc2019-02-05 20:55:11 -0800160 session_handle_t lh;
161 session_type_t st;
162 session_t *ls = 0;
163 int rv;
164
165 app_listener = app_listener_alloc (app);
Florin Corasa27a46e2019-02-18 13:02:28 -0800166 al_index = app_listener->al_index;
Florin Corasc9940fc2019-02-05 20:55:11 -0800167 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
168
169 /*
170 * Add session endpoint to local session table. Only binds to "inaddr_any"
171 * (i.e., zero address) are added to local scope table.
172 */
173 if (application_has_local_scope (app)
174 && session_endpoint_is_local ((session_endpoint_t *) sep))
175 {
Florin Corasd4295e62019-02-22 13:11:38 -0800176 session_type_t local_st;
Florin Corasc9940fc2019-02-05 20:55:11 -0800177
Florin Corasd4295e62019-02-22 13:11:38 -0800178 local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
179 sep->is_ip4);
180 ls = listen_session_alloc (0, local_st);
Florin Corasd4295e62019-02-22 13:11:38 -0800181 ls->app_wrk_index = sep->app_wrk_index;
182 lh = session_handle (ls);
183
184 if ((rv = session_listen (ls, sep)))
185 {
186 ls = session_get_from_handle (lh);
187 session_free (ls);
Xiaoming Jiang1f704252023-04-19 08:41:29 +0000188 app_listener_free (app, app_listener);
Florin Corasd4295e62019-02-22 13:11:38 -0800189 return rv;
190 }
191
192 ls = session_get_from_handle (lh);
Florin Coras97fef282023-12-21 19:41:12 -0800193 app_listener = app_listener_get (al_index);
Florin Corasd4295e62019-02-22 13:11:38 -0800194 app_listener->local_index = ls->session_index;
Florin Coras87d66332019-06-11 12:31:31 -0700195 app_listener->ls_handle = lh;
Florin Corasd4295e62019-02-22 13:11:38 -0800196 ls->al_index = al_index;
197
Florin Corasc9940fc2019-02-05 20:55:11 -0800198 table_index = application_local_session_table (app);
Florin Corasc9940fc2019-02-05 20:55:11 -0800199 session_lookup_add_session_endpoint (table_index,
200 (session_endpoint_t *) sep, lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800201 }
202
203 if (application_has_global_scope (app))
204 {
205 /*
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.
210 */
Florin Corasba7d8f52019-02-22 13:11:38 -0800211 ls = listen_session_alloc (0, st);
Florin Corasc9940fc2019-02-05 20:55:11 -0800212 ls->app_wrk_index = sep->app_wrk_index;
213
214 /* Listen pool can be reallocated if the transport is
215 * recursive (tls) */
Florin Corasd4295e62019-02-22 13:11:38 -0800216 lh = listen_session_get_handle (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800217
218 if ((rv = session_listen (ls, sep)))
219 {
Florin Corasd4295e62019-02-22 13:11:38 -0800220 ls = listen_session_get_from_handle (lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800221 session_free (ls);
Xiaoming Jiang1f704252023-04-19 08:41:29 +0000222 app_listener_free (app, app_listener);
Florin Corasc9940fc2019-02-05 20:55:11 -0800223 return rv;
224 }
Florin Corasd4295e62019-02-22 13:11:38 -0800225 ls = listen_session_get_from_handle (lh);
Florin Coras97fef282023-12-21 19:41:12 -0800226 app_listener = app_listener_get (al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800227 app_listener->session_index = ls->session_index;
Florin Coras87d66332019-06-11 12:31:31 -0700228 app_listener->ls_handle = lh;
Florin Corasa27a46e2019-02-18 13:02:28 -0800229 ls->al_index = al_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800230
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
235 * connections */
236 tc = session_get_transport (ls);
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200237 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Florin Coras95cd8642019-12-30 21:53:19 -0800238 {
239 fib_protocol_t fib_proto;
240 fib_proto = session_endpoint_fib_proto ((session_endpoint_t *) sep);
Florin Corasa4d09562021-07-08 08:25:09 -0700241 /* 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);
Florin Coras95cd8642019-12-30 21:53:19 -0800244 session_lookup_add_session_endpoint (table_index,
245 (session_endpoint_t *) sep,
246 lh);
247 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800248 }
249
Florin Coras2b81e3c2019-02-27 07:55:46 -0800250 if (!ls)
Florin Corasc9940fc2019-02-05 20:55:11 -0800251 {
252 app_listener_free (app, app_listener);
253 return -1;
254 }
255
256 *listener = app_listener;
257 return 0;
258}
259
260void
261app_listener_cleanup (app_listener_t * al)
262{
263 application_t *app = application_get (al->app_index);
Florin Corasd4295e62019-02-22 13:11:38 -0800264 session_t *ls;
Florin Corasc9940fc2019-02-05 20:55:11 -0800265
266 if (al->session_index != SESSION_INVALID_INDEX)
267 {
Florin Corasd4295e62019-02-22 13:11:38 -0800268 ls = session_get (al->session_index, 0);
Florin Corasc9940fc2019-02-05 20:55:11 -0800269 session_stop_listen (ls);
Florin Corasba7d8f52019-02-22 13:11:38 -0800270 listen_session_free (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800271 }
272 if (al->local_index != SESSION_INVALID_INDEX)
273 {
274 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Corasc9940fc2019-02-05 20:55:11 -0800275 u32 table_index;
276
277 table_index = application_local_session_table (app);
Florin Corasd4295e62019-02-22 13:11:38 -0800278 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800279 ct_session_endpoint (ls, &sep);
Florin Corasc9940fc2019-02-05 20:55:11 -0800280 session_lookup_del_session_endpoint (table_index, &sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800281 session_stop_listen (ls);
Florin Corasd4295e62019-02-22 13:11:38 -0800282 listen_session_free (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800283 }
284 app_listener_free (app, al);
285}
286
Florin Coras11e2cf52019-03-06 12:04:24 -0800287static app_worker_t *
Florin Coras97fef282023-12-21 19:41:12 -0800288app_listener_select_worker (app_listener_t *al)
Florin Corasc9940fc2019-02-05 20:55:11 -0800289{
Florin Coras97fef282023-12-21 19:41:12 -0800290 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800291 u32 wrk_index;
292
293 app = application_get (al->app_index);
294 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
295 if (wrk_index == ~0)
296 wrk_index = clib_bitmap_first_set (al->workers);
297
298 ASSERT (wrk_index != ~0);
299 al->accept_rotor = wrk_index;
300 return application_get_worker (app, wrk_index);
301}
302
303session_t *
304app_listener_get_session (app_listener_t * al)
305{
306 if (al->session_index == SESSION_INVALID_INDEX)
307 return 0;
308
309 return listen_session_get (al->session_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700310}
311
Florin Corasd4295e62019-02-22 13:11:38 -0800312session_t *
313app_listener_get_local_session (app_listener_t * al)
314{
315 if (al->local_index == SESSION_INVALID_INDEX)
316 return 0;
317 return listen_session_get (al->local_index);
318}
319
Florin Coras7428eaa2023-12-11 16:04:57 -0800320session_t *
321app_listener_get_wrk_cl_session (app_listener_t *al, u32 wrk_map_index)
322{
323 u32 si = vec_elt (al->cl_listeners, wrk_map_index);
324 return session_get (si, 0 /* listener thread */);
325}
326
Florin Coras15531972018-08-12 23:50:53 -0700327static app_worker_map_t *
328app_worker_map_alloc (application_t * app)
329{
330 app_worker_map_t *map;
331 pool_get (app->worker_maps, map);
Dave Barachb7b92992018-10-17 10:38:51 -0400332 clib_memset (map, 0, sizeof (*map));
Florin Coras15531972018-08-12 23:50:53 -0700333 return map;
334}
Dave Barach68b0fb02017-02-28 15:15:56 -0500335
Florin Coras15531972018-08-12 23:50:53 -0700336static u32
337app_worker_map_index (application_t * app, app_worker_map_t * map)
338{
339 return (map - app->worker_maps);
340}
341
342static void
343app_worker_map_free (application_t * app, app_worker_map_t * map)
344{
345 pool_put (app->worker_maps, map);
346}
347
348static app_worker_map_t *
349app_worker_map_get (application_t * app, u32 map_index)
350{
Florin Coras01f3f892018-12-02 12:45:53 -0800351 if (pool_is_free_index (app->worker_maps, map_index))
352 return 0;
Florin Coras15531972018-08-12 23:50:53 -0700353 return pool_elt_at_index (app->worker_maps, map_index);
354}
Florin Coras0bee9ce2018-03-22 21:24:31 -0700355
Florin Coras053a0e42018-11-13 15:52:38 -0800356static const u8 *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700357app_get_name (application_t * app)
358{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700359 return app->name;
360}
361
Florin Corascea194d2017-10-02 00:18:51 -0700362u32
363application_session_table (application_t * app, u8 fib_proto)
364{
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);
372 else
373 return session_lookup_get_index_for_fib (fib_proto,
374 app_ns->ip6_fib_index);
375}
376
377u32
378application_local_session_table (application_t * app)
379{
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;
385}
386
Florin Corascea194d2017-10-02 00:18:51 -0700387/**
Florin Coras053a0e42018-11-13 15:52:38 -0800388 * Returns app name for app-index
Florin Corascea194d2017-10-02 00:18:51 -0700389 */
Florin Coras053a0e42018-11-13 15:52:38 -0800390const u8 *
Florin Corascea194d2017-10-02 00:18:51 -0700391application_name_from_index (u32 app_index)
392{
393 application_t *app = application_get (app_index);
394 if (!app)
395 return 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800396 return app_get_name (app);
397}
398
399static void
400application_api_table_add (u32 app_index, u32 api_client_index)
401{
Florin Corasc1f5a432018-11-20 11:31:26 -0800402 if (api_client_index != APP_INVALID_INDEX)
403 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
Florin Coras053a0e42018-11-13 15:52:38 -0800404}
405
406static void
407application_api_table_del (u32 api_client_index)
408{
409 hash_unset (app_main.app_by_api_client_index, api_client_index);
Florin Corascea194d2017-10-02 00:18:51 -0700410}
411
Dave Barach68b0fb02017-02-28 15:15:56 -0500412static void
Florin Corasc1f5a432018-11-20 11:31:26 -0800413application_name_table_add (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500414{
Florin Corasc1f5a432018-11-20 11:31:26 -0800415 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500416}
417
418static void
Florin Corasc1f5a432018-11-20 11:31:26 -0800419application_name_table_del (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500420{
Florin Corasc1f5a432018-11-20 11:31:26 -0800421 hash_unset_mem (app_main.app_by_name, app->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500422}
423
424application_t *
425application_lookup (u32 api_client_index)
426{
427 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700428 p = hash_get (app_main.app_by_api_client_index, api_client_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500429 if (p)
Florin Coras053a0e42018-11-13 15:52:38 -0800430 return application_get_if_valid (p[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500431
432 return 0;
433}
434
Florin Coras6cf30ad2017-04-04 23:08:23 -0700435application_t *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700436application_lookup_name (const u8 * name)
437{
438 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700439 p = hash_get_mem (app_main.app_by_name, name);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700440 if (p)
441 return application_get (p[0]);
442
443 return 0;
444}
445
Florin Coras41d5f542021-01-15 13:49:33 -0800446void
447appsl_pending_rx_mqs_add_tail (appsl_wrk_t *aw, app_rx_mq_elt_t *elt)
448{
449 app_rx_mq_elt_t *head;
450
451 if (!aw->pending_rx_mqs)
452 {
453 elt->next = elt->prev = elt;
454 aw->pending_rx_mqs = elt;
455 return;
456 }
457
458 head = aw->pending_rx_mqs;
459
460 ASSERT (head != elt);
461
462 elt->prev = head->prev;
463 elt->next = head;
464
465 head->prev->next = elt;
466 head->prev = elt;
467}
468
469void
470appsl_pending_rx_mqs_del (appsl_wrk_t *aw, app_rx_mq_elt_t *elt)
471{
472 if (elt->next == elt)
473 {
474 elt->next = elt->prev = 0;
475 aw->pending_rx_mqs = 0;
476 return;
477 }
478
479 if (elt == aw->pending_rx_mqs)
480 aw->pending_rx_mqs = elt->next;
481
482 elt->next->prev = elt->prev;
483 elt->prev->next = elt->next;
484 elt->next = elt->prev = 0;
485}
486
487vlib_node_registration_t appsl_rx_mqs_input_node;
488
489VLIB_NODE_FN (appsl_rx_mqs_input_node)
490(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
491{
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;
497 appsl_wrk_t *aw;
498 u64 buf;
499
500 aw = &am->wrk[thread_index];
501 elt = aw->pending_rx_mqs;
502 if (!elt)
503 return 0;
504
505 wrk = session_main_get_worker (thread_index);
506
507 do
508 {
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);
512
513 next = elt->next;
514 appsl_pending_rx_mqs_del (aw, elt);
515 if (!svm_msg_q_is_empty (elt->mq))
516 {
517 elt->flags |= APP_RX_MQ_F_POSTPONED;
518 appsl_pending_rx_mqs_add_tail (aw, elt);
519 }
520 else
521 {
522 elt->flags = 0;
523 }
524 elt = next;
525 }
526 while (aw->pending_rx_mqs && elt != aw->pending_rx_mqs);
527
528 if (aw->pending_rx_mqs)
529 vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index);
530
Florin Coras7da88292021-03-18 15:04:34 -0700531 if (n_msgs && wrk->state == SESSION_WRK_INTERRUPT)
532 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
533
Florin Coras41d5f542021-01-15 13:49:33 -0800534 return n_msgs;
535}
536
537VLIB_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,
541};
542
543static clib_error_t *
544app_rx_mq_fd_read_ready (clib_file_t *cf)
545{
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;
550 application_t *app;
551 appsl_wrk_t *aw;
552
553 ASSERT (vlib_get_thread_index () == handle->thread_index);
554 app = application_get_if_valid (handle->app_index);
555 if (!app)
556 return 0;
557
558 mqe = &app->rx_mqs[handle->thread_index];
559 if ((mqe->flags & APP_RX_MQ_F_PENDING) || svm_msg_q_is_empty (mqe->mq))
560 return 0;
561
562 aw = &am->wrk[handle->thread_index];
563 appsl_pending_rx_mqs_add_tail (aw, mqe);
564 mqe->flags |= APP_RX_MQ_F_PENDING;
565
566 vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index);
567
568 return 0;
569}
570
571static clib_error_t *
572app_rx_mq_fd_write_ready (clib_file_t *cf)
573{
574 clib_warning ("should not be called");
575 return 0;
576}
577
578static void
579app_rx_mqs_epoll_add (application_t *app, app_rx_mq_elt_t *mqe)
580{
581 clib_file_t template = { 0 };
582 app_rx_mq_handle_t handle;
583 u32 thread_index;
584 int fd;
585
586 thread_index = mqe - app->rx_mqs;
587 fd = svm_msg_q_get_eventfd (mqe->mq);
588
589 handle.app_index = app->app_index;
590 handle.thread_index = thread_index;
591
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);
600}
601
602static void
603app_rx_mqs_epoll_del (application_t *app, app_rx_mq_elt_t *mqe)
604{
605 u32 thread_index = mqe - app->rx_mqs;
606 app_main_t *am = &app_main;
607 appsl_wrk_t *aw;
608
609 aw = &am->wrk[thread_index];
610
Florin Coras87d81ae2021-03-31 21:05:24 -0700611 session_wrk_handle_mq (session_main_get_worker (thread_index), mqe->mq);
612
Florin Coras41d5f542021-01-15 13:49:33 -0800613 if (mqe->flags & APP_RX_MQ_F_PENDING)
Florin Coras87d81ae2021-03-31 21:05:24 -0700614 appsl_pending_rx_mqs_del (aw, mqe);
Florin Coras41d5f542021-01-15 13:49:33 -0800615
616 clib_file_del_by_index (&file_main, mqe->file_index);
617}
618
619svm_msg_q_t *
620application_rx_mq_get (application_t *app, u32 mq_index)
621{
622 if (!app->rx_mqs)
623 return 0;
624
625 return app->rx_mqs[mq_index].mq;
626}
627
628static int
629app_rx_mqs_alloc (application_t *app)
630{
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;
635 int i;
636
637 props = application_segment_manager_properties (app);
638 evt_q_length = clib_max (props->evt_q_size, 128);
639
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 }
643 };
644 cfg->consumer_pid = 0;
645 cfg->n_rings = 2;
646 cfg->q_nitems = evt_q_length;
647 cfg->ring_cfgs = rc;
648
Florin Corasa54b62d2021-04-21 09:05:56 -0700649 eqs->ssvm.ssvm_size = svm_msg_q_size_to_alloc (cfg) * n_mqs + (1 << 20);
Xiaoming Jiang3d3dc292021-10-13 03:11:40 +0000650 eqs->ssvm.name = format (0, "%v-rx-mqs-seg%c", app->name, 0);
Florin Coras41d5f542021-01-15 13:49:33 -0800651
652 if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
653 {
654 clib_warning ("failed to initialize queue segment");
655 return SESSION_E_SEG_CREATE;
656 }
657
658 fifo_segment_init (eqs);
659
660 /* Fifo segment filled only with mqs */
661 eqs->h->n_mqs = n_mqs;
662 vec_validate (app->rx_mqs, n_mqs - 1);
663
664 for (i = 0; i < n_mqs; i++)
665 {
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))
668 {
669 clib_warning ("eventfd returned");
670 fifo_segment_cleanup (eqs);
671 ssvm_delete (&eqs->ssvm);
672 return SESSION_E_EVENTFD_ALLOC;
673 }
674 app_rx_mqs_epoll_add (app, &app->rx_mqs[i]);
675 app->rx_mqs[i].app_index = app->app_index;
676 }
677
678 return 0;
679}
680
681u8
682application_use_private_rx_mqs (void)
683{
684 return session_main.use_private_rx_mqs;
685}
686
687fifo_segment_t *
688application_get_rx_mqs_segment (application_t *app)
689{
690 if (application_use_private_rx_mqs ())
691 return &app->rx_mqs_segment;
Florin Coras8afe1b82021-11-17 23:38:54 -0800692 return session_main_get_wrk_mqs_segment ();
Florin Coras41d5f542021-01-15 13:49:33 -0800693}
694
695void
696application_enable_rx_mqs_nodes (u8 is_en)
697{
698 u8 state = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
699
700 foreach_vlib_main ()
701 vlib_node_set_state (this_vlib_main, appsl_rx_mqs_input_node.index, state);
702}
703
Florin Corasc1a42652019-02-08 18:27:29 -0800704static application_t *
Florin Coras15531972018-08-12 23:50:53 -0700705application_alloc (void)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700706{
707 application_t *app;
Florin Coras15531972018-08-12 23:50:53 -0700708 pool_get (app_main.app_pool, app);
Dave Barachb7b92992018-10-17 10:38:51 -0400709 clib_memset (app, 0, sizeof (*app));
Florin Coras15531972018-08-12 23:50:53 -0700710 app->app_index = app - app_main.app_pool;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700711 return app;
712}
713
Florin Coras15531972018-08-12 23:50:53 -0700714application_t *
715application_get (u32 app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500716{
Florin Coras15531972018-08-12 23:50:53 -0700717 if (app_index == APP_INVALID_INDEX)
718 return 0;
719 return pool_elt_at_index (app_main.app_pool, app_index);
720}
Dave Barach68b0fb02017-02-28 15:15:56 -0500721
Florin Coras15531972018-08-12 23:50:53 -0700722application_t *
723application_get_if_valid (u32 app_index)
724{
725 if (pool_is_free_index (app_main.app_pool, app_index))
726 return 0;
Florin Corasa5464812017-04-19 13:00:05 -0700727
Florin Coras15531972018-08-12 23:50:53 -0700728 return pool_elt_at_index (app_main.app_pool, app_index);
729}
Florin Coras7999e832017-10-31 01:51:04 -0700730
Florin Coras0242d302022-12-22 15:03:44 -0800731static int
732_null_app_tx_callback (session_t *s)
733{
734 return 0;
735}
736
Florin Corasd79b41e2017-03-04 05:37:52 -0800737static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700738application_verify_cb_fns (session_cb_vft_t * cb_fns)
Florin Corasd79b41e2017-03-04 05:37:52 -0800739{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700740 if (cb_fns->session_accept_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800741 clib_warning ("No accept callback function provided");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700742 if (cb_fns->session_connected_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800743 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");
Florin Coras0242d302022-12-22 15:03:44 -0800748 if (!cb_fns->builtin_app_tx_callback)
749 cb_fns->builtin_app_tx_callback = _null_app_tx_callback;
Florin Corasd79b41e2017-03-04 05:37:52 -0800750}
751
Florin Corasb384b542018-01-15 01:08:33 -0800752/**
753 * Check app config for given segment type
754 *
755 * Returns 1 on success and 0 otherwise
756 */
757static u8
758application_verify_cfg (ssvm_segment_type_t st)
759{
760 u8 is_valid;
761 if (st == SSVM_SEGMENT_MEMFD)
762 {
Florin Coras8afe1b82021-11-17 23:38:54 -0800763 is_valid = (session_main_get_wrk_mqs_segment () != 0);
Florin Corasb384b542018-01-15 01:08:33 -0800764 if (!is_valid)
765 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
766 return is_valid;
767 }
768 else if (st == SSVM_SEGMENT_SHM)
769 {
Florin Coras8afe1b82021-11-17 23:38:54 -0800770 is_valid = (session_main_get_wrk_mqs_segment () == 0);
Florin Corasb384b542018-01-15 01:08:33 -0800771 if (!is_valid)
772 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
773 return is_valid;
774 }
775 else
776 return 1;
777}
778
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200779static session_error_t
780application_alloc_and_init (app_init_args_t *a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500781{
Florin Corasa332c462018-01-31 06:52:17 -0800782 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
Florin Coras88001c62019-04-24 14:44:46 -0700783 segment_manager_props_t *props;
Florin Coras15531972018-08-12 23:50:53 -0700784 application_t *app;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700785 u64 *opts;
Dave Barach68b0fb02017-02-28 15:15:56 -0500786
Florin Coras15531972018-08-12 23:50:53 -0700787 app = application_alloc ();
Florin Corasbbc8fae2021-07-19 15:23:51 -0700788 opts = a->options;
Florin Corasb384b542018-01-15 01:08:33 -0800789 /*
790 * Make sure we support the requested configuration
791 */
Florin Corasbbc8fae2021-07-19 15:23:51 -0700792 if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN) &&
793 !(opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_MEMFD_FOR_BUILTIN))
Florin Coras6c10ab22020-11-08 16:50:39 -0800794 seg_type = SSVM_SEGMENT_PRIVATE;
Florin Corasb384b542018-01-15 01:08:33 -0800795
Florin Corasbbc8fae2021-07-19 15:23:51 -0700796 if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) &&
797 seg_type != SSVM_SEGMENT_MEMFD)
Florin Corasd8402ae2019-03-03 16:46:52 -0800798 {
799 clib_warning ("mq eventfds can only be used if socket transport is "
800 "used for binary api");
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200801 return SESSION_E_NOSUPPORT;
Florin Corasd8402ae2019-03-03 16:46:52 -0800802 }
803
Florin Corasa332c462018-01-31 06:52:17 -0800804 if (!application_verify_cfg (seg_type))
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200805 return SESSION_E_NOSUPPORT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500806
Florin Corasbbc8fae2021-07-19 15:23:51 -0700807 if (opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS] &&
808 opts[APP_OPTIONS_PREALLOC_FIFO_HDRS])
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200809 return SESSION_E_NOSUPPORT;
Florin Coras9845c202020-04-28 01:54:22 +0000810
Florin Coras15531972018-08-12 23:50:53 -0700811 /* Check that the obvious things are properly set up */
812 application_verify_cb_fns (a->session_cb_vft);
813
Florin Corasbbc8fae2021-07-19 15:23:51 -0700814 app->flags = opts[APP_OPTIONS_FLAGS];
Florin Coras15531972018-08-12 23:50:53 -0700815 app->cb_fns = *a->session_cb_vft;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700816 app->ns_index = opts[APP_OPTIONS_NAMESPACE];
817 app->proxied_transports = opts[APP_OPTIONS_PROXY_TRANSPORT];
Florin Coras15531972018-08-12 23:50:53 -0700818 app->name = vec_dup (a->name);
819
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;
824
Florin Corasa332c462018-01-31 06:52:17 -0800825 props = application_segment_manager_properties (app);
Florin Coras88001c62019-04-24 14:44:46 -0700826 segment_manager_props_init (props);
Florin Corasbbc8fae2021-07-19 15:23:51 -0700827 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])
Florin Corasb384b542018-01-15 01:08:33 -0800831 {
Florin Corasbbc8fae2021-07-19 15:23:51 -0700832 props->add_segment_size = opts[APP_OPTIONS_ADD_SEGMENT_SIZE];
Florin Corasb384b542018-01-15 01:08:33 -0800833 props->add_segment = 1;
834 }
Junfeng Wangc795b882022-08-12 16:24:46 +0800835 if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_HUGE_PAGE)
836 props->huge_page = 1;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700837 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)
Florin Coras99368312018-08-02 10:45:44 -0700844 props->use_mq_eventfd = 1;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700845 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];
Florin Corasa332c462018-01-31 06:52:17 -0800855 props->segment_type = seg_type;
Dave Barach68b0fb02017-02-28 15:15:56 -0500856
Florin Coras15531972018-08-12 23:50:53 -0700857 /* Add app to lookup by api_client_index table */
Florin Corasc1f5a432018-11-20 11:31:26 -0800858 if (!application_is_builtin (app))
859 application_api_table_add (app->app_index, a->api_client_index);
860 else
861 application_name_table_add (app);
862
863 a->app_index = app->app_index;
Florin Corasa332c462018-01-31 06:52:17 -0800864
Florin Coras15531972018-08-12 23:50:53 -0700865 APP_DBG ("New app name: %v api index: %u index %u", app->name,
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200866 a->api_client_index, app->app_index);
Florin Coras15531972018-08-12 23:50:53 -0700867
868 return 0;
869}
870
Florin Corasc1a42652019-02-08 18:27:29 -0800871static void
Florin Coras15531972018-08-12 23:50:53 -0700872application_free (application_t * app)
873{
874 app_worker_map_t *wrk_map;
875 app_worker_t *app_wrk;
876
877 /*
878 * The app event queue allocated in first segment is cleared with
879 * the segment manager. No need to explicitly free it.
880 */
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200881 APP_DBG ("Delete app name %v index: %d", app->name, app->app_index);
Florin Coras15531972018-08-12 23:50:53 -0700882
883 if (application_is_proxy (app))
884 application_remove_proxy (app);
885
Florin Corasab2f6db2018-08-31 14:31:41 -0700886 /*
887 * Free workers
888 */
889
Florin Coras15531972018-08-12 23:50:53 -0700890 pool_flush (wrk_map, app->worker_maps, ({
891 app_wrk = app_worker_get (wrk_map->wrk_index);
892 app_worker_free (app_wrk);
893 }));
Florin Coras15531972018-08-12 23:50:53 -0700894 pool_free (app->worker_maps);
895
Florin Corasab2f6db2018-08-31 14:31:41 -0700896 /*
Florin Coras41d5f542021-01-15 13:49:33 -0800897 * Free rx mqs if allocated
898 */
899 if (app->rx_mqs)
900 {
901 int i;
902 for (i = 0; i < vec_len (app->rx_mqs); i++)
903 app_rx_mqs_epoll_del (app, &app->rx_mqs[i]);
904
905 fifo_segment_cleanup (&app->rx_mqs_segment);
906 ssvm_delete (&app->rx_mqs_segment.ssvm);
907 vec_free (app->rx_mqs);
908 }
909
910 /*
Florin Corasab2f6db2018-08-31 14:31:41 -0700911 * Cleanup remaining state
912 */
Florin Corasc1f5a432018-11-20 11:31:26 -0800913 if (application_is_builtin (app))
914 application_name_table_del (app);
Florin Coras15531972018-08-12 23:50:53 -0700915 vec_free (app->name);
Florin Coras15531972018-08-12 23:50:53 -0700916 pool_put (app_main.app_pool, app);
917}
918
Florin Corasc1a42652019-02-08 18:27:29 -0800919static void
Florin Coras053a0e42018-11-13 15:52:38 -0800920application_detach_process (application_t * app, u32 api_client_index)
921{
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;
926
927 if (api_client_index == ~0)
928 {
929 application_free (app);
930 return;
931 }
932
933 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200934 app->app_index, api_client_index);
Florin Coras053a0e42018-11-13 15:52:38 -0800935
Damjan Marionb2c31b62020-12-13 21:47:40 +0100936 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras053a0e42018-11-13 15:52:38 -0800937 app_wrk = app_worker_get (wrk_map->wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800938 if (app_wrk->api_client_index == api_client_index)
Florin Coras053a0e42018-11-13 15:52:38 -0800939 vec_add1 (wrks, app_wrk->wrk_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100940 }
Florin Coras053a0e42018-11-13 15:52:38 -0800941
942 if (!vec_len (wrks))
943 {
944 clib_warning ("no workers for app %u api_index %u", app->app_index,
945 api_client_index);
946 return;
947 }
948
949 args->app_index = app->app_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800950 args->api_client_index = api_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800951 vec_foreach (wrk_index, wrks)
952 {
953 app_wrk = app_worker_get (wrk_index[0]);
Florin Coras349f8ca2018-11-20 16:52:49 -0800954 args->wrk_map_index = app_wrk->wrk_map_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800955 args->is_add = 0;
956 vnet_app_worker_add_del (args);
957 }
958 vec_free (wrks);
959}
960
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200961void
962application_namespace_cleanup (app_namespace_t *app_ns)
963{
964 u32 *app_indices = 0, *app_index;
965 application_t *app;
966 u32 ns_index;
967
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);
972
973 vec_foreach (app_index, app_indices)
974 {
975 app = application_get (*app_index);
976
977 if (application_is_proxy (app))
978 application_remove_proxy (app);
979 app->flags &= ~APP_OPTIONS_FLAGS_IS_PROXY;
980
981 application_free (app);
982 }
983 vec_free (app_indices);
984}
985
Florin Coras15531972018-08-12 23:50:53 -0700986app_worker_t *
987application_get_worker (application_t * app, u32 wrk_map_index)
988{
989 app_worker_map_t *map;
990 map = app_worker_map_get (app, wrk_map_index);
991 if (!map)
992 return 0;
993 return app_worker_get (map->wrk_index);
994}
995
996app_worker_t *
997application_get_default_worker (application_t * app)
998{
999 return application_get_worker (app, 0);
1000}
1001
Florin Coras053a0e42018-11-13 15:52:38 -08001002u32
1003application_n_workers (application_t * app)
1004{
1005 return pool_elts (app->worker_maps);
1006}
1007
Florin Coras15531972018-08-12 23:50:53 -07001008app_worker_t *
Florin Corasc9940fc2019-02-05 20:55:11 -08001009application_listener_select_worker (session_t * ls)
Florin Corasab2f6db2018-08-31 14:31:41 -07001010{
Florin Corasc9940fc2019-02-05 20:55:11 -08001011 app_listener_t *al;
Florin Corasab2f6db2018-08-31 14:31:41 -07001012
Florin Coras97fef282023-12-21 19:41:12 -08001013 al = app_listener_get (ls->al_index);
1014 return app_listener_select_worker (al);
Florin Corasab2f6db2018-08-31 14:31:41 -07001015}
1016
Florin Coras7428eaa2023-12-11 16:04:57 -08001017always_inline u32
1018app_listener_cl_flow_hash (session_dgram_hdr_t *hdr)
1019{
1020 u32 hash = 0;
1021
1022 if (hdr->is_ip4)
1023 {
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);
1028 }
1029 else
1030 {
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);
1037 }
1038
1039 return hash;
1040}
1041
1042session_t *
1043app_listener_select_wrk_cl_session (session_t *ls, session_dgram_hdr_t *hdr)
1044{
1045 u32 wrk_map_index = 0;
Florin Coras7428eaa2023-12-11 16:04:57 -08001046 app_listener_t *al;
1047
Florin Coras97fef282023-12-21 19:41:12 -08001048 al = app_listener_get (ls->al_index);
Florin Coras7428eaa2023-12-11 16:04:57 -08001049 /* Crude test to check if only worker 0 is set */
1050 if (al->workers[0] != 1)
1051 {
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);
1057 }
1058
1059 return app_listener_get_wrk_cl_session (al, wrk_map_index);
1060}
1061
Florin Coras15531972018-08-12 23:50:53 -07001062int
Florin Coras623eb562019-02-03 19:28:34 -08001063application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
Florin Coras15531972018-08-12 23:50:53 -07001064{
1065 app_worker_map_t *wrk_map;
1066 app_worker_t *app_wrk;
1067 segment_manager_t *sm;
1068 int rv;
1069
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);
1074
1075 /*
1076 * Setup first segment manager
1077 */
Florin Coras88001c62019-04-24 14:44:46 -07001078 sm = segment_manager_alloc ();
Florin Coras15531972018-08-12 23:50:53 -07001079 sm->app_wrk_index = app_wrk->wrk_index;
1080
Florin Corasa107f402020-09-29 19:18:46 -07001081 if ((rv = segment_manager_init_first (sm)))
Florin Coras15531972018-08-12 23:50:53 -07001082 {
1083 app_worker_free (app_wrk);
1084 return rv;
1085 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001086 sm->first_is_protected = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001087
Florin Corascea194d2017-10-02 00:18:51 -07001088 /*
Florin Coras15531972018-08-12 23:50:53 -07001089 * Setup app worker
Florin Corascea194d2017-10-02 00:18:51 -07001090 */
Florin Coras94a6df02021-05-06 15:32:14 -07001091 app_wrk->connects_seg_manager = segment_manager_index (sm);
Florin Coras15531972018-08-12 23:50:53 -07001092 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);
Dave Barach68b0fb02017-02-28 15:15:56 -05001095
Florin Coras15531972018-08-12 23:50:53 -07001096 *wrk = app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001097
Florin Coras6cf30ad2017-04-04 23:08:23 -07001098 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001099}
1100
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001101session_error_t
1102vnet_app_worker_add_del (vnet_app_worker_add_del_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001103{
Florin Coras88001c62019-04-24 14:44:46 -07001104 fifo_segment_t *fs;
Florin Corasc1a42652019-02-08 18:27:29 -08001105 app_worker_map_t *wrk_map;
1106 app_worker_t *app_wrk;
1107 segment_manager_t *sm;
1108 application_t *app;
1109 int rv;
1110
1111 app = application_get (a->app_index);
1112 if (!app)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001113 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001114
1115 if (a->is_add)
1116 {
1117 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
1118 return rv;
1119
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);
1123
Florin Coras94a6df02021-05-06 15:32:14 -07001124 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Corasc1a42652019-02-08 18:27:29 -08001125 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;
1131 }
1132 else
1133 {
1134 wrk_map = app_worker_map_get (app, a->wrk_map_index);
1135 if (!wrk_map)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001136 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001137
1138 app_wrk = app_worker_get (wrk_map->wrk_index);
1139 if (!app_wrk)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001140 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001141
1142 application_api_table_del (app_wrk->api_client_index);
Florin Coras98078ab2021-08-12 18:12:09 -07001143 if (appns_sapi_enabled ())
1144 sapi_socket_close_w_handle (app_wrk->api_client_index);
Florin Corasc1a42652019-02-08 18:27:29 -08001145 app_worker_free (app_wrk);
1146 app_worker_map_free (app, wrk_map);
1147 if (application_n_workers (app) == 0)
1148 application_free (app);
1149 }
1150 return 0;
1151}
1152
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001153static session_error_t
1154app_validate_namespace (u8 *namespace_id, u64 secret, u32 *app_ns_index)
Florin Corasc1a42652019-02-08 18:27:29 -08001155{
1156 app_namespace_t *app_ns;
1157 if (vec_len (namespace_id) == 0)
1158 {
1159 /* Use default namespace */
1160 *app_ns_index = 0;
1161 return 0;
1162 }
1163
1164 *app_ns_index = app_namespace_index_from_id (namespace_id);
1165 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001166 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001167 app_ns = app_namespace_get (*app_ns_index);
1168 if (!app_ns)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001169 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001170 if (app_ns->ns_secret != secret)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001171 return SESSION_E_WRONG_NS_SECRET;
Florin Corasc1a42652019-02-08 18:27:29 -08001172 return 0;
1173}
1174
1175static u8 *
1176app_name_from_api_index (u32 api_client_index)
1177{
1178 vl_api_registration_t *regp;
1179 regp = vl_api_client_index_to_registration (api_client_index);
1180 if (regp)
Florin Corasbee97682019-04-09 16:13:18 -07001181 return format (0, "%s", regp->name);
Florin Corasc1a42652019-02-08 18:27:29 -08001182
1183 clib_warning ("api client index %u does not have an api registration!",
1184 api_client_index);
Florin Corasbee97682019-04-09 16:13:18 -07001185 return format (0, "unknown");
Florin Corasc1a42652019-02-08 18:27:29 -08001186}
1187
1188/**
1189 * Attach application to vpp
1190 *
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.
1194 */
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001195session_error_t
1196vnet_application_attach (vnet_app_attach_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001197{
Florin Coras88001c62019-04-24 14:44:46 -07001198 fifo_segment_t *fs;
Florin Corasc1a42652019-02-08 18:27:29 -08001199 application_t *app = 0;
1200 app_worker_t *app_wrk;
1201 segment_manager_t *sm;
1202 u32 app_ns_index = 0;
1203 u8 *app_name = 0;
1204 u64 secret;
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001205 session_error_t rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001206
1207 if (a->api_client_index != APP_INVALID_INDEX)
1208 app = application_lookup (a->api_client_index);
1209 else if (a->name)
1210 app = application_lookup_name (a->name);
1211 else
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001212 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001213
1214 if (app)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001215 return SESSION_E_APP_ATTACHED;
Florin Corasc1a42652019-02-08 18:27:29 -08001216
Florin Coras61ae0562020-09-02 19:10:28 -07001217 /* Socket api sets the name and validates namespace prior to attach */
1218 if (!a->use_sock_api)
Florin Corasc1a42652019-02-08 18:27:29 -08001219 {
Florin Coras61ae0562020-09-02 19:10:28 -07001220 if (a->api_client_index != APP_INVALID_INDEX)
1221 {
1222 app_name = app_name_from_api_index (a->api_client_index);
1223 a->name = app_name;
1224 }
Florin Corasc1a42652019-02-08 18:27:29 -08001225
Florin Coras61ae0562020-09-02 19:10:28 -07001226 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
Florin Coras6c10ab22020-11-08 16:50:39 -08001227 if ((rv = app_validate_namespace (a->namespace_id, secret,
1228 &app_ns_index)))
Florin Coras61ae0562020-09-02 19:10:28 -07001229 return rv;
1230 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
1231 }
Florin Corasc1a42652019-02-08 18:27:29 -08001232
1233 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
1234 return rv;
1235
1236 app = application_get (a->app_index);
1237 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
1238 return rv;
1239
1240 a->app_evt_q = app_wrk->event_queue;
1241 app_wrk->api_client_index = a->api_client_index;
Florin Coras94a6df02021-05-06 15:32:14 -07001242 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Corasc1a42652019-02-08 18:27:29 -08001243 fs = segment_manager_get_segment_w_lock (sm, 0);
1244
1245 if (application_is_proxy (app))
fanyfb8b6fe42020-09-17 22:10:41 +08001246 {
1247 application_setup_proxy (app);
Florin Coras2a7c0b62020-09-28 23:40:28 -07001248 /* The segment manager pool is reallocated because a new listener
1249 * is added. Re-grab segment manager to avoid dangling reference */
Florin Coras94a6df02021-05-06 15:32:14 -07001250 sm = segment_manager_get (app_wrk->connects_seg_manager);
fanyfb8b6fe42020-09-17 22:10:41 +08001251 }
Florin Corasc1a42652019-02-08 18:27:29 -08001252
1253 ASSERT (vec_len (fs->ssvm.name) <= 128);
1254 a->segment = &fs->ssvm;
1255 a->segment_handle = segment_manager_segment_handle (sm, fs);
1256
1257 segment_manager_segment_reader_unlock (sm);
Florin Coras41d5f542021-01-15 13:49:33 -08001258
1259 if (!application_is_builtin (app) && application_use_private_rx_mqs ())
1260 rv = app_rx_mqs_alloc (app);
1261
Florin Corasc1a42652019-02-08 18:27:29 -08001262 vec_free (app_name);
Florin Coras41d5f542021-01-15 13:49:33 -08001263 return rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001264}
1265
1266/**
1267 * Detach application from vpp
1268 */
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001269session_error_t
1270vnet_application_detach (vnet_app_detach_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001271{
1272 application_t *app;
1273
1274 app = application_get_if_valid (a->app_index);
1275 if (!app)
1276 {
1277 clib_warning ("app not attached");
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001278 return SESSION_E_NOAPP;
Florin Corasc1a42652019-02-08 18:27:29 -08001279 }
1280
1281 app_interface_check_thread_and_barrier (vnet_application_detach, a);
1282 application_detach_process (app, a->api_client_index);
1283 return 0;
1284}
1285
Florin Corasc1a42652019-02-08 18:27:29 -08001286static u8
Florin Coras8c2bdf82022-04-15 12:37:48 -07001287session_endpoint_in_ns (session_endpoint_cfg_t *sep)
Florin Corasc1a42652019-02-08 18:27:29 -08001288{
Florin Coras8c2bdf82022-04-15 12:37:48 -07001289 u8 is_lep;
1290
1291 if (sep->flags & SESSION_ENDPT_CFG_F_PROXY_LISTEN)
1292 return 1;
1293
1294 is_lep = session_endpoint_is_local ((session_endpoint_t *) sep);
Florin Corasc1a42652019-02-08 18:27:29 -08001295 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
1296 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
1297 {
1298 clib_warning ("sw_if_index %u not configured with ip %U",
1299 sep->sw_if_index, format_ip46_address, &sep->ip,
1300 sep->is_ip4);
1301 return 0;
1302 }
Florin Coras8c2bdf82022-04-15 12:37:48 -07001303
Florin Corasc1a42652019-02-08 18:27:29 -08001304 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
1305}
1306
1307static void
1308session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
1309 application_t * app, u8 is_connect)
1310{
1311 app_namespace_t *app_ns;
1312 u32 ns_index, fib_index;
1313
1314 ns_index = app->ns_index;
1315
1316 /* App is a transport proto, so fetch the calling app's ns */
1317 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
Florin Coras8a140612019-02-18 22:39:39 -08001318 ns_index = sep->ns_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001319
Florin Corasc1a42652019-02-08 18:27:29 -08001320 app_ns = app_namespace_get (ns_index);
1321 if (!app_ns)
1322 return;
1323
1324 /* Ask transport and network to bind to/connect using local interface
1325 * that "supports" app's namespace. This will fix our local connection
1326 * endpoint.
1327 */
1328
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;
1332 else
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;
1336
1337 if (!is_connect)
1338 {
1339 sep->sw_if_index = app_ns->sw_if_index;
1340 }
1341 else
1342 {
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");
1347
1348 sep->peer.sw_if_index = app_ns->sw_if_index;
1349 }
1350}
1351
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001352session_error_t
1353vnet_listen (vnet_listen_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001354{
1355 app_listener_t *app_listener;
1356 app_worker_t *app_wrk;
1357 application_t *app;
1358 int rv;
1359
Florin Coras458089b2019-08-21 16:20:44 -07001360 ASSERT (vlib_thread_is_main_w_barrier ());
1361
Florin Corasc1a42652019-02-08 18:27:29 -08001362 app = application_get_if_valid (a->app_index);
1363 if (!app)
Florin Corasa8c3b862020-04-07 22:31:06 +00001364 return SESSION_E_NOAPP;
Florin Corasc1a42652019-02-08 18:27:29 -08001365
1366 app_wrk = application_get_worker (app, a->wrk_map_index);
1367 if (!app_wrk)
Florin Corasa8c3b862020-04-07 22:31:06 +00001368 return SESSION_E_INVALID_APPWRK;
Florin Corasc1a42652019-02-08 18:27:29 -08001369
1370 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
1371
1372 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
Florin Coras8c2bdf82022-04-15 12:37:48 -07001373 if (!session_endpoint_in_ns (&a->sep_ext))
Florin Corasa8c3b862020-04-07 22:31:06 +00001374 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001375
1376 /*
1377 * Check if we already have an app listener
1378 */
1379 app_listener = app_listener_lookup (app, &a->sep_ext);
1380 if (app_listener)
1381 {
1382 if (app_listener->app_index != app->app_index)
Florin Corasa8c3b862020-04-07 22:31:06 +00001383 return SESSION_E_ALREADY_LISTENING;
1384 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1385 return rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001386 a->handle = app_listener_handle (app_listener);
1387 return 0;
1388 }
1389
1390 /*
1391 * Create new app listener
1392 */
1393 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1394 return rv;
1395
1396 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1397 {
1398 app_listener_cleanup (app_listener);
1399 return rv;
1400 }
1401
1402 a->handle = app_listener_handle (app_listener);
1403 return 0;
1404}
1405
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001406session_error_t
1407vnet_connect (vnet_connect_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001408{
Florin Coras2b81e3c2019-02-27 07:55:46 -08001409 app_worker_t *client_wrk;
Florin Corasc1a42652019-02-08 18:27:29 -08001410 application_t *client;
Florin Corasc1a42652019-02-08 18:27:29 -08001411
Florin Coras309f7aa2022-03-18 08:33:08 -07001412 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Coras458089b2019-08-21 16:20:44 -07001413
Florin Corasc1a42652019-02-08 18:27:29 -08001414 if (session_endpoint_is_zero (&a->sep))
Florin Coras00e01d32019-10-21 16:07:46 -07001415 return SESSION_E_INVALID_RMT_IP;
Florin Corasc1a42652019-02-08 18:27:29 -08001416
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);
1420
Florin Coras89a9f612021-05-11 11:55:07 -07001421 a->sep_ext.opaque = a->api_context;
1422
Florin Corasc1a42652019-02-08 18:27:29 -08001423 /*
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.
1427 */
1428 if (application_has_local_scope (client))
1429 {
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001430 session_error_t rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001431
Florin Coras2b81e3c2019-02-27 07:55:46 -08001432 a->sep_ext.original_tp = a->sep_ext.transport_proto;
1433 a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE;
Florin Coras89a9f612021-05-11 11:55:07 -07001434 rv = app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh);
Florin Coras00e01d32019-10-21 16:07:46 -07001435 a->sep_ext.transport_proto = a->sep_ext.original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -07001436 if (!rv || rv != SESSION_E_LOCAL_CONNECT)
1437 return rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001438 }
Florin Corasc1a42652019-02-08 18:27:29 -08001439 /*
1440 * Not connecting to a local server, propagate to transport
1441 */
Florin Coras89a9f612021-05-11 11:55:07 -07001442 return app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh);
Florin Corasc1a42652019-02-08 18:27:29 -08001443}
1444
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001445session_error_t
1446vnet_unlisten (vnet_unlisten_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001447{
1448 app_worker_t *app_wrk;
1449 app_listener_t *al;
1450 application_t *app;
1451
Florin Coras458089b2019-08-21 16:20:44 -07001452 ASSERT (vlib_thread_is_main_w_barrier ());
1453
Florin Corasc1a42652019-02-08 18:27:29 -08001454 if (!(app = application_get_if_valid (a->app_index)))
Florin Corasa8c3b862020-04-07 22:31:06 +00001455 return SESSION_E_NOAPP;
Florin Corasc1a42652019-02-08 18:27:29 -08001456
Florin Coras92311f62019-03-01 19:26:31 -08001457 if (!(al = app_listener_get_w_handle (a->handle)))
Florin Corasa8c3b862020-04-07 22:31:06 +00001458 return SESSION_E_NOLISTEN;
Florin Coras92311f62019-03-01 19:26:31 -08001459
Florin Corasc1a42652019-02-08 18:27:29 -08001460 if (al->app_index != app->app_index)
1461 {
1462 clib_warning ("app doesn't own handle %llu!", a->handle);
Florin Corasa8c3b862020-04-07 22:31:06 +00001463 return SESSION_E_OWNER;
Florin Corasc1a42652019-02-08 18:27:29 -08001464 }
1465
1466 app_wrk = application_get_worker (app, a->wrk_map_index);
1467 if (!app_wrk)
1468 {
1469 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001470 return SESSION_E_INVALID_APPWRK;
Florin Corasc1a42652019-02-08 18:27:29 -08001471 }
1472
1473 return app_worker_stop_listen (app_wrk, al);
1474}
1475
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001476session_error_t
liuyacan534468e2021-05-09 03:50:40 +00001477vnet_shutdown_session (vnet_shutdown_args_t *a)
1478{
1479 app_worker_t *app_wrk;
1480 session_t *s;
1481
1482 s = session_get_from_handle_if_valid (a->handle);
1483 if (!s)
1484 return SESSION_E_NOSESSION;
1485
1486 app_wrk = app_worker_get (s->app_wrk_index);
1487 if (app_wrk->app_index != a->app_index)
1488 return SESSION_E_OWNER;
1489
1490 /* We're peeking into another's thread pool. Make sure */
1491 ASSERT (s->session_index == session_index_from_handle (a->handle));
1492
1493 session_half_close (s);
1494 return 0;
1495}
1496
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001497session_error_t
1498vnet_disconnect_session (vnet_disconnect_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001499{
Florin Coras2b81e3c2019-02-27 07:55:46 -08001500 app_worker_t *app_wrk;
1501 session_t *s;
Florin Corasc1a42652019-02-08 18:27:29 -08001502
Florin Coras2b81e3c2019-02-27 07:55:46 -08001503 s = session_get_from_handle_if_valid (a->handle);
1504 if (!s)
Florin Corasa8c3b862020-04-07 22:31:06 +00001505 return SESSION_E_NOSESSION;
1506
Florin Coras2b81e3c2019-02-27 07:55:46 -08001507 app_wrk = app_worker_get (s->app_wrk_index);
1508 if (app_wrk->app_index != a->app_index)
Florin Corasa8c3b862020-04-07 22:31:06 +00001509 return SESSION_E_OWNER;
Florin Corasc1a42652019-02-08 18:27:29 -08001510
Florin Coras2b81e3c2019-02-27 07:55:46 -08001511 /* We're peeking into another's thread pool. Make sure */
1512 ASSERT (s->session_index == session_index_from_handle (a->handle));
Florin Corasc1a42652019-02-08 18:27:29 -08001513
Florin Coras2b81e3c2019-02-27 07:55:46 -08001514 session_close (s);
Florin Corasc1a42652019-02-08 18:27:29 -08001515 return 0;
1516}
1517
1518int
Florin Coras623eb562019-02-03 19:28:34 -08001519application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1520{
1521 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1522 app_listener_t *app_listener;
1523 application_t *app;
Florin Corasa8c3b862020-04-07 22:31:06 +00001524 int rv;
Florin Coras623eb562019-02-03 19:28:34 -08001525
1526 if (!old_wrk)
Florin Corasa8c3b862020-04-07 22:31:06 +00001527 return SESSION_E_INVALID_APPWRK;
Florin Coras623eb562019-02-03 19:28:34 -08001528
1529 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1530 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1531 && s->rx_fifo)
Florin Coras19223e02019-03-03 14:56:05 -08001532 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras623eb562019-02-03 19:28:34 -08001533
Florin Coras623eb562019-02-03 19:28:34 -08001534 app = application_get (old_wrk->app_index);
1535 if (!app)
Florin Corasa8c3b862020-04-07 22:31:06 +00001536 return SESSION_E_NOAPP;
Florin Coras623eb562019-02-03 19:28:34 -08001537
Florin Coras97fef282023-12-21 19:41:12 -08001538 app_listener = app_listener_get (s->al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -08001539
1540 /* Only remove from lb for now */
Florin Coras623eb562019-02-03 19:28:34 -08001541 app_listener->workers = clib_bitmap_set (app_listener->workers,
1542 old_wrk->wrk_map_index, 0);
Florin Coras623eb562019-02-03 19:28:34 -08001543
Florin Corasa8c3b862020-04-07 22:31:06 +00001544 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1545 return rv;
Florin Corasab2f6db2018-08-31 14:31:41 -07001546
Florin Corasc9940fc2019-02-05 20:55:11 -08001547 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001548
Dave Barach68b0fb02017-02-28 15:15:56 -05001549 return 0;
1550}
1551
Dave Barach52851e62017-08-07 09:35:25 -04001552int
1553application_is_proxy (application_t * app)
1554{
Florin Coras7999e832017-10-31 01:51:04 -07001555 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1556}
1557
1558int
1559application_is_builtin (application_t * app)
1560{
1561 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1562}
1563
1564int
1565application_is_builtin_proxy (application_t * app)
1566{
1567 return (application_is_proxy (app) && application_is_builtin (app));
Dave Barach52851e62017-08-07 09:35:25 -04001568}
1569
Florin Corascea194d2017-10-02 00:18:51 -07001570u8
1571application_has_local_scope (application_t * app)
1572{
1573 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1574}
1575
1576u8
1577application_has_global_scope (application_t * app)
1578{
1579 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1580}
1581
qinyangaf9b7152023-06-27 01:11:53 -07001582int
1583application_original_dst_is_enabled (application_t *app)
1584{
1585 return app->flags & APP_OPTIONS_FLAGS_GET_ORIGINAL_DST;
1586}
1587
Florin Coras7999e832017-10-31 01:51:04 -07001588static clib_error_t *
1589application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1590 u8 transport_proto, u8 is_start)
1591{
Florin Coras7999e832017-10-31 01:51:04 -07001592 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1593 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
Florin Coras5665ced2018-10-25 18:03:45 -07001594 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Florin Coras7999e832017-10-31 01:51:04 -07001595 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -07001596 app_worker_t *app_wrk;
Florin Corasc9940fc2019-02-05 20:55:11 -08001597 app_listener_t *al;
Florin Coras288eaab2019-02-03 15:26:14 -08001598 session_t *s;
Florin Corasc9940fc2019-02-05 20:55:11 -08001599 u32 flags;
Florin Coras7999e832017-10-31 01:51:04 -07001600
Florin Coras15531972018-08-12 23:50:53 -07001601 /* TODO decide if we want proxy to be enabled for all workers */
1602 app_wrk = application_get_default_worker (app);
Florin Coras7999e832017-10-31 01:51:04 -07001603 if (is_start)
1604 {
Florin Coras15531972018-08-12 23:50:53 -07001605 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001606 if (!s)
1607 {
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;
Florin Corasab2f6db2018-08-31 14:31:41 -07001612 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
Florin Corasc9940fc2019-02-05 20:55:11 -08001613
1614 /* force global scope listener */
1615 flags = app->flags;
1616 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1617 app_listener_alloc_and_init (app, &sep, &al);
1618 app->flags = flags;
1619
1620 app_worker_start_listen (app_wrk, al);
1621 s = listen_session_get (al->session_index);
Florin Corasd5c604d2019-03-18 09:06:35 -07001622 s->flags |= SESSION_F_PROXY;
Florin Coras19b1f6a2017-12-11 03:37:03 -08001623 }
Florin Coras7999e832017-10-31 01:51:04 -07001624 }
1625 else
1626 {
Florin Coras623eb562019-02-03 19:28:34 -08001627 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001628 ASSERT (s);
Florin Coras7999e832017-10-31 01:51:04 -07001629 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001630
Florin Coras7999e832017-10-31 01:51:04 -07001631 tc = listen_session_get_transport (s);
1632
1633 if (!ip_is_zero (&tc->lcl_ip, 1))
1634 {
Florin Corasdbd44562017-11-09 19:30:17 -08001635 u32 sti;
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;
1639 sep.port = 0;
1640 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001641 if (is_start)
Florin Corasab2f6db2018-08-31 14:31:41 -07001642 session_lookup_add_session_endpoint (sti,
1643 (session_endpoint_t *) & sep,
1644 s->session_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001645 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001646 session_lookup_del_session_endpoint (sti,
1647 (session_endpoint_t *) & sep);
Florin Coras7999e832017-10-31 01:51:04 -07001648 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001649
Florin Coras7999e832017-10-31 01:51:04 -07001650 return 0;
1651}
1652
Florin Coras19b1f6a2017-12-11 03:37:03 -08001653static void
1654application_start_stop_proxy_local_scope (application_t * app,
1655 u8 transport_proto, u8 is_start)
1656{
1657 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1658 app_namespace_t *app_ns;
1659 app_ns = app_namespace_get (app->ns_index);
1660 sep.is_ip4 = 1;
1661 sep.transport_proto = transport_proto;
1662 sep.port = 0;
1663
1664 if (is_start)
1665 {
1666 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001667 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001668 sep.is_ip4 = 0;
1669 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001670 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001671 }
1672 else
1673 {
1674 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1675 sep.is_ip4 = 0;
1676 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1677 }
1678}
1679
Florin Coras7999e832017-10-31 01:51:04 -07001680void
Florin Coras561af9b2017-12-09 10:19:43 -08001681application_start_stop_proxy (application_t * app,
1682 transport_proto_t transport_proto, u8 is_start)
Florin Coras7999e832017-10-31 01:51:04 -07001683{
Florin Coras7999e832017-10-31 01:51:04 -07001684 if (application_has_local_scope (app))
Florin Coras19b1f6a2017-12-11 03:37:03 -08001685 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
Florin Coras7999e832017-10-31 01:51:04 -07001686
1687 if (application_has_global_scope (app))
1688 {
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);
1693 }
1694}
1695
1696void
1697application_setup_proxy (application_t * app)
1698{
1699 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001700 transport_proto_t tp;
1701
Florin Coras7999e832017-10-31 01:51:04 -07001702 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001703
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +02001704 transport_proto_foreach (tp, transports)
1705 application_start_stop_proxy (app, tp, 1);
Florin Coras7999e832017-10-31 01:51:04 -07001706}
1707
1708void
1709application_remove_proxy (application_t * app)
1710{
1711 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001712 transport_proto_t tp;
1713
Florin Coras7999e832017-10-31 01:51:04 -07001714 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001715
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +02001716 transport_proto_foreach (tp, transports)
1717 application_start_stop_proxy (app, tp, 0);
Florin Coras7999e832017-10-31 01:51:04 -07001718}
1719
Florin Coras88001c62019-04-24 14:44:46 -07001720segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -08001721application_segment_manager_properties (application_t * app)
1722{
1723 return &app->sm_properties;
1724}
1725
Florin Coras88001c62019-04-24 14:44:46 -07001726segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -08001727application_get_segment_manager_properties (u32 app_index)
1728{
1729 application_t *app = application_get (app_index);
1730 return &app->sm_properties;
1731}
1732
Florin Coras15531972018-08-12 23:50:53 -07001733static void
1734application_format_listeners (application_t * app, int verbose)
1735{
1736 vlib_main_t *vm = vlib_get_main ();
1737 app_worker_map_t *wrk_map;
1738 app_worker_t *app_wrk;
1739 u32 sm_index;
1740 u64 handle;
1741
1742 if (!app)
1743 {
Andreas Schultz972dc172020-05-15 11:50:07 +02001744 vlib_cli_output (vm, "%U", format_app_worker_listener, NULL /* header */,
Florin Coras15531972018-08-12 23:50:53 -07001745 0, 0, verbose);
1746 return;
1747 }
1748
Damjan Marionb2c31b62020-12-13 21:47:40 +01001749 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras15531972018-08-12 23:50:53 -07001750 app_wrk = app_worker_get (wrk_map->wrk_index);
1751 if (hash_elts (app_wrk->listeners_table) == 0)
1752 continue;
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);
1756 }));
Damjan Marionb2c31b62020-12-13 21:47:40 +01001757 }
Florin Coras15531972018-08-12 23:50:53 -07001758}
1759
1760static void
Florin Coras15531972018-08-12 23:50:53 -07001761application_format_connects (application_t * app, int verbose)
1762{
1763 app_worker_map_t *wrk_map;
1764 app_worker_t *app_wrk;
1765
1766 if (!app)
1767 {
1768 app_worker_format_connects (0, verbose);
1769 return;
1770 }
1771
Damjan Marionb2c31b62020-12-13 21:47:40 +01001772 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras15531972018-08-12 23:50:53 -07001773 app_wrk = app_worker_get (wrk_map->wrk_index);
1774 app_worker_format_connects (app_wrk, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001775 }
Florin Coras15531972018-08-12 23:50:53 -07001776}
1777
Florin Coras6cf30ad2017-04-04 23:08:23 -07001778u8 *
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001779format_cert_key_pair (u8 * s, va_list * args)
1780{
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);
1787 else
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001788 s = format (s, "%d (cert:%d, key:%d)", ckpair->cert_key_index,
1789 cert_len, key_len);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001790 return s;
1791}
1792
1793u8 *
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02001794format_crypto_engine (u8 * s, va_list * args)
1795{
1796 u32 engine = va_arg (*args, u32);
1797 switch (engine)
1798 {
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");
1809 default:
1810 return format (s, "unknown engine");
1811 }
1812 return s;
1813}
1814
1815uword
1816unformat_crypto_engine (unformat_input_t * input, va_list * args)
1817{
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;
1827 else
1828 return 0;
1829 return 1;
1830}
1831
1832u8 *
1833format_crypto_context (u8 * s, va_list * args)
1834{
1835 crypto_context_t *crctx = va_arg (*args, crypto_context_t *);
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001836 s = format (s, "[0x%x][sub%d,ckpair%x]", crctx->ctx_index,
1837 crctx->n_subscribers, crctx->ckpair_index);
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02001838 s = format (s, "[%U]", format_crypto_engine, crctx->crypto_engine);
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02001839 return s;
1840}
1841
1842u8 *
Florin Coras6cf30ad2017-04-04 23:08:23 -07001843format_application (u8 * s, va_list * args)
1844{
1845 application_t *app = va_arg (*args, application_t *);
1846 CLIB_UNUSED (int verbose) = va_arg (*args, int);
Florin Coras88001c62019-04-24 14:44:46 -07001847 segment_manager_props_t *props;
Florin Coras053a0e42018-11-13 15:52:38 -08001848 const u8 *app_ns_name, *app_name;
Florin Coras349f8ca2018-11-20 16:52:49 -08001849 app_worker_map_t *wrk_map;
1850 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001851
1852 if (app == 0)
1853 {
Florin Coras349f8ca2018-11-20 16:52:49 -08001854 if (!verbose)
Florin Corasc1f5a432018-11-20 11:31:26 -08001855 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
Florin Coras6cf30ad2017-04-04 23:08:23 -07001856 return s;
1857 }
1858
Florin Coras0bee9ce2018-03-22 21:24:31 -07001859 app_name = app_get_name (app);
Florin Corascea194d2017-10-02 00:18:51 -07001860 app_ns_name = app_namespace_id_from_index (app->ns_index);
Florin Corasa332c462018-01-31 06:52:17 -08001861 props = application_segment_manager_properties (app);
Florin Coras349f8ca2018-11-20 16:52:49 -08001862 if (!verbose)
1863 {
jiangxiaomingdfb30d92020-08-31 17:38:11 +08001864 s = format (s, "%-10u%-20v%-40v", app->app_index, app_name,
Florin Coras349f8ca2018-11-20 16:52:49 -08001865 app_ns_name);
1866 return s;
1867 }
1868
Florin Corasfa7512e2019-04-04 22:31:50 -07001869 s = format (s, "app-name %v app-index %u ns-index %u seg-size %U\n",
Florin Coras349f8ca2018-11-20 16:52:49 -08001870 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);
1875
Damjan Marionb2c31b62020-12-13 21:47:40 +01001876 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras349f8ca2018-11-20 16:52:49 -08001877 app_wrk = app_worker_get (wrk_map->wrk_index);
Florin Coras623eb562019-02-03 19:28:34 -08001878 s = format (s, "%U", format_app_worker, app_wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001879 }
Florin Coras349f8ca2018-11-20 16:52:49 -08001880
Dave Barach68b0fb02017-02-28 15:15:56 -05001881 return s;
1882}
1883
Florin Corasf8f516a2018-02-08 15:10:09 -08001884void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001885application_format_all_listeners (vlib_main_t * vm, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08001886{
1887 application_t *app;
Florin Corasf8f516a2018-02-08 15:10:09 -08001888
Florin Coras15531972018-08-12 23:50:53 -07001889 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08001890 {
1891 vlib_cli_output (vm, "No active server bindings");
1892 return;
1893 }
1894
Florin Coras2b81e3c2019-02-27 07:55:46 -08001895 application_format_listeners (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001896
Damjan Marionb2c31b62020-12-13 21:47:40 +01001897 pool_foreach (app, app_main.app_pool) {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001898 application_format_listeners (app, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001899 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001900}
1901
1902void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001903application_format_all_clients (vlib_main_t * vm, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08001904{
1905 application_t *app;
1906
Florin Coras15531972018-08-12 23:50:53 -07001907 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08001908 {
1909 vlib_cli_output (vm, "No active apps");
1910 return;
1911 }
1912
Florin Coras2b81e3c2019-02-27 07:55:46 -08001913 application_format_connects (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001914
Damjan Marionb2c31b62020-12-13 21:47:40 +01001915 pool_foreach (app, app_main.app_pool) {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001916 application_format_connects (app, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001917 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001918}
1919
Dave Barach68b0fb02017-02-28 15:15:56 -05001920static clib_error_t *
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001921show_certificate_command_fn (vlib_main_t * vm, unformat_input_t * input,
1922 vlib_cli_command_t * cmd)
1923{
1924 app_cert_key_pair_t *ckpair;
1925 session_cli_return_if_not_enabled ();
1926
Damjan Marionb2c31b62020-12-13 21:47:40 +01001927 pool_foreach (ckpair, app_main.cert_key_pair_store) {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001928 vlib_cli_output (vm, "%U", format_cert_key_pair, ckpair);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001929 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001930 return 0;
1931}
1932
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001933static inline void
1934appliction_format_app_mq (vlib_main_t * vm, application_t * app)
1935{
1936 app_worker_map_t *map;
1937 app_worker_t *wrk;
Florin Coras41d5f542021-01-15 13:49:33 -08001938 int i;
1939
Damjan Marionb2c31b62020-12-13 21:47:40 +01001940 pool_foreach (map, app->worker_maps) {
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001941 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,
1944 wrk->event_queue);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001945 }
Florin Coras41d5f542021-01-15 13:49:33 -08001946
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,
1949 app->rx_mqs[i].mq);
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001950}
1951
1952static clib_error_t *
1953appliction_format_all_app_mq (vlib_main_t * vm)
1954{
1955 application_t *app;
1956 int i, n_threads;
1957
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001958 n_threads = vlib_get_n_threads ();
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001959
1960 for (i = 0; i < n_threads; i++)
1961 {
1962 vlib_cli_output (vm, "[Ctrl%d]%U", i, format_svm_msg_q,
1963 session_main_get_vpp_event_queue (i));
1964 }
1965
Damjan Marionb2c31b62020-12-13 21:47:40 +01001966 pool_foreach (app, app_main.app_pool) {
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001967 appliction_format_app_mq (vm, app);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001968 }
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001969 return 0;
1970}
1971
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001972static clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -05001973show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1974 vlib_cli_command_t * cmd)
1975{
Florin Coras7e7b0302022-01-22 13:27:21 -08001976 int do_server = 0, do_client = 0, do_mq = 0, do_transports = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001977 application_t *app;
Florin Coras349f8ca2018-11-20 16:52:49 -08001978 u32 app_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001979 int verbose = 0;
Florin Coras7e7b0302022-01-22 13:27:21 -08001980 u8 is_ta;
Dave Barach68b0fb02017-02-28 15:15:56 -05001981
Florin Corascea194d2017-10-02 00:18:51 -07001982 session_cli_return_if_not_enabled ();
Florin Corase04c2992017-03-01 08:17:34 -08001983
Dave Barach68b0fb02017-02-28 15:15:56 -05001984 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1985 {
1986 if (unformat (input, "server"))
1987 do_server = 1;
1988 else if (unformat (input, "client"))
1989 do_client = 1;
Florin Coras7e7b0302022-01-22 13:27:21 -08001990 else if (unformat (input, "transports"))
1991 do_transports = 1;
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001992 else if (unformat (input, "mq"))
1993 do_mq = 1;
Florin Coras349f8ca2018-11-20 16:52:49 -08001994 else if (unformat (input, "%u", &app_index))
1995 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001996 else if (unformat (input, "verbose"))
1997 verbose = 1;
1998 else
Florin Coras349f8ca2018-11-20 16:52:49 -08001999 return clib_error_return (0, "unknown input `%U'",
2000 format_unformat_error, input);
Dave Barach68b0fb02017-02-28 15:15:56 -05002001 }
2002
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01002003 if (do_mq && app_index != ~0)
2004 {
2005 app = application_get_if_valid (app_index);
2006 if (!app)
2007 return clib_error_return (0, "No app with index %u", app_index);
2008
2009 appliction_format_app_mq (vm, app);
2010 return 0;
2011 }
2012
2013 if (do_mq)
2014 {
2015 appliction_format_all_app_mq (vm);
2016 return 0;
2017 }
2018
Dave Barach68b0fb02017-02-28 15:15:56 -05002019 if (do_server)
Florin Coras349f8ca2018-11-20 16:52:49 -08002020 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08002021 application_format_all_listeners (vm, verbose);
Florin Coras349f8ca2018-11-20 16:52:49 -08002022 return 0;
2023 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002024
2025 if (do_client)
Florin Coras349f8ca2018-11-20 16:52:49 -08002026 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08002027 application_format_all_clients (vm, verbose);
Florin Coras349f8ca2018-11-20 16:52:49 -08002028 return 0;
2029 }
2030
2031 if (app_index != ~0)
2032 {
Florin Coras47c40e22018-11-26 17:01:36 -08002033 app = application_get_if_valid (app_index);
Florin Coras349f8ca2018-11-20 16:52:49 -08002034 if (!app)
2035 return clib_error_return (0, "No app with index %u", app_index);
2036
2037 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
2038 return 0;
2039 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002040
Florin Coras6cf30ad2017-04-04 23:08:23 -07002041 /* Print app related info */
2042 if (!do_server && !do_client)
2043 {
Florin Coras349f8ca2018-11-20 16:52:49 -08002044 vlib_cli_output (vm, "%U", format_application, 0, 0);
Damjan Marionb2c31b62020-12-13 21:47:40 +01002045 pool_foreach (app, app_main.app_pool) {
Florin Coras7e7b0302022-01-22 13:27:21 -08002046 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);
Damjan Marionb2c31b62020-12-13 21:47:40 +01002049 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07002050 }
2051
Dave Barach68b0fb02017-02-28 15:15:56 -05002052 return 0;
2053}
2054
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002055/* Certificate store */
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002056
2057static app_cert_key_pair_t *
2058app_cert_key_pair_alloc ()
2059{
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;
2064 return ckpair;
2065}
2066
2067app_cert_key_pair_t *
2068app_cert_key_pair_get_if_valid (u32 index)
2069{
2070 if (pool_is_free_index (app_main.cert_key_pair_store, index))
2071 return 0;
2072 return app_cert_key_pair_get (index);
2073}
2074
2075app_cert_key_pair_t *
2076app_cert_key_pair_get (u32 index)
2077{
2078 return pool_elt_at_index (app_main.cert_key_pair_store, index);
2079}
2080
2081app_cert_key_pair_t *
2082app_cert_key_pair_get_default ()
2083{
2084 /* To maintain legacy bapi */
2085 return app_cert_key_pair_get (0);
2086}
2087
2088int
2089vnet_app_add_cert_key_pair (vnet_app_add_cert_key_pair_args_t * a)
2090{
2091 app_cert_key_pair_t *ckpair = app_cert_key_pair_alloc ();
Florin Corasa5a9efd2021-01-05 17:03:29 -08002092 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);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002096 a->index = ckpair->cert_key_index;
2097 return 0;
2098}
2099
2100int
Nathan Skrzypczak7fbdb6a2019-10-09 16:23:26 +02002101vnet_app_add_cert_key_interest (u32 index, u32 app_index)
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002102{
2103 app_cert_key_pair_t *ckpair;
2104 if (!(ckpair = app_cert_key_pair_get_if_valid (index)))
2105 return -1;
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002106 if (vec_search (ckpair->app_interests, app_index) != ~0)
2107 vec_add1 (ckpair->app_interests, app_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002108 return 0;
2109}
2110
2111int
2112vnet_app_del_cert_key_pair (u32 index)
2113{
2114 app_cert_key_pair_t *ckpair;
2115 application_t *app;
2116 u32 *app_index;
2117
2118 if (!(ckpair = app_cert_key_pair_get_if_valid (index)))
Filip Tehlar0028e6f2023-06-28 10:47:32 +02002119 return SESSION_E_INVALID;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002120
2121 vec_foreach (app_index, ckpair->app_interests)
2122 {
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);
2126 }
2127
2128 vec_free (ckpair->cert);
2129 vec_free (ckpair->key);
2130 pool_put (app_main.cert_key_pair_store, ckpair);
2131 return 0;
2132}
2133
2134clib_error_t *
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002135application_init (vlib_main_t * vm)
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002136{
Florin Coras41d5f542021-01-15 13:49:33 -08002137 app_main_t *am = &app_main;
2138 u32 n_workers;
2139
2140 n_workers = vlib_num_workers ();
2141
Florin Corasa5a9efd2021-01-05 17:03:29 -08002142 /* Index 0 was originally used by legacy apis, maintain as invalid */
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002143 (void) app_cert_key_pair_alloc ();
Florin Coras41d5f542021-01-15 13:49:33 -08002144 am->last_crypto_engine = CRYPTO_ENGINE_LAST;
2145 am->app_by_name = hash_create_vec (0, sizeof (u8), sizeof (uword));
2146
2147 vec_validate (am->wrk, n_workers);
2148
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002149 return 0;
2150}
2151
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002152VLIB_INIT_FUNCTION (application_init);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002153
Florin Coras7e7b0302022-01-22 13:27:21 -08002154VLIB_CLI_COMMAND (show_app_command, static) = {
Florin Corase04c2992017-03-01 08:17:34 -08002155 .path = "show app",
Florin Coras7e7b0302022-01-22 13:27:21 -08002156 .short_help = "show app [index] [server|client] [mq] [verbose] "
2157 "[transports]",
Florin Corase04c2992017-03-01 08:17:34 -08002158 .function = show_app_command_fn,
2159};
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002160
Florin Coras7e7b0302022-01-22 13:27:21 -08002161VLIB_CLI_COMMAND (show_certificate_command, static) = {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002162 .path = "show app certificate",
2163 .short_help = "list app certs and keys present in store",
2164 .function = show_certificate_command_fn,
2165};
Dave Barach68b0fb02017-02-28 15:15:56 -05002166
Florin Coras79ba25d2019-10-20 19:32:47 -07002167crypto_engine_type_t
2168app_crypto_engine_type_add (void)
2169{
2170 return (++app_main.last_crypto_engine);
2171}
2172
2173u8
2174app_crypto_engine_n_types (void)
2175{
2176 return (app_main.last_crypto_engine + 1);
2177}
2178
Dave Barach68b0fb02017-02-28 15:15:56 -05002179/*
2180 * fd.io coding-style-patch-verification: ON
2181 *
2182 * Local Variables:
2183 * eval: (c-set-style "gnu")
2184 * End:
2185 */