blob: 90c067d6d31b844e41ada2b02429c2f19765c9b4 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
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>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vnet/session/session.h>
20
Florin Coras15531972018-08-12 23:50:53 -070021static app_main_t app_main;
Dave Barach68b0fb02017-02-28 15:15:56 -050022
Florin Corasab2f6db2018-08-31 14:31:41 -070023static app_listener_t *
24app_listener_alloc (application_t * app)
25{
26 app_listener_t *app_listener;
27 pool_get (app->listeners, app_listener);
Dave Barachb7b92992018-10-17 10:38:51 -040028 clib_memset (app_listener, 0, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070029 app_listener->al_index = app_listener - app->listeners;
30 return app_listener;
31}
32
33static app_listener_t *
34app_listener_get (application_t * app, u32 app_listener_index)
35{
36 return pool_elt_at_index (app->listeners, app_listener_index);
37}
38
39static void
40app_listener_free (application_t * app, app_listener_t * app_listener)
41{
42 clib_bitmap_free (app_listener->workers);
43 pool_put (app->listeners, app_listener);
44 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040045 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070046}
47
48static app_listener_t *
49app_local_listener_alloc (application_t * app)
50{
51 app_listener_t *app_listener;
52 pool_get (app->local_listeners, app_listener);
Dave Barachb7b92992018-10-17 10:38:51 -040053 clib_memset (app_listener, 0, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070054 app_listener->al_index = app_listener - app->local_listeners;
55 return app_listener;
56}
57
58static app_listener_t *
59app_local_listener_get (application_t * app, u32 app_listener_index)
60{
61 return pool_elt_at_index (app->local_listeners, app_listener_index);
62}
63
64static void
65app_local_listener_free (application_t * app, app_listener_t * app_listener)
66{
67 clib_bitmap_free (app_listener->workers);
68 pool_put (app->local_listeners, app_listener);
69 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040070 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070071}
72
Florin Coras15531972018-08-12 23:50:53 -070073static app_worker_map_t *
74app_worker_map_alloc (application_t * app)
75{
76 app_worker_map_t *map;
77 pool_get (app->worker_maps, map);
Dave Barachb7b92992018-10-17 10:38:51 -040078 clib_memset (map, 0, sizeof (*map));
Florin Coras15531972018-08-12 23:50:53 -070079 return map;
80}
Dave Barach68b0fb02017-02-28 15:15:56 -050081
Florin Coras15531972018-08-12 23:50:53 -070082static u32
83app_worker_map_index (application_t * app, app_worker_map_t * map)
84{
85 return (map - app->worker_maps);
86}
87
88static void
89app_worker_map_free (application_t * app, app_worker_map_t * map)
90{
91 pool_put (app->worker_maps, map);
92}
93
94static app_worker_map_t *
95app_worker_map_get (application_t * app, u32 map_index)
96{
97 return pool_elt_at_index (app->worker_maps, map_index);
98}
Florin Coras0bee9ce2018-03-22 21:24:31 -070099
Florin Corascea194d2017-10-02 00:18:51 -0700100static u8 *
101app_get_name_from_reg_index (application_t * app)
102{
103 u8 *app_name;
104
105 vl_api_registration_t *regp;
106 regp = vl_api_client_index_to_registration (app->api_client_index);
107 if (!regp)
Florin Coras15531972018-08-12 23:50:53 -0700108 app_name = format (0, "builtin-%d%c", app->app_index, 0);
Florin Corascea194d2017-10-02 00:18:51 -0700109 else
110 app_name = format (0, "%s%c", regp->name, 0);
111
112 return app_name;
113}
114
Florin Coras0bee9ce2018-03-22 21:24:31 -0700115static u8 *
116app_get_name (application_t * app)
117{
118 if (!app->name)
119 return app_get_name_from_reg_index (app);
120 return app->name;
121}
122
Florin Corascea194d2017-10-02 00:18:51 -0700123u32
124application_session_table (application_t * app, u8 fib_proto)
125{
126 app_namespace_t *app_ns;
127 app_ns = app_namespace_get (app->ns_index);
128 if (!application_has_global_scope (app))
129 return APP_INVALID_INDEX;
130 if (fib_proto == FIB_PROTOCOL_IP4)
131 return session_lookup_get_index_for_fib (fib_proto,
132 app_ns->ip4_fib_index);
133 else
134 return session_lookup_get_index_for_fib (fib_proto,
135 app_ns->ip6_fib_index);
136}
137
138u32
139application_local_session_table (application_t * app)
140{
141 app_namespace_t *app_ns;
142 if (!application_has_local_scope (app))
143 return APP_INVALID_INDEX;
144 app_ns = app_namespace_get (app->ns_index);
145 return app_ns->local_table_index;
146}
147
Florin Corasab2f6db2018-08-31 14:31:41 -0700148static void
149application_local_listener_session_endpoint (local_session_t * ll,
150 session_endpoint_t * sep)
151{
152 sep->transport_proto =
153 session_type_transport_proto (ll->listener_session_type);
154 sep->port = ll->port;
155 sep->is_ip4 = ll->listener_session_type & 1;
156}
157
Dave Barach68b0fb02017-02-28 15:15:56 -0500158int
159application_api_queue_is_full (application_t * app)
160{
Florin Corase86a8ed2018-01-05 03:20:25 -0800161 svm_queue_t *q;
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
163 /* builtin servers are always OK */
164 if (app->api_client_index == ~0)
165 return 0;
166
167 q = vl_api_client_index_to_input_queue (app->api_client_index);
168 if (!q)
169 return 1;
170
171 if (q->cursize == q->maxsize)
172 return 1;
173 return 0;
174}
175
Florin Corascea194d2017-10-02 00:18:51 -0700176/**
177 * Returns app name
178 *
179 * Since the name is not stored per app, we generate it on the fly. It is
180 * the caller's responsibility to free the vector
181 */
182u8 *
183application_name_from_index (u32 app_index)
184{
185 application_t *app = application_get (app_index);
186 if (!app)
187 return 0;
188 return app_get_name_from_reg_index (app);
189}
190
Dave Barach68b0fb02017-02-28 15:15:56 -0500191static void
192application_table_add (application_t * app)
193{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700194 if (app->api_client_index != APP_INVALID_INDEX)
Florin Coras15531972018-08-12 23:50:53 -0700195 hash_set (app_main.app_by_api_client_index, app->api_client_index,
196 app->app_index);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700197 else if (app->name)
Florin Coras15531972018-08-12 23:50:53 -0700198 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500199}
200
201static void
202application_table_del (application_t * app)
203{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700204 if (app->api_client_index != APP_INVALID_INDEX)
Florin Coras15531972018-08-12 23:50:53 -0700205 hash_unset (app_main.app_by_api_client_index, app->api_client_index);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700206 else if (app->name)
Florin Coras15531972018-08-12 23:50:53 -0700207 hash_unset_mem (app_main.app_by_name, app->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500208}
209
210application_t *
211application_lookup (u32 api_client_index)
212{
213 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700214 p = hash_get (app_main.app_by_api_client_index, api_client_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500215 if (p)
216 return application_get (p[0]);
217
218 return 0;
219}
220
Florin Coras6cf30ad2017-04-04 23:08:23 -0700221application_t *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700222application_lookup_name (const u8 * name)
223{
224 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700225 p = hash_get_mem (app_main.app_by_name, name);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700226 if (p)
227 return application_get (p[0]);
228
229 return 0;
230}
231
232application_t *
Florin Coras15531972018-08-12 23:50:53 -0700233application_alloc (void)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700234{
235 application_t *app;
Florin Coras15531972018-08-12 23:50:53 -0700236 pool_get (app_main.app_pool, app);
Dave Barachb7b92992018-10-17 10:38:51 -0400237 clib_memset (app, 0, sizeof (*app));
Florin Coras15531972018-08-12 23:50:53 -0700238 app->app_index = app - app_main.app_pool;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700239 return app;
240}
241
Florin Coras15531972018-08-12 23:50:53 -0700242application_t *
243application_get (u32 app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500244{
Florin Coras15531972018-08-12 23:50:53 -0700245 if (app_index == APP_INVALID_INDEX)
246 return 0;
247 return pool_elt_at_index (app_main.app_pool, app_index);
248}
Dave Barach68b0fb02017-02-28 15:15:56 -0500249
Florin Coras15531972018-08-12 23:50:53 -0700250application_t *
251application_get_if_valid (u32 app_index)
252{
253 if (pool_is_free_index (app_main.app_pool, app_index))
254 return 0;
Florin Corasa5464812017-04-19 13:00:05 -0700255
Florin Coras15531972018-08-12 23:50:53 -0700256 return pool_elt_at_index (app_main.app_pool, app_index);
257}
Florin Coras7999e832017-10-31 01:51:04 -0700258
Florin Coras15531972018-08-12 23:50:53 -0700259u32
260application_index (application_t * app)
261{
262 return app - app_main.app_pool;
Dave Barach68b0fb02017-02-28 15:15:56 -0500263}
264
Florin Corasd79b41e2017-03-04 05:37:52 -0800265static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700266application_verify_cb_fns (session_cb_vft_t * cb_fns)
Florin Corasd79b41e2017-03-04 05:37:52 -0800267{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700268 if (cb_fns->session_accept_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800269 clib_warning ("No accept callback function provided");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700270 if (cb_fns->session_connected_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800271 clib_warning ("No session connected callback function provided");
272 if (cb_fns->session_disconnect_callback == 0)
273 clib_warning ("No session disconnect callback function provided");
274 if (cb_fns->session_reset_callback == 0)
275 clib_warning ("No session reset callback function provided");
276}
277
Florin Corasb384b542018-01-15 01:08:33 -0800278/**
279 * Check app config for given segment type
280 *
281 * Returns 1 on success and 0 otherwise
282 */
283static u8
284application_verify_cfg (ssvm_segment_type_t st)
285{
286 u8 is_valid;
287 if (st == SSVM_SEGMENT_MEMFD)
288 {
289 is_valid = (session_manager_get_evt_q_segment () != 0);
290 if (!is_valid)
291 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
292 return is_valid;
293 }
294 else if (st == SSVM_SEGMENT_SHM)
295 {
296 is_valid = (session_manager_get_evt_q_segment () == 0);
297 if (!is_valid)
298 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
299 return is_valid;
300 }
301 else
302 return 1;
303}
304
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305int
Florin Coras15531972018-08-12 23:50:53 -0700306application_alloc_and_init (app_init_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500307{
Florin Corasa332c462018-01-31 06:52:17 -0800308 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
Florin Corasb384b542018-01-15 01:08:33 -0800309 segment_manager_properties_t *props;
310 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700311 application_t *app;
312 u64 *options;
Dave Barach68b0fb02017-02-28 15:15:56 -0500313
Florin Coras15531972018-08-12 23:50:53 -0700314 app = application_alloc ();
315 options = a->options;
Florin Corasb384b542018-01-15 01:08:33 -0800316 /*
317 * Make sure we support the requested configuration
318 */
Florin Corasa332c462018-01-31 06:52:17 -0800319 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
320 {
Florin Coras15531972018-08-12 23:50:53 -0700321 reg = vl_api_client_index_to_registration (a->api_client_index);
Florin Corasa332c462018-01-31 06:52:17 -0800322 if (!reg)
323 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
324 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
325 seg_type = SSVM_SEGMENT_SHM;
326 }
327 else
328 {
Florin Coras99368312018-08-02 10:45:44 -0700329 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
330 {
331 clib_warning ("mq eventfds can only be used if socket transport is "
332 "used for api");
333 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
334 }
Florin Corasa332c462018-01-31 06:52:17 -0800335 seg_type = SSVM_SEGMENT_PRIVATE;
336 }
Florin Corasb384b542018-01-15 01:08:33 -0800337
Florin Corasa332c462018-01-31 06:52:17 -0800338 if (!application_verify_cfg (seg_type))
Florin Corasb384b542018-01-15 01:08:33 -0800339 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
Dave Barach68b0fb02017-02-28 15:15:56 -0500340
Florin Coras15531972018-08-12 23:50:53 -0700341 /* Check that the obvious things are properly set up */
342 application_verify_cb_fns (a->session_cb_vft);
343
344 app->api_client_index = a->api_client_index;
345 app->flags = options[APP_OPTIONS_FLAGS];
346 app->cb_fns = *a->session_cb_vft;
347 app->ns_index = options[APP_OPTIONS_NAMESPACE];
348 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
349 app->name = vec_dup (a->name);
350
351 /* If no scope enabled, default to global */
352 if (!application_has_global_scope (app)
353 && !application_has_local_scope (app))
354 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
355
Florin Corasa332c462018-01-31 06:52:17 -0800356 props = application_segment_manager_properties (app);
357 segment_manager_properties_init (props);
Florin Coras15531972018-08-12 23:50:53 -0700358 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
359 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
Florin Corasb384b542018-01-15 01:08:33 -0800360 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
361 {
362 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
363 props->add_segment = 1;
364 }
365 if (options[APP_OPTIONS_RX_FIFO_SIZE])
366 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
367 if (options[APP_OPTIONS_TX_FIFO_SIZE])
368 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
Florin Corasf8f516a2018-02-08 15:10:09 -0800369 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
370 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
Florin Coras99368312018-08-02 10:45:44 -0700371 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
372 props->use_mq_eventfd = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800373 if (options[APP_OPTIONS_TLS_ENGINE])
374 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
Florin Corasa332c462018-01-31 06:52:17 -0800375 props->segment_type = seg_type;
Dave Barach68b0fb02017-02-28 15:15:56 -0500376
Florin Coras15531972018-08-12 23:50:53 -0700377 /* Add app to lookup by api_client_index table */
378 application_table_add (app);
379 a->app_index = application_index (app);
Florin Corasa332c462018-01-31 06:52:17 -0800380
Florin Coras15531972018-08-12 23:50:53 -0700381 APP_DBG ("New app name: %v api index: %u index %u", app->name,
382 app->api_client_index, app->app_index);
383
384 return 0;
385}
386
387void
388application_free (application_t * app)
389{
390 app_worker_map_t *wrk_map;
391 app_worker_t *app_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700392 u32 table_index;
393 local_session_t *ll;
394 session_endpoint_t sep;
Florin Coras15531972018-08-12 23:50:53 -0700395
396 /*
397 * The app event queue allocated in first segment is cleared with
398 * the segment manager. No need to explicitly free it.
399 */
400 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
401 app->api_client_index, app->app_index);
402
403 if (application_is_proxy (app))
404 application_remove_proxy (app);
405
Florin Corasab2f6db2018-08-31 14:31:41 -0700406 /*
407 * Free workers
408 */
409
Florin Coras15531972018-08-12 23:50:53 -0700410 /* *INDENT-OFF* */
411 pool_flush (wrk_map, app->worker_maps, ({
412 app_wrk = app_worker_get (wrk_map->wrk_index);
413 app_worker_free (app_wrk);
414 }));
415 /* *INDENT-ON* */
416 pool_free (app->worker_maps);
417
Florin Corasab2f6db2018-08-31 14:31:41 -0700418 /*
419 * Free local listeners. Global table unbinds stop local listeners
420 * as well, but if we have only local binds, these won't be cleaned up.
421 * Don't bother with local accepted sessions, we clean them when
422 * cleaning up the worker.
423 */
424 table_index = application_local_session_table (app);
425 /* *INDENT-OFF* */
426 pool_foreach (ll, app->local_listen_sessions, ({
427 application_local_listener_session_endpoint (ll, &sep);
428 session_lookup_del_session_endpoint (table_index, &sep);
429 }));
430 /* *INDENT-ON* */
431 pool_free (app->local_listen_sessions);
432
433 /*
434 * Cleanup remaining state
435 */
Florin Coras15531972018-08-12 23:50:53 -0700436 application_table_del (app);
437 vec_free (app->name);
438 vec_free (app->tls_cert);
439 vec_free (app->tls_key);
440 pool_put (app_main.app_pool, app);
441}
442
443app_worker_t *
444application_get_worker (application_t * app, u32 wrk_map_index)
445{
446 app_worker_map_t *map;
447 map = app_worker_map_get (app, wrk_map_index);
448 if (!map)
449 return 0;
450 return app_worker_get (map->wrk_index);
451}
452
453app_worker_t *
454application_get_default_worker (application_t * app)
455{
456 return application_get_worker (app, 0);
457}
458
459app_worker_t *
Florin Corasab2f6db2018-08-31 14:31:41 -0700460application_listener_select_worker (stream_session_t * ls, u8 is_local)
461{
462 app_listener_t *app_listener;
463 application_t *app;
464 u32 wrk_index;
465
466 app = application_get (ls->app_index);
467 if (!is_local)
468 app_listener = app_listener_get (app, ls->listener_db_index);
469 else
470 app_listener = app_local_listener_get (app, ls->listener_db_index);
471
472 wrk_index = clib_bitmap_next_set (app_listener->workers,
473 app_listener->accept_rotor + 1);
474 if (wrk_index == ~0)
475 wrk_index = clib_bitmap_first_set (app_listener->workers);
476
477 ASSERT (wrk_index != ~0);
478 app_listener->accept_rotor = wrk_index;
479 return application_get_worker (app, wrk_index);
480}
481
482app_worker_t *
Florin Coras15531972018-08-12 23:50:53 -0700483app_worker_alloc (application_t * app)
484{
485 app_worker_t *app_wrk;
486 pool_get (app_main.workers, app_wrk);
Dave Barachb7b92992018-10-17 10:38:51 -0400487 clib_memset (app_wrk, 0, sizeof (*app_wrk));
Florin Coras15531972018-08-12 23:50:53 -0700488 app_wrk->wrk_index = app_wrk - app_main.workers;
489 app_wrk->app_index = app->app_index;
490 app_wrk->wrk_map_index = ~0;
491 app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
492 app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
493 app_wrk->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
494 APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index);
495 return app_wrk;
496}
497
498app_worker_t *
499app_worker_get (u32 wrk_index)
500{
501 return pool_elt_at_index (app_main.workers, wrk_index);
502}
503
504app_worker_t *
505app_worker_get_if_valid (u32 wrk_index)
506{
507 if (pool_is_free_index (app_main.workers, wrk_index))
508 return 0;
509 return pool_elt_at_index (app_main.workers, wrk_index);
510}
511
512void
513app_worker_free (app_worker_t * app_wrk)
514{
515 application_t *app = application_get (app_wrk->app_index);
516 vnet_unbind_args_t _a, *a = &_a;
517 u64 handle, *handles = 0;
518 segment_manager_t *sm;
519 u32 sm_index;
520 int i;
521
522 /*
523 * Listener cleanup
524 */
525
526 /* *INDENT-OFF* */
527 hash_foreach (handle, sm_index, app_wrk->listeners_table,
528 ({
529 vec_add1 (handles, handle);
530 sm = segment_manager_get (sm_index);
531 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
532 }));
533 /* *INDENT-ON* */
534
535 for (i = 0; i < vec_len (handles); i++)
536 {
537 a->app_index = app->app_index;
Florin Corasab2f6db2018-08-31 14:31:41 -0700538 a->wrk_map_index = app_wrk->wrk_map_index;
Florin Coras15531972018-08-12 23:50:53 -0700539 a->handle = handles[i];
540 /* seg manager is removed when unbind completes */
541 vnet_unbind (a);
542 }
543
544 /*
545 * Connects segment manager cleanup
546 */
547
548 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
549 {
550 sm = segment_manager_get (app_wrk->connects_seg_manager);
551 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
552 segment_manager_init_del (sm);
553 }
554
555 /* If first segment manager is used by a listener */
556 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
557 && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
558 {
559 sm = segment_manager_get (app_wrk->first_segment_manager);
Florin Coras400ded32018-10-03 01:00:57 -0700560 sm->first_is_protected = 0;
561 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras15531972018-08-12 23:50:53 -0700562 /* .. and has no fifos, e.g. it might be used for redirected sessions,
563 * remove it */
564 if (!segment_manager_has_fifos (sm))
Florin Coras400ded32018-10-03 01:00:57 -0700565 segment_manager_del (sm);
Florin Coras15531972018-08-12 23:50:53 -0700566 }
567
568 /*
569 * Local sessions
570 */
Florin Corasab2f6db2018-08-31 14:31:41 -0700571 app_worker_local_sessions_free (app_wrk);
Florin Coras15531972018-08-12 23:50:53 -0700572
573 pool_put (app_main.workers, app_wrk);
574 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400575 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
Florin Coras15531972018-08-12 23:50:53 -0700576}
577
578int
579app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk)
580{
581 app_worker_map_t *wrk_map;
582 app_worker_t *app_wrk;
583 segment_manager_t *sm;
584 int rv;
585
586 app_wrk = app_worker_alloc (app);
587 wrk_map = app_worker_map_alloc (app);
588 wrk_map->wrk_index = app_wrk->wrk_index;
589 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
590
591 /*
592 * Setup first segment manager
593 */
594 sm = segment_manager_new ();
595 sm->app_wrk_index = app_wrk->wrk_index;
596
597 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
598 app->sm_properties.prealloc_fifos)))
599 {
600 app_worker_free (app_wrk);
601 return rv;
602 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700603 sm->first_is_protected = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500604
Florin Corascea194d2017-10-02 00:18:51 -0700605 /*
Florin Coras15531972018-08-12 23:50:53 -0700606 * Setup app worker
Florin Corascea194d2017-10-02 00:18:51 -0700607 */
Florin Coras15531972018-08-12 23:50:53 -0700608 app_wrk->first_segment_manager = segment_manager_index (sm);
609 app_wrk->listeners_table = hash_create (0, sizeof (u64));
610 app_wrk->event_queue = segment_manager_event_queue (sm);
611 app_wrk->app_is_builtin = application_is_builtin (app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500612
Florin Corasf8f516a2018-02-08 15:10:09 -0800613 /*
614 * Segment manager for local sessions
615 */
616 sm = segment_manager_new ();
Florin Coras15531972018-08-12 23:50:53 -0700617 sm->app_wrk_index = app_wrk->wrk_index;
618 app_wrk->local_segment_manager = segment_manager_index (sm);
619 app_wrk->local_connects = hash_create (0, sizeof (u64));
620
621 *wrk = app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -0800622
Florin Coras6cf30ad2017-04-04 23:08:23 -0700623 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500624}
625
Florin Corasab2f6db2018-08-31 14:31:41 -0700626application_t *
627app_worker_get_app (u32 wrk_index)
628{
629 app_worker_t *app_wrk;
630 app_wrk = app_worker_get_if_valid (wrk_index);
631 if (!app_wrk)
632 return 0;
633 return application_get_if_valid (app_wrk->app_index);
634}
635
Florin Coras6cf30ad2017-04-04 23:08:23 -0700636static segment_manager_t *
Florin Corasab2f6db2018-08-31 14:31:41 -0700637app_worker_alloc_segment_manager (app_worker_t * app_wrk)
Dave Barach68b0fb02017-02-28 15:15:56 -0500638{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700639 segment_manager_t *sm = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500640
Florin Corasc87c91d2017-08-16 19:55:49 -0700641 /* If the first segment manager is not in use, don't allocate a new one */
Florin Coras15531972018-08-12 23:50:53 -0700642 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
643 && app_wrk->first_segment_manager_in_use == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500644 {
Florin Coras15531972018-08-12 23:50:53 -0700645 sm = segment_manager_get (app_wrk->first_segment_manager);
646 app_wrk->first_segment_manager_in_use = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700647 return sm;
Dave Barach68b0fb02017-02-28 15:15:56 -0500648 }
649
Florin Coras6cf30ad2017-04-04 23:08:23 -0700650 sm = segment_manager_new ();
Florin Coras15531972018-08-12 23:50:53 -0700651 sm->app_wrk_index = app_wrk->wrk_index;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700652
Florin Coras6cf30ad2017-04-04 23:08:23 -0700653 return sm;
654}
655
Florin Coras6cf30ad2017-04-04 23:08:23 -0700656int
Florin Corasab2f6db2018-08-31 14:31:41 -0700657app_worker_start_listen (app_worker_t * app_wrk, stream_session_t * ls)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700658{
659 segment_manager_t *sm;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700660
Florin Coras6cf30ad2017-04-04 23:08:23 -0700661 /* Allocate segment manager. All sessions derived out of a listen session
662 * have fifos allocated by the same segment manager. */
Florin Corasab2f6db2018-08-31 14:31:41 -0700663 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
664 return -1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700665
666 /* Add to app's listener table. Useful to find all child listeners
667 * when app goes down, although, just for unbinding this is not needed */
Florin Corasab2f6db2018-08-31 14:31:41 -0700668 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
669 segment_manager_index (sm));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700670
Florin Corasab2f6db2018-08-31 14:31:41 -0700671 if (!ls->server_rx_fifo
672 && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700673 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700674 if (session_alloc_fifos (sm, ls))
675 return -1;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700676 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700677 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700678}
679
Florin Coras6cf30ad2017-04-04 23:08:23 -0700680int
Florin Corasab2f6db2018-08-31 14:31:41 -0700681app_worker_stop_listen (app_worker_t * app_wrk, session_handle_t handle)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700682{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700683 segment_manager_t *sm;
Florin Corasab2f6db2018-08-31 14:31:41 -0700684 uword *sm_indexp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700685
Florin Corasab2f6db2018-08-31 14:31:41 -0700686 sm_indexp = hash_get (app_wrk->listeners_table, handle);
687 if (PREDICT_FALSE (!sm_indexp))
Florin Coras15531972018-08-12 23:50:53 -0700688 {
689 clib_warning ("listener handle was removed %llu!", handle);
690 return -1;
691 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700692
Florin Corasab2f6db2018-08-31 14:31:41 -0700693 sm = segment_manager_get (*sm_indexp);
694 if (app_wrk->first_segment_manager == *sm_indexp)
Florin Coras0e9c33b2017-08-14 22:33:41 -0700695 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700696 /* Delete sessions but don't remove segment manager */
Florin Coras15531972018-08-12 23:50:53 -0700697 app_wrk->first_segment_manager_in_use = 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700698 segment_manager_del_sessions (sm);
699 }
700 else
701 {
702 segment_manager_init_del (sm);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700703 }
Florin Coras15531972018-08-12 23:50:53 -0700704 hash_unset (app_wrk->listeners_table, handle);
Florin Corasab2f6db2018-08-31 14:31:41 -0700705
706 return 0;
707}
708
709/**
710 * Start listening local transport endpoint for requested transport.
711 *
712 * Creates a 'dummy' stream session with state LISTENING to be used in session
713 * lookups, prior to establishing connection. Requests transport to build
714 * it's own specific listening connection.
715 */
716int
717application_start_listen (application_t * app,
718 session_endpoint_extended_t * sep_ext,
719 session_handle_t * res)
720{
721 app_listener_t *app_listener;
722 u32 table_index, fib_proto;
723 session_endpoint_t *sep;
724 app_worker_t *app_wrk;
725 stream_session_t *ls;
726 session_handle_t lh;
727 session_type_t sst;
728
729 /*
730 * Check if sep is already listened on
731 */
732 sep = (session_endpoint_t *) sep_ext;
733 fib_proto = session_endpoint_fib_proto (sep);
734 table_index = application_session_table (app, fib_proto);
735 lh = session_lookup_endpoint_listener (table_index, sep, 1);
736 if (lh != SESSION_INVALID_HANDLE)
737 {
738 ls = listen_session_get_from_handle (lh);
739 if (ls->app_index != app->app_index)
740 return VNET_API_ERROR_ADDRESS_IN_USE;
741
742 app_wrk = app_worker_get (sep_ext->app_wrk_index);
743 if (ls->app_wrk_index == app_wrk->wrk_index)
744 return VNET_API_ERROR_ADDRESS_IN_USE;
745
746 if (app_worker_start_listen (app_wrk, ls))
747 return -1;
748
749 app_listener = app_listener_get (app, ls->listener_db_index);
750 app_listener->workers = clib_bitmap_set (app_listener->workers,
751 app_wrk->wrk_map_index, 1);
752
753 *res = listen_session_get_handle (ls);
754 return 0;
755 }
756
757 /*
758 * Allocate new listener for application
759 */
760 sst = session_type_from_proto_and_ip (sep_ext->transport_proto,
761 sep_ext->is_ip4);
762 ls = listen_session_new (0, sst);
763 ls->app_index = app->app_index;
Florin Coras74cac882018-09-07 09:13:15 -0700764 lh = listen_session_get_handle (ls);
Florin Corasab2f6db2018-08-31 14:31:41 -0700765 if (session_listen (ls, sep_ext))
766 goto err;
767
Florin Coras74cac882018-09-07 09:13:15 -0700768
769 ls = listen_session_get_from_handle (lh);
Florin Corasab2f6db2018-08-31 14:31:41 -0700770 app_listener = app_listener_alloc (app);
771 ls->listener_db_index = app_listener->al_index;
772
773 /*
774 * Setup app worker as a listener
775 */
776 app_wrk = app_worker_get (sep_ext->app_wrk_index);
777 ls->app_wrk_index = app_wrk->wrk_index;
778 if (app_worker_start_listen (app_wrk, ls))
779 goto err;
780 app_listener->workers = clib_bitmap_set (app_listener->workers,
781 app_wrk->wrk_map_index, 1);
782
Florin Coras74cac882018-09-07 09:13:15 -0700783 *res = lh;
Florin Corasab2f6db2018-08-31 14:31:41 -0700784 return 0;
785
786err:
787 listen_session_del (ls);
788 return -1;
789}
790
791/**
792 * Stop listening on session associated to handle
793 *
794 * @param handle listener handle
795 * @param app_index index of the app owning the handle.
796 * @param app_wrk_index index of the worker requesting the stop
797 */
798int
799application_stop_listen (u32 app_index, u32 app_wrk_index,
800 session_handle_t handle)
801{
802 app_listener_t *app_listener;
803 stream_session_t *listener;
804 app_worker_t *app_wrk;
805 application_t *app;
806
807 listener = listen_session_get_from_handle (handle);
808 app = application_get (app_index);
809 if (PREDICT_FALSE (!app || app->app_index != listener->app_index))
810 {
811 clib_warning ("app doesn't own handle %llu!", handle);
812 return -1;
813 }
814
815 app_listener = app_listener_get (app, listener->listener_db_index);
816 if (!clib_bitmap_get (app_listener->workers, app_wrk_index))
817 {
818 clib_warning ("worker not listening on handle %lu", handle);
819 return 0;
820 }
821
822 app_wrk = application_get_worker (app, app_wrk_index);
823 app_worker_stop_listen (app_wrk, handle);
824 clib_bitmap_set_no_check (app_listener->workers, app_wrk_index, 0);
825
826 if (clib_bitmap_is_zero (app_listener->workers))
827 {
828 session_stop_listen (listener);
829 app_listener_free (app, app_listener);
830 listen_session_del (listener);
831 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700832
Dave Barach68b0fb02017-02-28 15:15:56 -0500833 return 0;
834}
835
Florin Coras6cf30ad2017-04-04 23:08:23 -0700836int
Florin Coras15531972018-08-12 23:50:53 -0700837app_worker_open_session (app_worker_t * app, session_endpoint_t * sep,
838 u32 api_context)
Dave Barach68b0fb02017-02-28 15:15:56 -0500839{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700840 int rv;
841
842 /* Make sure we have a segment manager for connects */
Florin Coras15531972018-08-12 23:50:53 -0700843 app_worker_alloc_connects_segment_manager (app);
Florin Coras371ca502018-02-21 12:07:41 -0800844
Florin Coras15531972018-08-12 23:50:53 -0700845 if ((rv = session_open (app->wrk_index, sep, api_context)))
Florin Coras371ca502018-02-21 12:07:41 -0800846 return rv;
847
848 return 0;
849}
850
851int
Florin Coras15531972018-08-12 23:50:53 -0700852app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
Florin Coras371ca502018-02-21 12:07:41 -0800853{
854 segment_manager_t *sm;
855
Florin Coras15531972018-08-12 23:50:53 -0700856 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700857 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700858 sm = app_worker_alloc_segment_manager (app_wrk);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700859 if (sm == 0)
860 return -1;
Florin Coras15531972018-08-12 23:50:53 -0700861 app_wrk->connects_seg_manager = segment_manager_index (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700862 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700863 return 0;
864}
865
866segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700867app_worker_get_connect_segment_manager (app_worker_t * app)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700868{
869 ASSERT (app->connects_seg_manager != (u32) ~ 0);
870 return segment_manager_get (app->connects_seg_manager);
871}
872
873segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700874app_worker_get_listen_segment_manager (app_worker_t * app,
Florin Corasa44d6b12018-10-03 14:29:10 -0700875 stream_session_t * listener)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700876{
877 uword *smp;
Florin Corasa44d6b12018-10-03 14:29:10 -0700878 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700879 ASSERT (smp != 0);
880 return segment_manager_get (*smp);
881}
882
Florin Coras15531972018-08-12 23:50:53 -0700883clib_error_t *
884vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
885{
886 svm_fifo_segment_private_t *fs;
887 app_worker_map_t *wrk_map;
888 app_worker_t *app_wrk;
889 segment_manager_t *sm;
890 application_t *app;
891 int rv;
892
893 app = application_get (a->app_index);
894 if (!app)
895 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
896 "App %u does not exist", a->app_index);
897
898 if (a->is_add)
899 {
900 if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
901 return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv);
902 sm = segment_manager_get (app_wrk->first_segment_manager);
903 fs = segment_manager_get_segment_w_lock (sm, 0);
904 a->segment = &fs->ssvm;
905 segment_manager_segment_reader_unlock (sm);
906 a->evt_q = app_wrk->event_queue;
Florin Corasab2f6db2018-08-31 14:31:41 -0700907 a->wrk_index = app_wrk->wrk_map_index;
Florin Coras15531972018-08-12 23:50:53 -0700908 }
909 else
910 {
911 wrk_map = app_worker_map_get (app, a->wrk_index);
912 if (!wrk_map)
913 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
914 "App %u does not have worker %u",
915 app->app_index, a->wrk_index);
916 app_wrk = app_worker_get (wrk_map->wrk_index);
917 app_worker_map_free (app, wrk_map);
918 if (!app_wrk)
919 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
920 "No worker %u", a->wrk_index);
921 app_worker_free (app_wrk);
922 }
923 return 0;
924}
925
Florin Corasf8f516a2018-02-08 15:10:09 -0800926segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700927application_get_local_segment_manager (app_worker_t * app)
Florin Corasf8f516a2018-02-08 15:10:09 -0800928{
929 return segment_manager_get (app->local_segment_manager);
930}
931
932segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700933application_get_local_segment_manager_w_session (app_worker_t * app,
Florin Corasf8f516a2018-02-08 15:10:09 -0800934 local_session_t * ls)
935{
936 stream_session_t *listener;
937 if (application_local_session_listener_has_transport (ls))
938 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700939 listener = listen_session_get (ls->listener_index);
Florin Coras15531972018-08-12 23:50:53 -0700940 return app_worker_get_listen_segment_manager (app, listener);
Florin Corasf8f516a2018-02-08 15:10:09 -0800941 }
942 return segment_manager_get (app->local_segment_manager);
943}
944
Dave Barach52851e62017-08-07 09:35:25 -0400945int
946application_is_proxy (application_t * app)
947{
Florin Coras7999e832017-10-31 01:51:04 -0700948 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
949}
950
951int
952application_is_builtin (application_t * app)
953{
954 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
955}
956
957int
958application_is_builtin_proxy (application_t * app)
959{
960 return (application_is_proxy (app) && application_is_builtin (app));
Dave Barach52851e62017-08-07 09:35:25 -0400961}
962
Florin Corascea194d2017-10-02 00:18:51 -0700963u8
964application_has_local_scope (application_t * app)
965{
966 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
967}
968
969u8
970application_has_global_scope (application_t * app)
971{
972 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
973}
974
Florin Coras60116992018-08-27 09:52:18 -0700975u8
976application_use_mq_for_ctrl (application_t * app)
977{
978 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
979}
980
Florin Coras15531972018-08-12 23:50:53 -0700981/**
982 * Send an API message to the external app, to map new segment
983 */
984int
985app_worker_add_segment_notify (u32 app_wrk_index, ssvm_private_t * fs)
986{
987 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
988 application_t *app = application_get (app_wrk->app_index);
989 return app->cb_fns.add_segment_callback (app->api_client_index, fs);
990}
991
Florin Coras1c710452017-10-17 00:03:13 -0700992u32
Florin Coras15531972018-08-12 23:50:53 -0700993application_n_listeners (app_worker_t * app)
Florin Coras1c710452017-10-17 00:03:13 -0700994{
995 return hash_elts (app->listeners_table);
996}
997
998stream_session_t *
Florin Coras15531972018-08-12 23:50:53 -0700999app_worker_first_listener (app_worker_t * app, u8 fib_proto,
1000 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -07001001{
Florin Coras7999e832017-10-31 01:51:04 -07001002 stream_session_t *listener;
Florin Coras1c710452017-10-17 00:03:13 -07001003 u64 handle;
1004 u32 sm_index;
Florin Coras7999e832017-10-31 01:51:04 -07001005 u8 sst;
1006
1007 sst = session_type_from_proto_and_ip (transport_proto,
1008 fib_proto == FIB_PROTOCOL_IP4);
Florin Coras1c710452017-10-17 00:03:13 -07001009
1010 /* *INDENT-OFF* */
1011 hash_foreach (handle, sm_index, app->listeners_table, ({
Florin Coras7999e832017-10-31 01:51:04 -07001012 listener = listen_session_get_from_handle (handle);
Florin Corasc3ddea82017-11-27 03:12:00 -08001013 if (listener->session_type == sst
Florin Corasab2f6db2018-08-31 14:31:41 -07001014 && listener->enqueue_epoch != SESSION_PROXY_LISTENER_INDEX)
Florin Coras7999e832017-10-31 01:51:04 -07001015 return listener;
Florin Coras1c710452017-10-17 00:03:13 -07001016 }));
1017 /* *INDENT-ON* */
1018
1019 return 0;
1020}
1021
Florin Coras15531972018-08-12 23:50:53 -07001022u8
1023app_worker_application_is_builtin (app_worker_t * app_wrk)
1024{
1025 return app_wrk->app_is_builtin;
1026}
1027
Florin Coras19b1f6a2017-12-11 03:37:03 -08001028stream_session_t *
Florin Coras15531972018-08-12 23:50:53 -07001029application_proxy_listener (app_worker_t * app, u8 fib_proto,
Florin Coras19b1f6a2017-12-11 03:37:03 -08001030 u8 transport_proto)
1031{
1032 stream_session_t *listener;
1033 u64 handle;
1034 u32 sm_index;
1035 u8 sst;
1036
1037 sst = session_type_from_proto_and_ip (transport_proto,
1038 fib_proto == FIB_PROTOCOL_IP4);
1039
1040 /* *INDENT-OFF* */
1041 hash_foreach (handle, sm_index, app->listeners_table, ({
1042 listener = listen_session_get_from_handle (handle);
1043 if (listener->session_type == sst
Florin Corasab2f6db2018-08-31 14:31:41 -07001044 && listener->enqueue_epoch == SESSION_PROXY_LISTENER_INDEX)
Florin Coras19b1f6a2017-12-11 03:37:03 -08001045 return listener;
1046 }));
1047 /* *INDENT-ON* */
1048
1049 return 0;
1050}
1051
Florin Coras7999e832017-10-31 01:51:04 -07001052static clib_error_t *
1053application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1054 u8 transport_proto, u8 is_start)
1055{
Florin Coras7999e832017-10-31 01:51:04 -07001056 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1057 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
Florin Corasab2f6db2018-08-31 14:31:41 -07001058 session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL;
Florin Coras7999e832017-10-31 01:51:04 -07001059 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -07001060 app_worker_t *app_wrk;
Florin Coras7999e832017-10-31 01:51:04 -07001061 stream_session_t *s;
1062 u64 handle;
1063
Florin Coras15531972018-08-12 23:50:53 -07001064 /* TODO decide if we want proxy to be enabled for all workers */
1065 app_wrk = application_get_default_worker (app);
Florin Coras7999e832017-10-31 01:51:04 -07001066 if (is_start)
1067 {
Florin Coras15531972018-08-12 23:50:53 -07001068 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001069 if (!s)
1070 {
1071 sep.is_ip4 = is_ip4;
1072 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1073 sep.sw_if_index = app_ns->sw_if_index;
1074 sep.transport_proto = transport_proto;
Florin Corasab2f6db2018-08-31 14:31:41 -07001075 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1076 application_start_listen (app, &sep, &handle);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001077 s = listen_session_get_from_handle (handle);
Florin Corasab2f6db2018-08-31 14:31:41 -07001078 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
Florin Coras19b1f6a2017-12-11 03:37:03 -08001079 }
Florin Coras7999e832017-10-31 01:51:04 -07001080 }
1081 else
1082 {
Florin Coras15531972018-08-12 23:50:53 -07001083 s = application_proxy_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001084 ASSERT (s);
Florin Coras7999e832017-10-31 01:51:04 -07001085 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001086
Florin Coras7999e832017-10-31 01:51:04 -07001087 tc = listen_session_get_transport (s);
1088
1089 if (!ip_is_zero (&tc->lcl_ip, 1))
1090 {
Florin Corasdbd44562017-11-09 19:30:17 -08001091 u32 sti;
1092 sep.is_ip4 = is_ip4;
1093 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1094 sep.transport_proto = transport_proto;
1095 sep.port = 0;
1096 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001097 if (is_start)
Florin Corasab2f6db2018-08-31 14:31:41 -07001098 session_lookup_add_session_endpoint (sti,
1099 (session_endpoint_t *) & sep,
1100 s->session_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001101 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001102 session_lookup_del_session_endpoint (sti,
1103 (session_endpoint_t *) & sep);
Florin Coras7999e832017-10-31 01:51:04 -07001104 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001105
Florin Coras7999e832017-10-31 01:51:04 -07001106 return 0;
1107}
1108
Florin Coras19b1f6a2017-12-11 03:37:03 -08001109static void
1110application_start_stop_proxy_local_scope (application_t * app,
1111 u8 transport_proto, u8 is_start)
1112{
1113 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1114 app_namespace_t *app_ns;
1115 app_ns = app_namespace_get (app->ns_index);
1116 sep.is_ip4 = 1;
1117 sep.transport_proto = transport_proto;
1118 sep.port = 0;
1119
1120 if (is_start)
1121 {
1122 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001123 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001124 sep.is_ip4 = 0;
1125 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001126 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001127 }
1128 else
1129 {
1130 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1131 sep.is_ip4 = 0;
1132 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1133 }
1134}
1135
Florin Coras7999e832017-10-31 01:51:04 -07001136void
Florin Coras561af9b2017-12-09 10:19:43 -08001137application_start_stop_proxy (application_t * app,
1138 transport_proto_t transport_proto, u8 is_start)
Florin Coras7999e832017-10-31 01:51:04 -07001139{
Florin Coras7999e832017-10-31 01:51:04 -07001140 if (application_has_local_scope (app))
Florin Coras19b1f6a2017-12-11 03:37:03 -08001141 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
Florin Coras7999e832017-10-31 01:51:04 -07001142
1143 if (application_has_global_scope (app))
1144 {
1145 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1146 transport_proto, is_start);
1147 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1148 transport_proto, is_start);
1149 }
1150}
1151
1152void
1153application_setup_proxy (application_t * app)
1154{
1155 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001156 transport_proto_t tp;
1157
Florin Coras7999e832017-10-31 01:51:04 -07001158 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001159
1160 /* *INDENT-OFF* */
1161 transport_proto_foreach (tp, ({
1162 if (transports & (1 << tp))
1163 application_start_stop_proxy (app, tp, 1);
1164 }));
1165 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -07001166}
1167
1168void
1169application_remove_proxy (application_t * app)
1170{
1171 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001172 transport_proto_t tp;
1173
Florin Coras7999e832017-10-31 01:51:04 -07001174 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001175
1176 /* *INDENT-OFF* */
1177 transport_proto_foreach (tp, ({
1178 if (transports & (1 << tp))
1179 application_start_stop_proxy (app, tp, 0);
1180 }));
1181 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -07001182}
1183
Florin Corasa332c462018-01-31 06:52:17 -08001184segment_manager_properties_t *
1185application_segment_manager_properties (application_t * app)
1186{
1187 return &app->sm_properties;
1188}
1189
1190segment_manager_properties_t *
1191application_get_segment_manager_properties (u32 app_index)
1192{
1193 application_t *app = application_get (app_index);
1194 return &app->sm_properties;
1195}
1196
Florin Coras3c2fed52018-07-04 04:15:05 -07001197static inline int
1198app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
1199{
Florin Coras52207f12018-07-12 14:48:06 -07001200 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
Florin Coras3c2fed52018-07-04 04:15:05 -07001201 {
1202 clib_warning ("evt q full");
1203 svm_msg_q_free_msg (mq, msg);
1204 if (lock)
1205 svm_msg_q_unlock (mq);
1206 return -1;
1207 }
Florin Coras52207f12018-07-12 14:48:06 -07001208
1209 if (lock)
1210 {
1211 svm_msg_q_add_and_unlock (mq, msg);
1212 return 0;
1213 }
1214
1215 /* Even when not locking the ring, we must wait for queue mutex */
1216 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
1217 {
1218 clib_warning ("msg q add returned");
1219 return -1;
1220 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001221 return 0;
1222}
1223
1224static inline int
Florin Coras15531972018-08-12 23:50:53 -07001225app_send_io_evt_rx (app_worker_t * app_wrk, stream_session_t * s, u8 lock)
Florin Coras3c2fed52018-07-04 04:15:05 -07001226{
Florin Coras52207f12018-07-12 14:48:06 -07001227 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -07001228 svm_msg_q_msg_t msg;
1229 svm_msg_q_t *mq;
1230
Florin Coras460dce62018-07-27 05:45:06 -07001231 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
1232 && s->session_state != SESSION_STATE_LISTENING))
Florin Coras3c2fed52018-07-04 04:15:05 -07001233 {
1234 /* Session is closed so app will never clean up. Flush rx fifo */
Florin Coras84099552018-07-22 12:59:30 -07001235 if (s->session_state == SESSION_STATE_CLOSED)
1236 svm_fifo_dequeue_drop_all (s->server_rx_fifo);
Florin Coras3c2fed52018-07-04 04:15:05 -07001237 return 0;
1238 }
1239
Florin Coras15531972018-08-12 23:50:53 -07001240 if (app_worker_application_is_builtin (app_wrk))
1241 {
1242 application_t *app = application_get (app_wrk->app_index);
1243 return app->cb_fns.builtin_app_rx_callback (s);
1244 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001245
Florin Coras54693d22018-07-17 10:46:29 -07001246 if (svm_fifo_has_event (s->server_rx_fifo)
1247 || svm_fifo_is_empty (s->server_rx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -07001248 return 0;
1249
Florin Coras15531972018-08-12 23:50:53 -07001250 mq = app_wrk->event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001251 if (lock)
1252 svm_msg_q_lock (mq);
1253
1254 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1255 {
1256 clib_warning ("evt q rings full");
1257 if (lock)
1258 svm_msg_q_unlock (mq);
1259 return -1;
1260 }
1261
1262 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1263 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1264
Florin Coras52207f12018-07-12 14:48:06 -07001265 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -07001266 evt->fifo = s->server_rx_fifo;
1267 evt->event_type = FIFO_EVENT_APP_RX;
1268
Florin Coras41c9e042018-09-11 00:10:41 -07001269 (void) svm_fifo_set_event (s->server_rx_fifo);
1270
Florin Coras54693d22018-07-17 10:46:29 -07001271 if (app_enqueue_evt (mq, &msg, lock))
1272 return -1;
Florin Coras54693d22018-07-17 10:46:29 -07001273 return 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001274}
1275
1276static inline int
Florin Coras15531972018-08-12 23:50:53 -07001277app_send_io_evt_tx (app_worker_t * app_wrk, stream_session_t * s, u8 lock)
Florin Coras3c2fed52018-07-04 04:15:05 -07001278{
1279 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -07001280 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -07001281 svm_msg_q_msg_t msg;
1282
Florin Coras15531972018-08-12 23:50:53 -07001283 if (app_worker_application_is_builtin (app_wrk))
Florin Coras3c2fed52018-07-04 04:15:05 -07001284 return 0;
1285
Florin Coras15531972018-08-12 23:50:53 -07001286 mq = app_wrk->event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001287 if (lock)
1288 svm_msg_q_lock (mq);
1289
1290 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1291 {
1292 clib_warning ("evt q rings full");
1293 if (lock)
1294 svm_msg_q_unlock (mq);
1295 return -1;
1296 }
1297
1298 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1299 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1300
Florin Coras52207f12018-07-12 14:48:06 -07001301 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -07001302 evt->event_type = FIFO_EVENT_APP_TX;
1303 evt->fifo = s->server_tx_fifo;
1304
1305 return app_enqueue_evt (mq, &msg, lock);
1306}
1307
1308/* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001309typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras3c2fed52018-07-04 04:15:05 -07001310 stream_session_t *s,
1311 u8 lock);
Florin Coras460dce62018-07-27 05:45:06 -07001312static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = {
Florin Coras3c2fed52018-07-04 04:15:05 -07001313 app_send_io_evt_rx,
Florin Coras460dce62018-07-27 05:45:06 -07001314 0,
Florin Coras3c2fed52018-07-04 04:15:05 -07001315 app_send_io_evt_tx,
1316};
1317/* *INDENT-ON* */
1318
1319/**
1320 * Send event to application
1321 *
Florin Coras21795132018-09-09 09:40:51 -07001322 * Logic from queue perspective is non-blocking. If there's
1323 * not enough space to enqueue a message, we return.
Florin Coras3c2fed52018-07-04 04:15:05 -07001324 */
1325int
Florin Corasab2f6db2018-08-31 14:31:41 -07001326app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -07001327{
1328 ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
1329 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
1330}
1331
Florin Coras21795132018-09-09 09:40:51 -07001332/**
1333 * Send event to application
1334 *
1335 * Logic from queue perspective is blocking. However, if queue is full,
1336 * we return.
1337 */
Florin Coras3c2fed52018-07-04 04:15:05 -07001338int
Florin Corasab2f6db2018-08-31 14:31:41 -07001339app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s,
1340 u8 evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -07001341{
1342 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
1343}
1344
Florin Corasf8f516a2018-02-08 15:10:09 -08001345local_session_t *
Florin Corasab2f6db2018-08-31 14:31:41 -07001346application_local_session_alloc (app_worker_t * app_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001347{
1348 local_session_t *s;
Florin Corasab2f6db2018-08-31 14:31:41 -07001349 pool_get (app_wrk->local_sessions, s);
Dave Barachb7b92992018-10-17 10:38:51 -04001350 clib_memset (s, 0, sizeof (*s));
Florin Corasab2f6db2018-08-31 14:31:41 -07001351 s->app_wrk_index = app_wrk->wrk_index;
1352 s->session_index = s - app_wrk->local_sessions;
Florin Corasf8f516a2018-02-08 15:10:09 -08001353 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1354 return s;
1355}
1356
1357void
Florin Corasab2f6db2018-08-31 14:31:41 -07001358application_local_session_free (app_worker_t * app, local_session_t * s)
Florin Corasf8f516a2018-02-08 15:10:09 -08001359{
1360 pool_put (app->local_sessions, s);
1361 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -04001362 clib_memset (s, 0xfc, sizeof (*s));
Florin Corasf8f516a2018-02-08 15:10:09 -08001363}
1364
1365local_session_t *
Florin Coras15531972018-08-12 23:50:53 -07001366application_get_local_session (app_worker_t * app_wrk, u32 session_index)
Florin Corasf8f516a2018-02-08 15:10:09 -08001367{
Florin Coras15531972018-08-12 23:50:53 -07001368 if (pool_is_free_index (app_wrk->local_sessions, session_index))
Florin Coras99368312018-08-02 10:45:44 -07001369 return 0;
Florin Coras15531972018-08-12 23:50:53 -07001370 return pool_elt_at_index (app_wrk->local_sessions, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001371}
1372
1373local_session_t *
1374application_get_local_session_from_handle (session_handle_t handle)
1375{
Florin Coras15531972018-08-12 23:50:53 -07001376 app_worker_t *server_wrk;
1377 u32 session_index, server_wrk_index;
1378 local_session_parse_handle (handle, &server_wrk_index, &session_index);
1379 server_wrk = app_worker_get_if_valid (server_wrk_index);
1380 if (!server_wrk)
Florin Corasfcbda892018-08-23 19:27:03 -07001381 return 0;
Florin Coras15531972018-08-12 23:50:53 -07001382 return application_get_local_session (server_wrk, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001383}
1384
Florin Coras60116992018-08-27 09:52:18 -07001385local_session_t *
Florin Corasab2f6db2018-08-31 14:31:41 -07001386application_local_listen_session_alloc (application_t * app)
Florin Coras60116992018-08-27 09:52:18 -07001387{
Florin Corasab2f6db2018-08-31 14:31:41 -07001388 local_session_t *ll;
1389 pool_get (app->local_listen_sessions, ll);
Dave Barachb7b92992018-10-17 10:38:51 -04001390 clib_memset (ll, 0, sizeof (*ll));
Florin Corasab2f6db2018-08-31 14:31:41 -07001391 return ll;
Florin Coras60116992018-08-27 09:52:18 -07001392}
1393
Florin Corasab2f6db2018-08-31 14:31:41 -07001394u32
1395application_local_listener_index (application_t * app, local_session_t * ll)
Florin Corasf8f516a2018-02-08 15:10:09 -08001396{
Florin Corasab2f6db2018-08-31 14:31:41 -07001397 return (ll - app->local_listen_sessions);
1398}
1399
1400void
1401application_local_listen_session_free (application_t * app,
1402 local_session_t * ll)
1403{
1404 pool_put (app->local_listen_sessions, ll);
1405 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -04001406 clib_memset (ll, 0xfb, sizeof (*ll));
Florin Corasf8f516a2018-02-08 15:10:09 -08001407}
1408
1409int
Florin Corasab2f6db2018-08-31 14:31:41 -07001410application_start_local_listen (application_t * app,
1411 session_endpoint_extended_t * sep_ext,
Florin Corasf8f516a2018-02-08 15:10:09 -08001412 session_handle_t * handle)
1413{
Florin Corasab2f6db2018-08-31 14:31:41 -07001414 app_listener_t *app_listener;
1415 session_endpoint_t *sep;
1416 app_worker_t *app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001417 session_handle_t lh;
1418 local_session_t *ll;
1419 u32 table_index;
1420
Florin Corasab2f6db2018-08-31 14:31:41 -07001421 sep = (session_endpoint_t *) sep_ext;
Florin Coras15531972018-08-12 23:50:53 -07001422 table_index = application_local_session_table (app);
Florin Corasab2f6db2018-08-31 14:31:41 -07001423 app_wrk = app_worker_get (sep_ext->app_wrk_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001424
1425 /* An exact sep match, as opposed to session_lookup_local_listener */
1426 lh = session_lookup_endpoint_listener (table_index, sep, 1);
1427 if (lh != SESSION_INVALID_HANDLE)
Florin Corasab2f6db2018-08-31 14:31:41 -07001428 {
1429 ll = application_get_local_listener_w_handle (lh);
1430 if (ll->app_index != app->app_index)
1431 return VNET_API_ERROR_ADDRESS_IN_USE;
Florin Corasf8f516a2018-02-08 15:10:09 -08001432
Florin Corasab2f6db2018-08-31 14:31:41 -07001433 if (ll->app_wrk_index == app_wrk->wrk_index)
1434 return VNET_API_ERROR_ADDRESS_IN_USE;
1435
1436 app_listener = app_local_listener_get (app, ll->listener_db_index);
1437 app_listener->workers = clib_bitmap_set (app_listener->workers,
1438 app_wrk->wrk_map_index, 1);
1439 *handle = application_local_session_handle (ll);
1440 return 0;
1441 }
1442
1443 ll = application_local_listen_session_alloc (app);
Florin Corasf8f516a2018-02-08 15:10:09 -08001444 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
Florin Coras15531972018-08-12 23:50:53 -07001445 ll->app_wrk_index = app_wrk->app_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001446 ll->session_index = application_local_listener_index (app, ll);
1447 ll->port = sep_ext->port;
Florin Corasf8f516a2018-02-08 15:10:09 -08001448 /* Store the original session type for the unbind */
1449 ll->listener_session_type =
Florin Corasab2f6db2018-08-31 14:31:41 -07001450 session_type_from_proto_and_ip (sep_ext->transport_proto,
1451 sep_ext->is_ip4);
Florin Coras5fda7a32018-02-14 08:04:31 -08001452 ll->transport_listener_index = ~0;
Florin Corasab2f6db2018-08-31 14:31:41 -07001453 ll->app_index = app->app_index;
1454
1455 app_listener = app_local_listener_alloc (app);
1456 ll->listener_db_index = app_listener->al_index;
1457 app_listener->workers = clib_bitmap_set (app_listener->workers,
1458 app_wrk->wrk_map_index, 1);
Florin Corasf8f516a2018-02-08 15:10:09 -08001459
1460 *handle = application_local_session_handle (ll);
1461 session_lookup_add_session_endpoint (table_index, sep, *handle);
1462
1463 return 0;
1464}
1465
1466/**
1467 * Clean up local session table. If we have a listener session use it to
1468 * find the port and proto. If not, the handle must be a local table handle
1469 * so parse it.
1470 */
1471int
Florin Corasab2f6db2018-08-31 14:31:41 -07001472application_stop_local_listen (u32 app_index, u32 wrk_map_index,
1473 session_handle_t lh)
Florin Corasf8f516a2018-02-08 15:10:09 -08001474{
1475 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Corasab2f6db2018-08-31 14:31:41 -07001476 u32 table_index, ll_index, server_index;
1477 app_listener_t *app_listener;
Florin Coras15531972018-08-12 23:50:53 -07001478 app_worker_t *server_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001479 stream_session_t *sl = 0;
1480 local_session_t *ll, *ls;
Florin Coras15531972018-08-12 23:50:53 -07001481 application_t *server;
Florin Corasf8f516a2018-02-08 15:10:09 -08001482
Florin Coras15531972018-08-12 23:50:53 -07001483 server = application_get (app_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001484 table_index = application_local_session_table (server);
1485
1486 /* We have both local and global table binds. Figure from global what
1487 * the sep we should be cleaning up is.
1488 */
1489 if (!session_handle_is_local (lh))
1490 {
1491 sl = listen_session_get_from_handle (lh);
1492 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1493 {
1494 clib_warning ("broken listener");
1495 return -1;
1496 }
1497 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1498 if (lh == SESSION_INVALID_HANDLE)
1499 return -1;
1500 }
1501
Florin Corasab2f6db2018-08-31 14:31:41 -07001502 local_session_parse_handle (lh, &server_index, &ll_index);
1503 if (PREDICT_FALSE (server_index != app_index))
Florin Coras15531972018-08-12 23:50:53 -07001504 {
1505 clib_warning ("app %u does not own local handle 0x%lx", app_index, lh);
Florin Corasab2f6db2018-08-31 14:31:41 -07001506 return -1;
Florin Coras15531972018-08-12 23:50:53 -07001507 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001508
1509 ll = application_get_local_listen_session (server, ll_index);
Florin Coras15531972018-08-12 23:50:53 -07001510 if (PREDICT_FALSE (!ll))
Florin Corasf8f516a2018-02-08 15:10:09 -08001511 {
1512 clib_warning ("no local listener");
1513 return -1;
1514 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001515
Florin Corasab2f6db2018-08-31 14:31:41 -07001516 app_listener = app_local_listener_get (server, ll->listener_db_index);
1517 if (!clib_bitmap_get (app_listener->workers, wrk_map_index))
1518 {
1519 clib_warning ("app wrk %u not listening on handle %lu", wrk_map_index,
1520 lh);
1521 return -1;
1522 }
1523
1524 server_wrk = application_get_worker (server, wrk_map_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001525 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001526 pool_foreach (ls, server_wrk->local_sessions, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08001527 if (ls->listener_index == ll->session_index)
Florin Coras15531972018-08-12 23:50:53 -07001528 application_local_session_disconnect (server_wrk->app_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001529 }));
1530 /* *INDENT-ON* */
Florin Corasab2f6db2018-08-31 14:31:41 -07001531
1532 clib_bitmap_set_no_check (app_listener->workers, wrk_map_index, 0);
1533 if (clib_bitmap_is_zero (app_listener->workers))
1534 {
1535 app_local_listener_free (server, app_listener);
1536 application_local_listener_session_endpoint (ll, &sep);
1537 session_lookup_del_session_endpoint (table_index, &sep);
1538 application_local_listen_session_free (server, ll);
1539 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001540
1541 return 0;
1542}
1543
Florin Coras99368312018-08-02 10:45:44 -07001544static void
1545application_local_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
1546{
1547 int fd;
1548
1549 /*
1550 * segment manager initializes only the producer eventds, since vpp is
1551 * typically the producer. But for local sessions, we also pass to the
1552 * apps the mqs they listen on for events from peer apps, so they are also
1553 * consumer fds.
1554 */
1555 fd = svm_msg_q_get_producer_eventfd (sq);
1556 svm_msg_q_set_consumer_eventfd (sq, fd);
1557 fd = svm_msg_q_get_producer_eventfd (cq);
1558 svm_msg_q_set_consumer_eventfd (cq, fd);
1559}
1560
Florin Corasf8f516a2018-02-08 15:10:09 -08001561int
Florin Coras15531972018-08-12 23:50:53 -07001562application_local_session_connect (app_worker_t * client_wrk,
1563 app_worker_t * server_wrk,
Florin Corasf8f516a2018-02-08 15:10:09 -08001564 local_session_t * ll, u32 opaque)
1565{
1566 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1567 segment_manager_properties_t *props, *cprops;
Florin Coras54693d22018-07-17 10:46:29 -07001568 u32 round_rx_fifo_sz, round_tx_fifo_sz;
Florin Corasf8f516a2018-02-08 15:10:09 -08001569 int rv, has_transport, seg_index;
1570 svm_fifo_segment_private_t *seg;
Florin Coras15531972018-08-12 23:50:53 -07001571 application_t *server, *client;
Florin Corasf8f516a2018-02-08 15:10:09 -08001572 segment_manager_t *sm;
1573 local_session_t *ls;
Florin Coras3c2fed52018-07-04 04:15:05 -07001574 svm_msg_q_t *sq, *cq;
Florin Corasf8f516a2018-02-08 15:10:09 -08001575
Florin Corasab2f6db2018-08-31 14:31:41 -07001576 ls = application_local_session_alloc (server_wrk);
Florin Coras15531972018-08-12 23:50:53 -07001577 server = application_get (server_wrk->app_index);
1578 client = application_get (client_wrk->app_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001579
1580 props = application_segment_manager_properties (server);
1581 cprops = application_segment_manager_properties (client);
1582 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -07001583 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
Florin Coras54693d22018-07-17 10:46:29 -07001584 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
1585 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
1586 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
Florin Corasf8f516a2018-02-08 15:10:09 -08001587
1588 has_transport = session_has_transport ((stream_session_t *) ll);
1589 if (!has_transport)
1590 {
1591 /* Local sessions don't have backing transport */
1592 ls->port = ll->port;
Florin Coras15531972018-08-12 23:50:53 -07001593 sm = application_get_local_segment_manager (server_wrk);
Florin Corasf8f516a2018-02-08 15:10:09 -08001594 }
1595 else
1596 {
1597 stream_session_t *sl = (stream_session_t *) ll;
1598 transport_connection_t *tc;
1599 tc = listen_session_get_transport (sl);
1600 ls->port = tc->lcl_port;
Florin Coras15531972018-08-12 23:50:53 -07001601 sm = app_worker_get_listen_segment_manager (server_wrk, sl);
Florin Corasf8f516a2018-02-08 15:10:09 -08001602 }
1603
1604 seg_index = segment_manager_add_segment (sm, seg_size);
1605 if (seg_index < 0)
1606 {
1607 clib_warning ("failed to add new cut-through segment");
1608 return seg_index;
1609 }
1610 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -07001611 sq = segment_manager_alloc_queue (seg, props);
1612 cq = segment_manager_alloc_queue (seg, cprops);
1613
1614 if (props->use_mq_eventfd)
1615 application_local_session_fix_eventds (sq, cq);
1616
Florin Corasf8f516a2018-02-08 15:10:09 -08001617 ls->server_evt_q = pointer_to_uword (sq);
1618 ls->client_evt_q = pointer_to_uword (cq);
1619 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1620 props->tx_fifo_size,
1621 &ls->server_rx_fifo,
1622 &ls->server_tx_fifo);
1623 if (rv)
1624 {
1625 clib_warning ("failed to add fifos in cut-through segment");
1626 segment_manager_segment_reader_unlock (sm);
1627 goto failed;
1628 }
Florin Coras326b81e2018-10-04 19:03:05 -07001629 ls->server_rx_fifo->ct_session_index = ls->session_index;
1630 ls->server_tx_fifo->ct_session_index = ls->session_index;
Florin Corasf8f516a2018-02-08 15:10:09 -08001631 ls->svm_segment_index = seg_index;
1632 ls->listener_index = ll->session_index;
Florin Coras15531972018-08-12 23:50:53 -07001633 ls->client_wrk_index = client_wrk->wrk_index;
Florin Corasf8f516a2018-02-08 15:10:09 -08001634 ls->client_opaque = opaque;
1635 ls->listener_session_type = ll->session_type;
Florin Corasab2f6db2018-08-31 14:31:41 -07001636 ls->session_state = SESSION_STATE_READY;
Florin Corasf8f516a2018-02-08 15:10:09 -08001637
1638 if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
1639 &seg->ssvm)))
1640 {
1641 clib_warning ("failed to notify server of new segment");
1642 segment_manager_segment_reader_unlock (sm);
1643 goto failed;
1644 }
1645 segment_manager_segment_reader_unlock (sm);
1646 if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
1647 {
1648 clib_warning ("failed to send accept cut-through notify to server");
1649 goto failed;
1650 }
1651 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1652 application_local_session_connect_notify (ls);
1653
1654 return 0;
1655
1656failed:
1657 if (!has_transport)
1658 segment_manager_del_segment (sm, seg);
1659 return rv;
1660}
1661
1662static uword
1663application_client_local_connect_key (local_session_t * ls)
1664{
Florin Coras15531972018-08-12 23:50:53 -07001665 return ((uword) ls->app_wrk_index << 32 | (uword) ls->session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001666}
1667
1668static void
Florin Coras15531972018-08-12 23:50:53 -07001669application_client_local_connect_key_parse (uword key, u32 * app_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08001670 u32 * session_index)
1671{
Florin Coras15531972018-08-12 23:50:53 -07001672 *app_wrk_index = key >> 32;
Florin Corasf8f516a2018-02-08 15:10:09 -08001673 *session_index = key & 0xFFFFFFFF;
1674}
1675
1676int
1677application_local_session_connect_notify (local_session_t * ls)
1678{
1679 svm_fifo_segment_private_t *seg;
Florin Coras15531972018-08-12 23:50:53 -07001680 app_worker_t *client_wrk, *server_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001681 segment_manager_t *sm;
Florin Coras15531972018-08-12 23:50:53 -07001682 application_t *client;
Florin Corasf8f516a2018-02-08 15:10:09 -08001683 int rv, is_fail = 0;
1684 uword client_key;
1685
Florin Coras15531972018-08-12 23:50:53 -07001686 client_wrk = app_worker_get (ls->client_wrk_index);
1687 server_wrk = app_worker_get (ls->app_wrk_index);
1688 client = application_get (client_wrk->app_index);
1689
1690 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001691 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1692 if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1693 &seg->ssvm)))
1694 {
1695 clib_warning ("failed to notify client %u of new segment",
Florin Coras15531972018-08-12 23:50:53 -07001696 ls->client_wrk_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001697 segment_manager_segment_reader_unlock (sm);
Florin Coras15531972018-08-12 23:50:53 -07001698 application_local_session_disconnect (ls->client_wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001699 is_fail = 1;
1700 }
1701 else
1702 {
1703 segment_manager_segment_reader_unlock (sm);
1704 }
1705
Florin Coras15531972018-08-12 23:50:53 -07001706 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
1707 ls->client_opaque,
Florin Corasf8f516a2018-02-08 15:10:09 -08001708 (stream_session_t *) ls,
1709 is_fail);
1710
1711 client_key = application_client_local_connect_key (ls);
Florin Coras15531972018-08-12 23:50:53 -07001712 hash_set (client_wrk->local_connects, client_key, client_key);
Florin Corasf8f516a2018-02-08 15:10:09 -08001713 return 0;
1714}
1715
1716int
Florin Coras15531972018-08-12 23:50:53 -07001717application_local_session_cleanup (app_worker_t * client_wrk,
1718 app_worker_t * server_wrk,
Florin Corasf6647e02018-03-23 22:56:43 -07001719 local_session_t * ls)
Florin Corasf8f516a2018-02-08 15:10:09 -08001720{
1721 svm_fifo_segment_private_t *seg;
Florin Corasa44d6b12018-10-03 14:29:10 -07001722 stream_session_t *listener;
Florin Corasf8f516a2018-02-08 15:10:09 -08001723 segment_manager_t *sm;
1724 uword client_key;
Florin Corasf6647e02018-03-23 22:56:43 -07001725 u8 has_transport;
1726
Florin Corasa44d6b12018-10-03 14:29:10 -07001727 /* Retrieve listener transport type as it is the one that decides where
1728 * the fifos are allocated */
1729 has_transport = application_local_session_listener_has_transport (ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001730 if (!has_transport)
Florin Coras15531972018-08-12 23:50:53 -07001731 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001732 else
Florin Corasa44d6b12018-10-03 14:29:10 -07001733 {
1734 listener = listen_session_get (ls->listener_index);
1735 sm = app_worker_get_listen_segment_manager (server_wrk, listener);
1736 }
Florin Corasf6647e02018-03-23 22:56:43 -07001737
1738 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
Florin Coras15531972018-08-12 23:50:53 -07001739 if (client_wrk)
Florin Corasa44d6b12018-10-03 14:29:10 -07001740 {
1741 client_key = application_client_local_connect_key (ls);
1742 hash_unset (client_wrk->local_connects, client_key);
1743 }
Florin Corasf6647e02018-03-23 22:56:43 -07001744
1745 if (!has_transport)
1746 {
Florin Coras15531972018-08-12 23:50:53 -07001747 application_t *server = application_get (server_wrk->app_index);
Florin Corasf6647e02018-03-23 22:56:43 -07001748 server->cb_fns.del_segment_callback (server->api_client_index,
1749 &seg->ssvm);
Florin Coras15531972018-08-12 23:50:53 -07001750 if (client_wrk)
1751 {
1752 application_t *client = application_get (client_wrk->app_index);
1753 client->cb_fns.del_segment_callback (client->api_client_index,
1754 &seg->ssvm);
1755 }
Florin Corasf6647e02018-03-23 22:56:43 -07001756 segment_manager_del_segment (sm, seg);
1757 }
1758
Florin Corasab2f6db2018-08-31 14:31:41 -07001759 application_local_session_free (server_wrk, ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001760
1761 return 0;
1762}
1763
1764int
Florin Corasab2f6db2018-08-31 14:31:41 -07001765application_local_session_disconnect (u32 app_index, local_session_t * ls)
Florin Corasf6647e02018-03-23 22:56:43 -07001766{
Florin Coras15531972018-08-12 23:50:53 -07001767 app_worker_t *client_wrk, *server_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -07001768 u8 is_server = 0, is_client = 0;
1769 application_t *app;
1770
1771 app = application_get_if_valid (app_index);
1772 if (!app)
1773 return 0;
Florin Corasf8f516a2018-02-08 15:10:09 -08001774
Florin Coras15531972018-08-12 23:50:53 -07001775 client_wrk = app_worker_get_if_valid (ls->client_wrk_index);
1776 server_wrk = app_worker_get (ls->app_wrk_index);
1777
Florin Corasab2f6db2018-08-31 14:31:41 -07001778 if (server_wrk->app_index == app_index)
1779 is_server = 1;
1780 else if (client_wrk && client_wrk->app_index == app_index)
1781 is_client = 1;
1782
1783 if (!is_server && !is_client)
1784 {
1785 clib_warning ("app %u is neither client nor server for session 0x%lx",
1786 app_index, application_local_session_handle (ls));
1787 return VNET_API_ERROR_INVALID_VALUE;
1788 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001789
1790 if (ls->session_state == SESSION_STATE_CLOSED)
Florin Coras15531972018-08-12 23:50:53 -07001791 return application_local_session_cleanup (client_wrk, server_wrk, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001792
Florin Corasab2f6db2018-08-31 14:31:41 -07001793 if (app_index == ls->client_wrk_index)
Florin Corasf8f516a2018-02-08 15:10:09 -08001794 {
Florin Coras15531972018-08-12 23:50:53 -07001795 mq_send_local_session_disconnected_cb (ls->app_wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001796 }
1797 else
1798 {
Florin Coras15531972018-08-12 23:50:53 -07001799 if (!client_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001800 {
Florin Coras15531972018-08-12 23:50:53 -07001801 return application_local_session_cleanup (client_wrk, server_wrk,
1802 ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001803 }
1804 else if (ls->session_state < SESSION_STATE_READY)
1805 {
Florin Coras15531972018-08-12 23:50:53 -07001806 application_t *client = application_get (client_wrk->app_index);
1807 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08001808 ls->client_opaque,
1809 (stream_session_t *) ls,
1810 1 /* is_fail */ );
1811 ls->session_state = SESSION_STATE_CLOSED;
Florin Coras15531972018-08-12 23:50:53 -07001812 return application_local_session_cleanup (client_wrk, server_wrk,
1813 ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001814 }
1815 else
1816 {
Florin Coras15531972018-08-12 23:50:53 -07001817 mq_send_local_session_disconnected_cb (client_wrk->wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001818 }
1819 }
1820
1821 ls->session_state = SESSION_STATE_CLOSED;
1822
1823 return 0;
1824}
1825
Florin Corasf6647e02018-03-23 22:56:43 -07001826int
Florin Coras15531972018-08-12 23:50:53 -07001827application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index)
Florin Corasf6647e02018-03-23 22:56:43 -07001828{
Florin Coras15531972018-08-12 23:50:53 -07001829 app_worker_t *app_wrk;
Florin Corasf6647e02018-03-23 22:56:43 -07001830 local_session_t *ls;
Florin Coras15531972018-08-12 23:50:53 -07001831 app_wrk = app_worker_get (app_wrk_index);
1832 ls = application_get_local_session (app_wrk, ls_index);
1833 return application_local_session_disconnect (app_wrk_index, ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001834}
1835
Florin Corasf8f516a2018-02-08 15:10:09 -08001836void
Florin Corasab2f6db2018-08-31 14:31:41 -07001837app_worker_local_sessions_free (app_worker_t * app_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001838{
Florin Corasab2f6db2018-08-31 14:31:41 -07001839 u32 index, server_wrk_index, session_index;
Florin Corasf8f516a2018-02-08 15:10:09 -08001840 u64 handle, *handles = 0;
Florin Coras15531972018-08-12 23:50:53 -07001841 app_worker_t *server_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -07001842 segment_manager_t *sm;
1843 local_session_t *ls;
Florin Corasf8f516a2018-02-08 15:10:09 -08001844 int i;
1845
1846 /*
Florin Corasf8f516a2018-02-08 15:10:09 -08001847 * Local sessions
1848 */
Florin Coras15531972018-08-12 23:50:53 -07001849 if (app_wrk->local_sessions)
Florin Corasf8f516a2018-02-08 15:10:09 -08001850 {
1851 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001852 pool_foreach (ls, app_wrk->local_sessions, ({
1853 application_local_session_disconnect (app_wrk->wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001854 }));
1855 /* *INDENT-ON* */
1856 }
1857
1858 /*
1859 * Local connects
1860 */
1861 vec_reset_length (handles);
1862 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001863 hash_foreach (handle, index, app_wrk->local_connects, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08001864 vec_add1 (handles, handle);
1865 }));
1866 /* *INDENT-ON* */
1867
1868 for (i = 0; i < vec_len (handles); i++)
1869 {
Florin Coras15531972018-08-12 23:50:53 -07001870 application_client_local_connect_key_parse (handles[i],
1871 &server_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08001872 &session_index);
Florin Coras15531972018-08-12 23:50:53 -07001873 server_wrk = app_worker_get_if_valid (server_wrk_index);
1874 if (server_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001875 {
Florin Coras15531972018-08-12 23:50:53 -07001876 ls = application_get_local_session (server_wrk, session_index);
1877 application_local_session_disconnect (app_wrk->wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001878 }
1879 }
1880
Florin Coras15531972018-08-12 23:50:53 -07001881 sm = segment_manager_get (app_wrk->local_segment_manager);
1882 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Corasf8f516a2018-02-08 15:10:09 -08001883 segment_manager_del (sm);
1884}
1885
Florin Coras371ca502018-02-21 12:07:41 -08001886clib_error_t *
1887vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1888{
1889 application_t *app;
1890 app = application_get (a->app_index);
1891 if (!app)
1892 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1893 0, "app %u doesn't exist", a->app_index);
1894 app->tls_cert = vec_dup (a->cert);
1895 return 0;
1896}
1897
1898clib_error_t *
1899vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1900{
1901 application_t *app;
1902 app = application_get (a->app_index);
1903 if (!app)
1904 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1905 0, "app %u doesn't exist", a->app_index);
1906 app->tls_key = vec_dup (a->key);
1907 return 0;
1908}
1909
Dave Barach68b0fb02017-02-28 15:15:56 -05001910u8 *
Florin Coras15531972018-08-12 23:50:53 -07001911format_app_worker_listener (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -05001912{
Florin Coras15531972018-08-12 23:50:53 -07001913 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001914 u64 handle = va_arg (*args, u64);
Florin Corasf8f516a2018-02-08 15:10:09 -08001915 u32 sm_index = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -05001916 int verbose = va_arg (*args, int);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001917 stream_session_t *listener;
Florin Coras15531972018-08-12 23:50:53 -07001918 application_t *app;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001919 u8 *app_name, *str;
Dave Barach68b0fb02017-02-28 15:15:56 -05001920
Florin Coras15531972018-08-12 23:50:53 -07001921 if (!app_wrk)
Dave Barach68b0fb02017-02-28 15:15:56 -05001922 {
1923 if (verbose)
Florin Corasab2f6db2018-08-31 14:31:41 -07001924 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
1925 "Wrk", "API Client", "ListenerID", "SegManager");
Dave Barach68b0fb02017-02-28 15:15:56 -05001926 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001927 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
Dave Barach68b0fb02017-02-28 15:15:56 -05001928
1929 return s;
1930 }
1931
Florin Coras15531972018-08-12 23:50:53 -07001932 app = application_get (app_wrk->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001933 app_name = app_get_name_from_reg_index (app);
1934 listener = listen_session_get_from_handle (handle);
1935 str = format (0, "%U", format_stream_session, listener, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05001936
Dave Barach68b0fb02017-02-28 15:15:56 -05001937 if (verbose)
1938 {
Florin Coras74cac882018-09-07 09:13:15 -07001939 char buf[32];
1940 sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
1941 s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name,
1942 buf, app->api_client_index, handle, sm_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001943 }
1944 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001945 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001946
1947 vec_free (app_name);
1948 return s;
1949}
1950
Florin Coras15531972018-08-12 23:50:53 -07001951static void
1952application_format_listeners (application_t * app, int verbose)
1953{
1954 vlib_main_t *vm = vlib_get_main ();
1955 app_worker_map_t *wrk_map;
1956 app_worker_t *app_wrk;
1957 u32 sm_index;
1958 u64 handle;
1959
1960 if (!app)
1961 {
1962 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1963 0, 0, verbose);
1964 return;
1965 }
1966
1967 /* *INDENT-OFF* */
1968 pool_foreach (wrk_map, app->worker_maps, ({
1969 app_wrk = app_worker_get (wrk_map->wrk_index);
1970 if (hash_elts (app_wrk->listeners_table) == 0)
1971 continue;
1972 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1973 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1974 handle, sm_index, verbose);
1975 }));
1976 }));
1977 /* *INDENT-ON* */
1978}
1979
1980static void
1981app_worker_format_connects (app_worker_t * app_wrk, int verbose)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001982{
Florin Corasa332c462018-01-31 06:52:17 -08001983 svm_fifo_segment_private_t *fifo_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001984 vlib_main_t *vm = vlib_get_main ();
1985 segment_manager_t *sm;
1986 u8 *app_name, *s = 0;
Florin Coras15531972018-08-12 23:50:53 -07001987 application_t *app;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001988
1989 /* Header */
Florin Coras15531972018-08-12 23:50:53 -07001990 if (!app_wrk)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001991 {
1992 if (verbose)
1993 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1994 "API Client", "SegManager");
1995 else
1996 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1997 return;
1998 }
1999
Florin Coras15531972018-08-12 23:50:53 -07002000 app = application_get (app_wrk->app_index);
2001 if (app_wrk->connects_seg_manager == (u32) ~ 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07002002 return;
2003
2004 app_name = app_get_name_from_reg_index (app);
2005
2006 /* Across all fifo segments */
Florin Coras15531972018-08-12 23:50:53 -07002007 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002008
Florin Corasa332c462018-01-31 06:52:17 -08002009 /* *INDENT-OFF* */
2010 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
2011 svm_fifo_t *fifo;
2012 u8 *str;
2013
2014 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
2015 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -07002016 {
Florin Coras6cf30ad2017-04-04 23:08:23 -07002017 u32 session_index, thread_index;
2018 stream_session_t *session;
2019
Florin Corasa5464812017-04-19 13:00:05 -07002020 session_index = fifo->master_session_index;
2021 thread_index = fifo->master_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07002022
Florin Corascea194d2017-10-02 00:18:51 -07002023 session = session_get (session_index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002024 str = format (0, "%U", format_stream_session, session, verbose);
2025
2026 if (verbose)
2027 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
Florin Coras15531972018-08-12 23:50:53 -07002028 app->api_client_index, app_wrk->connects_seg_manager);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002029 else
2030 s = format (s, "%-40s%-20s", str, app_name);
2031
2032 vlib_cli_output (vm, "%v", s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002033 vec_reset_length (s);
2034 vec_free (str);
Dave Barach10d8cc62017-05-30 09:30:07 -04002035
2036 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -07002037 }
Florin Corasa332c462018-01-31 06:52:17 -08002038 vec_free (s);
2039 }));
2040 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07002041
2042 vec_free (app_name);
2043}
2044
Florin Coras15531972018-08-12 23:50:53 -07002045static void
2046application_format_connects (application_t * app, int verbose)
2047{
2048 app_worker_map_t *wrk_map;
2049 app_worker_t *app_wrk;
2050
2051 if (!app)
2052 {
2053 app_worker_format_connects (0, verbose);
2054 return;
2055 }
2056
2057 /* *INDENT-OFF* */
2058 pool_foreach (wrk_map, app->worker_maps, ({
2059 app_wrk = app_worker_get (wrk_map->wrk_index);
2060 app_worker_format_connects (app_wrk, verbose);
2061 }));
2062 /* *INDENT-ON* */
2063}
2064
2065static void
2066app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08002067{
2068 vlib_main_t *vm = vlib_get_main ();
2069 local_session_t *ls;
2070 transport_proto_t tp;
2071 u8 *conn = 0;
2072
2073 /* Header */
Florin Coras15531972018-08-12 23:50:53 -07002074 if (app_wrk == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -08002075 {
2076 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
2077 "ClientApp");
2078 return;
2079 }
2080
Florin Coras15531972018-08-12 23:50:53 -07002081 if (!pool_elts (app_wrk->local_sessions)
2082 && !pool_elts (app_wrk->local_connects))
2083 return;
2084
Florin Corasf8f516a2018-02-08 15:10:09 -08002085 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002086 pool_foreach (ls, app_wrk->local_sessions, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08002087 tp = session_type_transport_proto(ls->listener_session_type);
2088 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2089 ls->port);
Florin Coras15531972018-08-12 23:50:53 -07002090 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_wrk_index,
2091 ls->client_wrk_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08002092 vec_reset_length (conn);
2093 }));
2094 /* *INDENT-ON* */
2095
2096 vec_free (conn);
2097}
2098
Florin Coras15531972018-08-12 23:50:53 -07002099static void
2100application_format_local_sessions (application_t * app, int verbose)
2101{
Florin Corasab2f6db2018-08-31 14:31:41 -07002102 vlib_main_t *vm = vlib_get_main ();
Florin Coras15531972018-08-12 23:50:53 -07002103 app_worker_map_t *wrk_map;
2104 app_worker_t *app_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -07002105 transport_proto_t tp;
2106 local_session_t *ls;
2107 u8 *conn = 0;
Florin Coras15531972018-08-12 23:50:53 -07002108
2109 if (!app)
2110 {
2111 app_worker_format_local_sessions (0, verbose);
2112 return;
2113 }
2114
Florin Corasab2f6db2018-08-31 14:31:41 -07002115 /*
2116 * Format local listeners
2117 */
2118
2119 /* *INDENT-OFF* */
2120 pool_foreach (ls, app->local_listen_sessions, ({
2121 tp = session_type_transport_proto (ls->listener_session_type);
2122 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2123 ls->port);
2124 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
2125 vec_reset_length (conn);
2126 }));
2127 /* *INDENT-ON* */
2128
2129 /*
2130 * Format local accepted/connected sessions
2131 */
Florin Coras15531972018-08-12 23:50:53 -07002132 /* *INDENT-OFF* */
2133 pool_foreach (wrk_map, app->worker_maps, ({
2134 app_wrk = app_worker_get (wrk_map->wrk_index);
2135 app_worker_format_local_sessions (app_wrk, verbose);
2136 }));
2137 /* *INDENT-ON* */
2138}
2139
2140static void
2141app_worker_format_local_connects (app_worker_t * app, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08002142{
2143 vlib_main_t *vm = vlib_get_main ();
Florin Coras15531972018-08-12 23:50:53 -07002144 u32 app_wrk_index, session_index;
2145 app_worker_t *server_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08002146 local_session_t *ls;
2147 uword client_key;
2148 u64 value;
2149
2150 /* Header */
2151 if (app == 0)
2152 {
2153 if (verbose)
2154 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
2155 "Peer App", "SegManager");
2156 else
2157 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
2158 "Peer App");
2159 return;
2160 }
2161
Florin Coras15531972018-08-12 23:50:53 -07002162 if (!app->local_connects)
2163 return;
2164
Florin Corasf8f516a2018-02-08 15:10:09 -08002165 /* *INDENT-OFF* */
2166 hash_foreach (client_key, value, app->local_connects, ({
Florin Coras15531972018-08-12 23:50:53 -07002167 application_client_local_connect_key_parse (client_key, &app_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08002168 &session_index);
Florin Coras15531972018-08-12 23:50:53 -07002169 server_wrk = app_worker_get (app_wrk_index);
2170 ls = application_get_local_session (server_wrk, session_index);
2171 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_wrk_index,
2172 ls->client_wrk_index);
2173 }));
2174 /* *INDENT-ON* */
2175}
2176
2177static void
2178application_format_local_connects (application_t * app, int verbose)
2179{
2180 app_worker_map_t *wrk_map;
2181 app_worker_t *app_wrk;
2182
2183 if (!app)
2184 {
2185 app_worker_format_local_connects (0, verbose);
2186 return;
2187 }
2188
2189 /* *INDENT-OFF* */
2190 pool_foreach (wrk_map, app->worker_maps, ({
2191 app_wrk = app_worker_get (wrk_map->wrk_index);
2192 app_worker_format_local_connects (app_wrk, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002193 }));
2194 /* *INDENT-ON* */
2195}
2196
Florin Coras6cf30ad2017-04-04 23:08:23 -07002197u8 *
2198format_application (u8 * s, va_list * args)
2199{
2200 application_t *app = va_arg (*args, application_t *);
2201 CLIB_UNUSED (int verbose) = va_arg (*args, int);
Florin Corasad0c77f2017-11-09 18:00:15 -08002202 segment_manager_properties_t *props;
Florin Corascea194d2017-10-02 00:18:51 -07002203 const u8 *app_ns_name;
Florin Coras6cf30ad2017-04-04 23:08:23 -07002204 u8 *app_name;
2205
2206 if (app == 0)
2207 {
2208 if (verbose)
Florin Corascea194d2017-10-02 00:18:51 -07002209 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
Florin Coras078371e2018-05-11 09:20:12 -07002210 "API Client", "Namespace", "Add seg size", "Rx-f size",
2211 "Tx-f size");
Florin Coras6cf30ad2017-04-04 23:08:23 -07002212 else
Florin Coras0bee9ce2018-03-22 21:24:31 -07002213 s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
2214 "Namespace");
Florin Coras6cf30ad2017-04-04 23:08:23 -07002215 return s;
2216 }
2217
Florin Coras0bee9ce2018-03-22 21:24:31 -07002218 app_name = app_get_name (app);
Florin Corascea194d2017-10-02 00:18:51 -07002219 app_ns_name = app_namespace_id_from_index (app->ns_index);
Florin Corasa332c462018-01-31 06:52:17 -08002220 props = application_segment_manager_properties (app);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002221 if (verbose)
Florin Coras15531972018-08-12 23:50:53 -07002222 s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->app_index,
Florin Coras0bee9ce2018-03-22 21:24:31 -07002223 app_name, app->api_client_index, app->ns_index,
Florin Coras078371e2018-05-11 09:20:12 -07002224 format_memory_size, props->add_segment_size,
2225 format_memory_size, props->rx_fifo_size, format_memory_size,
Florin Coras0bee9ce2018-03-22 21:24:31 -07002226 props->tx_fifo_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002227 else
Florin Coras15531972018-08-12 23:50:53 -07002228 s = format (s, "%-10u%-20s%-15d%-40s", app->app_index, app_name,
Florin Coras8f107b32017-11-21 22:13:03 -08002229 app->api_client_index, app_ns_name);
Dave Barach68b0fb02017-02-28 15:15:56 -05002230 return s;
2231}
2232
Florin Corasf8f516a2018-02-08 15:10:09 -08002233void
2234application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
2235{
2236 application_t *app;
Florin Corasf8f516a2018-02-08 15:10:09 -08002237
Florin Coras15531972018-08-12 23:50:53 -07002238 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08002239 {
2240 vlib_cli_output (vm, "No active server bindings");
2241 return;
2242 }
2243
2244 if (do_local)
2245 {
2246 application_format_local_sessions (0, verbose);
2247 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002248 pool_foreach (app, app_main.app_pool, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08002249 application_format_local_sessions (app, verbose);
2250 }));
2251 /* *INDENT-ON* */
2252 }
2253 else
2254 {
Florin Coras15531972018-08-12 23:50:53 -07002255 application_format_listeners (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002256
2257 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002258 pool_foreach (app, app_main.app_pool, ({
2259 application_format_listeners (app, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002260 }));
2261 /* *INDENT-ON* */
2262 }
2263}
2264
2265void
2266application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
2267{
2268 application_t *app;
2269
Florin Coras15531972018-08-12 23:50:53 -07002270 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08002271 {
2272 vlib_cli_output (vm, "No active apps");
2273 return;
2274 }
2275
2276 if (do_local)
2277 {
2278 application_format_local_connects (0, verbose);
2279
2280 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002281 pool_foreach (app, app_main.app_pool, ({
2282 application_format_local_connects (app, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002283 }));
2284 /* *INDENT-ON* */
2285 }
2286 else
2287 {
2288 application_format_connects (0, verbose);
2289
2290 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002291 pool_foreach (app, app_main.app_pool, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08002292 application_format_connects (app, verbose);
2293 }));
2294 /* *INDENT-ON* */
2295 }
2296}
2297
Dave Barach68b0fb02017-02-28 15:15:56 -05002298static clib_error_t *
2299show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
2300 vlib_cli_command_t * cmd)
2301{
Florin Corasf8f516a2018-02-08 15:10:09 -08002302 int do_server = 0, do_client = 0, do_local = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002303 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05002304 int verbose = 0;
2305
Florin Corascea194d2017-10-02 00:18:51 -07002306 session_cli_return_if_not_enabled ();
Florin Corase04c2992017-03-01 08:17:34 -08002307
Dave Barach68b0fb02017-02-28 15:15:56 -05002308 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2309 {
2310 if (unformat (input, "server"))
2311 do_server = 1;
2312 else if (unformat (input, "client"))
2313 do_client = 1;
Florin Corasf8f516a2018-02-08 15:10:09 -08002314 else if (unformat (input, "local"))
2315 do_local = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05002316 else if (unformat (input, "verbose"))
2317 verbose = 1;
2318 else
2319 break;
2320 }
2321
2322 if (do_server)
Florin Corasf8f516a2018-02-08 15:10:09 -08002323 application_format_all_listeners (vm, do_local, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05002324
2325 if (do_client)
Florin Corasf8f516a2018-02-08 15:10:09 -08002326 application_format_all_clients (vm, do_local, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05002327
Florin Coras6cf30ad2017-04-04 23:08:23 -07002328 /* Print app related info */
2329 if (!do_server && !do_client)
2330 {
2331 vlib_cli_output (vm, "%U", format_application, 0, verbose);
Florin Corascea194d2017-10-02 00:18:51 -07002332 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002333 pool_foreach (app, app_main.app_pool, ({
Florin Corascea194d2017-10-02 00:18:51 -07002334 vlib_cli_output (vm, "%U", format_application, app, verbose);
2335 }));
2336 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07002337 }
2338
Dave Barach68b0fb02017-02-28 15:15:56 -05002339 return 0;
2340}
2341
Florin Corase04c2992017-03-01 08:17:34 -08002342/* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -05002343VLIB_CLI_COMMAND (show_app_command, static) =
2344{
Florin Corase04c2992017-03-01 08:17:34 -08002345 .path = "show app",
2346 .short_help = "show app [server|client] [verbose]",
2347 .function = show_app_command_fn,
2348};
2349/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05002350
2351/*
2352 * fd.io coding-style-patch-verification: ON
2353 *
2354 * Local Variables:
2355 * eval: (c-set-style "gnu")
2356 * End:
2357 */