Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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/segment_manager.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/session/application.h> |
| 19 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 20 | segment_manager_main_t segment_manager_main; |
| 21 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Counter used to build segment names |
| 24 | */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 25 | static u32 segment_name_counter = 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 28 | * Default fifo and segment size. TODO config. |
| 29 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 30 | static u32 default_fifo_size = 1 << 12; |
| 31 | static u32 default_segment_size = 1 << 20; |
| 32 | static u32 default_app_evt_queue_size = 128; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 33 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 34 | segment_manager_properties_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 35 | segment_manager_properties_get (segment_manager_t * sm) |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 36 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 37 | app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index); |
| 38 | return application_get_segment_manager_properties (app_wrk->app_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | segment_manager_properties_t * |
| 42 | segment_manager_properties_init (segment_manager_properties_t * props) |
| 43 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 44 | props->add_segment_size = default_segment_size; |
| 45 | props->rx_fifo_size = default_fifo_size; |
| 46 | props->tx_fifo_size = default_fifo_size; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 47 | props->evt_q_size = default_app_evt_queue_size; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 48 | return props; |
| 49 | } |
| 50 | |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 51 | static u8 |
| 52 | segment_manager_app_detached (segment_manager_t * sm) |
| 53 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 54 | return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX); |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 57 | void |
| 58 | segment_manager_app_detach (segment_manager_t * sm) |
| 59 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 60 | sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 63 | always_inline u32 |
| 64 | segment_manager_segment_index (segment_manager_t * sm, |
| 65 | svm_fifo_segment_private_t * seg) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 66 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 67 | return (seg - sm->segments); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Remove segment without lock |
| 72 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 73 | void |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 74 | segment_manager_del_segment (segment_manager_t * sm, |
| 75 | svm_fifo_segment_private_t * fs) |
| 76 | { |
| 77 | segment_manager_main_t *smm = &segment_manager_main; |
| 78 | |
| 79 | if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE) |
| 80 | clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va); |
| 81 | |
| 82 | ssvm_delete (&fs->ssvm); |
| 83 | |
| 84 | if (CLIB_DEBUG) |
| 85 | memset (fs, 0xfb, sizeof (*fs)); |
| 86 | pool_put (sm->segments, fs); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Removes segment after acquiring writer lock |
| 91 | */ |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 92 | static inline void |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 93 | segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index) |
| 94 | { |
| 95 | svm_fifo_segment_private_t *fs; |
| 96 | u8 is_prealloc; |
| 97 | |
| 98 | clib_rwlock_writer_lock (&sm->segments_rwlock); |
| 99 | fs = segment_manager_get_segment (sm, fs_index); |
| 100 | is_prealloc = svm_fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED; |
| 101 | if (is_prealloc && !segment_manager_app_detached (sm)) |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 102 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 103 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 104 | return; |
| 105 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 106 | |
| 107 | segment_manager_del_segment (sm, fs); |
| 108 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Reads a segment from the segment manager's pool without lock |
| 113 | */ |
| 114 | svm_fifo_segment_private_t * |
| 115 | segment_manager_get_segment (segment_manager_t * sm, u32 segment_index) |
| 116 | { |
| 117 | return pool_elt_at_index (sm->segments, segment_index); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Reads a segment from the segment manager's pool and acquires reader lock |
| 122 | * |
| 123 | * Caller must drop the reader's lock by calling |
| 124 | * @ref segment_manager_segment_reader_unlock once it finishes working with |
| 125 | * the segment. |
| 126 | */ |
| 127 | svm_fifo_segment_private_t * |
| 128 | segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index) |
| 129 | { |
| 130 | clib_rwlock_reader_lock (&sm->segments_rwlock); |
| 131 | return pool_elt_at_index (sm->segments, segment_index); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | segment_manager_segment_reader_unlock (segment_manager_t * sm) |
| 136 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 137 | ASSERT (sm->segments_rwlock->n_readers > 0); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 138 | clib_rwlock_reader_unlock (&sm->segments_rwlock); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | segment_manager_segment_writer_unlock (segment_manager_t * sm) |
| 143 | { |
| 144 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Adds segment to segment manager's pool |
| 149 | * |
| 150 | * If needed a writer's lock is acquired before allocating a new segment |
| 151 | * to avoid affecting any of the segments pool readers. |
| 152 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 153 | int |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 154 | segment_manager_add_segment (segment_manager_t * sm, u32 segment_size) |
| 155 | { |
| 156 | segment_manager_main_t *smm = &segment_manager_main; |
Florin Coras | 5da96a7 | 2018-07-12 01:45:13 -0700 | [diff] [blame] | 157 | u32 rnd_margin = 128 << 10, seg_index, page_size; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 158 | segment_manager_properties_t *props; |
| 159 | uword baseva = (u64) ~ 0, alloc_size; |
| 160 | svm_fifo_segment_private_t *seg; |
| 161 | u8 *seg_name; |
| 162 | int rv; |
| 163 | |
| 164 | props = segment_manager_properties_get (sm); |
| 165 | |
| 166 | /* Not configured for addition of new segments and not first */ |
| 167 | if (!props->add_segment && !segment_size) |
| 168 | { |
| 169 | clib_warning ("cannot allocate new segment"); |
| 170 | return VNET_API_ERROR_INVALID_VALUE; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Allocate fifo segment and lock if needed |
| 175 | */ |
| 176 | if (vlib_num_workers ()) |
| 177 | { |
| 178 | clib_rwlock_writer_lock (&sm->segments_rwlock); |
| 179 | pool_get (sm->segments, seg); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | pool_get (sm->segments, seg); |
| 184 | } |
| 185 | memset (seg, 0, sizeof (*seg)); |
| 186 | |
| 187 | /* |
| 188 | * Initialize ssvm segment and svm fifo private header |
| 189 | */ |
| 190 | segment_size = segment_size ? segment_size : props->add_segment_size; |
Florin Coras | 5da96a7 | 2018-07-12 01:45:13 -0700 | [diff] [blame] | 191 | page_size = clib_mem_get_page_size (); |
| 192 | segment_size = (segment_size + page_size - 1) & ~(page_size - 1); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 193 | if (props->segment_type != SSVM_SEGMENT_PRIVATE) |
| 194 | { |
| 195 | seg_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
| 196 | alloc_size = segment_size + rnd_margin; |
| 197 | baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0); |
| 198 | if (!baseva) |
| 199 | { |
| 200 | clib_warning ("out of space for segments"); |
| 201 | return -1; |
| 202 | } |
| 203 | } |
| 204 | else |
| 205 | seg_name = format (0, "%s%c", "process-private-segment", 0); |
| 206 | |
| 207 | seg->ssvm.ssvm_size = segment_size; |
| 208 | seg->ssvm.name = seg_name; |
| 209 | seg->ssvm.requested_va = baseva; |
| 210 | |
| 211 | if ((rv = ssvm_master_init (&seg->ssvm, props->segment_type))) |
| 212 | { |
| 213 | clib_warning ("svm_master_init ('%v', %u) failed", seg_name, |
| 214 | segment_size); |
| 215 | |
| 216 | if (props->segment_type != SSVM_SEGMENT_PRIVATE) |
| 217 | clib_valloc_free (&smm->va_allocator, baseva); |
| 218 | pool_put (sm->segments, seg); |
| 219 | return (rv); |
| 220 | } |
| 221 | |
| 222 | svm_fifo_segment_init (seg); |
| 223 | |
| 224 | /* |
| 225 | * Save segment index before dropping lock, if any held |
| 226 | */ |
| 227 | seg_index = seg - sm->segments; |
| 228 | |
| 229 | if (vlib_num_workers ()) |
| 230 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 231 | |
| 232 | return seg_index; |
| 233 | } |
| 234 | |
| 235 | segment_manager_t * |
| 236 | segment_manager_new () |
| 237 | { |
| 238 | segment_manager_main_t *smm = &segment_manager_main; |
| 239 | segment_manager_t *sm; |
| 240 | pool_get (smm->segment_managers, sm); |
| 241 | memset (sm, 0, sizeof (*sm)); |
| 242 | clib_rwlock_init (&sm->segments_rwlock); |
| 243 | return sm; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Initializes segment manager based on options provided. |
| 248 | * Returns error if ssvm segment(s) allocation fails. |
| 249 | */ |
| 250 | int |
| 251 | segment_manager_init (segment_manager_t * sm, u32 first_seg_size, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 252 | u32 prealloc_fifo_pairs) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 253 | { |
| 254 | u32 rx_fifo_size, tx_fifo_size, pair_size; |
| 255 | u32 rx_rounded_data_size, tx_rounded_data_size; |
| 256 | u64 approx_total_size, max_seg_size = |
| 257 | ((u64) 1 << 32) - clib_mem_get_page_size (); |
| 258 | segment_manager_properties_t *props; |
| 259 | svm_fifo_segment_private_t *segment; |
| 260 | u32 approx_segment_count; |
| 261 | int seg_index, i; |
| 262 | |
| 263 | props = segment_manager_properties_get (sm); |
| 264 | first_seg_size = clib_max (first_seg_size, default_segment_size); |
| 265 | |
| 266 | if (prealloc_fifo_pairs) |
| 267 | { |
| 268 | /* Figure out how many segments should be preallocated */ |
| 269 | rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size))); |
| 270 | tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size))); |
| 271 | |
| 272 | rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size; |
| 273 | tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size; |
| 274 | pair_size = rx_fifo_size + tx_fifo_size; |
| 275 | |
| 276 | approx_total_size = (u64) prealloc_fifo_pairs *pair_size; |
| 277 | if (first_seg_size > approx_total_size) |
| 278 | max_seg_size = first_seg_size; |
| 279 | approx_segment_count = (approx_total_size + (max_seg_size - 1)) |
| 280 | / max_seg_size; |
| 281 | |
| 282 | /* Allocate the segments */ |
| 283 | for (i = 0; i < approx_segment_count + 1; i++) |
| 284 | { |
| 285 | seg_index = segment_manager_add_segment (sm, max_seg_size); |
| 286 | if (seg_index < 0) |
| 287 | { |
| 288 | clib_warning ("Failed to preallocate segment %d", i); |
| 289 | return seg_index; |
| 290 | } |
| 291 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 292 | segment = segment_manager_get_segment (sm, seg_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 293 | if (i == 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 294 | sm->event_queue = segment_manager_alloc_queue (segment, props); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 295 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 296 | svm_fifo_segment_preallocate_fifo_pairs (segment, |
| 297 | props->rx_fifo_size, |
| 298 | props->tx_fifo_size, |
| 299 | &prealloc_fifo_pairs); |
| 300 | svm_fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED; |
| 301 | if (prealloc_fifo_pairs == 0) |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | seg_index = segment_manager_add_segment (sm, first_seg_size); |
| 308 | if (seg_index) |
| 309 | { |
| 310 | clib_warning ("Failed to allocate segment"); |
| 311 | return seg_index; |
| 312 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 313 | segment = segment_manager_get_segment (sm, seg_index); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 314 | sm->event_queue = segment_manager_alloc_queue (segment, props); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | u8 |
| 321 | segment_manager_has_fifos (segment_manager_t * sm) |
| 322 | { |
| 323 | svm_fifo_segment_private_t *seg; |
| 324 | u8 first = 1; |
| 325 | |
| 326 | /* *INDENT-OFF* */ |
| 327 | segment_manager_foreach_segment_w_lock (seg, sm, ({ |
| 328 | if (CLIB_DEBUG && !first && !svm_fifo_segment_has_fifos (seg) |
| 329 | && !(svm_fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED)) |
| 330 | { |
| 331 | clib_warning ("segment %d has no fifos!", |
| 332 | segment_manager_segment_index (sm, seg)); |
| 333 | first = 0; |
| 334 | } |
| 335 | if (svm_fifo_segment_has_fifos (seg)) |
| 336 | { |
| 337 | segment_manager_segment_reader_unlock (sm); |
| 338 | return 1; |
| 339 | } |
| 340 | })); |
| 341 | /* *INDENT-ON* */ |
| 342 | |
| 343 | return 0; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Initiate disconnects for all sessions 'owned' by a segment manager |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 348 | */ |
| 349 | void |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 350 | segment_manager_del_sessions (segment_manager_t * sm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 351 | { |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 352 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 353 | stream_session_t *session; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 354 | svm_fifo_t *fifo; |
| 355 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 356 | ASSERT (pool_elts (sm->segments) != 0); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 357 | |
| 358 | /* Across all fifo segments used by the server */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 359 | /* *INDENT-OFF* */ |
| 360 | segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({ |
| 361 | fifo = svm_fifo_segment_get_fifo_list (fifo_segment); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 362 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 363 | /* |
| 364 | * Remove any residual sessions from the session lookup table |
| 365 | * Don't bother deleting the individual fifos, we're going to |
| 366 | * throw away the fifo segment in a minute. |
| 367 | */ |
| 368 | while (fifo) |
| 369 | { |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 370 | if (fifo->master_thread_index == 255) |
| 371 | { |
| 372 | svm_fifo_t *next = fifo->next; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 373 | application_local_session_disconnect_w_index (sm->app_wrk_index, |
Florin Coras | f6647e0 | 2018-03-23 22:56:43 -0700 | [diff] [blame] | 374 | fifo->master_session_index); |
| 375 | fifo = next; |
| 376 | continue; |
| 377 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 378 | session = session_get (fifo->master_session_index, |
| 379 | fifo->master_thread_index); |
| 380 | stream_session_disconnect (session); |
| 381 | fifo = fifo->next; |
| 382 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 383 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 384 | /* Instead of removing the segment, test when cleaning up disconnected |
| 385 | * sessions if the segment can be removed. |
| 386 | */ |
| 387 | })); |
| 388 | /* *INDENT-ON* */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 389 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 390 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 391 | /** |
| 392 | * Removes segment manager. |
| 393 | * |
| 394 | * Since the fifos allocated in the segment keep backpointers to the sessions |
| 395 | * prior to removing the segment, we call session disconnect. This |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 396 | * subsequently propagates into transport. |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 397 | */ |
| 398 | void |
| 399 | segment_manager_del (segment_manager_t * sm) |
| 400 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 401 | segment_manager_main_t *smm = &segment_manager_main; |
| 402 | svm_fifo_segment_private_t *fifo_segment; |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 403 | |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 404 | ASSERT (!segment_manager_has_fifos (sm) |
| 405 | && segment_manager_app_detached (sm)); |
| 406 | |
| 407 | /* If we have empty preallocated segments that haven't been removed, remove |
| 408 | * them now. Apart from that, the first segment in the first segment manager |
| 409 | * is not removed when all fifos are removed. It can only be removed when |
| 410 | * the manager is explicitly deleted/detached by the app. */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 411 | clib_rwlock_writer_lock (&sm->segments_rwlock); |
| 412 | |
| 413 | /* *INDENT-OFF* */ |
| 414 | pool_foreach (fifo_segment, sm->segments, ({ |
| 415 | segment_manager_del_segment (sm, fifo_segment); |
| 416 | })); |
| 417 | /* *INDENT-ON* */ |
| 418 | |
| 419 | clib_rwlock_writer_unlock (&sm->segments_rwlock); |
| 420 | |
| 421 | clib_rwlock_free (&sm->segments_rwlock); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 422 | if (CLIB_DEBUG) |
| 423 | memset (sm, 0xfe, sizeof (*sm)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 424 | pool_put (smm->segment_managers, sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 427 | void |
| 428 | segment_manager_init_del (segment_manager_t * sm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 429 | { |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 430 | segment_manager_app_detach (sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 431 | if (segment_manager_has_fifos (sm)) |
| 432 | segment_manager_del_sessions (sm); |
| 433 | else |
| 434 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 435 | ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm)); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 436 | segment_manager_del (sm); |
| 437 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 440 | int |
| 441 | segment_manager_try_alloc_fifos (svm_fifo_segment_private_t * fifo_segment, |
| 442 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 443 | svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo) |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 444 | { |
| 445 | rx_fifo_size = clib_max (rx_fifo_size, default_fifo_size); |
| 446 | *rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size, |
| 447 | FIFO_SEGMENT_RX_FREELIST); |
| 448 | |
| 449 | tx_fifo_size = clib_max (tx_fifo_size, default_fifo_size); |
| 450 | *tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size, |
| 451 | FIFO_SEGMENT_TX_FREELIST); |
| 452 | |
| 453 | if (*rx_fifo == 0) |
| 454 | { |
| 455 | /* This would be very odd, but handle it... */ |
| 456 | if (*tx_fifo != 0) |
| 457 | { |
| 458 | svm_fifo_segment_free_fifo (fifo_segment, *tx_fifo, |
| 459 | FIFO_SEGMENT_TX_FREELIST); |
| 460 | *tx_fifo = 0; |
| 461 | } |
| 462 | return -1; |
| 463 | } |
| 464 | if (*tx_fifo == 0) |
| 465 | { |
| 466 | if (*rx_fifo != 0) |
| 467 | { |
| 468 | svm_fifo_segment_free_fifo (fifo_segment, *rx_fifo, |
| 469 | FIFO_SEGMENT_RX_FREELIST); |
| 470 | *rx_fifo = 0; |
| 471 | } |
| 472 | return -1; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 478 | int |
| 479 | segment_manager_alloc_session_fifos (segment_manager_t * sm, |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 480 | svm_fifo_t ** rx_fifo, |
| 481 | svm_fifo_t ** tx_fifo, |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 482 | u32 * fifo_segment_index) |
| 483 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 484 | svm_fifo_segment_private_t *fifo_segment = 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 485 | int alloc_fail = 1, rv = 0, new_fs_index; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 486 | segment_manager_properties_t *props; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 487 | u8 added_a_segment = 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 488 | u32 sm_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 489 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 490 | props = segment_manager_properties_get (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 491 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 492 | /* |
| 493 | * Find the first free segment to allocate the fifos in |
| 494 | */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 495 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 496 | /* *INDENT-OFF* */ |
| 497 | segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 498 | alloc_fail = segment_manager_try_alloc_fifos (fifo_segment, |
| 499 | props->rx_fifo_size, |
| 500 | props->tx_fifo_size, |
| 501 | rx_fifo, tx_fifo); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 502 | /* Exit with lock held, drop it after notifying app */ |
| 503 | if (!alloc_fail) |
| 504 | goto alloc_success; |
| 505 | })); |
| 506 | /* *INDENT-ON* */ |
| 507 | |
| 508 | alloc_check: |
| 509 | |
| 510 | if (!alloc_fail) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 511 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 512 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 513 | alloc_success: |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 514 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 515 | ASSERT (rx_fifo && tx_fifo); |
| 516 | sm_index = segment_manager_index (sm); |
| 517 | (*tx_fifo)->segment_manager = sm_index; |
| 518 | (*rx_fifo)->segment_manager = sm_index; |
| 519 | *fifo_segment_index = segment_manager_segment_index (sm, fifo_segment); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 520 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 521 | if (added_a_segment) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 522 | rv = app_worker_add_segment_notify (sm->app_wrk_index, |
| 523 | &fifo_segment->ssvm); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 524 | /* Drop the lock after app is notified */ |
| 525 | segment_manager_segment_reader_unlock (sm); |
| 526 | return rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 529 | /* |
| 530 | * Allocation failed, see if we can add a new segment |
| 531 | */ |
| 532 | if (props->add_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 533 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 534 | if (added_a_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 535 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 536 | clib_warning ("Added a segment, still can't allocate a fifo"); |
| 537 | segment_manager_segment_reader_unlock (sm); |
| 538 | return SESSION_ERROR_NEW_SEG_NO_SPACE; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 539 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 540 | if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 541 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 542 | clib_warning ("Failed to add new segment"); |
| 543 | return SESSION_ERROR_SEG_CREATE; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 544 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 545 | fifo_segment = segment_manager_get_segment_w_lock (sm, new_fs_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 546 | alloc_fail = segment_manager_try_alloc_fifos (fifo_segment, |
| 547 | props->rx_fifo_size, |
| 548 | props->tx_fifo_size, |
| 549 | rx_fifo, tx_fifo); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 550 | added_a_segment = 1; |
| 551 | goto alloc_check; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 552 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 553 | else |
| 554 | { |
| 555 | clib_warning ("Can't add new seg and no space to allocate fifos!"); |
| 556 | return SESSION_ERROR_NO_SPACE; |
| 557 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 561 | segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo, |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 562 | svm_fifo_t * tx_fifo) |
| 563 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 564 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 565 | segment_manager_t *sm; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 566 | |
| 567 | /* 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] | 568 | * as result of a detach. */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 569 | if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager))) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 570 | return; |
| 571 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 572 | fifo_segment = segment_manager_get_segment_w_lock (sm, segment_index); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 573 | svm_fifo_segment_free_fifo (fifo_segment, rx_fifo, |
| 574 | FIFO_SEGMENT_RX_FREELIST); |
| 575 | svm_fifo_segment_free_fifo (fifo_segment, tx_fifo, |
| 576 | FIFO_SEGMENT_TX_FREELIST); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 577 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 578 | /* |
| 579 | * Try to remove svm segment if it has no fifos. This can be done only if |
| 580 | * the segment is not the first in the segment manager or if it is first |
| 581 | * and it is not protected. Moreover, if the segment is first and the app |
| 582 | * has detached from the segment manager, remove the segment manager. |
| 583 | */ |
| 584 | if (!svm_fifo_segment_has_fifos (fifo_segment)) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 585 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 586 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 587 | |
| 588 | /* Remove segment if it holds no fifos or first but not protected */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 589 | if (segment_index != 0 || !sm->first_is_protected) |
| 590 | segment_manager_lock_and_del_segment (sm, segment_index); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 591 | |
| 592 | /* Remove segment manager if no sessions and detached from app */ |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 593 | if (segment_manager_app_detached (sm) |
| 594 | && !segment_manager_has_fifos (sm)) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 595 | segment_manager_del (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 596 | } |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 597 | else |
| 598 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 601 | u32 |
| 602 | segment_manager_evt_q_expected_size (u32 q_len) |
| 603 | { |
| 604 | u32 fifo_evt_size, notif_q_size, q_hdrs; |
| 605 | u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz; |
| 606 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 607 | fifo_evt_size = 1 << max_log2 (sizeof (session_event_t)); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 608 | notif_q_size = clib_max (16, q_len >> 4); |
| 609 | |
| 610 | msg_q_sz = q_len * sizeof (svm_msg_q_msg_t); |
| 611 | fifo_evt_ring_sz = q_len * fifo_evt_size; |
| 612 | session_ntf_ring_sz = notif_q_size * 256; |
| 613 | q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t); |
| 614 | |
| 615 | return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs); |
| 616 | } |
| 617 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 618 | /** |
| 619 | * Allocates shm queue in the first segment |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 620 | * |
| 621 | * Must be called with lock held |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 622 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 623 | svm_msg_q_t * |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 624 | segment_manager_alloc_queue (svm_fifo_segment_private_t * segment, |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 625 | segment_manager_properties_t * props) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 626 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 627 | u32 fifo_evt_size, session_evt_size = 256, notif_q_size; |
| 628 | svm_msg_q_cfg_t _cfg, *cfg = &_cfg; |
| 629 | svm_msg_q_t *q; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 630 | void *oldheap; |
| 631 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 632 | fifo_evt_size = sizeof (session_event_t); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 633 | notif_q_size = clib_max (16, props->evt_q_size >> 4); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 634 | /* *INDENT-OFF* */ |
| 635 | svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 636 | {props->evt_q_size, fifo_evt_size, 0}, |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 637 | {notif_q_size, session_evt_size, 0} |
| 638 | }; |
| 639 | /* *INDENT-ON* */ |
| 640 | cfg->consumer_pid = 0; |
| 641 | cfg->n_rings = 2; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 642 | cfg->q_nitems = props->evt_q_size; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 643 | cfg->ring_cfgs = rc; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 644 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 645 | oldheap = ssvm_push_heap (segment->ssvm.sh); |
| 646 | q = svm_msg_q_alloc (cfg); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 647 | ssvm_pop_heap (oldheap); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 648 | |
| 649 | if (props->use_mq_eventfd) |
| 650 | { |
| 651 | if (svm_msg_q_alloc_producer_eventfd (q)) |
| 652 | clib_warning ("failed to alloc eventfd"); |
| 653 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 654 | return q; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Frees shm queue allocated in the first segment |
| 659 | */ |
| 660 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 661 | segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 662 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 663 | svm_fifo_segment_private_t *segment; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 664 | ssvm_shared_header_t *sh; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 665 | void *oldheap; |
| 666 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 667 | ASSERT (!pool_is_free_index (sm->segments, 0)); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 668 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 669 | segment = segment_manager_get_segment_w_lock (sm, 0); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 670 | sh = segment->ssvm.sh; |
| 671 | |
| 672 | oldheap = ssvm_push_heap (sh); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 673 | svm_queue_free (q); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 674 | ssvm_pop_heap (oldheap); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 675 | segment_manager_segment_reader_unlock (sm); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Init segment vm address allocator |
| 680 | */ |
| 681 | void |
| 682 | segment_manager_main_init (segment_manager_main_init_args_t * a) |
| 683 | { |
| 684 | segment_manager_main_t *sm = &segment_manager_main; |
| 685 | clib_valloc_chunk_t _ip, *ip = &_ip; |
| 686 | |
| 687 | ip->baseva = a->baseva; |
| 688 | ip->size = a->size; |
| 689 | |
| 690 | clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ ); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 693 | static clib_error_t * |
| 694 | segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input, |
| 695 | vlib_cli_command_t * cmd) |
| 696 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 697 | segment_manager_main_t *smm = &segment_manager_main; |
| 698 | svm_fifo_segment_private_t *seg; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 699 | segment_manager_t *sm; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 700 | u8 show_segments = 0, verbose = 0; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 701 | uword address; |
| 702 | u64 size; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 703 | u32 active_fifos; |
| 704 | u32 free_fifos; |
| 705 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 706 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 707 | { |
| 708 | if (unformat (input, "segments")) |
| 709 | show_segments = 1; |
| 710 | else if (unformat (input, "verbose")) |
| 711 | verbose = 1; |
| 712 | else |
| 713 | return clib_error_return (0, "unknown input `%U'", |
| 714 | format_unformat_error, input); |
| 715 | } |
| 716 | vlib_cli_output (vm, "%d segment managers allocated", |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 717 | pool_elts (smm->segment_managers)); |
| 718 | if (verbose && pool_elts (smm->segment_managers)) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 719 | { |
| 720 | vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index", |
| 721 | "Segments"); |
| 722 | |
| 723 | /* *INDENT-OFF* */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 724 | pool_foreach (sm, smm->segment_managers, ({ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 725 | vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm), |
| 726 | sm->app_wrk_index, pool_elts (sm->segments)); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 727 | })); |
| 728 | /* *INDENT-ON* */ |
| 729 | |
| 730 | } |
| 731 | if (show_segments) |
| 732 | { |
Florin Coras | 2f8d8fa | 2018-01-26 06:36:04 -0800 | [diff] [blame] | 733 | vlib_cli_output (vm, "%-15s%15s%15s%15s%15s%15s", "Name", "Type", |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 734 | "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address"); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 735 | |
| 736 | /* *INDENT-OFF* */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 737 | pool_foreach (sm, smm->segment_managers, ({ |
| 738 | segment_manager_foreach_segment_w_lock (seg, sm, ({ |
| 739 | svm_fifo_segment_info (seg, &address, &size); |
| 740 | active_fifos = svm_fifo_segment_num_fifos (seg); |
| 741 | free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */); |
| 742 | vlib_cli_output (vm, "%-15v%15U%15llu%15u%15u%15llx", |
| 743 | ssvm_name (&seg->ssvm), format_svm_fifo_segment_type, |
| 744 | seg, size >> 20ULL, active_fifos, free_fifos, |
| 745 | address); |
| 746 | if (verbose) |
| 747 | vlib_cli_output (vm, "%U", format_svm_fifo_segment, seg, verbose); |
| 748 | })); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 749 | })); |
| 750 | /* *INDENT-ON* */ |
| 751 | |
| 752 | } |
| 753 | return 0; |
| 754 | } |
| 755 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 756 | /* *INDENT-OFF* */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 757 | VLIB_CLI_COMMAND (segment_manager_show_command, static) = |
| 758 | { |
| 759 | .path = "show segment-manager", |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 760 | .short_help = "show segment-manager [segments][verbose]", |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 761 | .function = segment_manager_show_fn, |
| 762 | }; |
| 763 | /* *INDENT-ON* */ |
| 764 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 765 | /* |
| 766 | * fd.io coding-style-patch-verification: ON |
| 767 | * |
| 768 | * Local Variables: |
| 769 | * eval: (c-set-style "gnu") |
| 770 | * End: |
| 771 | */ |