blob: 9e6a1b55cbbd36f80ed8e86ef253dd236f98781b [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
Florin Corasad0c77f2017-11-09 18:00:15 -080030/*
31 * Pool of segment manager properties
32 */
33static segment_manager_properties_t *segment_manager_properties_pool;
34
Florin Coras6cf30ad2017-04-04 23:08:23 -070035/**
Florin Corasa5464812017-04-19 13:00:05 -070036 * Process private segment index
37 */
Dave Barach2c25a622017-06-26 11:35:07 -040038u32 *private_segment_indices;
Florin Corasa5464812017-04-19 13:00:05 -070039
40/**
Florin Coras6cf30ad2017-04-04 23:08:23 -070041 * Default fifo and segment size. TODO config.
42 */
43u32 default_fifo_size = 1 << 16;
44u32 default_segment_size = 1 << 20;
45
Florin Corasad0c77f2017-11-09 18:00:15 -080046segment_manager_properties_t *
47segment_manager_properties_alloc (void)
48{
49 segment_manager_properties_t *props;
50 pool_get (segment_manager_properties_pool, props);
51 memset (props, 0, sizeof (*props));
52 return props;
53}
54
55void
56segment_manager_properties_free (segment_manager_properties_t * props)
57{
58 pool_put (segment_manager_properties_pool, props);
59 memset (props, 0xFB, sizeof (*props));
60}
61
62segment_manager_properties_t *
63segment_manager_properties_get (u32 smp_index)
64{
65 if (pool_is_free_index (segment_manager_properties_pool, smp_index))
66 return 0;
67 return pool_elt_at_index (segment_manager_properties_pool, smp_index);
68}
69
70u32
71segment_manager_properties_index (segment_manager_properties_t * p)
72{
73 return p - segment_manager_properties_pool;
74}
75
Florin Coras6cf30ad2017-04-04 23:08:23 -070076void
77segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size)
78{
79 svm_fifo_segment_private_t *s;
Florin Corasc87c91d2017-08-16 19:55:49 -070080 s = svm_fifo_segment_get_segment (index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070081 *name = s->h->segment_name;
82 *size = s->ssvm.ssvm_size;
83}
84
85always_inline int
86session_manager_add_segment_i (segment_manager_t * sm, u32 segment_size,
87 u8 * segment_name)
88{
89 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
Florin Corasad0c77f2017-11-09 18:00:15 -080090 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -070091 int rv;
92
93 memset (ca, 0, sizeof (*ca));
Florin Corasad0c77f2017-11-09 18:00:15 -080094 props = segment_manager_properties_get (sm->properties_index);
95 if (!props->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -070096 {
Florin Corasc87c91d2017-08-16 19:55:49 -070097 ca->segment_name = (char *) segment_name;
98 ca->segment_size = segment_size;
Florin Corasad0c77f2017-11-09 18:00:15 -080099 ca->rx_fifo_size = props->rx_fifo_size;
100 ca->tx_fifo_size = props->tx_fifo_size;
101 ca->preallocated_fifo_pairs = props->preallocated_fifo_pairs;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700102
Florin Corasc87c91d2017-08-16 19:55:49 -0700103 rv = svm_fifo_segment_create (ca);
104 if (rv)
105 {
106 clib_warning ("svm_fifo_segment_create ('%s', %d) failed",
107 ca->segment_name, ca->segment_size);
108 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
109 }
110 }
111 else
112 {
Dave Barach91f3e742017-09-01 19:12:11 -0400113 u32 rx_fifo_size, tx_fifo_size, rx_rounded_data_size,
114 tx_rounded_data_size;
115 u32 approx_segment_count;
116 u64 approx_total_size;
117
Florin Corasc87c91d2017-08-16 19:55:49 -0700118 ca->segment_name = "process-private-segment";
Florin Corasff6e7692017-12-11 04:59:01 -0800119 ca->segment_size = segment_size;
Florin Corasad0c77f2017-11-09 18:00:15 -0800120 ca->rx_fifo_size = props->rx_fifo_size;
121 ca->tx_fifo_size = props->tx_fifo_size;
122 ca->preallocated_fifo_pairs = props->preallocated_fifo_pairs;
123 ca->private_segment_count = props->private_segment_count;
Dave Barach91f3e742017-09-01 19:12:11 -0400124
125 /* Calculate space requirements */
126 rx_rounded_data_size = (1 << (max_log2 (ca->rx_fifo_size)));
127 tx_rounded_data_size = (1 << (max_log2 (ca->tx_fifo_size)));
128
129 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
130 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
131
132 approx_total_size = (u64) ca->preallocated_fifo_pairs
133 * (rx_fifo_size + tx_fifo_size);
Florin Corasff6e7692017-12-11 04:59:01 -0800134 approx_segment_count = (approx_total_size + (ca->segment_size - 1))
135 / (u64) ca->segment_size;
Dave Barach91f3e742017-09-01 19:12:11 -0400136
137 /* The user asked us to figure it out... */
138 if (ca->private_segment_count == 0)
139 {
140 ca->private_segment_count = approx_segment_count;
141 }
142 /* Follow directions, but issue a warning */
143 else if (approx_segment_count != ca->private_segment_count)
144 {
Florin Corasff6e7692017-12-11 04:59:01 -0800145 clib_warning ("Honoring segment count %u, calculated count was %u",
146 ca->private_segment_count, approx_segment_count);
Dave Barach91f3e742017-09-01 19:12:11 -0400147 }
148
Florin Corasc87c91d2017-08-16 19:55:49 -0700149 if (svm_fifo_segment_create_process_private (ca))
150 clib_warning ("Failed to create process private segment");
151
152 ASSERT (vec_len (ca->new_segment_indices));
153 }
Dave Barach2c25a622017-06-26 11:35:07 -0400154 vec_append (sm->segment_indices, ca->new_segment_indices);
155 vec_free (ca->new_segment_indices);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700156 return 0;
157}
158
159int
160session_manager_add_segment (segment_manager_t * sm)
161{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700162 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
Florin Corasad0c77f2017-11-09 18:00:15 -0800163 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700164 u32 add_segment_size;
Florin Corasad0c77f2017-11-09 18:00:15 -0800165 u8 *segment_name;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700166 int rv;
167
168 memset (ca, 0, sizeof (*ca));
Florin Corasad0c77f2017-11-09 18:00:15 -0800169 props = segment_manager_properties_get (sm->properties_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700170 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
Florin Corasad0c77f2017-11-09 18:00:15 -0800171 add_segment_size = props->add_segment_size ?
172 props->add_segment_size : default_segment_size;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700173
174 rv = session_manager_add_segment_i (sm, add_segment_size, segment_name);
175 vec_free (segment_name);
176 return rv;
177}
178
179int
180session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size)
181{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700182 u8 *segment_name;
183 int rv;
184
Florin Coras6cf30ad2017-04-04 23:08:23 -0700185 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
186 rv = session_manager_add_segment_i (sm, segment_size, segment_name);
187 vec_free (segment_name);
188 return rv;
189}
190
Florin Corasc87c91d2017-08-16 19:55:49 -0700191segment_manager_t *
192segment_manager_new ()
Florin Corasa5464812017-04-19 13:00:05 -0700193{
Florin Corasc87c91d2017-08-16 19:55:49 -0700194 segment_manager_t *sm;
195 pool_get (segment_managers, sm);
196 memset (sm, 0, sizeof (*sm));
197 return sm;
Florin Corasa5464812017-04-19 13:00:05 -0700198}
199
Florin Coras6cf30ad2017-04-04 23:08:23 -0700200/**
201 * Initializes segment manager based on options provided.
202 * Returns error if svm segment allocation fails.
203 */
204int
Florin Corasad0c77f2017-11-09 18:00:15 -0800205segment_manager_init (segment_manager_t * sm, u32 props_index,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700206 u32 first_seg_size)
207{
208 int rv;
209
210 /* app allocates these */
Florin Corasad0c77f2017-11-09 18:00:15 -0800211 sm->properties_index = props_index;
Florin Corasa5464812017-04-19 13:00:05 -0700212 first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
Florin Corasc87c91d2017-08-16 19:55:49 -0700213 rv = session_manager_add_first_segment (sm, first_seg_size);
214 if (rv)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700215 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700216 clib_warning ("Failed to allocate segment");
217 return rv;
Florin Corasa5464812017-04-19 13:00:05 -0700218 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700219
Florin Corasa5464812017-04-19 13:00:05 -0700220 clib_spinlock_init (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700221 return 0;
222}
223
Florin Corasc87c91d2017-08-16 19:55:49 -0700224u8
225segment_manager_has_fifos (segment_manager_t * sm)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400226{
Florin Corasc87c91d2017-08-16 19:55:49 -0700227 svm_fifo_segment_private_t *segment;
Florin Coras9d063042017-09-14 03:08:00 -0400228 int i;
229
230 for (i = 0; i < vec_len (sm->segment_indices); i++)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400231 {
Florin Coras9d063042017-09-14 03:08:00 -0400232 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
233 if (CLIB_DEBUG && i && !svm_fifo_segment_has_fifos (segment)
234 && !(segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED))
235 clib_warning ("segment %d has no fifos!", sm->segment_indices[i]);
236 if (svm_fifo_segment_has_fifos (segment))
237 return 1;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400238 }
Florin Coras9d063042017-09-14 03:08:00 -0400239 return 0;
240}
241
242static u8
243segment_manager_app_detached (segment_manager_t * sm)
244{
245 return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -0400246}
247
Florin Coras4e4531e2017-11-06 23:27:56 -0800248void
249segment_manager_app_detach (segment_manager_t * sm)
250{
251 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
252}
253
Florin Corasc87c91d2017-08-16 19:55:49 -0700254static void
255segment_manager_del_segment (segment_manager_t * sm, u32 segment_index)
256{
257 svm_fifo_segment_private_t *fifo_segment;
258 u32 svm_segment_index;
259 clib_spinlock_lock (&sm->lockp);
260 svm_segment_index = sm->segment_indices[segment_index];
261 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Florin Coras9d063042017-09-14 03:08:00 -0400262 if (!fifo_segment
263 || ((fifo_segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED)
264 && !segment_manager_app_detached (sm)))
265 {
266 clib_spinlock_unlock (&sm->lockp);
267 return;
268 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700269 svm_fifo_segment_delete (fifo_segment);
270 vec_del1 (sm->segment_indices, segment_index);
271 clib_spinlock_unlock (&sm->lockp);
272}
273
274/**
275 * Initiate disconnects for all sessions 'owned' by a segment manager
Florin Coras6cf30ad2017-04-04 23:08:23 -0700276 */
277void
Florin Corasc87c91d2017-08-16 19:55:49 -0700278segment_manager_del_sessions (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700279{
Dave Barach10d8cc62017-05-30 09:30:07 -0400280 int j;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400281 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700282 svm_fifo_t *fifo;
283
Dave Wallace7b749fe2017-07-05 14:30:46 -0400284 ASSERT (vec_len (sm->segment_indices));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700285
286 /* Across all fifo segments used by the server */
287 for (j = 0; j < vec_len (sm->segment_indices); j++)
288 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700289 fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400290 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700291
292 /*
293 * Remove any residual sessions from the session lookup table
294 * Don't bother deleting the individual fifos, we're going to
295 * throw away the fifo segment in a minute.
296 */
Dave Barach10d8cc62017-05-30 09:30:07 -0400297 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700298 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299 u32 session_index, thread_index;
300 stream_session_t *session;
301
Florin Corasa5464812017-04-19 13:00:05 -0700302 session_index = fifo->master_session_index;
303 thread_index = fifo->master_thread_index;
Florin Corascea194d2017-10-02 00:18:51 -0700304 session = session_get (session_index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305
Florin Coras6cf30ad2017-04-04 23:08:23 -0700306 /* Instead of directly removing the session call disconnect */
Florin Corasc87c91d2017-08-16 19:55:49 -0700307 if (session->session_state != SESSION_STATE_CLOSED)
308 {
309 session->session_state = SESSION_STATE_CLOSED;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700310 session_send_session_evt_to_thread (session_handle
Florin Corasc87c91d2017-08-16 19:55:49 -0700311 (session),
312 FIFO_EVENT_DISCONNECT,
313 thread_index);
314 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400315 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700316 }
317
Dave Barach10d8cc62017-05-30 09:30:07 -0400318 /* Instead of removing the segment, test when cleaning up disconnected
319 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700320 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700321 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700322}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700323
Florin Corasc87c91d2017-08-16 19:55:49 -0700324/**
325 * Removes segment manager.
326 *
327 * Since the fifos allocated in the segment keep backpointers to the sessions
328 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400329 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700330 */
331void
332segment_manager_del (segment_manager_t * sm)
333{
Florin Coras9d063042017-09-14 03:08:00 -0400334 int i;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400335
Florin Coras9d063042017-09-14 03:08:00 -0400336 ASSERT (!segment_manager_has_fifos (sm)
337 && segment_manager_app_detached (sm));
338
339 /* If we have empty preallocated segments that haven't been removed, remove
340 * them now. Apart from that, the first segment in the first segment manager
341 * is not removed when all fifos are removed. It can only be removed when
342 * the manager is explicitly deleted/detached by the app. */
343 for (i = vec_len (sm->segment_indices) - 1; i >= 0; i--)
Florin Corasc87c91d2017-08-16 19:55:49 -0700344 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700345 if (CLIB_DEBUG)
346 {
Florin Coras9d063042017-09-14 03:08:00 -0400347 svm_fifo_segment_private_t *segment;
348 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
349 ASSERT (!svm_fifo_segment_has_fifos (segment));
Florin Corasc87c91d2017-08-16 19:55:49 -0700350 }
Florin Coras9d063042017-09-14 03:08:00 -0400351 segment_manager_del_segment (sm, i);
Florin Corasc87c91d2017-08-16 19:55:49 -0700352 }
Florin Corasa5464812017-04-19 13:00:05 -0700353 clib_spinlock_free (&sm->lockp);
Florin Corasc87c91d2017-08-16 19:55:49 -0700354 if (CLIB_DEBUG)
355 memset (sm, 0xfe, sizeof (*sm));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700356 pool_put (segment_managers, sm);
357}
358
Florin Corasc87c91d2017-08-16 19:55:49 -0700359void
360segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361{
Florin Coras4e4531e2017-11-06 23:27:56 -0800362 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700363 if (segment_manager_has_fifos (sm))
364 segment_manager_del_sessions (sm);
365 else
366 {
Florin Coras9d063042017-09-14 03:08:00 -0400367 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700368 segment_manager_del (sm);
369 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700370}
371
372int
373segment_manager_alloc_session_fifos (segment_manager_t * sm,
374 svm_fifo_t ** server_rx_fifo,
375 svm_fifo_t ** server_tx_fifo,
376 u32 * fifo_segment_index)
377{
378 svm_fifo_segment_private_t *fifo_segment;
Florin Corasad0c77f2017-11-09 18:00:15 -0800379 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700380 u32 fifo_size, sm_index;
381 u8 added_a_segment = 0;
382 int i;
383
Florin Coras6cf30ad2017-04-04 23:08:23 -0700384 ASSERT (vec_len (sm->segment_indices));
385
Florin Corasa5464812017-04-19 13:00:05 -0700386 /* Make sure we don't have multiple threads trying to allocate segments
387 * at the same time. */
388 clib_spinlock_lock (&sm->lockp);
389
390 /* Allocate svm fifos */
Florin Corasad0c77f2017-11-09 18:00:15 -0800391 props = segment_manager_properties_get (sm->properties_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700392again:
393 for (i = 0; i < vec_len (sm->segment_indices); i++)
394 {
395 *fifo_segment_index = sm->segment_indices[i];
Florin Corasc87c91d2017-08-16 19:55:49 -0700396 fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700397
Florin Corasad0c77f2017-11-09 18:00:15 -0800398 fifo_size = props->rx_fifo_size;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700399 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400400 *server_rx_fifo =
401 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
402 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700403
Florin Corasad0c77f2017-11-09 18:00:15 -0800404 fifo_size = props->tx_fifo_size;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700405 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400406 *server_tx_fifo =
407 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
408 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700409
410 if (*server_rx_fifo == 0)
411 {
412 /* This would be very odd, but handle it... */
413 if (*server_tx_fifo != 0)
414 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400415 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
416 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700417 *server_tx_fifo = 0;
418 }
419 continue;
420 }
421 if (*server_tx_fifo == 0)
422 {
423 if (*server_rx_fifo != 0)
424 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400425 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
426 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427 *server_rx_fifo = 0;
428 }
429 continue;
430 }
431 break;
432 }
433
434 /* See if we're supposed to create another segment */
435 if (*server_rx_fifo == 0)
436 {
Florin Corasad0c77f2017-11-09 18:00:15 -0800437 if (props->add_segment && !props->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700438 {
439 if (added_a_segment)
440 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700441 clib_warning ("added a segment, still can't allocate a fifo");
Florin Corasf03a59a2017-06-09 21:07:32 -0700442 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700443 return SESSION_ERROR_NEW_SEG_NO_SPACE;
444 }
445
446 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700447 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700448 clib_spinlock_unlock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700449 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
450 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700451
452 added_a_segment = 1;
453 goto again;
454 }
455 else
456 {
457 clib_warning ("No space to allocate fifos!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700458 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 return SESSION_ERROR_NO_SPACE;
460 }
461 }
462
Florin Coras6cf30ad2017-04-04 23:08:23 -0700463 /* Backpointers to segment manager */
464 sm_index = segment_manager_index (sm);
465 (*server_tx_fifo)->segment_manager = sm_index;
466 (*server_rx_fifo)->segment_manager = sm_index;
467
Florin Corasa5464812017-04-19 13:00:05 -0700468 clib_spinlock_unlock (&sm->lockp);
469
470 if (added_a_segment)
Florin Corasc87c91d2017-08-16 19:55:49 -0700471 return application_add_segment_notify (sm->app_index,
472 *fifo_segment_index);
Florin Corasa5464812017-04-19 13:00:05 -0700473
Florin Coras6cf30ad2017-04-04 23:08:23 -0700474 return 0;
475}
476
477void
478segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
479 svm_fifo_t * tx_fifo)
480{
481 segment_manager_t *sm;
482 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700483 u32 i, segment_index = ~0;
484 u8 is_first;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700485
Florin Corasa5464812017-04-19 13:00:05 -0700486 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
487
488 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700489 * as result of a detach. */
Florin Corasa5464812017-04-19 13:00:05 -0700490 if (!sm)
491 return;
492
Florin Corasc87c91d2017-08-16 19:55:49 -0700493 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400494 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
495 FIFO_SEGMENT_RX_FREELIST);
496 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
497 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700498
Florin Corasc87c91d2017-08-16 19:55:49 -0700499 /*
500 * Try to remove svm segment if it has no fifos. This can be done only if
501 * the segment is not the first in the segment manager or if it is first
502 * and it is not protected. Moreover, if the segment is first and the app
503 * has detached from the segment manager, remove the segment manager.
504 */
505 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700506 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700507 is_first = sm->segment_indices[0] == svm_segment_index;
508
509 /* Remove segment if it holds no fifos or first but not protected */
510 if (!is_first || !sm->first_is_protected)
511 {
512 /* Find the segment manager segment index */
513 for (i = 0; i < vec_len (sm->segment_indices); i++)
514 if (sm->segment_indices[i] == svm_segment_index)
515 {
516 segment_index = i;
517 break;
518 }
519 ASSERT (segment_index != (u32) ~ 0);
520 segment_manager_del_segment (sm, segment_index);
521 }
522
523 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400524 if (segment_manager_app_detached (sm)
525 && !segment_manager_has_fifos (sm))
Florin Corasc87c91d2017-08-16 19:55:49 -0700526 segment_manager_del (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700527 }
528}
529
Florin Corasa5464812017-04-19 13:00:05 -0700530/**
531 * Allocates shm queue in the first segment
532 */
533unix_shared_memory_queue_t *
534segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
535{
536 ssvm_shared_header_t *sh;
537 svm_fifo_segment_private_t *segment;
538 unix_shared_memory_queue_t *q;
539 void *oldheap;
540
541 ASSERT (sm->segment_indices != 0);
542
Florin Corasc87c91d2017-08-16 19:55:49 -0700543 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700544 sh = segment->ssvm.sh;
545
546 oldheap = ssvm_push_heap (sh);
Florin Corasc87c91d2017-08-16 19:55:49 -0700547 q = unix_shared_memory_queue_init (queue_size,
548 sizeof (session_fifo_event_t),
549 0 /* consumer pid */ ,
550 0 /* signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700551 ssvm_pop_heap (oldheap);
552 return q;
553}
554
555/**
556 * Frees shm queue allocated in the first segment
557 */
558void
559segment_manager_dealloc_queue (segment_manager_t * sm,
560 unix_shared_memory_queue_t * q)
561{
562 ssvm_shared_header_t *sh;
563 svm_fifo_segment_private_t *segment;
564 void *oldheap;
565
566 ASSERT (sm->segment_indices != 0);
567
Florin Corasc87c91d2017-08-16 19:55:49 -0700568 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700569 sh = segment->ssvm.sh;
570
571 oldheap = ssvm_push_heap (sh);
572 unix_shared_memory_queue_free (q);
573 ssvm_pop_heap (oldheap);
574}
575
Florin Corasc87c91d2017-08-16 19:55:49 -0700576static clib_error_t *
577segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
578 vlib_cli_command_t * cmd)
579{
580 svm_fifo_segment_private_t *segments, *seg;
581 segment_manager_t *sm;
582 u8 show_segments = 0, verbose = 0, *name;
583 uword address;
584 u64 size;
Dave Barach91f3e742017-09-01 19:12:11 -0400585 u32 active_fifos;
586 u32 free_fifos;
587
Florin Corasc87c91d2017-08-16 19:55:49 -0700588 mheap_t *heap_header;
589
590 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
591 {
592 if (unformat (input, "segments"))
593 show_segments = 1;
594 else if (unformat (input, "verbose"))
595 verbose = 1;
596 else
597 return clib_error_return (0, "unknown input `%U'",
598 format_unformat_error, input);
599 }
600 vlib_cli_output (vm, "%d segment managers allocated",
601 pool_elts (segment_managers));
602 if (verbose && pool_elts (segment_managers))
603 {
604 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
605 "Segments");
606
607 /* *INDENT-OFF* */
608 pool_foreach (sm, segment_managers, ({
609 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm),
610 sm->app_index, vec_len (sm->segment_indices));
611 }));
612 /* *INDENT-ON* */
613
614 }
615 if (show_segments)
616 {
617 segments = svm_fifo_segment_segments_pool ();
618 vlib_cli_output (vm, "%d svm fifo segments allocated",
619 pool_elts (segments));
Florin Corasff6e7692017-12-11 04:59:01 -0800620 vlib_cli_output (vm, "%-25s%15s%16s%16s%16s", "Name",
Dave Barach91f3e742017-09-01 19:12:11 -0400621 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700622
623 /* *INDENT-OFF* */
624 pool_foreach (seg, segments, ({
625 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
626 {
627 address = pointer_to_uword (seg->ssvm.sh->heap);
628 if (seg->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)
629 name = format (0, "main heap");
630 else
631 name = format (0, "private heap");
632 heap_header = mheap_header (seg->ssvm.sh->heap);
633 size = heap_header->max_size;
634 }
635 else
636 {
637 address = seg->ssvm.sh->ssvm_va;
638 size = seg->ssvm.ssvm_size;
639 name = seg->ssvm.sh->name;
640 }
Dave Barach91f3e742017-09-01 19:12:11 -0400641 active_fifos = svm_fifo_segment_num_fifos (seg);
642 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
Florin Corasff6e7692017-12-11 04:59:01 -0800643 vlib_cli_output (vm, "%-25v%15llu%16u%16u%16llx",
Dave Barach91f3e742017-09-01 19:12:11 -0400644 name, size >> 20ULL, active_fifos, free_fifos,
Florin Corasc87c91d2017-08-16 19:55:49 -0700645 address);
Dave Barach91f3e742017-09-01 19:12:11 -0400646 if (verbose)
647 vlib_cli_output (vm, "%U",
648 format_svm_fifo_segment, seg, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700649 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
650 vec_free (name);
651 }));
652 /* *INDENT-ON* */
653
654 }
655 return 0;
656}
657
Florin Corasad0c77f2017-11-09 18:00:15 -0800658/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700659VLIB_CLI_COMMAND (segment_manager_show_command, static) =
660{
661 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400662 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700663 .function = segment_manager_show_fn,
664};
665/* *INDENT-ON* */
666
Florin Coras6cf30ad2017-04-04 23:08:23 -0700667/*
668 * fd.io coding-style-patch-verification: ON
669 *
670 * Local Variables:
671 * eval: (c-set-style "gnu")
672 * End:
673 */