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