FD.io VPP  v19.08-27-gf4dcae4
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 (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  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
122  {
124  u32 next_buffer = b->next_buffer;
125  b = vlib_get_buffer (vm, next_buffer);
126 
127  s =
128  format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
129  format_white_space, indent, next_buffer, b->current_length,
130  b->ref_count);
131  }
132 
133  return s;
134 }
135 
136 u8 *
137 format_vlib_buffer_and_data (u8 * s, va_list * args)
138 {
139  vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
140 
141  s = format (s, "%U, %U",
144 
145  return s;
146 }
147 
148 static u8 *
149 format_vlib_buffer_known_state (u8 * s, va_list * args)
150 {
152  char *t;
153 
154  switch (state)
155  {
156  case VLIB_BUFFER_UNKNOWN:
157  t = "unknown";
158  break;
159 
161  t = "known-allocated";
162  break;
163 
165  t = "known-free";
166  break;
167 
168  default:
169  t = "invalid";
170  break;
171  }
172 
173  return format (s, "%s", t);
174 }
175 
176 u8 *
177 format_vlib_buffer_contents (u8 * s, va_list * va)
178 {
179  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
180  vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
181 
182  while (1)
183  {
185  if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
186  break;
187  b = vlib_get_buffer (vm, b->next_buffer);
188  }
189 
190  return s;
191 }
192 
193 static u8 *
195  u32 bi,
196  uword follow_buffer_next, uword ** unique_hash)
197 {
199  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
200 
201  if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
202  return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
203 
204  if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
205  return format (0, "current data %d before pre-data", b->current_data);
206 
207  if (b->current_data + b->current_length >
209  return format (0, "%d-%d beyond end of buffer %d", b->current_data,
211 
212  if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
213  {
215  u8 *msg, *result;
216 
217  k = vlib_buffer_is_known (vm, b->next_buffer);
219  return format (0, "next 0x%x: %U",
221 
222  if (unique_hash)
223  {
224  if (hash_get (*unique_hash, b->next_buffer))
225  return format (0, "duplicate buffer 0x%x", b->next_buffer);
226 
227  hash_set1 (*unique_hash, b->next_buffer);
228  }
229 
230  msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
231  if (msg)
232  {
233  result = format (0, "next 0x%x: %v", b->next_buffer, msg);
234  vec_free (msg);
235  return result;
236  }
237  }
238 
239  return 0;
240 }
241 
242 u8 *
243 vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
244 {
245  return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
246  /* unique_hash */ 0);
247 }
248 
249 u8 *
251  u32 * buffers,
252  uword next_buffer_stride,
253  uword n_buffers,
254  vlib_buffer_known_state_t known_state,
255  uword follow_buffer_next)
256 {
257  uword i, *hash;
258  u32 bi, *b = buffers;
260  u8 *msg = 0, *result = 0;
261 
262  hash = hash_create (0, 0);
263  for (i = 0; i < n_buffers; i++)
264  {
265  bi = b[0];
266  b += next_buffer_stride;
267 
268  /* Buffer is not unique. */
269  if (hash_get (hash, bi))
270  {
271  msg = format (0, "not unique");
272  goto done;
273  }
274 
275  k = vlib_buffer_is_known (vm, bi);
276  if (k != known_state)
277  {
278  msg = format (0, "is %U; expected %U",
280  format_vlib_buffer_known_state, known_state);
281  goto done;
282  }
283 
284  msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
285  if (msg)
286  goto done;
287 
288  hash_set1 (hash, bi);
289  }
290 
291 done:
292  if (msg)
293  {
294  result = format (0, "0x%x: %v", bi, msg);
295  vec_free (msg);
296  }
297  hash_free (hash);
298  return result;
299 }
300 
301 /*
302  * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
303  * and vlib_mains[0] = &vlib_global_main from the beginning of time.
304  *
305  * The only place which should ever expand vlib_mains is start_workers()
306  * in threads.c. It knows about the bootstrap vector.
307  */
308 /* *INDENT-OFF* */
309 static struct
310 {
313 } __attribute__ ((packed)) __bootstrap_vlib_main_vector
314  __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
315 {
316  .h.len = 1,
317  .vm = &vlib_global_main,
318 };
319 /* *INDENT-ON* */
320 
321 vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
322 
323 
324 /* When debugging validate that given buffers are either known allocated
325  or known free. */
326 void
328  u32 * buffers,
329  uword n_buffers,
330  vlib_buffer_known_state_t expected_state)
331 {
333  u32 *b;
334  uword i, bi, is_free;
335 
336  if (CLIB_DEBUG == 0)
337  return;
338 
339  is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
340  b = buffers;
341  for (i = 0; i < n_buffers; i++)
342  {
344 
345  bi = b[0];
346  b += 1;
347  known = vlib_buffer_is_known (vm, bi);
348 
349  if (known == VLIB_BUFFER_UNKNOWN &&
350  expected_state == VLIB_BUFFER_KNOWN_FREE)
351  known = VLIB_BUFFER_KNOWN_FREE;
352 
353  if (known != expected_state)
354  {
355  clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
356  format_vlib_buffer_known_state, known, bi);
357  }
358 
363  }
364 }
365 
366 void
369  void *packet_data,
370  uword n_packet_data_bytes,
371  uword min_n_buffers_each_alloc, char *fmt, ...)
372 {
373  va_list va;
374 
375  va_start (va, fmt);
376  t->name = va_format (0, fmt, &va);
377  va_end (va);
378 
380 
381  clib_memset (t, 0, sizeof (t[0]));
382 
383  vec_add (t->packet_data, packet_data, n_packet_data_bytes);
384  t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
386 }
387 
388 void *
390  vlib_packet_template_t * t, u32 * bi_result)
391 {
392  u32 bi;
393  vlib_buffer_t *b;
394 
395  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
396  return 0;
397 
398  *bi_result = bi;
399 
400  b = vlib_get_buffer (vm, bi);
402  t->packet_data, vec_len (t->packet_data));
404 
405  return b->data;
406 }
407 
408 /* Append given data to end of buffer, possibly allocating new buffers. */
409 int
410 vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
411  u32 n_data_bytes)
412 {
413  u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
414  vlib_buffer_t *b;
415  void *d;
416 
417  bi = *buffer_index;
418  if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
419  goto out_of_buffers;
420 
421  d = data;
422  n_left = n_data_bytes;
423  n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
424 
425  b = vlib_get_buffer (vm, bi);
426  b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
427 
428  /* Get to the end of the chain before we try to append data... */
429  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
430  b = vlib_get_buffer (vm, b->next_buffer);
431 
432  while (1)
433  {
434  u32 n;
435 
436  ASSERT (n_buffer_bytes >= b->current_length);
437  n_left_this_buffer =
438  n_buffer_bytes - (b->current_data + b->current_length);
439  n = clib_min (n_left_this_buffer, n_left);
441  n);
442  b->current_length += n;
443  n_left -= n;
444  if (n_left == 0)
445  break;
446 
447  d += n;
448  if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
449  goto out_of_buffers;
450 
451  b->flags |= VLIB_BUFFER_NEXT_PRESENT;
452 
453  b = vlib_get_buffer (vm, b->next_buffer);
454  }
455 
456  *buffer_index = bi;
457  return 0;
458 
459 out_of_buffers:
460  clib_warning ("out of buffers");
461  return 1;
462 }
463 
464 u16
467  vlib_buffer_t ** last, void *data,
468  u16 data_len)
469 {
470  vlib_buffer_t *l = *last;
471  u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
472  u16 copied = 0;
473  ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
474  while (data_len)
475  {
476  u16 max = n_buffer_bytes - l->current_length - l->current_data;
477  if (max == 0)
478  {
479  if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
480  first->buffer_pool_index))
481  return copied;
482  *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
483  max = n_buffer_bytes - l->current_length - l->current_data;
484  }
485 
486  u16 len = (data_len > max) ? max : data_len;
488  data + copied, len);
489  vlib_buffer_chain_increase_length (first, l, len);
490  data_len -= len;
491  copied += len;
492  }
493  return copied;
494 }
495 
496 u8
498  u32 physmem_map_index)
499 {
501  vlib_buffer_pool_t *bp;
502  vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
503  uword start = pointer_to_uword (m->base);
504  uword size = (uword) m->n_pages << m->log2_page_size;
505  uword i, j;
506  u32 alloc_size, n_alloc_per_page;
507 
508  if (vec_len (bm->buffer_pools) >= 255)
509  return ~0;
510 
512 
513  if (bm->buffer_mem_size == 0)
514  {
515  bm->buffer_mem_start = start;
516  bm->buffer_mem_size = size;
517  }
518  else if (start < bm->buffer_mem_start)
519  {
520  bm->buffer_mem_size += bm->buffer_mem_start - start;
521  bm->buffer_mem_start = start;
522  if (size > bm->buffer_mem_size)
523  bm->buffer_mem_size = size;
524  }
525  else if (start > bm->buffer_mem_start)
526  {
527  uword new_size = start - bm->buffer_mem_start + size;
528  if (new_size > bm->buffer_mem_size)
529  bm->buffer_mem_size = new_size;
530  }
531 
532  if ((u64) bm->buffer_mem_size >
533  ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
534  {
535  clib_panic ("buffer memory size out of range!");
536  }
537 
538  bp->start = start;
539  bp->size = size;
540  bp->index = bp - bm->buffer_pools;
542  bp->buffer_template.ref_count = 1;
543  bp->physmem_map_index = physmem_map_index;
544  bp->name = format (0, "%s%c", name, 0);
545  bp->data_size = data_size;
546  bp->numa_node = m->numa_node;
547 
548  vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
550 
551  alloc_size = data_size + sizeof (vlib_buffer_t) + bm->ext_hdr_size;
552  n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
553 
554  /* preallocate buffer indices memory */
555  vec_validate_aligned (bp->buffers, m->n_pages * n_alloc_per_page,
558 
559  clib_spinlock_init (&bp->lock);
560 
561  for (j = 0; j < m->n_pages; j++)
562  for (i = 0; i < n_alloc_per_page; i++)
563  {
564  u8 *p;
565  u32 bi;
566 
567  p = m->base + (j << m->log2_page_size) + i * alloc_size;
568  p += bm->ext_hdr_size;
569 
571 
572  bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
573 
575  vlib_get_buffer (vm, bi);
576  }
577 
578  bp->n_buffers = vec_len (bp->buffers);
579  return bp->index;
580 }
581 
582 static u8 *
583 format_vlib_buffer_pool (u8 * s, va_list * va)
584 {
585  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
586  vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
588  u32 cached = 0;
589 
590  if (!bp)
591  return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
592  "Pool Name", "Index", "NUMA", "Size", "Data Size",
593  "Total", "Avail", "Cached", "Used");
594 
595  /* *INDENT-OFF* */
596  vec_foreach (bpt, bp->threads)
597  cached += vec_len (bpt->cached_buffers);
598  /* *INDENT-ON* */
599 
600  s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u",
601  bp->name, bp->index, bp->numa_node, bp->data_size +
602  sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size,
603  bp->data_size, bp->n_buffers, vec_len (bp->buffers), cached,
604  bp->n_buffers - vec_len (bp->buffers) - cached);
605 
606  return s;
607 }
608 
609 static clib_error_t *
611  unformat_input_t * input, vlib_cli_command_t * cmd)
612 {
614  vlib_buffer_pool_t *bp;
615 
616  vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, 0);
617 
618  /* *INDENT-OFF* */
619  vec_foreach (bp, bm->buffer_pools)
620  vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, bp);
621  /* *INDENT-ON* */
622 
623  return 0;
624 }
625 
626 /* *INDENT-OFF* */
627 VLIB_CLI_COMMAND (show_buffers_command, static) = {
628  .path = "show buffers",
629  .short_help = "Show packet buffer allocation",
630  .function = show_buffers,
631 };
632 /* *INDENT-ON* */
633 
634 clib_error_t *
636 {
638  vlib_buffer_pool_t *bp;
639 
640  /* *INDENT-OFF* */
641  vec_foreach (bp, bm->buffer_pools)
642  {
643  clib_spinlock_lock (&bp->lock);
644  vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
646  clib_spinlock_unlock (&bp->lock);
647  }
648  /* *INDENT-ON* */
649 
650  return 0;
651 }
652 
654 
655 static clib_error_t *
657  u8 * index)
658 {
660  clib_error_t *error;
661  u32 physmem_map_index;
662  uword n_pages, pagesize;
663  u32 buffers_per_numa;
664  u32 buffer_size = CLIB_CACHE_LINE_ROUND (bm->ext_hdr_size +
665  sizeof (vlib_buffer_t) +
667  (vm));
668  u8 *name;
669 
671  name = format (0, "buffers-numa-%d%c", numa_node, 0);
672 
673  buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
675 
676 retry:
677  n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
678  error = vlib_physmem_shared_map_create (vm, (char *) name,
679  n_pages * pagesize,
680  min_log2 (pagesize), numa_node,
681  &physmem_map_index);
682 
683  if (error && pagesize != clib_mem_get_page_size ())
684  {
685  vlib_log_warn (bm->log_default, "%U", format_clib_error, error);
686  clib_error_free (error);
687  vlib_log_warn (bm->log_default, "falling back to non-hugepage "
688  "backed buffer pool");
689  pagesize = clib_mem_get_page_size ();
690  buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
692  goto retry;
693  }
694 
695  if (error)
696  return error;
697 
698  vec_reset_length (name);
699  name = format (name, "default-numa-%d%c", numa_node, 0);
700 
701  *index = vlib_buffer_pool_create (vm, (char *) name,
703  physmem_map_index);
704 
705  if (*index == (u8) ~ 0)
706  return clib_error_return (0, "maximum number of buffer pools reached");
707 
708  return 0;
709 }
710 
711 void
713 {
714  vlib_buffer_main_t *bm;
715 
716  if (vm->buffer_main)
717  return;
718 
719  vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
720  clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
722 }
723 
724 static u32
726 {
727  u32 cached = 0;
729 
730  clib_spinlock_lock (&bp->lock);
731 
732  /* *INDENT-OFF* */
733  vec_foreach (bpt, bp->threads)
734  cached += vec_len (bpt->cached_buffers);
735  /* *INDENT-ON* */
736 
737  clib_spinlock_unlock (&bp->lock);
738 
739  return cached;
740 }
741 
742 static vlib_buffer_pool_t *
744 {
745  vlib_buffer_pool_t *bp;
746  if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index)
747  return 0;
748  bp = vec_elt_at_index (bm->buffer_pools, index);
749 
750  return bp;
751 }
752 
753 static void
755 {
758  if (!bp)
759  return;
760 
761  e->value = bp->n_buffers - vec_len (bp->buffers) - buffer_get_cached (bp);
762 }
763 
764 static void
766  u32 index)
767 {
770  if (!bp)
771  return;
772 
773  e->value = vec_len (bp->buffers);
774 }
775 
776 static void
778 {
781  if (!bp)
782  return;
783 
784  e->value = buffer_get_cached (bp);
785 }
786 
787 clib_error_t *
789 {
790  vlib_buffer_main_t *bm;
791  clib_error_t *err;
792  clib_bitmap_t *bmp = 0, *bmp_has_memory = 0;
793  u32 numa_node;
794  vlib_buffer_pool_t *bp;
795  u8 *name = 0, first_valid_buffer_pool_index = ~0;
796 
798 
799  bm = vm->buffer_main;
800  bm->log_default = vlib_log_register_class ("buffer", 0);
801  bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
802 
804 
805  if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
806  unformat_bitmap_list, &bmp)))
807  clib_error_free (err);
808 
809  if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U",
810  unformat_bitmap_list, &bmp_has_memory)))
811  clib_error_free (err);
812 
813  if (bmp && bmp_has_memory)
814  bmp = clib_bitmap_and (bmp, bmp_has_memory);
815 
816  /* no info from sysfs, assuming that only numa 0 exists */
817  if (bmp == 0)
818  bmp = clib_bitmap_set (bmp, 0, 1);
819 
821  clib_panic ("system have more than %u NUMA nodes",
823 
824  /* *INDENT-OFF* */
825  clib_bitmap_foreach (numa_node, bmp,
826  {
827  u8 *index = bm->default_buffer_pool_index_for_numa + numa_node;
828  index[0] = ~0;
829  if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index)))
830  {
831  clib_error_report (err);
832  clib_error_free (err);
833  continue;
834  }
835 
836  if (first_valid_buffer_pool_index == 0xff)
837  first_valid_buffer_pool_index = index[0];
838  });
839  /* *INDENT-ON* */
840 
841  if (first_valid_buffer_pool_index == (u8) ~ 0)
842  {
843  err = clib_error_return (0, "failed to allocate buffer pool(s)");
844  goto done;
845  }
846 
847  /* *INDENT-OFF* */
848  clib_bitmap_foreach (numa_node, bmp,
849  {
850  if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0)
851  bm->default_buffer_pool_index_for_numa[numa_node] =
852  first_valid_buffer_pool_index;
853  });
854  /* *INDENT-ON* */
855 
856  vec_foreach (bp, bm->buffer_pools)
857  {
858  if (bp->n_buffers == 0)
859  continue;
860 
861  vec_reset_length (name);
862  name = format (name, "/buffer-pools/%s/cached%c", bp->name, 0);
864  bp - bm->buffer_pools);
865 
866  vec_reset_length (name);
867  name = format (name, "/buffer-pools/%s/used%c", bp->name, 0);
869  bp - bm->buffer_pools);
870 
871  vec_reset_length (name);
872  name = format (name, "/buffer-pools/%s/available%c", bp->name, 0);
874  bp - bm->buffer_pools);
875  }
876 
877 done:
878  vec_free (bmp);
879  vec_free (bmp_has_memory);
880  vec_free (name);
881  return err;
882 }
883 
884 static clib_error_t *
886 {
887  vlib_buffer_main_t *bm;
888 
890 
891  bm = vm->buffer_main;
892 
894  {
895  if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
896  ;
897  else if (unformat (input, "default data-size %u",
898  &bm->default_data_size))
899  ;
900  else
901  return unformat_parse_error (input);
902  }
903 
904  unformat_free (input);
905  return 0;
906 }
907 
909 
910 
911 /** @endcond */
912 /*
913  * fd.io coding-style-patch-verification: ON
914  *
915  * Local Variables:
916  * eval: (c-set-style "gnu")
917  * End:
918  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:176
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:1937
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
Definition: stat_segment.h:62
#define hash_set(h, key, value)
Definition: hash.h:255
u32 flags
Definition: vhost_user.h:141
#define vlib_log_warn(...)
Definition: log.h:103
#define clib_min(x, y)
Definition: clib.h:295
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:100
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:78
#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:885
void vlib_buffer_main_alloc(vlib_main_t *vm)
Definition: buffer.c:712
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:754
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
#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:572
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:410
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
u32 physmem_map_index
Definition: buffer.h:428
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
u8 data[0]
Packet data.
Definition: buffer.h:181
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
int i
vlib_buffer_main_t * buffer_main
Definition: main.h:134
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
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
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
u8 data[128]
Definition: ipsec.api:249
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:450
static u8 * format_vlib_buffer_pool(u8 *s, va_list *va)
Definition: buffer.c:583
static clib_error_t * show_buffers(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: buffer.c:610
static vlib_buffer_known_state_t vlib_buffer_is_known(vlib_main_t *vm, u32 buffer_index)
Definition: buffer_funcs.h:462
unsigned char u8
Definition: types.h:56
u8 default_buffer_pool_index_for_numa[VLIB_BUFFER_MAX_NUMA_NODES]
Definition: buffer.h:459
static uword min_log2(uword x)
Definition: clib.h:144
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.
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:204
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:598
vlib_buffer_pool_thread_t * threads
Definition: buffer.h:436
#define vec_add1_aligned(V, E, A)
Add 1 element to end of vector (alignment specified).
Definition: vec.h:531
uint64_t value
Definition: stat_segment.h:68
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:465
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:389
static void buffer_gauges_update_available_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: buffer.c:765
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
uword buffer_mem_size
Definition: buffer.h:450
#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
vhost_vring_state_t state
Definition: vhost_user.h:146
unsigned int u32
Definition: types.h:88
u8 * format_vlib_buffer(u8 *s, va_list *args)
Definition: buffer.c:95
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:257
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:61
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
uword size
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:367
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:145
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
vlib_buffer_t buffer_template
Definition: buffer.h:439
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:451
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:194
static u32 buffer_get_cached(vlib_buffer_pool_t *bp)
Definition: buffer.c:725
u8 name[64]
Definition: memclnt.api:152
clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
Definition: sysfs.c:50
vlib_log_class_t log_default
Definition: buffer.h:467
#define STATIC_ASSERT_FITS_IN(s, e, o)
u8 len
Definition: ip_types.api:90
static vlib_buffer_pool_t * buffer_get_by_index(vlib_buffer_main_t *bm, u32 index)
Definition: buffer.c:743
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:458
#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:732
vlib_main_t * vm
Definition: buffer.c:312
vec_header_t h
Definition: buffer.c:311
u8 * format_vlib_buffer_contents(u8 *s, va_list *va)
Definition: buffer.c:177
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
u8 * vlib_validate_buffer(vlib_main_t *vm, u32 bi, uword follow_buffer_next)
Definition: buffer.c:243
#define VLIB_BUFFER_MAX_NUMA_NODES
Definition: buffer.h:442
#define clib_warning(format, args...)
Definition: error.h:59
clib_spinlock_t lock
Definition: buffer.h:433
clib_error_t * vlib_buffer_main_init(struct vlib_main_t *vm)
Definition: buffer.c:788
u32 buffers_per_numa
Definition: buffer.h:462
#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA
Definition: buffer.c:51
vlib_buffer_known_state_t
Definition: buffer_funcs.h:446
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
static void buffer_gauges_update_cached_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: buffer.c:777
#define hash_set1(h, key)
Definition: hash.h:258
#define hash_create(elts, value_bytes)
Definition: hash.h:696
static 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:523
#define ASSERT(truth)
static u8 * format_vlib_buffer_known_state(u8 *s, va_list *args)
Definition: buffer.c:149
u8 * format_vlib_buffer_and_data(u8 *s, va_list *args)
Definition: buffer.c:137
vector header structure
Definition: vec_bootstrap.h:55
#define clib_error_report(e)
Definition: error.h:113
uword clib_mem_get_default_hugepage_size(void)
Definition: mem.c:57
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
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)
#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:449
uword * buffer_known_hash
Definition: buffer.h:457
u32 default_data_size
Definition: buffer.h:464
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:250
#define clib_error_free(e)
Definition: error.h:86
clib_error_t * vlib_buffer_worker_init(vlib_main_t *vm)
Definition: buffer.c:635
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1487
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:497
#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
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:612
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:768
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:656
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:327
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