blob: 61108326abc67bf3259442bbbd4cc5b4d91b9df6 [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 */
90int
Florin Corasef4f3e72019-12-11 14:27:53 -080091segment_manager_add_segment (segment_manager_t * sm, uword segment_size)
Florin Coras88001c62019-04-24 14:44:46 -070092{
93 segment_manager_main_t *smm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -070094 segment_manager_props_t *props;
95 fifo_segment_t *fs;
Florin Coras9a45bd82020-12-28 16:28:07 -080096 u32 fs_index = ~0;
Florin Coras88001c62019-04-24 14:44:46 -070097 u8 *seg_name;
98 int rv;
99
100 props = segment_manager_properties_get (sm);
101
102 /* Not configured for addition of new segments and not first */
103 if (!props->add_segment && !segment_size)
104 {
105 clib_warning ("cannot allocate new segment");
106 return VNET_API_ERROR_INVALID_VALUE;
107 }
108
109 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700110 * Allocate fifo segment and grab lock if needed
Florin Coras88001c62019-04-24 14:44:46 -0700111 */
112 if (vlib_num_workers ())
113 clib_rwlock_writer_lock (&sm->segments_rwlock);
114
115 pool_get_zero (sm->segments, fs);
116
117 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700118 * Allocate ssvm segment
Florin Coras88001c62019-04-24 14:44:46 -0700119 */
120 segment_size = segment_size ? segment_size : props->add_segment_size;
Florin Coras9a45bd82020-12-28 16:28:07 -0800121 segment_size = round_pow2 (segment_size, clib_mem_get_page_size ());
Florin Corasf9d4ab42019-05-11 16:55:53 -0700122
Florin Coras88001c62019-04-24 14:44:46 -0700123 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
124 {
125 seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
Florin Coras88001c62019-04-24 14:44:46 -0700126 }
127 else
Florin Coras2a7c0b62020-09-28 23:40:28 -0700128 {
129 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
130 application_t *app = application_get (app_wrk->app_index);
131 seg_name = format (0, "%v segment%c", app->name, 0);
132 }
Florin Coras88001c62019-04-24 14:44:46 -0700133
134 fs->ssvm.ssvm_size = segment_size;
135 fs->ssvm.name = seg_name;
Florin Coras9a45bd82020-12-28 16:28:07 -0800136 fs->ssvm.requested_va = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700137
Florin Coras5220a262020-09-29 18:11:24 -0700138 if ((rv = ssvm_server_init (&fs->ssvm, props->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700139 {
140 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
141 segment_size);
Florin Coras88001c62019-04-24 14:44:46 -0700142 pool_put (sm->segments, fs);
143 goto done;
144 }
145
Florin Corasf9d4ab42019-05-11 16:55:53 -0700146 /*
147 * Initialize fifo segment
148 */
Florin Coras62ddc032019-12-08 18:30:42 -0800149 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700150 fifo_segment_init (fs);
151
152 /*
153 * Save segment index before dropping lock, if any held
154 */
155 fs_index = fs - sm->segments;
156
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000157 /*
158 * Set watermarks in segment
159 */
160 fs->h->high_watermark = sm->high_watermark;
161 fs->h->low_watermark = sm->low_watermark;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000162 fs->h->pct_first_alloc = props->pct_first_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000163 fs->h->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
164
Florin Coras88001c62019-04-24 14:44:46 -0700165done:
166
167 if (vlib_num_workers ())
168 clib_rwlock_writer_unlock (&sm->segments_rwlock);
169
170 return fs_index;
171}
172
173/**
Florin Corasa332c462018-01-31 06:52:17 -0800174 * Remove segment without lock
175 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800176void
Florin Coras88001c62019-04-24 14:44:46 -0700177segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800178{
Florin Corasa332c462018-01-31 06:52:17 -0800179 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800180 {
Florin Corasea7e7082020-07-07 17:57:28 -0700181 if (!segment_manager_app_detached (sm))
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800182 {
183 app_worker_t *app_wrk;
184 u64 segment_handle;
185 app_wrk = app_worker_get (sm->app_wrk_index);
186 segment_handle = segment_manager_segment_handle (sm, fs);
187 app_worker_del_segment_notify (app_wrk, segment_handle);
188 }
189 }
Florin Corasa332c462018-01-31 06:52:17 -0800190
Florin Corasc547e912020-12-08 17:50:45 -0800191 fifo_segment_cleanup (fs);
Florin Corasa332c462018-01-31 06:52:17 -0800192 ssvm_delete (&fs->ssvm);
193
194 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400195 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800196 pool_put (sm->segments, fs);
197}
198
Florin Coras57660d92020-04-04 22:45:34 +0000199static fifo_segment_t *
200segment_manager_get_segment_if_valid (segment_manager_t * sm,
201 u32 segment_index)
202{
203 if (pool_is_free_index (sm->segments, segment_index))
204 return 0;
205 return pool_elt_at_index (sm->segments, segment_index);
206}
207
Florin Corasa332c462018-01-31 06:52:17 -0800208/**
209 * Removes segment after acquiring writer lock
210 */
Florin Coras99368312018-08-02 10:45:44 -0700211static inline void
Florin Coras2d0e3de2020-10-23 16:31:40 -0700212sm_lock_and_del_segment_inline (segment_manager_t * sm, u32 fs_index)
Florin Corasa332c462018-01-31 06:52:17 -0800213{
Florin Coras88001c62019-04-24 14:44:46 -0700214 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800215 u8 is_prealloc;
216
217 clib_rwlock_writer_lock (&sm->segments_rwlock);
Florin Coras57660d92020-04-04 22:45:34 +0000218
219 fs = segment_manager_get_segment_if_valid (sm, fs_index);
220 if (!fs)
221 goto done;
222
Florin Coras88001c62019-04-24 14:44:46 -0700223 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800224 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras57660d92020-04-04 22:45:34 +0000225 goto done;
Florin Corasa332c462018-01-31 06:52:17 -0800226
227 segment_manager_del_segment (sm, fs);
Florin Coras57660d92020-04-04 22:45:34 +0000228
229done:
Florin Corasa332c462018-01-31 06:52:17 -0800230 clib_rwlock_writer_unlock (&sm->segments_rwlock);
231}
232
Florin Coras2d0e3de2020-10-23 16:31:40 -0700233void
234segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
235{
236 sm_lock_and_del_segment_inline (sm, fs_index);
237}
238
Florin Corasa332c462018-01-31 06:52:17 -0800239/**
240 * Reads a segment from the segment manager's pool without lock
241 */
Florin Coras88001c62019-04-24 14:44:46 -0700242fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800243segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
244{
245 return pool_elt_at_index (sm->segments, segment_index);
246}
247
Florin Corasfa76a762018-11-29 12:40:10 -0800248u64
249segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700250 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800251{
252 u32 segment_index = segment_manager_segment_index (sm, segment);
253 return (((u64) segment_manager_index (sm) << 32) | segment_index);
254}
255
Florin Coras88001c62019-04-24 14:44:46 -0700256u64
257segment_manager_make_segment_handle (u32 segment_manager_index,
258 u32 segment_index)
259{
260 return (((u64) segment_manager_index << 32) | segment_index);
261}
262
263fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800264segment_manager_get_segment_w_handle (u64 segment_handle)
265{
266 u32 sm_index, segment_index;
267 segment_manager_t *sm;
268
269 segment_manager_parse_segment_handle (segment_handle, &sm_index,
270 &segment_index);
271 sm = segment_manager_get (sm_index);
272 if (!sm || pool_is_free_index (sm->segments, segment_index))
273 return 0;
274 return pool_elt_at_index (sm->segments, segment_index);
275}
276
Florin Corasa332c462018-01-31 06:52:17 -0800277/**
278 * Reads a segment from the segment manager's pool and acquires reader lock
279 *
280 * Caller must drop the reader's lock by calling
281 * @ref segment_manager_segment_reader_unlock once it finishes working with
282 * the segment.
283 */
Florin Coras88001c62019-04-24 14:44:46 -0700284fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800285segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
286{
287 clib_rwlock_reader_lock (&sm->segments_rwlock);
288 return pool_elt_at_index (sm->segments, segment_index);
289}
290
291void
Florin Coras75ccf7b2020-03-05 19:44:02 +0000292segment_manager_segment_reader_lock (segment_manager_t * sm)
293{
294 clib_rwlock_reader_lock (&sm->segments_rwlock);
295}
296
297void
Florin Corasa332c462018-01-31 06:52:17 -0800298segment_manager_segment_reader_unlock (segment_manager_t * sm)
299{
300 clib_rwlock_reader_unlock (&sm->segments_rwlock);
301}
302
303void
304segment_manager_segment_writer_unlock (segment_manager_t * sm)
305{
306 clib_rwlock_writer_unlock (&sm->segments_rwlock);
307}
308
Florin Corasa332c462018-01-31 06:52:17 -0800309segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700310segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800311{
Florin Coras88001c62019-04-24 14:44:46 -0700312 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800313 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700314
315 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800316 clib_rwlock_init (&sm->segments_rwlock);
317 return sm;
318}
319
Florin Corasa332c462018-01-31 06:52:17 -0800320int
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000321segment_manager_init (segment_manager_t * sm)
Florin Corasa332c462018-01-31 06:52:17 -0800322{
Florin Coras88001c62019-04-24 14:44:46 -0700323 segment_manager_props_t *props;
Florin Corasa332c462018-01-31 06:52:17 -0800324
325 props = segment_manager_properties_get (sm);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000326
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000327 sm->max_fifo_size = props->max_fifo_size ?
328 props->max_fifo_size : sm_main.default_max_fifo_size;
329 sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
330
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000331 segment_manager_set_watermarks (sm,
332 props->high_watermark,
333 props->low_watermark);
Florin Corasa107f402020-09-29 19:18:46 -0700334 return 0;
335}
336
337/**
338 * Initializes segment manager based on options provided.
339 * Returns error if ssvm segment(s) allocation fails.
340 */
341int
342segment_manager_init_first (segment_manager_t * sm)
343{
344 segment_manager_props_t *props;
345 uword first_seg_size;
346 fifo_segment_t *fs;
347 int fs_index, i;
348
349 segment_manager_init (sm);
350 props = segment_manager_properties_get (sm);
351 first_seg_size = clib_max (props->segment_size,
352 sm_main.default_segment_size);
Florin Corasa332c462018-01-31 06:52:17 -0800353
Florin Coras9845c202020-04-28 01:54:22 +0000354 if (props->prealloc_fifos)
Florin Corasa332c462018-01-31 06:52:17 -0800355 {
Florin Coras9845c202020-04-28 01:54:22 +0000356 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
357 u32 rx_rounded_data_size, tx_rounded_data_size;
358 u32 prealloc_fifo_pairs = props->prealloc_fifos;
359 u32 rx_fifo_size, tx_fifo_size, pair_size;
360 u32 approx_segment_count;
361
Florin Corasa332c462018-01-31 06:52:17 -0800362 /* Figure out how many segments should be preallocated */
363 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
364 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
365
366 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
367 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
368 pair_size = rx_fifo_size + tx_fifo_size;
369
370 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
371 if (first_seg_size > approx_total_size)
372 max_seg_size = first_seg_size;
373 approx_segment_count = (approx_total_size + (max_seg_size - 1))
374 / max_seg_size;
375
376 /* Allocate the segments */
377 for (i = 0; i < approx_segment_count + 1; i++)
378 {
Florin Coras9845c202020-04-28 01:54:22 +0000379 fs_index = segment_manager_add_segment (sm, max_seg_size);
380 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800381 {
382 clib_warning ("Failed to preallocate segment %d", i);
Florin Coras9845c202020-04-28 01:54:22 +0000383 return fs_index;
Florin Corasa332c462018-01-31 06:52:17 -0800384 }
385
Florin Coras9845c202020-04-28 01:54:22 +0000386 fs = segment_manager_get_segment (sm, fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800387 if (i == 0)
Florin Coras9845c202020-04-28 01:54:22 +0000388 sm->event_queue = segment_manager_alloc_queue (fs, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800389
Florin Coras9845c202020-04-28 01:54:22 +0000390 fifo_segment_preallocate_fifo_pairs (fs,
Florin Coras88001c62019-04-24 14:44:46 -0700391 props->rx_fifo_size,
392 props->tx_fifo_size,
393 &prealloc_fifo_pairs);
Florin Coras9845c202020-04-28 01:54:22 +0000394 fifo_segment_flags (fs) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800395 if (prealloc_fifo_pairs == 0)
396 break;
397 }
Florin Coras9845c202020-04-28 01:54:22 +0000398 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800399 }
Florin Coras9845c202020-04-28 01:54:22 +0000400
401 fs_index = segment_manager_add_segment (sm, first_seg_size);
402 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800403 {
Florin Coras9845c202020-04-28 01:54:22 +0000404 clib_warning ("Failed to allocate segment");
405 return fs_index;
406 }
407
408 fs = segment_manager_get_segment (sm, fs_index);
409 sm->event_queue = segment_manager_alloc_queue (fs, props);
410
411 if (props->prealloc_fifo_hdrs)
412 {
413 u32 hdrs_per_slice;
414
415 /* Do not preallocate on slice associated to main thread */
416 i = (vlib_num_workers ()? 1 : 0);
417 hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i);
418
419 for (; i < fs->n_slices; i++)
Florin Corasa332c462018-01-31 06:52:17 -0800420 {
Florin Coras9845c202020-04-28 01:54:22 +0000421 if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
422 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
Florin Corasa332c462018-01-31 06:52:17 -0800423 }
Florin Corasa332c462018-01-31 06:52:17 -0800424 }
425
426 return 0;
427}
428
Florin Corasc8e812f2020-05-14 05:32:18 +0000429void
430segment_manager_cleanup_detached_listener (segment_manager_t * sm)
431{
432 app_worker_t *app_wrk;
433
434 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
435 if (!app_wrk)
436 return;
437
438 app_worker_del_detached_sm (app_wrk, segment_manager_index (sm));
439}
440
Florin Corasc87c91d2017-08-16 19:55:49 -0700441/**
Florin Coras88001c62019-04-24 14:44:46 -0700442 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700443 */
444void
Florin Coras88001c62019-04-24 14:44:46 -0700445segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700446{
Florin Coras88001c62019-04-24 14:44:46 -0700447 segment_manager_main_t *smm = &sm_main;
448 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400449
Florin Coras506aa3c2020-12-13 21:09:59 -0800450 ASSERT (vlib_get_thread_index () == 0
451 && !segment_manager_has_fifos (sm)
Florin Coras9d063042017-09-14 03:08:00 -0400452 && segment_manager_app_detached (sm));
453
Florin Corasc8e812f2020-05-14 05:32:18 +0000454 if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER)
455 segment_manager_cleanup_detached_listener (sm);
456
Florin Coras9d063042017-09-14 03:08:00 -0400457 /* If we have empty preallocated segments that haven't been removed, remove
458 * them now. Apart from that, the first segment in the first segment manager
459 * is not removed when all fifos are removed. It can only be removed when
460 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800461 clib_rwlock_writer_lock (&sm->segments_rwlock);
462
463 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100464 pool_foreach (fifo_segment, sm->segments) {
Florin Corasa332c462018-01-31 06:52:17 -0800465 segment_manager_del_segment (sm, fifo_segment);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100466 }
Florin Corasa332c462018-01-31 06:52:17 -0800467 /* *INDENT-ON* */
468
469 clib_rwlock_writer_unlock (&sm->segments_rwlock);
470
471 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700472 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400473 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800474 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700475}
476
Florin Coras506aa3c2020-12-13 21:09:59 -0800477static void
478sm_free_w_index_helper (void *arg)
479{
480 u32 sm_index = *(u32 *) arg;
481 segment_manager_t *sm;
482
483 ASSERT (vlib_get_thread_index () == 0);
484
485 if ((sm = segment_manager_get_if_valid (sm_index)))
486 segment_manager_free (sm);
487}
488
489static void
490segment_manager_free_safe (segment_manager_t * sm)
491{
492 if (!vlib_thread_is_main_w_barrier ())
493 {
494 u32 sm_index = segment_manager_index (sm);
495 vlib_rpc_call_main_thread (sm_free_w_index_helper, (u8 *) & sm_index,
496 sizeof (sm_index));
497 }
498 else
499 {
500 segment_manager_free (sm);
501 }
502}
503
Florin Corasc87c91d2017-08-16 19:55:49 -0700504void
Florin Coras88001c62019-04-24 14:44:46 -0700505segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700506{
Florin Coras506aa3c2020-12-13 21:09:59 -0800507 ASSERT (vlib_get_thread_index () == 0);
508
Florin Coras4e4531e2017-11-06 23:27:56 -0800509 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700510 if (segment_manager_has_fifos (sm))
511 segment_manager_del_sessions (sm);
512 else
513 {
Florin Coras9d063042017-09-14 03:08:00 -0400514 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700515 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700516 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700517}
518
Florin Coras88001c62019-04-24 14:44:46 -0700519segment_manager_t *
520segment_manager_get (u32 index)
521{
522 return pool_elt_at_index (sm_main.segment_managers, index);
523}
524
525segment_manager_t *
526segment_manager_get_if_valid (u32 index)
527{
528 if (pool_is_free_index (sm_main.segment_managers, index))
529 return 0;
530 return pool_elt_at_index (sm_main.segment_managers, index);
531}
532
533u32
534segment_manager_index (segment_manager_t * sm)
535{
536 return sm - sm_main.segment_managers;
537}
538
539u8
540segment_manager_has_fifos (segment_manager_t * sm)
541{
542 fifo_segment_t *seg;
543 u8 first = 1;
544
545 /* *INDENT-OFF* */
546 segment_manager_foreach_segment_w_lock (seg, sm, ({
547 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
548 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
549 {
550 clib_warning ("segment %d has no fifos!",
551 segment_manager_segment_index (sm, seg));
552 first = 0;
553 }
554 if (fifo_segment_has_fifos (seg))
555 {
556 segment_manager_segment_reader_unlock (sm);
557 return 1;
558 }
559 }));
560 /* *INDENT-ON* */
561
562 return 0;
563}
564
565/**
566 * Initiate disconnects for all sessions 'owned' by a segment manager
567 */
568void
569segment_manager_del_sessions (segment_manager_t * sm)
570{
Florin Coras88001c62019-04-24 14:44:46 -0700571 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800572 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700573 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800574 int slice_index;
575 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700576
577 ASSERT (pool_elts (sm->segments) != 0);
578
579 /* Across all fifo segments used by the server */
580 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800581 segment_manager_foreach_segment_w_lock (fs, sm, ({
582 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700583 {
Florin Coras62ddc032019-12-08 18:30:42 -0800584 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
585
586 /*
587 * Remove any residual sessions from the session lookup table
588 * Don't bother deleting the individual fifos, we're going to
589 * throw away the fifo segment in a minute.
590 */
591 while (f)
592 {
Florin Corasc547e912020-12-08 17:50:45 -0800593 session = session_get_if_valid (f->shr->master_session_index,
594 f->master_thread_index);
595 if (session)
596 vec_add1 (handles, session_handle (session));
597 f = f->next;
598 }
Florin Coras88001c62019-04-24 14:44:46 -0700599 }
600
601 /* Instead of removing the segment, test when cleaning up disconnected
602 * sessions if the segment can be removed.
603 */
604 }));
605 /* *INDENT-ON* */
606
607 vec_foreach (handle, handles)
Florin Coras77ea42b2020-04-14 23:52:12 +0000608 {
609 session = session_get_from_handle (*handle);
610 session_close (session);
611 /* Avoid propagating notifications back to the app */
612 session->app_wrk_index = APP_INVALID_INDEX;
613 }
Florin Coras88001c62019-04-24 14:44:46 -0700614}
615
Florin Corasf8f516a2018-02-08 15:10:09 -0800616int
Florin Coras88001c62019-04-24 14:44:46 -0700617segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment,
Florin Coras62ddc032019-12-08 18:30:42 -0800618 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800619 u32 rx_fifo_size, u32 tx_fifo_size,
620 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800621{
Florin Coras88001c62019-04-24 14:44:46 -0700622 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800623 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
624 rx_fifo_size,
625 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800626
Florin Coras88001c62019-04-24 14:44:46 -0700627 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800628 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
629 tx_fifo_size,
630 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800631
632 if (*rx_fifo == 0)
633 {
634 /* This would be very odd, but handle it... */
635 if (*tx_fifo != 0)
636 {
Florin Coras88001c62019-04-24 14:44:46 -0700637 fifo_segment_free_fifo (fifo_segment, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800638 *tx_fifo = 0;
639 }
640 return -1;
641 }
642 if (*tx_fifo == 0)
643 {
644 if (*rx_fifo != 0)
645 {
Florin Coras88001c62019-04-24 14:44:46 -0700646 fifo_segment_free_fifo (fifo_segment, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800647 *rx_fifo = 0;
648 }
649 return -1;
650 }
651
652 return 0;
653}
654
Florin Coras6cf30ad2017-04-04 23:08:23 -0700655int
656segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800657 u32 thread_index,
Florin Corasb384b542018-01-15 01:08:33 -0800658 svm_fifo_t ** rx_fifo,
Florin Coras1219b2d2019-04-23 15:53:43 -0700659 svm_fifo_t ** tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700660{
Florin Corasa332c462018-01-31 06:52:17 -0800661 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000662 uword free_bytes, max_free_bytes = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700663 segment_manager_props_t *props;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000664 fifo_segment_t *fs = 0, *cur;
Florin Coras88001c62019-04-24 14:44:46 -0700665 u32 sm_index, fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700666 u8 added_a_segment = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700667 u64 fs_handle;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700668
Florin Corasa332c462018-01-31 06:52:17 -0800669 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700670
Florin Corasa332c462018-01-31 06:52:17 -0800671 /*
672 * Find the first free segment to allocate the fifos in
673 */
Florin Coras75ccf7b2020-03-05 19:44:02 +0000674
675 segment_manager_segment_reader_lock (sm);
676
677 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100678 pool_foreach (cur, sm->segments) {
Florin Coras75ccf7b2020-03-05 19:44:02 +0000679 free_bytes = fifo_segment_available_bytes (cur);
680 if (free_bytes > max_free_bytes)
681 {
682 max_free_bytes = free_bytes;
683 fs = cur;
684 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100685 }
Florin Coras75ccf7b2020-03-05 19:44:02 +0000686 /* *INDENT-ON* */
Florin Corasa5464812017-04-19 13:00:05 -0700687
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000688 if (fs)
689 {
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000690 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
691 props->rx_fifo_size,
692 props->tx_fifo_size,
693 rx_fifo, tx_fifo);
694 /* On success, keep lock until fifos are initialized */
695 if (!alloc_fail)
696 goto alloc_success;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000697 }
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000698
Florin Coras75ccf7b2020-03-05 19:44:02 +0000699 segment_manager_segment_reader_unlock (sm);
Florin Corasa332c462018-01-31 06:52:17 -0800700
701alloc_check:
702
703 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700704 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700705
Florin Corasa332c462018-01-31 06:52:17 -0800706 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700707
Florin Corasa332c462018-01-31 06:52:17 -0800708 ASSERT (rx_fifo && tx_fifo);
709 sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700710 fs_index = segment_manager_segment_index (sm, fs);
Florin Corasa332c462018-01-31 06:52:17 -0800711 (*tx_fifo)->segment_manager = sm_index;
712 (*rx_fifo)->segment_manager = sm_index;
Florin Coras88001c62019-04-24 14:44:46 -0700713 (*tx_fifo)->segment_index = fs_index;
714 (*rx_fifo)->segment_index = fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700715
Florin Corasa332c462018-01-31 06:52:17 -0800716 if (added_a_segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800717 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800718 app_worker_t *app_wrk;
Florin Coras88001c62019-04-24 14:44:46 -0700719 fs_handle = segment_manager_segment_handle (sm, fs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800720 app_wrk = app_worker_get (sm->app_wrk_index);
Florin Coras88001c62019-04-24 14:44:46 -0700721 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
Florin Corasfa76a762018-11-29 12:40:10 -0800722 }
Florin Corasa332c462018-01-31 06:52:17 -0800723 /* Drop the lock after app is notified */
724 segment_manager_segment_reader_unlock (sm);
725 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700726 }
727
Florin Corasa332c462018-01-31 06:52:17 -0800728 /*
729 * Allocation failed, see if we can add a new segment
730 */
731 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700732 {
Florin Corasa332c462018-01-31 06:52:17 -0800733 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700734 {
Florin Corasa332c462018-01-31 06:52:17 -0800735 clib_warning ("Added a segment, still can't allocate a fifo");
736 segment_manager_segment_reader_unlock (sm);
Florin Coras00e01d32019-10-21 16:07:46 -0700737 return SESSION_E_SEG_NO_SPACE2;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700738 }
Florin Corasa332c462018-01-31 06:52:17 -0800739 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700740 {
Florin Corasa332c462018-01-31 06:52:17 -0800741 clib_warning ("Failed to add new segment");
Florin Coras00e01d32019-10-21 16:07:46 -0700742 return SESSION_E_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700743 }
Florin Coras88001c62019-04-24 14:44:46 -0700744 fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Coras62ddc032019-12-08 18:30:42 -0800745 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
746 props->rx_fifo_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800747 props->tx_fifo_size,
748 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800749 added_a_segment = 1;
750 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700751 }
Florin Corasa332c462018-01-31 06:52:17 -0800752 else
753 {
Florin Coras47cb2482020-07-13 08:52:53 -0700754 SESSION_DBG ("Can't add new seg and no space to allocate fifos!");
Florin Coras00e01d32019-10-21 16:07:46 -0700755 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800756 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700757}
758
759void
Florin Coras19223e02019-03-03 14:56:05 -0800760segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700761{
Florin Corasa332c462018-01-31 06:52:17 -0800762 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700763 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800764 u32 segment_index;
Florin Corasa5464812017-04-19 13:00:05 -0700765
Florin Coras58a93e82019-01-14 23:33:46 -0800766 if (!rx_fifo || !tx_fifo)
767 return;
768
Florin Corasa5464812017-04-19 13:00:05 -0700769 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700770 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800771 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700772 return;
773
Florin Coras19223e02019-03-03 14:56:05 -0800774 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700775 fs = segment_manager_get_segment_w_lock (sm, segment_index);
776 fifo_segment_free_fifo (fs, rx_fifo);
777 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700778
Florin Corasc87c91d2017-08-16 19:55:49 -0700779 /*
780 * Try to remove svm segment if it has no fifos. This can be done only if
781 * the segment is not the first in the segment manager or if it is first
782 * and it is not protected. Moreover, if the segment is first and the app
783 * has detached from the segment manager, remove the segment manager.
784 */
Florin Coras88001c62019-04-24 14:44:46 -0700785 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700786 {
Florin Corasa332c462018-01-31 06:52:17 -0800787 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700788
789 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800790 if (segment_index != 0 || !sm->first_is_protected)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700791 sm_lock_and_del_segment_inline (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700792
793 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400794 if (segment_manager_app_detached (sm)
795 && !segment_manager_has_fifos (sm))
Florin Coras506aa3c2020-12-13 21:09:59 -0800796 segment_manager_free_safe (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700797 }
Florin Corasa332c462018-01-31 06:52:17 -0800798 else
799 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700800}
801
Florin Coras6d7552c2020-04-09 01:49:45 +0000802void
Florin Coras0bc78d82021-01-09 14:34:01 -0800803segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f)
Florin Coras6d7552c2020-04-09 01:49:45 +0000804{
805 fifo_segment_t *fs;
806
Florin Coras0bc78d82021-01-09 14:34:01 -0800807 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000808 fifo_segment_detach_fifo (fs, f);
809 segment_manager_segment_reader_unlock (sm);
810}
811
812void
Florin Coras0bc78d82021-01-09 14:34:01 -0800813segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
814 session_t *s)
Florin Coras6d7552c2020-04-09 01:49:45 +0000815{
816 fifo_segment_t *fs;
817
Florin Coras0bc78d82021-01-09 14:34:01 -0800818 fs = segment_manager_get_segment_w_lock (sm, (*f)->segment_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000819 fifo_segment_attach_fifo (fs, f, s->thread_index);
820 segment_manager_segment_reader_unlock (sm);
821
Florin Coras0bc78d82021-01-09 14:34:01 -0800822 (*f)->shr->master_session_index = s->session_index;
823 (*f)->master_thread_index = s->thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +0000824}
825
Florin Coras3c2fed52018-07-04 04:15:05 -0700826u32
827segment_manager_evt_q_expected_size (u32 q_len)
828{
829 u32 fifo_evt_size, notif_q_size, q_hdrs;
830 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
831
Florin Coras52207f12018-07-12 14:48:06 -0700832 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700833 notif_q_size = clib_max (16, q_len >> 4);
834
835 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
836 fifo_evt_ring_sz = q_len * fifo_evt_size;
837 session_ntf_ring_sz = notif_q_size * 256;
838 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
839
840 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
841}
842
Florin Corasa5464812017-04-19 13:00:05 -0700843/**
844 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800845 *
846 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700847 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700848svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700849segment_manager_alloc_queue (fifo_segment_t * segment,
850 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700851{
Florin Coras3c2fed52018-07-04 04:15:05 -0700852 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
853 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
854 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700855
Florin Coras52207f12018-07-12 14:48:06 -0700856 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700857 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700858 /* *INDENT-OFF* */
859 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700860 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700861 {notif_q_size, session_evt_size, 0}
862 };
863 /* *INDENT-ON* */
864 cfg->consumer_pid = 0;
865 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700866 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700867 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700868
Florin Corasb4624182020-12-11 13:58:12 -0800869 q = fifo_segment_msg_q_alloc (segment, 0, cfg);
Florin Coras99368312018-08-02 10:45:44 -0700870
871 if (props->use_mq_eventfd)
872 {
873 if (svm_msg_q_alloc_producer_eventfd (q))
874 clib_warning ("failed to alloc eventfd");
875 }
Florin Corasa5464812017-04-19 13:00:05 -0700876 return q;
877}
878
Florin Coras88001c62019-04-24 14:44:46 -0700879svm_msg_q_t *
880segment_manager_event_queue (segment_manager_t * sm)
881{
882 return sm->event_queue;
883}
884
Florin Corasa5464812017-04-19 13:00:05 -0700885/**
886 * Frees shm queue allocated in the first segment
887 */
888void
Florin Corase86a8ed2018-01-05 03:20:25 -0800889segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700890{
Florin Coras88001c62019-04-24 14:44:46 -0700891 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800892 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700893 void *oldheap;
894
Florin Corasa332c462018-01-31 06:52:17 -0800895 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700896
Florin Corasa332c462018-01-31 06:52:17 -0800897 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700898 sh = segment->ssvm.sh;
899
900 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800901 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700902 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800903 segment_manager_segment_reader_unlock (sm);
904}
905
906/*
907 * Init segment vm address allocator
908 */
909void
Florin Coras9a45bd82020-12-28 16:28:07 -0800910segment_manager_main_init (void)
Florin Corasa332c462018-01-31 06:52:17 -0800911{
Florin Coras88001c62019-04-24 14:44:46 -0700912 segment_manager_main_t *sm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -0700913
914 sm->default_fifo_size = 1 << 12;
915 sm->default_segment_size = 1 << 20;
916 sm->default_app_mq_size = 128;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000917 sm->default_max_fifo_size = 4 << 20;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000918 sm->default_high_watermark = 80;
919 sm->default_low_watermark = 50;
Florin Corasa5464812017-04-19 13:00:05 -0700920}
921
Florin Corasc87c91d2017-08-16 19:55:49 -0700922static clib_error_t *
923segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
924 vlib_cli_command_t * cmd)
925{
Florin Coras88001c62019-04-24 14:44:46 -0700926 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -0800927 u8 show_segments = 0, verbose = 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000928 uword max_fifo_size;
Florin Coras5368bb02019-06-09 09:24:33 -0700929 segment_manager_t *sm;
930 fifo_segment_t *seg;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000931 app_worker_t *app_wrk;
932 application_t *app;
933 u8 custom_logic;
Dave Barach91f3e742017-09-01 19:12:11 -0400934
Florin Corasc87c91d2017-08-16 19:55:49 -0700935 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
936 {
937 if (unformat (input, "segments"))
938 show_segments = 1;
939 else if (unformat (input, "verbose"))
940 verbose = 1;
941 else
942 return clib_error_return (0, "unknown input `%U'",
943 format_unformat_error, input);
944 }
945 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800946 pool_elts (smm->segment_managers));
947 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700948 {
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000949 vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s",
950 "Index", "AppIndex", "Segments", "MaxFifoSize",
951 "HighWater", "LowWater", "FifoTuning");
Florin Corasc87c91d2017-08-16 19:55:49 -0700952
953 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100954 pool_foreach (sm, smm->segment_managers) {
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000955 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
956 app = app_wrk ? application_get (app_wrk->app_index) : 0;
957 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000958 max_fifo_size = sm->max_fifo_size;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000959
960 vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s",
961 segment_manager_index (sm),
962 sm->app_wrk_index, pool_elts (sm->segments),
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000963 format_memory_size, max_fifo_size,
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000964 sm->high_watermark, sm->low_watermark,
965 custom_logic ? "custom" : "none");
Damjan Marionb2c31b62020-12-13 21:47:40 +0100966 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700967 /* *INDENT-ON* */
968
Florin Coras2a7c0b62020-09-28 23:40:28 -0700969 vlib_cli_output (vm, "\n");
Florin Corasc87c91d2017-08-16 19:55:49 -0700970 }
971 if (show_segments)
972 {
Florin Coras5368bb02019-06-09 09:24:33 -0700973 vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700974
975 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100976 pool_foreach (sm, smm->segment_managers) {
Florin Corasa332c462018-01-31 06:52:17 -0800977 segment_manager_foreach_segment_w_lock (seg, sm, ({
Florin Coras5368bb02019-06-09 09:24:33 -0700978 vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
Florin Corasa332c462018-01-31 06:52:17 -0800979 }));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100980 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700981 /* *INDENT-ON* */
982
983 }
984 return 0;
985}
986
Florin Corasad0c77f2017-11-09 18:00:15 -0800987/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700988VLIB_CLI_COMMAND (segment_manager_show_command, static) =
989{
990 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400991 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700992 .function = segment_manager_show_fn,
993};
994/* *INDENT-ON* */
995
Florin Coras88001c62019-04-24 14:44:46 -0700996void
997segment_manager_format_sessions (segment_manager_t * sm, int verbose)
998{
Florin Coras88001c62019-04-24 14:44:46 -0700999 vlib_main_t *vm = vlib_get_main ();
1000 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -08001001 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -07001002 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -08001003 int slice_index;
1004 u8 *s = 0, *str;
1005 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -07001006
1007 if (!sm)
1008 {
1009 if (verbose)
1010 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1011 "API Client", "SegManager");
1012 else
1013 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1014 return;
1015 }
1016
1017 app_wrk = app_worker_get (sm->app_wrk_index);
1018 app_name = application_name_from_index (app_wrk->app_index);
1019
1020 clib_rwlock_reader_lock (&sm->segments_rwlock);
1021
1022 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001023 pool_foreach (fs, sm->segments) {
Florin Coras62ddc032019-12-08 18:30:42 -08001024 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001025 {
Florin Coras62ddc032019-12-08 18:30:42 -08001026 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1027 while (f)
1028 {
1029 u32 session_index, thread_index;
1030 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -07001031
Florin Corasc547e912020-12-08 17:50:45 -08001032 session_index = f->shr->master_session_index;
1033 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -07001034
Florin Corasc547e912020-12-08 17:50:45 -08001035 session = session_get (session_index, thread_index);
1036 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001037
Florin Corasc547e912020-12-08 17:50:45 -08001038 if (verbose)
1039 s = format (s, "%-40v%-20v%-15u%-10u", str, app_name,
1040 app_wrk->api_client_index,
1041 app_wrk->connects_seg_manager);
1042 else
1043 s = format (s, "%-40v%-20v", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001044
Florin Corasc547e912020-12-08 17:50:45 -08001045 vlib_cli_output (vm, "%v", s);
1046 vec_reset_length (s);
1047 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -07001048
Florin Corasc547e912020-12-08 17:50:45 -08001049 f = f->next;
1050 }
1051 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -07001052 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001053 }
Florin Coras88001c62019-04-24 14:44:46 -07001054 /* *INDENT-ON* */
1055
1056 clib_rwlock_reader_unlock (&sm->segments_rwlock);
1057}
1058
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001059void
1060segment_manager_set_watermarks (segment_manager_t * sm,
1061 u8 high_watermark, u8 low_watermark)
1062{
Florin Coras8c79a4e2020-02-25 22:28:27 +00001063 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001064 low_watermark <= high_watermark);
1065
1066 sm->high_watermark = high_watermark;
1067 sm->low_watermark = low_watermark;
1068}
1069
Florin Coras6cf30ad2017-04-04 23:08:23 -07001070/*
1071 * fd.io coding-style-patch-verification: ON
1072 *
1073 * Local Variables:
1074 * eval: (c-set-style "gnu")
1075 * End:
1076 */