blob: 4d95426568eef2375b5f67c05b88d6fbc820a3e9 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/session/application.h>
Florin Coras6cf30ad2017-04-04 23:08:23 -070017#include <vnet/session/application_interface.h>
Florin Corascea194d2017-10-02 00:18:51 -070018#include <vnet/session/application_namespace.h>
Florin Corasba7d8f52019-02-22 13:11:38 -080019#include <vnet/session/application_local.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <vnet/session/session.h>
21
Florin Coras15531972018-08-12 23:50:53 -070022static app_main_t app_main;
Dave Barach68b0fb02017-02-28 15:15:56 -050023
Florin Corasc1a42652019-02-08 18:27:29 -080024#define app_interface_check_thread_and_barrier(_fn, _arg) \
25 if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \
26 { \
27 vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \
28 return 0; \
29 }
30
Florin Corasab2f6db2018-08-31 14:31:41 -070031static app_listener_t *
32app_listener_alloc (application_t * app)
33{
34 app_listener_t *app_listener;
35 pool_get (app->listeners, app_listener);
Dave Barachb7b92992018-10-17 10:38:51 -040036 clib_memset (app_listener, 0, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070037 app_listener->al_index = app_listener - app->listeners;
Florin Corasc9940fc2019-02-05 20:55:11 -080038 app_listener->app_index = app->app_index;
39 app_listener->session_index = SESSION_INVALID_INDEX;
40 app_listener->local_index = SESSION_INVALID_INDEX;
Florin Corasab2f6db2018-08-31 14:31:41 -070041 return app_listener;
42}
43
Florin Corasc9940fc2019-02-05 20:55:11 -080044app_listener_t *
Florin Corasab2f6db2018-08-31 14:31:41 -070045app_listener_get (application_t * app, u32 app_listener_index)
46{
47 return pool_elt_at_index (app->listeners, app_listener_index);
48}
49
50static void
51app_listener_free (application_t * app, app_listener_t * app_listener)
52{
53 clib_bitmap_free (app_listener->workers);
54 pool_put (app->listeners, app_listener);
55 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040056 clib_memset (app_listener, 0xfa, sizeof (*app_listener));
Florin Corasab2f6db2018-08-31 14:31:41 -070057}
58
Florin Corasc9940fc2019-02-05 20:55:11 -080059static u32
60app_listener_id (app_listener_t * al)
61{
62 ASSERT (al->app_index < 1 << 16 && al->al_index < 1 << 16);
63 return (al->app_index << 16 | al->al_index);
64}
65
66session_handle_t
67app_listener_handle (app_listener_t * al)
68{
69 return ((u64) SESSION_LISTENER_PREFIX << 32 | (u64) app_listener_id (al));
Florin Corasab2f6db2018-08-31 14:31:41 -070070}
71
72static void
Florin Corasc9940fc2019-02-05 20:55:11 -080073app_listener_id_parse (u32 listener_id, u32 * app_index,
74 u32 * app_listener_index)
Florin Corasab2f6db2018-08-31 14:31:41 -070075{
Florin Corasc9940fc2019-02-05 20:55:11 -080076 *app_index = listener_id >> 16;
77 *app_listener_index = listener_id & 0xFFFF;
78}
79
80void
81app_listener_handle_parse (session_handle_t handle, u32 * app_index,
82 u32 * app_listener_index)
83{
84 app_listener_id_parse (handle & 0xFFFFFFFF, app_index, app_listener_index);
85}
86
87static app_listener_t *
88app_listener_get_w_id (u32 listener_id)
89{
90 u32 app_index, app_listener_index;
91 application_t *app;
92
93 app_listener_id_parse (listener_id, &app_index, &app_listener_index);
94 app = application_get_if_valid (app_index);
95 if (!app)
96 return 0;
97 return app_listener_get (app, app_listener_index);
98}
99
100app_listener_t *
101app_listener_get_w_session (session_t * ls)
102{
103 application_t *app;
104
105 app = application_get_if_valid (ls->app_index);
106 if (!app)
107 return 0;
108 return app_listener_get (app, ls->al_index);
109}
110
111app_listener_t *
112app_listener_get_w_handle (session_handle_t handle)
113{
114
115 if (handle >> 32 != SESSION_LISTENER_PREFIX)
116 return 0;
117
118 return app_listener_get_w_id (handle & 0xFFFFFFFF);
119}
120
121app_listener_t *
122app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
123{
124 u32 table_index, fib_proto;
125 session_endpoint_t *sep;
126 session_handle_t handle;
Florin Corasc9940fc2019-02-05 20:55:11 -0800127 session_t *ls;
128
129 sep = (session_endpoint_t *) sep_ext;
130 if (application_has_local_scope (app) && session_endpoint_is_local (sep))
131 {
132 table_index = application_local_session_table (app);
133 handle = session_lookup_endpoint_listener (table_index, sep, 1);
134 if (handle != SESSION_INVALID_HANDLE)
135 {
Florin Corasd4295e62019-02-22 13:11:38 -0800136 ls = listen_session_get_from_handle (handle);
137 return app_listener_get_w_session (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800138 }
139 }
140
141 fib_proto = session_endpoint_fib_proto (sep);
142 table_index = application_session_table (app, fib_proto);
143 handle = session_lookup_endpoint_listener (table_index, sep, 1);
144 if (handle != SESSION_INVALID_HANDLE)
145 {
146 ls = listen_session_get_from_handle (handle);
147 return app_listener_get_w_session ((session_t *) ls);
148 }
149
150 return 0;
151}
152
153int
154app_listener_alloc_and_init (application_t * app,
155 session_endpoint_cfg_t * sep,
156 app_listener_t ** listener)
157{
158 app_listener_t *app_listener;
Florin Corasd4295e62019-02-22 13:11:38 -0800159 transport_connection_t *tc;
Florin Corasc9940fc2019-02-05 20:55:11 -0800160 local_session_t *ll = 0;
161 session_handle_t lh;
162 session_type_t st;
163 session_t *ls = 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800164 u32 al_index;
Florin Corasc9940fc2019-02-05 20:55:11 -0800165 int rv;
166
167 app_listener = app_listener_alloc (app);
Florin Corasa27a46e2019-02-18 13:02:28 -0800168 al_index = app_listener->al_index;
Florin Corasc9940fc2019-02-05 20:55:11 -0800169 st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
170
171 /*
172 * Add session endpoint to local session table. Only binds to "inaddr_any"
173 * (i.e., zero address) are added to local scope table.
174 */
175 if (application_has_local_scope (app)
176 && session_endpoint_is_local ((session_endpoint_t *) sep))
177 {
Florin Corasd4295e62019-02-22 13:11:38 -0800178 session_type_t local_st;
Florin Corasc9940fc2019-02-05 20:55:11 -0800179 u32 table_index;
180
Florin Corasd4295e62019-02-22 13:11:38 -0800181 local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
182 sep->is_ip4);
183 ls = listen_session_alloc (0, local_st);
184 ls->app_index = app->app_index;
185 ls->app_wrk_index = sep->app_wrk_index;
186 lh = session_handle (ls);
187
188 if ((rv = session_listen (ls, sep)))
189 {
190 ls = session_get_from_handle (lh);
191 session_free (ls);
192 return rv;
193 }
194
195 ls = session_get_from_handle (lh);
196 app_listener = app_listener_get (app, al_index);
197 app_listener->local_index = ls->session_index;
198 ls->al_index = al_index;
199
Florin Corasc9940fc2019-02-05 20:55:11 -0800200 table_index = application_local_session_table (app);
Florin Corasc9940fc2019-02-05 20:55:11 -0800201 session_lookup_add_session_endpoint (table_index,
202 (session_endpoint_t *) sep, lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800203 }
204
205 if (application_has_global_scope (app))
206 {
207 /*
208 * Start listening on local endpoint for requested transport and scope.
209 * Creates a stream session with state LISTENING to be used in session
210 * lookups, prior to establishing connection. Requests transport to
211 * build it's own specific listening connection.
212 */
Florin Corasba7d8f52019-02-22 13:11:38 -0800213 ls = listen_session_alloc (0, st);
Florin Corasc9940fc2019-02-05 20:55:11 -0800214 ls->app_index = app->app_index;
215 ls->app_wrk_index = sep->app_wrk_index;
216
217 /* Listen pool can be reallocated if the transport is
218 * recursive (tls) */
Florin Corasd4295e62019-02-22 13:11:38 -0800219 lh = listen_session_get_handle (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800220
221 if ((rv = session_listen (ls, sep)))
222 {
Florin Corasd4295e62019-02-22 13:11:38 -0800223 ls = listen_session_get_from_handle (lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800224 session_free (ls);
225 return rv;
226 }
Florin Corasd4295e62019-02-22 13:11:38 -0800227 ls = listen_session_get_from_handle (lh);
Florin Corasa27a46e2019-02-18 13:02:28 -0800228 app_listener = app_listener_get (app, al_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800229 app_listener->session_index = ls->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800230 ls->al_index = al_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800231
232 /* Add to the global lookup table after transport was initialized.
233 * Lookup table needs to be populated only now because sessions
234 * with cut-through transport are are added to app local tables that
235 * are not related to network fibs, i.e., cannot be added as
236 * connections */
237 tc = session_get_transport (ls);
238 session_lookup_add_connection (tc, lh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800239 }
240
241 if (!ll && !ls)
242 {
243 app_listener_free (app, app_listener);
244 return -1;
245 }
246
247 *listener = app_listener;
248 return 0;
249}
250
251void
252app_listener_cleanup (app_listener_t * al)
253{
254 application_t *app = application_get (al->app_index);
Florin Corasd4295e62019-02-22 13:11:38 -0800255 session_t *ls;
Florin Corasc9940fc2019-02-05 20:55:11 -0800256
257 if (al->session_index != SESSION_INVALID_INDEX)
258 {
Florin Corasd4295e62019-02-22 13:11:38 -0800259 ls = session_get (al->session_index, 0);
Florin Corasc9940fc2019-02-05 20:55:11 -0800260 session_stop_listen (ls);
Florin Corasba7d8f52019-02-22 13:11:38 -0800261 listen_session_free (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800262 }
263 if (al->local_index != SESSION_INVALID_INDEX)
264 {
265 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Corasc9940fc2019-02-05 20:55:11 -0800266 u32 table_index;
267
268 table_index = application_local_session_table (app);
Florin Corasd4295e62019-02-22 13:11:38 -0800269 ls = listen_session_get (al->local_index);
270 application_local_listener_session_endpoint (ls, &sep);
Florin Corasc9940fc2019-02-05 20:55:11 -0800271 session_lookup_del_session_endpoint (table_index, &sep);
Florin Corasd4295e62019-02-22 13:11:38 -0800272 listen_session_free (ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800273 }
274 app_listener_free (app, al);
275}
276
277app_worker_t *
278app_listener_select_worker (app_listener_t * al)
279{
280 application_t *app;
281 u32 wrk_index;
282
283 app = application_get (al->app_index);
284 wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
285 if (wrk_index == ~0)
286 wrk_index = clib_bitmap_first_set (al->workers);
287
288 ASSERT (wrk_index != ~0);
289 al->accept_rotor = wrk_index;
290 return application_get_worker (app, wrk_index);
291}
292
293session_t *
294app_listener_get_session (app_listener_t * al)
295{
296 if (al->session_index == SESSION_INVALID_INDEX)
297 return 0;
298
299 return listen_session_get (al->session_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700300}
301
Florin Corasd4295e62019-02-22 13:11:38 -0800302session_t *
303app_listener_get_local_session (app_listener_t * al)
304{
305 if (al->local_index == SESSION_INVALID_INDEX)
306 return 0;
307 return listen_session_get (al->local_index);
308}
309
Florin Coras15531972018-08-12 23:50:53 -0700310static app_worker_map_t *
311app_worker_map_alloc (application_t * app)
312{
313 app_worker_map_t *map;
314 pool_get (app->worker_maps, map);
Dave Barachb7b92992018-10-17 10:38:51 -0400315 clib_memset (map, 0, sizeof (*map));
Florin Coras15531972018-08-12 23:50:53 -0700316 return map;
317}
Dave Barach68b0fb02017-02-28 15:15:56 -0500318
Florin Coras15531972018-08-12 23:50:53 -0700319static u32
320app_worker_map_index (application_t * app, app_worker_map_t * map)
321{
322 return (map - app->worker_maps);
323}
324
325static void
326app_worker_map_free (application_t * app, app_worker_map_t * map)
327{
328 pool_put (app->worker_maps, map);
329}
330
331static app_worker_map_t *
332app_worker_map_get (application_t * app, u32 map_index)
333{
Florin Coras01f3f892018-12-02 12:45:53 -0800334 if (pool_is_free_index (app->worker_maps, map_index))
335 return 0;
Florin Coras15531972018-08-12 23:50:53 -0700336 return pool_elt_at_index (app->worker_maps, map_index);
337}
Florin Coras0bee9ce2018-03-22 21:24:31 -0700338
Florin Coras053a0e42018-11-13 15:52:38 -0800339static const u8 *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700340app_get_name (application_t * app)
341{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700342 return app->name;
343}
344
Florin Corascea194d2017-10-02 00:18:51 -0700345u32
346application_session_table (application_t * app, u8 fib_proto)
347{
348 app_namespace_t *app_ns;
349 app_ns = app_namespace_get (app->ns_index);
350 if (!application_has_global_scope (app))
351 return APP_INVALID_INDEX;
352 if (fib_proto == FIB_PROTOCOL_IP4)
353 return session_lookup_get_index_for_fib (fib_proto,
354 app_ns->ip4_fib_index);
355 else
356 return session_lookup_get_index_for_fib (fib_proto,
357 app_ns->ip6_fib_index);
358}
359
360u32
361application_local_session_table (application_t * app)
362{
363 app_namespace_t *app_ns;
364 if (!application_has_local_scope (app))
365 return APP_INVALID_INDEX;
366 app_ns = app_namespace_get (app->ns_index);
367 return app_ns->local_table_index;
368}
369
Florin Corascea194d2017-10-02 00:18:51 -0700370/**
Florin Coras053a0e42018-11-13 15:52:38 -0800371 * Returns app name for app-index
Florin Corascea194d2017-10-02 00:18:51 -0700372 */
Florin Coras053a0e42018-11-13 15:52:38 -0800373const u8 *
Florin Corascea194d2017-10-02 00:18:51 -0700374application_name_from_index (u32 app_index)
375{
376 application_t *app = application_get (app_index);
377 if (!app)
378 return 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800379 return app_get_name (app);
380}
381
382static void
383application_api_table_add (u32 app_index, u32 api_client_index)
384{
Florin Corasc1f5a432018-11-20 11:31:26 -0800385 if (api_client_index != APP_INVALID_INDEX)
386 hash_set (app_main.app_by_api_client_index, api_client_index, app_index);
Florin Coras053a0e42018-11-13 15:52:38 -0800387}
388
389static void
390application_api_table_del (u32 api_client_index)
391{
392 hash_unset (app_main.app_by_api_client_index, api_client_index);
Florin Corascea194d2017-10-02 00:18:51 -0700393}
394
Dave Barach68b0fb02017-02-28 15:15:56 -0500395static void
Florin Corasc1f5a432018-11-20 11:31:26 -0800396application_name_table_add (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500397{
Florin Corasc1f5a432018-11-20 11:31:26 -0800398 hash_set_mem (app_main.app_by_name, app->name, app->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500399}
400
401static void
Florin Corasc1f5a432018-11-20 11:31:26 -0800402application_name_table_del (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500403{
Florin Corasc1f5a432018-11-20 11:31:26 -0800404 hash_unset_mem (app_main.app_by_name, app->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500405}
406
407application_t *
408application_lookup (u32 api_client_index)
409{
410 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700411 p = hash_get (app_main.app_by_api_client_index, api_client_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500412 if (p)
Florin Coras053a0e42018-11-13 15:52:38 -0800413 return application_get_if_valid (p[0]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500414
415 return 0;
416}
417
Florin Coras6cf30ad2017-04-04 23:08:23 -0700418application_t *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700419application_lookup_name (const u8 * name)
420{
421 uword *p;
Florin Coras15531972018-08-12 23:50:53 -0700422 p = hash_get_mem (app_main.app_by_name, name);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700423 if (p)
424 return application_get (p[0]);
425
426 return 0;
427}
428
Florin Corasc1a42652019-02-08 18:27:29 -0800429static application_t *
Florin Coras15531972018-08-12 23:50:53 -0700430application_alloc (void)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700431{
432 application_t *app;
Florin Coras15531972018-08-12 23:50:53 -0700433 pool_get (app_main.app_pool, app);
Dave Barachb7b92992018-10-17 10:38:51 -0400434 clib_memset (app, 0, sizeof (*app));
Florin Coras15531972018-08-12 23:50:53 -0700435 app->app_index = app - app_main.app_pool;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700436 return app;
437}
438
Florin Coras15531972018-08-12 23:50:53 -0700439application_t *
440application_get (u32 app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500441{
Florin Coras15531972018-08-12 23:50:53 -0700442 if (app_index == APP_INVALID_INDEX)
443 return 0;
444 return pool_elt_at_index (app_main.app_pool, app_index);
445}
Dave Barach68b0fb02017-02-28 15:15:56 -0500446
Florin Coras15531972018-08-12 23:50:53 -0700447application_t *
448application_get_if_valid (u32 app_index)
449{
450 if (pool_is_free_index (app_main.app_pool, app_index))
451 return 0;
Florin Corasa5464812017-04-19 13:00:05 -0700452
Florin Coras15531972018-08-12 23:50:53 -0700453 return pool_elt_at_index (app_main.app_pool, app_index);
454}
Florin Coras7999e832017-10-31 01:51:04 -0700455
Florin Corasd79b41e2017-03-04 05:37:52 -0800456static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700457application_verify_cb_fns (session_cb_vft_t * cb_fns)
Florin Corasd79b41e2017-03-04 05:37:52 -0800458{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 if (cb_fns->session_accept_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800460 clib_warning ("No accept callback function provided");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700461 if (cb_fns->session_connected_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800462 clib_warning ("No session connected callback function provided");
463 if (cb_fns->session_disconnect_callback == 0)
464 clib_warning ("No session disconnect callback function provided");
465 if (cb_fns->session_reset_callback == 0)
466 clib_warning ("No session reset callback function provided");
467}
468
Florin Corasb384b542018-01-15 01:08:33 -0800469/**
470 * Check app config for given segment type
471 *
472 * Returns 1 on success and 0 otherwise
473 */
474static u8
475application_verify_cfg (ssvm_segment_type_t st)
476{
477 u8 is_valid;
478 if (st == SSVM_SEGMENT_MEMFD)
479 {
480 is_valid = (session_manager_get_evt_q_segment () != 0);
481 if (!is_valid)
482 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
483 return is_valid;
484 }
485 else if (st == SSVM_SEGMENT_SHM)
486 {
487 is_valid = (session_manager_get_evt_q_segment () == 0);
488 if (!is_valid)
489 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
490 return is_valid;
491 }
492 else
493 return 1;
494}
495
Florin Corasc1a42652019-02-08 18:27:29 -0800496static int
Florin Coras15531972018-08-12 23:50:53 -0700497application_alloc_and_init (app_init_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500498{
Florin Corasa332c462018-01-31 06:52:17 -0800499 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
Florin Corasb384b542018-01-15 01:08:33 -0800500 segment_manager_properties_t *props;
501 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700502 application_t *app;
503 u64 *options;
Dave Barach68b0fb02017-02-28 15:15:56 -0500504
Florin Coras15531972018-08-12 23:50:53 -0700505 app = application_alloc ();
506 options = a->options;
Florin Corasb384b542018-01-15 01:08:33 -0800507 /*
508 * Make sure we support the requested configuration
509 */
Florin Corasa332c462018-01-31 06:52:17 -0800510 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
511 {
Florin Coras15531972018-08-12 23:50:53 -0700512 reg = vl_api_client_index_to_registration (a->api_client_index);
Florin Corasa332c462018-01-31 06:52:17 -0800513 if (!reg)
514 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
515 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
516 seg_type = SSVM_SEGMENT_SHM;
517 }
518 else
519 {
Florin Coras99368312018-08-02 10:45:44 -0700520 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
521 {
522 clib_warning ("mq eventfds can only be used if socket transport is "
523 "used for api");
524 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
525 }
Florin Corasa332c462018-01-31 06:52:17 -0800526 seg_type = SSVM_SEGMENT_PRIVATE;
527 }
Florin Corasb384b542018-01-15 01:08:33 -0800528
Florin Corasa332c462018-01-31 06:52:17 -0800529 if (!application_verify_cfg (seg_type))
Florin Corasb384b542018-01-15 01:08:33 -0800530 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
Dave Barach68b0fb02017-02-28 15:15:56 -0500531
Florin Coras15531972018-08-12 23:50:53 -0700532 /* Check that the obvious things are properly set up */
533 application_verify_cb_fns (a->session_cb_vft);
534
Florin Coras15531972018-08-12 23:50:53 -0700535 app->flags = options[APP_OPTIONS_FLAGS];
536 app->cb_fns = *a->session_cb_vft;
537 app->ns_index = options[APP_OPTIONS_NAMESPACE];
538 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
539 app->name = vec_dup (a->name);
540
541 /* If no scope enabled, default to global */
542 if (!application_has_global_scope (app)
543 && !application_has_local_scope (app))
544 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
545
Florin Corasa332c462018-01-31 06:52:17 -0800546 props = application_segment_manager_properties (app);
547 segment_manager_properties_init (props);
Florin Coras15531972018-08-12 23:50:53 -0700548 props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
549 props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
Florin Corasb384b542018-01-15 01:08:33 -0800550 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
551 {
552 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
553 props->add_segment = 1;
554 }
555 if (options[APP_OPTIONS_RX_FIFO_SIZE])
556 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
557 if (options[APP_OPTIONS_TX_FIFO_SIZE])
558 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
Florin Corasf8f516a2018-02-08 15:10:09 -0800559 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
560 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
Florin Coras99368312018-08-02 10:45:44 -0700561 if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
562 props->use_mq_eventfd = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800563 if (options[APP_OPTIONS_TLS_ENGINE])
564 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
Florin Corasa332c462018-01-31 06:52:17 -0800565 props->segment_type = seg_type;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566
Florin Coras15531972018-08-12 23:50:53 -0700567 /* Add app to lookup by api_client_index table */
Florin Corasc1f5a432018-11-20 11:31:26 -0800568 if (!application_is_builtin (app))
569 application_api_table_add (app->app_index, a->api_client_index);
570 else
571 application_name_table_add (app);
572
573 a->app_index = app->app_index;
Florin Corasa332c462018-01-31 06:52:17 -0800574
Florin Coras15531972018-08-12 23:50:53 -0700575 APP_DBG ("New app name: %v api index: %u index %u", app->name,
576 app->api_client_index, app->app_index);
577
578 return 0;
579}
580
Florin Corasc1a42652019-02-08 18:27:29 -0800581static void
Florin Coras15531972018-08-12 23:50:53 -0700582application_free (application_t * app)
583{
584 app_worker_map_t *wrk_map;
585 app_worker_t *app_wrk;
586
587 /*
588 * The app event queue allocated in first segment is cleared with
589 * the segment manager. No need to explicitly free it.
590 */
591 APP_DBG ("Delete app name %v api index: %d index: %d", app->name,
592 app->api_client_index, app->app_index);
593
594 if (application_is_proxy (app))
595 application_remove_proxy (app);
596
Florin Corasab2f6db2018-08-31 14:31:41 -0700597 /*
598 * Free workers
599 */
600
Florin Coras15531972018-08-12 23:50:53 -0700601 /* *INDENT-OFF* */
602 pool_flush (wrk_map, app->worker_maps, ({
603 app_wrk = app_worker_get (wrk_map->wrk_index);
604 app_worker_free (app_wrk);
605 }));
606 /* *INDENT-ON* */
607 pool_free (app->worker_maps);
608
Florin Corasab2f6db2018-08-31 14:31:41 -0700609 /*
Florin Corasab2f6db2018-08-31 14:31:41 -0700610 * Cleanup remaining state
611 */
Florin Corasc1f5a432018-11-20 11:31:26 -0800612 if (application_is_builtin (app))
613 application_name_table_del (app);
Florin Coras15531972018-08-12 23:50:53 -0700614 vec_free (app->name);
615 vec_free (app->tls_cert);
616 vec_free (app->tls_key);
617 pool_put (app_main.app_pool, app);
618}
619
Florin Corasc1a42652019-02-08 18:27:29 -0800620static void
Florin Coras053a0e42018-11-13 15:52:38 -0800621application_detach_process (application_t * app, u32 api_client_index)
622{
623 vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args;
624 app_worker_map_t *wrk_map;
625 u32 *wrks = 0, *wrk_index;
626 app_worker_t *app_wrk;
627
628 if (api_client_index == ~0)
629 {
630 application_free (app);
631 return;
632 }
633
634 APP_DBG ("Detaching for app %v index %u api client index %u", app->name,
635 app->app_index, app->api_client_index);
636
637 /* *INDENT-OFF* */
638 pool_foreach (wrk_map, app->worker_maps, ({
639 app_wrk = app_worker_get (wrk_map->wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800640 if (app_wrk->api_client_index == api_client_index)
Florin Coras053a0e42018-11-13 15:52:38 -0800641 vec_add1 (wrks, app_wrk->wrk_index);
642 }));
643 /* *INDENT-ON* */
644
645 if (!vec_len (wrks))
646 {
647 clib_warning ("no workers for app %u api_index %u", app->app_index,
648 api_client_index);
649 return;
650 }
651
652 args->app_index = app->app_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800653 args->api_client_index = api_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800654 vec_foreach (wrk_index, wrks)
655 {
656 app_wrk = app_worker_get (wrk_index[0]);
Florin Coras349f8ca2018-11-20 16:52:49 -0800657 args->wrk_map_index = app_wrk->wrk_map_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800658 args->is_add = 0;
659 vnet_app_worker_add_del (args);
660 }
661 vec_free (wrks);
662}
663
Florin Coras15531972018-08-12 23:50:53 -0700664app_worker_t *
665application_get_worker (application_t * app, u32 wrk_map_index)
666{
667 app_worker_map_t *map;
668 map = app_worker_map_get (app, wrk_map_index);
669 if (!map)
670 return 0;
671 return app_worker_get (map->wrk_index);
672}
673
674app_worker_t *
675application_get_default_worker (application_t * app)
676{
677 return application_get_worker (app, 0);
678}
679
Florin Coras053a0e42018-11-13 15:52:38 -0800680u32
681application_n_workers (application_t * app)
682{
683 return pool_elts (app->worker_maps);
684}
685
Florin Coras15531972018-08-12 23:50:53 -0700686app_worker_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800687application_listener_select_worker (session_t * ls)
Florin Corasab2f6db2018-08-31 14:31:41 -0700688{
Florin Corasc9940fc2019-02-05 20:55:11 -0800689 app_listener_t *al;
Florin Corasab2f6db2018-08-31 14:31:41 -0700690
Florin Corasc9940fc2019-02-05 20:55:11 -0800691 al = app_listener_get_w_session (ls);
692 return app_listener_select_worker (al);
Florin Corasab2f6db2018-08-31 14:31:41 -0700693}
694
Florin Coras15531972018-08-12 23:50:53 -0700695int
Florin Coras623eb562019-02-03 19:28:34 -0800696application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk)
Florin Coras15531972018-08-12 23:50:53 -0700697{
698 app_worker_map_t *wrk_map;
699 app_worker_t *app_wrk;
700 segment_manager_t *sm;
701 int rv;
702
703 app_wrk = app_worker_alloc (app);
704 wrk_map = app_worker_map_alloc (app);
705 wrk_map->wrk_index = app_wrk->wrk_index;
706 app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
707
708 /*
709 * Setup first segment manager
710 */
711 sm = segment_manager_new ();
712 sm->app_wrk_index = app_wrk->wrk_index;
713
714 if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
715 app->sm_properties.prealloc_fifos)))
716 {
717 app_worker_free (app_wrk);
718 return rv;
719 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700720 sm->first_is_protected = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500721
Florin Corascea194d2017-10-02 00:18:51 -0700722 /*
Florin Coras15531972018-08-12 23:50:53 -0700723 * Setup app worker
Florin Corascea194d2017-10-02 00:18:51 -0700724 */
Florin Coras15531972018-08-12 23:50:53 -0700725 app_wrk->first_segment_manager = segment_manager_index (sm);
726 app_wrk->listeners_table = hash_create (0, sizeof (u64));
727 app_wrk->event_queue = segment_manager_event_queue (sm);
728 app_wrk->app_is_builtin = application_is_builtin (app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500729
Florin Corasf8f516a2018-02-08 15:10:09 -0800730 /*
731 * Segment manager for local sessions
732 */
733 sm = segment_manager_new ();
Florin Coras15531972018-08-12 23:50:53 -0700734 sm->app_wrk_index = app_wrk->wrk_index;
735 app_wrk->local_segment_manager = segment_manager_index (sm);
736 app_wrk->local_connects = hash_create (0, sizeof (u64));
737
738 *wrk = app_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -0800739
Florin Coras6cf30ad2017-04-04 23:08:23 -0700740 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500741}
742
Florin Coras623eb562019-02-03 19:28:34 -0800743int
Florin Corasc1a42652019-02-08 18:27:29 -0800744vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a)
745{
746 svm_fifo_segment_private_t *fs;
747 app_worker_map_t *wrk_map;
748 app_worker_t *app_wrk;
749 segment_manager_t *sm;
750 application_t *app;
751 int rv;
752
753 app = application_get (a->app_index);
754 if (!app)
755 return VNET_API_ERROR_INVALID_VALUE;
756
757 if (a->is_add)
758 {
759 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
760 return rv;
761
762 /* Map worker api index to the app */
763 app_wrk->api_client_index = a->api_client_index;
764 application_api_table_add (app->app_index, a->api_client_index);
765
766 sm = segment_manager_get (app_wrk->first_segment_manager);
767 fs = segment_manager_get_segment_w_lock (sm, 0);
768 a->segment = &fs->ssvm;
769 a->segment_handle = segment_manager_segment_handle (sm, fs);
770 segment_manager_segment_reader_unlock (sm);
771 a->evt_q = app_wrk->event_queue;
772 a->wrk_map_index = app_wrk->wrk_map_index;
773 }
774 else
775 {
776 wrk_map = app_worker_map_get (app, a->wrk_map_index);
777 if (!wrk_map)
778 return VNET_API_ERROR_INVALID_VALUE;
779
780 app_wrk = app_worker_get (wrk_map->wrk_index);
781 if (!app_wrk)
782 return VNET_API_ERROR_INVALID_VALUE;
783
784 application_api_table_del (app_wrk->api_client_index);
785 app_worker_free (app_wrk);
786 app_worker_map_free (app, wrk_map);
787 if (application_n_workers (app) == 0)
788 application_free (app);
789 }
790 return 0;
791}
792
793static int
794app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
795{
796 app_namespace_t *app_ns;
797 if (vec_len (namespace_id) == 0)
798 {
799 /* Use default namespace */
800 *app_ns_index = 0;
801 return 0;
802 }
803
804 *app_ns_index = app_namespace_index_from_id (namespace_id);
805 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
806 return VNET_API_ERROR_APP_INVALID_NS;
807 app_ns = app_namespace_get (*app_ns_index);
808 if (!app_ns)
809 return VNET_API_ERROR_APP_INVALID_NS;
810 if (app_ns->ns_secret != secret)
811 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
812 return 0;
813}
814
815static u8 *
816app_name_from_api_index (u32 api_client_index)
817{
818 vl_api_registration_t *regp;
819 regp = vl_api_client_index_to_registration (api_client_index);
820 if (regp)
821 return format (0, "%s%c", regp->name, 0);
822
823 clib_warning ("api client index %u does not have an api registration!",
824 api_client_index);
825 return format (0, "unknown%c", 0);
826}
827
828/**
829 * Attach application to vpp
830 *
831 * Allocates a vpp app, i.e., a structure that keeps back pointers
832 * to external app and a segment manager for shared memory fifo based
833 * communication with the external app.
834 */
835int
836vnet_application_attach (vnet_app_attach_args_t * a)
837{
838 svm_fifo_segment_private_t *fs;
839 application_t *app = 0;
840 app_worker_t *app_wrk;
841 segment_manager_t *sm;
842 u32 app_ns_index = 0;
843 u8 *app_name = 0;
844 u64 secret;
845 int rv;
846
847 if (a->api_client_index != APP_INVALID_INDEX)
848 app = application_lookup (a->api_client_index);
849 else if (a->name)
850 app = application_lookup_name (a->name);
851 else
852 return VNET_API_ERROR_INVALID_VALUE;
853
854 if (app)
855 return VNET_API_ERROR_APP_ALREADY_ATTACHED;
856
857 if (a->api_client_index != APP_INVALID_INDEX)
858 {
859 app_name = app_name_from_api_index (a->api_client_index);
860 a->name = app_name;
861 }
862
863 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
864 if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
865 return rv;
866 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
867
868 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
869 return rv;
870
871 app = application_get (a->app_index);
872 if ((rv = application_alloc_worker_and_init (app, &app_wrk)))
873 return rv;
874
875 a->app_evt_q = app_wrk->event_queue;
876 app_wrk->api_client_index = a->api_client_index;
877 sm = segment_manager_get (app_wrk->first_segment_manager);
878 fs = segment_manager_get_segment_w_lock (sm, 0);
879
880 if (application_is_proxy (app))
881 application_setup_proxy (app);
882
883 ASSERT (vec_len (fs->ssvm.name) <= 128);
884 a->segment = &fs->ssvm;
885 a->segment_handle = segment_manager_segment_handle (sm, fs);
886
887 segment_manager_segment_reader_unlock (sm);
888 vec_free (app_name);
889 return 0;
890}
891
892/**
893 * Detach application from vpp
894 */
895int
896vnet_application_detach (vnet_app_detach_args_t * a)
897{
898 application_t *app;
899
900 app = application_get_if_valid (a->app_index);
901 if (!app)
902 {
903 clib_warning ("app not attached");
904 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
905 }
906
907 app_interface_check_thread_and_barrier (vnet_application_detach, a);
908 application_detach_process (app, a->api_client_index);
909 return 0;
910}
911
912
913static u8
914session_endpoint_in_ns (session_endpoint_t * sep)
915{
916 u8 is_lep = session_endpoint_is_local (sep);
917 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
918 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
919 {
920 clib_warning ("sw_if_index %u not configured with ip %U",
921 sep->sw_if_index, format_ip46_address, &sep->ip,
922 sep->is_ip4);
923 return 0;
924 }
925 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
926}
927
928static void
929session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
930 application_t * app, u8 is_connect)
931{
932 app_namespace_t *app_ns;
933 u32 ns_index, fib_index;
934
935 ns_index = app->ns_index;
936
937 /* App is a transport proto, so fetch the calling app's ns */
938 if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
Florin Coras8a140612019-02-18 22:39:39 -0800939 ns_index = sep->ns_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800940
Florin Corasc1a42652019-02-08 18:27:29 -0800941 app_ns = app_namespace_get (ns_index);
942 if (!app_ns)
943 return;
944
945 /* Ask transport and network to bind to/connect using local interface
946 * that "supports" app's namespace. This will fix our local connection
947 * endpoint.
948 */
949
950 /* If in default namespace and user requested a fib index use it */
951 if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
952 fib_index = sep->fib_index;
953 else
954 fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
955 sep->peer.fib_index = fib_index;
956 sep->fib_index = fib_index;
957
958 if (!is_connect)
959 {
960 sep->sw_if_index = app_ns->sw_if_index;
961 }
962 else
963 {
964 if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
965 && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
966 && sep->peer.sw_if_index != app_ns->sw_if_index)
967 clib_warning ("Local sw_if_index different from app ns sw_if_index");
968
969 sep->peer.sw_if_index = app_ns->sw_if_index;
970 }
971}
972
973int
974vnet_listen (vnet_listen_args_t * a)
975{
976 app_listener_t *app_listener;
977 app_worker_t *app_wrk;
978 application_t *app;
979 int rv;
980
981 app = application_get_if_valid (a->app_index);
982 if (!app)
983 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
984
985 app_wrk = application_get_worker (app, a->wrk_map_index);
986 if (!app_wrk)
987 return VNET_API_ERROR_INVALID_VALUE;
988
989 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
990
991 session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
992 if (!session_endpoint_in_ns (&a->sep))
993 return VNET_API_ERROR_INVALID_VALUE_2;
994
995 /*
996 * Check if we already have an app listener
997 */
998 app_listener = app_listener_lookup (app, &a->sep_ext);
999 if (app_listener)
1000 {
1001 if (app_listener->app_index != app->app_index)
1002 return VNET_API_ERROR_ADDRESS_IN_USE;
1003 if (app_worker_start_listen (app_wrk, app_listener))
1004 return -1;
1005 a->handle = app_listener_handle (app_listener);
1006 return 0;
1007 }
1008
1009 /*
1010 * Create new app listener
1011 */
1012 if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener)))
1013 return rv;
1014
1015 if ((rv = app_worker_start_listen (app_wrk, app_listener)))
1016 {
1017 app_listener_cleanup (app_listener);
1018 return rv;
1019 }
1020
1021 a->handle = app_listener_handle (app_listener);
1022 return 0;
1023}
1024
1025int
1026vnet_connect (vnet_connect_args_t * a)
1027{
1028 app_worker_t *server_wrk, *client_wrk;
1029 application_t *client;
Florin Corasc1a42652019-02-08 18:27:29 -08001030 app_listener_t *al;
1031 u32 table_index;
1032 session_t *ls;
1033 u8 fib_proto;
Florin Corasd4295e62019-02-22 13:11:38 -08001034 session_handle_t lh;
Florin Corasc1a42652019-02-08 18:27:29 -08001035
1036 if (session_endpoint_is_zero (&a->sep))
1037 return VNET_API_ERROR_INVALID_VALUE;
1038
1039 client = application_get (a->app_index);
1040 session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
1041 client_wrk = application_get_worker (client, a->wrk_map_index);
1042
1043 /*
1044 * First check the local scope for locally attached destinations.
1045 * If we have local scope, we pass *all* connects through it since we may
1046 * have special policy rules even for non-local destinations, think proxy.
1047 */
1048 if (application_has_local_scope (client))
1049 {
1050 table_index = application_local_session_table (client);
1051 lh = session_lookup_local_endpoint (table_index, &a->sep);
1052 if (lh == SESSION_DROP_HANDLE)
1053 return VNET_API_ERROR_APP_CONNECT_FILTERED;
1054
1055 if (lh == SESSION_INVALID_HANDLE)
1056 goto global_scope;
1057
Florin Corasd4295e62019-02-22 13:11:38 -08001058 ls = listen_session_get_from_handle (lh);
1059 al = app_listener_get_w_session (ls);
Florin Corasc1a42652019-02-08 18:27:29 -08001060
1061 /*
1062 * Break loop if rule in local table points to connecting app. This
1063 * can happen if client is a generic proxy. Route connect through
1064 * global table instead.
1065 */
1066 if (al->app_index == a->app_index)
1067 goto global_scope;
1068
1069 server_wrk = app_listener_select_worker (al);
Florin Corasd4295e62019-02-22 13:11:38 -08001070 return app_worker_local_session_connect (client_wrk, server_wrk, ls,
Florin Corasc1a42652019-02-08 18:27:29 -08001071 a->api_context);
1072 }
1073
1074 /*
1075 * If nothing found, check the global scope for locally attached
1076 * destinations. Make sure first that we're allowed to.
1077 */
1078
1079global_scope:
1080 if (session_endpoint_is_local (&a->sep))
1081 return VNET_API_ERROR_SESSION_CONNECT;
1082
1083 if (!application_has_global_scope (client))
1084 return VNET_API_ERROR_APP_CONNECT_SCOPE;
1085
1086 fib_proto = session_endpoint_fib_proto (&a->sep);
1087 table_index = application_session_table (client, fib_proto);
1088 ls = session_lookup_listener (table_index, &a->sep);
1089 if (ls)
1090 {
1091 al = app_listener_get_w_session (ls);
1092 server_wrk = app_listener_select_worker (al);
Florin Corasd4295e62019-02-22 13:11:38 -08001093 return app_worker_local_session_connect (client_wrk, server_wrk, ls,
Florin Corasc1a42652019-02-08 18:27:29 -08001094 a->api_context);
1095 }
1096
1097 /*
1098 * Not connecting to a local server, propagate to transport
1099 */
1100 if (app_worker_connect_session (client_wrk, &a->sep, a->api_context))
1101 return VNET_API_ERROR_SESSION_CONNECT;
1102 return 0;
1103}
1104
1105int
1106vnet_unlisten (vnet_unlisten_args_t * a)
1107{
1108 app_worker_t *app_wrk;
1109 app_listener_t *al;
1110 application_t *app;
1111
1112 if (!(app = application_get_if_valid (a->app_index)))
1113 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1114
1115 al = app_listener_get_w_handle (a->handle);
1116 if (al->app_index != app->app_index)
1117 {
1118 clib_warning ("app doesn't own handle %llu!", a->handle);
1119 return -1;
1120 }
1121
1122 app_wrk = application_get_worker (app, a->wrk_map_index);
1123 if (!app_wrk)
1124 {
1125 clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index);
1126 return -1;
1127 }
1128
1129 return app_worker_stop_listen (app_wrk, al);
1130}
1131
1132int
1133vnet_disconnect_session (vnet_disconnect_args_t * a)
1134{
1135 if (session_handle_is_local (a->handle))
1136 {
Florin Coras51a423d2019-02-19 20:57:06 -08001137 app_worker_t *client_wrk, *server_wrk;
Florin Corasc1a42652019-02-08 18:27:29 -08001138 local_session_t *ls;
Florin Coras51a423d2019-02-19 20:57:06 -08001139 u32 wrk_index = ~0;
Florin Corasc1a42652019-02-08 18:27:29 -08001140
1141 /* Disconnect reply came to worker 1 not main thread */
1142 app_interface_check_thread_and_barrier (vnet_disconnect_session, a);
1143
1144 if (!(ls = app_worker_get_local_session_from_handle (a->handle)))
1145 return 0;
1146
Florin Coras51a423d2019-02-19 20:57:06 -08001147 client_wrk = app_worker_get_if_valid (ls->client_wrk_index);
1148 server_wrk = app_worker_get (ls->app_wrk_index);
1149
1150 if (server_wrk->app_index == a->app_index)
1151 wrk_index = server_wrk->wrk_index;
1152 else if (client_wrk && client_wrk->app_index == a->app_index)
1153 wrk_index = client_wrk->wrk_index;
1154
1155 if (wrk_index == ~0)
1156 {
1157 clib_warning ("app %u does not own session 0x%lx", a->app_index,
1158 application_local_session_handle (ls));
1159 return VNET_API_ERROR_INVALID_VALUE;
1160 }
1161
1162 return app_worker_local_session_disconnect (wrk_index, ls);
Florin Corasc1a42652019-02-08 18:27:29 -08001163 }
1164 else
1165 {
1166 app_worker_t *app_wrk;
1167 session_t *s;
1168
1169 s = session_get_from_handle_if_valid (a->handle);
1170 if (!s)
1171 return VNET_API_ERROR_INVALID_VALUE;
1172 app_wrk = app_worker_get (s->app_wrk_index);
1173 if (app_wrk->app_index != a->app_index)
1174 return VNET_API_ERROR_INVALID_VALUE;
1175
1176 /* We're peeking into another's thread pool. Make sure */
1177 ASSERT (s->session_index == session_index_from_handle (a->handle));
1178
1179 session_close (s);
1180 }
1181 return 0;
1182}
1183
1184int
Florin Coras623eb562019-02-03 19:28:34 -08001185application_change_listener_owner (session_t * s, app_worker_t * app_wrk)
1186{
1187 app_worker_t *old_wrk = app_worker_get (s->app_wrk_index);
1188 app_listener_t *app_listener;
1189 application_t *app;
1190
1191 if (!old_wrk)
1192 return -1;
1193
1194 hash_unset (old_wrk->listeners_table, listen_session_get_handle (s));
1195 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL
1196 && s->rx_fifo)
1197 segment_manager_dealloc_fifos (s->rx_fifo->segment_index, s->rx_fifo,
1198 s->tx_fifo);
1199
Florin Coras623eb562019-02-03 19:28:34 -08001200 app = application_get (old_wrk->app_index);
1201 if (!app)
1202 return -1;
1203
Florin Corasc9940fc2019-02-05 20:55:11 -08001204 app_listener = app_listener_get (app, s->al_index);
1205
1206 /* Only remove from lb for now */
Florin Coras623eb562019-02-03 19:28:34 -08001207 app_listener->workers = clib_bitmap_set (app_listener->workers,
1208 old_wrk->wrk_map_index, 0);
Florin Coras623eb562019-02-03 19:28:34 -08001209
Florin Corasc9940fc2019-02-05 20:55:11 -08001210 if (app_worker_start_listen (app_wrk, app_listener))
1211 return -1;
Florin Corasab2f6db2018-08-31 14:31:41 -07001212
Florin Corasc9940fc2019-02-05 20:55:11 -08001213 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001214
Dave Barach68b0fb02017-02-28 15:15:56 -05001215 return 0;
1216}
1217
Dave Barach52851e62017-08-07 09:35:25 -04001218int
1219application_is_proxy (application_t * app)
1220{
Florin Coras7999e832017-10-31 01:51:04 -07001221 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
1222}
1223
1224int
1225application_is_builtin (application_t * app)
1226{
1227 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
1228}
1229
1230int
1231application_is_builtin_proxy (application_t * app)
1232{
1233 return (application_is_proxy (app) && application_is_builtin (app));
Dave Barach52851e62017-08-07 09:35:25 -04001234}
1235
Florin Corascea194d2017-10-02 00:18:51 -07001236u8
1237application_has_local_scope (application_t * app)
1238{
1239 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1240}
1241
1242u8
1243application_has_global_scope (application_t * app)
1244{
1245 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1246}
1247
Florin Coras60116992018-08-27 09:52:18 -07001248u8
1249application_use_mq_for_ctrl (application_t * app)
1250{
1251 return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
1252}
1253
Florin Coras7999e832017-10-31 01:51:04 -07001254static clib_error_t *
1255application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
1256 u8 transport_proto, u8 is_start)
1257{
Florin Coras7999e832017-10-31 01:51:04 -07001258 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
1259 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
Florin Coras5665ced2018-10-25 18:03:45 -07001260 session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
Florin Coras7999e832017-10-31 01:51:04 -07001261 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -07001262 app_worker_t *app_wrk;
Florin Corasc9940fc2019-02-05 20:55:11 -08001263 app_listener_t *al;
Florin Coras288eaab2019-02-03 15:26:14 -08001264 session_t *s;
Florin Corasc9940fc2019-02-05 20:55:11 -08001265 u32 flags;
Florin Coras7999e832017-10-31 01:51:04 -07001266
Florin Coras15531972018-08-12 23:50:53 -07001267 /* TODO decide if we want proxy to be enabled for all workers */
1268 app_wrk = application_get_default_worker (app);
Florin Coras7999e832017-10-31 01:51:04 -07001269 if (is_start)
1270 {
Florin Coras15531972018-08-12 23:50:53 -07001271 s = app_worker_first_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001272 if (!s)
1273 {
1274 sep.is_ip4 = is_ip4;
1275 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1276 sep.sw_if_index = app_ns->sw_if_index;
1277 sep.transport_proto = transport_proto;
Florin Corasab2f6db2018-08-31 14:31:41 -07001278 sep.app_wrk_index = app_wrk->wrk_index; /* only default */
Florin Corasc9940fc2019-02-05 20:55:11 -08001279
1280 /* force global scope listener */
1281 flags = app->flags;
1282 app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1283 app_listener_alloc_and_init (app, &sep, &al);
1284 app->flags = flags;
1285
1286 app_worker_start_listen (app_wrk, al);
1287 s = listen_session_get (al->session_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001288 s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX;
Florin Coras19b1f6a2017-12-11 03:37:03 -08001289 }
Florin Coras7999e832017-10-31 01:51:04 -07001290 }
1291 else
1292 {
Florin Coras623eb562019-02-03 19:28:34 -08001293 s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001294 ASSERT (s);
Florin Coras7999e832017-10-31 01:51:04 -07001295 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001296
Florin Coras7999e832017-10-31 01:51:04 -07001297 tc = listen_session_get_transport (s);
1298
1299 if (!ip_is_zero (&tc->lcl_ip, 1))
1300 {
Florin Corasdbd44562017-11-09 19:30:17 -08001301 u32 sti;
1302 sep.is_ip4 = is_ip4;
1303 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1304 sep.transport_proto = transport_proto;
1305 sep.port = 0;
1306 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001307 if (is_start)
Florin Corasab2f6db2018-08-31 14:31:41 -07001308 session_lookup_add_session_endpoint (sti,
1309 (session_endpoint_t *) & sep,
1310 s->session_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001311 else
Florin Corasab2f6db2018-08-31 14:31:41 -07001312 session_lookup_del_session_endpoint (sti,
1313 (session_endpoint_t *) & sep);
Florin Coras7999e832017-10-31 01:51:04 -07001314 }
Florin Coras19b1f6a2017-12-11 03:37:03 -08001315
Florin Coras7999e832017-10-31 01:51:04 -07001316 return 0;
1317}
1318
Florin Coras19b1f6a2017-12-11 03:37:03 -08001319static void
1320application_start_stop_proxy_local_scope (application_t * app,
1321 u8 transport_proto, u8 is_start)
1322{
1323 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1324 app_namespace_t *app_ns;
1325 app_ns = app_namespace_get (app->ns_index);
1326 sep.is_ip4 = 1;
1327 sep.transport_proto = transport_proto;
1328 sep.port = 0;
1329
1330 if (is_start)
1331 {
1332 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001333 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001334 sep.is_ip4 = 0;
1335 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
Florin Coras15531972018-08-12 23:50:53 -07001336 app->app_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -08001337 }
1338 else
1339 {
1340 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1341 sep.is_ip4 = 0;
1342 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
1343 }
1344}
1345
Florin Coras7999e832017-10-31 01:51:04 -07001346void
Florin Coras561af9b2017-12-09 10:19:43 -08001347application_start_stop_proxy (application_t * app,
1348 transport_proto_t transport_proto, u8 is_start)
Florin Coras7999e832017-10-31 01:51:04 -07001349{
Florin Coras7999e832017-10-31 01:51:04 -07001350 if (application_has_local_scope (app))
Florin Coras19b1f6a2017-12-11 03:37:03 -08001351 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
Florin Coras7999e832017-10-31 01:51:04 -07001352
1353 if (application_has_global_scope (app))
1354 {
1355 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
1356 transport_proto, is_start);
1357 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
1358 transport_proto, is_start);
1359 }
1360}
1361
1362void
1363application_setup_proxy (application_t * app)
1364{
1365 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001366 transport_proto_t tp;
1367
Florin Coras7999e832017-10-31 01:51:04 -07001368 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001369
1370 /* *INDENT-OFF* */
1371 transport_proto_foreach (tp, ({
1372 if (transports & (1 << tp))
1373 application_start_stop_proxy (app, tp, 1);
1374 }));
1375 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -07001376}
1377
1378void
1379application_remove_proxy (application_t * app)
1380{
1381 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -08001382 transport_proto_t tp;
1383
Florin Coras7999e832017-10-31 01:51:04 -07001384 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -08001385
1386 /* *INDENT-OFF* */
1387 transport_proto_foreach (tp, ({
1388 if (transports & (1 << tp))
1389 application_start_stop_proxy (app, tp, 0);
1390 }));
1391 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -07001392}
1393
Florin Corasa332c462018-01-31 06:52:17 -08001394segment_manager_properties_t *
1395application_segment_manager_properties (application_t * app)
1396{
1397 return &app->sm_properties;
1398}
1399
1400segment_manager_properties_t *
1401application_get_segment_manager_properties (u32 app_index)
1402{
1403 application_t *app = application_get (app_index);
1404 return &app->sm_properties;
1405}
1406
Florin Coras371ca502018-02-21 12:07:41 -08001407clib_error_t *
1408vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1409{
1410 application_t *app;
1411 app = application_get (a->app_index);
1412 if (!app)
1413 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1414 0, "app %u doesn't exist", a->app_index);
1415 app->tls_cert = vec_dup (a->cert);
1416 return 0;
1417}
1418
1419clib_error_t *
1420vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1421{
1422 application_t *app;
1423 app = application_get (a->app_index);
1424 if (!app)
1425 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1426 0, "app %u doesn't exist", a->app_index);
1427 app->tls_key = vec_dup (a->key);
1428 return 0;
1429}
1430
Florin Coras15531972018-08-12 23:50:53 -07001431static void
1432application_format_listeners (application_t * app, int verbose)
1433{
1434 vlib_main_t *vm = vlib_get_main ();
1435 app_worker_map_t *wrk_map;
1436 app_worker_t *app_wrk;
1437 u32 sm_index;
1438 u64 handle;
1439
1440 if (!app)
1441 {
1442 vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ ,
1443 0, 0, verbose);
1444 return;
1445 }
1446
1447 /* *INDENT-OFF* */
1448 pool_foreach (wrk_map, app->worker_maps, ({
1449 app_wrk = app_worker_get (wrk_map->wrk_index);
1450 if (hash_elts (app_wrk->listeners_table) == 0)
1451 continue;
1452 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
1453 vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk,
1454 handle, sm_index, verbose);
1455 }));
1456 }));
1457 /* *INDENT-ON* */
1458}
1459
1460static void
Florin Coras15531972018-08-12 23:50:53 -07001461application_format_connects (application_t * app, int verbose)
1462{
1463 app_worker_map_t *wrk_map;
1464 app_worker_t *app_wrk;
1465
1466 if (!app)
1467 {
1468 app_worker_format_connects (0, verbose);
1469 return;
1470 }
1471
1472 /* *INDENT-OFF* */
1473 pool_foreach (wrk_map, app->worker_maps, ({
1474 app_wrk = app_worker_get (wrk_map->wrk_index);
1475 app_worker_format_connects (app_wrk, verbose);
1476 }));
1477 /* *INDENT-ON* */
1478}
1479
1480static void
Florin Coras15531972018-08-12 23:50:53 -07001481application_format_local_sessions (application_t * app, int verbose)
1482{
1483 app_worker_map_t *wrk_map;
1484 app_worker_t *app_wrk;
1485
1486 if (!app)
1487 {
1488 app_worker_format_local_sessions (0, verbose);
1489 return;
1490 }
1491
Florin Corasab2f6db2018-08-31 14:31:41 -07001492 /*
Florin Corasab2f6db2018-08-31 14:31:41 -07001493 * Format local accepted/connected sessions
1494 */
Florin Coras15531972018-08-12 23:50:53 -07001495 /* *INDENT-OFF* */
1496 pool_foreach (wrk_map, app->worker_maps, ({
1497 app_wrk = app_worker_get (wrk_map->wrk_index);
1498 app_worker_format_local_sessions (app_wrk, verbose);
1499 }));
1500 /* *INDENT-ON* */
1501}
1502
1503static void
Florin Coras15531972018-08-12 23:50:53 -07001504application_format_local_connects (application_t * app, int verbose)
1505{
1506 app_worker_map_t *wrk_map;
1507 app_worker_t *app_wrk;
1508
1509 if (!app)
1510 {
1511 app_worker_format_local_connects (0, verbose);
1512 return;
1513 }
1514
1515 /* *INDENT-OFF* */
1516 pool_foreach (wrk_map, app->worker_maps, ({
1517 app_wrk = app_worker_get (wrk_map->wrk_index);
1518 app_worker_format_local_connects (app_wrk, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001519 }));
1520 /* *INDENT-ON* */
1521}
1522
Florin Coras6cf30ad2017-04-04 23:08:23 -07001523u8 *
1524format_application (u8 * s, va_list * args)
1525{
1526 application_t *app = va_arg (*args, application_t *);
1527 CLIB_UNUSED (int verbose) = va_arg (*args, int);
Florin Corasad0c77f2017-11-09 18:00:15 -08001528 segment_manager_properties_t *props;
Florin Coras053a0e42018-11-13 15:52:38 -08001529 const u8 *app_ns_name, *app_name;
Florin Coras349f8ca2018-11-20 16:52:49 -08001530 app_worker_map_t *wrk_map;
1531 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001532
1533 if (app == 0)
1534 {
Florin Coras349f8ca2018-11-20 16:52:49 -08001535 if (!verbose)
Florin Corasc1f5a432018-11-20 11:31:26 -08001536 s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace");
Florin Coras6cf30ad2017-04-04 23:08:23 -07001537 return s;
1538 }
1539
Florin Coras0bee9ce2018-03-22 21:24:31 -07001540 app_name = app_get_name (app);
Florin Corascea194d2017-10-02 00:18:51 -07001541 app_ns_name = app_namespace_id_from_index (app->ns_index);
Florin Corasa332c462018-01-31 06:52:17 -08001542 props = application_segment_manager_properties (app);
Florin Coras349f8ca2018-11-20 16:52:49 -08001543 if (!verbose)
1544 {
1545 s = format (s, "%-10u%-20s%-40s", app->app_index, app_name,
1546 app_ns_name);
1547 return s;
1548 }
1549
1550 s = format (s, "app-name %s app-index %u ns-index %u seg-size %U\n",
1551 app_name, app->app_index, app->ns_index,
1552 format_memory_size, props->add_segment_size);
1553 s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n",
1554 format_memory_size, props->rx_fifo_size,
1555 format_memory_size, props->tx_fifo_size);
1556
1557 /* *INDENT-OFF* */
1558 pool_foreach (wrk_map, app->worker_maps, ({
1559 app_wrk = app_worker_get (wrk_map->wrk_index);
Florin Coras623eb562019-02-03 19:28:34 -08001560 s = format (s, "%U", format_app_worker, app_wrk);
Florin Coras349f8ca2018-11-20 16:52:49 -08001561 }));
1562 /* *INDENT-ON* */
1563
Dave Barach68b0fb02017-02-28 15:15:56 -05001564 return s;
1565}
1566
Florin Corasf8f516a2018-02-08 15:10:09 -08001567void
1568application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1569{
1570 application_t *app;
Florin Corasf8f516a2018-02-08 15:10:09 -08001571
Florin Coras15531972018-08-12 23:50:53 -07001572 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08001573 {
1574 vlib_cli_output (vm, "No active server bindings");
1575 return;
1576 }
1577
1578 if (do_local)
1579 {
1580 application_format_local_sessions (0, verbose);
1581 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001582 pool_foreach (app, app_main.app_pool, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08001583 application_format_local_sessions (app, verbose);
1584 }));
1585 /* *INDENT-ON* */
1586 }
1587 else
1588 {
Florin Coras15531972018-08-12 23:50:53 -07001589 application_format_listeners (0, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001590
1591 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001592 pool_foreach (app, app_main.app_pool, ({
1593 application_format_listeners (app, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001594 }));
1595 /* *INDENT-ON* */
1596 }
1597}
1598
1599void
1600application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1601{
1602 application_t *app;
1603
Florin Coras15531972018-08-12 23:50:53 -07001604 if (!pool_elts (app_main.app_pool))
Florin Corasf8f516a2018-02-08 15:10:09 -08001605 {
1606 vlib_cli_output (vm, "No active apps");
1607 return;
1608 }
1609
1610 if (do_local)
1611 {
1612 application_format_local_connects (0, verbose);
1613
1614 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001615 pool_foreach (app, app_main.app_pool, ({
1616 application_format_local_connects (app, verbose);
Florin Corasf8f516a2018-02-08 15:10:09 -08001617 }));
1618 /* *INDENT-ON* */
1619 }
1620 else
1621 {
1622 application_format_connects (0, verbose);
1623
1624 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001625 pool_foreach (app, app_main.app_pool, ({
Florin Corasf8f516a2018-02-08 15:10:09 -08001626 application_format_connects (app, verbose);
1627 }));
1628 /* *INDENT-ON* */
1629 }
1630}
1631
Dave Barach68b0fb02017-02-28 15:15:56 -05001632static clib_error_t *
1633show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1634 vlib_cli_command_t * cmd)
1635{
Florin Corasf8f516a2018-02-08 15:10:09 -08001636 int do_server = 0, do_client = 0, do_local = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001637 application_t *app;
Florin Coras349f8ca2018-11-20 16:52:49 -08001638 u32 app_index = ~0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001639 int verbose = 0;
1640
Florin Corascea194d2017-10-02 00:18:51 -07001641 session_cli_return_if_not_enabled ();
Florin Corase04c2992017-03-01 08:17:34 -08001642
Dave Barach68b0fb02017-02-28 15:15:56 -05001643 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1644 {
1645 if (unformat (input, "server"))
1646 do_server = 1;
1647 else if (unformat (input, "client"))
1648 do_client = 1;
Florin Corasf8f516a2018-02-08 15:10:09 -08001649 else if (unformat (input, "local"))
1650 do_local = 1;
Florin Coras349f8ca2018-11-20 16:52:49 -08001651 else if (unformat (input, "%u", &app_index))
1652 ;
Dave Barach68b0fb02017-02-28 15:15:56 -05001653 else if (unformat (input, "verbose"))
1654 verbose = 1;
1655 else
Florin Coras349f8ca2018-11-20 16:52:49 -08001656 return clib_error_return (0, "unknown input `%U'",
1657 format_unformat_error, input);
Dave Barach68b0fb02017-02-28 15:15:56 -05001658 }
1659
1660 if (do_server)
Florin Coras349f8ca2018-11-20 16:52:49 -08001661 {
1662 application_format_all_listeners (vm, do_local, verbose);
1663 return 0;
1664 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001665
1666 if (do_client)
Florin Coras349f8ca2018-11-20 16:52:49 -08001667 {
1668 application_format_all_clients (vm, do_local, verbose);
1669 return 0;
1670 }
1671
1672 if (app_index != ~0)
1673 {
Florin Coras47c40e22018-11-26 17:01:36 -08001674 app = application_get_if_valid (app_index);
Florin Coras349f8ca2018-11-20 16:52:49 -08001675 if (!app)
1676 return clib_error_return (0, "No app with index %u", app_index);
1677
1678 vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1);
1679 return 0;
1680 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001681
Florin Coras6cf30ad2017-04-04 23:08:23 -07001682 /* Print app related info */
1683 if (!do_server && !do_client)
1684 {
Florin Coras349f8ca2018-11-20 16:52:49 -08001685 vlib_cli_output (vm, "%U", format_application, 0, 0);
Florin Corascea194d2017-10-02 00:18:51 -07001686 /* *INDENT-OFF* */
Florin Coras15531972018-08-12 23:50:53 -07001687 pool_foreach (app, app_main.app_pool, ({
Florin Coras349f8ca2018-11-20 16:52:49 -08001688 vlib_cli_output (vm, "%U", format_application, app, 0);
Florin Corascea194d2017-10-02 00:18:51 -07001689 }));
1690 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001691 }
1692
Dave Barach68b0fb02017-02-28 15:15:56 -05001693 return 0;
1694}
1695
Florin Corase04c2992017-03-01 08:17:34 -08001696/* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001697VLIB_CLI_COMMAND (show_app_command, static) =
1698{
Florin Corase04c2992017-03-01 08:17:34 -08001699 .path = "show app",
1700 .short_help = "show app [server|client] [verbose]",
1701 .function = show_app_command_fn,
1702};
1703/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001704
1705/*
1706 * fd.io coding-style-patch-verification: ON
1707 *
1708 * Local Variables:
1709 * eval: (c-set-style "gnu")
1710 * End:
1711 */