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;
38 /* Allocate rx fifo space. May fail. */
39 rx_fifo_space = clib_mem_alloc_aligned_at_offset
40 (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
41 0 /* os_out_of_memory */ );
44 tx_fifo_space = clib_mem_alloc_aligned_at_offset
45 (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
46 0 /* os_out_of_memory */ );
48 /* Make sure it worked. Clean up if it didn't... */
49 if (rx_fifo_space == 0 || tx_fifo_space == 0)
52 clib_mem_free (rx_fifo_space);
54 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
55 a->rx_fifo_size, a->preallocated_fifo_pairs);
58 clib_mem_free (tx_fifo_space);
60 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
61 a->tx_fifo_size, a->preallocated_fifo_pairs);
65 /* Carve rx fifo space */
66 f = (svm_fifo_t *) rx_fifo_space;
67 for (i = 0; i < a->preallocated_fifo_pairs; i++)
69 f->next = fsh->free_fifos[FIFO_SEGMENT_RX_FREELIST];
70 fsh->free_fifos[FIFO_SEGMENT_RX_FREELIST] = f;
71 rx_fifo_space += sizeof (*f) + a->rx_fifo_size;
72 f = (svm_fifo_t *) rx_fifo_space;
74 /* Carve tx fifo space */
75 f = (svm_fifo_t *) tx_fifo_space;
76 for (i = 0; i < a->preallocated_fifo_pairs; i++)
78 f->next = fsh->free_fifos[FIFO_SEGMENT_TX_FREELIST];
79 fsh->free_fifos[FIFO_SEGMENT_TX_FREELIST] = f;
80 tx_fifo_space += sizeof (*f) + a->tx_fifo_size;
81 f = (svm_fifo_t *) tx_fifo_space;
85 /** (master) create an svm fifo segment */
87 svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
90 svm_fifo_segment_private_t *s;
91 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
92 ssvm_shared_header_t *sh;
93 svm_fifo_segment_header_t *fsh;
96 /* Allocate a fresh segment */
97 pool_get (sm->segments, s);
98 memset (s, 0, sizeof (*s));
100 s->ssvm.ssvm_size = a->segment_size;
101 s->ssvm.i_am_master = 1;
102 s->ssvm.my_pid = getpid ();
103 s->ssvm.name = (u8 *) a->segment_name;
104 s->ssvm.requested_va = sm->next_baseva;
106 rv = ssvm_master_init (&s->ssvm, s - sm->segments);
110 _vec_len (s) = vec_len (s) - 1;
114 /* Note; requested_va updated due to seg base addr randomization */
115 sm->next_baseva = s->ssvm.requested_va + a->segment_size;
118 oldheap = ssvm_push_heap (sh);
120 /* Set up svm_fifo_segment shared header */
121 fsh = clib_mem_alloc (sizeof (*fsh));
122 memset (fsh, 0, sizeof (*fsh));
125 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
127 preallocate_fifo_pairs (fsh, a);
129 ssvm_pop_heap (oldheap);
132 a->new_segment_index = s - sm->segments;
136 /** Create an svm fifo segment in process-private memory */
138 svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
140 svm_fifo_segment_private_t *s;
141 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
142 ssvm_shared_header_t *sh;
143 svm_fifo_segment_header_t *fsh;
145 /* Allocate a fresh segment */
146 pool_get (sm->segments, s);
147 memset (s, 0, sizeof (*s));
149 s->ssvm.ssvm_size = ~0;
150 s->ssvm.i_am_master = 1;
151 s->ssvm.my_pid = getpid ();
152 s->ssvm.name = (u8 *) a->segment_name;
153 s->ssvm.requested_va = ~0;
155 /* Allocate a [sic] shared memory header, in process memory... */
156 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
159 memset (sh, 0, sizeof (*sh));
160 sh->heap = clib_mem_get_heap ();
162 /* Set up svm_fifo_segment shared header */
163 fsh = clib_mem_alloc (sizeof (*fsh));
164 memset (fsh, 0, sizeof (*fsh));
167 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
169 preallocate_fifo_pairs (fsh, a);
172 a->new_segment_index = s - sm->segments;
176 /** (slave) attach to an svm fifo segment */
178 svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
181 svm_fifo_segment_private_t *s;
182 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
183 ssvm_shared_header_t *sh;
184 svm_fifo_segment_header_t *fsh;
186 /* Allocate a fresh segment */
187 pool_get (sm->segments, s);
188 memset (s, 0, sizeof (*s));
190 s->ssvm.ssvm_size = a->segment_size;
191 s->ssvm.my_pid = getpid ();
192 s->ssvm.name = (u8 *) a->segment_name;
193 s->ssvm.requested_va = sm->next_baseva;
195 rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
199 _vec_len (s) = vec_len (s) - 1;
203 /* Fish the segment header */
205 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
208 a->new_segment_index = s - sm->segments;
213 svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
215 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
216 ssvm_delete (&s->ssvm);
217 pool_put (sm->segments, s);
221 svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
222 u32 data_size_in_bytes,
223 svm_fifo_segment_freelist_t list_index)
225 ssvm_shared_header_t *sh;
226 svm_fifo_segment_header_t *fsh;
231 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
233 ssvm_lock (sh, 1, 0);
234 oldheap = ssvm_push_heap (sh);
238 case FIFO_SEGMENT_RX_FREELIST:
239 case FIFO_SEGMENT_TX_FREELIST:
240 f = fsh->free_fifos[list_index];
243 fsh->free_fifos[list_index] = f->next;
244 /* (re)initialize the fifo, as in svm_fifo_create */
245 memset (f, 0, sizeof (*f));
246 f->nitems = data_size_in_bytes;
247 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
251 case FIFO_SEGMENT_FREELIST_NONE:
255 clib_warning ("ignore bogus freelist %d", list_index);
259 /* Note: this can fail, in which case: create another segment */
260 f = svm_fifo_create (data_size_in_bytes);
261 if (PREDICT_FALSE (f == 0))
263 ssvm_pop_heap (oldheap);
269 /* If rx_freelist add to active fifos list. When cleaning up segment,
270 * we need a list of active sessions that should be disconnected. Since
271 * both rx and tx fifos keep pointers to the session, it's enough to track
273 if (list_index == FIFO_SEGMENT_RX_FREELIST)
277 fsh->fifos->prev = f;
278 f->next = fsh->fifos;
283 ssvm_pop_heap (oldheap);
289 svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
290 svm_fifo_segment_freelist_t list_index)
292 ssvm_shared_header_t *sh;
293 svm_fifo_segment_header_t *fsh;
297 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
299 ssvm_lock (sh, 1, 0);
300 oldheap = ssvm_push_heap (sh);
304 case FIFO_SEGMENT_RX_FREELIST:
305 /* Remove from active list */
307 f->prev->next = f->next;
309 fsh->fifos = f->next;
311 f->next->prev = f->prev;
312 /* Fall through: we add only rx fifos to active pool */
313 case FIFO_SEGMENT_TX_FREELIST:
314 /* Add to free list */
315 f->next = fsh->free_fifos[list_index];
317 fsh->free_fifos[list_index] = f;
319 case FIFO_SEGMENT_FREELIST_NONE:
323 clib_warning ("ignore bogus freelist %d", list_index);
327 ssvm_pop_heap (oldheap);
332 svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
334 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
336 sm->next_baseva = baseva;
337 sm->timeout_in_seconds = timeout_in_seconds;
341 svm_fifo_segment_index (svm_fifo_segment_private_t * s)
343 return s - svm_fifo_segment_main.segments;
347 * fd.io coding-style-patch-verification: ON
350 * eval: (c-set-style "gnu")