blob: 0600b671be712011904304a6f6d8129a6622ac6a [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 */
29 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} segment_manager_main_t;
Florin Corasa5464812017-04-19 13:00:05 -070033
Florin Coras88001c62019-04-24 14:44:46 -070034static segment_manager_main_t sm_main;
Florin Coras6cf30ad2017-04-04 23:08:23 -070035
Florin Coras88001c62019-04-24 14:44:46 -070036#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
37do { \
38 clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
39 pool_foreach((VAR), ((SM)->segments), (BODY)); \
40 clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
41} while (0)
42
43static segment_manager_props_t *
Florin Corasa332c462018-01-31 06:52:17 -080044segment_manager_properties_get (segment_manager_t * sm)
Florin Corasad0c77f2017-11-09 18:00:15 -080045{
Florin Corasab2f6db2018-08-31 14:31:41 -070046 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
47 return application_get_segment_manager_properties (app_wrk->app_index);
Florin Corasa332c462018-01-31 06:52:17 -080048}
49
Florin Coras88001c62019-04-24 14:44:46 -070050segment_manager_props_t *
51segment_manager_props_init (segment_manager_props_t * props)
Florin Corasa332c462018-01-31 06:52:17 -080052{
Florin Coras88001c62019-04-24 14:44:46 -070053 props->add_segment_size = sm_main.default_segment_size;
54 props->rx_fifo_size = sm_main.default_fifo_size;
55 props->tx_fifo_size = sm_main.default_fifo_size;
56 props->evt_q_size = sm_main.default_app_mq_size;
Florin Coras62ddc032019-12-08 18:30:42 -080057 props->n_slices = vlib_num_workers () + 1;
Florin Corasad0c77f2017-11-09 18:00:15 -080058 return props;
59}
60
Florin Coras9d063042017-09-14 03:08:00 -040061static u8
62segment_manager_app_detached (segment_manager_t * sm)
63{
Florin Coras15531972018-08-12 23:50:53 -070064 return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -040065}
66
Florin Coras4e4531e2017-11-06 23:27:56 -080067void
68segment_manager_app_detach (segment_manager_t * sm)
69{
Florin Coras15531972018-08-12 23:50:53 -070070 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras4e4531e2017-11-06 23:27:56 -080071}
72
Florin Corasa332c462018-01-31 06:52:17 -080073always_inline u32
Florin Coras88001c62019-04-24 14:44:46 -070074segment_manager_segment_index (segment_manager_t * sm, fifo_segment_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070075{
Florin Corasa332c462018-01-31 06:52:17 -080076 return (seg - sm->segments);
77}
78
79/**
Florin Coras88001c62019-04-24 14:44:46 -070080 * Adds segment to segment manager's pool
81 *
82 * If needed a writer's lock is acquired before allocating a new segment
83 * to avoid affecting any of the segments pool readers.
84 */
85int
Florin Corasef4f3e72019-12-11 14:27:53 -080086segment_manager_add_segment (segment_manager_t * sm, uword segment_size)
Florin Coras88001c62019-04-24 14:44:46 -070087{
Florin Corasef4f3e72019-12-11 14:27:53 -080088 uword baseva = (uword) ~ 0ULL, alloc_size, page_size;
89 u32 rnd_margin = 128 << 10, fs_index = ~0;
Florin Coras88001c62019-04-24 14:44:46 -070090 segment_manager_main_t *smm = &sm_main;
Florin Coras88001c62019-04-24 14:44:46 -070091 segment_manager_props_t *props;
92 fifo_segment_t *fs;
93 u8 *seg_name;
94 int rv;
95
96 props = segment_manager_properties_get (sm);
97
98 /* Not configured for addition of new segments and not first */
99 if (!props->add_segment && !segment_size)
100 {
101 clib_warning ("cannot allocate new segment");
102 return VNET_API_ERROR_INVALID_VALUE;
103 }
104
105 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700106 * Allocate fifo segment and grab lock if needed
Florin Coras88001c62019-04-24 14:44:46 -0700107 */
108 if (vlib_num_workers ())
109 clib_rwlock_writer_lock (&sm->segments_rwlock);
110
111 pool_get_zero (sm->segments, fs);
112
113 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700114 * Allocate ssvm segment
Florin Coras88001c62019-04-24 14:44:46 -0700115 */
116 segment_size = segment_size ? segment_size : props->add_segment_size;
117 page_size = clib_mem_get_page_size ();
Florin Coras404b8a32019-05-09 12:08:06 -0700118 /* Protect against segment size u32 wrap */
119 segment_size = clib_max (segment_size + page_size - 1, segment_size);
120 segment_size = segment_size & ~(page_size - 1);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700121
Florin Coras88001c62019-04-24 14:44:46 -0700122 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
123 {
124 seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
125 alloc_size = (uword) segment_size + rnd_margin;
126 baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
127 if (!baseva)
128 {
129 clib_warning ("out of space for segments");
130 pool_put (sm->segments, fs);
131 goto done;
132 }
133 }
134 else
Florin Coras5368bb02019-06-09 09:24:33 -0700135 seg_name = format (0, "%s%c", "process-private", 0);
Florin Coras88001c62019-04-24 14:44:46 -0700136
137 fs->ssvm.ssvm_size = segment_size;
138 fs->ssvm.name = seg_name;
139 fs->ssvm.requested_va = baseva;
140
141 if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type)))
142 {
143 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
144 segment_size);
145
146 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
147 clib_valloc_free (&smm->va_allocator, baseva);
148 pool_put (sm->segments, fs);
149 goto done;
150 }
151
Florin Corasf9d4ab42019-05-11 16:55:53 -0700152 /*
153 * Initialize fifo segment
154 */
Florin Coras62ddc032019-12-08 18:30:42 -0800155 fs->n_slices = props->n_slices;
Florin Coras88001c62019-04-24 14:44:46 -0700156 fifo_segment_init (fs);
157
158 /*
159 * Save segment index before dropping lock, if any held
160 */
161 fs_index = fs - sm->segments;
162
163done:
164
165 if (vlib_num_workers ())
166 clib_rwlock_writer_unlock (&sm->segments_rwlock);
167
168 return fs_index;
169}
170
171/**
Florin Corasa332c462018-01-31 06:52:17 -0800172 * Remove segment without lock
173 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800174void
Florin Coras88001c62019-04-24 14:44:46 -0700175segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800176{
Florin Coras88001c62019-04-24 14:44:46 -0700177 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800178
179 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800180 {
181 clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va);
182
183 if (sm->app_wrk_index != SEGMENT_MANAGER_INVALID_APP_INDEX)
184 {
185 app_worker_t *app_wrk;
186 u64 segment_handle;
187 app_wrk = app_worker_get (sm->app_wrk_index);
188 segment_handle = segment_manager_segment_handle (sm, fs);
189 app_worker_del_segment_notify (app_wrk, segment_handle);
190 }
191 }
Florin Corasa332c462018-01-31 06:52:17 -0800192
193 ssvm_delete (&fs->ssvm);
194
195 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400196 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800197 pool_put (sm->segments, fs);
198}
199
200/**
201 * Removes segment after acquiring writer lock
202 */
Florin Coras99368312018-08-02 10:45:44 -0700203static inline void
Florin Corasa332c462018-01-31 06:52:17 -0800204segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
205{
Florin Coras88001c62019-04-24 14:44:46 -0700206 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800207 u8 is_prealloc;
208
209 clib_rwlock_writer_lock (&sm->segments_rwlock);
210 fs = segment_manager_get_segment (sm, fs_index);
Florin Coras88001c62019-04-24 14:44:46 -0700211 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800212 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras9d063042017-09-14 03:08:00 -0400213 {
Florin Corasa332c462018-01-31 06:52:17 -0800214 clib_rwlock_writer_unlock (&sm->segments_rwlock);
Florin Coras9d063042017-09-14 03:08:00 -0400215 return;
216 }
Florin Corasa332c462018-01-31 06:52:17 -0800217
218 segment_manager_del_segment (sm, fs);
219 clib_rwlock_writer_unlock (&sm->segments_rwlock);
220}
221
222/**
223 * Reads a segment from the segment manager's pool without lock
224 */
Florin Coras88001c62019-04-24 14:44:46 -0700225fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800226segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
227{
228 return pool_elt_at_index (sm->segments, segment_index);
229}
230
Florin Corasfa76a762018-11-29 12:40:10 -0800231u64
232segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700233 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800234{
235 u32 segment_index = segment_manager_segment_index (sm, segment);
236 return (((u64) segment_manager_index (sm) << 32) | segment_index);
237}
238
Florin Coras88001c62019-04-24 14:44:46 -0700239static void
Florin Corasfa76a762018-11-29 12:40:10 -0800240segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index,
241 u32 * segment_index)
242{
243 *sm_index = segment_handle >> 32;
244 *segment_index = segment_handle & 0xFFFFFFFF;
245}
246
Florin Coras88001c62019-04-24 14:44:46 -0700247u64
248segment_manager_make_segment_handle (u32 segment_manager_index,
249 u32 segment_index)
250{
251 return (((u64) segment_manager_index << 32) | segment_index);
252}
253
254fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800255segment_manager_get_segment_w_handle (u64 segment_handle)
256{
257 u32 sm_index, segment_index;
258 segment_manager_t *sm;
259
260 segment_manager_parse_segment_handle (segment_handle, &sm_index,
261 &segment_index);
262 sm = segment_manager_get (sm_index);
263 if (!sm || pool_is_free_index (sm->segments, segment_index))
264 return 0;
265 return pool_elt_at_index (sm->segments, segment_index);
266}
267
Florin Corasa332c462018-01-31 06:52:17 -0800268/**
269 * Reads a segment from the segment manager's pool and acquires reader lock
270 *
271 * Caller must drop the reader's lock by calling
272 * @ref segment_manager_segment_reader_unlock once it finishes working with
273 * the segment.
274 */
Florin Coras88001c62019-04-24 14:44:46 -0700275fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800276segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
277{
278 clib_rwlock_reader_lock (&sm->segments_rwlock);
279 return pool_elt_at_index (sm->segments, segment_index);
280}
281
282void
283segment_manager_segment_reader_unlock (segment_manager_t * sm)
284{
285 clib_rwlock_reader_unlock (&sm->segments_rwlock);
286}
287
288void
289segment_manager_segment_writer_unlock (segment_manager_t * sm)
290{
291 clib_rwlock_writer_unlock (&sm->segments_rwlock);
292}
293
Florin Corasa332c462018-01-31 06:52:17 -0800294segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700295segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800296{
Florin Coras88001c62019-04-24 14:44:46 -0700297 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800298 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700299
300 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800301 clib_rwlock_init (&sm->segments_rwlock);
302 return sm;
303}
304
305/**
306 * Initializes segment manager based on options provided.
307 * Returns error if ssvm segment(s) allocation fails.
308 */
309int
Florin Corasef4f3e72019-12-11 14:27:53 -0800310segment_manager_init (segment_manager_t * sm, uword first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800311 u32 prealloc_fifo_pairs)
Florin Corasa332c462018-01-31 06:52:17 -0800312{
313 u32 rx_fifo_size, tx_fifo_size, pair_size;
314 u32 rx_rounded_data_size, tx_rounded_data_size;
Florin Coras86f04502018-09-12 16:08:01 -0700315 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700316 segment_manager_props_t *props;
317 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800318 u32 approx_segment_count;
319 int seg_index, i;
320
321 props = segment_manager_properties_get (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700322 first_seg_size = clib_max (first_seg_size, sm_main.default_segment_size);
Florin Corasa332c462018-01-31 06:52:17 -0800323
324 if (prealloc_fifo_pairs)
325 {
326 /* Figure out how many segments should be preallocated */
327 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
328 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
329
330 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
331 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
332 pair_size = rx_fifo_size + tx_fifo_size;
333
334 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
335 if (first_seg_size > approx_total_size)
336 max_seg_size = first_seg_size;
337 approx_segment_count = (approx_total_size + (max_seg_size - 1))
338 / max_seg_size;
339
340 /* Allocate the segments */
341 for (i = 0; i < approx_segment_count + 1; i++)
342 {
343 seg_index = segment_manager_add_segment (sm, max_seg_size);
344 if (seg_index < 0)
345 {
346 clib_warning ("Failed to preallocate segment %d", i);
347 return seg_index;
348 }
349
Florin Corasa332c462018-01-31 06:52:17 -0800350 segment = segment_manager_get_segment (sm, seg_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800351 if (i == 0)
Florin Coras99368312018-08-02 10:45:44 -0700352 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800353
Florin Coras88001c62019-04-24 14:44:46 -0700354 fifo_segment_preallocate_fifo_pairs (segment,
355 props->rx_fifo_size,
356 props->tx_fifo_size,
357 &prealloc_fifo_pairs);
358 fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800359 if (prealloc_fifo_pairs == 0)
360 break;
361 }
362 }
363 else
364 {
365 seg_index = segment_manager_add_segment (sm, first_seg_size);
Florin Coras402377e2019-04-17 10:23:23 -0700366 if (seg_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800367 {
368 clib_warning ("Failed to allocate segment");
369 return seg_index;
370 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800371 segment = segment_manager_get_segment (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -0700372 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasa332c462018-01-31 06:52:17 -0800373 }
374
375 return 0;
376}
377
Florin Corasc87c91d2017-08-16 19:55:49 -0700378/**
Florin Coras88001c62019-04-24 14:44:46 -0700379 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380 */
381void
Florin Coras88001c62019-04-24 14:44:46 -0700382segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700383{
Florin Coras88001c62019-04-24 14:44:46 -0700384 segment_manager_main_t *smm = &sm_main;
385 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400386
Florin Coras9d063042017-09-14 03:08:00 -0400387 ASSERT (!segment_manager_has_fifos (sm)
388 && segment_manager_app_detached (sm));
389
390 /* If we have empty preallocated segments that haven't been removed, remove
391 * them now. Apart from that, the first segment in the first segment manager
392 * is not removed when all fifos are removed. It can only be removed when
393 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800394 clib_rwlock_writer_lock (&sm->segments_rwlock);
395
396 /* *INDENT-OFF* */
397 pool_foreach (fifo_segment, sm->segments, ({
398 segment_manager_del_segment (sm, fifo_segment);
399 }));
400 /* *INDENT-ON* */
401
402 clib_rwlock_writer_unlock (&sm->segments_rwlock);
403
404 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700405 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400406 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800407 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700408}
409
Florin Corasc87c91d2017-08-16 19:55:49 -0700410void
Florin Coras88001c62019-04-24 14:44:46 -0700411segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700412{
Florin Coras4e4531e2017-11-06 23:27:56 -0800413 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700414 if (segment_manager_has_fifos (sm))
415 segment_manager_del_sessions (sm);
416 else
417 {
Florin Coras9d063042017-09-14 03:08:00 -0400418 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700419 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700420 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700421}
422
Florin Coras88001c62019-04-24 14:44:46 -0700423segment_manager_t *
424segment_manager_get (u32 index)
425{
426 return pool_elt_at_index (sm_main.segment_managers, index);
427}
428
429segment_manager_t *
430segment_manager_get_if_valid (u32 index)
431{
432 if (pool_is_free_index (sm_main.segment_managers, index))
433 return 0;
434 return pool_elt_at_index (sm_main.segment_managers, index);
435}
436
437u32
438segment_manager_index (segment_manager_t * sm)
439{
440 return sm - sm_main.segment_managers;
441}
442
443u8
444segment_manager_has_fifos (segment_manager_t * sm)
445{
446 fifo_segment_t *seg;
447 u8 first = 1;
448
449 /* *INDENT-OFF* */
450 segment_manager_foreach_segment_w_lock (seg, sm, ({
451 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
452 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
453 {
454 clib_warning ("segment %d has no fifos!",
455 segment_manager_segment_index (sm, seg));
456 first = 0;
457 }
458 if (fifo_segment_has_fifos (seg))
459 {
460 segment_manager_segment_reader_unlock (sm);
461 return 1;
462 }
463 }));
464 /* *INDENT-ON* */
465
466 return 0;
467}
468
469/**
470 * Initiate disconnects for all sessions 'owned' by a segment manager
471 */
472void
473segment_manager_del_sessions (segment_manager_t * sm)
474{
Florin Coras88001c62019-04-24 14:44:46 -0700475 session_handle_t *handles = 0, *handle;
Florin Coras62ddc032019-12-08 18:30:42 -0800476 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700477 session_t *session;
Florin Coras62ddc032019-12-08 18:30:42 -0800478 int slice_index;
479 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700480
481 ASSERT (pool_elts (sm->segments) != 0);
482
483 /* Across all fifo segments used by the server */
484 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800485 segment_manager_foreach_segment_w_lock (fs, sm, ({
486 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700487 {
Florin Coras62ddc032019-12-08 18:30:42 -0800488 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
489
490 /*
491 * Remove any residual sessions from the session lookup table
492 * Don't bother deleting the individual fifos, we're going to
493 * throw away the fifo segment in a minute.
494 */
495 while (f)
496 {
497 session = session_get_if_valid (f->master_session_index,
498 f->master_thread_index);
499 if (session)
500 vec_add1 (handles, session_handle (session));
501 f = f->next;
502 }
Florin Coras88001c62019-04-24 14:44:46 -0700503 }
504
505 /* Instead of removing the segment, test when cleaning up disconnected
506 * sessions if the segment can be removed.
507 */
508 }));
509 /* *INDENT-ON* */
510
511 vec_foreach (handle, handles)
512 session_close (session_get_from_handle (*handle));
513}
514
Florin Corasf8f516a2018-02-08 15:10:09 -0800515int
Florin Coras88001c62019-04-24 14:44:46 -0700516segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment,
Florin Coras62ddc032019-12-08 18:30:42 -0800517 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800518 u32 rx_fifo_size, u32 tx_fifo_size,
519 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800520{
Florin Coras88001c62019-04-24 14:44:46 -0700521 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800522 *rx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
523 rx_fifo_size,
524 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800525
Florin Coras88001c62019-04-24 14:44:46 -0700526 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800527 *tx_fifo = fifo_segment_alloc_fifo_w_slice (fifo_segment, thread_index,
528 tx_fifo_size,
529 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800530
531 if (*rx_fifo == 0)
532 {
533 /* This would be very odd, but handle it... */
534 if (*tx_fifo != 0)
535 {
Florin Coras88001c62019-04-24 14:44:46 -0700536 fifo_segment_free_fifo (fifo_segment, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800537 *tx_fifo = 0;
538 }
539 return -1;
540 }
541 if (*tx_fifo == 0)
542 {
543 if (*rx_fifo != 0)
544 {
Florin Coras88001c62019-04-24 14:44:46 -0700545 fifo_segment_free_fifo (fifo_segment, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800546 *rx_fifo = 0;
547 }
548 return -1;
549 }
550
551 return 0;
552}
553
Florin Coras6cf30ad2017-04-04 23:08:23 -0700554int
555segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800556 u32 thread_index,
Florin Corasb384b542018-01-15 01:08:33 -0800557 svm_fifo_t ** rx_fifo,
Florin Coras1219b2d2019-04-23 15:53:43 -0700558 svm_fifo_t ** tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700559{
Florin Corasa332c462018-01-31 06:52:17 -0800560 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Coras88001c62019-04-24 14:44:46 -0700561 segment_manager_props_t *props;
562 fifo_segment_t *fs = 0;
563 u32 sm_index, fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700564 u8 added_a_segment = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700565 u64 fs_handle;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700566
Florin Corasa332c462018-01-31 06:52:17 -0800567 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700568
Florin Corasa332c462018-01-31 06:52:17 -0800569 /*
570 * Find the first free segment to allocate the fifos in
571 */
Florin Corasa5464812017-04-19 13:00:05 -0700572
Florin Corasa332c462018-01-31 06:52:17 -0800573 /* *INDENT-OFF* */
Florin Coras88001c62019-04-24 14:44:46 -0700574 segment_manager_foreach_segment_w_lock (fs, sm, ({
575 alloc_fail = segment_manager_try_alloc_fifos (fs,
Florin Coras62ddc032019-12-08 18:30:42 -0800576 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800577 props->rx_fifo_size,
578 props->tx_fifo_size,
579 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800580 /* Exit with lock held, drop it after notifying app */
581 if (!alloc_fail)
582 goto alloc_success;
583 }));
584 /* *INDENT-ON* */
585
586alloc_check:
587
588 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700589 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700590
Florin Corasa332c462018-01-31 06:52:17 -0800591 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700592
Florin Corasa332c462018-01-31 06:52:17 -0800593 ASSERT (rx_fifo && tx_fifo);
594 sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700595 fs_index = segment_manager_segment_index (sm, fs);
Florin Corasa332c462018-01-31 06:52:17 -0800596 (*tx_fifo)->segment_manager = sm_index;
597 (*rx_fifo)->segment_manager = sm_index;
Florin Coras88001c62019-04-24 14:44:46 -0700598 (*tx_fifo)->segment_index = fs_index;
599 (*rx_fifo)->segment_index = fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700600
Florin Corasa332c462018-01-31 06:52:17 -0800601 if (added_a_segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800602 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800603 app_worker_t *app_wrk;
Florin Coras88001c62019-04-24 14:44:46 -0700604 fs_handle = segment_manager_segment_handle (sm, fs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800605 app_wrk = app_worker_get (sm->app_wrk_index);
Florin Coras88001c62019-04-24 14:44:46 -0700606 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
Florin Corasfa76a762018-11-29 12:40:10 -0800607 }
Florin Corasa332c462018-01-31 06:52:17 -0800608 /* Drop the lock after app is notified */
609 segment_manager_segment_reader_unlock (sm);
610 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700611 }
612
Florin Corasa332c462018-01-31 06:52:17 -0800613 /*
614 * Allocation failed, see if we can add a new segment
615 */
616 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700617 {
Florin Corasa332c462018-01-31 06:52:17 -0800618 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700619 {
Florin Corasa332c462018-01-31 06:52:17 -0800620 clib_warning ("Added a segment, still can't allocate a fifo");
621 segment_manager_segment_reader_unlock (sm);
622 return SESSION_ERROR_NEW_SEG_NO_SPACE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700623 }
Florin Corasa332c462018-01-31 06:52:17 -0800624 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700625 {
Florin Corasa332c462018-01-31 06:52:17 -0800626 clib_warning ("Failed to add new segment");
627 return SESSION_ERROR_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700628 }
Florin Coras88001c62019-04-24 14:44:46 -0700629 fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Coras62ddc032019-12-08 18:30:42 -0800630 alloc_fail = segment_manager_try_alloc_fifos (fs, thread_index,
631 props->rx_fifo_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800632 props->tx_fifo_size,
633 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800634 added_a_segment = 1;
635 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700636 }
Florin Corasa332c462018-01-31 06:52:17 -0800637 else
638 {
639 clib_warning ("Can't add new seg and no space to allocate fifos!");
640 return SESSION_ERROR_NO_SPACE;
641 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700642}
643
644void
Florin Coras19223e02019-03-03 14:56:05 -0800645segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700646{
Florin Corasa332c462018-01-31 06:52:17 -0800647 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700648 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800649 u32 segment_index;
Florin Corasa5464812017-04-19 13:00:05 -0700650
Florin Coras58a93e82019-01-14 23:33:46 -0800651 if (!rx_fifo || !tx_fifo)
652 return;
653
Florin Corasa5464812017-04-19 13:00:05 -0700654 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700655 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800656 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700657 return;
658
Florin Coras19223e02019-03-03 14:56:05 -0800659 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700660 fs = segment_manager_get_segment_w_lock (sm, segment_index);
661 fifo_segment_free_fifo (fs, rx_fifo);
662 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700663
Florin Corasc87c91d2017-08-16 19:55:49 -0700664 /*
665 * Try to remove svm segment if it has no fifos. This can be done only if
666 * the segment is not the first in the segment manager or if it is first
667 * and it is not protected. Moreover, if the segment is first and the app
668 * has detached from the segment manager, remove the segment manager.
669 */
Florin Coras88001c62019-04-24 14:44:46 -0700670 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700671 {
Florin Corasa332c462018-01-31 06:52:17 -0800672 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700673
674 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800675 if (segment_index != 0 || !sm->first_is_protected)
676 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700677
678 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400679 if (segment_manager_app_detached (sm)
680 && !segment_manager_has_fifos (sm))
Florin Coras568ebc72018-09-18 16:12:50 -0700681 {
Florin Coras88001c62019-04-24 14:44:46 -0700682 segment_manager_free (sm);
Florin Coras568ebc72018-09-18 16:12:50 -0700683 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700684 }
Florin Corasa332c462018-01-31 06:52:17 -0800685 else
686 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700687}
688
Florin Corasb095a3c2019-04-25 12:58:46 -0700689int
690segment_manager_grow_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size)
691{
692 fifo_segment_t *fs;
693 int rv;
694
695 fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
696 rv = fifo_segment_grow_fifo (fs, f, size);
697 segment_manager_segment_reader_unlock (sm);
698
699 return rv;
700}
701
Florin Coras344ce422019-05-03 11:46:55 -0700702int
703segment_manager_collect_fifo_chunks (segment_manager_t * sm, svm_fifo_t * f)
704{
705 fifo_segment_t *fs;
706 int rv;
707
708 fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
709 rv = fifo_segment_collect_fifo_chunks (fs, f);
710 segment_manager_segment_reader_unlock (sm);
711
712 return rv;
713}
714
715int
716segment_manager_shrink_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size,
717 u8 is_producer)
718{
719 int rv;
720
721 rv = svm_fifo_reduce_size (f, size, is_producer);
722
723 /* Nothing to collect at this point */
724 if (!is_producer)
725 return rv;
726
727 if (f->flags & SVM_FIFO_F_COLLECT_CHUNKS)
728 segment_manager_collect_fifo_chunks (sm, f);
729
730 return rv;
731}
732
Florin Coras3c2fed52018-07-04 04:15:05 -0700733u32
734segment_manager_evt_q_expected_size (u32 q_len)
735{
736 u32 fifo_evt_size, notif_q_size, q_hdrs;
737 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
738
Florin Coras52207f12018-07-12 14:48:06 -0700739 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700740 notif_q_size = clib_max (16, q_len >> 4);
741
742 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
743 fifo_evt_ring_sz = q_len * fifo_evt_size;
744 session_ntf_ring_sz = notif_q_size * 256;
745 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
746
747 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
748}
749
Florin Corasa5464812017-04-19 13:00:05 -0700750/**
751 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800752 *
753 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700754 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700755svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700756segment_manager_alloc_queue (fifo_segment_t * segment,
757 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700758{
Florin Coras3c2fed52018-07-04 04:15:05 -0700759 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
760 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
761 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700762 void *oldheap;
763
Florin Coras52207f12018-07-12 14:48:06 -0700764 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700765 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700766 /* *INDENT-OFF* */
767 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700768 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700769 {notif_q_size, session_evt_size, 0}
770 };
771 /* *INDENT-ON* */
772 cfg->consumer_pid = 0;
773 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700774 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700775 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700776
Florin Coras3c2fed52018-07-04 04:15:05 -0700777 oldheap = ssvm_push_heap (segment->ssvm.sh);
778 q = svm_msg_q_alloc (cfg);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700779 fifo_segment_update_free_bytes (segment);
Florin Corasa5464812017-04-19 13:00:05 -0700780 ssvm_pop_heap (oldheap);
Florin Coras99368312018-08-02 10:45:44 -0700781
782 if (props->use_mq_eventfd)
783 {
784 if (svm_msg_q_alloc_producer_eventfd (q))
785 clib_warning ("failed to alloc eventfd");
786 }
Florin Corasa5464812017-04-19 13:00:05 -0700787 return q;
788}
789
Florin Coras88001c62019-04-24 14:44:46 -0700790svm_msg_q_t *
791segment_manager_event_queue (segment_manager_t * sm)
792{
793 return sm->event_queue;
794}
795
Florin Corasa5464812017-04-19 13:00:05 -0700796/**
797 * Frees shm queue allocated in the first segment
798 */
799void
Florin Corase86a8ed2018-01-05 03:20:25 -0800800segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700801{
Florin Coras88001c62019-04-24 14:44:46 -0700802 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800803 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700804 void *oldheap;
805
Florin Corasa332c462018-01-31 06:52:17 -0800806 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700807
Florin Corasa332c462018-01-31 06:52:17 -0800808 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700809 sh = segment->ssvm.sh;
810
811 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800812 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700813 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800814 segment_manager_segment_reader_unlock (sm);
815}
816
817/*
818 * Init segment vm address allocator
819 */
820void
821segment_manager_main_init (segment_manager_main_init_args_t * a)
822{
Florin Coras88001c62019-04-24 14:44:46 -0700823 segment_manager_main_t *sm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800824 clib_valloc_chunk_t _ip, *ip = &_ip;
825
826 ip->baseva = a->baseva;
827 ip->size = a->size;
828
829 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Coras88001c62019-04-24 14:44:46 -0700830
831 sm->default_fifo_size = 1 << 12;
832 sm->default_segment_size = 1 << 20;
833 sm->default_app_mq_size = 128;
Florin Corasa5464812017-04-19 13:00:05 -0700834}
835
Florin Corasc87c91d2017-08-16 19:55:49 -0700836static clib_error_t *
837segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
838 vlib_cli_command_t * cmd)
839{
Florin Coras88001c62019-04-24 14:44:46 -0700840 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -0800841 u8 show_segments = 0, verbose = 0;
Florin Coras5368bb02019-06-09 09:24:33 -0700842 segment_manager_t *sm;
843 fifo_segment_t *seg;
Dave Barach91f3e742017-09-01 19:12:11 -0400844
Florin Corasc87c91d2017-08-16 19:55:49 -0700845 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
846 {
847 if (unformat (input, "segments"))
848 show_segments = 1;
849 else if (unformat (input, "verbose"))
850 verbose = 1;
851 else
852 return clib_error_return (0, "unknown input `%U'",
853 format_unformat_error, input);
854 }
855 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800856 pool_elts (smm->segment_managers));
857 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700858 {
859 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
860 "Segments");
861
862 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800863 pool_foreach (sm, smm->segment_managers, ({
Florin Coras15531972018-08-12 23:50:53 -0700864 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm),
865 sm->app_wrk_index, pool_elts (sm->segments));
Florin Corasc87c91d2017-08-16 19:55:49 -0700866 }));
867 /* *INDENT-ON* */
868
869 }
870 if (show_segments)
871 {
Florin Coras5368bb02019-06-09 09:24:33 -0700872 vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700873
874 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800875 pool_foreach (sm, smm->segment_managers, ({
876 segment_manager_foreach_segment_w_lock (seg, sm, ({
Florin Coras5368bb02019-06-09 09:24:33 -0700877 vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
Florin Corasa332c462018-01-31 06:52:17 -0800878 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700879 }));
880 /* *INDENT-ON* */
881
882 }
883 return 0;
884}
885
Florin Corasad0c77f2017-11-09 18:00:15 -0800886/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700887VLIB_CLI_COMMAND (segment_manager_show_command, static) =
888{
889 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400890 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700891 .function = segment_manager_show_fn,
892};
893/* *INDENT-ON* */
894
Florin Coras88001c62019-04-24 14:44:46 -0700895void
896segment_manager_format_sessions (segment_manager_t * sm, int verbose)
897{
Florin Coras88001c62019-04-24 14:44:46 -0700898 vlib_main_t *vm = vlib_get_main ();
899 app_worker_t *app_wrk;
Florin Coras62ddc032019-12-08 18:30:42 -0800900 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700901 const u8 *app_name;
Florin Coras62ddc032019-12-08 18:30:42 -0800902 int slice_index;
903 u8 *s = 0, *str;
904 svm_fifo_t *f;
Florin Coras88001c62019-04-24 14:44:46 -0700905
906 if (!sm)
907 {
908 if (verbose)
909 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
910 "API Client", "SegManager");
911 else
912 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
913 return;
914 }
915
916 app_wrk = app_worker_get (sm->app_wrk_index);
917 app_name = application_name_from_index (app_wrk->app_index);
918
919 clib_rwlock_reader_lock (&sm->segments_rwlock);
920
921 /* *INDENT-OFF* */
Florin Coras62ddc032019-12-08 18:30:42 -0800922 pool_foreach (fs, sm->segments, ({
923 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -0700924 {
Florin Coras62ddc032019-12-08 18:30:42 -0800925 f = fifo_segment_get_slice_fifo_list (fs, slice_index);
926 while (f)
927 {
928 u32 session_index, thread_index;
929 session_t *session;
Florin Coras88001c62019-04-24 14:44:46 -0700930
Florin Coras62ddc032019-12-08 18:30:42 -0800931 session_index = f->master_session_index;
932 thread_index = f->master_thread_index;
Florin Coras88001c62019-04-24 14:44:46 -0700933
Florin Coras62ddc032019-12-08 18:30:42 -0800934 session = session_get (session_index, thread_index);
935 str = format (0, "%U", format_session, session, verbose);
Florin Coras88001c62019-04-24 14:44:46 -0700936
Florin Coras62ddc032019-12-08 18:30:42 -0800937 if (verbose)
938 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
939 app_wrk->api_client_index, app_wrk->connects_seg_manager);
940 else
941 s = format (s, "%-40s%-20s", str, app_name);
Florin Coras88001c62019-04-24 14:44:46 -0700942
Florin Coras62ddc032019-12-08 18:30:42 -0800943 vlib_cli_output (vm, "%v", s);
944 vec_reset_length (s);
945 vec_free (str);
Florin Coras88001c62019-04-24 14:44:46 -0700946
Florin Coras62ddc032019-12-08 18:30:42 -0800947 f = f->next;
948 }
949 vec_free (s);
Florin Coras88001c62019-04-24 14:44:46 -0700950 }
Florin Coras88001c62019-04-24 14:44:46 -0700951 }));
952 /* *INDENT-ON* */
953
954 clib_rwlock_reader_unlock (&sm->segments_rwlock);
955}
956
Florin Coras6cf30ad2017-04-04 23:08:23 -0700957/*
958 * fd.io coding-style-patch-verification: ON
959 *
960 * Local Variables:
961 * eval: (c-set-style "gnu")
962 * End:
963 */