blob: caf8eaa308a65937262444b461a8ab115f8365a8 [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 */
33u32 private_segment_index = ~0;
34
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);
70 vec_free (segment_name);
71 return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL;
72 }
73
74 vec_add1 (sm->segment_indices, ca->new_segment_index);
75
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
115 if (private_segment_index != ~0)
116 return;
117
118 memset (a, 0, sizeof (*a));
119 a->segment_name = "process-private-segment";
120 a->segment_size = ~0;
121 a->new_segment_index = ~0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400122 a->rx_fifo_size = props->rx_fifo_size;
123 a->tx_fifo_size = props->tx_fifo_size;
124 a->preallocated_fifo_pairs = props->preallocated_fifo_pairs;
Florin Corasa5464812017-04-19 13:00:05 -0700125
126 if (svm_fifo_segment_create_process_private (a))
127 clib_warning ("Failed to create process private segment");
128
129 private_segment_index = a->new_segment_index;
130 ASSERT (private_segment_index != ~0);
131}
132
Florin Coras6cf30ad2017-04-04 23:08:23 -0700133/**
134 * Initializes segment manager based on options provided.
135 * Returns error if svm segment allocation fails.
136 */
137int
138segment_manager_init (segment_manager_t * sm,
139 segment_manager_properties_t * properties,
140 u32 first_seg_size)
141{
142 int rv;
143
144 /* app allocates these */
145 sm->properties = properties;
146
Florin Corasa5464812017-04-19 13:00:05 -0700147 first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
148
149 if (sm->properties->use_private_segment == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700150 {
151 rv = session_manager_add_first_segment (sm, first_seg_size);
152 if (rv)
153 {
154 clib_warning ("Failed to allocate segment");
155 return rv;
156 }
157 }
Florin Corasa5464812017-04-19 13:00:05 -0700158 else
159 {
160 if (private_segment_index == ~0)
Dave Barach10d8cc62017-05-30 09:30:07 -0400161 segment_manager_alloc_process_private_segment (properties);
Florin Corasa5464812017-04-19 13:00:05 -0700162 ASSERT (private_segment_index != ~0);
163 vec_add1 (sm->segment_indices, private_segment_index);
164 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700165
Florin Corasa5464812017-04-19 13:00:05 -0700166 clib_spinlock_init (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700167 return 0;
168}
169
170/**
171 * Removes segment manager.
172 *
173 * Since the fifos allocated in the segment keep backpointers to the sessions
174 * prior to removing the segment, we call session disconnect. This
175 * subsequently propages into transport.
176 */
177void
178segment_manager_del (segment_manager_t * sm)
179{
Dave Barach10d8cc62017-05-30 09:30:07 -0400180 int j;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700181
182 /* Across all fifo segments used by the server */
183 for (j = 0; j < vec_len (sm->segment_indices); j++)
184 {
185 svm_fifo_segment_private_t *fifo_segment;
Dave Barach10d8cc62017-05-30 09:30:07 -0400186 svm_fifo_t *fifo;
187
Florin Coras6cf30ad2017-04-04 23:08:23 -0700188 /* Vector of fifos allocated in the segment */
189 fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400190 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700191
192 /*
193 * Remove any residual sessions from the session lookup table
194 * Don't bother deleting the individual fifos, we're going to
195 * throw away the fifo segment in a minute.
196 */
Dave Barach10d8cc62017-05-30 09:30:07 -0400197 while (fifo)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700198 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700199 u32 session_index, thread_index;
200 stream_session_t *session;
201
Florin Corasa5464812017-04-19 13:00:05 -0700202 session_index = fifo->master_session_index;
203 thread_index = fifo->master_thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700204
205 session = stream_session_get (session_index, thread_index);
206
Florin Coras6cf30ad2017-04-04 23:08:23 -0700207 /* Instead of directly removing the session call disconnect */
Florin Corasa5464812017-04-19 13:00:05 -0700208 session_send_session_evt_to_thread (stream_session_handle (session),
209 FIFO_EVENT_DISCONNECT,
Dave Barach10d8cc62017-05-30 09:30:07 -0400210 thread_index);
211 fifo = fifo->next;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700212 }
213
Dave Barach10d8cc62017-05-30 09:30:07 -0400214 /* Instead of removing the segment, test when cleaning up disconnected
215 * sessions if the segment can be removed.
Florin Coras6cf30ad2017-04-04 23:08:23 -0700216 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700217 }
218
Florin Corasa5464812017-04-19 13:00:05 -0700219 clib_spinlock_free (&sm->lockp);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700220 pool_put (segment_managers, sm);
221}
222
223static int
224segment_manager_notify_app_seg_add (segment_manager_t * sm,
225 u32 fifo_segment_index)
226{
227 application_t *app = application_get (sm->app_index);
228 u32 seg_size = 0;
229 u8 *seg_name;
230
231 /* Send an API message to the external app, to map new segment */
232 ASSERT (app->cb_fns.add_segment_callback);
233
234 segment_manager_get_segment_info (fifo_segment_index, &seg_name, &seg_size);
235 return app->cb_fns.add_segment_callback (app->api_client_index, seg_name,
236 seg_size);
237}
238
239int
240segment_manager_alloc_session_fifos (segment_manager_t * sm,
241 svm_fifo_t ** server_rx_fifo,
242 svm_fifo_t ** server_tx_fifo,
243 u32 * fifo_segment_index)
244{
245 svm_fifo_segment_private_t *fifo_segment;
246 u32 fifo_size, sm_index;
247 u8 added_a_segment = 0;
248 int i;
249
Florin Coras6cf30ad2017-04-04 23:08:23 -0700250 ASSERT (vec_len (sm->segment_indices));
251
Florin Corasa5464812017-04-19 13:00:05 -0700252 /* Make sure we don't have multiple threads trying to allocate segments
253 * at the same time. */
254 clib_spinlock_lock (&sm->lockp);
255
256 /* Allocate svm fifos */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700257again:
258 for (i = 0; i < vec_len (sm->segment_indices); i++)
259 {
260 *fifo_segment_index = sm->segment_indices[i];
261 fifo_segment = svm_fifo_get_segment (*fifo_segment_index);
262
Dave Barach10d8cc62017-05-30 09:30:07 -0400263 /* FC: cleanup, make sure sm->properties->xxx_fifo_size always set */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700264 fifo_size = sm->properties->rx_fifo_size;
265 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400266 *server_rx_fifo =
267 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
268 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700269
Dave Barach10d8cc62017-05-30 09:30:07 -0400270 /* FC: cleanup, make sure sm->properties->xxx_fifo_size always set */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700271 fifo_size = sm->properties->tx_fifo_size;
272 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400273 *server_tx_fifo =
274 svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
275 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700276
277 if (*server_rx_fifo == 0)
278 {
279 /* This would be very odd, but handle it... */
280 if (*server_tx_fifo != 0)
281 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400282 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo,
283 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700284 *server_tx_fifo = 0;
285 }
286 continue;
287 }
288 if (*server_tx_fifo == 0)
289 {
290 if (*server_rx_fifo != 0)
291 {
Dave Barach10d8cc62017-05-30 09:30:07 -0400292 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo,
293 FIFO_SEGMENT_RX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700294 *server_rx_fifo = 0;
295 }
296 continue;
297 }
298 break;
299 }
300
301 /* See if we're supposed to create another segment */
302 if (*server_rx_fifo == 0)
303 {
304 if (sm->properties->add_segment)
305 {
306 if (added_a_segment)
307 {
308 clib_warning ("added a segment, still cant allocate a fifo");
309 return SESSION_ERROR_NEW_SEG_NO_SPACE;
310 }
311
312 if (session_manager_add_segment (sm))
Florin Corasa5464812017-04-19 13:00:05 -0700313 {
314 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
315 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700316
317 added_a_segment = 1;
318 goto again;
319 }
320 else
321 {
322 clib_warning ("No space to allocate fifos!");
323 return SESSION_ERROR_NO_SPACE;
324 }
325 }
326
Florin Coras6cf30ad2017-04-04 23:08:23 -0700327 /* Backpointers to segment manager */
328 sm_index = segment_manager_index (sm);
329 (*server_tx_fifo)->segment_manager = sm_index;
330 (*server_rx_fifo)->segment_manager = sm_index;
331
Florin Corasa5464812017-04-19 13:00:05 -0700332 clib_spinlock_unlock (&sm->lockp);
333
334 if (added_a_segment)
335 return segment_manager_notify_app_seg_add (sm, *fifo_segment_index);
336
Florin Coras6cf30ad2017-04-04 23:08:23 -0700337 return 0;
338}
339
340void
341segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
342 svm_fifo_t * tx_fifo)
343{
344 segment_manager_t *sm;
345 svm_fifo_segment_private_t *fifo_segment;
346
Florin Corasa5464812017-04-19 13:00:05 -0700347 sm = segment_manager_get_if_valid (rx_fifo->segment_manager);
348
349 /* It's possible to have no segment manager if the session was removed
350 * as result of a detach */
351 if (!sm)
352 return;
353
Florin Coras6cf30ad2017-04-04 23:08:23 -0700354 fifo_segment = svm_fifo_get_segment (svm_segment_index);
Dave Barach10d8cc62017-05-30 09:30:07 -0400355 svm_fifo_segment_free_fifo (fifo_segment, rx_fifo,
356 FIFO_SEGMENT_RX_FREELIST);
357 svm_fifo_segment_free_fifo (fifo_segment, tx_fifo,
358 FIFO_SEGMENT_TX_FREELIST);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700359
Florin Corasa5464812017-04-19 13:00:05 -0700360 /* Remove segment only if it holds no fifos and not the first */
361 if (sm->segment_indices[0] != svm_segment_index
362 && !svm_fifo_segment_has_fifos (fifo_segment))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700363 {
Florin Corasa5464812017-04-19 13:00:05 -0700364 svm_fifo_segment_delete (fifo_segment);
365 vec_del1 (sm->segment_indices, svm_segment_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700366 }
367}
368
Florin Corasa5464812017-04-19 13:00:05 -0700369/**
370 * Allocates shm queue in the first segment
371 */
372unix_shared_memory_queue_t *
373segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size)
374{
375 ssvm_shared_header_t *sh;
376 svm_fifo_segment_private_t *segment;
377 unix_shared_memory_queue_t *q;
378 void *oldheap;
379
380 ASSERT (sm->segment_indices != 0);
381
382 segment = svm_fifo_get_segment (sm->segment_indices[0]);
383 sh = segment->ssvm.sh;
384
385 oldheap = ssvm_push_heap (sh);
386 q =
387 unix_shared_memory_queue_init (queue_size, sizeof (session_fifo_event_t),
388 0 /* consumer pid */ , 0
389 /* signal when queue non-empty */ );
390 ssvm_pop_heap (oldheap);
391 return q;
392}
393
394/**
395 * Frees shm queue allocated in the first segment
396 */
397void
398segment_manager_dealloc_queue (segment_manager_t * sm,
399 unix_shared_memory_queue_t * q)
400{
401 ssvm_shared_header_t *sh;
402 svm_fifo_segment_private_t *segment;
403 void *oldheap;
404
405 ASSERT (sm->segment_indices != 0);
406
407 segment = svm_fifo_get_segment (sm->segment_indices[0]);
408 sh = segment->ssvm.sh;
409
410 oldheap = ssvm_push_heap (sh);
411 unix_shared_memory_queue_free (q);
412 ssvm_pop_heap (oldheap);
413}
414
Florin Coras6cf30ad2017-04-04 23:08:23 -0700415/*
416 * fd.io coding-style-patch-verification: ON
417 *
418 * Local Variables:
419 * eval: (c-set-style "gnu")
420 * End:
421 */