Improve the svm fifo allocator
[vpp.git] / src / svm / svm_fifo_segment.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <svm/svm_fifo_segment.h>
17
18 svm_fifo_segment_main_t svm_fifo_segment_main;
19
20 static void
21 allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh,
22                          u32 data_size_in_bytes, int chunk_size)
23 {
24   int freelist_index;
25   u32 size;
26   u8 *fifo_space;
27   u32 rounded_data_size;
28   svm_fifo_t *f;
29   int i;
30
31   rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
32   freelist_index = max_log2 (rounded_data_size)
33     - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
34
35   /* Calculate space requirement $$$ round-up data_size_in_bytes */
36   size = (sizeof (*f) + rounded_data_size) * chunk_size;
37
38   /* Allocate fifo space. May fail. */
39   fifo_space = clib_mem_alloc_aligned_at_offset
40     (size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
41      0 /* os_out_of_memory */ );
42
43   /* Out of space.. */
44   if (fifo_space == 0)
45     return;
46
47   /* Carve fifo space */
48   f = (svm_fifo_t *) fifo_space;
49   for (i = 0; i < chunk_size; i++)
50     {
51       f->freelist_index = freelist_index;
52       f->next = fsh->free_fifos[freelist_index];
53       fsh->free_fifos[freelist_index] = f;
54       fifo_space += sizeof (*f) + rounded_data_size;
55       f = (svm_fifo_t *) fifo_space;
56     }
57 }
58
59 static void
60 preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh,
61                         svm_fifo_segment_create_args_t * a)
62 {
63   u32 rx_fifo_size, tx_fifo_size;
64   u32 rx_rounded_data_size, tx_rounded_data_size;
65   svm_fifo_t *f;
66   u8 *rx_fifo_space, *tx_fifo_space;
67   int rx_freelist_index, tx_freelist_index;
68   int i;
69
70   /* Parameter check */
71   if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0
72       || a->preallocated_fifo_pairs == 0)
73     return;
74
75   if (a->rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
76       a->rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
77     {
78       clib_warning ("rx fifo_size out of range %d", a->rx_fifo_size);
79       return;
80     }
81
82   if (a->tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE ||
83       a->tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE)
84     {
85       clib_warning ("tx fifo_size out of range %d", a->rx_fifo_size);
86       return;
87     }
88
89   rx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size)));
90
91   rx_freelist_index = max_log2 (a->rx_fifo_size)
92     - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
93
94   tx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size)));
95
96   tx_freelist_index = max_log2 (a->tx_fifo_size)
97     - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
98
99   /* Calculate space requirements */
100   rx_fifo_size = (sizeof (*f) + rx_rounded_data_size)
101     * a->preallocated_fifo_pairs;
102   tx_fifo_size = (sizeof (*f) + tx_rounded_data_size)
103     * a->preallocated_fifo_pairs;
104
105   vec_validate_init_empty (fsh->free_fifos,
106                            clib_max (rx_freelist_index, tx_freelist_index),
107                            0);
108   if (0)
109     clib_warning ("rx_fifo_size %u (%d mb), tx_fifo_size %u (%d mb)",
110                   rx_fifo_size, rx_fifo_size >> 20,
111                   tx_fifo_size, tx_fifo_size >> 20);
112
113   /* Allocate rx fifo space. May fail. */
114   rx_fifo_space = clib_mem_alloc_aligned_at_offset
115     (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
116      0 /* os_out_of_memory */ );
117
118   /* Same for TX */
119   tx_fifo_space = clib_mem_alloc_aligned_at_offset
120     (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
121      0 /* os_out_of_memory */ );
122
123   /* Make sure it worked. Clean up if it didn't... */
124   if (rx_fifo_space == 0 || tx_fifo_space == 0)
125     {
126       if (rx_fifo_space)
127         clib_mem_free (rx_fifo_space);
128       else
129         clib_warning ("rx fifo preallocation failure: size %d npairs %d",
130                       a->rx_fifo_size, a->preallocated_fifo_pairs);
131
132       if (tx_fifo_space)
133         clib_mem_free (tx_fifo_space);
134       else
135         clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
136                       a->tx_fifo_size, a->preallocated_fifo_pairs);
137       return;
138     }
139
140   /* Carve rx fifo space */
141   f = (svm_fifo_t *) rx_fifo_space;
142   for (i = 0; i < a->preallocated_fifo_pairs; i++)
143     {
144       f->freelist_index = rx_freelist_index;
145       f->next = fsh->free_fifos[rx_freelist_index];
146       fsh->free_fifos[rx_freelist_index] = f;
147       rx_fifo_space += sizeof (*f) + rx_rounded_data_size;
148       f = (svm_fifo_t *) rx_fifo_space;
149     }
150   /* Carve tx fifo space */
151   f = (svm_fifo_t *) tx_fifo_space;
152   for (i = 0; i < a->preallocated_fifo_pairs; i++)
153     {
154       f->freelist_index = tx_freelist_index;
155       f->next = fsh->free_fifos[tx_freelist_index];
156       fsh->free_fifos[tx_freelist_index] = f;
157       tx_fifo_space += sizeof (*f) + tx_rounded_data_size;
158       f = (svm_fifo_t *) tx_fifo_space;
159     }
160 }
161
162 /** (master) create an svm fifo segment */
163 int
164 svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
165 {
166   int rv;
167   svm_fifo_segment_private_t *s;
168   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
169   ssvm_shared_header_t *sh;
170   svm_fifo_segment_header_t *fsh;
171   void *oldheap;
172
173   /* Allocate a fresh segment */
174   pool_get (sm->segments, s);
175   memset (s, 0, sizeof (*s));
176
177   s->ssvm.ssvm_size = a->segment_size;
178   s->ssvm.i_am_master = 1;
179   s->ssvm.my_pid = getpid ();
180   s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
181   s->ssvm.requested_va = sm->next_baseva;
182
183   rv = ssvm_master_init (&s->ssvm, s - sm->segments);
184
185   if (rv)
186     {
187       _vec_len (s) = vec_len (s) - 1;
188       return (rv);
189     }
190
191   /* Note; requested_va updated due to seg base addr randomization */
192   sm->next_baseva = s->ssvm.requested_va + a->segment_size;
193
194   sh = s->ssvm.sh;
195   oldheap = ssvm_push_heap (sh);
196
197   /* Set up svm_fifo_segment shared header */
198   fsh = clib_mem_alloc (sizeof (*fsh));
199   memset (fsh, 0, sizeof (*fsh));
200   sh->opaque[0] = fsh;
201   s->h = fsh;
202   fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
203
204   preallocate_fifo_pairs (fsh, a);
205
206   ssvm_pop_heap (oldheap);
207
208   sh->ready = 1;
209   vec_add1 (a->new_segment_indices, s - sm->segments);
210   return (0);
211 }
212
213 /** Create an svm fifo segment in process-private memory */
214 int
215 svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
216 {
217   svm_fifo_segment_private_t *s;
218   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
219   ssvm_shared_header_t *sh;
220   svm_fifo_segment_header_t *fsh;
221   void *oldheap;
222   u8 **heaps = 0;
223   mheap_t *heap_header;
224   int segment_count = 1;
225   int i;
226
227   if (a->private_segment_count && a->private_segment_size)
228     {
229       void *mem;
230       u8 *heap;
231       u32 pagesize = clib_mem_get_page_size ();
232       u32 rnd_size;
233
234       for (i = 0; i < a->private_segment_count; i++)
235         {
236           rnd_size = (a->private_segment_size + (pagesize - 1)) & ~pagesize;
237
238           mem = mmap (0, rnd_size, PROT_READ | PROT_WRITE,
239                       MAP_PRIVATE | MAP_ANONYMOUS,
240                       -1 /* fd */ , 0 /* offset */ );
241
242           if (mem == MAP_FAILED)
243             {
244               clib_unix_warning ("mmap");
245               return -1;
246             }
247           heap = mheap_alloc (mem, rnd_size);
248           heap_header = mheap_header (heap);
249           heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
250           vec_add1 (heaps, heap);
251         }
252       segment_count = a->private_segment_count;
253     }
254
255   /* Spread preallocated fifo pairs across segments */
256   a->preallocated_fifo_pairs /= segment_count;
257
258   /* Allocate segments */
259   for (i = 0; i < segment_count; i++)
260     {
261       pool_get (sm->segments, s);
262       memset (s, 0, sizeof (*s));
263
264       s->ssvm.ssvm_size = ~0;
265       s->ssvm.i_am_master = 1;
266       s->ssvm.my_pid = getpid ();
267       s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
268       s->ssvm.requested_va = ~0;
269
270       /* Allocate a [sic] shared memory header, in process memory... */
271       sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
272       s->ssvm.sh = sh;
273
274       memset (sh, 0, sizeof (*sh));
275       sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap ();
276
277       /* Set up svm_fifo_segment shared header */
278       fsh = clib_mem_alloc (sizeof (*fsh));
279       memset (fsh, 0, sizeof (*fsh));
280       sh->opaque[0] = fsh;
281       s->h = fsh;
282       fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
283
284       if (a->private_segment_count)
285         {
286           oldheap = clib_mem_get_heap ();
287           clib_mem_set_heap (sh->heap);
288           preallocate_fifo_pairs (fsh, a);
289           clib_mem_set_heap (oldheap);
290         }
291
292       sh->ready = 1;
293       vec_add1 (a->new_segment_indices, s - sm->segments);
294     }
295   vec_free (heaps);
296   return (0);
297 }
298
299 /** (slave) attach to an svm fifo segment */
300 int
301 svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
302 {
303   int rv;
304   svm_fifo_segment_private_t *s;
305   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
306   ssvm_shared_header_t *sh;
307   svm_fifo_segment_header_t *fsh;
308
309   /* Allocate a fresh segment */
310   pool_get (sm->segments, s);
311   memset (s, 0, sizeof (*s));
312
313   s->ssvm.ssvm_size = a->segment_size;
314   s->ssvm.my_pid = getpid ();
315   s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
316   s->ssvm.requested_va = sm->next_baseva;
317
318   rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
319
320   if (rv)
321     {
322       _vec_len (s) = vec_len (s) - 1;
323       return (rv);
324     }
325
326   /* Fish the segment header */
327   sh = s->ssvm.sh;
328   fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
329   s->h = fsh;
330
331   vec_add1 (a->new_segment_indices, s - sm->segments);
332   return (0);
333 }
334
335 void
336 svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
337 {
338   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
339   ssvm_delete (&s->ssvm);
340   pool_put (sm->segments, s);
341 }
342
343 svm_fifo_t *
344 svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
345                              u32 data_size_in_bytes,
346                              svm_fifo_segment_freelist_t list_index)
347 {
348   ssvm_shared_header_t *sh;
349   svm_fifo_segment_header_t *fsh;
350   svm_fifo_t *f;
351   void *oldheap;
352   int freelist_index;
353
354   /*
355    * 2K minimum. It's not likely that anything good will happen
356    * with a 1K FIFO.
357    */
358   if (data_size_in_bytes < FIFO_SEGMENT_MIN_FIFO_SIZE ||
359       data_size_in_bytes > FIFO_SEGMENT_MAX_FIFO_SIZE)
360     {
361       clib_warning ("fifo size out of range %d", data_size_in_bytes);
362       return 0;
363     }
364
365   freelist_index = max_log2 (data_size_in_bytes)
366     - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE);
367
368   sh = s->ssvm.sh;
369   fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
370
371   ssvm_lock_non_recursive (sh, 1);
372   oldheap = ssvm_push_heap (sh);
373
374   switch (list_index)
375     {
376     case FIFO_SEGMENT_RX_FREELIST:
377     case FIFO_SEGMENT_TX_FREELIST:
378       vec_validate_init_empty (fsh->free_fifos, freelist_index, 0);
379
380       f = fsh->free_fifos[freelist_index];
381       if (PREDICT_FALSE (f == 0))
382         {
383           allocate_new_fifo_chunk (fsh, data_size_in_bytes,
384                                    FIFO_SEGMENT_ALLOC_CHUNK_SIZE);
385           f = fsh->free_fifos[freelist_index];
386         }
387       if (PREDICT_TRUE (f != 0))
388         {
389           fsh->free_fifos[freelist_index] = f->next;
390           /* (re)initialize the fifo, as in svm_fifo_create */
391           memset (f, 0, sizeof (*f));
392           f->nitems = data_size_in_bytes;
393           f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
394           f->refcnt = 1;
395           f->freelist_index = freelist_index;
396           goto found;
397         }
398       /* FALLTHROUGH */
399     case FIFO_SEGMENT_FREELIST_NONE:
400       break;
401
402     default:
403       clib_warning ("ignore bogus freelist %d", list_index);
404       break;
405     }
406
407   /* Note: this can fail, in which case: create another segment */
408   f = svm_fifo_create (data_size_in_bytes);
409   if (PREDICT_FALSE (f == 0))
410     {
411       ssvm_pop_heap (oldheap);
412       ssvm_unlock_non_recursive (sh);
413       return (0);
414     }
415   f->freelist_index = freelist_index;
416
417 found:
418   /* If rx_freelist add to active fifos list. When cleaning up segment,
419    * we need a list of active sessions that should be disconnected. Since
420    * both rx and tx fifos keep pointers to the session, it's enough to track
421    * only one. */
422   if (list_index == FIFO_SEGMENT_RX_FREELIST)
423     {
424       if (fsh->fifos)
425         {
426           fsh->fifos->prev = f;
427           f->next = fsh->fifos;
428         }
429       fsh->fifos = f;
430     }
431
432   ssvm_pop_heap (oldheap);
433   ssvm_unlock_non_recursive (sh);
434   return (f);
435 }
436
437 void
438 svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
439                             svm_fifo_segment_freelist_t list_index)
440 {
441   ssvm_shared_header_t *sh;
442   svm_fifo_segment_header_t *fsh;
443   void *oldheap;
444   int freelist_index;
445
446   ASSERT (f->refcnt > 0);
447
448   if (--f->refcnt > 0)
449     return;
450
451   sh = s->ssvm.sh;
452   fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
453
454   freelist_index = f->freelist_index;
455
456   ASSERT (freelist_index > 0 && freelist_index < vec_len (fsh->free_fifos));
457
458   ssvm_lock_non_recursive (sh, 2);
459   oldheap = ssvm_push_heap (sh);
460
461   switch (list_index)
462     {
463     case FIFO_SEGMENT_RX_FREELIST:
464       /* Remove from active list */
465       if (f->prev)
466         f->prev->next = f->next;
467       else
468         fsh->fifos = f->next;
469       if (f->next)
470         f->next->prev = f->prev;
471       /* Fall through: we add only rx fifos to active pool */
472     case FIFO_SEGMENT_TX_FREELIST:
473       /* Add to free list */
474       f->next = fsh->free_fifos[freelist_index];
475       f->prev = 0;
476       fsh->free_fifos[freelist_index] = f;
477       break;
478     case FIFO_SEGMENT_FREELIST_NONE:
479       break;
480
481     default:
482       clib_warning ("ignore bogus freelist %d", list_index);
483       break;
484     }
485
486   if (CLIB_DEBUG)
487     {
488       f->master_session_index = ~0;
489       f->master_thread_index = ~0;
490     }
491
492   ssvm_pop_heap (oldheap);
493   ssvm_unlock_non_recursive (sh);
494 }
495
496 void
497 svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
498 {
499   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
500
501   sm->next_baseva = baseva;
502   sm->timeout_in_seconds = timeout_in_seconds;
503 }
504
505 u32
506 svm_fifo_segment_index (svm_fifo_segment_private_t * s)
507 {
508   return s - svm_fifo_segment_main.segments;
509 }
510
511 /*
512  * fd.io coding-style-patch-verification: ON
513  *
514  * Local Variables:
515  * eval: (c-set-style "gnu")
516  * End:
517  */