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