blob: 262b7faab92a9025db7d2eae204badeb56b3bb11 [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;
45 s = svm_fifo_get_segment (index);
46 *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
59 ca->segment_name = (char *) segment_name;
60 ca->segment_size = segment_size;
Dave Barach10d8cc62017-05-30 09:30:07 -040061 ca->rx_fifo_size = sm->properties->rx_fifo_size;
62 ca->tx_fifo_size = sm->properties->tx_fifo_size;
63 ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
Florin Coras6cf30ad2017-04-04 23:08:23 -070064
65 rv = svm_fifo_segment_create (ca);
66 if (rv)
67 {
68 clib_warning ("svm_fifo_segment_create ('%s', %d) failed",
69 ca->segment_name, ca->segment_size);
Florin Coras6cf30ad2017-04-04 23:08:23 -070070 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
71 }
72
Dave Barach2c25a622017-06-26 11:35:07 -040073 vec_append (sm->segment_indices, ca->new_segment_indices);
74 vec_free (ca->new_segment_indices);
Florin Coras6cf30ad2017-04-04 23:08:23 -070075
76 return 0;
77}
78
79int
80session_manager_add_segment (segment_manager_t * sm)
81{
82 u8 *segment_name;
83 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
84 u32 add_segment_size;
85 int rv;
86
87 memset (ca, 0, sizeof (*ca));
88 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
89 add_segment_size = sm->properties->add_segment_size ?
90 sm->properties->add_segment_size : default_segment_size;
91
92 rv = session_manager_add_segment_i (sm, add_segment_size, segment_name);
93 vec_free (segment_name);
94 return rv;
95}
96
97int
98session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size)
99{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700100 u8 *segment_name;
101 int rv;
102
Florin Coras6cf30ad2017-04-04 23:08:23 -0700103 segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
104 rv = session_manager_add_segment_i (sm, segment_size, segment_name);
105 vec_free (segment_name);
106 return rv;
107}
108
Florin Corasa5464812017-04-19 13:00:05 -0700109static void
Dave Barach10d8cc62017-05-30 09:30:07 -0400110 segment_manager_alloc_process_private_segment
111 (segment_manager_properties_t * props)
Florin Corasa5464812017-04-19 13:00:05 -0700112{
113 svm_fifo_segment_create_args_t _a, *a = &_a;
114
Dave Barach2c25a622017-06-26 11:35:07 -0400115 if (private_segment_indices)
Florin Corasa5464812017-04-19 13:00:05 -0700116 return;
117
118 memset (a, 0, sizeof (*a));
119 a->segment_name = "process-private-segment";
120 a->segment_size = ~0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400121 a->rx_fifo_size = props->rx_fifo_size;
122 a->tx_fifo_size = props->tx_fifo_size;
123 a->preallocated_fifo_pairs = props->preallocated_fifo_pairs;
Dave Barach2c25a622017-06-26 11:35:07 -0400124 a->private_segment_count = props->private_segment_count;
125 a->private_segment_size = props->private_segment_size;
Florin Corasa5464812017-04-19 13:00:05 -0700126
127 if (svm_fifo_segment_create_process_private (a))
128 clib_warning ("Failed to create process private segment");
129
Dave Barach2c25a622017-06-26 11:35:07 -0400130 private_segment_indices = a->new_segment_indices;
131 ASSERT (vec_len (private_segment_indices));
Florin Corasa5464812017-04-19 13:00:05 -0700132}
133
Florin Coras6cf30ad2017-04-04 23:08:23 -0700134/**
135 * Initializes segment manager based on options provided.
136 * Returns error if svm segment allocation fails.
137 */
138int
139segment_manager_init (segment_manager_t * sm,
140 segment_manager_properties_t * properties,
141 u32 first_seg_size)
142{
143 int rv;
144
145 /* app allocates these */
146 sm->properties = properties;
147
Florin Corasa5464812017-04-19 13:00:05 -0700148 first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
149
150 if (sm->properties->use_private_segment == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700151 {
152 rv = session_manager_add_first_segment (sm, first_seg_size);
153 if (rv)
154 {
155 clib_warning ("Failed to allocate segment");
156 return rv;
157 }
158 }
Florin Corasa5464812017-04-19 13:00:05 -0700159 else
160 {
Dave Barach2c25a622017-06-26 11:35:07 -0400161 if (vec_len (private_segment_indices) == 0)
Dave Barach10d8cc62017-05-30 09:30:07 -0400162 segment_manager_alloc_process_private_segment (properties);
Dave Barach2c25a622017-06-26 11:35:07 -0400163 ASSERT (vec_len (private_segment_indices));
164 vec_append (sm->segment_indices, private_segment_indices);
Florin Corasa5464812017-04-19 13:00:05 -0700165 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700166
Florin Corasa5464812017-04-19 13:00:05 -0700167 clib_spinlock_init (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700168 return 0;
169}
170
Dave Wallace7b749fe2017-07-05 14:30:46 -0400171void
172segment_manager_first_segment_maybe_del (segment_manager_t * sm)
173{
174 svm_fifo_segment_private_t *fifo_segment;
175
176 /* If the first semgment has no fifos, then delete the 1st segment
177 */
178 fifo_segment = svm_fifo_get_segment (sm->segment_indices[0]);
179 if (!svm_fifo_segment_has_fifos (fifo_segment))
180 {
181 clib_spinlock_lock (&sm->lockp);
182 svm_fifo_segment_delete (fifo_segment);
183 vec_del1 (sm->segment_indices, 0);
184 clib_spinlock_unlock (&sm->lockp);
185 }
186}
187
188 /**
Florin Coras6cf30ad2017-04-04 23:08:23 -0700189 * Removes segment manager.
190 *
191 * Since the fifos allocated in the segment keep backpointers to the sessions
192 * prior to removing the segment, we call session disconnect. This
193 * subsequently propages into transport.
194 */
195void
196segment_manager_del (segment_manager_t * sm)
197{
Dave Barach10d8cc62017-05-30 09:30:07 -0400198 int j;
Dave Wallace7b749fe2017-07-05 14:30:46 -0400199 svm_fifo_segment_private_t *fifo_segment;
200 ASSERT (vec_len (sm->segment_indices));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700201
202 /* Across all fifo segments used by the server */
203 for (j = 0; j < vec_len (sm->segment_indices); j++)
204 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400205 svm_fifo_t *fifo;
206
Florin Coras6cf30ad2017-04-04 23:08:23 -0700207 /* Vector of fifos allocated in the segment */
208 fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400209 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700210
211 /*
212 * Remove any residual sessions from the session lookup table
213 * Don't bother deleting the individual fifos, we're going to
214 * throw away the fifo segment in a minute.
215 */
Dave Barach10d8cc62017-05-30 09:30:07 -0400216 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700217 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700218 u32 session_index, thread_index;
219 stream_session_t *session;
220
Florin Corasa5464812017-04-19 13:00:05 -0700221 session_index = fifo->master_session_index;
222 thread_index = fifo->master_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700223
224 session = stream_session_get (session_index, thread_index);
225
Florin Coras6cf30ad2017-04-04 23:08:23 -0700226 /* Instead of directly removing the session call disconnect */
Florin Corasa5464812017-04-19 13:00:05 -0700227 session_send_session_evt_to_thread (stream_session_handle (session),
228 FIFO_EVENT_DISCONNECT,
Dave Barach10d8cc62017-05-30 09:30:07 -0400229 thread_index);
230 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700231 }
232
Dave Barach10d8cc62017-05-30 09:30:07 -0400233 /* Instead of removing the segment, test when cleaning up disconnected
234 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700235 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700236 }
237
Dave Wallace7b749fe2017-07-05 14:30:46 -0400238 segment_manager_first_segment_maybe_del (sm);
239
Florin Corasa5464812017-04-19 13:00:05 -0700240 clib_spinlock_free (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700241 pool_put (segment_managers, sm);
242}
243
244static int
245segment_manager_notify_app_seg_add (segment_manager_t * sm,
246 u32 fifo_segment_index)
247{
248 application_t *app = application_get (sm->app_index);
249 u32 seg_size = 0;
250 u8 *seg_name;
251
252 /* Send an API message to the external app, to map new segment */
253 ASSERT (app->cb_fns.add_segment_callback);
254
255 segment_manager_get_segment_info (fifo_segment_index, &seg_name, &seg_size);
256 return app->cb_fns.add_segment_callback (app->api_client_index, seg_name,
257 seg_size);
258}
259
260int
261segment_manager_alloc_session_fifos (segment_manager_t * sm,
262 svm_fifo_t ** server_rx_fifo,
263 svm_fifo_t ** server_tx_fifo,
264 u32 * fifo_segment_index)
265{
266 svm_fifo_segment_private_t *fifo_segment;
267 u32 fifo_size, sm_index;
268 u8 added_a_segment = 0;
269 int i;
270
Florin Coras6cf30ad2017-04-04 23:08:23 -0700271 ASSERT (vec_len (sm->segment_indices));
272
Florin Corasa5464812017-04-19 13:00:05 -0700273 /* Make sure we don't have multiple threads trying to allocate segments
274 * at the same time. */
275 clib_spinlock_lock (&sm->lockp);
276
277 /* Allocate svm fifos */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700278again:
279 for (i = 0; i < vec_len (sm->segment_indices); i++)
280 {
281 *fifo_segment_index = sm->segment_indices[i];
282 fifo_segment = svm_fifo_get_segment (*fifo_segment_index);
283
Dave Barach10d8cc62017-05-30 09:30:07 -0400284 /* FC: cleanup, make sure sm->properties->xxx_fifo_size always set */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700285 fifo_size = sm->properties->rx_fifo_size;
286 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400287 *server_rx_fifo =
288 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
289 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700290
Dave Barach10d8cc62017-05-30 09:30:07 -0400291 /* FC: cleanup, make sure sm->properties->xxx_fifo_size always set */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700292 fifo_size = sm->properties->tx_fifo_size;
293 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400294 *server_tx_fifo =
295 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
296 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700297
298 if (*server_rx_fifo == 0)
299 {
300 /* This would be very odd, but handle it... */
301 if (*server_tx_fifo != 0)
302 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400303 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
304 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700305 *server_tx_fifo = 0;
306 }
307 continue;
308 }
309 if (*server_tx_fifo == 0)
310 {
311 if (*server_rx_fifo != 0)
312 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400313 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
314 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700315 *server_rx_fifo = 0;
316 }
317 continue;
318 }
319 break;
320 }
321
322 /* See if we're supposed to create another segment */
323 if (*server_rx_fifo == 0)
324 {
Dave Barach2c25a622017-06-26 11:35:07 -0400325 if (sm->properties->add_segment && !sm->properties->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700326 {
327 if (added_a_segment)
328 {
329 clib_warning ("added a segment, still cant allocate a fifo");
Florin Corasf03a59a2017-06-09 21:07:32 -0700330 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700331 return SESSION_ERROR_NEW_SEG_NO_SPACE;
332 }
333
334 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700335 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700336 clib_spinlock_unlock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700337 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
338 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700339
340 added_a_segment = 1;
341 goto again;
342 }
343 else
344 {
345 clib_warning ("No space to allocate fifos!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700346 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700347 return SESSION_ERROR_NO_SPACE;
348 }
349 }
350
Florin Coras6cf30ad2017-04-04 23:08:23 -0700351 /* Backpointers to segment manager */
352 sm_index = segment_manager_index (sm);
353 (*server_tx_fifo)->segment_manager = sm_index;
354 (*server_rx_fifo)->segment_manager = sm_index;
355
Florin Corasa5464812017-04-19 13:00:05 -0700356 clib_spinlock_unlock (&sm->lockp);
357
358 if (added_a_segment)
359 return segment_manager_notify_app_seg_add (sm, *fifo_segment_index);
360
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361 return 0;
362}
363
364void
365segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
366 svm_fifo_t * tx_fifo)
367{
368 segment_manager_t *sm;
369 svm_fifo_segment_private_t *fifo_segment;
370
Florin Corasa5464812017-04-19 13:00:05 -0700371 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
372
373 /* It's possible to have no segment manager if the session was removed
374 * as result of a detach */
375 if (!sm)
376 return;
377
Florin Coras6cf30ad2017-04-04 23:08:23 -0700378 fifo_segment = svm_fifo_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400379 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
380 FIFO_SEGMENT_RX_FREELIST);
381 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
382 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700383
Dave Barach2c25a622017-06-26 11:35:07 -0400384 /* Don't try to delete process-private segments */
385 if (sm->properties->private_segment_count > 0)
386 return;
387
Florin Corasa5464812017-04-19 13:00:05 -0700388 /* Remove segment only if it holds no fifos and not the first */
389 if (sm->segment_indices[0] != svm_segment_index
390 && !svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700392 clib_spinlock_lock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700393 svm_fifo_segment_delete (fifo_segment);
394 vec_del1 (sm->segment_indices, svm_segment_index);
Florin Corasf03a59a2017-06-09 21:07:32 -0700395 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700396 }
397}
398
Florin Corasa5464812017-04-19 13:00:05 -0700399/**
400 * Allocates shm queue in the first segment
401 */
402unix_shared_memory_queue_t *
403segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
404{
405 ssvm_shared_header_t *sh;
406 svm_fifo_segment_private_t *segment;
407 unix_shared_memory_queue_t *q;
408 void *oldheap;
409
410 ASSERT (sm->segment_indices != 0);
411
412 segment = svm_fifo_get_segment (sm->segment_indices[0]);
413 sh = segment->ssvm.sh;
414
415 oldheap = ssvm_push_heap (sh);
416 q =
417 unix_shared_memory_queue_init (queue_size, sizeof (session_fifo_event_t),
418 0 /* consumer pid */ , 0
419 /* signal when queue non-empty */ );
420 ssvm_pop_heap (oldheap);
421 return q;
422}
423
424/**
425 * Frees shm queue allocated in the first segment
426 */
427void
428segment_manager_dealloc_queue (segment_manager_t * sm,
429 unix_shared_memory_queue_t * q)
430{
431 ssvm_shared_header_t *sh;
432 svm_fifo_segment_private_t *segment;
433 void *oldheap;
434
435 ASSERT (sm->segment_indices != 0);
436
437 segment = svm_fifo_get_segment (sm->segment_indices[0]);
438 sh = segment->ssvm.sh;
439
440 oldheap = ssvm_push_heap (sh);
441 unix_shared_memory_queue_free (q);
442 ssvm_pop_heap (oldheap);
443}
444
Florin Coras6cf30ad2017-04-04 23:08:23 -0700445/*
446 * fd.io coding-style-patch-verification: ON
447 *
448 * Local Variables:
449 * eval: (c-set-style "gnu")
450 * End:
451 */