Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 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 Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 17 | #include <vnet/session/application_interface.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 18 | #include <vnet/session/session.h> |
| 19 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 20 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 21 | * Pool from which we allocate all applications |
| 22 | */ |
| 23 | static application_t *app_pool; |
| 24 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 25 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | * Hash table of apps by api client index |
| 27 | */ |
| 28 | static uword *app_by_api_client_index; |
| 29 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Default application event queue size |
| 32 | */ |
| 33 | static u32 default_app_evt_queue_size = 128; |
| 34 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 35 | int |
| 36 | application_api_queue_is_full (application_t * app) |
| 37 | { |
| 38 | unix_shared_memory_queue_t *q; |
| 39 | |
| 40 | /* builtin servers are always OK */ |
| 41 | if (app->api_client_index == ~0) |
| 42 | return 0; |
| 43 | |
| 44 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 45 | if (!q) |
| 46 | return 1; |
| 47 | |
| 48 | if (q->cursize == q->maxsize) |
| 49 | return 1; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | application_table_add (application_t * app) |
| 55 | { |
| 56 | hash_set (app_by_api_client_index, app->api_client_index, app->index); |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | application_table_del (application_t * app) |
| 61 | { |
| 62 | hash_unset (app_by_api_client_index, app->api_client_index); |
| 63 | } |
| 64 | |
| 65 | application_t * |
| 66 | application_lookup (u32 api_client_index) |
| 67 | { |
| 68 | uword *p; |
| 69 | p = hash_get (app_by_api_client_index, api_client_index); |
| 70 | if (p) |
| 71 | return application_get (p[0]); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 76 | application_t * |
| 77 | application_new () |
| 78 | { |
| 79 | application_t *app; |
| 80 | pool_get (app_pool, app); |
| 81 | memset (app, 0, sizeof (*app)); |
| 82 | app->index = application_get_index (app); |
| 83 | app->connects_seg_manager = ~0; |
| 84 | return app; |
| 85 | } |
| 86 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 87 | void |
| 88 | application_del (application_t * app) |
| 89 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 90 | segment_manager_t *sm; |
| 91 | u64 handle; |
| 92 | u32 index, *handles = 0; |
| 93 | int i; |
| 94 | vnet_unbind_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 95 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 96 | /* |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 97 | * The app event queue allocated in first segment is cleared with |
| 98 | * the segment manager. No need to explicitly free it. |
| 99 | */ |
| 100 | |
| 101 | /* |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 102 | * Cleanup segment managers |
| 103 | */ |
| 104 | if (app->connects_seg_manager != (u32) ~ 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 106 | sm = segment_manager_get (app->connects_seg_manager); |
| 107 | segment_manager_del (sm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 110 | /* *INDENT-OFF* */ |
| 111 | hash_foreach (handle, index, app->listeners_table, |
| 112 | ({ |
| 113 | vec_add1 (handles, handle); |
| 114 | })); |
| 115 | /* *INDENT-ON* */ |
| 116 | |
| 117 | /* Actual listener cleanup */ |
| 118 | for (i = 0; i < vec_len (handles); i++) |
| 119 | { |
| 120 | a->app_index = app->api_client_index; |
| 121 | a->handle = handles[i]; |
| 122 | /* seg manager is removed when unbind completes */ |
| 123 | vnet_unbind (a); |
| 124 | } |
| 125 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 126 | application_table_del (app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 127 | pool_put (app_pool, app); |
| 128 | } |
| 129 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 130 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 131 | application_verify_cb_fns (session_cb_vft_t * cb_fns) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 132 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 133 | if (cb_fns->session_accept_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 134 | clib_warning ("No accept callback function provided"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 135 | if (cb_fns->session_connected_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 136 | clib_warning ("No session connected callback function provided"); |
| 137 | if (cb_fns->session_disconnect_callback == 0) |
| 138 | clib_warning ("No session disconnect callback function provided"); |
| 139 | if (cb_fns->session_reset_callback == 0) |
| 140 | clib_warning ("No session reset callback function provided"); |
| 141 | } |
| 142 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 143 | int |
| 144 | application_init (application_t * app, u32 api_client_index, u64 * options, |
| 145 | session_cb_vft_t * cb_fns) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 146 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 147 | segment_manager_t *sm; |
| 148 | segment_manager_properties_t *props; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 149 | u32 app_evt_queue_size, first_seg_size; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 150 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 151 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 152 | app_evt_queue_size = options[APP_EVT_QUEUE_SIZE] > 0 ? |
| 153 | options[APP_EVT_QUEUE_SIZE] : default_app_evt_queue_size; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 154 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 155 | /* Setup segment manager */ |
| 156 | sm = segment_manager_new (); |
| 157 | sm->app_index = app->index; |
| 158 | props = &app->sm_properties; |
| 159 | props->add_segment_size = options[SESSION_OPTIONS_ADD_SEGMENT_SIZE]; |
| 160 | props->rx_fifo_size = options[SESSION_OPTIONS_RX_FIFO_SIZE]; |
| 161 | props->tx_fifo_size = options[SESSION_OPTIONS_TX_FIFO_SIZE]; |
| 162 | props->add_segment = props->add_segment_size != 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 163 | props->use_private_segment = options[APP_OPTIONS_FLAGS] |
| 164 | & APP_OPTIONS_FLAGS_BUILTIN_APP; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 165 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 166 | first_seg_size = options[SESSION_OPTIONS_SEGMENT_SIZE]; |
| 167 | if ((rv = segment_manager_init (sm, props, first_seg_size))) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 168 | return rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 169 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 170 | app->first_segment_manager = segment_manager_index (sm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 171 | app->api_client_index = api_client_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 172 | app->flags = options[APP_OPTIONS_FLAGS]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 173 | app->cb_fns = *cb_fns; |
| 174 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 175 | /* Allocate app event queue in the first shared-memory segment */ |
| 176 | app->event_queue = segment_manager_alloc_queue (sm, app_evt_queue_size); |
| 177 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 178 | /* Check that the obvious things are properly set up */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 179 | application_verify_cb_fns (cb_fns); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 180 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 181 | /* Add app to lookup by api_client_index table */ |
| 182 | application_table_add (app); |
| 183 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 184 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | application_t * |
| 188 | application_get (u32 index) |
| 189 | { |
| 190 | return pool_elt_at_index (app_pool, index); |
| 191 | } |
| 192 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 193 | application_t * |
| 194 | application_get_if_valid (u32 index) |
| 195 | { |
| 196 | if (pool_is_free_index (app_pool, index)) |
| 197 | return 0; |
| 198 | |
| 199 | return pool_elt_at_index (app_pool, index); |
| 200 | } |
| 201 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 202 | u32 |
| 203 | application_get_index (application_t * app) |
| 204 | { |
| 205 | return app - app_pool; |
| 206 | } |
| 207 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 208 | static segment_manager_t * |
| 209 | application_alloc_segment_manager (application_t * app) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 210 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 211 | segment_manager_t *sm = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 212 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 213 | if (app->first_segment_manager != (u32) ~ 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 214 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 215 | sm = segment_manager_get (app->first_segment_manager); |
| 216 | app->first_segment_manager = ~0; |
| 217 | return sm; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 220 | sm = segment_manager_new (); |
| 221 | if (segment_manager_init (sm, &app->sm_properties, 0)) |
| 222 | return 0; |
| 223 | return sm; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Start listening local transport endpoint for requested transport. |
| 228 | * |
| 229 | * Creates a 'dummy' stream session with state LISTENING to be used in session |
| 230 | * lookups, prior to establishing connection. Requests transport to build |
| 231 | * it's own specific listening connection. |
| 232 | */ |
| 233 | int |
| 234 | application_start_listen (application_t * srv, session_type_t session_type, |
| 235 | transport_endpoint_t * tep, u64 * res) |
| 236 | { |
| 237 | segment_manager_t *sm; |
| 238 | stream_session_t *s; |
| 239 | u64 handle; |
| 240 | |
| 241 | s = listen_session_new (session_type); |
| 242 | s->app_index = srv->index; |
| 243 | |
| 244 | if (stream_session_listen (s, tep)) |
| 245 | goto err; |
| 246 | |
| 247 | /* Allocate segment manager. All sessions derived out of a listen session |
| 248 | * have fifos allocated by the same segment manager. */ |
| 249 | sm = application_alloc_segment_manager (srv); |
| 250 | if (sm == 0) |
| 251 | goto err; |
| 252 | |
| 253 | /* Add to app's listener table. Useful to find all child listeners |
| 254 | * when app goes down, although, just for unbinding this is not needed */ |
| 255 | handle = listen_session_get_handle (s); |
| 256 | hash_set (srv->listeners_table, handle, segment_manager_index (sm)); |
| 257 | |
| 258 | *res = handle; |
| 259 | return 0; |
| 260 | |
| 261 | err: |
| 262 | listen_session_del (s); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Stop listening on session associated to handle |
| 268 | */ |
| 269 | int |
| 270 | application_stop_listen (application_t * srv, u64 handle) |
| 271 | { |
| 272 | stream_session_t *listener; |
| 273 | uword *indexp; |
| 274 | segment_manager_t *sm; |
| 275 | |
| 276 | if (srv && hash_get (srv->listeners_table, handle) == 0) |
| 277 | { |
| 278 | clib_warning ("app doesn't own handle %llu!", handle); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | listener = listen_session_get_from_handle (handle); |
| 283 | stream_session_stop_listen (listener); |
| 284 | |
| 285 | indexp = hash_get (srv->listeners_table, handle); |
| 286 | ASSERT (indexp); |
| 287 | |
| 288 | sm = segment_manager_get (*indexp); |
| 289 | segment_manager_del (sm); |
| 290 | hash_unset (srv->listeners_table, handle); |
| 291 | listen_session_del (listener); |
| 292 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 296 | int |
| 297 | application_open_session (application_t * app, session_type_t sst, |
| 298 | transport_endpoint_t * tep, u32 api_context) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 299 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 300 | segment_manager_t *sm; |
| 301 | transport_connection_t *tc = 0; |
| 302 | int rv; |
| 303 | |
| 304 | /* Make sure we have a segment manager for connects */ |
| 305 | if (app->connects_seg_manager == (u32) ~ 0) |
| 306 | { |
| 307 | sm = application_alloc_segment_manager (app); |
| 308 | if (sm == 0) |
| 309 | return -1; |
| 310 | app->connects_seg_manager = segment_manager_index (sm); |
| 311 | } |
| 312 | |
| 313 | if ((rv = stream_session_open (app->index, sst, tep, &tc))) |
| 314 | return rv; |
| 315 | |
| 316 | /* Store api_context for when the reply comes. Not the nicest thing |
| 317 | * but better allocating a separate half-open pool. */ |
| 318 | tc->s_index = api_context; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | segment_manager_t * |
| 324 | application_get_connect_segment_manager (application_t * app) |
| 325 | { |
| 326 | ASSERT (app->connects_seg_manager != (u32) ~ 0); |
| 327 | return segment_manager_get (app->connects_seg_manager); |
| 328 | } |
| 329 | |
| 330 | segment_manager_t * |
| 331 | application_get_listen_segment_manager (application_t * app, |
| 332 | stream_session_t * s) |
| 333 | { |
| 334 | uword *smp; |
| 335 | smp = hash_get (app->listeners_table, listen_session_get_handle (s)); |
| 336 | ASSERT (smp != 0); |
| 337 | return segment_manager_get (*smp); |
| 338 | } |
| 339 | |
| 340 | static u8 * |
| 341 | app_get_name_from_reg_index (application_t * app) |
| 342 | { |
| 343 | u8 *app_name; |
| 344 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | vl_api_registration_t *regp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 346 | regp = vl_api_client_index_to_registration (app->api_client_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 347 | if (!regp) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 348 | app_name = format (0, "builtin-%d%c", app->index, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 349 | else |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 350 | app_name = format (0, "%s%c", regp->name, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 351 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 352 | return app_name; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | u8 * |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 356 | format_application_listener (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 357 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 358 | application_t *app = va_arg (*args, application_t *); |
| 359 | u64 handle = va_arg (*args, u64); |
| 360 | u32 index = va_arg (*args, u32); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 361 | int verbose = va_arg (*args, int); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 362 | stream_session_t *listener; |
| 363 | u8 *app_name, *str; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 364 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 365 | if (app == 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 366 | { |
| 367 | if (verbose) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 368 | s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App", |
| 369 | "API Client", "ListenerID", "SegManager"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 370 | else |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 371 | s = format (s, "%-40s%-20s", "Connection", "App"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 372 | |
| 373 | return s; |
| 374 | } |
| 375 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 376 | app_name = app_get_name_from_reg_index (app); |
| 377 | listener = listen_session_get_from_handle (handle); |
| 378 | str = format (0, "%U", format_stream_session, listener, verbose); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 379 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | if (verbose) |
| 381 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 382 | s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name, |
| 383 | app->api_client_index, handle, index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 384 | } |
| 385 | else |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 386 | s = format (s, "%-40s%-20s", str, app_name); |
| 387 | |
| 388 | vec_free (app_name); |
| 389 | return s; |
| 390 | } |
| 391 | |
| 392 | void |
| 393 | application_format_connects (application_t * app, int verbose) |
| 394 | { |
| 395 | vlib_main_t *vm = vlib_get_main (); |
| 396 | segment_manager_t *sm; |
| 397 | u8 *app_name, *s = 0; |
| 398 | int i, j; |
| 399 | |
| 400 | /* Header */ |
| 401 | if (app == 0) |
| 402 | { |
| 403 | if (verbose) |
| 404 | vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App", |
| 405 | "API Client", "SegManager"); |
| 406 | else |
| 407 | vlib_cli_output (vm, "%-40s%-20s", "Connection", "App"); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | /* make sure */ |
| 412 | if (app->connects_seg_manager == (u32) ~ 0) |
| 413 | return; |
| 414 | |
| 415 | app_name = app_get_name_from_reg_index (app); |
| 416 | |
| 417 | /* Across all fifo segments */ |
| 418 | sm = segment_manager_get (app->connects_seg_manager); |
| 419 | for (j = 0; j < vec_len (sm->segment_indices); j++) |
| 420 | { |
| 421 | svm_fifo_segment_private_t *fifo_segment; |
| 422 | svm_fifo_t **fifos; |
| 423 | u8 *str; |
| 424 | |
| 425 | fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]); |
| 426 | fifos = svm_fifo_segment_get_fifos (fifo_segment); |
| 427 | for (i = 0; i < vec_len (fifos); i++) |
| 428 | { |
| 429 | svm_fifo_t *fifo; |
| 430 | u32 session_index, thread_index; |
| 431 | stream_session_t *session; |
| 432 | |
| 433 | /* There are 2 fifos/session. Avoid printing twice. */ |
| 434 | if (i % 2) |
| 435 | continue; |
| 436 | |
| 437 | fifo = fifos[i]; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 438 | session_index = fifo->master_session_index; |
| 439 | thread_index = fifo->master_thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 440 | |
| 441 | session = stream_session_get (session_index, thread_index); |
| 442 | str = format (0, "%U", format_stream_session, session, verbose); |
| 443 | |
| 444 | if (verbose) |
| 445 | s = format (s, "%-40s%-20s%-15u%-10u", str, app_name, |
| 446 | app->api_client_index, app->connects_seg_manager); |
| 447 | else |
| 448 | s = format (s, "%-40s%-20s", str, app_name); |
| 449 | |
| 450 | vlib_cli_output (vm, "%v", s); |
| 451 | |
| 452 | vec_reset_length (s); |
| 453 | vec_free (str); |
| 454 | } |
| 455 | vec_free (s); |
| 456 | } |
| 457 | |
| 458 | vec_free (app_name); |
| 459 | } |
| 460 | |
| 461 | u8 * |
| 462 | format_application (u8 * s, va_list * args) |
| 463 | { |
| 464 | application_t *app = va_arg (*args, application_t *); |
| 465 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
| 466 | u8 *app_name; |
| 467 | |
| 468 | if (app == 0) |
| 469 | { |
| 470 | if (verbose) |
| 471 | s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s", "Index", "Name", |
| 472 | "API Client", "Add seg size", "Rx fifo size", |
| 473 | "Tx fifo size"); |
| 474 | else |
| 475 | s = format (s, "%-10s%-20s%-20s", "Index", "Name", "API Client"); |
| 476 | return s; |
| 477 | } |
| 478 | |
| 479 | app_name = app_get_name_from_reg_index (app); |
| 480 | if (verbose) |
| 481 | s = format (s, "%-10d%-20s%-15d%-15d%-15d%-15d", app->index, app_name, |
| 482 | app->api_client_index, app->sm_properties.add_segment_size, |
| 483 | app->sm_properties.rx_fifo_size, |
| 484 | app->sm_properties.tx_fifo_size); |
| 485 | else |
| 486 | s = format (s, "%-10d%-20s%-20d", app->index, app_name, |
| 487 | app->api_client_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 488 | return s; |
| 489 | } |
| 490 | |
| 491 | static clib_error_t * |
| 492 | show_app_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 493 | vlib_cli_command_t * cmd) |
| 494 | { |
| 495 | application_t *app; |
| 496 | int do_server = 0; |
| 497 | int do_client = 0; |
| 498 | int verbose = 0; |
| 499 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 500 | if (!session_manager_is_enabled ()) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 501 | { |
| 502 | clib_error_return (0, "session layer is not enabled"); |
| 503 | } |
| 504 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 505 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 506 | { |
| 507 | if (unformat (input, "server")) |
| 508 | do_server = 1; |
| 509 | else if (unformat (input, "client")) |
| 510 | do_client = 1; |
| 511 | else if (unformat (input, "verbose")) |
| 512 | verbose = 1; |
| 513 | else |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | if (do_server) |
| 518 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 519 | u64 handle; |
| 520 | u32 index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 521 | if (pool_elts (app_pool)) |
| 522 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 523 | vlib_cli_output (vm, "%U", format_application_listener, |
| 524 | 0 /* header */ , 0, 0, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 525 | verbose); |
| 526 | /* *INDENT-OFF* */ |
| 527 | pool_foreach (app, app_pool, |
| 528 | ({ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 529 | /* App's listener sessions */ |
| 530 | if (hash_elts (app->listeners_table) == 0) |
| 531 | continue; |
| 532 | hash_foreach (handle, index, app->listeners_table, |
| 533 | ({ |
| 534 | vlib_cli_output (vm, "%U", format_application_listener, app, |
| 535 | handle, index, verbose); |
| 536 | })); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 537 | })); |
| 538 | /* *INDENT-ON* */ |
| 539 | } |
| 540 | else |
| 541 | vlib_cli_output (vm, "No active server bindings"); |
| 542 | } |
| 543 | |
| 544 | if (do_client) |
| 545 | { |
| 546 | if (pool_elts (app_pool)) |
| 547 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 548 | application_format_connects (0, verbose); |
| 549 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 550 | /* *INDENT-OFF* */ |
| 551 | pool_foreach (app, app_pool, |
| 552 | ({ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 553 | if (app->connects_seg_manager == (u32)~0) |
| 554 | continue; |
| 555 | application_format_connects (app, verbose); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 556 | })); |
| 557 | /* *INDENT-ON* */ |
| 558 | } |
| 559 | else |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 560 | vlib_cli_output (vm, "No active client bindings"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 561 | } |
| 562 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 563 | /* Print app related info */ |
| 564 | if (!do_server && !do_client) |
| 565 | { |
| 566 | vlib_cli_output (vm, "%U", format_application, 0, verbose); |
| 567 | pool_foreach (app, app_pool, ( |
| 568 | { |
| 569 | vlib_cli_output (vm, "%U", |
| 570 | format_application, app, |
| 571 | verbose); |
| 572 | } |
| 573 | )); |
| 574 | } |
| 575 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 579 | /* *INDENT-OFF* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 580 | VLIB_CLI_COMMAND (show_app_command, static) = |
| 581 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 582 | .path = "show app", |
| 583 | .short_help = "show app [server|client] [verbose]", |
| 584 | .function = show_app_command_fn, |
| 585 | }; |
| 586 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * fd.io coding-style-patch-verification: ON |
| 590 | * |
| 591 | * Local Variables: |
| 592 | * eval: (c-set-style "gnu") |
| 593 | * End: |
| 594 | */ |