blob: 69d4ecb9387fecb765e72160556a603e5badce3c [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 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 <svm/svm_fifo_segment.h>
17
18svm_fifo_segment_main_t svm_fifo_segment_main;
19
Dave Barach10d8cc62017-05-30 09:30:07 -040020static void
21preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh,
22 svm_fifo_segment_create_args_t * a)
23{
24 u32 rx_fifo_size, tx_fifo_size;
25 svm_fifo_t *f;
26 u8 *rx_fifo_space, *tx_fifo_space;
27 int i;
28
29 /* Parameter check */
30 if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0
31 || a->preallocated_fifo_pairs == 0)
32 return;
33
34 /* Calculate space requirements */
35 rx_fifo_size = (sizeof (*f) + a->rx_fifo_size) * a->preallocated_fifo_pairs;
36 tx_fifo_size = (sizeof (*f) + a->tx_fifo_size) * a->preallocated_fifo_pairs;
37
Dave Barach2c25a622017-06-26 11:35:07 -040038 if (0)
39 clib_warning ("rx_fifo_size %u (%d mb), tx_fifo_size %u (%d mb)",
40 rx_fifo_size, rx_fifo_size >> 20,
41 tx_fifo_size, tx_fifo_size >> 20);
42
Dave Barach10d8cc62017-05-30 09:30:07 -040043 /* Allocate rx fifo space. May fail. */
44 rx_fifo_space = clib_mem_alloc_aligned_at_offset
45 (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
46 0 /* os_out_of_memory */ );
47
48 /* Same for TX */
49 tx_fifo_space = clib_mem_alloc_aligned_at_offset
50 (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
51 0 /* os_out_of_memory */ );
52
53 /* Make sure it worked. Clean up if it didn't... */
54 if (rx_fifo_space == 0 || tx_fifo_space == 0)
55 {
56 if (rx_fifo_space)
57 clib_mem_free (rx_fifo_space);
58 else
59 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
60 a->rx_fifo_size, a->preallocated_fifo_pairs);
61
62 if (tx_fifo_space)
63 clib_mem_free (tx_fifo_space);
64 else
65 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
66 a->tx_fifo_size, a->preallocated_fifo_pairs);
67 return;
68 }
69
70 /* Carve rx fifo space */
71 f = (svm_fifo_t *) rx_fifo_space;
72 for (i = 0; i < a->preallocated_fifo_pairs; i++)
73 {
74 f->next = fsh->free_fifos[FIFO_SEGMENT_RX_FREELIST];
75 fsh->free_fifos[FIFO_SEGMENT_RX_FREELIST] = f;
76 rx_fifo_space += sizeof (*f) + a->rx_fifo_size;
77 f = (svm_fifo_t *) rx_fifo_space;
78 }
79 /* Carve tx fifo space */
80 f = (svm_fifo_t *) tx_fifo_space;
81 for (i = 0; i < a->preallocated_fifo_pairs; i++)
82 {
83 f->next = fsh->free_fifos[FIFO_SEGMENT_TX_FREELIST];
84 fsh->free_fifos[FIFO_SEGMENT_TX_FREELIST] = f;
85 tx_fifo_space += sizeof (*f) + a->tx_fifo_size;
86 f = (svm_fifo_t *) tx_fifo_space;
87 }
88}
89
Dave Barach68b0fb02017-02-28 15:15:56 -050090/** (master) create an svm fifo segment */
91int
92svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
93{
94 int rv;
95 svm_fifo_segment_private_t *s;
96 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
97 ssvm_shared_header_t *sh;
98 svm_fifo_segment_header_t *fsh;
99 void *oldheap;
100
101 /* Allocate a fresh segment */
102 pool_get (sm->segments, s);
103 memset (s, 0, sizeof (*s));
104
105 s->ssvm.ssvm_size = a->segment_size;
106 s->ssvm.i_am_master = 1;
107 s->ssvm.my_pid = getpid ();
108 s->ssvm.name = (u8 *) a->segment_name;
109 s->ssvm.requested_va = sm->next_baseva;
110
111 rv = ssvm_master_init (&s->ssvm, s - sm->segments);
112
113 if (rv)
114 {
115 _vec_len (s) = vec_len (s) - 1;
116 return (rv);
117 }
118
119 /* Note; requested_va updated due to seg base addr randomization */
120 sm->next_baseva = s->ssvm.requested_va + a->segment_size;
121
122 sh = s->ssvm.sh;
123 oldheap = ssvm_push_heap (sh);
124
125 /* Set up svm_fifo_segment shared header */
126 fsh = clib_mem_alloc (sizeof (*fsh));
127 memset (fsh, 0, sizeof (*fsh));
128 sh->opaque[0] = fsh;
129 s->h = fsh;
130 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
131
Dave Barach10d8cc62017-05-30 09:30:07 -0400132 preallocate_fifo_pairs (fsh, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500133
134 ssvm_pop_heap (oldheap);
135
136 sh->ready = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400137 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach68b0fb02017-02-28 15:15:56 -0500138 return (0);
139}
140
Florin Corasa5464812017-04-19 13:00:05 -0700141/** Create an svm fifo segment in process-private memory */
142int
143svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
144{
145 svm_fifo_segment_private_t *s;
146 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
147 ssvm_shared_header_t *sh;
148 svm_fifo_segment_header_t *fsh;
Dave Barach2c25a622017-06-26 11:35:07 -0400149 void *oldheap;
150 u8 **heaps = 0;
151 mheap_t *heap_header;
152 int segment_count = 1;
153 int i;
Florin Corasa5464812017-04-19 13:00:05 -0700154
Dave Barach2c25a622017-06-26 11:35:07 -0400155 if (a->private_segment_count && a->private_segment_size)
156 {
157 void *mem;
158 u8 *heap;
159 u32 pagesize = clib_mem_get_page_size ();
160 u32 rnd_size;
Florin Corasa5464812017-04-19 13:00:05 -0700161
Dave Barach2c25a622017-06-26 11:35:07 -0400162 for (i = 0; i < a->private_segment_count; i++)
163 {
164 rnd_size = (a->private_segment_size + (pagesize - 1)) & ~pagesize;
Florin Corasa5464812017-04-19 13:00:05 -0700165
Dave Barach2c25a622017-06-26 11:35:07 -0400166 mem = mmap (0, rnd_size, PROT_READ | PROT_WRITE,
167 MAP_PRIVATE | MAP_ANONYMOUS,
168 -1 /* fd */ , 0 /* offset */ );
Florin Corasa5464812017-04-19 13:00:05 -0700169
Dave Barach2c25a622017-06-26 11:35:07 -0400170 if (mem == MAP_FAILED)
171 {
172 clib_unix_warning ("mmap");
173 return -1;
174 }
175 heap = mheap_alloc (mem, rnd_size);
176 heap_header = mheap_header (heap);
177 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
178 vec_add1 (heaps, heap);
179 }
180 segment_count = a->private_segment_count;
181 }
Florin Corasa5464812017-04-19 13:00:05 -0700182
Dave Barach2c25a622017-06-26 11:35:07 -0400183 /* Spread preallocated fifo pairs across segments */
184 a->preallocated_fifo_pairs /= segment_count;
Florin Corasa5464812017-04-19 13:00:05 -0700185
Dave Barach2c25a622017-06-26 11:35:07 -0400186 /* Allocate segments */
187 for (i = 0; i < segment_count; i++)
188 {
189 pool_get (sm->segments, s);
190 memset (s, 0, sizeof (*s));
Dave Barach10d8cc62017-05-30 09:30:07 -0400191
Dave Barach2c25a622017-06-26 11:35:07 -0400192 s->ssvm.ssvm_size = ~0;
193 s->ssvm.i_am_master = 1;
194 s->ssvm.my_pid = getpid ();
195 s->ssvm.name = (u8 *) a->segment_name;
196 s->ssvm.requested_va = ~0;
197
198 /* Allocate a [sic] shared memory header, in process memory... */
199 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
200 s->ssvm.sh = sh;
201
202 memset (sh, 0, sizeof (*sh));
203 sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap ();
204
205 /* Set up svm_fifo_segment shared header */
206 fsh = clib_mem_alloc (sizeof (*fsh));
207 memset (fsh, 0, sizeof (*fsh));
208 sh->opaque[0] = fsh;
209 s->h = fsh;
210 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
211
212 if (a->private_segment_count)
213 {
214 oldheap = clib_mem_get_heap ();
215 clib_mem_set_heap (sh->heap);
216 preallocate_fifo_pairs (fsh, a);
217 clib_mem_set_heap (oldheap);
218 }
219
220 sh->ready = 1;
221 vec_add1 (a->new_segment_indices, s - sm->segments);
222 }
223 vec_free (heaps);
Florin Corasa5464812017-04-19 13:00:05 -0700224 return (0);
225}
226
Dave Barach68b0fb02017-02-28 15:15:56 -0500227/** (slave) attach to an svm fifo segment */
228int
229svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
230{
231 int rv;
232 svm_fifo_segment_private_t *s;
233 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
234 ssvm_shared_header_t *sh;
235 svm_fifo_segment_header_t *fsh;
236
237 /* Allocate a fresh segment */
238 pool_get (sm->segments, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500239 memset (s, 0, sizeof (*s));
240
241 s->ssvm.ssvm_size = a->segment_size;
242 s->ssvm.my_pid = getpid ();
243 s->ssvm.name = (u8 *) a->segment_name;
244 s->ssvm.requested_va = sm->next_baseva;
245
246 rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
247
248 if (rv)
249 {
250 _vec_len (s) = vec_len (s) - 1;
251 return (rv);
252 }
253
254 /* Fish the segment header */
255 sh = s->ssvm.sh;
256 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
257 s->h = fsh;
258
Dave Barach2c25a622017-06-26 11:35:07 -0400259 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach68b0fb02017-02-28 15:15:56 -0500260 return (0);
261}
262
263void
264svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
265{
266 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
267 ssvm_delete (&s->ssvm);
268 pool_put (sm->segments, s);
269}
270
271svm_fifo_t *
272svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
Dave Barach10d8cc62017-05-30 09:30:07 -0400273 u32 data_size_in_bytes,
274 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500275{
276 ssvm_shared_header_t *sh;
277 svm_fifo_segment_header_t *fsh;
278 svm_fifo_t *f;
279 void *oldheap;
280
281 sh = s->ssvm.sh;
282 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Florin Corasa5464812017-04-19 13:00:05 -0700283
Dave Barach2c25a622017-06-26 11:35:07 -0400284 ssvm_lock_non_recursive (sh, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285 oldheap = ssvm_push_heap (sh);
286
Dave Barach10d8cc62017-05-30 09:30:07 -0400287 switch (list_index)
288 {
289 case FIFO_SEGMENT_RX_FREELIST:
290 case FIFO_SEGMENT_TX_FREELIST:
291 f = fsh->free_fifos[list_index];
292 if (f)
293 {
294 fsh->free_fifos[list_index] = f->next;
295 /* (re)initialize the fifo, as in svm_fifo_create */
296 memset (f, 0, sizeof (*f));
297 f->nitems = data_size_in_bytes;
298 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
299 goto found;
300 }
301 /* FALLTHROUGH */
302 case FIFO_SEGMENT_FREELIST_NONE:
303 break;
304
305 default:
306 clib_warning ("ignore bogus freelist %d", list_index);
307 break;
308 }
309
Dave Barach68b0fb02017-02-28 15:15:56 -0500310 /* Note: this can fail, in which case: create another segment */
311 f = svm_fifo_create (data_size_in_bytes);
Florin Corasa5464812017-04-19 13:00:05 -0700312 if (PREDICT_FALSE (f == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500313 {
314 ssvm_pop_heap (oldheap);
Dave Barach2c25a622017-06-26 11:35:07 -0400315 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500316 return (0);
317 }
318
Dave Barach10d8cc62017-05-30 09:30:07 -0400319found:
320 /* If rx_freelist add to active fifos list. When cleaning up segment,
321 * we need a list of active sessions that should be disconnected. Since
322 * both rx and tx fifos keep pointers to the session, it's enough to track
323 * only one. */
324 if (list_index == FIFO_SEGMENT_RX_FREELIST)
325 {
326 if (fsh->fifos)
327 {
328 fsh->fifos->prev = f;
329 f->next = fsh->fifos;
330 }
331 fsh->fifos = f;
332 }
333
Dave Barach68b0fb02017-02-28 15:15:56 -0500334 ssvm_pop_heap (oldheap);
Dave Barach2c25a622017-06-26 11:35:07 -0400335 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 return (f);
337}
338
339void
Dave Barach10d8cc62017-05-30 09:30:07 -0400340svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
341 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500342{
343 ssvm_shared_header_t *sh;
344 svm_fifo_segment_header_t *fsh;
345 void *oldheap;
Dave Barach68b0fb02017-02-28 15:15:56 -0500346
Dave Barach2c25a622017-06-26 11:35:07 -0400347
Dave Barach68b0fb02017-02-28 15:15:56 -0500348 sh = s->ssvm.sh;
349 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Dave Barach68b0fb02017-02-28 15:15:56 -0500350
Dave Barach2c25a622017-06-26 11:35:07 -0400351 ssvm_lock_non_recursive (sh, 2);
Florin Corasa5464812017-04-19 13:00:05 -0700352 oldheap = ssvm_push_heap (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500353
Dave Barach10d8cc62017-05-30 09:30:07 -0400354 switch (list_index)
355 {
356 case FIFO_SEGMENT_RX_FREELIST:
357 /* Remove from active list */
358 if (f->prev)
359 f->prev->next = f->next;
Florin Corasf03a59a2017-06-09 21:07:32 -0700360 else
361 fsh->fifos = f->next;
Dave Barach10d8cc62017-05-30 09:30:07 -0400362 if (f->next)
363 f->next->prev = f->prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700364 /* Fall through: we add only rx fifos to active pool */
Dave Barach10d8cc62017-05-30 09:30:07 -0400365 case FIFO_SEGMENT_TX_FREELIST:
366 /* Add to free list */
367 f->next = fsh->free_fifos[list_index];
Florin Corasf03a59a2017-06-09 21:07:32 -0700368 f->prev = 0;
Dave Barach10d8cc62017-05-30 09:30:07 -0400369 fsh->free_fifos[list_index] = f;
Florin Corasf03a59a2017-06-09 21:07:32 -0700370 break;
Dave Barach10d8cc62017-05-30 09:30:07 -0400371 case FIFO_SEGMENT_FREELIST_NONE:
372 break;
373
374 default:
375 clib_warning ("ignore bogus freelist %d", list_index);
376 break;
377 }
378
Dave Barach68b0fb02017-02-28 15:15:56 -0500379 ssvm_pop_heap (oldheap);
Dave Barach2c25a622017-06-26 11:35:07 -0400380 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500381}
382
383void
384svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
385{
386 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
387
388 sm->next_baseva = baseva;
389 sm->timeout_in_seconds = timeout_in_seconds;
390}
391
392u32
393svm_fifo_segment_index (svm_fifo_segment_private_t * s)
394{
395 return s - svm_fifo_segment_main.segments;
396}
397
398/*
399 * fd.io coding-style-patch-verification: ON
400 *
401 * Local Variables:
402 * eval: (c-set-style "gnu")
403 * End:
404 */