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 *address = (char *) seg->ssvm.sh->ssvm_va;
194 *size = seg->ssvm.ssvm_size;
198 fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
199 u32 timeout_in_seconds)
201 sm->next_baseva = baseva;
202 sm->timeout_in_seconds = timeout_in_seconds;
206 fifo_init_for_segment (svm_fifo_t * f, svm_fifo_chunk_t * c)
208 f->start_chunk = f->end_chunk = c->next = c;
209 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk;
213 fifo_init_chunk_for_segment (svm_fifo_chunk_t * c, u32 size)
221 fs_free_list_for_size (u32 size)
223 return max_log2 (size) - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
227 fs_chunk_size_is_valid (u32 size)
230 * 4K minimum. It's not likely that anything good will happen
231 * with a smaller FIFO.
233 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
234 && size <= FIFO_SEGMENT_MAX_FIFO_SIZE;
238 fs_try_alloc_fifo_from_freelist (fifo_segment_header_t * fsh, u32 data_bytes)
245 fl_index = fs_free_list_for_size (data_bytes);
246 vec_validate_init_empty (fsh->free_chunks, fl_index, 0);
247 c = fsh->free_chunks[fl_index];
252 fsh->free_fifos = f->next;
253 fsh->free_chunks[fl_index] = c->next;
256 c->length = data_bytes;
257 memset (f, 0, sizeof (*f));
264 fs_try_allocate_fifo_batch (ssvm_shared_header_t * sh,
265 fifo_segment_header_t * fsh, u32 data_bytes)
267 u32 size, rounded_data_size;
275 rounded_data_size = (1 << (max_log2 (data_bytes)));
276 fl_index = fs_free_list_for_size (data_bytes);
277 vec_validate_init_empty (fsh->free_chunks, fl_index, 0);
279 oldheap = ssvm_push_heap (sh);
280 hdrs = sizeof (*f) + sizeof (*c);
281 size = (hdrs + rounded_data_size) * FIFO_SEGMENT_ALLOC_BATCH_SIZE;
282 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
283 0 /* align_offset */ ,
284 0 /* os_out_of_memory */ );
285 ssvm_pop_heap (oldheap);
291 /* Carve fifo + chunk space */
292 for (i = 0; i < FIFO_SEGMENT_ALLOC_BATCH_SIZE; i++)
294 f = (svm_fifo_t *) fmem;
295 memset (f, 0, sizeof (*f));
296 f->next = fsh->free_fifos;
298 c = (svm_fifo_chunk_t *) (fmem + sizeof (*f));
300 c->length = rounded_data_size;
301 c->next = fsh->free_chunks[fl_index];
302 fsh->free_chunks[fl_index] = c;
303 fmem += hdrs + rounded_data_size;
306 return fs_try_alloc_fifo_from_freelist (fsh, data_bytes);
310 * Allocate fifo in fifo segment
313 fifo_segment_alloc_fifo (fifo_segment_t * fs, u32 data_bytes,
314 fifo_segment_ftype_t ftype)
316 fifo_segment_header_t *fsh;
317 ssvm_shared_header_t *sh;
320 if (!fs_chunk_size_is_valid (data_bytes))
322 clib_warning ("fifo size out of range %d", data_bytes);
327 ssvm_lock_non_recursive (sh, 1);
330 /* Try the following steps in order:
331 * - grab fifo and chunk from freelists
332 * - batch fifo and chunk allocation
333 * - single fifo allocation
335 f = fs_try_alloc_fifo_from_freelist (fsh, data_bytes);
337 f = fs_try_allocate_fifo_batch (sh, fsh, data_bytes);
340 void *oldheap = ssvm_push_heap (sh);
341 f = svm_fifo_create (data_bytes);
342 ssvm_pop_heap (oldheap);
347 /* (re)initialize the fifo, as in svm_fifo_create */
348 svm_fifo_init (f, data_bytes);
350 /* If rx fifo type add to active fifos list. When cleaning up segment,
351 * we need a list of active sessions that should be disconnected. Since
352 * both rx and tx fifos keep pointers to the session, it's enough to track
354 if (ftype == FIFO_SEGMENT_RX_FIFO)
358 fsh->fifos->prev = f;
359 f->next = fsh->fifos;
362 f->flags |= SVM_FIFO_F_LL_TRACKED;
364 fsh->n_active_fifos++;
367 ssvm_unlock_non_recursive (sh);
372 * Free fifo allocated in fifo segment
375 fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
377 svm_fifo_chunk_t *cur, *next;
378 fifo_segment_header_t *fsh;
379 ssvm_shared_header_t *sh;
383 ASSERT (f->refcnt > 0);
391 ssvm_lock_non_recursive (sh, 2);
393 /* Remove from active list. Only rx fifos are tracked */
394 if (f->flags & SVM_FIFO_F_LL_TRACKED)
397 f->prev->next = f->next;
399 fsh->fifos = f->next;
401 f->next->prev = f->prev;
402 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
405 /* Add to free list */
406 f->next = fsh->free_fifos;
410 /* Free fifo chunks */
411 cur = f->start_chunk;
415 fl_index = fs_free_list_for_size (cur->length);
416 ASSERT (fl_index < vec_len (fsh->free_chunks));
417 cur->next = fsh->free_chunks[fl_index];
418 fsh->free_chunks[fl_index] = cur;
421 while (cur != f->start_chunk);
423 oldheap = ssvm_push_heap (sh);
424 svm_fifo_free_chunk_lookup (f);
425 ssvm_pop_heap (oldheap);
427 /* not allocated on segment heap */
428 svm_fifo_free_ooo_data (f);
432 f->master_session_index = ~0;
433 f->master_thread_index = ~0;
436 fsh->n_active_fifos--;
437 ssvm_unlock_non_recursive (sh);
441 * Pre-allocates fifo pairs in fifo segment
444 fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
445 u32 rx_fifo_size, u32 tx_fifo_size,
448 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size;
449 u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate;
450 ssvm_shared_header_t *sh = fs->ssvm.sh;
451 fifo_segment_header_t *fsh = fs->h;
452 int i, rx_fl_index, tx_fl_index;
453 u8 *rx_fifo_mem, *tx_fifo_mem;
454 uword space_available;
460 /* Parameter check */
461 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
464 if (!fs_chunk_size_is_valid (rx_fifo_size))
466 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
470 if (!fs_chunk_size_is_valid (tx_fifo_size))
472 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
476 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
477 rx_fl_index = fs_free_list_for_size (rx_fifo_size);
478 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
479 tx_fl_index = fs_free_list_for_size (tx_fifo_size);
481 hdrs = sizeof (*f) + sizeof (*c);
483 /* Calculate space requirements */
484 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
485 #if USE_DLMALLOC == 0
486 space_available = fs->ssvm.ssvm_size - mheap_bytes (sh->heap);
488 space_available = fs->ssvm.ssvm_size - mspace_usable_size (sh->heap);
491 pairs_to_allocate = clib_min (space_available / pair_size, *n_fifo_pairs);
492 rx_fifos_size = (hdrs + rx_rounded_data_size) * pairs_to_allocate;
493 tx_fifos_size = (hdrs + tx_rounded_data_size) * pairs_to_allocate;
495 vec_validate_init_empty (fsh->free_chunks,
496 clib_max (rx_fl_index, tx_fl_index), 0);
498 oldheap = ssvm_push_heap (sh);
500 /* Allocate rx and tx fifo memory. May fail. */
501 rx_fifo_mem = clib_mem_alloc_aligned_at_offset (rx_fifos_size,
502 CLIB_CACHE_LINE_BYTES,
503 0 /* align_offset */ ,
504 0 /* os_out_of_memory */ );
505 tx_fifo_mem = clib_mem_alloc_aligned_at_offset (tx_fifos_size,
506 CLIB_CACHE_LINE_BYTES,
507 0 /* align_offset */ ,
508 0 /* os_out_of_memory */ );
510 /* Make sure it worked. Clean up if it didn't... */
511 if (rx_fifo_mem == 0 || tx_fifo_mem == 0)
513 rx_fifo_mem ? clib_mem_free (rx_fifo_mem) : clib_mem_free (tx_fifo_mem);
514 clib_warning ("fifo preallocation failure: rx size %d tx size %u "
515 "npairs %d", rx_fifo_size, tx_fifo_size, *n_fifo_pairs);
516 ssvm_pop_heap (oldheap);
520 /* Carve rx and tx fifo memory */
521 for (i = 0; i < pairs_to_allocate; i++)
523 f = (svm_fifo_t *) rx_fifo_mem;
524 c = (svm_fifo_chunk_t *) (rx_fifo_mem + sizeof (*f));
525 fifo_init_chunk_for_segment (c, rx_rounded_data_size);
526 fifo_init_for_segment (f, c);
527 rx_fifo_mem += hdrs + rx_rounded_data_size;
529 f = (svm_fifo_t *) tx_fifo_mem;
530 c = (svm_fifo_chunk_t *) (tx_fifo_mem + sizeof (*f));
531 fifo_init_chunk_for_segment (c, tx_rounded_data_size);
532 fifo_init_for_segment (f, c);
533 tx_fifo_mem += hdrs + tx_rounded_data_size;
536 /* Account for the pairs allocated */
537 *n_fifo_pairs -= pairs_to_allocate;
538 ssvm_pop_heap (oldheap);
542 fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f, u32 chunk_size)
544 ssvm_shared_header_t *sh;
549 if (!fs_chunk_size_is_valid (chunk_size))
551 clib_warning ("chunk size out of range %d", chunk_size);
555 fl_index = fs_free_list_for_size (chunk_size);
558 ssvm_lock_non_recursive (sh, 1);
560 vec_validate_init_empty (fs->h->free_chunks, fl_index, 0);
561 c = fs->h->free_chunks[fl_index];
563 oldheap = ssvm_push_heap (sh);
567 c = svm_fifo_chunk_alloc (chunk_size);
570 ssvm_pop_heap (oldheap);
576 fs->h->free_chunks[fl_index] = c->next;
580 svm_fifo_add_chunk (f, c);
582 ssvm_pop_heap (oldheap);
583 ssvm_unlock_non_recursive (sh);
588 fifo_segment_collect_fifo_chunks (fifo_segment_t * fs, svm_fifo_t * f)
590 svm_fifo_chunk_t *cur, *next;
591 ssvm_shared_header_t *sh;
596 ssvm_lock_non_recursive (sh, 1);
598 oldheap = ssvm_push_heap (sh);
599 cur = svm_fifo_collect_chunks (f);
604 fl_index = fs_free_list_for_size (cur->length);
605 cur->next = fs->h->free_chunks[fl_index];
606 fs->h->free_chunks[fl_index] = cur;
610 ssvm_pop_heap (oldheap);
611 ssvm_unlock_non_recursive (sh);
617 * Get number of active fifos
620 fifo_segment_num_fifos (fifo_segment_t * fs)
622 return fs->h->n_active_fifos;
626 fifo_segment_num_free_fifos (fifo_segment_t * fs, u32 fifo_size_in_bytes)
628 fifo_segment_header_t *fsh;
629 ssvm_shared_header_t *sh;
634 fsh = (fifo_segment_header_t *) sh->opaque[0];
649 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
651 u32 count = 0, rounded_size, fl_index;
652 fifo_segment_header_t *fsh;
658 /* Count all free chunks? */
661 for (i = 0; i < vec_len (fsh->free_chunks); i++)
663 c = fsh->free_chunks[i];
676 rounded_size = (1 << (max_log2 (size)));
677 fl_index = fs_free_list_for_size (rounded_size);
679 if (fl_index >= vec_len (fsh->free_chunks))
682 c = fsh->free_chunks[fl_index];
695 fifo_segment_has_fifos (fifo_segment_t * fs)
697 return fs->h->fifos != 0;
701 fifo_segment_get_fifo_list (fifo_segment_t * fs)
707 format_fifo_segment_type (u8 * s, va_list * args)
710 sp = va_arg (*args, fifo_segment_t *);
711 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
713 if (st == SSVM_SEGMENT_PRIVATE)
714 s = format (s, "%s", "private-heap");
715 else if (st == SSVM_SEGMENT_MEMFD)
716 s = format (s, "%s", "memfd");
717 else if (st == SSVM_SEGMENT_SHM)
718 s = format (s, "%s", "shm");
720 s = format (s, "%s", "unknown");
725 * Segment format function
728 format_fifo_segment (u8 * s, va_list * args)
730 fifo_segment_t *sp = va_arg (*args, fifo_segment_t *);
731 int verbose __attribute__ ((unused)) = va_arg (*args, int);
732 fifo_segment_header_t *fsh = sp->h;
737 indent = format_get_indent (s) + 2;
738 #if USE_DLMALLOC == 0
739 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
740 format_mheap, svm_fifo_segment_heap (sp), verbose);
741 s = format (s, "%U segment has %u active fifos\n",
742 format_white_space, indent, fifo_segment_num_fifos (sp));
745 for (i = 0; i < vec_len (fsh->free_chunks); i++)
747 c = fsh->free_chunks[i];
757 s = format (s, "%U%-5u Kb: %u free",
758 format_white_space, indent + 2,
759 1 << (i + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE) - 10),
766 * fd.io coding-style-patch-verification: ON
769 * eval: (c-set-style "gnu")