Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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> |
| 17 | #include <vnet/session/application_interface.h> |
Florin Coras | 54a51fd | 2019-02-07 15:34:52 -0800 | [diff] [blame] | 18 | #include <vnet/session/session.h> |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Pool of workers associated to apps |
| 22 | */ |
| 23 | static app_worker_t *app_workers; |
| 24 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 25 | app_worker_t * |
| 26 | app_worker_alloc (application_t * app) |
| 27 | { |
| 28 | app_worker_t *app_wrk; |
| 29 | pool_get (app_workers, app_wrk); |
| 30 | clib_memset (app_wrk, 0, sizeof (*app_wrk)); |
| 31 | app_wrk->wrk_index = app_wrk - app_workers; |
| 32 | app_wrk->app_index = app->app_index; |
| 33 | app_wrk->wrk_map_index = ~0; |
| 34 | app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; |
| 35 | app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 36 | APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index); |
| 37 | return app_wrk; |
| 38 | } |
| 39 | |
| 40 | app_worker_t * |
| 41 | app_worker_get (u32 wrk_index) |
| 42 | { |
| 43 | return pool_elt_at_index (app_workers, wrk_index); |
| 44 | } |
| 45 | |
| 46 | app_worker_t * |
| 47 | app_worker_get_if_valid (u32 wrk_index) |
| 48 | { |
| 49 | if (pool_is_free_index (app_workers, wrk_index)) |
| 50 | return 0; |
| 51 | return pool_elt_at_index (app_workers, wrk_index); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | app_worker_free (app_worker_t * app_wrk) |
| 56 | { |
| 57 | application_t *app = application_get (app_wrk->app_index); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 58 | vnet_unlisten_args_t _a, *a = &_a; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 59 | u64 handle, *handles = 0; |
| 60 | segment_manager_t *sm; |
| 61 | u32 sm_index; |
| 62 | int i; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 63 | app_listener_t *al; |
| 64 | session_t *ls; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Listener cleanup |
| 68 | */ |
| 69 | |
| 70 | /* *INDENT-OFF* */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 71 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
| 72 | ls = listen_session_get_from_handle (handle); |
| 73 | al = app_listener_get (app, ls->al_index); |
| 74 | vec_add1 (handles, app_listener_handle (al)); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 75 | sm = segment_manager_get (sm_index); |
| 76 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
| 77 | })); |
| 78 | /* *INDENT-ON* */ |
| 79 | |
| 80 | for (i = 0; i < vec_len (handles); i++) |
| 81 | { |
| 82 | a->app_index = app->app_index; |
| 83 | a->wrk_map_index = app_wrk->wrk_map_index; |
| 84 | a->handle = handles[i]; |
| 85 | /* seg manager is removed when unbind completes */ |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 86 | (void) vnet_unlisten (a); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Connects segment manager cleanup |
| 91 | */ |
| 92 | |
| 93 | if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX) |
| 94 | { |
| 95 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
| 96 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
Florin Coras | 565115e | 2019-02-20 19:48:31 -0800 | [diff] [blame] | 97 | sm->first_is_protected = 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 98 | segment_manager_init_del (sm); |
| 99 | } |
| 100 | |
| 101 | /* If first segment manager is used by a listener */ |
| 102 | if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX |
| 103 | && app_wrk->first_segment_manager != app_wrk->connects_seg_manager) |
| 104 | { |
| 105 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 106 | sm->first_is_protected = 0; |
| 107 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
| 108 | /* .. and has no fifos, e.g. it might be used for redirected sessions, |
| 109 | * remove it */ |
| 110 | if (!segment_manager_has_fifos (sm)) |
| 111 | segment_manager_del (sm); |
| 112 | } |
| 113 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 114 | pool_put (app_workers, app_wrk); |
| 115 | if (CLIB_DEBUG) |
| 116 | clib_memset (app_wrk, 0xfe, sizeof (*app_wrk)); |
| 117 | } |
| 118 | |
| 119 | application_t * |
| 120 | app_worker_get_app (u32 wrk_index) |
| 121 | { |
| 122 | app_worker_t *app_wrk; |
| 123 | app_wrk = app_worker_get_if_valid (wrk_index); |
| 124 | if (!app_wrk) |
| 125 | return 0; |
| 126 | return application_get_if_valid (app_wrk->app_index); |
| 127 | } |
| 128 | |
| 129 | static segment_manager_t * |
| 130 | app_worker_alloc_segment_manager (app_worker_t * app_wrk) |
| 131 | { |
| 132 | segment_manager_t *sm = 0; |
| 133 | |
| 134 | /* If the first segment manager is not in use, don't allocate a new one */ |
| 135 | if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX |
| 136 | && app_wrk->first_segment_manager_in_use == 0) |
| 137 | { |
| 138 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 139 | app_wrk->first_segment_manager_in_use = 1; |
| 140 | return sm; |
| 141 | } |
| 142 | |
| 143 | sm = segment_manager_new (); |
| 144 | sm->app_wrk_index = app_wrk->wrk_index; |
| 145 | |
| 146 | return sm; |
| 147 | } |
| 148 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 149 | static int |
| 150 | app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s) |
| 151 | { |
| 152 | svm_fifo_t *rx_fifo = 0, *tx_fifo = 0; |
| 153 | u32 fifo_segment_index; |
| 154 | int rv; |
| 155 | |
| 156 | if ((rv = segment_manager_alloc_session_fifos (sm, &rx_fifo, &tx_fifo, |
| 157 | &fifo_segment_index))) |
| 158 | return rv; |
| 159 | |
| 160 | rx_fifo->master_session_index = s->session_index; |
| 161 | rx_fifo->master_thread_index = s->thread_index; |
| 162 | |
| 163 | tx_fifo->master_session_index = s->session_index; |
| 164 | tx_fifo->master_thread_index = s->thread_index; |
| 165 | |
| 166 | s->rx_fifo = rx_fifo; |
| 167 | s->tx_fifo = tx_fifo; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 171 | int |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 172 | app_worker_init_listener (app_worker_t * app_wrk, session_t * ls) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 173 | { |
| 174 | segment_manager_t *sm; |
| 175 | |
| 176 | /* Allocate segment manager. All sessions derived out of a listen session |
| 177 | * have fifos allocated by the same segment manager. */ |
| 178 | if (!(sm = app_worker_alloc_segment_manager (app_wrk))) |
| 179 | return -1; |
| 180 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 181 | /* Keep track of the segment manager for the listener or this worker */ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 182 | hash_set (app_wrk->listeners_table, listen_session_get_handle (ls), |
| 183 | segment_manager_index (sm)); |
| 184 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 185 | if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 186 | { |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 187 | if (!ls->rx_fifo && app_worker_alloc_session_fifos (sm, ls)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 188 | return -1; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 194 | app_worker_start_listen (app_worker_t * app_wrk, |
| 195 | app_listener_t * app_listener) |
| 196 | { |
| 197 | session_t *ls; |
| 198 | |
| 199 | if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index)) |
| 200 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 201 | |
| 202 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 203 | app_wrk->wrk_map_index, 1); |
| 204 | |
| 205 | if (app_listener->session_index != SESSION_INVALID_INDEX) |
| 206 | { |
| 207 | ls = session_get (app_listener->session_index, 0); |
| 208 | if (app_worker_init_listener (app_wrk, ls)) |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | if (app_listener->local_index != SESSION_INVALID_INDEX) |
| 213 | { |
| 214 | ls = session_get (app_listener->local_index, 0); |
| 215 | if (app_worker_init_listener (app_wrk, ls)) |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 222 | static void |
| 223 | app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 224 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 225 | session_handle_t handle; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 226 | segment_manager_t *sm; |
| 227 | uword *sm_indexp; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 228 | |
| 229 | handle = listen_session_get_handle (ls); |
| 230 | sm_indexp = hash_get (app_wrk->listeners_table, handle); |
| 231 | if (PREDICT_FALSE (!sm_indexp)) |
| 232 | return; |
| 233 | |
| 234 | sm = segment_manager_get (*sm_indexp); |
| 235 | if (app_wrk->first_segment_manager == *sm_indexp) |
| 236 | { |
| 237 | /* Delete sessions but don't remove segment manager */ |
| 238 | app_wrk->first_segment_manager_in_use = 0; |
| 239 | segment_manager_del_sessions (sm); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | segment_manager_init_del (sm); |
| 244 | } |
| 245 | hash_unset (app_wrk->listeners_table, handle); |
| 246 | } |
| 247 | |
| 248 | int |
| 249 | app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al) |
| 250 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 251 | session_t *ls; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 252 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 253 | if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index)) |
| 254 | return 0; |
| 255 | |
| 256 | if (al->session_index != SESSION_INVALID_INDEX) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 257 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 258 | ls = listen_session_get (al->session_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 259 | app_worker_stop_listen_session (app_wrk, ls); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 262 | if (al->local_index != SESSION_INVALID_INDEX) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 263 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 264 | ls = listen_session_get (al->local_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 265 | app_worker_stop_listen_session (app_wrk, ls); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 266 | } |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 267 | |
| 268 | clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0); |
| 269 | if (clib_bitmap_is_zero (al->workers)) |
| 270 | app_listener_cleanup (al); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | int |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 276 | app_worker_init_accepted (session_t * s) |
| 277 | { |
| 278 | app_worker_t *app_wrk; |
| 279 | segment_manager_t *sm; |
| 280 | session_t *listener; |
| 281 | |
| 282 | listener = listen_session_get (s->listener_index); |
| 283 | app_wrk = application_listener_select_worker (listener); |
| 284 | s->app_wrk_index = app_wrk->wrk_index; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 285 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 286 | sm = app_worker_get_listen_segment_manager (app_wrk, listener); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 287 | if (app_worker_alloc_session_fifos (sm, s)) |
| 288 | return -1; |
| 289 | |
| 290 | return 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int |
| 294 | app_worker_accept_notify (app_worker_t * app_wrk, session_t * s) |
| 295 | { |
| 296 | application_t *app = application_get (app_wrk->app_index); |
| 297 | return app->cb_fns.session_accept_callback (s); |
| 298 | } |
| 299 | |
| 300 | int |
| 301 | app_worker_init_connected (app_worker_t * app_wrk, session_t * s) |
| 302 | { |
| 303 | application_t *app = application_get (app_wrk->app_index); |
| 304 | segment_manager_t *sm; |
| 305 | |
| 306 | /* Allocate fifos for session, unless the app is a builtin proxy */ |
| 307 | if (!application_is_builtin_proxy (app)) |
| 308 | { |
| 309 | sm = app_worker_get_connect_segment_manager (app_wrk); |
| 310 | if (app_worker_alloc_session_fifos (sm, s)) |
| 311 | return -1; |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | app_worker_connect_notify (app_worker_t * app_wrk, session_t * s, u32 opaque) |
| 318 | { |
| 319 | application_t *app = application_get (app_wrk->app_index); |
| 320 | return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque, |
| 321 | s, s == 0 /* is_fail */ ); |
| 322 | } |
| 323 | |
| 324 | int |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 325 | app_worker_close_notify (app_worker_t * app_wrk, session_t * s) |
| 326 | { |
| 327 | application_t *app = application_get (app_wrk->app_index); |
| 328 | app->cb_fns.session_disconnect_callback (s); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 333 | app_worker_reset_notify (app_worker_t * app_wrk, session_t * s) |
| 334 | { |
| 335 | application_t *app = application_get (app_wrk->app_index); |
| 336 | app->cb_fns.session_reset_callback (s); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 341 | app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s) |
| 342 | { |
| 343 | application_t *app = application_get (app_wrk->app_index); |
| 344 | app->cb_fns.builtin_app_rx_callback (s); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 349 | app_worker_own_session (app_worker_t * app_wrk, session_t * s) |
| 350 | { |
| 351 | segment_manager_t *sm; |
| 352 | svm_fifo_t *rxf, *txf; |
| 353 | |
| 354 | if (s->session_state == SESSION_STATE_LISTENING) |
| 355 | return application_change_listener_owner (s, app_wrk); |
| 356 | |
| 357 | s->app_wrk_index = app_wrk->wrk_index; |
| 358 | |
| 359 | rxf = s->rx_fifo; |
| 360 | txf = s->tx_fifo; |
| 361 | |
| 362 | if (!rxf || !txf) |
| 363 | return 0; |
| 364 | |
| 365 | s->rx_fifo = 0; |
| 366 | s->tx_fifo = 0; |
| 367 | |
| 368 | sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 369 | if (app_worker_alloc_session_fifos (sm, s)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 370 | return -1; |
| 371 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 372 | if (!svm_fifo_is_empty_cons (rxf)) |
| 373 | svm_fifo_clone (s->rx_fifo, rxf); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 374 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 375 | if (!svm_fifo_is_empty_cons (txf)) |
| 376 | svm_fifo_clone (s->tx_fifo, txf); |
| 377 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 378 | |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 379 | segment_manager_dealloc_fifos (rxf, txf); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 385 | app_worker_connect_session (app_worker_t * app, session_endpoint_t * sep, |
| 386 | u32 api_context) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 387 | { |
| 388 | int rv; |
| 389 | |
| 390 | /* Make sure we have a segment manager for connects */ |
| 391 | app_worker_alloc_connects_segment_manager (app); |
| 392 | |
| 393 | if ((rv = session_open (app->wrk_index, sep, api_context))) |
| 394 | return rv; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | int |
| 400 | app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk) |
| 401 | { |
| 402 | segment_manager_t *sm; |
| 403 | |
| 404 | if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX) |
| 405 | { |
| 406 | sm = app_worker_alloc_segment_manager (app_wrk); |
| 407 | if (sm == 0) |
| 408 | return -1; |
| 409 | app_wrk->connects_seg_manager = segment_manager_index (sm); |
| 410 | } |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | segment_manager_t * |
| 415 | app_worker_get_connect_segment_manager (app_worker_t * app) |
| 416 | { |
| 417 | ASSERT (app->connects_seg_manager != (u32) ~ 0); |
| 418 | return segment_manager_get (app->connects_seg_manager); |
| 419 | } |
| 420 | |
| 421 | segment_manager_t * |
| 422 | app_worker_get_or_alloc_connect_segment_manager (app_worker_t * app_wrk) |
| 423 | { |
| 424 | if (app_wrk->connects_seg_manager == (u32) ~ 0) |
| 425 | app_worker_alloc_connects_segment_manager (app_wrk); |
| 426 | return segment_manager_get (app_wrk->connects_seg_manager); |
| 427 | } |
| 428 | |
| 429 | segment_manager_t * |
| 430 | app_worker_get_listen_segment_manager (app_worker_t * app, |
| 431 | session_t * listener) |
| 432 | { |
| 433 | uword *smp; |
| 434 | smp = hash_get (app->listeners_table, listen_session_get_handle (listener)); |
| 435 | ASSERT (smp != 0); |
| 436 | return segment_manager_get (*smp); |
| 437 | } |
| 438 | |
| 439 | session_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 440 | app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto, |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 441 | u8 transport_proto) |
| 442 | { |
| 443 | session_t *listener; |
| 444 | u64 handle; |
| 445 | u32 sm_index; |
| 446 | u8 sst; |
| 447 | |
| 448 | sst = session_type_from_proto_and_ip (transport_proto, |
| 449 | fib_proto == FIB_PROTOCOL_IP4); |
| 450 | |
| 451 | /* *INDENT-OFF* */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 452 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 453 | listener = listen_session_get_from_handle (handle); |
| 454 | if (listener->session_type == sst |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 455 | && !(listener->flags & SESSION_F_PROXY)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 456 | return listener; |
| 457 | })); |
| 458 | /* *INDENT-ON* */ |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | session_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 464 | app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto, |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 465 | u8 transport_proto) |
| 466 | { |
| 467 | session_t *listener; |
| 468 | u64 handle; |
| 469 | u32 sm_index; |
| 470 | u8 sst; |
| 471 | |
| 472 | sst = session_type_from_proto_and_ip (transport_proto, |
| 473 | fib_proto == FIB_PROTOCOL_IP4); |
| 474 | |
| 475 | /* *INDENT-OFF* */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 476 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 477 | listener = listen_session_get_from_handle (handle); |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 478 | if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 479 | return listener; |
| 480 | })); |
| 481 | /* *INDENT-ON* */ |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Send an API message to the external app, to map new segment |
| 488 | */ |
| 489 | int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 490 | app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 491 | { |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 492 | application_t *app = application_get (app_wrk->app_index); |
| 493 | return app->cb_fns.add_segment_callback (app_wrk->api_client_index, |
| 494 | segment_handle); |
| 495 | } |
| 496 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 497 | int |
| 498 | app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle) |
| 499 | { |
| 500 | application_t *app = application_get (app_wrk->app_index); |
| 501 | return app->cb_fns.del_segment_callback (app_wrk->api_client_index, |
| 502 | segment_handle); |
| 503 | } |
| 504 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 505 | static inline u8 |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 506 | app_worker_application_is_builtin (app_worker_t * app_wrk) |
| 507 | { |
| 508 | return app_wrk->app_is_builtin; |
| 509 | } |
| 510 | |
| 511 | static inline int |
| 512 | app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock) |
| 513 | { |
| 514 | if (PREDICT_FALSE (svm_msg_q_is_full (mq))) |
| 515 | { |
| 516 | clib_warning ("evt q full"); |
| 517 | svm_msg_q_free_msg (mq, msg); |
| 518 | if (lock) |
| 519 | svm_msg_q_unlock (mq); |
| 520 | return -1; |
| 521 | } |
| 522 | |
| 523 | if (lock) |
| 524 | { |
| 525 | svm_msg_q_add_and_unlock (mq, msg); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* Even when not locking the ring, we must wait for queue mutex */ |
| 530 | if (svm_msg_q_add (mq, msg, SVM_Q_WAIT)) |
| 531 | { |
| 532 | clib_warning ("msg q add returned"); |
| 533 | return -1; |
| 534 | } |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static inline int |
| 539 | app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s, u8 lock) |
| 540 | { |
| 541 | session_event_t *evt; |
| 542 | svm_msg_q_msg_t msg; |
| 543 | svm_msg_q_t *mq; |
| 544 | |
| 545 | if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY |
| 546 | && s->session_state != SESSION_STATE_LISTENING)) |
Florin Coras | 096f2f8 | 2019-04-02 10:17:48 -0700 | [diff] [blame] | 547 | return 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 548 | |
| 549 | if (app_worker_application_is_builtin (app_wrk)) |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 550 | return app_worker_builtin_rx (app_wrk, s); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 551 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 552 | if (svm_fifo_has_event (s->rx_fifo)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 553 | return 0; |
| 554 | |
| 555 | mq = app_wrk->event_queue; |
| 556 | if (lock) |
| 557 | svm_msg_q_lock (mq); |
| 558 | |
| 559 | if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))) |
| 560 | { |
| 561 | clib_warning ("evt q rings full"); |
| 562 | if (lock) |
| 563 | svm_msg_q_unlock (mq); |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 568 | ASSERT (!svm_msg_q_msg_is_invalid (&msg)); |
| 569 | |
| 570 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | c0737e9 | 2019-03-04 14:19:39 -0800 | [diff] [blame] | 571 | evt->session_index = s->rx_fifo->client_session_index; |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 572 | evt->event_type = SESSION_IO_EVT_RX; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 573 | |
| 574 | (void) svm_fifo_set_event (s->rx_fifo); |
| 575 | |
| 576 | if (app_enqueue_evt (mq, &msg, lock)) |
| 577 | return -1; |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static inline int |
| 582 | app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s, u8 lock) |
| 583 | { |
| 584 | svm_msg_q_t *mq; |
| 585 | session_event_t *evt; |
| 586 | svm_msg_q_msg_t msg; |
| 587 | |
| 588 | if (app_worker_application_is_builtin (app_wrk)) |
| 589 | return 0; |
| 590 | |
| 591 | mq = app_wrk->event_queue; |
| 592 | if (lock) |
| 593 | svm_msg_q_lock (mq); |
| 594 | |
| 595 | if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))) |
| 596 | { |
| 597 | clib_warning ("evt q rings full"); |
| 598 | if (lock) |
| 599 | svm_msg_q_unlock (mq); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 604 | ASSERT (!svm_msg_q_msg_is_invalid (&msg)); |
| 605 | |
| 606 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 607 | evt->event_type = SESSION_IO_EVT_TX; |
Florin Coras | c0737e9 | 2019-03-04 14:19:39 -0800 | [diff] [blame] | 608 | evt->session_index = s->tx_fifo->client_session_index; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 609 | |
| 610 | return app_enqueue_evt (mq, &msg, lock); |
| 611 | } |
| 612 | |
| 613 | /* *INDENT-OFF* */ |
| 614 | typedef int (app_send_evt_handler_fn) (app_worker_t *app, |
| 615 | session_t *s, |
| 616 | u8 lock); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 617 | static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = { |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 618 | app_send_io_evt_rx, |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 619 | app_send_io_evt_tx, |
| 620 | }; |
| 621 | /* *INDENT-ON* */ |
| 622 | |
| 623 | /** |
| 624 | * Send event to application |
| 625 | * |
| 626 | * Logic from queue perspective is non-blocking. If there's |
| 627 | * not enough space to enqueue a message, we return. |
| 628 | */ |
| 629 | int |
| 630 | app_worker_send_event (app_worker_t * app, session_t * s, u8 evt_type) |
| 631 | { |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 632 | ASSERT (app && evt_type <= SESSION_IO_EVT_TX); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 633 | return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ ); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Send event to application |
| 638 | * |
| 639 | * Logic from queue perspective is blocking. However, if queue is full, |
| 640 | * we return. |
| 641 | */ |
| 642 | int |
| 643 | app_worker_lock_and_send_event (app_worker_t * app, session_t * s, |
| 644 | u8 evt_type) |
| 645 | { |
| 646 | return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ ); |
| 647 | } |
| 648 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 649 | u8 * |
| 650 | format_app_worker_listener (u8 * s, va_list * args) |
| 651 | { |
| 652 | app_worker_t *app_wrk = va_arg (*args, app_worker_t *); |
| 653 | u64 handle = va_arg (*args, u64); |
| 654 | u32 sm_index = va_arg (*args, u32); |
| 655 | int verbose = va_arg (*args, int); |
| 656 | session_t *listener; |
| 657 | const u8 *app_name; |
| 658 | u8 *str; |
| 659 | |
| 660 | if (!app_wrk) |
| 661 | { |
| 662 | if (verbose) |
| 663 | s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App", |
| 664 | "Wrk", "API Client", "ListenerID", "SegManager"); |
| 665 | else |
| 666 | s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk"); |
| 667 | |
| 668 | return s; |
| 669 | } |
| 670 | |
| 671 | app_name = application_name_from_index (app_wrk->app_index); |
| 672 | listener = listen_session_get_from_handle (handle); |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 673 | str = format (0, "%U", format_session, listener, verbose); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 674 | |
| 675 | if (verbose) |
| 676 | { |
| 677 | char buf[32]; |
| 678 | sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index); |
| 679 | s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name, |
| 680 | buf, app_wrk->api_client_index, handle, sm_index); |
| 681 | } |
| 682 | else |
| 683 | s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index); |
| 684 | |
| 685 | return s; |
| 686 | } |
| 687 | |
| 688 | u8 * |
| 689 | format_app_worker (u8 * s, va_list * args) |
| 690 | { |
| 691 | app_worker_t *app_wrk = va_arg (*args, app_worker_t *); |
| 692 | u32 indent = 1; |
| 693 | |
| 694 | s = format (s, "%U wrk-index %u app-index %u map-index %u " |
| 695 | "api-client-index %d\n", format_white_space, indent, |
| 696 | app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index, |
| 697 | app_wrk->api_client_index); |
| 698 | return s; |
| 699 | } |
| 700 | |
| 701 | void |
| 702 | app_worker_format_connects (app_worker_t * app_wrk, int verbose) |
| 703 | { |
| 704 | svm_fifo_segment_private_t *fifo_segment; |
| 705 | vlib_main_t *vm = vlib_get_main (); |
| 706 | segment_manager_t *sm; |
| 707 | const u8 *app_name; |
| 708 | u8 *s = 0; |
| 709 | |
| 710 | /* Header */ |
| 711 | if (!app_wrk) |
| 712 | { |
| 713 | if (verbose) |
| 714 | vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App", |
| 715 | "API Client", "SegManager"); |
| 716 | else |
| 717 | vlib_cli_output (vm, "%-40s%-20s", "Connection", "App"); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | if (app_wrk->connects_seg_manager == (u32) ~ 0) |
| 722 | return; |
| 723 | |
| 724 | app_name = application_name_from_index (app_wrk->app_index); |
| 725 | |
| 726 | /* Across all fifo segments */ |
| 727 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
| 728 | |
| 729 | /* *INDENT-OFF* */ |
| 730 | segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({ |
| 731 | svm_fifo_t *fifo; |
| 732 | u8 *str; |
| 733 | |
| 734 | fifo = svm_fifo_segment_get_fifo_list (fifo_segment); |
| 735 | while (fifo) |
| 736 | { |
| 737 | u32 session_index, thread_index; |
| 738 | session_t *session; |
| 739 | |
| 740 | session_index = fifo->master_session_index; |
| 741 | thread_index = fifo->master_thread_index; |
| 742 | |
| 743 | session = session_get (session_index, thread_index); |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 744 | str = format (0, "%U", format_session, session, verbose); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 745 | |
| 746 | if (verbose) |
| 747 | s = format (s, "%-40s%-20s%-15u%-10u", str, app_name, |
| 748 | app_wrk->api_client_index, app_wrk->connects_seg_manager); |
| 749 | else |
| 750 | s = format (s, "%-40s%-20s", str, app_name); |
| 751 | |
| 752 | vlib_cli_output (vm, "%v", s); |
| 753 | vec_reset_length (s); |
| 754 | vec_free (str); |
| 755 | |
| 756 | fifo = fifo->next; |
| 757 | } |
| 758 | vec_free (s); |
| 759 | })); |
| 760 | /* *INDENT-ON* */ |
| 761 | } |
| 762 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 763 | /* |
| 764 | * fd.io coding-style-patch-verification: ON |
| 765 | * |
| 766 | * Local Variables: |
| 767 | * eval: (c-set-style "gnu") |
| 768 | * End: |
| 769 | */ |