blob: f5a40244400d0e7a9ad30c18f04710633a5cb6eb [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 Coras4e4531e2017-11-06 23:27:56 -0800220void
221segment_manager_app_detach (segment_manager_t * sm)
222{
223 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
224}
225
Florin Corasc87c91d2017-08-16 19:55:49 -0700226static void
227segment_manager_del_segment (segment_manager_t * sm, u32 segment_index)
228{
229 svm_fifo_segment_private_t *fifo_segment;
230 u32 svm_segment_index;
231 clib_spinlock_lock (&sm->lockp);
232 svm_segment_index = sm->segment_indices[segment_index];
233 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Florin Coras9d063042017-09-14 03:08:00 -0400234 if (!fifo_segment
235 || ((fifo_segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED)
236 && !segment_manager_app_detached (sm)))
237 {
238 clib_spinlock_unlock (&sm->lockp);
239 return;
240 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700241 svm_fifo_segment_delete (fifo_segment);
242 vec_del1 (sm->segment_indices, segment_index);
243 clib_spinlock_unlock (&sm->lockp);
244}
245
246/**
247 * Initiate disconnects for all sessions 'owned' by a segment manager
Florin Coras6cf30ad2017-04-04 23:08:23 -0700248 */
249void
Florin Corasc87c91d2017-08-16 19:55:49 -0700250segment_manager_del_sessions (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700251{
Dave Barach10d8cc62017-05-30 09:30:07 -0400252 int j;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400253 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700254 svm_fifo_t *fifo;
255
Dave Wallace7b749fe2017-07-05 14:30:46 -0400256 ASSERT (vec_len (sm->segment_indices));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700257
258 /* Across all fifo segments used by the server */
259 for (j = 0; j < vec_len (sm->segment_indices); j++)
260 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700261 fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400262 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700263
264 /*
265 * Remove any residual sessions from the session lookup table
266 * Don't bother deleting the individual fifos, we're going to
267 * throw away the fifo segment in a minute.
268 */
Dave Barach10d8cc62017-05-30 09:30:07 -0400269 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700270 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700271 u32 session_index, thread_index;
272 stream_session_t *session;
273
Florin Corasa5464812017-04-19 13:00:05 -0700274 session_index = fifo->master_session_index;
275 thread_index = fifo->master_thread_index;
Florin Corascea194d2017-10-02 00:18:51 -0700276 session = session_get (session_index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700277
Florin Coras6cf30ad2017-04-04 23:08:23 -0700278 /* Instead of directly removing the session call disconnect */
Florin Corasc87c91d2017-08-16 19:55:49 -0700279 if (session->session_state != SESSION_STATE_CLOSED)
280 {
281 session->session_state = SESSION_STATE_CLOSED;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700282 session_send_session_evt_to_thread (session_handle
Florin Corasc87c91d2017-08-16 19:55:49 -0700283 (session),
284 FIFO_EVENT_DISCONNECT,
285 thread_index);
286 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400287 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700288 }
289
Dave Barach10d8cc62017-05-30 09:30:07 -0400290 /* Instead of removing the segment, test when cleaning up disconnected
291 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700292 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700293 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700294}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700295
Florin Corasc87c91d2017-08-16 19:55:49 -0700296/**
297 * Removes segment manager.
298 *
299 * Since the fifos allocated in the segment keep backpointers to the sessions
300 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400301 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700302 */
303void
304segment_manager_del (segment_manager_t * sm)
305{
Florin Coras9d063042017-09-14 03:08:00 -0400306 int i;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400307
Florin Coras9d063042017-09-14 03:08:00 -0400308 ASSERT (!segment_manager_has_fifos (sm)
309 && segment_manager_app_detached (sm));
310
311 /* If we have empty preallocated segments that haven't been removed, remove
312 * them now. Apart from that, the first segment in the first segment manager
313 * is not removed when all fifos are removed. It can only be removed when
314 * the manager is explicitly deleted/detached by the app. */
315 for (i = vec_len (sm->segment_indices) - 1; i >= 0; i--)
Florin Corasc87c91d2017-08-16 19:55:49 -0700316 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700317 if (CLIB_DEBUG)
318 {
Florin Coras9d063042017-09-14 03:08:00 -0400319 svm_fifo_segment_private_t *segment;
320 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
321 ASSERT (!svm_fifo_segment_has_fifos (segment));
Florin Corasc87c91d2017-08-16 19:55:49 -0700322 }
Florin Coras9d063042017-09-14 03:08:00 -0400323 segment_manager_del_segment (sm, i);
Florin Corasc87c91d2017-08-16 19:55:49 -0700324 }
Florin Corasa5464812017-04-19 13:00:05 -0700325 clib_spinlock_free (&sm->lockp);
Florin Corasc87c91d2017-08-16 19:55:49 -0700326 if (CLIB_DEBUG)
327 memset (sm, 0xfe, sizeof (*sm));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700328 pool_put (segment_managers, sm);
329}
330
Florin Corasc87c91d2017-08-16 19:55:49 -0700331void
332segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700333{
Florin Coras4e4531e2017-11-06 23:27:56 -0800334 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700335 if (segment_manager_has_fifos (sm))
336 segment_manager_del_sessions (sm);
337 else
338 {
Florin Coras9d063042017-09-14 03:08:00 -0400339 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700340 segment_manager_del (sm);
341 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700342}
343
344int
345segment_manager_alloc_session_fifos (segment_manager_t * sm,
346 svm_fifo_t ** server_rx_fifo,
347 svm_fifo_t ** server_tx_fifo,
348 u32 * fifo_segment_index)
349{
350 svm_fifo_segment_private_t *fifo_segment;
351 u32 fifo_size, sm_index;
352 u8 added_a_segment = 0;
353 int i;
354
Florin Coras6cf30ad2017-04-04 23:08:23 -0700355 ASSERT (vec_len (sm->segment_indices));
356
Florin Corasa5464812017-04-19 13:00:05 -0700357 /* Make sure we don't have multiple threads trying to allocate segments
358 * at the same time. */
359 clib_spinlock_lock (&sm->lockp);
360
361 /* Allocate svm fifos */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700362again:
363 for (i = 0; i < vec_len (sm->segment_indices); i++)
364 {
365 *fifo_segment_index = sm->segment_indices[i];
Florin Corasc87c91d2017-08-16 19:55:49 -0700366 fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700367
368 fifo_size = sm->properties->rx_fifo_size;
369 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400370 *server_rx_fifo =
371 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
372 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700373
374 fifo_size = sm->properties->tx_fifo_size;
375 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400376 *server_tx_fifo =
377 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
378 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700379
380 if (*server_rx_fifo == 0)
381 {
382 /* This would be very odd, but handle it... */
383 if (*server_tx_fifo != 0)
384 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400385 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
386 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700387 *server_tx_fifo = 0;
388 }
389 continue;
390 }
391 if (*server_tx_fifo == 0)
392 {
393 if (*server_rx_fifo != 0)
394 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400395 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
396 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700397 *server_rx_fifo = 0;
398 }
399 continue;
400 }
401 break;
402 }
403
404 /* See if we're supposed to create another segment */
405 if (*server_rx_fifo == 0)
406 {
Dave Barach2c25a622017-06-26 11:35:07 -0400407 if (sm->properties->add_segment && !sm->properties->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700408 {
409 if (added_a_segment)
410 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700411 clib_warning ("added a segment, still can't allocate a fifo");
Florin Corasf03a59a2017-06-09 21:07:32 -0700412 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700413 return SESSION_ERROR_NEW_SEG_NO_SPACE;
414 }
415
416 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700417 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700418 clib_spinlock_unlock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700419 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
420 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700421
422 added_a_segment = 1;
423 goto again;
424 }
425 else
426 {
427 clib_warning ("No space to allocate fifos!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700428 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700429 return SESSION_ERROR_NO_SPACE;
430 }
431 }
432
Florin Coras6cf30ad2017-04-04 23:08:23 -0700433 /* Backpointers to segment manager */
434 sm_index = segment_manager_index (sm);
435 (*server_tx_fifo)->segment_manager = sm_index;
436 (*server_rx_fifo)->segment_manager = sm_index;
437
Florin Corasa5464812017-04-19 13:00:05 -0700438 clib_spinlock_unlock (&sm->lockp);
439
440 if (added_a_segment)
Florin Corasc87c91d2017-08-16 19:55:49 -0700441 return application_add_segment_notify (sm->app_index,
442 *fifo_segment_index);
Florin Corasa5464812017-04-19 13:00:05 -0700443
Florin Coras6cf30ad2017-04-04 23:08:23 -0700444 return 0;
445}
446
447void
448segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
449 svm_fifo_t * tx_fifo)
450{
451 segment_manager_t *sm;
452 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700453 u32 i, segment_index = ~0;
454 u8 is_first;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700455
Florin Corasa5464812017-04-19 13:00:05 -0700456 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
457
458 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700459 * as result of a detach. */
Florin Corasa5464812017-04-19 13:00:05 -0700460 if (!sm)
461 return;
462
Florin Corasc87c91d2017-08-16 19:55:49 -0700463 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400464 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
465 FIFO_SEGMENT_RX_FREELIST);
466 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
467 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700468
Florin Corasc87c91d2017-08-16 19:55:49 -0700469 /*
470 * Try to remove svm segment if it has no fifos. This can be done only if
471 * the segment is not the first in the segment manager or if it is first
472 * and it is not protected. Moreover, if the segment is first and the app
473 * has detached from the segment manager, remove the segment manager.
474 */
475 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700476 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700477 is_first = sm->segment_indices[0] == svm_segment_index;
478
479 /* Remove segment if it holds no fifos or first but not protected */
480 if (!is_first || !sm->first_is_protected)
481 {
482 /* Find the segment manager segment index */
483 for (i = 0; i < vec_len (sm->segment_indices); i++)
484 if (sm->segment_indices[i] == svm_segment_index)
485 {
486 segment_index = i;
487 break;
488 }
489 ASSERT (segment_index != (u32) ~ 0);
490 segment_manager_del_segment (sm, segment_index);
491 }
492
493 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400494 if (segment_manager_app_detached (sm)
495 && !segment_manager_has_fifos (sm))
Florin Corasc87c91d2017-08-16 19:55:49 -0700496 segment_manager_del (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700497 }
498}
499
Florin Corasa5464812017-04-19 13:00:05 -0700500/**
501 * Allocates shm queue in the first segment
502 */
503unix_shared_memory_queue_t *
504segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
505{
506 ssvm_shared_header_t *sh;
507 svm_fifo_segment_private_t *segment;
508 unix_shared_memory_queue_t *q;
509 void *oldheap;
510
511 ASSERT (sm->segment_indices != 0);
512
Florin Corasc87c91d2017-08-16 19:55:49 -0700513 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700514 sh = segment->ssvm.sh;
515
516 oldheap = ssvm_push_heap (sh);
Florin Corasc87c91d2017-08-16 19:55:49 -0700517 q = unix_shared_memory_queue_init (queue_size,
518 sizeof (session_fifo_event_t),
519 0 /* consumer pid */ ,
520 0 /* signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700521 ssvm_pop_heap (oldheap);
522 return q;
523}
524
525/**
526 * Frees shm queue allocated in the first segment
527 */
528void
529segment_manager_dealloc_queue (segment_manager_t * sm,
530 unix_shared_memory_queue_t * q)
531{
532 ssvm_shared_header_t *sh;
533 svm_fifo_segment_private_t *segment;
534 void *oldheap;
535
536 ASSERT (sm->segment_indices != 0);
537
Florin Corasc87c91d2017-08-16 19:55:49 -0700538 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700539 sh = segment->ssvm.sh;
540
541 oldheap = ssvm_push_heap (sh);
542 unix_shared_memory_queue_free (q);
543 ssvm_pop_heap (oldheap);
544}
545
Florin Corasc87c91d2017-08-16 19:55:49 -0700546static clib_error_t *
547segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
548 vlib_cli_command_t * cmd)
549{
550 svm_fifo_segment_private_t *segments, *seg;
551 segment_manager_t *sm;
552 u8 show_segments = 0, verbose = 0, *name;
553 uword address;
554 u64 size;
Dave Barach91f3e742017-09-01 19:12:11 -0400555 u32 active_fifos;
556 u32 free_fifos;
557
Florin Corasc87c91d2017-08-16 19:55:49 -0700558 mheap_t *heap_header;
559
560 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
561 {
562 if (unformat (input, "segments"))
563 show_segments = 1;
564 else if (unformat (input, "verbose"))
565 verbose = 1;
566 else
567 return clib_error_return (0, "unknown input `%U'",
568 format_unformat_error, input);
569 }
570 vlib_cli_output (vm, "%d segment managers allocated",
571 pool_elts (segment_managers));
572 if (verbose && pool_elts (segment_managers))
573 {
574 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
575 "Segments");
576
577 /* *INDENT-OFF* */
578 pool_foreach (sm, segment_managers, ({
579 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm),
580 sm->app_index, vec_len (sm->segment_indices));
581 }));
582 /* *INDENT-ON* */
583
584 }
585 if (show_segments)
586 {
587 segments = svm_fifo_segment_segments_pool ();
588 vlib_cli_output (vm, "%d svm fifo segments allocated",
589 pool_elts (segments));
Dave Barach91f3e742017-09-01 19:12:11 -0400590 vlib_cli_output (vm, "%-20s%=12s%=16s%=16s%=16s", "Name",
591 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700592
593 /* *INDENT-OFF* */
594 pool_foreach (seg, segments, ({
595 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
596 {
597 address = pointer_to_uword (seg->ssvm.sh->heap);
598 if (seg->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)
599 name = format (0, "main heap");
600 else
601 name = format (0, "private heap");
602 heap_header = mheap_header (seg->ssvm.sh->heap);
603 size = heap_header->max_size;
604 }
605 else
606 {
607 address = seg->ssvm.sh->ssvm_va;
608 size = seg->ssvm.ssvm_size;
609 name = seg->ssvm.sh->name;
610 }
Dave Barach91f3e742017-09-01 19:12:11 -0400611 active_fifos = svm_fifo_segment_num_fifos (seg);
612 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
613 vlib_cli_output (vm, "%-20v%=16llu%=16u%=16u%16llx",
614 name, size >> 20ULL, active_fifos, free_fifos,
Florin Corasc87c91d2017-08-16 19:55:49 -0700615 address);
Dave Barach91f3e742017-09-01 19:12:11 -0400616 if (verbose)
617 vlib_cli_output (vm, "%U",
618 format_svm_fifo_segment, seg, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700619 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
620 vec_free (name);
621 }));
622 /* *INDENT-ON* */
623
624 }
625 return 0;
626}
627
628 /* *INDENT-OFF* */
629VLIB_CLI_COMMAND (segment_manager_show_command, static) =
630{
631 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400632 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700633 .function = segment_manager_show_fn,
634};
635/* *INDENT-ON* */
636
Florin Coras6cf30ad2017-04-04 23:08:23 -0700637/*
638 * fd.io coding-style-patch-verification: ON
639 *
640 * Local Variables:
641 * eval: (c-set-style "gnu")
642 * End:
643 */