blob: 97d9976b39e028093a7168e1686fb660d49adc67 [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
Dave Barach818eb542017-08-02 13:56:13 -040021allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh,
22 u32 data_size_in_bytes, int chunk_size)
23{
24 int freelist_index;
25 u32 size;
26 u8 *fifo_space;
27 u32 rounded_data_size;
28 svm_fifo_t *f;
29 int i;
30
31 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
32 freelist_index = max_log2 (rounded_data_size)
33 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
34
35 /* Calculate space requirement $$$ round-up data_size_in_bytes */
36 size = (sizeof (*f) + rounded_data_size) * chunk_size;
37
38 /* Allocate fifo space. May fail. */
39 fifo_space = clib_mem_alloc_aligned_at_offset
40 (size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
41 0 /* os_out_of_memory */ );
42
43 /* Out of space.. */
44 if (fifo_space == 0)
45 return;
46
47 /* Carve fifo space */
48 f = (svm_fifo_t *) fifo_space;
49 for (i = 0; i < chunk_size; i++)
50 {
51 f->freelist_index = freelist_index;
52 f->next = fsh->free_fifos[freelist_index];
53 fsh->free_fifos[freelist_index] = f;
54 fifo_space += sizeof (*f) + rounded_data_size;
55 f = (svm_fifo_t *) fifo_space;
56 }
57}
58
59static void
Dave Barach10d8cc62017-05-30 09:30:07 -040060preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh,
61 svm_fifo_segment_create_args_t * a)
62{
63 u32 rx_fifo_size, tx_fifo_size;
Dave Barach818eb542017-08-02 13:56:13 -040064 u32 rx_rounded_data_size, tx_rounded_data_size;
Dave Barach10d8cc62017-05-30 09:30:07 -040065 svm_fifo_t *f;
66 u8 *rx_fifo_space, *tx_fifo_space;
Dave Barach818eb542017-08-02 13:56:13 -040067 int rx_freelist_index, tx_freelist_index;
Dave Barach10d8cc62017-05-30 09:30:07 -040068 int i;
69
70 /* Parameter check */
71 if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0
72 || a->preallocated_fifo_pairs == 0)
73 return;
74
Dave Barach818eb542017-08-02 13:56:13 -040075 if (a->rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
76 a->rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
77 {
78 clib_warning ("rx fifo_size out of range %d", a->rx_fifo_size);
79 return;
80 }
Dave Barach10d8cc62017-05-30 09:30:07 -040081
Dave Barach818eb542017-08-02 13:56:13 -040082 if (a->tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
83 a->tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
84 {
85 clib_warning ("tx fifo_size out of range %d", a->rx_fifo_size);
86 return;
87 }
88
89 rx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size)));
90
91 rx_freelist_index = max_log2 (a->rx_fifo_size)
92 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
93
94 tx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size)));
95
96 tx_freelist_index = max_log2 (a->tx_fifo_size)
97 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
98
99 /* Calculate space requirements */
100 rx_fifo_size = (sizeof (*f) + rx_rounded_data_size)
101 * a->preallocated_fifo_pairs;
102 tx_fifo_size = (sizeof (*f) + tx_rounded_data_size)
103 * a->preallocated_fifo_pairs;
104
105 vec_validate_init_empty (fsh->free_fifos,
106 clib_max (rx_freelist_index, tx_freelist_index),
107 0);
Dave Barach2c25a622017-06-26 11:35:07 -0400108 if (0)
109 clib_warning ("rx_fifo_size %u (%d mb), tx_fifo_size %u (%d mb)",
110 rx_fifo_size, rx_fifo_size >> 20,
111 tx_fifo_size, tx_fifo_size >> 20);
112
Dave Barach10d8cc62017-05-30 09:30:07 -0400113 /* Allocate rx fifo space. May fail. */
114 rx_fifo_space = clib_mem_alloc_aligned_at_offset
115 (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
116 0 /* os_out_of_memory */ );
117
118 /* Same for TX */
119 tx_fifo_space = clib_mem_alloc_aligned_at_offset
120 (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
121 0 /* os_out_of_memory */ );
122
123 /* Make sure it worked. Clean up if it didn't... */
124 if (rx_fifo_space == 0 || tx_fifo_space == 0)
125 {
126 if (rx_fifo_space)
127 clib_mem_free (rx_fifo_space);
128 else
129 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
130 a->rx_fifo_size, a->preallocated_fifo_pairs);
131
132 if (tx_fifo_space)
133 clib_mem_free (tx_fifo_space);
134 else
135 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
136 a->tx_fifo_size, a->preallocated_fifo_pairs);
137 return;
138 }
139
140 /* Carve rx fifo space */
141 f = (svm_fifo_t *) rx_fifo_space;
142 for (i = 0; i < a->preallocated_fifo_pairs; i++)
143 {
Dave Barach818eb542017-08-02 13:56:13 -0400144 f->freelist_index = rx_freelist_index;
145 f->next = fsh->free_fifos[rx_freelist_index];
146 fsh->free_fifos[rx_freelist_index] = f;
147 rx_fifo_space += sizeof (*f) + rx_rounded_data_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400148 f = (svm_fifo_t *) rx_fifo_space;
149 }
150 /* Carve tx fifo space */
151 f = (svm_fifo_t *) tx_fifo_space;
152 for (i = 0; i < a->preallocated_fifo_pairs; i++)
153 {
Dave Barach818eb542017-08-02 13:56:13 -0400154 f->freelist_index = tx_freelist_index;
155 f->next = fsh->free_fifos[tx_freelist_index];
156 fsh->free_fifos[tx_freelist_index] = f;
157 tx_fifo_space += sizeof (*f) + tx_rounded_data_size;
Dave Barach10d8cc62017-05-30 09:30:07 -0400158 f = (svm_fifo_t *) tx_fifo_space;
159 }
160}
161
Dave Barach68b0fb02017-02-28 15:15:56 -0500162/** (master) create an svm fifo segment */
163int
164svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
165{
166 int rv;
167 svm_fifo_segment_private_t *s;
168 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
169 ssvm_shared_header_t *sh;
170 svm_fifo_segment_header_t *fsh;
171 void *oldheap;
172
173 /* Allocate a fresh segment */
174 pool_get (sm->segments, s);
175 memset (s, 0, sizeof (*s));
176
177 s->ssvm.ssvm_size = a->segment_size;
178 s->ssvm.i_am_master = 1;
179 s->ssvm.my_pid = getpid ();
Dave Wallaceb8856642017-07-31 13:33:11 -0400180 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500181 s->ssvm.requested_va = sm->next_baseva;
182
183 rv = ssvm_master_init (&s->ssvm, s - sm->segments);
184
185 if (rv)
186 {
187 _vec_len (s) = vec_len (s) - 1;
188 return (rv);
189 }
190
Florin Corasc87c91d2017-08-16 19:55:49 -0700191 /* Note: requested_va updated due to seg base addr randomization */
Dave Barach68b0fb02017-02-28 15:15:56 -0500192 sm->next_baseva = s->ssvm.requested_va + a->segment_size;
193
194 sh = s->ssvm.sh;
195 oldheap = ssvm_push_heap (sh);
196
197 /* Set up svm_fifo_segment shared header */
198 fsh = clib_mem_alloc (sizeof (*fsh));
199 memset (fsh, 0, sizeof (*fsh));
200 sh->opaque[0] = fsh;
201 s->h = fsh;
202 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
Dave Barach10d8cc62017-05-30 09:30:07 -0400203 preallocate_fifo_pairs (fsh, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500204
205 ssvm_pop_heap (oldheap);
206
207 sh->ready = 1;
Dave Barach2c25a622017-06-26 11:35:07 -0400208 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach68b0fb02017-02-28 15:15:56 -0500209 return (0);
210}
211
Florin Corasa5464812017-04-19 13:00:05 -0700212/** Create an svm fifo segment in process-private memory */
213int
214svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
215{
216 svm_fifo_segment_private_t *s;
217 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
218 ssvm_shared_header_t *sh;
219 svm_fifo_segment_header_t *fsh;
Dave Barach2c25a622017-06-26 11:35:07 -0400220 void *oldheap;
221 u8 **heaps = 0;
222 mheap_t *heap_header;
223 int segment_count = 1;
224 int i;
Florin Corasa5464812017-04-19 13:00:05 -0700225
Dave Barach2c25a622017-06-26 11:35:07 -0400226 if (a->private_segment_count && a->private_segment_size)
227 {
Dave Barach2c25a622017-06-26 11:35:07 -0400228 u8 *heap;
229 u32 pagesize = clib_mem_get_page_size ();
230 u32 rnd_size;
Florin Corasc87c91d2017-08-16 19:55:49 -0700231 rnd_size = (a->private_segment_size + (pagesize - 1)) & ~pagesize;
Florin Corasa5464812017-04-19 13:00:05 -0700232
Dave Barach2c25a622017-06-26 11:35:07 -0400233 for (i = 0; i < a->private_segment_count; i++)
234 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700235 heap = mheap_alloc (0, rnd_size);
236 if (heap == 0)
Dave Barach2c25a622017-06-26 11:35:07 -0400237 {
Florin Corasc87c91d2017-08-16 19:55:49 -0700238 clib_unix_warning ("mheap alloc");
Dave Barach2c25a622017-06-26 11:35:07 -0400239 return -1;
240 }
Dave Barach2c25a622017-06-26 11:35:07 -0400241 heap_header = mheap_header (heap);
242 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
243 vec_add1 (heaps, heap);
244 }
245 segment_count = a->private_segment_count;
246 }
Florin Corasa5464812017-04-19 13:00:05 -0700247
Dave Barach2c25a622017-06-26 11:35:07 -0400248 /* Spread preallocated fifo pairs across segments */
249 a->preallocated_fifo_pairs /= segment_count;
Florin Corasa5464812017-04-19 13:00:05 -0700250
Dave Barach2c25a622017-06-26 11:35:07 -0400251 /* Allocate segments */
252 for (i = 0; i < segment_count; i++)
253 {
254 pool_get (sm->segments, s);
255 memset (s, 0, sizeof (*s));
Dave Barach10d8cc62017-05-30 09:30:07 -0400256
Dave Barach2c25a622017-06-26 11:35:07 -0400257 s->ssvm.ssvm_size = ~0;
258 s->ssvm.i_am_master = 1;
259 s->ssvm.my_pid = getpid ();
Dave Wallaceb8856642017-07-31 13:33:11 -0400260 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
Dave Barach2c25a622017-06-26 11:35:07 -0400261 s->ssvm.requested_va = ~0;
262
263 /* Allocate a [sic] shared memory header, in process memory... */
264 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
265 s->ssvm.sh = sh;
266
267 memset (sh, 0, sizeof (*sh));
268 sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap ();
269
270 /* Set up svm_fifo_segment shared header */
271 fsh = clib_mem_alloc (sizeof (*fsh));
272 memset (fsh, 0, sizeof (*fsh));
273 sh->opaque[0] = fsh;
274 s->h = fsh;
Florin Corasc87c91d2017-08-16 19:55:49 -0700275 fsh->flags = FIFO_SEGMENT_F_IS_PRIVATE;
276 if (!a->private_segment_count)
277 fsh->flags |= FIFO_SEGMENT_F_IS_MAIN_HEAP;
Dave Barach2c25a622017-06-26 11:35:07 -0400278 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
279
280 if (a->private_segment_count)
281 {
282 oldheap = clib_mem_get_heap ();
283 clib_mem_set_heap (sh->heap);
284 preallocate_fifo_pairs (fsh, a);
285 clib_mem_set_heap (oldheap);
286 }
Dave Barach2c25a622017-06-26 11:35:07 -0400287 sh->ready = 1;
288 vec_add1 (a->new_segment_indices, s - sm->segments);
289 }
290 vec_free (heaps);
Florin Corasa5464812017-04-19 13:00:05 -0700291 return (0);
292}
293
Dave Barach68b0fb02017-02-28 15:15:56 -0500294/** (slave) attach to an svm fifo segment */
295int
296svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
297{
298 int rv;
299 svm_fifo_segment_private_t *s;
300 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
301 ssvm_shared_header_t *sh;
302 svm_fifo_segment_header_t *fsh;
303
304 /* Allocate a fresh segment */
305 pool_get (sm->segments, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500306 memset (s, 0, sizeof (*s));
307
308 s->ssvm.ssvm_size = a->segment_size;
309 s->ssvm.my_pid = getpid ();
Dave Wallaceb8856642017-07-31 13:33:11 -0400310 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500311 s->ssvm.requested_va = sm->next_baseva;
312
313 rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
314
315 if (rv)
316 {
317 _vec_len (s) = vec_len (s) - 1;
318 return (rv);
319 }
320
321 /* Fish the segment header */
322 sh = s->ssvm.sh;
323 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
324 s->h = fsh;
325
Dave Barach2c25a622017-06-26 11:35:07 -0400326 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 return (0);
328}
329
330void
331svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
332{
333 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
Florin Corasc87c91d2017-08-16 19:55:49 -0700334 if (s->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
335 {
336 /* Don't try to free vpp's heap! */
337 if (!(s->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP))
338 mheap_free (s->ssvm.sh->heap);
339 clib_mem_free (s->ssvm.sh);
340 clib_mem_free (s->h);
341 pool_put (sm->segments, s);
342 }
343 else
344 {
345 ssvm_delete (&s->ssvm);
346 pool_put (sm->segments, s);
347 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500348}
349
350svm_fifo_t *
351svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
Dave Barach10d8cc62017-05-30 09:30:07 -0400352 u32 data_size_in_bytes,
353 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500354{
355 ssvm_shared_header_t *sh;
356 svm_fifo_segment_header_t *fsh;
357 svm_fifo_t *f;
358 void *oldheap;
Dave Barach818eb542017-08-02 13:56:13 -0400359 int freelist_index;
360
361 /*
Florin Corasc87c91d2017-08-16 19:55:49 -0700362 * 4K minimum. It's not likely that anything good will happen
363 * with a smaller FIFO.
Dave Barach818eb542017-08-02 13:56:13 -0400364 */
365 if (data_size_in_bytes < FIFO_SEGMENT_MIN_FIFO_SIZE ||
366 data_size_in_bytes > FIFO_SEGMENT_MAX_FIFO_SIZE)
367 {
368 clib_warning ("fifo size out of range %d", data_size_in_bytes);
369 return 0;
370 }
371
372 freelist_index = max_log2 (data_size_in_bytes)
373 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
Dave Barach68b0fb02017-02-28 15:15:56 -0500374
375 sh = s->ssvm.sh;
376 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Florin Corasa5464812017-04-19 13:00:05 -0700377
Dave Barach2c25a622017-06-26 11:35:07 -0400378 ssvm_lock_non_recursive (sh, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500379 oldheap = ssvm_push_heap (sh);
380
Dave Barach10d8cc62017-05-30 09:30:07 -0400381 switch (list_index)
382 {
383 case FIFO_SEGMENT_RX_FREELIST:
384 case FIFO_SEGMENT_TX_FREELIST:
Dave Barach818eb542017-08-02 13:56:13 -0400385 vec_validate_init_empty (fsh->free_fifos, freelist_index, 0);
386
387 f = fsh->free_fifos[freelist_index];
388 if (PREDICT_FALSE (f == 0))
Dave Barach10d8cc62017-05-30 09:30:07 -0400389 {
Dave Barach818eb542017-08-02 13:56:13 -0400390 allocate_new_fifo_chunk (fsh, data_size_in_bytes,
391 FIFO_SEGMENT_ALLOC_CHUNK_SIZE);
392 f = fsh->free_fifos[freelist_index];
393 }
394 if (PREDICT_TRUE (f != 0))
395 {
396 fsh->free_fifos[freelist_index] = f->next;
Dave Barach10d8cc62017-05-30 09:30:07 -0400397 /* (re)initialize the fifo, as in svm_fifo_create */
398 memset (f, 0, sizeof (*f));
399 f->nitems = data_size_in_bytes;
400 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Dave Barach52851e62017-08-07 09:35:25 -0400401 f->refcnt = 1;
Dave Barach818eb542017-08-02 13:56:13 -0400402 f->freelist_index = freelist_index;
Dave Barach10d8cc62017-05-30 09:30:07 -0400403 goto found;
404 }
405 /* FALLTHROUGH */
406 case FIFO_SEGMENT_FREELIST_NONE:
407 break;
408
409 default:
410 clib_warning ("ignore bogus freelist %d", list_index);
411 break;
412 }
413
Dave Barach68b0fb02017-02-28 15:15:56 -0500414 /* Note: this can fail, in which case: create another segment */
415 f = svm_fifo_create (data_size_in_bytes);
Florin Corasa5464812017-04-19 13:00:05 -0700416 if (PREDICT_FALSE (f == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 {
418 ssvm_pop_heap (oldheap);
Dave Barach2c25a622017-06-26 11:35:07 -0400419 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500420 return (0);
421 }
Dave Barach818eb542017-08-02 13:56:13 -0400422 f->freelist_index = freelist_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500423
Dave Barach10d8cc62017-05-30 09:30:07 -0400424found:
425 /* If rx_freelist add to active fifos list. When cleaning up segment,
426 * we need a list of active sessions that should be disconnected. Since
427 * both rx and tx fifos keep pointers to the session, it's enough to track
428 * only one. */
429 if (list_index == FIFO_SEGMENT_RX_FREELIST)
430 {
431 if (fsh->fifos)
432 {
433 fsh->fifos->prev = f;
434 f->next = fsh->fifos;
435 }
436 fsh->fifos = f;
437 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700438 fsh->n_active_fifos++;
Dave Barach10d8cc62017-05-30 09:30:07 -0400439
Dave Barach68b0fb02017-02-28 15:15:56 -0500440 ssvm_pop_heap (oldheap);
Dave Barach2c25a622017-06-26 11:35:07 -0400441 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500442 return (f);
443}
444
445void
Dave Barach10d8cc62017-05-30 09:30:07 -0400446svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
447 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500448{
449 ssvm_shared_header_t *sh;
450 svm_fifo_segment_header_t *fsh;
451 void *oldheap;
Dave Barach818eb542017-08-02 13:56:13 -0400452 int freelist_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500453
Dave Barach52851e62017-08-07 09:35:25 -0400454 ASSERT (f->refcnt > 0);
455
456 if (--f->refcnt > 0)
457 return;
Dave Barach2c25a622017-06-26 11:35:07 -0400458
Dave Barach68b0fb02017-02-28 15:15:56 -0500459 sh = s->ssvm.sh;
460 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Dave Barach68b0fb02017-02-28 15:15:56 -0500461
Dave Barach818eb542017-08-02 13:56:13 -0400462 freelist_index = f->freelist_index;
463
Dave Barachb7f1faa2017-08-29 11:43:37 -0400464 ASSERT (freelist_index < vec_len (fsh->free_fifos));
Dave Barach818eb542017-08-02 13:56:13 -0400465
Dave Barach2c25a622017-06-26 11:35:07 -0400466 ssvm_lock_non_recursive (sh, 2);
Florin Corasa5464812017-04-19 13:00:05 -0700467 oldheap = ssvm_push_heap (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500468
Dave Barach10d8cc62017-05-30 09:30:07 -0400469 switch (list_index)
470 {
471 case FIFO_SEGMENT_RX_FREELIST:
472 /* Remove from active list */
473 if (f->prev)
474 f->prev->next = f->next;
Florin Corasf03a59a2017-06-09 21:07:32 -0700475 else
476 fsh->fifos = f->next;
Dave Barach10d8cc62017-05-30 09:30:07 -0400477 if (f->next)
478 f->next->prev = f->prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700479 /* Fall through: we add only rx fifos to active pool */
Dave Barach10d8cc62017-05-30 09:30:07 -0400480 case FIFO_SEGMENT_TX_FREELIST:
481 /* Add to free list */
Dave Barach818eb542017-08-02 13:56:13 -0400482 f->next = fsh->free_fifos[freelist_index];
Florin Corasf03a59a2017-06-09 21:07:32 -0700483 f->prev = 0;
Dave Barach818eb542017-08-02 13:56:13 -0400484 fsh->free_fifos[freelist_index] = f;
Florin Corasf03a59a2017-06-09 21:07:32 -0700485 break;
Dave Barach10d8cc62017-05-30 09:30:07 -0400486 case FIFO_SEGMENT_FREELIST_NONE:
487 break;
488
489 default:
490 clib_warning ("ignore bogus freelist %d", list_index);
491 break;
492 }
493
Florin Coras68810622017-07-24 17:40:28 -0700494 if (CLIB_DEBUG)
495 {
496 f->master_session_index = ~0;
497 f->master_thread_index = ~0;
498 }
499
Florin Corasc87c91d2017-08-16 19:55:49 -0700500 fsh->n_active_fifos--;
Dave Barach68b0fb02017-02-28 15:15:56 -0500501 ssvm_pop_heap (oldheap);
Dave Barach2c25a622017-06-26 11:35:07 -0400502 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500503}
504
505void
506svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
507{
508 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
509
510 sm->next_baseva = baseva;
511 sm->timeout_in_seconds = timeout_in_seconds;
512}
513
514u32
515svm_fifo_segment_index (svm_fifo_segment_private_t * s)
516{
517 return s - svm_fifo_segment_main.segments;
518}
519
Florin Corasc87c91d2017-08-16 19:55:49 -0700520/**
521 * Retrieve svm segments pool. Used only for debug purposes.
522 */
523svm_fifo_segment_private_t *
524svm_fifo_segment_segments_pool (void)
525{
526 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
527 return sm->segments;
528}
529
530/**
531 * Get number of active fifos
532 */
533u32
534svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment)
535{
536 return fifo_segment->h->n_active_fifos;
537}
538
Dave Barach68b0fb02017-02-28 15:15:56 -0500539/*
540 * fd.io coding-style-patch-verification: ON
541 *
542 * Local Variables:
543 * eval: (c-set-style "gnu")
544 * End:
545 */