diff options
Diffstat (limited to 'src/svm')
-rw-r--r-- | src/svm/ssvm.c | 59 | ||||
-rw-r--r-- | src/svm/ssvm.h | 5 | ||||
-rw-r--r-- | src/svm/svm_fifo_segment.c | 233 | ||||
-rw-r--r-- | src/svm/svm_fifo_segment.h | 30 | ||||
-rw-r--r-- | src/svm/test_svm_fifo1.c | 5 |
5 files changed, 173 insertions, 159 deletions
diff --git a/src/svm/ssvm.c b/src/svm/ssvm.c index 50552198137..7077f8be641 100644 --- a/src/svm/ssvm.c +++ b/src/svm/ssvm.c @@ -19,11 +19,11 @@ typedef int (*init_fn) (ssvm_private_t *); typedef void (*delete_fn) (ssvm_private_t *); static init_fn master_init_fns[SSVM_N_SEGMENT_TYPES] = - { ssvm_master_init_shm, ssvm_master_init_memfd }; + { ssvm_master_init_shm, ssvm_master_init_memfd, ssvm_master_init_private }; static init_fn slave_init_fns[SSVM_N_SEGMENT_TYPES] = - { ssvm_slave_init_shm, ssvm_slave_init_memfd }; + { ssvm_slave_init_shm, ssvm_slave_init_memfd, ssvm_slave_init_private }; static delete_fn delete_fns[SSVM_N_SEGMENT_TYPES] = - { ssvm_delete_shm, ssvm_delete_memfd }; + { ssvm_delete_shm, ssvm_delete_memfd, ssvm_delete_private }; int ssvm_master_init_shm (ssvm_private_t * ssvm) @@ -315,6 +315,59 @@ ssvm_delete_memfd (ssvm_private_t * memfd) close (memfd->fd); } +/** + * Initialize segment in a private heap + */ +int +ssvm_master_init_private (ssvm_private_t * ssvm) +{ + u32 pagesize = clib_mem_get_page_size (); + ssvm_shared_header_t *sh; + mheap_t *heap_header; + u32 rnd_size = 0; + u8 *heap; + + rnd_size = (ssvm->ssvm_size + (pagesize - 1)) & ~pagesize; + heap = mheap_alloc (0, rnd_size); + if (heap == 0) + { + clib_unix_warning ("mheap alloc"); + return -1; + } + heap_header = mheap_header (heap); + heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; + + ssvm->ssvm_size = rnd_size; + ssvm->i_am_master = 1; + ssvm->my_pid = getpid (); + ssvm->requested_va = ~0; + + /* Allocate a [sic] shared memory header, in process memory... */ + sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); + ssvm->sh = sh; + + memset (sh, 0, sizeof (*sh)); + sh->heap = heap; + sh->type = SSVM_SEGMENT_PRIVATE; + + return 0; +} + +int +ssvm_slave_init_private (ssvm_private_t * ssvm) +{ + clib_warning ("BUG: this should not be called!"); + return -1; +} + +void +ssvm_delete_private (ssvm_private_t * ssvm) +{ + vec_free (ssvm->name); + mheap_free (ssvm->sh->heap); + clib_mem_free (ssvm->sh); +} + int ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type) { diff --git a/src/svm/ssvm.h b/src/svm/ssvm.h index 9bf009e73d2..09e1707450b 100644 --- a/src/svm/ssvm.h +++ b/src/svm/ssvm.h @@ -49,6 +49,7 @@ typedef enum ssvm_segment_type_ { SSVM_SEGMENT_SHM = 0, SSVM_SEGMENT_MEMFD, + SSVM_SEGMENT_PRIVATE, SSVM_N_SEGMENT_TYPES /**< Private segments */ } ssvm_segment_type_t; @@ -195,6 +196,10 @@ int ssvm_master_init_memfd (ssvm_private_t * memfd); int ssvm_slave_init_memfd (ssvm_private_t * memfd); void ssvm_delete_memfd (ssvm_private_t * memfd); +int ssvm_master_init_private (ssvm_private_t * ssvm); +int ssvm_slave_init_private (ssvm_private_t * ssvm); +void ssvm_delete_private (ssvm_private_t * ssvm); + ssvm_segment_type_t ssvm_type (const ssvm_private_t * ssvm); u8 *ssvm_name (const ssvm_private_t * ssvm); diff --git a/src/svm/svm_fifo_segment.c b/src/svm/svm_fifo_segment.c index 4d1833eee7e..459b1d1d8e5 100644 --- a/src/svm/svm_fifo_segment.c +++ b/src/svm/svm_fifo_segment.c @@ -56,69 +56,75 @@ allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh, } } -static void -preallocate_fifo_pairs (svm_fifo_segment_private_t * s, - svm_fifo_segment_create_args_t * a, u32 protect_size) +/** + * Pre-allocates fifo pairs in fifo segment + * + * The number of fifos pre-allocated is the minimum of the requested number + * of pairs and the maximum number that fit within the segment. If the maximum + * is hit, the number of fifo pairs requested is updated by subtracting the + * number of fifos that have been successfully allocated. + */ +void +svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s, + u32 rx_fifo_size, u32 tx_fifo_size, + u32 * n_fifo_pairs) { - svm_fifo_segment_header_t *fsh = s->h; - u32 rx_fifo_size, tx_fifo_size, pairs_to_allocate; u32 rx_rounded_data_size, tx_rounded_data_size, pair_size; - svm_fifo_t *f; - u8 *rx_fifo_space, *tx_fifo_space; + u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate; int rx_freelist_index, tx_freelist_index; + ssvm_shared_header_t *sh = s->ssvm.sh; + svm_fifo_segment_header_t *fsh = s->h; + u8 *rx_fifo_space, *tx_fifo_space; + uword space_available; + void *oldheap; + svm_fifo_t *f; int i; /* Parameter check */ - if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0 - || a->preallocated_fifo_pairs == 0) + if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0) return; - if (a->rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || - a->rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) + if (rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || + rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) { - clib_warning ("rx fifo_size out of range %d", a->rx_fifo_size); + clib_warning ("rx fifo_size out of range %d", rx_fifo_size); return; } - if (a->tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || - a->tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) + if (tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || + tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) { - clib_warning ("tx fifo_size out of range %d", a->rx_fifo_size); + clib_warning ("tx fifo_size out of range %d", rx_fifo_size); return; } - rx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size))); - - rx_freelist_index = max_log2 (a->rx_fifo_size) + rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size))); + rx_freelist_index = max_log2 (rx_fifo_size) - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); - - tx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size))); - - tx_freelist_index = max_log2 (a->tx_fifo_size) + tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size))); + tx_freelist_index = max_log2 (tx_fifo_size) - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); /* Calculate space requirements */ pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size; - if (protect_size) - protect_size += mheap_bytes (s->ssvm.sh->heap); - pairs_to_allocate = - clib_min ((s->ssvm.ssvm_size - protect_size) / pair_size, - a->preallocated_fifo_pairs); - rx_fifo_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate; - tx_fifo_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate; + space_available = s->ssvm.ssvm_size - mheap_bytes (sh->heap); + pairs_to_allocate = clib_min (space_available / pair_size, *n_fifo_pairs); + rx_fifos_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate; + tx_fifos_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate; vec_validate_init_empty (fsh->free_fifos, clib_max (rx_freelist_index, tx_freelist_index), 0); + oldheap = ssvm_push_heap (sh); /* Allocate rx fifo space. May fail. */ rx_fifo_space = clib_mem_alloc_aligned_at_offset - (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , + (rx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , 0 /* os_out_of_memory */ ); /* Same for TX */ tx_fifo_space = clib_mem_alloc_aligned_at_offset - (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , + (tx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , 0 /* os_out_of_memory */ ); /* Make sure it worked. Clean up if it didn't... */ @@ -128,13 +134,14 @@ preallocate_fifo_pairs (svm_fifo_segment_private_t * s, clib_mem_free (rx_fifo_space); else clib_warning ("rx fifo preallocation failure: size %d npairs %d", - a->rx_fifo_size, a->preallocated_fifo_pairs); + rx_fifo_size, *n_fifo_pairs); if (tx_fifo_space) clib_mem_free (tx_fifo_space); else clib_warning ("tx fifo preallocation failure: size %d nfifos %d", - a->tx_fifo_size, a->preallocated_fifo_pairs); + tx_fifo_size, *n_fifo_pairs); + ssvm_pop_heap (oldheap); return; } @@ -160,7 +167,31 @@ preallocate_fifo_pairs (svm_fifo_segment_private_t * s, } /* Account for the pairs allocated */ - a->preallocated_fifo_pairs -= pairs_to_allocate; + *n_fifo_pairs -= pairs_to_allocate; + ssvm_pop_heap (oldheap); +} + +/** + * Initialize svm fifo segment shared header + */ +int +svm_fifo_segment_init (svm_fifo_segment_private_t * s) +{ + svm_fifo_segment_header_t *fsh; + ssvm_shared_header_t *sh; + void *oldheap; + + sh = s->ssvm.sh; + oldheap = ssvm_push_heap (sh); + + fsh = clib_mem_alloc (sizeof (*fsh)); + memset (fsh, 0, sizeof (*fsh)); + s->h = sh->opaque[0] = fsh; + + ssvm_pop_heap (oldheap); + + sh->ready = 1; + return (0); } /** @@ -171,9 +202,6 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a) { svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; svm_fifo_segment_private_t *s; - ssvm_shared_header_t *sh; - svm_fifo_segment_header_t *fsh; - void *oldheap; int rv; /* Allocate a fresh segment */ @@ -195,18 +223,7 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a) /* Note: requested_va updated due to seg base addr randomization */ sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size; - sh = s->ssvm.sh; - oldheap = ssvm_push_heap (sh); - - /* Set up svm_fifo_segment shared header */ - fsh = clib_mem_alloc (sizeof (*fsh)); - memset (fsh, 0, sizeof (*fsh)); - s->h = sh->opaque[0] = fsh; - preallocate_fifo_pairs (s, a, a->seg_protected_space); - - ssvm_pop_heap (oldheap); - - sh->ready = 1; + svm_fifo_segment_init (s); vec_add1 (a->new_segment_indices, s - sm->segments); return (0); } @@ -217,78 +234,45 @@ svm_fifo_segment_create (svm_fifo_segment_create_args_t * a) int svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a) { - svm_fifo_segment_private_t *s; svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; + svm_fifo_segment_private_t *s; ssvm_shared_header_t *sh; - svm_fifo_segment_header_t *fsh; - void *oldheap; - u8 **heaps = 0; mheap_t *heap_header; - int segment_count = 1; u32 rnd_size = 0; - int i; + u8 *heap; + u32 pagesize = clib_mem_get_page_size (); - if (a->private_segment_count) - { - u8 *heap; - u32 pagesize = clib_mem_get_page_size (); - rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize; + pool_get (sm->segments, s); + memset (s, 0, sizeof (*s)); - for (i = 0; i < a->private_segment_count; i++) - { - heap = mheap_alloc (0, rnd_size); - if (heap == 0) - { - clib_unix_warning ("mheap alloc"); - return -1; - } - heap_header = mheap_header (heap); - heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; - vec_add1 (heaps, heap); - } - segment_count = a->private_segment_count; - } + rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize; - /* Allocate segments */ - for (i = 0; i < segment_count; i++) + heap = mheap_alloc (0, rnd_size); + if (heap == 0) { - pool_get (sm->segments, s); - memset (s, 0, sizeof (*s)); - - s->ssvm.ssvm_size = rnd_size; - s->ssvm.i_am_master = 1; - s->ssvm.my_pid = getpid (); - s->ssvm.name = format (0, "%s%c", a->segment_name, 0); - s->ssvm.requested_va = ~0; - - /* Allocate a [sic] shared memory header, in process memory... */ - sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); - s->ssvm.sh = sh; - - memset (sh, 0, sizeof (*sh)); - sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap (); - - /* Set up svm_fifo_segment shared header */ - fsh = clib_mem_alloc (sizeof (*fsh)); - memset (fsh, 0, sizeof (*fsh)); - fsh->flags = FIFO_SEGMENT_F_IS_PRIVATE; - s->h = sh->opaque[0] = fsh; - if (!a->private_segment_count) - fsh->flags |= FIFO_SEGMENT_F_IS_MAIN_HEAP; - - if (a->private_segment_count) - { - if (i != 0) - fsh->flags |= FIFO_SEGMENT_F_IS_PREALLOCATED; - oldheap = clib_mem_get_heap (); - clib_mem_set_heap (sh->heap); - preallocate_fifo_pairs (s, a, i == 0 ? a->seg_protected_space : 0); - clib_mem_set_heap (oldheap); - } - sh->ready = 1; - vec_add1 (a->new_segment_indices, s - sm->segments); + clib_unix_warning ("mheap alloc"); + pool_put (sm->segments, s); + return -1; } - vec_free (heaps); + heap_header = mheap_header (heap); + heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; + + s->ssvm.ssvm_size = rnd_size; + s->ssvm.i_am_master = 1; + s->ssvm.my_pid = getpid (); + s->ssvm.name = format (0, "%s%c", a->segment_name, 0); + s->ssvm.requested_va = ~0; + + /* Allocate a [sic] shared memory header, in process memory... */ + sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); + s->ssvm.sh = sh; + + memset (sh, 0, sizeof (*sh)); + sh->heap = heap; + + svm_fifo_segment_init (s); + vec_add1 (a->new_segment_indices, s - sm->segments); + return (0); } @@ -333,18 +317,7 @@ svm_fifo_segment_delete (svm_fifo_segment_private_t * s) { svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; - if (s->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) - { - /* Don't try to free vpp's heap! */ - if (!(s->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)) - mheap_free (s->ssvm.sh->heap); - clib_mem_free (s->ssvm.sh); - clib_mem_free (s->h); - } - else - { - ssvm_delete (&s->ssvm); - } + ssvm_delete (&s->ssvm); memset (s, 0xfe, sizeof (*s)); pool_put (sm->segments, s); } @@ -378,10 +351,10 @@ svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s, - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); sh = s->ssvm.sh; - fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; ssvm_lock_non_recursive (sh, 1); oldheap = ssvm_push_heap (sh); + fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; switch (list_index) { @@ -508,7 +481,7 @@ svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f, } void -svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds) +svm_fifo_segment_main_init (u64 baseva, u32 timeout_in_seconds) { svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; @@ -595,7 +568,7 @@ void svm_fifo_segment_info (svm_fifo_segment_private_t * seg, uword * address, u64 * size) { - if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) + if (ssvm_type (&seg->ssvm) == SSVM_SEGMENT_PRIVATE) { mheap_t *heap_header; @@ -623,12 +596,8 @@ format_svm_fifo_segment_type (u8 * s, va_list * args) sp = va_arg (*args, svm_fifo_segment_private_t *); ssvm_segment_type_t st = ssvm_type (&sp->ssvm); - if ((sp->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) - && !(sp->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)) + if (st == SSVM_SEGMENT_PRIVATE) s = format (s, "%s", "private-heap"); - else if ((sp->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) - && (sp->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)) - s = format (s, "%s", "main-heap"); else if (st == SSVM_SEGMENT_MEMFD) s = format (s, "%s", "memfd"); else if (st == SSVM_SEGMENT_SHM) diff --git a/src/svm/svm_fifo_segment.h b/src/svm/svm_fifo_segment.h index 0e86c25deab..bf8d5139e3c 100644 --- a/src/svm/svm_fifo_segment.h +++ b/src/svm/svm_fifo_segment.h @@ -31,10 +31,8 @@ typedef enum #define FIFO_SEGMENT_MAX_FIFO_SIZE (8<<20) /* 8mb max fifo size */ #define FIFO_SEGMENT_ALLOC_CHUNK_SIZE 32 /* Allocation quantum */ -#define FIFO_SEGMENT_F_IS_PRIVATE 1 << 0 /* Private segment */ -#define FIFO_SEGMENT_F_IS_MAIN_HEAP 1 << 1 /* Segment is main heap */ -#define FIFO_SEGMENT_F_IS_PREALLOCATED 1 << 2 /* Segment is preallocated */ -#define FIFO_SEGMENT_F_WILL_DELETE 1 << 3 /* Segment will be removed */ +#define FIFO_SEGMENT_F_IS_PREALLOCATED 1 << 0 /* Segment is preallocated */ +#define FIFO_SEGMENT_F_WILL_DELETE 1 << 1 /* Segment will be removed */ typedef struct { @@ -69,14 +67,11 @@ typedef struct char *segment_name; u32 segment_size; u32 *new_segment_indices; - u32 rx_fifo_size; - u32 tx_fifo_size; - u32 preallocated_fifo_pairs; - u32 private_segment_count; - u32 seg_protected_space; int memfd_fd; } svm_fifo_segment_create_args_t; +#define svm_fifo_segment_flags(_seg) _seg->h->flags + static inline svm_fifo_segment_private_t * svm_fifo_segment_get_segment (u32 segment_index) { @@ -96,19 +91,14 @@ svm_fifo_segment_get_fifo_list (svm_fifo_segment_private_t * fifo_segment) return fifo_segment->h->fifos; } -#define foreach_ssvm_fifo_segment_api_error \ -_(OUT_OF_SPACE, "Out of space in segment", -200) - -typedef enum -{ -#define _(n,s,c) SSVM_FIFO_SEGMENT_API_ERROR_##n = c, - foreach_ssvm_fifo_segment_api_error -#undef _ -} ssvm_fifo_segment_api_error_enum_t; - +int svm_fifo_segment_init (svm_fifo_segment_private_t * s); int svm_fifo_segment_create (svm_fifo_segment_create_args_t * a); int svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a); +void svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s, + u32 rx_fifo_size, + u32 tx_fifo_size, + u32 * n_fifo_pairs); int svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a); void svm_fifo_segment_delete (svm_fifo_segment_private_t * s); @@ -118,7 +108,7 @@ svm_fifo_t *svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s, void svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f, svm_fifo_segment_freelist_t index); -void svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds); +void svm_fifo_segment_main_init (u64 baseva, u32 timeout_in_seconds); u32 svm_fifo_segment_index (svm_fifo_segment_private_t * s); u32 svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment); u32 svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t * diff --git a/src/svm/test_svm_fifo1.c b/src/svm/test_svm_fifo1.c index 3bdca9499f9..d5b2c98dfc3 100644 --- a/src/svm/test_svm_fifo1.c +++ b/src/svm/test_svm_fifo1.c @@ -30,9 +30,6 @@ hello_world (int verbose) a->segment_name = "fifo-test1"; a->segment_size = 256 << 10; - a->rx_fifo_size = 4096; - a->tx_fifo_size = 4096; - a->preallocated_fifo_pairs = 4; rv = svm_fifo_segment_create (a); @@ -276,7 +273,7 @@ test_ssvm_fifo1 (unformat_input_t * input) int verbose = 0; int test_id = 0; - svm_fifo_segment_init (0x200000000ULL, 20); + svm_fifo_segment_main_init (0x200000000ULL, 20); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { |