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> |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 18 | #include <vnet/session/application_namespace.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | #include <vnet/session/session.h> |
| 20 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 21 | static app_main_t app_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 22 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 23 | static app_listener_t * |
| 24 | app_listener_alloc (application_t * app) |
| 25 | { |
| 26 | app_listener_t *app_listener; |
| 27 | pool_get (app->listeners, app_listener); |
| 28 | memset (app_listener, 0, sizeof (*app_listener)); |
| 29 | app_listener->al_index = app_listener - app->listeners; |
| 30 | return app_listener; |
| 31 | } |
| 32 | |
| 33 | static app_listener_t * |
| 34 | app_listener_get (application_t * app, u32 app_listener_index) |
| 35 | { |
| 36 | return pool_elt_at_index (app->listeners, app_listener_index); |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | app_listener_free (application_t * app, app_listener_t * app_listener) |
| 41 | { |
| 42 | clib_bitmap_free (app_listener->workers); |
| 43 | pool_put (app->listeners, app_listener); |
| 44 | if (CLIB_DEBUG) |
| 45 | memset (app_listener, 0xfa, sizeof (*app_listener)); |
| 46 | } |
| 47 | |
| 48 | static app_listener_t * |
| 49 | app_local_listener_alloc (application_t * app) |
| 50 | { |
| 51 | app_listener_t *app_listener; |
| 52 | pool_get (app->local_listeners, app_listener); |
| 53 | memset (app_listener, 0, sizeof (*app_listener)); |
| 54 | app_listener->al_index = app_listener - app->local_listeners; |
| 55 | return app_listener; |
| 56 | } |
| 57 | |
| 58 | static app_listener_t * |
| 59 | app_local_listener_get (application_t * app, u32 app_listener_index) |
| 60 | { |
| 61 | return pool_elt_at_index (app->local_listeners, app_listener_index); |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | app_local_listener_free (application_t * app, app_listener_t * app_listener) |
| 66 | { |
| 67 | clib_bitmap_free (app_listener->workers); |
| 68 | pool_put (app->local_listeners, app_listener); |
| 69 | if (CLIB_DEBUG) |
| 70 | memset (app_listener, 0xfa, sizeof (*app_listener)); |
| 71 | } |
| 72 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 73 | static app_worker_map_t * |
| 74 | app_worker_map_alloc (application_t * app) |
| 75 | { |
| 76 | app_worker_map_t *map; |
| 77 | pool_get (app->worker_maps, map); |
| 78 | memset (map, 0, sizeof (*map)); |
| 79 | return map; |
| 80 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 81 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 82 | static u32 |
| 83 | app_worker_map_index (application_t * app, app_worker_map_t * map) |
| 84 | { |
| 85 | return (map - app->worker_maps); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | app_worker_map_free (application_t * app, app_worker_map_t * map) |
| 90 | { |
| 91 | pool_put (app->worker_maps, map); |
| 92 | } |
| 93 | |
| 94 | static app_worker_map_t * |
| 95 | app_worker_map_get (application_t * app, u32 map_index) |
| 96 | { |
| 97 | return pool_elt_at_index (app->worker_maps, map_index); |
| 98 | } |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 99 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 100 | static u8 * |
| 101 | app_get_name_from_reg_index (application_t * app) |
| 102 | { |
| 103 | u8 *app_name; |
| 104 | |
| 105 | vl_api_registration_t *regp; |
| 106 | regp = vl_api_client_index_to_registration (app->api_client_index); |
| 107 | if (!regp) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 108 | app_name = format (0, "builtin-%d%c", app->app_index, 0); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 109 | else |
| 110 | app_name = format (0, "%s%c", regp->name, 0); |
| 111 | |
| 112 | return app_name; |
| 113 | } |
| 114 | |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 115 | static u8 * |
| 116 | app_get_name (application_t * app) |
| 117 | { |
| 118 | if (!app->name) |
| 119 | return app_get_name_from_reg_index (app); |
| 120 | return app->name; |
| 121 | } |
| 122 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 123 | u32 |
| 124 | application_session_table (application_t * app, u8 fib_proto) |
| 125 | { |
| 126 | app_namespace_t *app_ns; |
| 127 | app_ns = app_namespace_get (app->ns_index); |
| 128 | if (!application_has_global_scope (app)) |
| 129 | return APP_INVALID_INDEX; |
| 130 | if (fib_proto == FIB_PROTOCOL_IP4) |
| 131 | return session_lookup_get_index_for_fib (fib_proto, |
| 132 | app_ns->ip4_fib_index); |
| 133 | else |
| 134 | return session_lookup_get_index_for_fib (fib_proto, |
| 135 | app_ns->ip6_fib_index); |
| 136 | } |
| 137 | |
| 138 | u32 |
| 139 | application_local_session_table (application_t * app) |
| 140 | { |
| 141 | app_namespace_t *app_ns; |
| 142 | if (!application_has_local_scope (app)) |
| 143 | return APP_INVALID_INDEX; |
| 144 | app_ns = app_namespace_get (app->ns_index); |
| 145 | return app_ns->local_table_index; |
| 146 | } |
| 147 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 148 | static void |
| 149 | application_local_listener_session_endpoint (local_session_t * ll, |
| 150 | session_endpoint_t * sep) |
| 151 | { |
| 152 | sep->transport_proto = |
| 153 | session_type_transport_proto (ll->listener_session_type); |
| 154 | sep->port = ll->port; |
| 155 | sep->is_ip4 = ll->listener_session_type & 1; |
| 156 | } |
| 157 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 158 | int |
| 159 | application_api_queue_is_full (application_t * app) |
| 160 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 161 | svm_queue_t *q; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 162 | |
| 163 | /* builtin servers are always OK */ |
| 164 | if (app->api_client_index == ~0) |
| 165 | return 0; |
| 166 | |
| 167 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 168 | if (!q) |
| 169 | return 1; |
| 170 | |
| 171 | if (q->cursize == q->maxsize) |
| 172 | return 1; |
| 173 | return 0; |
| 174 | } |
| 175 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Returns app name |
| 178 | * |
| 179 | * Since the name is not stored per app, we generate it on the fly. It is |
| 180 | * the caller's responsibility to free the vector |
| 181 | */ |
| 182 | u8 * |
| 183 | application_name_from_index (u32 app_index) |
| 184 | { |
| 185 | application_t *app = application_get (app_index); |
| 186 | if (!app) |
| 187 | return 0; |
| 188 | return app_get_name_from_reg_index (app); |
| 189 | } |
| 190 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 191 | static void |
| 192 | application_table_add (application_t * app) |
| 193 | { |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 194 | if (app->api_client_index != APP_INVALID_INDEX) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 195 | hash_set (app_main.app_by_api_client_index, app->api_client_index, |
| 196 | app->app_index); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 197 | else if (app->name) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 198 | hash_set_mem (app_main.app_by_name, app->name, app->app_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void |
| 202 | application_table_del (application_t * app) |
| 203 | { |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 204 | if (app->api_client_index != APP_INVALID_INDEX) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 205 | hash_unset (app_main.app_by_api_client_index, app->api_client_index); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 206 | else if (app->name) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 207 | hash_unset_mem (app_main.app_by_name, app->name); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | application_t * |
| 211 | application_lookup (u32 api_client_index) |
| 212 | { |
| 213 | uword *p; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 214 | p = hash_get (app_main.app_by_api_client_index, api_client_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 215 | if (p) |
| 216 | return application_get (p[0]); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 221 | application_t * |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 222 | application_lookup_name (const u8 * name) |
| 223 | { |
| 224 | uword *p; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 225 | p = hash_get_mem (app_main.app_by_name, name); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 226 | if (p) |
| 227 | return application_get (p[0]); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | application_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 233 | application_alloc (void) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 234 | { |
| 235 | application_t *app; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 236 | pool_get (app_main.app_pool, app); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 237 | memset (app, 0, sizeof (*app)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 238 | app->app_index = app - app_main.app_pool; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 239 | return app; |
| 240 | } |
| 241 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 242 | application_t * |
| 243 | application_get (u32 app_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 244 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 245 | if (app_index == APP_INVALID_INDEX) |
| 246 | return 0; |
| 247 | return pool_elt_at_index (app_main.app_pool, app_index); |
| 248 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 249 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 250 | application_t * |
| 251 | application_get_if_valid (u32 app_index) |
| 252 | { |
| 253 | if (pool_is_free_index (app_main.app_pool, app_index)) |
| 254 | return 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 255 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 256 | return pool_elt_at_index (app_main.app_pool, app_index); |
| 257 | } |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 258 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 259 | u32 |
| 260 | application_index (application_t * app) |
| 261 | { |
| 262 | return app - app_main.app_pool; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 265 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 266 | application_verify_cb_fns (session_cb_vft_t * cb_fns) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 267 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 268 | if (cb_fns->session_accept_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 269 | clib_warning ("No accept callback function provided"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 270 | if (cb_fns->session_connected_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 271 | clib_warning ("No session connected callback function provided"); |
| 272 | if (cb_fns->session_disconnect_callback == 0) |
| 273 | clib_warning ("No session disconnect callback function provided"); |
| 274 | if (cb_fns->session_reset_callback == 0) |
| 275 | clib_warning ("No session reset callback function provided"); |
| 276 | } |
| 277 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Check app config for given segment type |
| 280 | * |
| 281 | * Returns 1 on success and 0 otherwise |
| 282 | */ |
| 283 | static u8 |
| 284 | application_verify_cfg (ssvm_segment_type_t st) |
| 285 | { |
| 286 | u8 is_valid; |
| 287 | if (st == SSVM_SEGMENT_MEMFD) |
| 288 | { |
| 289 | is_valid = (session_manager_get_evt_q_segment () != 0); |
| 290 | if (!is_valid) |
| 291 | clib_warning ("memfd seg: vpp's event qs IN binary api svm region"); |
| 292 | return is_valid; |
| 293 | } |
| 294 | else if (st == SSVM_SEGMENT_SHM) |
| 295 | { |
| 296 | is_valid = (session_manager_get_evt_q_segment () == 0); |
| 297 | if (!is_valid) |
| 298 | clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region"); |
| 299 | return is_valid; |
| 300 | } |
| 301 | else |
| 302 | return 1; |
| 303 | } |
| 304 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 305 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 306 | application_alloc_and_init (app_init_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 308 | ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 309 | segment_manager_properties_t *props; |
| 310 | vl_api_registration_t *reg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 311 | application_t *app; |
| 312 | u64 *options; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 313 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 314 | app = application_alloc (); |
| 315 | options = a->options; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 316 | /* |
| 317 | * Make sure we support the requested configuration |
| 318 | */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 319 | if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN)) |
| 320 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 321 | reg = vl_api_client_index_to_registration (a->api_client_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 322 | if (!reg) |
| 323 | return VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
| 324 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
| 325 | seg_type = SSVM_SEGMENT_SHM; |
| 326 | } |
| 327 | else |
| 328 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 329 | if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 330 | { |
| 331 | clib_warning ("mq eventfds can only be used if socket transport is " |
| 332 | "used for api"); |
| 333 | return VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
| 334 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 335 | seg_type = SSVM_SEGMENT_PRIVATE; |
| 336 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 337 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 338 | if (!application_verify_cfg (seg_type)) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 339 | return VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 340 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 341 | /* Check that the obvious things are properly set up */ |
| 342 | application_verify_cb_fns (a->session_cb_vft); |
| 343 | |
| 344 | app->api_client_index = a->api_client_index; |
| 345 | app->flags = options[APP_OPTIONS_FLAGS]; |
| 346 | app->cb_fns = *a->session_cb_vft; |
| 347 | app->ns_index = options[APP_OPTIONS_NAMESPACE]; |
| 348 | app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT]; |
| 349 | app->name = vec_dup (a->name); |
| 350 | |
| 351 | /* If no scope enabled, default to global */ |
| 352 | if (!application_has_global_scope (app) |
| 353 | && !application_has_local_scope (app)) |
| 354 | app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 355 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 356 | props = application_segment_manager_properties (app); |
| 357 | segment_manager_properties_init (props); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 358 | props->segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE]; |
| 359 | props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS]; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 360 | if (options[APP_OPTIONS_ADD_SEGMENT_SIZE]) |
| 361 | { |
| 362 | props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE]; |
| 363 | props->add_segment = 1; |
| 364 | } |
| 365 | if (options[APP_OPTIONS_RX_FIFO_SIZE]) |
| 366 | props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE]; |
| 367 | if (options[APP_OPTIONS_TX_FIFO_SIZE]) |
| 368 | props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE]; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 369 | if (options[APP_OPTIONS_EVT_QUEUE_SIZE]) |
| 370 | props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE]; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 371 | if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 372 | props->use_mq_eventfd = 1; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 373 | if (options[APP_OPTIONS_TLS_ENGINE]) |
| 374 | app->tls_engine = options[APP_OPTIONS_TLS_ENGINE]; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 375 | props->segment_type = seg_type; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 376 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 377 | /* Add app to lookup by api_client_index table */ |
| 378 | application_table_add (app); |
| 379 | a->app_index = application_index (app); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 380 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 381 | APP_DBG ("New app name: %v api index: %u index %u", app->name, |
| 382 | app->api_client_index, app->app_index); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | void |
| 388 | application_free (application_t * app) |
| 389 | { |
| 390 | app_worker_map_t *wrk_map; |
| 391 | app_worker_t *app_wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 392 | u32 table_index; |
| 393 | local_session_t *ll; |
| 394 | session_endpoint_t sep; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | * The app event queue allocated in first segment is cleared with |
| 398 | * the segment manager. No need to explicitly free it. |
| 399 | */ |
| 400 | APP_DBG ("Delete app name %v api index: %d index: %d", app->name, |
| 401 | app->api_client_index, app->app_index); |
| 402 | |
| 403 | if (application_is_proxy (app)) |
| 404 | application_remove_proxy (app); |
| 405 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 406 | /* |
| 407 | * Free workers |
| 408 | */ |
| 409 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 410 | /* *INDENT-OFF* */ |
| 411 | pool_flush (wrk_map, app->worker_maps, ({ |
| 412 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 413 | app_worker_free (app_wrk); |
| 414 | })); |
| 415 | /* *INDENT-ON* */ |
| 416 | pool_free (app->worker_maps); |
| 417 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 418 | /* |
| 419 | * Free local listeners. Global table unbinds stop local listeners |
| 420 | * as well, but if we have only local binds, these won't be cleaned up. |
| 421 | * Don't bother with local accepted sessions, we clean them when |
| 422 | * cleaning up the worker. |
| 423 | */ |
| 424 | table_index = application_local_session_table (app); |
| 425 | /* *INDENT-OFF* */ |
| 426 | pool_foreach (ll, app->local_listen_sessions, ({ |
| 427 | application_local_listener_session_endpoint (ll, &sep); |
| 428 | session_lookup_del_session_endpoint (table_index, &sep); |
| 429 | })); |
| 430 | /* *INDENT-ON* */ |
| 431 | pool_free (app->local_listen_sessions); |
| 432 | |
| 433 | /* |
| 434 | * Cleanup remaining state |
| 435 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 436 | application_table_del (app); |
| 437 | vec_free (app->name); |
| 438 | vec_free (app->tls_cert); |
| 439 | vec_free (app->tls_key); |
| 440 | pool_put (app_main.app_pool, app); |
| 441 | } |
| 442 | |
| 443 | app_worker_t * |
| 444 | application_get_worker (application_t * app, u32 wrk_map_index) |
| 445 | { |
| 446 | app_worker_map_t *map; |
| 447 | map = app_worker_map_get (app, wrk_map_index); |
| 448 | if (!map) |
| 449 | return 0; |
| 450 | return app_worker_get (map->wrk_index); |
| 451 | } |
| 452 | |
| 453 | app_worker_t * |
| 454 | application_get_default_worker (application_t * app) |
| 455 | { |
| 456 | return application_get_worker (app, 0); |
| 457 | } |
| 458 | |
| 459 | app_worker_t * |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 460 | application_listener_select_worker (stream_session_t * ls, u8 is_local) |
| 461 | { |
| 462 | app_listener_t *app_listener; |
| 463 | application_t *app; |
| 464 | u32 wrk_index; |
| 465 | |
| 466 | app = application_get (ls->app_index); |
| 467 | if (!is_local) |
| 468 | app_listener = app_listener_get (app, ls->listener_db_index); |
| 469 | else |
| 470 | app_listener = app_local_listener_get (app, ls->listener_db_index); |
| 471 | |
| 472 | wrk_index = clib_bitmap_next_set (app_listener->workers, |
| 473 | app_listener->accept_rotor + 1); |
| 474 | if (wrk_index == ~0) |
| 475 | wrk_index = clib_bitmap_first_set (app_listener->workers); |
| 476 | |
| 477 | ASSERT (wrk_index != ~0); |
| 478 | app_listener->accept_rotor = wrk_index; |
| 479 | return application_get_worker (app, wrk_index); |
| 480 | } |
| 481 | |
| 482 | app_worker_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 483 | app_worker_alloc (application_t * app) |
| 484 | { |
| 485 | app_worker_t *app_wrk; |
| 486 | pool_get (app_main.workers, app_wrk); |
| 487 | memset (app_wrk, 0, sizeof (*app_wrk)); |
| 488 | app_wrk->wrk_index = app_wrk - app_main.workers; |
| 489 | app_wrk->app_index = app->app_index; |
| 490 | app_wrk->wrk_map_index = ~0; |
| 491 | app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; |
| 492 | app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; |
| 493 | app_wrk->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; |
| 494 | APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index); |
| 495 | return app_wrk; |
| 496 | } |
| 497 | |
| 498 | app_worker_t * |
| 499 | app_worker_get (u32 wrk_index) |
| 500 | { |
| 501 | return pool_elt_at_index (app_main.workers, wrk_index); |
| 502 | } |
| 503 | |
| 504 | app_worker_t * |
| 505 | app_worker_get_if_valid (u32 wrk_index) |
| 506 | { |
| 507 | if (pool_is_free_index (app_main.workers, wrk_index)) |
| 508 | return 0; |
| 509 | return pool_elt_at_index (app_main.workers, wrk_index); |
| 510 | } |
| 511 | |
| 512 | void |
| 513 | app_worker_free (app_worker_t * app_wrk) |
| 514 | { |
| 515 | application_t *app = application_get (app_wrk->app_index); |
| 516 | vnet_unbind_args_t _a, *a = &_a; |
| 517 | u64 handle, *handles = 0; |
| 518 | segment_manager_t *sm; |
| 519 | u32 sm_index; |
| 520 | int i; |
| 521 | |
| 522 | /* |
| 523 | * Listener cleanup |
| 524 | */ |
| 525 | |
| 526 | /* *INDENT-OFF* */ |
| 527 | hash_foreach (handle, sm_index, app_wrk->listeners_table, |
| 528 | ({ |
| 529 | vec_add1 (handles, handle); |
| 530 | sm = segment_manager_get (sm_index); |
| 531 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
| 532 | })); |
| 533 | /* *INDENT-ON* */ |
| 534 | |
| 535 | for (i = 0; i < vec_len (handles); i++) |
| 536 | { |
| 537 | a->app_index = app->app_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 538 | a->wrk_map_index = app_wrk->wrk_map_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 539 | a->handle = handles[i]; |
| 540 | /* seg manager is removed when unbind completes */ |
| 541 | vnet_unbind (a); |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Connects segment manager cleanup |
| 546 | */ |
| 547 | |
| 548 | if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX) |
| 549 | { |
| 550 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
| 551 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
| 552 | segment_manager_init_del (sm); |
| 553 | } |
| 554 | |
| 555 | /* If first segment manager is used by a listener */ |
| 556 | if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX |
| 557 | && app_wrk->first_segment_manager != app_wrk->connects_seg_manager) |
| 558 | { |
| 559 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 560 | /* .. and has no fifos, e.g. it might be used for redirected sessions, |
| 561 | * remove it */ |
| 562 | if (!segment_manager_has_fifos (sm)) |
| 563 | { |
| 564 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
| 565 | segment_manager_del (sm); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Local sessions |
| 571 | */ |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 572 | app_worker_local_sessions_free (app_wrk); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 573 | |
| 574 | pool_put (app_main.workers, app_wrk); |
| 575 | if (CLIB_DEBUG) |
| 576 | memset (app_wrk, 0xfe, sizeof (*app_wrk)); |
| 577 | } |
| 578 | |
| 579 | int |
| 580 | app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk) |
| 581 | { |
| 582 | app_worker_map_t *wrk_map; |
| 583 | app_worker_t *app_wrk; |
| 584 | segment_manager_t *sm; |
| 585 | int rv; |
| 586 | |
| 587 | app_wrk = app_worker_alloc (app); |
| 588 | wrk_map = app_worker_map_alloc (app); |
| 589 | wrk_map->wrk_index = app_wrk->wrk_index; |
| 590 | app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map); |
| 591 | |
| 592 | /* |
| 593 | * Setup first segment manager |
| 594 | */ |
| 595 | sm = segment_manager_new (); |
| 596 | sm->app_wrk_index = app_wrk->wrk_index; |
| 597 | |
| 598 | if ((rv = segment_manager_init (sm, app->sm_properties.segment_size, |
| 599 | app->sm_properties.prealloc_fifos))) |
| 600 | { |
| 601 | app_worker_free (app_wrk); |
| 602 | return rv; |
| 603 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 604 | sm->first_is_protected = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 605 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 606 | /* |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 607 | * Setup app worker |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 608 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 609 | app_wrk->first_segment_manager = segment_manager_index (sm); |
| 610 | app_wrk->listeners_table = hash_create (0, sizeof (u64)); |
| 611 | app_wrk->event_queue = segment_manager_event_queue (sm); |
| 612 | app_wrk->app_is_builtin = application_is_builtin (app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 613 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 614 | /* |
| 615 | * Segment manager for local sessions |
| 616 | */ |
| 617 | sm = segment_manager_new (); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 618 | sm->app_wrk_index = app_wrk->wrk_index; |
| 619 | app_wrk->local_segment_manager = segment_manager_index (sm); |
| 620 | app_wrk->local_connects = hash_create (0, sizeof (u64)); |
| 621 | |
| 622 | *wrk = app_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 623 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 624 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 627 | application_t * |
| 628 | app_worker_get_app (u32 wrk_index) |
| 629 | { |
| 630 | app_worker_t *app_wrk; |
| 631 | app_wrk = app_worker_get_if_valid (wrk_index); |
| 632 | if (!app_wrk) |
| 633 | return 0; |
| 634 | return application_get_if_valid (app_wrk->app_index); |
| 635 | } |
| 636 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 637 | static segment_manager_t * |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 638 | app_worker_alloc_segment_manager (app_worker_t * app_wrk) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 639 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 640 | segment_manager_t *sm = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 641 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 642 | /* If the first segment manager is not in use, don't allocate a new one */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 643 | if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX |
| 644 | && app_wrk->first_segment_manager_in_use == 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 645 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 646 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 647 | app_wrk->first_segment_manager_in_use = 1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 648 | return sm; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 649 | } |
| 650 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 651 | sm = segment_manager_new (); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 652 | sm->app_wrk_index = app_wrk->wrk_index; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 653 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 654 | return sm; |
| 655 | } |
| 656 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 657 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 658 | app_worker_start_listen (app_worker_t * app_wrk, stream_session_t * ls) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 659 | { |
| 660 | segment_manager_t *sm; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 661 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 662 | /* Allocate segment manager. All sessions derived out of a listen session |
| 663 | * have fifos allocated by the same segment manager. */ |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 664 | if (!(sm = app_worker_alloc_segment_manager (app_wrk))) |
| 665 | return -1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 666 | |
| 667 | /* Add to app's listener table. Useful to find all child listeners |
| 668 | * when app goes down, although, just for unbinding this is not needed */ |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 669 | hash_set (app_wrk->listeners_table, listen_session_get_handle (ls), |
| 670 | segment_manager_index (sm)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 671 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 672 | if (!ls->server_rx_fifo |
| 673 | && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 674 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 675 | if (session_alloc_fifos (sm, ls)) |
| 676 | return -1; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 677 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 678 | return 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 681 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 682 | app_worker_stop_listen (app_worker_t * app_wrk, session_handle_t handle) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 683 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 684 | segment_manager_t *sm; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 685 | uword *sm_indexp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 686 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 687 | sm_indexp = hash_get (app_wrk->listeners_table, handle); |
| 688 | if (PREDICT_FALSE (!sm_indexp)) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 689 | { |
| 690 | clib_warning ("listener handle was removed %llu!", handle); |
| 691 | return -1; |
| 692 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 693 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 694 | sm = segment_manager_get (*sm_indexp); |
| 695 | if (app_wrk->first_segment_manager == *sm_indexp) |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 696 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 697 | /* Delete sessions but don't remove segment manager */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 698 | app_wrk->first_segment_manager_in_use = 0; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 699 | segment_manager_del_sessions (sm); |
| 700 | } |
| 701 | else |
| 702 | { |
| 703 | segment_manager_init_del (sm); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 704 | } |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 705 | hash_unset (app_wrk->listeners_table, handle); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Start listening local transport endpoint for requested transport. |
| 712 | * |
| 713 | * Creates a 'dummy' stream session with state LISTENING to be used in session |
| 714 | * lookups, prior to establishing connection. Requests transport to build |
| 715 | * it's own specific listening connection. |
| 716 | */ |
| 717 | int |
| 718 | application_start_listen (application_t * app, |
| 719 | session_endpoint_extended_t * sep_ext, |
| 720 | session_handle_t * res) |
| 721 | { |
| 722 | app_listener_t *app_listener; |
| 723 | u32 table_index, fib_proto; |
| 724 | session_endpoint_t *sep; |
| 725 | app_worker_t *app_wrk; |
| 726 | stream_session_t *ls; |
| 727 | session_handle_t lh; |
| 728 | session_type_t sst; |
| 729 | |
| 730 | /* |
| 731 | * Check if sep is already listened on |
| 732 | */ |
| 733 | sep = (session_endpoint_t *) sep_ext; |
| 734 | fib_proto = session_endpoint_fib_proto (sep); |
| 735 | table_index = application_session_table (app, fib_proto); |
| 736 | lh = session_lookup_endpoint_listener (table_index, sep, 1); |
| 737 | if (lh != SESSION_INVALID_HANDLE) |
| 738 | { |
| 739 | ls = listen_session_get_from_handle (lh); |
| 740 | if (ls->app_index != app->app_index) |
| 741 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 742 | |
| 743 | app_wrk = app_worker_get (sep_ext->app_wrk_index); |
| 744 | if (ls->app_wrk_index == app_wrk->wrk_index) |
| 745 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 746 | |
| 747 | if (app_worker_start_listen (app_wrk, ls)) |
| 748 | return -1; |
| 749 | |
| 750 | app_listener = app_listener_get (app, ls->listener_db_index); |
| 751 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 752 | app_wrk->wrk_map_index, 1); |
| 753 | |
| 754 | *res = listen_session_get_handle (ls); |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Allocate new listener for application |
| 760 | */ |
| 761 | sst = session_type_from_proto_and_ip (sep_ext->transport_proto, |
| 762 | sep_ext->is_ip4); |
| 763 | ls = listen_session_new (0, sst); |
| 764 | ls->app_index = app->app_index; |
Florin Coras | 74cac88 | 2018-09-07 09:13:15 -0700 | [diff] [blame^] | 765 | lh = listen_session_get_handle (ls); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 766 | if (session_listen (ls, sep_ext)) |
| 767 | goto err; |
| 768 | |
Florin Coras | 74cac88 | 2018-09-07 09:13:15 -0700 | [diff] [blame^] | 769 | |
| 770 | ls = listen_session_get_from_handle (lh); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 771 | app_listener = app_listener_alloc (app); |
| 772 | ls->listener_db_index = app_listener->al_index; |
| 773 | |
| 774 | /* |
| 775 | * Setup app worker as a listener |
| 776 | */ |
| 777 | app_wrk = app_worker_get (sep_ext->app_wrk_index); |
| 778 | ls->app_wrk_index = app_wrk->wrk_index; |
| 779 | if (app_worker_start_listen (app_wrk, ls)) |
| 780 | goto err; |
| 781 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 782 | app_wrk->wrk_map_index, 1); |
| 783 | |
Florin Coras | 74cac88 | 2018-09-07 09:13:15 -0700 | [diff] [blame^] | 784 | *res = lh; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 785 | return 0; |
| 786 | |
| 787 | err: |
| 788 | listen_session_del (ls); |
| 789 | return -1; |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Stop listening on session associated to handle |
| 794 | * |
| 795 | * @param handle listener handle |
| 796 | * @param app_index index of the app owning the handle. |
| 797 | * @param app_wrk_index index of the worker requesting the stop |
| 798 | */ |
| 799 | int |
| 800 | application_stop_listen (u32 app_index, u32 app_wrk_index, |
| 801 | session_handle_t handle) |
| 802 | { |
| 803 | app_listener_t *app_listener; |
| 804 | stream_session_t *listener; |
| 805 | app_worker_t *app_wrk; |
| 806 | application_t *app; |
| 807 | |
| 808 | listener = listen_session_get_from_handle (handle); |
| 809 | app = application_get (app_index); |
| 810 | if (PREDICT_FALSE (!app || app->app_index != listener->app_index)) |
| 811 | { |
| 812 | clib_warning ("app doesn't own handle %llu!", handle); |
| 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | app_listener = app_listener_get (app, listener->listener_db_index); |
| 817 | if (!clib_bitmap_get (app_listener->workers, app_wrk_index)) |
| 818 | { |
| 819 | clib_warning ("worker not listening on handle %lu", handle); |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | app_wrk = application_get_worker (app, app_wrk_index); |
| 824 | app_worker_stop_listen (app_wrk, handle); |
| 825 | clib_bitmap_set_no_check (app_listener->workers, app_wrk_index, 0); |
| 826 | |
| 827 | if (clib_bitmap_is_zero (app_listener->workers)) |
| 828 | { |
| 829 | session_stop_listen (listener); |
| 830 | app_listener_free (app, app_listener); |
| 831 | listen_session_del (listener); |
| 832 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 833 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 837 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 838 | app_worker_open_session (app_worker_t * app, session_endpoint_t * sep, |
| 839 | u32 api_context) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 840 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 841 | int rv; |
| 842 | |
| 843 | /* Make sure we have a segment manager for connects */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 844 | app_worker_alloc_connects_segment_manager (app); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 845 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 846 | if ((rv = session_open (app->wrk_index, sep, api_context))) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 847 | return rv; |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 853 | app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 854 | { |
| 855 | segment_manager_t *sm; |
| 856 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 857 | if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 858 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 859 | sm = app_worker_alloc_segment_manager (app_wrk); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 860 | if (sm == 0) |
| 861 | return -1; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 862 | app_wrk->connects_seg_manager = segment_manager_index (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 863 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | segment_manager_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 868 | app_worker_get_connect_segment_manager (app_worker_t * app) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 869 | { |
| 870 | ASSERT (app->connects_seg_manager != (u32) ~ 0); |
| 871 | return segment_manager_get (app->connects_seg_manager); |
| 872 | } |
| 873 | |
| 874 | segment_manager_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 875 | app_worker_get_listen_segment_manager (app_worker_t * app, |
| 876 | stream_session_t * s) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 877 | { |
| 878 | uword *smp; |
| 879 | smp = hash_get (app->listeners_table, listen_session_get_handle (s)); |
| 880 | ASSERT (smp != 0); |
| 881 | return segment_manager_get (*smp); |
| 882 | } |
| 883 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 884 | clib_error_t * |
| 885 | vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a) |
| 886 | { |
| 887 | svm_fifo_segment_private_t *fs; |
| 888 | app_worker_map_t *wrk_map; |
| 889 | app_worker_t *app_wrk; |
| 890 | segment_manager_t *sm; |
| 891 | application_t *app; |
| 892 | int rv; |
| 893 | |
| 894 | app = application_get (a->app_index); |
| 895 | if (!app) |
| 896 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 897 | "App %u does not exist", a->app_index); |
| 898 | |
| 899 | if (a->is_add) |
| 900 | { |
| 901 | if ((rv = app_worker_alloc_and_init (app, &app_wrk))) |
| 902 | return clib_error_return_code (0, rv, 0, "app wrk init: %d", rv); |
| 903 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 904 | fs = segment_manager_get_segment_w_lock (sm, 0); |
| 905 | a->segment = &fs->ssvm; |
| 906 | segment_manager_segment_reader_unlock (sm); |
| 907 | a->evt_q = app_wrk->event_queue; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 908 | a->wrk_index = app_wrk->wrk_map_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 909 | } |
| 910 | else |
| 911 | { |
| 912 | wrk_map = app_worker_map_get (app, a->wrk_index); |
| 913 | if (!wrk_map) |
| 914 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 915 | "App %u does not have worker %u", |
| 916 | app->app_index, a->wrk_index); |
| 917 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 918 | app_worker_map_free (app, wrk_map); |
| 919 | if (!app_wrk) |
| 920 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 921 | "No worker %u", a->wrk_index); |
| 922 | app_worker_free (app_wrk); |
| 923 | } |
| 924 | return 0; |
| 925 | } |
| 926 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 927 | segment_manager_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 928 | application_get_local_segment_manager (app_worker_t * app) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 929 | { |
| 930 | return segment_manager_get (app->local_segment_manager); |
| 931 | } |
| 932 | |
| 933 | segment_manager_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 934 | application_get_local_segment_manager_w_session (app_worker_t * app, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 935 | local_session_t * ls) |
| 936 | { |
| 937 | stream_session_t *listener; |
| 938 | if (application_local_session_listener_has_transport (ls)) |
| 939 | { |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 940 | listener = listen_session_get (ls->listener_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 941 | return app_worker_get_listen_segment_manager (app, listener); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 942 | } |
| 943 | return segment_manager_get (app->local_segment_manager); |
| 944 | } |
| 945 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 946 | int |
| 947 | application_is_proxy (application_t * app) |
| 948 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 949 | return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY); |
| 950 | } |
| 951 | |
| 952 | int |
| 953 | application_is_builtin (application_t * app) |
| 954 | { |
| 955 | return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN); |
| 956 | } |
| 957 | |
| 958 | int |
| 959 | application_is_builtin_proxy (application_t * app) |
| 960 | { |
| 961 | return (application_is_proxy (app) && application_is_builtin (app)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 962 | } |
| 963 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 964 | u8 |
| 965 | application_has_local_scope (application_t * app) |
| 966 | { |
| 967 | return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 968 | } |
| 969 | |
| 970 | u8 |
| 971 | application_has_global_scope (application_t * app) |
| 972 | { |
| 973 | return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 974 | } |
| 975 | |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 976 | u8 |
| 977 | application_use_mq_for_ctrl (application_t * app) |
| 978 | { |
| 979 | return app->flags & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS; |
| 980 | } |
| 981 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 982 | /** |
| 983 | * Send an API message to the external app, to map new segment |
| 984 | */ |
| 985 | int |
| 986 | app_worker_add_segment_notify (u32 app_wrk_index, ssvm_private_t * fs) |
| 987 | { |
| 988 | app_worker_t *app_wrk = app_worker_get (app_wrk_index); |
| 989 | application_t *app = application_get (app_wrk->app_index); |
| 990 | return app->cb_fns.add_segment_callback (app->api_client_index, fs); |
| 991 | } |
| 992 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 993 | u32 |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 994 | application_n_listeners (app_worker_t * app) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 995 | { |
| 996 | return hash_elts (app->listeners_table); |
| 997 | } |
| 998 | |
| 999 | stream_session_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1000 | app_worker_first_listener (app_worker_t * app, u8 fib_proto, |
| 1001 | u8 transport_proto) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1002 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1003 | stream_session_t *listener; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1004 | u64 handle; |
| 1005 | u32 sm_index; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1006 | u8 sst; |
| 1007 | |
| 1008 | sst = session_type_from_proto_and_ip (transport_proto, |
| 1009 | fib_proto == FIB_PROTOCOL_IP4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1010 | |
| 1011 | /* *INDENT-OFF* */ |
| 1012 | hash_foreach (handle, sm_index, app->listeners_table, ({ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1013 | listener = listen_session_get_from_handle (handle); |
Florin Coras | c3ddea8 | 2017-11-27 03:12:00 -0800 | [diff] [blame] | 1014 | if (listener->session_type == sst |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1015 | && listener->enqueue_epoch != SESSION_PROXY_LISTENER_INDEX) |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1016 | return listener; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1017 | })); |
| 1018 | /* *INDENT-ON* */ |
| 1019 | |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1023 | u8 |
| 1024 | app_worker_application_is_builtin (app_worker_t * app_wrk) |
| 1025 | { |
| 1026 | return app_wrk->app_is_builtin; |
| 1027 | } |
| 1028 | |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1029 | stream_session_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1030 | application_proxy_listener (app_worker_t * app, u8 fib_proto, |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1031 | u8 transport_proto) |
| 1032 | { |
| 1033 | stream_session_t *listener; |
| 1034 | u64 handle; |
| 1035 | u32 sm_index; |
| 1036 | u8 sst; |
| 1037 | |
| 1038 | sst = session_type_from_proto_and_ip (transport_proto, |
| 1039 | fib_proto == FIB_PROTOCOL_IP4); |
| 1040 | |
| 1041 | /* *INDENT-OFF* */ |
| 1042 | hash_foreach (handle, sm_index, app->listeners_table, ({ |
| 1043 | listener = listen_session_get_from_handle (handle); |
| 1044 | if (listener->session_type == sst |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1045 | && listener->enqueue_epoch == SESSION_PROXY_LISTENER_INDEX) |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1046 | return listener; |
| 1047 | })); |
| 1048 | /* *INDENT-ON* */ |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1053 | static clib_error_t * |
| 1054 | application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto, |
| 1055 | u8 transport_proto, u8 is_start) |
| 1056 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1057 | app_namespace_t *app_ns = app_namespace_get (app->ns_index); |
| 1058 | u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1059 | session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1060 | transport_connection_t *tc; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1061 | app_worker_t *app_wrk; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1062 | stream_session_t *s; |
| 1063 | u64 handle; |
| 1064 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1065 | /* TODO decide if we want proxy to be enabled for all workers */ |
| 1066 | app_wrk = application_get_default_worker (app); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1067 | if (is_start) |
| 1068 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1069 | s = app_worker_first_listener (app_wrk, fib_proto, transport_proto); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1070 | if (!s) |
| 1071 | { |
| 1072 | sep.is_ip4 = is_ip4; |
| 1073 | sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1074 | sep.sw_if_index = app_ns->sw_if_index; |
| 1075 | sep.transport_proto = transport_proto; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1076 | sep.app_wrk_index = app_wrk->wrk_index; /* only default */ |
| 1077 | application_start_listen (app, &sep, &handle); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1078 | s = listen_session_get_from_handle (handle); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1079 | s->enqueue_epoch = SESSION_PROXY_LISTENER_INDEX; |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1080 | } |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1081 | } |
| 1082 | else |
| 1083 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1084 | s = application_proxy_listener (app_wrk, fib_proto, transport_proto); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1085 | ASSERT (s); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1086 | } |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1087 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1088 | tc = listen_session_get_transport (s); |
| 1089 | |
| 1090 | if (!ip_is_zero (&tc->lcl_ip, 1)) |
| 1091 | { |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 1092 | u32 sti; |
| 1093 | sep.is_ip4 = is_ip4; |
| 1094 | sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1095 | sep.transport_proto = transport_proto; |
| 1096 | sep.port = 0; |
| 1097 | sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1098 | if (is_start) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1099 | session_lookup_add_session_endpoint (sti, |
| 1100 | (session_endpoint_t *) & sep, |
| 1101 | s->session_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1102 | else |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1103 | session_lookup_del_session_endpoint (sti, |
| 1104 | (session_endpoint_t *) & sep); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1105 | } |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1106 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1110 | static void |
| 1111 | application_start_stop_proxy_local_scope (application_t * app, |
| 1112 | u8 transport_proto, u8 is_start) |
| 1113 | { |
| 1114 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
| 1115 | app_namespace_t *app_ns; |
| 1116 | app_ns = app_namespace_get (app->ns_index); |
| 1117 | sep.is_ip4 = 1; |
| 1118 | sep.transport_proto = transport_proto; |
| 1119 | sep.port = 0; |
| 1120 | |
| 1121 | if (is_start) |
| 1122 | { |
| 1123 | session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1124 | app->app_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1125 | sep.is_ip4 = 0; |
| 1126 | session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1127 | app->app_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1128 | } |
| 1129 | else |
| 1130 | { |
| 1131 | session_lookup_del_session_endpoint (app_ns->local_table_index, &sep); |
| 1132 | sep.is_ip4 = 0; |
| 1133 | session_lookup_del_session_endpoint (app_ns->local_table_index, &sep); |
| 1134 | } |
| 1135 | } |
| 1136 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1137 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1138 | application_start_stop_proxy (application_t * app, |
| 1139 | transport_proto_t transport_proto, u8 is_start) |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1140 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1141 | if (application_has_local_scope (app)) |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1142 | application_start_stop_proxy_local_scope (app, transport_proto, is_start); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1143 | |
| 1144 | if (application_has_global_scope (app)) |
| 1145 | { |
| 1146 | application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4, |
| 1147 | transport_proto, is_start); |
| 1148 | application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6, |
| 1149 | transport_proto, is_start); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | void |
| 1154 | application_setup_proxy (application_t * app) |
| 1155 | { |
| 1156 | u16 transports = app->proxied_transports; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1157 | transport_proto_t tp; |
| 1158 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1159 | ASSERT (application_is_proxy (app)); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1160 | |
| 1161 | /* *INDENT-OFF* */ |
| 1162 | transport_proto_foreach (tp, ({ |
| 1163 | if (transports & (1 << tp)) |
| 1164 | application_start_stop_proxy (app, tp, 1); |
| 1165 | })); |
| 1166 | /* *INDENT-ON* */ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | void |
| 1170 | application_remove_proxy (application_t * app) |
| 1171 | { |
| 1172 | u16 transports = app->proxied_transports; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1173 | transport_proto_t tp; |
| 1174 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1175 | ASSERT (application_is_proxy (app)); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1176 | |
| 1177 | /* *INDENT-OFF* */ |
| 1178 | transport_proto_foreach (tp, ({ |
| 1179 | if (transports & (1 << tp)) |
| 1180 | application_start_stop_proxy (app, tp, 0); |
| 1181 | })); |
| 1182 | /* *INDENT-ON* */ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1185 | segment_manager_properties_t * |
| 1186 | application_segment_manager_properties (application_t * app) |
| 1187 | { |
| 1188 | return &app->sm_properties; |
| 1189 | } |
| 1190 | |
| 1191 | segment_manager_properties_t * |
| 1192 | application_get_segment_manager_properties (u32 app_index) |
| 1193 | { |
| 1194 | application_t *app = application_get (app_index); |
| 1195 | return &app->sm_properties; |
| 1196 | } |
| 1197 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1198 | static inline int |
| 1199 | app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock) |
| 1200 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1201 | if (PREDICT_FALSE (svm_msg_q_is_full (mq))) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1202 | { |
| 1203 | clib_warning ("evt q full"); |
| 1204 | svm_msg_q_free_msg (mq, msg); |
| 1205 | if (lock) |
| 1206 | svm_msg_q_unlock (mq); |
| 1207 | return -1; |
| 1208 | } |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1209 | |
| 1210 | if (lock) |
| 1211 | { |
| 1212 | svm_msg_q_add_and_unlock (mq, msg); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /* Even when not locking the ring, we must wait for queue mutex */ |
| 1217 | if (svm_msg_q_add (mq, msg, SVM_Q_WAIT)) |
| 1218 | { |
| 1219 | clib_warning ("msg q add returned"); |
| 1220 | return -1; |
| 1221 | } |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | static inline int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1226 | app_send_io_evt_rx (app_worker_t * app_wrk, stream_session_t * s, u8 lock) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1227 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1228 | session_event_t *evt; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1229 | svm_msg_q_msg_t msg; |
| 1230 | svm_msg_q_t *mq; |
| 1231 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1232 | if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY |
| 1233 | && s->session_state != SESSION_STATE_LISTENING)) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1234 | { |
| 1235 | /* Session is closed so app will never clean up. Flush rx fifo */ |
Florin Coras | 8409955 | 2018-07-22 12:59:30 -0700 | [diff] [blame] | 1236 | if (s->session_state == SESSION_STATE_CLOSED) |
| 1237 | svm_fifo_dequeue_drop_all (s->server_rx_fifo); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
| 1240 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1241 | if (app_worker_application_is_builtin (app_wrk)) |
| 1242 | { |
| 1243 | application_t *app = application_get (app_wrk->app_index); |
| 1244 | return app->cb_fns.builtin_app_rx_callback (s); |
| 1245 | } |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1246 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1247 | if (svm_fifo_has_event (s->server_rx_fifo) |
| 1248 | || svm_fifo_is_empty (s->server_rx_fifo)) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1249 | return 0; |
| 1250 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1251 | mq = app_wrk->event_queue; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1252 | if (lock) |
| 1253 | svm_msg_q_lock (mq); |
| 1254 | |
| 1255 | if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))) |
| 1256 | { |
| 1257 | clib_warning ("evt q rings full"); |
| 1258 | if (lock) |
| 1259 | svm_msg_q_unlock (mq); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
| 1263 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 1264 | ASSERT (!svm_msg_q_msg_is_invalid (&msg)); |
| 1265 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1266 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1267 | evt->fifo = s->server_rx_fifo; |
| 1268 | evt->event_type = FIFO_EVENT_APP_RX; |
| 1269 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1270 | if (app_enqueue_evt (mq, &msg, lock)) |
| 1271 | return -1; |
Florin Coras | d3ca8ff | 2018-07-28 04:53:30 -0700 | [diff] [blame] | 1272 | (void) svm_fifo_set_event (s->server_rx_fifo); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1273 | return 0; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | static inline int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1277 | app_send_io_evt_tx (app_worker_t * app_wrk, stream_session_t * s, u8 lock) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1278 | { |
| 1279 | svm_msg_q_t *mq; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1280 | session_event_t *evt; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1281 | svm_msg_q_msg_t msg; |
| 1282 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1283 | if (app_worker_application_is_builtin (app_wrk)) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1284 | return 0; |
| 1285 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1286 | mq = app_wrk->event_queue; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1287 | if (lock) |
| 1288 | svm_msg_q_lock (mq); |
| 1289 | |
| 1290 | if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))) |
| 1291 | { |
| 1292 | clib_warning ("evt q rings full"); |
| 1293 | if (lock) |
| 1294 | svm_msg_q_unlock (mq); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
| 1298 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 1299 | ASSERT (!svm_msg_q_msg_is_invalid (&msg)); |
| 1300 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1301 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1302 | evt->event_type = FIFO_EVENT_APP_TX; |
| 1303 | evt->fifo = s->server_tx_fifo; |
| 1304 | |
| 1305 | return app_enqueue_evt (mq, &msg, lock); |
| 1306 | } |
| 1307 | |
| 1308 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1309 | typedef int (app_send_evt_handler_fn) (app_worker_t *app, |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1310 | stream_session_t *s, |
| 1311 | u8 lock); |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1312 | static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1313 | app_send_io_evt_rx, |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 1314 | 0, |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1315 | app_send_io_evt_tx, |
| 1316 | }; |
| 1317 | /* *INDENT-ON* */ |
| 1318 | |
| 1319 | /** |
| 1320 | * Send event to application |
| 1321 | * |
| 1322 | * Logic from queue perspective is non-blocking. That is, if there's |
| 1323 | * not enough space to enqueue a message, we return. However, if the lock |
| 1324 | * flag is set, we do wait for queue mutex. |
| 1325 | */ |
| 1326 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1327 | app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt_type) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1328 | { |
| 1329 | ASSERT (app && evt_type <= FIFO_EVENT_APP_TX); |
| 1330 | return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ ); |
| 1331 | } |
| 1332 | |
| 1333 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1334 | app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s, |
| 1335 | u8 evt_type) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1336 | { |
| 1337 | return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ ); |
| 1338 | } |
| 1339 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1340 | local_session_t * |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1341 | application_local_session_alloc (app_worker_t * app_wrk) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1342 | { |
| 1343 | local_session_t *s; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1344 | pool_get (app_wrk->local_sessions, s); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1345 | memset (s, 0, sizeof (*s)); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1346 | s->app_wrk_index = app_wrk->wrk_index; |
| 1347 | s->session_index = s - app_wrk->local_sessions; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1348 | s->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0); |
| 1349 | return s; |
| 1350 | } |
| 1351 | |
| 1352 | void |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1353 | application_local_session_free (app_worker_t * app, local_session_t * s) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1354 | { |
| 1355 | pool_put (app->local_sessions, s); |
| 1356 | if (CLIB_DEBUG) |
| 1357 | memset (s, 0xfc, sizeof (*s)); |
| 1358 | } |
| 1359 | |
| 1360 | local_session_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1361 | application_get_local_session (app_worker_t * app_wrk, u32 session_index) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1362 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1363 | if (pool_is_free_index (app_wrk->local_sessions, session_index)) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1364 | return 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1365 | return pool_elt_at_index (app_wrk->local_sessions, session_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | local_session_t * |
| 1369 | application_get_local_session_from_handle (session_handle_t handle) |
| 1370 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1371 | app_worker_t *server_wrk; |
| 1372 | u32 session_index, server_wrk_index; |
| 1373 | local_session_parse_handle (handle, &server_wrk_index, &session_index); |
| 1374 | server_wrk = app_worker_get_if_valid (server_wrk_index); |
| 1375 | if (!server_wrk) |
Florin Coras | fcbda89 | 2018-08-23 19:27:03 -0700 | [diff] [blame] | 1376 | return 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1377 | return application_get_local_session (server_wrk, session_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 1380 | local_session_t * |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1381 | application_local_listen_session_alloc (application_t * app) |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 1382 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1383 | local_session_t *ll; |
| 1384 | pool_get (app->local_listen_sessions, ll); |
| 1385 | memset (ll, 0, sizeof (*ll)); |
| 1386 | return ll; |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1389 | u32 |
| 1390 | application_local_listener_index (application_t * app, local_session_t * ll) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1391 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1392 | return (ll - app->local_listen_sessions); |
| 1393 | } |
| 1394 | |
| 1395 | void |
| 1396 | application_local_listen_session_free (application_t * app, |
| 1397 | local_session_t * ll) |
| 1398 | { |
| 1399 | pool_put (app->local_listen_sessions, ll); |
| 1400 | if (CLIB_DEBUG) |
| 1401 | memset (ll, 0xfb, sizeof (*ll)); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1405 | application_start_local_listen (application_t * app, |
| 1406 | session_endpoint_extended_t * sep_ext, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1407 | session_handle_t * handle) |
| 1408 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1409 | app_listener_t *app_listener; |
| 1410 | session_endpoint_t *sep; |
| 1411 | app_worker_t *app_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1412 | session_handle_t lh; |
| 1413 | local_session_t *ll; |
| 1414 | u32 table_index; |
| 1415 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1416 | sep = (session_endpoint_t *) sep_ext; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1417 | table_index = application_local_session_table (app); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1418 | app_wrk = app_worker_get (sep_ext->app_wrk_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1419 | |
| 1420 | /* An exact sep match, as opposed to session_lookup_local_listener */ |
| 1421 | lh = session_lookup_endpoint_listener (table_index, sep, 1); |
| 1422 | if (lh != SESSION_INVALID_HANDLE) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1423 | { |
| 1424 | ll = application_get_local_listener_w_handle (lh); |
| 1425 | if (ll->app_index != app->app_index) |
| 1426 | return VNET_API_ERROR_ADDRESS_IN_USE; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1427 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1428 | if (ll->app_wrk_index == app_wrk->wrk_index) |
| 1429 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 1430 | |
| 1431 | app_listener = app_local_listener_get (app, ll->listener_db_index); |
| 1432 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 1433 | app_wrk->wrk_map_index, 1); |
| 1434 | *handle = application_local_session_handle (ll); |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | ll = application_local_listen_session_alloc (app); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1439 | ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1440 | ll->app_wrk_index = app_wrk->app_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1441 | ll->session_index = application_local_listener_index (app, ll); |
| 1442 | ll->port = sep_ext->port; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1443 | /* Store the original session type for the unbind */ |
| 1444 | ll->listener_session_type = |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1445 | session_type_from_proto_and_ip (sep_ext->transport_proto, |
| 1446 | sep_ext->is_ip4); |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 1447 | ll->transport_listener_index = ~0; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1448 | ll->app_index = app->app_index; |
| 1449 | |
| 1450 | app_listener = app_local_listener_alloc (app); |
| 1451 | ll->listener_db_index = app_listener->al_index; |
| 1452 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 1453 | app_wrk->wrk_map_index, 1); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1454 | |
| 1455 | *handle = application_local_session_handle (ll); |
| 1456 | session_lookup_add_session_endpoint (table_index, sep, *handle); |
| 1457 | |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | /** |
| 1462 | * Clean up local session table. If we have a listener session use it to |
| 1463 | * find the port and proto. If not, the handle must be a local table handle |
| 1464 | * so parse it. |
| 1465 | */ |
| 1466 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1467 | application_stop_local_listen (u32 app_index, u32 wrk_map_index, |
| 1468 | session_handle_t lh) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1469 | { |
| 1470 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1471 | u32 table_index, ll_index, server_index; |
| 1472 | app_listener_t *app_listener; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1473 | app_worker_t *server_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1474 | stream_session_t *sl = 0; |
| 1475 | local_session_t *ll, *ls; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1476 | application_t *server; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1477 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1478 | server = application_get (app_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1479 | table_index = application_local_session_table (server); |
| 1480 | |
| 1481 | /* We have both local and global table binds. Figure from global what |
| 1482 | * the sep we should be cleaning up is. |
| 1483 | */ |
| 1484 | if (!session_handle_is_local (lh)) |
| 1485 | { |
| 1486 | sl = listen_session_get_from_handle (lh); |
| 1487 | if (!sl || listen_session_get_local_session_endpoint (sl, &sep)) |
| 1488 | { |
| 1489 | clib_warning ("broken listener"); |
| 1490 | return -1; |
| 1491 | } |
| 1492 | lh = session_lookup_endpoint_listener (table_index, &sep, 0); |
| 1493 | if (lh == SESSION_INVALID_HANDLE) |
| 1494 | return -1; |
| 1495 | } |
| 1496 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1497 | local_session_parse_handle (lh, &server_index, &ll_index); |
| 1498 | if (PREDICT_FALSE (server_index != app_index)) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1499 | { |
| 1500 | clib_warning ("app %u does not own local handle 0x%lx", app_index, lh); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1501 | return -1; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1502 | } |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1503 | |
| 1504 | ll = application_get_local_listen_session (server, ll_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1505 | if (PREDICT_FALSE (!ll)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1506 | { |
| 1507 | clib_warning ("no local listener"); |
| 1508 | return -1; |
| 1509 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1510 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1511 | app_listener = app_local_listener_get (server, ll->listener_db_index); |
| 1512 | if (!clib_bitmap_get (app_listener->workers, wrk_map_index)) |
| 1513 | { |
| 1514 | clib_warning ("app wrk %u not listening on handle %lu", wrk_map_index, |
| 1515 | lh); |
| 1516 | return -1; |
| 1517 | } |
| 1518 | |
| 1519 | server_wrk = application_get_worker (server, wrk_map_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1520 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1521 | pool_foreach (ls, server_wrk->local_sessions, ({ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1522 | if (ls->listener_index == ll->session_index) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1523 | application_local_session_disconnect (server_wrk->app_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1524 | })); |
| 1525 | /* *INDENT-ON* */ |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1526 | |
| 1527 | clib_bitmap_set_no_check (app_listener->workers, wrk_map_index, 0); |
| 1528 | if (clib_bitmap_is_zero (app_listener->workers)) |
| 1529 | { |
| 1530 | app_local_listener_free (server, app_listener); |
| 1531 | application_local_listener_session_endpoint (ll, &sep); |
| 1532 | session_lookup_del_session_endpoint (table_index, &sep); |
| 1533 | application_local_listen_session_free (server, ll); |
| 1534 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1535 | |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1539 | static void |
| 1540 | application_local_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq) |
| 1541 | { |
| 1542 | int fd; |
| 1543 | |
| 1544 | /* |
| 1545 | * segment manager initializes only the producer eventds, since vpp is |
| 1546 | * typically the producer. But for local sessions, we also pass to the |
| 1547 | * apps the mqs they listen on for events from peer apps, so they are also |
| 1548 | * consumer fds. |
| 1549 | */ |
| 1550 | fd = svm_msg_q_get_producer_eventfd (sq); |
| 1551 | svm_msg_q_set_consumer_eventfd (sq, fd); |
| 1552 | fd = svm_msg_q_get_producer_eventfd (cq); |
| 1553 | svm_msg_q_set_consumer_eventfd (cq, fd); |
| 1554 | } |
| 1555 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1556 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1557 | application_local_session_connect (app_worker_t * client_wrk, |
| 1558 | app_worker_t * server_wrk, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1559 | local_session_t * ll, u32 opaque) |
| 1560 | { |
| 1561 | u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10; |
| 1562 | segment_manager_properties_t *props, *cprops; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1563 | u32 round_rx_fifo_sz, round_tx_fifo_sz; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1564 | int rv, has_transport, seg_index; |
| 1565 | svm_fifo_segment_private_t *seg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1566 | application_t *server, *client; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1567 | segment_manager_t *sm; |
| 1568 | local_session_t *ls; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1569 | svm_msg_q_t *sq, *cq; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1570 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1571 | ls = application_local_session_alloc (server_wrk); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1572 | server = application_get (server_wrk->app_index); |
| 1573 | client = application_get (client_wrk->app_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1574 | |
| 1575 | props = application_segment_manager_properties (server); |
| 1576 | cprops = application_segment_manager_properties (client); |
| 1577 | evt_q_elts = props->evt_q_size + cprops->evt_q_size; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1578 | evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 1579 | round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size); |
| 1580 | round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size); |
| 1581 | seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1582 | |
| 1583 | has_transport = session_has_transport ((stream_session_t *) ll); |
| 1584 | if (!has_transport) |
| 1585 | { |
| 1586 | /* Local sessions don't have backing transport */ |
| 1587 | ls->port = ll->port; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1588 | sm = application_get_local_segment_manager (server_wrk); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1589 | } |
| 1590 | else |
| 1591 | { |
| 1592 | stream_session_t *sl = (stream_session_t *) ll; |
| 1593 | transport_connection_t *tc; |
| 1594 | tc = listen_session_get_transport (sl); |
| 1595 | ls->port = tc->lcl_port; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1596 | sm = app_worker_get_listen_segment_manager (server_wrk, sl); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | seg_index = segment_manager_add_segment (sm, seg_size); |
| 1600 | if (seg_index < 0) |
| 1601 | { |
| 1602 | clib_warning ("failed to add new cut-through segment"); |
| 1603 | return seg_index; |
| 1604 | } |
| 1605 | seg = segment_manager_get_segment_w_lock (sm, seg_index); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1606 | sq = segment_manager_alloc_queue (seg, props); |
| 1607 | cq = segment_manager_alloc_queue (seg, cprops); |
| 1608 | |
| 1609 | if (props->use_mq_eventfd) |
| 1610 | application_local_session_fix_eventds (sq, cq); |
| 1611 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1612 | ls->server_evt_q = pointer_to_uword (sq); |
| 1613 | ls->client_evt_q = pointer_to_uword (cq); |
| 1614 | rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size, |
| 1615 | props->tx_fifo_size, |
| 1616 | &ls->server_rx_fifo, |
| 1617 | &ls->server_tx_fifo); |
| 1618 | if (rv) |
| 1619 | { |
| 1620 | clib_warning ("failed to add fifos in cut-through segment"); |
| 1621 | segment_manager_segment_reader_unlock (sm); |
| 1622 | goto failed; |
| 1623 | } |
| 1624 | ls->server_rx_fifo->master_session_index = ls->session_index; |
| 1625 | ls->server_tx_fifo->master_session_index = ls->session_index; |
| 1626 | ls->server_rx_fifo->master_thread_index = ~0; |
| 1627 | ls->server_tx_fifo->master_thread_index = ~0; |
| 1628 | ls->svm_segment_index = seg_index; |
| 1629 | ls->listener_index = ll->session_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1630 | ls->client_wrk_index = client_wrk->wrk_index; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1631 | ls->client_opaque = opaque; |
| 1632 | ls->listener_session_type = ll->session_type; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1633 | ls->session_state = SESSION_STATE_READY; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1634 | |
| 1635 | if ((rv = server->cb_fns.add_segment_callback (server->api_client_index, |
| 1636 | &seg->ssvm))) |
| 1637 | { |
| 1638 | clib_warning ("failed to notify server of new segment"); |
| 1639 | segment_manager_segment_reader_unlock (sm); |
| 1640 | goto failed; |
| 1641 | } |
| 1642 | segment_manager_segment_reader_unlock (sm); |
| 1643 | if ((rv = server->cb_fns.session_accept_callback ((stream_session_t *) ls))) |
| 1644 | { |
| 1645 | clib_warning ("failed to send accept cut-through notify to server"); |
| 1646 | goto failed; |
| 1647 | } |
| 1648 | if (server->flags & APP_OPTIONS_FLAGS_IS_BUILTIN) |
| 1649 | application_local_session_connect_notify (ls); |
| 1650 | |
| 1651 | return 0; |
| 1652 | |
| 1653 | failed: |
| 1654 | if (!has_transport) |
| 1655 | segment_manager_del_segment (sm, seg); |
| 1656 | return rv; |
| 1657 | } |
| 1658 | |
| 1659 | static uword |
| 1660 | application_client_local_connect_key (local_session_t * ls) |
| 1661 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1662 | return ((uword) ls->app_wrk_index << 32 | (uword) ls->session_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1666 | application_client_local_connect_key_parse (uword key, u32 * app_wrk_index, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1667 | u32 * session_index) |
| 1668 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1669 | *app_wrk_index = key >> 32; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1670 | *session_index = key & 0xFFFFFFFF; |
| 1671 | } |
| 1672 | |
| 1673 | int |
| 1674 | application_local_session_connect_notify (local_session_t * ls) |
| 1675 | { |
| 1676 | svm_fifo_segment_private_t *seg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1677 | app_worker_t *client_wrk, *server_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1678 | segment_manager_t *sm; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1679 | application_t *client; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1680 | int rv, is_fail = 0; |
| 1681 | uword client_key; |
| 1682 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1683 | client_wrk = app_worker_get (ls->client_wrk_index); |
| 1684 | server_wrk = app_worker_get (ls->app_wrk_index); |
| 1685 | client = application_get (client_wrk->app_index); |
| 1686 | |
| 1687 | sm = application_get_local_segment_manager_w_session (server_wrk, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1688 | seg = segment_manager_get_segment_w_lock (sm, ls->svm_segment_index); |
| 1689 | if ((rv = client->cb_fns.add_segment_callback (client->api_client_index, |
| 1690 | &seg->ssvm))) |
| 1691 | { |
| 1692 | clib_warning ("failed to notify client %u of new segment", |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1693 | ls->client_wrk_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1694 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1695 | application_local_session_disconnect (ls->client_wrk_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1696 | is_fail = 1; |
| 1697 | } |
| 1698 | else |
| 1699 | { |
| 1700 | segment_manager_segment_reader_unlock (sm); |
| 1701 | } |
| 1702 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1703 | client->cb_fns.session_connected_callback (client_wrk->wrk_index, |
| 1704 | ls->client_opaque, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1705 | (stream_session_t *) ls, |
| 1706 | is_fail); |
| 1707 | |
| 1708 | client_key = application_client_local_connect_key (ls); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1709 | hash_set (client_wrk->local_connects, client_key, client_key); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1714 | application_local_session_cleanup (app_worker_t * client_wrk, |
| 1715 | app_worker_t * server_wrk, |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1716 | local_session_t * ls) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1717 | { |
| 1718 | svm_fifo_segment_private_t *seg; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1719 | segment_manager_t *sm; |
| 1720 | uword client_key; |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1721 | u8 has_transport; |
| 1722 | |
| 1723 | has_transport = session_has_transport ((stream_session_t *) ls); |
| 1724 | client_key = application_client_local_connect_key (ls); |
| 1725 | if (!has_transport) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1726 | sm = application_get_local_segment_manager_w_session (server_wrk, ls); |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1727 | else |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1728 | sm = app_worker_get_listen_segment_manager (server_wrk, |
| 1729 | (stream_session_t *) ls); |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1730 | |
| 1731 | seg = segment_manager_get_segment (sm, ls->svm_segment_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1732 | if (client_wrk) |
| 1733 | hash_unset (client_wrk->local_connects, client_key); |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1734 | |
| 1735 | if (!has_transport) |
| 1736 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1737 | application_t *server = application_get (server_wrk->app_index); |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1738 | server->cb_fns.del_segment_callback (server->api_client_index, |
| 1739 | &seg->ssvm); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1740 | if (client_wrk) |
| 1741 | { |
| 1742 | application_t *client = application_get (client_wrk->app_index); |
| 1743 | client->cb_fns.del_segment_callback (client->api_client_index, |
| 1744 | &seg->ssvm); |
| 1745 | } |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1746 | segment_manager_del_segment (sm, seg); |
| 1747 | } |
| 1748 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1749 | application_local_session_free (server_wrk, ls); |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1750 | |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | int |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1755 | application_local_session_disconnect (u32 app_index, local_session_t * ls) |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1756 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1757 | app_worker_t *client_wrk, *server_wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1758 | u8 is_server = 0, is_client = 0; |
| 1759 | application_t *app; |
| 1760 | |
| 1761 | app = application_get_if_valid (app_index); |
| 1762 | if (!app) |
| 1763 | return 0; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1764 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1765 | client_wrk = app_worker_get_if_valid (ls->client_wrk_index); |
| 1766 | server_wrk = app_worker_get (ls->app_wrk_index); |
| 1767 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1768 | if (server_wrk->app_index == app_index) |
| 1769 | is_server = 1; |
| 1770 | else if (client_wrk && client_wrk->app_index == app_index) |
| 1771 | is_client = 1; |
| 1772 | |
| 1773 | if (!is_server && !is_client) |
| 1774 | { |
| 1775 | clib_warning ("app %u is neither client nor server for session 0x%lx", |
| 1776 | app_index, application_local_session_handle (ls)); |
| 1777 | return VNET_API_ERROR_INVALID_VALUE; |
| 1778 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1779 | |
| 1780 | if (ls->session_state == SESSION_STATE_CLOSED) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1781 | return application_local_session_cleanup (client_wrk, server_wrk, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1782 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1783 | if (app_index == ls->client_wrk_index) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1784 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1785 | mq_send_local_session_disconnected_cb (ls->app_wrk_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1786 | } |
| 1787 | else |
| 1788 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1789 | if (!client_wrk) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1790 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1791 | return application_local_session_cleanup (client_wrk, server_wrk, |
| 1792 | ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1793 | } |
| 1794 | else if (ls->session_state < SESSION_STATE_READY) |
| 1795 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1796 | application_t *client = application_get (client_wrk->app_index); |
| 1797 | client->cb_fns.session_connected_callback (client_wrk->wrk_index, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1798 | ls->client_opaque, |
| 1799 | (stream_session_t *) ls, |
| 1800 | 1 /* is_fail */ ); |
| 1801 | ls->session_state = SESSION_STATE_CLOSED; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1802 | return application_local_session_cleanup (client_wrk, server_wrk, |
| 1803 | ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1804 | } |
| 1805 | else |
| 1806 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1807 | mq_send_local_session_disconnected_cb (client_wrk->wrk_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | ls->session_state = SESSION_STATE_CLOSED; |
| 1812 | |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1816 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1817 | application_local_session_disconnect_w_index (u32 app_wrk_index, u32 ls_index) |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1818 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1819 | app_worker_t *app_wrk; |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1820 | local_session_t *ls; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1821 | app_wrk = app_worker_get (app_wrk_index); |
| 1822 | ls = application_get_local_session (app_wrk, ls_index); |
| 1823 | return application_local_session_disconnect (app_wrk_index, ls); |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1826 | void |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1827 | app_worker_local_sessions_free (app_worker_t * app_wrk) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1828 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1829 | u32 index, server_wrk_index, session_index; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1830 | u64 handle, *handles = 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1831 | app_worker_t *server_wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1832 | segment_manager_t *sm; |
| 1833 | local_session_t *ls; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1834 | int i; |
| 1835 | |
| 1836 | /* |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1837 | * Local sessions |
| 1838 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1839 | if (app_wrk->local_sessions) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1840 | { |
| 1841 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1842 | pool_foreach (ls, app_wrk->local_sessions, ({ |
| 1843 | application_local_session_disconnect (app_wrk->wrk_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1844 | })); |
| 1845 | /* *INDENT-ON* */ |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Local connects |
| 1850 | */ |
| 1851 | vec_reset_length (handles); |
| 1852 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1853 | hash_foreach (handle, index, app_wrk->local_connects, ({ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1854 | vec_add1 (handles, handle); |
| 1855 | })); |
| 1856 | /* *INDENT-ON* */ |
| 1857 | |
| 1858 | for (i = 0; i < vec_len (handles); i++) |
| 1859 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1860 | application_client_local_connect_key_parse (handles[i], |
| 1861 | &server_wrk_index, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1862 | &session_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1863 | server_wrk = app_worker_get_if_valid (server_wrk_index); |
| 1864 | if (server_wrk) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1865 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1866 | ls = application_get_local_session (server_wrk, session_index); |
| 1867 | application_local_session_disconnect (app_wrk->wrk_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1871 | sm = segment_manager_get (app_wrk->local_segment_manager); |
| 1872 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1873 | segment_manager_del (sm); |
| 1874 | } |
| 1875 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1876 | clib_error_t * |
| 1877 | vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a) |
| 1878 | { |
| 1879 | application_t *app; |
| 1880 | app = application_get (a->app_index); |
| 1881 | if (!app) |
| 1882 | return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED, |
| 1883 | 0, "app %u doesn't exist", a->app_index); |
| 1884 | app->tls_cert = vec_dup (a->cert); |
| 1885 | return 0; |
| 1886 | } |
| 1887 | |
| 1888 | clib_error_t * |
| 1889 | vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a) |
| 1890 | { |
| 1891 | application_t *app; |
| 1892 | app = application_get (a->app_index); |
| 1893 | if (!app) |
| 1894 | return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED, |
| 1895 | 0, "app %u doesn't exist", a->app_index); |
| 1896 | app->tls_key = vec_dup (a->key); |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1900 | u8 * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1901 | format_app_worker_listener (u8 * s, va_list * args) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1902 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1903 | app_worker_t *app_wrk = va_arg (*args, app_worker_t *); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1904 | u64 handle = va_arg (*args, u64); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1905 | u32 sm_index = va_arg (*args, u32); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1906 | int verbose = va_arg (*args, int); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1907 | stream_session_t *listener; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1908 | application_t *app; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1909 | u8 *app_name, *str; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1910 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1911 | if (!app_wrk) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1912 | { |
| 1913 | if (verbose) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1914 | s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App", |
| 1915 | "Wrk", "API Client", "ListenerID", "SegManager"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1916 | else |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1917 | s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1918 | |
| 1919 | return s; |
| 1920 | } |
| 1921 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1922 | app = application_get (app_wrk->app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1923 | app_name = app_get_name_from_reg_index (app); |
| 1924 | listener = listen_session_get_from_handle (handle); |
| 1925 | str = format (0, "%U", format_stream_session, listener, verbose); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1926 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1927 | if (verbose) |
| 1928 | { |
Florin Coras | 74cac88 | 2018-09-07 09:13:15 -0700 | [diff] [blame^] | 1929 | char buf[32]; |
| 1930 | sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index); |
| 1931 | s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name, |
| 1932 | buf, app->api_client_index, handle, sm_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1933 | } |
| 1934 | else |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1935 | s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1936 | |
| 1937 | vec_free (app_name); |
| 1938 | return s; |
| 1939 | } |
| 1940 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1941 | static void |
| 1942 | application_format_listeners (application_t * app, int verbose) |
| 1943 | { |
| 1944 | vlib_main_t *vm = vlib_get_main (); |
| 1945 | app_worker_map_t *wrk_map; |
| 1946 | app_worker_t *app_wrk; |
| 1947 | u32 sm_index; |
| 1948 | u64 handle; |
| 1949 | |
| 1950 | if (!app) |
| 1951 | { |
| 1952 | vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ , |
| 1953 | 0, 0, verbose); |
| 1954 | return; |
| 1955 | } |
| 1956 | |
| 1957 | /* *INDENT-OFF* */ |
| 1958 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 1959 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 1960 | if (hash_elts (app_wrk->listeners_table) == 0) |
| 1961 | continue; |
| 1962 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
| 1963 | vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk, |
| 1964 | handle, sm_index, verbose); |
| 1965 | })); |
| 1966 | })); |
| 1967 | /* *INDENT-ON* */ |
| 1968 | } |
| 1969 | |
| 1970 | static void |
| 1971 | app_worker_format_connects (app_worker_t * app_wrk, int verbose) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1972 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1973 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1974 | vlib_main_t *vm = vlib_get_main (); |
| 1975 | segment_manager_t *sm; |
| 1976 | u8 *app_name, *s = 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1977 | application_t *app; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1978 | |
| 1979 | /* Header */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1980 | if (!app_wrk) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1981 | { |
| 1982 | if (verbose) |
| 1983 | vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App", |
| 1984 | "API Client", "SegManager"); |
| 1985 | else |
| 1986 | vlib_cli_output (vm, "%-40s%-20s", "Connection", "App"); |
| 1987 | return; |
| 1988 | } |
| 1989 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1990 | app = application_get (app_wrk->app_index); |
| 1991 | if (app_wrk->connects_seg_manager == (u32) ~ 0) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1992 | return; |
| 1993 | |
| 1994 | app_name = app_get_name_from_reg_index (app); |
| 1995 | |
| 1996 | /* Across all fifo segments */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1997 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1998 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1999 | /* *INDENT-OFF* */ |
| 2000 | segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({ |
| 2001 | svm_fifo_t *fifo; |
| 2002 | u8 *str; |
| 2003 | |
| 2004 | fifo = svm_fifo_segment_get_fifo_list (fifo_segment); |
| 2005 | while (fifo) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2006 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2007 | u32 session_index, thread_index; |
| 2008 | stream_session_t *session; |
| 2009 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 2010 | session_index = fifo->master_session_index; |
| 2011 | thread_index = fifo->master_thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2012 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2013 | session = session_get (session_index, thread_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2014 | str = format (0, "%U", format_stream_session, session, verbose); |
| 2015 | |
| 2016 | if (verbose) |
| 2017 | s = format (s, "%-40s%-20s%-15u%-10u", str, app_name, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2018 | app->api_client_index, app_wrk->connects_seg_manager); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2019 | else |
| 2020 | s = format (s, "%-40s%-20s", str, app_name); |
| 2021 | |
| 2022 | vlib_cli_output (vm, "%v", s); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2023 | vec_reset_length (s); |
| 2024 | vec_free (str); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2025 | |
| 2026 | fifo = fifo->next; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2027 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 2028 | vec_free (s); |
| 2029 | })); |
| 2030 | /* *INDENT-ON* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2031 | |
| 2032 | vec_free (app_name); |
| 2033 | } |
| 2034 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2035 | static void |
| 2036 | application_format_connects (application_t * app, int verbose) |
| 2037 | { |
| 2038 | app_worker_map_t *wrk_map; |
| 2039 | app_worker_t *app_wrk; |
| 2040 | |
| 2041 | if (!app) |
| 2042 | { |
| 2043 | app_worker_format_connects (0, verbose); |
| 2044 | return; |
| 2045 | } |
| 2046 | |
| 2047 | /* *INDENT-OFF* */ |
| 2048 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 2049 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 2050 | app_worker_format_connects (app_wrk, verbose); |
| 2051 | })); |
| 2052 | /* *INDENT-ON* */ |
| 2053 | } |
| 2054 | |
| 2055 | static void |
| 2056 | app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2057 | { |
| 2058 | vlib_main_t *vm = vlib_get_main (); |
| 2059 | local_session_t *ls; |
| 2060 | transport_proto_t tp; |
| 2061 | u8 *conn = 0; |
| 2062 | |
| 2063 | /* Header */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2064 | if (app_wrk == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2065 | { |
| 2066 | vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "ServerApp", |
| 2067 | "ClientApp"); |
| 2068 | return; |
| 2069 | } |
| 2070 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2071 | if (!pool_elts (app_wrk->local_sessions) |
| 2072 | && !pool_elts (app_wrk->local_connects)) |
| 2073 | return; |
| 2074 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2075 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2076 | pool_foreach (ls, app_wrk->local_sessions, ({ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2077 | tp = session_type_transport_proto(ls->listener_session_type); |
| 2078 | conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp, |
| 2079 | ls->port); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2080 | vlib_cli_output (vm, "%-40v%-15u%-20u", conn, ls->app_wrk_index, |
| 2081 | ls->client_wrk_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2082 | vec_reset_length (conn); |
| 2083 | })); |
| 2084 | /* *INDENT-ON* */ |
| 2085 | |
| 2086 | vec_free (conn); |
| 2087 | } |
| 2088 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2089 | static void |
| 2090 | application_format_local_sessions (application_t * app, int verbose) |
| 2091 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 2092 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2093 | app_worker_map_t *wrk_map; |
| 2094 | app_worker_t *app_wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 2095 | transport_proto_t tp; |
| 2096 | local_session_t *ls; |
| 2097 | u8 *conn = 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2098 | |
| 2099 | if (!app) |
| 2100 | { |
| 2101 | app_worker_format_local_sessions (0, verbose); |
| 2102 | return; |
| 2103 | } |
| 2104 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 2105 | /* |
| 2106 | * Format local listeners |
| 2107 | */ |
| 2108 | |
| 2109 | /* *INDENT-OFF* */ |
| 2110 | pool_foreach (ls, app->local_listen_sessions, ({ |
| 2111 | tp = session_type_transport_proto (ls->listener_session_type); |
| 2112 | conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp, |
| 2113 | ls->port); |
| 2114 | vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*"); |
| 2115 | vec_reset_length (conn); |
| 2116 | })); |
| 2117 | /* *INDENT-ON* */ |
| 2118 | |
| 2119 | /* |
| 2120 | * Format local accepted/connected sessions |
| 2121 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2122 | /* *INDENT-OFF* */ |
| 2123 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 2124 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 2125 | app_worker_format_local_sessions (app_wrk, verbose); |
| 2126 | })); |
| 2127 | /* *INDENT-ON* */ |
| 2128 | } |
| 2129 | |
| 2130 | static void |
| 2131 | app_worker_format_local_connects (app_worker_t * app, int verbose) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2132 | { |
| 2133 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2134 | u32 app_wrk_index, session_index; |
| 2135 | app_worker_t *server_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2136 | local_session_t *ls; |
| 2137 | uword client_key; |
| 2138 | u64 value; |
| 2139 | |
| 2140 | /* Header */ |
| 2141 | if (app == 0) |
| 2142 | { |
| 2143 | if (verbose) |
| 2144 | vlib_cli_output (vm, "%-40s%-15s%-20s%-10s", "Connection", "App", |
| 2145 | "Peer App", "SegManager"); |
| 2146 | else |
| 2147 | vlib_cli_output (vm, "%-40s%-15s%-20s", "Connection", "App", |
| 2148 | "Peer App"); |
| 2149 | return; |
| 2150 | } |
| 2151 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2152 | if (!app->local_connects) |
| 2153 | return; |
| 2154 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2155 | /* *INDENT-OFF* */ |
| 2156 | hash_foreach (client_key, value, app->local_connects, ({ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2157 | application_client_local_connect_key_parse (client_key, &app_wrk_index, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2158 | &session_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2159 | server_wrk = app_worker_get (app_wrk_index); |
| 2160 | ls = application_get_local_session (server_wrk, session_index); |
| 2161 | vlib_cli_output (vm, "%-40s%-15s%-20s", "TODO", ls->app_wrk_index, |
| 2162 | ls->client_wrk_index); |
| 2163 | })); |
| 2164 | /* *INDENT-ON* */ |
| 2165 | } |
| 2166 | |
| 2167 | static void |
| 2168 | application_format_local_connects (application_t * app, int verbose) |
| 2169 | { |
| 2170 | app_worker_map_t *wrk_map; |
| 2171 | app_worker_t *app_wrk; |
| 2172 | |
| 2173 | if (!app) |
| 2174 | { |
| 2175 | app_worker_format_local_connects (0, verbose); |
| 2176 | return; |
| 2177 | } |
| 2178 | |
| 2179 | /* *INDENT-OFF* */ |
| 2180 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 2181 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 2182 | app_worker_format_local_connects (app_wrk, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2183 | })); |
| 2184 | /* *INDENT-ON* */ |
| 2185 | } |
| 2186 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2187 | u8 * |
| 2188 | format_application (u8 * s, va_list * args) |
| 2189 | { |
| 2190 | application_t *app = va_arg (*args, application_t *); |
| 2191 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 2192 | segment_manager_properties_t *props; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2193 | const u8 *app_ns_name; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2194 | u8 *app_name; |
| 2195 | |
| 2196 | if (app == 0) |
| 2197 | { |
| 2198 | if (verbose) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2199 | s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s%-15s", "Index", "Name", |
Florin Coras | 078371e | 2018-05-11 09:20:12 -0700 | [diff] [blame] | 2200 | "API Client", "Namespace", "Add seg size", "Rx-f size", |
| 2201 | "Tx-f size"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2202 | else |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 2203 | s = format (s, "%-10s%-20s%-15s%-40s", "Index", "Name", "API Client", |
| 2204 | "Namespace"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2205 | return s; |
| 2206 | } |
| 2207 | |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 2208 | app_name = app_get_name (app); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2209 | app_ns_name = app_namespace_id_from_index (app->ns_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 2210 | props = application_segment_manager_properties (app); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2211 | if (verbose) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2212 | s = format (s, "%-10u%-20s%-15d%-15u%-15U%-15U%-15U", app->app_index, |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 2213 | app_name, app->api_client_index, app->ns_index, |
Florin Coras | 078371e | 2018-05-11 09:20:12 -0700 | [diff] [blame] | 2214 | format_memory_size, props->add_segment_size, |
| 2215 | format_memory_size, props->rx_fifo_size, format_memory_size, |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 2216 | props->tx_fifo_size); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2217 | else |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2218 | s = format (s, "%-10u%-20s%-15d%-40s", app->app_index, app_name, |
Florin Coras | 8f107b3 | 2017-11-21 22:13:03 -0800 | [diff] [blame] | 2219 | app->api_client_index, app_ns_name); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2220 | return s; |
| 2221 | } |
| 2222 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2223 | void |
| 2224 | application_format_all_listeners (vlib_main_t * vm, int do_local, int verbose) |
| 2225 | { |
| 2226 | application_t *app; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2227 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2228 | if (!pool_elts (app_main.app_pool)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2229 | { |
| 2230 | vlib_cli_output (vm, "No active server bindings"); |
| 2231 | return; |
| 2232 | } |
| 2233 | |
| 2234 | if (do_local) |
| 2235 | { |
| 2236 | application_format_local_sessions (0, verbose); |
| 2237 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2238 | pool_foreach (app, app_main.app_pool, ({ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2239 | application_format_local_sessions (app, verbose); |
| 2240 | })); |
| 2241 | /* *INDENT-ON* */ |
| 2242 | } |
| 2243 | else |
| 2244 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2245 | application_format_listeners (0, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2246 | |
| 2247 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2248 | pool_foreach (app, app_main.app_pool, ({ |
| 2249 | application_format_listeners (app, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2250 | })); |
| 2251 | /* *INDENT-ON* */ |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | void |
| 2256 | application_format_all_clients (vlib_main_t * vm, int do_local, int verbose) |
| 2257 | { |
| 2258 | application_t *app; |
| 2259 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2260 | if (!pool_elts (app_main.app_pool)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2261 | { |
| 2262 | vlib_cli_output (vm, "No active apps"); |
| 2263 | return; |
| 2264 | } |
| 2265 | |
| 2266 | if (do_local) |
| 2267 | { |
| 2268 | application_format_local_connects (0, verbose); |
| 2269 | |
| 2270 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2271 | pool_foreach (app, app_main.app_pool, ({ |
| 2272 | application_format_local_connects (app, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2273 | })); |
| 2274 | /* *INDENT-ON* */ |
| 2275 | } |
| 2276 | else |
| 2277 | { |
| 2278 | application_format_connects (0, verbose); |
| 2279 | |
| 2280 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2281 | pool_foreach (app, app_main.app_pool, ({ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2282 | application_format_connects (app, verbose); |
| 2283 | })); |
| 2284 | /* *INDENT-ON* */ |
| 2285 | } |
| 2286 | } |
| 2287 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2288 | static clib_error_t * |
| 2289 | show_app_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 2290 | vlib_cli_command_t * cmd) |
| 2291 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2292 | int do_server = 0, do_client = 0, do_local = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2293 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2294 | int verbose = 0; |
| 2295 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2296 | session_cli_return_if_not_enabled (); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2297 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2298 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2299 | { |
| 2300 | if (unformat (input, "server")) |
| 2301 | do_server = 1; |
| 2302 | else if (unformat (input, "client")) |
| 2303 | do_client = 1; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2304 | else if (unformat (input, "local")) |
| 2305 | do_local = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2306 | else if (unformat (input, "verbose")) |
| 2307 | verbose = 1; |
| 2308 | else |
| 2309 | break; |
| 2310 | } |
| 2311 | |
| 2312 | if (do_server) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2313 | application_format_all_listeners (vm, do_local, verbose); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2314 | |
| 2315 | if (do_client) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 2316 | application_format_all_clients (vm, do_local, verbose); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2317 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2318 | /* Print app related info */ |
| 2319 | if (!do_server && !do_client) |
| 2320 | { |
| 2321 | vlib_cli_output (vm, "%U", format_application, 0, verbose); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2322 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 2323 | pool_foreach (app, app_main.app_pool, ({ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2324 | vlib_cli_output (vm, "%U", format_application, app, verbose); |
| 2325 | })); |
| 2326 | /* *INDENT-ON* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
| 2331 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2332 | /* *INDENT-OFF* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2333 | VLIB_CLI_COMMAND (show_app_command, static) = |
| 2334 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2335 | .path = "show app", |
| 2336 | .short_help = "show app [server|client] [verbose]", |
| 2337 | .function = show_app_command_fn, |
| 2338 | }; |
| 2339 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2340 | |
| 2341 | /* |
| 2342 | * fd.io coding-style-patch-verification: ON |
| 2343 | * |
| 2344 | * Local Variables: |
| 2345 | * eval: (c-set-style "gnu") |
| 2346 | * End: |
| 2347 | */ |