blob: 76cc046efec2e80c4268e88a15c9659d9abb64d0 [file] [log] [blame]
Florin Coras88001c62019-04-24 14:44:46 -07001/*
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:
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/fifo_segment.h>
Florin Corasc547e912020-12-08 17:50:45 -080017#include <vppinfra/mem.h>
Florin Coras88001c62019-04-24 14:44:46 -070018
Florin Coras213b1bb2020-12-07 14:33:58 -080019static inline void *
20fsh_alloc_aligned (fifo_segment_header_t *fsh, uword size, uword align)
21{
22 uword cur_pos, cur_pos_align, new_pos;
23
24 cur_pos = clib_atomic_load_relax_n (&fsh->byte_index);
25 cur_pos_align = round_pow2_u64 (cur_pos, align);
26 size = round_pow2_u64 (size, align);
27 new_pos = cur_pos_align + size;
28
29 if (new_pos >= fsh->max_byte_index)
30 return 0;
31
32 while (!clib_atomic_cmp_and_swap_acq_relax (&fsh->byte_index, &cur_pos,
33 &new_pos, 1 /* weak */))
34 {
35 cur_pos_align = round_pow2_u64 (cur_pos, align);
36 new_pos = cur_pos_align + size;
37 if (new_pos >= fsh->max_byte_index)
38 return 0;
39 }
40 return uword_to_pointer ((u8 *) fsh + cur_pos_align, void *);
41}
42
43static inline void *
44fsh_alloc (fifo_segment_header_t *fsh, uword size)
45{
46 return fsh_alloc_aligned (fsh, size, 8);
47}
48
Florin Coras06d47752020-03-06 02:23:58 +000049static inline fifo_segment_slice_t *
50fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
51{
52 return &fsh->slices[slice_index];
53}
54
Florin Coras1f952d32020-12-09 19:43:21 -080055static inline fifo_slice_private_t *
56fs_slice_private_get (fifo_segment_t *fs, u32 slice_index)
57{
58 ASSERT (slice_index < fs->n_slices);
59 return &fs->slices[slice_index];
60}
61
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000062static char *fifo_segment_mem_status_strings[] = {
63#define _(sym,str) str,
64 foreach_segment_mem_status
65#undef _
66};
67
Florin Coras62ddc032019-12-08 18:30:42 -080068static inline uword
69fsh_n_free_bytes (fifo_segment_header_t * fsh)
70{
Florin Coras213b1bb2020-12-07 14:33:58 -080071 uword cur_pos = clib_atomic_load_relax_n (&fsh->byte_index);
72 ASSERT (fsh->max_byte_index > cur_pos);
73 return fsh->max_byte_index - cur_pos;
Florin Coras8122cc22019-12-18 13:06:41 -080074}
75
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000076static inline void
Florin Coras79b3f8c2020-11-20 07:34:53 -080077fsh_cached_bytes_add (fifo_segment_header_t * fsh, uword size)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000078{
79 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
80}
81
82static inline void
Florin Coras79b3f8c2020-11-20 07:34:53 -080083fsh_cached_bytes_sub (fifo_segment_header_t * fsh, uword size)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000084{
85 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
86}
87
88static inline uword
89fsh_n_cached_bytes (fifo_segment_header_t * fsh)
90{
91 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000092 return n_cached;
93}
94
Florin Coras06d47752020-03-06 02:23:58 +000095static inline void
96fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
97{
98 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
99}
100
Florin Coras7a90e502020-04-09 04:46:14 +0000101static inline u32
102fsh_n_active_fifos (fifo_segment_header_t * fsh)
103{
104 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
105}
106
Florin Coras06d47752020-03-06 02:23:58 +0000107static inline uword
108fsh_virtual_mem (fifo_segment_header_t * fsh)
109{
110 fifo_segment_slice_t *fss;
111 uword total_vm = 0;
112 int i;
113
114 for (i = 0; i < fsh->n_slices; i++)
115 {
116 fss = fsh_slice_get (fsh, i);
117 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
118 }
119 return total_vm;
120}
121
122void
123fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
124 int n_bytes)
125{
126 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
127 fss->virtual_mem += n_bytes;
128}
129
Florin Coras213b1bb2020-12-07 14:33:58 -0800130static inline void
131fss_chunk_freelist_lock (fifo_segment_slice_t *fss)
Florin Coras8122cc22019-12-18 13:06:41 -0800132{
Florin Coras213b1bb2020-12-07 14:33:58 -0800133 u32 free = 0;
134 while (!clib_atomic_cmp_and_swap_acq_relax_n (&fss->chunk_lock, &free, 1, 0))
135 {
136 /* atomic load limits number of compare_exchange executions */
137 while (clib_atomic_load_relax_n (&fss->chunk_lock))
138 CLIB_PAUSE ();
139 /* on failure, compare_exchange writes (*p)->lock into free */
140 free = 0;
141 }
142}
Florin Coras8122cc22019-12-18 13:06:41 -0800143
Florin Coras213b1bb2020-12-07 14:33:58 -0800144static inline void
145fss_chunk_freelist_unlock (fifo_segment_slice_t *fss)
146{
147 /* Make sure all reads/writes are complete before releasing the lock */
148 clib_atomic_release (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800149}
150
Florin Coras473c8462020-11-16 18:11:51 -0800151static inline int
152fss_chunk_fl_index_is_valid (fifo_segment_slice_t * fss, u32 fl_index)
153{
Florin Coras4b8011e2020-12-07 19:38:23 -0800154 return (fl_index < FS_CHUNK_VEC_LEN);
Florin Coras473c8462020-11-16 18:11:51 -0800155}
156
157static void
Florin Corasaf588822020-12-09 12:51:13 -0800158fss_chunk_free_list_push (fifo_segment_header_t *fsh,
159 fifo_segment_slice_t *fss, u32 fl_index,
160 svm_fifo_chunk_t *c)
Florin Coras473c8462020-11-16 18:11:51 -0800161{
Florin Coras213b1bb2020-12-07 14:33:58 -0800162 fss_chunk_freelist_lock (fss);
Florin Coras473c8462020-11-16 18:11:51 -0800163 c->next = fss->free_chunks[fl_index];
Florin Corasaf588822020-12-09 12:51:13 -0800164 fss->free_chunks[fl_index] = fs_chunk_sptr (fsh, c);
Florin Coras213b1bb2020-12-07 14:33:58 -0800165 fss_chunk_freelist_unlock (fss);
Florin Coras473c8462020-11-16 18:11:51 -0800166}
167
168static void
Florin Corasaf588822020-12-09 12:51:13 -0800169fss_chunk_free_list_push_list (fifo_segment_header_t *fsh,
170 fifo_segment_slice_t *fss, u32 fl_index,
171 svm_fifo_chunk_t *head, svm_fifo_chunk_t *tail)
Florin Coras473c8462020-11-16 18:11:51 -0800172{
Florin Coras213b1bb2020-12-07 14:33:58 -0800173 fss_chunk_freelist_lock (fss);
Florin Coras473c8462020-11-16 18:11:51 -0800174 tail->next = fss->free_chunks[fl_index];
Florin Corasaf588822020-12-09 12:51:13 -0800175 fss->free_chunks[fl_index] = fs_chunk_sptr (fsh, head);
Florin Coras213b1bb2020-12-07 14:33:58 -0800176 fss_chunk_freelist_unlock (fss);
Florin Coras473c8462020-11-16 18:11:51 -0800177}
178
179static svm_fifo_chunk_t *
Florin Corasaf588822020-12-09 12:51:13 -0800180fss_chunk_free_list_pop (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss,
181 u32 fl_index)
Florin Coras473c8462020-11-16 18:11:51 -0800182{
183 svm_fifo_chunk_t *c;
184
185 ASSERT (fss_chunk_fl_index_is_valid (fss, fl_index));
186
Florin Coras213b1bb2020-12-07 14:33:58 -0800187 fss_chunk_freelist_lock (fss);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800188
Florin Coras473c8462020-11-16 18:11:51 -0800189 if (!fss->free_chunks[fl_index])
Florin Corasd01d2ce2020-11-17 18:42:38 -0800190 {
Florin Coras213b1bb2020-12-07 14:33:58 -0800191 fss_chunk_freelist_unlock (fss);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800192 return 0;
193 }
Florin Coras473c8462020-11-16 18:11:51 -0800194
Florin Corasaf588822020-12-09 12:51:13 -0800195 c = fs_chunk_ptr (fsh, fss->free_chunks[fl_index]);
Florin Coras473c8462020-11-16 18:11:51 -0800196 fss->free_chunks[fl_index] = c->next;
197
Florin Coras213b1bb2020-12-07 14:33:58 -0800198 fss_chunk_freelist_unlock (fss);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800199
Florin Coras473c8462020-11-16 18:11:51 -0800200 return c;
201}
202
Florin Coras17672aa2020-12-29 16:55:32 -0800203static void
204fss_fifo_free_list_push (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss,
205 svm_fifo_shared_t *sf)
206{
207 sf->next = fss->free_fifos;
208 fss->free_fifos = fs_sptr (fsh, sf);
209}
210
211static void
212fss_fifo_free_list_push_list (fifo_segment_header_t *fsh,
213 fifo_segment_slice_t *fss,
214 svm_fifo_shared_t *head, svm_fifo_shared_t *tail)
215{
216 tail->next = fss->free_fifos;
217 fss->free_fifos = fs_sptr (fsh, head);
218}
219
220svm_fifo_shared_t *
221fss_fifo_free_list_pop (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss)
222{
223 svm_fifo_shared_t *sf;
224 sf = fs_ptr (fsh, fss->free_fifos);
225 fss->free_fifos = sf->next;
226 return sf;
227}
228
Florin Coras473c8462020-11-16 18:11:51 -0800229static inline void
Florin Coras1f952d32020-12-09 19:43:21 -0800230pfss_fifo_add_active_list (fifo_slice_private_t *pfss, svm_fifo_t *f)
Florin Coras473c8462020-11-16 18:11:51 -0800231{
Florin Coras1f952d32020-12-09 19:43:21 -0800232 if (pfss->active_fifos)
Florin Coras473c8462020-11-16 18:11:51 -0800233 {
Florin Coras1f952d32020-12-09 19:43:21 -0800234 pfss->active_fifos->prev = f;
235 f->next = pfss->active_fifos;
Florin Coras473c8462020-11-16 18:11:51 -0800236 }
Florin Coras1f952d32020-12-09 19:43:21 -0800237 pfss->active_fifos = f;
Florin Coras473c8462020-11-16 18:11:51 -0800238}
239
240static inline void
Florin Coras1f952d32020-12-09 19:43:21 -0800241pfss_fifo_del_active_list (fifo_slice_private_t *pfss, svm_fifo_t *f)
Florin Coras473c8462020-11-16 18:11:51 -0800242{
243 if (f->flags & SVM_FIFO_F_LL_TRACKED)
244 {
245 if (f->prev)
246 f->prev->next = f->next;
247 else
Florin Coras1f952d32020-12-09 19:43:21 -0800248 pfss->active_fifos = f->next;
Florin Coras473c8462020-11-16 18:11:51 -0800249 if (f->next)
250 f->next->prev = f->prev;
251 }
252}
253
Florin Corasd01d2ce2020-11-17 18:42:38 -0800254static inline uword
255fss_fl_chunk_bytes (fifo_segment_slice_t * fss)
256{
257 return clib_atomic_load_relax_n (&fss->n_fl_chunk_bytes);
258}
259
260static inline void
261fss_fl_chunk_bytes_add (fifo_segment_slice_t * fss, uword size)
262{
263 clib_atomic_fetch_add_relax (&fss->n_fl_chunk_bytes, size);
264}
265
266static inline void
267fss_fl_chunk_bytes_sub (fifo_segment_slice_t * fss, uword size)
268{
269 clib_atomic_fetch_sub_relax (&fss->n_fl_chunk_bytes, size);
270}
271
Florin Corasf9d4ab42019-05-11 16:55:53 -0700272/**
Florin Coras88001c62019-04-24 14:44:46 -0700273 * Initialize fifo segment shared header
274 */
275int
276fifo_segment_init (fifo_segment_t * fs)
277{
Florin Corasc547e912020-12-08 17:50:45 -0800278 u32 align = 8, offset = 2 * 4096, slices_sz, i;
Florin Coras4b8011e2020-12-07 19:38:23 -0800279 uword max_fifo, seg_start, seg_sz;
Florin Coras88001c62019-04-24 14:44:46 -0700280 fifo_segment_header_t *fsh;
281 ssvm_shared_header_t *sh;
Florin Coras213b1bb2020-12-07 14:33:58 -0800282 void *seg_data;
Florin Coras88001c62019-04-24 14:44:46 -0700283
Florin Coras213b1bb2020-12-07 14:33:58 -0800284 /* TODO remove ssvm heap entirely */
Florin Coras88001c62019-04-24 14:44:46 -0700285 sh = fs->ssvm.sh;
Florin Coras88001c62019-04-24 14:44:46 -0700286
Florin Coras213b1bb2020-12-07 14:33:58 -0800287 seg_data = (u8 *) sh + offset;
Florin Coras4b8011e2020-12-07 19:38:23 -0800288 seg_sz = sh->ssvm_size - offset;
289
290 fs->n_slices = clib_max (fs->n_slices, 1);
291 slices_sz = sizeof (fifo_segment_slice_t) * fs->n_slices;
Florin Coras213b1bb2020-12-07 14:33:58 -0800292
293 seg_start = round_pow2_u64 (pointer_to_uword (seg_data), align);
294 fsh = uword_to_pointer (seg_start, void *);
Florin Coras4b8011e2020-12-07 19:38:23 -0800295 memset (fsh, 0, sizeof (*fsh) + slices_sz);
Florin Coras213b1bb2020-12-07 14:33:58 -0800296
Florin Coras4b8011e2020-12-07 19:38:23 -0800297 fsh->byte_index = sizeof (*fsh) + slices_sz;
298 fsh->max_byte_index = seg_sz;
Florin Coras62ddc032019-12-08 18:30:42 -0800299 fsh->n_slices = fs->n_slices;
Florin Coras4b8011e2020-12-07 19:38:23 -0800300 max_fifo = clib_min ((seg_sz - slices_sz) / 2, FIFO_SEGMENT_MAX_FIFO_SIZE);
301 fsh->max_log2_fifo_size = min_log2 (max_fifo);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000302 fsh->n_cached_bytes = 0;
Florin Coras213b1bb2020-12-07 14:33:58 -0800303 fsh->n_reserved_bytes = fsh->byte_index;
Florin Coras80b74252021-01-27 18:08:25 -0800304 fsh->start_byte_index = fsh->byte_index;
Florin Coras4b8011e2020-12-07 19:38:23 -0800305 ASSERT (fsh->max_byte_index <= sh->ssvm_size - offset);
306
307 fs->max_byte_index = fsh->max_byte_index;
Florin Coras14f066e2020-12-10 18:52:40 -0800308 fs->h = fsh;
309 sh->opaque[0] = (void *) ((u8 *) fsh - (u8 *) fs->ssvm.sh);
310
311 /* Allow random offsets */
312 fs->ssvm.sh->ssvm_va = 0;
Florin Coras4b8011e2020-12-07 19:38:23 -0800313
Florin Corasc547e912020-12-08 17:50:45 -0800314 vec_validate (fs->slices, fs->n_slices - 1);
315 for (i = 0; i < fs->n_slices; i++)
316 fs->slices[i].fifos =
317 clib_mem_bulk_init (sizeof (svm_fifo_t), CLIB_CACHE_LINE_BYTES, 32);
318
Florin Coras88001c62019-04-24 14:44:46 -0700319 sh->ready = 1;
320 return (0);
321}
322
323/**
Florin Coras88001c62019-04-24 14:44:46 -0700324 * Create a fifo segment and initialize as master
325 */
326int
327fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
328{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700329 fifo_segment_t *fs;
330 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700331 int rv;
332
Florin Coras88001c62019-04-24 14:44:46 -0700333 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700334 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700335
Florin Corasf9d4ab42019-05-11 16:55:53 -0700336 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
337 fs->ssvm.ssvm_size = a->segment_size;
Florin Coras5220a262020-09-29 18:11:24 -0700338 fs->ssvm.is_server = 1;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700339 fs->ssvm.my_pid = getpid ();
340 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
341 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700342
Florin Coras5220a262020-09-29 18:11:24 -0700343 if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700344 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700345 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700346 return (rv);
347 }
348
349 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700350 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700351
Florin Corasf9d4ab42019-05-11 16:55:53 -0700352 fifo_segment_init (fs);
353 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700354 return (0);
355}
356
357/**
358 * Attach as slave to a fifo segment
359 */
360int
361fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
362{
Florin Coras4b8011e2020-12-07 19:38:23 -0800363 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800364 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700365 int rv;
366
Florin Coras62ddc032019-12-08 18:30:42 -0800367 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700368
Florin Coras62ddc032019-12-08 18:30:42 -0800369 fs->ssvm.ssvm_size = a->segment_size;
370 fs->ssvm.my_pid = getpid ();
371 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
Florin Coras14f066e2020-12-10 18:52:40 -0800372 fs->ssvm.requested_va = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700373 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800374 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700375 else
Florin Coras62ddc032019-12-08 18:30:42 -0800376 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700377
Florin Coras5220a262020-09-29 18:11:24 -0700378 if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700379 {
Florin Coras14f066e2020-12-10 18:52:40 -0800380 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700381 return (rv);
382 }
383
Florin Coras4b8011e2020-12-07 19:38:23 -0800384 /* Probably a segment without fifos */
Florin Coras14f066e2020-12-10 18:52:40 -0800385 if (!fs->ssvm.sh->opaque[0])
Florin Coras4b8011e2020-12-07 19:38:23 -0800386 goto done;
387
Florin Coras14f066e2020-12-10 18:52:40 -0800388 fsh = fs->h = (void *) fs->ssvm.sh + (uword) fs->ssvm.sh->opaque[0];
Florin Coras4b8011e2020-12-07 19:38:23 -0800389 fs->max_byte_index = fsh->max_byte_index;
Florin Corasc547e912020-12-08 17:50:45 -0800390 vec_validate (fs->slices, 0);
391 fs->slices[0].fifos =
392 clib_mem_bulk_init (sizeof (svm_fifo_t), CLIB_CACHE_LINE_BYTES, 32);
Florin Coras4b8011e2020-12-07 19:38:23 -0800393
394done:
Florin Coras62ddc032019-12-08 18:30:42 -0800395 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700396 return (0);
397}
398
399void
400fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
401{
Florin Corasc547e912020-12-08 17:50:45 -0800402 fifo_segment_cleanup (s);
Florin Coras88001c62019-04-24 14:44:46 -0700403 ssvm_delete (&s->ssvm);
404 clib_memset (s, 0xfe, sizeof (*s));
405 pool_put (sm->segments, s);
406}
407
408u32
409fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
410{
411 return s - sm->segments;
412}
413
Florin Coras88001c62019-04-24 14:44:46 -0700414fifo_segment_t *
415fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
416{
417 return pool_elt_at_index (sm->segments, segment_index);
418}
419
420void
421fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
422{
Florin Coras404b8a32019-05-09 12:08:06 -0700423 *address = (char *) seg->ssvm.sh->ssvm_va;
424 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700425}
426
427void
428fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
429 u32 timeout_in_seconds)
430{
431 sm->next_baseva = baseva;
432 sm->timeout_in_seconds = timeout_in_seconds;
433}
434
Florin Corasf9d4ab42019-05-11 16:55:53 -0700435static inline u32
436fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700437{
Florin Corasf22f4e52019-12-19 16:10:58 -0800438 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
439 return 0;
Florin Coras4b8011e2020-12-07 19:38:23 -0800440 return clib_min (max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE,
441 FS_CHUNK_VEC_LEN - 1);
Florin Coras88001c62019-04-24 14:44:46 -0700442}
443
Florin Corasf9d4ab42019-05-11 16:55:53 -0700444static inline u32
445fs_freelist_index_to_size (u32 fl_index)
446{
Florin Coras62ddc032019-12-08 18:30:42 -0800447 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700448}
449
Florin Coras88001c62019-04-24 14:44:46 -0700450static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800451fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700452{
453 /*
454 * 4K minimum. It's not likely that anything good will happen
455 * with a smaller FIFO.
456 */
Florin Coras4b8011e2020-12-07 19:38:23 -0800457 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE &&
458 size <= (1ULL << fsh->max_log2_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700459}
460
Florin Coras8e755a12020-02-06 16:59:31 +0000461svm_fifo_chunk_t *
462fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
463 fifo_segment_slice_t * fss, u32 data_bytes)
464{
465 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
466 svm_fifo_chunk_t *c, *first = 0, *next;
467
468 fl_index = fs_freelist_for_size (req_bytes);
469 if (fl_index > 0)
470 fl_index -= 1;
471
472 fl_size = fs_freelist_index_to_size (fl_index);
473
474 while (req_bytes)
475 {
Florin Corasaf588822020-12-09 12:51:13 -0800476 c = fss_chunk_free_list_pop (fsh, fss, fl_index);
Florin Coras8e755a12020-02-06 16:59:31 +0000477 if (c)
478 {
Florin Corasaf588822020-12-09 12:51:13 -0800479 c->next = fs_chunk_sptr (fsh, first);
Florin Coras8e755a12020-02-06 16:59:31 +0000480 first = c;
481 n_alloc += fl_size;
482 req_bytes -= clib_min (fl_size, req_bytes);
483 }
484 else
485 {
486 /* Failed to allocate with smaller chunks */
487 if (fl_index == 0)
488 {
Florin Coras473c8462020-11-16 18:11:51 -0800489 /* Free all chunks if any allocated */
Florin Coras8e755a12020-02-06 16:59:31 +0000490 c = first;
491 while (c)
492 {
493 fl_index = fs_freelist_for_size (c->length);
Florin Corasaf588822020-12-09 12:51:13 -0800494 next = fs_chunk_ptr (fsh, c->next);
495 fss_chunk_free_list_push (fsh, fss, fl_index, c);
Florin Coras8e755a12020-02-06 16:59:31 +0000496 c = next;
497 }
498 n_alloc = 0;
499 first = 0;
Florin Coras473c8462020-11-16 18:11:51 -0800500 /* As last attempt, try allocating a chunk larger than
501 * the requested size, if possible */
502 fl_index = fs_freelist_for_size (data_bytes) + 1;
503 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
504 return 0;
Florin Corasaf588822020-12-09 12:51:13 -0800505 first = fss_chunk_free_list_pop (fsh, fss, fl_index);
Florin Coras473c8462020-11-16 18:11:51 -0800506 if (first)
Florin Coras8e755a12020-02-06 16:59:31 +0000507 {
Florin Coras473c8462020-11-16 18:11:51 -0800508 first->next = 0;
509 n_alloc = fs_freelist_index_to_size (fl_index);
510 goto done;
Florin Coras8e755a12020-02-06 16:59:31 +0000511 }
Florin Coras8e755a12020-02-06 16:59:31 +0000512 return 0;
513 }
514 fl_index -= 1;
515 fl_size = fl_size >> 1;
516 }
517 }
518
Florin Coras473c8462020-11-16 18:11:51 -0800519done:
Florin Corasd01d2ce2020-11-17 18:42:38 -0800520 fss_fl_chunk_bytes_sub (fss, n_alloc);
Florin Coras8e755a12020-02-06 16:59:31 +0000521 fsh_cached_bytes_sub (fsh, n_alloc);
522 return first;
523}
524
Florin Corasf9d4ab42019-05-11 16:55:53 -0700525static int
Florin Coras473c8462020-11-16 18:11:51 -0800526fsh_try_alloc_fifo_hdr_batch (fifo_segment_header_t * fsh,
527 fifo_segment_slice_t * fss, u32 batch_size)
Florin Coras8e755a12020-02-06 16:59:31 +0000528{
Florin Coras17672aa2020-12-29 16:55:32 -0800529 svm_fifo_shared_t *f, *head = 0, *tail;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800530 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700531 u8 *fmem;
532 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700533
Benoît Gannef23b1512020-11-25 15:06:01 +0100534 ASSERT (batch_size != 0);
535
Florin Coras473c8462020-11-16 18:11:51 -0800536 size = (uword) sizeof (*f) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700537
Florin Coras213b1bb2020-12-07 14:33:58 -0800538 fmem = fsh_alloc_aligned (fsh, size, CLIB_CACHE_LINE_BYTES);
Florin Corascefd5d82019-05-05 13:19:57 -0700539 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700540 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700541
Florin Coras635f5062020-04-28 20:40:57 +0000542 /* Carve fifo hdr space */
Florin Coras17672aa2020-12-29 16:55:32 -0800543 tail = f = (svm_fifo_shared_t *) fmem;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700544 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700545 {
Florin Coras17672aa2020-12-29 16:55:32 -0800546 clib_memset (f, 0, sizeof (*f));
547 f->next = fs_sptr (fsh, head);
548 head = f;
Florin Coras635f5062020-04-28 20:40:57 +0000549 fmem += sizeof (*f);
Florin Coras17672aa2020-12-29 16:55:32 -0800550 f = (svm_fifo_shared_t *) fmem;
Florin Coras635f5062020-04-28 20:40:57 +0000551 }
552
Florin Coras17672aa2020-12-29 16:55:32 -0800553 fss_fifo_free_list_push_list (fsh, fss, head, tail);
554
Florin Coras473c8462020-11-16 18:11:51 -0800555 return 0;
556}
557
558static int
559fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
560 fifo_segment_slice_t * fss,
561 u32 fl_index, u32 batch_size)
562{
Florin Coras473c8462020-11-16 18:11:51 -0800563 svm_fifo_chunk_t *c, *head = 0, *tail;
Florin Coras79b3f8c2020-11-20 07:34:53 -0800564 uword size, total_chunk_bytes;
565 u32 rounded_data_size;
Florin Coras473c8462020-11-16 18:11:51 -0800566 u8 *cmem;
567 int i;
568
Benoît Gannef23b1512020-11-25 15:06:01 +0100569 ASSERT (batch_size != 0);
570
Florin Coras473c8462020-11-16 18:11:51 -0800571 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras79b3f8c2020-11-20 07:34:53 -0800572 total_chunk_bytes = (uword) batch_size *rounded_data_size;
Florin Coras473c8462020-11-16 18:11:51 -0800573 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
574
Florin Coras213b1bb2020-12-07 14:33:58 -0800575 cmem = fsh_alloc_aligned (fsh, size, 8 /* chunk hdr is 24B */);
Florin Coras473c8462020-11-16 18:11:51 -0800576 if (cmem == 0)
577 return -1;
578
579 /* Carve fifo + chunk space */
580 tail = c = (svm_fifo_chunk_t *) cmem;
Florin Coras635f5062020-04-28 20:40:57 +0000581 for (i = 0; i < batch_size; i++)
582 {
Florin Corascefd5d82019-05-05 13:19:57 -0700583 c->start_byte = 0;
584 c->length = rounded_data_size;
Florin Corasaf588822020-12-09 12:51:13 -0800585 c->next = fs_chunk_sptr (fsh, head);
Florin Coras473c8462020-11-16 18:11:51 -0800586 head = c;
587 cmem += sizeof (*c) + rounded_data_size;
588 c = (svm_fifo_chunk_t *) cmem;
Florin Coras88001c62019-04-24 14:44:46 -0700589 }
Florin Corascefd5d82019-05-05 13:19:57 -0700590
Florin Corasaf588822020-12-09 12:51:13 -0800591 fss_chunk_free_list_push_list (fsh, fss, fl_index, head, tail);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000592 fss->num_chunks[fl_index] += batch_size;
Florin Coras79b3f8c2020-11-20 07:34:53 -0800593 fss_fl_chunk_bytes_add (fss, total_chunk_bytes);
594 fsh_cached_bytes_add (fsh, total_chunk_bytes);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700595
596 return 0;
597}
598
Florin Coras473c8462020-11-16 18:11:51 -0800599static int
600fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
601 fifo_segment_slice_t * fss,
602 u32 fl_index, u32 batch_size)
603{
604 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size))
605 return 0;
606 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
607}
608
Florin Corasc547e912020-12-08 17:50:45 -0800609static svm_fifo_shared_t *
610fsh_try_alloc_fifo_hdr (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss)
Florin Coras473c8462020-11-16 18:11:51 -0800611{
Florin Coras17672aa2020-12-29 16:55:32 -0800612 svm_fifo_shared_t *sf;
Florin Coras473c8462020-11-16 18:11:51 -0800613
614 if (!fss->free_fifos)
615 {
616 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss,
617 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
618 return 0;
619 }
620
Florin Coras17672aa2020-12-29 16:55:32 -0800621 sf = fss_fifo_free_list_pop (fsh, fss);
622 clib_memset (sf, 0, sizeof (*sf));
623
624 return sf;
Florin Coras473c8462020-11-16 18:11:51 -0800625}
626
627static svm_fifo_chunk_t *
628fsh_try_alloc_chunk (fifo_segment_header_t * fsh,
629 fifo_segment_slice_t * fss, u32 data_bytes)
630{
631 svm_fifo_chunk_t *c;
632 u32 fl_index;
633
634 fl_index = fs_freelist_for_size (data_bytes);
635
636free_list:
Florin Corasaf588822020-12-09 12:51:13 -0800637 c = fss_chunk_free_list_pop (fsh, fss, fl_index);
Florin Coras473c8462020-11-16 18:11:51 -0800638 if (c)
639 {
640 c->next = 0;
Florin Corasd01d2ce2020-11-17 18:42:38 -0800641 fss_fl_chunk_bytes_sub (fss, fs_freelist_index_to_size (fl_index));
Florin Coras473c8462020-11-16 18:11:51 -0800642 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
643 }
644 else
645 {
646 u32 chunk_size, batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
647 uword n_free;
648
649 chunk_size = fs_freelist_index_to_size (fl_index);
650 n_free = fsh_n_free_bytes (fsh);
651
652 if (chunk_size <= n_free)
653 {
654 batch = chunk_size * batch <= n_free ? batch : 1;
655 if (!fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch))
656 goto free_list;
657 }
658 /* Failed to allocate larger chunk, try to allocate multi-chunk
659 * that is close to what was actually requested */
Florin Corasd01d2ce2020-11-17 18:42:38 -0800660 if (data_bytes <= fss_fl_chunk_bytes (fss))
Florin Coras473c8462020-11-16 18:11:51 -0800661 {
662 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
663 if (c)
664 goto done;
665 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
666 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
Florin Coras213b1bb2020-12-07 14:33:58 -0800667 goto done;
Florin Coras473c8462020-11-16 18:11:51 -0800668 }
Florin Corasd01d2ce2020-11-17 18:42:38 -0800669 if (data_bytes <= fss_fl_chunk_bytes (fss) + n_free)
Florin Coras473c8462020-11-16 18:11:51 -0800670 {
671 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
672
Florin Corasd01d2ce2020-11-17 18:42:38 -0800673 batch = (data_bytes - fss_fl_chunk_bytes (fss)) / min_size;
Florin Coras473c8462020-11-16 18:11:51 -0800674 batch = clib_min (batch + 1, n_free / min_size);
675 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
Florin Coras213b1bb2020-12-07 14:33:58 -0800676 goto done;
Florin Coras473c8462020-11-16 18:11:51 -0800677 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
678 }
679 }
680
681done:
682
683 return c;
684}
685
Florin Corasf9d4ab42019-05-11 16:55:53 -0700686/**
687 * Try to allocate new fifo
688 *
689 * Tries the following steps in order:
690 * - grab fifo and chunk from freelists
691 * - batch fifo and chunk allocation
692 * - single fifo allocation
693 * - grab multiple fifo chunks from freelists
694 */
Florin Corasc547e912020-12-08 17:50:45 -0800695static svm_fifo_shared_t *
696fs_try_alloc_fifo (fifo_segment_header_t *fsh, u32 slice_index, u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700697{
Florin Corasc547e912020-12-08 17:50:45 -0800698 fifo_segment_slice_t *fss;
Florin Corasd01d2ce2020-11-17 18:42:38 -0800699 u32 fl_index, min_size;
Florin Coras473c8462020-11-16 18:11:51 -0800700 svm_fifo_chunk_t *c;
Florin Corasc547e912020-12-08 17:50:45 -0800701 svm_fifo_shared_t *sf = 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700702
Florin Corasc547e912020-12-08 17:50:45 -0800703 fss = fsh_slice_get (fsh, slice_index);
Florin Coras2de9c0f2020-02-02 19:30:39 +0000704 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
705 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700706
Florin Coras473c8462020-11-16 18:11:51 -0800707 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
Florin Coras0e6199d2020-04-17 20:15:22 +0000708 return 0;
709
Florin Corasc547e912020-12-08 17:50:45 -0800710 sf = fsh_try_alloc_fifo_hdr (fsh, fss);
711 if (!sf)
Florin Corasd01d2ce2020-11-17 18:42:38 -0800712 return 0;
Florin Coras8122cc22019-12-18 13:06:41 -0800713
Florin Coras473c8462020-11-16 18:11:51 -0800714 c = fsh_try_alloc_chunk (fsh, fss, min_size);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800715 if (!c)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700716 {
Florin Coras17672aa2020-12-29 16:55:32 -0800717 fss_fifo_free_list_push (fsh, fss, sf);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800718 return 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700719 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700720
Florin Corasaf588822020-12-09 12:51:13 -0800721 sf->start_chunk = fs_chunk_sptr (fsh, c);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800722 while (c->next)
Florin Corasaf588822020-12-09 12:51:13 -0800723 c = fs_chunk_ptr (fsh, c->next);
724 sf->end_chunk = fs_chunk_sptr (fsh, c);
Florin Corasc547e912020-12-08 17:50:45 -0800725 sf->size = data_bytes;
726 sf->slice_index = slice_index;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700727
Florin Corasc547e912020-12-08 17:50:45 -0800728 return sf;
Florin Coras88001c62019-04-24 14:44:46 -0700729}
730
Florin Corasf22f4e52019-12-19 16:10:58 -0800731svm_fifo_chunk_t *
732fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
733{
734 fifo_segment_slice_t *fss;
735 svm_fifo_chunk_t *c;
Florin Corasf22f4e52019-12-19 16:10:58 -0800736
Florin Corasf22f4e52019-12-19 16:10:58 -0800737 fss = fsh_slice_get (fsh, slice_index);
Florin Coras473c8462020-11-16 18:11:51 -0800738 c = fsh_try_alloc_chunk (fsh, fss, chunk_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800739
740 return c;
741}
742
743static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000744fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000745 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800746{
Florin Coras473c8462020-11-16 18:11:51 -0800747 u32 n_collect = 0, fl_index;
Florin Corasf22f4e52019-12-19 16:10:58 -0800748 svm_fifo_chunk_t *next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800749
Florin Coras9e61d9a2020-02-05 21:13:18 +0000750 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800751 {
Benoît Gannedf601ae2020-10-20 14:31:55 +0200752 CLIB_MEM_UNPOISON (c, sizeof (*c));
Florin Corasaf588822020-12-09 12:51:13 -0800753 next = fs_chunk_ptr (fsh, c->next);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000754 fl_index = fs_freelist_for_size (c->length);
Florin Corasaf588822020-12-09 12:51:13 -0800755 fss_chunk_free_list_push (fsh, fss, fl_index, c);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000756 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000757 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800758 }
759
Florin Corasd01d2ce2020-11-17 18:42:38 -0800760 fss_fl_chunk_bytes_add (fss, n_collect);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000761 fsh_cached_bytes_add (fsh, n_collect);
Florin Corasf22f4e52019-12-19 16:10:58 -0800762}
763
764void
765fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000766 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800767{
768 fifo_segment_slice_t *fss;
769 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000770 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800771}
772
Florin Corasc547e912020-12-08 17:50:45 -0800773svm_fifo_t *
774fs_fifo_alloc (fifo_segment_t *fs, u32 slice_index)
775{
776 fifo_slice_private_t *pfss = &fs->slices[slice_index];
777 svm_fifo_t *f;
778
779 f = clib_mem_bulk_alloc (pfss->fifos);
780 clib_memset (f, 0, sizeof (*f));
781 return f;
782}
783
784void
785fs_fifo_free (fifo_segment_t *fs, svm_fifo_t *f)
786{
787 u32 slice_index = f->shr->slice_index;
788 fifo_slice_private_t *pfss;
789
790 pfss = &fs->slices[slice_index];
791 clib_mem_bulk_free (pfss->fifos, f);
792}
793
794void
795fifo_segment_cleanup (fifo_segment_t *fs)
796{
797 int slice_index;
Florin Corasb4624182020-12-11 13:58:12 -0800798 svm_msg_q_t *mq = 0;
Florin Corasc547e912020-12-08 17:50:45 -0800799
800 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
801 clib_mem_bulk_destroy (fs->slices[slice_index].fifos);
Florin Corasb4624182020-12-11 13:58:12 -0800802
803 vec_foreach (fs->mqs, mq)
804 vec_free (mq->rings);
805
806 vec_free (fs->mqs);
Florin Corasc547e912020-12-08 17:50:45 -0800807}
808
Florin Coras88001c62019-04-24 14:44:46 -0700809/**
810 * Allocate fifo in fifo segment
811 */
812svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800813fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
814 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700815{
Florin Coras62ddc032019-12-08 18:30:42 -0800816 fifo_segment_header_t *fsh = fs->h;
Florin Coras1f952d32020-12-09 19:43:21 -0800817 fifo_slice_private_t *pfss;
Florin Coras62ddc032019-12-08 18:30:42 -0800818 fifo_segment_slice_t *fss;
Florin Corasc547e912020-12-08 17:50:45 -0800819 svm_fifo_shared_t *sf;
Florin Coras88001c62019-04-24 14:44:46 -0700820 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700821
Florin Coras62ddc032019-12-08 18:30:42 -0800822 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700823
Florin Coras4b8011e2020-12-07 19:38:23 -0800824 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_fifo_size))
Florin Coras0e6199d2020-04-17 20:15:22 +0000825 return 0;
826
Florin Corasc547e912020-12-08 17:50:45 -0800827 sf = fs_try_alloc_fifo (fsh, slice_index, data_bytes);
828 if (!sf)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700829 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700830
Florin Corasc547e912020-12-08 17:50:45 -0800831 f = fs_fifo_alloc (fs, slice_index);
832 f->fs_hdr = fsh;
833 f->shr = sf;
Florin Coras62ddc032019-12-08 18:30:42 -0800834
Florin Corascefd5d82019-05-05 13:19:57 -0700835 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700836
Florin Corasc547e912020-12-08 17:50:45 -0800837 fss = fsh_slice_get (fsh, slice_index);
Florin Coras1f952d32020-12-09 19:43:21 -0800838 pfss = fs_slice_private_get (fs, slice_index);
Florin Corasc547e912020-12-08 17:50:45 -0800839
Florin Coras88001c62019-04-24 14:44:46 -0700840 /* If rx fifo type add to active fifos list. When cleaning up segment,
841 * we need a list of active sessions that should be disconnected. Since
842 * both rx and tx fifos keep pointers to the session, it's enough to track
843 * only one. */
844 if (ftype == FIFO_SEGMENT_RX_FIFO)
845 {
Florin Coras1f952d32020-12-09 19:43:21 -0800846 pfss_fifo_add_active_list (pfss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700847 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800848 }
849
Florin Coras62ddc032019-12-08 18:30:42 -0800850 fsh_active_fifos_update (fsh, 1);
Florin Coras06d47752020-03-06 02:23:58 +0000851 fss->virtual_mem += svm_fifo_size (f);
Florin Coras88001c62019-04-24 14:44:46 -0700852
853done:
Florin Coras88001c62019-04-24 14:44:46 -0700854 return (f);
855}
856
Florin Corasc547e912020-12-08 17:50:45 -0800857svm_fifo_t *
Florin Coras14f066e2020-12-10 18:52:40 -0800858fifo_segment_alloc_fifo_w_offset (fifo_segment_t *fs, uword offset)
Florin Corasc547e912020-12-08 17:50:45 -0800859{
860 svm_fifo_t *f = fs_fifo_alloc (fs, 0);
Florin Coras14f066e2020-12-10 18:52:40 -0800861 svm_fifo_shared_t *sf;
862
863 sf = (svm_fifo_shared_t *) ((u8 *) fs->h + offset);
Florin Corasc547e912020-12-08 17:50:45 -0800864 f->fs_hdr = fs->h;
865 f->shr = sf;
866
867 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
868 f->segment_index = SVM_FIFO_INVALID_INDEX;
869 f->refcnt = 1;
870 return f;
871}
872
Florin Coras88001c62019-04-24 14:44:46 -0700873/**
874 * Free fifo allocated in fifo segment
875 */
876void
Florin Corasb095a3c2019-04-25 12:58:46 -0700877fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700878{
Florin Coras62ddc032019-12-08 18:30:42 -0800879 fifo_segment_header_t *fsh = fs->h;
Florin Coras1f952d32020-12-09 19:43:21 -0800880 fifo_slice_private_t *pfss;
Florin Coras62ddc032019-12-08 18:30:42 -0800881 fifo_segment_slice_t *fss;
Florin Corasc547e912020-12-08 17:50:45 -0800882 svm_fifo_shared_t *sf;
Florin Coras88001c62019-04-24 14:44:46 -0700883
884 ASSERT (f->refcnt > 0);
885
886 if (--f->refcnt > 0)
887 return;
888
Florin Corasc547e912020-12-08 17:50:45 -0800889 /*
890 * Cleanup shared state
891 */
892
893 sf = f->shr;
894 fss = fsh_slice_get (fsh, sf->slice_index);
Florin Coras1f952d32020-12-09 19:43:21 -0800895 pfss = fs_slice_private_get (fs, sf->slice_index);
Florin Corasc547e912020-12-08 17:50:45 -0800896
897 /* Free fifo chunks */
Florin Corasaf588822020-12-09 12:51:13 -0800898 fsh_slice_collect_chunks (fsh, fss, fs_chunk_ptr (fsh, f->shr->start_chunk));
Florin Corasc547e912020-12-08 17:50:45 -0800899
900 sf->start_chunk = sf->end_chunk = 0;
901 sf->head_chunk = sf->tail_chunk = 0;
902
903 /* Add to free list */
Florin Coras17672aa2020-12-29 16:55:32 -0800904 fss_fifo_free_list_push (fsh, fss, sf);
Florin Corasc547e912020-12-08 17:50:45 -0800905
906 fss->virtual_mem -= svm_fifo_size (f);
907
908 /*
909 * Cleanup private state
910 */
Florin Coras88001c62019-04-24 14:44:46 -0700911
912 /* Remove from active list. Only rx fifos are tracked */
913 if (f->flags & SVM_FIFO_F_LL_TRACKED)
914 {
Florin Coras1f952d32020-12-09 19:43:21 -0800915 pfss_fifo_del_active_list (pfss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700916 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
917 }
918
Florin Corasf22f4e52019-12-19 16:10:58 -0800919 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700920 svm_fifo_free_ooo_data (f);
921
Florin Coras88001c62019-04-24 14:44:46 -0700922 if (CLIB_DEBUG)
923 {
Florin Corasc547e912020-12-08 17:50:45 -0800924 sf->master_session_index = ~0;
Florin Coras88001c62019-04-24 14:44:46 -0700925 f->master_thread_index = ~0;
926 }
927
Florin Corasc547e912020-12-08 17:50:45 -0800928 f->ooo_enq = f->ooo_deq = 0;
Florin Coras06d47752020-03-06 02:23:58 +0000929 f->prev = 0;
Florin Corasc547e912020-12-08 17:50:45 -0800930
931 fs_fifo_free (fs, f);
Florin Coras06d47752020-03-06 02:23:58 +0000932
Florin Coras62ddc032019-12-08 18:30:42 -0800933 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700934}
935
Florin Coras6d7552c2020-04-09 01:49:45 +0000936void
Florin Coras0bc78d82021-01-09 14:34:01 -0800937fifo_segment_detach_fifo (fifo_segment_t *fs, svm_fifo_t **f)
Florin Coras6d7552c2020-04-09 01:49:45 +0000938{
Florin Coras1f952d32020-12-09 19:43:21 -0800939 fifo_slice_private_t *pfss;
Florin Coras6d7552c2020-04-09 01:49:45 +0000940 fifo_segment_slice_t *fss;
Florin Coras0bc78d82021-01-09 14:34:01 -0800941 u32 fl_index, slice_index;
942 svm_fifo_chunk_t **c;
943 svm_fifo_t *of = *f;
Florin Coras6d7552c2020-04-09 01:49:45 +0000944
Florin Coras0bc78d82021-01-09 14:34:01 -0800945 slice_index = of->master_thread_index;
946 fss = fsh_slice_get (fs->h, slice_index);
947 pfss = fs_slice_private_get (fs, slice_index);
948 fss->virtual_mem -= svm_fifo_size (of);
949 if (of->flags & SVM_FIFO_F_LL_TRACKED)
950 pfss_fifo_del_active_list (pfss, of);
Florin Coras6d7552c2020-04-09 01:49:45 +0000951
Florin Coras0bc78d82021-01-09 14:34:01 -0800952 /* Update slice counts for chunks that were detached */
953 vec_foreach (c, of->chunks_at_attach)
Florin Coras6d7552c2020-04-09 01:49:45 +0000954 {
Florin Coras0bc78d82021-01-09 14:34:01 -0800955 fl_index = fs_freelist_for_size ((*c)->length);
Florin Coras6d7552c2020-04-09 01:49:45 +0000956 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
Florin Coras6d7552c2020-04-09 01:49:45 +0000957 }
Florin Coras0bc78d82021-01-09 14:34:01 -0800958 vec_free (of->chunks_at_attach);
959
960 clib_mem_bulk_free (pfss->fifos, *f);
961 *f = 0;
Florin Coras6d7552c2020-04-09 01:49:45 +0000962}
963
964void
Florin Coras0bc78d82021-01-09 14:34:01 -0800965fifo_segment_attach_fifo (fifo_segment_t *fs, svm_fifo_t **f, u32 slice_index)
Florin Coras6d7552c2020-04-09 01:49:45 +0000966{
Florin Coras1f952d32020-12-09 19:43:21 -0800967 fifo_slice_private_t *pfss;
Florin Coras6d7552c2020-04-09 01:49:45 +0000968 fifo_segment_slice_t *fss;
969 svm_fifo_chunk_t *c;
Florin Coras0bc78d82021-01-09 14:34:01 -0800970 svm_fifo_t *nf, *of;
Florin Coras6d7552c2020-04-09 01:49:45 +0000971 u32 fl_index;
972
Florin Coras0bc78d82021-01-09 14:34:01 -0800973 nf = fs_fifo_alloc (fs, slice_index);
974 clib_memcpy_fast (nf, *f, sizeof (*nf));
Florin Coras6d7552c2020-04-09 01:49:45 +0000975
Florin Coras0bc78d82021-01-09 14:34:01 -0800976 fss = fsh_slice_get (fs->h, slice_index);
977 pfss = fs_slice_private_get (fs, slice_index);
978 fss->virtual_mem += svm_fifo_size (nf);
979 if (nf->flags & SVM_FIFO_F_LL_TRACKED)
980 pfss_fifo_add_active_list (pfss, nf);
981
982 /* Update allocated chunks for fifo segment and build list
983 * of chunks to be freed at detach */
984 of = *f;
985 of->chunks_at_attach = 0;
986
987 c = fs_chunk_ptr (fs->h, nf->shr->start_chunk);
Florin Coras6d7552c2020-04-09 01:49:45 +0000988 while (c)
989 {
990 fl_index = fs_freelist_for_size (c->length);
991 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
Florin Coras0bc78d82021-01-09 14:34:01 -0800992 vec_add1 (of->chunks_at_attach, c);
Florin Corasaf588822020-12-09 12:51:13 -0800993 c = fs_chunk_ptr (fs->h, c->next);
Florin Coras6d7552c2020-04-09 01:49:45 +0000994 }
Florin Coras0bc78d82021-01-09 14:34:01 -0800995
996 nf->shr->slice_index = slice_index;
997 *f = nf;
Florin Coras6d7552c2020-04-09 01:49:45 +0000998}
999
Florin Coras14f066e2020-12-10 18:52:40 -08001000uword
1001fifo_segment_fifo_offset (svm_fifo_t *f)
1002{
1003 return (u8 *) f->shr - (u8 *) f->fs_hdr;
1004}
1005
Florin Corasb4624182020-12-11 13:58:12 -08001006svm_msg_q_t *
1007fifo_segment_msg_q_alloc (fifo_segment_t *fs, u32 mq_index,
1008 svm_msg_q_cfg_t *cfg)
1009{
1010 fifo_segment_header_t *fsh = fs->h;
1011 svm_msg_q_shared_t *smq;
1012 svm_msg_q_t *mq;
1013 void *base;
1014 u32 size;
1015
1016 if (!fs->mqs)
1017 {
1018 u32 n_mqs = clib_max (fs->h->n_mqs, 1);
1019 vec_validate (fs->mqs, n_mqs - 1);
1020 }
1021
1022 size = svm_msg_q_size_to_alloc (cfg);
1023 base = fsh_alloc_aligned (fsh, size, 8);
1024 fsh->n_reserved_bytes += size;
1025
1026 smq = svm_msg_q_init (base, cfg);
1027 mq = vec_elt_at_index (fs->mqs, mq_index);
1028 svm_msg_q_attach (mq, smq);
1029
1030 return mq;
1031}
1032
1033svm_msg_q_t *
1034fifo_segment_msg_q_attach (fifo_segment_t *fs, uword offset, u32 mq_index)
1035{
1036 svm_msg_q_t *mq;
1037
1038 if (!fs->mqs)
1039 {
1040 u32 n_mqs = clib_max (fs->h->n_mqs, 1);
1041 vec_validate (fs->mqs, n_mqs - 1);
1042 }
1043
1044 mq = vec_elt_at_index (fs->mqs, mq_index);
1045
Florin Coras86f12322021-01-22 15:05:14 -08001046 if (!mq->q.shr)
Florin Corasb4624182020-12-11 13:58:12 -08001047 {
1048 svm_msg_q_shared_t *smq;
1049 smq = (svm_msg_q_shared_t *) ((u8 *) fs->h + offset);
1050 svm_msg_q_attach (mq, smq);
1051 }
1052
1053 ASSERT (fifo_segment_msg_q_offset (fs, mq_index) == offset);
1054
1055 return mq;
1056}
1057
Florin Coras80b74252021-01-27 18:08:25 -08001058void
1059fifo_segment_msg_qs_discover (fifo_segment_t *fs, int *fds, u32 n_fds)
1060{
1061 svm_msg_q_shared_t *smq;
1062 u32 n_mqs, size, i;
1063 uword offset = 0, n_alloced;
1064 svm_msg_q_t *mq;
1065
1066 n_mqs = fs->h->n_mqs;
1067 if (n_fds && n_mqs != n_fds)
1068 {
1069 clib_warning ("expected %u fds got %u", n_mqs, n_fds);
1070 return;
1071 }
1072
1073 vec_validate (fs->mqs, n_mqs - 1);
1074 n_alloced = fs->h->n_reserved_bytes - fs->h->start_byte_index;
1075 ASSERT (n_alloced % n_mqs == 0);
1076 size = n_alloced / n_mqs;
1077
1078 offset = fs->h->start_byte_index;
1079 for (i = 0; i < n_mqs; i++)
1080 {
1081 mq = vec_elt_at_index (fs->mqs, i);
1082 smq = (svm_msg_q_shared_t *) ((u8 *) fs->h + offset);
1083 svm_msg_q_attach (mq, smq);
1084 if (n_fds)
1085 svm_msg_q_set_eventfd (mq, fds[i]);
1086 offset += size;
1087 }
1088}
1089
Florin Corasb4624182020-12-11 13:58:12 -08001090uword
1091fifo_segment_msg_q_offset (fifo_segment_t *fs, u32 mq_index)
1092{
1093 svm_msg_q_t *mq = vec_elt_at_index (fs->mqs, mq_index);
1094
Florin Coras86f12322021-01-22 15:05:14 -08001095 if (mq->q.shr == 0)
Florin Corasb4624182020-12-11 13:58:12 -08001096 return ~0ULL;
1097
Florin Coras86f12322021-01-22 15:05:14 -08001098 return (uword) ((u8 *) mq->q.shr - (u8 *) fs->h) -
1099 sizeof (svm_msg_q_shared_t);
Florin Corasb4624182020-12-11 13:58:12 -08001100}
1101
Florin Corasf9d4ab42019-05-11 16:55:53 -07001102int
Florin Coras62ddc032019-12-08 18:30:42 -08001103fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
1104 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001105{
1106 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -08001107 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001108
Florin Coras62ddc032019-12-08 18:30:42 -08001109 fss = fsh_slice_get (fsh, slice_index);
Florin Corasfe1ab7e2020-11-26 11:34:38 -08001110 return fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001111}
1112
1113int
Florin Coras62ddc032019-12-08 18:30:42 -08001114fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
1115 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001116{
Florin Coras62ddc032019-12-08 18:30:42 -08001117 fifo_segment_header_t *fsh = fs->h;
1118 fifo_segment_slice_t *fss;
Florin Coras473c8462020-11-16 18:11:51 -08001119 u32 fl_index;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001120
Florin Coras62ddc032019-12-08 18:30:42 -08001121 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -07001122 {
1123 clib_warning ("chunk size out of range %d", chunk_size);
1124 return -1;
1125 }
1126
1127 fl_index = fs_freelist_for_size (chunk_size);
Florin Coras62ddc032019-12-08 18:30:42 -08001128 fss = fsh_slice_get (fsh, slice_index);
1129
Florin Coras473c8462020-11-16 18:11:51 -08001130 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001131}
1132
Florin Coras88001c62019-04-24 14:44:46 -07001133/**
1134 * Pre-allocates fifo pairs in fifo segment
1135 */
1136void
1137fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
1138 u32 rx_fifo_size, u32 tx_fifo_size,
1139 u32 * n_fifo_pairs)
1140{
Florin Corasf9d4ab42019-05-11 16:55:53 -07001141 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +00001142 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -08001143 fifo_segment_header_t *fsh = fs->h;
1144 int rx_fl_index, tx_fl_index, i;
1145 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -07001146 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -07001147
1148 /* Parameter check */
1149 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
1150 return;
1151
Florin Coras62ddc032019-12-08 18:30:42 -08001152 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001153 {
1154 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
1155 return;
1156 }
1157
Florin Coras62ddc032019-12-08 18:30:42 -08001158 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -07001159 {
1160 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
1161 return;
1162 }
1163
1164 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001165 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001166 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001167 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -07001168
Florin Corasf9d4ab42019-05-11 16:55:53 -07001169 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -07001170
Florin Coras88001c62019-04-24 14:44:46 -07001171 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -07001172 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras213b1bb2020-12-07 14:33:58 -08001173 space_available = fsh_n_free_bytes (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -07001174 pairs_to_alloc = space_available / pair_size;
1175 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -08001176 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +00001177 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -07001178
Florin Coras62ddc032019-12-08 18:30:42 -08001179 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -07001180 return;
Florin Coras88001c62019-04-24 14:44:46 -07001181
Florin Coras62ddc032019-12-08 18:30:42 -08001182 for (i = 0; i < fs->n_slices; i++)
1183 {
Florin Coras8d0149d2020-01-02 19:22:51 +00001184 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
Benoît Gannef23b1512020-11-25 15:06:01 +01001185 if (0 == alloc_now)
1186 break;
1187
1188 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +00001189 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
1190 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
1191 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
1192 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -07001193
Florin Coras8d0149d2020-01-02 19:22:51 +00001194 /* Account for the pairs allocated */
1195 *n_fifo_pairs -= alloc_now;
1196 }
Florin Coras88001c62019-04-24 14:44:46 -07001197}
1198
1199/**
1200 * Get number of active fifos
1201 */
1202u32
1203fifo_segment_num_fifos (fifo_segment_t * fs)
1204{
Florin Coras7a90e502020-04-09 04:46:14 +00001205 return fsh_n_active_fifos (fs->h);
Florin Coras88001c62019-04-24 14:44:46 -07001206}
1207
Florin Coras62ddc032019-12-08 18:30:42 -08001208static u32
Florin Coras17672aa2020-12-29 16:55:32 -08001209fs_slice_num_free_fifos (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss)
Florin Coras88001c62019-04-24 14:44:46 -07001210{
Florin Corasc547e912020-12-08 17:50:45 -08001211 svm_fifo_shared_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -07001212 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -07001213
Florin Coras17672aa2020-12-29 16:55:32 -08001214 f = fs_ptr (fsh, fss->free_fifos);
Florin Coras88001c62019-04-24 14:44:46 -07001215 if (f == 0)
1216 return 0;
1217
1218 while (f)
1219 {
Florin Coras17672aa2020-12-29 16:55:32 -08001220 f = fs_ptr (fsh, f->next);
Florin Coras88001c62019-04-24 14:44:46 -07001221 count++;
1222 }
1223 return count;
1224}
1225
Florin Corasb095a3c2019-04-25 12:58:46 -07001226u32
Florin Coras62ddc032019-12-08 18:30:42 -08001227fifo_segment_num_free_fifos (fifo_segment_t * fs)
1228{
1229 fifo_segment_header_t *fsh = fs->h;
1230 fifo_segment_slice_t *fss;
1231 int slice_index;
1232 u32 count = 0;
1233
1234 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1235 {
1236 fss = fsh_slice_get (fsh, slice_index);
Florin Coras17672aa2020-12-29 16:55:32 -08001237 count += fs_slice_num_free_fifos (fsh, fss);
Florin Coras62ddc032019-12-08 18:30:42 -08001238 }
1239 return count;
1240}
1241
1242static u32
Florin Corasaf588822020-12-09 12:51:13 -08001243fs_slice_num_free_chunks (fifo_segment_header_t *fsh,
1244 fifo_segment_slice_t *fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001245{
1246 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001247 svm_fifo_chunk_t *c;
1248 int i;
1249
Florin Corasb095a3c2019-04-25 12:58:46 -07001250 /* Count all free chunks? */
1251 if (size == ~0)
1252 {
Florin Coras4b8011e2020-12-07 19:38:23 -08001253 for (i = 0; i < FS_CHUNK_VEC_LEN; i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001254 {
Florin Corasaf588822020-12-09 12:51:13 -08001255 c = fs_chunk_ptr (fsh, fss->free_chunks[i]);
Florin Corasb095a3c2019-04-25 12:58:46 -07001256 if (c == 0)
1257 continue;
1258
1259 while (c)
1260 {
Florin Corasaf588822020-12-09 12:51:13 -08001261 c = fs_chunk_ptr (fsh, c->next);
Florin Corasb095a3c2019-04-25 12:58:46 -07001262 count++;
1263 }
1264 }
1265 return count;
1266 }
1267
1268 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001269 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001270
Florin Coras4b8011e2020-12-07 19:38:23 -08001271 if (fl_index >= FS_CHUNK_VEC_LEN)
Florin Corasb095a3c2019-04-25 12:58:46 -07001272 return 0;
1273
Florin Corasaf588822020-12-09 12:51:13 -08001274 c = fs_chunk_ptr (fsh, fss->free_chunks[fl_index]);
Florin Corasb095a3c2019-04-25 12:58:46 -07001275 if (c == 0)
1276 return 0;
1277
1278 while (c)
1279 {
Florin Corasaf588822020-12-09 12:51:13 -08001280 c = fs_chunk_ptr (fsh, c->next);
Florin Corasb095a3c2019-04-25 12:58:46 -07001281 count++;
1282 }
1283 return count;
1284}
1285
Florin Coras62ddc032019-12-08 18:30:42 -08001286u32
1287fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1288{
1289 fifo_segment_header_t *fsh = fs->h;
1290 fifo_segment_slice_t *fss;
1291 int slice_index;
1292 u32 count = 0;
1293
1294 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1295 {
1296 fss = fsh_slice_get (fsh, slice_index);
Florin Corasaf588822020-12-09 12:51:13 -08001297 count += fs_slice_num_free_chunks (fsh, fss, size);
Florin Coras62ddc032019-12-08 18:30:42 -08001298 }
1299 return count;
1300}
1301
Florin Corasef4f3e72019-12-11 14:27:53 -08001302uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001303fifo_segment_size (fifo_segment_t * fs)
1304{
Florin Coras213b1bb2020-12-07 14:33:58 -08001305 return fs->h->max_byte_index - fs->h->n_reserved_bytes;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001306}
1307
1308u8
1309fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1310{
1311 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1312}
1313
1314void
1315fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1316{
1317 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1318}
1319
Florin Coras213b1bb2020-12-07 14:33:58 -08001320void *
1321fifo_segment_alloc (fifo_segment_t *fs, uword size)
1322{
1323 void *rv = fsh_alloc (fs->h, size);
1324 /* Mark externally allocated bytes as reserved. This helps
1325 * @ref fifo_segment_size report bytes used only for fifos */
1326 fs->h->n_reserved_bytes += size;
1327 return rv;
1328}
1329
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001330uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001331fifo_segment_free_bytes (fifo_segment_t * fs)
1332{
Florin Coras62ddc032019-12-08 18:30:42 -08001333 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001334}
1335
Florin Coras62ddc032019-12-08 18:30:42 -08001336uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001337fifo_segment_cached_bytes (fifo_segment_t * fs)
1338{
1339 return fsh_n_cached_bytes (fs->h);
1340}
1341
1342uword
Florin Coras75ccf7b2020-03-05 19:44:02 +00001343fifo_segment_available_bytes (fifo_segment_t * fs)
1344{
1345 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1346}
1347
1348uword
Florin Coras1c91c772019-06-25 18:14:13 -07001349fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001350{
Florin Coras62ddc032019-12-08 18:30:42 -08001351 fifo_segment_header_t *fsh = fs->h;
1352 fifo_segment_slice_t *fss;
1353 uword n_bytes = 0;
1354 int slice_index;
1355
1356 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1357 {
1358 fss = fsh_slice_get (fsh, slice_index);
Florin Corasd01d2ce2020-11-17 18:42:38 -08001359 n_bytes += fss_fl_chunk_bytes (fss);
Florin Coras62ddc032019-12-08 18:30:42 -08001360 }
1361
1362 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001363}
1364
Florin Coras88001c62019-04-24 14:44:46 -07001365u8
1366fifo_segment_has_fifos (fifo_segment_t * fs)
1367{
Florin Coras7a90e502020-04-09 04:46:14 +00001368 return (fsh_n_active_fifos (fs->h) != 0);
Florin Coras88001c62019-04-24 14:44:46 -07001369}
1370
1371svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001372fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001373{
Florin Coras1f952d32020-12-09 19:43:21 -08001374 fifo_slice_private_t *pfss;
Florin Coras62ddc032019-12-08 18:30:42 -08001375
Florin Coras1f952d32020-12-09 19:43:21 -08001376 pfss = fs_slice_private_get (fs, slice_index);
1377 return pfss->active_fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001378}
1379
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001380u8
1381fifo_segment_get_mem_usage (fifo_segment_t * fs)
1382{
1383 uword size, in_use;
1384
1385 size = fifo_segment_size (fs);
1386 in_use =
1387 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1388 return (in_use * 100) / size;
1389}
1390
1391fifo_segment_mem_status_t
1392fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1393{
1394 if (!fsh->high_watermark || !fsh->low_watermark)
1395 return MEMORY_PRESSURE_NO_PRESSURE;
1396
1397 /* once the no-memory is detected, the status continues
1398 * until memory usage gets below the high watermark
1399 */
1400 if (fsh_has_reached_mem_limit (fsh))
1401 {
1402 if (usage >= fsh->high_watermark)
1403 return MEMORY_PRESSURE_NO_MEMORY;
1404 else
1405 fsh_reset_mem_limit (fsh);
1406 }
1407
1408 if (usage >= fsh->high_watermark)
1409 return MEMORY_PRESSURE_HIGH_PRESSURE;
1410
1411 else if (usage >= fsh->low_watermark)
1412 return MEMORY_PRESSURE_LOW_PRESSURE;
1413
1414 return MEMORY_PRESSURE_NO_PRESSURE;
1415}
1416
1417fifo_segment_mem_status_t
1418fifo_segment_get_mem_status (fifo_segment_t * fs)
1419{
1420 fifo_segment_header_t *fsh = fs->h;
1421 u8 usage = fifo_segment_get_mem_usage (fs);
1422
1423 return fifo_segment_determine_status (fsh, usage);
1424}
1425
Florin Coras88001c62019-04-24 14:44:46 -07001426u8 *
1427format_fifo_segment_type (u8 * s, va_list * args)
1428{
1429 fifo_segment_t *sp;
1430 sp = va_arg (*args, fifo_segment_t *);
1431 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1432
1433 if (st == SSVM_SEGMENT_PRIVATE)
Florin Coras2a7c0b62020-09-28 23:40:28 -07001434 s = format (s, "%s", "private");
Florin Coras88001c62019-04-24 14:44:46 -07001435 else if (st == SSVM_SEGMENT_MEMFD)
1436 s = format (s, "%s", "memfd");
1437 else if (st == SSVM_SEGMENT_SHM)
1438 s = format (s, "%s", "shm");
1439 else
1440 s = format (s, "%s", "unknown");
1441 return s;
1442}
1443
1444/**
1445 * Segment format function
1446 */
1447u8 *
1448format_fifo_segment (u8 * s, va_list * args)
1449{
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001450 u32 count, indent, active_fifos, free_fifos;
Florin Coras5368bb02019-06-09 09:24:33 -07001451 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001452 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001453 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1454 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001455 uword tracked_cached_bytes;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001456 uword fifo_hdr = 0, reserved;
Florin Coras5368bb02019-06-09 09:24:33 -07001457 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001458 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001459 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001460 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001461 char *address;
1462 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001463 int i;
Florin Coras06d47752020-03-06 02:23:58 +00001464 uword allocated, in_use, virt;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001465 f64 usage;
1466 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001467
1468 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001469
Florin Coras5368bb02019-06-09 09:24:33 -07001470 if (fs == 0)
1471 {
Florin Coras2a7c0b62020-09-28 23:40:28 -07001472 s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
Florin Coras5368bb02019-06-09 09:24:33 -07001473 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1474 return s;
1475 }
1476
Florin Coras5368bb02019-06-09 09:24:33 -07001477 fifo_segment_info (fs, &address, &size);
1478 active_fifos = fifo_segment_num_fifos (fs);
1479 free_fifos = fifo_segment_num_free_fifos (fs);
1480
Florin Coras2a7c0b62020-09-28 23:40:28 -07001481 s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
Florin Coras5368bb02019-06-09 09:24:33 -07001482 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1483 free_fifos, address);
1484
1485 if (!verbose)
1486 return s;
1487
Florin Coras62ddc032019-12-08 18:30:42 -08001488 fsh = fs->h;
1489
1490 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1491 if (free_chunks)
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001492 s =
1493 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1494 indent + 2);
Florin Corasf8461bf2019-11-07 17:00:15 -08001495 else
1496 s = format (s, "\n");
1497
Florin Coras62ddc032019-12-08 18:30:42 -08001498 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001499 {
Florin Coras62ddc032019-12-08 18:30:42 -08001500 fss = fsh_slice_get (fsh, slice_index);
Florin Coras4b8011e2020-12-07 19:38:23 -08001501 for (i = 0; i < FS_CHUNK_VEC_LEN; i++)
Florin Coras88001c62019-04-24 14:44:46 -07001502 {
Florin Corasaf588822020-12-09 12:51:13 -08001503 c = fs_chunk_ptr (fsh, fss->free_chunks[i]);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001504 if (c == 0 && fss->num_chunks[i] == 0)
Florin Coras62ddc032019-12-08 18:30:42 -08001505 continue;
1506 count = 0;
1507 while (c)
1508 {
Florin Corasaf588822020-12-09 12:51:13 -08001509 c = fs_chunk_ptr (fsh, c->next);
Florin Coras62ddc032019-12-08 18:30:42 -08001510 count++;
1511 }
1512
1513 chunk_size = fs_freelist_index_to_size (i);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001514 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1515 chunk_size >> 10, count, fss->num_chunks[i]);
Florin Coras62ddc032019-12-08 18:30:42 -08001516
1517 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001518 }
Florin Coras88001c62019-04-24 14:44:46 -07001519 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001520
1521 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1522 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001523 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001524 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001525 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1526 allocated = fifo_segment_size (fs);
1527 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1528 usage = (100.0 * in_use) / allocated;
1529 mem_st = fifo_segment_get_mem_status (fs);
Florin Coras06d47752020-03-06 02:23:58 +00001530 virt = fsh_virtual_mem (fsh);
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001531 reserved = fsh->n_reserved_bytes;
Florin Corasf8461bf2019-11-07 17:00:15 -08001532
Florin Coras06d47752020-03-06 02:23:58 +00001533 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1534 " %U (%lu)\n", format_white_space, indent + 2,
1535 format_memory_size, free_seg_bytes, free_seg_bytes,
1536 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001537 format_memory_size, reserved, reserved);
Florin Coras06d47752020-03-06 02:23:58 +00001538 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1539 " %U (%lu)\n", format_white_space, indent + 2,
1540 format_memory_size, chunk_bytes, chunk_bytes,
1541 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1542 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1543 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1544 format_white_space, indent + 2, fsh->n_active_fifos,
1545 format_memory_size, fifo_hdr, fifo_hdr);
1546 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1547 format_white_space, indent + 2, usage, format_memory_size,
1548 in_use, format_memory_size, allocated, format_memory_size, virt,
1549 fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001550 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001551
Florin Coras88001c62019-04-24 14:44:46 -07001552 return s;
1553}
1554
1555/*
1556 * fd.io coding-style-patch-verification: ON
1557 *
1558 * Local Variables:
1559 * eval: (c-set-style "gnu")
1560 * End:
1561 */