blob: e25967ee81a14997b8c59989aa79915b5a603a19 [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>
17
Florin Coras06d47752020-03-06 02:23:58 +000018static inline fifo_segment_slice_t *
19fsh_slice_get (fifo_segment_header_t * fsh, u32 slice_index)
20{
21 return &fsh->slices[slice_index];
22}
23
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000024static char *fifo_segment_mem_status_strings[] = {
25#define _(sym,str) str,
26 foreach_segment_mem_status
27#undef _
28};
29
Florin Coras88001c62019-04-24 14:44:46 -070030/**
Florin Corasf9d4ab42019-05-11 16:55:53 -070031 * Fifo segment free space
32 *
33 * Queries the underlying memory manager, dlmalloc, for free space. Since this
34 * ends up walking the internal data structures, it should not be called
35 * indiscriminately.
36 *
37 * @param fs fifo segment
38 * @return number of free bytes
39 */
Florin Corasef4f3e72019-12-11 14:27:53 -080040static uword
Florin Coras4453c092019-12-19 10:13:15 -080041fsh_free_space (fifo_segment_header_t * fsh)
Florin Corasf9d4ab42019-05-11 16:55:53 -070042{
Damjan Marion4537c302020-09-28 19:03:37 +020043 return clib_mem_get_heap_free_space (fsh->ssvm_sh->heap);
Florin Corasf9d4ab42019-05-11 16:55:53 -070044}
45
Florin Coras62ddc032019-12-08 18:30:42 -080046static inline void
47fsh_free_bytes_sub (fifo_segment_header_t * fsh, int size)
48{
49 clib_atomic_fetch_sub_rel (&fsh->n_free_bytes, size);
50}
51
52static inline uword
53fsh_n_free_bytes (fifo_segment_header_t * fsh)
54{
Florin Coras8122cc22019-12-18 13:06:41 -080055 uword n_free = clib_atomic_load_relax_n (&fsh->n_free_bytes);
56 return n_free > fsh->n_reserved_bytes ? n_free - fsh->n_reserved_bytes : 0;
57}
58
59static inline void
Florin Coras4453c092019-12-19 10:13:15 -080060fsh_update_free_bytes (fifo_segment_header_t * fsh)
Florin Coras8122cc22019-12-18 13:06:41 -080061{
Florin Coras4453c092019-12-19 10:13:15 -080062 clib_atomic_store_rel_n (&fsh->n_free_bytes, fsh_free_space (fsh));
Florin Coras8122cc22019-12-18 13:06:41 -080063}
64
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000065static inline void
Florin Coras79b3f8c2020-11-20 07:34:53 -080066fsh_cached_bytes_add (fifo_segment_header_t * fsh, uword size)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000067{
68 clib_atomic_fetch_add_rel (&fsh->n_cached_bytes, size);
69}
70
71static inline void
Florin Coras79b3f8c2020-11-20 07:34:53 -080072fsh_cached_bytes_sub (fifo_segment_header_t * fsh, uword size)
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000073{
74 clib_atomic_fetch_sub_rel (&fsh->n_cached_bytes, size);
75}
76
77static inline uword
78fsh_n_cached_bytes (fifo_segment_header_t * fsh)
79{
80 uword n_cached = clib_atomic_load_relax_n (&fsh->n_cached_bytes);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000081 return n_cached;
82}
83
Florin Coras06d47752020-03-06 02:23:58 +000084static inline void
85fsh_active_fifos_update (fifo_segment_header_t * fsh, int inc)
86{
87 clib_atomic_fetch_add_rel (&fsh->n_active_fifos, inc);
88}
89
Florin Coras7a90e502020-04-09 04:46:14 +000090static inline u32
91fsh_n_active_fifos (fifo_segment_header_t * fsh)
92{
93 return clib_atomic_load_relax_n (&fsh->n_active_fifos);
94}
95
Florin Coras06d47752020-03-06 02:23:58 +000096static inline uword
97fsh_virtual_mem (fifo_segment_header_t * fsh)
98{
99 fifo_segment_slice_t *fss;
100 uword total_vm = 0;
101 int i;
102
103 for (i = 0; i < fsh->n_slices; i++)
104 {
105 fss = fsh_slice_get (fsh, i);
106 total_vm += clib_atomic_load_relax_n (&fss->virtual_mem);
107 }
108 return total_vm;
109}
110
111void
112fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
113 int n_bytes)
114{
115 fifo_segment_slice_t *fss = fsh_slice_get (fsh, slice_index);
116 fss->virtual_mem += n_bytes;
117}
118
Florin Coras8122cc22019-12-18 13:06:41 -0800119static void
120fsh_check_mem (fifo_segment_header_t * fsh)
121{
122 uword thresh;
123
124 if (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT)
125 return;
126
127 thresh = clib_max (0.01 * fsh->ssvm_sh->ssvm_size,
128 2 * fsh->n_reserved_bytes);
129 if (fsh->n_free_bytes > thresh)
130 return;
131
132 fsh->flags |= FIFO_SEGMENT_F_MEM_LIMIT;
Florin Coras4453c092019-12-19 10:13:15 -0800133 fsh_update_free_bytes (fsh);
Florin Coras62ddc032019-12-08 18:30:42 -0800134}
135
Florin Coras473c8462020-11-16 18:11:51 -0800136static inline int
137fss_chunk_fl_index_is_valid (fifo_segment_slice_t * fss, u32 fl_index)
138{
139 return (fl_index < vec_len (fss->free_chunks));
140}
141
142static void
143fss_chunk_free_list_push (fifo_segment_slice_t * fss, u32 fl_index,
144 svm_fifo_chunk_t * c)
145{
Florin Corasd01d2ce2020-11-17 18:42:38 -0800146 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras473c8462020-11-16 18:11:51 -0800147 c->next = fss->free_chunks[fl_index];
148 fss->free_chunks[fl_index] = c;
Florin Corasd01d2ce2020-11-17 18:42:38 -0800149 clib_spinlock_unlock (&fss->chunk_lock);
Florin Coras473c8462020-11-16 18:11:51 -0800150}
151
152static void
153fss_chunk_free_list_push_list (fifo_segment_slice_t * fss, u32 fl_index,
154 svm_fifo_chunk_t * head,
155 svm_fifo_chunk_t * tail)
156{
Florin Corasd01d2ce2020-11-17 18:42:38 -0800157 clib_spinlock_lock (&fss->chunk_lock);
Florin Coras473c8462020-11-16 18:11:51 -0800158 tail->next = fss->free_chunks[fl_index];
159 fss->free_chunks[fl_index] = head;
Florin Corasd01d2ce2020-11-17 18:42:38 -0800160 clib_spinlock_unlock (&fss->chunk_lock);
Florin Coras473c8462020-11-16 18:11:51 -0800161}
162
163static svm_fifo_chunk_t *
164fss_chunk_free_list_pop (fifo_segment_slice_t * fss, u32 fl_index)
165{
166 svm_fifo_chunk_t *c;
167
168 ASSERT (fss_chunk_fl_index_is_valid (fss, fl_index));
169
Florin Corasd01d2ce2020-11-17 18:42:38 -0800170 clib_spinlock_lock (&fss->chunk_lock);
171
Florin Coras473c8462020-11-16 18:11:51 -0800172 if (!fss->free_chunks[fl_index])
Florin Corasd01d2ce2020-11-17 18:42:38 -0800173 {
174 clib_spinlock_unlock (&fss->chunk_lock);
175 return 0;
176 }
Florin Coras473c8462020-11-16 18:11:51 -0800177
178 c = fss->free_chunks[fl_index];
179 fss->free_chunks[fl_index] = c->next;
180
Florin Corasd01d2ce2020-11-17 18:42:38 -0800181 clib_spinlock_unlock (&fss->chunk_lock);
182
Florin Coras473c8462020-11-16 18:11:51 -0800183 return c;
184}
185
186static inline void
187fss_fifo_add_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
188{
189 if (fss->fifos)
190 {
191 fss->fifos->prev = f;
192 f->next = fss->fifos;
193 }
194 fss->fifos = f;
195}
196
197static inline void
198fss_fifo_del_active_list (fifo_segment_slice_t * fss, svm_fifo_t * f)
199{
200 if (f->flags & SVM_FIFO_F_LL_TRACKED)
201 {
202 if (f->prev)
203 f->prev->next = f->next;
204 else
205 fss->fifos = f->next;
206 if (f->next)
207 f->next->prev = f->prev;
208 }
209}
210
Florin Corasd01d2ce2020-11-17 18:42:38 -0800211static inline uword
212fss_fl_chunk_bytes (fifo_segment_slice_t * fss)
213{
214 return clib_atomic_load_relax_n (&fss->n_fl_chunk_bytes);
215}
216
217static inline void
218fss_fl_chunk_bytes_add (fifo_segment_slice_t * fss, uword size)
219{
220 clib_atomic_fetch_add_relax (&fss->n_fl_chunk_bytes, size);
221}
222
223static inline void
224fss_fl_chunk_bytes_sub (fifo_segment_slice_t * fss, uword size)
225{
226 clib_atomic_fetch_sub_relax (&fss->n_fl_chunk_bytes, size);
227}
228
Florin Corasf9d4ab42019-05-11 16:55:53 -0700229/**
Florin Coras88001c62019-04-24 14:44:46 -0700230 * Initialize fifo segment shared header
231 */
232int
233fifo_segment_init (fifo_segment_t * fs)
234{
235 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800236 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700237 ssvm_shared_header_t *sh;
Florin Coras05c73172020-03-05 20:36:40 +0000238 u32 max_chunk_sz;
Florin Coras62ddc032019-12-08 18:30:42 -0800239 uword max_fifo;
Florin Coras88001c62019-04-24 14:44:46 -0700240 void *oldheap;
Florin Coras62ddc032019-12-08 18:30:42 -0800241 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700242
243 sh = fs->ssvm.sh;
244 oldheap = ssvm_push_heap (sh);
245
Florin Coras62ddc032019-12-08 18:30:42 -0800246 /*
247 * Manually align the fifo segment header to sizeof(uword) = 8 bytes.
248 * Long story made short: the "process-private" fifo segment
249 * is allocated from the main heap, not mmapped. dlmalloc
250 * only guarantees 4-byte alignment, and on aarch64
251 * the fsh can end up 4-byte but not 8-byte aligned.
252 * That eventually causes the atomic op in fifo_segment_update_free_bytes
253 * to backfire.
254 */
255 fsh = clib_mem_alloc_aligned (sizeof (*fsh), sizeof (uword));
Florin Coras88001c62019-04-24 14:44:46 -0700256 clib_memset (fsh, 0, sizeof (*fsh));
257 fs->h = sh->opaque[0] = fsh;
Florin Coras62ddc032019-12-08 18:30:42 -0800258 fs->n_slices = clib_max (fs->n_slices, 1);
259
260 fsh->ssvm_sh = fs->ssvm.sh;
261 fsh->n_slices = fs->n_slices;
Florin Coras4453c092019-12-19 10:13:15 -0800262 max_fifo = clib_min ((fsh_free_space (fsh) - 4096) / 2,
Florin Coras62ddc032019-12-08 18:30:42 -0800263 FIFO_SEGMENT_MAX_FIFO_SIZE);
264 fsh->max_log2_chunk_size = max_log2 (max_fifo);
265
266 fsh->slices = clib_mem_alloc (sizeof (*fss) * fs->n_slices);
267 clib_memset (fsh->slices, 0, sizeof (*fss) * fs->n_slices);
268 max_chunk_sz = fsh->max_log2_chunk_size - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
269
270 for (i = 0; i < fs->n_slices; i++)
271 {
272 fss = fsh_slice_get (fsh, i);
273 vec_validate_init_empty (fss->free_chunks, max_chunk_sz, 0);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000274 vec_validate_init_empty (fss->num_chunks, max_chunk_sz, 0);
Florin Corasf22f4e52019-12-19 16:10:58 -0800275 clib_spinlock_init (&fss->chunk_lock);
Florin Coras62ddc032019-12-08 18:30:42 -0800276 }
Florin Coras88001c62019-04-24 14:44:46 -0700277
278 ssvm_pop_heap (oldheap);
279
Florin Coras4453c092019-12-19 10:13:15 -0800280 fsh->n_free_bytes = fsh_free_space (fsh);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000281 fsh->n_cached_bytes = 0;
Florin Coras05c73172020-03-05 20:36:40 +0000282 fsh->n_reserved_bytes = clib_min (0.01 * fsh->n_free_bytes, 256 << 10);
Florin Coras88001c62019-04-24 14:44:46 -0700283 sh->ready = 1;
284 return (0);
285}
286
287/**
Florin Coras88001c62019-04-24 14:44:46 -0700288 * Create a fifo segment and initialize as master
289 */
290int
291fifo_segment_create (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
292{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700293 fifo_segment_t *fs;
294 uword baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700295 int rv;
296
Florin Coras88001c62019-04-24 14:44:46 -0700297 /* Allocate a fresh segment */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700298 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700299
Florin Corasf9d4ab42019-05-11 16:55:53 -0700300 baseva = a->segment_type == SSVM_SEGMENT_PRIVATE ? ~0ULL : sm->next_baseva;
301 fs->ssvm.ssvm_size = a->segment_size;
Florin Coras5220a262020-09-29 18:11:24 -0700302 fs->ssvm.is_server = 1;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700303 fs->ssvm.my_pid = getpid ();
304 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
305 fs->ssvm.requested_va = baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700306
Florin Coras5220a262020-09-29 18:11:24 -0700307 if ((rv = ssvm_server_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700308 {
Florin Corasf9d4ab42019-05-11 16:55:53 -0700309 pool_put (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700310 return (rv);
311 }
312
313 /* Note: requested_va updated due to seg base addr randomization */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700314 sm->next_baseva = fs->ssvm.sh->ssvm_va + fs->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700315
Florin Corasf9d4ab42019-05-11 16:55:53 -0700316 fifo_segment_init (fs);
317 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700318 return (0);
319}
320
321/**
322 * Attach as slave to a fifo segment
323 */
324int
325fifo_segment_attach (fifo_segment_main_t * sm, fifo_segment_create_args_t * a)
326{
Florin Coras62ddc032019-12-08 18:30:42 -0800327 fifo_segment_t *fs;
Florin Coras88001c62019-04-24 14:44:46 -0700328 int rv;
329
Florin Coras62ddc032019-12-08 18:30:42 -0800330 pool_get_zero (sm->segments, fs);
Florin Coras88001c62019-04-24 14:44:46 -0700331
Florin Coras62ddc032019-12-08 18:30:42 -0800332 fs->ssvm.ssvm_size = a->segment_size;
333 fs->ssvm.my_pid = getpid ();
334 fs->ssvm.name = format (0, "%s%c", a->segment_name, 0);
335 fs->ssvm.requested_va = sm->next_baseva;
Florin Coras88001c62019-04-24 14:44:46 -0700336 if (a->segment_type == SSVM_SEGMENT_MEMFD)
Florin Coras62ddc032019-12-08 18:30:42 -0800337 fs->ssvm.fd = a->memfd_fd;
Florin Coras88001c62019-04-24 14:44:46 -0700338 else
Florin Coras62ddc032019-12-08 18:30:42 -0800339 fs->ssvm.attach_timeout = sm->timeout_in_seconds;
Florin Coras88001c62019-04-24 14:44:46 -0700340
Florin Coras5220a262020-09-29 18:11:24 -0700341 if ((rv = ssvm_client_init (&fs->ssvm, a->segment_type)))
Florin Coras88001c62019-04-24 14:44:46 -0700342 {
Florin Coras62ddc032019-12-08 18:30:42 -0800343 _vec_len (fs) = vec_len (fs) - 1;
Florin Coras88001c62019-04-24 14:44:46 -0700344 return (rv);
345 }
346
347 /* Fish the segment header */
Florin Coras62ddc032019-12-08 18:30:42 -0800348 fs->h = fs->ssvm.sh->opaque[0];
Florin Coras88001c62019-04-24 14:44:46 -0700349
Florin Coras62ddc032019-12-08 18:30:42 -0800350 vec_add1 (a->new_segment_indices, fs - sm->segments);
Florin Coras88001c62019-04-24 14:44:46 -0700351 return (0);
352}
353
354void
355fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * s)
356{
357 ssvm_delete (&s->ssvm);
358 clib_memset (s, 0xfe, sizeof (*s));
359 pool_put (sm->segments, s);
360}
361
362u32
363fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * s)
364{
365 return s - sm->segments;
366}
367
Florin Coras88001c62019-04-24 14:44:46 -0700368fifo_segment_t *
369fifo_segment_get_segment (fifo_segment_main_t * sm, u32 segment_index)
370{
371 return pool_elt_at_index (sm->segments, segment_index);
372}
373
374void
375fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size)
376{
Florin Coras404b8a32019-05-09 12:08:06 -0700377 *address = (char *) seg->ssvm.sh->ssvm_va;
378 *size = seg->ssvm.ssvm_size;
Florin Coras88001c62019-04-24 14:44:46 -0700379}
380
381void
382fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
383 u32 timeout_in_seconds)
384{
385 sm->next_baseva = baseva;
386 sm->timeout_in_seconds = timeout_in_seconds;
387}
388
Florin Corasf9d4ab42019-05-11 16:55:53 -0700389static inline u32
390fs_freelist_for_size (u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700391{
Florin Corasf22f4e52019-12-19 16:10:58 -0800392 if (PREDICT_FALSE (size < FIFO_SEGMENT_MIN_FIFO_SIZE))
393 return 0;
Florin Coras62ddc032019-12-08 18:30:42 -0800394 return max_log2 (size) - FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE;
Florin Coras88001c62019-04-24 14:44:46 -0700395}
396
Florin Corasf9d4ab42019-05-11 16:55:53 -0700397static inline u32
398fs_freelist_index_to_size (u32 fl_index)
399{
Florin Coras62ddc032019-12-08 18:30:42 -0800400 return 1 << (fl_index + FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700401}
402
Florin Coras88001c62019-04-24 14:44:46 -0700403static inline int
Florin Coras62ddc032019-12-08 18:30:42 -0800404fs_chunk_size_is_valid (fifo_segment_header_t * fsh, u32 size)
Florin Coras88001c62019-04-24 14:44:46 -0700405{
406 /*
407 * 4K minimum. It's not likely that anything good will happen
408 * with a smaller FIFO.
409 */
410 return size >= FIFO_SEGMENT_MIN_FIFO_SIZE
Florin Corasd18528f2020-03-27 18:41:54 +0000411 && size <= (1ULL << fsh->max_log2_chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700412}
413
Florin Coras8e755a12020-02-06 16:59:31 +0000414svm_fifo_chunk_t *
415fs_try_alloc_multi_chunk (fifo_segment_header_t * fsh,
416 fifo_segment_slice_t * fss, u32 data_bytes)
417{
418 u32 fl_index, fl_size, n_alloc = 0, req_bytes = data_bytes;
419 svm_fifo_chunk_t *c, *first = 0, *next;
420
421 fl_index = fs_freelist_for_size (req_bytes);
422 if (fl_index > 0)
423 fl_index -= 1;
424
425 fl_size = fs_freelist_index_to_size (fl_index);
426
427 while (req_bytes)
428 {
Florin Coras473c8462020-11-16 18:11:51 -0800429 c = fss_chunk_free_list_pop (fss, fl_index);
Florin Coras8e755a12020-02-06 16:59:31 +0000430 if (c)
431 {
Florin Coras8e755a12020-02-06 16:59:31 +0000432 c->next = first;
433 first = c;
434 n_alloc += fl_size;
435 req_bytes -= clib_min (fl_size, req_bytes);
436 }
437 else
438 {
439 /* Failed to allocate with smaller chunks */
440 if (fl_index == 0)
441 {
Florin Coras473c8462020-11-16 18:11:51 -0800442 /* Free all chunks if any allocated */
Florin Coras8e755a12020-02-06 16:59:31 +0000443 c = first;
444 while (c)
445 {
446 fl_index = fs_freelist_for_size (c->length);
Florin Coras8e755a12020-02-06 16:59:31 +0000447 next = c->next;
Florin Coras473c8462020-11-16 18:11:51 -0800448 fss_chunk_free_list_push (fss, fl_index, c);
Florin Coras8e755a12020-02-06 16:59:31 +0000449 c = next;
450 }
451 n_alloc = 0;
452 first = 0;
Florin Coras473c8462020-11-16 18:11:51 -0800453 /* As last attempt, try allocating a chunk larger than
454 * the requested size, if possible */
455 fl_index = fs_freelist_for_size (data_bytes) + 1;
456 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
457 return 0;
458 first = fss_chunk_free_list_pop (fss, fl_index);
459 if (first)
Florin Coras8e755a12020-02-06 16:59:31 +0000460 {
Florin Coras473c8462020-11-16 18:11:51 -0800461 first->next = 0;
462 n_alloc = fs_freelist_index_to_size (fl_index);
463 goto done;
Florin Coras8e755a12020-02-06 16:59:31 +0000464 }
Florin Coras8e755a12020-02-06 16:59:31 +0000465 return 0;
466 }
467 fl_index -= 1;
468 fl_size = fl_size >> 1;
469 }
470 }
471
Florin Coras473c8462020-11-16 18:11:51 -0800472done:
Florin Corasd01d2ce2020-11-17 18:42:38 -0800473 fss_fl_chunk_bytes_sub (fss, n_alloc);
Florin Coras8e755a12020-02-06 16:59:31 +0000474 fsh_cached_bytes_sub (fsh, n_alloc);
475 return first;
476}
477
Florin Corasf9d4ab42019-05-11 16:55:53 -0700478static int
Florin Coras473c8462020-11-16 18:11:51 -0800479fsh_try_alloc_fifo_hdr_batch (fifo_segment_header_t * fsh,
480 fifo_segment_slice_t * fss, u32 batch_size)
Florin Coras8e755a12020-02-06 16:59:31 +0000481{
Florin Coras88001c62019-04-24 14:44:46 -0700482 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700483 void *oldheap;
Florin Coras36a0c4d2020-01-31 08:28:02 -0800484 uword size;
Florin Corascefd5d82019-05-05 13:19:57 -0700485 u8 *fmem;
486 int i;
Florin Coras88001c62019-04-24 14:44:46 -0700487
Benoît Gannef23b1512020-11-25 15:06:01 +0100488 ASSERT (batch_size != 0);
489
Florin Coras473c8462020-11-16 18:11:51 -0800490 size = (uword) sizeof (*f) * batch_size;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700491
Florin Coras62ddc032019-12-08 18:30:42 -0800492 oldheap = ssvm_push_heap (fsh->ssvm_sh);
Florin Corascefd5d82019-05-05 13:19:57 -0700493 fmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
494 0 /* align_offset */ ,
495 0 /* os_out_of_memory */ );
496 ssvm_pop_heap (oldheap);
Florin Coras88001c62019-04-24 14:44:46 -0700497
498 /* Out of space.. */
Florin Corascefd5d82019-05-05 13:19:57 -0700499 if (fmem == 0)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700500 return -1;
Florin Coras88001c62019-04-24 14:44:46 -0700501
Florin Coras635f5062020-04-28 20:40:57 +0000502 /* Carve fifo hdr space */
Florin Corasf9d4ab42019-05-11 16:55:53 -0700503 for (i = 0; i < batch_size; i++)
Florin Coras88001c62019-04-24 14:44:46 -0700504 {
Florin Corascefd5d82019-05-05 13:19:57 -0700505 f = (svm_fifo_t *) fmem;
506 memset (f, 0, sizeof (*f));
Florin Coras62ddc032019-12-08 18:30:42 -0800507 f->next = fss->free_fifos;
508 fss->free_fifos = f;
Florin Coras635f5062020-04-28 20:40:57 +0000509 fmem += sizeof (*f);
510 }
511
Florin Corasfe1ab7e2020-11-26 11:34:38 -0800512 fsh_free_bytes_sub (fsh, size);
513
Florin Coras473c8462020-11-16 18:11:51 -0800514 return 0;
515}
516
517static int
518fsh_try_alloc_chunk_batch (fifo_segment_header_t * fsh,
519 fifo_segment_slice_t * fss,
520 u32 fl_index, u32 batch_size)
521{
Florin Coras473c8462020-11-16 18:11:51 -0800522 svm_fifo_chunk_t *c, *head = 0, *tail;
Florin Coras79b3f8c2020-11-20 07:34:53 -0800523 uword size, total_chunk_bytes;
524 u32 rounded_data_size;
Florin Coras473c8462020-11-16 18:11:51 -0800525 void *oldheap;
Florin Coras473c8462020-11-16 18:11:51 -0800526 u8 *cmem;
527 int i;
528
Benoît Gannef23b1512020-11-25 15:06:01 +0100529 ASSERT (batch_size != 0);
530
Florin Coras473c8462020-11-16 18:11:51 -0800531 rounded_data_size = fs_freelist_index_to_size (fl_index);
Florin Coras79b3f8c2020-11-20 07:34:53 -0800532 total_chunk_bytes = (uword) batch_size *rounded_data_size;
Florin Coras473c8462020-11-16 18:11:51 -0800533 size = (uword) (sizeof (*c) + rounded_data_size) * batch_size;
534
535 oldheap = ssvm_push_heap (fsh->ssvm_sh);
536 cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
537 0 /* align_offset */ ,
538 0 /* os_out_of_memory */ );
539 ssvm_pop_heap (oldheap);
540
541 /* Out of space.. */
542 if (cmem == 0)
543 return -1;
544
545 /* Carve fifo + chunk space */
546 tail = c = (svm_fifo_chunk_t *) cmem;
Florin Coras635f5062020-04-28 20:40:57 +0000547 for (i = 0; i < batch_size; i++)
548 {
Florin Corascefd5d82019-05-05 13:19:57 -0700549 c->start_byte = 0;
550 c->length = rounded_data_size;
Florin Coras473c8462020-11-16 18:11:51 -0800551 c->next = head;
552 head = c;
553 cmem += sizeof (*c) + rounded_data_size;
554 c = (svm_fifo_chunk_t *) cmem;
Florin Coras88001c62019-04-24 14:44:46 -0700555 }
Florin Corascefd5d82019-05-05 13:19:57 -0700556
Florin Coras473c8462020-11-16 18:11:51 -0800557 fss_chunk_free_list_push_list (fss, fl_index, head, tail);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +0000558 fss->num_chunks[fl_index] += batch_size;
Florin Coras79b3f8c2020-11-20 07:34:53 -0800559 fss_fl_chunk_bytes_add (fss, total_chunk_bytes);
560 fsh_cached_bytes_add (fsh, total_chunk_bytes);
Florin Coras62ddc032019-12-08 18:30:42 -0800561 fsh_free_bytes_sub (fsh, size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700562
563 return 0;
564}
565
Florin Coras473c8462020-11-16 18:11:51 -0800566static int
567fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
568 fifo_segment_slice_t * fss,
569 u32 fl_index, u32 batch_size)
570{
571 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size))
572 return 0;
573 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
574}
575
576static svm_fifo_t *
577fsh_try_alloc_fifo_hdr (fifo_segment_header_t * fsh,
578 fifo_segment_slice_t * fss)
579{
580 svm_fifo_t *f;
581
582 if (!fss->free_fifos)
583 {
584 if (fsh_try_alloc_fifo_hdr_batch (fsh, fss,
585 FIFO_SEGMENT_ALLOC_BATCH_SIZE))
586 return 0;
587 }
588
589 f = fss->free_fifos;
590 fss->free_fifos = f->next;
591 memset (f, 0, sizeof (*f));
592 return f;
593}
594
595static svm_fifo_chunk_t *
596fsh_try_alloc_chunk (fifo_segment_header_t * fsh,
597 fifo_segment_slice_t * fss, u32 data_bytes)
598{
599 svm_fifo_chunk_t *c;
600 u32 fl_index;
601
602 fl_index = fs_freelist_for_size (data_bytes);
603
604free_list:
605 c = fss_chunk_free_list_pop (fss, fl_index);
606 if (c)
607 {
608 c->next = 0;
Florin Corasd01d2ce2020-11-17 18:42:38 -0800609 fss_fl_chunk_bytes_sub (fss, fs_freelist_index_to_size (fl_index));
Florin Coras473c8462020-11-16 18:11:51 -0800610 fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
611 }
612 else
613 {
614 u32 chunk_size, batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
615 uword n_free;
616
617 chunk_size = fs_freelist_index_to_size (fl_index);
618 n_free = fsh_n_free_bytes (fsh);
619
620 if (chunk_size <= n_free)
621 {
622 batch = chunk_size * batch <= n_free ? batch : 1;
623 if (!fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch))
624 goto free_list;
625 }
626 /* Failed to allocate larger chunk, try to allocate multi-chunk
627 * that is close to what was actually requested */
Florin Corasd01d2ce2020-11-17 18:42:38 -0800628 if (data_bytes <= fss_fl_chunk_bytes (fss))
Florin Coras473c8462020-11-16 18:11:51 -0800629 {
630 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
631 if (c)
632 goto done;
633 batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
634 if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
635 {
636 fsh_check_mem (fsh);
637 goto done;
638 }
639 }
Florin Corasd01d2ce2020-11-17 18:42:38 -0800640 if (data_bytes <= fss_fl_chunk_bytes (fss) + n_free)
Florin Coras473c8462020-11-16 18:11:51 -0800641 {
642 u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
643
Florin Corasd01d2ce2020-11-17 18:42:38 -0800644 batch = (data_bytes - fss_fl_chunk_bytes (fss)) / min_size;
Florin Coras473c8462020-11-16 18:11:51 -0800645 batch = clib_min (batch + 1, n_free / min_size);
646 if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
647 {
648 fsh_check_mem (fsh);
649 goto done;
650 }
651 c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
652 }
653 }
654
655done:
656
657 return c;
658}
659
Florin Corasf9d4ab42019-05-11 16:55:53 -0700660/**
661 * Try to allocate new fifo
662 *
663 * Tries the following steps in order:
664 * - grab fifo and chunk from freelists
665 * - batch fifo and chunk allocation
666 * - single fifo allocation
667 * - grab multiple fifo chunks from freelists
668 */
669static svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800670fs_try_alloc_fifo (fifo_segment_header_t * fsh, fifo_segment_slice_t * fss,
671 u32 data_bytes)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700672{
Florin Corasd01d2ce2020-11-17 18:42:38 -0800673 u32 fl_index, min_size;
Florin Coras473c8462020-11-16 18:11:51 -0800674 svm_fifo_chunk_t *c;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700675 svm_fifo_t *f = 0;
676
Florin Coras2de9c0f2020-02-02 19:30:39 +0000677 min_size = clib_max ((fsh->pct_first_alloc * data_bytes) / 100, 4096);
678 fl_index = fs_freelist_for_size (min_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700679
Florin Coras473c8462020-11-16 18:11:51 -0800680 if (!fss_chunk_fl_index_is_valid (fss, fl_index))
Florin Coras0e6199d2020-04-17 20:15:22 +0000681 return 0;
682
Florin Coras473c8462020-11-16 18:11:51 -0800683 f = fsh_try_alloc_fifo_hdr (fsh, fss);
684 if (!f)
Florin Corasd01d2ce2020-11-17 18:42:38 -0800685 return 0;
Florin Coras8122cc22019-12-18 13:06:41 -0800686
Florin Coras473c8462020-11-16 18:11:51 -0800687 c = fsh_try_alloc_chunk (fsh, fss, min_size);
Florin Corasd01d2ce2020-11-17 18:42:38 -0800688 if (!c)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700689 {
Florin Coras473c8462020-11-16 18:11:51 -0800690 f->next = fss->free_fifos;
691 fss->free_fifos = f;
Florin Corasd01d2ce2020-11-17 18:42:38 -0800692 return 0;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700693 }
Florin Corasf9d4ab42019-05-11 16:55:53 -0700694
Florin Corasd01d2ce2020-11-17 18:42:38 -0800695 f->start_chunk = c;
696 while (c->next)
697 c = c->next;
698 f->end_chunk = c;
699 f->size = data_bytes;
700 f->fs_hdr = fsh;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700701
702 return f;
Florin Coras88001c62019-04-24 14:44:46 -0700703}
704
Florin Corasf22f4e52019-12-19 16:10:58 -0800705svm_fifo_chunk_t *
706fsh_alloc_chunk (fifo_segment_header_t * fsh, u32 slice_index, u32 chunk_size)
707{
708 fifo_segment_slice_t *fss;
709 svm_fifo_chunk_t *c;
Florin Corasf22f4e52019-12-19 16:10:58 -0800710
Florin Corasf22f4e52019-12-19 16:10:58 -0800711 fss = fsh_slice_get (fsh, slice_index);
Florin Coras473c8462020-11-16 18:11:51 -0800712 c = fsh_try_alloc_chunk (fsh, fss, chunk_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800713
714 return c;
715}
716
717static void
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000718fsh_slice_collect_chunks (fifo_segment_header_t * fsh,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000719 fifo_segment_slice_t * fss, svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800720{
Florin Coras473c8462020-11-16 18:11:51 -0800721 u32 n_collect = 0, fl_index;
Florin Corasf22f4e52019-12-19 16:10:58 -0800722 svm_fifo_chunk_t *next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800723
Florin Coras9e61d9a2020-02-05 21:13:18 +0000724 while (c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800725 {
Benoît Gannedf601ae2020-10-20 14:31:55 +0200726 CLIB_MEM_UNPOISON (c, sizeof (*c));
Florin Coras9e61d9a2020-02-05 21:13:18 +0000727 next = c->next;
728 fl_index = fs_freelist_for_size (c->length);
Florin Coras473c8462020-11-16 18:11:51 -0800729 fss_chunk_free_list_push (fss, fl_index, c);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000730 n_collect += fs_freelist_index_to_size (fl_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000731 c = next;
Florin Corasf22f4e52019-12-19 16:10:58 -0800732 }
733
Florin Corasd01d2ce2020-11-17 18:42:38 -0800734 fss_fl_chunk_bytes_add (fss, n_collect);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000735 fsh_cached_bytes_add (fsh, n_collect);
Florin Corasf22f4e52019-12-19 16:10:58 -0800736}
737
738void
739fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
Florin Coras9e61d9a2020-02-05 21:13:18 +0000740 svm_fifo_chunk_t * c)
Florin Corasf22f4e52019-12-19 16:10:58 -0800741{
742 fifo_segment_slice_t *fss;
743 fss = fsh_slice_get (fsh, slice_index);
Florin Coras9e61d9a2020-02-05 21:13:18 +0000744 fsh_slice_collect_chunks (fsh, fss, c);
Florin Corasf22f4e52019-12-19 16:10:58 -0800745}
746
Florin Coras88001c62019-04-24 14:44:46 -0700747/**
748 * Allocate fifo in fifo segment
749 */
750svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -0800751fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs, u32 slice_index,
752 u32 data_bytes, fifo_segment_ftype_t ftype)
Florin Coras88001c62019-04-24 14:44:46 -0700753{
Florin Coras62ddc032019-12-08 18:30:42 -0800754 fifo_segment_header_t *fsh = fs->h;
755 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700756 svm_fifo_t *f = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700757
Florin Coras62ddc032019-12-08 18:30:42 -0800758 ASSERT (slice_index < fs->n_slices);
Florin Coras88001c62019-04-24 14:44:46 -0700759
Florin Coras0e6199d2020-04-17 20:15:22 +0000760 if (PREDICT_FALSE (data_bytes > 1 << fsh->max_log2_chunk_size))
761 return 0;
762
Florin Coras62ddc032019-12-08 18:30:42 -0800763 fss = fsh_slice_get (fsh, slice_index);
764 f = fs_try_alloc_fifo (fsh, fss, data_bytes);
Florin Corascefd5d82019-05-05 13:19:57 -0700765 if (!f)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700766 goto done;
Florin Coras88001c62019-04-24 14:44:46 -0700767
Florin Coras62ddc032019-12-08 18:30:42 -0800768 f->slice_index = slice_index;
769
Florin Corascefd5d82019-05-05 13:19:57 -0700770 svm_fifo_init (f, data_bytes);
Florin Coras88001c62019-04-24 14:44:46 -0700771
Florin Coras88001c62019-04-24 14:44:46 -0700772 /* If rx fifo type add to active fifos list. When cleaning up segment,
773 * we need a list of active sessions that should be disconnected. Since
774 * both rx and tx fifos keep pointers to the session, it's enough to track
775 * only one. */
776 if (ftype == FIFO_SEGMENT_RX_FIFO)
777 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000778 fss_fifo_add_active_list (fss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700779 f->flags |= SVM_FIFO_F_LL_TRACKED;
Florin Corasf22f4e52019-12-19 16:10:58 -0800780 }
781
Florin Coras62ddc032019-12-08 18:30:42 -0800782 fsh_active_fifos_update (fsh, 1);
Florin Coras06d47752020-03-06 02:23:58 +0000783 fss->virtual_mem += svm_fifo_size (f);
Florin Coras88001c62019-04-24 14:44:46 -0700784
785done:
Florin Coras88001c62019-04-24 14:44:46 -0700786 return (f);
787}
788
789/**
790 * Free fifo allocated in fifo segment
791 */
792void
Florin Corasb095a3c2019-04-25 12:58:46 -0700793fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f)
Florin Coras88001c62019-04-24 14:44:46 -0700794{
Florin Coras62ddc032019-12-08 18:30:42 -0800795 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800796 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700797
798 ASSERT (f->refcnt > 0);
799
800 if (--f->refcnt > 0)
801 return;
802
Florin Coras62ddc032019-12-08 18:30:42 -0800803 fss = fsh_slice_get (fsh, f->slice_index);
Florin Coras88001c62019-04-24 14:44:46 -0700804
805 /* Remove from active list. Only rx fifos are tracked */
806 if (f->flags & SVM_FIFO_F_LL_TRACKED)
807 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000808 fss_fifo_del_active_list (fss, f);
Florin Coras88001c62019-04-24 14:44:46 -0700809 f->flags &= ~SVM_FIFO_F_LL_TRACKED;
810 }
811
Florin Corascefd5d82019-05-05 13:19:57 -0700812 /* Free fifo chunks */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000813 fsh_slice_collect_chunks (fsh, fss, f->start_chunk);
Florin Corascefd5d82019-05-05 13:19:57 -0700814
Florin Corasf22f4e52019-12-19 16:10:58 -0800815 f->start_chunk = f->end_chunk = 0;
Florin Coras1c91c772019-06-25 18:14:13 -0700816 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = 0;
817
Florin Corasb095a3c2019-04-25 12:58:46 -0700818 /* not allocated on segment heap */
Florin Corasf22f4e52019-12-19 16:10:58 -0800819 svm_fifo_free_chunk_lookup (f);
Florin Corasb095a3c2019-04-25 12:58:46 -0700820 svm_fifo_free_ooo_data (f);
821
Florin Coras88001c62019-04-24 14:44:46 -0700822 if (CLIB_DEBUG)
823 {
824 f->master_session_index = ~0;
825 f->master_thread_index = ~0;
826 }
827
Florin Coras06d47752020-03-06 02:23:58 +0000828 fss->virtual_mem -= svm_fifo_size (f);
829
830 /* Add to free list */
831 f->next = fss->free_fifos;
832 f->prev = 0;
833 fss->free_fifos = f;
834
Florin Coras62ddc032019-12-08 18:30:42 -0800835 fsh_active_fifos_update (fsh, -1);
Florin Coras88001c62019-04-24 14:44:46 -0700836}
837
Florin Coras6d7552c2020-04-09 01:49:45 +0000838void
839fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f)
840{
841 fifo_segment_slice_t *fss;
842 svm_fifo_chunk_t *c;
843 u32 fl_index;
844
845 ASSERT (f->refcnt == 1);
846
847 fss = fsh_slice_get (fs->h, f->slice_index);
848 fss->virtual_mem -= svm_fifo_size (f);
849 if (f->flags & SVM_FIFO_F_LL_TRACKED)
850 fss_fifo_del_active_list (fss, f);
851
852 c = f->start_chunk;
853 while (c)
854 {
855 fl_index = fs_freelist_for_size (c->length);
856 clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
857 c = c->next;
858 }
859}
860
861void
862fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
863 u32 slice_index)
864{
865 fifo_segment_slice_t *fss;
866 svm_fifo_chunk_t *c;
867 u32 fl_index;
868
869 f->slice_index = slice_index;
870 fss = fsh_slice_get (fs->h, f->slice_index);
871 fss->virtual_mem += svm_fifo_size (f);
872 if (f->flags & SVM_FIFO_F_LL_TRACKED)
873 fss_fifo_add_active_list (fss, f);
874
875 c = f->start_chunk;
876 while (c)
877 {
878 fl_index = fs_freelist_for_size (c->length);
879 clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
880 c = c->next;
881 }
882}
883
Florin Corasf9d4ab42019-05-11 16:55:53 -0700884int
Florin Coras62ddc032019-12-08 18:30:42 -0800885fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
886 u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700887{
888 fifo_segment_header_t *fsh = fs->h;
Florin Coras62ddc032019-12-08 18:30:42 -0800889 fifo_segment_slice_t *fss;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700890
Florin Coras62ddc032019-12-08 18:30:42 -0800891 fss = fsh_slice_get (fsh, slice_index);
Florin Corasfe1ab7e2020-11-26 11:34:38 -0800892 return fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700893}
894
895int
Florin Coras62ddc032019-12-08 18:30:42 -0800896fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
897 u32 chunk_size, u32 batch_size)
Florin Corasf9d4ab42019-05-11 16:55:53 -0700898{
Florin Coras62ddc032019-12-08 18:30:42 -0800899 fifo_segment_header_t *fsh = fs->h;
900 fifo_segment_slice_t *fss;
Florin Coras473c8462020-11-16 18:11:51 -0800901 u32 fl_index;
Florin Corasf9d4ab42019-05-11 16:55:53 -0700902
Florin Coras62ddc032019-12-08 18:30:42 -0800903 if (!fs_chunk_size_is_valid (fsh, chunk_size))
Florin Corasf9d4ab42019-05-11 16:55:53 -0700904 {
905 clib_warning ("chunk size out of range %d", chunk_size);
906 return -1;
907 }
908
909 fl_index = fs_freelist_for_size (chunk_size);
Florin Coras62ddc032019-12-08 18:30:42 -0800910 fss = fsh_slice_get (fsh, slice_index);
911
Florin Coras473c8462020-11-16 18:11:51 -0800912 return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
Florin Corasf9d4ab42019-05-11 16:55:53 -0700913}
914
Florin Coras88001c62019-04-24 14:44:46 -0700915/**
916 * Pre-allocates fifo pairs in fifo segment
917 */
918void
919fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
920 u32 rx_fifo_size, u32 tx_fifo_size,
921 u32 * n_fifo_pairs)
922{
Florin Corasf9d4ab42019-05-11 16:55:53 -0700923 u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
Florin Coras8d0149d2020-01-02 19:22:51 +0000924 u32 hdrs, pairs_per_slice, alloc_now;
Florin Coras62ddc032019-12-08 18:30:42 -0800925 fifo_segment_header_t *fsh = fs->h;
926 int rx_fl_index, tx_fl_index, i;
927 fifo_segment_slice_t *fss;
Florin Coras88001c62019-04-24 14:44:46 -0700928 uword space_available;
Florin Coras88001c62019-04-24 14:44:46 -0700929
930 /* Parameter check */
931 if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
932 return;
933
Florin Coras62ddc032019-12-08 18:30:42 -0800934 if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700935 {
936 clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
937 return;
938 }
939
Florin Coras62ddc032019-12-08 18:30:42 -0800940 if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
Florin Coras88001c62019-04-24 14:44:46 -0700941 {
942 clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
943 return;
944 }
945
946 rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700947 rx_fl_index = fs_freelist_for_size (rx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700948 tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -0700949 tx_fl_index = fs_freelist_for_size (tx_fifo_size);
Florin Coras88001c62019-04-24 14:44:46 -0700950
Florin Corasf9d4ab42019-05-11 16:55:53 -0700951 hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
Florin Corascefd5d82019-05-05 13:19:57 -0700952
Florin Coras88001c62019-04-24 14:44:46 -0700953 /* Calculate space requirements */
Florin Corascefd5d82019-05-05 13:19:57 -0700954 pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
Florin Coras4453c092019-12-19 10:13:15 -0800955 space_available = fsh_free_space (fsh);
Florin Coraseaacce42019-07-02 13:07:37 -0700956 pairs_to_alloc = space_available / pair_size;
957 pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
Florin Coras62ddc032019-12-08 18:30:42 -0800958 pairs_per_slice = pairs_to_alloc / fs->n_slices;
Florin Coras8d0149d2020-01-02 19:22:51 +0000959 pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
Florin Coraseaacce42019-07-02 13:07:37 -0700960
Florin Coras62ddc032019-12-08 18:30:42 -0800961 if (!pairs_per_slice)
Florin Coraseaacce42019-07-02 13:07:37 -0700962 return;
Florin Coras88001c62019-04-24 14:44:46 -0700963
Florin Coras62ddc032019-12-08 18:30:42 -0800964 for (i = 0; i < fs->n_slices; i++)
965 {
Florin Coras8d0149d2020-01-02 19:22:51 +0000966 alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
Benoît Gannef23b1512020-11-25 15:06:01 +0100967 if (0 == alloc_now)
968 break;
969
970 fss = fsh_slice_get (fsh, i);
Florin Coras8d0149d2020-01-02 19:22:51 +0000971 if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
972 clib_warning ("rx prealloc failed: pairs %u", alloc_now);
973 if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
974 clib_warning ("tx prealloc failed: pairs %u", alloc_now);
Florin Coras88001c62019-04-24 14:44:46 -0700975
Florin Coras8d0149d2020-01-02 19:22:51 +0000976 /* Account for the pairs allocated */
977 *n_fifo_pairs -= alloc_now;
978 }
Florin Coras88001c62019-04-24 14:44:46 -0700979}
980
981/**
982 * Get number of active fifos
983 */
984u32
985fifo_segment_num_fifos (fifo_segment_t * fs)
986{
Florin Coras7a90e502020-04-09 04:46:14 +0000987 return fsh_n_active_fifos (fs->h);
Florin Coras88001c62019-04-24 14:44:46 -0700988}
989
Florin Coras62ddc032019-12-08 18:30:42 -0800990static u32
991fs_slice_num_free_fifos (fifo_segment_slice_t * fss)
Florin Coras88001c62019-04-24 14:44:46 -0700992{
Florin Coras88001c62019-04-24 14:44:46 -0700993 svm_fifo_t *f;
Florin Corascefd5d82019-05-05 13:19:57 -0700994 u32 count = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700995
Florin Coras62ddc032019-12-08 18:30:42 -0800996 f = fss->free_fifos;
Florin Coras88001c62019-04-24 14:44:46 -0700997 if (f == 0)
998 return 0;
999
1000 while (f)
1001 {
1002 f = f->next;
1003 count++;
1004 }
1005 return count;
1006}
1007
Florin Corasb095a3c2019-04-25 12:58:46 -07001008u32
Florin Coras62ddc032019-12-08 18:30:42 -08001009fifo_segment_num_free_fifos (fifo_segment_t * fs)
1010{
1011 fifo_segment_header_t *fsh = fs->h;
1012 fifo_segment_slice_t *fss;
1013 int slice_index;
1014 u32 count = 0;
1015
1016 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1017 {
1018 fss = fsh_slice_get (fsh, slice_index);
1019 count += fs_slice_num_free_fifos (fss);
1020 }
1021 return count;
1022}
1023
1024static u32
1025fs_slice_num_free_chunks (fifo_segment_slice_t * fss, u32 size)
Florin Corasb095a3c2019-04-25 12:58:46 -07001026{
1027 u32 count = 0, rounded_size, fl_index;
Florin Corasb095a3c2019-04-25 12:58:46 -07001028 svm_fifo_chunk_t *c;
1029 int i;
1030
Florin Corasb095a3c2019-04-25 12:58:46 -07001031 /* Count all free chunks? */
1032 if (size == ~0)
1033 {
Florin Coras62ddc032019-12-08 18:30:42 -08001034 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Corasb095a3c2019-04-25 12:58:46 -07001035 {
Florin Coras62ddc032019-12-08 18:30:42 -08001036 c = fss->free_chunks[i];
Florin Corasb095a3c2019-04-25 12:58:46 -07001037 if (c == 0)
1038 continue;
1039
1040 while (c)
1041 {
1042 c = c->next;
1043 count++;
1044 }
1045 }
1046 return count;
1047 }
1048
1049 rounded_size = (1 << (max_log2 (size)));
Florin Corasf9d4ab42019-05-11 16:55:53 -07001050 fl_index = fs_freelist_for_size (rounded_size);
Florin Corasb095a3c2019-04-25 12:58:46 -07001051
Florin Coras62ddc032019-12-08 18:30:42 -08001052 if (fl_index >= vec_len (fss->free_chunks))
Florin Corasb095a3c2019-04-25 12:58:46 -07001053 return 0;
1054
Florin Coras62ddc032019-12-08 18:30:42 -08001055 c = fss->free_chunks[fl_index];
Florin Corasb095a3c2019-04-25 12:58:46 -07001056 if (c == 0)
1057 return 0;
1058
1059 while (c)
1060 {
1061 c = c->next;
1062 count++;
1063 }
1064 return count;
1065}
1066
Florin Coras62ddc032019-12-08 18:30:42 -08001067u32
1068fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size)
1069{
1070 fifo_segment_header_t *fsh = fs->h;
1071 fifo_segment_slice_t *fss;
1072 int slice_index;
1073 u32 count = 0;
1074
1075 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1076 {
1077 fss = fsh_slice_get (fsh, slice_index);
1078 count += fs_slice_num_free_chunks (fss, size);
1079 }
1080 return count;
1081}
1082
Florin Corasf9d4ab42019-05-11 16:55:53 -07001083void
1084fifo_segment_update_free_bytes (fifo_segment_t * fs)
1085{
Florin Coras4453c092019-12-19 10:13:15 -08001086 fsh_update_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001087}
1088
Florin Corasef4f3e72019-12-11 14:27:53 -08001089uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001090fifo_segment_size (fifo_segment_t * fs)
1091{
1092 return fs->ssvm.ssvm_size;
1093}
1094
1095u8
1096fsh_has_reached_mem_limit (fifo_segment_header_t * fsh)
1097{
1098 return (fsh->flags & FIFO_SEGMENT_F_MEM_LIMIT) ? 1 : 0;
1099}
1100
1101void
1102fsh_reset_mem_limit (fifo_segment_header_t * fsh)
1103{
1104 fsh->flags &= ~FIFO_SEGMENT_F_MEM_LIMIT;
1105}
1106
1107uword
Florin Corasf9d4ab42019-05-11 16:55:53 -07001108fifo_segment_free_bytes (fifo_segment_t * fs)
1109{
Florin Coras62ddc032019-12-08 18:30:42 -08001110 return fsh_n_free_bytes (fs->h);
Florin Corasf9d4ab42019-05-11 16:55:53 -07001111}
1112
Florin Coras62ddc032019-12-08 18:30:42 -08001113uword
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001114fifo_segment_cached_bytes (fifo_segment_t * fs)
1115{
1116 return fsh_n_cached_bytes (fs->h);
1117}
1118
1119uword
Florin Coras75ccf7b2020-03-05 19:44:02 +00001120fifo_segment_available_bytes (fifo_segment_t * fs)
1121{
1122 return fsh_n_free_bytes (fs->h) + fsh_n_cached_bytes (fs->h);
1123}
1124
1125uword
Florin Coras1c91c772019-06-25 18:14:13 -07001126fifo_segment_fl_chunk_bytes (fifo_segment_t * fs)
Florin Corasf9d4ab42019-05-11 16:55:53 -07001127{
Florin Coras62ddc032019-12-08 18:30:42 -08001128 fifo_segment_header_t *fsh = fs->h;
1129 fifo_segment_slice_t *fss;
1130 uword n_bytes = 0;
1131 int slice_index;
1132
1133 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
1134 {
1135 fss = fsh_slice_get (fsh, slice_index);
Florin Corasd01d2ce2020-11-17 18:42:38 -08001136 n_bytes += fss_fl_chunk_bytes (fss);
Florin Coras62ddc032019-12-08 18:30:42 -08001137 }
1138
1139 return n_bytes;
Florin Corasf9d4ab42019-05-11 16:55:53 -07001140}
1141
Florin Coras88001c62019-04-24 14:44:46 -07001142u8
1143fifo_segment_has_fifos (fifo_segment_t * fs)
1144{
Florin Coras7a90e502020-04-09 04:46:14 +00001145 return (fsh_n_active_fifos (fs->h) != 0);
Florin Coras88001c62019-04-24 14:44:46 -07001146}
1147
1148svm_fifo_t *
Florin Coras62ddc032019-12-08 18:30:42 -08001149fifo_segment_get_slice_fifo_list (fifo_segment_t * fs, u32 slice_index)
Florin Coras88001c62019-04-24 14:44:46 -07001150{
Florin Coras62ddc032019-12-08 18:30:42 -08001151 fifo_segment_header_t *fsh = fs->h;
1152 fifo_segment_slice_t *fss;
1153
1154 fss = fsh_slice_get (fsh, slice_index);
1155 return fss->fifos;
Florin Coras88001c62019-04-24 14:44:46 -07001156}
1157
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001158u8
1159fifo_segment_get_mem_usage (fifo_segment_t * fs)
1160{
1161 uword size, in_use;
1162
1163 size = fifo_segment_size (fs);
1164 in_use =
1165 size - fifo_segment_free_bytes (fs) - fifo_segment_cached_bytes (fs);
1166 return (in_use * 100) / size;
1167}
1168
1169fifo_segment_mem_status_t
1170fifo_segment_determine_status (fifo_segment_header_t * fsh, u8 usage)
1171{
1172 if (!fsh->high_watermark || !fsh->low_watermark)
1173 return MEMORY_PRESSURE_NO_PRESSURE;
1174
1175 /* once the no-memory is detected, the status continues
1176 * until memory usage gets below the high watermark
1177 */
1178 if (fsh_has_reached_mem_limit (fsh))
1179 {
1180 if (usage >= fsh->high_watermark)
1181 return MEMORY_PRESSURE_NO_MEMORY;
1182 else
1183 fsh_reset_mem_limit (fsh);
1184 }
1185
1186 if (usage >= fsh->high_watermark)
1187 return MEMORY_PRESSURE_HIGH_PRESSURE;
1188
1189 else if (usage >= fsh->low_watermark)
1190 return MEMORY_PRESSURE_LOW_PRESSURE;
1191
1192 return MEMORY_PRESSURE_NO_PRESSURE;
1193}
1194
1195fifo_segment_mem_status_t
1196fifo_segment_get_mem_status (fifo_segment_t * fs)
1197{
1198 fifo_segment_header_t *fsh = fs->h;
1199 u8 usage = fifo_segment_get_mem_usage (fs);
1200
1201 return fifo_segment_determine_status (fsh, usage);
1202}
1203
Florin Coras88001c62019-04-24 14:44:46 -07001204u8 *
1205format_fifo_segment_type (u8 * s, va_list * args)
1206{
1207 fifo_segment_t *sp;
1208 sp = va_arg (*args, fifo_segment_t *);
1209 ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
1210
1211 if (st == SSVM_SEGMENT_PRIVATE)
Florin Coras2a7c0b62020-09-28 23:40:28 -07001212 s = format (s, "%s", "private");
Florin Coras88001c62019-04-24 14:44:46 -07001213 else if (st == SSVM_SEGMENT_MEMFD)
1214 s = format (s, "%s", "memfd");
1215 else if (st == SSVM_SEGMENT_SHM)
1216 s = format (s, "%s", "shm");
1217 else
1218 s = format (s, "%s", "unknown");
1219 return s;
1220}
1221
1222/**
1223 * Segment format function
1224 */
1225u8 *
1226format_fifo_segment (u8 * s, va_list * args)
1227{
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001228 u32 count, indent, active_fifos, free_fifos;
Florin Coras5368bb02019-06-09 09:24:33 -07001229 fifo_segment_t *fs = va_arg (*args, fifo_segment_t *);
Florin Coras88001c62019-04-24 14:44:46 -07001230 int verbose __attribute__ ((unused)) = va_arg (*args, int);
Florin Corasef4f3e72019-12-11 14:27:53 -08001231 uword est_chunk_bytes, est_free_seg_bytes, free_chunks;
1232 uword chunk_bytes = 0, free_seg_bytes, chunk_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001233 uword tracked_cached_bytes;
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001234 uword fifo_hdr = 0, reserved;
Florin Coras5368bb02019-06-09 09:24:33 -07001235 fifo_segment_header_t *fsh;
Florin Coras62ddc032019-12-08 18:30:42 -08001236 fifo_segment_slice_t *fss;
Florin Corascefd5d82019-05-05 13:19:57 -07001237 svm_fifo_chunk_t *c;
Florin Coras62ddc032019-12-08 18:30:42 -08001238 u32 slice_index;
Florin Coras5368bb02019-06-09 09:24:33 -07001239 char *address;
1240 size_t size;
Florin Coras88001c62019-04-24 14:44:46 -07001241 int i;
Florin Coras06d47752020-03-06 02:23:58 +00001242 uword allocated, in_use, virt;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001243 f64 usage;
1244 fifo_segment_mem_status_t mem_st;
Florin Coras88001c62019-04-24 14:44:46 -07001245
1246 indent = format_get_indent (s) + 2;
Florin Coras88001c62019-04-24 14:44:46 -07001247
Florin Coras5368bb02019-06-09 09:24:33 -07001248 if (fs == 0)
1249 {
Florin Coras2a7c0b62020-09-28 23:40:28 -07001250 s = format (s, "%-20s%10s%15s%15s%15s%15s", "Name", "Type",
Florin Coras5368bb02019-06-09 09:24:33 -07001251 "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address");
1252 return s;
1253 }
1254
Florin Coras5368bb02019-06-09 09:24:33 -07001255 fifo_segment_info (fs, &address, &size);
1256 active_fifos = fifo_segment_num_fifos (fs);
1257 free_fifos = fifo_segment_num_free_fifos (fs);
1258
Florin Coras2a7c0b62020-09-28 23:40:28 -07001259 s = format (s, "%-20v%10U%15llu%15u%15u%15llx", ssvm_name (&fs->ssvm),
Florin Coras5368bb02019-06-09 09:24:33 -07001260 format_fifo_segment_type, fs, size >> 20ULL, active_fifos,
1261 free_fifos, address);
1262
1263 if (!verbose)
1264 return s;
1265
Florin Coras62ddc032019-12-08 18:30:42 -08001266 fsh = fs->h;
1267
1268 free_chunks = fifo_segment_num_free_chunks (fs, ~0);
1269 if (free_chunks)
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001270 s =
1271 format (s, "\n\n%UFree/Allocated chunks by size:\n", format_white_space,
1272 indent + 2);
Florin Corasf8461bf2019-11-07 17:00:15 -08001273 else
1274 s = format (s, "\n");
1275
Florin Coras62ddc032019-12-08 18:30:42 -08001276 for (slice_index = 0; slice_index < fs->n_slices; slice_index++)
Florin Coras88001c62019-04-24 14:44:46 -07001277 {
Florin Coras62ddc032019-12-08 18:30:42 -08001278 fss = fsh_slice_get (fsh, slice_index);
1279 for (i = 0; i < vec_len (fss->free_chunks); i++)
Florin Coras88001c62019-04-24 14:44:46 -07001280 {
Florin Coras62ddc032019-12-08 18:30:42 -08001281 c = fss->free_chunks[i];
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001282 if (c == 0 && fss->num_chunks[i] == 0)
Florin Coras62ddc032019-12-08 18:30:42 -08001283 continue;
1284 count = 0;
1285 while (c)
1286 {
1287 c = c->next;
1288 count++;
1289 }
1290
1291 chunk_size = fs_freelist_index_to_size (i);
Ryujiro Shibuyad3588722020-03-13 12:45:06 +00001292 s = format (s, "%U%-5u kB: %u/%u\n", format_white_space, indent + 2,
1293 chunk_size >> 10, count, fss->num_chunks[i]);
Florin Coras62ddc032019-12-08 18:30:42 -08001294
1295 chunk_bytes += count * chunk_size;
Florin Coras88001c62019-04-24 14:44:46 -07001296 }
Florin Coras88001c62019-04-24 14:44:46 -07001297 }
Florin Corasf8461bf2019-11-07 17:00:15 -08001298
1299 fifo_hdr = free_fifos * sizeof (svm_fifo_t);
1300 est_chunk_bytes = fifo_segment_fl_chunk_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001301 est_free_seg_bytes = fifo_segment_free_bytes (fs);
Florin Corasf8461bf2019-11-07 17:00:15 -08001302 fifo_segment_update_free_bytes (fs);
Florin Coras62ddc032019-12-08 18:30:42 -08001303 free_seg_bytes = fifo_segment_free_bytes (fs);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +00001304 tracked_cached_bytes = fifo_segment_cached_bytes (fs);
1305 allocated = fifo_segment_size (fs);
1306 in_use = fifo_segment_size (fs) - est_free_seg_bytes - tracked_cached_bytes;
1307 usage = (100.0 * in_use) / allocated;
1308 mem_st = fifo_segment_get_mem_status (fs);
Florin Coras06d47752020-03-06 02:23:58 +00001309 virt = fsh_virtual_mem (fsh);
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001310 reserved = fsh->n_reserved_bytes;
Florin Corasf8461bf2019-11-07 17:00:15 -08001311
Florin Coras06d47752020-03-06 02:23:58 +00001312 s = format (s, "\n%Useg free bytes: %U (%lu) estimated: %U (%lu) reserved:"
1313 " %U (%lu)\n", format_white_space, indent + 2,
1314 format_memory_size, free_seg_bytes, free_seg_bytes,
1315 format_memory_size, est_free_seg_bytes, est_free_seg_bytes,
Ryujiro Shibuya65c30ce2020-03-26 07:29:09 +00001316 format_memory_size, reserved, reserved);
Florin Coras06d47752020-03-06 02:23:58 +00001317 s = format (s, "%Uchunk free bytes: %U (%lu) estimated: %U (%lu) tracked:"
1318 " %U (%lu)\n", format_white_space, indent + 2,
1319 format_memory_size, chunk_bytes, chunk_bytes,
1320 format_memory_size, est_chunk_bytes, est_chunk_bytes,
1321 format_memory_size, tracked_cached_bytes, tracked_cached_bytes);
1322 s = format (s, "%Ufifo active: %u hdr free bytes: %U (%u) \n",
1323 format_white_space, indent + 2, fsh->n_active_fifos,
1324 format_memory_size, fifo_hdr, fifo_hdr);
1325 s = format (s, "%Usegment usage: %.2f%% (%U / %U) virt: %U status: %s\n",
1326 format_white_space, indent + 2, usage, format_memory_size,
1327 in_use, format_memory_size, allocated, format_memory_size, virt,
1328 fifo_segment_mem_status_strings[mem_st]);
Florin Corasf8461bf2019-11-07 17:00:15 -08001329 s = format (s, "\n");
Florin Coras5368bb02019-06-09 09:24:33 -07001330
Florin Coras88001c62019-04-24 14:44:46 -07001331 return s;
1332}
1333
1334/*
1335 * fd.io coding-style-patch-verification: ON
1336 *
1337 * Local Variables:
1338 * eval: (c-set-style "gnu")
1339 * End:
1340 */