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