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