blob: cf867401ea24bef7dfe405b600dce6fd9d328718 [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{
34 app_listener_t *app_listener;
35 pool_get (app->listeners, app_listener);
Dave Barachb7b92992018-10-17 10:38:51 -040036 clib_memset (app_listener, 0, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070037 app_listener->al_index = app_listener - app->listeners;
Florin Corasc9940fc2019-02-05 20:55:11 -080038 app_listener->app_index = app->app_index;
39 app_listener->session_index = SESSION_INVALID_INDEX;
40 app_listener->local_index = SESSION_INVALID_INDEX;
Florin Coras87d66332019-06-11 12:31:31 -070041 app_listener->ls_handle = SESSION_INVALID_HANDLE;
Florin Corasab2f6db2018-08-31 14:31:41 -070042 return app_listener;
43}
44
Florin Corasc9940fc2019-02-05 20:55:11 -080045app_listener_t *
Florin Corasab2f6db2018-08-31 14:31:41 -070046app_listener_get (application_t * app, u32 app_listener_index)
47{
48 return pool_elt_at_index (app->listeners, app_listener_index);
49}
50
51static void
52app_listener_free (application_t * app, app_listener_t * app_listener)
53{
54 clib_bitmap_free (app_listener->workers);
Florin Corasab2f6db2018-08-31 14:31:41 -070055 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040056 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
BenoƮt Ganned4aeb842019-07-18 18:38:42 +020057 pool_put (app->listeners, app_listener);
Florin Corasab2f6db2018-08-31 14:31:41 -070058}
59
Florin Corasc9940fc2019-02-05 20:55:11 -080060session_handle_t
61app_listener_handle (app_listener_t * al)
62{
Florin Coras87d66332019-06-11 12:31:31 -070063 return al->ls_handle;
Florin Corasc9940fc2019-02-05 20:55:11 -080064}
65
66app_listener_t *
67app_listener_get_w_session (session_t * ls)
68{
69 application_t *app;
70
71 app = application_get_if_valid (ls->app_index);
72 if (!app)
73 return 0;
74 return app_listener_get (app, ls->al_index);
75}
76
Florin Coras87d66332019-06-11 12:31:31 -070077session_handle_t
78app_listen_session_handle (session_t * ls)
79{
80 app_listener_t *al;
81 al = app_listener_get_w_session (ls);
82 if (!al)
83 return listen_session_get_handle (ls);
84 return al->ls_handle;
85}
86
Florin Corasc9940fc2019-02-05 20:55:11 -080087app_listener_t *
88app_listener_get_w_handle (session_handle_t handle)
89{
Florin Coras87d66332019-06-11 12:31:31 -070090 session_t *ls;
91 ls = session_get_from_handle_if_valid (handle);
92 if (!ls)
Florin Corasc9940fc2019-02-05 20:55:11 -080093 return 0;
Florin Coras87d66332019-06-11 12:31:31 -070094 return app_listener_get_w_session (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -080095}
96
97app_listener_t *
98app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
99{
100 u32 table_index, fib_proto;
101 session_endpoint_t *sep;
102 session_handle_t handle;
Florin Corasc9940fc2019-02-05 20:55:11 -0800103 session_t *ls;
liuyacan694046c2021-04-30 08:57:37 +0000104 void *iface_ip;
105 ip46_address_t original_ip;
Florin Corasc9940fc2019-02-05 20:55:11 -0800106
107 sep = (session_endpoint_t *) sep_ext;
108 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
109 {
110 table_index = application_local_session_table (app);
111 handle = session_lookup_endpoint_listener (table_index, sep, 1);
112 if (handle != SESSION_INVALID_HANDLE)
113 {
Florin Corasd4295e62019-02-22 13:11:38 -0800114 ls = listen_session_get_from_handle (handle);
115 return app_listener_get_w_session (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800116 }
117 }
118
119 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800120 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800121 handle = session_lookup_endpoint_listener (table_index, sep, 1);
122 if (handle != SESSION_INVALID_HANDLE)
123 {
124 ls = listen_session_get_from_handle (handle);
125 return app_listener_get_w_session ((session_t *) ls);
126 }
127
liuyacan694046c2021-04-30 08:57:37 +0000128 /*
129 * When binds to "inaddr_any", we add zero address in the local lookup table
130 * and interface address in the global lookup table. If local scope disable,
131 * the latter is the only clue to find the listener.
132 */
133 if (!application_has_local_scope (app) &&
134 ip_is_zero (&sep_ext->ip, sep_ext->is_ip4) &&
135 sep_ext->sw_if_index != ENDPOINT_INVALID_INDEX)
136 {
137 if ((iface_ip = ip_interface_get_first_ip (sep_ext->sw_if_index,
138 sep_ext->is_ip4)))
139 {
140 ip_copy (&original_ip, &sep_ext->ip, sep_ext->is_ip4);
141 ip_set (&sep_ext->ip, iface_ip, sep_ext->is_ip4);
142 handle = session_lookup_endpoint_listener (table_index, sep, 1);
143 ip_copy (&sep_ext->ip, &original_ip, sep_ext->is_ip4);
144 if (handle != SESSION_INVALID_HANDLE)
145 {
146 ls = listen_session_get_from_handle (handle);
147 return app_listener_get_w_session ((session_t *) ls);
148 }
149 }
150 }
151
Florin Corasc9940fc2019-02-05 20:55:11 -0800152 return 0;
153}
154
155int
156app_listener_alloc_and_init (application_t * app,
157 session_endpoint_cfg_t * sep,
158 app_listener_t ** listener)
159{
160 app_listener_t *app_listener;
Florin Corasd4295e62019-02-22 13:11:38 -0800161 transport_connection_t *tc;
Florin Coras95cd8642019-12-30 21:53:19 -0800162 u32 al_index, table_index;
Florin Corasc9940fc2019-02-05 20:55:11 -0800163 session_handle_t lh;
164 session_type_t st;
165 session_t *ls = 0;
166 int rv;
167
168 app_listener = app_listener_alloc (app);
Florin Corasa27a46e2019-02-18 13:02:28 -0800169 al_index = app_listener->al_index;
Florin Corasc9940fc2019-02-05 20:55:11 -0800170 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
171
172 /*
173 * Add session endpoint to local session table. Only binds to "inaddr_any"
174 * (i.e., zero address) are added to local scope table.
175 */
176 if (application_has_local_scope (app)
177 && session_endpoint_is_local ((session_endpoint_t *) sep))
178 {
Florin Corasd4295e62019-02-22 13:11:38 -0800179 session_type_t local_st;
Florin Corasc9940fc2019-02-05 20:55:11 -0800180
Florin Corasd4295e62019-02-22 13:11:38 -0800181 local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
182 sep->is_ip4);
183 ls = listen_session_alloc (0, local_st);
184 ls->app_index = app->app_index;
185 ls->app_wrk_index = sep->app_wrk_index;
186 lh = session_handle (ls);
187
188 if ((rv = session_listen (ls, sep)))
189 {
190 ls = session_get_from_handle (lh);
191 session_free (ls);
Xiaoming Jiang1f704252023-04-19 08:41:29 +0000192 app_listener_free (app, app_listener);
Florin Corasd4295e62019-02-22 13:11:38 -0800193 return rv;
194 }
195
196 ls = session_get_from_handle (lh);
197 app_listener = app_listener_get (app, al_index);
198 app_listener->local_index = ls->session_index;
Florin Coras87d66332019-06-11 12:31:31 -0700199 app_listener->ls_handle = lh;
Florin Corasd4295e62019-02-22 13:11:38 -0800200 ls->al_index = al_index;
201
Florin Corasc9940fc2019-02-05 20:55:11 -0800202 table_index = application_local_session_table (app);
Florin Corasc9940fc2019-02-05 20:55:11 -0800203 session_lookup_add_session_endpoint (table_index,
204 (session_endpoint_t *) sep, lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800205 }
206
207 if (application_has_global_scope (app))
208 {
209 /*
210 * Start listening on local endpoint for requested transport and scope.
211 * Creates a stream session with state LISTENING to be used in session
212 * lookups, prior to establishing connection. Requests transport to
213 * build it's own specific listening connection.
214 */
Florin Corasba7d8f52019-02-22 13:11:38 -0800215 ls = listen_session_alloc (0, st);
Florin Corasc9940fc2019-02-05 20:55:11 -0800216 ls->app_index = app->app_index;
217 ls->app_wrk_index = sep->app_wrk_index;
218
219 /* Listen pool can be reallocated if the transport is
220 * recursive (tls) */
Florin Corasd4295e62019-02-22 13:11:38 -0800221 lh = listen_session_get_handle (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800222
223 if ((rv = session_listen (ls, sep)))
224 {
Florin Corasd4295e62019-02-22 13:11:38 -0800225 ls = listen_session_get_from_handle (lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800226 session_free (ls);
Xiaoming Jiang1f704252023-04-19 08:41:29 +0000227 app_listener_free (app, app_listener);
Florin Corasc9940fc2019-02-05 20:55:11 -0800228 return rv;
229 }
Florin Corasd4295e62019-02-22 13:11:38 -0800230 ls = listen_session_get_from_handle (lh);
Florin Corasa27a46e2019-02-18 13:02:28 -0800231 app_listener = app_listener_get (app, al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800232 app_listener->session_index = ls->session_index;
Florin Coras87d66332019-06-11 12:31:31 -0700233 app_listener->ls_handle = lh;
Florin Corasa27a46e2019-02-18 13:02:28 -0800234 ls->al_index = al_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800235
236 /* Add to the global lookup table after transport was initialized.
237 * Lookup table needs to be populated only now because sessions
238 * with cut-through transport are are added to app local tables that
239 * are not related to network fibs, i.e., cannot be added as
240 * connections */
241 tc = session_get_transport (ls);
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200242 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Florin Coras95cd8642019-12-30 21:53:19 -0800243 {
244 fib_protocol_t fib_proto;
245 fib_proto = session_endpoint_fib_proto ((session_endpoint_t *) sep);
Florin Corasa4d09562021-07-08 08:25:09 -0700246 /* Assume namespace vetted previously so make sure table exists */
247 table_index = session_lookup_get_or_alloc_index_for_fib (
248 fib_proto, sep->fib_index);
Florin Coras95cd8642019-12-30 21:53:19 -0800249 session_lookup_add_session_endpoint (table_index,
250 (session_endpoint_t *) sep,
251 lh);
252 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800253 }
254
Florin Coras2b81e3c2019-02-27 07:55:46 -0800255 if (!ls)
Florin Corasc9940fc2019-02-05 20:55:11 -0800256 {
257 app_listener_free (app, app_listener);
258 return -1;
259 }
260
261 *listener = app_listener;
262 return 0;
263}
264
265void
266app_listener_cleanup (app_listener_t * al)
267{
268 application_t *app = application_get (al->app_index);
Florin Corasd4295e62019-02-22 13:11:38 -0800269 session_t *ls;
Florin Corasc9940fc2019-02-05 20:55:11 -0800270
271 if (al->session_index != SESSION_INVALID_INDEX)
272 {
Florin Corasd4295e62019-02-22 13:11:38 -0800273 ls = session_get (al->session_index, 0);
Florin Corasc9940fc2019-02-05 20:55:11 -0800274 session_stop_listen (ls);
Florin Corasba7d8f52019-02-22 13:11:38 -0800275 listen_session_free (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800276 }
277 if (al->local_index != SESSION_INVALID_INDEX)
278 {
279 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Corasc9940fc2019-02-05 20:55:11 -0800280 u32 table_index;
281
282 table_index = application_local_session_table (app);
Florin Corasd4295e62019-02-22 13:11:38 -0800283 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800284 ct_session_endpoint (ls, &sep);
Florin Corasc9940fc2019-02-05 20:55:11 -0800285 session_lookup_del_session_endpoint (table_index, &sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800286 session_stop_listen (ls);
Florin Corasd4295e62019-02-22 13:11:38 -0800287 listen_session_free (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800288 }
289 app_listener_free (app, al);
290}
291
Florin Coras11e2cf52019-03-06 12:04:24 -0800292static app_worker_t *
293app_listener_select_worker (application_t * app, app_listener_t * al)
Florin Corasc9940fc2019-02-05 20:55:11 -0800294{
Florin Corasc9940fc2019-02-05 20:55:11 -0800295 u32 wrk_index;
296
297 app = application_get (al->app_index);
298 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
299 if (wrk_index == ~0)
300 wrk_index = clib_bitmap_first_set (al->workers);
301
302 ASSERT (wrk_index != ~0);
303 al->accept_rotor = wrk_index;
304 return application_get_worker (app, wrk_index);
305}
306
307session_t *
308app_listener_get_session (app_listener_t * al)
309{
310 if (al->session_index == SESSION_INVALID_INDEX)
311 return 0;
312
313 return listen_session_get (al->session_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700314}
315
Florin Corasd4295e62019-02-22 13:11:38 -0800316session_t *
317app_listener_get_local_session (app_listener_t * al)
318{
319 if (al->local_index == SESSION_INVALID_INDEX)
320 return 0;
321 return listen_session_get (al->local_index);
322}
323
Florin Coras15531972018-08-12 23:50:53 -0700324static app_worker_map_t *
325app_worker_map_alloc (application_t * app)
326{
327 app_worker_map_t *map;
328 pool_get (app->worker_maps, map);
Dave Barachb7b92992018-10-17 10:38:51 -0400329 clib_memset (map, 0, sizeof (*map));
Florin Coras15531972018-08-12 23:50:53 -0700330 return map;
331}
Dave Barach68b0fb02017-02-28 15:15:56 -0500332
Florin Coras15531972018-08-12 23:50:53 -0700333static u32
334app_worker_map_index (application_t * app, app_worker_map_t * map)
335{
336 return (map - app->worker_maps);
337}
338
339static void
340app_worker_map_free (application_t * app, app_worker_map_t * map)
341{
342 pool_put (app->worker_maps, map);
343}
344
345static app_worker_map_t *
346app_worker_map_get (application_t * app, u32 map_index)
347{
Florin Coras01f3f892018-12-02 12:45:53 -0800348 if (pool_is_free_index (app->worker_maps, map_index))
349 return 0;
Florin Coras15531972018-08-12 23:50:53 -0700350 return pool_elt_at_index (app->worker_maps, map_index);
351}
Florin Coras0bee9ce2018-03-22 21:24:31 -0700352
Florin Coras053a0e42018-11-13 15:52:38 -0800353static const u8 *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700354app_get_name (application_t * app)
355{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700356 return app->name;
357}
358
Florin Corascea194d2017-10-02 00:18:51 -0700359u32
360application_session_table (application_t * app, u8 fib_proto)
361{
362 app_namespace_t *app_ns;
363 app_ns = app_namespace_get (app->ns_index);
364 if (!application_has_global_scope (app))
365 return APP_INVALID_INDEX;
366 if (fib_proto == FIB_PROTOCOL_IP4)
367 return session_lookup_get_index_for_fib (fib_proto,
368 app_ns->ip4_fib_index);
369 else
370 return session_lookup_get_index_for_fib (fib_proto,
371 app_ns->ip6_fib_index);
372}
373
374u32
375application_local_session_table (application_t * app)
376{
377 app_namespace_t *app_ns;
378 if (!application_has_local_scope (app))
379 return APP_INVALID_INDEX;
380 app_ns = app_namespace_get (app->ns_index);
381 return app_ns->local_table_index;
382}
383
Florin Corascea194d2017-10-02 00:18:51 -0700384/**
Florin Coras053a0e42018-11-13 15:52:38 -0800385 * Returns app name for app-index
Florin Corascea194d2017-10-02 00:18:51 -0700386 */
Florin Coras053a0e42018-11-13 15:52:38 -0800387const u8 *
Florin Corascea194d2017-10-02 00:18:51 -0700388application_name_from_index (u32 app_index)
389{
390 application_t *app = application_get (app_index);
391 if (!app)
392 return 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800393 return app_get_name (app);
394}
395
396static void
397application_api_table_add (u32 app_index, u32 api_client_index)
398{
Florin Corasc1f5a432018-11-20 11:31:26 -0800399 if (api_client_index != APP_INVALID_INDEX)
400 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
Florin Coras053a0e42018-11-13 15:52:38 -0800401}
402
403static void
404application_api_table_del (u32 api_client_index)
405{
406 hash_unset (app_main.app_by_api_client_index, api_client_index);
Florin Corascea194d2017-10-02 00:18:51 -0700407}
408
Dave Barach68b0fb02017-02-28 15:15:56 -0500409static void
Florin Corasc1f5a432018-11-20 11:31:26 -0800410application_name_table_add (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500411{
Florin Corasc1f5a432018-11-20 11:31:26 -0800412 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500413}
414
415static void
Florin Corasc1f5a432018-11-20 11:31:26 -0800416application_name_table_del (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500417{
Florin Corasc1f5a432018-11-20 11:31:26 -0800418 hash_unset_mem (app_main.app_by_name, app->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500419}
420
421application_t *
422application_lookup (u32 api_client_index)
423{
424 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700425 p = hash_get (app_main.app_by_api_client_index, api_client_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500426 if (p)
Florin Coras053a0e42018-11-13 15:52:38 -0800427 return application_get_if_valid (p[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500428
429 return 0;
430}
431
Florin Coras6cf30ad2017-04-04 23:08:23 -0700432application_t *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700433application_lookup_name (const u8 * name)
434{
435 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700436 p = hash_get_mem (app_main.app_by_name, name);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700437 if (p)
438 return application_get (p[0]);
439
440 return 0;
441}
442
Florin Coras41d5f542021-01-15 13:49:33 -0800443void
444appsl_pending_rx_mqs_add_tail (appsl_wrk_t *aw, app_rx_mq_elt_t *elt)
445{
446 app_rx_mq_elt_t *head;
447
448 if (!aw->pending_rx_mqs)
449 {
450 elt->next = elt->prev = elt;
451 aw->pending_rx_mqs = elt;
452 return;
453 }
454
455 head = aw->pending_rx_mqs;
456
457 ASSERT (head != elt);
458
459 elt->prev = head->prev;
460 elt->next = head;
461
462 head->prev->next = elt;
463 head->prev = elt;
464}
465
466void
467appsl_pending_rx_mqs_del (appsl_wrk_t *aw, app_rx_mq_elt_t *elt)
468{
469 if (elt->next == elt)
470 {
471 elt->next = elt->prev = 0;
472 aw->pending_rx_mqs = 0;
473 return;
474 }
475
476 if (elt == aw->pending_rx_mqs)
477 aw->pending_rx_mqs = elt->next;
478
479 elt->next->prev = elt->prev;
480 elt->prev->next = elt->next;
481 elt->next = elt->prev = 0;
482}
483
484vlib_node_registration_t appsl_rx_mqs_input_node;
485
486VLIB_NODE_FN (appsl_rx_mqs_input_node)
487(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
488{
489 u32 thread_index = vm->thread_index, n_msgs = 0;
490 app_rx_mq_elt_t *elt, *next;
491 app_main_t *am = &app_main;
492 session_worker_t *wrk;
493 int __clib_unused rv;
494 appsl_wrk_t *aw;
495 u64 buf;
496
497 aw = &am->wrk[thread_index];
498 elt = aw->pending_rx_mqs;
499 if (!elt)
500 return 0;
501
502 wrk = session_main_get_worker (thread_index);
503
504 do
505 {
506 if (!(elt->flags & APP_RX_MQ_F_POSTPONED))
507 rv = read (svm_msg_q_get_eventfd (elt->mq), &buf, sizeof (buf));
508 n_msgs += session_wrk_handle_mq (wrk, elt->mq);
509
510 next = elt->next;
511 appsl_pending_rx_mqs_del (aw, elt);
512 if (!svm_msg_q_is_empty (elt->mq))
513 {
514 elt->flags |= APP_RX_MQ_F_POSTPONED;
515 appsl_pending_rx_mqs_add_tail (aw, elt);
516 }
517 else
518 {
519 elt->flags = 0;
520 }
521 elt = next;
522 }
523 while (aw->pending_rx_mqs && elt != aw->pending_rx_mqs);
524
525 if (aw->pending_rx_mqs)
526 vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index);
527
Florin Coras7da88292021-03-18 15:04:34 -0700528 if (n_msgs && wrk->state == SESSION_WRK_INTERRUPT)
529 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
530
Florin Coras41d5f542021-01-15 13:49:33 -0800531 return n_msgs;
532}
533
534VLIB_REGISTER_NODE (appsl_rx_mqs_input_node) = {
535 .name = "appsl-rx-mqs-input",
536 .type = VLIB_NODE_TYPE_INPUT,
537 .state = VLIB_NODE_STATE_DISABLED,
538};
539
540static clib_error_t *
541app_rx_mq_fd_read_ready (clib_file_t *cf)
542{
543 app_rx_mq_handle_t *handle = (app_rx_mq_handle_t *) &cf->private_data;
544 vlib_main_t *vm = vlib_get_main ();
545 app_main_t *am = &app_main;
546 app_rx_mq_elt_t *mqe;
547 application_t *app;
548 appsl_wrk_t *aw;
549
550 ASSERT (vlib_get_thread_index () == handle->thread_index);
551 app = application_get_if_valid (handle->app_index);
552 if (!app)
553 return 0;
554
555 mqe = &app->rx_mqs[handle->thread_index];
556 if ((mqe->flags & APP_RX_MQ_F_PENDING) || svm_msg_q_is_empty (mqe->mq))
557 return 0;
558
559 aw = &am->wrk[handle->thread_index];
560 appsl_pending_rx_mqs_add_tail (aw, mqe);
561 mqe->flags |= APP_RX_MQ_F_PENDING;
562
563 vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index);
564
565 return 0;
566}
567
568static clib_error_t *
569app_rx_mq_fd_write_ready (clib_file_t *cf)
570{
571 clib_warning ("should not be called");
572 return 0;
573}
574
575static void
576app_rx_mqs_epoll_add (application_t *app, app_rx_mq_elt_t *mqe)
577{
578 clib_file_t template = { 0 };
579 app_rx_mq_handle_t handle;
580 u32 thread_index;
581 int fd;
582
583 thread_index = mqe - app->rx_mqs;
584 fd = svm_msg_q_get_eventfd (mqe->mq);
585
586 handle.app_index = app->app_index;
587 handle.thread_index = thread_index;
588
589 template.read_function = app_rx_mq_fd_read_ready;
590 template.write_function = app_rx_mq_fd_write_ready;
591 template.file_descriptor = fd;
592 template.private_data = handle.as_u64;
593 template.polling_thread_index = thread_index;
594 template.description =
595 format (0, "app-%u-rx-mq-%u", app->app_index, thread_index);
596 mqe->file_index = clib_file_add (&file_main, &template);
597}
598
599static void
600app_rx_mqs_epoll_del (application_t *app, app_rx_mq_elt_t *mqe)
601{
602 u32 thread_index = mqe - app->rx_mqs;
603 app_main_t *am = &app_main;
604 appsl_wrk_t *aw;
605
606 aw = &am->wrk[thread_index];
607
Florin Coras87d81ae2021-03-31 21:05:24 -0700608 session_wrk_handle_mq (session_main_get_worker (thread_index), mqe->mq);
609
Florin Coras41d5f542021-01-15 13:49:33 -0800610 if (mqe->flags & APP_RX_MQ_F_PENDING)
Florin Coras87d81ae2021-03-31 21:05:24 -0700611 appsl_pending_rx_mqs_del (aw, mqe);
Florin Coras41d5f542021-01-15 13:49:33 -0800612
613 clib_file_del_by_index (&file_main, mqe->file_index);
614}
615
616svm_msg_q_t *
617application_rx_mq_get (application_t *app, u32 mq_index)
618{
619 if (!app->rx_mqs)
620 return 0;
621
622 return app->rx_mqs[mq_index].mq;
623}
624
625static int
626app_rx_mqs_alloc (application_t *app)
627{
628 u32 evt_q_length, evt_size = sizeof (session_event_t);
629 fifo_segment_t *eqs = &app->rx_mqs_segment;
630 u32 n_mqs = vlib_num_workers () + 1;
631 segment_manager_props_t *props;
632 int i;
633
634 props = application_segment_manager_properties (app);
635 evt_q_length = clib_max (props->evt_q_size, 128);
636
637 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
638 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
639 { evt_q_length, evt_size, 0 }, { evt_q_length >> 1, 256, 0 }
640 };
641 cfg->consumer_pid = 0;
642 cfg->n_rings = 2;
643 cfg->q_nitems = evt_q_length;
644 cfg->ring_cfgs = rc;
645
Florin Corasa54b62d2021-04-21 09:05:56 -0700646 eqs->ssvm.ssvm_size = svm_msg_q_size_to_alloc (cfg) * n_mqs + (1 << 20);
Xiaoming Jiang3d3dc292021-10-13 03:11:40 +0000647 eqs->ssvm.name = format (0, "%v-rx-mqs-seg%c", app->name, 0);
Florin Coras41d5f542021-01-15 13:49:33 -0800648
649 if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
650 {
651 clib_warning ("failed to initialize queue segment");
652 return SESSION_E_SEG_CREATE;
653 }
654
655 fifo_segment_init (eqs);
656
657 /* Fifo segment filled only with mqs */
658 eqs->h->n_mqs = n_mqs;
659 vec_validate (app->rx_mqs, n_mqs - 1);
660
661 for (i = 0; i < n_mqs; i++)
662 {
663 app->rx_mqs[i].mq = fifo_segment_msg_q_alloc (eqs, i, cfg);
664 if (svm_msg_q_alloc_eventfd (app->rx_mqs[i].mq))
665 {
666 clib_warning ("eventfd returned");
667 fifo_segment_cleanup (eqs);
668 ssvm_delete (&eqs->ssvm);
669 return SESSION_E_EVENTFD_ALLOC;
670 }
671 app_rx_mqs_epoll_add (app, &app->rx_mqs[i]);
672 app->rx_mqs[i].app_index = app->app_index;
673 }
674
675 return 0;
676}
677
678u8
679application_use_private_rx_mqs (void)
680{
681 return session_main.use_private_rx_mqs;
682}
683
684fifo_segment_t *
685application_get_rx_mqs_segment (application_t *app)
686{
687 if (application_use_private_rx_mqs ())
688 return &app->rx_mqs_segment;
Florin Coras8afe1b82021-11-17 23:38:54 -0800689 return session_main_get_wrk_mqs_segment ();
Florin Coras41d5f542021-01-15 13:49:33 -0800690}
691
692void
693application_enable_rx_mqs_nodes (u8 is_en)
694{
695 u8 state = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
696
697 foreach_vlib_main ()
698 vlib_node_set_state (this_vlib_main, appsl_rx_mqs_input_node.index, state);
699}
700
Florin Corasc1a42652019-02-08 18:27:29 -0800701static application_t *
Florin Coras15531972018-08-12 23:50:53 -0700702application_alloc (void)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700703{
704 application_t *app;
Florin Coras15531972018-08-12 23:50:53 -0700705 pool_get (app_main.app_pool, app);
Dave Barachb7b92992018-10-17 10:38:51 -0400706 clib_memset (app, 0, sizeof (*app));
Florin Coras15531972018-08-12 23:50:53 -0700707 app->app_index = app - app_main.app_pool;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700708 return app;
709}
710
Florin Coras15531972018-08-12 23:50:53 -0700711application_t *
712application_get (u32 app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500713{
Florin Coras15531972018-08-12 23:50:53 -0700714 if (app_index == APP_INVALID_INDEX)
715 return 0;
716 return pool_elt_at_index (app_main.app_pool, app_index);
717}
Dave Barach68b0fb02017-02-28 15:15:56 -0500718
Florin Coras15531972018-08-12 23:50:53 -0700719application_t *
720application_get_if_valid (u32 app_index)
721{
722 if (pool_is_free_index (app_main.app_pool, app_index))
723 return 0;
Florin Corasa5464812017-04-19 13:00:05 -0700724
Florin Coras15531972018-08-12 23:50:53 -0700725 return pool_elt_at_index (app_main.app_pool, app_index);
726}
Florin Coras7999e832017-10-31 01:51:04 -0700727
Florin Corasd79b41e2017-03-04 05:37:52 -0800728static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700729application_verify_cb_fns (session_cb_vft_t * cb_fns)
Florin Corasd79b41e2017-03-04 05:37:52 -0800730{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700731 if (cb_fns->session_accept_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800732 clib_warning ("No accept callback function provided");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700733 if (cb_fns->session_connected_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800734 clib_warning ("No session connected callback function provided");
735 if (cb_fns->session_disconnect_callback == 0)
736 clib_warning ("No session disconnect callback function provided");
737 if (cb_fns->session_reset_callback == 0)
738 clib_warning ("No session reset callback function provided");
739}
740
Florin Corasb384b542018-01-15 01:08:33 -0800741/**
742 * Check app config for given segment type
743 *
744 * Returns 1 on success and 0 otherwise
745 */
746static u8
747application_verify_cfg (ssvm_segment_type_t st)
748{
749 u8 is_valid;
750 if (st == SSVM_SEGMENT_MEMFD)
751 {
Florin Coras8afe1b82021-11-17 23:38:54 -0800752 is_valid = (session_main_get_wrk_mqs_segment () != 0);
Florin Corasb384b542018-01-15 01:08:33 -0800753 if (!is_valid)
754 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
755 return is_valid;
756 }
757 else if (st == SSVM_SEGMENT_SHM)
758 {
Florin Coras8afe1b82021-11-17 23:38:54 -0800759 is_valid = (session_main_get_wrk_mqs_segment () == 0);
Florin Corasb384b542018-01-15 01:08:33 -0800760 if (!is_valid)
761 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
762 return is_valid;
763 }
764 else
765 return 1;
766}
767
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200768static session_error_t
769application_alloc_and_init (app_init_args_t *a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500770{
Florin Corasa332c462018-01-31 06:52:17 -0800771 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
Florin Coras88001c62019-04-24 14:44:46 -0700772 segment_manager_props_t *props;
Florin Coras15531972018-08-12 23:50:53 -0700773 application_t *app;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700774 u64 *opts;
Dave Barach68b0fb02017-02-28 15:15:56 -0500775
Florin Coras15531972018-08-12 23:50:53 -0700776 app = application_alloc ();
Florin Corasbbc8fae2021-07-19 15:23:51 -0700777 opts = a->options;
Florin Corasb384b542018-01-15 01:08:33 -0800778 /*
779 * Make sure we support the requested configuration
780 */
Florin Corasbbc8fae2021-07-19 15:23:51 -0700781 if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN) &&
782 !(opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_MEMFD_FOR_BUILTIN))
Florin Coras6c10ab22020-11-08 16:50:39 -0800783 seg_type = SSVM_SEGMENT_PRIVATE;
Florin Corasb384b542018-01-15 01:08:33 -0800784
Florin Corasbbc8fae2021-07-19 15:23:51 -0700785 if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) &&
786 seg_type != SSVM_SEGMENT_MEMFD)
Florin Corasd8402ae2019-03-03 16:46:52 -0800787 {
788 clib_warning ("mq eventfds can only be used if socket transport is "
789 "used for binary api");
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200790 return SESSION_E_NOSUPPORT;
Florin Corasd8402ae2019-03-03 16:46:52 -0800791 }
792
Florin Corasa332c462018-01-31 06:52:17 -0800793 if (!application_verify_cfg (seg_type))
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200794 return SESSION_E_NOSUPPORT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500795
Florin Corasbbc8fae2021-07-19 15:23:51 -0700796 if (opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS] &&
797 opts[APP_OPTIONS_PREALLOC_FIFO_HDRS])
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200798 return SESSION_E_NOSUPPORT;
Florin Coras9845c202020-04-28 01:54:22 +0000799
Florin Coras15531972018-08-12 23:50:53 -0700800 /* Check that the obvious things are properly set up */
801 application_verify_cb_fns (a->session_cb_vft);
802
Florin Corasbbc8fae2021-07-19 15:23:51 -0700803 app->flags = opts[APP_OPTIONS_FLAGS];
Florin Coras15531972018-08-12 23:50:53 -0700804 app->cb_fns = *a->session_cb_vft;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700805 app->ns_index = opts[APP_OPTIONS_NAMESPACE];
806 app->proxied_transports = opts[APP_OPTIONS_PROXY_TRANSPORT];
Florin Coras15531972018-08-12 23:50:53 -0700807 app->name = vec_dup (a->name);
808
809 /* If no scope enabled, default to global */
810 if (!application_has_global_scope (app)
811 && !application_has_local_scope (app))
812 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
813
Florin Corasa332c462018-01-31 06:52:17 -0800814 props = application_segment_manager_properties (app);
Florin Coras88001c62019-04-24 14:44:46 -0700815 segment_manager_props_init (props);
Florin Corasbbc8fae2021-07-19 15:23:51 -0700816 props->segment_size = opts[APP_OPTIONS_SEGMENT_SIZE];
817 props->prealloc_fifos = opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
818 props->prealloc_fifo_hdrs = opts[APP_OPTIONS_PREALLOC_FIFO_HDRS];
819 if (opts[APP_OPTIONS_ADD_SEGMENT_SIZE])
Florin Corasb384b542018-01-15 01:08:33 -0800820 {
Florin Corasbbc8fae2021-07-19 15:23:51 -0700821 props->add_segment_size = opts[APP_OPTIONS_ADD_SEGMENT_SIZE];
Florin Corasb384b542018-01-15 01:08:33 -0800822 props->add_segment = 1;
823 }
Junfeng Wangc795b882022-08-12 16:24:46 +0800824 if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_HUGE_PAGE)
825 props->huge_page = 1;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700826 if (opts[APP_OPTIONS_RX_FIFO_SIZE])
827 props->rx_fifo_size = opts[APP_OPTIONS_RX_FIFO_SIZE];
828 if (opts[APP_OPTIONS_TX_FIFO_SIZE])
829 props->tx_fifo_size = opts[APP_OPTIONS_TX_FIFO_SIZE];
830 if (opts[APP_OPTIONS_EVT_QUEUE_SIZE])
831 props->evt_q_size = opts[APP_OPTIONS_EVT_QUEUE_SIZE];
832 if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
Florin Coras99368312018-08-02 10:45:44 -0700833 props->use_mq_eventfd = 1;
Florin Corasbbc8fae2021-07-19 15:23:51 -0700834 if (opts[APP_OPTIONS_TLS_ENGINE])
835 app->tls_engine = opts[APP_OPTIONS_TLS_ENGINE];
836 if (opts[APP_OPTIONS_MAX_FIFO_SIZE])
837 props->max_fifo_size = opts[APP_OPTIONS_MAX_FIFO_SIZE];
838 if (opts[APP_OPTIONS_HIGH_WATERMARK])
839 props->high_watermark = opts[APP_OPTIONS_HIGH_WATERMARK];
840 if (opts[APP_OPTIONS_LOW_WATERMARK])
841 props->low_watermark = opts[APP_OPTIONS_LOW_WATERMARK];
842 if (opts[APP_OPTIONS_PCT_FIRST_ALLOC])
843 props->pct_first_alloc = opts[APP_OPTIONS_PCT_FIRST_ALLOC];
Florin Corasa332c462018-01-31 06:52:17 -0800844 props->segment_type = seg_type;
Dave Barach68b0fb02017-02-28 15:15:56 -0500845
Florin Coras15531972018-08-12 23:50:53 -0700846 /* Add app to lookup by api_client_index table */
Florin Corasc1f5a432018-11-20 11:31:26 -0800847 if (!application_is_builtin (app))
848 application_api_table_add (app->app_index, a->api_client_index);
849 else
850 application_name_table_add (app);
851
852 a->app_index = app->app_index;
Florin Corasa332c462018-01-31 06:52:17 -0800853
Florin Coras15531972018-08-12 23:50:53 -0700854 APP_DBG ("New app name: %v api index: %u index %u", app->name,
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200855 a->api_client_index, app->app_index);
Florin Coras15531972018-08-12 23:50:53 -0700856
857 return 0;
858}
859
Florin Corasc1a42652019-02-08 18:27:29 -0800860static void
Florin Coras15531972018-08-12 23:50:53 -0700861application_free (application_t * app)
862{
863 app_worker_map_t *wrk_map;
864 app_worker_t *app_wrk;
865
866 /*
867 * The app event queue allocated in first segment is cleared with
868 * the segment manager. No need to explicitly free it.
869 */
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200870 APP_DBG ("Delete app name %v index: %d", app->name, app->app_index);
Florin Coras15531972018-08-12 23:50:53 -0700871
872 if (application_is_proxy (app))
873 application_remove_proxy (app);
874
Florin Corasab2f6db2018-08-31 14:31:41 -0700875 /*
876 * Free workers
877 */
878
Florin Coras15531972018-08-12 23:50:53 -0700879 /* *INDENT-OFF* */
880 pool_flush (wrk_map, app->worker_maps, ({
881 app_wrk = app_worker_get (wrk_map->wrk_index);
882 app_worker_free (app_wrk);
883 }));
884 /* *INDENT-ON* */
885 pool_free (app->worker_maps);
886
Florin Corasab2f6db2018-08-31 14:31:41 -0700887 /*
Florin Coras41d5f542021-01-15 13:49:33 -0800888 * Free rx mqs if allocated
889 */
890 if (app->rx_mqs)
891 {
892 int i;
893 for (i = 0; i < vec_len (app->rx_mqs); i++)
894 app_rx_mqs_epoll_del (app, &app->rx_mqs[i]);
895
896 fifo_segment_cleanup (&app->rx_mqs_segment);
897 ssvm_delete (&app->rx_mqs_segment.ssvm);
898 vec_free (app->rx_mqs);
899 }
900
901 /*
Florin Corasab2f6db2018-08-31 14:31:41 -0700902 * Cleanup remaining state
903 */
Florin Corasc1f5a432018-11-20 11:31:26 -0800904 if (application_is_builtin (app))
905 application_name_table_del (app);
Florin Coras15531972018-08-12 23:50:53 -0700906 vec_free (app->name);
Florin Coras15531972018-08-12 23:50:53 -0700907 pool_put (app_main.app_pool, app);
908}
909
Florin Corasc1a42652019-02-08 18:27:29 -0800910static void
Florin Coras053a0e42018-11-13 15:52:38 -0800911application_detach_process (application_t * app, u32 api_client_index)
912{
913 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
914 app_worker_map_t *wrk_map;
915 u32 *wrks = 0, *wrk_index;
916 app_worker_t *app_wrk;
917
918 if (api_client_index == ~0)
919 {
920 application_free (app);
921 return;
922 }
923
924 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200925 app->app_index, api_client_index);
Florin Coras053a0e42018-11-13 15:52:38 -0800926
927 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100928 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras053a0e42018-11-13 15:52:38 -0800929 app_wrk = app_worker_get (wrk_map->wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800930 if (app_wrk->api_client_index == api_client_index)
Florin Coras053a0e42018-11-13 15:52:38 -0800931 vec_add1 (wrks, app_wrk->wrk_index);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100932 }
Florin Coras053a0e42018-11-13 15:52:38 -0800933 /* *INDENT-ON* */
934
935 if (!vec_len (wrks))
936 {
937 clib_warning ("no workers for app %u api_index %u", app->app_index,
938 api_client_index);
939 return;
940 }
941
942 args->app_index = app->app_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800943 args->api_client_index = api_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800944 vec_foreach (wrk_index, wrks)
945 {
946 app_wrk = app_worker_get (wrk_index[0]);
Florin Coras349f8ca2018-11-20 16:52:49 -0800947 args->wrk_map_index = app_wrk->wrk_map_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800948 args->is_add = 0;
949 vnet_app_worker_add_del (args);
950 }
951 vec_free (wrks);
952}
953
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200954void
955application_namespace_cleanup (app_namespace_t *app_ns)
956{
957 u32 *app_indices = 0, *app_index;
958 application_t *app;
959 u32 ns_index;
960
961 ns_index = app_namespace_index (app_ns);
962 pool_foreach (app, app_main.app_pool)
963 if (app->ns_index == ns_index)
964 vec_add1 (app_indices, app->ns_index);
965
966 vec_foreach (app_index, app_indices)
967 {
968 app = application_get (*app_index);
969
970 if (application_is_proxy (app))
971 application_remove_proxy (app);
972 app->flags &= ~APP_OPTIONS_FLAGS_IS_PROXY;
973
974 application_free (app);
975 }
976 vec_free (app_indices);
977}
978
Florin Coras15531972018-08-12 23:50:53 -0700979app_worker_t *
980application_get_worker (application_t * app, u32 wrk_map_index)
981{
982 app_worker_map_t *map;
983 map = app_worker_map_get (app, wrk_map_index);
984 if (!map)
985 return 0;
986 return app_worker_get (map->wrk_index);
987}
988
989app_worker_t *
990application_get_default_worker (application_t * app)
991{
992 return application_get_worker (app, 0);
993}
994
Florin Coras053a0e42018-11-13 15:52:38 -0800995u32
996application_n_workers (application_t * app)
997{
998 return pool_elts (app->worker_maps);
999}
1000
Florin Coras15531972018-08-12 23:50:53 -07001001app_worker_t *
Florin Corasc9940fc2019-02-05 20:55:11 -08001002application_listener_select_worker (session_t * ls)
Florin Corasab2f6db2018-08-31 14:31:41 -07001003{
Florin Coras11e2cf52019-03-06 12:04:24 -08001004 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -08001005 app_listener_t *al;
Florin Corasab2f6db2018-08-31 14:31:41 -07001006
Florin Coras11e2cf52019-03-06 12:04:24 -08001007 app = application_get (ls->app_index);
1008 al = app_listener_get (app, ls->al_index);
1009 return app_listener_select_worker (app, al);
Florin Corasab2f6db2018-08-31 14:31:41 -07001010}
1011
Florin Coras15531972018-08-12 23:50:53 -07001012int
Florin Coras623eb562019-02-03 19:28:34 -08001013application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
Florin Coras15531972018-08-12 23:50:53 -07001014{
1015 app_worker_map_t *wrk_map;
1016 app_worker_t *app_wrk;
1017 segment_manager_t *sm;
1018 int rv;
1019
1020 app_wrk = app_worker_alloc (app);
1021 wrk_map = app_worker_map_alloc (app);
1022 wrk_map->wrk_index = app_wrk->wrk_index;
1023 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
1024
1025 /*
1026 * Setup first segment manager
1027 */
Florin Coras88001c62019-04-24 14:44:46 -07001028 sm = segment_manager_alloc ();
Florin Coras15531972018-08-12 23:50:53 -07001029 sm->app_wrk_index = app_wrk->wrk_index;
1030
Florin Corasa107f402020-09-29 19:18:46 -07001031 if ((rv = segment_manager_init_first (sm)))
Florin Coras15531972018-08-12 23:50:53 -07001032 {
1033 app_worker_free (app_wrk);
1034 return rv;
1035 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001036 sm->first_is_protected = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001037
Florin Corascea194d2017-10-02 00:18:51 -07001038 /*
Florin Coras15531972018-08-12 23:50:53 -07001039 * Setup app worker
Florin Corascea194d2017-10-02 00:18:51 -07001040 */
Florin Coras94a6df02021-05-06 15:32:14 -07001041 app_wrk->connects_seg_manager = segment_manager_index (sm);
Florin Coras15531972018-08-12 23:50:53 -07001042 app_wrk->listeners_table = hash_create (0, sizeof (u64));
1043 app_wrk->event_queue = segment_manager_event_queue (sm);
1044 app_wrk->app_is_builtin = application_is_builtin (app);
Dave Barach68b0fb02017-02-28 15:15:56 -05001045
Florin Coras15531972018-08-12 23:50:53 -07001046 *wrk = app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001047
Florin Coras6cf30ad2017-04-04 23:08:23 -07001048 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001049}
1050
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001051session_error_t
1052vnet_app_worker_add_del (vnet_app_worker_add_del_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001053{
Florin Coras88001c62019-04-24 14:44:46 -07001054 fifo_segment_t *fs;
Florin Corasc1a42652019-02-08 18:27:29 -08001055 app_worker_map_t *wrk_map;
1056 app_worker_t *app_wrk;
1057 segment_manager_t *sm;
1058 application_t *app;
1059 int rv;
1060
1061 app = application_get (a->app_index);
1062 if (!app)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001063 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001064
1065 if (a->is_add)
1066 {
1067 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
1068 return rv;
1069
1070 /* Map worker api index to the app */
1071 app_wrk->api_client_index = a->api_client_index;
1072 application_api_table_add (app->app_index, a->api_client_index);
1073
Florin Coras94a6df02021-05-06 15:32:14 -07001074 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Corasc1a42652019-02-08 18:27:29 -08001075 fs = segment_manager_get_segment_w_lock (sm, 0);
1076 a->segment = &fs->ssvm;
1077 a->segment_handle = segment_manager_segment_handle (sm, fs);
1078 segment_manager_segment_reader_unlock (sm);
1079 a->evt_q = app_wrk->event_queue;
1080 a->wrk_map_index = app_wrk->wrk_map_index;
1081 }
1082 else
1083 {
1084 wrk_map = app_worker_map_get (app, a->wrk_map_index);
1085 if (!wrk_map)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001086 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001087
1088 app_wrk = app_worker_get (wrk_map->wrk_index);
1089 if (!app_wrk)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001090 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001091
1092 application_api_table_del (app_wrk->api_client_index);
Florin Coras98078ab2021-08-12 18:12:09 -07001093 if (appns_sapi_enabled ())
1094 sapi_socket_close_w_handle (app_wrk->api_client_index);
Florin Corasc1a42652019-02-08 18:27:29 -08001095 app_worker_free (app_wrk);
1096 app_worker_map_free (app, wrk_map);
1097 if (application_n_workers (app) == 0)
1098 application_free (app);
1099 }
1100 return 0;
1101}
1102
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001103static session_error_t
1104app_validate_namespace (u8 *namespace_id, u64 secret, u32 *app_ns_index)
Florin Corasc1a42652019-02-08 18:27:29 -08001105{
1106 app_namespace_t *app_ns;
1107 if (vec_len (namespace_id) == 0)
1108 {
1109 /* Use default namespace */
1110 *app_ns_index = 0;
1111 return 0;
1112 }
1113
1114 *app_ns_index = app_namespace_index_from_id (namespace_id);
1115 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001116 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001117 app_ns = app_namespace_get (*app_ns_index);
1118 if (!app_ns)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001119 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001120 if (app_ns->ns_secret != secret)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001121 return SESSION_E_WRONG_NS_SECRET;
Florin Corasc1a42652019-02-08 18:27:29 -08001122 return 0;
1123}
1124
1125static u8 *
1126app_name_from_api_index (u32 api_client_index)
1127{
1128 vl_api_registration_t *regp;
1129 regp = vl_api_client_index_to_registration (api_client_index);
1130 if (regp)
Florin Corasbee97682019-04-09 16:13:18 -07001131 return format (0, "%s", regp->name);
Florin Corasc1a42652019-02-08 18:27:29 -08001132
1133 clib_warning ("api client index %u does not have an api registration!",
1134 api_client_index);
Florin Corasbee97682019-04-09 16:13:18 -07001135 return format (0, "unknown");
Florin Corasc1a42652019-02-08 18:27:29 -08001136}
1137
1138/**
1139 * Attach application to vpp
1140 *
1141 * Allocates a vpp app, i.e., a structure that keeps back pointers
1142 * to external app and a segment manager for shared memory fifo based
1143 * communication with the external app.
1144 */
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001145session_error_t
1146vnet_application_attach (vnet_app_attach_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001147{
Florin Coras88001c62019-04-24 14:44:46 -07001148 fifo_segment_t *fs;
Florin Corasc1a42652019-02-08 18:27:29 -08001149 application_t *app = 0;
1150 app_worker_t *app_wrk;
1151 segment_manager_t *sm;
1152 u32 app_ns_index = 0;
1153 u8 *app_name = 0;
1154 u64 secret;
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001155 session_error_t rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001156
1157 if (a->api_client_index != APP_INVALID_INDEX)
1158 app = application_lookup (a->api_client_index);
1159 else if (a->name)
1160 app = application_lookup_name (a->name);
1161 else
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001162 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001163
1164 if (app)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001165 return SESSION_E_APP_ATTACHED;
Florin Corasc1a42652019-02-08 18:27:29 -08001166
Florin Coras61ae0562020-09-02 19:10:28 -07001167 /* Socket api sets the name and validates namespace prior to attach */
1168 if (!a->use_sock_api)
Florin Corasc1a42652019-02-08 18:27:29 -08001169 {
Florin Coras61ae0562020-09-02 19:10:28 -07001170 if (a->api_client_index != APP_INVALID_INDEX)
1171 {
1172 app_name = app_name_from_api_index (a->api_client_index);
1173 a->name = app_name;
1174 }
Florin Corasc1a42652019-02-08 18:27:29 -08001175
Florin Coras61ae0562020-09-02 19:10:28 -07001176 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
Florin Coras6c10ab22020-11-08 16:50:39 -08001177 if ((rv = app_validate_namespace (a->namespace_id, secret,
1178 &app_ns_index)))
Florin Coras61ae0562020-09-02 19:10:28 -07001179 return rv;
1180 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
1181 }
Florin Corasc1a42652019-02-08 18:27:29 -08001182
1183 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
1184 return rv;
1185
1186 app = application_get (a->app_index);
1187 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
1188 return rv;
1189
1190 a->app_evt_q = app_wrk->event_queue;
1191 app_wrk->api_client_index = a->api_client_index;
Florin Coras94a6df02021-05-06 15:32:14 -07001192 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Corasc1a42652019-02-08 18:27:29 -08001193 fs = segment_manager_get_segment_w_lock (sm, 0);
1194
1195 if (application_is_proxy (app))
fanyfb8b6fe42020-09-17 22:10:41 +08001196 {
1197 application_setup_proxy (app);
Florin Coras2a7c0b62020-09-28 23:40:28 -07001198 /* The segment manager pool is reallocated because a new listener
1199 * is added. Re-grab segment manager to avoid dangling reference */
Florin Coras94a6df02021-05-06 15:32:14 -07001200 sm = segment_manager_get (app_wrk->connects_seg_manager);
fanyfb8b6fe42020-09-17 22:10:41 +08001201 }
Florin Corasc1a42652019-02-08 18:27:29 -08001202
1203 ASSERT (vec_len (fs->ssvm.name) <= 128);
1204 a->segment = &fs->ssvm;
1205 a->segment_handle = segment_manager_segment_handle (sm, fs);
1206
1207 segment_manager_segment_reader_unlock (sm);
Florin Coras41d5f542021-01-15 13:49:33 -08001208
1209 if (!application_is_builtin (app) && application_use_private_rx_mqs ())
1210 rv = app_rx_mqs_alloc (app);
1211
Florin Corasc1a42652019-02-08 18:27:29 -08001212 vec_free (app_name);
Florin Coras41d5f542021-01-15 13:49:33 -08001213 return rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001214}
1215
1216/**
1217 * Detach application from vpp
1218 */
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001219session_error_t
1220vnet_application_detach (vnet_app_detach_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001221{
1222 application_t *app;
1223
1224 app = application_get_if_valid (a->app_index);
1225 if (!app)
1226 {
1227 clib_warning ("app not attached");
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001228 return SESSION_E_NOAPP;
Florin Corasc1a42652019-02-08 18:27:29 -08001229 }
1230
1231 app_interface_check_thread_and_barrier (vnet_application_detach, a);
1232 application_detach_process (app, a->api_client_index);
1233 return 0;
1234}
1235
Florin Corasc1a42652019-02-08 18:27:29 -08001236static u8
Florin Coras8c2bdf82022-04-15 12:37:48 -07001237session_endpoint_in_ns (session_endpoint_cfg_t *sep)
Florin Corasc1a42652019-02-08 18:27:29 -08001238{
Florin Coras8c2bdf82022-04-15 12:37:48 -07001239 u8 is_lep;
1240
1241 if (sep->flags & SESSION_ENDPT_CFG_F_PROXY_LISTEN)
1242 return 1;
1243
1244 is_lep = session_endpoint_is_local ((session_endpoint_t *) sep);
Florin Corasc1a42652019-02-08 18:27:29 -08001245 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
1246 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
1247 {
1248 clib_warning ("sw_if_index %u not configured with ip %U",
1249 sep->sw_if_index, format_ip46_address, &sep->ip,
1250 sep->is_ip4);
1251 return 0;
1252 }
Florin Coras8c2bdf82022-04-15 12:37:48 -07001253
Florin Corasc1a42652019-02-08 18:27:29 -08001254 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
1255}
1256
1257static void
1258session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
1259 application_t * app, u8 is_connect)
1260{
1261 app_namespace_t *app_ns;
1262 u32 ns_index, fib_index;
1263
1264 ns_index = app->ns_index;
1265
1266 /* App is a transport proto, so fetch the calling app's ns */
1267 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
Florin Coras8a140612019-02-18 22:39:39 -08001268 ns_index = sep->ns_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001269
Florin Corasc1a42652019-02-08 18:27:29 -08001270 app_ns = app_namespace_get (ns_index);
1271 if (!app_ns)
1272 return;
1273
1274 /* Ask transport and network to bind to/connect using local interface
1275 * that "supports" app's namespace. This will fix our local connection
1276 * endpoint.
1277 */
1278
1279 /* If in default namespace and user requested a fib index use it */
1280 if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
1281 fib_index = sep->fib_index;
1282 else
1283 fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1284 sep->peer.fib_index = fib_index;
1285 sep->fib_index = fib_index;
1286
1287 if (!is_connect)
1288 {
1289 sep->sw_if_index = app_ns->sw_if_index;
1290 }
1291 else
1292 {
1293 if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
1294 && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
1295 && sep->peer.sw_if_index != app_ns->sw_if_index)
1296 clib_warning ("Local sw_if_index different from app ns sw_if_index");
1297
1298 sep->peer.sw_if_index = app_ns->sw_if_index;
1299 }
1300}
1301
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001302session_error_t
1303vnet_listen (vnet_listen_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001304{
1305 app_listener_t *app_listener;
1306 app_worker_t *app_wrk;
1307 application_t *app;
1308 int rv;
1309
Florin Coras458089b2019-08-21 16:20:44 -07001310 ASSERT (vlib_thread_is_main_w_barrier ());
1311
Florin Corasc1a42652019-02-08 18:27:29 -08001312 app = application_get_if_valid (a->app_index);
1313 if (!app)
Florin Corasa8c3b862020-04-07 22:31:06 +00001314 return SESSION_E_NOAPP;
Florin Corasc1a42652019-02-08 18:27:29 -08001315
1316 app_wrk = application_get_worker (app, a->wrk_map_index);
1317 if (!app_wrk)
Florin Corasa8c3b862020-04-07 22:31:06 +00001318 return SESSION_E_INVALID_APPWRK;
Florin Corasc1a42652019-02-08 18:27:29 -08001319
1320 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
1321
1322 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
Florin Coras8c2bdf82022-04-15 12:37:48 -07001323 if (!session_endpoint_in_ns (&a->sep_ext))
Florin Corasa8c3b862020-04-07 22:31:06 +00001324 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001325
1326 /*
1327 * Check if we already have an app listener
1328 */
1329 app_listener = app_listener_lookup (app, &a->sep_ext);
1330 if (app_listener)
1331 {
1332 if (app_listener->app_index != app->app_index)
Florin Corasa8c3b862020-04-07 22:31:06 +00001333 return SESSION_E_ALREADY_LISTENING;
1334 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1335 return rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001336 a->handle = app_listener_handle (app_listener);
1337 return 0;
1338 }
1339
1340 /*
1341 * Create new app listener
1342 */
1343 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1344 return rv;
1345
1346 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1347 {
1348 app_listener_cleanup (app_listener);
1349 return rv;
1350 }
1351
1352 a->handle = app_listener_handle (app_listener);
1353 return 0;
1354}
1355
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001356session_error_t
1357vnet_connect (vnet_connect_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001358{
Florin Coras2b81e3c2019-02-27 07:55:46 -08001359 app_worker_t *client_wrk;
Florin Corasc1a42652019-02-08 18:27:29 -08001360 application_t *client;
Florin Corasc1a42652019-02-08 18:27:29 -08001361
Florin Coras309f7aa2022-03-18 08:33:08 -07001362 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Coras458089b2019-08-21 16:20:44 -07001363
Florin Corasc1a42652019-02-08 18:27:29 -08001364 if (session_endpoint_is_zero (&a->sep))
Florin Coras00e01d32019-10-21 16:07:46 -07001365 return SESSION_E_INVALID_RMT_IP;
Florin Corasc1a42652019-02-08 18:27:29 -08001366
1367 client = application_get (a->app_index);
1368 session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1369 client_wrk = application_get_worker (client, a->wrk_map_index);
1370
Florin Coras89a9f612021-05-11 11:55:07 -07001371 a->sep_ext.opaque = a->api_context;
1372
Florin Corasc1a42652019-02-08 18:27:29 -08001373 /*
1374 * First check the local scope for locally attached destinations.
1375 * If we have local scope, we pass *all* connects through it since we may
1376 * have special policy rules even for non-local destinations, think proxy.
1377 */
1378 if (application_has_local_scope (client))
1379 {
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001380 session_error_t rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001381
Florin Coras2b81e3c2019-02-27 07:55:46 -08001382 a->sep_ext.original_tp = a->sep_ext.transport_proto;
1383 a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE;
Florin Coras89a9f612021-05-11 11:55:07 -07001384 rv = app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh);
Florin Coras00e01d32019-10-21 16:07:46 -07001385 a->sep_ext.transport_proto = a->sep_ext.original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -07001386 if (!rv || rv != SESSION_E_LOCAL_CONNECT)
1387 return rv;
Florin Corasc1a42652019-02-08 18:27:29 -08001388 }
Florin Corasc1a42652019-02-08 18:27:29 -08001389 /*
1390 * Not connecting to a local server, propagate to transport
1391 */
Florin Coras89a9f612021-05-11 11:55:07 -07001392 return app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh);
Florin Corasc1a42652019-02-08 18:27:29 -08001393}
1394
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001395session_error_t
1396vnet_unlisten (vnet_unlisten_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001397{
1398 app_worker_t *app_wrk;
1399 app_listener_t *al;
1400 application_t *app;
1401
Florin Coras458089b2019-08-21 16:20:44 -07001402 ASSERT (vlib_thread_is_main_w_barrier ());
1403
Florin Corasc1a42652019-02-08 18:27:29 -08001404 if (!(app = application_get_if_valid (a->app_index)))
Florin Corasa8c3b862020-04-07 22:31:06 +00001405 return SESSION_E_NOAPP;
Florin Corasc1a42652019-02-08 18:27:29 -08001406
Florin Coras92311f62019-03-01 19:26:31 -08001407 if (!(al = app_listener_get_w_handle (a->handle)))
Florin Corasa8c3b862020-04-07 22:31:06 +00001408 return SESSION_E_NOLISTEN;
Florin Coras92311f62019-03-01 19:26:31 -08001409
Florin Corasc1a42652019-02-08 18:27:29 -08001410 if (al->app_index != app->app_index)
1411 {
1412 clib_warning ("app doesn't own handle %llu!", a->handle);
Florin Corasa8c3b862020-04-07 22:31:06 +00001413 return SESSION_E_OWNER;
Florin Corasc1a42652019-02-08 18:27:29 -08001414 }
1415
1416 app_wrk = application_get_worker (app, a->wrk_map_index);
1417 if (!app_wrk)
1418 {
1419 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001420 return SESSION_E_INVALID_APPWRK;
Florin Corasc1a42652019-02-08 18:27:29 -08001421 }
1422
1423 return app_worker_stop_listen (app_wrk, al);
1424}
1425
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001426session_error_t
liuyacan534468e2021-05-09 03:50:40 +00001427vnet_shutdown_session (vnet_shutdown_args_t *a)
1428{
1429 app_worker_t *app_wrk;
1430 session_t *s;
1431
1432 s = session_get_from_handle_if_valid (a->handle);
1433 if (!s)
1434 return SESSION_E_NOSESSION;
1435
1436 app_wrk = app_worker_get (s->app_wrk_index);
1437 if (app_wrk->app_index != a->app_index)
1438 return SESSION_E_OWNER;
1439
1440 /* We're peeking into another's thread pool. Make sure */
1441 ASSERT (s->session_index == session_index_from_handle (a->handle));
1442
1443 session_half_close (s);
1444 return 0;
1445}
1446
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001447session_error_t
1448vnet_disconnect_session (vnet_disconnect_args_t *a)
Florin Corasc1a42652019-02-08 18:27:29 -08001449{
Florin Coras2b81e3c2019-02-27 07:55:46 -08001450 app_worker_t *app_wrk;
1451 session_t *s;
Florin Corasc1a42652019-02-08 18:27:29 -08001452
Florin Coras2b81e3c2019-02-27 07:55:46 -08001453 s = session_get_from_handle_if_valid (a->handle);
1454 if (!s)
Florin Corasa8c3b862020-04-07 22:31:06 +00001455 return SESSION_E_NOSESSION;
1456
Florin Coras2b81e3c2019-02-27 07:55:46 -08001457 app_wrk = app_worker_get (s->app_wrk_index);
1458 if (app_wrk->app_index != a->app_index)
Florin Corasa8c3b862020-04-07 22:31:06 +00001459 return SESSION_E_OWNER;
Florin Corasc1a42652019-02-08 18:27:29 -08001460
Florin Coras2b81e3c2019-02-27 07:55:46 -08001461 /* We're peeking into another's thread pool. Make sure */
1462 ASSERT (s->session_index == session_index_from_handle (a->handle));
Florin Corasc1a42652019-02-08 18:27:29 -08001463
Florin Coras2b81e3c2019-02-27 07:55:46 -08001464 session_close (s);
Florin Corasc1a42652019-02-08 18:27:29 -08001465 return 0;
1466}
1467
1468int
Florin Coras623eb562019-02-03 19:28:34 -08001469application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1470{
1471 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1472 app_listener_t *app_listener;
1473 application_t *app;
Florin Corasa8c3b862020-04-07 22:31:06 +00001474 int rv;
Florin Coras623eb562019-02-03 19:28:34 -08001475
1476 if (!old_wrk)
Florin Corasa8c3b862020-04-07 22:31:06 +00001477 return SESSION_E_INVALID_APPWRK;
Florin Coras623eb562019-02-03 19:28:34 -08001478
1479 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1480 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1481 && s->rx_fifo)
Florin Coras19223e02019-03-03 14:56:05 -08001482 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras623eb562019-02-03 19:28:34 -08001483
Florin Coras623eb562019-02-03 19:28:34 -08001484 app = application_get (old_wrk->app_index);
1485 if (!app)
Florin Corasa8c3b862020-04-07 22:31:06 +00001486 return SESSION_E_NOAPP;
Florin Coras623eb562019-02-03 19:28:34 -08001487
Florin Corasc9940fc2019-02-05 20:55:11 -08001488 app_listener = app_listener_get (app, s->al_index);
1489
1490 /* Only remove from lb for now */
Florin Coras623eb562019-02-03 19:28:34 -08001491 app_listener->workers = clib_bitmap_set (app_listener->workers,
1492 old_wrk->wrk_map_index, 0);
Florin Coras623eb562019-02-03 19:28:34 -08001493
Florin Corasa8c3b862020-04-07 22:31:06 +00001494 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1495 return rv;
Florin Corasab2f6db2018-08-31 14:31:41 -07001496
Florin Corasc9940fc2019-02-05 20:55:11 -08001497 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001498
Dave Barach68b0fb02017-02-28 15:15:56 -05001499 return 0;
1500}
1501
Dave Barach52851e62017-08-07 09:35:25 -04001502int
1503application_is_proxy (application_t * app)
1504{
Florin Coras7999e832017-10-31 01:51:04 -07001505 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1506}
1507
1508int
1509application_is_builtin (application_t * app)
1510{
1511 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1512}
1513
1514int
1515application_is_builtin_proxy (application_t * app)
1516{
1517 return (application_is_proxy (app) && application_is_builtin (app));
Dave Barach52851e62017-08-07 09:35:25 -04001518}
1519
Florin Corascea194d2017-10-02 00:18:51 -07001520u8
1521application_has_local_scope (application_t * app)
1522{
1523 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1524}
1525
1526u8
1527application_has_global_scope (application_t * app)
1528{
1529 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1530}
1531
qinyangaf9b7152023-06-27 01:11:53 -07001532int
1533application_original_dst_is_enabled (application_t *app)
1534{
1535 return app->flags & APP_OPTIONS_FLAGS_GET_ORIGINAL_DST;
1536}
1537
Florin Coras7999e832017-10-31 01:51:04 -07001538static clib_error_t *
1539application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1540 u8 transport_proto, u8 is_start)
1541{
Florin Coras7999e832017-10-31 01:51:04 -07001542 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1543 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
Florin Coras5665ced2018-10-25 18:03:45 -07001544 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Florin Coras7999e832017-10-31 01:51:04 -07001545 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -07001546 app_worker_t *app_wrk;
Florin Corasc9940fc2019-02-05 20:55:11 -08001547 app_listener_t *al;
Florin Coras288eaab2019-02-03 15:26:14 -08001548 session_t *s;
Florin Corasc9940fc2019-02-05 20:55:11 -08001549 u32 flags;
Florin Coras7999e832017-10-31 01:51:04 -07001550
Florin Coras15531972018-08-12 23:50:53 -07001551 /* TODO decide if we want proxy to be enabled for all workers */
1552 app_wrk = application_get_default_worker (app);
Florin Coras7999e832017-10-31 01:51:04 -07001553 if (is_start)
1554 {
Florin Coras15531972018-08-12 23:50:53 -07001555 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001556 if (!s)
1557 {
1558 sep.is_ip4 = is_ip4;
1559 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1560 sep.sw_if_index = app_ns->sw_if_index;
1561 sep.transport_proto = transport_proto;
Florin Corasab2f6db2018-08-31 14:31:41 -07001562 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
Florin Corasc9940fc2019-02-05 20:55:11 -08001563
1564 /* force global scope listener */
1565 flags = app->flags;
1566 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1567 app_listener_alloc_and_init (app, &sep, &al);
1568 app->flags = flags;
1569
1570 app_worker_start_listen (app_wrk, al);
1571 s = listen_session_get (al->session_index);
Florin Corasd5c604d2019-03-18 09:06:35 -07001572 s->flags |= SESSION_F_PROXY;
Florin Coras19b1f6a2017-12-11 03:37:03 -08001573 }
Florin Coras7999e832017-10-31 01:51:04 -07001574 }
1575 else
1576 {
Florin Coras623eb562019-02-03 19:28:34 -08001577 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001578 ASSERT (s);
Florin Coras7999e832017-10-31 01:51:04 -07001579 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001580
Florin Coras7999e832017-10-31 01:51:04 -07001581 tc = listen_session_get_transport (s);
1582
1583 if (!ip_is_zero (&tc->lcl_ip, 1))
1584 {
Florin Corasdbd44562017-11-09 19:30:17 -08001585 u32 sti;
1586 sep.is_ip4 = is_ip4;
1587 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1588 sep.transport_proto = transport_proto;
1589 sep.port = 0;
1590 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001591 if (is_start)
Florin Corasab2f6db2018-08-31 14:31:41 -07001592 session_lookup_add_session_endpoint (sti,
1593 (session_endpoint_t *) & sep,
1594 s->session_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001595 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001596 session_lookup_del_session_endpoint (sti,
1597 (session_endpoint_t *) & sep);
Florin Coras7999e832017-10-31 01:51:04 -07001598 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001599
Florin Coras7999e832017-10-31 01:51:04 -07001600 return 0;
1601}
1602
Florin Coras19b1f6a2017-12-11 03:37:03 -08001603static void
1604application_start_stop_proxy_local_scope (application_t * app,
1605 u8 transport_proto, u8 is_start)
1606{
1607 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1608 app_namespace_t *app_ns;
1609 app_ns = app_namespace_get (app->ns_index);
1610 sep.is_ip4 = 1;
1611 sep.transport_proto = transport_proto;
1612 sep.port = 0;
1613
1614 if (is_start)
1615 {
1616 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001617 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001618 sep.is_ip4 = 0;
1619 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001620 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001621 }
1622 else
1623 {
1624 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1625 sep.is_ip4 = 0;
1626 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1627 }
1628}
1629
Florin Coras7999e832017-10-31 01:51:04 -07001630void
Florin Coras561af9b2017-12-09 10:19:43 -08001631application_start_stop_proxy (application_t * app,
1632 transport_proto_t transport_proto, u8 is_start)
Florin Coras7999e832017-10-31 01:51:04 -07001633{
Florin Coras7999e832017-10-31 01:51:04 -07001634 if (application_has_local_scope (app))
Florin Coras19b1f6a2017-12-11 03:37:03 -08001635 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
Florin Coras7999e832017-10-31 01:51:04 -07001636
1637 if (application_has_global_scope (app))
1638 {
1639 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1640 transport_proto, is_start);
1641 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1642 transport_proto, is_start);
1643 }
1644}
1645
1646void
1647application_setup_proxy (application_t * app)
1648{
1649 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001650 transport_proto_t tp;
1651
Florin Coras7999e832017-10-31 01:51:04 -07001652 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001653
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +02001654 transport_proto_foreach (tp, transports)
1655 application_start_stop_proxy (app, tp, 1);
Florin Coras7999e832017-10-31 01:51:04 -07001656}
1657
1658void
1659application_remove_proxy (application_t * app)
1660{
1661 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001662 transport_proto_t tp;
1663
Florin Coras7999e832017-10-31 01:51:04 -07001664 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001665
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +02001666 transport_proto_foreach (tp, transports)
1667 application_start_stop_proxy (app, tp, 0);
Florin Coras7999e832017-10-31 01:51:04 -07001668}
1669
Florin Coras88001c62019-04-24 14:44:46 -07001670segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -08001671application_segment_manager_properties (application_t * app)
1672{
1673 return &app->sm_properties;
1674}
1675
Florin Coras88001c62019-04-24 14:44:46 -07001676segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -08001677application_get_segment_manager_properties (u32 app_index)
1678{
1679 application_t *app = application_get (app_index);
1680 return &app->sm_properties;
1681}
1682
Florin Coras15531972018-08-12 23:50:53 -07001683static void
1684application_format_listeners (application_t * app, int verbose)
1685{
1686 vlib_main_t *vm = vlib_get_main ();
1687 app_worker_map_t *wrk_map;
1688 app_worker_t *app_wrk;
1689 u32 sm_index;
1690 u64 handle;
1691
1692 if (!app)
1693 {
Andreas Schultz972dc172020-05-15 11:50:07 +02001694 vlib_cli_output (vm, "%U", format_app_worker_listener, NULL /* header */,
Florin Coras15531972018-08-12 23:50:53 -07001695 0, 0, verbose);
1696 return;
1697 }
1698
1699 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001700 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras15531972018-08-12 23:50:53 -07001701 app_wrk = app_worker_get (wrk_map->wrk_index);
1702 if (hash_elts (app_wrk->listeners_table) == 0)
1703 continue;
1704 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1705 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1706 handle, sm_index, verbose);
1707 }));
Damjan Marionb2c31b62020-12-13 21:47:40 +01001708 }
Florin Coras15531972018-08-12 23:50:53 -07001709 /* *INDENT-ON* */
1710}
1711
1712static void
Florin Coras15531972018-08-12 23:50:53 -07001713application_format_connects (application_t * app, int verbose)
1714{
1715 app_worker_map_t *wrk_map;
1716 app_worker_t *app_wrk;
1717
1718 if (!app)
1719 {
1720 app_worker_format_connects (0, verbose);
1721 return;
1722 }
1723
1724 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001725 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras15531972018-08-12 23:50:53 -07001726 app_wrk = app_worker_get (wrk_map->wrk_index);
1727 app_worker_format_connects (app_wrk, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001728 }
Florin Coras15531972018-08-12 23:50:53 -07001729 /* *INDENT-ON* */
1730}
1731
Florin Coras6cf30ad2017-04-04 23:08:23 -07001732u8 *
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001733format_cert_key_pair (u8 * s, va_list * args)
1734{
1735 app_cert_key_pair_t *ckpair = va_arg (*args, app_cert_key_pair_t *);
1736 int key_len = 0, cert_len = 0;
1737 cert_len = vec_len (ckpair->cert);
1738 key_len = vec_len (ckpair->key);
1739 if (ckpair->cert_key_index == 0)
1740 s = format (s, "DEFAULT (cert:%d, key:%d)", cert_len, key_len);
1741 else
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001742 s = format (s, "%d (cert:%d, key:%d)", ckpair->cert_key_index,
1743 cert_len, key_len);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001744 return s;
1745}
1746
1747u8 *
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02001748format_crypto_engine (u8 * s, va_list * args)
1749{
1750 u32 engine = va_arg (*args, u32);
1751 switch (engine)
1752 {
1753 case CRYPTO_ENGINE_NONE:
1754 return format (s, "none");
1755 case CRYPTO_ENGINE_MBEDTLS:
1756 return format (s, "mbedtls");
1757 case CRYPTO_ENGINE_OPENSSL:
1758 return format (s, "openssl");
1759 case CRYPTO_ENGINE_PICOTLS:
1760 return format (s, "picotls");
1761 case CRYPTO_ENGINE_VPP:
1762 return format (s, "vpp");
1763 default:
1764 return format (s, "unknown engine");
1765 }
1766 return s;
1767}
1768
1769uword
1770unformat_crypto_engine (unformat_input_t * input, va_list * args)
1771{
1772 u8 *a = va_arg (*args, u8 *);
1773 if (unformat (input, "mbedtls"))
1774 *a = CRYPTO_ENGINE_MBEDTLS;
1775 else if (unformat (input, "openssl"))
1776 *a = CRYPTO_ENGINE_OPENSSL;
1777 else if (unformat (input, "picotls"))
1778 *a = CRYPTO_ENGINE_PICOTLS;
1779 else if (unformat (input, "vpp"))
1780 *a = CRYPTO_ENGINE_VPP;
1781 else
1782 return 0;
1783 return 1;
1784}
1785
1786u8 *
1787format_crypto_context (u8 * s, va_list * args)
1788{
1789 crypto_context_t *crctx = va_arg (*args, crypto_context_t *);
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001790 s = format (s, "[0x%x][sub%d,ckpair%x]", crctx->ctx_index,
1791 crctx->n_subscribers, crctx->ckpair_index);
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02001792 s = format (s, "[%U]", format_crypto_engine, crctx->crypto_engine);
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02001793 return s;
1794}
1795
1796u8 *
Florin Coras6cf30ad2017-04-04 23:08:23 -07001797format_application (u8 * s, va_list * args)
1798{
1799 application_t *app = va_arg (*args, application_t *);
1800 CLIB_UNUSED (int verbose) = va_arg (*args, int);
Florin Coras88001c62019-04-24 14:44:46 -07001801 segment_manager_props_t *props;
Florin Coras053a0e42018-11-13 15:52:38 -08001802 const u8 *app_ns_name, *app_name;
Florin Coras349f8ca2018-11-20 16:52:49 -08001803 app_worker_map_t *wrk_map;
1804 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001805
1806 if (app == 0)
1807 {
Florin Coras349f8ca2018-11-20 16:52:49 -08001808 if (!verbose)
Florin Corasc1f5a432018-11-20 11:31:26 -08001809 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
Florin Coras6cf30ad2017-04-04 23:08:23 -07001810 return s;
1811 }
1812
Florin Coras0bee9ce2018-03-22 21:24:31 -07001813 app_name = app_get_name (app);
Florin Corascea194d2017-10-02 00:18:51 -07001814 app_ns_name = app_namespace_id_from_index (app->ns_index);
Florin Corasa332c462018-01-31 06:52:17 -08001815 props = application_segment_manager_properties (app);
Florin Coras349f8ca2018-11-20 16:52:49 -08001816 if (!verbose)
1817 {
jiangxiaomingdfb30d92020-08-31 17:38:11 +08001818 s = format (s, "%-10u%-20v%-40v", app->app_index, app_name,
Florin Coras349f8ca2018-11-20 16:52:49 -08001819 app_ns_name);
1820 return s;
1821 }
1822
Florin Corasfa7512e2019-04-04 22:31:50 -07001823 s = format (s, "app-name %v app-index %u ns-index %u seg-size %U\n",
Florin Coras349f8ca2018-11-20 16:52:49 -08001824 app_name, app->app_index, app->ns_index,
1825 format_memory_size, props->add_segment_size);
1826 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1827 format_memory_size, props->rx_fifo_size,
1828 format_memory_size, props->tx_fifo_size);
1829
1830 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001831 pool_foreach (wrk_map, app->worker_maps) {
Florin Coras349f8ca2018-11-20 16:52:49 -08001832 app_wrk = app_worker_get (wrk_map->wrk_index);
Florin Coras623eb562019-02-03 19:28:34 -08001833 s = format (s, "%U", format_app_worker, app_wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001834 }
Florin Coras349f8ca2018-11-20 16:52:49 -08001835 /* *INDENT-ON* */
1836
Dave Barach68b0fb02017-02-28 15:15:56 -05001837 return s;
1838}
1839
Florin Corasf8f516a2018-02-08 15:10:09 -08001840void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001841application_format_all_listeners (vlib_main_t * vm, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08001842{
1843 application_t *app;
Florin Corasf8f516a2018-02-08 15:10:09 -08001844
Florin Coras15531972018-08-12 23:50:53 -07001845 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08001846 {
1847 vlib_cli_output (vm, "No active server bindings");
1848 return;
1849 }
1850
Florin Coras2b81e3c2019-02-27 07:55:46 -08001851 application_format_listeners (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001852
Florin Coras2b81e3c2019-02-27 07:55:46 -08001853 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001854 pool_foreach (app, app_main.app_pool) {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001855 application_format_listeners (app, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001856 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001857 /* *INDENT-ON* */
Florin Corasf8f516a2018-02-08 15:10:09 -08001858}
1859
1860void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001861application_format_all_clients (vlib_main_t * vm, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08001862{
1863 application_t *app;
1864
Florin Coras15531972018-08-12 23:50:53 -07001865 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08001866 {
1867 vlib_cli_output (vm, "No active apps");
1868 return;
1869 }
1870
Florin Coras2b81e3c2019-02-27 07:55:46 -08001871 application_format_connects (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001872
Florin Coras2b81e3c2019-02-27 07:55:46 -08001873 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001874 pool_foreach (app, app_main.app_pool) {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001875 application_format_connects (app, verbose);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001876 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001877 /* *INDENT-ON* */
Florin Corasf8f516a2018-02-08 15:10:09 -08001878}
1879
Dave Barach68b0fb02017-02-28 15:15:56 -05001880static clib_error_t *
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001881show_certificate_command_fn (vlib_main_t * vm, unformat_input_t * input,
1882 vlib_cli_command_t * cmd)
1883{
1884 app_cert_key_pair_t *ckpair;
1885 session_cli_return_if_not_enabled ();
1886
1887 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001888 pool_foreach (ckpair, app_main.cert_key_pair_store) {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001889 vlib_cli_output (vm, "%U", format_cert_key_pair, ckpair);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001890 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001891 /* *INDENT-ON* */
1892 return 0;
1893}
1894
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001895static inline void
1896appliction_format_app_mq (vlib_main_t * vm, application_t * app)
1897{
1898 app_worker_map_t *map;
1899 app_worker_t *wrk;
Florin Coras41d5f542021-01-15 13:49:33 -08001900 int i;
1901
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001902 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001903 pool_foreach (map, app->worker_maps) {
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001904 wrk = app_worker_get (map->wrk_index);
1905 vlib_cli_output (vm, "[A%d][%d]%U", app->app_index,
1906 map->wrk_index, format_svm_msg_q,
1907 wrk->event_queue);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001908 }
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001909 /* *INDENT-ON* */
Florin Coras41d5f542021-01-15 13:49:33 -08001910
1911 for (i = 0; i < vec_len (app->rx_mqs); i++)
1912 vlib_cli_output (vm, "[A%d][R%d]%U", app->app_index, i, format_svm_msg_q,
1913 app->rx_mqs[i].mq);
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001914}
1915
1916static clib_error_t *
1917appliction_format_all_app_mq (vlib_main_t * vm)
1918{
1919 application_t *app;
1920 int i, n_threads;
1921
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001922 n_threads = vlib_get_n_threads ();
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001923
1924 for (i = 0; i < n_threads; i++)
1925 {
1926 vlib_cli_output (vm, "[Ctrl%d]%U", i, format_svm_msg_q,
1927 session_main_get_vpp_event_queue (i));
1928 }
1929
1930 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001931 pool_foreach (app, app_main.app_pool) {
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001932 appliction_format_app_mq (vm, app);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001933 }
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001934 /* *INDENT-ON* */
1935 return 0;
1936}
1937
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001938static clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -05001939show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1940 vlib_cli_command_t * cmd)
1941{
Florin Coras7e7b0302022-01-22 13:27:21 -08001942 int do_server = 0, do_client = 0, do_mq = 0, do_transports = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001943 application_t *app;
Florin Coras349f8ca2018-11-20 16:52:49 -08001944 u32 app_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001945 int verbose = 0;
Florin Coras7e7b0302022-01-22 13:27:21 -08001946 u8 is_ta;
Dave Barach68b0fb02017-02-28 15:15:56 -05001947
Florin Corascea194d2017-10-02 00:18:51 -07001948 session_cli_return_if_not_enabled ();
Florin Corase04c2992017-03-01 08:17:34 -08001949
Dave Barach68b0fb02017-02-28 15:15:56 -05001950 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1951 {
1952 if (unformat (input, "server"))
1953 do_server = 1;
1954 else if (unformat (input, "client"))
1955 do_client = 1;
Florin Coras7e7b0302022-01-22 13:27:21 -08001956 else if (unformat (input, "transports"))
1957 do_transports = 1;
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001958 else if (unformat (input, "mq"))
1959 do_mq = 1;
Florin Coras349f8ca2018-11-20 16:52:49 -08001960 else if (unformat (input, "%u", &app_index))
1961 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001962 else if (unformat (input, "verbose"))
1963 verbose = 1;
1964 else
Florin Coras349f8ca2018-11-20 16:52:49 -08001965 return clib_error_return (0, "unknown input `%U'",
1966 format_unformat_error, input);
Dave Barach68b0fb02017-02-28 15:15:56 -05001967 }
1968
Nathan Skrzypczakcfdb1092019-12-02 16:44:42 +01001969 if (do_mq && app_index != ~0)
1970 {
1971 app = application_get_if_valid (app_index);
1972 if (!app)
1973 return clib_error_return (0, "No app with index %u", app_index);
1974
1975 appliction_format_app_mq (vm, app);
1976 return 0;
1977 }
1978
1979 if (do_mq)
1980 {
1981 appliction_format_all_app_mq (vm);
1982 return 0;
1983 }
1984
Dave Barach68b0fb02017-02-28 15:15:56 -05001985 if (do_server)
Florin Coras349f8ca2018-11-20 16:52:49 -08001986 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001987 application_format_all_listeners (vm, verbose);
Florin Coras349f8ca2018-11-20 16:52:49 -08001988 return 0;
1989 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001990
1991 if (do_client)
Florin Coras349f8ca2018-11-20 16:52:49 -08001992 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001993 application_format_all_clients (vm, verbose);
Florin Coras349f8ca2018-11-20 16:52:49 -08001994 return 0;
1995 }
1996
1997 if (app_index != ~0)
1998 {
Florin Coras47c40e22018-11-26 17:01:36 -08001999 app = application_get_if_valid (app_index);
Florin Coras349f8ca2018-11-20 16:52:49 -08002000 if (!app)
2001 return clib_error_return (0, "No app with index %u", app_index);
2002
2003 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
2004 return 0;
2005 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002006
Florin Coras6cf30ad2017-04-04 23:08:23 -07002007 /* Print app related info */
2008 if (!do_server && !do_client)
2009 {
Florin Coras349f8ca2018-11-20 16:52:49 -08002010 vlib_cli_output (vm, "%U", format_application, 0, 0);
Damjan Marionb2c31b62020-12-13 21:47:40 +01002011 pool_foreach (app, app_main.app_pool) {
Florin Coras7e7b0302022-01-22 13:27:21 -08002012 is_ta = app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
2013 if ((!do_transports && !is_ta) || (do_transports && is_ta))
2014 vlib_cli_output (vm, "%U", format_application, app, 0);
Damjan Marionb2c31b62020-12-13 21:47:40 +01002015 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07002016 }
2017
Dave Barach68b0fb02017-02-28 15:15:56 -05002018 return 0;
2019}
2020
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002021/* Certificate store */
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002022
2023static app_cert_key_pair_t *
2024app_cert_key_pair_alloc ()
2025{
2026 app_cert_key_pair_t *ckpair;
2027 pool_get (app_main.cert_key_pair_store, ckpair);
2028 clib_memset (ckpair, 0, sizeof (*ckpair));
2029 ckpair->cert_key_index = ckpair - app_main.cert_key_pair_store;
2030 return ckpair;
2031}
2032
2033app_cert_key_pair_t *
2034app_cert_key_pair_get_if_valid (u32 index)
2035{
2036 if (pool_is_free_index (app_main.cert_key_pair_store, index))
2037 return 0;
2038 return app_cert_key_pair_get (index);
2039}
2040
2041app_cert_key_pair_t *
2042app_cert_key_pair_get (u32 index)
2043{
2044 return pool_elt_at_index (app_main.cert_key_pair_store, index);
2045}
2046
2047app_cert_key_pair_t *
2048app_cert_key_pair_get_default ()
2049{
2050 /* To maintain legacy bapi */
2051 return app_cert_key_pair_get (0);
2052}
2053
2054int
2055vnet_app_add_cert_key_pair (vnet_app_add_cert_key_pair_args_t * a)
2056{
2057 app_cert_key_pair_t *ckpair = app_cert_key_pair_alloc ();
Florin Corasa5a9efd2021-01-05 17:03:29 -08002058 vec_validate (ckpair->cert, a->cert_len - 1);
2059 clib_memcpy_fast (ckpair->cert, a->cert, a->cert_len);
2060 vec_validate (ckpair->key, a->key_len - 1);
2061 clib_memcpy_fast (ckpair->key, a->key, a->key_len);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002062 a->index = ckpair->cert_key_index;
2063 return 0;
2064}
2065
2066int
Nathan Skrzypczak7fbdb6a2019-10-09 16:23:26 +02002067vnet_app_add_cert_key_interest (u32 index, u32 app_index)
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002068{
2069 app_cert_key_pair_t *ckpair;
2070 if (!(ckpair = app_cert_key_pair_get_if_valid (index)))
2071 return -1;
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002072 if (vec_search (ckpair->app_interests, app_index) != ~0)
2073 vec_add1 (ckpair->app_interests, app_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002074 return 0;
2075}
2076
2077int
2078vnet_app_del_cert_key_pair (u32 index)
2079{
2080 app_cert_key_pair_t *ckpair;
2081 application_t *app;
2082 u32 *app_index;
2083
2084 if (!(ckpair = app_cert_key_pair_get_if_valid (index)))
Filip Tehlar0028e6f2023-06-28 10:47:32 +02002085 return SESSION_E_INVALID;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002086
2087 vec_foreach (app_index, ckpair->app_interests)
2088 {
2089 if ((app = application_get_if_valid (*app_index))
2090 && app->cb_fns.app_cert_key_pair_delete_callback)
2091 app->cb_fns.app_cert_key_pair_delete_callback (ckpair);
2092 }
2093
2094 vec_free (ckpair->cert);
2095 vec_free (ckpair->key);
2096 pool_put (app_main.cert_key_pair_store, ckpair);
2097 return 0;
2098}
2099
2100clib_error_t *
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002101application_init (vlib_main_t * vm)
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002102{
Florin Coras41d5f542021-01-15 13:49:33 -08002103 app_main_t *am = &app_main;
2104 u32 n_workers;
2105
2106 n_workers = vlib_num_workers ();
2107
Florin Corasa5a9efd2021-01-05 17:03:29 -08002108 /* Index 0 was originally used by legacy apis, maintain as invalid */
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002109 (void) app_cert_key_pair_alloc ();
Florin Coras41d5f542021-01-15 13:49:33 -08002110 am->last_crypto_engine = CRYPTO_ENGINE_LAST;
2111 am->app_by_name = hash_create_vec (0, sizeof (u8), sizeof (uword));
2112
2113 vec_validate (am->wrk, n_workers);
2114
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002115 return 0;
2116}
2117
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +02002118VLIB_INIT_FUNCTION (application_init);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002119
Florin Coras7e7b0302022-01-22 13:27:21 -08002120VLIB_CLI_COMMAND (show_app_command, static) = {
Florin Corase04c2992017-03-01 08:17:34 -08002121 .path = "show app",
Florin Coras7e7b0302022-01-22 13:27:21 -08002122 .short_help = "show app [index] [server|client] [mq] [verbose] "
2123 "[transports]",
Florin Corase04c2992017-03-01 08:17:34 -08002124 .function = show_app_command_fn,
2125};
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002126
Florin Coras7e7b0302022-01-22 13:27:21 -08002127VLIB_CLI_COMMAND (show_certificate_command, static) = {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02002128 .path = "show app certificate",
2129 .short_help = "list app certs and keys present in store",
2130 .function = show_certificate_command_fn,
2131};
Dave Barach68b0fb02017-02-28 15:15:56 -05002132
Florin Coras79ba25d2019-10-20 19:32:47 -07002133crypto_engine_type_t
2134app_crypto_engine_type_add (void)
2135{
2136 return (++app_main.last_crypto_engine);
2137}
2138
2139u8
2140app_crypto_engine_n_types (void)
2141{
2142 return (app_main.last_crypto_engine + 1);
2143}
2144
Dave Barach68b0fb02017-02-28 15:15:56 -05002145/*
2146 * fd.io coding-style-patch-verification: ON
2147 *
2148 * Local Variables:
2149 * eval: (c-set-style "gnu")
2150 * End:
2151 */