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