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/fifo_segment.h>
19 * Initialize fifo segment shared header
22 fifo_segment_init (fifo_segment_t * fs)
24 fifo_segment_header_t *fsh;
25 ssvm_shared_header_t *sh;
29 oldheap = ssvm_push_heap (sh);
31 fsh = clib_mem_alloc (sizeof (*fsh));
32 clib_memset (fsh, 0, sizeof (*fsh));
33 fs->h = sh->opaque[0] = fsh;
35 ssvm_pop_heap (oldheap);
42 * Create a fifo segment in process-private memory
45 fifo_segment_create_process_private (fifo_segment_main_t * sm,
46 fifo_segment_create_args_t * a)
48 u32 pagesize = clib_mem_get_page_size ();
49 ssvm_shared_header_t *sh;
54 pool_get (sm->segments, s);
55 clib_memset (s, 0, sizeof (*s));
57 rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize;
60 heap = mheap_alloc (0, rnd_size);
63 clib_unix_warning ("mheap alloc");
64 pool_put (sm->segments, s);
69 heap_header = mheap_header (heap);
70 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
73 heap = create_mspace (rnd_size, 1 /* locked */ );
76 s->ssvm.ssvm_size = rnd_size;
77 s->ssvm.i_am_master = 1;
78 s->ssvm.my_pid = getpid ();
79 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
80 s->ssvm.requested_va = ~0;
82 /* Allocate a [sic] shared memory header, in process memory... */
83 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
86 clib_memset (sh, 0, sizeof (*sh));
89 fifo_segment_init (s);
90 vec_add1 (a->new_segment_indices, s - sm->segments);
96 * Create a fifo segment and initialize as master
99 fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
104 if (a->segment_type == SSVM_SEGMENT_PRIVATE)
105 return fifo_segment_create_process_private (sm, a);
107 /* Allocate a fresh segment */
108 pool_get (sm->segments, s);
109 clib_memset (s, 0, sizeof (*s));
111 s->ssvm.ssvm_size = a->segment_size;
112 s->ssvm.i_am_master = 1;
113 s->ssvm.my_pid = getpid ();
114 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
115 s->ssvm.requested_va = sm->next_baseva;
117 if ((rv = ssvm_master_init (&s->ssvm, a->segment_type)))
119 pool_put (sm->segments, s);
123 /* Note: requested_va updated due to seg base addr randomization */
124 sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size;
126 fifo_segment_init (s);
127 vec_add1 (a->new_segment_indices, s - sm->segments);
132 * Attach as slave to a fifo segment
135 fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
140 pool_get_zero (sm->segments, s);
142 s->ssvm.ssvm_size = a->segment_size;
143 s->ssvm.my_pid = getpid ();
144 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
145 s->ssvm.requested_va = sm->next_baseva;
146 if (a->segment_type == SSVM_SEGMENT_MEMFD)
147 s->ssvm.fd = a->memfd_fd;
149 s->ssvm.attach_timeout = sm->timeout_in_seconds;
151 if ((rv = ssvm_slave_init (&s->ssvm, a->segment_type)))
153 _vec_len (s) = vec_len (s) - 1;
157 /* Fish the segment header */
158 s->h = s->ssvm.sh->opaque[0];
160 vec_add1 (a->new_segment_indices, s - sm->segments);
165 fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
167 ssvm_delete (&s->ssvm);
168 clib_memset (s, 0xfe, sizeof (*s));
169 pool_put (sm->segments, s);
173 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
175 return s - sm->segments;
179 svm_fifo_segment_heap (fifo_segment_t * seg)
181 return seg->ssvm.sh->heap;
185 fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
187 return pool_elt_at_index (sm->segments, segment_index);
191 fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
193 if (ssvm_type (&seg->ssvm) == SSVM_SEGMENT_PRIVATE)
195 #if USE_DLMALLOC == 0
196 mheap_t *heap_header;
198 *address = pointer_to_uword (seg->ssvm.sh->heap);
199 heap_header = mheap_header (seg->ssvm.sh->heap);
200 *size = heap_header->max_size;
202 mspace_get_address_and_size (seg->ssvm.sh->heap, address, size);
207 *address = (char *) seg->ssvm.sh->ssvm_va;
208 *size = seg->ssvm.ssvm_size;
213 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
214 u32 timeout_in_seconds)
216 sm->next_baseva = baseva;
217 sm->timeout_in_seconds = timeout_in_seconds;
221 fifo_init_for_segment (fifo_segment_header_t * fsh, svm_fifo_t * f,
222 u32 size, u32 fl_index)
224 f->freelist_index = fl_index;
225 f->default_chunk.start_byte = 0;
226 f->default_chunk.length = size;
227 f->default_chunk.next = f->start_chunk = f->end_chunk = &f->default_chunk;
228 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk;
229 f->next = fsh->free_fifos[fl_index];
230 fsh->free_fifos[fl_index] = f;
234 fs_free_list_for_size (u32 size)
236 return max_log2 (size) - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
240 fs_chunk_size_is_valid (u32 size)
243 * 4K minimum. It's not likely that anything good will happen
244 * with a smaller FIFO.
246 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
247 && size <= FIFO_SEGMENT_MAX_FIFO_SIZE;
251 allocate_new_fifo_batch (fifo_segment_header_t * fsh,
252 u32 data_size_in_bytes, int chunk_size)
254 u32 size, rounded_data_size;
259 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
260 fl_index = fs_free_list_for_size (data_size_in_bytes);
262 /* Calculate space requirement $$$ round-up data_size_in_bytes */
263 size = (sizeof (*f) + rounded_data_size) * chunk_size;
265 /* Allocate fifo space. May fail. */
266 fifos_mem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
267 0 /* align_offset */ ,
268 0 /* os_out_of_memory */ );
274 /* Carve fifo space */
275 for (i = 0; i < chunk_size; i++)
277 f = (svm_fifo_t *) fifos_mem;
278 fifo_init_for_segment (fsh, f, rounded_data_size, fl_index);
279 fifos_mem += sizeof (*f) + rounded_data_size;
284 * Allocate fifo in fifo segment
287 fifo_segment_alloc_fifo (fifo_segment_t * fs, u32 data_bytes,
288 fifo_segment_ftype_t ftype)
290 fifo_segment_header_t *fsh;
291 ssvm_shared_header_t *sh;
296 if (!fs_chunk_size_is_valid (data_bytes))
298 clib_warning ("fifo size out of range %d", data_bytes);
302 fl_index = fs_free_list_for_size (data_bytes);
305 ssvm_lock_non_recursive (sh, 1);
308 vec_validate_init_empty (fsh->free_fifos, fl_index, 0);
309 f = fsh->free_fifos[fl_index];
311 /* Try to allocate batch of fifos */
312 if (PREDICT_FALSE (!f))
314 oldheap = ssvm_push_heap (sh);
315 allocate_new_fifo_batch (fsh, data_bytes,
316 FIFO_SEGMENT_ALLOC_BATCH_SIZE);
317 ssvm_pop_heap (oldheap);
318 f = fsh->free_fifos[fl_index];
320 if (PREDICT_TRUE (f != 0))
322 fsh->free_fifos[fl_index] = f->next;
323 /* (re)initialize the fifo, as in svm_fifo_create */
324 memset (f, 0, sizeof (*f));
325 svm_fifo_init (f, data_bytes);
329 /* Failed to allocate batch, try just one fifo. This can also fail,
330 * in which case, create another segment */
331 oldheap = ssvm_push_heap (sh);
332 f = svm_fifo_create (data_bytes);
333 ssvm_pop_heap (oldheap);
335 if (PREDICT_FALSE (f == 0))
338 f->freelist_index = fl_index;
341 /* If rx fifo type add to active fifos list. When cleaning up segment,
342 * we need a list of active sessions that should be disconnected. Since
343 * both rx and tx fifos keep pointers to the session, it's enough to track
345 if (ftype == FIFO_SEGMENT_RX_FIFO)
349 fsh->fifos->prev = f;
350 f->next = fsh->fifos;
353 f->flags |= SVM_FIFO_F_LL_TRACKED;
355 fsh->n_active_fifos++;
358 ssvm_unlock_non_recursive (sh);
363 * Free fifo allocated in fifo segment
366 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
368 fifo_segment_header_t *fsh;
369 ssvm_shared_header_t *sh;
372 ASSERT (f->refcnt > 0);
379 fl_index = f->freelist_index;
381 ASSERT (fl_index < vec_len (fsh->free_fifos));
383 ssvm_lock_non_recursive (sh, 2);
385 /* Remove from active list. Only rx fifos are tracked */
386 if (f->flags & SVM_FIFO_F_LL_TRACKED)
389 f->prev->next = f->next;
391 fsh->fifos = f->next;
393 f->next->prev = f->prev;
394 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
397 /* Add to free list */
398 f->next = fsh->free_fifos[fl_index];
400 fsh->free_fifos[fl_index] = f;
402 /* If fifo has more chunks, free them */
403 if (f->flags & SVM_FIFO_F_MULTI_CHUNK)
405 svm_fifo_chunk_t *cur, *next;
408 next = f->start_chunk->next;
409 while (next != f->start_chunk)
413 fl_index = fs_free_list_for_size (cur->length);
414 cur->next = fsh->free_chunks[fl_index];
415 fsh->free_chunks[fl_index] = cur;
417 oldheap = ssvm_push_heap (sh);
418 svm_fifo_free_chunk_lookup (f);
419 ssvm_pop_heap (oldheap);
422 /* not allocated on segment heap */
423 svm_fifo_free_ooo_data (f);
427 f->master_session_index = ~0;
428 f->master_thread_index = ~0;
431 fsh->n_active_fifos--;
432 ssvm_unlock_non_recursive (sh);
436 * Pre-allocates fifo pairs in fifo segment
439 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
440 u32 rx_fifo_size, u32 tx_fifo_size,
443 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size;
444 u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate;
445 ssvm_shared_header_t *sh = fs->ssvm.sh;
446 fifo_segment_header_t *fsh = fs->h;
447 int i, rx_fl_index, tx_fl_index;
448 u8 *rx_fifo_mem, *tx_fifo_mem;
449 uword space_available;
453 /* Parameter check */
454 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
457 if (!fs_chunk_size_is_valid (rx_fifo_size))
459 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
463 if (!fs_chunk_size_is_valid (tx_fifo_size))
465 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
469 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
470 rx_fl_index = fs_free_list_for_size (rx_fifo_size);
471 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
472 tx_fl_index = fs_free_list_for_size (tx_fifo_size);
474 /* Calculate space requirements */
475 pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size;
476 #if USE_DLMALLOC == 0
477 space_available = fs->ssvm.ssvm_size - mheap_bytes (sh->heap);
479 space_available = fs->ssvm.ssvm_size - mspace_usable_size (sh->heap);
482 pairs_to_allocate = clib_min (space_available / pair_size, *n_fifo_pairs);
483 rx_fifos_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate;
484 tx_fifos_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate;
486 vec_validate_init_empty (fsh->free_fifos,
487 clib_max (rx_fl_index, tx_fl_index), 0);
489 oldheap = ssvm_push_heap (sh);
491 /* Allocate rx and tx fifo memory. May fail. */
492 rx_fifo_mem = clib_mem_alloc_aligned_at_offset (rx_fifos_size,
493 CLIB_CACHE_LINE_BYTES,
494 0 /* align_offset */ ,
495 0 /* os_out_of_memory */ );
496 tx_fifo_mem = clib_mem_alloc_aligned_at_offset (tx_fifos_size,
497 CLIB_CACHE_LINE_BYTES,
498 0 /* align_offset */ ,
499 0 /* os_out_of_memory */ );
501 /* Make sure it worked. Clean up if it didn't... */
502 if (rx_fifo_mem == 0 || tx_fifo_mem == 0)
504 rx_fifo_mem ? clib_mem_free (rx_fifo_mem) : clib_mem_free (tx_fifo_mem);
505 clib_warning ("fifo preallocation failure: rx size %d tx size %u "
506 "npairs %d", rx_fifo_size, tx_fifo_size, *n_fifo_pairs);
507 ssvm_pop_heap (oldheap);
511 /* Carve rx and tx fifo memory */
512 for (i = 0; i < pairs_to_allocate; i++)
514 f = (svm_fifo_t *) rx_fifo_mem;
515 fifo_init_for_segment (fsh, f, rx_rounded_data_size, rx_fl_index);
516 rx_fifo_mem += sizeof (*f) + rx_rounded_data_size;
518 f = (svm_fifo_t *) tx_fifo_mem;
519 fifo_init_for_segment (fsh, f, tx_rounded_data_size, tx_fl_index);
520 tx_fifo_mem += sizeof (*f) + tx_rounded_data_size;
523 /* Account for the pairs allocated */
524 *n_fifo_pairs -= pairs_to_allocate;
525 ssvm_pop_heap (oldheap);
529 fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size)
531 ssvm_shared_header_t *sh;
536 if (!fs_chunk_size_is_valid (chunk_size))
538 clib_warning ("chunk size out of range %d", chunk_size);
542 fl_index = fs_free_list_for_size (chunk_size);
545 ssvm_lock_non_recursive (sh, 1);
547 vec_validate_init_empty (fs->h->free_chunks, fl_index, 0);
548 c = fs->h->free_chunks[fl_index];
550 oldheap = ssvm_push_heap (sh);
554 c = svm_fifo_chunk_alloc (chunk_size);
557 ssvm_pop_heap (oldheap);
563 fs->h->free_chunks[fl_index] = c->next;
566 svm_fifo_add_chunk (f, c);
568 ssvm_pop_heap (oldheap);
569 ssvm_unlock_non_recursive (sh);
574 fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f)
576 svm_fifo_chunk_t *cur, *next;
577 ssvm_shared_header_t *sh;
582 ssvm_lock_non_recursive (sh, 1);
584 oldheap = ssvm_push_heap (sh);
585 cur = svm_fifo_collect_chunks (f);
590 fl_index = fs_free_list_for_size (cur->length);
591 cur->next = fs->h->free_chunks[fl_index];
592 fs->h->free_chunks[fl_index] = cur;
596 ssvm_pop_heap (oldheap);
597 ssvm_unlock_non_recursive (sh);
603 * Get number of active fifos
606 fifo_segment_num_fifos (fifo_segment_t * fs)
608 return fs->h->n_active_fifos;
612 fifo_segment_num_free_fifos (fifo_segment_t * fs, u32 fifo_size_in_bytes)
614 u32 count = 0, rounded_data_size, fl_index;
615 fifo_segment_header_t *fsh;
616 ssvm_shared_header_t *sh;
621 fsh = (fifo_segment_header_t *) sh->opaque[0];
623 /* Count all free fifos? */
624 if (fifo_size_in_bytes == ~0)
626 for (i = 0; i < vec_len (fsh->free_fifos); i++)
628 f = fsh->free_fifos[i];
641 rounded_data_size = (1 << (max_log2 (fifo_size_in_bytes)));
642 fl_index = fs_free_list_for_size (rounded_data_size);
644 if (fl_index >= vec_len (fsh->free_fifos))
647 f = fsh->free_fifos[fl_index];
660 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
662 u32 count = 0, rounded_size, fl_index;
663 fifo_segment_header_t *fsh;
669 /* Count all free chunks? */
672 for (i = 0; i < vec_len (fsh->free_chunks); i++)
674 c = fsh->free_chunks[i];
687 rounded_size = (1 << (max_log2 (size)));
688 fl_index = fs_free_list_for_size (rounded_size);
690 if (fl_index >= vec_len (fsh->free_chunks))
693 c = fsh->free_chunks[fl_index];
706 fifo_segment_has_fifos (fifo_segment_t * fs)
708 return fs->h->fifos != 0;
712 fifo_segment_get_fifo_list (fifo_segment_t * fs)
718 format_fifo_segment_type (u8 * s, va_list * args)
721 sp = va_arg (*args, fifo_segment_t *);
722 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
724 if (st == SSVM_SEGMENT_PRIVATE)
725 s = format (s, "%s", "private-heap");
726 else if (st == SSVM_SEGMENT_MEMFD)
727 s = format (s, "%s", "memfd");
728 else if (st == SSVM_SEGMENT_SHM)
729 s = format (s, "%s", "shm");
731 s = format (s, "%s", "unknown");
736 * Segment format function
739 format_fifo_segment (u8 * s, va_list * args)
741 fifo_segment_t *sp = va_arg (*args, fifo_segment_t *);
742 int verbose __attribute__ ((unused)) = va_arg (*args, int);
743 fifo_segment_header_t *fsh = sp->h;
748 indent = format_get_indent (s) + 2;
749 #if USE_DLMALLOC == 0
750 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
751 format_mheap, svm_fifo_segment_heap (sp), verbose);
752 s = format (s, "%U segment has %u active fifos\n",
753 format_white_space, indent, fifo_segment_num_fifos (sp));
756 for (i = 0; i < vec_len (fsh->free_fifos); i++)
758 f = fsh->free_fifos[i];
768 s = format (s, "%U%-5u Kb: %u free",
769 format_white_space, indent + 2,
770 1 << (i + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE) - 10),
777 * fd.io coding-style-patch-verification: ON
780 * eval: (c-set-style "gnu")