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; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 29 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 30 | pool_get (app_workers, app_wrk); |
| 31 | clib_memset (app_wrk, 0, sizeof (*app_wrk)); |
| 32 | app_wrk->wrk_index = app_wrk - app_workers; |
| 33 | app_wrk->app_index = app->app_index; |
| 34 | app_wrk->wrk_map_index = ~0; |
| 35 | app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX; |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 36 | clib_spinlock_init (&app_wrk->detached_seg_managers_lock); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 37 | vec_validate (app_wrk->wrk_evts, vlib_num_workers ()); |
| 38 | vec_validate (app_wrk->wrk_mq_congested, vlib_num_workers ()); |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 39 | 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] | 40 | return app_wrk; |
| 41 | } |
| 42 | |
| 43 | app_worker_t * |
| 44 | app_worker_get (u32 wrk_index) |
| 45 | { |
| 46 | return pool_elt_at_index (app_workers, wrk_index); |
| 47 | } |
| 48 | |
| 49 | app_worker_t * |
| 50 | app_worker_get_if_valid (u32 wrk_index) |
| 51 | { |
| 52 | if (pool_is_free_index (app_workers, wrk_index)) |
| 53 | return 0; |
| 54 | return pool_elt_at_index (app_workers, wrk_index); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | app_worker_free (app_worker_t * app_wrk) |
| 59 | { |
| 60 | application_t *app = application_get (app_wrk->app_index); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 61 | vnet_unlisten_args_t _a, *a = &_a; |
Florin Coras | bf39597 | 2020-04-30 15:05:24 +0000 | [diff] [blame] | 62 | u64 handle, *handles = 0, *sm_indices = 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 63 | segment_manager_t *sm; |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 64 | session_handle_t *sh; |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 65 | session_t *ls; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 66 | u32 sm_index; |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 67 | int i; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 68 | |
| 69 | /* |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 70 | * Cleanup vpp wrk events |
| 71 | */ |
| 72 | app_worker_del_all_events (app_wrk); |
| 73 | for (i = 0; i < vec_len (app_wrk->wrk_evts); i++) |
| 74 | clib_fifo_free (app_wrk->wrk_evts[i]); |
| 75 | |
| 76 | vec_free (app_wrk->wrk_evts); |
| 77 | vec_free (app_wrk->wrk_mq_congested); |
| 78 | |
| 79 | /* |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 80 | * Listener cleanup |
| 81 | */ |
| 82 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 83 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
| 84 | ls = listen_session_get_from_handle (handle); |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 85 | vec_add1 (handles, app_listen_session_handle (ls)); |
Florin Coras | bf39597 | 2020-04-30 15:05:24 +0000 | [diff] [blame] | 86 | vec_add1 (sm_indices, sm_index); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 87 | sm = segment_manager_get (sm_index); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 88 | })); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 89 | |
| 90 | for (i = 0; i < vec_len (handles); i++) |
| 91 | { |
Florin Coras | bf39597 | 2020-04-30 15:05:24 +0000 | [diff] [blame] | 92 | /* Cleanup listener */ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 93 | a->app_index = app->app_index; |
| 94 | a->wrk_map_index = app_wrk->wrk_map_index; |
| 95 | a->handle = handles[i]; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 96 | (void) vnet_unlisten (a); |
Florin Coras | bf39597 | 2020-04-30 15:05:24 +0000 | [diff] [blame] | 97 | |
| 98 | sm = segment_manager_get_if_valid (sm_indices[i]); |
| 99 | if (sm && !segment_manager_app_detached (sm)) |
| 100 | { |
| 101 | sm->first_is_protected = 0; |
| 102 | segment_manager_init_free (sm); |
| 103 | } |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 104 | } |
Florin Coras | ed4e612 | 2024-01-12 13:10:17 -0500 | [diff] [blame] | 105 | vec_free (handles); |
Florin Coras | bf39597 | 2020-04-30 15:05:24 +0000 | [diff] [blame] | 106 | vec_free (sm_indices); |
| 107 | hash_free (app_wrk->listeners_table); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Connects segment manager cleanup |
| 111 | */ |
| 112 | |
| 113 | if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX) |
| 114 | { |
| 115 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
| 116 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
Florin Coras | 565115e | 2019-02-20 19:48:31 -0800 | [diff] [blame] | 117 | sm->first_is_protected = 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 118 | segment_manager_init_free (sm); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Half-open cleanup |
| 123 | */ |
| 124 | |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 125 | pool_foreach (sh, app_wrk->half_open_table) |
| 126 | session_cleanup_half_open (*sh); |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 127 | |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 128 | pool_free (app_wrk->half_open_table); |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 129 | |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 130 | /* |
| 131 | * Detached listener segment managers cleanup |
| 132 | */ |
| 133 | for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++) |
| 134 | { |
| 135 | sm = segment_manager_get (app_wrk->detached_seg_managers[i]); |
| 136 | segment_manager_init_free (sm); |
| 137 | } |
| 138 | vec_free (app_wrk->detached_seg_managers); |
| 139 | clib_spinlock_free (&app_wrk->detached_seg_managers_lock); |
| 140 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 141 | if (CLIB_DEBUG) |
| 142 | clib_memset (app_wrk, 0xfe, sizeof (*app_wrk)); |
Benoît Ganne | d4aeb84 | 2019-07-18 18:38:42 +0200 | [diff] [blame] | 143 | pool_put (app_workers, app_wrk); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | application_t * |
| 147 | app_worker_get_app (u32 wrk_index) |
| 148 | { |
| 149 | app_worker_t *app_wrk; |
| 150 | app_wrk = app_worker_get_if_valid (wrk_index); |
| 151 | if (!app_wrk) |
| 152 | return 0; |
| 153 | return application_get_if_valid (app_wrk->app_index); |
| 154 | } |
| 155 | |
| 156 | static segment_manager_t * |
| 157 | app_worker_alloc_segment_manager (app_worker_t * app_wrk) |
| 158 | { |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 159 | segment_manager_t *sm; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 160 | |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 161 | sm = segment_manager_alloc (); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 162 | sm->app_wrk_index = app_wrk->wrk_index; |
Florin Coras | a107f40 | 2020-09-29 19:18:46 -0700 | [diff] [blame] | 163 | segment_manager_init (sm); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 164 | return sm; |
| 165 | } |
| 166 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 167 | static int |
| 168 | app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s) |
| 169 | { |
| 170 | svm_fifo_t *rx_fifo = 0, *tx_fifo = 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 171 | int rv; |
| 172 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 173 | if ((rv = segment_manager_alloc_session_fifos (sm, s->thread_index, |
| 174 | &rx_fifo, &tx_fifo))) |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 175 | return rv; |
| 176 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 177 | rx_fifo->shr->master_session_index = s->session_index; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 178 | rx_fifo->master_thread_index = s->thread_index; |
| 179 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 180 | tx_fifo->shr->master_session_index = s->session_index; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 181 | tx_fifo->master_thread_index = s->thread_index; |
| 182 | |
| 183 | s->rx_fifo = rx_fifo; |
| 184 | s->tx_fifo = tx_fifo; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 188 | int |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 189 | app_worker_alloc_wrk_cl_session (app_worker_t *app_wrk, session_t *ls) |
| 190 | { |
| 191 | svm_fifo_t *rx_fifo = 0, *tx_fifo = 0; |
| 192 | segment_manager_t *sm; |
| 193 | session_handle_t lsh; |
| 194 | app_listener_t *al; |
| 195 | session_t *s; |
| 196 | |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 197 | al = app_listener_get (ls->al_index); |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 198 | sm = app_worker_get_listen_segment_manager (app_wrk, ls); |
| 199 | lsh = session_handle (ls); |
| 200 | |
| 201 | s = session_alloc (0 /* listener on main worker */); |
| 202 | session_set_state (s, SESSION_STATE_LISTENING); |
| 203 | s->flags |= SESSION_F_IS_CLESS; |
| 204 | s->app_wrk_index = app_wrk->wrk_index; |
| 205 | ls = session_get_from_handle (lsh); |
| 206 | s->session_type = ls->session_type; |
| 207 | s->connection_index = ls->connection_index; |
| 208 | |
| 209 | segment_manager_alloc_session_fifos (sm, s->thread_index, &rx_fifo, |
| 210 | &tx_fifo); |
| 211 | |
| 212 | rx_fifo->shr->master_session_index = s->session_index; |
| 213 | rx_fifo->master_thread_index = s->thread_index; |
| 214 | |
| 215 | tx_fifo->shr->master_session_index = s->session_index; |
| 216 | tx_fifo->master_thread_index = s->thread_index; |
| 217 | |
| 218 | s->rx_fifo = rx_fifo; |
| 219 | s->tx_fifo = tx_fifo; |
| 220 | |
| 221 | vec_validate (al->cl_listeners, app_wrk->wrk_map_index); |
| 222 | al->cl_listeners[app_wrk->wrk_map_index] = s->session_index; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | app_worker_free_wrk_cl_session (app_worker_t *app_wrk, session_t *ls) |
| 229 | { |
| 230 | app_listener_t *al; |
| 231 | session_t *s; |
| 232 | |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 233 | al = app_listener_get (ls->al_index); |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 234 | |
| 235 | s = app_listener_get_wrk_cl_session (al, app_wrk->wrk_map_index); |
| 236 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 237 | session_free (s); |
| 238 | |
| 239 | al->cl_listeners[app_wrk->wrk_map_index] = SESSION_INVALID_INDEX; |
| 240 | } |
| 241 | |
| 242 | int |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 243 | app_worker_init_listener (app_worker_t * app_wrk, session_t * ls) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 244 | { |
| 245 | segment_manager_t *sm; |
| 246 | |
| 247 | /* Allocate segment manager. All sessions derived out of a listen session |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 248 | * have fifos allocated by the same segment manager. |
| 249 | * TODO(fcoras): limit memory consumption by cless listeners */ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 250 | if (!(sm = app_worker_alloc_segment_manager (app_wrk))) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 251 | return SESSION_E_ALLOC; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 252 | |
Florin Coras | 1a4aaf1 | 2021-11-27 10:30:03 -0800 | [diff] [blame] | 253 | /* Once the first segment is mapped, don't remove it until unlisten */ |
| 254 | sm->first_is_protected = 1; |
| 255 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 256 | /* Keep track of the segment manager for the listener or this worker */ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 257 | hash_set (app_wrk->listeners_table, listen_session_get_handle (ls), |
| 258 | segment_manager_index (sm)); |
| 259 | |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 260 | if (ls->flags & SESSION_F_IS_CLESS) |
| 261 | return app_worker_alloc_wrk_cl_session (app_wrk, ls); |
| 262 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 266 | session_error_t |
| 267 | app_worker_start_listen (app_worker_t *app_wrk, app_listener_t *app_listener) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 268 | { |
| 269 | session_t *ls; |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 270 | int rv; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 271 | |
| 272 | if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 273 | return SESSION_E_ALREADY_LISTENING; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 274 | |
| 275 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 276 | app_wrk->wrk_map_index, 1); |
| 277 | |
| 278 | if (app_listener->session_index != SESSION_INVALID_INDEX) |
| 279 | { |
| 280 | ls = session_get (app_listener->session_index, 0); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 281 | if ((rv = app_worker_init_listener (app_wrk, ls))) |
| 282 | return rv; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (app_listener->local_index != SESSION_INVALID_INDEX) |
| 286 | { |
| 287 | ls = session_get (app_listener->local_index, 0); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 288 | if ((rv = app_worker_init_listener (app_wrk, ls))) |
| 289 | return rv; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 295 | static void |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 296 | app_worker_add_detached_sm (app_worker_t * app_wrk, u32 sm_index) |
| 297 | { |
| 298 | vec_add1 (app_wrk->detached_seg_managers, sm_index); |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index) |
| 303 | { |
| 304 | u32 i; |
| 305 | |
| 306 | clib_spinlock_lock (&app_wrk->detached_seg_managers_lock); |
| 307 | for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++) |
| 308 | { |
| 309 | if (app_wrk->detached_seg_managers[i] == sm_index) |
| 310 | { |
| 311 | vec_del1 (app_wrk->detached_seg_managers, i); |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | clib_spinlock_unlock (&app_wrk->detached_seg_managers_lock); |
| 316 | } |
| 317 | |
| 318 | static void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 319 | 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] | 320 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 321 | session_handle_t handle; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 322 | segment_manager_t *sm; |
| 323 | uword *sm_indexp; |
liuyacan | 87d48ad | 2021-04-28 11:34:03 +0000 | [diff] [blame] | 324 | session_state_t *states = 0; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 325 | |
| 326 | handle = listen_session_get_handle (ls); |
| 327 | sm_indexp = hash_get (app_wrk->listeners_table, handle); |
| 328 | if (PREDICT_FALSE (!sm_indexp)) |
| 329 | return; |
| 330 | |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 331 | if (ls->flags & SESSION_F_IS_CLESS) |
| 332 | app_worker_free_wrk_cl_session (app_wrk, ls); |
Florin Coras | 87b7e3d | 2020-03-27 15:06:07 +0000 | [diff] [blame] | 333 | |
Florin Coras | 1bd4616 | 2020-04-13 23:35:55 +0000 | [diff] [blame] | 334 | /* Try to cleanup segment manager */ |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 335 | sm = segment_manager_get (*sm_indexp); |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 336 | if (sm) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 337 | { |
Florin Coras | 1a4aaf1 | 2021-11-27 10:30:03 -0800 | [diff] [blame] | 338 | sm->first_is_protected = 0; |
Florin Coras | 1bd4616 | 2020-04-13 23:35:55 +0000 | [diff] [blame] | 339 | segment_manager_app_detach (sm); |
| 340 | if (!segment_manager_has_fifos (sm)) |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 341 | { |
| 342 | /* Empty segment manager, cleanup it up */ |
| 343 | segment_manager_free (sm); |
| 344 | } |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 345 | else |
| 346 | { |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 347 | /* Delete sessions in CREATED state */ |
| 348 | vec_add1 (states, SESSION_STATE_CREATED); |
| 349 | segment_manager_del_sessions_filter (sm, states); |
| 350 | vec_free (states); |
| 351 | |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 352 | /* Track segment manager in case app detaches and all the |
| 353 | * outstanding sessions need to be closed */ |
| 354 | app_worker_add_detached_sm (app_wrk, *sm_indexp); |
| 355 | sm->flags |= SEG_MANAGER_F_DETACHED_LISTENER; |
| 356 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 357 | } |
Florin Coras | 1bd4616 | 2020-04-13 23:35:55 +0000 | [diff] [blame] | 358 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 359 | hash_unset (app_wrk->listeners_table, handle); |
| 360 | } |
| 361 | |
| 362 | int |
| 363 | app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al) |
| 364 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 365 | session_t *ls; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 366 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 367 | if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index)) |
| 368 | return 0; |
| 369 | |
| 370 | if (al->session_index != SESSION_INVALID_INDEX) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 371 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 372 | ls = listen_session_get (al->session_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 373 | app_worker_stop_listen_session (app_wrk, ls); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 376 | if (al->local_index != SESSION_INVALID_INDEX) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 377 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 378 | ls = listen_session_get (al->local_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 379 | app_worker_stop_listen_session (app_wrk, ls); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 380 | } |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 381 | |
| 382 | clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0); |
| 383 | if (clib_bitmap_is_zero (al->workers)) |
| 384 | app_listener_cleanup (al); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | int |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 390 | app_worker_init_accepted (session_t * s) |
| 391 | { |
| 392 | app_worker_t *app_wrk; |
| 393 | segment_manager_t *sm; |
| 394 | session_t *listener; |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 395 | application_t *app; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 396 | |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 397 | listener = listen_session_get_from_handle (s->listener_handle); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 398 | app_wrk = application_listener_select_worker (listener); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 399 | if (PREDICT_FALSE (app_worker_mq_is_congested (app_wrk))) |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 400 | return -1; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 401 | |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 402 | s->app_wrk_index = app_wrk->wrk_index; |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 403 | app = application_get (app_wrk->app_index); |
| 404 | if (app->cb_fns.fifo_tuning_callback) |
| 405 | s->flags |= SESSION_F_CUSTOM_FIFO_TUNING; |
| 406 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 407 | sm = app_worker_get_listen_segment_manager (app_wrk, listener); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 408 | if (app_worker_alloc_session_fifos (sm, s)) |
| 409 | return -1; |
| 410 | |
| 411 | return 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | int |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 415 | app_worker_listened_notify (app_worker_t *app_wrk, session_handle_t alsh, |
| 416 | u32 opaque, session_error_t err) |
| 417 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 418 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_BOUND, |
| 419 | .as_u64[0] = alsh, |
Florin Coras | 4197fb9 | 2023-10-27 09:21:50 -0700 | [diff] [blame] | 420 | .as_u64[1] = (u64) opaque << 32 | (u32) err }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 421 | |
| 422 | app_worker_add_event_custom (app_wrk, 0 /* thread index */, &evt); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int |
| 428 | app_worker_unlisten_reply (app_worker_t *app_wrk, session_handle_t sh, |
| 429 | u32 opaque, session_error_t err) |
| 430 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 431 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY, |
| 432 | .as_u64[0] = sh, |
| 433 | .as_u64[1] = (u64) opaque << 32 | (u32) err }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 434 | |
| 435 | app_worker_add_event_custom (app_wrk, 0 /* thread index */, &evt); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | int |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 440 | app_worker_accept_notify (app_worker_t * app_wrk, session_t * s) |
| 441 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 442 | app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_ACCEPTED); |
| 443 | return 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int |
| 447 | app_worker_init_connected (app_worker_t * app_wrk, session_t * s) |
| 448 | { |
| 449 | application_t *app = application_get (app_wrk->app_index); |
| 450 | segment_manager_t *sm; |
| 451 | |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 452 | if (app->cb_fns.fifo_tuning_callback) |
| 453 | s->flags |= SESSION_F_CUSTOM_FIFO_TUNING; |
| 454 | |
Florin Coras | e655240 | 2020-11-11 13:25:53 -0800 | [diff] [blame] | 455 | /* Allocate fifos for session, unless the app is a builtin proxy */ |
| 456 | if (application_is_builtin_proxy (app)) |
Florin Coras | 2ceb818 | 2023-08-31 17:33:47 -0700 | [diff] [blame] | 457 | return app->cb_fns.proxy_alloc_session_fifos (s); |
Florin Coras | e655240 | 2020-11-11 13:25:53 -0800 | [diff] [blame] | 458 | |
| 459 | sm = app_worker_get_connect_segment_manager (app_wrk); |
| 460 | return app_worker_alloc_session_fifos (sm, s); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | int |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 464 | app_worker_connect_notify (app_worker_t * app_wrk, session_t * s, |
| 465 | session_error_t err, u32 opaque) |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 466 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 467 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_CONNECTED, |
| 468 | .as_u64[0] = s ? s->session_index : ~0, |
| 469 | .as_u64[1] = (u64) opaque << 32 | (u32) err }; |
| 470 | u32 thread_index = s ? s->thread_index : vlib_get_thread_index (); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 471 | |
| 472 | app_worker_add_event_custom (app_wrk, thread_index, &evt); |
| 473 | return 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | int |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 477 | app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh) |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 478 | { |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 479 | session_handle_t *shp; |
| 480 | |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 481 | ASSERT (session_vlib_thread_is_cl_thread ()); |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 482 | pool_get (app_wrk->half_open_table, shp); |
| 483 | *shp = sh; |
| 484 | |
| 485 | return (shp - app_wrk->half_open_table); |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | int |
Florin Coras | 2c876f9 | 2021-05-10 21:12:27 -0700 | [diff] [blame] | 489 | app_worker_del_half_open (app_worker_t *app_wrk, session_t *s) |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 490 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 491 | app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_HALF_CLEANUP); |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 495 | int |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 496 | app_worker_close_notify (app_worker_t * app_wrk, session_t * s) |
| 497 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 498 | app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_DISCONNECTED); |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | int |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 503 | app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s) |
| 504 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 505 | app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_TRANSPORT_CLOSED); |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | int |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 510 | app_worker_reset_notify (app_worker_t * app_wrk, session_t * s) |
| 511 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 512 | app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_RESET); |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | int |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 517 | app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s, |
| 518 | session_cleanup_ntf_t ntf) |
| 519 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 520 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_CLEANUP, |
| 521 | .as_u64[0] = (u64) ntf << 32 | s->session_index, |
| 522 | .as_u64[1] = pointer_to_uword (session_cleanup) }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 523 | |
| 524 | app_worker_add_event_custom (app_wrk, s->thread_index, &evt); |
| 525 | |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | int |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 530 | app_worker_cleanup_notify_custom (app_worker_t *app_wrk, session_t *s, |
| 531 | session_cleanup_ntf_t ntf, |
| 532 | void (*cleanup_cb) (session_t *s)) |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 533 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 534 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_CLEANUP, |
| 535 | .as_u64[0] = (u64) ntf << 32 | s->session_index, |
| 536 | .as_u64[1] = pointer_to_uword (cleanup_cb) }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 537 | |
| 538 | app_worker_add_event_custom (app_wrk, s->thread_index, &evt); |
| 539 | |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | int |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 544 | app_worker_rx_notify (app_worker_t *app_wrk, session_t *s) |
Florin Coras | 0e573f5 | 2019-05-07 16:28:16 -0700 | [diff] [blame] | 545 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 546 | app_worker_add_event (app_wrk, s, SESSION_IO_EVT_RX); |
Florin Coras | 0e573f5 | 2019-05-07 16:28:16 -0700 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | int |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 551 | app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s, |
| 552 | session_handle_t new_sh) |
| 553 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 554 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_MIGRATED, |
| 555 | .as_u64[0] = s->session_index, |
| 556 | .as_u64[1] = new_sh }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 557 | |
| 558 | app_worker_add_event_custom (app_wrk, s->thread_index, &evt); |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | int |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 563 | app_worker_own_session (app_worker_t * app_wrk, session_t * s) |
| 564 | { |
| 565 | segment_manager_t *sm; |
| 566 | svm_fifo_t *rxf, *txf; |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 567 | int rv; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 568 | |
| 569 | if (s->session_state == SESSION_STATE_LISTENING) |
| 570 | return application_change_listener_owner (s, app_wrk); |
| 571 | |
| 572 | s->app_wrk_index = app_wrk->wrk_index; |
| 573 | |
| 574 | rxf = s->rx_fifo; |
| 575 | txf = s->tx_fifo; |
| 576 | |
| 577 | if (!rxf || !txf) |
| 578 | return 0; |
| 579 | |
| 580 | s->rx_fifo = 0; |
| 581 | s->tx_fifo = 0; |
| 582 | |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 583 | sm = app_worker_get_connect_segment_manager (app_wrk); |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 584 | if ((rv = app_worker_alloc_session_fifos (sm, s))) |
| 585 | return rv; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 586 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 587 | if (!svm_fifo_is_empty_cons (rxf)) |
| 588 | svm_fifo_clone (s->rx_fifo, rxf); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 589 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 590 | if (!svm_fifo_is_empty_cons (txf)) |
| 591 | svm_fifo_clone (s->tx_fifo, txf); |
| 592 | |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 593 | segment_manager_dealloc_fifos (rxf, txf); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | int |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 599 | app_worker_connect_session (app_worker_t *app_wrk, session_endpoint_cfg_t *sep, |
| 600 | session_handle_t *rsh) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 601 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 602 | if (PREDICT_FALSE (app_worker_mq_is_congested (app_wrk))) |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 603 | return SESSION_E_REFUSED; |
| 604 | |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 605 | sep->app_wrk_index = app_wrk->wrk_index; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 606 | |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 607 | return session_open (sep, rsh); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | int |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 611 | app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s, |
| 612 | svm_fifo_t * f, |
| 613 | session_ft_action_t act, u32 len) |
| 614 | { |
| 615 | application_t *app = application_get (app_wrk->app_index); |
| 616 | return app->cb_fns.fifo_tuning_callback (s, f, act, len); |
| 617 | } |
| 618 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 619 | segment_manager_t * |
| 620 | app_worker_get_connect_segment_manager (app_worker_t * app) |
| 621 | { |
| 622 | ASSERT (app->connects_seg_manager != (u32) ~ 0); |
| 623 | return segment_manager_get (app->connects_seg_manager); |
| 624 | } |
| 625 | |
| 626 | segment_manager_t * |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 627 | app_worker_get_listen_segment_manager (app_worker_t * app, |
| 628 | session_t * listener) |
| 629 | { |
| 630 | uword *smp; |
| 631 | smp = hash_get (app->listeners_table, listen_session_get_handle (listener)); |
Dave Barach | 47d41ad | 2020-02-17 09:13:26 -0500 | [diff] [blame] | 632 | ALWAYS_ASSERT (smp != 0); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 633 | return segment_manager_get (*smp); |
| 634 | } |
| 635 | |
| 636 | session_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 637 | app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto, |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 638 | u8 transport_proto) |
| 639 | { |
| 640 | session_t *listener; |
| 641 | u64 handle; |
| 642 | u32 sm_index; |
| 643 | u8 sst; |
| 644 | |
| 645 | sst = session_type_from_proto_and_ip (transport_proto, |
| 646 | fib_proto == FIB_PROTOCOL_IP4); |
| 647 | |
| 648 | /* *INDENT-OFF* */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 649 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 650 | listener = listen_session_get_from_handle (handle); |
| 651 | if (listener->session_type == sst |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 652 | && !(listener->flags & SESSION_F_PROXY)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 653 | return listener; |
| 654 | })); |
| 655 | /* *INDENT-ON* */ |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | session_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 661 | app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto, |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 662 | u8 transport_proto) |
| 663 | { |
| 664 | session_t *listener; |
| 665 | u64 handle; |
| 666 | u32 sm_index; |
| 667 | u8 sst; |
| 668 | |
| 669 | sst = session_type_from_proto_and_ip (transport_proto, |
| 670 | fib_proto == FIB_PROTOCOL_IP4); |
| 671 | |
| 672 | /* *INDENT-OFF* */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 673 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 674 | listener = listen_session_get_from_handle (handle); |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 675 | if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY)) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 676 | return listener; |
| 677 | })); |
| 678 | /* *INDENT-ON* */ |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Send an API message to the external app, to map new segment |
| 685 | */ |
| 686 | int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 687 | 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] | 688 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 689 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT, |
| 690 | .as_u64[1] = segment_handle }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 691 | |
| 692 | app_worker_add_event_custom (app_wrk, vlib_get_thread_index (), &evt); |
| 693 | |
| 694 | return 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 697 | int |
| 698 | app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle) |
| 699 | { |
Florin Coras | 09c0e8f | 2023-08-11 14:40:13 -0700 | [diff] [blame] | 700 | session_event_t evt = { .event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT, |
| 701 | .as_u64[1] = segment_handle }; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 702 | |
| 703 | app_worker_add_event_custom (app_wrk, vlib_get_thread_index (), &evt); |
| 704 | |
| 705 | return 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 708 | static int |
| 709 | app_wrk_send_fd (app_worker_t *app_wrk, int fd) |
| 710 | { |
| 711 | if (!appns_sapi_enabled ()) |
| 712 | { |
| 713 | vl_api_registration_t *reg; |
| 714 | clib_error_t *error; |
| 715 | |
| 716 | reg = |
| 717 | vl_mem_api_client_index_to_registration (app_wrk->api_client_index); |
| 718 | if (!reg) |
| 719 | { |
| 720 | clib_warning ("no api registration for client: %u", |
| 721 | app_wrk->api_client_index); |
| 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
| 726 | return -1; |
| 727 | |
| 728 | error = vl_api_send_fd_msg (reg, &fd, 1); |
| 729 | if (error) |
| 730 | { |
| 731 | clib_error_report (error); |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | app_sapi_msg_t smsg = { 0 }; |
| 739 | app_namespace_t *app_ns; |
| 740 | clib_error_t *error; |
| 741 | application_t *app; |
| 742 | clib_socket_t *cs; |
| 743 | u32 cs_index; |
| 744 | |
| 745 | app = application_get (app_wrk->app_index); |
| 746 | app_ns = app_namespace_get (app->ns_index); |
| 747 | cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index); |
| 748 | cs = appns_sapi_get_socket (app_ns, cs_index); |
| 749 | if (PREDICT_FALSE (!cs)) |
| 750 | return -1; |
| 751 | |
| 752 | /* There's no payload for the message only the type */ |
| 753 | smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS; |
| 754 | error = clib_socket_sendmsg (cs, &smsg, sizeof (smsg), &fd, 1); |
| 755 | if (error) |
| 756 | { |
| 757 | clib_error_report (error); |
| 758 | return -1; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 764 | void |
| 765 | app_worker_add_event (app_worker_t *app_wrk, session_t *s, |
| 766 | session_evt_type_t evt_type) |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 767 | { |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 768 | session_event_t *evt; |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 769 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 770 | ASSERT (s->thread_index == vlib_get_thread_index ()); |
| 771 | clib_fifo_add2 (app_wrk->wrk_evts[s->thread_index], evt); |
| 772 | evt->session_index = s->session_index; |
| 773 | evt->event_type = evt_type; |
| 774 | evt->postponed = 0; |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 775 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 776 | /* First event for this app_wrk. Schedule it for handling in session input */ |
| 777 | if (clib_fifo_elts (app_wrk->wrk_evts[s->thread_index]) == 1) |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 778 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 779 | session_worker_t *wrk = session_main_get_worker (s->thread_index); |
| 780 | session_wrk_program_app_wrk_evts (wrk, app_wrk->wrk_index); |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 781 | } |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 784 | void |
| 785 | app_worker_add_event_custom (app_worker_t *app_wrk, u32 thread_index, |
| 786 | session_event_t *evt) |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 787 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 788 | clib_fifo_add1 (app_wrk->wrk_evts[thread_index], *evt); |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 789 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 790 | /* First event for this app_wrk. Schedule it for handling in session input */ |
| 791 | if (clib_fifo_elts (app_wrk->wrk_evts[thread_index]) == 1) |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 792 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 793 | session_worker_t *wrk = session_main_get_worker (thread_index); |
| 794 | session_wrk_program_app_wrk_evts (wrk, app_wrk->wrk_index); |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 795 | } |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | always_inline void |
| 799 | app_wrk_send_ctrl_evt_inline (app_worker_t *app_wrk, u8 evt_type, void *msg, |
| 800 | u32 msg_len, int fd) |
| 801 | { |
| 802 | svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg; |
| 803 | svm_msg_q_t *mq = app_wrk->event_queue; |
| 804 | session_event_t *evt; |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 805 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 806 | ASSERT (!svm_msg_q_or_ring_is_full (mq, SESSION_MQ_CTRL_EVT_RING)); |
| 807 | *mq_msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_CTRL_EVT_RING); |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 808 | |
| 809 | evt = svm_msg_q_msg_data (mq, mq_msg); |
| 810 | clib_memset (evt, 0, sizeof (*evt)); |
| 811 | evt->event_type = evt_type; |
| 812 | clib_memcpy_fast (evt->data, msg, msg_len); |
| 813 | |
| 814 | if (fd != -1) |
| 815 | app_wrk_send_fd (app_wrk, fd); |
| 816 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 817 | svm_msg_q_add_raw (mq, mq_msg); |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void |
| 821 | app_wrk_send_ctrl_evt_fd (app_worker_t *app_wrk, u8 evt_type, void *msg, |
| 822 | u32 msg_len, int fd) |
| 823 | { |
| 824 | app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, fd); |
| 825 | } |
| 826 | |
| 827 | void |
| 828 | app_wrk_send_ctrl_evt (app_worker_t *app_wrk, u8 evt_type, void *msg, |
| 829 | u32 msg_len) |
| 830 | { |
| 831 | app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, -1); |
| 832 | } |
| 833 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 834 | u8 |
| 835 | app_worker_mq_wrk_is_congested (app_worker_t *app_wrk, u32 thread_index) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 836 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 837 | return app_wrk->wrk_mq_congested[thread_index] > 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 840 | void |
| 841 | app_worker_set_mq_wrk_congested (app_worker_t *app_wrk, u32 thread_index) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 842 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 843 | clib_atomic_fetch_add_relax (&app_wrk->mq_congested, 1); |
| 844 | ASSERT (thread_index == vlib_get_thread_index ()); |
| 845 | app_wrk->wrk_mq_congested[thread_index] = 1; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 848 | void |
| 849 | app_worker_unset_wrk_mq_congested (app_worker_t *app_wrk, u32 thread_index) |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 850 | { |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 851 | clib_atomic_fetch_sub_relax (&app_wrk->mq_congested, 1); |
| 852 | ASSERT (thread_index == vlib_get_thread_index ()); |
| 853 | app_wrk->wrk_mq_congested[thread_index] = 0; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 856 | u8 * |
| 857 | format_app_worker_listener (u8 * s, va_list * args) |
| 858 | { |
| 859 | app_worker_t *app_wrk = va_arg (*args, app_worker_t *); |
| 860 | u64 handle = va_arg (*args, u64); |
| 861 | u32 sm_index = va_arg (*args, u32); |
| 862 | int verbose = va_arg (*args, int); |
| 863 | session_t *listener; |
| 864 | const u8 *app_name; |
| 865 | u8 *str; |
| 866 | |
| 867 | if (!app_wrk) |
| 868 | { |
| 869 | if (verbose) |
Xiaoming Jiang | 806709f | 2021-06-23 09:07:57 +0000 | [diff] [blame] | 870 | s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s%-15s%-15s%-10s", |
| 871 | "Connection", "App", "Wrk", "API Client", "ListenerID", |
| 872 | "SegManager"); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 873 | else |
Xiaoming Jiang | 806709f | 2021-06-23 09:07:57 +0000 | [diff] [blame] | 874 | s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s", "Connection", |
| 875 | "App", "Wrk"); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 876 | |
| 877 | return s; |
| 878 | } |
| 879 | |
| 880 | app_name = application_name_from_index (app_wrk->app_index); |
| 881 | listener = listen_session_get_from_handle (handle); |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 882 | str = format (0, "%U", format_session, listener, verbose); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 883 | |
| 884 | if (verbose) |
| 885 | { |
Dave Barach | 3e07a4a | 2020-04-04 10:05:48 -0400 | [diff] [blame] | 886 | u8 *buf; |
| 887 | buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index); |
Xiaoming Jiang | 806709f | 2021-06-23 09:07:57 +0000 | [diff] [blame] | 888 | s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%-10v%-15u%-15u%-10u", str, |
| 889 | app_name, buf, app_wrk->api_client_index, handle, sm_index); |
Dave Barach | 3e07a4a | 2020-04-04 10:05:48 -0400 | [diff] [blame] | 890 | vec_free (buf); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 891 | } |
| 892 | else |
Xiaoming Jiang | 806709f | 2021-06-23 09:07:57 +0000 | [diff] [blame] | 893 | s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%=10u", str, app_name, |
| 894 | app_wrk->wrk_map_index); |
jiangxiaoming | 6b410e6 | 2020-10-10 15:23:54 +0800 | [diff] [blame] | 895 | |
| 896 | vec_free (str); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 897 | |
| 898 | return s; |
| 899 | } |
| 900 | |
| 901 | u8 * |
| 902 | format_app_worker (u8 * s, va_list * args) |
| 903 | { |
| 904 | app_worker_t *app_wrk = va_arg (*args, app_worker_t *); |
| 905 | u32 indent = 1; |
| 906 | |
Florin Coras | 20c2423 | 2021-11-22 21:19:01 -0800 | [diff] [blame] | 907 | s = format (s, |
| 908 | "%U wrk-index %u app-index %u map-index %u " |
| 909 | "api-client-index %d mq-cong %u\n", |
| 910 | format_white_space, indent, app_wrk->wrk_index, |
| 911 | app_wrk->app_index, app_wrk->wrk_map_index, |
| 912 | app_wrk->api_client_index, app_wrk->mq_congested); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 913 | return s; |
| 914 | } |
| 915 | |
| 916 | void |
| 917 | app_worker_format_connects (app_worker_t * app_wrk, int verbose) |
| 918 | { |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 919 | segment_manager_t *sm; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 920 | |
| 921 | /* Header */ |
| 922 | if (!app_wrk) |
| 923 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 924 | segment_manager_format_sessions (0, verbose); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 925 | return; |
| 926 | } |
| 927 | |
| 928 | if (app_wrk->connects_seg_manager == (u32) ~ 0) |
| 929 | return; |
| 930 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 931 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 932 | segment_manager_format_sessions (sm, verbose); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 935 | /* |
| 936 | * fd.io coding-style-patch-verification: ON |
| 937 | * |
| 938 | * Local Variables: |
| 939 | * eval: (c-set-style "gnu") |
| 940 | * End: |
| 941 | */ |