blob: e448023ddd0dd6de5842e6828d4f60432886491f [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";
119 ca->segment_size = ~0;
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;
124 ca->private_segment_size = props->private_segment_size;
Florin Corasc87c91d2017-08-16 19:55:49 -0700125
Dave Barach91f3e742017-09-01 19:12:11 -0400126 /* Default to a small private segment */
127 if (ca->private_segment_size == 0)
128 ca->private_segment_size = 128 << 20;
129
130 /* Calculate space requirements */
131 rx_rounded_data_size = (1 << (max_log2 (ca->rx_fifo_size)));
132 tx_rounded_data_size = (1 << (max_log2 (ca->tx_fifo_size)));
133
134 rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size;
135 tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size;
136
137 approx_total_size = (u64) ca->preallocated_fifo_pairs
138 * (rx_fifo_size + tx_fifo_size);
139 approx_segment_count =
140 (approx_total_size +
141 (ca->private_segment_size - 1)) / (u64) ca->private_segment_size;
142
143 /* The user asked us to figure it out... */
144 if (ca->private_segment_count == 0)
145 {
146 ca->private_segment_count = approx_segment_count;
147 }
148 /* Follow directions, but issue a warning */
149 else if (approx_segment_count != ca->private_segment_count)
150 {
151 clib_warning
152 ("Honoring segment count %u, but calculated count was %u",
153 ca->private_segment_count, approx_segment_count);
154 }
155
Florin Corasc87c91d2017-08-16 19:55:49 -0700156 if (svm_fifo_segment_create_process_private (ca))
157 clib_warning ("Failed to create process private segment");
158
159 ASSERT (vec_len (ca->new_segment_indices));
160 }
Dave Barach2c25a622017-06-26 11:35:07 -0400161 vec_append (sm->segment_indices, ca->new_segment_indices);
162 vec_free (ca->new_segment_indices);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700163 return 0;
164}
165
166int
167session_manager_add_segment (segment_manager_t * sm)
168{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700169 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
Florin Corasad0c77f2017-11-09 18:00:15 -0800170 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700171 u32 add_segment_size;
Florin Corasad0c77f2017-11-09 18:00:15 -0800172 u8 *segment_name;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700173 int rv;
174
175 memset (ca, 0, sizeof (*ca));
Florin Corasad0c77f2017-11-09 18:00:15 -0800176 props = segment_manager_properties_get (sm->properties_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700177 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
Florin Corasad0c77f2017-11-09 18:00:15 -0800178 add_segment_size = props->add_segment_size ?
179 props->add_segment_size : default_segment_size;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700180
181 rv = session_manager_add_segment_i (sm, add_segment_size, segment_name);
182 vec_free (segment_name);
183 return rv;
184}
185
186int
187session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size)
188{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700189 u8 *segment_name;
190 int rv;
191
Florin Coras6cf30ad2017-04-04 23:08:23 -0700192 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
193 rv = session_manager_add_segment_i (sm, segment_size, segment_name);
194 vec_free (segment_name);
195 return rv;
196}
197
Florin Corasc87c91d2017-08-16 19:55:49 -0700198segment_manager_t *
199segment_manager_new ()
Florin Corasa5464812017-04-19 13:00:05 -0700200{
Florin Corasc87c91d2017-08-16 19:55:49 -0700201 segment_manager_t *sm;
202 pool_get (segment_managers, sm);
203 memset (sm, 0, sizeof (*sm));
204 return sm;
Florin Corasa5464812017-04-19 13:00:05 -0700205}
206
Florin Coras6cf30ad2017-04-04 23:08:23 -0700207/**
208 * Initializes segment manager based on options provided.
209 * Returns error if svm segment allocation fails.
210 */
211int
Florin Corasad0c77f2017-11-09 18:00:15 -0800212segment_manager_init (segment_manager_t * sm, u32 props_index,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700213 u32 first_seg_size)
214{
215 int rv;
216
217 /* app allocates these */
Florin Corasad0c77f2017-11-09 18:00:15 -0800218 sm->properties_index = props_index;
Florin Corasa5464812017-04-19 13:00:05 -0700219 first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
Florin Corasc87c91d2017-08-16 19:55:49 -0700220 rv = session_manager_add_first_segment (sm, first_seg_size);
221 if (rv)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700222 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700223 clib_warning ("Failed to allocate segment");
224 return rv;
Florin Corasa5464812017-04-19 13:00:05 -0700225 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700226
Florin Corasa5464812017-04-19 13:00:05 -0700227 clib_spinlock_init (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700228 return 0;
229}
230
Florin Corasc87c91d2017-08-16 19:55:49 -0700231u8
232segment_manager_has_fifos (segment_manager_t * sm)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400233{
Florin Corasc87c91d2017-08-16 19:55:49 -0700234 svm_fifo_segment_private_t *segment;
Florin Coras9d063042017-09-14 03:08:00 -0400235 int i;
236
237 for (i = 0; i < vec_len (sm->segment_indices); i++)
Dave Wallace7b749fe2017-07-05 14:30:46 -0400238 {
Florin Coras9d063042017-09-14 03:08:00 -0400239 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
240 if (CLIB_DEBUG && i && !svm_fifo_segment_has_fifos (segment)
241 && !(segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED))
242 clib_warning ("segment %d has no fifos!", sm->segment_indices[i]);
243 if (svm_fifo_segment_has_fifos (segment))
244 return 1;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400245 }
Florin Coras9d063042017-09-14 03:08:00 -0400246 return 0;
247}
248
249static u8
250segment_manager_app_detached (segment_manager_t * sm)
251{
252 return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX);
Dave Wallace7b749fe2017-07-05 14:30:46 -0400253}
254
Florin Coras4e4531e2017-11-06 23:27:56 -0800255void
256segment_manager_app_detach (segment_manager_t * sm)
257{
258 sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
259}
260
Florin Corasc87c91d2017-08-16 19:55:49 -0700261static void
262segment_manager_del_segment (segment_manager_t * sm, u32 segment_index)
263{
264 svm_fifo_segment_private_t *fifo_segment;
265 u32 svm_segment_index;
266 clib_spinlock_lock (&sm->lockp);
267 svm_segment_index = sm->segment_indices[segment_index];
268 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Florin Coras9d063042017-09-14 03:08:00 -0400269 if (!fifo_segment
270 || ((fifo_segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED)
271 && !segment_manager_app_detached (sm)))
272 {
273 clib_spinlock_unlock (&sm->lockp);
274 return;
275 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700276 svm_fifo_segment_delete (fifo_segment);
277 vec_del1 (sm->segment_indices, segment_index);
278 clib_spinlock_unlock (&sm->lockp);
279}
280
281/**
282 * Initiate disconnects for all sessions 'owned' by a segment manager
Florin Coras6cf30ad2017-04-04 23:08:23 -0700283 */
284void
Florin Corasc87c91d2017-08-16 19:55:49 -0700285segment_manager_del_sessions (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286{
Dave Barach10d8cc62017-05-30 09:30:07 -0400287 int j;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400288 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700289 svm_fifo_t *fifo;
290
Dave Wallace7b749fe2017-07-05 14:30:46 -0400291 ASSERT (vec_len (sm->segment_indices));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700292
293 /* Across all fifo segments used by the server */
294 for (j = 0; j < vec_len (sm->segment_indices); j++)
295 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700296 fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400297 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700298
299 /*
300 * Remove any residual sessions from the session lookup table
301 * Don't bother deleting the individual fifos, we're going to
302 * throw away the fifo segment in a minute.
303 */
Dave Barach10d8cc62017-05-30 09:30:07 -0400304 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700306 u32 session_index, thread_index;
307 stream_session_t *session;
308
Florin Corasa5464812017-04-19 13:00:05 -0700309 session_index = fifo->master_session_index;
310 thread_index = fifo->master_thread_index;
Florin Corascea194d2017-10-02 00:18:51 -0700311 session = session_get (session_index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700312
Florin Coras6cf30ad2017-04-04 23:08:23 -0700313 /* Instead of directly removing the session call disconnect */
Florin Corasc87c91d2017-08-16 19:55:49 -0700314 if (session->session_state != SESSION_STATE_CLOSED)
315 {
316 session->session_state = SESSION_STATE_CLOSED;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700317 session_send_session_evt_to_thread (session_handle
Florin Corasc87c91d2017-08-16 19:55:49 -0700318 (session),
319 FIFO_EVENT_DISCONNECT,
320 thread_index);
321 }
Dave Barach10d8cc62017-05-30 09:30:07 -0400322 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700323 }
324
Dave Barach10d8cc62017-05-30 09:30:07 -0400325 /* Instead of removing the segment, test when cleaning up disconnected
326 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700327 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700328 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700329}
Florin Coras6cf30ad2017-04-04 23:08:23 -0700330
Florin Corasc87c91d2017-08-16 19:55:49 -0700331/**
332 * Removes segment manager.
333 *
334 * Since the fifos allocated in the segment keep backpointers to the sessions
335 * prior to removing the segment, we call session disconnect. This
Florin Coras9d063042017-09-14 03:08:00 -0400336 * subsequently propagates into transport.
Florin Corasc87c91d2017-08-16 19:55:49 -0700337 */
338void
339segment_manager_del (segment_manager_t * sm)
340{
Florin Coras9d063042017-09-14 03:08:00 -0400341 int i;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400342
Florin Coras9d063042017-09-14 03:08:00 -0400343 ASSERT (!segment_manager_has_fifos (sm)
344 && segment_manager_app_detached (sm));
345
346 /* If we have empty preallocated segments that haven't been removed, remove
347 * them now. Apart from that, the first segment in the first segment manager
348 * is not removed when all fifos are removed. It can only be removed when
349 * the manager is explicitly deleted/detached by the app. */
350 for (i = vec_len (sm->segment_indices) - 1; i >= 0; i--)
Florin Corasc87c91d2017-08-16 19:55:49 -0700351 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700352 if (CLIB_DEBUG)
353 {
Florin Coras9d063042017-09-14 03:08:00 -0400354 svm_fifo_segment_private_t *segment;
355 segment = svm_fifo_segment_get_segment (sm->segment_indices[i]);
356 ASSERT (!svm_fifo_segment_has_fifos (segment));
Florin Corasc87c91d2017-08-16 19:55:49 -0700357 }
Florin Coras9d063042017-09-14 03:08:00 -0400358 segment_manager_del_segment (sm, i);
Florin Corasc87c91d2017-08-16 19:55:49 -0700359 }
Florin Corasa5464812017-04-19 13:00:05 -0700360 clib_spinlock_free (&sm->lockp);
Florin Corasc87c91d2017-08-16 19:55:49 -0700361 if (CLIB_DEBUG)
362 memset (sm, 0xfe, sizeof (*sm));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700363 pool_put (segment_managers, sm);
364}
365
Florin Corasc87c91d2017-08-16 19:55:49 -0700366void
367segment_manager_init_del (segment_manager_t * sm)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700368{
Florin Coras4e4531e2017-11-06 23:27:56 -0800369 segment_manager_app_detach (sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700370 if (segment_manager_has_fifos (sm))
371 segment_manager_del_sessions (sm);
372 else
373 {
Florin Coras9d063042017-09-14 03:08:00 -0400374 ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm));
Florin Corasc87c91d2017-08-16 19:55:49 -0700375 segment_manager_del (sm);
376 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700377}
378
379int
380segment_manager_alloc_session_fifos (segment_manager_t * sm,
381 svm_fifo_t ** server_rx_fifo,
382 svm_fifo_t ** server_tx_fifo,
383 u32 * fifo_segment_index)
384{
385 svm_fifo_segment_private_t *fifo_segment;
Florin Corasad0c77f2017-11-09 18:00:15 -0800386 segment_manager_properties_t *props;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700387 u32 fifo_size, sm_index;
388 u8 added_a_segment = 0;
389 int i;
390
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391 ASSERT (vec_len (sm->segment_indices));
392
Florin Corasa5464812017-04-19 13:00:05 -0700393 /* Make sure we don't have multiple threads trying to allocate segments
394 * at the same time. */
395 clib_spinlock_lock (&sm->lockp);
396
397 /* Allocate svm fifos */
Florin Corasad0c77f2017-11-09 18:00:15 -0800398 props = segment_manager_properties_get (sm->properties_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700399again:
400 for (i = 0; i < vec_len (sm->segment_indices); i++)
401 {
402 *fifo_segment_index = sm->segment_indices[i];
Florin Corasc87c91d2017-08-16 19:55:49 -0700403 fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700404
Florin Corasad0c77f2017-11-09 18:00:15 -0800405 fifo_size = props->rx_fifo_size;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700406 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400407 *server_rx_fifo =
408 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
409 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700410
Florin Corasad0c77f2017-11-09 18:00:15 -0800411 fifo_size = props->tx_fifo_size;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700412 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400413 *server_tx_fifo =
414 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
415 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700416
417 if (*server_rx_fifo == 0)
418 {
419 /* This would be very odd, but handle it... */
420 if (*server_tx_fifo != 0)
421 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400422 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
423 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700424 *server_tx_fifo = 0;
425 }
426 continue;
427 }
428 if (*server_tx_fifo == 0)
429 {
430 if (*server_rx_fifo != 0)
431 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400432 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
433 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700434 *server_rx_fifo = 0;
435 }
436 continue;
437 }
438 break;
439 }
440
441 /* See if we're supposed to create another segment */
442 if (*server_rx_fifo == 0)
443 {
Florin Corasad0c77f2017-11-09 18:00:15 -0800444 if (props->add_segment && !props->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700445 {
446 if (added_a_segment)
447 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700448 clib_warning ("added a segment, still can't allocate a fifo");
Florin Corasf03a59a2017-06-09 21:07:32 -0700449 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700450 return SESSION_ERROR_NEW_SEG_NO_SPACE;
451 }
452
453 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700454 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700455 clib_spinlock_unlock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700456 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
457 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700458
459 added_a_segment = 1;
460 goto again;
461 }
462 else
463 {
464 clib_warning ("No space to allocate fifos!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700465 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700466 return SESSION_ERROR_NO_SPACE;
467 }
468 }
469
Florin Coras6cf30ad2017-04-04 23:08:23 -0700470 /* Backpointers to segment manager */
471 sm_index = segment_manager_index (sm);
472 (*server_tx_fifo)->segment_manager = sm_index;
473 (*server_rx_fifo)->segment_manager = sm_index;
474
Florin Corasa5464812017-04-19 13:00:05 -0700475 clib_spinlock_unlock (&sm->lockp);
476
477 if (added_a_segment)
Florin Corasc87c91d2017-08-16 19:55:49 -0700478 return application_add_segment_notify (sm->app_index,
479 *fifo_segment_index);
Florin Corasa5464812017-04-19 13:00:05 -0700480
Florin Coras6cf30ad2017-04-04 23:08:23 -0700481 return 0;
482}
483
484void
485segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
486 svm_fifo_t * tx_fifo)
487{
488 segment_manager_t *sm;
489 svm_fifo_segment_private_t *fifo_segment;
Florin Corasc87c91d2017-08-16 19:55:49 -0700490 u32 i, segment_index = ~0;
491 u8 is_first;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700492
Florin Corasa5464812017-04-19 13:00:05 -0700493 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
494
495 /* It's possible to have no segment manager if the session was removed
Florin Corasc87c91d2017-08-16 19:55:49 -0700496 * as result of a detach. */
Florin Corasa5464812017-04-19 13:00:05 -0700497 if (!sm)
498 return;
499
Florin Corasc87c91d2017-08-16 19:55:49 -0700500 fifo_segment = svm_fifo_segment_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400501 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
502 FIFO_SEGMENT_RX_FREELIST);
503 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
504 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700505
Florin Corasc87c91d2017-08-16 19:55:49 -0700506 /*
507 * Try to remove svm segment if it has no fifos. This can be done only if
508 * the segment is not the first in the segment manager or if it is first
509 * and it is not protected. Moreover, if the segment is first and the app
510 * has detached from the segment manager, remove the segment manager.
511 */
512 if (!svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700513 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700514 is_first = sm->segment_indices[0] == svm_segment_index;
515
516 /* Remove segment if it holds no fifos or first but not protected */
517 if (!is_first || !sm->first_is_protected)
518 {
519 /* Find the segment manager segment index */
520 for (i = 0; i < vec_len (sm->segment_indices); i++)
521 if (sm->segment_indices[i] == svm_segment_index)
522 {
523 segment_index = i;
524 break;
525 }
526 ASSERT (segment_index != (u32) ~ 0);
527 segment_manager_del_segment (sm, segment_index);
528 }
529
530 /* Remove segment manager if no sessions and detached from app */
Florin Coras9d063042017-09-14 03:08:00 -0400531 if (segment_manager_app_detached (sm)
532 && !segment_manager_has_fifos (sm))
Florin Corasc87c91d2017-08-16 19:55:49 -0700533 segment_manager_del (sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700534 }
535}
536
Florin Corasa5464812017-04-19 13:00:05 -0700537/**
538 * Allocates shm queue in the first segment
539 */
540unix_shared_memory_queue_t *
541segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
542{
543 ssvm_shared_header_t *sh;
544 svm_fifo_segment_private_t *segment;
545 unix_shared_memory_queue_t *q;
546 void *oldheap;
547
548 ASSERT (sm->segment_indices != 0);
549
Florin Corasc87c91d2017-08-16 19:55:49 -0700550 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700551 sh = segment->ssvm.sh;
552
553 oldheap = ssvm_push_heap (sh);
Florin Corasc87c91d2017-08-16 19:55:49 -0700554 q = unix_shared_memory_queue_init (queue_size,
555 sizeof (session_fifo_event_t),
556 0 /* consumer pid */ ,
557 0 /* signal when queue non-empty */ );
Florin Corasa5464812017-04-19 13:00:05 -0700558 ssvm_pop_heap (oldheap);
559 return q;
560}
561
562/**
563 * Frees shm queue allocated in the first segment
564 */
565void
566segment_manager_dealloc_queue (segment_manager_t * sm,
567 unix_shared_memory_queue_t * q)
568{
569 ssvm_shared_header_t *sh;
570 svm_fifo_segment_private_t *segment;
571 void *oldheap;
572
573 ASSERT (sm->segment_indices != 0);
574
Florin Corasc87c91d2017-08-16 19:55:49 -0700575 segment = svm_fifo_segment_get_segment (sm->segment_indices[0]);
Florin Corasa5464812017-04-19 13:00:05 -0700576 sh = segment->ssvm.sh;
577
578 oldheap = ssvm_push_heap (sh);
579 unix_shared_memory_queue_free (q);
580 ssvm_pop_heap (oldheap);
581}
582
Florin Corasc87c91d2017-08-16 19:55:49 -0700583static clib_error_t *
584segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input,
585 vlib_cli_command_t * cmd)
586{
587 svm_fifo_segment_private_t *segments, *seg;
588 segment_manager_t *sm;
589 u8 show_segments = 0, verbose = 0, *name;
590 uword address;
591 u64 size;
Dave Barach91f3e742017-09-01 19:12:11 -0400592 u32 active_fifos;
593 u32 free_fifos;
594
Florin Corasc87c91d2017-08-16 19:55:49 -0700595 mheap_t *heap_header;
596
597 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
598 {
599 if (unformat (input, "segments"))
600 show_segments = 1;
601 else if (unformat (input, "verbose"))
602 verbose = 1;
603 else
604 return clib_error_return (0, "unknown input `%U'",
605 format_unformat_error, input);
606 }
607 vlib_cli_output (vm, "%d segment managers allocated",
608 pool_elts (segment_managers));
609 if (verbose && pool_elts (segment_managers))
610 {
611 vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index",
612 "Segments");
613
614 /* *INDENT-OFF* */
615 pool_foreach (sm, segment_managers, ({
616 vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm),
617 sm->app_index, vec_len (sm->segment_indices));
618 }));
619 /* *INDENT-ON* */
620
621 }
622 if (show_segments)
623 {
624 segments = svm_fifo_segment_segments_pool ();
625 vlib_cli_output (vm, "%d svm fifo segments allocated",
626 pool_elts (segments));
Dave Barach91f3e742017-09-01 19:12:11 -0400627 vlib_cli_output (vm, "%-20s%=12s%=16s%=16s%=16s", "Name",
628 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
Florin Corasc87c91d2017-08-16 19:55:49 -0700629
630 /* *INDENT-OFF* */
631 pool_foreach (seg, segments, ({
632 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
633 {
634 address = pointer_to_uword (seg->ssvm.sh->heap);
635 if (seg->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)
636 name = format (0, "main heap");
637 else
638 name = format (0, "private heap");
639 heap_header = mheap_header (seg->ssvm.sh->heap);
640 size = heap_header->max_size;
641 }
642 else
643 {
644 address = seg->ssvm.sh->ssvm_va;
645 size = seg->ssvm.ssvm_size;
646 name = seg->ssvm.sh->name;
647 }
Dave Barach91f3e742017-09-01 19:12:11 -0400648 active_fifos = svm_fifo_segment_num_fifos (seg);
649 free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */);
650 vlib_cli_output (vm, "%-20v%=16llu%=16u%=16u%16llx",
651 name, size >> 20ULL, active_fifos, free_fifos,
Florin Corasc87c91d2017-08-16 19:55:49 -0700652 address);
Dave Barach91f3e742017-09-01 19:12:11 -0400653 if (verbose)
654 vlib_cli_output (vm, "%U",
655 format_svm_fifo_segment, seg, verbose);
Florin Corasc87c91d2017-08-16 19:55:49 -0700656 if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
657 vec_free (name);
658 }));
659 /* *INDENT-ON* */
660
661 }
662 return 0;
663}
664
Florin Corasad0c77f2017-11-09 18:00:15 -0800665/* *INDENT-OFF* */
Florin Corasc87c91d2017-08-16 19:55:49 -0700666VLIB_CLI_COMMAND (segment_manager_show_command, static) =
667{
668 .path = "show segment-manager",
Dave Barach91f3e742017-09-01 19:12:11 -0400669 .short_help = "show segment-manager [segments][verbose]",
Florin Corasc87c91d2017-08-16 19:55:49 -0700670 .function = segment_manager_show_fn,
671};
672/* *INDENT-ON* */
673
Florin Coras6cf30ad2017-04-04 23:08:23 -0700674/*
675 * fd.io coding-style-patch-verification: ON
676 *
677 * Local Variables:
678 * eval: (c-set-style "gnu")
679 * End:
680 */