blob: 638f078e20dada7a6ad1b88e1016b23552101331 [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 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)
Dave Barachb7b92992018-10-17 10:38:51 -040085 clib_memset (fs, 0xfb, sizeof (*fs));
Florin Corasa332c462018-01-31 06:52:17 -080086 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
Florin Corasfa76a762018-11-29 12:40:10 -0800120u64
121segment_manager_segment_handle (segment_manager_t * sm,
122 svm_fifo_segment_private_t * segment)
123{
124 u32 segment_index = segment_manager_segment_index (sm, segment);
125 return (((u64) segment_manager_index (sm) << 32) | segment_index);
126}
127
128void
129segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index,
130 u32 * segment_index)
131{
132 *sm_index = segment_handle >> 32;
133 *segment_index = segment_handle & 0xFFFFFFFF;
134}
135
136svm_fifo_segment_private_t *
137segment_manager_get_segment_w_handle (u64 segment_handle)
138{
139 u32 sm_index, segment_index;
140 segment_manager_t *sm;
141
142 segment_manager_parse_segment_handle (segment_handle, &sm_index,
143 &segment_index);
144 sm = segment_manager_get (sm_index);
145 if (!sm || pool_is_free_index (sm->segments, segment_index))
146 return 0;
147 return pool_elt_at_index (sm->segments, segment_index);
148}
149
Florin Corasa332c462018-01-31 06:52:17 -0800150/**
151 * Reads a segment from the segment manager's pool and acquires reader lock
152 *
153 * Caller must drop the reader's lock by calling
154 * @ref segment_manager_segment_reader_unlock once it finishes working with
155 * the segment.
156 */
157svm_fifo_segment_private_t *
158segment_manager_get_segment_w_lock (segment_manager_t * sm, u32 segment_index)
159{
160 clib_rwlock_reader_lock (&sm->segments_rwlock);
161 return pool_elt_at_index (sm->segments, segment_index);
162}
163
164void
165segment_manager_segment_reader_unlock (segment_manager_t * sm)
166{
Florin Corasf8f516a2018-02-08 15:10:09 -0800167 ASSERT (sm->segments_rwlock->n_readers > 0);
Florin Corasa332c462018-01-31 06:52:17 -0800168 clib_rwlock_reader_unlock (&sm->segments_rwlock);
169}
170
171void
172segment_manager_segment_writer_unlock (segment_manager_t * sm)
173{
174 clib_rwlock_writer_unlock (&sm->segments_rwlock);
175}
176
177/**
178 * Adds segment to segment manager's pool
179 *
180 * If needed a writer's lock is acquired before allocating a new segment
181 * to avoid affecting any of the segments pool readers.
182 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800183int
Florin Corasa332c462018-01-31 06:52:17 -0800184segment_manager_add_segment (segment_manager_t * sm, u32 segment_size)
185{
186 segment_manager_main_t *smm = &segment_manager_main;
Florin Coras5da96a72018-07-12 01:45:13 -0700187 u32 rnd_margin = 128 << 10, seg_index, page_size;
Florin Corasa332c462018-01-31 06:52:17 -0800188 segment_manager_properties_t *props;
David Johnsond9818dd2018-12-14 14:53:41 -0500189 uword baseva = (uword) ~ 0ULL, alloc_size;
Florin Corasa332c462018-01-31 06:52:17 -0800190 svm_fifo_segment_private_t *seg;
191 u8 *seg_name;
192 int rv;
193
194 props = segment_manager_properties_get (sm);
195
196 /* Not configured for addition of new segments and not first */
197 if (!props->add_segment && !segment_size)
198 {
199 clib_warning ("cannot allocate new segment");
200 return VNET_API_ERROR_INVALID_VALUE;
201 }
202
203 /*
204 * Allocate fifo segment and lock if needed
205 */
206 if (vlib_num_workers ())
207 {
208 clib_rwlock_writer_lock (&sm->segments_rwlock);
209 pool_get (sm->segments, seg);
210 }
211 else
212 {
213 pool_get (sm->segments, seg);
214 }
Dave Barachb7b92992018-10-17 10:38:51 -0400215 clib_memset (seg, 0, sizeof (*seg));
Florin Corasa332c462018-01-31 06:52:17 -0800216
217 /*
218 * Initialize ssvm segment and svm fifo private header
219 */
220 segment_size = segment_size ? segment_size : props->add_segment_size;
Florin Coras5da96a72018-07-12 01:45:13 -0700221 page_size = clib_mem_get_page_size ();
222 segment_size = (segment_size + page_size - 1) & ~(page_size - 1);
Florin Corasa332c462018-01-31 06:52:17 -0800223 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
224 {
225 seg_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
Florin Coras86f04502018-09-12 16:08:01 -0700226 alloc_size = (uword) segment_size + rnd_margin;
Florin Corasa332c462018-01-31 06:52:17 -0800227 baseva = clib_valloc_alloc (&smm->va_allocator, alloc_size, 0);
228 if (!baseva)
229 {
230 clib_warning ("out of space for segments");
231 return -1;
232 }
233 }
234 else
235 seg_name = format (0, "%s%c", "process-private-segment", 0);
236
237 seg->ssvm.ssvm_size = segment_size;
238 seg->ssvm.name = seg_name;
239 seg->ssvm.requested_va = baseva;
240
241 if ((rv = ssvm_master_init (&seg->ssvm, props->segment_type)))
242 {
243 clib_warning ("svm_master_init ('%v', %u) failed", seg_name,
244 segment_size);
245
246 if (props->segment_type != SSVM_SEGMENT_PRIVATE)
247 clib_valloc_free (&smm->va_allocator, baseva);
248 pool_put (sm->segments, seg);
249 return (rv);
250 }
251
252 svm_fifo_segment_init (seg);
253
254 /*
255 * Save segment index before dropping lock, if any held
256 */
257 seg_index = seg - sm->segments;
258
259 if (vlib_num_workers ())
260 clib_rwlock_writer_unlock (&sm->segments_rwlock);
261
262 return seg_index;
263}
264
265segment_manager_t *
266segment_manager_new ()
267{
268 segment_manager_main_t *smm = &segment_manager_main;
269 segment_manager_t *sm;
270 pool_get (smm->segment_managers, sm);
Dave Barachb7b92992018-10-17 10:38:51 -0400271 clib_memset (sm, 0, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800272 clib_rwlock_init (&sm->segments_rwlock);
273 return sm;
274}
275
276/**
277 * Initializes segment manager based on options provided.
278 * Returns error if ssvm segment(s) allocation fails.
279 */
280int
281segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800282 u32 prealloc_fifo_pairs)
Florin Corasa332c462018-01-31 06:52:17 -0800283{
284 u32 rx_fifo_size, tx_fifo_size, pair_size;
285 u32 rx_rounded_data_size, tx_rounded_data_size;
Florin Coras86f04502018-09-12 16:08:01 -0700286 u64 approx_total_size, max_seg_size = ((u64) 1 << 32) - (128 << 10);
Florin Corasa332c462018-01-31 06:52:17 -0800287 segment_manager_properties_t *props;
288 svm_fifo_segment_private_t *segment;
289 u32 approx_segment_count;
290 int seg_index, i;
291
292 props = segment_manager_properties_get (sm);
293 first_seg_size = clib_max (first_seg_size, default_segment_size);
294
295 if (prealloc_fifo_pairs)
296 {
297 /* Figure out how many segments should be preallocated */
298 rx_rounded_data_size = (1 << (max_log2 (props->rx_fifo_size)));
299 tx_rounded_data_size = (1 << (max_log2 (props->tx_fifo_size)));
300
301 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
302 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
303 pair_size = rx_fifo_size + tx_fifo_size;
304
305 approx_total_size = (u64) prealloc_fifo_pairs *pair_size;
306 if (first_seg_size > approx_total_size)
307 max_seg_size = first_seg_size;
308 approx_segment_count = (approx_total_size + (max_seg_size - 1))
309 / max_seg_size;
310
311 /* Allocate the segments */
312 for (i = 0; i < approx_segment_count + 1; i++)
313 {
314 seg_index = segment_manager_add_segment (sm, max_seg_size);
315 if (seg_index < 0)
316 {
317 clib_warning ("Failed to preallocate segment %d", i);
318 return seg_index;
319 }
320
Florin Corasa332c462018-01-31 06:52:17 -0800321 segment = segment_manager_get_segment (sm, seg_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800322 if (i == 0)
Florin Coras99368312018-08-02 10:45:44 -0700323 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasf8f516a2018-02-08 15:10:09 -0800324
Florin Corasa332c462018-01-31 06:52:17 -0800325 svm_fifo_segment_preallocate_fifo_pairs (segment,
326 props->rx_fifo_size,
327 props->tx_fifo_size,
328 &prealloc_fifo_pairs);
329 svm_fifo_segment_flags (segment) = FIFO_SEGMENT_F_IS_PREALLOCATED;
330 if (prealloc_fifo_pairs == 0)
331 break;
332 }
333 }
334 else
335 {
336 seg_index = segment_manager_add_segment (sm, first_seg_size);
337 if (seg_index)
338 {
339 clib_warning ("Failed to allocate segment");
340 return seg_index;
341 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800342 segment = segment_manager_get_segment (sm, seg_index);
Florin Coras99368312018-08-02 10:45:44 -0700343 sm->event_queue = segment_manager_alloc_queue (segment, props);
Florin Corasa332c462018-01-31 06:52:17 -0800344 }
345
346 return 0;
347}
348
349u8
350segment_manager_has_fifos (segment_manager_t * sm)
351{
352 svm_fifo_segment_private_t *seg;
353 u8 first = 1;
354
355 /* *INDENT-OFF* */
356 segment_manager_foreach_segment_w_lock (seg, sm, ({
357 if (CLIB_DEBUG && !first && !svm_fifo_segment_has_fifos (seg)
358 && !(svm_fifo_segment_flags (seg) & FIFO_SEGMENT_F_IS_PREALLOCATED))
359 {
360 clib_warning ("segment %d has no fifos!",
361 segment_manager_segment_index (sm, seg));
362 first = 0;
363 }
364 if (svm_fifo_segment_has_fifos (seg))
365 {
366 segment_manager_segment_reader_unlock (sm);
367 return 1;
368 }
369 }));
370 /* *INDENT-ON* */
371
372 return 0;
Florin Corasc87c91d2017-08-16 19:55:49 -0700373}
374
375/**
376 * Initiate disconnects for all sessions 'owned' by a segment manager
Florin Coras6cf30ad2017-04-04 23:08:23 -0700377 */
378void
Florin Corasc87c91d2017-08-16 19:55:49 -0700379segment_manager_del_sessions (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380{
Dave Wallace7b749fe2017-07-05 14:30:46 -0400381 svm_fifo_segment_private_t *fifo_segment;
Florin Coras288eaab2019-02-03 15:26:14 -0800382 session_t *session;
Florin Corasc87c91d2017-08-16 19:55:49 -0700383 svm_fifo_t *fifo;
384
Florin Corasa332c462018-01-31 06:52:17 -0800385 ASSERT (pool_elts (sm->segments) != 0);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700386
387 /* Across all fifo segments used by the server */
Florin Corasa332c462018-01-31 06:52:17 -0800388 /* *INDENT-OFF* */
389 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
390 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391
Florin Corasa332c462018-01-31 06:52:17 -0800392 /*
393 * Remove any residual sessions from the session lookup table
394 * Don't bother deleting the individual fifos, we're going to
395 * throw away the fifo segment in a minute.
396 */
397 while (fifo)
398 {
Florin Coras326b81e2018-10-04 19:03:05 -0700399 if (fifo->ct_session_index != SVM_FIFO_INVALID_SESSION_INDEX)
Florin Corasf6647e02018-03-23 22:56:43 -0700400 {
401 svm_fifo_t *next = fifo->next;
Florin Coras623eb562019-02-03 19:28:34 -0800402 app_worker_local_session_disconnect_w_index (sm->app_wrk_index,
Florin Coras326b81e2018-10-04 19:03:05 -0700403 fifo->ct_session_index);
Florin Corasf6647e02018-03-23 22:56:43 -0700404 fifo = next;
405 continue;
406 }
Florin Corasa332c462018-01-31 06:52:17 -0800407 session = session_get (fifo->master_session_index,
408 fifo->master_thread_index);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800409 session_close (session);
Florin Corasa332c462018-01-31 06:52:17 -0800410 fifo = fifo->next;
411 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700412
Florin Corasa332c462018-01-31 06:52:17 -0800413 /* Instead of removing the segment, test when cleaning up disconnected
414 * sessions if the segment can be removed.
415 */
416 }));
417 /* *INDENT-ON* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700418}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700419
Florin Corasc87c91d2017-08-16 19:55:49 -0700420/**
421 * Removes segment manager.
422 *
423 * Since the fifos allocated in the segment keep backpointers to the sessions
424 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400425 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700426 */
427void
428segment_manager_del (segment_manager_t * sm)
429{
Florin Corasa332c462018-01-31 06:52:17 -0800430 segment_manager_main_t *smm = &segment_manager_main;
431 svm_fifo_segment_private_t *fifo_segment;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400432
Florin Coras9d063042017-09-14 03:08:00 -0400433 ASSERT (!segment_manager_has_fifos (sm)
434 && segment_manager_app_detached (sm));
435
436 /* If we have empty preallocated segments that haven't been removed, remove
437 * them now. Apart from that, the first segment in the first segment manager
438 * is not removed when all fifos are removed. It can only be removed when
439 * the manager is explicitly deleted/detached by the app. */
Florin Corasa332c462018-01-31 06:52:17 -0800440 clib_rwlock_writer_lock (&sm->segments_rwlock);
441
442 /* *INDENT-OFF* */
443 pool_foreach (fifo_segment, sm->segments, ({
444 segment_manager_del_segment (sm, fifo_segment);
445 }));
446 /* *INDENT-ON* */
447
448 clib_rwlock_writer_unlock (&sm->segments_rwlock);
449
450 clib_rwlock_free (&sm->segments_rwlock);
Florin Corasc87c91d2017-08-16 19:55:49 -0700451 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400452 clib_memset (sm, 0xfe, sizeof (*sm));
Florin Corasa332c462018-01-31 06:52:17 -0800453 pool_put (smm->segment_managers, sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700454}
455
Florin Corasc87c91d2017-08-16 19:55:49 -0700456void
457segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700458{
Florin Coras4e4531e2017-11-06 23:27:56 -0800459 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700460 if (segment_manager_has_fifos (sm))
461 segment_manager_del_sessions (sm);
462 else
463 {
Florin Coras9d063042017-09-14 03:08:00 -0400464 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700465 segment_manager_del (sm);
466 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700467}
468
Florin Corasf8f516a2018-02-08 15:10:09 -0800469int
470segment_manager_try_alloc_fifos (svm_fifo_segment_private_t * fifo_segment,
471 u32 rx_fifo_size, u32 tx_fifo_size,
472 svm_fifo_t ** rx_fifo, svm_fifo_t ** tx_fifo)
Florin Corasa332c462018-01-31 06:52:17 -0800473{
474 rx_fifo_size = clib_max (rx_fifo_size, default_fifo_size);
475 *rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, rx_fifo_size,
476 FIFO_SEGMENT_RX_FREELIST);
477
478 tx_fifo_size = clib_max (tx_fifo_size, default_fifo_size);
479 *tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, tx_fifo_size,
480 FIFO_SEGMENT_TX_FREELIST);
481
482 if (*rx_fifo == 0)
483 {
484 /* This would be very odd, but handle it... */
485 if (*tx_fifo != 0)
486 {
487 svm_fifo_segment_free_fifo (fifo_segment, *tx_fifo,
488 FIFO_SEGMENT_TX_FREELIST);
489 *tx_fifo = 0;
490 }
491 return -1;
492 }
493 if (*tx_fifo == 0)
494 {
495 if (*rx_fifo != 0)
496 {
497 svm_fifo_segment_free_fifo (fifo_segment, *rx_fifo,
498 FIFO_SEGMENT_RX_FREELIST);
499 *rx_fifo = 0;
500 }
501 return -1;
502 }
503
504 return 0;
505}
506
Florin Coras6cf30ad2017-04-04 23:08:23 -0700507int
508segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Corasb384b542018-01-15 01:08:33 -0800509 svm_fifo_t ** rx_fifo,
510 svm_fifo_t ** tx_fifo,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700511 u32 * fifo_segment_index)
512{
Florin Corasf8f516a2018-02-08 15:10:09 -0800513 svm_fifo_segment_private_t *fifo_segment = 0;
Florin Corasa332c462018-01-31 06:52:17 -0800514 int alloc_fail = 1, rv = 0, new_fs_index;
Florin Corasad0c77f2017-11-09 18:00:15 -0800515 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700516 u8 added_a_segment = 0;
Florin Corasfa76a762018-11-29 12:40:10 -0800517 u64 segment_handle;
Florin Corasa332c462018-01-31 06:52:17 -0800518 u32 sm_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700519
Florin Corasa332c462018-01-31 06:52:17 -0800520 props = segment_manager_properties_get (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700521
Florin Corasa332c462018-01-31 06:52:17 -0800522 /*
523 * Find the first free segment to allocate the fifos in
524 */
Florin Corasa5464812017-04-19 13:00:05 -0700525
Florin Corasa332c462018-01-31 06:52:17 -0800526 /* *INDENT-OFF* */
527 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
Florin Corasf8f516a2018-02-08 15:10:09 -0800528 alloc_fail = segment_manager_try_alloc_fifos (fifo_segment,
529 props->rx_fifo_size,
530 props->tx_fifo_size,
531 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800532 /* Exit with lock held, drop it after notifying app */
533 if (!alloc_fail)
534 goto alloc_success;
535 }));
536 /* *INDENT-ON* */
537
538alloc_check:
539
540 if (!alloc_fail)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700541 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700542
Florin Corasa332c462018-01-31 06:52:17 -0800543 alloc_success:
Florin Coras6cf30ad2017-04-04 23:08:23 -0700544
Florin Corasa332c462018-01-31 06:52:17 -0800545 ASSERT (rx_fifo && tx_fifo);
546 sm_index = segment_manager_index (sm);
Florin Corasfa76a762018-11-29 12:40:10 -0800547 *fifo_segment_index = segment_manager_segment_index (sm, fifo_segment);
Florin Corasa332c462018-01-31 06:52:17 -0800548 (*tx_fifo)->segment_manager = sm_index;
549 (*rx_fifo)->segment_manager = sm_index;
Florin Corasfa76a762018-11-29 12:40:10 -0800550 (*tx_fifo)->segment_index = *fifo_segment_index;
551 (*rx_fifo)->segment_index = *fifo_segment_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700552
Florin Corasa332c462018-01-31 06:52:17 -0800553 if (added_a_segment)
Florin Corasfa76a762018-11-29 12:40:10 -0800554 {
555 segment_handle = segment_manager_segment_handle (sm, fifo_segment);
556 rv = app_worker_add_segment_notify (sm->app_wrk_index,
557 segment_handle);
558 }
Florin Corasa332c462018-01-31 06:52:17 -0800559 /* Drop the lock after app is notified */
560 segment_manager_segment_reader_unlock (sm);
561 return rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700562 }
563
Florin Corasa332c462018-01-31 06:52:17 -0800564 /*
565 * Allocation failed, see if we can add a new segment
566 */
567 if (props->add_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700568 {
Florin Corasa332c462018-01-31 06:52:17 -0800569 if (added_a_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700570 {
Florin Corasa332c462018-01-31 06:52:17 -0800571 clib_warning ("Added a segment, still can't allocate a fifo");
572 segment_manager_segment_reader_unlock (sm);
573 return SESSION_ERROR_NEW_SEG_NO_SPACE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700574 }
Florin Corasa332c462018-01-31 06:52:17 -0800575 if ((new_fs_index = segment_manager_add_segment (sm, 0)) < 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700576 {
Florin Corasa332c462018-01-31 06:52:17 -0800577 clib_warning ("Failed to add new segment");
578 return SESSION_ERROR_SEG_CREATE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700579 }
Florin Corasa332c462018-01-31 06:52:17 -0800580 fifo_segment = segment_manager_get_segment_w_lock (sm, new_fs_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800581 alloc_fail = segment_manager_try_alloc_fifos (fifo_segment,
582 props->rx_fifo_size,
583 props->tx_fifo_size,
584 rx_fifo, tx_fifo);
Florin Corasa332c462018-01-31 06:52:17 -0800585 added_a_segment = 1;
586 goto alloc_check;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700587 }
Florin Corasa332c462018-01-31 06:52:17 -0800588 else
589 {
590 clib_warning ("Can't add new seg and no space to allocate fifos!");
591 return SESSION_ERROR_NO_SPACE;
592 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700593}
594
595void
Florin Corasa332c462018-01-31 06:52:17 -0800596segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700597 svm_fifo_t * tx_fifo)
598{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700599 svm_fifo_segment_private_t *fifo_segment;
Florin Corasa332c462018-01-31 06:52:17 -0800600 segment_manager_t *sm;
Florin Corasa5464812017-04-19 13:00:05 -0700601
Florin Coras58a93e82019-01-14 23:33:46 -0800602 if (!rx_fifo || !tx_fifo)
603 return;
604
Florin Corasa5464812017-04-19 13:00:05 -0700605 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700606 * as result of a detach. */
Florin Corasa332c462018-01-31 06:52:17 -0800607 if (!(sm = segment_manager_get_if_valid (rx_fifo->segment_manager)))
Florin Corasa5464812017-04-19 13:00:05 -0700608 return;
609
Florin Corasa332c462018-01-31 06:52:17 -0800610 fifo_segment = segment_manager_get_segment_w_lock (sm, segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400611 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
612 FIFO_SEGMENT_RX_FREELIST);
613 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
614 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700615
Florin Corasc87c91d2017-08-16 19:55:49 -0700616 /*
617 * Try to remove svm segment if it has no fifos. This can be done only if
618 * the segment is not the first in the segment manager or if it is first
619 * and it is not protected. Moreover, if the segment is first and the app
620 * has detached from the segment manager, remove the segment manager.
621 */
622 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700623 {
Florin Corasa332c462018-01-31 06:52:17 -0800624 segment_manager_segment_reader_unlock (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700625
626 /* Remove segment if it holds no fifos or first but not protected */
Florin Corasa332c462018-01-31 06:52:17 -0800627 if (segment_index != 0 || !sm->first_is_protected)
628 segment_manager_lock_and_del_segment (sm, segment_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700629
630 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400631 if (segment_manager_app_detached (sm)
632 && !segment_manager_has_fifos (sm))
Florin Coras568ebc72018-09-18 16:12:50 -0700633 {
634 segment_manager_del (sm);
635 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700636 }
Florin Corasa332c462018-01-31 06:52:17 -0800637 else
638 segment_manager_segment_reader_unlock (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700639}
640
Florin Coras3c2fed52018-07-04 04:15:05 -0700641u32
642segment_manager_evt_q_expected_size (u32 q_len)
643{
644 u32 fifo_evt_size, notif_q_size, q_hdrs;
645 u32 msg_q_sz, fifo_evt_ring_sz, session_ntf_ring_sz;
646
Florin Coras52207f12018-07-12 14:48:06 -0700647 fifo_evt_size = 1 << max_log2 (sizeof (session_event_t));
Florin Coras3c2fed52018-07-04 04:15:05 -0700648 notif_q_size = clib_max (16, q_len >> 4);
649
650 msg_q_sz = q_len * sizeof (svm_msg_q_msg_t);
651 fifo_evt_ring_sz = q_len * fifo_evt_size;
652 session_ntf_ring_sz = notif_q_size * 256;
653 q_hdrs = sizeof (svm_queue_t) + sizeof (svm_msg_q_t);
654
655 return (msg_q_sz + fifo_evt_ring_sz + session_ntf_ring_sz + q_hdrs);
656}
657
Florin Corasa5464812017-04-19 13:00:05 -0700658/**
659 * Allocates shm queue in the first segment
Florin Corasa332c462018-01-31 06:52:17 -0800660 *
661 * Must be called with lock held
Florin Corasa5464812017-04-19 13:00:05 -0700662 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700663svm_msg_q_t *
Florin Corasf8f516a2018-02-08 15:10:09 -0800664segment_manager_alloc_queue (svm_fifo_segment_private_t * segment,
Florin Coras99368312018-08-02 10:45:44 -0700665 segment_manager_properties_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700666{
Florin Coras3c2fed52018-07-04 04:15:05 -0700667 u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
668 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
669 svm_msg_q_t *q;
Florin Corasa5464812017-04-19 13:00:05 -0700670 void *oldheap;
671
Florin Coras52207f12018-07-12 14:48:06 -0700672 fifo_evt_size = sizeof (session_event_t);
Florin Coras99368312018-08-02 10:45:44 -0700673 notif_q_size = clib_max (16, props->evt_q_size >> 4);
Florin Coras3c2fed52018-07-04 04:15:05 -0700674 /* *INDENT-OFF* */
675 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
Florin Coras99368312018-08-02 10:45:44 -0700676 {props->evt_q_size, fifo_evt_size, 0},
Florin Coras3c2fed52018-07-04 04:15:05 -0700677 {notif_q_size, session_evt_size, 0}
678 };
679 /* *INDENT-ON* */
680 cfg->consumer_pid = 0;
681 cfg->n_rings = 2;
Florin Coras99368312018-08-02 10:45:44 -0700682 cfg->q_nitems = props->evt_q_size;
Florin Coras3c2fed52018-07-04 04:15:05 -0700683 cfg->ring_cfgs = rc;
Florin Corasa5464812017-04-19 13:00:05 -0700684
Florin Coras3c2fed52018-07-04 04:15:05 -0700685 oldheap = ssvm_push_heap (segment->ssvm.sh);
686 q = svm_msg_q_alloc (cfg);
Florin Corasa5464812017-04-19 13:00:05 -0700687 ssvm_pop_heap (oldheap);
Florin Coras99368312018-08-02 10:45:44 -0700688
689 if (props->use_mq_eventfd)
690 {
691 if (svm_msg_q_alloc_producer_eventfd (q))
692 clib_warning ("failed to alloc eventfd");
693 }
Florin Corasa5464812017-04-19 13:00:05 -0700694 return q;
695}
696
697/**
698 * Frees shm queue allocated in the first segment
699 */
700void
Florin Corase86a8ed2018-01-05 03:20:25 -0800701segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q)
Florin Corasa5464812017-04-19 13:00:05 -0700702{
Florin Corasa5464812017-04-19 13:00:05 -0700703 svm_fifo_segment_private_t *segment;
Florin Corasa332c462018-01-31 06:52:17 -0800704 ssvm_shared_header_t *sh;
Florin Corasa5464812017-04-19 13:00:05 -0700705 void *oldheap;
706
Florin Corasa332c462018-01-31 06:52:17 -0800707 ASSERT (!pool_is_free_index (sm->segments, 0));
Florin Corasa5464812017-04-19 13:00:05 -0700708
Florin Corasa332c462018-01-31 06:52:17 -0800709 segment = segment_manager_get_segment_w_lock (sm, 0);
Florin Corasa5464812017-04-19 13:00:05 -0700710 sh = segment->ssvm.sh;
711
712 oldheap = ssvm_push_heap (sh);
Florin Corase86a8ed2018-01-05 03:20:25 -0800713 svm_queue_free (q);
Florin Corasa5464812017-04-19 13:00:05 -0700714 ssvm_pop_heap (oldheap);
Florin Corasa332c462018-01-31 06:52:17 -0800715 segment_manager_segment_reader_unlock (sm);
716}
717
718/*
719 * Init segment vm address allocator
720 */
721void
722segment_manager_main_init (segment_manager_main_init_args_t * a)
723{
724 segment_manager_main_t *sm = &segment_manager_main;
725 clib_valloc_chunk_t _ip, *ip = &_ip;
726
727 ip->baseva = a->baseva;
728 ip->size = a->size;
729
730 clib_valloc_init (&sm->va_allocator, ip, 1 /* lock */ );
Florin Corasa5464812017-04-19 13:00:05 -0700731}
732
Florin Corasc87c91d2017-08-16 19:55:49 -0700733static clib_error_t *
734segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
735 vlib_cli_command_t * cmd)
736{
Florin Corasa332c462018-01-31 06:52:17 -0800737 segment_manager_main_t *smm = &segment_manager_main;
738 svm_fifo_segment_private_t *seg;
Florin Corasc87c91d2017-08-16 19:55:49 -0700739 segment_manager_t *sm;
Florin Corasb384b542018-01-15 01:08:33 -0800740 u8 show_segments = 0, verbose = 0;
David Johnsond9818dd2018-12-14 14:53:41 -0500741 char *address;
742 size_t size;
Dave Barach91f3e742017-09-01 19:12:11 -0400743 u32 active_fifos;
744 u32 free_fifos;
745
Florin Corasc87c91d2017-08-16 19:55:49 -0700746 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
747 {
748 if (unformat (input, "segments"))
749 show_segments = 1;
750 else if (unformat (input, "verbose"))
751 verbose = 1;
752 else
753 return clib_error_return (0, "unknown input `%U'",
754 format_unformat_error, input);
755 }
756 vlib_cli_output (vm, "%d segment managers allocated",
Florin Corasa332c462018-01-31 06:52:17 -0800757 pool_elts (smm->segment_managers));
758 if (verbose && pool_elts (smm->segment_managers))
Florin Corasc87c91d2017-08-16 19:55:49 -0700759 {
760 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
761 "Segments");
762
763 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800764 pool_foreach (sm, smm->segment_managers, ({
Florin Coras15531972018-08-12 23:50:53 -0700765 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index (sm),
766 sm->app_wrk_index, pool_elts (sm->segments));
Florin Corasc87c91d2017-08-16 19:55:49 -0700767 }));
768 /* *INDENT-ON* */
769
770 }
771 if (show_segments)
772 {
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800773 vlib_cli_output (vm, "%-15s%15s%15s%15s%15s%15s", "Name", "Type",
Dave Barach91f3e742017-09-01 19:12:11 -0400774 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700775
776 /* *INDENT-OFF* */
Florin Corasa332c462018-01-31 06:52:17 -0800777 pool_foreach (sm, smm->segment_managers, ({
778 segment_manager_foreach_segment_w_lock (seg, sm, ({
779 svm_fifo_segment_info (seg, &address, &size);
780 active_fifos = svm_fifo_segment_num_fifos (seg);
781 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
782 vlib_cli_output (vm, "%-15v%15U%15llu%15u%15u%15llx",
783 ssvm_name (&seg->ssvm), format_svm_fifo_segment_type,
784 seg, size >> 20ULL, active_fifos, free_fifos,
785 address);
786 if (verbose)
787 vlib_cli_output (vm, "%U", format_svm_fifo_segment, seg, verbose);
788 }));
Florin Corasc87c91d2017-08-16 19:55:49 -0700789 }));
790 /* *INDENT-ON* */
791
792 }
793 return 0;
794}
795
Florin Corasad0c77f2017-11-09 18:00:15 -0800796/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700797VLIB_CLI_COMMAND (segment_manager_show_command, static) =
798{
799 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400800 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700801 .function = segment_manager_show_fn,
802};
803/* *INDENT-ON* */
804
Florin Coras6cf30ad2017-04-04 23:08:23 -0700805/*
806 * fd.io coding-style-patch-verification: ON
807 *
808 * Local Variables:
809 * eval: (c-set-style "gnu")
810 * End:
811 */