blob: 9304dd70b5df1a34add1733cf78ca6638980aff2 [file] [log] [blame]
Florin Coras6cf30ad2017-04-04 23:08:23 -07001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * 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 Corasa332c462018-01-31 06:52:17 -080020segment_manager_main_t segment_manager_main;
21
Florin Coras6cf30ad2017-04-04 23:08:23 -070022/**
23 * Counter used to build segment names
24 */
Florin Corasa332c462018-01-31 06:52:17 -080025static u32 segment_name_counter = 0;
Florin Corasa5464812017-04-19 13:00:05 -070026
27/**
Florin Coras6cf30ad2017-04-04 23:08:23 -070028 * Default fifo and segment size. TODO config.
29 */
Florin Corasf8f516a2018-02-08 15:10:09 -080030static u32 default_fifo_size = 1 << 12;
31static u32 default_segment_size = 1 << 20;
32static u32 default_app_evt_queue_size = 128;
Florin Coras6cf30ad2017-04-04 23:08:23 -070033
Florin Corasad0c77f2017-11-09 18:00:15 -080034segment_manager_properties_t *
Florin Corasa332c462018-01-31 06:52:17 -080035segment_manager_properties_get (segment_manager_t * sm)
Florin Corasad0c77f2017-11-09 18:00:15 -080036{
Florin Corasa332c462018-01-31 06:52:17 -080037 return application_get_segment_manager_properties (sm->app_index);
38}
39
40segment_manager_properties_t *
41segment_manager_properties_init (segment_manager_properties_t * props)
42{
Florin Corasb384b542018-01-15 01:08:33 -080043 props->add_segment_size = default_segment_size;
44 props->rx_fifo_size = default_fifo_size;
45 props->tx_fifo_size = default_fifo_size;
Florin Corasf8f516a2018-02-08 15:10:09 -080046 props->evt_q_size = default_app_evt_queue_size;
Florin Corasad0c77f2017-11-09 18:00:15 -080047 return props;
48}
49
Florin Coras9d063042017-09-14 03:08:00 -040050static u8
51segment_manager_app_detached (segment_manager_t * sm)
52{
53 return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -040054}
55
Florin Coras4e4531e2017-11-06 23:27:56 -080056void
57segment_manager_app_detach (segment_manager_t * sm)
58{
59 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
60}
61
Florin Corasa332c462018-01-31 06:52:17 -080062always_inline u32
63segment_manager_segment_index (segment_manager_t * sm,
64 svm_fifo_segment_private_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070065{
Florin Corasa332c462018-01-31 06:52:17 -080066 return (seg - sm->segments);
67}
68
69/**
70 * Remove segment without lock
71 */
Florin Corasf8f516a2018-02-08 15:10:09 -080072void
Florin Corasa332c462018-01-31 06:52:17 -080073segment_manager_del_segment (segment_manager_t * sm,
74 svm_fifo_segment_private_t * fs)
75{
76 segment_manager_main_t *smm = &segment_manager_main;
77
78 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
79 clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va);
80
81 ssvm_delete (&fs->ssvm);
82
83 if (CLIB_DEBUG)
84 memset (fs, 0xfb, sizeof (*fs));
85 pool_put (sm->segments, fs);
86}
87
88/**
89 * Removes segment after acquiring writer lock
90 */
91always_inline void
92segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
93{
94 svm_fifo_segment_private_t *fs;
95 u8 is_prealloc;
96
97 clib_rwlock_writer_lock (&sm->segments_rwlock);
98 fs = segment_manager_get_segment (sm, fs_index);
99 is_prealloc = svm_fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
100 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras9d063042017-09-14 03:08:00 -0400101 {
Florin Corasa332c462018-01-31 06:52:17 -0800102 clib_rwlock_writer_unlock (&sm->segments_rwlock);
Florin Coras9d063042017-09-14 03:08:00 -0400103 return;
104 }
Florin Corasa332c462018-01-31 06:52:17 -0800105
106 segment_manager_del_segment (sm, fs);
107 clib_rwlock_writer_unlock (&sm->segments_rwlock);
108}
109
110/**
111 * Reads a segment from the segment manager's pool without lock
112 */
113svm_fifo_segment_private_t *
114segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
115{
116 return pool_elt_at_index (sm->segments, segment_index);
117}
118
119/**
120 * Reads a segment from the segment manager's pool and acquires reader lock
121 *
122 * Caller must drop the reader's lock by calling
123 * @ref segment_manager_segment_reader_unlock once it finishes working with
124 * the segment.
125 */
126svm_fifo_segment_private_t *
127segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
128{
129 clib_rwlock_reader_lock (&sm->segments_rwlock);
130 return pool_elt_at_index (sm->segments, segment_index);
131}
132
133void
134segment_manager_segment_reader_unlock (segment_manager_t * sm)
135{
Florin Corasf8f516a2018-02-08 15:10:09 -0800136 ASSERT (sm->segments_rwlock->n_readers > 0);
Florin Corasa332c462018-01-31 06:52:17 -0800137 clib_rwlock_reader_unlock (&sm->segments_rwlock);
138}
139
140void
141segment_manager_segment_writer_unlock (segment_manager_t * sm)
142{
143 clib_rwlock_writer_unlock (&sm->segments_rwlock);
144}
145
146/**
147 * Adds segment to segment manager's pool
148 *
149 * If needed a writer's lock is acquired before allocating a new segment
150 * to avoid affecting any of the segments pool readers.
151 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800152int
Florin Corasa332c462018-01-31 06:52:17 -0800153segment_manager_add_segment (segment_manager_t * sm, u32 segment_size)
154{
155 segment_manager_main_t *smm = &segment_manager_main;
156 u32 rnd_margin = 128 << 10, seg_index;
157 segment_manager_properties_t *props;
158 uword baseva = (u64) ~ 0, alloc_size;
159 svm_fifo_segment_private_t *seg;
160 u8 *seg_name;
161 int rv;
162
163 props = segment_manager_properties_get (sm);
164
165 /* Not configured for addition of new segments and not first */
166 if (!props->add_segment && !segment_size)
167 {
168 clib_warning ("cannot allocate new segment");
169 return VNET_API_ERROR_INVALID_VALUE;
170 }
171
172 /*
173 * Allocate fifo segment and lock if needed
174 */
175 if (vlib_num_workers ())
176 {
177 clib_rwlock_writer_lock (&sm->segments_rwlock);
178 pool_get (sm->segments, seg);
179 }
180 else
181 {
182 pool_get (sm->segments, seg);
183 }
184 memset (seg, 0, sizeof (*seg));
185
186 /*
187 * Initialize ssvm segment and svm fifo private header
188 */
189 segment_size = segment_size ? segment_size : props->add_segment_size;
190 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
191 {
192 seg_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
193 alloc_size = segment_size + rnd_margin;
194 baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
195 if (!baseva)
196 {
197 clib_warning ("out of space for segments");
198 return -1;
199 }
200 }
201 else
202 seg_name = format (0, "%s%c", "process-private-segment", 0);
203
204 seg->ssvm.ssvm_size = segment_size;
205 seg->ssvm.name = seg_name;
206 seg->ssvm.requested_va = baseva;
207
208 if ((rv = ssvm_master_init (&seg->ssvm, props->segment_type)))
209 {
210 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
211 segment_size);
212
213 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
214 clib_valloc_free (&smm->va_allocator, baseva);
215 pool_put (sm->segments, seg);
216 return (rv);
217 }
218
219 svm_fifo_segment_init (seg);
220
221 /*
222 * Save segment index before dropping lock, if any held
223 */
224 seg_index = seg - sm->segments;
225
226 if (vlib_num_workers ())
227 clib_rwlock_writer_unlock (&sm->segments_rwlock);
228
229 return seg_index;
230}
231
232segment_manager_t *
233segment_manager_new ()
234{
235 segment_manager_main_t *smm = &segment_manager_main;
236 segment_manager_t *sm;
237 pool_get (smm->segment_managers, sm);
238 memset (sm, 0, sizeof (*sm));
239 clib_rwlock_init (&sm->segments_rwlock);
240 return sm;
241}
242
243/**
244 * Initializes segment manager based on options provided.
245 * Returns error if ssvm segment(s) allocation fails.
246 */
247int
248segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800249 u32 prealloc_fifo_pairs)
Florin Corasa332c462018-01-31 06:52:17 -0800250{
251 u32 rx_fifo_size, tx_fifo_size, pair_size;
252 u32 rx_rounded_data_size, tx_rounded_data_size;
253 u64 approx_total_size, max_seg_size =
254 ((u64) 1 << 32) - clib_mem_get_page_size ();
255 segment_manager_properties_t *props;
256 svm_fifo_segment_private_t *segment;
257 u32 approx_segment_count;
258 int seg_index, i;
259
260 props = segment_manager_properties_get (sm);
261 first_seg_size = clib_max (first_seg_size, default_segment_size);
262
263 if (prealloc_fifo_pairs)
264 {
265 /* Figure out how many segments should be preallocated */
266 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
267 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
268
269 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
270 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
271 pair_size = rx_fifo_size + tx_fifo_size;
272
273 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
274 if (first_seg_size > approx_total_size)
275 max_seg_size = first_seg_size;
276 approx_segment_count = (approx_total_size + (max_seg_size - 1))
277 / max_seg_size;
278
279 /* Allocate the segments */
280 for (i = 0; i < approx_segment_count + 1; i++)
281 {
282 seg_index = segment_manager_add_segment (sm, max_seg_size);
283 if (seg_index < 0)
284 {
285 clib_warning ("Failed to preallocate segment %d", i);
286 return seg_index;
287 }
288
Florin Corasa332c462018-01-31 06:52:17 -0800289 segment = segment_manager_get_segment (sm, seg_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800290 if (i == 0)
291 sm->event_queue = segment_manager_alloc_queue (segment,
292 props->evt_q_size);
293
Florin Corasa332c462018-01-31 06:52:17 -0800294 svm_fifo_segment_preallocate_fifo_pairs (segment,
295 props->rx_fifo_size,
296 props->tx_fifo_size,
297 &prealloc_fifo_pairs);
298 svm_fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED;
299 if (prealloc_fifo_pairs == 0)
300 break;
301 }
302 }
303 else
304 {
305 seg_index = segment_manager_add_segment (sm, first_seg_size);
306 if (seg_index)
307 {
308 clib_warning ("Failed to allocate segment");
309 return seg_index;
310 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800311 segment = segment_manager_get_segment (sm, seg_index);
312 sm->event_queue = segment_manager_alloc_queue (segment,
313 props->evt_q_size);
Florin Corasa332c462018-01-31 06:52:17 -0800314 }
315
316 return 0;
317}
318
319u8
320segment_manager_has_fifos (segment_manager_t * sm)
321{
322 svm_fifo_segment_private_t *seg;
323 u8 first = 1;
324
325 /* *INDENT-OFF* */
326 segment_manager_foreach_segment_w_lock (seg, sm, ({
327 if (CLIB_DEBUG && !first && !svm_fifo_segment_has_fifos (seg)
328 && !(svm_fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
329 {
330 clib_warning ("segment %d has no fifos!",
331 segment_manager_segment_index (sm, seg));
332 first = 0;
333 }
334 if (svm_fifo_segment_has_fifos (seg))
335 {
336 segment_manager_segment_reader_unlock (sm);
337 return 1;
338 }
339 }));
340 /* *INDENT-ON* */
341
342 return 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700343}
344
345/**
346 * Initiate disconnects for all sessions 'owned' by a segment manager
Florin Coras6cf30ad2017-04-04 23:08:23 -0700347 */
348void
Florin Corasc87c91d2017-08-16 19:55:49 -0700349segment_manager_del_sessions (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700350{
Dave Wallace7b749fe2017-07-05 14:30:46 -0400351 svm_fifo_segment_private_t *fifo_segment;
Florin Corasa332c462018-01-31 06:52:17 -0800352 stream_session_t *session;
Florin Corasc87c91d2017-08-16 19:55:49 -0700353 svm_fifo_t *fifo;
354
Florin Corasa332c462018-01-31 06:52:17 -0800355 ASSERT (pool_elts (sm->segments) != 0);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700356
357 /* Across all fifo segments used by the server */
Florin Corasa332c462018-01-31 06:52:17 -0800358 /* *INDENT-OFF* */
359 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
360 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361
Florin Corasa332c462018-01-31 06:52:17 -0800362 /*
363 * Remove any residual sessions from the session lookup table
364 * Don't bother deleting the individual fifos, we're going to
365 * throw away the fifo segment in a minute.
366 */
367 while (fifo)
368 {
369 session = session_get (fifo->master_session_index,
370 fifo->master_thread_index);
371 stream_session_disconnect (session);
372 fifo = fifo->next;
373 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700374
Florin Corasa332c462018-01-31 06:52:17 -0800375 /* Instead of removing the segment, test when cleaning up disconnected
376 * sessions if the segment can be removed.
377 */
378 }));
379 /* *INDENT-ON* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700380}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700381
Florin Corasc87c91d2017-08-16 19:55:49 -0700382/**
383 * Removes segment manager.
384 *
385 * Since the fifos allocated in the segment keep backpointers to the sessions
386 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400387 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700388 */
389void
390segment_manager_del (segment_manager_t * sm)
391{
Florin Corasa332c462018-01-31 06:52:17 -0800392 segment_manager_main_t *smm = &segment_manager_main;
393 svm_fifo_segment_private_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400394
Florin Coras9d063042017-09-14 03:08:00 -0400395 ASSERT (!segment_manager_has_fifos (sm)
396 && segment_manager_app_detached (sm));
397
398 /* If we have empty preallocated segments that haven't been removed, remove
399 * them now. Apart from that, the first segment in the first segment manager
400 * is not removed when all fifos are removed. It can only be removed when
401 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800402 clib_rwlock_writer_lock (&sm->segments_rwlock);
403
404 /* *INDENT-OFF* */
405 pool_foreach (fifo_segment, sm->segments, ({
406 segment_manager_del_segment (sm, fifo_segment);
407 }));
408 /* *INDENT-ON* */
409
410 clib_rwlock_writer_unlock (&sm->segments_rwlock);
411
412 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700413 if (CLIB_DEBUG)
414 memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800415 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700416}
417
Florin Corasc87c91d2017-08-16 19:55:49 -0700418void
419segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700420{
Florin Coras4e4531e2017-11-06 23:27:56 -0800421 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700422 if (segment_manager_has_fifos (sm))
423 segment_manager_del_sessions (sm);
424 else
425 {
Florin Coras9d063042017-09-14 03:08:00 -0400426 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700427 segment_manager_del (sm);
428 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700429}
430
Florin Corasf8f516a2018-02-08 15:10:09 -0800431int
432segment_manager_try_alloc_fifos (svm_fifo_segment_private_t * fifo_segment,
433 u32 rx_fifo_size, u32 tx_fifo_size,
434 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800435{
436 rx_fifo_size = clib_max (rx_fifo_size, default_fifo_size);
437 *rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size,
438 FIFO_SEGMENT_RX_FREELIST);
439
440 tx_fifo_size = clib_max (tx_fifo_size, default_fifo_size);
441 *tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size,
442 FIFO_SEGMENT_TX_FREELIST);
443
444 if (*rx_fifo == 0)
445 {
446 /* This would be very odd, but handle it... */
447 if (*tx_fifo != 0)
448 {
449 svm_fifo_segment_free_fifo (fifo_segment, *tx_fifo,
450 FIFO_SEGMENT_TX_FREELIST);
451 *tx_fifo = 0;
452 }
453 return -1;
454 }
455 if (*tx_fifo == 0)
456 {
457 if (*rx_fifo != 0)
458 {
459 svm_fifo_segment_free_fifo (fifo_segment, *rx_fifo,
460 FIFO_SEGMENT_RX_FREELIST);
461 *rx_fifo = 0;
462 }
463 return -1;
464 }
465
466 return 0;
467}
468
Florin Coras6cf30ad2017-04-04 23:08:23 -0700469int
470segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Corasb384b542018-01-15 01:08:33 -0800471 svm_fifo_t ** rx_fifo,
472 svm_fifo_t ** tx_fifo,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700473 u32 * fifo_segment_index)
474{
Florin Corasf8f516a2018-02-08 15:10:09 -0800475 svm_fifo_segment_private_t *fifo_segment = 0;
Florin Corasa332c462018-01-31 06:52:17 -0800476 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Corasad0c77f2017-11-09 18:00:15 -0800477 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700478 u8 added_a_segment = 0;
Florin Corasa332c462018-01-31 06:52:17 -0800479 u32 sm_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700480
Florin Corasa332c462018-01-31 06:52:17 -0800481 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700482
Florin Corasa332c462018-01-31 06:52:17 -0800483 /*
484 * Find the first free segment to allocate the fifos in
485 */
Florin Corasa5464812017-04-19 13:00:05 -0700486
Florin Corasa332c462018-01-31 06:52:17 -0800487 /* *INDENT-OFF* */
488 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
Florin Corasf8f516a2018-02-08 15:10:09 -0800489 alloc_fail = segment_manager_try_alloc_fifos (fifo_segment,
490 props->rx_fifo_size,
491 props->tx_fifo_size,
492 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800493 /* Exit with lock held, drop it after notifying app */
494 if (!alloc_fail)
495 goto alloc_success;
496 }));
497 /* *INDENT-ON* */
498
499alloc_check:
500
501 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700502 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700503
Florin Corasa332c462018-01-31 06:52:17 -0800504 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700505
Florin Corasa332c462018-01-31 06:52:17 -0800506 ASSERT (rx_fifo && tx_fifo);
507 sm_index = segment_manager_index (sm);
508 (*tx_fifo)->segment_manager = sm_index;
509 (*rx_fifo)->segment_manager = sm_index;
510 *fifo_segment_index = segment_manager_segment_index (sm, fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700511
Florin Corasa332c462018-01-31 06:52:17 -0800512 if (added_a_segment)
513 rv = application_add_segment_notify (sm->app_index,
514 &fifo_segment->ssvm);
515 /* Drop the lock after app is notified */
516 segment_manager_segment_reader_unlock (sm);
517 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700518 }
519
Florin Corasa332c462018-01-31 06:52:17 -0800520 /*
521 * Allocation failed, see if we can add a new segment
522 */
523 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700524 {
Florin Corasa332c462018-01-31 06:52:17 -0800525 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700526 {
Florin Corasa332c462018-01-31 06:52:17 -0800527 clib_warning ("Added a segment, still can't allocate a fifo");
528 segment_manager_segment_reader_unlock (sm);
529 return SESSION_ERROR_NEW_SEG_NO_SPACE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700530 }
Florin Corasa332c462018-01-31 06:52:17 -0800531 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700532 {
Florin Corasa332c462018-01-31 06:52:17 -0800533 clib_warning ("Failed to add new segment");
534 return SESSION_ERROR_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700535 }
Florin Corasa332c462018-01-31 06:52:17 -0800536 fifo_segment = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800537 alloc_fail = segment_manager_try_alloc_fifos (fifo_segment,
538 props->rx_fifo_size,
539 props->tx_fifo_size,
540 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800541 added_a_segment = 1;
542 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700543 }
Florin Corasa332c462018-01-31 06:52:17 -0800544 else
545 {
546 clib_warning ("Can't add new seg and no space to allocate fifos!");
547 return SESSION_ERROR_NO_SPACE;
548 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700549}
550
551void
Florin Corasa332c462018-01-31 06:52:17 -0800552segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700553 svm_fifo_t * tx_fifo)
554{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700555 svm_fifo_segment_private_t *fifo_segment;
Florin Corasa332c462018-01-31 06:52:17 -0800556 segment_manager_t *sm;
Florin Corasa5464812017-04-19 13:00:05 -0700557
558 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700559 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800560 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700561 return;
562
Florin Corasa332c462018-01-31 06:52:17 -0800563 fifo_segment = segment_manager_get_segment_w_lock (sm, segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400564 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
565 FIFO_SEGMENT_RX_FREELIST);
566 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
567 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700568
Florin Corasc87c91d2017-08-16 19:55:49 -0700569 /*
570 * Try to remove svm segment if it has no fifos. This can be done only if
571 * the segment is not the first in the segment manager or if it is first
572 * and it is not protected. Moreover, if the segment is first and the app
573 * has detached from the segment manager, remove the segment manager.
574 */
575 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700576 {
Florin Corasa332c462018-01-31 06:52:17 -0800577 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700578
579 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800580 if (segment_index != 0 || !sm->first_is_protected)
581 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700582
583 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400584 if (segment_manager_app_detached (sm)
585 && !segment_manager_has_fifos (sm))
Florin Corasc87c91d2017-08-16 19:55:49 -0700586 segment_manager_del (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700587 }
Florin Corasa332c462018-01-31 06:52:17 -0800588 else
589 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700590}
591
Florin Corasa5464812017-04-19 13:00:05 -0700592/**
593 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800594 *
595 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700596 */
Florin Corase86a8ed2018-01-05 03:20:25 -0800597svm_queue_t *
Florin Corasf8f516a2018-02-08 15:10:09 -0800598segment_manager_alloc_queue (svm_fifo_segment_private_t * segment,
599 u32 queue_size)
Florin Corasa5464812017-04-19 13:00:05 -0700600{
Florin Corasa332c462018-01-31 06:52:17 -0800601 ssvm_shared_header_t *sh;
Florin Corase86a8ed2018-01-05 03:20:25 -0800602 svm_queue_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700603 void *oldheap;
604
Florin Corasa5464812017-04-19 13:00:05 -0700605 sh = segment->ssvm.sh;
606
607 oldheap = ssvm_push_heap (sh);
Florin Corasa332c462018-01-31 06:52:17 -0800608 q = svm_queue_init (queue_size, sizeof (session_fifo_event_t),
609 0 /* consumer pid */ ,
Florin Corase86a8ed2018-01-05 03:20:25 -0800610 0 /* signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700611 ssvm_pop_heap (oldheap);
612 return q;
613}
614
615/**
616 * Frees shm queue allocated in the first segment
617 */
618void
Florin Corase86a8ed2018-01-05 03:20:25 -0800619segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700620{
Florin Corasa5464812017-04-19 13:00:05 -0700621 svm_fifo_segment_private_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800622 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700623 void *oldheap;
624
Florin Corasa332c462018-01-31 06:52:17 -0800625 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700626
Florin Corasa332c462018-01-31 06:52:17 -0800627 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700628 sh = segment->ssvm.sh;
629
630 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800631 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700632 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800633 segment_manager_segment_reader_unlock (sm);
634}
635
636/*
637 * Init segment vm address allocator
638 */
639void
640segment_manager_main_init (segment_manager_main_init_args_t * a)
641{
642 segment_manager_main_t *sm = &segment_manager_main;
643 clib_valloc_chunk_t _ip, *ip = &_ip;
644
645 ip->baseva = a->baseva;
646 ip->size = a->size;
647
648 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Corasa5464812017-04-19 13:00:05 -0700649}
650
Florin Corasc87c91d2017-08-16 19:55:49 -0700651static clib_error_t *
652segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
653 vlib_cli_command_t * cmd)
654{
Florin Corasa332c462018-01-31 06:52:17 -0800655 segment_manager_main_t *smm = &segment_manager_main;
656 svm_fifo_segment_private_t *seg;
Florin Corasc87c91d2017-08-16 19:55:49 -0700657 segment_manager_t *sm;
Florin Corasb384b542018-01-15 01:08:33 -0800658 u8 show_segments = 0, verbose = 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700659 uword address;
660 u64 size;
Dave Barach91f3e742017-09-01 19:12:11 -0400661 u32 active_fifos;
662 u32 free_fifos;
663
Florin Corasc87c91d2017-08-16 19:55:49 -0700664 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
665 {
666 if (unformat (input, "segments"))
667 show_segments = 1;
668 else if (unformat (input, "verbose"))
669 verbose = 1;
670 else
671 return clib_error_return (0, "unknown input `%U'",
672 format_unformat_error, input);
673 }
674 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800675 pool_elts (smm->segment_managers));
676 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700677 {
678 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
679 "Segments");
680
681 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800682 pool_foreach (sm, smm->segment_managers, ({
Florin Corasc87c91d2017-08-16 19:55:49 -0700683 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm),
Florin Corasa332c462018-01-31 06:52:17 -0800684 sm->app_index, pool_elts (sm->segments));
Florin Corasc87c91d2017-08-16 19:55:49 -0700685 }));
686 /* *INDENT-ON* */
687
688 }
689 if (show_segments)
690 {
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800691 vlib_cli_output (vm, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
Dave Barach91f3e742017-09-01 19:12:11 -0400692 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700693
694 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800695 pool_foreach (sm, smm->segment_managers, ({
696 segment_manager_foreach_segment_w_lock (seg, sm, ({
697 svm_fifo_segment_info (seg, &address, &size);
698 active_fifos = svm_fifo_segment_num_fifos (seg);
699 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
700 vlib_cli_output (vm, "%-15v%15U%15llu%15u%15u%15llx",
701 ssvm_name (&seg->ssvm), format_svm_fifo_segment_type,
702 seg, size >> 20ULL, active_fifos, free_fifos,
703 address);
704 if (verbose)
705 vlib_cli_output (vm, "%U", format_svm_fifo_segment, seg, verbose);
706 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700707 }));
708 /* *INDENT-ON* */
709
710 }
711 return 0;
712}
713
Florin Corasad0c77f2017-11-09 18:00:15 -0800714/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700715VLIB_CLI_COMMAND (segment_manager_show_command, static) =
716{
717 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400718 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700719 .function = segment_manager_show_fn,
720};
721/* *INDENT-ON* */
722
Florin Coras6cf30ad2017-04-04 23:08:23 -0700723/*
724 * fd.io coding-style-patch-verification: ON
725 *
726 * Local Variables:
727 * eval: (c-set-style "gnu")
728 * End:
729 */