blob: 757e12e1b58b0867306ca35332576d8a95d4f653 [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 Coras6cf30ad2017-04-04 23:08:23 -070021/**
Dave Barach68b0fb02017-02-28 15:15:56 -050022 * Pool from which we allocate all applications
23 */
24static application_t *app_pool;
25
Florin Coras6cf30ad2017-04-04 23:08:23 -070026/**
Dave Barach68b0fb02017-02-28 15:15:56 -050027 * Hash table of apps by api client index
28 */
29static uword *app_by_api_client_index;
30
Florin Coras0bee9ce2018-03-22 21:24:31 -070031/**
32 * Hash table of builtin apps by name
33 */
34static uword *app_by_name;
35
Florin Corascea194d2017-10-02 00:18:51 -070036static u8 *
37app_get_name_from_reg_index (application_t * app)
38{
39 u8 *app_name;
40
41 vl_api_registration_t *regp;
42 regp = vl_api_client_index_to_registration (app->api_client_index);
43 if (!regp)
44 app_name = format (0, "builtin-%d%c", app->index, 0);
45 else
46 app_name = format (0, "%s%c", regp->name, 0);
47
48 return app_name;
49}
50
Florin Coras0bee9ce2018-03-22 21:24:31 -070051static u8 *
52app_get_name (application_t * app)
53{
54 if (!app->name)
55 return app_get_name_from_reg_index (app);
56 return app->name;
57}
58
Florin Corascea194d2017-10-02 00:18:51 -070059u32
60application_session_table (application_t * app, u8 fib_proto)
61{
62 app_namespace_t *app_ns;
63 app_ns = app_namespace_get (app->ns_index);
64 if (!application_has_global_scope (app))
65 return APP_INVALID_INDEX;
66 if (fib_proto == FIB_PROTOCOL_IP4)
67 return session_lookup_get_index_for_fib (fib_proto,
68 app_ns->ip4_fib_index);
69 else
70 return session_lookup_get_index_for_fib (fib_proto,
71 app_ns->ip6_fib_index);
72}
73
74u32
75application_local_session_table (application_t * app)
76{
77 app_namespace_t *app_ns;
78 if (!application_has_local_scope (app))
79 return APP_INVALID_INDEX;
80 app_ns = app_namespace_get (app->ns_index);
81 return app_ns->local_table_index;
82}
83
Dave Barach68b0fb02017-02-28 15:15:56 -050084int
85application_api_queue_is_full (application_t * app)
86{
Florin Corase86a8ed2018-01-05 03:20:25 -080087 svm_queue_t *q;
Dave Barach68b0fb02017-02-28 15:15:56 -050088
89 /* builtin servers are always OK */
90 if (app->api_client_index == ~0)
91 return 0;
92
93 q = vl_api_client_index_to_input_queue (app->api_client_index);
94 if (!q)
95 return 1;
96
97 if (q->cursize == q->maxsize)
98 return 1;
99 return 0;
100}
101
Florin Corascea194d2017-10-02 00:18:51 -0700102/**
103 * Returns app name
104 *
105 * Since the name is not stored per app, we generate it on the fly. It is
106 * the caller's responsibility to free the vector
107 */
108u8 *
109application_name_from_index (u32 app_index)
110{
111 application_t *app = application_get (app_index);
112 if (!app)
113 return 0;
114 return app_get_name_from_reg_index (app);
115}
116
Dave Barach68b0fb02017-02-28 15:15:56 -0500117static void
118application_table_add (application_t * app)
119{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700120 if (app->api_client_index != APP_INVALID_INDEX)
121 hash_set (app_by_api_client_index, app->api_client_index, app->index);
122 else if (app->name)
123 hash_set_mem (app_by_name, app->name, app->index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500124}
125
126static void
127application_table_del (application_t * app)
128{
Florin Coras0bee9ce2018-03-22 21:24:31 -0700129 if (app->api_client_index != APP_INVALID_INDEX)
130 hash_unset (app_by_api_client_index, app->api_client_index);
131 else if (app->name)
132 hash_unset_mem (app_by_name, app->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500133}
134
135application_t *
136application_lookup (u32 api_client_index)
137{
138 uword *p;
139 p = hash_get (app_by_api_client_index, api_client_index);
140 if (p)
141 return application_get (p[0]);
142
143 return 0;
144}
145
Florin Coras6cf30ad2017-04-04 23:08:23 -0700146application_t *
Florin Coras0bee9ce2018-03-22 21:24:31 -0700147application_lookup_name (const u8 * name)
148{
149 uword *p;
150 p = hash_get_mem (app_by_name, name);
151 if (p)
152 return application_get (p[0]);
153
154 return 0;
155}
156
157application_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -0700158application_new ()
159{
160 application_t *app;
161 pool_get (app_pool, app);
162 memset (app, 0, sizeof (*app));
163 app->index = application_get_index (app);
Florin Corasc87c91d2017-08-16 19:55:49 -0700164 app->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
165 app->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
Florin Corasf8f516a2018-02-08 15:10:09 -0800166 app->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400167 if (CLIB_DEBUG > 1)
168 clib_warning ("[%d] New app (%d)", getpid (), app->index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700169 return app;
170}
171
Dave Barach68b0fb02017-02-28 15:15:56 -0500172void
173application_del (application_t * app)
174{
Florin Corasad0c77f2017-11-09 18:00:15 -0800175 vnet_unbind_args_t _a, *a = &_a;
Florin Coras7999e832017-10-31 01:51:04 -0700176 u64 handle, *handles = 0;
Florin Corasf8f516a2018-02-08 15:10:09 -0800177 segment_manager_t *sm;
Florin Coras7999e832017-10-31 01:51:04 -0700178 u32 index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700179 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500180
Florin Coras6cf30ad2017-04-04 23:08:23 -0700181 /*
Florin Corasa5464812017-04-19 13:00:05 -0700182 * The app event queue allocated in first segment is cleared with
183 * the segment manager. No need to explicitly free it.
184 */
Dave Wallace7b749fe2017-07-05 14:30:46 -0400185 if (CLIB_DEBUG > 1)
186 clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
Florin Corasa5464812017-04-19 13:00:05 -0700187
Florin Coras7999e832017-10-31 01:51:04 -0700188 if (application_is_proxy (app))
189 application_remove_proxy (app);
190
Florin Corasa5464812017-04-19 13:00:05 -0700191 /*
Florin Corasc87c91d2017-08-16 19:55:49 -0700192 * Listener cleanup
Florin Coras6cf30ad2017-04-04 23:08:23 -0700193 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500194
Florin Coras6cf30ad2017-04-04 23:08:23 -0700195 /* *INDENT-OFF* */
196 hash_foreach (handle, index, app->listeners_table,
197 ({
198 vec_add1 (handles, handle);
Florin Coras9d063042017-09-14 03:08:00 -0400199 sm = segment_manager_get (index);
200 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700201 }));
202 /* *INDENT-ON* */
203
Florin Coras6cf30ad2017-04-04 23:08:23 -0700204 for (i = 0; i < vec_len (handles); i++)
205 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700206 a->app_index = app->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700207 a->handle = handles[i];
208 /* seg manager is removed when unbind completes */
209 vnet_unbind (a);
210 }
211
Florin Corasc87c91d2017-08-16 19:55:49 -0700212 /*
213 * Connects segment manager cleanup
214 */
215
216 if (app->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
217 {
218 sm = segment_manager_get (app->connects_seg_manager);
219 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
220 segment_manager_init_del (sm);
221 }
222
Florin Corasc87c91d2017-08-16 19:55:49 -0700223 /* If first segment manager is used by a listener */
224 if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
225 && app->first_segment_manager != app->connects_seg_manager)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400226 {
227 sm = segment_manager_get (app->first_segment_manager);
Florin Corasc87c91d2017-08-16 19:55:49 -0700228 /* .. and has no fifos, e.g. it might be used for redirected sessions,
229 * remove it */
230 if (!segment_manager_has_fifos (sm))
231 {
232 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
233 segment_manager_del (sm);
234 }
Dave Wallace7b749fe2017-07-05 14:30:46 -0400235 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800236
237 /*
238 * Local connections cleanup
239 */
240 application_local_sessions_del (app);
241
Florin Coras371ca502018-02-21 12:07:41 -0800242 vec_free (app->tls_cert);
243 vec_free (app->tls_key);
244
Dave Barach68b0fb02017-02-28 15:15:56 -0500245 application_table_del (app);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700246 vec_free (app->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500247 pool_put (app_pool, app);
248}
249
Florin Corasd79b41e2017-03-04 05:37:52 -0800250static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700251application_verify_cb_fns (session_cb_vft_t * cb_fns)
Florin Corasd79b41e2017-03-04 05:37:52 -0800252{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700253 if (cb_fns->session_accept_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800254 clib_warning ("No accept callback function provided");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700255 if (cb_fns->session_connected_callback == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800256 clib_warning ("No session connected callback function provided");
257 if (cb_fns->session_disconnect_callback == 0)
258 clib_warning ("No session disconnect callback function provided");
259 if (cb_fns->session_reset_callback == 0)
260 clib_warning ("No session reset callback function provided");
261}
262
Florin Corasb384b542018-01-15 01:08:33 -0800263/**
264 * Check app config for given segment type
265 *
266 * Returns 1 on success and 0 otherwise
267 */
268static u8
269application_verify_cfg (ssvm_segment_type_t st)
270{
271 u8 is_valid;
272 if (st == SSVM_SEGMENT_MEMFD)
273 {
274 is_valid = (session_manager_get_evt_q_segment () != 0);
275 if (!is_valid)
276 clib_warning ("memfd seg: vpp's event qs IN binary api svm region");
277 return is_valid;
278 }
279 else if (st == SSVM_SEGMENT_SHM)
280 {
281 is_valid = (session_manager_get_evt_q_segment () == 0);
282 if (!is_valid)
283 clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region");
284 return is_valid;
285 }
286 else
287 return 1;
288}
289
Florin Coras6cf30ad2017-04-04 23:08:23 -0700290int
Florin Coras0bee9ce2018-03-22 21:24:31 -0700291application_init (application_t * app, u32 api_client_index, u8 * app_name,
292 u64 * options, session_cb_vft_t * cb_fns)
Dave Barach68b0fb02017-02-28 15:15:56 -0500293{
Florin Corasa332c462018-01-31 06:52:17 -0800294 ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD;
Florin Corasf8f516a2018-02-08 15:10:09 -0800295 u32 first_seg_size, prealloc_fifo_pairs;
Florin Corasb384b542018-01-15 01:08:33 -0800296 segment_manager_properties_t *props;
297 vl_api_registration_t *reg;
298 segment_manager_t *sm;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500300
Florin Corasb384b542018-01-15 01:08:33 -0800301 /*
302 * Make sure we support the requested configuration
303 */
Florin Corasb384b542018-01-15 01:08:33 -0800304
Florin Corasa332c462018-01-31 06:52:17 -0800305 if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN))
306 {
307 reg = vl_api_client_index_to_registration (api_client_index);
308 if (!reg)
309 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
310 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
311 seg_type = SSVM_SEGMENT_SHM;
312 }
313 else
314 {
315 seg_type = SSVM_SEGMENT_PRIVATE;
316 }
Florin Corasb384b542018-01-15 01:08:33 -0800317
Florin Corasa332c462018-01-31 06:52:17 -0800318 if (!application_verify_cfg (seg_type))
Florin Corasb384b542018-01-15 01:08:33 -0800319 return VNET_API_ERROR_APP_UNSUPPORTED_CFG;
Dave Barach68b0fb02017-02-28 15:15:56 -0500320
Florin Corascea194d2017-10-02 00:18:51 -0700321 /*
322 * Setup segment manager
323 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700324 sm = segment_manager_new ();
325 sm->app_index = app->index;
Florin Corasa332c462018-01-31 06:52:17 -0800326 props = application_segment_manager_properties (app);
327 segment_manager_properties_init (props);
Florin Corasb384b542018-01-15 01:08:33 -0800328 if (options[APP_OPTIONS_ADD_SEGMENT_SIZE])
329 {
330 props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE];
331 props->add_segment = 1;
332 }
333 if (options[APP_OPTIONS_RX_FIFO_SIZE])
334 props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE];
335 if (options[APP_OPTIONS_TX_FIFO_SIZE])
336 props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE];
Florin Corasf8f516a2018-02-08 15:10:09 -0800337 if (options[APP_OPTIONS_EVT_QUEUE_SIZE])
338 props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE];
Florin Coras58d36f02018-03-09 13:05:53 -0800339 if (options[APP_OPTIONS_TLS_ENGINE])
340 app->tls_engine = options[APP_OPTIONS_TLS_ENGINE];
Florin Corasa332c462018-01-31 06:52:17 -0800341 props->segment_type = seg_type;
Dave Barach68b0fb02017-02-28 15:15:56 -0500342
Florin Corasff6e7692017-12-11 04:59:01 -0800343 first_seg_size = options[APP_OPTIONS_SEGMENT_SIZE];
Florin Corasa332c462018-01-31 06:52:17 -0800344 prealloc_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
345
Florin Corasf8f516a2018-02-08 15:10:09 -0800346 if ((rv = segment_manager_init (sm, first_seg_size, prealloc_fifo_pairs)))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700347 return rv;
Florin Corasc87c91d2017-08-16 19:55:49 -0700348 sm->first_is_protected = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500349
Florin Corascea194d2017-10-02 00:18:51 -0700350 /*
351 * Setup application
352 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700353 app->first_segment_manager = segment_manager_index (sm);
Dave Barach68b0fb02017-02-28 15:15:56 -0500354 app->api_client_index = api_client_index;
Florin Corasa5464812017-04-19 13:00:05 -0700355 app->flags = options[APP_OPTIONS_FLAGS];
Dave Barach68b0fb02017-02-28 15:15:56 -0500356 app->cb_fns = *cb_fns;
Florin Corascea194d2017-10-02 00:18:51 -0700357 app->ns_index = options[APP_OPTIONS_NAMESPACE];
Florin Coras7999e832017-10-31 01:51:04 -0700358 app->listeners_table = hash_create (0, sizeof (u64));
Florin Corasf8f516a2018-02-08 15:10:09 -0800359 app->local_connects = hash_create (0, sizeof (u64));
Florin Coras7999e832017-10-31 01:51:04 -0700360 app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT];
Florin Corasa332c462018-01-31 06:52:17 -0800361 app->event_queue = segment_manager_event_queue (sm);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700362 app->name = vec_dup (app_name);
Florin Corascea194d2017-10-02 00:18:51 -0700363
364 /* If no scope enabled, default to global */
365 if (!application_has_global_scope (app)
366 && !application_has_local_scope (app))
367 app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500368
Florin Corasd79b41e2017-03-04 05:37:52 -0800369 /* Check that the obvious things are properly set up */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700370 application_verify_cb_fns (cb_fns);
Florin Corasd79b41e2017-03-04 05:37:52 -0800371
Dave Barach68b0fb02017-02-28 15:15:56 -0500372 /* Add app to lookup by api_client_index table */
373 application_table_add (app);
374
Florin Corasf8f516a2018-02-08 15:10:09 -0800375 /*
376 * Segment manager for local sessions
377 */
378 sm = segment_manager_new ();
379 sm->app_index = app->index;
380 app->local_segment_manager = segment_manager_index (sm);
381
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500383}
384
385application_t *
386application_get (u32 index)
387{
Florin Corascea194d2017-10-02 00:18:51 -0700388 if (index == APP_INVALID_INDEX)
389 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500390 return pool_elt_at_index (app_pool, index);
391}
392
Florin Corase04c2992017-03-01 08:17:34 -0800393application_t *
394application_get_if_valid (u32 index)
395{
396 if (pool_is_free_index (app_pool, index))
397 return 0;
398
399 return pool_elt_at_index (app_pool, index);
400}
401
Dave Barach68b0fb02017-02-28 15:15:56 -0500402u32
403application_get_index (application_t * app)
404{
405 return app - app_pool;
406}
407
Florin Coras6cf30ad2017-04-04 23:08:23 -0700408static segment_manager_t *
409application_alloc_segment_manager (application_t * app)
Dave Barach68b0fb02017-02-28 15:15:56 -0500410{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700411 segment_manager_t *sm = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500412
Florin Corasc87c91d2017-08-16 19:55:49 -0700413 /* If the first segment manager is not in use, don't allocate a new one */
414 if (app->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
Florin Coras0e9c33b2017-08-14 22:33:41 -0700415 && app->first_segment_manager_in_use == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700417 sm = segment_manager_get (app->first_segment_manager);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700418 app->first_segment_manager_in_use = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700419 return sm;
Dave Barach68b0fb02017-02-28 15:15:56 -0500420 }
421
Florin Coras6cf30ad2017-04-04 23:08:23 -0700422 sm = segment_manager_new ();
Florin Corasa332c462018-01-31 06:52:17 -0800423 sm->app_index = app->index;
Florin Coras0e9c33b2017-08-14 22:33:41 -0700424
Florin Coras6cf30ad2017-04-04 23:08:23 -0700425 return sm;
426}
427
428/**
429 * Start listening local transport endpoint for requested transport.
430 *
431 * Creates a 'dummy' stream session with state LISTENING to be used in session
432 * lookups, prior to establishing connection. Requests transport to build
433 * it's own specific listening connection.
434 */
435int
Florin Corascea194d2017-10-02 00:18:51 -0700436application_start_listen (application_t * srv, session_endpoint_t * sep,
Florin Corasf8f516a2018-02-08 15:10:09 -0800437 session_handle_t * res)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700438{
439 segment_manager_t *sm;
440 stream_session_t *s;
Florin Corasf8f516a2018-02-08 15:10:09 -0800441 session_handle_t handle;
Florin Corascea194d2017-10-02 00:18:51 -0700442 session_type_t sst;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700443
Florin Corascea194d2017-10-02 00:18:51 -0700444 sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
Florin Coras5c9083d2018-04-13 06:39:07 -0700445 s = listen_session_new (0, sst);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700446 s->app_index = srv->index;
447
Florin Coras6cf30ad2017-04-04 23:08:23 -0700448 /* Allocate segment manager. All sessions derived out of a listen session
449 * have fifos allocated by the same segment manager. */
Florin Coras7fb0fe12018-04-09 09:24:52 -0700450 if (!(sm = application_alloc_segment_manager (srv)))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700451 goto err;
452
453 /* Add to app's listener table. Useful to find all child listeners
454 * when app goes down, although, just for unbinding this is not needed */
455 handle = listen_session_get_handle (s);
456 hash_set (srv->listeners_table, handle, segment_manager_index (sm));
457
Florin Coras7fb0fe12018-04-09 09:24:52 -0700458 if (stream_session_listen (s, sep))
459 {
460 segment_manager_del (sm);
461 hash_unset (srv->listeners_table, handle);
462 goto err;
463 }
464
Florin Coras6cf30ad2017-04-04 23:08:23 -0700465 *res = handle;
466 return 0;
467
468err:
469 listen_session_del (s);
470 return -1;
471}
472
473/**
474 * Stop listening on session associated to handle
475 */
476int
Florin Corasf8f516a2018-02-08 15:10:09 -0800477application_stop_listen (application_t * srv, session_handle_t handle)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700478{
479 stream_session_t *listener;
480 uword *indexp;
481 segment_manager_t *sm;
482
483 if (srv && hash_get (srv->listeners_table, handle) == 0)
484 {
485 clib_warning ("app doesn't own handle %llu!", handle);
486 return -1;
487 }
488
489 listener = listen_session_get_from_handle (handle);
490 stream_session_stop_listen (listener);
491
492 indexp = hash_get (srv->listeners_table, handle);
493 ASSERT (indexp);
494
495 sm = segment_manager_get (*indexp);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700496 if (srv->first_segment_manager == *indexp)
497 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700498 /* Delete sessions but don't remove segment manager */
Florin Coras0e9c33b2017-08-14 22:33:41 -0700499 srv->first_segment_manager_in_use = 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700500 segment_manager_del_sessions (sm);
501 }
502 else
503 {
504 segment_manager_init_del (sm);
Florin Coras0e9c33b2017-08-14 22:33:41 -0700505 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700506 hash_unset (srv->listeners_table, handle);
507 listen_session_del (listener);
508
Dave Barach68b0fb02017-02-28 15:15:56 -0500509 return 0;
510}
511
Florin Coras6cf30ad2017-04-04 23:08:23 -0700512int
Florin Corascea194d2017-10-02 00:18:51 -0700513application_open_session (application_t * app, session_endpoint_t * sep,
514 u32 api_context)
Dave Barach68b0fb02017-02-28 15:15:56 -0500515{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700516 int rv;
517
518 /* Make sure we have a segment manager for connects */
Florin Coras371ca502018-02-21 12:07:41 -0800519 application_alloc_connects_segment_manager (app);
520
521 if ((rv = session_open (app->index, sep, api_context)))
522 return rv;
523
524 return 0;
525}
526
527int
528application_alloc_connects_segment_manager (application_t * app)
529{
530 segment_manager_t *sm;
531
Florin Corascea194d2017-10-02 00:18:51 -0700532 if (app->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700533 {
534 sm = application_alloc_segment_manager (app);
535 if (sm == 0)
536 return -1;
537 app->connects_seg_manager = segment_manager_index (sm);
538 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700539 return 0;
540}
541
542segment_manager_t *
543application_get_connect_segment_manager (application_t * app)
544{
545 ASSERT (app->connects_seg_manager != (u32) ~ 0);
546 return segment_manager_get (app->connects_seg_manager);
547}
548
549segment_manager_t *
550application_get_listen_segment_manager (application_t * app,
551 stream_session_t * s)
552{
553 uword *smp;
554 smp = hash_get (app->listeners_table, listen_session_get_handle (s));
555 ASSERT (smp != 0);
556 return segment_manager_get (*smp);
557}
558
Florin Corasf8f516a2018-02-08 15:10:09 -0800559segment_manager_t *
560application_get_local_segment_manager (application_t * app)
561{
562 return segment_manager_get (app->local_segment_manager);
563}
564
565segment_manager_t *
566application_get_local_segment_manager_w_session (application_t * app,
567 local_session_t * ls)
568{
569 stream_session_t *listener;
570 if (application_local_session_listener_has_transport (ls))
571 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700572 listener = listen_session_get (ls->listener_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800573 return application_get_listen_segment_manager (app, listener);
574 }
575 return segment_manager_get (app->local_segment_manager);
576}
577
Dave Barach52851e62017-08-07 09:35:25 -0400578int
579application_is_proxy (application_t * app)
580{
Florin Coras7999e832017-10-31 01:51:04 -0700581 return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY);
582}
583
584int
585application_is_builtin (application_t * app)
586{
587 return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN);
588}
589
590int
591application_is_builtin_proxy (application_t * app)
592{
593 return (application_is_proxy (app) && application_is_builtin (app));
Dave Barach52851e62017-08-07 09:35:25 -0400594}
595
Florin Corasa332c462018-01-31 06:52:17 -0800596/**
597 * Send an API message to the external app, to map new segment
598 */
Florin Corasc87c91d2017-08-16 19:55:49 -0700599int
Florin Corasa332c462018-01-31 06:52:17 -0800600application_add_segment_notify (u32 app_index, ssvm_private_t * fs)
Florin Corasc87c91d2017-08-16 19:55:49 -0700601{
602 application_t *app = application_get (app_index);
Florin Corasa332c462018-01-31 06:52:17 -0800603 return app->cb_fns.add_segment_callback (app->api_client_index, fs);
Florin Corasc87c91d2017-08-16 19:55:49 -0700604}
605
Florin Corascea194d2017-10-02 00:18:51 -0700606u8
607application_has_local_scope (application_t * app)
608{
609 return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
610}
611
612u8
613application_has_global_scope (application_t * app)
614{
615 return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
616}
617
Florin Coras1c710452017-10-17 00:03:13 -0700618u32
619application_n_listeners (application_t * app)
620{
621 return hash_elts (app->listeners_table);
622}
623
624stream_session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700625application_first_listener (application_t * app, u8 fib_proto,
626 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700627{
Florin Coras7999e832017-10-31 01:51:04 -0700628 stream_session_t *listener;
Florin Coras1c710452017-10-17 00:03:13 -0700629 u64 handle;
630 u32 sm_index;
Florin Coras7999e832017-10-31 01:51:04 -0700631 u8 sst;
632
633 sst = session_type_from_proto_and_ip (transport_proto,
634 fib_proto == FIB_PROTOCOL_IP4);
Florin Coras1c710452017-10-17 00:03:13 -0700635
636 /* *INDENT-OFF* */
637 hash_foreach (handle, sm_index, app->listeners_table, ({
Florin Coras7999e832017-10-31 01:51:04 -0700638 listener = listen_session_get_from_handle (handle);
Florin Corasc3ddea82017-11-27 03:12:00 -0800639 if (listener->session_type == sst
640 && listener->listener_index != SESSION_PROXY_LISTENER_INDEX)
Florin Coras7999e832017-10-31 01:51:04 -0700641 return listener;
Florin Coras1c710452017-10-17 00:03:13 -0700642 }));
643 /* *INDENT-ON* */
644
645 return 0;
646}
647
Florin Coras19b1f6a2017-12-11 03:37:03 -0800648stream_session_t *
649application_proxy_listener (application_t * app, u8 fib_proto,
650 u8 transport_proto)
651{
652 stream_session_t *listener;
653 u64 handle;
654 u32 sm_index;
655 u8 sst;
656
657 sst = session_type_from_proto_and_ip (transport_proto,
658 fib_proto == FIB_PROTOCOL_IP4);
659
660 /* *INDENT-OFF* */
661 hash_foreach (handle, sm_index, app->listeners_table, ({
662 listener = listen_session_get_from_handle (handle);
663 if (listener->session_type == sst
664 && listener->listener_index == SESSION_PROXY_LISTENER_INDEX)
665 return listener;
666 }));
667 /* *INDENT-ON* */
668
669 return 0;
670}
671
Florin Coras7999e832017-10-31 01:51:04 -0700672static clib_error_t *
673application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto,
674 u8 transport_proto, u8 is_start)
675{
Florin Coras7999e832017-10-31 01:51:04 -0700676 app_namespace_t *app_ns = app_namespace_get (app->ns_index);
677 u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4);
678 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
679 transport_connection_t *tc;
680 stream_session_t *s;
681 u64 handle;
682
683 if (is_start)
684 {
Florin Coras19b1f6a2017-12-11 03:37:03 -0800685 s = application_first_listener (app, fib_proto, transport_proto);
686 if (!s)
687 {
688 sep.is_ip4 = is_ip4;
689 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
690 sep.sw_if_index = app_ns->sw_if_index;
691 sep.transport_proto = transport_proto;
692 application_start_listen (app, &sep, &handle);
693 s = listen_session_get_from_handle (handle);
694 s->listener_index = SESSION_PROXY_LISTENER_INDEX;
695 }
Florin Coras7999e832017-10-31 01:51:04 -0700696 }
697 else
698 {
Florin Coras19b1f6a2017-12-11 03:37:03 -0800699 s = application_proxy_listener (app, fib_proto, transport_proto);
700 ASSERT (s);
Florin Coras7999e832017-10-31 01:51:04 -0700701 }
Florin Coras19b1f6a2017-12-11 03:37:03 -0800702
Florin Coras7999e832017-10-31 01:51:04 -0700703 tc = listen_session_get_transport (s);
704
705 if (!ip_is_zero (&tc->lcl_ip, 1))
706 {
Florin Corasdbd44562017-11-09 19:30:17 -0800707 u32 sti;
708 sep.is_ip4 = is_ip4;
709 sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
710 sep.transport_proto = transport_proto;
711 sep.port = 0;
712 sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index);
Florin Coras19b1f6a2017-12-11 03:37:03 -0800713 if (is_start)
714 session_lookup_add_session_endpoint (sti, &sep, s->session_index);
715 else
716 session_lookup_del_session_endpoint (sti, &sep);
Florin Coras7999e832017-10-31 01:51:04 -0700717 }
Florin Coras19b1f6a2017-12-11 03:37:03 -0800718
Florin Coras7999e832017-10-31 01:51:04 -0700719 return 0;
720}
721
Florin Coras19b1f6a2017-12-11 03:37:03 -0800722static void
723application_start_stop_proxy_local_scope (application_t * app,
724 u8 transport_proto, u8 is_start)
725{
726 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
727 app_namespace_t *app_ns;
728 app_ns = app_namespace_get (app->ns_index);
729 sep.is_ip4 = 1;
730 sep.transport_proto = transport_proto;
731 sep.port = 0;
732
733 if (is_start)
734 {
735 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
736 app->index);
737 sep.is_ip4 = 0;
738 session_lookup_add_session_endpoint (app_ns->local_table_index, &sep,
739 app->index);
740 }
741 else
742 {
743 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
744 sep.is_ip4 = 0;
745 session_lookup_del_session_endpoint (app_ns->local_table_index, &sep);
746 }
747}
748
Florin Coras7999e832017-10-31 01:51:04 -0700749void
Florin Coras561af9b2017-12-09 10:19:43 -0800750application_start_stop_proxy (application_t * app,
751 transport_proto_t transport_proto, u8 is_start)
Florin Coras7999e832017-10-31 01:51:04 -0700752{
Florin Coras7999e832017-10-31 01:51:04 -0700753 if (application_has_local_scope (app))
Florin Coras19b1f6a2017-12-11 03:37:03 -0800754 application_start_stop_proxy_local_scope (app, transport_proto, is_start);
Florin Coras7999e832017-10-31 01:51:04 -0700755
756 if (application_has_global_scope (app))
757 {
758 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4,
759 transport_proto, is_start);
760 application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6,
761 transport_proto, is_start);
762 }
763}
764
765void
766application_setup_proxy (application_t * app)
767{
768 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -0800769 transport_proto_t tp;
770
Florin Coras7999e832017-10-31 01:51:04 -0700771 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -0800772
773 /* *INDENT-OFF* */
774 transport_proto_foreach (tp, ({
775 if (transports & (1 << tp))
776 application_start_stop_proxy (app, tp, 1);
777 }));
778 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -0700779}
780
781void
782application_remove_proxy (application_t * app)
783{
784 u16 transports = app->proxied_transports;
Florin Coras561af9b2017-12-09 10:19:43 -0800785 transport_proto_t tp;
786
Florin Coras7999e832017-10-31 01:51:04 -0700787 ASSERT (application_is_proxy (app));
Florin Coras561af9b2017-12-09 10:19:43 -0800788
789 /* *INDENT-OFF* */
790 transport_proto_foreach (tp, ({
791 if (transports & (1 << tp))
792 application_start_stop_proxy (app, tp, 0);
793 }));
794 /* *INDENT-ON* */
Florin Coras7999e832017-10-31 01:51:04 -0700795}
796
Florin Corasa332c462018-01-31 06:52:17 -0800797segment_manager_properties_t *
798application_segment_manager_properties (application_t * app)
799{
800 return &app->sm_properties;
801}
802
803segment_manager_properties_t *
804application_get_segment_manager_properties (u32 app_index)
805{
806 application_t *app = application_get (app_index);
807 return &app->sm_properties;
808}
809
Florin Coras3c2fed52018-07-04 04:15:05 -0700810static inline int
811app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
812{
Florin Coras52207f12018-07-12 14:48:06 -0700813 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
Florin Coras3c2fed52018-07-04 04:15:05 -0700814 {
815 clib_warning ("evt q full");
816 svm_msg_q_free_msg (mq, msg);
817 if (lock)
818 svm_msg_q_unlock (mq);
819 return -1;
820 }
Florin Coras52207f12018-07-12 14:48:06 -0700821
822 if (lock)
823 {
824 svm_msg_q_add_and_unlock (mq, msg);
825 return 0;
826 }
827
828 /* Even when not locking the ring, we must wait for queue mutex */
829 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
830 {
831 clib_warning ("msg q add returned");
832 return -1;
833 }
Florin Coras3c2fed52018-07-04 04:15:05 -0700834 return 0;
835}
836
837static inline int
838app_send_io_evt_rx (application_t * app, stream_session_t * s, u8 lock)
839{
Florin Coras52207f12018-07-12 14:48:06 -0700840 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -0700841 svm_msg_q_msg_t msg;
842 svm_msg_q_t *mq;
843
Florin Coras460dce62018-07-27 05:45:06 -0700844 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
845 && s->session_state != SESSION_STATE_LISTENING))
Florin Coras3c2fed52018-07-04 04:15:05 -0700846 {
847 /* Session is closed so app will never clean up. Flush rx fifo */
Florin Coras84099552018-07-22 12:59:30 -0700848 if (s->session_state == SESSION_STATE_CLOSED)
849 svm_fifo_dequeue_drop_all (s->server_rx_fifo);
Florin Coras3c2fed52018-07-04 04:15:05 -0700850 return 0;
851 }
852
Florin Coras3c2fed52018-07-04 04:15:05 -0700853 if (app->cb_fns.builtin_app_rx_callback)
854 return app->cb_fns.builtin_app_rx_callback (s);
855
Florin Coras54693d22018-07-17 10:46:29 -0700856 if (svm_fifo_has_event (s->server_rx_fifo)
857 || svm_fifo_is_empty (s->server_rx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -0700858 return 0;
859
860 mq = app->event_queue;
861 if (lock)
862 svm_msg_q_lock (mq);
863
864 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
865 {
866 clib_warning ("evt q rings full");
867 if (lock)
868 svm_msg_q_unlock (mq);
869 return -1;
870 }
871
872 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
873 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
874
Florin Coras52207f12018-07-12 14:48:06 -0700875 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -0700876 evt->fifo = s->server_rx_fifo;
877 evt->event_type = FIFO_EVENT_APP_RX;
878
Florin Coras54693d22018-07-17 10:46:29 -0700879 if (app_enqueue_evt (mq, &msg, lock))
880 return -1;
Florin Corasd3ca8ff2018-07-28 04:53:30 -0700881 (void) svm_fifo_set_event (s->server_rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -0700882 return 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700883}
884
885static inline int
886app_send_io_evt_tx (application_t * app, stream_session_t * s, u8 lock)
887{
888 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -0700889 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -0700890 svm_msg_q_msg_t msg;
891
892 if (application_is_builtin (app))
893 return 0;
894
895 mq = app->event_queue;
896 if (lock)
897 svm_msg_q_lock (mq);
898
899 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
900 {
901 clib_warning ("evt q rings full");
902 if (lock)
903 svm_msg_q_unlock (mq);
904 return -1;
905 }
906
907 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
908 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
909
Florin Coras52207f12018-07-12 14:48:06 -0700910 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -0700911 evt->event_type = FIFO_EVENT_APP_TX;
912 evt->fifo = s->server_tx_fifo;
913
914 return app_enqueue_evt (mq, &msg, lock);
915}
916
917/* *INDENT-OFF* */
918typedef int (app_send_evt_handler_fn) (application_t *app,
919 stream_session_t *s,
920 u8 lock);
Florin Coras460dce62018-07-27 05:45:06 -0700921static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = {
Florin Coras3c2fed52018-07-04 04:15:05 -0700922 app_send_io_evt_rx,
Florin Coras460dce62018-07-27 05:45:06 -0700923 0,
Florin Coras3c2fed52018-07-04 04:15:05 -0700924 app_send_io_evt_tx,
925};
926/* *INDENT-ON* */
927
928/**
929 * Send event to application
930 *
931 * Logic from queue perspective is non-blocking. That is, if there's
932 * not enough space to enqueue a message, we return. However, if the lock
933 * flag is set, we do wait for queue mutex.
934 */
935int
936application_send_event (application_t * app, stream_session_t * s,
937 u8 evt_type)
938{
939 ASSERT (app && evt_type <= FIFO_EVENT_APP_TX);
940 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
941}
942
943int
944application_lock_and_send_event (application_t * app, stream_session_t * s,
945 u8 evt_type)
946{
947 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
948}
949
Florin Corasf8f516a2018-02-08 15:10:09 -0800950local_session_t *
951application_alloc_local_session (application_t * app)
952{
953 local_session_t *s;
954 pool_get (app->local_sessions, s);
955 memset (s, 0, sizeof (*s));
956 s->app_index = app->index;
957 s->session_index = s - app->local_sessions;
958 s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
959 return s;
960}
961
962void
963application_free_local_session (application_t * app, local_session_t * s)
964{
965 pool_put (app->local_sessions, s);
966 if (CLIB_DEBUG)
967 memset (s, 0xfc, sizeof (*s));
968}
969
970local_session_t *
971application_get_local_session (application_t * app, u32 session_index)
972{
973 return pool_elt_at_index (app->local_sessions, session_index);
974}
975
976local_session_t *
977application_get_local_session_from_handle (session_handle_t handle)
978{
979 application_t *server;
980 u32 session_index, server_index;
981 local_session_parse_handle (handle, &server_index, &session_index);
982 server = application_get (server_index);
983 return application_get_local_session (server, session_index);
984}
985
986always_inline void
987application_local_listener_session_endpoint (local_session_t * ll,
988 session_endpoint_t * sep)
989{
990 sep->transport_proto =
991 session_type_transport_proto (ll->listener_session_type);
992 sep->port = ll->port;
993 sep->is_ip4 = ll->listener_session_type & 1;
994}
995
996int
997application_start_local_listen (application_t * server,
998 session_endpoint_t * sep,
999 session_handle_t * handle)
1000{
1001 session_handle_t lh;
1002 local_session_t *ll;
1003 u32 table_index;
1004
1005 table_index = application_local_session_table (server);
1006
1007 /* An exact sep match, as opposed to session_lookup_local_listener */
1008 lh = session_lookup_endpoint_listener (table_index, sep, 1);
1009 if (lh != SESSION_INVALID_HANDLE)
1010 return VNET_API_ERROR_ADDRESS_IN_USE;
1011
1012 pool_get (server->local_listen_sessions, ll);
1013 memset (ll, 0, sizeof (*ll));
1014 ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
1015 ll->app_index = server->index;
1016 ll->session_index = ll - server->local_listen_sessions;
1017 ll->port = sep->port;
1018 /* Store the original session type for the unbind */
1019 ll->listener_session_type =
1020 session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
Florin Coras5fda7a32018-02-14 08:04:31 -08001021 ll->transport_listener_index = ~0;
Florin Corasf8f516a2018-02-08 15:10:09 -08001022
1023 *handle = application_local_session_handle (ll);
1024 session_lookup_add_session_endpoint (table_index, sep, *handle);
1025
1026 return 0;
1027}
1028
1029/**
1030 * Clean up local session table. If we have a listener session use it to
1031 * find the port and proto. If not, the handle must be a local table handle
1032 * so parse it.
1033 */
1034int
1035application_stop_local_listen (application_t * server, session_handle_t lh)
1036{
1037 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
1038 u32 table_index, ll_index, server_index;
1039 stream_session_t *sl = 0;
1040 local_session_t *ll, *ls;
1041
1042 table_index = application_local_session_table (server);
1043
1044 /* We have both local and global table binds. Figure from global what
1045 * the sep we should be cleaning up is.
1046 */
1047 if (!session_handle_is_local (lh))
1048 {
1049 sl = listen_session_get_from_handle (lh);
1050 if (!sl || listen_session_get_local_session_endpoint (sl, &sep))
1051 {
1052 clib_warning ("broken listener");
1053 return -1;
1054 }
1055 lh = session_lookup_endpoint_listener (table_index, &sep, 0);
1056 if (lh == SESSION_INVALID_HANDLE)
1057 return -1;
1058 }
1059
1060 local_session_parse_handle (lh, &server_index, &ll_index);
1061 ASSERT (server->index == server_index);
1062 if (!(ll = application_get_local_listen_session (server, ll_index)))
1063 {
1064 clib_warning ("no local listener");
1065 return -1;
1066 }
1067 application_local_listener_session_endpoint (ll, &sep);
1068 session_lookup_del_session_endpoint (table_index, &sep);
1069
1070 /* *INDENT-OFF* */
1071 pool_foreach (ls, server->local_sessions, ({
1072 if (ls->listener_index == ll->session_index)
1073 application_local_session_disconnect (server->index, ls);
1074 }));
1075 /* *INDENT-ON* */
1076 pool_put_index (server->local_listen_sessions, ll->session_index);
1077
1078 return 0;
1079}
1080
1081int
1082application_local_session_connect (u32 table_index, application_t * client,
1083 application_t * server,
1084 local_session_t * ll, u32 opaque)
1085{
1086 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
1087 segment_manager_properties_t *props, *cprops;
Florin Coras54693d22018-07-17 10:46:29 -07001088 u32 round_rx_fifo_sz, round_tx_fifo_sz;
Florin Corasf8f516a2018-02-08 15:10:09 -08001089 int rv, has_transport, seg_index;
1090 svm_fifo_segment_private_t *seg;
1091 segment_manager_t *sm;
1092 local_session_t *ls;
Florin Coras3c2fed52018-07-04 04:15:05 -07001093 svm_msg_q_t *sq, *cq;
Florin Corasf8f516a2018-02-08 15:10:09 -08001094
1095 ls = application_alloc_local_session (server);
1096
1097 props = application_segment_manager_properties (server);
1098 cprops = application_segment_manager_properties (client);
1099 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -07001100 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
Florin Coras54693d22018-07-17 10:46:29 -07001101 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
1102 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
1103 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
Florin Corasf8f516a2018-02-08 15:10:09 -08001104
1105 has_transport = session_has_transport ((stream_session_t *) ll);
1106 if (!has_transport)
1107 {
1108 /* Local sessions don't have backing transport */
1109 ls->port = ll->port;
1110 sm = application_get_local_segment_manager (server);
1111 }
1112 else
1113 {
1114 stream_session_t *sl = (stream_session_t *) ll;
1115 transport_connection_t *tc;
1116 tc = listen_session_get_transport (sl);
1117 ls->port = tc->lcl_port;
1118 sm = application_get_listen_segment_manager (server, sl);
1119 }
1120
1121 seg_index = segment_manager_add_segment (sm, seg_size);
1122 if (seg_index < 0)
1123 {
1124 clib_warning ("failed to add new cut-through segment");
1125 return seg_index;
1126 }
1127 seg = segment_manager_get_segment_w_lock (sm, seg_index);
1128 sq = segment_manager_alloc_queue (seg, props->evt_q_size);
1129 cq = segment_manager_alloc_queue (seg, cprops->evt_q_size);
1130 ls->server_evt_q = pointer_to_uword (sq);
1131 ls->client_evt_q = pointer_to_uword (cq);
1132 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
1133 props->tx_fifo_size,
1134 &ls->server_rx_fifo,
1135 &ls->server_tx_fifo);
1136 if (rv)
1137 {
1138 clib_warning ("failed to add fifos in cut-through segment");
1139 segment_manager_segment_reader_unlock (sm);
1140 goto failed;
1141 }
1142 ls->server_rx_fifo->master_session_index = ls->session_index;
1143 ls->server_tx_fifo->master_session_index = ls->session_index;
1144 ls->server_rx_fifo->master_thread_index = ~0;
1145 ls->server_tx_fifo->master_thread_index = ~0;
1146 ls->svm_segment_index = seg_index;
1147 ls->listener_index = ll->session_index;
1148 ls->client_index = client->index;
1149 ls->client_opaque = opaque;
1150 ls->listener_session_type = ll->session_type;
1151
1152 if ((rv = server->cb_fns.add_segment_callback (server->api_client_index,
1153 &seg->ssvm)))
1154 {
1155 clib_warning ("failed to notify server of new segment");
1156 segment_manager_segment_reader_unlock (sm);
1157 goto failed;
1158 }
1159 segment_manager_segment_reader_unlock (sm);
1160 if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls)))
1161 {
1162 clib_warning ("failed to send accept cut-through notify to server");
1163 goto failed;
1164 }
1165 if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN)
1166 application_local_session_connect_notify (ls);
1167
1168 return 0;
1169
1170failed:
1171 if (!has_transport)
1172 segment_manager_del_segment (sm, seg);
1173 return rv;
1174}
1175
1176static uword
1177application_client_local_connect_key (local_session_t * ls)
1178{
1179 return ((uword) ls->app_index << 32 | (uword) ls->session_index);
1180}
1181
1182static void
1183application_client_local_connect_key_parse (uword key, u32 * app_index,
1184 u32 * session_index)
1185{
1186 *app_index = key >> 32;
1187 *session_index = key & 0xFFFFFFFF;
1188}
1189
1190int
1191application_local_session_connect_notify (local_session_t * ls)
1192{
1193 svm_fifo_segment_private_t *seg;
1194 application_t *client, *server;
1195 segment_manager_t *sm;
1196 int rv, is_fail = 0;
1197 uword client_key;
1198
1199 client = application_get (ls->client_index);
1200 server = application_get (ls->app_index);
1201 sm = application_get_local_segment_manager_w_session (server, ls);
1202 seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index);
1203 if ((rv = client->cb_fns.add_segment_callback (client->api_client_index,
1204 &seg->ssvm)))
1205 {
1206 clib_warning ("failed to notify client %u of new segment",
1207 ls->client_index);
1208 segment_manager_segment_reader_unlock (sm);
1209 application_local_session_disconnect (ls->client_index, ls);
1210 is_fail = 1;
1211 }
1212 else
1213 {
1214 segment_manager_segment_reader_unlock (sm);
1215 }
1216
1217 client->cb_fns.session_connected_callback (client->index, ls->client_opaque,
1218 (stream_session_t *) ls,
1219 is_fail);
1220
1221 client_key = application_client_local_connect_key (ls);
1222 hash_set (client->local_connects, client_key, client_key);
1223 return 0;
1224}
1225
1226int
Florin Corasf6647e02018-03-23 22:56:43 -07001227application_local_session_cleanup (application_t * client,
1228 application_t * server,
1229 local_session_t * ls)
Florin Corasf8f516a2018-02-08 15:10:09 -08001230{
1231 svm_fifo_segment_private_t *seg;
Florin Corasf8f516a2018-02-08 15:10:09 -08001232 segment_manager_t *sm;
1233 uword client_key;
Florin Corasf6647e02018-03-23 22:56:43 -07001234 u8 has_transport;
1235
1236 has_transport = session_has_transport ((stream_session_t *) ls);
1237 client_key = application_client_local_connect_key (ls);
1238 if (!has_transport)
1239 sm = application_get_local_segment_manager_w_session (server, ls);
1240 else
1241 sm = application_get_listen_segment_manager (server,
1242 (stream_session_t *) ls);
1243
1244 seg = segment_manager_get_segment (sm, ls->svm_segment_index);
1245 if (client)
1246 hash_unset (client->local_connects, client_key);
1247
1248 if (!has_transport)
1249 {
1250 server->cb_fns.del_segment_callback (server->api_client_index,
1251 &seg->ssvm);
1252 if (client)
1253 client->cb_fns.del_segment_callback (client->api_client_index,
1254 &seg->ssvm);
1255 segment_manager_del_segment (sm, seg);
1256 }
1257
1258 application_free_local_session (server, ls);
1259
1260 return 0;
1261}
1262
1263int
1264application_local_session_disconnect (u32 app_index, local_session_t * ls)
1265{
1266 application_t *client, *server;
Florin Corasf8f516a2018-02-08 15:10:09 -08001267
1268 client = application_get_if_valid (ls->client_index);
1269 server = application_get (ls->app_index);
1270
1271 if (ls->session_state == SESSION_STATE_CLOSED)
Florin Corasf6647e02018-03-23 22:56:43 -07001272 return application_local_session_cleanup (client, server, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001273
1274 if (app_index == ls->client_index)
1275 {
1276 send_local_session_disconnect_callback (ls->app_index, ls);
1277 }
1278 else
1279 {
1280 if (!client)
1281 {
Florin Corasf6647e02018-03-23 22:56:43 -07001282 return application_local_session_cleanup (client, server, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001283 }
1284 else if (ls->session_state < SESSION_STATE_READY)
1285 {
1286 client->cb_fns.session_connected_callback (client->index,
1287 ls->client_opaque,
1288 (stream_session_t *) ls,
1289 1 /* is_fail */ );
1290 ls->session_state = SESSION_STATE_CLOSED;
Florin Corasf6647e02018-03-23 22:56:43 -07001291 return application_local_session_cleanup (client, server, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001292 }
1293 else
1294 {
Florin Corasf6647e02018-03-23 22:56:43 -07001295 send_local_session_disconnect_callback (client->index, ls);
Florin Corasf8f516a2018-02-08 15:10:09 -08001296 }
1297 }
1298
1299 ls->session_state = SESSION_STATE_CLOSED;
1300
1301 return 0;
1302}
1303
Florin Corasf6647e02018-03-23 22:56:43 -07001304int
1305application_local_session_disconnect_w_index (u32 app_index, u32 ls_index)
1306{
1307 application_t *app;
1308 local_session_t *ls;
1309 app = application_get (app_index);
1310 ls = application_get_local_session (app, ls_index);
1311 return application_local_session_disconnect (app_index, ls);
1312}
1313
Florin Corasf8f516a2018-02-08 15:10:09 -08001314void
1315application_local_sessions_del (application_t * app)
1316{
1317 u32 index, server_index, session_index, table_index;
1318 segment_manager_t *sm;
1319 u64 handle, *handles = 0;
1320 local_session_t *ls, *ll;
1321 application_t *server;
1322 session_endpoint_t sep;
1323 int i;
1324
1325 /*
1326 * Local listens. Don't bother with local sessions, we clean them lower
1327 */
1328 table_index = application_local_session_table (app);
1329 /* *INDENT-OFF* */
1330 pool_foreach (ll, app->local_listen_sessions, ({
1331 application_local_listener_session_endpoint (ll, &sep);
1332 session_lookup_del_session_endpoint (table_index, &sep);
1333 }));
1334 /* *INDENT-ON* */
1335
1336 /*
1337 * Local sessions
1338 */
1339 if (app->local_sessions)
1340 {
1341 /* *INDENT-OFF* */
1342 pool_foreach (ls, app->local_sessions, ({
1343 application_local_session_disconnect (app->index, ls);
1344 }));
1345 /* *INDENT-ON* */
1346 }
1347
1348 /*
1349 * Local connects
1350 */
1351 vec_reset_length (handles);
1352 /* *INDENT-OFF* */
1353 hash_foreach (handle, index, app->local_connects, ({
1354 vec_add1 (handles, handle);
1355 }));
1356 /* *INDENT-ON* */
1357
1358 for (i = 0; i < vec_len (handles); i++)
1359 {
1360 application_client_local_connect_key_parse (handles[i], &server_index,
1361 &session_index);
1362 server = application_get_if_valid (server_index);
1363 if (server)
1364 {
1365 ls = application_get_local_session (server, session_index);
1366 application_local_session_disconnect (app->index, ls);
1367 }
1368 }
1369
1370 sm = segment_manager_get (app->local_segment_manager);
1371 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
1372 segment_manager_del (sm);
1373}
1374
Florin Coras371ca502018-02-21 12:07:41 -08001375clib_error_t *
1376vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a)
1377{
1378 application_t *app;
1379 app = application_get (a->app_index);
1380 if (!app)
1381 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1382 0, "app %u doesn't exist", a->app_index);
1383 app->tls_cert = vec_dup (a->cert);
1384 return 0;
1385}
1386
1387clib_error_t *
1388vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a)
1389{
1390 application_t *app;
1391 app = application_get (a->app_index);
1392 if (!app)
1393 return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED,
1394 0, "app %u doesn't exist", a->app_index);
1395 app->tls_key = vec_dup (a->key);
1396 return 0;
1397}
1398
Dave Barach68b0fb02017-02-28 15:15:56 -05001399u8 *
Florin Coras6cf30ad2017-04-04 23:08:23 -07001400format_application_listener (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -05001401{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001402 application_t *app = va_arg (*args, application_t *);
1403 u64 handle = va_arg (*args, u64);
Florin Corasf8f516a2018-02-08 15:10:09 -08001404 u32 sm_index = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -05001405 int verbose = va_arg (*args, int);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001406 stream_session_t *listener;
1407 u8 *app_name, *str;
Dave Barach68b0fb02017-02-28 15:15:56 -05001408
Florin Coras6cf30ad2017-04-04 23:08:23 -07001409 if (app == 0)
Dave Barach68b0fb02017-02-28 15:15:56 -05001410 {
1411 if (verbose)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001412 s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
1413 "API Client", "ListenerID", "SegManager");
Dave Barach68b0fb02017-02-28 15:15:56 -05001414 else
Florin Coras6cf30ad2017-04-04 23:08:23 -07001415 s = format (s, "%-40s%-20s", "Connection", "App");
Dave Barach68b0fb02017-02-28 15:15:56 -05001416
1417 return s;
1418 }
1419
Florin Coras6cf30ad2017-04-04 23:08:23 -07001420 app_name = app_get_name_from_reg_index (app);
1421 listener = listen_session_get_from_handle (handle);
1422 str = format (0, "%U", format_stream_session, listener, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05001423
Dave Barach68b0fb02017-02-28 15:15:56 -05001424 if (verbose)
1425 {
Florin Coras6cf30ad2017-04-04 23:08:23 -07001426 s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
Florin Corasf8f516a2018-02-08 15:10:09 -08001427 app->api_client_index, handle, sm_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001428 }
1429 else
Florin Coras6cf30ad2017-04-04 23:08:23 -07001430 s = format (s, "%-40s%-20s", str, app_name);
1431
1432 vec_free (app_name);
1433 return s;
1434}
1435
1436void
1437application_format_connects (application_t * app, int verbose)
1438{
Florin Corasa332c462018-01-31 06:52:17 -08001439 svm_fifo_segment_private_t *fifo_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001440 vlib_main_t *vm = vlib_get_main ();
1441 segment_manager_t *sm;
1442 u8 *app_name, *s = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001443
1444 /* Header */
1445 if (app == 0)
1446 {
1447 if (verbose)
1448 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1449 "API Client", "SegManager");
1450 else
1451 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1452 return;
1453 }
1454
1455 /* make sure */
1456 if (app->connects_seg_manager == (u32) ~ 0)
1457 return;
1458
1459 app_name = app_get_name_from_reg_index (app);
1460
1461 /* Across all fifo segments */
1462 sm = segment_manager_get (app->connects_seg_manager);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001463
Florin Corasa332c462018-01-31 06:52:17 -08001464 /* *INDENT-OFF* */
1465 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
1466 svm_fifo_t *fifo;
1467 u8 *str;
1468
1469 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
1470 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001471 {
Florin Coras6cf30ad2017-04-04 23:08:23 -07001472 u32 session_index, thread_index;
1473 stream_session_t *session;
1474
Florin Corasa5464812017-04-19 13:00:05 -07001475 session_index = fifo->master_session_index;
1476 thread_index = fifo->master_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001477
Florin Corascea194d2017-10-02 00:18:51 -07001478 session = session_get (session_index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001479 str = format (0, "%U", format_stream_session, session, verbose);
1480
1481 if (verbose)
1482 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
1483 app->api_client_index, app->connects_seg_manager);
1484 else
1485 s = format (s, "%-40s%-20s", str, app_name);
1486
1487 vlib_cli_output (vm, "%v", s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001488 vec_reset_length (s);
1489 vec_free (str);
Dave Barach10d8cc62017-05-30 09:30:07 -04001490
1491 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001492 }
Florin Corasa332c462018-01-31 06:52:17 -08001493 vec_free (s);
1494 }));
1495 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001496
1497 vec_free (app_name);
1498}
1499
Florin Corasf8f516a2018-02-08 15:10:09 -08001500void
1501application_format_local_sessions (application_t * app, int verbose)
1502{
1503 vlib_main_t *vm = vlib_get_main ();
1504 local_session_t *ls;
1505 transport_proto_t tp;
1506 u8 *conn = 0;
1507
1508 /* Header */
1509 if (app == 0)
1510 {
1511 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp",
1512 "ClientApp");
1513 return;
1514 }
1515
1516 /* *INDENT-OFF* */
1517 pool_foreach (ls, app->local_listen_sessions, ({
1518 tp = session_type_transport_proto(ls->listener_session_type);
1519 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1520 ls->port);
1521 vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_index, "*");
1522 vec_reset_length (conn);
1523 }));
1524 pool_foreach (ls, app->local_sessions, ({
1525 tp = session_type_transport_proto(ls->listener_session_type);
1526 conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
1527 ls->port);
1528 vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_index,
1529 ls->client_index);
1530 vec_reset_length (conn);
1531 }));
1532 /* *INDENT-ON* */
1533
1534 vec_free (conn);
1535}
1536
1537void
1538application_format_local_connects (application_t * app, int verbose)
1539{
1540 vlib_main_t *vm = vlib_get_main ();
1541 u32 app_index, session_index;
1542 application_t *server;
1543 local_session_t *ls;
1544 uword client_key;
1545 u64 value;
1546
1547 /* Header */
1548 if (app == 0)
1549 {
1550 if (verbose)
1551 vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App",
1552 "Peer App", "SegManager");
1553 else
1554 vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App",
1555 "Peer App");
1556 return;
1557 }
1558
1559 /* *INDENT-OFF* */
1560 hash_foreach (client_key, value, app->local_connects, ({
1561 application_client_local_connect_key_parse (client_key, &app_index,
1562 &session_index);
1563 server = application_get (app_index);
1564 ls = application_get_local_session (server, session_index);
1565 vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_index, ls->client_index);
1566 }));
1567 /* *INDENT-ON* */
1568}
1569
Florin Coras6cf30ad2017-04-04 23:08:23 -07001570u8 *
1571format_application (u8 * s, va_list * args)
1572{
1573 application_t *app = va_arg (*args, application_t *);
1574 CLIB_UNUSED (int verbose) = va_arg (*args, int);
Florin Corasad0c77f2017-11-09 18:00:15 -08001575 segment_manager_properties_t *props;
Florin Corascea194d2017-10-02 00:18:51 -07001576 const u8 *app_ns_name;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001577 u8 *app_name;
1578
1579 if (app == 0)
1580 {
1581 if (verbose)
Florin Corascea194d2017-10-02 00:18:51 -07001582 s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name",
Florin Coras078371e2018-05-11 09:20:12 -07001583 "API Client", "Namespace", "Add seg size", "Rx-f size",
1584 "Tx-f size");
Florin Coras6cf30ad2017-04-04 23:08:23 -07001585 else
Florin Coras0bee9ce2018-03-22 21:24:31 -07001586 s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client",
1587 "Namespace");
Florin Coras6cf30ad2017-04-04 23:08:23 -07001588 return s;
1589 }
1590
Florin Coras0bee9ce2018-03-22 21:24:31 -07001591 app_name = app_get_name (app);
Florin Corascea194d2017-10-02 00:18:51 -07001592 app_ns_name = app_namespace_id_from_index (app->ns_index);
Florin Corasa332c462018-01-31 06:52:17 -08001593 props = application_segment_manager_properties (app);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001594 if (verbose)
Florin Coras078371e2018-05-11 09:20:12 -07001595 s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->index,
Florin Coras0bee9ce2018-03-22 21:24:31 -07001596 app_name, app->api_client_index, app->ns_index,
Florin Coras078371e2018-05-11 09:20:12 -07001597 format_memory_size, props->add_segment_size,
1598 format_memory_size, props->rx_fifo_size, format_memory_size,
Florin Coras0bee9ce2018-03-22 21:24:31 -07001599 props->tx_fifo_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001600 else
Florin Coras078371e2018-05-11 09:20:12 -07001601 s = format (s, "%-10u%-20s%-15d%-40s", app->index, app_name,
Florin Coras8f107b32017-11-21 22:13:03 -08001602 app->api_client_index, app_ns_name);
Dave Barach68b0fb02017-02-28 15:15:56 -05001603 return s;
1604}
1605
Florin Corasf8f516a2018-02-08 15:10:09 -08001606
1607void
1608application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose)
1609{
1610 application_t *app;
1611 u32 sm_index;
1612 u64 handle;
1613
1614 if (!pool_elts (app_pool))
1615 {
1616 vlib_cli_output (vm, "No active server bindings");
1617 return;
1618 }
1619
1620 if (do_local)
1621 {
1622 application_format_local_sessions (0, verbose);
1623 /* *INDENT-OFF* */
1624 pool_foreach (app, app_pool, ({
1625 if (!pool_elts (app->local_sessions)
1626 && !pool_elts(app->local_connects))
1627 continue;
1628 application_format_local_sessions (app, verbose);
1629 }));
1630 /* *INDENT-ON* */
1631 }
1632 else
1633 {
1634 vlib_cli_output (vm, "%U", format_application_listener, 0 /* header */ ,
1635 0, 0, verbose);
1636
1637 /* *INDENT-OFF* */
1638 pool_foreach (app, app_pool, ({
1639 if (hash_elts (app->listeners_table) == 0)
1640 continue;
1641 hash_foreach (handle, sm_index, app->listeners_table, ({
1642 vlib_cli_output (vm, "%U", format_application_listener, app,
1643 handle, sm_index, verbose);
1644 }));
1645 }));
1646 /* *INDENT-ON* */
1647 }
1648}
1649
1650void
1651application_format_all_clients (vlib_main_t * vm, int do_local, int verbose)
1652{
1653 application_t *app;
1654
1655 if (!pool_elts (app_pool))
1656 {
1657 vlib_cli_output (vm, "No active apps");
1658 return;
1659 }
1660
1661 if (do_local)
1662 {
1663 application_format_local_connects (0, verbose);
1664
1665 /* *INDENT-OFF* */
1666 pool_foreach (app, app_pool, ({
1667 if (app->local_connects)
1668 application_format_local_connects (app, verbose);
1669 }));
1670 /* *INDENT-ON* */
1671 }
1672 else
1673 {
1674 application_format_connects (0, verbose);
1675
1676 /* *INDENT-OFF* */
1677 pool_foreach (app, app_pool, ({
1678 if (app->connects_seg_manager == (u32)~0)
1679 continue;
1680 application_format_connects (app, verbose);
1681 }));
1682 /* *INDENT-ON* */
1683 }
1684}
1685
Dave Barach68b0fb02017-02-28 15:15:56 -05001686static clib_error_t *
1687show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
1688 vlib_cli_command_t * cmd)
1689{
Florin Corasf8f516a2018-02-08 15:10:09 -08001690 int do_server = 0, do_client = 0, do_local = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001691 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001692 int verbose = 0;
1693
Florin Corascea194d2017-10-02 00:18:51 -07001694 session_cli_return_if_not_enabled ();
Florin Corase04c2992017-03-01 08:17:34 -08001695
Dave Barach68b0fb02017-02-28 15:15:56 -05001696 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1697 {
1698 if (unformat (input, "server"))
1699 do_server = 1;
1700 else if (unformat (input, "client"))
1701 do_client = 1;
Florin Corasf8f516a2018-02-08 15:10:09 -08001702 else if (unformat (input, "local"))
1703 do_local = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001704 else if (unformat (input, "verbose"))
1705 verbose = 1;
1706 else
1707 break;
1708 }
1709
1710 if (do_server)
Florin Corasf8f516a2018-02-08 15:10:09 -08001711 application_format_all_listeners (vm, do_local, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05001712
1713 if (do_client)
Florin Corasf8f516a2018-02-08 15:10:09 -08001714 application_format_all_clients (vm, do_local, verbose);
Dave Barach68b0fb02017-02-28 15:15:56 -05001715
Florin Coras6cf30ad2017-04-04 23:08:23 -07001716 /* Print app related info */
1717 if (!do_server && !do_client)
1718 {
1719 vlib_cli_output (vm, "%U", format_application, 0, verbose);
Florin Corascea194d2017-10-02 00:18:51 -07001720 /* *INDENT-OFF* */
1721 pool_foreach (app, app_pool, ({
1722 vlib_cli_output (vm, "%U", format_application, app, verbose);
1723 }));
1724 /* *INDENT-ON* */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001725 }
1726
Dave Barach68b0fb02017-02-28 15:15:56 -05001727 return 0;
1728}
1729
Florin Corase04c2992017-03-01 08:17:34 -08001730/* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001731VLIB_CLI_COMMAND (show_app_command, static) =
1732{
Florin Corase04c2992017-03-01 08:17:34 -08001733 .path = "show app",
1734 .short_help = "show app [server|client] [verbose]",
1735 .function = show_app_command_fn,
1736};
1737/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001738
1739/*
1740 * fd.io coding-style-patch-verification: ON
1741 *
1742 * Local Variables:
1743 * eval: (c-set-style "gnu")
1744 * End:
1745 */