blob: 1bdbb88471d183273a7242486c07a42046c5df5e [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
Dave Barach10d8cc62017-05-30 09:30:07 -040018static void
Dave Barach818eb542017-08-02 13:56:13 -040019allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh,
20 u32 data_size_in_bytes, int chunk_size)
21{
22 int freelist_index;
23 u32 size;
24 u8 *fifo_space;
25 u32 rounded_data_size;
26 svm_fifo_t *f;
27 int i;
28
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);
32
33 /* Calculate space requirement $$$ round-up data_size_in_bytes */
34 size = (sizeof (*f) + rounded_data_size) * chunk_size;
35
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 */ );
40
41 /* Out of space.. */
42 if (fifo_space == 0)
43 return;
44
45 /* Carve fifo space */
46 f = (svm_fifo_t *) fifo_space;
47 for (i = 0; i < chunk_size; i++)
48 {
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;
54 }
55}
56
Florin Corasa332c462018-01-31 06:52:17 -080057/**
58 * Pre-allocates fifo pairs in fifo segment
59 *
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.
64 */
65void
66svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
67 u32 rx_fifo_size, u32 tx_fifo_size,
68 u32 * n_fifo_pairs)
Dave Barach10d8cc62017-05-30 09:30:07 -040069{
Florin Coras9d063042017-09-14 03:08:00 -040070 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size;
Florin Corasa332c462018-01-31 06:52:17 -080071 u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate;
Dave Barach818eb542017-08-02 13:56:13 -040072 int rx_freelist_index, tx_freelist_index;
Florin Corasa332c462018-01-31 06:52:17 -080073 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;
77 void *oldheap;
78 svm_fifo_t *f;
Dave Barach10d8cc62017-05-30 09:30:07 -040079 int i;
80
81 /* Parameter check */
Florin Corasa332c462018-01-31 06:52:17 -080082 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
Dave Barach10d8cc62017-05-30 09:30:07 -040083 return;
84
Florin Corasa332c462018-01-31 06:52:17 -080085 if (rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
86 rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
Dave Barach818eb542017-08-02 13:56:13 -040087 {
Florin Corasa332c462018-01-31 06:52:17 -080088 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
Dave Barach818eb542017-08-02 13:56:13 -040089 return;
90 }
Dave Barach10d8cc62017-05-30 09:30:07 -040091
Florin Corasa332c462018-01-31 06:52:17 -080092 if (tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
93 tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
Dave Barach818eb542017-08-02 13:56:13 -040094 {
Florin Corasa332c462018-01-31 06:52:17 -080095 clib_warning ("tx fifo_size out of range %d", rx_fifo_size);
Dave Barach818eb542017-08-02 13:56:13 -040096 return;
97 }
98
Florin Corasa332c462018-01-31 06:52:17 -080099 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
100 rx_freelist_index = max_log2 (rx_fifo_size)
Dave Barach818eb542017-08-02 13:56:13 -0400101 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
Florin Corasa332c462018-01-31 06:52:17 -0800102 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
103 tx_freelist_index = max_log2 (tx_fifo_size)
Dave Barach818eb542017-08-02 13:56:13 -0400104 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
105
106 /* Calculate space requirements */
Florin Coras9d063042017-09-14 03:08:00 -0400107 pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size;
Dave Barach6a5adc32018-07-04 10:56:23 -0400108#if USE_DLMALLOC == 0
Florin Corasa332c462018-01-31 06:52:17 -0800109 space_available = s->ssvm.ssvm_size - mheap_bytes (sh->heap);
Dave Barach6a5adc32018-07-04 10:56:23 -0400110#else
111 space_available = s->ssvm.ssvm_size - mspace_usable_size (sh->heap);
112#endif
113
Florin Corasa332c462018-01-31 06:52:17 -0800114 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;
Dave Barach818eb542017-08-02 13:56:13 -0400117
118 vec_validate_init_empty (fsh->free_fifos,
119 clib_max (rx_freelist_index, tx_freelist_index),
120 0);
Dave Barach2c25a622017-06-26 11:35:07 -0400121
Florin Corasa332c462018-01-31 06:52:17 -0800122 oldheap = ssvm_push_heap (sh);
Dave Barach10d8cc62017-05-30 09:30:07 -0400123 /* Allocate rx fifo space. May fail. */
124 rx_fifo_space = clib_mem_alloc_aligned_at_offset
Florin Corasa332c462018-01-31 06:52:17 -0800125 (rx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
Dave Barach10d8cc62017-05-30 09:30:07 -0400126 0 /* os_out_of_memory */ );
127
128 /* Same for TX */
129 tx_fifo_space = clib_mem_alloc_aligned_at_offset
Florin Corasa332c462018-01-31 06:52:17 -0800130 (tx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
Dave Barach10d8cc62017-05-30 09:30:07 -0400131 0 /* os_out_of_memory */ );
132
133 /* Make sure it worked. Clean up if it didn't... */
134 if (rx_fifo_space == 0 || tx_fifo_space == 0)
135 {
136 if (rx_fifo_space)
137 clib_mem_free (rx_fifo_space);
138 else
139 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
Florin Corasa332c462018-01-31 06:52:17 -0800140 rx_fifo_size, *n_fifo_pairs);
Dave Barach10d8cc62017-05-30 09:30:07 -0400141
142 if (tx_fifo_space)
143 clib_mem_free (tx_fifo_space);
144 else
145 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
Florin Corasa332c462018-01-31 06:52:17 -0800146 tx_fifo_size, *n_fifo_pairs);
147 ssvm_pop_heap (oldheap);
Dave Barach10d8cc62017-05-30 09:30:07 -0400148 return;
149 }
150
151 /* Carve rx fifo space */
152 f = (svm_fifo_t *) rx_fifo_space;
Florin Coras9d063042017-09-14 03:08:00 -0400153 for (i = 0; i < pairs_to_allocate; i++)
Dave Barach10d8cc62017-05-30 09:30:07 -0400154 {
Dave Barach818eb542017-08-02 13:56:13 -0400155 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;
Dave Barach10d8cc62017-05-30 09:30:07 -0400159 f = (svm_fifo_t *) rx_fifo_space;
160 }
161 /* Carve tx fifo space */
162 f = (svm_fifo_t *) tx_fifo_space;
Florin Coras9d063042017-09-14 03:08:00 -0400163 for (i = 0; i < pairs_to_allocate; i++)
Dave Barach10d8cc62017-05-30 09:30:07 -0400164 {
Dave Barach818eb542017-08-02 13:56:13 -0400165 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;
Dave Barach10d8cc62017-05-30 09:30:07 -0400169 f = (svm_fifo_t *) tx_fifo_space;
170 }
Florin Coras9d063042017-09-14 03:08:00 -0400171
172 /* Account for the pairs allocated */
Florin Corasa332c462018-01-31 06:52:17 -0800173 *n_fifo_pairs -= pairs_to_allocate;
174 ssvm_pop_heap (oldheap);
175}
176
177/**
178 * Initialize svm fifo segment shared header
179 */
180int
181svm_fifo_segment_init (svm_fifo_segment_private_t * s)
182{
183 svm_fifo_segment_header_t *fsh;
184 ssvm_shared_header_t *sh;
185 void *oldheap;
186
187 sh = s->ssvm.sh;
188 oldheap = ssvm_push_heap (sh);
189
190 fsh = clib_mem_alloc (sizeof (*fsh));
Dave Barachb7b92992018-10-17 10:38:51 -0400191 clib_memset (fsh, 0, sizeof (*fsh));
Florin Corasa332c462018-01-31 06:52:17 -0800192 s->h = sh->opaque[0] = fsh;
193
194 ssvm_pop_heap (oldheap);
195
196 sh->ready = 1;
197 return (0);
Dave Barach10d8cc62017-05-30 09:30:07 -0400198}
199
Florin Corasb384b542018-01-15 01:08:33 -0800200/**
201 * Create an svm fifo segment and initialize as master
202 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500203int
Florin Corasadc74d72018-12-02 13:36:00 -0800204svm_fifo_segment_create (svm_fifo_segment_main_t * sm,
205 svm_fifo_segment_create_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500206{
Florin Corasb384b542018-01-15 01:08:33 -0800207 svm_fifo_segment_private_t *s;
Florin Corasb384b542018-01-15 01:08:33 -0800208 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500209
210 /* Allocate a fresh segment */
211 pool_get (sm->segments, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400212 clib_memset (s, 0, sizeof (*s));
Dave Barach68b0fb02017-02-28 15:15:56 -0500213
214 s->ssvm.ssvm_size = a->segment_size;
215 s->ssvm.i_am_master = 1;
216 s->ssvm.my_pid = getpid ();
Dave Wallaceb8856642017-07-31 13:33:11 -0400217 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 s->ssvm.requested_va = sm->next_baseva;
219
Florin Corasb384b542018-01-15 01:08:33 -0800220 if ((rv = ssvm_master_init (&s->ssvm, a->segment_type)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500221 {
Florin Corasb384b542018-01-15 01:08:33 -0800222 pool_put (sm->segments, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 return (rv);
224 }
225
Florin Corasc87c91d2017-08-16 19:55:49 -0700226 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasb384b542018-01-15 01:08:33 -0800227 sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500228
Florin Corasa332c462018-01-31 06:52:17 -0800229 svm_fifo_segment_init (s);
Dave Barach2c25a622017-06-26 11:35:07 -0400230 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach68b0fb02017-02-28 15:15:56 -0500231 return (0);
232}
233
Florin Corasb384b542018-01-15 01:08:33 -0800234/**
235 * Create an svm fifo segment in process-private memory
236 */
Florin Corasa5464812017-04-19 13:00:05 -0700237int
Florin Corasadc74d72018-12-02 13:36:00 -0800238svm_fifo_segment_create_process_private (svm_fifo_segment_main_t * sm,
239 svm_fifo_segment_create_args_t * a)
Florin Corasa5464812017-04-19 13:00:05 -0700240{
Florin Corasa332c462018-01-31 06:52:17 -0800241 svm_fifo_segment_private_t *s;
Florin Corasa5464812017-04-19 13:00:05 -0700242 ssvm_shared_header_t *sh;
Dave Barach91f3e742017-09-01 19:12:11 -0400243 u32 rnd_size = 0;
Florin Corasa332c462018-01-31 06:52:17 -0800244 u8 *heap;
245 u32 pagesize = clib_mem_get_page_size ();
Florin Corasa5464812017-04-19 13:00:05 -0700246
Florin Corasa332c462018-01-31 06:52:17 -0800247 pool_get (sm->segments, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400248 clib_memset (s, 0, sizeof (*s));
Florin Corasa332c462018-01-31 06:52:17 -0800249
250 rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize;
251
Dave Barach6a5adc32018-07-04 10:56:23 -0400252#if USE_DLMALLOC == 0
Florin Corasa332c462018-01-31 06:52:17 -0800253 heap = mheap_alloc (0, rnd_size);
254 if (heap == 0)
Dave Barach2c25a622017-06-26 11:35:07 -0400255 {
Florin Corasa332c462018-01-31 06:52:17 -0800256 clib_unix_warning ("mheap alloc");
257 pool_put (sm->segments, s);
258 return -1;
Dave Barach2c25a622017-06-26 11:35:07 -0400259 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400260 {
261 mheap_t *heap_header;
262 heap_header = mheap_header (heap);
263 heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
264 }
265#else
266 heap = create_mspace (rnd_size, 1 /* locked */ );
267#endif
Florin Corasa5464812017-04-19 13:00:05 -0700268
Florin Corasa332c462018-01-31 06:52:17 -0800269 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;
Dave Barach10d8cc62017-05-30 09:30:07 -0400274
Florin Corasa332c462018-01-31 06:52:17 -0800275 /* Allocate a [sic] shared memory header, in process memory... */
276 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
277 s->ssvm.sh = sh;
Dave Barach2c25a622017-06-26 11:35:07 -0400278
Dave Barachb7b92992018-10-17 10:38:51 -0400279 clib_memset (sh, 0, sizeof (*sh));
Florin Corasa332c462018-01-31 06:52:17 -0800280 sh->heap = heap;
Dave Barach2c25a622017-06-26 11:35:07 -0400281
Florin Corasa332c462018-01-31 06:52:17 -0800282 svm_fifo_segment_init (s);
283 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach2c25a622017-06-26 11:35:07 -0400284
Florin Corasa5464812017-04-19 13:00:05 -0700285 return (0);
286}
287
Florin Corasb384b542018-01-15 01:08:33 -0800288/**
289 * Attach as slave to an svm fifo segment
290 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500291int
Florin Corasadc74d72018-12-02 13:36:00 -0800292svm_fifo_segment_attach (svm_fifo_segment_main_t * sm,
293 svm_fifo_segment_create_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500294{
Florin Corasb384b542018-01-15 01:08:33 -0800295 svm_fifo_segment_private_t *s;
296 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500297
298 /* Allocate a fresh segment */
299 pool_get (sm->segments, s);
Dave Barachb7b92992018-10-17 10:38:51 -0400300 clib_memset (s, 0, sizeof (*s));
Dave Barach68b0fb02017-02-28 15:15:56 -0500301
302 s->ssvm.ssvm_size = a->segment_size;
303 s->ssvm.my_pid = getpid ();
Dave Wallaceb8856642017-07-31 13:33:11 -0400304 s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 s->ssvm.requested_va = sm->next_baseva;
Florin Corasb384b542018-01-15 01:08:33 -0800306 if (a->segment_type == SSVM_SEGMENT_MEMFD)
307 s->ssvm.fd = a->memfd_fd;
308 else
309 s->ssvm.attach_timeout = sm->timeout_in_seconds;
Dave Barach68b0fb02017-02-28 15:15:56 -0500310
Florin Corasb384b542018-01-15 01:08:33 -0800311 if ((rv = ssvm_slave_init (&s->ssvm, a->segment_type)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500312 {
313 _vec_len (s) = vec_len (s) - 1;
314 return (rv);
315 }
316
317 /* Fish the segment header */
Florin Corasb384b542018-01-15 01:08:33 -0800318 s->h = s->ssvm.sh->opaque[0];
Dave Barach68b0fb02017-02-28 15:15:56 -0500319
Dave Barach2c25a622017-06-26 11:35:07 -0400320 vec_add1 (a->new_segment_indices, s - sm->segments);
Dave Barach68b0fb02017-02-28 15:15:56 -0500321 return (0);
322}
323
324void
Florin Corasadc74d72018-12-02 13:36:00 -0800325svm_fifo_segment_delete (svm_fifo_segment_main_t * sm,
326 svm_fifo_segment_private_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500327{
Florin Corasa332c462018-01-31 06:52:17 -0800328 ssvm_delete (&s->ssvm);
Dave Barachb7b92992018-10-17 10:38:51 -0400329 clib_memset (s, 0xfe, sizeof (*s));
Florin Corasb384b542018-01-15 01:08:33 -0800330 pool_put (sm->segments, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500331}
332
Florin Corasb384b542018-01-15 01:08:33 -0800333/**
334 * Allocate fifo in svm segment
335 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500336svm_fifo_t *
Florin Corasfa76a762018-11-29 12:40:10 -0800337svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * fs,
Dave Barach10d8cc62017-05-30 09:30:07 -0400338 u32 data_size_in_bytes,
339 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500340{
341 ssvm_shared_header_t *sh;
342 svm_fifo_segment_header_t *fsh;
Florin Corasb0823242018-04-27 09:46:46 -0700343 svm_fifo_t *f = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500344 void *oldheap;
Dave Barach818eb542017-08-02 13:56:13 -0400345 int freelist_index;
346
347 /*
Florin Corasc87c91d2017-08-16 19:55:49 -0700348 * 4K minimum. It's not likely that anything good will happen
349 * with a smaller FIFO.
Dave Barach818eb542017-08-02 13:56:13 -0400350 */
351 if (data_size_in_bytes < FIFO_SEGMENT_MIN_FIFO_SIZE ||
352 data_size_in_bytes > FIFO_SEGMENT_MAX_FIFO_SIZE)
353 {
354 clib_warning ("fifo size out of range %d", data_size_in_bytes);
355 return 0;
356 }
357
358 freelist_index = max_log2 (data_size_in_bytes)
359 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
Dave Barach68b0fb02017-02-28 15:15:56 -0500360
Florin Corasfa76a762018-11-29 12:40:10 -0800361 sh = fs->ssvm.sh;
Dave Barach2c25a622017-06-26 11:35:07 -0400362 ssvm_lock_non_recursive (sh, 1);
Florin Corasa332c462018-01-31 06:52:17 -0800363 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Dave Barach68b0fb02017-02-28 15:15:56 -0500364
Dave Barach10d8cc62017-05-30 09:30:07 -0400365 switch (list_index)
366 {
367 case FIFO_SEGMENT_RX_FREELIST:
368 case FIFO_SEGMENT_TX_FREELIST:
Dave Barach818eb542017-08-02 13:56:13 -0400369 vec_validate_init_empty (fsh->free_fifos, freelist_index, 0);
Dave Barach818eb542017-08-02 13:56:13 -0400370 f = fsh->free_fifos[freelist_index];
Florin Corasb0823242018-04-27 09:46:46 -0700371 if (PREDICT_FALSE (!f))
Dave Barach10d8cc62017-05-30 09:30:07 -0400372 {
Florin Corasb0823242018-04-27 09:46:46 -0700373 oldheap = ssvm_push_heap (sh);
Dave Barach818eb542017-08-02 13:56:13 -0400374 allocate_new_fifo_chunk (fsh, data_size_in_bytes,
375 FIFO_SEGMENT_ALLOC_CHUNK_SIZE);
Florin Corasb0823242018-04-27 09:46:46 -0700376 ssvm_pop_heap (oldheap);
Dave Barach818eb542017-08-02 13:56:13 -0400377 f = fsh->free_fifos[freelist_index];
378 }
379 if (PREDICT_TRUE (f != 0))
380 {
381 fsh->free_fifos[freelist_index] = f->next;
Dave Barach10d8cc62017-05-30 09:30:07 -0400382 /* (re)initialize the fifo, as in svm_fifo_create */
Dave Barachb7b92992018-10-17 10:38:51 -0400383 clib_memset (f, 0, sizeof (*f));
Dave Barach10d8cc62017-05-30 09:30:07 -0400384 f->nitems = data_size_in_bytes;
385 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
Florin Coras326b81e2018-10-04 19:03:05 -0700386 f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX;
Dave Barach52851e62017-08-07 09:35:25 -0400387 f->refcnt = 1;
Dave Barach818eb542017-08-02 13:56:13 -0400388 f->freelist_index = freelist_index;
Dave Barach10d8cc62017-05-30 09:30:07 -0400389 goto found;
390 }
Florin Corasb0823242018-04-27 09:46:46 -0700391 break;
Dave Barach10d8cc62017-05-30 09:30:07 -0400392 case FIFO_SEGMENT_FREELIST_NONE:
393 break;
394
395 default:
396 clib_warning ("ignore bogus freelist %d", list_index);
397 break;
398 }
399
Florin Corasb0823242018-04-27 09:46:46 -0700400 /* Catch all that allocates just one fifo. Note: this can fail,
401 * in which case: create another segment */
402 oldheap = ssvm_push_heap (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500403 f = svm_fifo_create (data_size_in_bytes);
Florin Corasb0823242018-04-27 09:46:46 -0700404 ssvm_pop_heap (oldheap);
Florin Corasa5464812017-04-19 13:00:05 -0700405 if (PREDICT_FALSE (f == 0))
Florin Corasb0823242018-04-27 09:46:46 -0700406 goto done;
Dave Barach818eb542017-08-02 13:56:13 -0400407 f->freelist_index = freelist_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500408
Dave Barach10d8cc62017-05-30 09:30:07 -0400409found:
410 /* If rx_freelist add to active fifos list. When cleaning up segment,
411 * we need a list of active sessions that should be disconnected. Since
412 * both rx and tx fifos keep pointers to the session, it's enough to track
413 * only one. */
414 if (list_index == FIFO_SEGMENT_RX_FREELIST)
415 {
416 if (fsh->fifos)
417 {
418 fsh->fifos->prev = f;
419 f->next = fsh->fifos;
420 }
421 fsh->fifos = f;
422 }
Florin Corasc87c91d2017-08-16 19:55:49 -0700423 fsh->n_active_fifos++;
Dave Barach10d8cc62017-05-30 09:30:07 -0400424
Florin Corasb0823242018-04-27 09:46:46 -0700425done:
Dave Barach2c25a622017-06-26 11:35:07 -0400426 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500427 return (f);
428}
429
430void
Dave Barach10d8cc62017-05-30 09:30:07 -0400431svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
432 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500433{
434 ssvm_shared_header_t *sh;
435 svm_fifo_segment_header_t *fsh;
Dave Barach818eb542017-08-02 13:56:13 -0400436 int freelist_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500437
Dave Barach52851e62017-08-07 09:35:25 -0400438 ASSERT (f->refcnt > 0);
439
440 if (--f->refcnt > 0)
441 return;
Dave Barach2c25a622017-06-26 11:35:07 -0400442
Dave Barach68b0fb02017-02-28 15:15:56 -0500443 sh = s->ssvm.sh;
444 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Dave Barach68b0fb02017-02-28 15:15:56 -0500445
Dave Barach818eb542017-08-02 13:56:13 -0400446 freelist_index = f->freelist_index;
447
Dave Barachb7f1faa2017-08-29 11:43:37 -0400448 ASSERT (freelist_index < vec_len (fsh->free_fifos));
Dave Barach818eb542017-08-02 13:56:13 -0400449
Dave Barach2c25a622017-06-26 11:35:07 -0400450 ssvm_lock_non_recursive (sh, 2);
Dave Barach68b0fb02017-02-28 15:15:56 -0500451
Dave Barach10d8cc62017-05-30 09:30:07 -0400452 switch (list_index)
453 {
454 case FIFO_SEGMENT_RX_FREELIST:
455 /* Remove from active list */
456 if (f->prev)
457 f->prev->next = f->next;
Florin Corasf03a59a2017-06-09 21:07:32 -0700458 else
459 fsh->fifos = f->next;
Dave Barach10d8cc62017-05-30 09:30:07 -0400460 if (f->next)
461 f->next->prev = f->prev;
Florin Corasf03a59a2017-06-09 21:07:32 -0700462 /* Fall through: we add only rx fifos to active pool */
Dave Barach10d8cc62017-05-30 09:30:07 -0400463 case FIFO_SEGMENT_TX_FREELIST:
464 /* Add to free list */
Dave Barach818eb542017-08-02 13:56:13 -0400465 f->next = fsh->free_fifos[freelist_index];
Florin Corasf03a59a2017-06-09 21:07:32 -0700466 f->prev = 0;
Dave Barach818eb542017-08-02 13:56:13 -0400467 fsh->free_fifos[freelist_index] = f;
Florin Corasf03a59a2017-06-09 21:07:32 -0700468 break;
Dave Barach10d8cc62017-05-30 09:30:07 -0400469 case FIFO_SEGMENT_FREELIST_NONE:
470 break;
471
472 default:
473 clib_warning ("ignore bogus freelist %d", list_index);
474 break;
475 }
476
Florin Coras68810622017-07-24 17:40:28 -0700477 if (CLIB_DEBUG)
478 {
479 f->master_session_index = ~0;
480 f->master_thread_index = ~0;
481 }
482
Florin Corasc87c91d2017-08-16 19:55:49 -0700483 fsh->n_active_fifos--;
Dave Barach2c25a622017-06-26 11:35:07 -0400484 ssvm_unlock_non_recursive (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500485}
486
487void
Florin Corasadc74d72018-12-02 13:36:00 -0800488svm_fifo_segment_main_init (svm_fifo_segment_main_t * sm, u64 baseva,
489 u32 timeout_in_seconds)
Dave Barach68b0fb02017-02-28 15:15:56 -0500490{
Dave Barach68b0fb02017-02-28 15:15:56 -0500491 sm->next_baseva = baseva;
492 sm->timeout_in_seconds = timeout_in_seconds;
493}
494
495u32
Florin Corasadc74d72018-12-02 13:36:00 -0800496svm_fifo_segment_index (svm_fifo_segment_main_t * sm,
497 svm_fifo_segment_private_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500498{
Florin Corasadc74d72018-12-02 13:36:00 -0800499 return s - sm->segments;
Dave Barach68b0fb02017-02-28 15:15:56 -0500500}
501
Florin Corasc87c91d2017-08-16 19:55:49 -0700502/**
503 * Retrieve svm segments pool. Used only for debug purposes.
504 */
505svm_fifo_segment_private_t *
Florin Corasadc74d72018-12-02 13:36:00 -0800506svm_fifo_segment_segments_pool (svm_fifo_segment_main_t * sm)
Florin Corasc87c91d2017-08-16 19:55:49 -0700507{
Florin Corasc87c91d2017-08-16 19:55:49 -0700508 return sm->segments;
509}
510
511/**
512 * Get number of active fifos
513 */
514u32
515svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment)
516{
517 return fifo_segment->h->n_active_fifos;
518}
519
Dave Barach91f3e742017-09-01 19:12:11 -0400520u32
521svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t * fifo_segment,
522 u32 fifo_size_in_bytes)
523{
524 ssvm_shared_header_t *sh;
525 svm_fifo_segment_header_t *fsh;
526 svm_fifo_t *f;
527 int i;
528 u32 count = 0, rounded_data_size, freelist_index;
529
530 sh = fifo_segment->ssvm.sh;
531 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
532
533 /* Count all free fifos? */
534 if (fifo_size_in_bytes == ~0)
535 {
536 for (i = 0; i < vec_len (fsh->free_fifos); i++)
537 {
538 f = fsh->free_fifos[i];
539 if (f == 0)
540 continue;
541
542 while (f)
543 {
544 f = f->next;
545 count++;
546 }
547 }
548 return count;
549 }
550
551 rounded_data_size = (1 << (max_log2 (fifo_size_in_bytes)));
552 freelist_index = max_log2 (rounded_data_size)
553 - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
554
Eyal Baricd307742018-07-22 12:45:15 +0300555 if (freelist_index >= vec_len (fsh->free_fifos))
Dave Barach91f3e742017-09-01 19:12:11 -0400556 return 0;
557
558 f = fsh->free_fifos[freelist_index];
559 if (f == 0)
560 return 0;
561
562 while (f)
563 {
564 f = f->next;
565 count++;
566 }
567 return count;
568}
569
Florin Corasb384b542018-01-15 01:08:33 -0800570void
David Johnsond9818dd2018-12-14 14:53:41 -0500571svm_fifo_segment_info (svm_fifo_segment_private_t * seg, char **address,
572 size_t * size)
Florin Corasb384b542018-01-15 01:08:33 -0800573{
Florin Corasa332c462018-01-31 06:52:17 -0800574 if (ssvm_type (&seg->ssvm) == SSVM_SEGMENT_PRIVATE)
Florin Corasb384b542018-01-15 01:08:33 -0800575 {
Dave Barach6a5adc32018-07-04 10:56:23 -0400576#if USE_DLMALLOC == 0
Florin Corasb384b542018-01-15 01:08:33 -0800577 mheap_t *heap_header;
578
579 *address = pointer_to_uword (seg->ssvm.sh->heap);
580 heap_header = mheap_header (seg->ssvm.sh->heap);
581 *size = heap_header->max_size;
Dave Barach6a5adc32018-07-04 10:56:23 -0400582#else
David Johnsond9818dd2018-12-14 14:53:41 -0500583 mspace_get_address_and_size (seg->ssvm.sh->heap, address, size);
Dave Barach6a5adc32018-07-04 10:56:23 -0400584#endif
Florin Corasb384b542018-01-15 01:08:33 -0800585 }
586 else
587 {
David Johnsond9818dd2018-12-14 14:53:41 -0500588 *address = (char *) seg->ssvm.sh->ssvm_va;
Florin Corasb384b542018-01-15 01:08:33 -0800589 *size = seg->ssvm.ssvm_size;
590 }
591}
592
Dave Barach6a5adc32018-07-04 10:56:23 -0400593void *
Florin Corasb384b542018-01-15 01:08:33 -0800594svm_fifo_segment_heap (svm_fifo_segment_private_t * seg)
595{
596 return seg->ssvm.sh->heap;
597}
598
599u8 *
600format_svm_fifo_segment_type (u8 * s, va_list * args)
601{
602 svm_fifo_segment_private_t *sp;
603 sp = va_arg (*args, svm_fifo_segment_private_t *);
604 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
605
Florin Corasa332c462018-01-31 06:52:17 -0800606 if (st == SSVM_SEGMENT_PRIVATE)
Florin Corasb384b542018-01-15 01:08:33 -0800607 s = format (s, "%s", "private-heap");
Florin Corasb384b542018-01-15 01:08:33 -0800608 else if (st == SSVM_SEGMENT_MEMFD)
609 s = format (s, "%s", "memfd");
610 else if (st == SSVM_SEGMENT_SHM)
611 s = format (s, "%s", "shm");
612 else
613 s = format (s, "%s", "unknown");
614 return s;
615}
616
Dave Barach91f3e742017-09-01 19:12:11 -0400617/**
618 * Segment format function
619 */
620u8 *
621format_svm_fifo_segment (u8 * s, va_list * args)
622{
623 svm_fifo_segment_private_t *sp
624 = va_arg (*args, svm_fifo_segment_private_t *);
Dave Barach6a5adc32018-07-04 10:56:23 -0400625 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasb384b542018-01-15 01:08:33 -0800626 svm_fifo_segment_header_t *fsh = sp->h;
627 u32 count, indent;
Dave Barach91f3e742017-09-01 19:12:11 -0400628 svm_fifo_t *f;
629 int i;
Dave Barach91f3e742017-09-01 19:12:11 -0400630
Florin Corasb384b542018-01-15 01:08:33 -0800631 indent = format_get_indent (s) + 2;
Dave Barach6a5adc32018-07-04 10:56:23 -0400632#if USE_DLMALLOC == 0
Florin Corasb384b542018-01-15 01:08:33 -0800633 s = format (s, "%U segment heap: %U\n", format_white_space, indent,
634 format_mheap, svm_fifo_segment_heap (sp), verbose);
Dave Barach91f3e742017-09-01 19:12:11 -0400635 s = format (s, "%U segment has %u active fifos\n",
636 format_white_space, indent, svm_fifo_segment_num_fifos (sp));
Dave Barach6a5adc32018-07-04 10:56:23 -0400637#endif
Dave Barach91f3e742017-09-01 19:12:11 -0400638
639 for (i = 0; i < vec_len (fsh->free_fifos); i++)
640 {
641 f = fsh->free_fifos[i];
642 if (f == 0)
643 continue;
644 count = 0;
645 while (f)
646 {
647 f = f->next;
648 count++;
649 }
650
651 s = format (s, "%U%-5u Kb: %u free",
652 format_white_space, indent + 2,
653 1 << (i + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE) - 10),
654 count);
655 }
656 return s;
657}
658
Dave Barach68b0fb02017-02-28 15:15:56 -0500659/*
660 * fd.io coding-style-patch-verification: ON
661 *
662 * Local Variables:
663 * eval: (c-set-style "gnu")
664 * End:
665 */