blob: 9c33e35e8f0a0202e77286289cfdda9f593af392 [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 Corasad0c77f2017-11-09 18:00:15 -080057 return props;
58}
59
Florin Coras9d063042017-09-14 03:08:00 -040060static u8
61segment_manager_app_detached (segment_manager_t * sm)
62{
Florin Coras15531972018-08-12 23:50:53 -070063 return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -040064}
65
Florin Coras4e4531e2017-11-06 23:27:56 -080066void
67segment_manager_app_detach (segment_manager_t * sm)
68{
Florin Coras15531972018-08-12 23:50:53 -070069 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras4e4531e2017-11-06 23:27:56 -080070}
71
Florin Corasa332c462018-01-31 06:52:17 -080072always_inline u32
Florin Coras88001c62019-04-24 14:44:46 -070073segment_manager_segment_index (segment_manager_t * sm, fifo_segment_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070074{
Florin Corasa332c462018-01-31 06:52:17 -080075 return (seg - sm->segments);
76}
77
78/**
Florin Coras88001c62019-04-24 14:44:46 -070079 * Adds segment to segment manager's pool
80 *
81 * If needed a writer's lock is acquired before allocating a new segment
82 * to avoid affecting any of the segments pool readers.
83 */
84int
85segment_manager_add_segment (segment_manager_t * sm, u32 segment_size)
86{
87 segment_manager_main_t *smm = &sm_main;
88 u32 rnd_margin = 128 << 10, fs_index = ~0, page_size;
89 uword baseva = (uword) ~ 0ULL, alloc_size;
90 segment_manager_props_t *props;
91 fifo_segment_t *fs;
92 u8 *seg_name;
93 int rv;
94
95 props = segment_manager_properties_get (sm);
96
97 /* Not configured for addition of new segments and not first */
98 if (!props->add_segment && !segment_size)
99 {
100 clib_warning ("cannot allocate new segment");
101 return VNET_API_ERROR_INVALID_VALUE;
102 }
103
104 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700105 * Allocate fifo segment and grab lock if needed
Florin Coras88001c62019-04-24 14:44:46 -0700106 */
107 if (vlib_num_workers ())
108 clib_rwlock_writer_lock (&sm->segments_rwlock);
109
110 pool_get_zero (sm->segments, fs);
111
112 /*
Florin Corasf9d4ab42019-05-11 16:55:53 -0700113 * Allocate ssvm segment
Florin Coras88001c62019-04-24 14:44:46 -0700114 */
115 segment_size = segment_size ? segment_size : props->add_segment_size;
116 page_size = clib_mem_get_page_size ();
Florin Coras404b8a32019-05-09 12:08:06 -0700117 /* Protect against segment size u32 wrap */
118 segment_size = clib_max (segment_size + page_size - 1, segment_size);
119 segment_size = segment_size & ~(page_size - 1);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700120
Florin Coras88001c62019-04-24 14:44:46 -0700121 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
122 {
123 seg_name = format (0, "%d-%d%c", getpid (), smm->seg_name_counter++, 0);
124 alloc_size = (uword) segment_size + rnd_margin;
125 baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
126 if (!baseva)
127 {
128 clib_warning ("out of space for segments");
129 pool_put (sm->segments, fs);
130 goto done;
131 }
132 }
133 else
Florin Coras5368bb02019-06-09 09:24:33 -0700134 seg_name = format (0, "%s%c", "process-private", 0);
Florin Coras88001c62019-04-24 14:44:46 -0700135
136 fs->ssvm.ssvm_size = segment_size;
137 fs->ssvm.name = seg_name;
138 fs->ssvm.requested_va = baseva;
139
140 if ((rv = ssvm_master_init (&fs->ssvm, props->segment_type)))
141 {
142 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
143 segment_size);
144
145 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
146 clib_valloc_free (&smm->va_allocator, baseva);
147 pool_put (sm->segments, fs);
148 goto done;
149 }
150
Florin Corasf9d4ab42019-05-11 16:55:53 -0700151 /*
152 * Initialize fifo segment
153 */
Florin Coras88001c62019-04-24 14:44:46 -0700154 fifo_segment_init (fs);
155
156 /*
157 * Save segment index before dropping lock, if any held
158 */
159 fs_index = fs - sm->segments;
160
161done:
162
163 if (vlib_num_workers ())
164 clib_rwlock_writer_unlock (&sm->segments_rwlock);
165
166 return fs_index;
167}
168
169/**
Florin Corasa332c462018-01-31 06:52:17 -0800170 * Remove segment without lock
171 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800172void
Florin Coras88001c62019-04-24 14:44:46 -0700173segment_manager_del_segment (segment_manager_t * sm, fifo_segment_t * fs)
Florin Corasa332c462018-01-31 06:52:17 -0800174{
Florin Coras88001c62019-04-24 14:44:46 -0700175 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800176
177 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
Florin Corasa0dbf9e2019-03-01 17:12:02 -0800178 {
179 clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va);
180
181 if (sm->app_wrk_index != SEGMENT_MANAGER_INVALID_APP_INDEX)
182 {
183 app_worker_t *app_wrk;
184 u64 segment_handle;
185 app_wrk = app_worker_get (sm->app_wrk_index);
186 segment_handle = segment_manager_segment_handle (sm, fs);
187 app_worker_del_segment_notify (app_wrk, segment_handle);
188 }
189 }
Florin Corasa332c462018-01-31 06:52:17 -0800190
191 ssvm_delete (&fs->ssvm);
192
193 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400194 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -0800195 pool_put (sm->segments, fs);
196}
197
198/**
199 * Removes segment after acquiring writer lock
200 */
Florin Coras99368312018-08-02 10:45:44 -0700201static inline void
Florin Corasa332c462018-01-31 06:52:17 -0800202segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
203{
Florin Coras88001c62019-04-24 14:44:46 -0700204 fifo_segment_t *fs;
Florin Corasa332c462018-01-31 06:52:17 -0800205 u8 is_prealloc;
206
207 clib_rwlock_writer_lock (&sm->segments_rwlock);
208 fs = segment_manager_get_segment (sm, fs_index);
Florin Coras88001c62019-04-24 14:44:46 -0700209 is_prealloc = fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800210 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras9d063042017-09-14 03:08:00 -0400211 {
Florin Corasa332c462018-01-31 06:52:17 -0800212 clib_rwlock_writer_unlock (&sm->segments_rwlock);
Florin Coras9d063042017-09-14 03:08:00 -0400213 return;
214 }
Florin Corasa332c462018-01-31 06:52:17 -0800215
216 segment_manager_del_segment (sm, fs);
217 clib_rwlock_writer_unlock (&sm->segments_rwlock);
218}
219
220/**
221 * Reads a segment from the segment manager's pool without lock
222 */
Florin Coras88001c62019-04-24 14:44:46 -0700223fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800224segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
225{
226 return pool_elt_at_index (sm->segments, segment_index);
227}
228
Florin Corasfa76a762018-11-29 12:40:10 -0800229u64
230segment_manager_segment_handle (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700231 fifo_segment_t * segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800232{
233 u32 segment_index = segment_manager_segment_index (sm, segment);
234 return (((u64) segment_manager_index (sm) << 32) | segment_index);
235}
236
Florin Coras88001c62019-04-24 14:44:46 -0700237static void
Florin Corasfa76a762018-11-29 12:40:10 -0800238segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index,
239 u32 * segment_index)
240{
241 *sm_index = segment_handle >> 32;
242 *segment_index = segment_handle & 0xFFFFFFFF;
243}
244
Florin Coras88001c62019-04-24 14:44:46 -0700245u64
246segment_manager_make_segment_handle (u32 segment_manager_index,
247 u32 segment_index)
248{
249 return (((u64) segment_manager_index << 32) | segment_index);
250}
251
252fifo_segment_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800253segment_manager_get_segment_w_handle (u64 segment_handle)
254{
255 u32 sm_index, segment_index;
256 segment_manager_t *sm;
257
258 segment_manager_parse_segment_handle (segment_handle, &sm_index,
259 &segment_index);
260 sm = segment_manager_get (sm_index);
261 if (!sm || pool_is_free_index (sm->segments, segment_index))
262 return 0;
263 return pool_elt_at_index (sm->segments, segment_index);
264}
265
Florin Corasa332c462018-01-31 06:52:17 -0800266/**
267 * Reads a segment from the segment manager's pool and acquires reader lock
268 *
269 * Caller must drop the reader's lock by calling
270 * @ref segment_manager_segment_reader_unlock once it finishes working with
271 * the segment.
272 */
Florin Coras88001c62019-04-24 14:44:46 -0700273fifo_segment_t *
Florin Corasa332c462018-01-31 06:52:17 -0800274segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
275{
276 clib_rwlock_reader_lock (&sm->segments_rwlock);
277 return pool_elt_at_index (sm->segments, segment_index);
278}
279
280void
281segment_manager_segment_reader_unlock (segment_manager_t * sm)
282{
283 clib_rwlock_reader_unlock (&sm->segments_rwlock);
284}
285
286void
287segment_manager_segment_writer_unlock (segment_manager_t * sm)
288{
289 clib_rwlock_writer_unlock (&sm->segments_rwlock);
290}
291
Florin Corasa332c462018-01-31 06:52:17 -0800292segment_manager_t *
Florin Coras88001c62019-04-24 14:44:46 -0700293segment_manager_alloc (void)
Florin Corasa332c462018-01-31 06:52:17 -0800294{
Florin Coras88001c62019-04-24 14:44:46 -0700295 segment_manager_main_t *smm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800296 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -0700297
298 pool_get_zero (smm->segment_managers, sm);
Florin Corasa332c462018-01-31 06:52:17 -0800299 clib_rwlock_init (&sm->segments_rwlock);
300 return sm;
301}
302
303/**
304 * Initializes segment manager based on options provided.
305 * Returns error if ssvm segment(s) allocation fails.
306 */
307int
308segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800309 u32 prealloc_fifo_pairs)
Florin Corasa332c462018-01-31 06:52:17 -0800310{
311 u32 rx_fifo_size, tx_fifo_size, pair_size;
312 u32 rx_rounded_data_size, tx_rounded_data_size;
Florin Coras86f04502018-09-12 16:08:01 -0700313 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700314 segment_manager_props_t *props;
315 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800316 u32 approx_segment_count;
317 int seg_index, i;
318
319 props = segment_manager_properties_get (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700320 first_seg_size = clib_max (first_seg_size, sm_main.default_segment_size);
Florin Corasa332c462018-01-31 06:52:17 -0800321
322 if (prealloc_fifo_pairs)
323 {
324 /* Figure out how many segments should be preallocated */
325 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
326 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
327
328 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
329 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
330 pair_size = rx_fifo_size + tx_fifo_size;
331
332 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
333 if (first_seg_size > approx_total_size)
334 max_seg_size = first_seg_size;
335 approx_segment_count = (approx_total_size + (max_seg_size - 1))
336 / max_seg_size;
337
338 /* Allocate the segments */
339 for (i = 0; i < approx_segment_count + 1; i++)
340 {
341 seg_index = segment_manager_add_segment (sm, max_seg_size);
342 if (seg_index < 0)
343 {
344 clib_warning ("Failed to preallocate segment %d", i);
345 return seg_index;
346 }
347
Florin Corasa332c462018-01-31 06:52:17 -0800348 segment = segment_manager_get_segment (sm, seg_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800349 if (i == 0)
Florin Coras99368312018-08-02 10:45:44 -0700350 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800351
Florin Coras88001c62019-04-24 14:44:46 -0700352 fifo_segment_preallocate_fifo_pairs (segment,
353 props->rx_fifo_size,
354 props->tx_fifo_size,
355 &prealloc_fifo_pairs);
356 fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED;
Florin Corasa332c462018-01-31 06:52:17 -0800357 if (prealloc_fifo_pairs == 0)
358 break;
359 }
360 }
361 else
362 {
363 seg_index = segment_manager_add_segment (sm, first_seg_size);
Florin Coras402377e2019-04-17 10:23:23 -0700364 if (seg_index < 0)
Florin Corasa332c462018-01-31 06:52:17 -0800365 {
366 clib_warning ("Failed to allocate segment");
367 return seg_index;
368 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800369 segment = segment_manager_get_segment (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -0700370 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasa332c462018-01-31 06:52:17 -0800371 }
372
373 return 0;
374}
375
Florin Corasc87c91d2017-08-16 19:55:49 -0700376/**
Florin Coras88001c62019-04-24 14:44:46 -0700377 * Cleanup segment manager.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700378 */
379void
Florin Coras88001c62019-04-24 14:44:46 -0700380segment_manager_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700381{
Florin Coras88001c62019-04-24 14:44:46 -0700382 segment_manager_main_t *smm = &sm_main;
383 fifo_segment_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400384
Florin Coras9d063042017-09-14 03:08:00 -0400385 ASSERT (!segment_manager_has_fifos (sm)
386 && segment_manager_app_detached (sm));
387
388 /* If we have empty preallocated segments that haven't been removed, remove
389 * them now. Apart from that, the first segment in the first segment manager
390 * is not removed when all fifos are removed. It can only be removed when
391 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800392 clib_rwlock_writer_lock (&sm->segments_rwlock);
393
394 /* *INDENT-OFF* */
395 pool_foreach (fifo_segment, sm->segments, ({
396 segment_manager_del_segment (sm, fifo_segment);
397 }));
398 /* *INDENT-ON* */
399
400 clib_rwlock_writer_unlock (&sm->segments_rwlock);
401
402 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700403 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400404 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800405 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700406}
407
Florin Corasc87c91d2017-08-16 19:55:49 -0700408void
Florin Coras88001c62019-04-24 14:44:46 -0700409segment_manager_init_free (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700410{
Florin Coras4e4531e2017-11-06 23:27:56 -0800411 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700412 if (segment_manager_has_fifos (sm))
413 segment_manager_del_sessions (sm);
414 else
415 {
Florin Coras9d063042017-09-14 03:08:00 -0400416 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Coras88001c62019-04-24 14:44:46 -0700417 segment_manager_free (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700418 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700419}
420
Florin Coras88001c62019-04-24 14:44:46 -0700421segment_manager_t *
422segment_manager_get (u32 index)
423{
424 return pool_elt_at_index (sm_main.segment_managers, index);
425}
426
427segment_manager_t *
428segment_manager_get_if_valid (u32 index)
429{
430 if (pool_is_free_index (sm_main.segment_managers, index))
431 return 0;
432 return pool_elt_at_index (sm_main.segment_managers, index);
433}
434
435u32
436segment_manager_index (segment_manager_t * sm)
437{
438 return sm - sm_main.segment_managers;
439}
440
441u8
442segment_manager_has_fifos (segment_manager_t * sm)
443{
444 fifo_segment_t *seg;
445 u8 first = 1;
446
447 /* *INDENT-OFF* */
448 segment_manager_foreach_segment_w_lock (seg, sm, ({
449 if (CLIB_DEBUG && !first && !fifo_segment_has_fifos (seg)
450 && !(fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
451 {
452 clib_warning ("segment %d has no fifos!",
453 segment_manager_segment_index (sm, seg));
454 first = 0;
455 }
456 if (fifo_segment_has_fifos (seg))
457 {
458 segment_manager_segment_reader_unlock (sm);
459 return 1;
460 }
461 }));
462 /* *INDENT-ON* */
463
464 return 0;
465}
466
467/**
468 * Initiate disconnects for all sessions 'owned' by a segment manager
469 */
470void
471segment_manager_del_sessions (segment_manager_t * sm)
472{
473 fifo_segment_t *fifo_segment;
474 session_handle_t *handles = 0, *handle;
475 session_t *session;
476 svm_fifo_t *fifo;
477
478 ASSERT (pool_elts (sm->segments) != 0);
479
480 /* Across all fifo segments used by the server */
481 /* *INDENT-OFF* */
482 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
483 fifo = fifo_segment_get_fifo_list (fifo_segment);
484
485 /*
486 * Remove any residual sessions from the session lookup table
487 * Don't bother deleting the individual fifos, we're going to
488 * throw away the fifo segment in a minute.
489 */
490 while (fifo)
491 {
492 session = session_get_if_valid (fifo->master_session_index,
493 fifo->master_thread_index);
494 if (session)
495 vec_add1 (handles, session_handle (session));
496 fifo = fifo->next;
497 }
498
499 /* Instead of removing the segment, test when cleaning up disconnected
500 * sessions if the segment can be removed.
501 */
502 }));
503 /* *INDENT-ON* */
504
505 vec_foreach (handle, handles)
506 session_close (session_get_from_handle (*handle));
507}
508
Florin Corasf8f516a2018-02-08 15:10:09 -0800509int
Florin Coras88001c62019-04-24 14:44:46 -0700510segment_manager_try_alloc_fifos (fifo_segment_t * fifo_segment,
Florin Corasf8f516a2018-02-08 15:10:09 -0800511 u32 rx_fifo_size, u32 tx_fifo_size,
512 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800513{
Florin Coras88001c62019-04-24 14:44:46 -0700514 rx_fifo_size = clib_max (rx_fifo_size, sm_main.default_fifo_size);
515 *rx_fifo = fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size,
516 FIFO_SEGMENT_RX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800517
Florin Coras88001c62019-04-24 14:44:46 -0700518 tx_fifo_size = clib_max (tx_fifo_size, sm_main.default_fifo_size);
519 *tx_fifo = fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size,
520 FIFO_SEGMENT_TX_FIFO);
Florin Corasa332c462018-01-31 06:52:17 -0800521
522 if (*rx_fifo == 0)
523 {
524 /* This would be very odd, but handle it... */
525 if (*tx_fifo != 0)
526 {
Florin Coras88001c62019-04-24 14:44:46 -0700527 fifo_segment_free_fifo (fifo_segment, *tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800528 *tx_fifo = 0;
529 }
530 return -1;
531 }
532 if (*tx_fifo == 0)
533 {
534 if (*rx_fifo != 0)
535 {
Florin Coras88001c62019-04-24 14:44:46 -0700536 fifo_segment_free_fifo (fifo_segment, *rx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800537 *rx_fifo = 0;
538 }
539 return -1;
540 }
541
542 return 0;
543}
544
Florin Coras6cf30ad2017-04-04 23:08:23 -0700545int
546segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Corasb384b542018-01-15 01:08:33 -0800547 svm_fifo_t ** rx_fifo,
Florin Coras1219b2d2019-04-23 15:53:43 -0700548 svm_fifo_t ** tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700549{
Florin Corasa332c462018-01-31 06:52:17 -0800550 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Coras88001c62019-04-24 14:44:46 -0700551 segment_manager_props_t *props;
552 fifo_segment_t *fs = 0;
553 u32 sm_index, fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700554 u8 added_a_segment = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700555 u64 fs_handle;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700556
Florin Corasa332c462018-01-31 06:52:17 -0800557 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700558
Florin Corasa332c462018-01-31 06:52:17 -0800559 /*
560 * Find the first free segment to allocate the fifos in
561 */
Florin Corasa5464812017-04-19 13:00:05 -0700562
Florin Corasa332c462018-01-31 06:52:17 -0800563 /* *INDENT-OFF* */
Florin Coras88001c62019-04-24 14:44:46 -0700564 segment_manager_foreach_segment_w_lock (fs, sm, ({
565 alloc_fail = segment_manager_try_alloc_fifos (fs,
Florin Corasf8f516a2018-02-08 15:10:09 -0800566 props->rx_fifo_size,
567 props->tx_fifo_size,
568 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800569 /* Exit with lock held, drop it after notifying app */
570 if (!alloc_fail)
571 goto alloc_success;
572 }));
573 /* *INDENT-ON* */
574
575alloc_check:
576
577 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700578 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700579
Florin Corasa332c462018-01-31 06:52:17 -0800580 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700581
Florin Corasa332c462018-01-31 06:52:17 -0800582 ASSERT (rx_fifo && tx_fifo);
583 sm_index = segment_manager_index (sm);
Florin Coras88001c62019-04-24 14:44:46 -0700584 fs_index = segment_manager_segment_index (sm, fs);
Florin Corasa332c462018-01-31 06:52:17 -0800585 (*tx_fifo)->segment_manager = sm_index;
586 (*rx_fifo)->segment_manager = sm_index;
Florin Coras88001c62019-04-24 14:44:46 -0700587 (*tx_fifo)->segment_index = fs_index;
588 (*rx_fifo)->segment_index = fs_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700589
Florin Corasa332c462018-01-31 06:52:17 -0800590 if (added_a_segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800591 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800592 app_worker_t *app_wrk;
Florin Coras88001c62019-04-24 14:44:46 -0700593 fs_handle = segment_manager_segment_handle (sm, fs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800594 app_wrk = app_worker_get (sm->app_wrk_index);
Florin Coras88001c62019-04-24 14:44:46 -0700595 rv = app_worker_add_segment_notify (app_wrk, fs_handle);
Florin Corasfa76a762018-11-29 12:40:10 -0800596 }
Florin Corasa332c462018-01-31 06:52:17 -0800597 /* Drop the lock after app is notified */
598 segment_manager_segment_reader_unlock (sm);
599 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700600 }
601
Florin Corasa332c462018-01-31 06:52:17 -0800602 /*
603 * Allocation failed, see if we can add a new segment
604 */
605 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700606 {
Florin Corasa332c462018-01-31 06:52:17 -0800607 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700608 {
Florin Corasa332c462018-01-31 06:52:17 -0800609 clib_warning ("Added a segment, still can't allocate a fifo");
610 segment_manager_segment_reader_unlock (sm);
611 return SESSION_ERROR_NEW_SEG_NO_SPACE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700612 }
Florin Corasa332c462018-01-31 06:52:17 -0800613 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700614 {
Florin Corasa332c462018-01-31 06:52:17 -0800615 clib_warning ("Failed to add new segment");
616 return SESSION_ERROR_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700617 }
Florin Coras88001c62019-04-24 14:44:46 -0700618 fs = segment_manager_get_segment_w_lock (sm, new_fs_index);
619 alloc_fail = segment_manager_try_alloc_fifos (fs, props->rx_fifo_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800620 props->tx_fifo_size,
621 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800622 added_a_segment = 1;
623 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700624 }
Florin Corasa332c462018-01-31 06:52:17 -0800625 else
626 {
627 clib_warning ("Can't add new seg and no space to allocate fifos!");
628 return SESSION_ERROR_NO_SPACE;
629 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700630}
631
632void
Florin Coras19223e02019-03-03 14:56:05 -0800633segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, svm_fifo_t * tx_fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700634{
Florin Corasa332c462018-01-31 06:52:17 -0800635 segment_manager_t *sm;
Florin Corasb095a3c2019-04-25 12:58:46 -0700636 fifo_segment_t *fs;
Florin Coras19223e02019-03-03 14:56:05 -0800637 u32 segment_index;
Florin Corasa5464812017-04-19 13:00:05 -0700638
Florin Coras58a93e82019-01-14 23:33:46 -0800639 if (!rx_fifo || !tx_fifo)
640 return;
641
Florin Corasa5464812017-04-19 13:00:05 -0700642 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700643 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800644 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700645 return;
646
Florin Coras19223e02019-03-03 14:56:05 -0800647 segment_index = rx_fifo->segment_index;
Florin Coras88001c62019-04-24 14:44:46 -0700648 fs = segment_manager_get_segment_w_lock (sm, segment_index);
649 fifo_segment_free_fifo (fs, rx_fifo);
650 fifo_segment_free_fifo (fs, tx_fifo);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700651
Florin Corasc87c91d2017-08-16 19:55:49 -0700652 /*
653 * Try to remove svm segment if it has no fifos. This can be done only if
654 * the segment is not the first in the segment manager or if it is first
655 * and it is not protected. Moreover, if the segment is first and the app
656 * has detached from the segment manager, remove the segment manager.
657 */
Florin Coras88001c62019-04-24 14:44:46 -0700658 if (!fifo_segment_has_fifos (fs))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700659 {
Florin Corasa332c462018-01-31 06:52:17 -0800660 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700661
662 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800663 if (segment_index != 0 || !sm->first_is_protected)
664 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700665
666 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400667 if (segment_manager_app_detached (sm)
668 && !segment_manager_has_fifos (sm))
Florin Coras568ebc72018-09-18 16:12:50 -0700669 {
Florin Coras88001c62019-04-24 14:44:46 -0700670 segment_manager_free (sm);
Florin Coras568ebc72018-09-18 16:12:50 -0700671 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700672 }
Florin Corasa332c462018-01-31 06:52:17 -0800673 else
674 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700675}
676
Florin Corasb095a3c2019-04-25 12:58:46 -0700677int
678segment_manager_grow_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size)
679{
680 fifo_segment_t *fs;
681 int rv;
682
683 fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
684 rv = fifo_segment_grow_fifo (fs, f, size);
685 segment_manager_segment_reader_unlock (sm);
686
687 return rv;
688}
689
Florin Coras344ce422019-05-03 11:46:55 -0700690int
691segment_manager_collect_fifo_chunks (segment_manager_t * sm, svm_fifo_t * f)
692{
693 fifo_segment_t *fs;
694 int rv;
695
696 fs = segment_manager_get_segment_w_lock (sm, f->segment_index);
697 rv = fifo_segment_collect_fifo_chunks (fs, f);
698 segment_manager_segment_reader_unlock (sm);
699
700 return rv;
701}
702
703int
704segment_manager_shrink_fifo (segment_manager_t * sm, svm_fifo_t * f, u32 size,
705 u8 is_producer)
706{
707 int rv;
708
709 rv = svm_fifo_reduce_size (f, size, is_producer);
710
711 /* Nothing to collect at this point */
712 if (!is_producer)
713 return rv;
714
715 if (f->flags & SVM_FIFO_F_COLLECT_CHUNKS)
716 segment_manager_collect_fifo_chunks (sm, f);
717
718 return rv;
719}
720
Florin Coras3c2fed52018-07-04 04:15:05 -0700721u32
722segment_manager_evt_q_expected_size (u32 q_len)
723{
724 u32 fifo_evt_size, notif_q_size, q_hdrs;
725 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
726
Florin Coras52207f12018-07-12 14:48:06 -0700727 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700728 notif_q_size = clib_max (16, q_len >> 4);
729
730 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
731 fifo_evt_ring_sz = q_len * fifo_evt_size;
732 session_ntf_ring_sz = notif_q_size * 256;
733 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
734
735 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
736}
737
Florin Corasa5464812017-04-19 13:00:05 -0700738/**
739 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800740 *
741 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700742 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700743svm_msg_q_t *
Florin Coras88001c62019-04-24 14:44:46 -0700744segment_manager_alloc_queue (fifo_segment_t * segment,
745 segment_manager_props_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700746{
Florin Coras3c2fed52018-07-04 04:15:05 -0700747 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
748 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
749 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700750 void *oldheap;
751
Florin Coras52207f12018-07-12 14:48:06 -0700752 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700753 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700754 /* *INDENT-OFF* */
755 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700756 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700757 {notif_q_size, session_evt_size, 0}
758 };
759 /* *INDENT-ON* */
760 cfg->consumer_pid = 0;
761 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700762 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700763 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700764
Florin Coras3c2fed52018-07-04 04:15:05 -0700765 oldheap = ssvm_push_heap (segment->ssvm.sh);
766 q = svm_msg_q_alloc (cfg);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700767 fifo_segment_update_free_bytes (segment);
Florin Corasa5464812017-04-19 13:00:05 -0700768 ssvm_pop_heap (oldheap);
Florin Coras99368312018-08-02 10:45:44 -0700769
770 if (props->use_mq_eventfd)
771 {
772 if (svm_msg_q_alloc_producer_eventfd (q))
773 clib_warning ("failed to alloc eventfd");
774 }
Florin Corasa5464812017-04-19 13:00:05 -0700775 return q;
776}
777
Florin Coras88001c62019-04-24 14:44:46 -0700778svm_msg_q_t *
779segment_manager_event_queue (segment_manager_t * sm)
780{
781 return sm->event_queue;
782}
783
Florin Corasa5464812017-04-19 13:00:05 -0700784/**
785 * Frees shm queue allocated in the first segment
786 */
787void
Florin Corase86a8ed2018-01-05 03:20:25 -0800788segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700789{
Florin Coras88001c62019-04-24 14:44:46 -0700790 fifo_segment_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800791 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700792 void *oldheap;
793
Florin Corasa332c462018-01-31 06:52:17 -0800794 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700795
Florin Corasa332c462018-01-31 06:52:17 -0800796 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700797 sh = segment->ssvm.sh;
798
799 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800800 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700801 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800802 segment_manager_segment_reader_unlock (sm);
803}
804
805/*
806 * Init segment vm address allocator
807 */
808void
809segment_manager_main_init (segment_manager_main_init_args_t * a)
810{
Florin Coras88001c62019-04-24 14:44:46 -0700811 segment_manager_main_t *sm = &sm_main;
Florin Corasa332c462018-01-31 06:52:17 -0800812 clib_valloc_chunk_t _ip, *ip = &_ip;
813
814 ip->baseva = a->baseva;
815 ip->size = a->size;
816
817 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Coras88001c62019-04-24 14:44:46 -0700818
819 sm->default_fifo_size = 1 << 12;
820 sm->default_segment_size = 1 << 20;
821 sm->default_app_mq_size = 128;
Florin Corasa5464812017-04-19 13:00:05 -0700822}
823
Florin Corasc87c91d2017-08-16 19:55:49 -0700824static clib_error_t *
825segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
826 vlib_cli_command_t * cmd)
827{
Florin Coras88001c62019-04-24 14:44:46 -0700828 segment_manager_main_t *smm = &sm_main;
Florin Corasb384b542018-01-15 01:08:33 -0800829 u8 show_segments = 0, verbose = 0;
Florin Coras5368bb02019-06-09 09:24:33 -0700830 segment_manager_t *sm;
831 fifo_segment_t *seg;
Dave Barach91f3e742017-09-01 19:12:11 -0400832
Florin Corasc87c91d2017-08-16 19:55:49 -0700833 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
834 {
835 if (unformat (input, "segments"))
836 show_segments = 1;
837 else if (unformat (input, "verbose"))
838 verbose = 1;
839 else
840 return clib_error_return (0, "unknown input `%U'",
841 format_unformat_error, input);
842 }
843 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800844 pool_elts (smm->segment_managers));
845 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700846 {
847 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
848 "Segments");
849
850 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800851 pool_foreach (sm, smm->segment_managers, ({
Florin Coras15531972018-08-12 23:50:53 -0700852 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm),
853 sm->app_wrk_index, pool_elts (sm->segments));
Florin Corasc87c91d2017-08-16 19:55:49 -0700854 }));
855 /* *INDENT-ON* */
856
857 }
858 if (show_segments)
859 {
Florin Coras5368bb02019-06-09 09:24:33 -0700860 vlib_cli_output (vm, "%U", format_fifo_segment, 0, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700861
862 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800863 pool_foreach (sm, smm->segment_managers, ({
864 segment_manager_foreach_segment_w_lock (seg, sm, ({
Florin Coras5368bb02019-06-09 09:24:33 -0700865 vlib_cli_output (vm, "%U", format_fifo_segment, seg, verbose);
Florin Corasa332c462018-01-31 06:52:17 -0800866 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700867 }));
868 /* *INDENT-ON* */
869
870 }
871 return 0;
872}
873
Florin Corasad0c77f2017-11-09 18:00:15 -0800874/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700875VLIB_CLI_COMMAND (segment_manager_show_command, static) =
876{
877 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400878 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700879 .function = segment_manager_show_fn,
880};
881/* *INDENT-ON* */
882
Florin Coras88001c62019-04-24 14:44:46 -0700883void
884segment_manager_format_sessions (segment_manager_t * sm, int verbose)
885{
886 fifo_segment_t *fifo_segment;
887 vlib_main_t *vm = vlib_get_main ();
888 app_worker_t *app_wrk;
889 const u8 *app_name;
890 u8 *s = 0;
891
892 if (!sm)
893 {
894 if (verbose)
895 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
896 "API Client", "SegManager");
897 else
898 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
899 return;
900 }
901
902 app_wrk = app_worker_get (sm->app_wrk_index);
903 app_name = application_name_from_index (app_wrk->app_index);
904
905 clib_rwlock_reader_lock (&sm->segments_rwlock);
906
907 /* *INDENT-OFF* */
908 pool_foreach (fifo_segment, sm->segments, ({
909 svm_fifo_t *fifo;
910 u8 *str;
911
912 fifo = fifo_segment_get_fifo_list (fifo_segment);
913 while (fifo)
914 {
915 u32 session_index, thread_index;
916 session_t *session;
917
918 session_index = fifo->master_session_index;
919 thread_index = fifo->master_thread_index;
920
921 session = session_get (session_index, thread_index);
922 str = format (0, "%U", format_session, session, verbose);
923
924 if (verbose)
925 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
926 app_wrk->api_client_index, app_wrk->connects_seg_manager);
927 else
928 s = format (s, "%-40s%-20s", str, app_name);
929
930 vlib_cli_output (vm, "%v", s);
931 vec_reset_length (s);
932 vec_free (str);
933
934 fifo = fifo->next;
935 }
936 vec_free (s);
937 }));
938 /* *INDENT-ON* */
939
940 clib_rwlock_reader_unlock (&sm->segments_rwlock);
941}
942
Florin Coras6cf30ad2017-04-04 23:08:23 -0700943/*
944 * fd.io coding-style-patch-verification: ON
945 *
946 * Local Variables:
947 * eval: (c-set-style "gnu")
948 * End:
949 */