blob: 48d027553b1ced8b39bb6feeb4d08b3285e0eb62 [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
20/**
21 * Counter used to build segment names
22 */
23u32 segment_name_counter = 0;
24
25/**
26 * Pool of segment managers
27 */
28segment_manager_t *segment_managers = 0;
29
30/**
Florin Corasa5464812017-04-19 13:00:05 -070031 * Process private segment index
32 */
Dave Barach2c25a622017-06-26 11:35:07 -040033u32 *private_segment_indices;
Florin Corasa5464812017-04-19 13:00:05 -070034
35/**
Florin Coras6cf30ad2017-04-04 23:08:23 -070036 * Default fifo and segment size. TODO config.
37 */
38u32 default_fifo_size = 1 << 16;
39u32 default_segment_size = 1 << 20;
40
41void
42segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size)
43{
44 svm_fifo_segment_private_t *s;
Florin Corasc87c91d2017-08-16 19:55:49 -070045 s = svm_fifo_segment_get_segment (index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070046 *name = s->h->segment_name;
47 *size = s->ssvm.ssvm_size;
48}
49
50always_inline int
51session_manager_add_segment_i (segment_manager_t * sm, u32 segment_size,
52 u8 * segment_name)
53{
54 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
55 int rv;
56
57 memset (ca, 0, sizeof (*ca));
58
Florin Corasc87c91d2017-08-16 19:55:49 -070059 if (!sm->properties->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -070060 {
Florin Corasc87c91d2017-08-16 19:55:49 -070061 ca->segment_name = (char *) segment_name;
62 ca->segment_size = segment_size;
63 ca->rx_fifo_size = sm->properties->rx_fifo_size;
64 ca->tx_fifo_size = sm->properties->tx_fifo_size;
65 ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
Florin Coras6cf30ad2017-04-04 23:08:23 -070066
Florin Corasc87c91d2017-08-16 19:55:49 -070067 rv = svm_fifo_segment_create (ca);
68 if (rv)
69 {
70 clib_warning ("svm_fifo_segment_create ('%s', %d) failed",
71 ca->segment_name, ca->segment_size);
72 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
73 }
74 }
75 else
76 {
Dave Barach91f3e742017-09-01 19:12:11 -040077 u32 rx_fifo_size, tx_fifo_size, rx_rounded_data_size,
78 tx_rounded_data_size;
79 u32 approx_segment_count;
80 u64 approx_total_size;
81
Florin Corasc87c91d2017-08-16 19:55:49 -070082 ca->segment_name = "process-private-segment";
83 ca->segment_size = ~0;
84 ca->rx_fifo_size = sm->properties->rx_fifo_size;
85 ca->tx_fifo_size = sm->properties->tx_fifo_size;
86 ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
87 ca->private_segment_count = sm->properties->private_segment_count;
88 ca->private_segment_size = sm->properties->private_segment_size;
89
Dave Barach91f3e742017-09-01 19:12:11 -040090 /* Default to a small private segment */
91 if (ca->private_segment_size == 0)
92 ca->private_segment_size = 128 << 20;
93
94 /* Calculate space requirements */
95 rx_rounded_data_size = (1 << (max_log2 (ca->rx_fifo_size)));
96 tx_rounded_data_size = (1 << (max_log2 (ca->tx_fifo_size)));
97
98 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
99 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
100
101 approx_total_size = (u64) ca->preallocated_fifo_pairs
102 * (rx_fifo_size + tx_fifo_size);
103 approx_segment_count =
104 (approx_total_size +
105 (ca->private_segment_size - 1)) / (u64) ca->private_segment_size;
106
107 /* The user asked us to figure it out... */
108 if (ca->private_segment_count == 0)
109 {
110 ca->private_segment_count = approx_segment_count;
111 }
112 /* Follow directions, but issue a warning */
113 else if (approx_segment_count != ca->private_segment_count)
114 {
115 clib_warning
116 ("Honoring segment count %u, but calculated count was %u",
117 ca->private_segment_count, approx_segment_count);
118 }
119
Florin Corasc87c91d2017-08-16 19:55:49 -0700120 if (svm_fifo_segment_create_process_private (ca))
121 clib_warning ("Failed to create process private segment");
122
123 ASSERT (vec_len (ca->new_segment_indices));
124 }
Dave Barach2c25a622017-06-26 11:35:07 -0400125 vec_append (sm->segment_indices, ca->new_segment_indices);
126 vec_free (ca->new_segment_indices);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700127 return 0;
128}
129
130int
131session_manager_add_segment (segment_manager_t * sm)
132{
133 u8 *segment_name;
134 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
135 u32 add_segment_size;
136 int rv;
137
138 memset (ca, 0, sizeof (*ca));
139 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
140 add_segment_size = sm->properties->add_segment_size ?
141 sm->properties->add_segment_size : default_segment_size;
142
143 rv = session_manager_add_segment_i (sm, add_segment_size, segment_name);
144 vec_free (segment_name);
145 return rv;
146}
147
148int
149session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size)
150{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700151 u8 *segment_name;
152 int rv;
153
Florin Coras6cf30ad2017-04-04 23:08:23 -0700154 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
155 rv = session_manager_add_segment_i (sm, segment_size, segment_name);
156 vec_free (segment_name);
157 return rv;
158}
159
Florin Corasc87c91d2017-08-16 19:55:49 -0700160segment_manager_t *
161segment_manager_new ()
Florin Corasa5464812017-04-19 13:00:05 -0700162{
Florin Corasc87c91d2017-08-16 19:55:49 -0700163 segment_manager_t *sm;
164 pool_get (segment_managers, sm);
165 memset (sm, 0, sizeof (*sm));
166 return sm;
Florin Corasa5464812017-04-19 13:00:05 -0700167}
168
Florin Coras6cf30ad2017-04-04 23:08:23 -0700169/**
170 * Initializes segment manager based on options provided.
171 * Returns error if svm segment allocation fails.
172 */
173int
174segment_manager_init (segment_manager_t * sm,
175 segment_manager_properties_t * properties,
176 u32 first_seg_size)
177{
178 int rv;
179
180 /* app allocates these */
181 sm->properties = properties;
182
Florin Corasa5464812017-04-19 13:00:05 -0700183 first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
184
Florin Corasc87c91d2017-08-16 19:55:49 -0700185 rv = session_manager_add_first_segment (sm, first_seg_size);
186 if (rv)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700187 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700188 clib_warning ("Failed to allocate segment");
189 return rv;
Florin Corasa5464812017-04-19 13:00:05 -0700190 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700191
Florin Corasa5464812017-04-19 13:00:05 -0700192 clib_spinlock_init (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700193 return 0;
194}
195
Florin Corasc87c91d2017-08-16 19:55:49 -0700196u8
197segment_manager_has_fifos (segment_manager_t * sm)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400198{
Florin Corasc87c91d2017-08-16 19:55:49 -0700199 svm_fifo_segment_private_t *segment;
Florin Coras9d063042017-09-14 03:08:00 -0400200 int i;
201
202 for (i = 0; i < vec_len (sm->segment_indices); i++)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400203 {
Florin Coras9d063042017-09-14 03:08:00 -0400204 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
205 if (CLIB_DEBUG && i && !svm_fifo_segment_has_fifos (segment)
206 && !(segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED))
207 clib_warning ("segment %d has no fifos!", sm->segment_indices[i]);
208 if (svm_fifo_segment_has_fifos (segment))
209 return 1;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400210 }
Florin Coras9d063042017-09-14 03:08:00 -0400211 return 0;
212}
213
214static u8
215segment_manager_app_detached (segment_manager_t * sm)
216{
217 return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -0400218}
219
Florin Corasc87c91d2017-08-16 19:55:49 -0700220static void
221segment_manager_del_segment (segment_manager_t * sm, u32 segment_index)
222{
223 svm_fifo_segment_private_t *fifo_segment;
224 u32 svm_segment_index;
225 clib_spinlock_lock (&sm->lockp);
226 svm_segment_index = sm->segment_indices[segment_index];
227 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Florin Coras9d063042017-09-14 03:08:00 -0400228 if (!fifo_segment
229 || ((fifo_segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED)
230 && !segment_manager_app_detached (sm)))
231 {
232 clib_spinlock_unlock (&sm->lockp);
233 return;
234 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700235 svm_fifo_segment_delete (fifo_segment);
236 vec_del1 (sm->segment_indices, segment_index);
237 clib_spinlock_unlock (&sm->lockp);
238}
239
240/**
241 * Initiate disconnects for all sessions 'owned' by a segment manager
Florin Coras6cf30ad2017-04-04 23:08:23 -0700242 */
243void
Florin Corasc87c91d2017-08-16 19:55:49 -0700244segment_manager_del_sessions (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700245{
Dave Barach10d8cc62017-05-30 09:30:07 -0400246 int j;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400247 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700248 svm_fifo_t *fifo;
249
Dave Wallace7b749fe2017-07-05 14:30:46 -0400250 ASSERT (vec_len (sm->segment_indices));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700251
252 /* Across all fifo segments used by the server */
253 for (j = 0; j < vec_len (sm->segment_indices); j++)
254 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700255 fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400256 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700257
258 /*
259 * Remove any residual sessions from the session lookup table
260 * Don't bother deleting the individual fifos, we're going to
261 * throw away the fifo segment in a minute.
262 */
Dave Barach10d8cc62017-05-30 09:30:07 -0400263 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700264 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700265 u32 session_index, thread_index;
266 stream_session_t *session;
267
Florin Corasa5464812017-04-19 13:00:05 -0700268 session_index = fifo->master_session_index;
269 thread_index = fifo->master_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700270 session = stream_session_get (session_index, thread_index);
271
Florin Coras6cf30ad2017-04-04 23:08:23 -0700272 /* Instead of directly removing the session call disconnect */
Florin Corasc87c91d2017-08-16 19:55:49 -0700273 if (session->session_state != SESSION_STATE_CLOSED)
274 {
275 session->session_state = SESSION_STATE_CLOSED;
276 session_send_session_evt_to_thread (stream_session_handle
277 (session),
278 FIFO_EVENT_DISCONNECT,
279 thread_index);
280 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400281 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700282 }
283
Dave Barach10d8cc62017-05-30 09:30:07 -0400284 /* Instead of removing the segment, test when cleaning up disconnected
285 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700287 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700288}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700289
Florin Corasc87c91d2017-08-16 19:55:49 -0700290/**
291 * Removes segment manager.
292 *
293 * Since the fifos allocated in the segment keep backpointers to the sessions
294 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400295 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700296 */
297void
298segment_manager_del (segment_manager_t * sm)
299{
Florin Coras9d063042017-09-14 03:08:00 -0400300 int i;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400301
Florin Coras9d063042017-09-14 03:08:00 -0400302 ASSERT (!segment_manager_has_fifos (sm)
303 && segment_manager_app_detached (sm));
304
305 /* If we have empty preallocated segments that haven't been removed, remove
306 * them now. Apart from that, the first segment in the first segment manager
307 * is not removed when all fifos are removed. It can only be removed when
308 * the manager is explicitly deleted/detached by the app. */
309 for (i = vec_len (sm->segment_indices) - 1; i >= 0; i--)
Florin Corasc87c91d2017-08-16 19:55:49 -0700310 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700311 if (CLIB_DEBUG)
312 {
Florin Coras9d063042017-09-14 03:08:00 -0400313 svm_fifo_segment_private_t *segment;
314 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
315 ASSERT (!svm_fifo_segment_has_fifos (segment));
Florin Corasc87c91d2017-08-16 19:55:49 -0700316 }
Florin Coras9d063042017-09-14 03:08:00 -0400317 segment_manager_del_segment (sm, i);
Florin Corasc87c91d2017-08-16 19:55:49 -0700318 }
Florin Corasa5464812017-04-19 13:00:05 -0700319 clib_spinlock_free (&sm->lockp);
Florin Corasc87c91d2017-08-16 19:55:49 -0700320 if (CLIB_DEBUG)
321 memset (sm, 0xfe, sizeof (*sm));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700322 pool_put (segment_managers, sm);
323}
324
Florin Corasc87c91d2017-08-16 19:55:49 -0700325void
326segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700327{
Florin Corasc87c91d2017-08-16 19:55:49 -0700328 if (segment_manager_has_fifos (sm))
329 segment_manager_del_sessions (sm);
330 else
331 {
Florin Coras9d063042017-09-14 03:08:00 -0400332 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700333 segment_manager_del (sm);
334 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700335}
336
337int
338segment_manager_alloc_session_fifos (segment_manager_t * sm,
339 svm_fifo_t ** server_rx_fifo,
340 svm_fifo_t ** server_tx_fifo,
341 u32 * fifo_segment_index)
342{
343 svm_fifo_segment_private_t *fifo_segment;
344 u32 fifo_size, sm_index;
345 u8 added_a_segment = 0;
346 int i;
347
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348 ASSERT (vec_len (sm->segment_indices));
349
Florin Corasa5464812017-04-19 13:00:05 -0700350 /* Make sure we don't have multiple threads trying to allocate segments
351 * at the same time. */
352 clib_spinlock_lock (&sm->lockp);
353
354 /* Allocate svm fifos */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700355again:
356 for (i = 0; i < vec_len (sm->segment_indices); i++)
357 {
358 *fifo_segment_index = sm->segment_indices[i];
Florin Corasc87c91d2017-08-16 19:55:49 -0700359 fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700360
361 fifo_size = sm->properties->rx_fifo_size;
362 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400363 *server_rx_fifo =
364 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
365 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700366
367 fifo_size = sm->properties->tx_fifo_size;
368 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400369 *server_tx_fifo =
370 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
371 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700372
373 if (*server_rx_fifo == 0)
374 {
375 /* This would be very odd, but handle it... */
376 if (*server_tx_fifo != 0)
377 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400378 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
379 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380 *server_tx_fifo = 0;
381 }
382 continue;
383 }
384 if (*server_tx_fifo == 0)
385 {
386 if (*server_rx_fifo != 0)
387 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400388 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
389 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700390 *server_rx_fifo = 0;
391 }
392 continue;
393 }
394 break;
395 }
396
397 /* See if we're supposed to create another segment */
398 if (*server_rx_fifo == 0)
399 {
Dave Barach2c25a622017-06-26 11:35:07 -0400400 if (sm->properties->add_segment && !sm->properties->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700401 {
402 if (added_a_segment)
403 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700404 clib_warning ("added a segment, still can't allocate a fifo");
Florin Corasf03a59a2017-06-09 21:07:32 -0700405 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700406 return SESSION_ERROR_NEW_SEG_NO_SPACE;
407 }
408
409 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700410 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700411 clib_spinlock_unlock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700412 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
413 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700414
415 added_a_segment = 1;
416 goto again;
417 }
418 else
419 {
420 clib_warning ("No space to allocate fifos!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700421 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700422 return SESSION_ERROR_NO_SPACE;
423 }
424 }
425
Florin Coras6cf30ad2017-04-04 23:08:23 -0700426 /* Backpointers to segment manager */
427 sm_index = segment_manager_index (sm);
428 (*server_tx_fifo)->segment_manager = sm_index;
429 (*server_rx_fifo)->segment_manager = sm_index;
430
Florin Corasa5464812017-04-19 13:00:05 -0700431 clib_spinlock_unlock (&sm->lockp);
432
433 if (added_a_segment)
Florin Corasc87c91d2017-08-16 19:55:49 -0700434 return application_add_segment_notify (sm->app_index,
435 *fifo_segment_index);
Florin Corasa5464812017-04-19 13:00:05 -0700436
Florin Coras6cf30ad2017-04-04 23:08:23 -0700437 return 0;
438}
439
440void
441segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
442 svm_fifo_t * tx_fifo)
443{
444 segment_manager_t *sm;
445 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700446 u32 i, segment_index = ~0;
447 u8 is_first;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700448
Florin Corasa5464812017-04-19 13:00:05 -0700449 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
450
451 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700452 * as result of a detach. */
Florin Corasa5464812017-04-19 13:00:05 -0700453 if (!sm)
454 return;
455
Florin Corasc87c91d2017-08-16 19:55:49 -0700456 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400457 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
458 FIFO_SEGMENT_RX_FREELIST);
459 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
460 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700461
Florin Corasc87c91d2017-08-16 19:55:49 -0700462 /*
463 * Try to remove svm segment if it has no fifos. This can be done only if
464 * the segment is not the first in the segment manager or if it is first
465 * and it is not protected. Moreover, if the segment is first and the app
466 * has detached from the segment manager, remove the segment manager.
467 */
468 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700469 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700470 is_first = sm->segment_indices[0] == svm_segment_index;
471
472 /* Remove segment if it holds no fifos or first but not protected */
473 if (!is_first || !sm->first_is_protected)
474 {
475 /* Find the segment manager segment index */
476 for (i = 0; i < vec_len (sm->segment_indices); i++)
477 if (sm->segment_indices[i] == svm_segment_index)
478 {
479 segment_index = i;
480 break;
481 }
482 ASSERT (segment_index != (u32) ~ 0);
483 segment_manager_del_segment (sm, segment_index);
484 }
485
486 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400487 if (segment_manager_app_detached (sm)
488 && !segment_manager_has_fifos (sm))
Florin Corasc87c91d2017-08-16 19:55:49 -0700489 segment_manager_del (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700490 }
491}
492
Florin Corasa5464812017-04-19 13:00:05 -0700493/**
494 * Allocates shm queue in the first segment
495 */
496unix_shared_memory_queue_t *
497segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
498{
499 ssvm_shared_header_t *sh;
500 svm_fifo_segment_private_t *segment;
501 unix_shared_memory_queue_t *q;
502 void *oldheap;
503
504 ASSERT (sm->segment_indices != 0);
505
Florin Corasc87c91d2017-08-16 19:55:49 -0700506 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700507 sh = segment->ssvm.sh;
508
509 oldheap = ssvm_push_heap (sh);
Florin Corasc87c91d2017-08-16 19:55:49 -0700510 q = unix_shared_memory_queue_init (queue_size,
511 sizeof (session_fifo_event_t),
512 0 /* consumer pid */ ,
513 0 /* signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700514 ssvm_pop_heap (oldheap);
515 return q;
516}
517
518/**
519 * Frees shm queue allocated in the first segment
520 */
521void
522segment_manager_dealloc_queue (segment_manager_t * sm,
523 unix_shared_memory_queue_t * q)
524{
525 ssvm_shared_header_t *sh;
526 svm_fifo_segment_private_t *segment;
527 void *oldheap;
528
529 ASSERT (sm->segment_indices != 0);
530
Florin Corasc87c91d2017-08-16 19:55:49 -0700531 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700532 sh = segment->ssvm.sh;
533
534 oldheap = ssvm_push_heap (sh);
535 unix_shared_memory_queue_free (q);
536 ssvm_pop_heap (oldheap);
537}
538
Florin Corasc87c91d2017-08-16 19:55:49 -0700539static clib_error_t *
540segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
541 vlib_cli_command_t * cmd)
542{
543 svm_fifo_segment_private_t *segments, *seg;
544 segment_manager_t *sm;
545 u8 show_segments = 0, verbose = 0, *name;
546 uword address;
547 u64 size;
Dave Barach91f3e742017-09-01 19:12:11 -0400548 u32 active_fifos;
549 u32 free_fifos;
550
Florin Corasc87c91d2017-08-16 19:55:49 -0700551 mheap_t *heap_header;
552
553 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
554 {
555 if (unformat (input, "segments"))
556 show_segments = 1;
557 else if (unformat (input, "verbose"))
558 verbose = 1;
559 else
560 return clib_error_return (0, "unknown input `%U'",
561 format_unformat_error, input);
562 }
563 vlib_cli_output (vm, "%d segment managers allocated",
564 pool_elts (segment_managers));
565 if (verbose && pool_elts (segment_managers))
566 {
567 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
568 "Segments");
569
570 /* *INDENT-OFF* */
571 pool_foreach (sm, segment_managers, ({
572 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm),
573 sm->app_index, vec_len (sm->segment_indices));
574 }));
575 /* *INDENT-ON* */
576
577 }
578 if (show_segments)
579 {
580 segments = svm_fifo_segment_segments_pool ();
581 vlib_cli_output (vm, "%d svm fifo segments allocated",
582 pool_elts (segments));
Dave Barach91f3e742017-09-01 19:12:11 -0400583 vlib_cli_output (vm, "%-20s%=12s%=16s%=16s%=16s", "Name",
584 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700585
586 /* *INDENT-OFF* */
587 pool_foreach (seg, segments, ({
588 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
589 {
590 address = pointer_to_uword (seg->ssvm.sh->heap);
591 if (seg->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)
592 name = format (0, "main heap");
593 else
594 name = format (0, "private heap");
595 heap_header = mheap_header (seg->ssvm.sh->heap);
596 size = heap_header->max_size;
597 }
598 else
599 {
600 address = seg->ssvm.sh->ssvm_va;
601 size = seg->ssvm.ssvm_size;
602 name = seg->ssvm.sh->name;
603 }
Dave Barach91f3e742017-09-01 19:12:11 -0400604 active_fifos = svm_fifo_segment_num_fifos (seg);
605 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
606 vlib_cli_output (vm, "%-20v%=16llu%=16u%=16u%16llx",
607 name, size >> 20ULL, active_fifos, free_fifos,
Florin Corasc87c91d2017-08-16 19:55:49 -0700608 address);
Dave Barach91f3e742017-09-01 19:12:11 -0400609 if (verbose)
610 vlib_cli_output (vm, "%U",
611 format_svm_fifo_segment, seg, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700612 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
613 vec_free (name);
614 }));
615 /* *INDENT-ON* */
616
617 }
618 return 0;
619}
620
621 /* *INDENT-OFF* */
622VLIB_CLI_COMMAND (segment_manager_show_command, static) =
623{
624 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400625 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700626 .function = segment_manager_show_fn,
627};
628/* *INDENT-ON* */
629
Florin Coras6cf30ad2017-04-04 23:08:23 -0700630/*
631 * fd.io coding-style-patch-verification: ON
632 *
633 * Local Variables:
634 * eval: (c-set-style "gnu")
635 * End:
636 */