blob: 43977063e0efd247dd459a7c4bba70b4f7b4df6b [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 Coras1f152cd2017-08-18 19:28:03 -0700227 session->session_state = SESSION_STATE_CLOSED;
Florin Corasa5464812017-04-19 13:00:05 -0700228 session_send_session_evt_to_thread (stream_session_handle (session),
229 FIFO_EVENT_DISCONNECT,
Dave Barach10d8cc62017-05-30 09:30:07 -0400230 thread_index);
231 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700232 }
233
Dave Barach10d8cc62017-05-30 09:30:07 -0400234 /* Instead of removing the segment, test when cleaning up disconnected
235 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700236 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700237 }
238
Dave Wallace7b749fe2017-07-05 14:30:46 -0400239 segment_manager_first_segment_maybe_del (sm);
240
Florin Corasa5464812017-04-19 13:00:05 -0700241 clib_spinlock_free (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700242 pool_put (segment_managers, sm);
243}
244
245static int
246segment_manager_notify_app_seg_add (segment_manager_t * sm,
247 u32 fifo_segment_index)
248{
249 application_t *app = application_get (sm->app_index);
250 u32 seg_size = 0;
251 u8 *seg_name;
252
253 /* Send an API message to the external app, to map new segment */
254 ASSERT (app->cb_fns.add_segment_callback);
255
256 segment_manager_get_segment_info (fifo_segment_index, &seg_name, &seg_size);
257 return app->cb_fns.add_segment_callback (app->api_client_index, seg_name,
258 seg_size);
259}
260
261int
262segment_manager_alloc_session_fifos (segment_manager_t * sm,
263 svm_fifo_t ** server_rx_fifo,
264 svm_fifo_t ** server_tx_fifo,
265 u32 * fifo_segment_index)
266{
267 svm_fifo_segment_private_t *fifo_segment;
268 u32 fifo_size, sm_index;
269 u8 added_a_segment = 0;
270 int i;
271
Florin Coras6cf30ad2017-04-04 23:08:23 -0700272 ASSERT (vec_len (sm->segment_indices));
273
Florin Corasa5464812017-04-19 13:00:05 -0700274 /* Make sure we don't have multiple threads trying to allocate segments
275 * at the same time. */
276 clib_spinlock_lock (&sm->lockp);
277
278 /* Allocate svm fifos */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700279again:
280 for (i = 0; i < vec_len (sm->segment_indices); i++)
281 {
282 *fifo_segment_index = sm->segment_indices[i];
283 fifo_segment = svm_fifo_get_segment (*fifo_segment_index);
284
Dave Barach10d8cc62017-05-30 09:30:07 -0400285 /* FC: cleanup, make sure sm->properties->xxx_fifo_size always set */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286 fifo_size = sm->properties->rx_fifo_size;
287 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400288 *server_rx_fifo =
289 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
290 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700291
Dave Barach10d8cc62017-05-30 09:30:07 -0400292 /* FC: cleanup, make sure sm->properties->xxx_fifo_size always set */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700293 fifo_size = sm->properties->tx_fifo_size;
294 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400295 *server_tx_fifo =
296 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
297 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700298
299 if (*server_rx_fifo == 0)
300 {
301 /* This would be very odd, but handle it... */
302 if (*server_tx_fifo != 0)
303 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400304 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
305 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700306 *server_tx_fifo = 0;
307 }
308 continue;
309 }
310 if (*server_tx_fifo == 0)
311 {
312 if (*server_rx_fifo != 0)
313 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400314 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
315 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700316 *server_rx_fifo = 0;
317 }
318 continue;
319 }
320 break;
321 }
322
323 /* See if we're supposed to create another segment */
324 if (*server_rx_fifo == 0)
325 {
Dave Barach2c25a622017-06-26 11:35:07 -0400326 if (sm->properties->add_segment && !sm->properties->use_private_segment)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700327 {
328 if (added_a_segment)
329 {
330 clib_warning ("added a segment, still cant allocate a fifo");
Florin Corasf03a59a2017-06-09 21:07:32 -0700331 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700332 return SESSION_ERROR_NEW_SEG_NO_SPACE;
333 }
334
335 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700336 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700337 clib_spinlock_unlock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700338 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
339 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700340
341 added_a_segment = 1;
342 goto again;
343 }
344 else
345 {
346 clib_warning ("No space to allocate fifos!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700347 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348 return SESSION_ERROR_NO_SPACE;
349 }
350 }
351
Florin Coras6cf30ad2017-04-04 23:08:23 -0700352 /* Backpointers to segment manager */
353 sm_index = segment_manager_index (sm);
354 (*server_tx_fifo)->segment_manager = sm_index;
355 (*server_rx_fifo)->segment_manager = sm_index;
356
Florin Corasa5464812017-04-19 13:00:05 -0700357 clib_spinlock_unlock (&sm->lockp);
358
359 if (added_a_segment)
360 return segment_manager_notify_app_seg_add (sm, *fifo_segment_index);
361
Florin Coras6cf30ad2017-04-04 23:08:23 -0700362 return 0;
363}
364
365void
366segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
367 svm_fifo_t * tx_fifo)
368{
369 segment_manager_t *sm;
370 svm_fifo_segment_private_t *fifo_segment;
371
Florin Corasa5464812017-04-19 13:00:05 -0700372 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
373
374 /* It's possible to have no segment manager if the session was removed
375 * as result of a detach */
376 if (!sm)
377 return;
378
Florin Coras6cf30ad2017-04-04 23:08:23 -0700379 fifo_segment = svm_fifo_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400380 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
381 FIFO_SEGMENT_RX_FREELIST);
382 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
383 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700384
Dave Barach2c25a622017-06-26 11:35:07 -0400385 /* Don't try to delete process-private segments */
386 if (sm->properties->private_segment_count > 0)
387 return;
388
Florin Corasa5464812017-04-19 13:00:05 -0700389 /* Remove segment only if it holds no fifos and not the first */
390 if (sm->segment_indices[0] != svm_segment_index
391 && !svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700392 {
Florin Corasf03a59a2017-06-09 21:07:32 -0700393 clib_spinlock_lock (&sm->lockp);
Florin Corasa5464812017-04-19 13:00:05 -0700394 svm_fifo_segment_delete (fifo_segment);
395 vec_del1 (sm->segment_indices, svm_segment_index);
Florin Corasf03a59a2017-06-09 21:07:32 -0700396 clib_spinlock_unlock (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700397 }
398}
399
Florin Corasa5464812017-04-19 13:00:05 -0700400/**
401 * Allocates shm queue in the first segment
402 */
403unix_shared_memory_queue_t *
404segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
405{
406 ssvm_shared_header_t *sh;
407 svm_fifo_segment_private_t *segment;
408 unix_shared_memory_queue_t *q;
409 void *oldheap;
410
411 ASSERT (sm->segment_indices != 0);
412
413 segment = svm_fifo_get_segment (sm->segment_indices[0]);
414 sh = segment->ssvm.sh;
415
416 oldheap = ssvm_push_heap (sh);
417 q =
418 unix_shared_memory_queue_init (queue_size, sizeof (session_fifo_event_t),
419 0 /* consumer pid */ , 0
420 /* signal when queue non-empty */ );
421 ssvm_pop_heap (oldheap);
422 return q;
423}
424
425/**
426 * Frees shm queue allocated in the first segment
427 */
428void
429segment_manager_dealloc_queue (segment_manager_t * sm,
430 unix_shared_memory_queue_t * q)
431{
432 ssvm_shared_header_t *sh;
433 svm_fifo_segment_private_t *segment;
434 void *oldheap;
435
436 ASSERT (sm->segment_indices != 0);
437
438 segment = svm_fifo_get_segment (sm->segment_indices[0]);
439 sh = segment->ssvm.sh;
440
441 oldheap = ssvm_push_heap (sh);
442 unix_shared_memory_queue_free (q);
443 ssvm_pop_heap (oldheap);
444}
445
Florin Coras6cf30ad2017-04-04 23:08:23 -0700446/*
447 * fd.io coding-style-patch-verification: ON
448 *
449 * Local Variables:
450 * eval: (c-set-style "gnu")
451 * End:
452 */