blob: bf62040271f09b9b36492c0ea7be4e4d511c36db [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 Corasab2f6db2018-08-31 14:31:41 -070037 app_worker_t *app_wrk = app_worker_get (sm->app_wrk_index);
38 return application_get_segment_manager_properties (app_wrk->app_index);
Florin Corasa332c462018-01-31 06:52:17 -080039}
40
41segment_manager_properties_t *
42segment_manager_properties_init (segment_manager_properties_t * props)
43{
Florin Corasb384b542018-01-15 01:08:33 -080044 props->add_segment_size = default_segment_size;
45 props->rx_fifo_size = default_fifo_size;
46 props->tx_fifo_size = default_fifo_size;
Florin Corasf8f516a2018-02-08 15:10:09 -080047 props->evt_q_size = default_app_evt_queue_size;
Florin Corasad0c77f2017-11-09 18:00:15 -080048 return props;
49}
50
Florin Coras9d063042017-09-14 03:08:00 -040051static u8
52segment_manager_app_detached (segment_manager_t * sm)
53{
Florin Coras15531972018-08-12 23:50:53 -070054 return (sm->app_wrk_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -040055}
56
Florin Coras4e4531e2017-11-06 23:27:56 -080057void
58segment_manager_app_detach (segment_manager_t * sm)
59{
Florin Coras15531972018-08-12 23:50:53 -070060 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras4e4531e2017-11-06 23:27:56 -080061}
62
Florin Corasa332c462018-01-31 06:52:17 -080063always_inline u32
64segment_manager_segment_index (segment_manager_t * sm,
65 svm_fifo_segment_private_t * seg)
Florin Corasc87c91d2017-08-16 19:55:49 -070066{
Florin Corasa332c462018-01-31 06:52:17 -080067 return (seg - sm->segments);
68}
69
70/**
71 * Remove segment without lock
72 */
Florin Corasf8f516a2018-02-08 15:10:09 -080073void
Florin Corasa332c462018-01-31 06:52:17 -080074segment_manager_del_segment (segment_manager_t * sm,
75 svm_fifo_segment_private_t * fs)
76{
77 segment_manager_main_t *smm = &segment_manager_main;
78
79 if (ssvm_type (&fs->ssvm) != SSVM_SEGMENT_PRIVATE)
80 clib_valloc_free (&smm->va_allocator, fs->ssvm.requested_va);
81
82 ssvm_delete (&fs->ssvm);
83
84 if (CLIB_DEBUG)
85 memset (fs, 0xfb, sizeof (*fs));
86 pool_put (sm->segments, fs);
87}
88
89/**
90 * Removes segment after acquiring writer lock
91 */
Florin Coras99368312018-08-02 10:45:44 -070092static inline void
Florin Corasa332c462018-01-31 06:52:17 -080093segment_manager_lock_and_del_segment (segment_manager_t * sm, u32 fs_index)
94{
95 svm_fifo_segment_private_t *fs;
96 u8 is_prealloc;
97
98 clib_rwlock_writer_lock (&sm->segments_rwlock);
99 fs = segment_manager_get_segment (sm, fs_index);
100 is_prealloc = svm_fifo_segment_flags (fs) & FIFO_SEGMENT_F_IS_PREALLOCATED;
101 if (is_prealloc && !segment_manager_app_detached (sm))
Florin Coras9d063042017-09-14 03:08:00 -0400102 {
Florin Corasa332c462018-01-31 06:52:17 -0800103 clib_rwlock_writer_unlock (&sm->segments_rwlock);
Florin Coras9d063042017-09-14 03:08:00 -0400104 return;
105 }
Florin Corasa332c462018-01-31 06:52:17 -0800106
107 segment_manager_del_segment (sm, fs);
108 clib_rwlock_writer_unlock (&sm->segments_rwlock);
109}
110
111/**
112 * Reads a segment from the segment manager's pool without lock
113 */
114svm_fifo_segment_private_t *
115segment_manager_get_segment (segment_manager_t * sm, u32 segment_index)
116{
117 return pool_elt_at_index (sm->segments, segment_index);
118}
119
120/**
121 * Reads a segment from the segment manager's pool and acquires reader lock
122 *
123 * Caller must drop the reader's lock by calling
124 * @ref segment_manager_segment_reader_unlock once it finishes working with
125 * the segment.
126 */
127svm_fifo_segment_private_t *
128segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
129{
130 clib_rwlock_reader_lock (&sm->segments_rwlock);
131 return pool_elt_at_index (sm->segments, segment_index);
132}
133
134void
135segment_manager_segment_reader_unlock (segment_manager_t * sm)
136{
Florin Corasf8f516a2018-02-08 15:10:09 -0800137 ASSERT (sm->segments_rwlock->n_readers > 0);
Florin Corasa332c462018-01-31 06:52:17 -0800138 clib_rwlock_reader_unlock (&sm->segments_rwlock);
139}
140
141void
142segment_manager_segment_writer_unlock (segment_manager_t * sm)
143{
144 clib_rwlock_writer_unlock (&sm->segments_rwlock);
145}
146
147/**
148 * Adds segment to segment manager's pool
149 *
150 * If needed a writer's lock is acquired before allocating a new segment
151 * to avoid affecting any of the segments pool readers.
152 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800153int
Florin Corasa332c462018-01-31 06:52:17 -0800154segment_manager_add_segment (segment_manager_t * sm, u32 segment_size)
155{
156 segment_manager_main_t *smm = &segment_manager_main;
Florin Coras5da96a72018-07-12 01:45:13 -0700157 u32 rnd_margin = 128 << 10, seg_index, page_size;
Florin Corasa332c462018-01-31 06:52:17 -0800158 segment_manager_properties_t *props;
159 uword baseva = (u64) ~ 0, alloc_size;
160 svm_fifo_segment_private_t *seg;
161 u8 *seg_name;
162 int rv;
163
164 props = segment_manager_properties_get (sm);
165
166 /* Not configured for addition of new segments and not first */
167 if (!props->add_segment && !segment_size)
168 {
169 clib_warning ("cannot allocate new segment");
170 return VNET_API_ERROR_INVALID_VALUE;
171 }
172
173 /*
174 * Allocate fifo segment and lock if needed
175 */
176 if (vlib_num_workers ())
177 {
178 clib_rwlock_writer_lock (&sm->segments_rwlock);
179 pool_get (sm->segments, seg);
180 }
181 else
182 {
183 pool_get (sm->segments, seg);
184 }
185 memset (seg, 0, sizeof (*seg));
186
187 /*
188 * Initialize ssvm segment and svm fifo private header
189 */
190 segment_size = segment_size ? segment_size : props->add_segment_size;
Florin Coras5da96a72018-07-12 01:45:13 -0700191 page_size = clib_mem_get_page_size ();
192 segment_size = (segment_size + page_size - 1) & ~(page_size - 1);
Florin Corasa332c462018-01-31 06:52:17 -0800193 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
194 {
195 seg_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
Florin Coras86f04502018-09-12 16:08:01 -0700196 alloc_size = (uword) segment_size + rnd_margin;
Florin Corasa332c462018-01-31 06:52:17 -0800197 baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
198 if (!baseva)
199 {
200 clib_warning ("out of space for segments");
201 return -1;
202 }
203 }
204 else
205 seg_name = format (0, "%s%c", "process-private-segment", 0);
206
207 seg->ssvm.ssvm_size = segment_size;
208 seg->ssvm.name = seg_name;
209 seg->ssvm.requested_va = baseva;
210
211 if ((rv = ssvm_master_init (&seg->ssvm, props->segment_type)))
212 {
213 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
214 segment_size);
215
216 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
217 clib_valloc_free (&smm->va_allocator, baseva);
218 pool_put (sm->segments, seg);
219 return (rv);
220 }
221
222 svm_fifo_segment_init (seg);
223
224 /*
225 * Save segment index before dropping lock, if any held
226 */
227 seg_index = seg - sm->segments;
228
229 if (vlib_num_workers ())
230 clib_rwlock_writer_unlock (&sm->segments_rwlock);
231
232 return seg_index;
233}
234
235segment_manager_t *
236segment_manager_new ()
237{
238 segment_manager_main_t *smm = &segment_manager_main;
239 segment_manager_t *sm;
240 pool_get (smm->segment_managers, sm);
241 memset (sm, 0, sizeof (*sm));
242 clib_rwlock_init (&sm->segments_rwlock);
243 return sm;
244}
245
246/**
247 * Initializes segment manager based on options provided.
248 * Returns error if ssvm segment(s) allocation fails.
249 */
250int
251segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800252 u32 prealloc_fifo_pairs)
Florin Corasa332c462018-01-31 06:52:17 -0800253{
254 u32 rx_fifo_size, tx_fifo_size, pair_size;
255 u32 rx_rounded_data_size, tx_rounded_data_size;
Florin Coras86f04502018-09-12 16:08:01 -0700256 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
Florin Corasa332c462018-01-31 06:52:17 -0800257 segment_manager_properties_t *props;
258 svm_fifo_segment_private_t *segment;
259 u32 approx_segment_count;
260 int seg_index, i;
261
262 props = segment_manager_properties_get (sm);
263 first_seg_size = clib_max (first_seg_size, default_segment_size);
264
265 if (prealloc_fifo_pairs)
266 {
267 /* Figure out how many segments should be preallocated */
268 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
269 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
270
271 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
272 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
273 pair_size = rx_fifo_size + tx_fifo_size;
274
275 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
276 if (first_seg_size > approx_total_size)
277 max_seg_size = first_seg_size;
278 approx_segment_count = (approx_total_size + (max_seg_size - 1))
279 / max_seg_size;
280
281 /* Allocate the segments */
282 for (i = 0; i < approx_segment_count + 1; i++)
283 {
284 seg_index = segment_manager_add_segment (sm, max_seg_size);
285 if (seg_index < 0)
286 {
287 clib_warning ("Failed to preallocate segment %d", i);
288 return seg_index;
289 }
290
Florin Corasa332c462018-01-31 06:52:17 -0800291 segment = segment_manager_get_segment (sm, seg_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800292 if (i == 0)
Florin Coras99368312018-08-02 10:45:44 -0700293 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800294
Florin Corasa332c462018-01-31 06:52:17 -0800295 svm_fifo_segment_preallocate_fifo_pairs (segment,
296 props->rx_fifo_size,
297 props->tx_fifo_size,
298 &prealloc_fifo_pairs);
299 svm_fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED;
300 if (prealloc_fifo_pairs == 0)
301 break;
302 }
303 }
304 else
305 {
306 seg_index = segment_manager_add_segment (sm, first_seg_size);
307 if (seg_index)
308 {
309 clib_warning ("Failed to allocate segment");
310 return seg_index;
311 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800312 segment = segment_manager_get_segment (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -0700313 sm->event_queue = segment_manager_alloc_queue (segment, props);
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 {
Florin Corasf6647e02018-03-23 22:56:43 -0700369 if (fifo->master_thread_index == 255)
370 {
371 svm_fifo_t *next = fifo->next;
Florin Coras15531972018-08-12 23:50:53 -0700372 application_local_session_disconnect_w_index (sm->app_wrk_index,
Florin Corasf6647e02018-03-23 22:56:43 -0700373 fifo->master_session_index);
374 fifo = next;
375 continue;
376 }
Florin Corasa332c462018-01-31 06:52:17 -0800377 session = session_get (fifo->master_session_index,
378 fifo->master_thread_index);
379 stream_session_disconnect (session);
380 fifo = fifo->next;
381 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700382
Florin Corasa332c462018-01-31 06:52:17 -0800383 /* Instead of removing the segment, test when cleaning up disconnected
384 * sessions if the segment can be removed.
385 */
386 }));
387 /* *INDENT-ON* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700388}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700389
Florin Corasc87c91d2017-08-16 19:55:49 -0700390/**
391 * Removes segment manager.
392 *
393 * Since the fifos allocated in the segment keep backpointers to the sessions
394 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400395 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700396 */
397void
398segment_manager_del (segment_manager_t * sm)
399{
Florin Corasa332c462018-01-31 06:52:17 -0800400 segment_manager_main_t *smm = &segment_manager_main;
401 svm_fifo_segment_private_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400402
Florin Coras9d063042017-09-14 03:08:00 -0400403 ASSERT (!segment_manager_has_fifos (sm)
404 && segment_manager_app_detached (sm));
405
406 /* If we have empty preallocated segments that haven't been removed, remove
407 * them now. Apart from that, the first segment in the first segment manager
408 * is not removed when all fifos are removed. It can only be removed when
409 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800410 clib_rwlock_writer_lock (&sm->segments_rwlock);
411
412 /* *INDENT-OFF* */
413 pool_foreach (fifo_segment, sm->segments, ({
414 segment_manager_del_segment (sm, fifo_segment);
415 }));
416 /* *INDENT-ON* */
417
418 clib_rwlock_writer_unlock (&sm->segments_rwlock);
419
420 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700421 if (CLIB_DEBUG)
422 memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800423 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700424}
425
Florin Corasc87c91d2017-08-16 19:55:49 -0700426void
427segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700428{
Florin Coras4e4531e2017-11-06 23:27:56 -0800429 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700430 if (segment_manager_has_fifos (sm))
431 segment_manager_del_sessions (sm);
432 else
433 {
Florin Coras9d063042017-09-14 03:08:00 -0400434 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700435 segment_manager_del (sm);
436 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700437}
438
Florin Corasf8f516a2018-02-08 15:10:09 -0800439int
440segment_manager_try_alloc_fifos (svm_fifo_segment_private_t * fifo_segment,
441 u32 rx_fifo_size, u32 tx_fifo_size,
442 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800443{
444 rx_fifo_size = clib_max (rx_fifo_size, default_fifo_size);
445 *rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size,
446 FIFO_SEGMENT_RX_FREELIST);
447
448 tx_fifo_size = clib_max (tx_fifo_size, default_fifo_size);
449 *tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size,
450 FIFO_SEGMENT_TX_FREELIST);
451
452 if (*rx_fifo == 0)
453 {
454 /* This would be very odd, but handle it... */
455 if (*tx_fifo != 0)
456 {
457 svm_fifo_segment_free_fifo (fifo_segment, *tx_fifo,
458 FIFO_SEGMENT_TX_FREELIST);
459 *tx_fifo = 0;
460 }
461 return -1;
462 }
463 if (*tx_fifo == 0)
464 {
465 if (*rx_fifo != 0)
466 {
467 svm_fifo_segment_free_fifo (fifo_segment, *rx_fifo,
468 FIFO_SEGMENT_RX_FREELIST);
469 *rx_fifo = 0;
470 }
471 return -1;
472 }
473
474 return 0;
475}
476
Florin Coras6cf30ad2017-04-04 23:08:23 -0700477int
478segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Corasb384b542018-01-15 01:08:33 -0800479 svm_fifo_t ** rx_fifo,
480 svm_fifo_t ** tx_fifo,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481 u32 * fifo_segment_index)
482{
Florin Corasf8f516a2018-02-08 15:10:09 -0800483 svm_fifo_segment_private_t *fifo_segment = 0;
Florin Corasa332c462018-01-31 06:52:17 -0800484 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Corasad0c77f2017-11-09 18:00:15 -0800485 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700486 u8 added_a_segment = 0;
Florin Corasa332c462018-01-31 06:52:17 -0800487 u32 sm_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700488
Florin Corasa332c462018-01-31 06:52:17 -0800489 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700490
Florin Corasa332c462018-01-31 06:52:17 -0800491 /*
492 * Find the first free segment to allocate the fifos in
493 */
Florin Corasa5464812017-04-19 13:00:05 -0700494
Florin Corasa332c462018-01-31 06:52:17 -0800495 /* *INDENT-OFF* */
496 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
Florin Corasf8f516a2018-02-08 15:10:09 -0800497 alloc_fail = segment_manager_try_alloc_fifos (fifo_segment,
498 props->rx_fifo_size,
499 props->tx_fifo_size,
500 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800501 /* Exit with lock held, drop it after notifying app */
502 if (!alloc_fail)
503 goto alloc_success;
504 }));
505 /* *INDENT-ON* */
506
507alloc_check:
508
509 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700510 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700511
Florin Corasa332c462018-01-31 06:52:17 -0800512 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700513
Florin Corasa332c462018-01-31 06:52:17 -0800514 ASSERT (rx_fifo && tx_fifo);
515 sm_index = segment_manager_index (sm);
516 (*tx_fifo)->segment_manager = sm_index;
517 (*rx_fifo)->segment_manager = sm_index;
518 *fifo_segment_index = segment_manager_segment_index (sm, fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700519
Florin Corasa332c462018-01-31 06:52:17 -0800520 if (added_a_segment)
Florin Coras15531972018-08-12 23:50:53 -0700521 rv = app_worker_add_segment_notify (sm->app_wrk_index,
522 &fifo_segment->ssvm);
Florin Corasa332c462018-01-31 06:52:17 -0800523 /* Drop the lock after app is notified */
524 segment_manager_segment_reader_unlock (sm);
525 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700526 }
527
Florin Corasa332c462018-01-31 06:52:17 -0800528 /*
529 * Allocation failed, see if we can add a new segment
530 */
531 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700532 {
Florin Corasa332c462018-01-31 06:52:17 -0800533 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700534 {
Florin Corasa332c462018-01-31 06:52:17 -0800535 clib_warning ("Added a segment, still can't allocate a fifo");
536 segment_manager_segment_reader_unlock (sm);
537 return SESSION_ERROR_NEW_SEG_NO_SPACE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700538 }
Florin Corasa332c462018-01-31 06:52:17 -0800539 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700540 {
Florin Corasa332c462018-01-31 06:52:17 -0800541 clib_warning ("Failed to add new segment");
542 return SESSION_ERROR_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700543 }
Florin Corasa332c462018-01-31 06:52:17 -0800544 fifo_segment = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800545 alloc_fail = segment_manager_try_alloc_fifos (fifo_segment,
546 props->rx_fifo_size,
547 props->tx_fifo_size,
548 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800549 added_a_segment = 1;
550 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700551 }
Florin Corasa332c462018-01-31 06:52:17 -0800552 else
553 {
554 clib_warning ("Can't add new seg and no space to allocate fifos!");
555 return SESSION_ERROR_NO_SPACE;
556 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700557}
558
559void
Florin Corasa332c462018-01-31 06:52:17 -0800560segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700561 svm_fifo_t * tx_fifo)
562{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563 svm_fifo_segment_private_t *fifo_segment;
Florin Corasa332c462018-01-31 06:52:17 -0800564 segment_manager_t *sm;
Florin Corasa5464812017-04-19 13:00:05 -0700565
566 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700567 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800568 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700569 return;
570
Florin Corasa332c462018-01-31 06:52:17 -0800571 fifo_segment = segment_manager_get_segment_w_lock (sm, segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400572 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
573 FIFO_SEGMENT_RX_FREELIST);
574 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
575 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700576
Florin Corasc87c91d2017-08-16 19:55:49 -0700577 /*
578 * Try to remove svm segment if it has no fifos. This can be done only if
579 * the segment is not the first in the segment manager or if it is first
580 * and it is not protected. Moreover, if the segment is first and the app
581 * has detached from the segment manager, remove the segment manager.
582 */
583 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700584 {
Florin Corasa332c462018-01-31 06:52:17 -0800585 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700586
587 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800588 if (segment_index != 0 || !sm->first_is_protected)
589 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700590
591 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400592 if (segment_manager_app_detached (sm)
593 && !segment_manager_has_fifos (sm))
Florin Coras568ebc72018-09-18 16:12:50 -0700594 {
595 segment_manager_del (sm);
596 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597 }
Florin Corasa332c462018-01-31 06:52:17 -0800598 else
599 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700600}
601
Florin Coras3c2fed52018-07-04 04:15:05 -0700602u32
603segment_manager_evt_q_expected_size (u32 q_len)
604{
605 u32 fifo_evt_size, notif_q_size, q_hdrs;
606 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
607
Florin Coras52207f12018-07-12 14:48:06 -0700608 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700609 notif_q_size = clib_max (16, q_len >> 4);
610
611 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
612 fifo_evt_ring_sz = q_len * fifo_evt_size;
613 session_ntf_ring_sz = notif_q_size * 256;
614 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
615
616 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
617}
618
Florin Corasa5464812017-04-19 13:00:05 -0700619/**
620 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800621 *
622 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700623 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700624svm_msg_q_t *
Florin Corasf8f516a2018-02-08 15:10:09 -0800625segment_manager_alloc_queue (svm_fifo_segment_private_t * segment,
Florin Coras99368312018-08-02 10:45:44 -0700626 segment_manager_properties_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700627{
Florin Coras3c2fed52018-07-04 04:15:05 -0700628 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
629 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
630 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700631 void *oldheap;
632
Florin Coras52207f12018-07-12 14:48:06 -0700633 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700634 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700635 /* *INDENT-OFF* */
636 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700637 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700638 {notif_q_size, session_evt_size, 0}
639 };
640 /* *INDENT-ON* */
641 cfg->consumer_pid = 0;
642 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700643 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700644 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700645
Florin Coras3c2fed52018-07-04 04:15:05 -0700646 oldheap = ssvm_push_heap (segment->ssvm.sh);
647 q = svm_msg_q_alloc (cfg);
Florin Corasa5464812017-04-19 13:00:05 -0700648 ssvm_pop_heap (oldheap);
Florin Coras99368312018-08-02 10:45:44 -0700649
650 if (props->use_mq_eventfd)
651 {
652 if (svm_msg_q_alloc_producer_eventfd (q))
653 clib_warning ("failed to alloc eventfd");
654 }
Florin Corasa5464812017-04-19 13:00:05 -0700655 return q;
656}
657
658/**
659 * Frees shm queue allocated in the first segment
660 */
661void
Florin Corase86a8ed2018-01-05 03:20:25 -0800662segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700663{
Florin Corasa5464812017-04-19 13:00:05 -0700664 svm_fifo_segment_private_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800665 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700666 void *oldheap;
667
Florin Corasa332c462018-01-31 06:52:17 -0800668 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700669
Florin Corasa332c462018-01-31 06:52:17 -0800670 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700671 sh = segment->ssvm.sh;
672
673 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800674 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700675 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800676 segment_manager_segment_reader_unlock (sm);
677}
678
679/*
680 * Init segment vm address allocator
681 */
682void
683segment_manager_main_init (segment_manager_main_init_args_t * a)
684{
685 segment_manager_main_t *sm = &segment_manager_main;
686 clib_valloc_chunk_t _ip, *ip = &_ip;
687
688 ip->baseva = a->baseva;
689 ip->size = a->size;
690
691 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Corasa5464812017-04-19 13:00:05 -0700692}
693
Florin Corasc87c91d2017-08-16 19:55:49 -0700694static clib_error_t *
695segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
696 vlib_cli_command_t * cmd)
697{
Florin Corasa332c462018-01-31 06:52:17 -0800698 segment_manager_main_t *smm = &segment_manager_main;
699 svm_fifo_segment_private_t *seg;
Florin Corasc87c91d2017-08-16 19:55:49 -0700700 segment_manager_t *sm;
Florin Corasb384b542018-01-15 01:08:33 -0800701 u8 show_segments = 0, verbose = 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700702 uword address;
703 u64 size;
Dave Barach91f3e742017-09-01 19:12:11 -0400704 u32 active_fifos;
705 u32 free_fifos;
706
Florin Corasc87c91d2017-08-16 19:55:49 -0700707 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
708 {
709 if (unformat (input, "segments"))
710 show_segments = 1;
711 else if (unformat (input, "verbose"))
712 verbose = 1;
713 else
714 return clib_error_return (0, "unknown input `%U'",
715 format_unformat_error, input);
716 }
717 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800718 pool_elts (smm->segment_managers));
719 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700720 {
721 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
722 "Segments");
723
724 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800725 pool_foreach (sm, smm->segment_managers, ({
Florin Coras15531972018-08-12 23:50:53 -0700726 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm),
727 sm->app_wrk_index, pool_elts (sm->segments));
Florin Corasc87c91d2017-08-16 19:55:49 -0700728 }));
729 /* *INDENT-ON* */
730
731 }
732 if (show_segments)
733 {
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800734 vlib_cli_output (vm, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
Dave Barach91f3e742017-09-01 19:12:11 -0400735 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700736
737 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800738 pool_foreach (sm, smm->segment_managers, ({
739 segment_manager_foreach_segment_w_lock (seg, sm, ({
740 svm_fifo_segment_info (seg, &address, &size);
741 active_fifos = svm_fifo_segment_num_fifos (seg);
742 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
743 vlib_cli_output (vm, "%-15v%15U%15llu%15u%15u%15llx",
744 ssvm_name (&seg->ssvm), format_svm_fifo_segment_type,
745 seg, size >> 20ULL, active_fifos, free_fifos,
746 address);
747 if (verbose)
748 vlib_cli_output (vm, "%U", format_svm_fifo_segment, seg, verbose);
749 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700750 }));
751 /* *INDENT-ON* */
752
753 }
754 return 0;
755}
756
Florin Corasad0c77f2017-11-09 18:00:15 -0800757/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700758VLIB_CLI_COMMAND (segment_manager_show_command, static) =
759{
760 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400761 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700762 .function = segment_manager_show_fn,
763};
764/* *INDENT-ON* */
765
Florin Coras6cf30ad2017-04-04 23:08:23 -0700766/*
767 * fd.io coding-style-patch-verification: ON
768 *
769 * Local Variables:
770 * eval: (c-set-style "gnu")
771 * End:
772 */