blob: 14e4e219996cc133996c99054bd6b212e70fa0ee [file] [log] [blame]
Florin Coras6cf30ad2017-04-04 23:08:23 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras6cf30ad2017-04-04 23:08:23 -07003 * 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 Coras88001c62019-04-24 14:44:46 -070020typedef struct segment_manager_main_
21{
22 segment_manager_t *segment_managers; /**< Pool of segment managers */
Florin Coras88001c62019-04-24 14:44:46 -070023 u32 seg_name_counter; /**< Counter for segment names */
Florin Corasa332c462018-01-31 06:52:17 -080024
Florin Coras88001c62019-04-24 14:44:46 -070025 /*
26 * Configuration
27 */
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000028 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 Coras88001c62019-04-24 14:44:46 -070034} segment_manager_main_t;
Florin Corasa5464812017-04-19 13:00:05 -070035
Florin Coras88001c62019-04-24 14:44:46 -070036static segment_manager_main_t sm_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -070037
Florin Coras88001c62019-04-24 14:44:46 -070038#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
39do { \
40 clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
Damjan Marionb2c31b62020-12-13 21:47:40 +010041 pool_foreach((VAR), ((SM)->segments)) (BODY); \
Florin Coras88001c62019-04-24 14:44:46 -070042 clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
43} while (0)
44
45static segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -080046segment_manager_properties_get (segment_manager_t * sm)
Florin Corasad0c77f2017-11-09 18:00:15 -080047{
Florin Corasab2f6db2018-08-31 14:31:41 -070048 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
49 return application_get_segment_manager_properties (app_wrk->app_index);
Florin Corasa332c462018-01-31 06:52:17 -080050}
51
Florin Coras88001c62019-04-24 14:44:46 -070052segment_manager_props_t *
53segment_manager_props_init (segment_manager_props_t * props)
Florin Corasa332c462018-01-31 06:52:17 -080054{
Florin Coras88001c62019-04-24 14:44:46 -070055 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 Shibuyad8f48e22020-01-22 12:11:42 +000059 props->max_fifo_size = sm_main.default_max_fifo_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000060 props->high_watermark = sm_main.default_high_watermark;
61 props->low_watermark = sm_main.default_low_watermark;
Florin Coras62ddc032019-12-08 18:30:42 -080062 props->n_slices = vlib_num_workers () + 1;
Florin Corasad0c77f2017-11-09 18:00:15 -080063 return props;
64}
65
Florin Corasbf395972020-04-30 15:05:24 +000066u8
Florin Coras9d063042017-09-14 03:08:00 -040067segment_manager_app_detached (segment_manager_t * sm)
68{
Florin Corasc8e812f2020-05-14 05:32:18 +000069 return (sm->flags & SEG_MANAGER_F_DETACHED);
Dave Wallace7b749fe2017-07-05 14:30:46 -040070}
71
Florin Coras4e4531e2017-11-06 23:27:56 -080072void
73segment_manager_app_detach (segment_manager_t * sm)
74{
Florin Corasc8e812f2020-05-14 05:32:18 +000075 sm->flags |= SEG_MANAGER_F_DETACHED;
Florin Coras4e4531e2017-11-06 23:27:56 -080076}
77
Florin Corasa332c462018-01-31 06:52:17 -080078always_inline u32
Florin Coras88001c62019-04-24 14:44:46 -070079segment_manager_segment_index (segment_manager_t * sm, fifo_segment_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070080{
Florin Corasa332c462018-01-31 06:52:17 -080081 return (seg - sm->segments);
82}
83
84/**
Florin Coras88001c62019-04-24 14:44:46 -070085 * 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 Coras6973c732021-06-17 15:53:38 -070090static inline int
91segment_manager_add_segment_inline (segment_manager_t *sm, uword segment_size,
Florin Corasc8249772021-11-30 20:21:57 -080092 u8 notify_app, u8 flags, u8 need_lock)
Florin Coras88001c62019-04-24 14:44:46 -070093{
94 segment_manager_main_t *smm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -070095 segment_manager_props_t *props;
Florin Corasa0024a62021-12-16 10:09:17 -080096 app_worker_t *app_wrk;
Florin Coras88001c62019-04-24 14:44:46 -070097 fifo_segment_t *fs;
Florin Coras9a45bd82020-12-28 16:28:07 -080098 u32 fs_index = ~0;
Florin Coras88001c62019-04-24 14:44:46 -070099 u8 *seg_name;
100 int rv;
101
102 props = segment_manager_properties_get (sm);
Florin Corasa0024a62021-12-16 10:09:17 -0800103 app_wrk = app_worker_get (sm->app_wrk_index);
Florin Coras88001c62019-04-24 14:44:46 -0700104
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 Corasf9d4ab42019-05-11 16:55:53 -0700113 * Allocate fifo segment and grab lock if needed
Florin Coras88001c62019-04-24 14:44:46 -0700114 */
Florin Corasc8249772021-11-30 20:21:57 -0800115 if (need_lock)
Florin Coras88001c62019-04-24 14:44:46 -0700116 clib_rwlock_writer_lock (&sm->segments_rwlock);
117
118 pool_get_zero (sm->segments, fs);
119
120 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700121 * Allocate ssvm segment
Florin Coras88001c62019-04-24 14:44:46 -0700122 */
123 segment_size = segment_size ? segment_size : props->add_segment_size;
Filip Tehlar2711ca72021-11-24 10:30:59 +0000124 /* 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 Coras9a45bd82020-12-28 16:28:07 -0800130 segment_size = round_pow2 (segment_size, clib_mem_get_page_size ());
Florin Corasf9d4ab42019-05-11 16:55:53 -0700131
Florin Corasa0024a62021-12-16 10:09:17 -0800132 seg_name = format (0, "seg-%u-%u-%u%c", app_wrk->app_index,
133 app_wrk->wrk_index, smm->seg_name_counter++, 0);
Florin Coras88001c62019-04-24 14:44:46 -0700134
135 fs->ssvm.ssvm_size = segment_size;
136 fs->ssvm.name = seg_name;
Florin Coras9a45bd82020-12-28 16:28:07 -0800137 fs->ssvm.requested_va = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700138
Florin Coras5220a262020-09-29 18:11:24 -0700139 if ((rv = ssvm_server_init (&fs->ssvm, props->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700140 {
141 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
142 segment_size);
Florin Coras88001c62019-04-24 14:44:46 -0700143 pool_put (sm->segments, fs);
144 goto done;
145 }
146
Florin Corasf9d4ab42019-05-11 16:55:53 -0700147 /*
148 * Initialize fifo segment
149 */
Florin Coras62ddc032019-12-08 18:30:42 -0800150 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700151 fifo_segment_init (fs);
152
153 /*
154 * Save segment index before dropping lock, if any held
155 */
156 fs_index = fs - sm->segments;
Florin Corascdfe8ab2021-12-22 12:54:17 -0800157 fs->fs_index = fs_index;
158 fs->sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700159
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000160 /*
161 * Set watermarks in segment
162 */
Florin Corasafbb33a2021-08-10 16:56:34 -0700163 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 Coras2de9c0f2020-02-02 19:30:39 +0000167 fs->h->pct_first_alloc = props->pct_first_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000168
liuyacan9f299032021-04-25 20:11:30 +0800169 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)
wanghanlin19f6e262021-11-29 15:05:23 +0800177 {
178 fs_index = rv;
179 goto done;
180 }
liuyacan9f299032021-04-25 20:11:30 +0800181 }
Florin Coras88001c62019-04-24 14:44:46 -0700182done:
183
Florin Corasc8249772021-11-30 20:21:57 -0800184 if (need_lock)
Florin Coras88001c62019-04-24 14:44:46 -0700185 clib_rwlock_writer_unlock (&sm->segments_rwlock);
186
187 return fs_index;
188}
189
Florin Coras6973c732021-06-17 15:53:38 -0700190int
191segment_manager_add_segment (segment_manager_t *sm, uword segment_size,
192 u8 notify_app)
193{
Florin Corasc8249772021-11-30 20:21:57 -0800194 return segment_manager_add_segment_inline (sm, segment_size, notify_app,
195 0 /* flags */, 0 /* need_lock */);
Florin Coras6973c732021-06-17 15:53:38 -0700196}
197
198int
199segment_manager_add_segment2 (segment_manager_t *sm, uword segment_size,
200 u8 flags)
201{
Florin Corasc8249772021-11-30 20:21:57 -0800202 return segment_manager_add_segment_inline (sm, segment_size, 0, flags,
203 vlib_num_workers ());
Florin Coras6973c732021-06-17 15:53:38 -0700204}
205
Florin Coras88001c62019-04-24 14:44:46 -0700206/**
Florin Corasa332c462018-01-31 06:52:17 -0800207 * Remove segment without lock
208 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800209void
Florin Coras88001c62019-04-24 14:44:46 -0700210segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800211{
Florin Corasa332c462018-01-31 06:52:17 -0800212 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800213 {
Florin Corasea7e7082020-07-07 17:57:28 -0700214 if (!segment_manager_app_detached (sm))
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800215 {
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 Corasa332c462018-01-31 06:52:17 -0800223
Florin Corasc547e912020-12-08 17:50:45 -0800224 fifo_segment_cleanup (fs);
Florin Corasa332c462018-01-31 06:52:17 -0800225 ssvm_delete (&fs->ssvm);
226
227 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400228 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800229 pool_put (sm->segments, fs);
230}
231
Florin Coras57660d92020-04-04 22:45:34 +0000232static fifo_segment_t *
233segment_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 Corasa332c462018-01-31 06:52:17 -0800241/**
242 * Removes segment after acquiring writer lock
243 */
Florin Coras99368312018-08-02 10:45:44 -0700244static inline void
Florin Coras02cc7192021-11-27 14:44:59 -0800245sm_lock_and_del_segment_inline (segment_manager_t *sm, u32 fs_index,
246 u8 check_if_empty)
Florin Corasa332c462018-01-31 06:52:17 -0800247{
Florin Coras88001c62019-04-24 14:44:46 -0700248 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800249 u8 is_prealloc;
250
251 clib_rwlock_writer_lock (&sm->segments_rwlock);
Florin Coras57660d92020-04-04 22:45:34 +0000252
253 fs = segment_manager_get_segment_if_valid (sm, fs_index);
254 if (!fs)
255 goto done;
256
Florin Coras02cc7192021-11-27 14:44:59 -0800257 if (check_if_empty && fifo_segment_has_fifos (fs))
258 goto done;
259
Florin Coras88001c62019-04-24 14:44:46 -0700260 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800261 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras57660d92020-04-04 22:45:34 +0000262 goto done;
Florin Corasa332c462018-01-31 06:52:17 -0800263
264 segment_manager_del_segment (sm, fs);
Florin Coras57660d92020-04-04 22:45:34 +0000265
266done:
Florin Corasa332c462018-01-31 06:52:17 -0800267 clib_rwlock_writer_unlock (&sm->segments_rwlock);
268}
269
Florin Coras2d0e3de2020-10-23 16:31:40 -0700270void
271segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
272{
Florin Coras02cc7192021-11-27 14:44:59 -0800273 sm_lock_and_del_segment_inline (sm, fs_index, 0 /* check_if_empty */);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700274}
275
Florin Corasa332c462018-01-31 06:52:17 -0800276/**
277 * Reads a segment from the segment manager's pool without lock
278 */
Florin Coras88001c62019-04-24 14:44:46 -0700279fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800280segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
281{
282 return pool_elt_at_index (sm->segments, segment_index);
283}
284
Florin Corasfa76a762018-11-29 12:40:10 -0800285u64
286segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700287 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800288{
289 u32 segment_index = segment_manager_segment_index (sm, segment);
290 return (((u64) segment_manager_index (sm) << 32) | segment_index);
291}
292
Florin Coras88001c62019-04-24 14:44:46 -0700293u64
294segment_manager_make_segment_handle (u32 segment_manager_index,
295 u32 segment_index)
296{
297 return (((u64) segment_manager_index << 32) | segment_index);
298}
299
300fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800301segment_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 Corasa332c462018-01-31 06:52:17 -0800314/**
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 Coras88001c62019-04-24 14:44:46 -0700321fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800322segment_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
328void
Florin Coras75ccf7b2020-03-05 19:44:02 +0000329segment_manager_segment_reader_lock (segment_manager_t * sm)
330{
331 clib_rwlock_reader_lock (&sm->segments_rwlock);
332}
333
334void
Florin Corasa332c462018-01-31 06:52:17 -0800335segment_manager_segment_reader_unlock (segment_manager_t * sm)
336{
337 clib_rwlock_reader_unlock (&sm->segments_rwlock);
338}
339
Florin Corasa332c462018-01-31 06:52:17 -0800340segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700341segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800342{
Florin Coras88001c62019-04-24 14:44:46 -0700343 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800344 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700345
346 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800347 clib_rwlock_init (&sm->segments_rwlock);
348 return sm;
349}
350
Florin Corasa332c462018-01-31 06:52:17 -0800351int
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000352segment_manager_init (segment_manager_t * sm)
Florin Corasa332c462018-01-31 06:52:17 -0800353{
Florin Coras88001c62019-04-24 14:44:46 -0700354 segment_manager_props_t *props;
Florin Corasa332c462018-01-31 06:52:17 -0800355
356 props = segment_manager_properties_get (sm);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000357
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000358 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 Shibuya234fe892019-12-25 07:40:54 +0000362 segment_manager_set_watermarks (sm,
363 props->high_watermark,
364 props->low_watermark);
Florin Corasa107f402020-09-29 19:18:46 -0700365 return 0;
366}
367
368/**
369 * Initializes segment manager based on options provided.
370 * Returns error if ssvm segment(s) allocation fails.
371 */
372int
373segment_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 Corasa332c462018-01-31 06:52:17 -0800384
Florin Coras9845c202020-04-28 01:54:22 +0000385 if (props->prealloc_fifos)
Florin Corasa332c462018-01-31 06:52:17 -0800386 {
Florin Coras9845c202020-04-28 01:54:22 +0000387 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 Corasa332c462018-01-31 06:52:17 -0800393 /* 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 {
liuyacan9f299032021-04-25 20:11:30 +0800410 fs_index = segment_manager_add_segment (sm, max_seg_size, 0);
Florin Coras9845c202020-04-28 01:54:22 +0000411 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800412 {
413 clib_warning ("Failed to preallocate segment %d", i);
Florin Coras9845c202020-04-28 01:54:22 +0000414 return fs_index;
Florin Corasa332c462018-01-31 06:52:17 -0800415 }
416
Florin Coras9845c202020-04-28 01:54:22 +0000417 fs = segment_manager_get_segment (sm, fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800418 if (i == 0)
Florin Coras9845c202020-04-28 01:54:22 +0000419 sm->event_queue = segment_manager_alloc_queue (fs, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800420
Florin Coras9845c202020-04-28 01:54:22 +0000421 fifo_segment_preallocate_fifo_pairs (fs,
Florin Coras88001c62019-04-24 14:44:46 -0700422 props->rx_fifo_size,
423 props->tx_fifo_size,
424 &prealloc_fifo_pairs);
Florin Coras9845c202020-04-28 01:54:22 +0000425 fifo_segment_flags (fs) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800426 if (prealloc_fifo_pairs == 0)
427 break;
428 }
Florin Coras9845c202020-04-28 01:54:22 +0000429 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800430 }
Florin Coras9845c202020-04-28 01:54:22 +0000431
liuyacan9f299032021-04-25 20:11:30 +0800432 fs_index = segment_manager_add_segment (sm, first_seg_size, 0);
Florin Coras9845c202020-04-28 01:54:22 +0000433 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800434 {
Florin Coras9845c202020-04-28 01:54:22 +0000435 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 Corasa332c462018-01-31 06:52:17 -0800451 {
Florin Coras9845c202020-04-28 01:54:22 +0000452 if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
453 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
Florin Corasa332c462018-01-31 06:52:17 -0800454 }
Florin Corasa332c462018-01-31 06:52:17 -0800455 }
456
457 return 0;
458}
459
Florin Corasc8e812f2020-05-14 05:32:18 +0000460void
461segment_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 Corasc87c91d2017-08-16 19:55:49 -0700472/**
Florin Coras88001c62019-04-24 14:44:46 -0700473 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700474 */
475void
Florin Coras88001c62019-04-24 14:44:46 -0700476segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700477{
Florin Coras88001c62019-04-24 14:44:46 -0700478 segment_manager_main_t *smm = &sm_main;
479 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400480
Florin Coras506aa3c2020-12-13 21:09:59 -0800481 ASSERT (vlib_get_thread_index () == 0
482 && !segment_manager_has_fifos (sm)
Florin Coras9d063042017-09-14 03:08:00 -0400483 && segment_manager_app_detached (sm));
484
Florin Corasc8e812f2020-05-14 05:32:18 +0000485 if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER)
486 segment_manager_cleanup_detached_listener (sm);
487
Florin Coras9d063042017-09-14 03:08:00 -0400488 /* 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 Corasa332c462018-01-31 06:52:17 -0800492 clib_rwlock_writer_lock (&sm->segments_rwlock);
493
494 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100495 pool_foreach (fifo_segment, sm->segments) {
Florin Corasa332c462018-01-31 06:52:17 -0800496 segment_manager_del_segment (sm, fifo_segment);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100497 }
Florin Corasa332c462018-01-31 06:52:17 -0800498 /* *INDENT-ON* */
499
Florin Corasf1af21c2021-02-26 19:19:11 -0800500 pool_free (sm->segments);
Florin Corasa332c462018-01-31 06:52:17 -0800501 clib_rwlock_writer_unlock (&sm->segments_rwlock);
502
503 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700504 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400505 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800506 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700507}
508
Florin Coras506aa3c2020-12-13 21:09:59 -0800509static void
510sm_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 Corasda78c5a2021-06-09 14:55:24 -0700521void
522segment_manager_free_safe (segment_manager_t *sm)
Florin Coras506aa3c2020-12-13 21:09:59 -0800523{
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 Corasc87c91d2017-08-16 19:55:49 -0700536void
Florin Coras88001c62019-04-24 14:44:46 -0700537segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700538{
Florin Coras506aa3c2020-12-13 21:09:59 -0800539 ASSERT (vlib_get_thread_index () == 0);
540
Florin Coras4e4531e2017-11-06 23:27:56 -0800541 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700542 if (segment_manager_has_fifos (sm))
543 segment_manager_del_sessions (sm);
544 else
545 {
Florin Coras9d063042017-09-14 03:08:00 -0400546 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700547 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700548 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700549}
550
Florin Coras88001c62019-04-24 14:44:46 -0700551segment_manager_t *
552segment_manager_get (u32 index)
553{
554 return pool_elt_at_index (sm_main.segment_managers, index);
555}
556
557segment_manager_t *
558segment_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
565u32
566segment_manager_index (segment_manager_t * sm)
567{
568 return sm - sm_main.segment_managers;
569}
570
571u8
572segment_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 */
600void
601segment_manager_del_sessions (segment_manager_t * sm)
602{
Florin Coras88001c62019-04-24 14:44:46 -0700603 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800604 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700605 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800606 int slice_index;
607 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700608
609 ASSERT (pool_elts (sm->segments) != 0);
610
611 /* Across all fifo segments used by the server */
612 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800613 segment_manager_foreach_segment_w_lock (fs, sm, ({
614 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700615 {
Florin Coras62ddc032019-12-08 18:30:42 -0800616 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 Corasc547e912020-12-08 17:50:45 -0800625 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 Coras88001c62019-04-24 14:44:46 -0700631 }
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 Coras77ea42b2020-04-14 23:52:12 +0000640 {
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 Corasf1af21c2021-02-26 19:19:11 -0800646 vec_free (handles);
Florin Coras88001c62019-04-24 14:44:46 -0700647}
648
liuyacan87d48ad2021-04-28 11:34:03 +0000649/**
650 * Initiate disconnects for sessions in specified state 'owned' by a segment
651 * manager
652 */
653void
654segment_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 Corasf8f516a2018-02-08 15:10:09 -0800702int
Florin Corascdfe8ab2021-12-22 12:54:17 -0800703segment_manager_try_alloc_fifos (fifo_segment_t *fs, u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800704 u32 rx_fifo_size, u32 tx_fifo_size,
Florin Corascdfe8ab2021-12-22 12:54:17 -0800705 svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800706{
Florin Coras88001c62019-04-24 14:44:46 -0700707 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Corascdfe8ab2021-12-22 12:54:17 -0800708 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fs, thread_index, rx_fifo_size,
Florin Coras62ddc032019-12-08 18:30:42 -0800709 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800710
Florin Coras88001c62019-04-24 14:44:46 -0700711 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Corascdfe8ab2021-12-22 12:54:17 -0800712 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fs, thread_index, tx_fifo_size,
Florin Coras62ddc032019-12-08 18:30:42 -0800713 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800714
715 if (*rx_fifo == 0)
716 {
717 /* This would be very odd, but handle it... */
718 if (*tx_fifo != 0)
719 {
Florin Corascdfe8ab2021-12-22 12:54:17 -0800720 fifo_segment_free_fifo (fs, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800721 *tx_fifo = 0;
722 }
Florin Corascdfe8ab2021-12-22 12:54:17 -0800723 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800724 }
725 if (*tx_fifo == 0)
726 {
727 if (*rx_fifo != 0)
728 {
Florin Corascdfe8ab2021-12-22 12:54:17 -0800729 fifo_segment_free_fifo (fs, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800730 *rx_fifo = 0;
731 }
Florin Corascdfe8ab2021-12-22 12:54:17 -0800732 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800733 }
734
735 return 0;
736}
737
Florin Corasc8249772021-11-30 20:21:57 -0800738static inline int
739sm_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 Coras6cf30ad2017-04-04 23:08:23 -0700743{
Florin Corasc8249772021-11-30 20:21:57 -0800744 uword free_bytes, max_free_bytes;
745 fifo_segment_t *cur, *fs = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700746
Florin Corasc8249772021-11-30 20:21:57 -0800747 max_free_bytes = props->rx_fifo_size + props->tx_fifo_size - 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700748
Florin Corasc8249772021-11-30 20:21:57 -0800749 pool_foreach (cur, sm->segments)
750 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700751 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 Coras75ccf7b2020-03-05 19:44:02 +0000759 }
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000760
Florin Corasc8249772021-11-30 20:21:57 -0800761 if (PREDICT_FALSE (!fs))
762 return SESSION_E_SEG_NO_SPACE;
763
Florin Corascdfe8ab2021-12-22 12:54:17 -0800764 return segment_manager_try_alloc_fifos (
765 fs, thread_index, props->rx_fifo_size, props->tx_fifo_size, rx_fifo,
766 tx_fifo);
Florin Corasc8249772021-11-30 20:21:57 -0800767}
768
769static int
770sm_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 Corascdfe8ab2021-12-22 12:54:17 -0800787 if (!rv)
Florin Corasc8249772021-11-30 20:21:57 -0800788 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 Corasc8249772021-11-30 20:21:57 -0800807done:
808
809 clib_rwlock_writer_unlock (&sm->segments_rwlock);
810
811 return rv;
812}
813
814int
815segment_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 Corascdfe8ab2021-12-22 12:54:17 -0800821 int rv;
Florin Corasc8249772021-11-30 20:21:57 -0800822
823 props = segment_manager_properties_get (sm);
Florin Corasc8249772021-11-30 20:21:57 -0800824
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 Corascdfe8ab2021-12-22 12:54:17 -0800832 rv = sm_lookup_segment_and_alloc_fifos (sm, props, thread_index, rx_fifo,
833 tx_fifo);
Florin Corasc8249772021-11-30 20:21:57 -0800834
Florin Coras75ccf7b2020-03-05 19:44:02 +0000835 segment_manager_segment_reader_unlock (sm);
Florin Corasa332c462018-01-31 06:52:17 -0800836
Florin Corasa332c462018-01-31 06:52:17 -0800837 /*
Florin Corasc8249772021-11-30 20:21:57 -0800838 * Slow path: if no fifo segment or alloc fail grab writer lock and try
839 * to allocate new segment
Florin Corasa332c462018-01-31 06:52:17 -0800840 */
Florin Corascdfe8ab2021-12-22 12:54:17 -0800841 if (PREDICT_FALSE (rv < 0))
842 return sm_lock_and_alloc_segment_and_fifos (sm, props, thread_index,
843 rx_fifo, tx_fifo);
liuyacan9f299032021-04-25 20:11:30 +0800844
Florin Corasc8249772021-11-30 20:21:57 -0800845 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700846}
847
848void
Florin Coras19223e02019-03-03 14:56:05 -0800849segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700850{
Florin Corasa332c462018-01-31 06:52:17 -0800851 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700852 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800853 u32 segment_index;
Florin Coras02cc7192021-11-27 14:44:59 -0800854 u8 try_delete = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700855
Florin Coras58a93e82019-01-14 23:33:46 -0800856 if (!rx_fifo || !tx_fifo)
857 return;
858
Florin Coras51222342022-06-01 14:44:11 -0700859 /* Thread that allocated the fifos must be the one to clean them up */
860 ASSERT (rx_fifo->master_thread_index == vlib_get_thread_index () ||
861 rx_fifo->refcnt > 1);
862
Florin Corasa5464812017-04-19 13:00:05 -0700863 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700864 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800865 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700866 return;
867
Florin Coras19223e02019-03-03 14:56:05 -0800868 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700869 fs = segment_manager_get_segment_w_lock (sm, segment_index);
870 fifo_segment_free_fifo (fs, rx_fifo);
871 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700872
Florin Corasc87c91d2017-08-16 19:55:49 -0700873 /*
Florin Coras02cc7192021-11-27 14:44:59 -0800874 * Try to remove fifo segment if it has no fifos. This can be done only if
Florin Corasc87c91d2017-08-16 19:55:49 -0700875 * the segment is not the first in the segment manager or if it is first
876 * and it is not protected. Moreover, if the segment is first and the app
877 * has detached from the segment manager, remove the segment manager.
878 */
Florin Coras88001c62019-04-24 14:44:46 -0700879 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700880 {
Florin Coras02cc7192021-11-27 14:44:59 -0800881 /* If first, remove only if not protected */
882 try_delete = segment_index != 0 || !sm->first_is_protected;
883 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700884
Florin Coras02cc7192021-11-27 14:44:59 -0800885 segment_manager_segment_reader_unlock (sm);
886
887 if (PREDICT_FALSE (try_delete))
888 {
889 /* Only remove if empty after writer lock acquired */
890 sm_lock_and_del_segment_inline (sm, segment_index,
891 1 /* check_if_empty */);
Florin Corasc87c91d2017-08-16 19:55:49 -0700892
893 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400894 if (segment_manager_app_detached (sm)
895 && !segment_manager_has_fifos (sm))
Florin Coras506aa3c2020-12-13 21:09:59 -0800896 segment_manager_free_safe (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700897 }
898}
899
Florin Coras6d7552c2020-04-09 01:49:45 +0000900void
Florin Coras0bc78d82021-01-09 14:34:01 -0800901segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f)
Florin Coras6d7552c2020-04-09 01:49:45 +0000902{
903 fifo_segment_t *fs;
904
Florin Coras0bc78d82021-01-09 14:34:01 -0800905 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000906 fifo_segment_detach_fifo (fs, f);
907 segment_manager_segment_reader_unlock (sm);
908}
909
910void
Florin Coras0bc78d82021-01-09 14:34:01 -0800911segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
912 session_t *s)
Florin Coras6d7552c2020-04-09 01:49:45 +0000913{
914 fifo_segment_t *fs;
915
Florin Coras0bc78d82021-01-09 14:34:01 -0800916 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000917 fifo_segment_attach_fifo (fs, f, s->thread_index);
918 segment_manager_segment_reader_unlock (sm);
919
Florin Coras0bc78d82021-01-09 14:34:01 -0800920 (*f)->shr->master_session_index = s->session_index;
921 (*f)->master_thread_index = s->thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +0000922}
923
Florin Coras3c2fed52018-07-04 04:15:05 -0700924u32
925segment_manager_evt_q_expected_size (u32 q_len)
926{
927 u32 fifo_evt_size, notif_q_size, q_hdrs;
928 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
929
Florin Coras52207f12018-07-12 14:48:06 -0700930 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700931 notif_q_size = clib_max (16, q_len >> 4);
932
933 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
934 fifo_evt_ring_sz = q_len * fifo_evt_size;
935 session_ntf_ring_sz = notif_q_size * 256;
936 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
937
938 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
939}
940
Florin Corasa5464812017-04-19 13:00:05 -0700941/**
942 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800943 *
944 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700945 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700946svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700947segment_manager_alloc_queue (fifo_segment_t * segment,
948 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700949{
Florin Coras3c2fed52018-07-04 04:15:05 -0700950 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
951 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
952 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700953
Florin Coras52207f12018-07-12 14:48:06 -0700954 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700955 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700956 /* *INDENT-OFF* */
957 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700958 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700959 {notif_q_size, session_evt_size, 0}
960 };
961 /* *INDENT-ON* */
962 cfg->consumer_pid = 0;
963 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700964 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700965 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700966
Florin Corasb4624182020-12-11 13:58:12 -0800967 q = fifo_segment_msg_q_alloc (segment, 0, cfg);
Florin Coras99368312018-08-02 10:45:44 -0700968
969 if (props->use_mq_eventfd)
970 {
Florin Coras86f12322021-01-22 15:05:14 -0800971 if (svm_msg_q_alloc_eventfd (q))
Florin Coras99368312018-08-02 10:45:44 -0700972 clib_warning ("failed to alloc eventfd");
973 }
Florin Corasa5464812017-04-19 13:00:05 -0700974 return q;
975}
976
Florin Coras88001c62019-04-24 14:44:46 -0700977svm_msg_q_t *
978segment_manager_event_queue (segment_manager_t * sm)
979{
980 return sm->event_queue;
981}
982
Florin Corasa5464812017-04-19 13:00:05 -0700983/**
984 * Frees shm queue allocated in the first segment
985 */
986void
Florin Corase86a8ed2018-01-05 03:20:25 -0800987segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700988{
Florin Coras88001c62019-04-24 14:44:46 -0700989 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800990 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700991 void *oldheap;
992
Florin Corasa332c462018-01-31 06:52:17 -0800993 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700994
Florin Corasa332c462018-01-31 06:52:17 -0800995 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700996 sh = segment->ssvm.sh;
997
998 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800999 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -07001000 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -08001001 segment_manager_segment_reader_unlock (sm);
1002}
1003
1004/*
1005 * Init segment vm address allocator
1006 */
1007void
Florin Coras9a45bd82020-12-28 16:28:07 -08001008segment_manager_main_init (void)
Florin Corasa332c462018-01-31 06:52:17 -08001009{
Florin Coras88001c62019-04-24 14:44:46 -07001010 segment_manager_main_t *sm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -07001011
1012 sm->default_fifo_size = 1 << 12;
1013 sm->default_segment_size = 1 << 20;
1014 sm->default_app_mq_size = 128;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001015 sm->default_max_fifo_size = 4 << 20;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001016 sm->default_high_watermark = 80;
1017 sm->default_low_watermark = 50;
Florin Corasa5464812017-04-19 13:00:05 -07001018}
1019
Florin Corasa0024a62021-12-16 10:09:17 -08001020static u8 *
1021format_segment_manager (u8 *s, va_list *args)
1022{
1023 segment_manager_t *sm = va_arg (*args, segment_manager_t *);
1024 int verbose = va_arg (*args, int);
1025 app_worker_t *app_wrk;
1026 uword max_fifo_size;
1027 fifo_segment_t *seg;
1028 application_t *app;
1029 u8 custom_logic;
1030
1031 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
1032 app = app_wrk ? application_get (app_wrk->app_index) : 0;
1033 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
1034 max_fifo_size = sm->max_fifo_size;
1035
1036 s = format (s,
1037 "[%u] %v app-wrk: %u segs: %u max-fifo-sz: %U "
1038 "wmarks: %u %u %s flags: 0x%x",
Florin Coras36eaaf32022-01-10 13:44:45 -08001039 segment_manager_index (sm), app ? app->name : 0,
1040 sm->app_wrk_index, pool_elts (sm->segments), format_memory_size,
1041 max_fifo_size, sm->high_watermark, sm->low_watermark,
Florin Corasa0024a62021-12-16 10:09:17 -08001042 custom_logic ? "custom-tuning" : "no-tuning", sm->flags);
1043
1044 if (!verbose || !pool_elts (sm->segments))
1045 return s;
1046
1047 s = format (s, "\n\n");
1048
1049 segment_manager_foreach_segment_w_lock (
1050 seg, sm, ({ s = format (s, " *%U", format_fifo_segment, seg, verbose); }));
1051
1052 return s;
1053}
1054
Florin Corasc87c91d2017-08-16 19:55:49 -07001055static clib_error_t *
1056segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
1057 vlib_cli_command_t * cmd)
1058{
Florin Corasa0024a62021-12-16 10:09:17 -08001059 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras88001c62019-04-24 14:44:46 -07001060 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -08001061 u8 show_segments = 0, verbose = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001062 segment_manager_t *sm;
Florin Corasa0024a62021-12-16 10:09:17 -08001063 u32 sm_index = ~0;
Dave Barach91f3e742017-09-01 19:12:11 -04001064
Florin Corasa0024a62021-12-16 10:09:17 -08001065 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corasc87c91d2017-08-16 19:55:49 -07001066 {
Florin Corasa0024a62021-12-16 10:09:17 -08001067 vlib_cli_output (vm, "%d segment managers allocated",
1068 pool_elts (smm->segment_managers));
1069 return 0;
Florin Corasc87c91d2017-08-16 19:55:49 -07001070 }
Florin Corasa0024a62021-12-16 10:09:17 -08001071
1072 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corasc87c91d2017-08-16 19:55:49 -07001073 {
Florin Corasa0024a62021-12-16 10:09:17 -08001074 if (unformat (line_input, "segments"))
1075 show_segments = 1;
1076 else if (unformat (line_input, "verbose"))
1077 verbose = 1;
1078 else if (unformat (line_input, "index %u", &sm_index))
1079 ;
1080 else
1081 {
1082 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
1083 line_input);
1084 goto done;
1085 }
1086 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001087
Florin Corasa0024a62021-12-16 10:09:17 -08001088 if (!pool_elts (smm->segment_managers))
1089 goto done;
1090
1091 if (sm_index != ~0)
1092 {
1093 sm = segment_manager_get_if_valid (sm_index);
1094 if (!sm)
1095 {
1096 vlib_cli_output (vm, "segment manager %u not allocated", sm_index);
1097 goto done;
1098 }
1099 vlib_cli_output (vm, "%U", format_segment_manager, sm, 1 /* verbose */);
1100 goto done;
1101 }
1102
1103 if (verbose || show_segments)
1104 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001105 pool_foreach (sm, smm->segment_managers) {
Florin Corasa0024a62021-12-16 10:09:17 -08001106 vlib_cli_output (vm, "%U", format_segment_manager, sm,
1107 show_segments);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001108 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001109
Florin Coras2a7c0b62020-09-28 23:40:28 -07001110 vlib_cli_output (vm, "\n");
Florin Corasc87c91d2017-08-16 19:55:49 -07001111 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001112
Florin Corasa0024a62021-12-16 10:09:17 -08001113done:
Florin Corasc87c91d2017-08-16 19:55:49 -07001114
Florin Corasa0024a62021-12-16 10:09:17 -08001115 unformat_free (line_input);
1116
Florin Corasc87c91d2017-08-16 19:55:49 -07001117 return 0;
1118}
1119
Florin Corasad0c77f2017-11-09 18:00:15 -08001120/* *INDENT-OFF* */
Florin Corasa0024a62021-12-16 10:09:17 -08001121VLIB_CLI_COMMAND (segment_manager_show_command, static) = {
Florin Corasc87c91d2017-08-16 19:55:49 -07001122 .path = "show segment-manager",
Florin Corasa0024a62021-12-16 10:09:17 -08001123 .short_help = "show segment-manager [segments][verbose][index <nn>]",
Florin Corasc87c91d2017-08-16 19:55:49 -07001124 .function = segment_manager_show_fn,
1125};
1126/* *INDENT-ON* */
1127
Florin Coras88001c62019-04-24 14:44:46 -07001128void
1129segment_manager_format_sessions (segment_manager_t * sm, int verbose)
1130{
Florin Coras88001c62019-04-24 14:44:46 -07001131 vlib_main_t *vm = vlib_get_main ();
1132 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -08001133 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -07001134 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -08001135 int slice_index;
1136 u8 *s = 0, *str;
1137 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -07001138
1139 if (!sm)
1140 {
1141 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001142 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s%-15s%-10s",
1143 "Connection", "App", "API Client", "SegManager");
Florin Coras88001c62019-04-24 14:44:46 -07001144 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001145 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s", "Connection",
1146 "App");
Florin Coras88001c62019-04-24 14:44:46 -07001147 return;
1148 }
1149
1150 app_wrk = app_worker_get (sm->app_wrk_index);
1151 app_name = application_name_from_index (app_wrk->app_index);
1152
1153 clib_rwlock_reader_lock (&sm->segments_rwlock);
1154
1155 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001156 pool_foreach (fs, sm->segments) {
Florin Coras62ddc032019-12-08 18:30:42 -08001157 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001158 {
Florin Coras62ddc032019-12-08 18:30:42 -08001159 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1160 while (f)
1161 {
1162 u32 session_index, thread_index;
1163 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -07001164
Florin Corasc547e912020-12-08 17:50:45 -08001165 session_index = f->shr->master_session_index;
1166 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -07001167
Florin Corasc547e912020-12-08 17:50:45 -08001168 session = session_get (session_index, thread_index);
1169 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001170
Florin Corasc547e912020-12-08 17:50:45 -08001171 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001172 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v%-15u%-10u", str,
1173 app_name, app_wrk->api_client_index,
Florin Corasc547e912020-12-08 17:50:45 -08001174 app_wrk->connects_seg_manager);
1175 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001176 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001177
Florin Corasc547e912020-12-08 17:50:45 -08001178 vlib_cli_output (vm, "%v", s);
1179 vec_reset_length (s);
1180 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -07001181
Florin Corasc547e912020-12-08 17:50:45 -08001182 f = f->next;
1183 }
1184 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -07001185 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001186 }
Florin Coras88001c62019-04-24 14:44:46 -07001187 /* *INDENT-ON* */
1188
1189 clib_rwlock_reader_unlock (&sm->segments_rwlock);
1190}
1191
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001192void
1193segment_manager_set_watermarks (segment_manager_t * sm,
1194 u8 high_watermark, u8 low_watermark)
1195{
Florin Coras8c79a4e2020-02-25 22:28:27 +00001196 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001197 low_watermark <= high_watermark);
1198
1199 sm->high_watermark = high_watermark;
1200 sm->low_watermark = low_watermark;
1201}
1202
Florin Coras6cf30ad2017-04-04 23:08:23 -07001203/*
1204 * fd.io coding-style-patch-verification: ON
1205 *
1206 * Local Variables:
1207 * eval: (c-set-style "gnu")
1208 * End:
1209 */