blob: 8cf32896631799b51a48d7069751f701258030f5 [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);
28 memset (app_listener, 0, sizeof (*app_listener));
29 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)
45 memset (app_listener, 0xfa, sizeof (*app_listener));
46}
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);
53 memset (app_listener, 0, sizeof (*app_listener));
54 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)
70 memset (app_listener, 0xfa, sizeof (*app_listener));
71}
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);
78 memset (map, 0, sizeof (*map));
79 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);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700237 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);
487 memset (app_wrk, 0, sizeof (*app_wrk));
488 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);
560 /* .. and has no fifos, e.g. it might be used for redirected sessions,
561 * remove it */
562 if (!segment_manager_has_fifos (sm))
563 {
564 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
565 segment_manager_del (sm);
566 }
567 }
568
569 /*
570 * Local sessions
571 */
Florin Corasab2f6db2018-08-31 14:31:41 -0700572 app_worker_local_sessions_free (app_wrk);
Florin Coras15531972018-08-12 23:50:53 -0700573
574 pool_put (app_main.workers, app_wrk);
575 if (CLIB_DEBUG)
576 memset (app_wrk, 0xfe, sizeof (*app_wrk));
577}
578
579int
580app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk)
581{
582 app_worker_map_t *wrk_map;
583 app_worker_t *app_wrk;
584 segment_manager_t *sm;
585 int rv;
586
587 app_wrk = app_worker_alloc (app);
588 wrk_map = app_worker_map_alloc (app);
589 wrk_map->wrk_index = app_wrk->wrk_index;
590 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
591
592 /*
593 * Setup first segment manager
594 */
595 sm = segment_manager_new ();
596 sm->app_wrk_index = app_wrk->wrk_index;
597
598 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
599 app->sm_properties.prealloc_fifos)))
600 {
601 app_worker_free (app_wrk);
602 return rv;
603 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700604 sm->first_is_protected = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500605
Florin Corascea194d2017-10-02 00:18:51 -0700606 /*
Florin Coras15531972018-08-12 23:50:53 -0700607 * Setup app worker
Florin Corascea194d2017-10-02 00:18:51 -0700608 */
Florin Coras15531972018-08-12 23:50:53 -0700609 app_wrk->first_segment_manager = segment_manager_index (sm);
610 app_wrk->listeners_table = hash_create (0, sizeof (u64));
611 app_wrk->event_queue = segment_manager_event_queue (sm);
612 app_wrk->app_is_builtin = application_is_builtin (app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500613
Florin Corasf8f516a2018-02-08 15:10:09 -0800614 /*
615 * Segment manager for local sessions
616 */
617 sm = segment_manager_new ();
Florin Coras15531972018-08-12 23:50:53 -0700618 sm->app_wrk_index = app_wrk->wrk_index;
619 app_wrk->local_segment_manager = segment_manager_index (sm);
620 app_wrk->local_connects = hash_create (0, sizeof (u64));
621
622 *wrk = app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -0800623
Florin Coras6cf30ad2017-04-04 23:08:23 -0700624 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500625}
626
Florin Corasab2f6db2018-08-31 14:31:41 -0700627application_t *
628app_worker_get_app (u32 wrk_index)
629{
630 app_worker_t *app_wrk;
631 app_wrk = app_worker_get_if_valid (wrk_index);
632 if (!app_wrk)
633 return 0;
634 return application_get_if_valid (app_wrk->app_index);
635}
636
Florin Coras6cf30ad2017-04-04 23:08:23 -0700637static segment_manager_t *
Florin Corasab2f6db2018-08-31 14:31:41 -0700638app_worker_alloc_segment_manager (app_worker_t * app_wrk)
Dave Barach68b0fb02017-02-28 15:15:56 -0500639{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700640 segment_manager_t *sm = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500641
Florin Corasc87c91d2017-08-16 19:55:49 -0700642 /* If the first segment manager is not in use, don't allocate a new one */
Florin Coras15531972018-08-12 23:50:53 -0700643 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
644 && app_wrk->first_segment_manager_in_use == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500645 {
Florin Coras15531972018-08-12 23:50:53 -0700646 sm = segment_manager_get (app_wrk->first_segment_manager);
647 app_wrk->first_segment_manager_in_use = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700648 return sm;
Dave Barach68b0fb02017-02-28 15:15:56 -0500649 }
650
Florin Coras6cf30ad2017-04-04 23:08:23 -0700651 sm = segment_manager_new ();
Florin Coras15531972018-08-12 23:50:53 -0700652 sm->app_wrk_index = app_wrk->wrk_index;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700653
Florin Coras6cf30ad2017-04-04 23:08:23 -0700654 return sm;
655}
656
Florin Coras6cf30ad2017-04-04 23:08:23 -0700657int
Florin Corasab2f6db2018-08-31 14:31:41 -0700658app_worker_start_listen (app_worker_t * app_wrk, stream_session_t * ls)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700659{
660 segment_manager_t *sm;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700661
Florin Coras6cf30ad2017-04-04 23:08:23 -0700662 /* Allocate segment manager. All sessions derived out of a listen session
663 * have fifos allocated by the same segment manager. */
Florin Corasab2f6db2018-08-31 14:31:41 -0700664 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
665 return -1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700666
667 /* Add to app's listener table. Useful to find all child listeners
668 * when app goes down, although, just for unbinding this is not needed */
Florin Corasab2f6db2018-08-31 14:31:41 -0700669 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
670 segment_manager_index (sm));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700671
Florin Corasab2f6db2018-08-31 14:31:41 -0700672 if (!ls->server_rx_fifo
673 && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700674 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700675 if (session_alloc_fifos (sm, ls))
676 return -1;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700677 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700678 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700679}
680
Florin Coras6cf30ad2017-04-04 23:08:23 -0700681int
Florin Corasab2f6db2018-08-31 14:31:41 -0700682app_worker_stop_listen (app_worker_t * app_wrk, session_handle_t handle)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700683{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700684 segment_manager_t *sm;
Florin Corasab2f6db2018-08-31 14:31:41 -0700685 uword *sm_indexp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700686
Florin Corasab2f6db2018-08-31 14:31:41 -0700687 sm_indexp = hash_get (app_wrk->listeners_table, handle);
688 if (PREDICT_FALSE (!sm_indexp))
Florin Coras15531972018-08-12 23:50:53 -0700689 {
690 clib_warning ("listener handle was removed %llu!", handle);
691 return -1;
692 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700693
Florin Corasab2f6db2018-08-31 14:31:41 -0700694 sm = segment_manager_get (*sm_indexp);
695 if (app_wrk->first_segment_manager == *sm_indexp)
Florin Coras0e9c33b2017-08-14 22:33:41 -0700696 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700697 /* Delete sessions but don't remove segment manager */
Florin Coras15531972018-08-12 23:50:53 -0700698 app_wrk->first_segment_manager_in_use = 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700699 segment_manager_del_sessions (sm);
700 }
701 else
702 {
703 segment_manager_init_del (sm);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700704 }
Florin Coras15531972018-08-12 23:50:53 -0700705 hash_unset (app_wrk->listeners_table, handle);
Florin Corasab2f6db2018-08-31 14:31:41 -0700706
707 return 0;
708}
709
710/**
711 * Start listening local transport endpoint for requested transport.
712 *
713 * Creates a 'dummy' stream session with state LISTENING to be used in session
714 * lookups, prior to establishing connection. Requests transport to build
715 * it's own specific listening connection.
716 */
717int
718application_start_listen (application_t * app,
719 session_endpoint_extended_t * sep_ext,
720 session_handle_t * res)
721{
722 app_listener_t *app_listener;
723 u32 table_index, fib_proto;
724 session_endpoint_t *sep;
725 app_worker_t *app_wrk;
726 stream_session_t *ls;
727 session_handle_t lh;
728 session_type_t sst;
729
730 /*
731 * Check if sep is already listened on
732 */
733 sep = (session_endpoint_t *) sep_ext;
734 fib_proto = session_endpoint_fib_proto (sep);
735 table_index = application_session_table (app, fib_proto);
736 lh = session_lookup_endpoint_listener (table_index, sep, 1);
737 if (lh != SESSION_INVALID_HANDLE)
738 {
739 ls = listen_session_get_from_handle (lh);
740 if (ls->app_index != app->app_index)
741 return VNET_API_ERROR_ADDRESS_IN_USE;
742
743 app_wrk = app_worker_get (sep_ext->app_wrk_index);
744 if (ls->app_wrk_index == app_wrk->wrk_index)
745 return VNET_API_ERROR_ADDRESS_IN_USE;
746
747 if (app_worker_start_listen (app_wrk, ls))
748 return -1;
749
750 app_listener = app_listener_get (app, ls->listener_db_index);
751 app_listener->workers = clib_bitmap_set (app_listener->workers,
752 app_wrk->wrk_map_index, 1);
753
754 *res = listen_session_get_handle (ls);
755 return 0;
756 }
757
758 /*
759 * Allocate new listener for application
760 */
761 sst = session_type_from_proto_and_ip (sep_ext->transport_proto,
762 sep_ext->is_ip4);
763 ls = listen_session_new (0, sst);
764 ls->app_index = app->app_index;
765
766 if (session_listen (ls, sep_ext))
767 goto err;
768
769 app_listener = app_listener_alloc (app);
770 ls->listener_db_index = app_listener->al_index;
771
772 /*
773 * Setup app worker as a listener
774 */
775 app_wrk = app_worker_get (sep_ext->app_wrk_index);
776 ls->app_wrk_index = app_wrk->wrk_index;
777 if (app_worker_start_listen (app_wrk, ls))
778 goto err;
779 app_listener->workers = clib_bitmap_set (app_listener->workers,
780 app_wrk->wrk_map_index, 1);
781
782 *res = listen_session_get_handle (ls);
783 return 0;
784
785err:
786 listen_session_del (ls);
787 return -1;
788}
789
790/**
791 * Stop listening on session associated to handle
792 *
793 * @param handle listener handle
794 * @param app_index index of the app owning the handle.
795 * @param app_wrk_index index of the worker requesting the stop
796 */
797int
798application_stop_listen (u32 app_index, u32 app_wrk_index,
799 session_handle_t handle)
800{
801 app_listener_t *app_listener;
802 stream_session_t *listener;
803 app_worker_t *app_wrk;
804 application_t *app;
805
806 listener = listen_session_get_from_handle (handle);
807 app = application_get (app_index);
808 if (PREDICT_FALSE (!app || app->app_index != listener->app_index))
809 {
810 clib_warning ("app doesn't own handle %llu!", handle);
811 return -1;
812 }
813
814 app_listener = app_listener_get (app, listener->listener_db_index);
815 if (!clib_bitmap_get (app_listener->workers, app_wrk_index))
816 {
817 clib_warning ("worker not listening on handle %lu", handle);
818 return 0;
819 }
820
821 app_wrk = application_get_worker (app, app_wrk_index);
822 app_worker_stop_listen (app_wrk, handle);
823 clib_bitmap_set_no_check (app_listener->workers, app_wrk_index, 0);
824
825 if (clib_bitmap_is_zero (app_listener->workers))
826 {
827 session_stop_listen (listener);
828 app_listener_free (app, app_listener);
829 listen_session_del (listener);
830 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700831
Dave Barach68b0fb02017-02-28 15:15:56 -0500832 return 0;
833}
834
Florin Coras6cf30ad2017-04-04 23:08:23 -0700835int
Florin Coras15531972018-08-12 23:50:53 -0700836app_worker_open_session (app_worker_t * app, session_endpoint_t * sep,
837 u32 api_context)
Dave Barach68b0fb02017-02-28 15:15:56 -0500838{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700839 int rv;
840
841 /* Make sure we have a segment manager for connects */
Florin Coras15531972018-08-12 23:50:53 -0700842 app_worker_alloc_connects_segment_manager (app);
Florin Coras371ca502018-02-21 12:07:41 -0800843
Florin Coras15531972018-08-12 23:50:53 -0700844 if ((rv = session_open (app->wrk_index, sep, api_context)))
Florin Coras371ca502018-02-21 12:07:41 -0800845 return rv;
846
847 return 0;
848}
849
850int
Florin Coras15531972018-08-12 23:50:53 -0700851app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
Florin Coras371ca502018-02-21 12:07:41 -0800852{
853 segment_manager_t *sm;
854
Florin Coras15531972018-08-12 23:50:53 -0700855 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700856 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700857 sm = app_worker_alloc_segment_manager (app_wrk);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700858 if (sm == 0)
859 return -1;
Florin Coras15531972018-08-12 23:50:53 -0700860 app_wrk->connects_seg_manager = segment_manager_index (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700861 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700862 return 0;
863}
864
865segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700866app_worker_get_connect_segment_manager (app_worker_t * app)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700867{
868 ASSERT (app->connects_seg_manager != (u32) ~ 0);
869 return segment_manager_get (app->connects_seg_manager);
870}
871
872segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700873app_worker_get_listen_segment_manager (app_worker_t * app,
874 stream_session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700875{
876 uword *smp;
877 smp = hash_get (app->listeners_table, listen_session_get_handle (s));
878 ASSERT (smp != 0);
879 return segment_manager_get (*smp);
880}
881
Florin Coras15531972018-08-12 23:50:53 -0700882clib_error_t *
883vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
884{
885 svm_fifo_segment_private_t *fs;
886 app_worker_map_t *wrk_map;
887 app_worker_t *app_wrk;
888 segment_manager_t *sm;
889 application_t *app;
890 int rv;
891
892 app = application_get (a->app_index);
893 if (!app)
894 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
895 "App %u does not exist", a->app_index);
896
897 if (a->is_add)
898 {
899 if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
900 return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv);
901 sm = segment_manager_get (app_wrk->first_segment_manager);
902 fs = segment_manager_get_segment_w_lock (sm, 0);
903 a->segment = &fs->ssvm;
904 segment_manager_segment_reader_unlock (sm);
905 a->evt_q = app_wrk->event_queue;
Florin Corasab2f6db2018-08-31 14:31:41 -0700906 a->wrk_index = app_wrk->wrk_map_index;
Florin Coras15531972018-08-12 23:50:53 -0700907 }
908 else
909 {
910 wrk_map = app_worker_map_get (app, a->wrk_index);
911 if (!wrk_map)
912 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
913 "App %u does not have worker %u",
914 app->app_index, a->wrk_index);
915 app_wrk = app_worker_get (wrk_map->wrk_index);
916 app_worker_map_free (app, wrk_map);
917 if (!app_wrk)
918 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
919 "No worker %u", a->wrk_index);
920 app_worker_free (app_wrk);
921 }
922 return 0;
923}
924
Florin Corasf8f516a2018-02-08 15:10:09 -0800925segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700926application_get_local_segment_manager (app_worker_t * app)
Florin Corasf8f516a2018-02-08 15:10:09 -0800927{
928 return segment_manager_get (app->local_segment_manager);
929}
930
931segment_manager_t *
Florin Coras15531972018-08-12 23:50:53 -0700932application_get_local_segment_manager_w_session (app_worker_t * app,
Florin Corasf8f516a2018-02-08 15:10:09 -0800933 local_session_t * ls)
934{
935 stream_session_t *listener;
936 if (application_local_session_listener_has_transport (ls))
937 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700938 listener = listen_session_get (ls->listener_index);
Florin Coras15531972018-08-12 23:50:53 -0700939 return app_worker_get_listen_segment_manager (app, listener);
Florin Corasf8f516a2018-02-08 15:10:09 -0800940 }
941 return segment_manager_get (app->local_segment_manager);
942}
943
Dave Barach52851e62017-08-07 09:35:25 -0400944int
945application_is_proxy (application_t * app)
946{
Florin Coras7999e832017-10-31 01:51:04 -0700947 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
948}
949
950int
951application_is_builtin (application_t * app)
952{
953 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
954}
955
956int
957application_is_builtin_proxy (application_t * app)
958{
959 return (application_is_proxy (app) && application_is_builtin (app));
Dave Barach52851e62017-08-07 09:35:25 -0400960}
961
Florin Corascea194d2017-10-02 00:18:51 -0700962u8
963application_has_local_scope (application_t * app)
964{
965 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
966}
967
968u8
969application_has_global_scope (application_t * app)
970{
971 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
972}
973
Florin Coras60116992018-08-27 09:52:18 -0700974u8
975application_use_mq_for_ctrl (application_t * app)
976{
977 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
978}
979
Florin Coras15531972018-08-12 23:50:53 -0700980/**
981 * Send an API message to the external app, to map new segment
982 */
983int
984app_worker_add_segment_notify (u32 app_wrk_index, ssvm_private_t * fs)
985{
986 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
987 application_t *app = application_get (app_wrk->app_index);
988 return app->cb_fns.add_segment_callback (app->api_client_index, fs);
989}
990
Florin Coras1c710452017-10-17 00:03:13 -0700991u32
Florin Coras15531972018-08-12 23:50:53 -0700992application_n_listeners (app_worker_t * app)
Florin Coras1c710452017-10-17 00:03:13 -0700993{
994 return hash_elts (app->listeners_table);
995}
996
997stream_session_t *
Florin Coras15531972018-08-12 23:50:53 -0700998app_worker_first_listener (app_worker_t * app, u8 fib_proto,
999 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -07001000{
Florin Coras7999e832017-10-31 01:51:04 -07001001 stream_session_t *listener;
Florin Coras1c710452017-10-17 00:03:13 -07001002 u64 handle;
1003 u32 sm_index;
Florin Coras7999e832017-10-31 01:51:04 -07001004 u8 sst;
1005
1006 sst = session_type_from_proto_and_ip (transport_proto,
1007 fib_proto == FIB_PROTOCOL_IP4);
Florin Coras1c710452017-10-17 00:03:13 -07001008
1009 /* *INDENT-OFF* */
1010 hash_foreach (handle, sm_index, app->listeners_table, ({
Florin Coras7999e832017-10-31 01:51:04 -07001011 listener = listen_session_get_from_handle (handle);
Florin Corasc3ddea82017-11-27 03:12:00 -08001012 if (listener->session_type == sst
Florin Corasab2f6db2018-08-31 14:31:41 -07001013 && listener->enqueue_epoch != SESSION_PROXY_LISTENER_INDEX)
Florin Coras7999e832017-10-31 01:51:04 -07001014 return listener;
Florin Coras1c710452017-10-17 00:03:13 -07001015 }));
1016 /* *INDENT-ON* */
1017
1018 return 0;
1019}
1020
Florin Coras15531972018-08-12 23:50:53 -07001021u8
1022app_worker_application_is_builtin (app_worker_t * app_wrk)
1023{
1024 return app_wrk->app_is_builtin;
1025}
1026
Florin Coras19b1f6a2017-12-11 03:37:03 -08001027stream_session_t *
Florin Coras15531972018-08-12 23:50:53 -07001028application_proxy_listener (app_worker_t * app, u8 fib_proto,
Florin Coras19b1f6a2017-12-11 03:37:03 -08001029 u8 transport_proto)
1030{
1031 stream_session_t *listener;
1032 u64 handle;
1033 u32 sm_index;
1034 u8 sst;
1035
1036 sst = session_type_from_proto_and_ip (transport_proto,
1037 fib_proto == FIB_PROTOCOL_IP4);
1038
1039 /* *INDENT-OFF* */
1040 hash_foreach (handle, sm_index, app->listeners_table, ({
1041 listener = listen_session_get_from_handle (handle);
1042 if (listener->session_type == sst
Florin Corasab2f6db2018-08-31 14:31:41 -07001043 && listener->enqueue_epoch == SESSION_PROXY_LISTENER_INDEX)
Florin Coras19b1f6a2017-12-11 03:37:03 -08001044 return listener;
1045 }));
1046 /* *INDENT-ON* */
1047
1048 return 0;
1049}
1050
Florin Coras7999e832017-10-31 01:51:04 -07001051static clib_error_t *
1052application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1053 u8 transport_proto, u8 is_start)
1054{
Florin Coras7999e832017-10-31 01:51:04 -07001055 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1056 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
Florin Corasab2f6db2018-08-31 14:31:41 -07001057 session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL;
Florin Coras7999e832017-10-31 01:51:04 -07001058 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -07001059 app_worker_t *app_wrk;
Florin Coras7999e832017-10-31 01:51:04 -07001060 stream_session_t *s;
1061 u64 handle;
1062
Florin Coras15531972018-08-12 23:50:53 -07001063 /* TODO decide if we want proxy to be enabled for all workers */
1064 app_wrk = application_get_default_worker (app);
Florin Coras7999e832017-10-31 01:51:04 -07001065 if (is_start)
1066 {
Florin Coras15531972018-08-12 23:50:53 -07001067 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001068 if (!s)
1069 {
1070 sep.is_ip4 = is_ip4;
1071 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1072 sep.sw_if_index = app_ns->sw_if_index;
1073 sep.transport_proto = transport_proto;
Florin Corasab2f6db2018-08-31 14:31:41 -07001074 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
1075 application_start_listen (app, &sep, &handle);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001076 s = listen_session_get_from_handle (handle);
Florin Corasab2f6db2018-08-31 14:31:41 -07001077 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
Florin Coras19b1f6a2017-12-11 03:37:03 -08001078 }
Florin Coras7999e832017-10-31 01:51:04 -07001079 }
1080 else
1081 {
Florin Coras15531972018-08-12 23:50:53 -07001082 s = application_proxy_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001083 ASSERT (s);
Florin Coras7999e832017-10-31 01:51:04 -07001084 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001085
Florin Coras7999e832017-10-31 01:51:04 -07001086 tc = listen_session_get_transport (s);
1087
1088 if (!ip_is_zero (&tc->lcl_ip, 1))
1089 {
Florin Corasdbd44562017-11-09 19:30:17 -08001090 u32 sti;
1091 sep.is_ip4 = is_ip4;
1092 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1093 sep.transport_proto = transport_proto;
1094 sep.port = 0;
1095 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001096 if (is_start)
Florin Corasab2f6db2018-08-31 14:31:41 -07001097 session_lookup_add_session_endpoint (sti,
1098 (session_endpoint_t *) & sep,
1099 s->session_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001100 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001101 session_lookup_del_session_endpoint (sti,
1102 (session_endpoint_t *) & sep);
Florin Coras7999e832017-10-31 01:51:04 -07001103 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001104
Florin Coras7999e832017-10-31 01:51:04 -07001105 return 0;
1106}
1107
Florin Coras19b1f6a2017-12-11 03:37:03 -08001108static void
1109application_start_stop_proxy_local_scope (application_t * app,
1110 u8 transport_proto, u8 is_start)
1111{
1112 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1113 app_namespace_t *app_ns;
1114 app_ns = app_namespace_get (app->ns_index);
1115 sep.is_ip4 = 1;
1116 sep.transport_proto = transport_proto;
1117 sep.port = 0;
1118
1119 if (is_start)
1120 {
1121 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001122 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001123 sep.is_ip4 = 0;
1124 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001125 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001126 }
1127 else
1128 {
1129 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1130 sep.is_ip4 = 0;
1131 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1132 }
1133}
1134
Florin Coras7999e832017-10-31 01:51:04 -07001135void
Florin Coras561af9b2017-12-09 10:19:43 -08001136application_start_stop_proxy (application_t * app,
1137 transport_proto_t transport_proto, u8 is_start)
Florin Coras7999e832017-10-31 01:51:04 -07001138{
Florin Coras7999e832017-10-31 01:51:04 -07001139 if (application_has_local_scope (app))
Florin Coras19b1f6a2017-12-11 03:37:03 -08001140 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
Florin Coras7999e832017-10-31 01:51:04 -07001141
1142 if (application_has_global_scope (app))
1143 {
1144 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1145 transport_proto, is_start);
1146 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1147 transport_proto, is_start);
1148 }
1149}
1150
1151void
1152application_setup_proxy (application_t * app)
1153{
1154 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001155 transport_proto_t tp;
1156
Florin Coras7999e832017-10-31 01:51:04 -07001157 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001158
1159 /* *INDENT-OFF* */
1160 transport_proto_foreach (tp, ({
1161 if (transports & (1 << tp))
1162 application_start_stop_proxy (app, tp, 1);
1163 }));
1164 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -07001165}
1166
1167void
1168application_remove_proxy (application_t * app)
1169{
1170 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001171 transport_proto_t tp;
1172
Florin Coras7999e832017-10-31 01:51:04 -07001173 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001174
1175 /* *INDENT-OFF* */
1176 transport_proto_foreach (tp, ({
1177 if (transports & (1 << tp))
1178 application_start_stop_proxy (app, tp, 0);
1179 }));
1180 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -07001181}
1182
Florin Corasa332c462018-01-31 06:52:17 -08001183segment_manager_properties_t *
1184application_segment_manager_properties (application_t * app)
1185{
1186 return &app->sm_properties;
1187}
1188
1189segment_manager_properties_t *
1190application_get_segment_manager_properties (u32 app_index)
1191{
1192 application_t *app = application_get (app_index);
1193 return &app->sm_properties;
1194}
1195
Florin Coras3c2fed52018-07-04 04:15:05 -07001196static inline int
1197app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
1198{
Florin Coras52207f12018-07-12 14:48:06 -07001199 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
Florin Coras3c2fed52018-07-04 04:15:05 -07001200 {
1201 clib_warning ("evt q full");
1202 svm_msg_q_free_msg (mq, msg);
1203 if (lock)
1204 svm_msg_q_unlock (mq);
1205 return -1;
1206 }
Florin Coras52207f12018-07-12 14:48:06 -07001207
1208 if (lock)
1209 {
1210 svm_msg_q_add_and_unlock (mq, msg);
1211 return 0;
1212 }
1213
1214 /* Even when not locking the ring, we must wait for queue mutex */
1215 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
1216 {
1217 clib_warning ("msg q add returned");
1218 return -1;
1219 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001220 return 0;
1221}
1222
1223static inline int
Florin Coras15531972018-08-12 23:50:53 -07001224app_send_io_evt_rx (app_worker_t * app_wrk, stream_session_t * s, u8 lock)
Florin Coras3c2fed52018-07-04 04:15:05 -07001225{
Florin Coras52207f12018-07-12 14:48:06 -07001226 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -07001227 svm_msg_q_msg_t msg;
1228 svm_msg_q_t *mq;
1229
Florin Coras460dce62018-07-27 05:45:06 -07001230 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
1231 && s->session_state != SESSION_STATE_LISTENING))
Florin Coras3c2fed52018-07-04 04:15:05 -07001232 {
1233 /* Session is closed so app will never clean up. Flush rx fifo */
Florin Coras84099552018-07-22 12:59:30 -07001234 if (s->session_state == SESSION_STATE_CLOSED)
1235 svm_fifo_dequeue_drop_all (s->server_rx_fifo);
Florin Coras3c2fed52018-07-04 04:15:05 -07001236 return 0;
1237 }
1238
Florin Coras15531972018-08-12 23:50:53 -07001239 if (app_worker_application_is_builtin (app_wrk))
1240 {
1241 application_t *app = application_get (app_wrk->app_index);
1242 return app->cb_fns.builtin_app_rx_callback (s);
1243 }
Florin Coras3c2fed52018-07-04 04:15:05 -07001244
Florin Coras54693d22018-07-17 10:46:29 -07001245 if (svm_fifo_has_event (s->server_rx_fifo)
1246 || svm_fifo_is_empty (s->server_rx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -07001247 return 0;
1248
Florin Coras15531972018-08-12 23:50:53 -07001249 mq = app_wrk->event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001250 if (lock)
1251 svm_msg_q_lock (mq);
1252
1253 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1254 {
1255 clib_warning ("evt q rings full");
1256 if (lock)
1257 svm_msg_q_unlock (mq);
1258 return -1;
1259 }
1260
1261 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1262 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1263
Florin Coras52207f12018-07-12 14:48:06 -07001264 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -07001265 evt->fifo = s->server_rx_fifo;
1266 evt->event_type = FIFO_EVENT_APP_RX;
1267
Florin Coras54693d22018-07-17 10:46:29 -07001268 if (app_enqueue_evt (mq, &msg, lock))
1269 return -1;
Florin Corasd3ca8ff2018-07-28 04:53:30 -07001270 (void) svm_fifo_set_event (s->server_rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001271 return 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001272}
1273
1274static inline int
Florin Coras15531972018-08-12 23:50:53 -07001275app_send_io_evt_tx (app_worker_t * app_wrk, stream_session_t * s, u8 lock)
Florin Coras3c2fed52018-07-04 04:15:05 -07001276{
1277 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -07001278 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -07001279 svm_msg_q_msg_t msg;
1280
Florin Coras15531972018-08-12 23:50:53 -07001281 if (app_worker_application_is_builtin (app_wrk))
Florin Coras3c2fed52018-07-04 04:15:05 -07001282 return 0;
1283
Florin Coras15531972018-08-12 23:50:53 -07001284 mq = app_wrk->event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001285 if (lock)
1286 svm_msg_q_lock (mq);
1287
1288 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
1289 {
1290 clib_warning ("evt q rings full");
1291 if (lock)
1292 svm_msg_q_unlock (mq);
1293 return -1;
1294 }
1295
1296 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
1297 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
1298
Florin Coras52207f12018-07-12 14:48:06 -07001299 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -07001300 evt->event_type = FIFO_EVENT_APP_TX;
1301 evt->fifo = s->server_tx_fifo;
1302
1303 return app_enqueue_evt (mq, &msg, lock);
1304}
1305
1306/* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001307typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras3c2fed52018-07-04 04:15:05 -07001308 stream_session_t *s,
1309 u8 lock);
Florin Coras460dce62018-07-27 05:45:06 -07001310static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = {
Florin Coras3c2fed52018-07-04 04:15:05 -07001311 app_send_io_evt_rx,
Florin Coras460dce62018-07-27 05:45:06 -07001312 0,
Florin Coras3c2fed52018-07-04 04:15:05 -07001313 app_send_io_evt_tx,
1314};
1315/* *INDENT-ON* */
1316
1317/**
1318 * Send event to application
1319 *
1320 * Logic from queue perspective is non-blocking. That is, if there's
1321 * not enough space to enqueue a message, we return. However, if the lock
1322 * flag is set, we do wait for queue mutex.
1323 */
1324int
Florin Corasab2f6db2018-08-31 14:31:41 -07001325app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -07001326{
1327 ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
1328 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
1329}
1330
1331int
Florin Corasab2f6db2018-08-31 14:31:41 -07001332app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s,
1333 u8 evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -07001334{
1335 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
1336}
1337
Florin Corasf8f516a2018-02-08 15:10:09 -08001338local_session_t *
Florin Corasab2f6db2018-08-31 14:31:41 -07001339application_local_session_alloc (app_worker_t * app_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001340{
1341 local_session_t *s;
Florin Corasab2f6db2018-08-31 14:31:41 -07001342 pool_get (app_wrk->local_sessions, s);
Florin Corasf8f516a2018-02-08 15:10:09 -08001343 memset (s, 0, sizeof (*s));
Florin Corasab2f6db2018-08-31 14:31:41 -07001344 s->app_wrk_index = app_wrk->wrk_index;
1345 s->session_index = s - app_wrk->local_sessions;
Florin Corasf8f516a2018-02-08 15:10:09 -08001346 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1347 return s;
1348}
1349
1350void
Florin Corasab2f6db2018-08-31 14:31:41 -07001351application_local_session_free (app_worker_t * app, local_session_t * s)
Florin Corasf8f516a2018-02-08 15:10:09 -08001352{
1353 pool_put (app->local_sessions, s);
1354 if (CLIB_DEBUG)
1355 memset (s, 0xfc, sizeof (*s));
1356}
1357
1358local_session_t *
Florin Coras15531972018-08-12 23:50:53 -07001359application_get_local_session (app_worker_t * app_wrk, u32 session_index)
Florin Corasf8f516a2018-02-08 15:10:09 -08001360{
Florin Coras15531972018-08-12 23:50:53 -07001361 if (pool_is_free_index (app_wrk->local_sessions, session_index))
Florin Coras99368312018-08-02 10:45:44 -07001362 return 0;
Florin Coras15531972018-08-12 23:50:53 -07001363 return pool_elt_at_index (app_wrk->local_sessions, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001364}
1365
1366local_session_t *
1367application_get_local_session_from_handle (session_handle_t handle)
1368{
Florin Coras15531972018-08-12 23:50:53 -07001369 app_worker_t *server_wrk;
1370 u32 session_index, server_wrk_index;
1371 local_session_parse_handle (handle, &server_wrk_index, &session_index);
1372 server_wrk = app_worker_get_if_valid (server_wrk_index);
1373 if (!server_wrk)
Florin Corasfcbda892018-08-23 19:27:03 -07001374 return 0;
Florin Coras15531972018-08-12 23:50:53 -07001375 return application_get_local_session (server_wrk, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001376}
1377
Florin Coras60116992018-08-27 09:52:18 -07001378local_session_t *
Florin Corasab2f6db2018-08-31 14:31:41 -07001379application_local_listen_session_alloc (application_t * app)
Florin Coras60116992018-08-27 09:52:18 -07001380{
Florin Corasab2f6db2018-08-31 14:31:41 -07001381 local_session_t *ll;
1382 pool_get (app->local_listen_sessions, ll);
1383 memset (ll, 0, sizeof (*ll));
1384 return ll;
Florin Coras60116992018-08-27 09:52:18 -07001385}
1386
Florin Corasab2f6db2018-08-31 14:31:41 -07001387u32
1388application_local_listener_index (application_t * app, local_session_t * ll)
Florin Corasf8f516a2018-02-08 15:10:09 -08001389{
Florin Corasab2f6db2018-08-31 14:31:41 -07001390 return (ll - app->local_listen_sessions);
1391}
1392
1393void
1394application_local_listen_session_free (application_t * app,
1395 local_session_t * ll)
1396{
1397 pool_put (app->local_listen_sessions, ll);
1398 if (CLIB_DEBUG)
1399 memset (ll, 0xfb, sizeof (*ll));
Florin Corasf8f516a2018-02-08 15:10:09 -08001400}
1401
1402int
Florin Corasab2f6db2018-08-31 14:31:41 -07001403application_start_local_listen (application_t * app,
1404 session_endpoint_extended_t * sep_ext,
Florin Corasf8f516a2018-02-08 15:10:09 -08001405 session_handle_t * handle)
1406{
Florin Corasab2f6db2018-08-31 14:31:41 -07001407 app_listener_t *app_listener;
1408 session_endpoint_t *sep;
1409 app_worker_t *app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001410 session_handle_t lh;
1411 local_session_t *ll;
1412 u32 table_index;
1413
Florin Corasab2f6db2018-08-31 14:31:41 -07001414 sep = (session_endpoint_t *) sep_ext;
Florin Coras15531972018-08-12 23:50:53 -07001415 table_index = application_local_session_table (app);
Florin Corasab2f6db2018-08-31 14:31:41 -07001416 app_wrk = app_worker_get (sep_ext->app_wrk_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001417
1418 /* An exact sep match, as opposed to session_lookup_local_listener */
1419 lh = session_lookup_endpoint_listener (table_index, sep, 1);
1420 if (lh != SESSION_INVALID_HANDLE)
Florin Corasab2f6db2018-08-31 14:31:41 -07001421 {
1422 ll = application_get_local_listener_w_handle (lh);
1423 if (ll->app_index != app->app_index)
1424 return VNET_API_ERROR_ADDRESS_IN_USE;
Florin Corasf8f516a2018-02-08 15:10:09 -08001425
Florin Corasab2f6db2018-08-31 14:31:41 -07001426 if (ll->app_wrk_index == app_wrk->wrk_index)
1427 return VNET_API_ERROR_ADDRESS_IN_USE;
1428
1429 app_listener = app_local_listener_get (app, ll->listener_db_index);
1430 app_listener->workers = clib_bitmap_set (app_listener->workers,
1431 app_wrk->wrk_map_index, 1);
1432 *handle = application_local_session_handle (ll);
1433 return 0;
1434 }
1435
1436 ll = application_local_listen_session_alloc (app);
Florin Corasf8f516a2018-02-08 15:10:09 -08001437 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
Florin Coras15531972018-08-12 23:50:53 -07001438 ll->app_wrk_index = app_wrk->app_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001439 ll->session_index = application_local_listener_index (app, ll);
1440 ll->port = sep_ext->port;
Florin Corasf8f516a2018-02-08 15:10:09 -08001441 /* Store the original session type for the unbind */
1442 ll->listener_session_type =
Florin Corasab2f6db2018-08-31 14:31:41 -07001443 session_type_from_proto_and_ip (sep_ext->transport_proto,
1444 sep_ext->is_ip4);
Florin Coras5fda7a32018-02-14 08:04:31 -08001445 ll->transport_listener_index = ~0;
Florin Corasab2f6db2018-08-31 14:31:41 -07001446 ll->app_index = app->app_index;
1447
1448 app_listener = app_local_listener_alloc (app);
1449 ll->listener_db_index = app_listener->al_index;
1450 app_listener->workers = clib_bitmap_set (app_listener->workers,
1451 app_wrk->wrk_map_index, 1);
Florin Corasf8f516a2018-02-08 15:10:09 -08001452
1453 *handle = application_local_session_handle (ll);
1454 session_lookup_add_session_endpoint (table_index, sep, *handle);
1455
1456 return 0;
1457}
1458
1459/**
1460 * Clean up local session table. If we have a listener session use it to
1461 * find the port and proto. If not, the handle must be a local table handle
1462 * so parse it.
1463 */
1464int
Florin Corasab2f6db2018-08-31 14:31:41 -07001465application_stop_local_listen (u32 app_index, u32 wrk_map_index,
1466 session_handle_t lh)
Florin Corasf8f516a2018-02-08 15:10:09 -08001467{
1468 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Corasab2f6db2018-08-31 14:31:41 -07001469 u32 table_index, ll_index, server_index;
1470 app_listener_t *app_listener;
Florin Coras15531972018-08-12 23:50:53 -07001471 app_worker_t *server_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001472 stream_session_t *sl = 0;
1473 local_session_t *ll, *ls;
Florin Coras15531972018-08-12 23:50:53 -07001474 application_t *server;
Florin Corasf8f516a2018-02-08 15:10:09 -08001475
Florin Coras15531972018-08-12 23:50:53 -07001476 server = application_get (app_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001477 table_index = application_local_session_table (server);
1478
1479 /* We have both local and global table binds. Figure from global what
1480 * the sep we should be cleaning up is.
1481 */
1482 if (!session_handle_is_local (lh))
1483 {
1484 sl = listen_session_get_from_handle (lh);
1485 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1486 {
1487 clib_warning ("broken listener");
1488 return -1;
1489 }
1490 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1491 if (lh == SESSION_INVALID_HANDLE)
1492 return -1;
1493 }
1494
Florin Corasab2f6db2018-08-31 14:31:41 -07001495 local_session_parse_handle (lh, &server_index, &ll_index);
1496 if (PREDICT_FALSE (server_index != app_index))
Florin Coras15531972018-08-12 23:50:53 -07001497 {
1498 clib_warning ("app %u does not own local handle 0x%lx", app_index, lh);
Florin Corasab2f6db2018-08-31 14:31:41 -07001499 return -1;
Florin Coras15531972018-08-12 23:50:53 -07001500 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001501
1502 ll = application_get_local_listen_session (server, ll_index);
Florin Coras15531972018-08-12 23:50:53 -07001503 if (PREDICT_FALSE (!ll))
Florin Corasf8f516a2018-02-08 15:10:09 -08001504 {
1505 clib_warning ("no local listener");
1506 return -1;
1507 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001508
Florin Corasab2f6db2018-08-31 14:31:41 -07001509 app_listener = app_local_listener_get (server, ll->listener_db_index);
1510 if (!clib_bitmap_get (app_listener->workers, wrk_map_index))
1511 {
1512 clib_warning ("app wrk %u not listening on handle %lu", wrk_map_index,
1513 lh);
1514 return -1;
1515 }
1516
1517 server_wrk = application_get_worker (server, wrk_map_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001518 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001519 pool_foreach (ls, server_wrk->local_sessions, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08001520 if (ls->listener_index == ll->session_index)
Florin Coras15531972018-08-12 23:50:53 -07001521 application_local_session_disconnect (server_wrk->app_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001522 }));
1523 /* *INDENT-ON* */
Florin Corasab2f6db2018-08-31 14:31:41 -07001524
1525 clib_bitmap_set_no_check (app_listener->workers, wrk_map_index, 0);
1526 if (clib_bitmap_is_zero (app_listener->workers))
1527 {
1528 app_local_listener_free (server, app_listener);
1529 application_local_listener_session_endpoint (ll, &sep);
1530 session_lookup_del_session_endpoint (table_index, &sep);
1531 application_local_listen_session_free (server, ll);
1532 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001533
1534 return 0;
1535}
1536
Florin Coras99368312018-08-02 10:45:44 -07001537static void
1538application_local_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
1539{
1540 int fd;
1541
1542 /*
1543 * segment manager initializes only the producer eventds, since vpp is
1544 * typically the producer. But for local sessions, we also pass to the
1545 * apps the mqs they listen on for events from peer apps, so they are also
1546 * consumer fds.
1547 */
1548 fd = svm_msg_q_get_producer_eventfd (sq);
1549 svm_msg_q_set_consumer_eventfd (sq, fd);
1550 fd = svm_msg_q_get_producer_eventfd (cq);
1551 svm_msg_q_set_consumer_eventfd (cq, fd);
1552}
1553
Florin Corasf8f516a2018-02-08 15:10:09 -08001554int
Florin Coras15531972018-08-12 23:50:53 -07001555application_local_session_connect (app_worker_t * client_wrk,
1556 app_worker_t * server_wrk,
Florin Corasf8f516a2018-02-08 15:10:09 -08001557 local_session_t * ll, u32 opaque)
1558{
1559 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1560 segment_manager_properties_t *props, *cprops;
Florin Coras54693d22018-07-17 10:46:29 -07001561 u32 round_rx_fifo_sz, round_tx_fifo_sz;
Florin Corasf8f516a2018-02-08 15:10:09 -08001562 int rv, has_transport, seg_index;
1563 svm_fifo_segment_private_t *seg;
Florin Coras15531972018-08-12 23:50:53 -07001564 application_t *server, *client;
Florin Corasf8f516a2018-02-08 15:10:09 -08001565 segment_manager_t *sm;
1566 local_session_t *ls;
Florin Coras3c2fed52018-07-04 04:15:05 -07001567 svm_msg_q_t *sq, *cq;
Florin Corasf8f516a2018-02-08 15:10:09 -08001568
Florin Corasab2f6db2018-08-31 14:31:41 -07001569 ls = application_local_session_alloc (server_wrk);
Florin Coras15531972018-08-12 23:50:53 -07001570 server = application_get (server_wrk->app_index);
1571 client = application_get (client_wrk->app_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001572
1573 props = application_segment_manager_properties (server);
1574 cprops = application_segment_manager_properties (client);
1575 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -07001576 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
Florin Coras54693d22018-07-17 10:46:29 -07001577 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
1578 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
1579 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
Florin Corasf8f516a2018-02-08 15:10:09 -08001580
1581 has_transport = session_has_transport ((stream_session_t *) ll);
1582 if (!has_transport)
1583 {
1584 /* Local sessions don't have backing transport */
1585 ls->port = ll->port;
Florin Coras15531972018-08-12 23:50:53 -07001586 sm = application_get_local_segment_manager (server_wrk);
Florin Corasf8f516a2018-02-08 15:10:09 -08001587 }
1588 else
1589 {
1590 stream_session_t *sl = (stream_session_t *) ll;
1591 transport_connection_t *tc;
1592 tc = listen_session_get_transport (sl);
1593 ls->port = tc->lcl_port;
Florin Coras15531972018-08-12 23:50:53 -07001594 sm = app_worker_get_listen_segment_manager (server_wrk, sl);
Florin Corasf8f516a2018-02-08 15:10:09 -08001595 }
1596
1597 seg_index = segment_manager_add_segment (sm, seg_size);
1598 if (seg_index < 0)
1599 {
1600 clib_warning ("failed to add new cut-through segment");
1601 return seg_index;
1602 }
1603 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -07001604 sq = segment_manager_alloc_queue (seg, props);
1605 cq = segment_manager_alloc_queue (seg, cprops);
1606
1607 if (props->use_mq_eventfd)
1608 application_local_session_fix_eventds (sq, cq);
1609
Florin Corasf8f516a2018-02-08 15:10:09 -08001610 ls->server_evt_q = pointer_to_uword (sq);
1611 ls->client_evt_q = pointer_to_uword (cq);
1612 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1613 props->tx_fifo_size,
1614 &ls->server_rx_fifo,
1615 &ls->server_tx_fifo);
1616 if (rv)
1617 {
1618 clib_warning ("failed to add fifos in cut-through segment");
1619 segment_manager_segment_reader_unlock (sm);
1620 goto failed;
1621 }
1622 ls->server_rx_fifo->master_session_index = ls->session_index;
1623 ls->server_tx_fifo->master_session_index = ls->session_index;
1624 ls->server_rx_fifo->master_thread_index = ~0;
1625 ls->server_tx_fifo->master_thread_index = ~0;
1626 ls->svm_segment_index = seg_index;
1627 ls->listener_index = ll->session_index;
Florin Coras15531972018-08-12 23:50:53 -07001628 ls->client_wrk_index = client_wrk->wrk_index;
Florin Corasf8f516a2018-02-08 15:10:09 -08001629 ls->client_opaque = opaque;
1630 ls->listener_session_type = ll->session_type;
Florin Corasab2f6db2018-08-31 14:31:41 -07001631 ls->session_state = SESSION_STATE_READY;
Florin Corasf8f516a2018-02-08 15:10:09 -08001632
1633 if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
1634 &seg->ssvm)))
1635 {
1636 clib_warning ("failed to notify server of new segment");
1637 segment_manager_segment_reader_unlock (sm);
1638 goto failed;
1639 }
1640 segment_manager_segment_reader_unlock (sm);
1641 if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
1642 {
1643 clib_warning ("failed to send accept cut-through notify to server");
1644 goto failed;
1645 }
1646 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1647 application_local_session_connect_notify (ls);
1648
1649 return 0;
1650
1651failed:
1652 if (!has_transport)
1653 segment_manager_del_segment (sm, seg);
1654 return rv;
1655}
1656
1657static uword
1658application_client_local_connect_key (local_session_t * ls)
1659{
Florin Coras15531972018-08-12 23:50:53 -07001660 return ((uword) ls->app_wrk_index << 32 | (uword) ls->session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001661}
1662
1663static void
Florin Coras15531972018-08-12 23:50:53 -07001664application_client_local_connect_key_parse (uword key, u32 * app_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08001665 u32 * session_index)
1666{
Florin Coras15531972018-08-12 23:50:53 -07001667 *app_wrk_index = key >> 32;
Florin Corasf8f516a2018-02-08 15:10:09 -08001668 *session_index = key & 0xFFFFFFFF;
1669}
1670
1671int
1672application_local_session_connect_notify (local_session_t * ls)
1673{
1674 svm_fifo_segment_private_t *seg;
Florin Coras15531972018-08-12 23:50:53 -07001675 app_worker_t *client_wrk, *server_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08001676 segment_manager_t *sm;
Florin Coras15531972018-08-12 23:50:53 -07001677 application_t *client;
Florin Corasf8f516a2018-02-08 15:10:09 -08001678 int rv, is_fail = 0;
1679 uword client_key;
1680
Florin Coras15531972018-08-12 23:50:53 -07001681 client_wrk = app_worker_get (ls->client_wrk_index);
1682 server_wrk = app_worker_get (ls->app_wrk_index);
1683 client = application_get (client_wrk->app_index);
1684
1685 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001686 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1687 if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1688 &seg->ssvm)))
1689 {
1690 clib_warning ("failed to notify client %u of new segment",
Florin Coras15531972018-08-12 23:50:53 -07001691 ls->client_wrk_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08001692 segment_manager_segment_reader_unlock (sm);
Florin Coras15531972018-08-12 23:50:53 -07001693 application_local_session_disconnect (ls->client_wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001694 is_fail = 1;
1695 }
1696 else
1697 {
1698 segment_manager_segment_reader_unlock (sm);
1699 }
1700
Florin Coras15531972018-08-12 23:50:53 -07001701 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
1702 ls->client_opaque,
Florin Corasf8f516a2018-02-08 15:10:09 -08001703 (stream_session_t *) ls,
1704 is_fail);
1705
1706 client_key = application_client_local_connect_key (ls);
Florin Coras15531972018-08-12 23:50:53 -07001707 hash_set (client_wrk->local_connects, client_key, client_key);
Florin Corasf8f516a2018-02-08 15:10:09 -08001708 return 0;
1709}
1710
1711int
Florin Coras15531972018-08-12 23:50:53 -07001712application_local_session_cleanup (app_worker_t * client_wrk,
1713 app_worker_t * server_wrk,
Florin Corasf6647e02018-03-23 22:56:43 -07001714 local_session_t * ls)
Florin Corasf8f516a2018-02-08 15:10:09 -08001715{
1716 svm_fifo_segment_private_t *seg;
Florin Corasf8f516a2018-02-08 15:10:09 -08001717 segment_manager_t *sm;
1718 uword client_key;
Florin Corasf6647e02018-03-23 22:56:43 -07001719 u8 has_transport;
1720
1721 has_transport = session_has_transport ((stream_session_t *) ls);
1722 client_key = application_client_local_connect_key (ls);
1723 if (!has_transport)
Florin Coras15531972018-08-12 23:50:53 -07001724 sm = application_get_local_segment_manager_w_session (server_wrk, ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001725 else
Florin Coras15531972018-08-12 23:50:53 -07001726 sm = app_worker_get_listen_segment_manager (server_wrk,
1727 (stream_session_t *) ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001728
1729 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
Florin Coras15531972018-08-12 23:50:53 -07001730 if (client_wrk)
1731 hash_unset (client_wrk->local_connects, client_key);
Florin Corasf6647e02018-03-23 22:56:43 -07001732
1733 if (!has_transport)
1734 {
Florin Coras15531972018-08-12 23:50:53 -07001735 application_t *server = application_get (server_wrk->app_index);
Florin Corasf6647e02018-03-23 22:56:43 -07001736 server->cb_fns.del_segment_callback (server->api_client_index,
1737 &seg->ssvm);
Florin Coras15531972018-08-12 23:50:53 -07001738 if (client_wrk)
1739 {
1740 application_t *client = application_get (client_wrk->app_index);
1741 client->cb_fns.del_segment_callback (client->api_client_index,
1742 &seg->ssvm);
1743 }
Florin Corasf6647e02018-03-23 22:56:43 -07001744 segment_manager_del_segment (sm, seg);
1745 }
1746
Florin Corasab2f6db2018-08-31 14:31:41 -07001747 application_local_session_free (server_wrk, ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001748
1749 return 0;
1750}
1751
1752int
Florin Corasab2f6db2018-08-31 14:31:41 -07001753application_local_session_disconnect (u32 app_index, local_session_t * ls)
Florin Corasf6647e02018-03-23 22:56:43 -07001754{
Florin Coras15531972018-08-12 23:50:53 -07001755 app_worker_t *client_wrk, *server_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -07001756 u8 is_server = 0, is_client = 0;
1757 application_t *app;
1758
1759 app = application_get_if_valid (app_index);
1760 if (!app)
1761 return 0;
Florin Corasf8f516a2018-02-08 15:10:09 -08001762
Florin Coras15531972018-08-12 23:50:53 -07001763 client_wrk = app_worker_get_if_valid (ls->client_wrk_index);
1764 server_wrk = app_worker_get (ls->app_wrk_index);
1765
Florin Corasab2f6db2018-08-31 14:31:41 -07001766 if (server_wrk->app_index == app_index)
1767 is_server = 1;
1768 else if (client_wrk && client_wrk->app_index == app_index)
1769 is_client = 1;
1770
1771 if (!is_server && !is_client)
1772 {
1773 clib_warning ("app %u is neither client nor server for session 0x%lx",
1774 app_index, application_local_session_handle (ls));
1775 return VNET_API_ERROR_INVALID_VALUE;
1776 }
Florin Corasf8f516a2018-02-08 15:10:09 -08001777
1778 if (ls->session_state == SESSION_STATE_CLOSED)
Florin Coras15531972018-08-12 23:50:53 -07001779 return application_local_session_cleanup (client_wrk, server_wrk, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001780
Florin Corasab2f6db2018-08-31 14:31:41 -07001781 if (app_index == ls->client_wrk_index)
Florin Corasf8f516a2018-02-08 15:10:09 -08001782 {
Florin Coras15531972018-08-12 23:50:53 -07001783 mq_send_local_session_disconnected_cb (ls->app_wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001784 }
1785 else
1786 {
Florin Coras15531972018-08-12 23:50:53 -07001787 if (!client_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001788 {
Florin Coras15531972018-08-12 23:50:53 -07001789 return application_local_session_cleanup (client_wrk, server_wrk,
1790 ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001791 }
1792 else if (ls->session_state < SESSION_STATE_READY)
1793 {
Florin Coras15531972018-08-12 23:50:53 -07001794 application_t *client = application_get (client_wrk->app_index);
1795 client->cb_fns.session_connected_callback (client_wrk->wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08001796 ls->client_opaque,
1797 (stream_session_t *) ls,
1798 1 /* is_fail */ );
1799 ls->session_state = SESSION_STATE_CLOSED;
Florin Coras15531972018-08-12 23:50:53 -07001800 return application_local_session_cleanup (client_wrk, server_wrk,
1801 ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001802 }
1803 else
1804 {
Florin Coras15531972018-08-12 23:50:53 -07001805 mq_send_local_session_disconnected_cb (client_wrk->wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001806 }
1807 }
1808
1809 ls->session_state = SESSION_STATE_CLOSED;
1810
1811 return 0;
1812}
1813
Florin Corasf6647e02018-03-23 22:56:43 -07001814int
Florin Coras15531972018-08-12 23:50:53 -07001815application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index)
Florin Corasf6647e02018-03-23 22:56:43 -07001816{
Florin Coras15531972018-08-12 23:50:53 -07001817 app_worker_t *app_wrk;
Florin Corasf6647e02018-03-23 22:56:43 -07001818 local_session_t *ls;
Florin Coras15531972018-08-12 23:50:53 -07001819 app_wrk = app_worker_get (app_wrk_index);
1820 ls = application_get_local_session (app_wrk, ls_index);
1821 return application_local_session_disconnect (app_wrk_index, ls);
Florin Corasf6647e02018-03-23 22:56:43 -07001822}
1823
Florin Corasf8f516a2018-02-08 15:10:09 -08001824void
Florin Corasab2f6db2018-08-31 14:31:41 -07001825app_worker_local_sessions_free (app_worker_t * app_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001826{
Florin Corasab2f6db2018-08-31 14:31:41 -07001827 u32 index, server_wrk_index, session_index;
Florin Corasf8f516a2018-02-08 15:10:09 -08001828 u64 handle, *handles = 0;
Florin Coras15531972018-08-12 23:50:53 -07001829 app_worker_t *server_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -07001830 segment_manager_t *sm;
1831 local_session_t *ls;
Florin Corasf8f516a2018-02-08 15:10:09 -08001832 int i;
1833
1834 /*
Florin Corasf8f516a2018-02-08 15:10:09 -08001835 * Local sessions
1836 */
Florin Coras15531972018-08-12 23:50:53 -07001837 if (app_wrk->local_sessions)
Florin Corasf8f516a2018-02-08 15:10:09 -08001838 {
1839 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001840 pool_foreach (ls, app_wrk->local_sessions, ({
1841 application_local_session_disconnect (app_wrk->wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001842 }));
1843 /* *INDENT-ON* */
1844 }
1845
1846 /*
1847 * Local connects
1848 */
1849 vec_reset_length (handles);
1850 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001851 hash_foreach (handle, index, app_wrk->local_connects, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08001852 vec_add1 (handles, handle);
1853 }));
1854 /* *INDENT-ON* */
1855
1856 for (i = 0; i < vec_len (handles); i++)
1857 {
Florin Coras15531972018-08-12 23:50:53 -07001858 application_client_local_connect_key_parse (handles[i],
1859 &server_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08001860 &session_index);
Florin Coras15531972018-08-12 23:50:53 -07001861 server_wrk = app_worker_get_if_valid (server_wrk_index);
1862 if (server_wrk)
Florin Corasf8f516a2018-02-08 15:10:09 -08001863 {
Florin Coras15531972018-08-12 23:50:53 -07001864 ls = application_get_local_session (server_wrk, session_index);
1865 application_local_session_disconnect (app_wrk->wrk_index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001866 }
1867 }
1868
Florin Coras15531972018-08-12 23:50:53 -07001869 sm = segment_manager_get (app_wrk->local_segment_manager);
1870 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Corasf8f516a2018-02-08 15:10:09 -08001871 segment_manager_del (sm);
1872}
1873
Florin Coras371ca502018-02-21 12:07:41 -08001874clib_error_t *
1875vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1876{
1877 application_t *app;
1878 app = application_get (a->app_index);
1879 if (!app)
1880 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1881 0, "app %u doesn't exist", a->app_index);
1882 app->tls_cert = vec_dup (a->cert);
1883 return 0;
1884}
1885
1886clib_error_t *
1887vnet_app_add_tls_key (vnet_app_add_tls_key_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_key = vec_dup (a->key);
1895 return 0;
1896}
1897
Dave Barach68b0fb02017-02-28 15:15:56 -05001898u8 *
Florin Coras15531972018-08-12 23:50:53 -07001899format_app_worker_listener (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -05001900{
Florin Coras15531972018-08-12 23:50:53 -07001901 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001902 u64 handle = va_arg (*args, u64);
Florin Corasf8f516a2018-02-08 15:10:09 -08001903 u32 sm_index = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -05001904 int verbose = va_arg (*args, int);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001905 stream_session_t *listener;
Florin Coras15531972018-08-12 23:50:53 -07001906 application_t *app;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001907 u8 *app_name, *str;
Dave Barach68b0fb02017-02-28 15:15:56 -05001908
Florin Coras15531972018-08-12 23:50:53 -07001909 if (!app_wrk)
Dave Barach68b0fb02017-02-28 15:15:56 -05001910 {
1911 if (verbose)
Florin Corasab2f6db2018-08-31 14:31:41 -07001912 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
1913 "Wrk", "API Client", "ListenerID", "SegManager");
Dave Barach68b0fb02017-02-28 15:15:56 -05001914 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001915 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
Dave Barach68b0fb02017-02-28 15:15:56 -05001916
1917 return s;
1918 }
1919
Florin Coras15531972018-08-12 23:50:53 -07001920 app = application_get (app_wrk->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001921 app_name = app_get_name_from_reg_index (app);
1922 listener = listen_session_get_from_handle (handle);
1923 str = format (0, "%U", format_stream_session, listener, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05001924
Dave Barach68b0fb02017-02-28 15:15:56 -05001925 if (verbose)
1926 {
Florin Corasab2f6db2018-08-31 14:31:41 -07001927 s = format (s, "%-40s%-25s%=10u%-15u%-15u%-10u", str, app_name,
1928 app_wrk->wrk_map_index, app->api_client_index, handle,
1929 sm_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001930 }
1931 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001932 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001933
1934 vec_free (app_name);
1935 return s;
1936}
1937
Florin Coras15531972018-08-12 23:50:53 -07001938static void
1939application_format_listeners (application_t * app, int verbose)
1940{
1941 vlib_main_t *vm = vlib_get_main ();
1942 app_worker_map_t *wrk_map;
1943 app_worker_t *app_wrk;
1944 u32 sm_index;
1945 u64 handle;
1946
1947 if (!app)
1948 {
1949 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1950 0, 0, verbose);
1951 return;
1952 }
1953
1954 /* *INDENT-OFF* */
1955 pool_foreach (wrk_map, app->worker_maps, ({
1956 app_wrk = app_worker_get (wrk_map->wrk_index);
1957 if (hash_elts (app_wrk->listeners_table) == 0)
1958 continue;
1959 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1960 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1961 handle, sm_index, verbose);
1962 }));
1963 }));
1964 /* *INDENT-ON* */
1965}
1966
1967static void
1968app_worker_format_connects (app_worker_t * app_wrk, int verbose)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001969{
Florin Corasa332c462018-01-31 06:52:17 -08001970 svm_fifo_segment_private_t *fifo_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001971 vlib_main_t *vm = vlib_get_main ();
1972 segment_manager_t *sm;
1973 u8 *app_name, *s = 0;
Florin Coras15531972018-08-12 23:50:53 -07001974 application_t *app;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001975
1976 /* Header */
Florin Coras15531972018-08-12 23:50:53 -07001977 if (!app_wrk)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001978 {
1979 if (verbose)
1980 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1981 "API Client", "SegManager");
1982 else
1983 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1984 return;
1985 }
1986
Florin Coras15531972018-08-12 23:50:53 -07001987 app = application_get (app_wrk->app_index);
1988 if (app_wrk->connects_seg_manager == (u32) ~ 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001989 return;
1990
1991 app_name = app_get_name_from_reg_index (app);
1992
1993 /* Across all fifo segments */
Florin Coras15531972018-08-12 23:50:53 -07001994 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001995
Florin Corasa332c462018-01-31 06:52:17 -08001996 /* *INDENT-OFF* */
1997 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
1998 svm_fifo_t *fifo;
1999 u8 *str;
2000
2001 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
2002 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -07002003 {
Florin Coras6cf30ad2017-04-04 23:08:23 -07002004 u32 session_index, thread_index;
2005 stream_session_t *session;
2006
Florin Corasa5464812017-04-19 13:00:05 -07002007 session_index = fifo->master_session_index;
2008 thread_index = fifo->master_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07002009
Florin Corascea194d2017-10-02 00:18:51 -07002010 session = session_get (session_index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002011 str = format (0, "%U", format_stream_session, session, verbose);
2012
2013 if (verbose)
2014 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
Florin Coras15531972018-08-12 23:50:53 -07002015 app->api_client_index, app_wrk->connects_seg_manager);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002016 else
2017 s = format (s, "%-40s%-20s", str, app_name);
2018
2019 vlib_cli_output (vm, "%v", s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002020 vec_reset_length (s);
2021 vec_free (str);
Dave Barach10d8cc62017-05-30 09:30:07 -04002022
2023 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -07002024 }
Florin Corasa332c462018-01-31 06:52:17 -08002025 vec_free (s);
2026 }));
2027 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07002028
2029 vec_free (app_name);
2030}
2031
Florin Coras15531972018-08-12 23:50:53 -07002032static void
2033application_format_connects (application_t * app, int verbose)
2034{
2035 app_worker_map_t *wrk_map;
2036 app_worker_t *app_wrk;
2037
2038 if (!app)
2039 {
2040 app_worker_format_connects (0, verbose);
2041 return;
2042 }
2043
2044 /* *INDENT-OFF* */
2045 pool_foreach (wrk_map, app->worker_maps, ({
2046 app_wrk = app_worker_get (wrk_map->wrk_index);
2047 app_worker_format_connects (app_wrk, verbose);
2048 }));
2049 /* *INDENT-ON* */
2050}
2051
2052static void
2053app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08002054{
2055 vlib_main_t *vm = vlib_get_main ();
2056 local_session_t *ls;
2057 transport_proto_t tp;
2058 u8 *conn = 0;
2059
2060 /* Header */
Florin Coras15531972018-08-12 23:50:53 -07002061 if (app_wrk == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -08002062 {
2063 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
2064 "ClientApp");
2065 return;
2066 }
2067
Florin Coras15531972018-08-12 23:50:53 -07002068 if (!pool_elts (app_wrk->local_sessions)
2069 && !pool_elts (app_wrk->local_connects))
2070 return;
2071
Florin Corasf8f516a2018-02-08 15:10:09 -08002072 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002073 pool_foreach (ls, app_wrk->local_sessions, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08002074 tp = session_type_transport_proto(ls->listener_session_type);
2075 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2076 ls->port);
Florin Coras15531972018-08-12 23:50:53 -07002077 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_wrk_index,
2078 ls->client_wrk_index);
Florin Corasf8f516a2018-02-08 15:10:09 -08002079 vec_reset_length (conn);
2080 }));
2081 /* *INDENT-ON* */
2082
2083 vec_free (conn);
2084}
2085
Florin Coras15531972018-08-12 23:50:53 -07002086static void
2087application_format_local_sessions (application_t * app, int verbose)
2088{
Florin Corasab2f6db2018-08-31 14:31:41 -07002089 vlib_main_t *vm = vlib_get_main ();
Florin Coras15531972018-08-12 23:50:53 -07002090 app_worker_map_t *wrk_map;
2091 app_worker_t *app_wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -07002092 transport_proto_t tp;
2093 local_session_t *ls;
2094 u8 *conn = 0;
Florin Coras15531972018-08-12 23:50:53 -07002095
2096 if (!app)
2097 {
2098 app_worker_format_local_sessions (0, verbose);
2099 return;
2100 }
2101
Florin Corasab2f6db2018-08-31 14:31:41 -07002102 /*
2103 * Format local listeners
2104 */
2105
2106 /* *INDENT-OFF* */
2107 pool_foreach (ls, app->local_listen_sessions, ({
2108 tp = session_type_transport_proto (ls->listener_session_type);
2109 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
2110 ls->port);
2111 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
2112 vec_reset_length (conn);
2113 }));
2114 /* *INDENT-ON* */
2115
2116 /*
2117 * Format local accepted/connected sessions
2118 */
Florin Coras15531972018-08-12 23:50:53 -07002119 /* *INDENT-OFF* */
2120 pool_foreach (wrk_map, app->worker_maps, ({
2121 app_wrk = app_worker_get (wrk_map->wrk_index);
2122 app_worker_format_local_sessions (app_wrk, verbose);
2123 }));
2124 /* *INDENT-ON* */
2125}
2126
2127static void
2128app_worker_format_local_connects (app_worker_t * app, int verbose)
Florin Corasf8f516a2018-02-08 15:10:09 -08002129{
2130 vlib_main_t *vm = vlib_get_main ();
Florin Coras15531972018-08-12 23:50:53 -07002131 u32 app_wrk_index, session_index;
2132 app_worker_t *server_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -08002133 local_session_t *ls;
2134 uword client_key;
2135 u64 value;
2136
2137 /* Header */
2138 if (app == 0)
2139 {
2140 if (verbose)
2141 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
2142 "Peer App", "SegManager");
2143 else
2144 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
2145 "Peer App");
2146 return;
2147 }
2148
Florin Coras15531972018-08-12 23:50:53 -07002149 if (!app->local_connects)
2150 return;
2151
Florin Corasf8f516a2018-02-08 15:10:09 -08002152 /* *INDENT-OFF* */
2153 hash_foreach (client_key, value, app->local_connects, ({
Florin Coras15531972018-08-12 23:50:53 -07002154 application_client_local_connect_key_parse (client_key, &app_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -08002155 &session_index);
Florin Coras15531972018-08-12 23:50:53 -07002156 server_wrk = app_worker_get (app_wrk_index);
2157 ls = application_get_local_session (server_wrk, session_index);
2158 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_wrk_index,
2159 ls->client_wrk_index);
2160 }));
2161 /* *INDENT-ON* */
2162}
2163
2164static void
2165application_format_local_connects (application_t * app, int verbose)
2166{
2167 app_worker_map_t *wrk_map;
2168 app_worker_t *app_wrk;
2169
2170 if (!app)
2171 {
2172 app_worker_format_local_connects (0, verbose);
2173 return;
2174 }
2175
2176 /* *INDENT-OFF* */
2177 pool_foreach (wrk_map, app->worker_maps, ({
2178 app_wrk = app_worker_get (wrk_map->wrk_index);
2179 app_worker_format_local_connects (app_wrk, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002180 }));
2181 /* *INDENT-ON* */
2182}
2183
Florin Coras6cf30ad2017-04-04 23:08:23 -07002184u8 *
2185format_application (u8 * s, va_list * args)
2186{
2187 application_t *app = va_arg (*args, application_t *);
2188 CLIB_UNUSED (int verbose) = va_arg (*args, int);
Florin Corasad0c77f2017-11-09 18:00:15 -08002189 segment_manager_properties_t *props;
Florin Corascea194d2017-10-02 00:18:51 -07002190 const u8 *app_ns_name;
Florin Coras6cf30ad2017-04-04 23:08:23 -07002191 u8 *app_name;
2192
2193 if (app == 0)
2194 {
2195 if (verbose)
Florin Corascea194d2017-10-02 00:18:51 -07002196 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
Florin Coras078371e2018-05-11 09:20:12 -07002197 "API Client", "Namespace", "Add seg size", "Rx-f size",
2198 "Tx-f size");
Florin Coras6cf30ad2017-04-04 23:08:23 -07002199 else
Florin Coras0bee9ce2018-03-22 21:24:31 -07002200 s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
2201 "Namespace");
Florin Coras6cf30ad2017-04-04 23:08:23 -07002202 return s;
2203 }
2204
Florin Coras0bee9ce2018-03-22 21:24:31 -07002205 app_name = app_get_name (app);
Florin Corascea194d2017-10-02 00:18:51 -07002206 app_ns_name = app_namespace_id_from_index (app->ns_index);
Florin Corasa332c462018-01-31 06:52:17 -08002207 props = application_segment_manager_properties (app);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002208 if (verbose)
Florin Coras15531972018-08-12 23:50:53 -07002209 s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->app_index,
Florin Coras0bee9ce2018-03-22 21:24:31 -07002210 app_name, app->api_client_index, app->ns_index,
Florin Coras078371e2018-05-11 09:20:12 -07002211 format_memory_size, props->add_segment_size,
2212 format_memory_size, props->rx_fifo_size, format_memory_size,
Florin Coras0bee9ce2018-03-22 21:24:31 -07002213 props->tx_fifo_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -07002214 else
Florin Coras15531972018-08-12 23:50:53 -07002215 s = format (s, "%-10u%-20s%-15d%-40s", app->app_index, app_name,
Florin Coras8f107b32017-11-21 22:13:03 -08002216 app->api_client_index, app_ns_name);
Dave Barach68b0fb02017-02-28 15:15:56 -05002217 return s;
2218}
2219
Florin Corasf8f516a2018-02-08 15:10:09 -08002220void
2221application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
2222{
2223 application_t *app;
Florin Corasf8f516a2018-02-08 15:10:09 -08002224
Florin Coras15531972018-08-12 23:50:53 -07002225 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08002226 {
2227 vlib_cli_output (vm, "No active server bindings");
2228 return;
2229 }
2230
2231 if (do_local)
2232 {
2233 application_format_local_sessions (0, verbose);
2234 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002235 pool_foreach (app, app_main.app_pool, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08002236 application_format_local_sessions (app, verbose);
2237 }));
2238 /* *INDENT-ON* */
2239 }
2240 else
2241 {
Florin Coras15531972018-08-12 23:50:53 -07002242 application_format_listeners (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002243
2244 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002245 pool_foreach (app, app_main.app_pool, ({
2246 application_format_listeners (app, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002247 }));
2248 /* *INDENT-ON* */
2249 }
2250}
2251
2252void
2253application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
2254{
2255 application_t *app;
2256
Florin Coras15531972018-08-12 23:50:53 -07002257 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08002258 {
2259 vlib_cli_output (vm, "No active apps");
2260 return;
2261 }
2262
2263 if (do_local)
2264 {
2265 application_format_local_connects (0, verbose);
2266
2267 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002268 pool_foreach (app, app_main.app_pool, ({
2269 application_format_local_connects (app, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08002270 }));
2271 /* *INDENT-ON* */
2272 }
2273 else
2274 {
2275 application_format_connects (0, verbose);
2276
2277 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002278 pool_foreach (app, app_main.app_pool, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08002279 application_format_connects (app, verbose);
2280 }));
2281 /* *INDENT-ON* */
2282 }
2283}
2284
Dave Barach68b0fb02017-02-28 15:15:56 -05002285static clib_error_t *
2286show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
2287 vlib_cli_command_t * cmd)
2288{
Florin Corasf8f516a2018-02-08 15:10:09 -08002289 int do_server = 0, do_client = 0, do_local = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002290 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05002291 int verbose = 0;
2292
Florin Corascea194d2017-10-02 00:18:51 -07002293 session_cli_return_if_not_enabled ();
Florin Corase04c2992017-03-01 08:17:34 -08002294
Dave Barach68b0fb02017-02-28 15:15:56 -05002295 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2296 {
2297 if (unformat (input, "server"))
2298 do_server = 1;
2299 else if (unformat (input, "client"))
2300 do_client = 1;
Florin Corasf8f516a2018-02-08 15:10:09 -08002301 else if (unformat (input, "local"))
2302 do_local = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05002303 else if (unformat (input, "verbose"))
2304 verbose = 1;
2305 else
2306 break;
2307 }
2308
2309 if (do_server)
Florin Corasf8f516a2018-02-08 15:10:09 -08002310 application_format_all_listeners (vm, do_local, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05002311
2312 if (do_client)
Florin Corasf8f516a2018-02-08 15:10:09 -08002313 application_format_all_clients (vm, do_local, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05002314
Florin Coras6cf30ad2017-04-04 23:08:23 -07002315 /* Print app related info */
2316 if (!do_server && !do_client)
2317 {
2318 vlib_cli_output (vm, "%U", format_application, 0, verbose);
Florin Corascea194d2017-10-02 00:18:51 -07002319 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07002320 pool_foreach (app, app_main.app_pool, ({
Florin Corascea194d2017-10-02 00:18:51 -07002321 vlib_cli_output (vm, "%U", format_application, app, verbose);
2322 }));
2323 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07002324 }
2325
Dave Barach68b0fb02017-02-28 15:15:56 -05002326 return 0;
2327}
2328
Florin Corase04c2992017-03-01 08:17:34 -08002329/* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -05002330VLIB_CLI_COMMAND (show_app_command, static) =
2331{
Florin Corase04c2992017-03-01 08:17:34 -08002332 .path = "show app",
2333 .short_help = "show app [server|client] [verbose]",
2334 .function = show_app_command_fn,
2335};
2336/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05002337
2338/*
2339 * fd.io coding-style-patch-verification: ON
2340 *
2341 * Local Variables:
2342 * eval: (c-set-style "gnu")
2343 * End:
2344 */