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