FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
buffer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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  * buffer.c: allocate/free network buffers.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 /**
41  * @file
42  *
43  * Allocate/free network buffers.
44  */
45 
46 #include <vppinfra/linux/sysfs.h>
47 #include <vlib/vlib.h>
48 #include <vlib/unix/unix.h>
49 #include <vpp/stats/stat_segment.h>
50 
51 #define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384
52 #define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192
53 
54 #ifdef CLIB_HAVE_VEC128
55 /* Assumptions by vlib_buffer_free_inline: */
57 STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16);
58 STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16);
59 #endif
60 
61 /* Make sure that buffer template size is not accidentally changed */
62 STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
63 
64 u16 __vlib_buffer_external_hdr_size = 0;
65 
66 static void
68  u32 index);
69 
70 static void
72  u32 index);
73 
74 static void
76 
77 uword
79  vlib_buffer_t * b_first)
80 {
81  vlib_buffer_t *b = b_first;
82  uword l_first = b_first->current_length;
83  uword l = 0;
84  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
85  {
86  b = vlib_get_buffer (vm, b->next_buffer);
87  l += b->current_length;
88  }
90  b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
91  return l + l_first;
92 }
93 
94 u8 *
95 format_vlib_buffer_no_chain (u8 * s, va_list * args)
96 {
97  vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
98  u32 indent = format_get_indent (s);
99  u8 *a = 0;
100 
101 #define _(bit, name, v) \
102  if (v && (b->flags & VLIB_BUFFER_##name)) \
103  a = format (a, "%s ", v);
105 #undef _
106  s = format (s, "current data %d, length %d, buffer-pool %d, "
107  "ref-count %u", b->current_data, b->current_length,
109 
110  if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
111  s = format (s, ", totlen-nifb %d",
113 
114  if (b->flags & VLIB_BUFFER_IS_TRACED)
115  s = format (s, ", trace handle 0x%x", b->trace_handle);
116 
117  if (a)
118  s = format (s, "\n%U%v", format_white_space, indent, a);
119  vec_free (a);
120 
121  return s;
122 }
123 
124 u8 *
125 format_vlib_buffer (u8 * s, va_list * args)
126 {
127  vlib_main_t *vm = vlib_get_main ();
128  vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
129  u32 indent = format_get_indent (s);
130 
131  s = format (s, "%U", format_vlib_buffer_no_chain, b);
132 
133  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
134  {
135  u32 next_buffer = b->next_buffer;
136  b = vlib_get_buffer (vm, next_buffer);
137 
138  s =
139  format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
140  format_white_space, indent, next_buffer, b->current_length,
141  b->ref_count);
142  }
143 
144  return s;
145 }
146 
147 u8 *
148 format_vlib_buffer_and_data (u8 * s, va_list * args)
149 {
150  vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
151 
152  s = format (s, "%U, %U",
155 
156  return s;
157 }
158 
159 static u8 *
160 format_vlib_buffer_known_state (u8 * s, va_list * args)
161 {
163  char *t;
164 
165  switch (state)
166  {
167  case VLIB_BUFFER_UNKNOWN:
168  t = "unknown";
169  break;
170 
172  t = "known-allocated";
173  break;
174 
176  t = "known-free";
177  break;
178 
179  default:
180  t = "invalid";
181  break;
182  }
183 
184  return format (s, "%s", t);
185 }
186 
187 u8 *
188 format_vlib_buffer_contents (u8 * s, va_list * va)
189 {
190  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
191  vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
192 
193  while (1)
194  {
196  if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
197  break;
198  b = vlib_get_buffer (vm, b->next_buffer);
199  }
200 
201  return s;
202 }
203 
204 static u8 *
206  u32 bi,
207  uword follow_buffer_next, uword ** unique_hash)
208 {
210  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
211 
212  if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
213  return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
214 
215  if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
216  return format (0, "current data %d before pre-data", b->current_data);
217 
218  if (b->current_data + b->current_length >
220  return format (0, "%d-%d beyond end of buffer %d", b->current_data,
222 
223  if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
224  {
226  u8 *msg, *result;
227 
228  k = vlib_buffer_is_known (vm, b->next_buffer);
230  return format (0, "next 0x%x: %U",
232 
233  if (unique_hash)
234  {
235  if (hash_get (*unique_hash, b->next_buffer))
236  return format (0, "duplicate buffer 0x%x", b->next_buffer);
237 
238  hash_set1 (*unique_hash, b->next_buffer);
239  }
240 
241  msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
242  if (msg)
243  {
244  result = format (0, "next 0x%x: %v", b->next_buffer, msg);
245  vec_free (msg);
246  return result;
247  }
248  }
249 
250  return 0;
251 }
252 
253 u8 *
254 vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
255 {
256  return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
257  /* unique_hash */ 0);
258 }
259 
260 u8 *
262  u32 * buffers,
263  uword next_buffer_stride,
264  uword n_buffers,
265  vlib_buffer_known_state_t known_state,
266  uword follow_buffer_next)
267 {
268  uword i, *hash;
269  u32 bi, *b = buffers;
271  u8 *msg = 0, *result = 0;
272 
273  hash = hash_create (0, 0);
274  for (i = 0; i < n_buffers; i++)
275  {
276  bi = b[0];
277  b += next_buffer_stride;
278 
279  /* Buffer is not unique. */
280  if (hash_get (hash, bi))
281  {
282  msg = format (0, "not unique");
283  goto done;
284  }
285 
286  k = vlib_buffer_is_known (vm, bi);
287  if (k != known_state)
288  {
289  msg = format (0, "is %U; expected %U",
291  format_vlib_buffer_known_state, known_state);
292  goto done;
293  }
294 
295  msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
296  if (msg)
297  goto done;
298 
299  hash_set1 (hash, bi);
300  }
301 
302 done:
303  if (msg)
304  {
305  result = format (0, "0x%x: %v", bi, msg);
306  vec_free (msg);
307  }
308  hash_free (hash);
309  return result;
310 }
311 
312 /*
313  * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
314  * and vlib_mains[0] = &vlib_global_main from the beginning of time.
315  *
316  * The only place which should ever expand vlib_mains is start_workers()
317  * in threads.c. It knows about the bootstrap vector.
318  */
319 /* *INDENT-OFF* */
320 static struct
321 {
324 } __attribute__ ((packed)) __bootstrap_vlib_main_vector
325  __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
326 {
327  .h.len = 1,
328  .vm = &vlib_global_main,
329 };
330 /* *INDENT-ON* */
331 
332 vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
333 
334 
335 /* When debugging validate that given buffers are either known allocated
336  or known free. */
337 void
339  u32 * buffers,
340  uword n_buffers,
341  vlib_buffer_known_state_t expected_state)
342 {
344  u32 *b;
345  uword i, bi, is_free;
346 
347  if (CLIB_DEBUG == 0)
348  return;
349 
350  is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
351  b = buffers;
352  for (i = 0; i < n_buffers; i++)
353  {
355 
356  bi = b[0];
357  b += 1;
358  known = vlib_buffer_is_known (vm, bi);
359 
360  if (known == VLIB_BUFFER_UNKNOWN &&
361  expected_state == VLIB_BUFFER_KNOWN_FREE)
362  known = VLIB_BUFFER_KNOWN_FREE;
363 
364  if (known != expected_state)
365  {
366  clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
367  format_vlib_buffer_known_state, known, bi);
368  }
369 
374  }
375 }
376 
377 void
380  void *packet_data,
381  uword n_packet_data_bytes,
382  uword min_n_buffers_each_alloc, char *fmt, ...)
383 {
384  va_list va;
385 
386  va_start (va, fmt);
387  t->name = va_format (0, fmt, &va);
388  va_end (va);
389 
391 
392  clib_memset (t, 0, sizeof (t[0]));
393 
394  vec_add (t->packet_data, packet_data, n_packet_data_bytes);
395  t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
397 }
398 
399 void *
401  vlib_packet_template_t * t, u32 * bi_result)
402 {
403  u32 bi;
404  vlib_buffer_t *b;
405 
406  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
407  return 0;
408 
409  *bi_result = bi;
410 
411  b = vlib_get_buffer (vm, bi);
413  t->packet_data, vec_len (t->packet_data));
415 
416  return b->data;
417 }
418 
419 /* Append given data to end of buffer, possibly allocating new buffers. */
420 int
421 vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
422  u32 n_data_bytes)
423 {
424  u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
425  vlib_buffer_t *b;
426  void *d;
427 
428  bi = *buffer_index;
429  if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
430  goto out_of_buffers;
431 
432  d = data;
433  n_left = n_data_bytes;
434  n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
435 
436  b = vlib_get_buffer (vm, bi);
437  b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
438 
439  /* Get to the end of the chain before we try to append data... */
440  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
441  b = vlib_get_buffer (vm, b->next_buffer);
442 
443  while (1)
444  {
445  u32 n;
446 
447  ASSERT (n_buffer_bytes >= b->current_length);
448  n_left_this_buffer =
449  n_buffer_bytes - (b->current_data + b->current_length);
450  n = clib_min (n_left_this_buffer, n_left);
452  n);
453  b->current_length += n;
454  n_left -= n;
455  if (n_left == 0)
456  break;
457 
458  d += n;
459  if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
460  goto out_of_buffers;
461 
462  b->flags |= VLIB_BUFFER_NEXT_PRESENT;
463 
464  b = vlib_get_buffer (vm, b->next_buffer);
465  }
466 
467  *buffer_index = bi;
468  return 0;
469 
470 out_of_buffers:
471  clib_warning ("out of buffers");
472  return 1;
473 }
474 
475 u16
478  vlib_buffer_t ** last, void *data,
479  u16 data_len)
480 {
481  vlib_buffer_t *l = *last;
482  u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
483  u16 copied = 0;
484  ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
485  while (data_len)
486  {
487  u16 max = n_buffer_bytes - l->current_length - l->current_data;
488  if (max == 0)
489  {
490  if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
491  first->buffer_pool_index))
492  return copied;
493  *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
494  max = n_buffer_bytes - l->current_length - l->current_data;
495  }
496 
497  u16 len = (data_len > max) ? max : data_len;
499  data + copied, len);
500  vlib_buffer_chain_increase_length (first, l, len);
501  data_len -= len;
502  copied += len;
503  }
504  return copied;
505 }
506 
507 static uword
508 vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size)
509 {
510  uword alloc_size = ext_hdr_size + sizeof (vlib_buffer_t) + data_size;
511  alloc_size = CLIB_CACHE_LINE_ROUND (alloc_size);
512 
513  /* in case when we have even number of cachelines, we add one more for
514  * better cache occupancy */
515  alloc_size |= CLIB_CACHE_LINE_BYTES;
516 
517  return alloc_size;
518 }
519 
520 u8
521 vlib_buffer_pool_create (vlib_main_t * vm, char *name, u32 data_size,
522  u32 physmem_map_index)
523 {
525  vlib_buffer_pool_t *bp;
526  vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
527  uword start = pointer_to_uword (m->base);
528  uword size = (uword) m->n_pages << m->log2_page_size;
529  uword i, j;
530  u32 alloc_size, n_alloc_per_page;
531 
532  if (vec_len (bm->buffer_pools) >= 255)
533  return ~0;
534 
536 
537  if (bm->buffer_mem_size == 0)
538  {
539  bm->buffer_mem_start = start;
540  bm->buffer_mem_size = size;
541  }
542  else if (start < bm->buffer_mem_start)
543  {
544  bm->buffer_mem_size += bm->buffer_mem_start - start;
545  bm->buffer_mem_start = start;
546  if (size > bm->buffer_mem_size)
547  bm->buffer_mem_size = size;
548  }
549  else if (start > bm->buffer_mem_start)
550  {
551  uword new_size = start - bm->buffer_mem_start + size;
552  if (new_size > bm->buffer_mem_size)
553  bm->buffer_mem_size = new_size;
554  }
555 
556  if ((u64) bm->buffer_mem_size >
557  ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
558  {
559  clib_panic ("buffer memory size out of range!");
560  }
561 
562  bp->start = start;
563  bp->size = size;
564  bp->index = bp - bm->buffer_pools;
566  bp->buffer_template.ref_count = 1;
567  bp->physmem_map_index = physmem_map_index;
568  bp->name = format (0, "%s%c", name, 0);
569  bp->data_size = data_size;
570  bp->numa_node = m->numa_node;
571 
572  vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
574 
575  alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size);
576  n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
577 
578  /* preallocate buffer indices memory */
579  bp->n_buffers = m->n_pages * n_alloc_per_page;
580  bp->buffers = clib_mem_alloc_aligned (bp->n_buffers * sizeof (u32),
582 
583  clib_spinlock_init (&bp->lock);
584 
585  for (j = 0; j < m->n_pages; j++)
586  for (i = 0; i < n_alloc_per_page; i++)
587  {
588  u8 *p;
589  u32 bi;
590 
591  p = m->base + (j << m->log2_page_size) + i * alloc_size;
592  p += bm->ext_hdr_size;
593 
594  /*
595  * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
596  * Allows various places to ASSERT (bi != 0). Much easier
597  * than debugging downstream crashes in successor nodes.
598  */
599  if (p == m->base)
600  continue;
601 
603 
604  bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
605 
606  bp->buffers[bp->n_avail++] = bi;
607 
608  vlib_get_buffer (vm, bi);
609  }
610 
611  return bp->index;
612 }
613 
614 static u8 *
615 format_vlib_buffer_pool (u8 * s, va_list * va)
616 {
617  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
618  vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
620  u32 cached = 0;
621 
622  if (!bp)
623  return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
624  "Pool Name", "Index", "NUMA", "Size", "Data Size",
625  "Total", "Avail", "Cached", "Used");
626 
627  /* *INDENT-OFF* */
628  vec_foreach (bpt, bp->threads)
629  cached += bpt->n_cached;
630  /* *INDENT-ON* */
631 
632  s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u",
633  bp->name, bp->index, bp->numa_node, bp->data_size +
634  sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size,
635  bp->data_size, bp->n_buffers, bp->n_avail, cached,
636  bp->n_buffers - bp->n_avail - cached);
637 
638  return s;
639 }
640 
641 static clib_error_t *
643  unformat_input_t * input, vlib_cli_command_t * cmd)
644 {
646  vlib_buffer_pool_t *bp;
647 
648  vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, 0);
649 
650  /* *INDENT-OFF* */
651  vec_foreach (bp, bm->buffer_pools)
652  vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, bp);
653  /* *INDENT-ON* */
654 
655  return 0;
656 }
657 
658 /* *INDENT-OFF* */
659 VLIB_CLI_COMMAND (show_buffers_command, static) = {
660  .path = "show buffers",
661  .short_help = "Show packet buffer allocation",
662  .function = show_buffers,
663 };
664 /* *INDENT-ON* */
665 
666 clib_error_t *
668 {
670  vlib_buffer_pool_t *bp;
671 
672  /* *INDENT-OFF* */
673  vec_foreach (bp, bm->buffer_pools)
674  {
675  clib_spinlock_lock (&bp->lock);
676  vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
678  clib_spinlock_unlock (&bp->lock);
679  }
680  /* *INDENT-ON* */
681 
682  return 0;
683 }
684 
686 
687 static clib_error_t *
689  u8 * index)
690 {
692  clib_error_t *error;
693  u32 physmem_map_index;
694  uword n_pages, pagesize;
695  u32 buffers_per_numa;
696  u32 buffer_size = vlib_buffer_alloc_size (bm->ext_hdr_size,
698  (vm));
699  u8 *name;
700 
702  name = format (0, "buffers-numa-%d%c", numa_node, 0);
703 
704  buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
706 
707 retry:
708 
709  if (buffer_size > pagesize)
710  {
711  error =
713  "buffer size (%llu) is greater than page size (%llu)",
714  buffer_size, pagesize);
715  goto done;
716  }
717 
718  n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
719  error = vlib_physmem_shared_map_create (vm, (char *) name,
720  n_pages * pagesize,
721  min_log2 (pagesize), numa_node,
722  &physmem_map_index);
723 
724  if (error && pagesize != clib_mem_get_page_size ())
725  {
726  vlib_log_warn (bm->log_default, "%U", format_clib_error, error);
727  clib_error_free (error);
728  vlib_log_warn (bm->log_default, "falling back to non-hugepage "
729  "backed buffer pool");
730  pagesize = clib_mem_get_page_size ();
731  buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
733  goto retry;
734  }
735 
736  if (error)
737  goto done;
738 
739  vec_reset_length (name);
740  name = format (name, "default-numa-%d%c", numa_node, 0);
741 
742  *index = vlib_buffer_pool_create (vm, (char *) name,
744  physmem_map_index);
745 
746  if (*index == (u8) ~ 0)
747  {
748  error = clib_error_return (0, "maximum number of buffer pools reached");
749  goto done;
750  }
751 
752 done:
753  vec_free (name);
754  return error;
755 }
756 
757 void
759 {
760  vlib_buffer_main_t *bm;
761 
762  if (vm->buffer_main)
763  return;
764 
765  vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
766  clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
768 }
769 
770 static u32
772 {
773  u32 cached = 0;
775 
776  clib_spinlock_lock (&bp->lock);
777 
778  /* *INDENT-OFF* */
779  vec_foreach (bpt, bp->threads)
780  cached += bpt->n_cached;
781  /* *INDENT-ON* */
782 
783  clib_spinlock_unlock (&bp->lock);
784 
785  return cached;
786 }
787 
788 static vlib_buffer_pool_t *
790 {
791  vlib_buffer_pool_t *bp;
792  if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index)
793  return 0;
794  bp = vec_elt_at_index (bm->buffer_pools, index);
795 
796  return bp;
797 }
798 
799 static void
801 {
802  vlib_main_t *vm = vlib_get_main ();
804  if (!bp)
805  return;
806 
807  e->value = bp->n_buffers - bp->n_avail - buffer_get_cached (bp);
808 }
809 
810 static void
812  u32 index)
813 {
814  vlib_main_t *vm = vlib_get_main ();
816  if (!bp)
817  return;
818 
819  e->value = bp->n_avail;
820 }
821 
822 static void
824 {
825  vlib_main_t *vm = vlib_get_main ();
827  if (!bp)
828  return;
829 
830  e->value = buffer_get_cached (bp);
831 }
832 
833 clib_error_t *
835 {
836  vlib_buffer_main_t *bm;
837  clib_error_t *err;
838  clib_bitmap_t *bmp = 0, *bmp_has_memory = 0;
839  u32 numa_node;
840  vlib_buffer_pool_t *bp;
841  u8 *name = 0, first_valid_buffer_pool_index = ~0;
842 
844 
845  bm = vm->buffer_main;
846  bm->log_default = vlib_log_register_class ("buffer", 0);
847  bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
848 
850 
851  if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
852  unformat_bitmap_list, &bmp)))
853  clib_error_free (err);
854 
855  if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U",
856  unformat_bitmap_list, &bmp_has_memory)))
857  clib_error_free (err);
858 
859  if (bmp && bmp_has_memory)
860  bmp = clib_bitmap_and (bmp, bmp_has_memory);
861 
862  /* no info from sysfs, assuming that only numa 0 exists */
863  if (bmp == 0)
864  bmp = clib_bitmap_set (bmp, 0, 1);
865 
867  clib_panic ("system have more than %u NUMA nodes",
869 
870  /* *INDENT-OFF* */
871  clib_bitmap_foreach (numa_node, bmp,
872  {
873  u8 *index = bm->default_buffer_pool_index_for_numa + numa_node;
874  index[0] = ~0;
875  if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index)))
876  {
877  clib_error_report (err);
878  clib_error_free (err);
879  continue;
880  }
881 
882  if (first_valid_buffer_pool_index == 0xff)
883  first_valid_buffer_pool_index = index[0];
884  });
885  /* *INDENT-ON* */
886 
887  if (first_valid_buffer_pool_index == (u8) ~ 0)
888  {
889  err = clib_error_return (0, "failed to allocate buffer pool(s)");
890  goto done;
891  }
892 
893  /* *INDENT-OFF* */
894  clib_bitmap_foreach (numa_node, bmp,
895  {
896  if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0)
897  bm->default_buffer_pool_index_for_numa[numa_node] =
898  first_valid_buffer_pool_index;
899  });
900  /* *INDENT-ON* */
901 
902  vec_foreach (bp, bm->buffer_pools)
903  {
904  if (bp->n_buffers == 0)
905  continue;
906 
907  vec_reset_length (name);
908  name = format (name, "/buffer-pools/%s/cached%c", bp->name, 0);
910  bp - bm->buffer_pools);
911 
912  vec_reset_length (name);
913  name = format (name, "/buffer-pools/%s/used%c", bp->name, 0);
915  bp - bm->buffer_pools);
916 
917  vec_reset_length (name);
918  name = format (name, "/buffer-pools/%s/available%c", bp->name, 0);
920  bp - bm->buffer_pools);
921  }
922 
923 done:
924  vec_free (bmp);
925  vec_free (bmp_has_memory);
926  vec_free (name);
927  return err;
928 }
929 
930 static clib_error_t *
932 {
933  vlib_buffer_main_t *bm;
934 
936 
937  bm = vm->buffer_main;
938 
940  {
941  if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
942  ;
943  else if (unformat (input, "default data-size %u",
944  &bm->default_data_size))
945  ;
946  else
947  return unformat_parse_error (input);
948  }
949 
950  unformat_free (input);
951  return 0;
952 }
953 
955 
956 #if VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0
957 u32
958 vlib_buffer_alloc_may_fail (vlib_main_t * vm, u32 n_buffers)
959 {
960  f64 r;
961 
963 
964  /* Fail this request? */
965  if (r > vm->buffer_alloc_success_rate)
966  n_buffers--;
967  /* 5% chance of returning nothing at all */
968  if (r > vm->buffer_alloc_success_rate && r > 0.95)
969  n_buffers = 0;
970 
971  return n_buffers;
972 }
973 #endif
974 
975 /** @endcond */
976 /*
977  * fd.io coding-style-patch-verification: ON
978  *
979  * Local Variables:
980  * eval: (c-set-style "gnu")
981  * End:
982  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:209
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
vlib_main_t vlib_global_main
Definition: main.c:1983
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
#define hash_set(h, key, value)
Definition: hash.h:255
#define vlib_log_warn(...)
Definition: log.h:106
#define clib_min(x, y)
Definition: clib.h:327
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:119
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
#define CLIB_CACHE_LINE_ROUND(x)
Definition: cache.h:61
a
Definition: bitmap.h:538
static clib_error_t * vlib_buffers_configure(vlib_main_t *vm, unformat_input_t *input)
Definition: buffer.c:931
void vlib_buffer_main_alloc(vlib_main_t *vm)
Definition: buffer.c:758
STATIC_ASSERT_OFFSET_OF(vlib_buffer_t, template_end, 64)
static void buffer_gauges_update_used_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: buffer.c:800
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:642
static void vlib_buffer_chain_increase_length(vlib_buffer_t *first, vlib_buffer_t *last, i32 len)
int vlib_buffer_add_data(vlib_main_t *vm, u32 *buffer_index, void *data, u32 n_data_bytes)
Definition: buffer.c:421
f64 buffer_alloc_success_rate
Definition: main.h:319
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
u32 physmem_map_index
Definition: buffer.h:430
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
#define CLIB_LOG2_CACHE_LINE_BYTES
Definition: cache.h:50
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
vlib_buffer_main_t * buffer_main
Definition: main.h:183
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:387
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:87
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:520
static u8 * format_vlib_buffer_pool(u8 *s, va_list *va)
Definition: buffer.c:615
static clib_error_t * show_buffers(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: buffer.c:642
static vlib_buffer_known_state_t vlib_buffer_is_known(vlib_main_t *vm, u32 buffer_index)
Definition: buffer_funcs.h:498
unsigned char u8
Definition: types.h:56
u8 default_buffer_pool_index_for_numa[VLIB_BUFFER_MAX_NUMA_NODES]
Definition: buffer.h:462
u8 data[128]
Definition: ipsec_types.api:89
static uword min_log2(uword x)
Definition: clib.h:161
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:133
uword vlib_buffer_length_in_chain_slow_path(vlib_main_t *vm, vlib_buffer_t *b_first)
Definition: buffer.c:78
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:205
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:668
vlib_buffer_pool_thread_t * threads
Definition: buffer.h:439
uint64_t value
u16 vlib_buffer_chain_append_data_with_alloc(vlib_main_t *vm, vlib_buffer_t *first, vlib_buffer_t **last, void *data, u16 data_len)
Definition: buffer.c:476
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:400
static void buffer_gauges_update_available_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: buffer.c:811
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
uword buffer_mem_size
Definition: buffer.h:453
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
#define clib_error_return(e, args...)
Definition: error.h:99
u8 * format_vlib_buffer_no_chain(u8 *s, va_list *args)
Definition: buffer.c:95
unsigned int u32
Definition: types.h:88
u8 * format_vlib_buffer(u8 *s, va_list *args)
Definition: buffer.c:125
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:293
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
#define hash_get(h, key)
Definition: hash.h:249
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
clib_error_t * vlib_physmem_shared_map_create(vlib_main_t *vm, char *name, uword size, u32 log2_page_sz, u32 numa_node, u32 *map_index)
Definition: physmem.c:42
void vlib_packet_template_init(vlib_main_t *vm, vlib_packet_template_t *t, void *packet_data, uword n_packet_data_bytes, uword min_n_buffers_each_alloc, char *fmt,...)
Definition: buffer.c:378
static uword clib_bitmap_last_set(uword *ai)
Return the higest numbered set bit in a bitmap.
Definition: bitmap.h:423
static_always_inline void vlib_buffer_copy_template(vlib_buffer_t *b, vlib_buffer_t *bt)
Definition: buffer_funcs.h:181
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
uword clib_mem_get_page_size(void)
Definition: mem.c:51
u8 data_len
Definition: ikev2_types.api:24
u32 size
Definition: vhost_user.h:106
vlib_buffer_t buffer_template
Definition: buffer.h:442
vec_header_t h
Definition: buffer.c:322
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define hash_free(h)
Definition: hash.h:310
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:454
u32 trace_handle
Specifies trace buffer handle if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:163
static u8 * vlib_validate_buffer_helper(vlib_main_t *vm, u32 bi, uword follow_buffer_next, uword **unique_hash)
Definition: buffer.c:205
static u32 buffer_get_cached(vlib_buffer_pool_t *bp)
Definition: buffer.c:771
clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
Definition: sysfs.c:50
vlib_log_class_t log_default
Definition: buffer.h:470
#define STATIC_ASSERT_FITS_IN(s, e, o)
u8 len
Definition: ip_types.api:92
static vlib_buffer_pool_t * buffer_get_by_index(vlib_buffer_main_t *bm, u32 index)
Definition: buffer.c:789
static_always_inline u32 vlib_buffer_get_default_data_size(vlib_main_t *vm)
Definition: buffer_funcs.h:96
clib_spinlock_t buffer_known_hash_lockp
Definition: buffer.h:461
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:226
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
clib_error_t * stat_segment_register_gauge(u8 *name, stat_segment_update_fn update_fn, u32 caller_index)
Definition: stat_segment.c:783
vlib_main_t * vm
Definition: buffer.c:323
u8 * format_vlib_buffer_contents(u8 *s, va_list *va)
Definition: buffer.c:188
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
u8 * vlib_validate_buffer(vlib_main_t *vm, u32 bi, uword follow_buffer_next)
Definition: buffer.c:254
#define VLIB_BUFFER_MAX_NUMA_NODES
Definition: buffer.h:445
#define clib_warning(format, args...)
Definition: error.h:59
u8 data[]
Packet data.
Definition: buffer.h:181
clib_spinlock_t lock
Definition: buffer.h:436
clib_error_t * vlib_buffer_main_init(struct vlib_main_t *vm)
Definition: buffer.c:834
u32 buffers_per_numa
Definition: buffer.h:465
#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA
Definition: buffer.c:51
vlib_buffer_known_state_t
Definition: buffer_funcs.h:482
string name[64]
Definition: ip.api:44
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
static uword vlib_buffer_alloc_size(uword ext_hdr_size, uword data_size)
Definition: buffer.c:508
static void buffer_gauges_update_cached_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: buffer.c:823
#define hash_set1(h, key)
Definition: hash.h:258
#define hash_create(elts, value_bytes)
Definition: hash.h:696
#define ASSERT(truth)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
static u8 * format_vlib_buffer_known_state(u8 *s, va_list *args)
Definition: buffer.c:160
u8 * format_vlib_buffer_and_data(u8 *s, va_list *args)
Definition: buffer.c:148
vector header structure
Definition: vec_bootstrap.h:55
#define clib_error_report(e)
Definition: error.h:113
static f64 random_f64(u32 *seed)
Generate f64 random number in the interval [0,1].
Definition: random.h:145
uword clib_mem_get_default_hugepage_size(void)
Definition: mem.c:57
static void * clib_mem_alloc(uword size)
Definition: mem.h:157
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define unformat_parse_error(input)
Definition: format.h:269
VLIB_WORKER_INIT_FUNCTION(vlib_buffer_worker_init)
static vlib_buffer_t * vlib_buffer_chain_buffer(vlib_main_t *vm, vlib_buffer_t *last, u32 next_bi)
u32 buffer_alloc_success_seed
Definition: main.h:318
#define VLIB_BUFFER_DEFAULT_DATA_SIZE
Definition: buffer.h:53
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
uword buffer_mem_start
Definition: buffer.h:452
uword * buffer_known_hash
Definition: buffer.h:460
u32 default_data_size
Definition: buffer.h:467
u8 * vlib_validate_buffers(vlib_main_t *vm, u32 *buffers, uword next_buffer_stride, uword n_buffers, vlib_buffer_known_state_t known_state, uword follow_buffer_next)
Definition: buffer.c:261
u32 index
Definition: flow_types.api:221
#define clib_error_free(e)
Definition: error.h:86
clib_error_t * vlib_buffer_worker_init(vlib_main_t *vm)
Definition: buffer.c:667
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:165
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1554
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
uword clib_bitmap_t
Definition: bitmap.h:50
#define vec_foreach(var, vec)
Vector iterator.
u8 vlib_buffer_pool_create(vlib_main_t *vm, char *name, u32 data_size, u32 physmem_map_index)
Definition: buffer.c:521
#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV
Definition: buffer.c:52
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
volatile u8 ref_count
Reference count for this buffer.
Definition: buffer.h:130
static uword * clib_bitmap_and(uword *ai, uword *bi)
Logical operator across two bitmaps.
static clib_error_t * vlib_buffer_main_init_numa_node(struct vlib_main_t *vm, u32 numa_node, u8 *index)
Definition: buffer.c:688
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
#define clib_panic(format, args...)
Definition: error.h:72
void vlib_buffer_validate_alloc_free(vlib_main_t *vm, u32 *buffers, uword n_buffers, vlib_buffer_known_state_t expected_state)
Definition: buffer.c:338
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
static __clib_warn_unused_result u32 vlib_buffer_alloc_from_pool(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u8 buffer_pool_index)
Allocate buffers from specific pool into supplied array.
Definition: buffer_funcs.h:566