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