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