Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 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/segment_manager.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/session/application.h> |
| 19 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 20 | typedef struct segment_manager_main_ |
| 21 | { |
| 22 | segment_manager_t *segment_managers; /**< Pool of segment managers */ |
| 23 | clib_valloc_main_t va_allocator; /**< Virtual address allocator */ |
| 24 | u32 seg_name_counter; /**< Counter for segment names */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 25 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 26 | /* |
| 27 | * Configuration |
| 28 | */ |
| 29 | u32 default_fifo_size; /**< default rx/tx fifo size */ |
| 30 | u32 default_segment_size; /**< default fifo segment size */ |
| 31 | u32 default_app_mq_size; /**< default app msg q size */ |
| 32 | } segment_manager_main_t; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 33 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 34 | static segment_manager_main_t sm_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 35 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 36 | #define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \ |
| 37 | do { \ |
| 38 | clib_rwlock_reader_lock (&(SM)->segments_rwlock); \ |
| 39 | pool_foreach((VAR), ((SM)->segments), (BODY)); \ |
| 40 | clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \ |
| 41 | } while (0) |
| 42 | |
| 43 | static segment_manager_props_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 44 | segment_manager_properties_get (segment_manager_t * sm) |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 45 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 46 | app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index); |
| 47 | return application_get_segment_manager_properties (app_wrk->app_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 50 | segment_manager_props_t * |
| 51 | segment_manager_props_init (segment_manager_props_t * props) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 52 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 53 | props->add_segment_size = sm_main.default_segment_size; |
| 54 | props->rx_fifo_size = sm_main.default_fifo_size; |
| 55 | props->tx_fifo_size = sm_main.default_fifo_size; |
| 56 | props->evt_q_size = sm_main.default_app_mq_size; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 57 | return props; |
| 58 | } |
| 59 | |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 60 | static u8 |
| 61 | segment_manager_app_detached (segment_manager_t * sm) |
| 62 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 63 | return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX); |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 66 | void |
| 67 | segment_manager_app_detach (segment_manager_t * sm) |
| 68 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 69 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 72 | always_inline u32 |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 73 | segment_manager_segment_index (segment_manager_t * sm, fifo_segment_t * seg) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 74 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 75 | return (seg - sm->segments); |
| 76 | } |
| 77 | |
| 78 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 79 | * Adds segment to segment manager's pool |
| 80 | * |
| 81 | * If needed a writer's lock is acquired before allocating a new segment |
| 82 | * to avoid affecting any of the segments pool readers. |
| 83 | */ |
| 84 | int |
| 85 | segment_manager_add_segment (segment_manager_t * sm, u32 segment_size) |
| 86 | { |
| 87 | segment_manager_main_t *smm = &sm_main; |
| 88 | u32 rnd_margin = 128 << 10, fs_index = ~0, page_size; |
| 89 | uword baseva = (uword) ~ 0ULL, alloc_size; |
| 90 | segment_manager_props_t *props; |
| 91 | fifo_segment_t *fs; |
| 92 | u8 *seg_name; |
| 93 | int rv; |
| 94 | |
| 95 | props = segment_manager_properties_get (sm); |
| 96 | |
| 97 | /* Not configured for addition of new segments and not first */ |
| 98 | if (!props->add_segment && !segment_size) |
| 99 | { |
| 100 | clib_warning ("cannot allocate new segment"); |
| 101 | return VNET_API_ERROR_INVALID_VALUE; |
| 102 | } |
| 103 | |
| 104 | /* |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 105 | * Allocate fifo segment and grab lock if needed |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 106 | */ |
| 107 | if (vlib_num_workers ()) |
| 108 | clib_rwlock_writer_lock (&sm->segments_rwlock); |
| 109 | |
| 110 | pool_get_zero (sm->segments, fs); |
| 111 | |
| 112 | /* |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 113 | * Allocate ssvm segment |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 114 | */ |
| 115 | segment_size = segment_size ? segment_size : props->add_segment_size; |
| 116 | page_size = clib_mem_get_page_size (); |
Florin Coras | 404b8a3 | 2019-05-09 12:08:06 -0700 | [diff] [blame] | 117 | /* Protect against segment size u32 wrap */ |
| 118 | segment_size = clib_max (segment_size + page_size - 1, segment_size); |
| 119 | segment_size = segment_size & ~(page_size - 1); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 120 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 121 | if (props->segment_type != SSVM_SEGMENT_PRIVATE) |
| 122 | { |
| 123 | seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0); |
| 124 | alloc_size = (uword) segment_size + rnd_margin; |
| 125 | baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0); |
| 126 | if (!baseva) |
| 127 | { |
| 128 | clib_warning ("out of space for segments"); |
| 129 | pool_put (sm->segments, fs); |
| 130 | goto done; |
| 131 | } |
| 132 | } |
| 133 | else |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 134 | seg_name = format (0, "%s%c", "process-private", 0); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 135 | |
| 136 | fs->ssvm.ssvm_size = segment_size; |
| 137 | fs->ssvm.name = seg_name; |
| 138 | fs->ssvm.requested_va = baseva; |
| 139 | |
| 140 | if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type))) |
| 141 | { |
| 142 | clib_warning ("svm_master_init ('%v', %u) failed", seg_name, |
| 143 | segment_size); |
| 144 | |
| 145 | if (props->segment_type != SSVM_SEGMENT_PRIVATE) |
| 146 | clib_valloc_free (&smm->va_allocator, baseva); |
| 147 | pool_put (sm->segments, fs); |
| 148 | goto done; |
| 149 | } |
| 150 | |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Initialize fifo segment |
| 153 | */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 154 | fifo_segment_init (fs); |
| 155 | |
| 156 | /* |
| 157 | * Save segment index before dropping lock, if any held |
| 158 | */ |
| 159 | fs_index = fs - sm->segments; |
| 160 | |
| 161 | done: |
| 162 | |
| 163 | if (vlib_num_workers ()) |
| 164 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 165 | |
| 166 | return fs_index; |
| 167 | } |
| 168 | |
| 169 | /** |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 170 | * Remove segment without lock |
| 171 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 172 | void |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 173 | segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 174 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 175 | segment_manager_main_t *smm = &sm_main; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 176 | |
| 177 | if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE) |
Florin Coras | a0dbf9e | 2019-03-01 17:12:02 -0800 | [diff] [blame] | 178 | { |
| 179 | clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va); |
| 180 | |
| 181 | if (sm->app_wrk_index != SEGMENT_MANAGER_INVALID_APP_INDEX) |
| 182 | { |
| 183 | app_worker_t *app_wrk; |
| 184 | u64 segment_handle; |
| 185 | app_wrk = app_worker_get (sm->app_wrk_index); |
| 186 | segment_handle = segment_manager_segment_handle (sm, fs); |
| 187 | app_worker_del_segment_notify (app_wrk, segment_handle); |
| 188 | } |
| 189 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 190 | |
| 191 | ssvm_delete (&fs->ssvm); |
| 192 | |
| 193 | if (CLIB_DEBUG) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 194 | clib_memset (fs, 0xfb, sizeof (*fs)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 195 | pool_put (sm->segments, fs); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Removes segment after acquiring writer lock |
| 200 | */ |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 201 | static inline void |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 202 | segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index) |
| 203 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 204 | fifo_segment_t *fs; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 205 | u8 is_prealloc; |
| 206 | |
| 207 | clib_rwlock_writer_lock (&sm->segments_rwlock); |
| 208 | fs = segment_manager_get_segment (sm, fs_index); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 209 | is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 210 | if (is_prealloc && !segment_manager_app_detached (sm)) |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 211 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 212 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 213 | return; |
| 214 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 215 | |
| 216 | segment_manager_del_segment (sm, fs); |
| 217 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Reads a segment from the segment manager's pool without lock |
| 222 | */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 223 | fifo_segment_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 224 | segment_manager_get_segment (segment_manager_t * sm, u32 segment_index) |
| 225 | { |
| 226 | return pool_elt_at_index (sm->segments, segment_index); |
| 227 | } |
| 228 | |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 229 | u64 |
| 230 | segment_manager_segment_handle (segment_manager_t * sm, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 231 | fifo_segment_t * segment) |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 232 | { |
| 233 | u32 segment_index = segment_manager_segment_index (sm, segment); |
| 234 | return (((u64) segment_manager_index (sm) << 32) | segment_index); |
| 235 | } |
| 236 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 237 | static void |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 238 | segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index, |
| 239 | u32 * segment_index) |
| 240 | { |
| 241 | *sm_index = segment_handle >> 32; |
| 242 | *segment_index = segment_handle & 0xFFFFFFFF; |
| 243 | } |
| 244 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 245 | u64 |
| 246 | segment_manager_make_segment_handle (u32 segment_manager_index, |
| 247 | u32 segment_index) |
| 248 | { |
| 249 | return (((u64) segment_manager_index << 32) | segment_index); |
| 250 | } |
| 251 | |
| 252 | fifo_segment_t * |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 253 | segment_manager_get_segment_w_handle (u64 segment_handle) |
| 254 | { |
| 255 | u32 sm_index, segment_index; |
| 256 | segment_manager_t *sm; |
| 257 | |
| 258 | segment_manager_parse_segment_handle (segment_handle, &sm_index, |
| 259 | &segment_index); |
| 260 | sm = segment_manager_get (sm_index); |
| 261 | if (!sm || pool_is_free_index (sm->segments, segment_index)) |
| 262 | return 0; |
| 263 | return pool_elt_at_index (sm->segments, segment_index); |
| 264 | } |
| 265 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Reads a segment from the segment manager's pool and acquires reader lock |
| 268 | * |
| 269 | * Caller must drop the reader's lock by calling |
| 270 | * @ref segment_manager_segment_reader_unlock once it finishes working with |
| 271 | * the segment. |
| 272 | */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 273 | fifo_segment_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 274 | segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index) |
| 275 | { |
| 276 | clib_rwlock_reader_lock (&sm->segments_rwlock); |
| 277 | return pool_elt_at_index (sm->segments, segment_index); |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | segment_manager_segment_reader_unlock (segment_manager_t * sm) |
| 282 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 283 | ASSERT (sm->segments_rwlock->n_readers > 0); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 284 | clib_rwlock_reader_unlock (&sm->segments_rwlock); |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | segment_manager_segment_writer_unlock (segment_manager_t * sm) |
| 289 | { |
| 290 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 291 | } |
| 292 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 293 | segment_manager_t * |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 294 | segment_manager_alloc (void) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 295 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 296 | segment_manager_main_t *smm = &sm_main; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 297 | segment_manager_t *sm; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 298 | |
| 299 | pool_get_zero (smm->segment_managers, sm); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 300 | clib_rwlock_init (&sm->segments_rwlock); |
| 301 | return sm; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Initializes segment manager based on options provided. |
| 306 | * Returns error if ssvm segment(s) allocation fails. |
| 307 | */ |
| 308 | int |
| 309 | segment_manager_init (segment_manager_t * sm, u32 first_seg_size, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 310 | u32 prealloc_fifo_pairs) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 311 | { |
| 312 | u32 rx_fifo_size, tx_fifo_size, pair_size; |
| 313 | u32 rx_rounded_data_size, tx_rounded_data_size; |
Florin Coras | 86f0450 | 2018-09-12 16:08:01 -0700 | [diff] [blame] | 314 | u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 315 | segment_manager_props_t *props; |
| 316 | fifo_segment_t *segment; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 317 | u32 approx_segment_count; |
| 318 | int seg_index, i; |
| 319 | |
| 320 | props = segment_manager_properties_get (sm); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 321 | first_seg_size = clib_max (first_seg_size, sm_main.default_segment_size); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 322 | |
| 323 | if (prealloc_fifo_pairs) |
| 324 | { |
| 325 | /* Figure out how many segments should be preallocated */ |
| 326 | rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size))); |
| 327 | tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size))); |
| 328 | |
| 329 | rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size; |
| 330 | tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size; |
| 331 | pair_size = rx_fifo_size + tx_fifo_size; |
| 332 | |
| 333 | approx_total_size = (u64) prealloc_fifo_pairs *pair_size; |
| 334 | if (first_seg_size > approx_total_size) |
| 335 | max_seg_size = first_seg_size; |
| 336 | approx_segment_count = (approx_total_size + (max_seg_size - 1)) |
| 337 | / max_seg_size; |
| 338 | |
| 339 | /* Allocate the segments */ |
| 340 | for (i = 0; i < approx_segment_count + 1; i++) |
| 341 | { |
| 342 | seg_index = segment_manager_add_segment (sm, max_seg_size); |
| 343 | if (seg_index < 0) |
| 344 | { |
| 345 | clib_warning ("Failed to preallocate segment %d", i); |
| 346 | return seg_index; |
| 347 | } |
| 348 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 349 | segment = segment_manager_get_segment (sm, seg_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 350 | if (i == 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 351 | sm->event_queue = segment_manager_alloc_queue (segment, props); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 352 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 353 | fifo_segment_preallocate_fifo_pairs (segment, |
| 354 | props->rx_fifo_size, |
| 355 | props->tx_fifo_size, |
| 356 | &prealloc_fifo_pairs); |
| 357 | fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 358 | if (prealloc_fifo_pairs == 0) |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | seg_index = segment_manager_add_segment (sm, first_seg_size); |
Florin Coras | 402377e | 2019-04-17 10:23:23 -0700 | [diff] [blame] | 365 | if (seg_index < 0) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 366 | { |
| 367 | clib_warning ("Failed to allocate segment"); |
| 368 | return seg_index; |
| 369 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 370 | segment = segment_manager_get_segment (sm, seg_index); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 371 | sm->event_queue = segment_manager_alloc_queue (segment, props); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 377 | /** |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 378 | * Cleanup segment manager. |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 379 | */ |
| 380 | void |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 381 | segment_manager_free (segment_manager_t * sm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 382 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 383 | segment_manager_main_t *smm = &sm_main; |
| 384 | fifo_segment_t *fifo_segment; |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 385 | |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 386 | ASSERT (!segment_manager_has_fifos (sm) |
| 387 | && segment_manager_app_detached (sm)); |
| 388 | |
| 389 | /* If we have empty preallocated segments that haven't been removed, remove |
| 390 | * them now. Apart from that, the first segment in the first segment manager |
| 391 | * is not removed when all fifos are removed. It can only be removed when |
| 392 | * the manager is explicitly deleted/detached by the app. */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 393 | clib_rwlock_writer_lock (&sm->segments_rwlock); |
| 394 | |
| 395 | /* *INDENT-OFF* */ |
| 396 | pool_foreach (fifo_segment, sm->segments, ({ |
| 397 | segment_manager_del_segment (sm, fifo_segment); |
| 398 | })); |
| 399 | /* *INDENT-ON* */ |
| 400 | |
| 401 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 402 | |
| 403 | clib_rwlock_free (&sm->segments_rwlock); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 404 | if (CLIB_DEBUG) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 405 | clib_memset (sm, 0xfe, sizeof (*sm)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 406 | pool_put (smm->segment_managers, sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 409 | void |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 410 | segment_manager_init_free (segment_manager_t * sm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 411 | { |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 412 | segment_manager_app_detach (sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 413 | if (segment_manager_has_fifos (sm)) |
| 414 | segment_manager_del_sessions (sm); |
| 415 | else |
| 416 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 417 | ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm)); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 418 | segment_manager_free (sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 419 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 422 | segment_manager_t * |
| 423 | segment_manager_get (u32 index) |
| 424 | { |
| 425 | return pool_elt_at_index (sm_main.segment_managers, index); |
| 426 | } |
| 427 | |
| 428 | segment_manager_t * |
| 429 | segment_manager_get_if_valid (u32 index) |
| 430 | { |
| 431 | if (pool_is_free_index (sm_main.segment_managers, index)) |
| 432 | return 0; |
| 433 | return pool_elt_at_index (sm_main.segment_managers, index); |
| 434 | } |
| 435 | |
| 436 | u32 |
| 437 | segment_manager_index (segment_manager_t * sm) |
| 438 | { |
| 439 | return sm - sm_main.segment_managers; |
| 440 | } |
| 441 | |
| 442 | u8 |
| 443 | segment_manager_has_fifos (segment_manager_t * sm) |
| 444 | { |
| 445 | fifo_segment_t *seg; |
| 446 | u8 first = 1; |
| 447 | |
| 448 | /* *INDENT-OFF* */ |
| 449 | segment_manager_foreach_segment_w_lock (seg, sm, ({ |
| 450 | if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg) |
| 451 | && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED)) |
| 452 | { |
| 453 | clib_warning ("segment %d has no fifos!", |
| 454 | segment_manager_segment_index (sm, seg)); |
| 455 | first = 0; |
| 456 | } |
| 457 | if (fifo_segment_has_fifos (seg)) |
| 458 | { |
| 459 | segment_manager_segment_reader_unlock (sm); |
| 460 | return 1; |
| 461 | } |
| 462 | })); |
| 463 | /* *INDENT-ON* */ |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Initiate disconnects for all sessions 'owned' by a segment manager |
| 470 | */ |
| 471 | void |
| 472 | segment_manager_del_sessions (segment_manager_t * sm) |
| 473 | { |
| 474 | fifo_segment_t *fifo_segment; |
| 475 | session_handle_t *handles = 0, *handle; |
| 476 | session_t *session; |
| 477 | svm_fifo_t *fifo; |
| 478 | |
| 479 | ASSERT (pool_elts (sm->segments) != 0); |
| 480 | |
| 481 | /* Across all fifo segments used by the server */ |
| 482 | /* *INDENT-OFF* */ |
| 483 | segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({ |
| 484 | fifo = fifo_segment_get_fifo_list (fifo_segment); |
| 485 | |
| 486 | /* |
| 487 | * Remove any residual sessions from the session lookup table |
| 488 | * Don't bother deleting the individual fifos, we're going to |
| 489 | * throw away the fifo segment in a minute. |
| 490 | */ |
| 491 | while (fifo) |
| 492 | { |
| 493 | session = session_get_if_valid (fifo->master_session_index, |
| 494 | fifo->master_thread_index); |
| 495 | if (session) |
| 496 | vec_add1 (handles, session_handle (session)); |
| 497 | fifo = fifo->next; |
| 498 | } |
| 499 | |
| 500 | /* Instead of removing the segment, test when cleaning up disconnected |
| 501 | * sessions if the segment can be removed. |
| 502 | */ |
| 503 | })); |
| 504 | /* *INDENT-ON* */ |
| 505 | |
| 506 | vec_foreach (handle, handles) |
| 507 | session_close (session_get_from_handle (*handle)); |
| 508 | } |
| 509 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 510 | int |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 511 | segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 512 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 513 | svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 514 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 515 | rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size); |
| 516 | *rx_fifo = fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size, |
| 517 | FIFO_SEGMENT_RX_FIFO); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 518 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 519 | tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size); |
| 520 | *tx_fifo = fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size, |
| 521 | FIFO_SEGMENT_TX_FIFO); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 522 | |
| 523 | if (*rx_fifo == 0) |
| 524 | { |
| 525 | /* This would be very odd, but handle it... */ |
| 526 | if (*tx_fifo != 0) |
| 527 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 528 | fifo_segment_free_fifo (fifo_segment, *tx_fifo); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 529 | *tx_fifo = 0; |
| 530 | } |
| 531 | return -1; |
| 532 | } |
| 533 | if (*tx_fifo == 0) |
| 534 | { |
| 535 | if (*rx_fifo != 0) |
| 536 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 537 | fifo_segment_free_fifo (fifo_segment, *rx_fifo); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 538 | *rx_fifo = 0; |
| 539 | } |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 546 | int |
| 547 | segment_manager_alloc_session_fifos (segment_manager_t * sm, |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 548 | svm_fifo_t ** rx_fifo, |
Florin Coras | 1219b2d | 2019-04-23 15:53:43 -0700 | [diff] [blame] | 549 | svm_fifo_t ** tx_fifo) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 550 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 551 | int alloc_fail = 1, rv = 0, new_fs_index; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 552 | segment_manager_props_t *props; |
| 553 | fifo_segment_t *fs = 0; |
| 554 | u32 sm_index, fs_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 555 | u8 added_a_segment = 0; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 556 | u64 fs_handle; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 557 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 558 | props = segment_manager_properties_get (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 559 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 560 | /* |
| 561 | * Find the first free segment to allocate the fifos in |
| 562 | */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 563 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 564 | /* *INDENT-OFF* */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 565 | segment_manager_foreach_segment_w_lock (fs, sm, ({ |
| 566 | alloc_fail = segment_manager_try_alloc_fifos (fs, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 567 | props->rx_fifo_size, |
| 568 | props->tx_fifo_size, |
| 569 | rx_fifo, tx_fifo); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 570 | /* Exit with lock held, drop it after notifying app */ |
| 571 | if (!alloc_fail) |
| 572 | goto alloc_success; |
| 573 | })); |
| 574 | /* *INDENT-ON* */ |
| 575 | |
| 576 | alloc_check: |
| 577 | |
| 578 | if (!alloc_fail) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 579 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 580 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 581 | alloc_success: |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 582 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 583 | ASSERT (rx_fifo && tx_fifo); |
| 584 | sm_index = segment_manager_index (sm); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 585 | fs_index = segment_manager_segment_index (sm, fs); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 586 | (*tx_fifo)->segment_manager = sm_index; |
| 587 | (*rx_fifo)->segment_manager = sm_index; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 588 | (*tx_fifo)->segment_index = fs_index; |
| 589 | (*rx_fifo)->segment_index = fs_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 590 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 591 | if (added_a_segment) |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 592 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 593 | app_worker_t *app_wrk; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 594 | fs_handle = segment_manager_segment_handle (sm, fs); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 595 | app_wrk = app_worker_get (sm->app_wrk_index); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 596 | rv = app_worker_add_segment_notify (app_wrk, fs_handle); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 597 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 598 | /* Drop the lock after app is notified */ |
| 599 | segment_manager_segment_reader_unlock (sm); |
| 600 | return rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 603 | /* |
| 604 | * Allocation failed, see if we can add a new segment |
| 605 | */ |
| 606 | if (props->add_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 607 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 608 | if (added_a_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 609 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 610 | clib_warning ("Added a segment, still can't allocate a fifo"); |
| 611 | segment_manager_segment_reader_unlock (sm); |
| 612 | return SESSION_ERROR_NEW_SEG_NO_SPACE; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 613 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 614 | if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 615 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 616 | clib_warning ("Failed to add new segment"); |
| 617 | return SESSION_ERROR_SEG_CREATE; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 618 | } |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 619 | fs = segment_manager_get_segment_w_lock (sm, new_fs_index); |
| 620 | alloc_fail = segment_manager_try_alloc_fifos (fs, props->rx_fifo_size, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 621 | props->tx_fifo_size, |
| 622 | rx_fifo, tx_fifo); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 623 | added_a_segment = 1; |
| 624 | goto alloc_check; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 625 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 626 | else |
| 627 | { |
| 628 | clib_warning ("Can't add new seg and no space to allocate fifos!"); |
| 629 | return SESSION_ERROR_NO_SPACE; |
| 630 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 634 | segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 635 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 636 | segment_manager_t *sm; |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 637 | fifo_segment_t *fs; |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 638 | u32 segment_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 639 | |
Florin Coras | 58a93e8 | 2019-01-14 23:33:46 -0800 | [diff] [blame] | 640 | if (!rx_fifo || !tx_fifo) |
| 641 | return; |
| 642 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 643 | /* It's possible to have no segment manager if the session was removed |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 644 | * as result of a detach. */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 645 | if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager))) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 646 | return; |
| 647 | |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 648 | segment_index = rx_fifo->segment_index; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 649 | fs = segment_manager_get_segment_w_lock (sm, segment_index); |
| 650 | fifo_segment_free_fifo (fs, rx_fifo); |
| 651 | fifo_segment_free_fifo (fs, tx_fifo); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 652 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 653 | /* |
| 654 | * Try to remove svm segment if it has no fifos. This can be done only if |
| 655 | * the segment is not the first in the segment manager or if it is first |
| 656 | * and it is not protected. Moreover, if the segment is first and the app |
| 657 | * has detached from the segment manager, remove the segment manager. |
| 658 | */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 659 | if (!fifo_segment_has_fifos (fs)) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 660 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 661 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 662 | |
| 663 | /* Remove segment if it holds no fifos or first but not protected */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 664 | if (segment_index != 0 || !sm->first_is_protected) |
| 665 | segment_manager_lock_and_del_segment (sm, segment_index); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 666 | |
| 667 | /* Remove segment manager if no sessions and detached from app */ |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 668 | if (segment_manager_app_detached (sm) |
| 669 | && !segment_manager_has_fifos (sm)) |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 670 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 671 | segment_manager_free (sm); |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 672 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 673 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 674 | else |
| 675 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 678 | int |
| 679 | segment_manager_grow_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size) |
| 680 | { |
| 681 | fifo_segment_t *fs; |
| 682 | int rv; |
| 683 | |
| 684 | fs = segment_manager_get_segment_w_lock (sm, f->segment_index); |
| 685 | rv = fifo_segment_grow_fifo (fs, f, size); |
| 686 | segment_manager_segment_reader_unlock (sm); |
| 687 | |
| 688 | return rv; |
| 689 | } |
| 690 | |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 691 | int |
| 692 | segment_manager_collect_fifo_chunks (segment_manager_t * sm, svm_fifo_t * f) |
| 693 | { |
| 694 | fifo_segment_t *fs; |
| 695 | int rv; |
| 696 | |
| 697 | fs = segment_manager_get_segment_w_lock (sm, f->segment_index); |
| 698 | rv = fifo_segment_collect_fifo_chunks (fs, f); |
| 699 | segment_manager_segment_reader_unlock (sm); |
| 700 | |
| 701 | return rv; |
| 702 | } |
| 703 | |
| 704 | int |
| 705 | segment_manager_shrink_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size, |
| 706 | u8 is_producer) |
| 707 | { |
| 708 | int rv; |
| 709 | |
| 710 | rv = svm_fifo_reduce_size (f, size, is_producer); |
| 711 | |
| 712 | /* Nothing to collect at this point */ |
| 713 | if (!is_producer) |
| 714 | return rv; |
| 715 | |
| 716 | if (f->flags & SVM_FIFO_F_COLLECT_CHUNKS) |
| 717 | segment_manager_collect_fifo_chunks (sm, f); |
| 718 | |
| 719 | return rv; |
| 720 | } |
| 721 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 722 | u32 |
| 723 | segment_manager_evt_q_expected_size (u32 q_len) |
| 724 | { |
| 725 | u32 fifo_evt_size, notif_q_size, q_hdrs; |
| 726 | u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz; |
| 727 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 728 | fifo_evt_size = 1 << max_log2 (sizeof (session_event_t)); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 729 | notif_q_size = clib_max (16, q_len >> 4); |
| 730 | |
| 731 | msg_q_sz = q_len * sizeof (svm_msg_q_msg_t); |
| 732 | fifo_evt_ring_sz = q_len * fifo_evt_size; |
| 733 | session_ntf_ring_sz = notif_q_size * 256; |
| 734 | q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t); |
| 735 | |
| 736 | return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs); |
| 737 | } |
| 738 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 739 | /** |
| 740 | * Allocates shm queue in the first segment |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 741 | * |
| 742 | * Must be called with lock held |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 743 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 744 | svm_msg_q_t * |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 745 | segment_manager_alloc_queue (fifo_segment_t * segment, |
| 746 | segment_manager_props_t * props) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 747 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 748 | u32 fifo_evt_size, session_evt_size = 256, notif_q_size; |
| 749 | svm_msg_q_cfg_t _cfg, *cfg = &_cfg; |
| 750 | svm_msg_q_t *q; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 751 | void *oldheap; |
| 752 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 753 | fifo_evt_size = sizeof (session_event_t); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 754 | notif_q_size = clib_max (16, props->evt_q_size >> 4); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 755 | /* *INDENT-OFF* */ |
| 756 | svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 757 | {props->evt_q_size, fifo_evt_size, 0}, |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 758 | {notif_q_size, session_evt_size, 0} |
| 759 | }; |
| 760 | /* *INDENT-ON* */ |
| 761 | cfg->consumer_pid = 0; |
| 762 | cfg->n_rings = 2; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 763 | cfg->q_nitems = props->evt_q_size; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 764 | cfg->ring_cfgs = rc; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 765 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 766 | oldheap = ssvm_push_heap (segment->ssvm.sh); |
| 767 | q = svm_msg_q_alloc (cfg); |
Florin Coras | f9d4ab4 | 2019-05-11 16:55:53 -0700 | [diff] [blame] | 768 | fifo_segment_update_free_bytes (segment); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 769 | ssvm_pop_heap (oldheap); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 770 | |
| 771 | if (props->use_mq_eventfd) |
| 772 | { |
| 773 | if (svm_msg_q_alloc_producer_eventfd (q)) |
| 774 | clib_warning ("failed to alloc eventfd"); |
| 775 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 776 | return q; |
| 777 | } |
| 778 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 779 | svm_msg_q_t * |
| 780 | segment_manager_event_queue (segment_manager_t * sm) |
| 781 | { |
| 782 | return sm->event_queue; |
| 783 | } |
| 784 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 785 | /** |
| 786 | * Frees shm queue allocated in the first segment |
| 787 | */ |
| 788 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 789 | segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 790 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 791 | fifo_segment_t *segment; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 792 | ssvm_shared_header_t *sh; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 793 | void *oldheap; |
| 794 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 795 | ASSERT (!pool_is_free_index (sm->segments, 0)); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 796 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 797 | segment = segment_manager_get_segment_w_lock (sm, 0); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 798 | sh = segment->ssvm.sh; |
| 799 | |
| 800 | oldheap = ssvm_push_heap (sh); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 801 | svm_queue_free (q); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 802 | ssvm_pop_heap (oldheap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 803 | segment_manager_segment_reader_unlock (sm); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Init segment vm address allocator |
| 808 | */ |
| 809 | void |
| 810 | segment_manager_main_init (segment_manager_main_init_args_t * a) |
| 811 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 812 | segment_manager_main_t *sm = &sm_main; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 813 | clib_valloc_chunk_t _ip, *ip = &_ip; |
| 814 | |
| 815 | ip->baseva = a->baseva; |
| 816 | ip->size = a->size; |
| 817 | |
| 818 | clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ ); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 819 | |
| 820 | sm->default_fifo_size = 1 << 12; |
| 821 | sm->default_segment_size = 1 << 20; |
| 822 | sm->default_app_mq_size = 128; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 825 | static clib_error_t * |
| 826 | segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input, |
| 827 | vlib_cli_command_t * cmd) |
| 828 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 829 | segment_manager_main_t *smm = &sm_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 830 | u8 show_segments = 0, verbose = 0; |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 831 | segment_manager_t *sm; |
| 832 | fifo_segment_t *seg; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 833 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 834 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 835 | { |
| 836 | if (unformat (input, "segments")) |
| 837 | show_segments = 1; |
| 838 | else if (unformat (input, "verbose")) |
| 839 | verbose = 1; |
| 840 | else |
| 841 | return clib_error_return (0, "unknown input `%U'", |
| 842 | format_unformat_error, input); |
| 843 | } |
| 844 | vlib_cli_output (vm, "%d segment managers allocated", |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 845 | pool_elts (smm->segment_managers)); |
| 846 | if (verbose && pool_elts (smm->segment_managers)) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 847 | { |
| 848 | vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index", |
| 849 | "Segments"); |
| 850 | |
| 851 | /* *INDENT-OFF* */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 852 | pool_foreach (sm, smm->segment_managers, ({ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 853 | vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm), |
| 854 | sm->app_wrk_index, pool_elts (sm->segments)); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 855 | })); |
| 856 | /* *INDENT-ON* */ |
| 857 | |
| 858 | } |
| 859 | if (show_segments) |
| 860 | { |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 861 | vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 862 | |
| 863 | /* *INDENT-OFF* */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 864 | pool_foreach (sm, smm->segment_managers, ({ |
| 865 | segment_manager_foreach_segment_w_lock (seg, sm, ({ |
Florin Coras | 5368bb0 | 2019-06-09 09:24:33 -0700 | [diff] [blame] | 866 | vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 867 | })); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 868 | })); |
| 869 | /* *INDENT-ON* */ |
| 870 | |
| 871 | } |
| 872 | return 0; |
| 873 | } |
| 874 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 875 | /* *INDENT-OFF* */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 876 | VLIB_CLI_COMMAND (segment_manager_show_command, static) = |
| 877 | { |
| 878 | .path = "show segment-manager", |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 879 | .short_help = "show segment-manager [segments][verbose]", |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 880 | .function = segment_manager_show_fn, |
| 881 | }; |
| 882 | /* *INDENT-ON* */ |
| 883 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 884 | void |
| 885 | segment_manager_format_sessions (segment_manager_t * sm, int verbose) |
| 886 | { |
| 887 | fifo_segment_t *fifo_segment; |
| 888 | vlib_main_t *vm = vlib_get_main (); |
| 889 | app_worker_t *app_wrk; |
| 890 | const u8 *app_name; |
| 891 | u8 *s = 0; |
| 892 | |
| 893 | if (!sm) |
| 894 | { |
| 895 | if (verbose) |
| 896 | vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App", |
| 897 | "API Client", "SegManager"); |
| 898 | else |
| 899 | vlib_cli_output (vm, "%-40s%-20s", "Connection", "App"); |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | app_wrk = app_worker_get (sm->app_wrk_index); |
| 904 | app_name = application_name_from_index (app_wrk->app_index); |
| 905 | |
| 906 | clib_rwlock_reader_lock (&sm->segments_rwlock); |
| 907 | |
| 908 | /* *INDENT-OFF* */ |
| 909 | pool_foreach (fifo_segment, sm->segments, ({ |
| 910 | svm_fifo_t *fifo; |
| 911 | u8 *str; |
| 912 | |
| 913 | fifo = fifo_segment_get_fifo_list (fifo_segment); |
| 914 | while (fifo) |
| 915 | { |
| 916 | u32 session_index, thread_index; |
| 917 | session_t *session; |
| 918 | |
| 919 | session_index = fifo->master_session_index; |
| 920 | thread_index = fifo->master_thread_index; |
| 921 | |
| 922 | session = session_get (session_index, thread_index); |
| 923 | str = format (0, "%U", format_session, session, verbose); |
| 924 | |
| 925 | if (verbose) |
| 926 | s = format (s, "%-40s%-20s%-15u%-10u", str, app_name, |
| 927 | app_wrk->api_client_index, app_wrk->connects_seg_manager); |
| 928 | else |
| 929 | s = format (s, "%-40s%-20s", str, app_name); |
| 930 | |
| 931 | vlib_cli_output (vm, "%v", s); |
| 932 | vec_reset_length (s); |
| 933 | vec_free (str); |
| 934 | |
| 935 | fifo = fifo->next; |
| 936 | } |
| 937 | vec_free (s); |
| 938 | })); |
| 939 | /* *INDENT-ON* */ |
| 940 | |
| 941 | clib_rwlock_reader_unlock (&sm->segments_rwlock); |
| 942 | } |
| 943 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 944 | /* |
| 945 | * fd.io coding-style-patch-verification: ON |
| 946 | * |
| 947 | * Local Variables: |
| 948 | * eval: (c-set-style "gnu") |
| 949 | * End: |
| 950 | */ |