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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <svm/svm_fifo_segment.h>
18 svm_fifo_segment_main_t svm_fifo_segment_main;
21 preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh,
22 svm_fifo_segment_create_args_t * a)
24 u32 rx_fifo_size, tx_fifo_size;
26 u8 *rx_fifo_space, *tx_fifo_space;
30 if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0
31 || a->preallocated_fifo_pairs == 0)
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;
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);
43 /* 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 */ );
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 */ );
53 /* Make sure it worked. Clean up if it didn't... */
54 if (rx_fifo_space == 0 || tx_fifo_space == 0)
57 clib_mem_free (rx_fifo_space);
59 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
60 a->rx_fifo_size, a->preallocated_fifo_pairs);
63 clib_mem_free (tx_fifo_space);
65 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
66 a->tx_fifo_size, a->preallocated_fifo_pairs);
70 /* Carve rx fifo space */
71 f = (svm_fifo_t *) rx_fifo_space;
72 for (i = 0; i < a->preallocated_fifo_pairs; i++)
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;
79 /* Carve tx fifo space */
80 f = (svm_fifo_t *) tx_fifo_space;
81 for (i = 0; i < a->preallocated_fifo_pairs; i++)
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;
90 /** (master) create an svm fifo segment */
92 svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
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;
101 /* Allocate a fresh segment */
102 pool_get (sm->segments, s);
103 memset (s, 0, sizeof (*s));
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;
111 rv = ssvm_master_init (&s->ssvm, s - sm->segments);
115 _vec_len (s) = vec_len (s) - 1;
119 /* Note; requested_va updated due to seg base addr randomization */
120 sm->next_baseva = s->ssvm.requested_va + a->segment_size;
123 oldheap = ssvm_push_heap (sh);
125 /* Set up svm_fifo_segment shared header */
126 fsh = clib_mem_alloc (sizeof (*fsh));
127 memset (fsh, 0, sizeof (*fsh));
130 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
132 preallocate_fifo_pairs (fsh, a);
134 ssvm_pop_heap (oldheap);
137 vec_add1 (a->new_segment_indices, s - sm->segments);
141 /** Create an svm fifo segment in process-private memory */
143 svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
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;
151 mheap_t *heap_header;
152 int segment_count = 1;
155 if (a->private_segment_count && a->private_segment_size)
159 u32 pagesize = clib_mem_get_page_size ();
162 for (i = 0; i < a->private_segment_count; i++)
164 rnd_size = (a->private_segment_size + (pagesize - 1)) & ~pagesize;
166 mem = mmap (0, rnd_size, PROT_READ | PROT_WRITE,
167 MAP_PRIVATE | MAP_ANONYMOUS,
168 -1 /* fd */ , 0 /* offset */ );
170 if (mem == MAP_FAILED)
172 clib_unix_warning ("mmap");
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);
180 segment_count = a->private_segment_count;
183 /* Spread preallocated fifo pairs across segments */
184 a->preallocated_fifo_pairs /= segment_count;
186 /* Allocate segments */
187 for (i = 0; i < segment_count; i++)
189 pool_get (sm->segments, s);
190 memset (s, 0, sizeof (*s));
192 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;
198 /* Allocate a [sic] shared memory header, in process memory... */
199 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
202 memset (sh, 0, sizeof (*sh));
203 sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap ();
205 /* Set up svm_fifo_segment shared header */
206 fsh = clib_mem_alloc (sizeof (*fsh));
207 memset (fsh, 0, sizeof (*fsh));
210 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
212 if (a->private_segment_count)
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);
221 vec_add1 (a->new_segment_indices, s - sm->segments);
227 /** (slave) attach to an svm fifo segment */
229 svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
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;
237 /* Allocate a fresh segment */
238 pool_get (sm->segments, s);
239 memset (s, 0, sizeof (*s));
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;
246 rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
250 _vec_len (s) = vec_len (s) - 1;
254 /* Fish the segment header */
256 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
259 vec_add1 (a->new_segment_indices, s - sm->segments);
264 svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
266 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
267 ssvm_delete (&s->ssvm);
268 pool_put (sm->segments, s);
272 svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
273 u32 data_size_in_bytes,
274 svm_fifo_segment_freelist_t list_index)
276 ssvm_shared_header_t *sh;
277 svm_fifo_segment_header_t *fsh;
282 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
284 ssvm_lock_non_recursive (sh, 1);
285 oldheap = ssvm_push_heap (sh);
289 case FIFO_SEGMENT_RX_FREELIST:
290 case FIFO_SEGMENT_TX_FREELIST:
291 f = fsh->free_fifos[list_index];
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;
302 case FIFO_SEGMENT_FREELIST_NONE:
306 clib_warning ("ignore bogus freelist %d", list_index);
310 /* Note: this can fail, in which case: create another segment */
311 f = svm_fifo_create (data_size_in_bytes);
312 if (PREDICT_FALSE (f == 0))
314 ssvm_pop_heap (oldheap);
315 ssvm_unlock_non_recursive (sh);
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
324 if (list_index == FIFO_SEGMENT_RX_FREELIST)
328 fsh->fifos->prev = f;
329 f->next = fsh->fifos;
334 ssvm_pop_heap (oldheap);
335 ssvm_unlock_non_recursive (sh);
340 svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
341 svm_fifo_segment_freelist_t list_index)
343 ssvm_shared_header_t *sh;
344 svm_fifo_segment_header_t *fsh;
349 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
351 ssvm_lock_non_recursive (sh, 2);
352 oldheap = ssvm_push_heap (sh);
356 case FIFO_SEGMENT_RX_FREELIST:
357 /* Remove from active list */
359 f->prev->next = f->next;
361 fsh->fifos = f->next;
363 f->next->prev = f->prev;
364 /* Fall through: we add only rx fifos to active pool */
365 case FIFO_SEGMENT_TX_FREELIST:
366 /* Add to free list */
367 f->next = fsh->free_fifos[list_index];
369 fsh->free_fifos[list_index] = f;
371 case FIFO_SEGMENT_FREELIST_NONE:
375 clib_warning ("ignore bogus freelist %d", list_index);
379 ssvm_pop_heap (oldheap);
380 ssvm_unlock_non_recursive (sh);
384 svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
386 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
388 sm->next_baseva = baseva;
389 sm->timeout_in_seconds = timeout_in_seconds;
393 svm_fifo_segment_index (svm_fifo_segment_private_t * s)
395 return s - svm_fifo_segment_main.segments;
399 * fd.io coding-style-patch-verification: ON
402 * eval: (c-set-style "gnu")