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