blob: 716f2a39a4e62a6688d85ae42c9dd6c2ff1ebf14 [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 Coras9d063042017-09-14 03:08:00 -040067static u8
68segment_manager_app_detached (segment_manager_t * sm)
69{
Florin Coras15531972018-08-12 23:50:53 -070070 return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
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 Coras15531972018-08-12 23:50:53 -070076 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
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 Coras5368bb02019-06-09 09:24:33 -0700141 seg_name = format (0, "%s%c", "process-private", 0);
Florin Coras88001c62019-04-24 14:44:46 -0700142
143 fs->ssvm.ssvm_size = segment_size;
144 fs->ssvm.name = seg_name;
145 fs->ssvm.requested_va = baseva;
146
147 if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type)))
148 {
149 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
150 segment_size);
151
152 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
153 clib_valloc_free (&smm->va_allocator, baseva);
154 pool_put (sm->segments, fs);
155 goto done;
156 }
157
Florin Corasf9d4ab42019-05-11 16:55:53 -0700158 /*
159 * Initialize fifo segment
160 */
Florin Coras62ddc032019-12-08 18:30:42 -0800161 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700162 fifo_segment_init (fs);
163
164 /*
165 * Save segment index before dropping lock, if any held
166 */
167 fs_index = fs - sm->segments;
168
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000169 /*
170 * Set watermarks in segment
171 */
172 fs->h->high_watermark = sm->high_watermark;
173 fs->h->low_watermark = sm->low_watermark;
Florin Coras2de9c0f2020-02-02 19:30:39 +0000174 fs->h->pct_first_alloc = props->pct_first_alloc;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000175 fs->h->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
176
Florin Coras88001c62019-04-24 14:44:46 -0700177done:
178
179 if (vlib_num_workers ())
180 clib_rwlock_writer_unlock (&sm->segments_rwlock);
181
182 return fs_index;
183}
184
185/**
Florin Corasa332c462018-01-31 06:52:17 -0800186 * Remove segment without lock
187 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800188void
Florin Coras88001c62019-04-24 14:44:46 -0700189segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800190{
Florin Coras88001c62019-04-24 14:44:46 -0700191 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800192
193 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800194 {
195 clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va);
196
197 if (sm->app_wrk_index != SEGMENT_MANAGER_INVALID_APP_INDEX)
198 {
199 app_worker_t *app_wrk;
200 u64 segment_handle;
201 app_wrk = app_worker_get (sm->app_wrk_index);
202 segment_handle = segment_manager_segment_handle (sm, fs);
203 app_worker_del_segment_notify (app_wrk, segment_handle);
204 }
205 }
Florin Corasa332c462018-01-31 06:52:17 -0800206
207 ssvm_delete (&fs->ssvm);
208
209 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400210 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800211 pool_put (sm->segments, fs);
212}
213
Florin Coras57660d92020-04-04 22:45:34 +0000214static fifo_segment_t *
215segment_manager_get_segment_if_valid (segment_manager_t * sm,
216 u32 segment_index)
217{
218 if (pool_is_free_index (sm->segments, segment_index))
219 return 0;
220 return pool_elt_at_index (sm->segments, segment_index);
221}
222
Florin Corasa332c462018-01-31 06:52:17 -0800223/**
224 * Removes segment after acquiring writer lock
225 */
Florin Coras99368312018-08-02 10:45:44 -0700226static inline void
Florin Corasa332c462018-01-31 06:52:17 -0800227segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
228{
Florin Coras88001c62019-04-24 14:44:46 -0700229 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800230 u8 is_prealloc;
231
232 clib_rwlock_writer_lock (&sm->segments_rwlock);
Florin Coras57660d92020-04-04 22:45:34 +0000233
234 fs = segment_manager_get_segment_if_valid (sm, fs_index);
235 if (!fs)
236 goto done;
237
Florin Coras88001c62019-04-24 14:44:46 -0700238 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800239 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras57660d92020-04-04 22:45:34 +0000240 goto done;
Florin Corasa332c462018-01-31 06:52:17 -0800241
242 segment_manager_del_segment (sm, fs);
Florin Coras57660d92020-04-04 22:45:34 +0000243
244done:
Florin Corasa332c462018-01-31 06:52:17 -0800245 clib_rwlock_writer_unlock (&sm->segments_rwlock);
246}
247
248/**
249 * Reads a segment from the segment manager's pool without lock
250 */
Florin Coras88001c62019-04-24 14:44:46 -0700251fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800252segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
253{
254 return pool_elt_at_index (sm->segments, segment_index);
255}
256
Florin Corasfa76a762018-11-29 12:40:10 -0800257u64
258segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700259 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800260{
261 u32 segment_index = segment_manager_segment_index (sm, segment);
262 return (((u64) segment_manager_index (sm) << 32) | segment_index);
263}
264
Florin Coras88001c62019-04-24 14:44:46 -0700265static void
Florin Corasfa76a762018-11-29 12:40:10 -0800266segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index,
267 u32 * segment_index)
268{
269 *sm_index = segment_handle >> 32;
270 *segment_index = segment_handle & 0xFFFFFFFF;
271}
272
Florin Coras88001c62019-04-24 14:44:46 -0700273u64
274segment_manager_make_segment_handle (u32 segment_manager_index,
275 u32 segment_index)
276{
277 return (((u64) segment_manager_index << 32) | segment_index);
278}
279
280fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800281segment_manager_get_segment_w_handle (u64 segment_handle)
282{
283 u32 sm_index, segment_index;
284 segment_manager_t *sm;
285
286 segment_manager_parse_segment_handle (segment_handle, &sm_index,
287 &segment_index);
288 sm = segment_manager_get (sm_index);
289 if (!sm || pool_is_free_index (sm->segments, segment_index))
290 return 0;
291 return pool_elt_at_index (sm->segments, segment_index);
292}
293
Florin Corasa332c462018-01-31 06:52:17 -0800294/**
295 * Reads a segment from the segment manager's pool and acquires reader lock
296 *
297 * Caller must drop the reader's lock by calling
298 * @ref segment_manager_segment_reader_unlock once it finishes working with
299 * the segment.
300 */
Florin Coras88001c62019-04-24 14:44:46 -0700301fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800302segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
303{
304 clib_rwlock_reader_lock (&sm->segments_rwlock);
305 return pool_elt_at_index (sm->segments, segment_index);
306}
307
308void
Florin Coras75ccf7b2020-03-05 19:44:02 +0000309segment_manager_segment_reader_lock (segment_manager_t * sm)
310{
311 clib_rwlock_reader_lock (&sm->segments_rwlock);
312}
313
314void
Florin Corasa332c462018-01-31 06:52:17 -0800315segment_manager_segment_reader_unlock (segment_manager_t * sm)
316{
317 clib_rwlock_reader_unlock (&sm->segments_rwlock);
318}
319
320void
321segment_manager_segment_writer_unlock (segment_manager_t * sm)
322{
323 clib_rwlock_writer_unlock (&sm->segments_rwlock);
324}
325
Florin Corasa332c462018-01-31 06:52:17 -0800326segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700327segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800328{
Florin Coras88001c62019-04-24 14:44:46 -0700329 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800330 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700331
332 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800333 clib_rwlock_init (&sm->segments_rwlock);
334 return sm;
335}
336
337/**
338 * Initializes segment manager based on options provided.
339 * Returns error if ssvm segment(s) allocation fails.
340 */
341int
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000342segment_manager_init (segment_manager_t * sm)
Florin Corasa332c462018-01-31 06:52:17 -0800343{
344 u32 rx_fifo_size, tx_fifo_size, pair_size;
345 u32 rx_rounded_data_size, tx_rounded_data_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000346 uword first_seg_size;
347 u32 prealloc_fifo_pairs;
Florin Coras86f04502018-09-12 16:08:01 -0700348 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700349 segment_manager_props_t *props;
350 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800351 u32 approx_segment_count;
352 int seg_index, i;
353
354 props = segment_manager_properties_get (sm);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000355 first_seg_size = clib_max (props->segment_size,
356 sm_main.default_segment_size);
357 prealloc_fifo_pairs = props->prealloc_fifos;
358
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000359 sm->max_fifo_size = props->max_fifo_size ?
360 props->max_fifo_size : sm_main.default_max_fifo_size;
361 sm->max_fifo_size = clib_max (sm->max_fifo_size, 4096);
362
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000363 segment_manager_set_watermarks (sm,
364 props->high_watermark,
365 props->low_watermark);
Florin Corasa332c462018-01-31 06:52:17 -0800366
367 if (prealloc_fifo_pairs)
368 {
369 /* Figure out how many segments should be preallocated */
370 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
371 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
372
373 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
374 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
375 pair_size = rx_fifo_size + tx_fifo_size;
376
377 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
378 if (first_seg_size > approx_total_size)
379 max_seg_size = first_seg_size;
380 approx_segment_count = (approx_total_size + (max_seg_size - 1))
381 / max_seg_size;
382
383 /* Allocate the segments */
384 for (i = 0; i < approx_segment_count + 1; i++)
385 {
386 seg_index = segment_manager_add_segment (sm, max_seg_size);
387 if (seg_index < 0)
388 {
389 clib_warning ("Failed to preallocate segment %d", i);
390 return seg_index;
391 }
392
Florin Corasa332c462018-01-31 06:52:17 -0800393 segment = segment_manager_get_segment (sm, seg_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800394 if (i == 0)
Florin Coras99368312018-08-02 10:45:44 -0700395 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800396
Florin Coras88001c62019-04-24 14:44:46 -0700397 fifo_segment_preallocate_fifo_pairs (segment,
398 props->rx_fifo_size,
399 props->tx_fifo_size,
400 &prealloc_fifo_pairs);
401 fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800402 if (prealloc_fifo_pairs == 0)
403 break;
404 }
405 }
406 else
407 {
408 seg_index = segment_manager_add_segment (sm, first_seg_size);
Florin Coras402377e2019-04-17 10:23:23 -0700409 if (seg_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800410 {
411 clib_warning ("Failed to allocate segment");
412 return seg_index;
413 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800414 segment = segment_manager_get_segment (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -0700415 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasa332c462018-01-31 06:52:17 -0800416 }
417
418 return 0;
419}
420
Florin Corasc87c91d2017-08-16 19:55:49 -0700421/**
Florin Coras88001c62019-04-24 14:44:46 -0700422 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700423 */
424void
Florin Coras88001c62019-04-24 14:44:46 -0700425segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700426{
Florin Coras88001c62019-04-24 14:44:46 -0700427 segment_manager_main_t *smm = &sm_main;
428 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400429
Florin Coras9d063042017-09-14 03:08:00 -0400430 ASSERT (!segment_manager_has_fifos (sm)
431 && segment_manager_app_detached (sm));
432
433 /* If we have empty preallocated segments that haven't been removed, remove
434 * them now. Apart from that, the first segment in the first segment manager
435 * is not removed when all fifos are removed. It can only be removed when
436 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800437 clib_rwlock_writer_lock (&sm->segments_rwlock);
438
439 /* *INDENT-OFF* */
440 pool_foreach (fifo_segment, sm->segments, ({
441 segment_manager_del_segment (sm, fifo_segment);
442 }));
443 /* *INDENT-ON* */
444
445 clib_rwlock_writer_unlock (&sm->segments_rwlock);
446
447 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700448 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400449 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800450 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700451}
452
Florin Corasc87c91d2017-08-16 19:55:49 -0700453void
Florin Coras88001c62019-04-24 14:44:46 -0700454segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700455{
Florin Coras4e4531e2017-11-06 23:27:56 -0800456 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700457 if (segment_manager_has_fifos (sm))
458 segment_manager_del_sessions (sm);
459 else
460 {
Florin Coras9d063042017-09-14 03:08:00 -0400461 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700462 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700463 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700464}
465
Florin Coras88001c62019-04-24 14:44:46 -0700466segment_manager_t *
467segment_manager_get (u32 index)
468{
469 return pool_elt_at_index (sm_main.segment_managers, index);
470}
471
472segment_manager_t *
473segment_manager_get_if_valid (u32 index)
474{
475 if (pool_is_free_index (sm_main.segment_managers, index))
476 return 0;
477 return pool_elt_at_index (sm_main.segment_managers, index);
478}
479
480u32
481segment_manager_index (segment_manager_t * sm)
482{
483 return sm - sm_main.segment_managers;
484}
485
486u8
487segment_manager_has_fifos (segment_manager_t * sm)
488{
489 fifo_segment_t *seg;
490 u8 first = 1;
491
492 /* *INDENT-OFF* */
493 segment_manager_foreach_segment_w_lock (seg, sm, ({
494 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
495 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
496 {
497 clib_warning ("segment %d has no fifos!",
498 segment_manager_segment_index (sm, seg));
499 first = 0;
500 }
501 if (fifo_segment_has_fifos (seg))
502 {
503 segment_manager_segment_reader_unlock (sm);
504 return 1;
505 }
506 }));
507 /* *INDENT-ON* */
508
509 return 0;
510}
511
512/**
513 * Initiate disconnects for all sessions 'owned' by a segment manager
514 */
515void
516segment_manager_del_sessions (segment_manager_t * sm)
517{
Florin Coras88001c62019-04-24 14:44:46 -0700518 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800519 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700520 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800521 int slice_index;
522 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700523
524 ASSERT (pool_elts (sm->segments) != 0);
525
526 /* Across all fifo segments used by the server */
527 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800528 segment_manager_foreach_segment_w_lock (fs, sm, ({
529 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700530 {
Florin Coras62ddc032019-12-08 18:30:42 -0800531 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
532
533 /*
534 * Remove any residual sessions from the session lookup table
535 * Don't bother deleting the individual fifos, we're going to
536 * throw away the fifo segment in a minute.
537 */
538 while (f)
539 {
540 session = session_get_if_valid (f->master_session_index,
541 f->master_thread_index);
542 if (session)
543 vec_add1 (handles, session_handle (session));
544 f = f->next;
545 }
Florin Coras88001c62019-04-24 14:44:46 -0700546 }
547
548 /* Instead of removing the segment, test when cleaning up disconnected
549 * sessions if the segment can be removed.
550 */
551 }));
552 /* *INDENT-ON* */
553
554 vec_foreach (handle, handles)
555 session_close (session_get_from_handle (*handle));
556}
557
Florin Corasf8f516a2018-02-08 15:10:09 -0800558int
Florin Coras88001c62019-04-24 14:44:46 -0700559segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment,
Florin Coras62ddc032019-12-08 18:30:42 -0800560 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800561 u32 rx_fifo_size, u32 tx_fifo_size,
562 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800563{
Florin Coras88001c62019-04-24 14:44:46 -0700564 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800565 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
566 rx_fifo_size,
567 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800568
Florin Coras88001c62019-04-24 14:44:46 -0700569 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800570 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
571 tx_fifo_size,
572 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800573
574 if (*rx_fifo == 0)
575 {
576 /* This would be very odd, but handle it... */
577 if (*tx_fifo != 0)
578 {
Florin Coras88001c62019-04-24 14:44:46 -0700579 fifo_segment_free_fifo (fifo_segment, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800580 *tx_fifo = 0;
581 }
582 return -1;
583 }
584 if (*tx_fifo == 0)
585 {
586 if (*rx_fifo != 0)
587 {
Florin Coras88001c62019-04-24 14:44:46 -0700588 fifo_segment_free_fifo (fifo_segment, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800589 *rx_fifo = 0;
590 }
591 return -1;
592 }
593
594 return 0;
595}
596
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597int
598segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800599 u32 thread_index,
Florin Corasb384b542018-01-15 01:08:33 -0800600 svm_fifo_t ** rx_fifo,
Florin Coras1219b2d2019-04-23 15:53:43 -0700601 svm_fifo_t ** tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700602{
Florin Corasa332c462018-01-31 06:52:17 -0800603 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000604 uword free_bytes, max_free_bytes = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700605 segment_manager_props_t *props;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000606 fifo_segment_t *fs = 0, *cur;
Florin Coras88001c62019-04-24 14:44:46 -0700607 u32 sm_index, fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700608 u8 added_a_segment = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700609 u64 fs_handle;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700610
Florin Corasa332c462018-01-31 06:52:17 -0800611 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700612
Florin Corasa332c462018-01-31 06:52:17 -0800613 /*
614 * Find the first free segment to allocate the fifos in
615 */
Florin Coras75ccf7b2020-03-05 19:44:02 +0000616
617 segment_manager_segment_reader_lock (sm);
618
619 /* *INDENT-OFF* */
620 pool_foreach (cur, sm->segments, ({
621 free_bytes = fifo_segment_available_bytes (cur);
622 if (free_bytes > max_free_bytes)
623 {
624 max_free_bytes = free_bytes;
625 fs = cur;
626 }
627 }));
628 /* *INDENT-ON* */
Florin Corasa5464812017-04-19 13:00:05 -0700629
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000630 if (fs)
631 {
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000632 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
633 props->rx_fifo_size,
634 props->tx_fifo_size,
635 rx_fifo, tx_fifo);
636 /* On success, keep lock until fifos are initialized */
637 if (!alloc_fail)
638 goto alloc_success;
Florin Coras75ccf7b2020-03-05 19:44:02 +0000639 }
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000640
Florin Coras75ccf7b2020-03-05 19:44:02 +0000641 segment_manager_segment_reader_unlock (sm);
Florin Corasa332c462018-01-31 06:52:17 -0800642
643alloc_check:
644
645 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700646 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700647
Florin Corasa332c462018-01-31 06:52:17 -0800648 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700649
Florin Corasa332c462018-01-31 06:52:17 -0800650 ASSERT (rx_fifo && tx_fifo);
651 sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700652 fs_index = segment_manager_segment_index (sm, fs);
Florin Corasa332c462018-01-31 06:52:17 -0800653 (*tx_fifo)->segment_manager = sm_index;
654 (*rx_fifo)->segment_manager = sm_index;
Florin Coras88001c62019-04-24 14:44:46 -0700655 (*tx_fifo)->segment_index = fs_index;
656 (*rx_fifo)->segment_index = fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700657
Florin Corasa332c462018-01-31 06:52:17 -0800658 if (added_a_segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800659 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800660 app_worker_t *app_wrk;
Florin Coras88001c62019-04-24 14:44:46 -0700661 fs_handle = segment_manager_segment_handle (sm, fs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800662 app_wrk = app_worker_get (sm->app_wrk_index);
Florin Coras88001c62019-04-24 14:44:46 -0700663 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
Florin Corasfa76a762018-11-29 12:40:10 -0800664 }
Florin Corasa332c462018-01-31 06:52:17 -0800665 /* Drop the lock after app is notified */
666 segment_manager_segment_reader_unlock (sm);
667 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700668 }
669
Florin Corasa332c462018-01-31 06:52:17 -0800670 /*
671 * Allocation failed, see if we can add a new segment
672 */
673 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700674 {
Florin Corasa332c462018-01-31 06:52:17 -0800675 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700676 {
Florin Corasa332c462018-01-31 06:52:17 -0800677 clib_warning ("Added a segment, still can't allocate a fifo");
678 segment_manager_segment_reader_unlock (sm);
Florin Coras00e01d32019-10-21 16:07:46 -0700679 return SESSION_E_SEG_NO_SPACE2;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700680 }
Florin Corasa332c462018-01-31 06:52:17 -0800681 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700682 {
Florin Corasa332c462018-01-31 06:52:17 -0800683 clib_warning ("Failed to add new segment");
Florin Coras00e01d32019-10-21 16:07:46 -0700684 return SESSION_E_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700685 }
Florin Coras88001c62019-04-24 14:44:46 -0700686 fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Coras62ddc032019-12-08 18:30:42 -0800687 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
688 props->rx_fifo_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800689 props->tx_fifo_size,
690 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800691 added_a_segment = 1;
692 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700693 }
Florin Corasa332c462018-01-31 06:52:17 -0800694 else
695 {
696 clib_warning ("Can't add new seg and no space to allocate fifos!");
Florin Coras00e01d32019-10-21 16:07:46 -0700697 return SESSION_E_SEG_NO_SPACE;
Florin Corasa332c462018-01-31 06:52:17 -0800698 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700699}
700
701void
Florin Coras19223e02019-03-03 14:56:05 -0800702segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700703{
Florin Corasa332c462018-01-31 06:52:17 -0800704 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700705 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800706 u32 segment_index;
Florin Corasa5464812017-04-19 13:00:05 -0700707
Florin Coras58a93e82019-01-14 23:33:46 -0800708 if (!rx_fifo || !tx_fifo)
709 return;
710
Florin Corasa5464812017-04-19 13:00:05 -0700711 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700712 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800713 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700714 return;
715
Florin Coras19223e02019-03-03 14:56:05 -0800716 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700717 fs = segment_manager_get_segment_w_lock (sm, segment_index);
718 fifo_segment_free_fifo (fs, rx_fifo);
719 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700720
Florin Corasc87c91d2017-08-16 19:55:49 -0700721 /*
722 * Try to remove svm segment if it has no fifos. This can be done only if
723 * the segment is not the first in the segment manager or if it is first
724 * and it is not protected. Moreover, if the segment is first and the app
725 * has detached from the segment manager, remove the segment manager.
726 */
Florin Coras88001c62019-04-24 14:44:46 -0700727 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700728 {
Florin Corasa332c462018-01-31 06:52:17 -0800729 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700730
731 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800732 if (segment_index != 0 || !sm->first_is_protected)
733 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700734
735 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400736 if (segment_manager_app_detached (sm)
737 && !segment_manager_has_fifos (sm))
Florin Coras568ebc72018-09-18 16:12:50 -0700738 {
Florin Coras88001c62019-04-24 14:44:46 -0700739 segment_manager_free (sm);
Florin Coras568ebc72018-09-18 16:12:50 -0700740 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700741 }
Florin Corasa332c462018-01-31 06:52:17 -0800742 else
743 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700744}
745
Florin Coras3c2fed52018-07-04 04:15:05 -0700746u32
747segment_manager_evt_q_expected_size (u32 q_len)
748{
749 u32 fifo_evt_size, notif_q_size, q_hdrs;
750 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
751
Florin Coras52207f12018-07-12 14:48:06 -0700752 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700753 notif_q_size = clib_max (16, q_len >> 4);
754
755 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
756 fifo_evt_ring_sz = q_len * fifo_evt_size;
757 session_ntf_ring_sz = notif_q_size * 256;
758 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
759
760 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
761}
762
Florin Corasa5464812017-04-19 13:00:05 -0700763/**
764 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800765 *
766 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700767 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700768svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700769segment_manager_alloc_queue (fifo_segment_t * segment,
770 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700771{
Florin Coras3c2fed52018-07-04 04:15:05 -0700772 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
773 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
774 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700775 void *oldheap;
776
Florin Coras52207f12018-07-12 14:48:06 -0700777 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700778 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700779 /* *INDENT-OFF* */
780 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700781 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700782 {notif_q_size, session_evt_size, 0}
783 };
784 /* *INDENT-ON* */
785 cfg->consumer_pid = 0;
786 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700787 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700788 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700789
Florin Coras3c2fed52018-07-04 04:15:05 -0700790 oldheap = ssvm_push_heap (segment->ssvm.sh);
791 q = svm_msg_q_alloc (cfg);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700792 fifo_segment_update_free_bytes (segment);
Florin Corasa5464812017-04-19 13:00:05 -0700793 ssvm_pop_heap (oldheap);
Florin Coras99368312018-08-02 10:45:44 -0700794
795 if (props->use_mq_eventfd)
796 {
797 if (svm_msg_q_alloc_producer_eventfd (q))
798 clib_warning ("failed to alloc eventfd");
799 }
Florin Corasa5464812017-04-19 13:00:05 -0700800 return q;
801}
802
Florin Coras88001c62019-04-24 14:44:46 -0700803svm_msg_q_t *
804segment_manager_event_queue (segment_manager_t * sm)
805{
806 return sm->event_queue;
807}
808
Florin Corasa5464812017-04-19 13:00:05 -0700809/**
810 * Frees shm queue allocated in the first segment
811 */
812void
Florin Corase86a8ed2018-01-05 03:20:25 -0800813segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700814{
Florin Coras88001c62019-04-24 14:44:46 -0700815 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800816 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700817 void *oldheap;
818
Florin Corasa332c462018-01-31 06:52:17 -0800819 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700820
Florin Corasa332c462018-01-31 06:52:17 -0800821 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700822 sh = segment->ssvm.sh;
823
824 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800825 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700826 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800827 segment_manager_segment_reader_unlock (sm);
828}
829
830/*
831 * Init segment vm address allocator
832 */
833void
834segment_manager_main_init (segment_manager_main_init_args_t * a)
835{
Florin Coras88001c62019-04-24 14:44:46 -0700836 segment_manager_main_t *sm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800837 clib_valloc_chunk_t _ip, *ip = &_ip;
838
839 ip->baseva = a->baseva;
840 ip->size = a->size;
841
842 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Coras88001c62019-04-24 14:44:46 -0700843
844 sm->default_fifo_size = 1 << 12;
845 sm->default_segment_size = 1 << 20;
846 sm->default_app_mq_size = 128;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000847 sm->default_max_fifo_size = 4 << 20;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000848 sm->default_high_watermark = 80;
849 sm->default_low_watermark = 50;
Florin Corasa5464812017-04-19 13:00:05 -0700850}
851
Florin Corasc87c91d2017-08-16 19:55:49 -0700852static clib_error_t *
853segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
854 vlib_cli_command_t * cmd)
855{
Florin Coras88001c62019-04-24 14:44:46 -0700856 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -0800857 u8 show_segments = 0, verbose = 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000858 uword max_fifo_size;
Florin Coras5368bb02019-06-09 09:24:33 -0700859 segment_manager_t *sm;
860 fifo_segment_t *seg;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000861 app_worker_t *app_wrk;
862 application_t *app;
863 u8 custom_logic;
Dave Barach91f3e742017-09-01 19:12:11 -0400864
Florin Corasc87c91d2017-08-16 19:55:49 -0700865 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
866 {
867 if (unformat (input, "segments"))
868 show_segments = 1;
869 else if (unformat (input, "verbose"))
870 verbose = 1;
871 else
872 return clib_error_return (0, "unknown input `%U'",
873 format_unformat_error, input);
874 }
875 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800876 pool_elts (smm->segment_managers));
877 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700878 {
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000879 vlib_cli_output (vm, "%-6s%=10s%=10s%=13s%=11s%=11s%=12s",
880 "Index", "AppIndex", "Segments", "MaxFifoSize",
881 "HighWater", "LowWater", "FifoTuning");
Florin Corasc87c91d2017-08-16 19:55:49 -0700882
883 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800884 pool_foreach (sm, smm->segment_managers, ({
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000885 app_wrk = app_worker_get_if_valid (sm->app_wrk_index);
886 app = app_wrk ? application_get (app_wrk->app_index) : 0;
887 custom_logic = (app && (app->cb_fns.fifo_tuning_callback)) ? 1 : 0;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000888 max_fifo_size = sm->max_fifo_size;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000889
890 vlib_cli_output (vm, "%-6d%=10d%=10d%=13U%=11d%=11d%=12s",
891 segment_manager_index (sm),
892 sm->app_wrk_index, pool_elts (sm->segments),
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +0000893 format_memory_size, max_fifo_size,
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000894 sm->high_watermark, sm->low_watermark,
895 custom_logic ? "custom" : "none");
Florin Corasc87c91d2017-08-16 19:55:49 -0700896 }));
897 /* *INDENT-ON* */
898
899 }
900 if (show_segments)
901 {
Florin Coras5368bb02019-06-09 09:24:33 -0700902 vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700903
904 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800905 pool_foreach (sm, smm->segment_managers, ({
906 segment_manager_foreach_segment_w_lock (seg, sm, ({
Florin Coras5368bb02019-06-09 09:24:33 -0700907 vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
Florin Corasa332c462018-01-31 06:52:17 -0800908 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700909 }));
910 /* *INDENT-ON* */
911
912 }
913 return 0;
914}
915
Florin Corasad0c77f2017-11-09 18:00:15 -0800916/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700917VLIB_CLI_COMMAND (segment_manager_show_command, static) =
918{
919 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400920 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700921 .function = segment_manager_show_fn,
922};
923/* *INDENT-ON* */
924
Florin Coras88001c62019-04-24 14:44:46 -0700925void
926segment_manager_format_sessions (segment_manager_t * sm, int verbose)
927{
Florin Coras88001c62019-04-24 14:44:46 -0700928 vlib_main_t *vm = vlib_get_main ();
929 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -0800930 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700931 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -0800932 int slice_index;
933 u8 *s = 0, *str;
934 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700935
936 if (!sm)
937 {
938 if (verbose)
939 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
940 "API Client", "SegManager");
941 else
942 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
943 return;
944 }
945
946 app_wrk = app_worker_get (sm->app_wrk_index);
947 app_name = application_name_from_index (app_wrk->app_index);
948
949 clib_rwlock_reader_lock (&sm->segments_rwlock);
950
951 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800952 pool_foreach (fs, sm->segments, ({
953 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700954 {
Florin Coras62ddc032019-12-08 18:30:42 -0800955 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
956 while (f)
957 {
958 u32 session_index, thread_index;
959 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -0700960
Florin Coras62ddc032019-12-08 18:30:42 -0800961 session_index = f->master_session_index;
962 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -0700963
Florin Coras62ddc032019-12-08 18:30:42 -0800964 session = session_get (session_index, thread_index);
965 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -0700966
Florin Coras62ddc032019-12-08 18:30:42 -0800967 if (verbose)
968 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
969 app_wrk->api_client_index, app_wrk->connects_seg_manager);
970 else
971 s = format (s, "%-40s%-20s", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -0700972
Florin Coras62ddc032019-12-08 18:30:42 -0800973 vlib_cli_output (vm, "%v", s);
974 vec_reset_length (s);
975 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -0700976
Florin Coras62ddc032019-12-08 18:30:42 -0800977 f = f->next;
978 }
979 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -0700980 }
Florin Coras88001c62019-04-24 14:44:46 -0700981 }));
982 /* *INDENT-ON* */
983
984 clib_rwlock_reader_unlock (&sm->segments_rwlock);
985}
986
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000987void
988segment_manager_set_watermarks (segment_manager_t * sm,
989 u8 high_watermark, u8 low_watermark)
990{
Florin Coras8c79a4e2020-02-25 22:28:27 +0000991 ASSERT (high_watermark <= 100 && low_watermark <= 100 &&
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000992 low_watermark <= high_watermark);
993
994 sm->high_watermark = high_watermark;
995 sm->low_watermark = low_watermark;
996}
997
Florin Coras6cf30ad2017-04-04 23:08:23 -0700998/*
999 * fd.io coding-style-patch-verification: ON
1000 *
1001 * Local Variables:
1002 * eval: (c-set-style "gnu")
1003 * End:
1004 */