2 * Copyright (c) 2016-2019 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>
19 allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh,
20 u32 data_size_in_bytes, int chunk_size)
25 u32 rounded_data_size;
29 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
30 freelist_index = max_log2 (rounded_data_size)
31 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
33 /* Calculate space requirement $$$ round-up data_size_in_bytes */
34 size = (sizeof (*f) + rounded_data_size) * chunk_size;
36 /* Allocate fifo space. May fail. */
37 fifo_space = clib_mem_alloc_aligned_at_offset
38 (size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
39 0 /* os_out_of_memory */ );
45 /* Carve fifo space */
46 f = (svm_fifo_t *) fifo_space;
47 for (i = 0; i < chunk_size; i++)
49 f->freelist_index = freelist_index;
50 f->next = fsh->free_fifos[freelist_index];
51 fsh->free_fifos[freelist_index] = f;
52 fifo_space += sizeof (*f) + rounded_data_size;
53 f = (svm_fifo_t *) fifo_space;
58 * Pre-allocates fifo pairs in fifo segment
60 * The number of fifos pre-allocated is the minimum of the requested number
61 * of pairs and the maximum number that fit within the segment. If the maximum
62 * is hit, the number of fifo pairs requested is updated by subtracting the
63 * number of fifos that have been successfully allocated.
66 svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
67 u32 rx_fifo_size, u32 tx_fifo_size,
70 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size;
71 u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate;
72 int rx_freelist_index, tx_freelist_index;
73 ssvm_shared_header_t *sh = s->ssvm.sh;
74 svm_fifo_segment_header_t *fsh = s->h;
75 u8 *rx_fifo_space, *tx_fifo_space;
76 uword space_available;
82 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
85 if (rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
86 rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
88 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
92 if (tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
93 tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
95 clib_warning ("tx fifo_size out of range %d", rx_fifo_size);
99 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
100 rx_freelist_index = max_log2 (rx_fifo_size)
101 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
102 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
103 tx_freelist_index = max_log2 (tx_fifo_size)
104 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
106 /* Calculate space requirements */
107 pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size;
108 #if USE_DLMALLOC == 0
109 space_available = s->ssvm.ssvm_size - mheap_bytes (sh->heap);
111 space_available = s->ssvm.ssvm_size - mspace_usable_size (sh->heap);
114 pairs_to_allocate = clib_min (space_available / pair_size, *n_fifo_pairs);
115 rx_fifos_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate;
116 tx_fifos_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate;
118 vec_validate_init_empty (fsh->free_fifos,
119 clib_max (rx_freelist_index, tx_freelist_index),
122 oldheap = ssvm_push_heap (sh);
123 /* Allocate rx fifo space. May fail. */
124 rx_fifo_space = clib_mem_alloc_aligned_at_offset
125 (rx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
126 0 /* os_out_of_memory */ );
129 tx_fifo_space = clib_mem_alloc_aligned_at_offset
130 (tx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
131 0 /* os_out_of_memory */ );
133 /* Make sure it worked. Clean up if it didn't... */
134 if (rx_fifo_space == 0 || tx_fifo_space == 0)
137 clib_mem_free (rx_fifo_space);
139 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
140 rx_fifo_size, *n_fifo_pairs);
143 clib_mem_free (tx_fifo_space);
145 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
146 tx_fifo_size, *n_fifo_pairs);
147 ssvm_pop_heap (oldheap);
151 /* Carve rx fifo space */
152 f = (svm_fifo_t *) rx_fifo_space;
153 for (i = 0; i < pairs_to_allocate; i++)
155 f->freelist_index = rx_freelist_index;
156 f->next = fsh->free_fifos[rx_freelist_index];
157 fsh->free_fifos[rx_freelist_index] = f;
158 rx_fifo_space += sizeof (*f) + rx_rounded_data_size;
159 f = (svm_fifo_t *) rx_fifo_space;
161 /* Carve tx fifo space */
162 f = (svm_fifo_t *) tx_fifo_space;
163 for (i = 0; i < pairs_to_allocate; i++)
165 f->freelist_index = tx_freelist_index;
166 f->next = fsh->free_fifos[tx_freelist_index];
167 fsh->free_fifos[tx_freelist_index] = f;
168 tx_fifo_space += sizeof (*f) + tx_rounded_data_size;
169 f = (svm_fifo_t *) tx_fifo_space;
172 /* Account for the pairs allocated */
173 *n_fifo_pairs -= pairs_to_allocate;
174 ssvm_pop_heap (oldheap);
178 * Initialize svm fifo segment shared header
181 svm_fifo_segment_init (svm_fifo_segment_private_t * s)
183 svm_fifo_segment_header_t *fsh;
184 ssvm_shared_header_t *sh;
188 oldheap = ssvm_push_heap (sh);
190 fsh = clib_mem_alloc (sizeof (*fsh));
191 clib_memset (fsh, 0, sizeof (*fsh));
192 s->h = sh->opaque[0] = fsh;
194 ssvm_pop_heap (oldheap);
201 * Create an svm fifo segment and initialize as master
204 svm_fifo_segment_create (svm_fifo_segment_main_t * sm,
205 svm_fifo_segment_create_args_t * a)
207 svm_fifo_segment_private_t *s;
210 /* Allocate a fresh segment */
211 pool_get (sm->segments, s);
212 clib_memset (s, 0, sizeof (*s));
214 s->ssvm.ssvm_size = a->segment_size;
215 s->ssvm.i_am_master = 1;
216 s->ssvm.my_pid = getpid ();
217 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
218 s->ssvm.requested_va = sm->next_baseva;
220 if ((rv = ssvm_master_init (&s->ssvm, a->segment_type)))
222 pool_put (sm->segments, s);
226 /* Note: requested_va updated due to seg base addr randomization */
227 sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size;
229 svm_fifo_segment_init (s);
230 vec_add1 (a->new_segment_indices, s - sm->segments);
235 * Create an svm fifo segment in process-private memory
238 svm_fifo_segment_create_process_private (svm_fifo_segment_main_t * sm,
239 svm_fifo_segment_create_args_t * a)
241 svm_fifo_segment_private_t *s;
242 ssvm_shared_header_t *sh;
245 u32 pagesize = clib_mem_get_page_size ();
247 pool_get (sm->segments, s);
248 clib_memset (s, 0, sizeof (*s));
250 rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize;
252 #if USE_DLMALLOC == 0
253 heap = mheap_alloc (0, rnd_size);
256 clib_unix_warning ("mheap alloc");
257 pool_put (sm->segments, s);
261 mheap_t *heap_header;
262 heap_header = mheap_header (heap);
263 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
266 heap = create_mspace (rnd_size, 1 /* locked */ );
269 s->ssvm.ssvm_size = rnd_size;
270 s->ssvm.i_am_master = 1;
271 s->ssvm.my_pid = getpid ();
272 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
273 s->ssvm.requested_va = ~0;
275 /* Allocate a [sic] shared memory header, in process memory... */
276 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
279 clib_memset (sh, 0, sizeof (*sh));
282 svm_fifo_segment_init (s);
283 vec_add1 (a->new_segment_indices, s - sm->segments);
289 * Attach as slave to an svm fifo segment
292 svm_fifo_segment_attach (svm_fifo_segment_main_t * sm,
293 svm_fifo_segment_create_args_t * a)
295 svm_fifo_segment_private_t *s;
298 /* Allocate a fresh segment */
299 pool_get (sm->segments, s);
300 clib_memset (s, 0, sizeof (*s));
302 s->ssvm.ssvm_size = a->segment_size;
303 s->ssvm.my_pid = getpid ();
304 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
305 s->ssvm.requested_va = sm->next_baseva;
306 if (a->segment_type == SSVM_SEGMENT_MEMFD)
307 s->ssvm.fd = a->memfd_fd;
309 s->ssvm.attach_timeout = sm->timeout_in_seconds;
311 if ((rv = ssvm_slave_init (&s->ssvm, a->segment_type)))
313 _vec_len (s) = vec_len (s) - 1;
317 /* Fish the segment header */
318 s->h = s->ssvm.sh->opaque[0];
320 vec_add1 (a->new_segment_indices, s - sm->segments);
325 svm_fifo_segment_delete (svm_fifo_segment_main_t * sm,
326 svm_fifo_segment_private_t * s)
328 ssvm_delete (&s->ssvm);
329 clib_memset (s, 0xfe, sizeof (*s));
330 pool_put (sm->segments, s);
334 * Allocate fifo in svm segment
337 svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * fs,
338 u32 data_size_in_bytes,
339 svm_fifo_segment_freelist_t list_index)
341 ssvm_shared_header_t *sh;
342 svm_fifo_segment_header_t *fsh;
348 * 4K minimum. It's not likely that anything good will happen
349 * with a smaller FIFO.
351 if (data_size_in_bytes < FIFO_SEGMENT_MIN_FIFO_SIZE ||
352 data_size_in_bytes > FIFO_SEGMENT_MAX_FIFO_SIZE)
354 clib_warning ("fifo size out of range %d", data_size_in_bytes);
358 freelist_index = max_log2 (data_size_in_bytes)
359 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
362 ssvm_lock_non_recursive (sh, 1);
363 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
367 case FIFO_SEGMENT_RX_FREELIST:
368 case FIFO_SEGMENT_TX_FREELIST:
369 vec_validate_init_empty (fsh->free_fifos, freelist_index, 0);
370 f = fsh->free_fifos[freelist_index];
371 if (PREDICT_FALSE (!f))
373 oldheap = ssvm_push_heap (sh);
374 allocate_new_fifo_chunk (fsh, data_size_in_bytes,
375 FIFO_SEGMENT_ALLOC_CHUNK_SIZE);
376 ssvm_pop_heap (oldheap);
377 f = fsh->free_fifos[freelist_index];
379 if (PREDICT_TRUE (f != 0))
381 fsh->free_fifos[freelist_index] = f->next;
382 /* (re)initialize the fifo, as in svm_fifo_create */
383 clib_memset (f, 0, sizeof (*f));
384 f->size = (1 << (max_log2 (data_size_in_bytes)));
386 * usable size of the fifo set to rounded_data_size - 1
387 * to differentiate between free fifo and empty fifo.
389 f->nitems = f->size - 1;
390 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
391 f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX;
393 f->freelist_index = freelist_index;
397 case FIFO_SEGMENT_FREELIST_NONE:
401 clib_warning ("ignore bogus freelist %d", list_index);
405 /* Catch all that allocates just one fifo. Note: this can fail,
406 * in which case: create another segment */
407 oldheap = ssvm_push_heap (sh);
408 f = svm_fifo_create (data_size_in_bytes);
409 ssvm_pop_heap (oldheap);
410 if (PREDICT_FALSE (f == 0))
412 f->freelist_index = freelist_index;
415 /* If rx_freelist add to active fifos list. When cleaning up segment,
416 * we need a list of active sessions that should be disconnected. Since
417 * both rx and tx fifos keep pointers to the session, it's enough to track
419 if (list_index == FIFO_SEGMENT_RX_FREELIST)
423 fsh->fifos->prev = f;
424 f->next = fsh->fifos;
428 fsh->n_active_fifos++;
431 ssvm_unlock_non_recursive (sh);
436 svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
437 svm_fifo_segment_freelist_t list_index)
439 ssvm_shared_header_t *sh;
440 svm_fifo_segment_header_t *fsh;
443 ASSERT (f->refcnt > 0);
449 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
451 freelist_index = f->freelist_index;
453 ASSERT (freelist_index < vec_len (fsh->free_fifos));
455 ssvm_lock_non_recursive (sh, 2);
459 case FIFO_SEGMENT_RX_FREELIST:
460 /* Remove from active list */
462 f->prev->next = f->next;
464 fsh->fifos = f->next;
466 f->next->prev = f->prev;
467 /* Fall through: we add only rx fifos to active pool */
468 case FIFO_SEGMENT_TX_FREELIST:
469 /* Add to free list */
470 f->next = fsh->free_fifos[freelist_index];
472 fsh->free_fifos[freelist_index] = f;
474 case FIFO_SEGMENT_FREELIST_NONE:
478 clib_warning ("ignore bogus freelist %d", list_index);
484 f->master_session_index = ~0;
485 f->master_thread_index = ~0;
488 fsh->n_active_fifos--;
489 ssvm_unlock_non_recursive (sh);
493 svm_fifo_segment_main_init (svm_fifo_segment_main_t * sm, u64 baseva,
494 u32 timeout_in_seconds)
496 sm->next_baseva = baseva;
497 sm->timeout_in_seconds = timeout_in_seconds;
501 svm_fifo_segment_index (svm_fifo_segment_main_t * sm,
502 svm_fifo_segment_private_t * s)
504 return s - sm->segments;
508 * Retrieve svm segments pool. Used only for debug purposes.
510 svm_fifo_segment_private_t *
511 svm_fifo_segment_segments_pool (svm_fifo_segment_main_t * sm)
517 * Get number of active fifos
520 svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment)
522 return fifo_segment->h->n_active_fifos;
526 svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t * fifo_segment,
527 u32 fifo_size_in_bytes)
529 ssvm_shared_header_t *sh;
530 svm_fifo_segment_header_t *fsh;
533 u32 count = 0, rounded_data_size, freelist_index;
535 sh = fifo_segment->ssvm.sh;
536 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
538 /* Count all free fifos? */
539 if (fifo_size_in_bytes == ~0)
541 for (i = 0; i < vec_len (fsh->free_fifos); i++)
543 f = fsh->free_fifos[i];
556 rounded_data_size = (1 << (max_log2 (fifo_size_in_bytes)));
557 freelist_index = max_log2 (rounded_data_size)
558 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
560 if (freelist_index >= vec_len (fsh->free_fifos))
563 f = fsh->free_fifos[freelist_index];
576 svm_fifo_segment_info (svm_fifo_segment_private_t * seg, char **address,
579 if (ssvm_type (&seg->ssvm) == SSVM_SEGMENT_PRIVATE)
581 #if USE_DLMALLOC == 0
582 mheap_t *heap_header;
584 *address = pointer_to_uword (seg->ssvm.sh->heap);
585 heap_header = mheap_header (seg->ssvm.sh->heap);
586 *size = heap_header->max_size;
588 mspace_get_address_and_size (seg->ssvm.sh->heap, address, size);
593 *address = (char *) seg->ssvm.sh->ssvm_va;
594 *size = seg->ssvm.ssvm_size;
599 svm_fifo_segment_heap (svm_fifo_segment_private_t * seg)
601 return seg->ssvm.sh->heap;
605 format_svm_fifo_segment_type (u8 * s, va_list * args)
607 svm_fifo_segment_private_t *sp;
608 sp = va_arg (*args, svm_fifo_segment_private_t *);
609 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
611 if (st == SSVM_SEGMENT_PRIVATE)
612 s = format (s, "%s", "private-heap");
613 else if (st == SSVM_SEGMENT_MEMFD)
614 s = format (s, "%s", "memfd");
615 else if (st == SSVM_SEGMENT_SHM)
616 s = format (s, "%s", "shm");
618 s = format (s, "%s", "unknown");
623 * Segment format function
626 format_svm_fifo_segment (u8 * s, va_list * args)
628 svm_fifo_segment_private_t *sp
629 = va_arg (*args, svm_fifo_segment_private_t *);
630 int verbose __attribute__ ((unused)) = va_arg (*args, int);
631 svm_fifo_segment_header_t *fsh = sp->h;
636 indent = format_get_indent (s) + 2;
637 #if USE_DLMALLOC == 0
638 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
639 format_mheap, svm_fifo_segment_heap (sp), verbose);
640 s = format (s, "%U segment has %u active fifos\n",
641 format_white_space, indent, svm_fifo_segment_num_fifos (sp));
644 for (i = 0; i < vec_len (fsh->free_fifos); i++)
646 f = fsh->free_fifos[i];
656 s = format (s, "%U%-5u Kb: %u free",
657 format_white_space, indent + 2,
658 1 << (i + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE) - 10),
665 * fd.io coding-style-patch-verification: ON
668 * eval: (c-set-style "gnu")