blob: e00a76164480e04cbc5a9588b88539478d2c8f33 [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 */
23 clib_valloc_main_t va_allocator; /**< Virtual address allocator */
24 u32 seg_name_counter; /**< Counter for segment names */
Florin Corasa332c462018-01-31 06:52:17 -080025
Florin Coras88001c62019-04-24 14:44:46 -070026 /*
27 * Configuration
28 */
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000029 u32 default_fifo_size; /**< default rx/tx fifo size */
30 u32 default_segment_size; /**< default fifo segment size */
31 u32 default_app_mq_size; /**< default app msg q size */
32 u32 default_max_fifo_size; /**< default max fifo size */
33 u8 default_high_watermark; /**< default high watermark % */
34 u8 default_low_watermark; /**< default low watermark % */
Florin Coras88001c62019-04-24 14:44:46 -070035} segment_manager_main_t;
Florin Corasa5464812017-04-19 13:00:05 -070036
Florin Coras88001c62019-04-24 14:44:46 -070037static segment_manager_main_t sm_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -070038
Florin Coras88001c62019-04-24 14:44:46 -070039#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
40do { \
41 clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
42 pool_foreach((VAR), ((SM)->segments), (BODY)); \
43 clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
44} while (0)
45
46static segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -080047segment_manager_properties_get (segment_manager_t * sm)
Florin Corasad0c77f2017-11-09 18:00:15 -080048{
Florin Corasab2f6db2018-08-31 14:31:41 -070049 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
50 return application_get_segment_manager_properties (app_wrk->app_index);
Florin Corasa332c462018-01-31 06:52:17 -080051}
52
Florin Coras88001c62019-04-24 14:44:46 -070053segment_manager_props_t *
54segment_manager_props_init (segment_manager_props_t * props)
Florin Corasa332c462018-01-31 06:52:17 -080055{
Florin Coras88001c62019-04-24 14:44:46 -070056 props->add_segment_size = sm_main.default_segment_size;
57 props->rx_fifo_size = sm_main.default_fifo_size;
58 props->tx_fifo_size = sm_main.default_fifo_size;
59 props->evt_q_size = sm_main.default_app_mq_size;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000060 props->max_fifo_size = sm_main.default_max_fifo_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000061 props->high_watermark = sm_main.default_high_watermark;
62 props->low_watermark = sm_main.default_low_watermark;
Florin Coras62ddc032019-12-08 18:30:42 -080063 props->n_slices = vlib_num_workers () + 1;
Florin Corasad0c77f2017-11-09 18:00:15 -080064 return props;
65}
66
Florin Corasbf395972020-04-30 15:05:24 +000067u8
Florin Coras9d063042017-09-14 03:08:00 -040068segment_manager_app_detached (segment_manager_t * sm)
69{
Florin Corasc8e812f2020-05-14 05:32:18 +000070 return (sm->flags & SEG_MANAGER_F_DETACHED);
Dave Wallace7b749fe2017-07-05 14:30:46 -040071}
72
Florin Coras4e4531e2017-11-06 23:27:56 -080073void
74segment_manager_app_detach (segment_manager_t * sm)
75{
Florin Corasc8e812f2020-05-14 05:32:18 +000076 sm->flags |= SEG_MANAGER_F_DETACHED;
Florin Coras4e4531e2017-11-06 23:27:56 -080077}
78
Florin Corasa332c462018-01-31 06:52:17 -080079always_inline u32
Florin Coras88001c62019-04-24 14:44:46 -070080segment_manager_segment_index (segment_manager_t * sm, fifo_segment_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070081{
Florin Corasa332c462018-01-31 06:52:17 -080082 return (seg - sm->segments);
83}
84
85/**
Florin Coras88001c62019-04-24 14:44:46 -070086 * Adds segment to segment manager's pool
87 *
88 * If needed a writer's lock is acquired before allocating a new segment
89 * to avoid affecting any of the segments pool readers.
90 */
91int
Florin Corasef4f3e72019-12-11 14:27:53 -080092segment_manager_add_segment (segment_manager_t * sm, uword segment_size)
Florin Coras88001c62019-04-24 14:44:46 -070093{
Florin Corasef4f3e72019-12-11 14:27:53 -080094 uword baseva = (uword) ~ 0ULL, alloc_size, page_size;
95 u32 rnd_margin = 128 << 10, fs_index = ~0;
Florin Coras88001c62019-04-24 14:44:46 -070096 segment_manager_main_t *smm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -070097 segment_manager_props_t *props;
98 fifo_segment_t *fs;
99 u8 *seg_name;
100 int rv;
101
102 props = segment_manager_properties_get (sm);
103
104 /* Not configured for addition of new segments and not first */
105 if (!props->add_segment && !segment_size)
106 {
107 clib_warning ("cannot allocate new segment");
108 return VNET_API_ERROR_INVALID_VALUE;
109 }
110
111 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700112 * Allocate fifo segment and grab lock if needed
Florin Coras88001c62019-04-24 14:44:46 -0700113 */
114 if (vlib_num_workers ())
115 clib_rwlock_writer_lock (&sm->segments_rwlock);
116
117 pool_get_zero (sm->segments, fs);
118
119 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700120 * Allocate ssvm segment
Florin Coras88001c62019-04-24 14:44:46 -0700121 */
122 segment_size = segment_size ? segment_size : props->add_segment_size;
123 page_size = clib_mem_get_page_size ();
Florin Coras404b8a32019-05-09 12:08:06 -0700124 /* Protect against segment size u32 wrap */
125 segment_size = clib_max (segment_size + page_size - 1, segment_size);
126 segment_size = segment_size & ~(page_size - 1);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700127
Florin Coras88001c62019-04-24 14:44:46 -0700128 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
129 {
130 seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
131 alloc_size = (uword) segment_size + rnd_margin;
132 baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
133 if (!baseva)
134 {
135 clib_warning ("out of space for segments");
136 pool_put (sm->segments, fs);
137 goto done;
138 }
139 }
140 else
Florin Coras2a7c0b62020-09-28 23:40:28 -0700141 {
142 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
143 application_t *app = application_get (app_wrk->app_index);
144 seg_name = format (0, "%v segment%c", app->name, 0);
145 }
Florin Coras88001c62019-04-24 14:44:46 -0700146
147 fs->ssvm.ssvm_size = segment_size;
148 fs->ssvm.name = seg_name;
Florin Coras2a7c0b62020-09-28 23:40:28 -0700149 /* clib_mem_vm_map_shared consumes first page before requested_va */
150 fs->ssvm.requested_va = baseva + page_size;
Florin Coras88001c62019-04-24 14:44:46 -0700151
Florin Coras5220a262020-09-29 18:11:24 -0700152 if ((rv = ssvm_server_init (&fs->ssvm, props->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700153 {
154 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
155 segment_size);
156
157 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
158 clib_valloc_free (&smm->va_allocator, baseva);
159 pool_put (sm->segments, fs);
160 goto done;
161 }
162
Florin Corasf9d4ab42019-05-11 16:55:53 -0700163 /*
164 * Initialize fifo segment
165 */
Florin Coras62ddc032019-12-08 18:30:42 -0800166 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700167 fifo_segment_init (fs);
168
169 /*
170 * Save segment index before dropping lock, if any held
171 */
172 fs_index = fs - sm->segments;
173
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000174 /*
175 * Set watermarks in segment
176 */
177 fs->h->high_watermark = sm->high_watermark;
178 fs->h->low_watermark = sm->low_watermark;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000179 fs->h->pct_first_alloc = props->pct_first_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000180 fs->h->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
181
Florin Coras88001c62019-04-24 14:44:46 -0700182done:
183
184 if (vlib_num_workers ())
185 clib_rwlock_writer_unlock (&sm->segments_rwlock);
186
187 return fs_index;
188}
189
190/**
Florin Corasa332c462018-01-31 06:52:17 -0800191 * Remove segment without lock
192 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800193void
Florin Coras88001c62019-04-24 14:44:46 -0700194segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800195{
Florin Coras88001c62019-04-24 14:44:46 -0700196 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800197
198 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800199 {
Florin Coras2a7c0b62020-09-28 23:40:28 -0700200 /* clib_mem_vm_map_shared consumes first page before requested_va */
201 clib_valloc_free (&smm->va_allocator,
202 fs->ssvm.requested_va - clib_mem_get_page_size ());
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800203
Florin Corasea7e7082020-07-07 17:57:28 -0700204 if (!segment_manager_app_detached (sm))
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800205 {
206 app_worker_t *app_wrk;
207 u64 segment_handle;
208 app_wrk = app_worker_get (sm->app_wrk_index);
209 segment_handle = segment_manager_segment_handle (sm, fs);
210 app_worker_del_segment_notify (app_wrk, segment_handle);
211 }
212 }
Florin Corasa332c462018-01-31 06:52:17 -0800213
214 ssvm_delete (&fs->ssvm);
215
216 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400217 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800218 pool_put (sm->segments, fs);
219}
220
Florin Coras57660d92020-04-04 22:45:34 +0000221static fifo_segment_t *
222segment_manager_get_segment_if_valid (segment_manager_t * sm,
223 u32 segment_index)
224{
225 if (pool_is_free_index (sm->segments, segment_index))
226 return 0;
227 return pool_elt_at_index (sm->segments, segment_index);
228}
229
Florin Corasa332c462018-01-31 06:52:17 -0800230/**
231 * Removes segment after acquiring writer lock
232 */
Florin Coras99368312018-08-02 10:45:44 -0700233static inline void
Florin Corasa332c462018-01-31 06:52:17 -0800234segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
235{
Florin Coras88001c62019-04-24 14:44:46 -0700236 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800237 u8 is_prealloc;
238
239 clib_rwlock_writer_lock (&sm->segments_rwlock);
Florin Coras57660d92020-04-04 22:45:34 +0000240
241 fs = segment_manager_get_segment_if_valid (sm, fs_index);
242 if (!fs)
243 goto done;
244
Florin Coras88001c62019-04-24 14:44:46 -0700245 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800246 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras57660d92020-04-04 22:45:34 +0000247 goto done;
Florin Corasa332c462018-01-31 06:52:17 -0800248
249 segment_manager_del_segment (sm, fs);
Florin Coras57660d92020-04-04 22:45:34 +0000250
251done:
Florin Corasa332c462018-01-31 06:52:17 -0800252 clib_rwlock_writer_unlock (&sm->segments_rwlock);
253}
254
255/**
256 * Reads a segment from the segment manager's pool without lock
257 */
Florin Coras88001c62019-04-24 14:44:46 -0700258fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800259segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
260{
261 return pool_elt_at_index (sm->segments, segment_index);
262}
263
Florin Corasfa76a762018-11-29 12:40:10 -0800264u64
265segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700266 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800267{
268 u32 segment_index = segment_manager_segment_index (sm, segment);
269 return (((u64) segment_manager_index (sm) << 32) | segment_index);
270}
271
Florin Coras88001c62019-04-24 14:44:46 -0700272u64
273segment_manager_make_segment_handle (u32 segment_manager_index,
274 u32 segment_index)
275{
276 return (((u64) segment_manager_index << 32) | segment_index);
277}
278
279fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800280segment_manager_get_segment_w_handle (u64 segment_handle)
281{
282 u32 sm_index, segment_index;
283 segment_manager_t *sm;
284
285 segment_manager_parse_segment_handle (segment_handle, &sm_index,
286 &segment_index);
287 sm = segment_manager_get (sm_index);
288 if (!sm || pool_is_free_index (sm->segments, segment_index))
289 return 0;
290 return pool_elt_at_index (sm->segments, segment_index);
291}
292
Florin Corasa332c462018-01-31 06:52:17 -0800293/**
294 * Reads a segment from the segment manager's pool and acquires reader lock
295 *
296 * Caller must drop the reader's lock by calling
297 * @ref segment_manager_segment_reader_unlock once it finishes working with
298 * the segment.
299 */
Florin Coras88001c62019-04-24 14:44:46 -0700300fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800301segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
302{
303 clib_rwlock_reader_lock (&sm->segments_rwlock);
304 return pool_elt_at_index (sm->segments, segment_index);
305}
306
307void
Florin Coras75ccf7b2020-03-05 19:44:02 +0000308segment_manager_segment_reader_lock (segment_manager_t * sm)
309{
310 clib_rwlock_reader_lock (&sm->segments_rwlock);
311}
312
313void
Florin Corasa332c462018-01-31 06:52:17 -0800314segment_manager_segment_reader_unlock (segment_manager_t * sm)
315{
316 clib_rwlock_reader_unlock (&sm->segments_rwlock);
317}
318
319void
320segment_manager_segment_writer_unlock (segment_manager_t * sm)
321{
322 clib_rwlock_writer_unlock (&sm->segments_rwlock);
323}
324
Florin Corasa332c462018-01-31 06:52:17 -0800325segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700326segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800327{
Florin Coras88001c62019-04-24 14:44:46 -0700328 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800329 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700330
331 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800332 clib_rwlock_init (&sm->segments_rwlock);
333 return sm;
334}
335
Florin Corasa332c462018-01-31 06:52:17 -0800336int
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000337segment_manager_init (segment_manager_t * sm)
Florin Corasa332c462018-01-31 06:52:17 -0800338{
Florin Coras88001c62019-04-24 14:44:46 -0700339 segment_manager_props_t *props;
Florin Corasa332c462018-01-31 06:52:17 -0800340
341 props = segment_manager_properties_get (sm);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000342
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000343 sm->max_fifo_size = props->max_fifo_size ?
344 props->max_fifo_size : sm_main.default_max_fifo_size;
345 sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
346
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000347 segment_manager_set_watermarks (sm,
348 props->high_watermark,
349 props->low_watermark);
Florin Corasa107f402020-09-29 19:18:46 -0700350 return 0;
351}
352
353/**
354 * Initializes segment manager based on options provided.
355 * Returns error if ssvm segment(s) allocation fails.
356 */
357int
358segment_manager_init_first (segment_manager_t * sm)
359{
360 segment_manager_props_t *props;
361 uword first_seg_size;
362 fifo_segment_t *fs;
363 int fs_index, i;
364
365 segment_manager_init (sm);
366 props = segment_manager_properties_get (sm);
367 first_seg_size = clib_max (props->segment_size,
368 sm_main.default_segment_size);
Florin Corasa332c462018-01-31 06:52:17 -0800369
Florin Coras9845c202020-04-28 01:54:22 +0000370 if (props->prealloc_fifos)
Florin Corasa332c462018-01-31 06:52:17 -0800371 {
Florin Coras9845c202020-04-28 01:54:22 +0000372 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
373 u32 rx_rounded_data_size, tx_rounded_data_size;
374 u32 prealloc_fifo_pairs = props->prealloc_fifos;
375 u32 rx_fifo_size, tx_fifo_size, pair_size;
376 u32 approx_segment_count;
377
Florin Corasa332c462018-01-31 06:52:17 -0800378 /* Figure out how many segments should be preallocated */
379 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
380 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
381
382 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
383 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
384 pair_size = rx_fifo_size + tx_fifo_size;
385
386 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
387 if (first_seg_size > approx_total_size)
388 max_seg_size = first_seg_size;
389 approx_segment_count = (approx_total_size + (max_seg_size - 1))
390 / max_seg_size;
391
392 /* Allocate the segments */
393 for (i = 0; i < approx_segment_count + 1; i++)
394 {
Florin Coras9845c202020-04-28 01:54:22 +0000395 fs_index = segment_manager_add_segment (sm, max_seg_size);
396 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800397 {
398 clib_warning ("Failed to preallocate segment %d", i);
Florin Coras9845c202020-04-28 01:54:22 +0000399 return fs_index;
Florin Corasa332c462018-01-31 06:52:17 -0800400 }
401
Florin Coras9845c202020-04-28 01:54:22 +0000402 fs = segment_manager_get_segment (sm, fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800403 if (i == 0)
Florin Coras9845c202020-04-28 01:54:22 +0000404 sm->event_queue = segment_manager_alloc_queue (fs, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800405
Florin Coras9845c202020-04-28 01:54:22 +0000406 fifo_segment_preallocate_fifo_pairs (fs,
Florin Coras88001c62019-04-24 14:44:46 -0700407 props->rx_fifo_size,
408 props->tx_fifo_size,
409 &prealloc_fifo_pairs);
Florin Coras9845c202020-04-28 01:54:22 +0000410 fifo_segment_flags (fs) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800411 if (prealloc_fifo_pairs == 0)
412 break;
413 }
Florin Coras9845c202020-04-28 01:54:22 +0000414 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800415 }
Florin Coras9845c202020-04-28 01:54:22 +0000416
417 fs_index = segment_manager_add_segment (sm, first_seg_size);
418 if (fs_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800419 {
Florin Coras9845c202020-04-28 01:54:22 +0000420 clib_warning ("Failed to allocate segment");
421 return fs_index;
422 }
423
424 fs = segment_manager_get_segment (sm, fs_index);
425 sm->event_queue = segment_manager_alloc_queue (fs, props);
426
427 if (props->prealloc_fifo_hdrs)
428 {
429 u32 hdrs_per_slice;
430
431 /* Do not preallocate on slice associated to main thread */
432 i = (vlib_num_workers ()? 1 : 0);
433 hdrs_per_slice = props->prealloc_fifo_hdrs / (fs->n_slices - i);
434
435 for (; i < fs->n_slices; i++)
Florin Corasa332c462018-01-31 06:52:17 -0800436 {
Florin Coras9845c202020-04-28 01:54:22 +0000437 if (fifo_segment_prealloc_fifo_hdrs (fs, i, hdrs_per_slice))
438 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
Florin Corasa332c462018-01-31 06:52:17 -0800439 }
Florin Corasa332c462018-01-31 06:52:17 -0800440 }
441
442 return 0;
443}
444
Florin Corasc8e812f2020-05-14 05:32:18 +0000445void
446segment_manager_cleanup_detached_listener (segment_manager_t * sm)
447{
448 app_worker_t *app_wrk;
449
450 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
451 if (!app_wrk)
452 return;
453
454 app_worker_del_detached_sm (app_wrk, segment_manager_index (sm));
455}
456
Florin Corasc87c91d2017-08-16 19:55:49 -0700457/**
Florin Coras88001c62019-04-24 14:44:46 -0700458 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 */
460void
Florin Coras88001c62019-04-24 14:44:46 -0700461segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700462{
Florin Coras88001c62019-04-24 14:44:46 -0700463 segment_manager_main_t *smm = &sm_main;
464 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400465
Florin Coras9d063042017-09-14 03:08:00 -0400466 ASSERT (!segment_manager_has_fifos (sm)
467 && segment_manager_app_detached (sm));
468
Florin Corasc8e812f2020-05-14 05:32:18 +0000469 if (sm->flags & SEG_MANAGER_F_DETACHED_LISTENER)
470 segment_manager_cleanup_detached_listener (sm);
471
Florin Coras9d063042017-09-14 03:08:00 -0400472 /* If we have empty preallocated segments that haven't been removed, remove
473 * them now. Apart from that, the first segment in the first segment manager
474 * is not removed when all fifos are removed. It can only be removed when
475 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800476 clib_rwlock_writer_lock (&sm->segments_rwlock);
477
478 /* *INDENT-OFF* */
479 pool_foreach (fifo_segment, sm->segments, ({
480 segment_manager_del_segment (sm, fifo_segment);
481 }));
482 /* *INDENT-ON* */
483
484 clib_rwlock_writer_unlock (&sm->segments_rwlock);
485
486 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700487 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400488 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800489 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700490}
491
Florin Corasc87c91d2017-08-16 19:55:49 -0700492void
Florin Coras88001c62019-04-24 14:44:46 -0700493segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700494{
Florin Coras4e4531e2017-11-06 23:27:56 -0800495 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700496 if (segment_manager_has_fifos (sm))
497 segment_manager_del_sessions (sm);
498 else
499 {
Florin Coras9d063042017-09-14 03:08:00 -0400500 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700501 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700502 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700503}
504
Florin Coras88001c62019-04-24 14:44:46 -0700505segment_manager_t *
506segment_manager_get (u32 index)
507{
508 return pool_elt_at_index (sm_main.segment_managers, index);
509}
510
511segment_manager_t *
512segment_manager_get_if_valid (u32 index)
513{
514 if (pool_is_free_index (sm_main.segment_managers, index))
515 return 0;
516 return pool_elt_at_index (sm_main.segment_managers, index);
517}
518
519u32
520segment_manager_index (segment_manager_t * sm)
521{
522 return sm - sm_main.segment_managers;
523}
524
525u8
526segment_manager_has_fifos (segment_manager_t * sm)
527{
528 fifo_segment_t *seg;
529 u8 first = 1;
530
531 /* *INDENT-OFF* */
532 segment_manager_foreach_segment_w_lock (seg, sm, ({
533 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
534 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
535 {
536 clib_warning ("segment %d has no fifos!",
537 segment_manager_segment_index (sm, seg));
538 first = 0;
539 }
540 if (fifo_segment_has_fifos (seg))
541 {
542 segment_manager_segment_reader_unlock (sm);
543 return 1;
544 }
545 }));
546 /* *INDENT-ON* */
547
548 return 0;
549}
550
551/**
552 * Initiate disconnects for all sessions 'owned' by a segment manager
553 */
554void
555segment_manager_del_sessions (segment_manager_t * sm)
556{
Florin Coras88001c62019-04-24 14:44:46 -0700557 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800558 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700559 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800560 int slice_index;
561 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700562
563 ASSERT (pool_elts (sm->segments) != 0);
564
565 /* Across all fifo segments used by the server */
566 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800567 segment_manager_foreach_segment_w_lock (fs, sm, ({
568 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700569 {
Florin Coras62ddc032019-12-08 18:30:42 -0800570 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
571
572 /*
573 * Remove any residual sessions from the session lookup table
574 * Don't bother deleting the individual fifos, we're going to
575 * throw away the fifo segment in a minute.
576 */
577 while (f)
578 {
579 session = session_get_if_valid (f->master_session_index,
580 f->master_thread_index);
581 if (session)
582 vec_add1 (handles, session_handle (session));
583 f = f->next;
584 }
Florin Coras88001c62019-04-24 14:44:46 -0700585 }
586
587 /* Instead of removing the segment, test when cleaning up disconnected
588 * sessions if the segment can be removed.
589 */
590 }));
591 /* *INDENT-ON* */
592
593 vec_foreach (handle, handles)
Florin Coras77ea42b2020-04-14 23:52:12 +0000594 {
595 session = session_get_from_handle (*handle);
596 session_close (session);
597 /* Avoid propagating notifications back to the app */
598 session->app_wrk_index = APP_INVALID_INDEX;
599 }
Florin Coras88001c62019-04-24 14:44:46 -0700600}
601
Florin Corasf8f516a2018-02-08 15:10:09 -0800602int
Florin Coras88001c62019-04-24 14:44:46 -0700603segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment,
Florin Coras62ddc032019-12-08 18:30:42 -0800604 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800605 u32 rx_fifo_size, u32 tx_fifo_size,
606 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800607{
Florin Coras88001c62019-04-24 14:44:46 -0700608 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800609 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
610 rx_fifo_size,
611 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800612
Florin Coras88001c62019-04-24 14:44:46 -0700613 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800614 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
615 tx_fifo_size,
616 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800617
618 if (*rx_fifo == 0)
619 {
620 /* This would be very odd, but handle it... */
621 if (*tx_fifo != 0)
622 {
Florin Coras88001c62019-04-24 14:44:46 -0700623 fifo_segment_free_fifo (fifo_segment, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800624 *tx_fifo = 0;
625 }
626 return -1;
627 }
628 if (*tx_fifo == 0)
629 {
630 if (*rx_fifo != 0)
631 {
Florin Coras88001c62019-04-24 14:44:46 -0700632 fifo_segment_free_fifo (fifo_segment, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800633 *rx_fifo = 0;
634 }
635 return -1;
636 }
637
638 return 0;
639}
640
Florin Coras6cf30ad2017-04-04 23:08:23 -0700641int
642segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800643 u32 thread_index,
Florin Corasb384b542018-01-15 01:08:33 -0800644 svm_fifo_t ** rx_fifo,
Florin Coras1219b2d2019-04-23 15:53:43 -0700645 svm_fifo_t ** tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700646{
Florin Corasa332c462018-01-31 06:52:17 -0800647 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000648 uword free_bytes, max_free_bytes = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700649 segment_manager_props_t *props;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000650 fifo_segment_t *fs = 0, *cur;
Florin Coras88001c62019-04-24 14:44:46 -0700651 u32 sm_index, fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700652 u8 added_a_segment = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700653 u64 fs_handle;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700654
Florin Corasa332c462018-01-31 06:52:17 -0800655 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700656
Florin Corasa332c462018-01-31 06:52:17 -0800657 /*
658 * Find the first free segment to allocate the fifos in
659 */
Florin Coras75ccf7b2020-03-05 19:44:02 +0000660
661 segment_manager_segment_reader_lock (sm);
662
663 /* *INDENT-OFF* */
664 pool_foreach (cur, sm->segments, ({
665 free_bytes = fifo_segment_available_bytes (cur);
666 if (free_bytes > max_free_bytes)
667 {
668 max_free_bytes = free_bytes;
669 fs = cur;
670 }
671 }));
672 /* *INDENT-ON* */
Florin Corasa5464812017-04-19 13:00:05 -0700673
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000674 if (fs)
675 {
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000676 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
677 props->rx_fifo_size,
678 props->tx_fifo_size,
679 rx_fifo, tx_fifo);
680 /* On success, keep lock until fifos are initialized */
681 if (!alloc_fail)
682 goto alloc_success;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000683 }
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000684
Florin Coras75ccf7b2020-03-05 19:44:02 +0000685 segment_manager_segment_reader_unlock (sm);
Florin Corasa332c462018-01-31 06:52:17 -0800686
687alloc_check:
688
689 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700690 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700691
Florin Corasa332c462018-01-31 06:52:17 -0800692 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700693
Florin Corasa332c462018-01-31 06:52:17 -0800694 ASSERT (rx_fifo && tx_fifo);
695 sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700696 fs_index = segment_manager_segment_index (sm, fs);
Florin Corasa332c462018-01-31 06:52:17 -0800697 (*tx_fifo)->segment_manager = sm_index;
698 (*rx_fifo)->segment_manager = sm_index;
Florin Coras88001c62019-04-24 14:44:46 -0700699 (*tx_fifo)->segment_index = fs_index;
700 (*rx_fifo)->segment_index = fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700701
Florin Corasa332c462018-01-31 06:52:17 -0800702 if (added_a_segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800703 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800704 app_worker_t *app_wrk;
Florin Coras88001c62019-04-24 14:44:46 -0700705 fs_handle = segment_manager_segment_handle (sm, fs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800706 app_wrk = app_worker_get (sm->app_wrk_index);
Florin Coras88001c62019-04-24 14:44:46 -0700707 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
Florin Corasfa76a762018-11-29 12:40:10 -0800708 }
Florin Corasa332c462018-01-31 06:52:17 -0800709 /* Drop the lock after app is notified */
710 segment_manager_segment_reader_unlock (sm);
711 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700712 }
713
Florin Corasa332c462018-01-31 06:52:17 -0800714 /*
715 * Allocation failed, see if we can add a new segment
716 */
717 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700718 {
Florin Corasa332c462018-01-31 06:52:17 -0800719 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700720 {
Florin Corasa332c462018-01-31 06:52:17 -0800721 clib_warning ("Added a segment, still can't allocate a fifo");
722 segment_manager_segment_reader_unlock (sm);
Florin Coras00e01d32019-10-21 16:07:46 -0700723 return SESSION_E_SEG_NO_SPACE2;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700724 }
Florin Corasa332c462018-01-31 06:52:17 -0800725 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700726 {
Florin Corasa332c462018-01-31 06:52:17 -0800727 clib_warning ("Failed to add new segment");
Florin Coras00e01d32019-10-21 16:07:46 -0700728 return SESSION_E_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700729 }
Florin Coras88001c62019-04-24 14:44:46 -0700730 fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Coras62ddc032019-12-08 18:30:42 -0800731 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
732 props->rx_fifo_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800733 props->tx_fifo_size,
734 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800735 added_a_segment = 1;
736 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700737 }
Florin Corasa332c462018-01-31 06:52:17 -0800738 else
739 {
Florin Coras47cb2482020-07-13 08:52:53 -0700740 SESSION_DBG ("Can't add new seg and no space to allocate fifos!");
Florin Coras00e01d32019-10-21 16:07:46 -0700741 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800742 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700743}
744
745void
Florin Coras19223e02019-03-03 14:56:05 -0800746segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700747{
Florin Corasa332c462018-01-31 06:52:17 -0800748 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700749 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800750 u32 segment_index;
Florin Corasa5464812017-04-19 13:00:05 -0700751
Florin Coras58a93e82019-01-14 23:33:46 -0800752 if (!rx_fifo || !tx_fifo)
753 return;
754
Florin Corasa5464812017-04-19 13:00:05 -0700755 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700756 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800757 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700758 return;
759
Florin Coras19223e02019-03-03 14:56:05 -0800760 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700761 fs = segment_manager_get_segment_w_lock (sm, segment_index);
762 fifo_segment_free_fifo (fs, rx_fifo);
763 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700764
Florin Corasc87c91d2017-08-16 19:55:49 -0700765 /*
766 * Try to remove svm segment if it has no fifos. This can be done only if
767 * the segment is not the first in the segment manager or if it is first
768 * and it is not protected. Moreover, if the segment is first and the app
769 * has detached from the segment manager, remove the segment manager.
770 */
Florin Coras88001c62019-04-24 14:44:46 -0700771 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700772 {
Florin Corasa332c462018-01-31 06:52:17 -0800773 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700774
775 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800776 if (segment_index != 0 || !sm->first_is_protected)
777 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700778
779 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400780 if (segment_manager_app_detached (sm)
781 && !segment_manager_has_fifos (sm))
Florin Coras568ebc72018-09-18 16:12:50 -0700782 {
Florin Coras88001c62019-04-24 14:44:46 -0700783 segment_manager_free (sm);
Florin Coras568ebc72018-09-18 16:12:50 -0700784 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700785 }
Florin Corasa332c462018-01-31 06:52:17 -0800786 else
787 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700788}
789
Florin Coras6d7552c2020-04-09 01:49:45 +0000790void
791segment_manager_detach_fifo (segment_manager_t * sm, svm_fifo_t * f)
792{
793 fifo_segment_t *fs;
794
795 fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
796 fifo_segment_detach_fifo (fs, f);
797 segment_manager_segment_reader_unlock (sm);
798}
799
800void
801segment_manager_attach_fifo (segment_manager_t * sm, svm_fifo_t * f,
802 session_t * s)
803{
804 fifo_segment_t *fs;
805
806 fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
807 fifo_segment_attach_fifo (fs, f, s->thread_index);
808 segment_manager_segment_reader_unlock (sm);
809
810 f->master_session_index = s->session_index;
811 f->master_thread_index = s->thread_index;
812}
813
Florin Coras3c2fed52018-07-04 04:15:05 -0700814u32
815segment_manager_evt_q_expected_size (u32 q_len)
816{
817 u32 fifo_evt_size, notif_q_size, q_hdrs;
818 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
819
Florin Coras52207f12018-07-12 14:48:06 -0700820 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700821 notif_q_size = clib_max (16, q_len >> 4);
822
823 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
824 fifo_evt_ring_sz = q_len * fifo_evt_size;
825 session_ntf_ring_sz = notif_q_size * 256;
826 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
827
828 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
829}
830
Florin Corasa5464812017-04-19 13:00:05 -0700831/**
832 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800833 *
834 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700835 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700836svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700837segment_manager_alloc_queue (fifo_segment_t * segment,
838 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700839{
Florin Coras3c2fed52018-07-04 04:15:05 -0700840 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
841 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
842 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700843 void *oldheap;
844
Florin Coras52207f12018-07-12 14:48:06 -0700845 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700846 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700847 /* *INDENT-OFF* */
848 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700849 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700850 {notif_q_size, session_evt_size, 0}
851 };
852 /* *INDENT-ON* */
853 cfg->consumer_pid = 0;
854 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700855 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700856 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700857
Florin Coras3c2fed52018-07-04 04:15:05 -0700858 oldheap = ssvm_push_heap (segment->ssvm.sh);
859 q = svm_msg_q_alloc (cfg);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700860 fifo_segment_update_free_bytes (segment);
Florin Corasa5464812017-04-19 13:00:05 -0700861 ssvm_pop_heap (oldheap);
Florin Coras99368312018-08-02 10:45:44 -0700862
863 if (props->use_mq_eventfd)
864 {
865 if (svm_msg_q_alloc_producer_eventfd (q))
866 clib_warning ("failed to alloc eventfd");
867 }
Florin Corasa5464812017-04-19 13:00:05 -0700868 return q;
869}
870
Florin Coras88001c62019-04-24 14:44:46 -0700871svm_msg_q_t *
872segment_manager_event_queue (segment_manager_t * sm)
873{
874 return sm->event_queue;
875}
876
Florin Corasa5464812017-04-19 13:00:05 -0700877/**
878 * Frees shm queue allocated in the first segment
879 */
880void
Florin Corase86a8ed2018-01-05 03:20:25 -0800881segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700882{
Florin Coras88001c62019-04-24 14:44:46 -0700883 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800884 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700885 void *oldheap;
886
Florin Corasa332c462018-01-31 06:52:17 -0800887 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700888
Florin Corasa332c462018-01-31 06:52:17 -0800889 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700890 sh = segment->ssvm.sh;
891
892 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800893 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700894 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800895 segment_manager_segment_reader_unlock (sm);
896}
897
898/*
899 * Init segment vm address allocator
900 */
901void
902segment_manager_main_init (segment_manager_main_init_args_t * a)
903{
Florin Coras88001c62019-04-24 14:44:46 -0700904 segment_manager_main_t *sm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800905 clib_valloc_chunk_t _ip, *ip = &_ip;
906
907 ip->baseva = a->baseva;
908 ip->size = a->size;
909
910 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Coras88001c62019-04-24 14:44:46 -0700911
912 sm->default_fifo_size = 1 << 12;
913 sm->default_segment_size = 1 << 20;
914 sm->default_app_mq_size = 128;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000915 sm->default_max_fifo_size = 4 << 20;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000916 sm->default_high_watermark = 80;
917 sm->default_low_watermark = 50;
Florin Corasa5464812017-04-19 13:00:05 -0700918}
919
Florin Corasc87c91d2017-08-16 19:55:49 -0700920static clib_error_t *
921segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
922 vlib_cli_command_t * cmd)
923{
Florin Coras88001c62019-04-24 14:44:46 -0700924 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -0800925 u8 show_segments = 0, verbose = 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000926 uword max_fifo_size;
Florin Coras5368bb02019-06-09 09:24:33 -0700927 segment_manager_t *sm;
928 fifo_segment_t *seg;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000929 app_worker_t *app_wrk;
930 application_t *app;
931 u8 custom_logic;
Dave Barach91f3e742017-09-01 19:12:11 -0400932
Florin Corasc87c91d2017-08-16 19:55:49 -0700933 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
934 {
935 if (unformat (input, "segments"))
936 show_segments = 1;
937 else if (unformat (input, "verbose"))
938 verbose = 1;
939 else
940 return clib_error_return (0, "unknown input `%U'",
941 format_unformat_error, input);
942 }
943 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800944 pool_elts (smm->segment_managers));
945 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700946 {
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000947 vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s",
948 "Index", "AppIndex", "Segments", "MaxFifoSize",
949 "HighWater", "LowWater", "FifoTuning");
Florin Corasc87c91d2017-08-16 19:55:49 -0700950
951 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800952 pool_foreach (sm, smm->segment_managers, ({
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000953 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
954 app = app_wrk ? application_get (app_wrk->app_index) : 0;
955 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000956 max_fifo_size = sm->max_fifo_size;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000957
958 vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s",
959 segment_manager_index (sm),
960 sm->app_wrk_index, pool_elts (sm->segments),
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000961 format_memory_size, max_fifo_size,
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000962 sm->high_watermark, sm->low_watermark,
963 custom_logic ? "custom" : "none");
Florin Corasc87c91d2017-08-16 19:55:49 -0700964 }));
965 /* *INDENT-ON* */
966
Florin Coras2a7c0b62020-09-28 23:40:28 -0700967 vlib_cli_output (vm, "\n");
Florin Corasc87c91d2017-08-16 19:55:49 -0700968 }
969 if (show_segments)
970 {
Florin Coras5368bb02019-06-09 09:24:33 -0700971 vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700972
973 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800974 pool_foreach (sm, smm->segment_managers, ({
975 segment_manager_foreach_segment_w_lock (seg, sm, ({
Florin Coras5368bb02019-06-09 09:24:33 -0700976 vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
Florin Corasa332c462018-01-31 06:52:17 -0800977 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700978 }));
979 /* *INDENT-ON* */
980
981 }
982 return 0;
983}
984
Florin Corasad0c77f2017-11-09 18:00:15 -0800985/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700986VLIB_CLI_COMMAND (segment_manager_show_command, static) =
987{
988 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400989 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700990 .function = segment_manager_show_fn,
991};
992/* *INDENT-ON* */
993
Florin Coras88001c62019-04-24 14:44:46 -0700994void
995segment_manager_format_sessions (segment_manager_t * sm, int verbose)
996{
Florin Coras88001c62019-04-24 14:44:46 -0700997 vlib_main_t *vm = vlib_get_main ();
998 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -0800999 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -07001000 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -08001001 int slice_index;
1002 u8 *s = 0, *str;
1003 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -07001004
1005 if (!sm)
1006 {
1007 if (verbose)
1008 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
1009 "API Client", "SegManager");
1010 else
1011 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
1012 return;
1013 }
1014
1015 app_wrk = app_worker_get (sm->app_wrk_index);
1016 app_name = application_name_from_index (app_wrk->app_index);
1017
1018 clib_rwlock_reader_lock (&sm->segments_rwlock);
1019
1020 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -08001021 pool_foreach (fs, sm->segments, ({
1022 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001023 {
Florin Coras62ddc032019-12-08 18:30:42 -08001024 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
1025 while (f)
1026 {
1027 u32 session_index, thread_index;
1028 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -07001029
Florin Coras62ddc032019-12-08 18:30:42 -08001030 session_index = f->master_session_index;
1031 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -07001032
Florin Coras62ddc032019-12-08 18:30:42 -08001033 session = session_get (session_index, thread_index);
1034 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -07001035
Florin Coras62ddc032019-12-08 18:30:42 -08001036 if (verbose)
jiangxiaoming6b410e62020-10-10 15:23:54 +08001037 s = format (s, "%-40v%-20v%-15u%-10u", str, app_name,
Florin Coras62ddc032019-12-08 18:30:42 -08001038 app_wrk->api_client_index, app_wrk->connects_seg_manager);
1039 else
jiangxiaoming6b410e62020-10-10 15:23:54 +08001040 s = format (s, "%-40v%-20v", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001041
Florin Coras62ddc032019-12-08 18:30:42 -08001042 vlib_cli_output (vm, "%v", s);
1043 vec_reset_length (s);
1044 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -07001045
Florin Coras62ddc032019-12-08 18:30:42 -08001046 f = f->next;
1047 }
1048 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -07001049 }
Florin Coras88001c62019-04-24 14:44:46 -07001050 }));
1051 /* *INDENT-ON* */
1052
1053 clib_rwlock_reader_unlock (&sm->segments_rwlock);
1054}
1055
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001056void
1057segment_manager_set_watermarks (segment_manager_t * sm,
1058 u8 high_watermark, u8 low_watermark)
1059{
Florin Coras8c79a4e2020-02-25 22:28:27 +00001060 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001061 low_watermark <= high_watermark);
1062
1063 sm->high_watermark = high_watermark;
1064 sm->low_watermark = low_watermark;
1065}
1066
Florin Coras6cf30ad2017-04-04 23:08:23 -07001067/*
1068 * fd.io coding-style-patch-verification: ON
1069 *
1070 * Local Variables:
1071 * eval: (c-set-style "gnu")
1072 * End:
1073 */