blob: 2b44d92e0b52a66eddc1c95accf4f7cd0e816d5c [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 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100108 SESSION_DBG ("cannot allocate new segment");
109 return SESSION_E_INVALID;
Florin Coras88001c62019-04-24 14:44:46 -0700110 }
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;
Junfeng Wangc795b882022-08-12 16:24:46 +0800130
131 if (props->huge_page)
132 {
133 uword hugepage_size = clib_mem_get_default_hugepage_size ();
134 segment_size = round_pow2 (segment_size, hugepage_size);
135 fs->ssvm.huge_page = 1;
136 }
137 else
138 segment_size = round_pow2 (segment_size, clib_mem_get_page_size ());
Florin Corasf9d4ab42019-05-11 16:55:53 -0700139
Florin Corasa0024a62021-12-16 10:09:17 -0800140 seg_name = format (0, "seg-%u-%u-%u%c", app_wrk->app_index,
141 app_wrk->wrk_index, smm->seg_name_counter++, 0);
Florin Coras88001c62019-04-24 14:44:46 -0700142
143 fs->ssvm.ssvm_size = segment_size;
144 fs->ssvm.name = seg_name;
Florin Coras9a45bd82020-12-28 16:28:07 -0800145 fs->ssvm.requested_va = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700146
Florin Coras5220a262020-09-29 18:11:24 -0700147 if ((rv = ssvm_server_init (&fs->ssvm, props->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700148 {
149 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
150 segment_size);
Florin Coras88001c62019-04-24 14:44:46 -0700151 pool_put (sm->segments, fs);
152 goto done;
153 }
154
Florin Corasf9d4ab42019-05-11 16:55:53 -0700155 /*
156 * Initialize fifo segment
157 */
Florin Coras62ddc032019-12-08 18:30:42 -0800158 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700159 fifo_segment_init (fs);
160
161 /*
162 * Save segment index before dropping lock, if any held
163 */
164 fs_index = fs - sm->segments;
Florin Corascdfe8ab2021-12-22 12:54:17 -0800165 fs->fs_index = fs_index;
166 fs->sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700167
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000168 /*
169 * Set watermarks in segment
170 */
Florin Corasafbb33a2021-08-10 16:56:34 -0700171 fs->high_watermark = sm->high_watermark;
172 fs->low_watermark = sm->low_watermark;
173 fs->flags = flags;
174 fs->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000175 fs->h->pct_first_alloc = props->pct_first_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000176
liuyacan9f299032021-04-25 20:11:30 +0800177 if (notify_app)
178 {
179 app_worker_t *app_wrk;
180 u64 fs_handle;
181 fs_handle = segment_manager_segment_handle (sm, fs);
182 app_wrk = app_worker_get (sm->app_wrk_index);
183 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
184 if (rv)
wanghanlin19f6e262021-11-29 15:05:23 +0800185 {
186 fs_index = rv;
187 goto done;
188 }
liuyacan9f299032021-04-25 20:11:30 +0800189 }
Florin Coras88001c62019-04-24 14:44:46 -0700190done:
191
Florin Corasc8249772021-11-30 20:21:57 -0800192 if (need_lock)
Florin Coras88001c62019-04-24 14:44:46 -0700193 clib_rwlock_writer_unlock (&sm->segments_rwlock);
194
195 return fs_index;
196}
197
Florin Coras6973c732021-06-17 15:53:38 -0700198int
199segment_manager_add_segment (segment_manager_t *sm, uword segment_size,
200 u8 notify_app)
201{
Florin Corasc8249772021-11-30 20:21:57 -0800202 return segment_manager_add_segment_inline (sm, segment_size, notify_app,
203 0 /* flags */, 0 /* need_lock */);
Florin Coras6973c732021-06-17 15:53:38 -0700204}
205
206int
207segment_manager_add_segment2 (segment_manager_t *sm, uword segment_size,
208 u8 flags)
209{
Florin Corasc8249772021-11-30 20:21:57 -0800210 return segment_manager_add_segment_inline (sm, segment_size, 0, flags,
211 vlib_num_workers ());
Florin Coras6973c732021-06-17 15:53:38 -0700212}
213
Florin Coras88001c62019-04-24 14:44:46 -0700214/**
Florin Corasa332c462018-01-31 06:52:17 -0800215 * Remove segment without lock
216 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800217void
Florin Coras88001c62019-04-24 14:44:46 -0700218segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800219{
Florin Corasa332c462018-01-31 06:52:17 -0800220 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800221 {
Florin Corasea7e7082020-07-07 17:57:28 -0700222 if (!segment_manager_app_detached (sm))
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800223 {
224 app_worker_t *app_wrk;
225 u64 segment_handle;
226 app_wrk = app_worker_get (sm->app_wrk_index);
227 segment_handle = segment_manager_segment_handle (sm, fs);
228 app_worker_del_segment_notify (app_wrk, segment_handle);
229 }
230 }
Florin Corasa332c462018-01-31 06:52:17 -0800231
Florin Corasc547e912020-12-08 17:50:45 -0800232 fifo_segment_cleanup (fs);
Florin Corasa332c462018-01-31 06:52:17 -0800233 ssvm_delete (&fs->ssvm);
234
235 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400236 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800237 pool_put (sm->segments, fs);
238}
239
Florin Coras57660d92020-04-04 22:45:34 +0000240static fifo_segment_t *
241segment_manager_get_segment_if_valid (segment_manager_t * sm,
242 u32 segment_index)
243{
244 if (pool_is_free_index (sm->segments, segment_index))
245 return 0;
246 return pool_elt_at_index (sm->segments, segment_index);
247}
248
Florin Corasa332c462018-01-31 06:52:17 -0800249/**
250 * Removes segment after acquiring writer lock
251 */
Florin Coras99368312018-08-02 10:45:44 -0700252static inline void
Florin Coras02cc7192021-11-27 14:44:59 -0800253sm_lock_and_del_segment_inline (segment_manager_t *sm, u32 fs_index,
254 u8 check_if_empty)
Florin Corasa332c462018-01-31 06:52:17 -0800255{
Florin Coras88001c62019-04-24 14:44:46 -0700256 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800257 u8 is_prealloc;
258
259 clib_rwlock_writer_lock (&sm->segments_rwlock);
Florin Coras57660d92020-04-04 22:45:34 +0000260
261 fs = segment_manager_get_segment_if_valid (sm, fs_index);
262 if (!fs)
263 goto done;
264
Florin Coras02cc7192021-11-27 14:44:59 -0800265 if (check_if_empty && fifo_segment_has_fifos (fs))
266 goto done;
267
Florin Coras88001c62019-04-24 14:44:46 -0700268 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800269 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras57660d92020-04-04 22:45:34 +0000270 goto done;
Florin Corasa332c462018-01-31 06:52:17 -0800271
272 segment_manager_del_segment (sm, fs);
Florin Coras57660d92020-04-04 22:45:34 +0000273
274done:
Florin Corasa332c462018-01-31 06:52:17 -0800275 clib_rwlock_writer_unlock (&sm->segments_rwlock);
276}
277
Florin Coras2d0e3de2020-10-23 16:31:40 -0700278void
279segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
280{
Florin Coras02cc7192021-11-27 14:44:59 -0800281 sm_lock_and_del_segment_inline (sm, fs_index, 0 /* check_if_empty */);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700282}
283
Florin Corasa332c462018-01-31 06:52:17 -0800284/**
285 * Reads a segment from the segment manager's pool without lock
286 */
Florin Coras88001c62019-04-24 14:44:46 -0700287fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800288segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
289{
290 return pool_elt_at_index (sm->segments, segment_index);
291}
292
Florin Corasfa76a762018-11-29 12:40:10 -0800293u64
294segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700295 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800296{
297 u32 segment_index = segment_manager_segment_index (sm, segment);
298 return (((u64) segment_manager_index (sm) << 32) | segment_index);
299}
300
Florin Coras88001c62019-04-24 14:44:46 -0700301u64
302segment_manager_make_segment_handle (u32 segment_manager_index,
303 u32 segment_index)
304{
305 return (((u64) segment_manager_index << 32) | segment_index);
306}
307
308fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800309segment_manager_get_segment_w_handle (u64 segment_handle)
310{
311 u32 sm_index, segment_index;
312 segment_manager_t *sm;
313
314 segment_manager_parse_segment_handle (segment_handle, &sm_index,
315 &segment_index);
316 sm = segment_manager_get (sm_index);
317 if (!sm || pool_is_free_index (sm->segments, segment_index))
318 return 0;
319 return pool_elt_at_index (sm->segments, segment_index);
320}
321
Florin Corasa332c462018-01-31 06:52:17 -0800322/**
323 * Reads a segment from the segment manager's pool and acquires reader lock
324 *
325 * Caller must drop the reader's lock by calling
326 * @ref segment_manager_segment_reader_unlock once it finishes working with
327 * the segment.
328 */
Florin Coras88001c62019-04-24 14:44:46 -0700329fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800330segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
331{
332 clib_rwlock_reader_lock (&sm->segments_rwlock);
333 return pool_elt_at_index (sm->segments, segment_index);
334}
335
336void
Florin Coras75ccf7b2020-03-05 19:44:02 +0000337segment_manager_segment_reader_lock (segment_manager_t * sm)
338{
339 clib_rwlock_reader_lock (&sm->segments_rwlock);
340}
341
342void
Florin Corasa332c462018-01-31 06:52:17 -0800343segment_manager_segment_reader_unlock (segment_manager_t * sm)
344{
345 clib_rwlock_reader_unlock (&sm->segments_rwlock);
346}
347
Florin Corasa332c462018-01-31 06:52:17 -0800348segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700349segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800350{
Florin Coras88001c62019-04-24 14:44:46 -0700351 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800352 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700353
354 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800355 clib_rwlock_init (&sm->segments_rwlock);
356 return sm;
357}
358
Florin Corasa332c462018-01-31 06:52:17 -0800359int
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000360segment_manager_init (segment_manager_t * sm)
Florin Corasa332c462018-01-31 06:52:17 -0800361{
Florin Coras88001c62019-04-24 14:44:46 -0700362 segment_manager_props_t *props;
Florin Corasa332c462018-01-31 06:52:17 -0800363
364 props = segment_manager_properties_get (sm);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000365
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000366 sm->max_fifo_size = props->max_fifo_size ?
367 props->max_fifo_size : sm_main.default_max_fifo_size;
368 sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
369
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000370 segment_manager_set_watermarks (sm,
371 props->high_watermark,
372 props->low_watermark);
Florin Corasa107f402020-09-29 19:18:46 -0700373 return 0;
374}
375
376/**
377 * Initializes segment manager based on options provided.
378 * Returns error if ssvm segment(s) allocation fails.
379 */
380int
381segment_manager_init_first (segment_manager_t * sm)
382{
383 segment_manager_props_t *props;
384 uword first_seg_size;
385 fifo_segment_t *fs;
386 int fs_index, i;
387
388 segment_manager_init (sm);
389 props = segment_manager_properties_get (sm);
390 first_seg_size = clib_max (props->segment_size,
391 sm_main.default_segment_size);
Florin Corasa332c462018-01-31 06:52:17 -0800392
Florin Coras9845c202020-04-28 01:54:22 +0000393 if (props->prealloc_fifos)
Florin Corasa332c462018-01-31 06:52:17 -0800394 {
Florin Coras9845c202020-04-28 01:54:22 +0000395 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
396 u32 rx_rounded_data_size, tx_rounded_data_size;
397 u32 prealloc_fifo_pairs = props->prealloc_fifos;
398 u32 rx_fifo_size, tx_fifo_size, pair_size;
399 u32 approx_segment_count;
400
Florin Corasa332c462018-01-31 06:52:17 -0800401 /* Figure out how many segments should be preallocated */
402 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
403 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
404
405 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
406 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
407 pair_size = rx_fifo_size + tx_fifo_size;
408
409 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
410 if (first_seg_size > approx_total_size)
411 max_seg_size = first_seg_size;
412 approx_segment_count = (approx_total_size + (max_seg_size - 1))
413 / max_seg_size;
414
415 /* Allocate the segments */
416 for (i = 0; i < approx_segment_count + 1; i++)
417 {
liuyacan9f299032021-04-25 20:11:30 +0800418 fs_index = segment_manager_add_segment (sm, max_seg_size, 0);
Florin Coras9845c202020-04-28 01:54:22 +0000419 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800420 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100421 SESSION_DBG ("Failed to preallocate segment %d", i);
Florin Coras9845c202020-04-28 01:54:22 +0000422 return fs_index;
Florin Corasa332c462018-01-31 06:52:17 -0800423 }
424
Florin Coras9845c202020-04-28 01:54:22 +0000425 fs = segment_manager_get_segment (sm, fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800426 if (i == 0)
Florin Coras9845c202020-04-28 01:54:22 +0000427 sm->event_queue = segment_manager_alloc_queue (fs, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800428
Florin Coras9845c202020-04-28 01:54:22 +0000429 fifo_segment_preallocate_fifo_pairs (fs,
Florin Coras88001c62019-04-24 14:44:46 -0700430 props->rx_fifo_size,
431 props->tx_fifo_size,
432 &prealloc_fifo_pairs);
Florin Coras9845c202020-04-28 01:54:22 +0000433 fifo_segment_flags (fs) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800434 if (prealloc_fifo_pairs == 0)
435 break;
436 }
Florin Coras9845c202020-04-28 01:54:22 +0000437 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800438 }
Florin Coras9845c202020-04-28 01:54:22 +0000439
liuyacan9f299032021-04-25 20:11:30 +0800440 fs_index = segment_manager_add_segment (sm, first_seg_size, 0);
Florin Coras9845c202020-04-28 01:54:22 +0000441 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800442 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100443 SESSION_DBG ("Failed to allocate segment");
Florin Coras9845c202020-04-28 01:54:22 +0000444 return fs_index;
445 }
446
447 fs = segment_manager_get_segment (sm, fs_index);
448 sm->event_queue = segment_manager_alloc_queue (fs, props);
449
450 if (props->prealloc_fifo_hdrs)
451 {
452 u32 hdrs_per_slice;
453
454 /* Do not preallocate on slice associated to main thread */
455 i = (vlib_num_workers ()? 1 : 0);
456 hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i);
457
458 for (; i < fs->n_slices; i++)
Florin Corasa332c462018-01-31 06:52:17 -0800459 {
Florin Coras9845c202020-04-28 01:54:22 +0000460 if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100461 return SESSION_E_SEG_CREATE;
Florin Corasa332c462018-01-31 06:52:17 -0800462 }
Florin Corasa332c462018-01-31 06:52:17 -0800463 }
464
465 return 0;
466}
467
Florin Corasc8e812f2020-05-14 05:32:18 +0000468void
469segment_manager_cleanup_detached_listener (segment_manager_t * sm)
470{
471 app_worker_t *app_wrk;
472
473 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
474 if (!app_wrk)
475 return;
476
477 app_worker_del_detached_sm (app_wrk, segment_manager_index (sm));
478}
479
Florin Corasc87c91d2017-08-16 19:55:49 -0700480/**
Florin Coras88001c62019-04-24 14:44:46 -0700481 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700482 */
483void
Florin Coras88001c62019-04-24 14:44:46 -0700484segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700485{
Florin Coras88001c62019-04-24 14:44:46 -0700486 segment_manager_main_t *smm = &sm_main;
487 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400488
Florin Coras506aa3c2020-12-13 21:09:59 -0800489 ASSERT (vlib_get_thread_index () == 0
490 && !segment_manager_has_fifos (sm)
Florin Coras9d063042017-09-14 03:08:00 -0400491 && segment_manager_app_detached (sm));
492
Florin Corasc8e812f2020-05-14 05:32:18 +0000493 if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER)
494 segment_manager_cleanup_detached_listener (sm);
495
Florin Coras9d063042017-09-14 03:08:00 -0400496 /* If we have empty preallocated segments that haven't been removed, remove
497 * them now. Apart from that, the first segment in the first segment manager
498 * is not removed when all fifos are removed. It can only be removed when
499 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800500 clib_rwlock_writer_lock (&sm->segments_rwlock);
501
Damjan Marionb2c31b62020-12-13 21:47:40 +0100502 pool_foreach (fifo_segment, sm->segments) {
Florin Corasa332c462018-01-31 06:52:17 -0800503 segment_manager_del_segment (sm, fifo_segment);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100504 }
Florin Corasa332c462018-01-31 06:52:17 -0800505
Florin Corasf1af21c2021-02-26 19:19:11 -0800506 pool_free (sm->segments);
Florin Corasa332c462018-01-31 06:52:17 -0800507 clib_rwlock_writer_unlock (&sm->segments_rwlock);
508
509 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700510 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400511 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800512 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700513}
514
Florin Coras506aa3c2020-12-13 21:09:59 -0800515static void
516sm_free_w_index_helper (void *arg)
517{
518 u32 sm_index = *(u32 *) arg;
519 segment_manager_t *sm;
520
521 ASSERT (vlib_get_thread_index () == 0);
522
523 if ((sm = segment_manager_get_if_valid (sm_index)))
524 segment_manager_free (sm);
525}
526
Florin Corasda78c5a2021-06-09 14:55:24 -0700527void
528segment_manager_free_safe (segment_manager_t *sm)
Florin Coras506aa3c2020-12-13 21:09:59 -0800529{
530 if (!vlib_thread_is_main_w_barrier ())
531 {
532 u32 sm_index = segment_manager_index (sm);
533 vlib_rpc_call_main_thread (sm_free_w_index_helper, (u8 *) & sm_index,
534 sizeof (sm_index));
535 }
536 else
537 {
538 segment_manager_free (sm);
539 }
540}
541
Florin Corasc87c91d2017-08-16 19:55:49 -0700542void
Florin Coras88001c62019-04-24 14:44:46 -0700543segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700544{
Florin Coras506aa3c2020-12-13 21:09:59 -0800545 ASSERT (vlib_get_thread_index () == 0);
546
Florin Coras4e4531e2017-11-06 23:27:56 -0800547 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700548 if (segment_manager_has_fifos (sm))
549 segment_manager_del_sessions (sm);
550 else
551 {
Florin Coras9d063042017-09-14 03:08:00 -0400552 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700553 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700554 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700555}
556
Florin Coras88001c62019-04-24 14:44:46 -0700557segment_manager_t *
558segment_manager_get (u32 index)
559{
560 return pool_elt_at_index (sm_main.segment_managers, index);
561}
562
563segment_manager_t *
564segment_manager_get_if_valid (u32 index)
565{
566 if (pool_is_free_index (sm_main.segment_managers, index))
567 return 0;
568 return pool_elt_at_index (sm_main.segment_managers, index);
569}
570
571u32
572segment_manager_index (segment_manager_t * sm)
573{
574 return sm - sm_main.segment_managers;
575}
576
577u8
578segment_manager_has_fifos (segment_manager_t * sm)
579{
580 fifo_segment_t *seg;
581 u8 first = 1;
582
Florin Coras88001c62019-04-24 14:44:46 -0700583 segment_manager_foreach_segment_w_lock (seg, sm, ({
584 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
585 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
586 {
587 clib_warning ("segment %d has no fifos!",
588 segment_manager_segment_index (sm, seg));
589 first = 0;
590 }
591 if (fifo_segment_has_fifos (seg))
592 {
593 segment_manager_segment_reader_unlock (sm);
594 return 1;
595 }
596 }));
Florin Coras88001c62019-04-24 14:44:46 -0700597
598 return 0;
599}
600
601/**
602 * Initiate disconnects for all sessions 'owned' by a segment manager
603 */
604void
605segment_manager_del_sessions (segment_manager_t * sm)
606{
Florin Coras88001c62019-04-24 14:44:46 -0700607 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800608 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700609 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800610 int slice_index;
611 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700612
613 ASSERT (pool_elts (sm->segments) != 0);
614
615 /* Across all fifo segments used by the server */
Florin Coras62ddc032019-12-08 18:30:42 -0800616 segment_manager_foreach_segment_w_lock (fs, sm, ({
617 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700618 {
Florin Coras62ddc032019-12-08 18:30:42 -0800619 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
620
621 /*
622 * Remove any residual sessions from the session lookup table
623 * Don't bother deleting the individual fifos, we're going to
624 * throw away the fifo segment in a minute.
625 */
626 while (f)
627 {
Florin Corasc547e912020-12-08 17:50:45 -0800628 session = session_get_if_valid (f->shr->master_session_index,
629 f->master_thread_index);
630 if (session)
631 vec_add1 (handles, session_handle (session));
632 f = f->next;
633 }
Florin Coras88001c62019-04-24 14:44:46 -0700634 }
635
636 /* Instead of removing the segment, test when cleaning up disconnected
637 * sessions if the segment can be removed.
638 */
639 }));
Florin Coras88001c62019-04-24 14:44:46 -0700640
641 vec_foreach (handle, handles)
Florin Coras77ea42b2020-04-14 23:52:12 +0000642 {
643 session = session_get_from_handle (*handle);
644 session_close (session);
645 /* Avoid propagating notifications back to the app */
646 session->app_wrk_index = APP_INVALID_INDEX;
647 }
Florin Corasf1af21c2021-02-26 19:19:11 -0800648 vec_free (handles);
Florin Coras88001c62019-04-24 14:44:46 -0700649}
650
liuyacan87d48ad2021-04-28 11:34:03 +0000651/**
652 * Initiate disconnects for sessions in specified state 'owned' by a segment
653 * manager
654 */
655void
656segment_manager_del_sessions_filter (segment_manager_t *sm,
657 session_state_t *states)
658{
659 session_handle_t *handles = 0, *handle;
660 fifo_segment_t *fs;
661 session_t *session;
662 int slice_index;
663 svm_fifo_t *f;
664
665 ASSERT (pool_elts (sm->segments) != 0);
666
667 /* Across all fifo segments used by the server */
668 segment_manager_foreach_segment_w_lock (
669 fs, sm, ({
670 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
671 {
672 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
673 while (f)
674 {
675 session = session_get_if_valid (f->shr->master_session_index,
676 f->master_thread_index);
677 if (session)
678 {
679 session_state_t *state;
680 vec_foreach (state, states)
681 {
682 if (session->session_state == *state)
683 {
684 vec_add1 (handles, session_handle (session));
685 break;
686 }
687 }
688 }
689 f = f->next;
690 }
691 }
692 }));
693
694 vec_foreach (handle, handles)
695 {
696 session = session_get_from_handle (*handle);
697 session_close (session);
698 /* Avoid propagating notifications back to the app */
699 session->app_wrk_index = APP_INVALID_INDEX;
700 }
701 vec_free (handles);
702}
703
Florin Corasf8f516a2018-02-08 15:10:09 -0800704int
Florin Corascdfe8ab2021-12-22 12:54:17 -0800705segment_manager_try_alloc_fifos (fifo_segment_t *fs, u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800706 u32 rx_fifo_size, u32 tx_fifo_size,
Florin Corascdfe8ab2021-12-22 12:54:17 -0800707 svm_fifo_t **rx_fifo, svm_fifo_t **tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800708{
Florin Coras88001c62019-04-24 14:44:46 -0700709 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Corascdfe8ab2021-12-22 12:54:17 -0800710 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fs, thread_index, rx_fifo_size,
Florin Coras62ddc032019-12-08 18:30:42 -0800711 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800712
Florin Coras88001c62019-04-24 14:44:46 -0700713 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Corascdfe8ab2021-12-22 12:54:17 -0800714 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fs, thread_index, tx_fifo_size,
Florin Coras62ddc032019-12-08 18:30:42 -0800715 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800716
717 if (*rx_fifo == 0)
718 {
719 /* This would be very odd, but handle it... */
720 if (*tx_fifo != 0)
721 {
Florin Corascdfe8ab2021-12-22 12:54:17 -0800722 fifo_segment_free_fifo (fs, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800723 *tx_fifo = 0;
724 }
Florin Corascdfe8ab2021-12-22 12:54:17 -0800725 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800726 }
727 if (*tx_fifo == 0)
728 {
729 if (*rx_fifo != 0)
730 {
Florin Corascdfe8ab2021-12-22 12:54:17 -0800731 fifo_segment_free_fifo (fs, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800732 *rx_fifo = 0;
733 }
Florin Corascdfe8ab2021-12-22 12:54:17 -0800734 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800735 }
736
737 return 0;
738}
739
Florin Corasc8249772021-11-30 20:21:57 -0800740static inline int
741sm_lookup_segment_and_alloc_fifos (segment_manager_t *sm,
742 segment_manager_props_t *props,
743 u32 thread_index, svm_fifo_t **rx_fifo,
744 svm_fifo_t **tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700745{
Florin Corasc8249772021-11-30 20:21:57 -0800746 uword free_bytes, max_free_bytes;
747 fifo_segment_t *cur, *fs = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700748
Florin Corasc8249772021-11-30 20:21:57 -0800749 max_free_bytes = props->rx_fifo_size + props->tx_fifo_size - 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700750
Florin Corasc8249772021-11-30 20:21:57 -0800751 pool_foreach (cur, sm->segments)
752 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700753 if (fifo_segment_flags (cur) & FIFO_SEGMENT_F_CUSTOM_USE)
754 continue;
755 free_bytes = fifo_segment_available_bytes (cur);
756 if (free_bytes > max_free_bytes)
757 {
758 max_free_bytes = free_bytes;
759 fs = cur;
760 }
Florin Coras75ccf7b2020-03-05 19:44:02 +0000761 }
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000762
Florin Corasc8249772021-11-30 20:21:57 -0800763 if (PREDICT_FALSE (!fs))
764 return SESSION_E_SEG_NO_SPACE;
765
Florin Corascdfe8ab2021-12-22 12:54:17 -0800766 return segment_manager_try_alloc_fifos (
767 fs, thread_index, props->rx_fifo_size, props->tx_fifo_size, rx_fifo,
768 tx_fifo);
Florin Corasc8249772021-11-30 20:21:57 -0800769}
770
771static int
772sm_lock_and_alloc_segment_and_fifos (segment_manager_t *sm,
773 segment_manager_props_t *props,
774 u32 thread_index, svm_fifo_t **rx_fifo,
775 svm_fifo_t **tx_fifo)
776{
777 int new_fs_index, rv;
778 fifo_segment_t *fs;
779
780 if (!props->add_segment)
781 return SESSION_E_SEG_NO_SPACE;
782
783 clib_rwlock_writer_lock (&sm->segments_rwlock);
784
785 /* Make sure there really is no free space. Another worker might've freed
786 * some fifos or allocated a segment */
787 rv = sm_lookup_segment_and_alloc_fifos (sm, props, thread_index, rx_fifo,
788 tx_fifo);
Florin Corascdfe8ab2021-12-22 12:54:17 -0800789 if (!rv)
Florin Corasc8249772021-11-30 20:21:57 -0800790 goto done;
791
792 new_fs_index =
793 segment_manager_add_segment (sm, 0 /* segment_size*/, 1 /* notify_app */);
794 if (new_fs_index < 0)
795 {
796 rv = SESSION_E_SEG_CREATE;
797 goto done;
798 }
799 fs = segment_manager_get_segment (sm, new_fs_index);
800 rv = segment_manager_try_alloc_fifos (fs, thread_index, props->rx_fifo_size,
801 props->tx_fifo_size, rx_fifo, tx_fifo);
802 if (rv)
803 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100804 SESSION_DBG ("Added a segment, still can't allocate a fifo");
Florin Corasc8249772021-11-30 20:21:57 -0800805 rv = SESSION_E_SEG_NO_SPACE2;
806 goto done;
807 }
808
Florin Corasc8249772021-11-30 20:21:57 -0800809done:
810
811 clib_rwlock_writer_unlock (&sm->segments_rwlock);
812
813 return rv;
814}
815
816int
817segment_manager_alloc_session_fifos (segment_manager_t * sm,
818 u32 thread_index,
819 svm_fifo_t ** rx_fifo,
820 svm_fifo_t ** tx_fifo)
821{
822 segment_manager_props_t *props;
Florin Corascdfe8ab2021-12-22 12:54:17 -0800823 int rv;
Florin Corasc8249772021-11-30 20:21:57 -0800824
825 props = segment_manager_properties_get (sm);
Florin Corasc8249772021-11-30 20:21:57 -0800826
827 /*
828 * Fast path: find the first segment with enough free space and
829 * try to allocate the fifos. Done with reader lock
830 */
831
832 segment_manager_segment_reader_lock (sm);
833
Florin Corascdfe8ab2021-12-22 12:54:17 -0800834 rv = sm_lookup_segment_and_alloc_fifos (sm, props, thread_index, rx_fifo,
835 tx_fifo);
Florin Corasc8249772021-11-30 20:21:57 -0800836
Florin Coras75ccf7b2020-03-05 19:44:02 +0000837 segment_manager_segment_reader_unlock (sm);
Florin Corasa332c462018-01-31 06:52:17 -0800838
Florin Corasa332c462018-01-31 06:52:17 -0800839 /*
Florin Corasc8249772021-11-30 20:21:57 -0800840 * Slow path: if no fifo segment or alloc fail grab writer lock and try
841 * to allocate new segment
Florin Corasa332c462018-01-31 06:52:17 -0800842 */
Florin Corascdfe8ab2021-12-22 12:54:17 -0800843 if (PREDICT_FALSE (rv < 0))
844 return sm_lock_and_alloc_segment_and_fifos (sm, props, thread_index,
845 rx_fifo, tx_fifo);
liuyacan9f299032021-04-25 20:11:30 +0800846
Florin Corasc8249772021-11-30 20:21:57 -0800847 return 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700848}
849
850void
Florin Coras19223e02019-03-03 14:56:05 -0800851segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700852{
Florin Corasa332c462018-01-31 06:52:17 -0800853 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700854 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800855 u32 segment_index;
Florin Coras02cc7192021-11-27 14:44:59 -0800856 u8 try_delete = 0;
Florin Corasa5464812017-04-19 13:00:05 -0700857
Florin Coras58a93e82019-01-14 23:33:46 -0800858 if (!rx_fifo || !tx_fifo)
859 return;
860
Florin Coras51222342022-06-01 14:44:11 -0700861 /* Thread that allocated the fifos must be the one to clean them up */
862 ASSERT (rx_fifo->master_thread_index == vlib_get_thread_index () ||
Florin Coras0242d302022-12-22 15:03:44 -0800863 rx_fifo->refcnt > 1 || vlib_thread_is_main_w_barrier ());
Florin Coras51222342022-06-01 14:44:11 -0700864
Florin Corasa5464812017-04-19 13:00:05 -0700865 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700866 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800867 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700868 return;
869
Florin Coras19223e02019-03-03 14:56:05 -0800870 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700871 fs = segment_manager_get_segment_w_lock (sm, segment_index);
872 fifo_segment_free_fifo (fs, rx_fifo);
873 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700874
Florin Corasc87c91d2017-08-16 19:55:49 -0700875 /*
Florin Coras02cc7192021-11-27 14:44:59 -0800876 * Try to remove fifo segment if it has no fifos. This can be done only if
Florin Corasc87c91d2017-08-16 19:55:49 -0700877 * the segment is not the first in the segment manager or if it is first
878 * and it is not protected. Moreover, if the segment is first and the app
879 * has detached from the segment manager, remove the segment manager.
880 */
Florin Coras88001c62019-04-24 14:44:46 -0700881 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700882 {
Florin Coras02cc7192021-11-27 14:44:59 -0800883 /* If first, remove only if not protected */
884 try_delete = segment_index != 0 || !sm->first_is_protected;
885 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700886
Florin Coras02cc7192021-11-27 14:44:59 -0800887 segment_manager_segment_reader_unlock (sm);
888
889 if (PREDICT_FALSE (try_delete))
890 {
891 /* Only remove if empty after writer lock acquired */
892 sm_lock_and_del_segment_inline (sm, segment_index,
893 1 /* check_if_empty */);
Florin Corasc87c91d2017-08-16 19:55:49 -0700894
895 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400896 if (segment_manager_app_detached (sm)
897 && !segment_manager_has_fifos (sm))
Florin Coras506aa3c2020-12-13 21:09:59 -0800898 segment_manager_free_safe (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700899 }
900}
901
Florin Coras6d7552c2020-04-09 01:49:45 +0000902void
Florin Coras0bc78d82021-01-09 14:34:01 -0800903segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f)
Florin Coras6d7552c2020-04-09 01:49:45 +0000904{
905 fifo_segment_t *fs;
906
Florin Coras0bc78d82021-01-09 14:34:01 -0800907 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000908 fifo_segment_detach_fifo (fs, f);
909 segment_manager_segment_reader_unlock (sm);
910}
911
912void
Florin Coras0bc78d82021-01-09 14:34:01 -0800913segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
914 session_t *s)
Florin Coras6d7552c2020-04-09 01:49:45 +0000915{
916 fifo_segment_t *fs;
917
Florin Coras0bc78d82021-01-09 14:34:01 -0800918 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000919 fifo_segment_attach_fifo (fs, f, s->thread_index);
920 segment_manager_segment_reader_unlock (sm);
921
Florin Coras0bc78d82021-01-09 14:34:01 -0800922 (*f)->shr->master_session_index = s->session_index;
923 (*f)->master_thread_index = s->thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +0000924}
925
Florin Coras3c2fed52018-07-04 04:15:05 -0700926u32
927segment_manager_evt_q_expected_size (u32 q_len)
928{
929 u32 fifo_evt_size, notif_q_size, q_hdrs;
930 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
931
Florin Coras52207f12018-07-12 14:48:06 -0700932 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700933 notif_q_size = clib_max (16, q_len >> 4);
934
935 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
936 fifo_evt_ring_sz = q_len * fifo_evt_size;
937 session_ntf_ring_sz = notif_q_size * 256;
938 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
939
940 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
941}
942
Florin Corasa5464812017-04-19 13:00:05 -0700943/**
944 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800945 *
946 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700947 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700948svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700949segment_manager_alloc_queue (fifo_segment_t * segment,
950 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700951{
Florin Coras3c2fed52018-07-04 04:15:05 -0700952 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
953 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
954 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700955
Florin Coras52207f12018-07-12 14:48:06 -0700956 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700957 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700958 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700959 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700960 {notif_q_size, session_evt_size, 0}
961 };
Florin Coras3c2fed52018-07-04 04:15:05 -0700962 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 *
Steven Luongfd896fc2024-12-05 13:16:04 -08001021format_segment_manager_flags (u8 *s, va_list *args)
1022{
1023 int flags = va_arg (*args, int);
1024 typedef struct sm_flags_struct
1025 {
1026 u8 bit;
1027 char *str;
1028 } sm_flags_struct_t;
1029 sm_flags_struct_t *entry;
1030 static sm_flags_struct_t sm_flags_array[] = {
1031#define _(b, v, s) \
1032 { \
1033 .bit = 1 << b, \
1034 .str = #s, \
1035 },
1036 foreach_seg_manager_flag
1037#undef _
1038 { .str = NULL }
1039 };
1040
1041 entry = sm_flags_array;
1042 while (entry->str)
1043 {
1044 if (flags & entry->bit)
1045 s = format (s, "%s ", entry->str, entry->bit);
1046 entry++;
1047 }
1048 return s;
1049}
1050
1051u8 *
Florin Corasa0024a62021-12-16 10:09:17 -08001052format_segment_manager (u8 *s, va_list *args)
1053{
1054 segment_manager_t *sm = va_arg (*args, segment_manager_t *);
1055 int verbose = va_arg (*args, int);
Steven Luongfd896fc2024-12-05 13:16:04 -08001056 int indent = format_get_indent (s);
Florin Corasa0024a62021-12-16 10:09:17 -08001057 app_worker_t *app_wrk;
1058 uword max_fifo_size;
1059 fifo_segment_t *seg;
1060 application_t *app;
1061 u8 custom_logic;
1062
1063 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
1064 app = app_wrk ? application_get (app_wrk->app_index) : 0;
1065 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
1066 max_fifo_size = sm->max_fifo_size;
1067
1068 s = format (s,
Steven Luongfd896fc2024-12-05 13:16:04 -08001069 "%U[%u] %v app-wrk: %u segs: %u max-fifo-sz: %U "
1070 "wmarks: %u %u %s flags: %U",
1071 format_white_space, indent, segment_manager_index (sm),
1072 app ? app->name : 0, sm->app_wrk_index, pool_elts (sm->segments),
1073 format_memory_size, max_fifo_size, sm->high_watermark,
1074 sm->low_watermark, custom_logic ? "custom-tuning" : "no-tuning",
1075 format_segment_manager_flags, (int) sm->flags);
Florin Corasa0024a62021-12-16 10:09:17 -08001076
1077 if (!verbose || !pool_elts (sm->segments))
1078 return s;
1079
1080 s = format (s, "\n\n");
1081
1082 segment_manager_foreach_segment_w_lock (
Steven Luongfd896fc2024-12-05 13:16:04 -08001083 seg, sm, ({
1084 s = format (s, "%U *%U", format_white_space, indent, format_fifo_segment,
1085 seg, verbose);
1086 }));
Florin Corasa0024a62021-12-16 10:09:17 -08001087
1088 return s;
1089}
1090
Florin Corasc87c91d2017-08-16 19:55:49 -07001091static clib_error_t *
1092segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
1093 vlib_cli_command_t * cmd)
1094{
Florin Corasa0024a62021-12-16 10:09:17 -08001095 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras88001c62019-04-24 14:44:46 -07001096 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -08001097 u8 show_segments = 0, verbose = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001098 segment_manager_t *sm;
Florin Corasa0024a62021-12-16 10:09:17 -08001099 u32 sm_index = ~0;
Dave Barach91f3e742017-09-01 19:12:11 -04001100
Florin Corasa0024a62021-12-16 10:09:17 -08001101 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corasc87c91d2017-08-16 19:55:49 -07001102 {
Florin Corasa0024a62021-12-16 10:09:17 -08001103 vlib_cli_output (vm, "%d segment managers allocated",
1104 pool_elts (smm->segment_managers));
1105 return 0;
Florin Corasc87c91d2017-08-16 19:55:49 -07001106 }
Florin Corasa0024a62021-12-16 10:09:17 -08001107
1108 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corasc87c91d2017-08-16 19:55:49 -07001109 {
Florin Corasa0024a62021-12-16 10:09:17 -08001110 if (unformat (line_input, "segments"))
1111 show_segments = 1;
1112 else if (unformat (line_input, "verbose"))
1113 verbose = 1;
1114 else if (unformat (line_input, "index %u", &sm_index))
1115 ;
1116 else
1117 {
1118 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
1119 line_input);
1120 goto done;
1121 }
1122 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001123
Florin Corasa0024a62021-12-16 10:09:17 -08001124 if (!pool_elts (smm->segment_managers))
1125 goto done;
1126
1127 if (sm_index != ~0)
1128 {
1129 sm = segment_manager_get_if_valid (sm_index);
1130 if (!sm)
1131 {
1132 vlib_cli_output (vm, "segment manager %u not allocated", sm_index);
1133 goto done;
1134 }
1135 vlib_cli_output (vm, "%U", format_segment_manager, sm, 1 /* verbose */);
1136 goto done;
1137 }
1138
1139 if (verbose || show_segments)
1140 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001141 pool_foreach (sm, smm->segment_managers) {
Florin Corasa0024a62021-12-16 10:09:17 -08001142 vlib_cli_output (vm, "%U", format_segment_manager, sm,
1143 show_segments);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001144 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001145
Florin Coras2a7c0b62020-09-28 23:40:28 -07001146 vlib_cli_output (vm, "\n");
Florin Corasc87c91d2017-08-16 19:55:49 -07001147 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001148
Florin Corasa0024a62021-12-16 10:09:17 -08001149done:
Florin Corasc87c91d2017-08-16 19:55:49 -07001150
Florin Corasa0024a62021-12-16 10:09:17 -08001151 unformat_free (line_input);
1152
Florin Corasc87c91d2017-08-16 19:55:49 -07001153 return 0;
1154}
1155
Florin Corasa0024a62021-12-16 10:09:17 -08001156VLIB_CLI_COMMAND (segment_manager_show_command, static) = {
Florin Corasc87c91d2017-08-16 19:55:49 -07001157 .path = "show segment-manager",
Florin Corasa0024a62021-12-16 10:09:17 -08001158 .short_help = "show segment-manager [segments][verbose][index <nn>]",
Florin Corasc87c91d2017-08-16 19:55:49 -07001159 .function = segment_manager_show_fn,
1160};
Florin Corasc87c91d2017-08-16 19:55:49 -07001161
Florin Coras88001c62019-04-24 14:44:46 -07001162void
1163segment_manager_format_sessions (segment_manager_t * sm, int verbose)
1164{
Florin Coras88001c62019-04-24 14:44:46 -07001165 vlib_main_t *vm = vlib_get_main ();
1166 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -08001167 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -07001168 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -08001169 int slice_index;
1170 u8 *s = 0, *str;
1171 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -07001172
1173 if (!sm)
1174 {
1175 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001176 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s%-15s%-10s",
1177 "Connection", "App", "API Client", "SegManager");
Florin Coras88001c62019-04-24 14:44:46 -07001178 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001179 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s", "Connection",
1180 "App");
Florin Coras88001c62019-04-24 14:44:46 -07001181 return;
1182 }
1183
1184 app_wrk = app_worker_get (sm->app_wrk_index);
1185 app_name = application_name_from_index (app_wrk->app_index);
1186
1187 clib_rwlock_reader_lock (&sm->segments_rwlock);
1188
Damjan Marionb2c31b62020-12-13 21:47:40 +01001189 pool_foreach (fs, sm->segments) {
Florin Coras62ddc032019-12-08 18:30:42 -08001190 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001191 {
Florin Coras62ddc032019-12-08 18:30:42 -08001192 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1193 while (f)
1194 {
1195 u32 session_index, thread_index;
1196 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -07001197
Florin Corasc547e912020-12-08 17:50:45 -08001198 session_index = f->shr->master_session_index;
1199 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -07001200
Florin Corasc547e912020-12-08 17:50:45 -08001201 session = session_get (session_index, thread_index);
1202 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001203
Florin Corasc547e912020-12-08 17:50:45 -08001204 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001205 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v%-15u%-10u", str,
1206 app_name, app_wrk->api_client_index,
Florin Corasc547e912020-12-08 17:50:45 -08001207 app_wrk->connects_seg_manager);
1208 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001209 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001210
Florin Corasc547e912020-12-08 17:50:45 -08001211 vlib_cli_output (vm, "%v", s);
1212 vec_reset_length (s);
1213 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -07001214
Florin Corasc547e912020-12-08 17:50:45 -08001215 f = f->next;
1216 }
1217 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -07001218 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001219 }
Florin Coras88001c62019-04-24 14:44:46 -07001220
1221 clib_rwlock_reader_unlock (&sm->segments_rwlock);
1222}
1223
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001224void
1225segment_manager_set_watermarks (segment_manager_t * sm,
1226 u8 high_watermark, u8 low_watermark)
1227{
Florin Coras8c79a4e2020-02-25 22:28:27 +00001228 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001229 low_watermark <= high_watermark);
1230
1231 sm->high_watermark = high_watermark;
1232 sm->low_watermark = low_watermark;
1233}
1234
Florin Coras6cf30ad2017-04-04 23:08:23 -07001235/*
1236 * fd.io coding-style-patch-verification: ON
1237 *
1238 * Local Variables:
1239 * eval: (c-set-style "gnu")
1240 * End:
1241 */