blob: e72e833f53c18d6ba3ae1262a5c81ca5211def19 [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 Corasa5464812017-04-19 13:00:05 -0700859 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700860 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800861 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700862 return;
863
Florin Coras19223e02019-03-03 14:56:05 -0800864 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700865 fs = segment_manager_get_segment_w_lock (sm, segment_index);
866 fifo_segment_free_fifo (fs, rx_fifo);
867 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700868
Florin Corasc87c91d2017-08-16 19:55:49 -0700869 /*
Florin Coras02cc7192021-11-27 14:44:59 -0800870 * Try to remove fifo segment if it has no fifos. This can be done only if
Florin Corasc87c91d2017-08-16 19:55:49 -0700871 * the segment is not the first in the segment manager or if it is first
872 * and it is not protected. Moreover, if the segment is first and the app
873 * has detached from the segment manager, remove the segment manager.
874 */
Florin Coras88001c62019-04-24 14:44:46 -0700875 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700876 {
Florin Coras02cc7192021-11-27 14:44:59 -0800877 /* If first, remove only if not protected */
878 try_delete = segment_index != 0 || !sm->first_is_protected;
879 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700880
Florin Coras02cc7192021-11-27 14:44:59 -0800881 segment_manager_segment_reader_unlock (sm);
882
883 if (PREDICT_FALSE (try_delete))
884 {
885 /* Only remove if empty after writer lock acquired */
886 sm_lock_and_del_segment_inline (sm, segment_index,
887 1 /* check_if_empty */);
Florin Corasc87c91d2017-08-16 19:55:49 -0700888
889 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400890 if (segment_manager_app_detached (sm)
891 && !segment_manager_has_fifos (sm))
Florin Coras506aa3c2020-12-13 21:09:59 -0800892 segment_manager_free_safe (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700893 }
894}
895
Florin Coras6d7552c2020-04-09 01:49:45 +0000896void
Florin Coras0bc78d82021-01-09 14:34:01 -0800897segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f)
Florin Coras6d7552c2020-04-09 01:49:45 +0000898{
899 fifo_segment_t *fs;
900
Florin Coras0bc78d82021-01-09 14:34:01 -0800901 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000902 fifo_segment_detach_fifo (fs, f);
903 segment_manager_segment_reader_unlock (sm);
904}
905
906void
Florin Coras0bc78d82021-01-09 14:34:01 -0800907segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
908 session_t *s)
Florin Coras6d7552c2020-04-09 01:49:45 +0000909{
910 fifo_segment_t *fs;
911
Florin Coras0bc78d82021-01-09 14:34:01 -0800912 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000913 fifo_segment_attach_fifo (fs, f, s->thread_index);
914 segment_manager_segment_reader_unlock (sm);
915
Florin Coras0bc78d82021-01-09 14:34:01 -0800916 (*f)->shr->master_session_index = s->session_index;
917 (*f)->master_thread_index = s->thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +0000918}
919
Florin Coras3c2fed52018-07-04 04:15:05 -0700920u32
921segment_manager_evt_q_expected_size (u32 q_len)
922{
923 u32 fifo_evt_size, notif_q_size, q_hdrs;
924 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
925
Florin Coras52207f12018-07-12 14:48:06 -0700926 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700927 notif_q_size = clib_max (16, q_len >> 4);
928
929 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
930 fifo_evt_ring_sz = q_len * fifo_evt_size;
931 session_ntf_ring_sz = notif_q_size * 256;
932 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
933
934 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
935}
936
Florin Corasa5464812017-04-19 13:00:05 -0700937/**
938 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800939 *
940 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700941 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700942svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700943segment_manager_alloc_queue (fifo_segment_t * segment,
944 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700945{
Florin Coras3c2fed52018-07-04 04:15:05 -0700946 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
947 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
948 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700949
Florin Coras52207f12018-07-12 14:48:06 -0700950 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700951 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700952 /* *INDENT-OFF* */
953 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700954 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700955 {notif_q_size, session_evt_size, 0}
956 };
957 /* *INDENT-ON* */
958 cfg->consumer_pid = 0;
959 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700960 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700961 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700962
Florin Corasb4624182020-12-11 13:58:12 -0800963 q = fifo_segment_msg_q_alloc (segment, 0, cfg);
Florin Coras99368312018-08-02 10:45:44 -0700964
965 if (props->use_mq_eventfd)
966 {
Florin Coras86f12322021-01-22 15:05:14 -0800967 if (svm_msg_q_alloc_eventfd (q))
Florin Coras99368312018-08-02 10:45:44 -0700968 clib_warning ("failed to alloc eventfd");
969 }
Florin Corasa5464812017-04-19 13:00:05 -0700970 return q;
971}
972
Florin Coras88001c62019-04-24 14:44:46 -0700973svm_msg_q_t *
974segment_manager_event_queue (segment_manager_t * sm)
975{
976 return sm->event_queue;
977}
978
Florin Corasa5464812017-04-19 13:00:05 -0700979/**
980 * Frees shm queue allocated in the first segment
981 */
982void
Florin Corase86a8ed2018-01-05 03:20:25 -0800983segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700984{
Florin Coras88001c62019-04-24 14:44:46 -0700985 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800986 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700987 void *oldheap;
988
Florin Corasa332c462018-01-31 06:52:17 -0800989 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700990
Florin Corasa332c462018-01-31 06:52:17 -0800991 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700992 sh = segment->ssvm.sh;
993
994 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800995 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700996 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800997 segment_manager_segment_reader_unlock (sm);
998}
999
1000/*
1001 * Init segment vm address allocator
1002 */
1003void
Florin Coras9a45bd82020-12-28 16:28:07 -08001004segment_manager_main_init (void)
Florin Corasa332c462018-01-31 06:52:17 -08001005{
Florin Coras88001c62019-04-24 14:44:46 -07001006 segment_manager_main_t *sm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -07001007
1008 sm->default_fifo_size = 1 << 12;
1009 sm->default_segment_size = 1 << 20;
1010 sm->default_app_mq_size = 128;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +00001011 sm->default_max_fifo_size = 4 << 20;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001012 sm->default_high_watermark = 80;
1013 sm->default_low_watermark = 50;
Florin Corasa5464812017-04-19 13:00:05 -07001014}
1015
Florin Corasa0024a62021-12-16 10:09:17 -08001016static u8 *
1017format_segment_manager (u8 *s, va_list *args)
1018{
1019 segment_manager_t *sm = va_arg (*args, segment_manager_t *);
1020 int verbose = va_arg (*args, int);
1021 app_worker_t *app_wrk;
1022 uword max_fifo_size;
1023 fifo_segment_t *seg;
1024 application_t *app;
1025 u8 custom_logic;
1026
1027 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
1028 app = app_wrk ? application_get (app_wrk->app_index) : 0;
1029 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
1030 max_fifo_size = sm->max_fifo_size;
1031
1032 s = format (s,
1033 "[%u] %v app-wrk: %u segs: %u max-fifo-sz: %U "
1034 "wmarks: %u %u %s flags: 0x%x",
Florin Coras36eaaf32022-01-10 13:44:45 -08001035 segment_manager_index (sm), app ? app->name : 0,
1036 sm->app_wrk_index, pool_elts (sm->segments), format_memory_size,
1037 max_fifo_size, sm->high_watermark, sm->low_watermark,
Florin Corasa0024a62021-12-16 10:09:17 -08001038 custom_logic ? "custom-tuning" : "no-tuning", sm->flags);
1039
1040 if (!verbose || !pool_elts (sm->segments))
1041 return s;
1042
1043 s = format (s, "\n\n");
1044
1045 segment_manager_foreach_segment_w_lock (
1046 seg, sm, ({ s = format (s, " *%U", format_fifo_segment, seg, verbose); }));
1047
1048 return s;
1049}
1050
Florin Corasc87c91d2017-08-16 19:55:49 -07001051static clib_error_t *
1052segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
1053 vlib_cli_command_t * cmd)
1054{
Florin Corasa0024a62021-12-16 10:09:17 -08001055 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras88001c62019-04-24 14:44:46 -07001056 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -08001057 u8 show_segments = 0, verbose = 0;
Florin Coras5368bb02019-06-09 09:24:33 -07001058 segment_manager_t *sm;
Florin Corasa0024a62021-12-16 10:09:17 -08001059 u32 sm_index = ~0;
Dave Barach91f3e742017-09-01 19:12:11 -04001060
Florin Corasa0024a62021-12-16 10:09:17 -08001061 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corasc87c91d2017-08-16 19:55:49 -07001062 {
Florin Corasa0024a62021-12-16 10:09:17 -08001063 vlib_cli_output (vm, "%d segment managers allocated",
1064 pool_elts (smm->segment_managers));
1065 return 0;
Florin Corasc87c91d2017-08-16 19:55:49 -07001066 }
Florin Corasa0024a62021-12-16 10:09:17 -08001067
1068 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corasc87c91d2017-08-16 19:55:49 -07001069 {
Florin Corasa0024a62021-12-16 10:09:17 -08001070 if (unformat (line_input, "segments"))
1071 show_segments = 1;
1072 else if (unformat (line_input, "verbose"))
1073 verbose = 1;
1074 else if (unformat (line_input, "index %u", &sm_index))
1075 ;
1076 else
1077 {
1078 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
1079 line_input);
1080 goto done;
1081 }
1082 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001083
Florin Corasa0024a62021-12-16 10:09:17 -08001084 if (!pool_elts (smm->segment_managers))
1085 goto done;
1086
1087 if (sm_index != ~0)
1088 {
1089 sm = segment_manager_get_if_valid (sm_index);
1090 if (!sm)
1091 {
1092 vlib_cli_output (vm, "segment manager %u not allocated", sm_index);
1093 goto done;
1094 }
1095 vlib_cli_output (vm, "%U", format_segment_manager, sm, 1 /* verbose */);
1096 goto done;
1097 }
1098
1099 if (verbose || show_segments)
1100 {
Damjan Marionb2c31b62020-12-13 21:47:40 +01001101 pool_foreach (sm, smm->segment_managers) {
Florin Corasa0024a62021-12-16 10:09:17 -08001102 vlib_cli_output (vm, "%U", format_segment_manager, sm,
1103 show_segments);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001104 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001105
Florin Coras2a7c0b62020-09-28 23:40:28 -07001106 vlib_cli_output (vm, "\n");
Florin Corasc87c91d2017-08-16 19:55:49 -07001107 }
Florin Corasc87c91d2017-08-16 19:55:49 -07001108
Florin Corasa0024a62021-12-16 10:09:17 -08001109done:
Florin Corasc87c91d2017-08-16 19:55:49 -07001110
Florin Corasa0024a62021-12-16 10:09:17 -08001111 unformat_free (line_input);
1112
Florin Corasc87c91d2017-08-16 19:55:49 -07001113 return 0;
1114}
1115
Florin Corasad0c77f2017-11-09 18:00:15 -08001116/* *INDENT-OFF* */
Florin Corasa0024a62021-12-16 10:09:17 -08001117VLIB_CLI_COMMAND (segment_manager_show_command, static) = {
Florin Corasc87c91d2017-08-16 19:55:49 -07001118 .path = "show segment-manager",
Florin Corasa0024a62021-12-16 10:09:17 -08001119 .short_help = "show segment-manager [segments][verbose][index <nn>]",
Florin Corasc87c91d2017-08-16 19:55:49 -07001120 .function = segment_manager_show_fn,
1121};
1122/* *INDENT-ON* */
1123
Florin Coras88001c62019-04-24 14:44:46 -07001124void
1125segment_manager_format_sessions (segment_manager_t * sm, int verbose)
1126{
Florin Coras88001c62019-04-24 14:44:46 -07001127 vlib_main_t *vm = vlib_get_main ();
1128 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -08001129 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -07001130 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -08001131 int slice_index;
1132 u8 *s = 0, *str;
1133 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -07001134
1135 if (!sm)
1136 {
1137 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001138 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s%-15s%-10s",
1139 "Connection", "App", "API Client", "SegManager");
Florin Coras88001c62019-04-24 14:44:46 -07001140 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001141 vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-20s", "Connection",
1142 "App");
Florin Coras88001c62019-04-24 14:44:46 -07001143 return;
1144 }
1145
1146 app_wrk = app_worker_get (sm->app_wrk_index);
1147 app_name = application_name_from_index (app_wrk->app_index);
1148
1149 clib_rwlock_reader_lock (&sm->segments_rwlock);
1150
1151 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001152 pool_foreach (fs, sm->segments) {
Florin Coras62ddc032019-12-08 18:30:42 -08001153 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001154 {
Florin Coras62ddc032019-12-08 18:30:42 -08001155 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1156 while (f)
1157 {
1158 u32 session_index, thread_index;
1159 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -07001160
Florin Corasc547e912020-12-08 17:50:45 -08001161 session_index = f->shr->master_session_index;
1162 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -07001163
Florin Corasc547e912020-12-08 17:50:45 -08001164 session = session_get (session_index, thread_index);
1165 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001166
Florin Corasc547e912020-12-08 17:50:45 -08001167 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001168 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v%-15u%-10u", str,
1169 app_name, app_wrk->api_client_index,
Florin Corasc547e912020-12-08 17:50:45 -08001170 app_wrk->connects_seg_manager);
1171 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +00001172 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-20v", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001173
Florin Corasc547e912020-12-08 17:50:45 -08001174 vlib_cli_output (vm, "%v", s);
1175 vec_reset_length (s);
1176 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -07001177
Florin Corasc547e912020-12-08 17:50:45 -08001178 f = f->next;
1179 }
1180 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -07001181 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001182 }
Florin Coras88001c62019-04-24 14:44:46 -07001183 /* *INDENT-ON* */
1184
1185 clib_rwlock_reader_unlock (&sm->segments_rwlock);
1186}
1187
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001188void
1189segment_manager_set_watermarks (segment_manager_t * sm,
1190 u8 high_watermark, u8 low_watermark)
1191{
Florin Coras8c79a4e2020-02-25 22:28:27 +00001192 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001193 low_watermark <= high_watermark);
1194
1195 sm->high_watermark = high_watermark;
1196 sm->low_watermark = low_watermark;
1197}
1198
Florin Coras6cf30ad2017-04-04 23:08:23 -07001199/*
1200 * fd.io coding-style-patch-verification: ON
1201 *
1202 * Local Variables:
1203 * eval: (c-set-style "gnu")
1204 * End:
1205 */