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