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