FD.io VPP  v18.01-8-g0eacf49
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 <vlib/vlib.h>
47 #include <vlib/unix/unix.h>
48 
49 vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0;
50 static u32 vlib_buffer_physmem_sz = 32 << 20;
51 
52 uword
54  vlib_buffer_t * b_first)
55 {
56  vlib_buffer_t *b = b_first;
57  uword l_first = b_first->current_length;
58  uword l = 0;
59  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
60  {
61  b = vlib_get_buffer (vm, b->next_buffer);
62  l += b->current_length;
63  }
66  return l + l_first;
67 }
68 
69 u8 *
70 format_vlib_buffer (u8 * s, va_list * args)
71 {
72  vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
73  u32 indent = format_get_indent (s);
74 
75  s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
78 
80  s = format (s, ", totlen-nifb %d",
82 
84  s = format (s, ", trace 0x%x", b->trace_index);
85 
86  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
87  {
89  u32 next_buffer = b->next_buffer;
90  b = vlib_get_buffer (vm, next_buffer);
91 
92  s =
93  format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
94  format_white_space, indent, next_buffer, b->current_length,
95  b->n_add_refs);
96  }
97 
98  return s;
99 }
100 
101 u8 *
102 format_vlib_buffer_and_data (u8 * s, va_list * args)
103 {
104  vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
105 
106  s = format (s, "%U, %U",
109 
110  return s;
111 }
112 
113 static u8 *
114 format_vlib_buffer_known_state (u8 * s, va_list * args)
115 {
117  char *t;
118 
119  switch (state)
120  {
121  case VLIB_BUFFER_UNKNOWN:
122  t = "unknown";
123  break;
124 
126  t = "known-allocated";
127  break;
128 
130  t = "known-free";
131  break;
132 
133  default:
134  t = "invalid";
135  break;
136  }
137 
138  return format (s, "%s", t);
139 }
140 
141 u8 *
142 format_vlib_buffer_contents (u8 * s, va_list * va)
143 {
144  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
145  vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
146 
147  while (1)
148  {
150  if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
151  break;
152  b = vlib_get_buffer (vm, b->next_buffer);
153  }
154 
155  return s;
156 }
157 
158 static u8 *
160  u32 bi,
161  uword follow_buffer_next, uword ** unique_hash)
162 {
163  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
166 
169  return format (0, "unknown free list 0x%x",
171 
172  fl =
175 
176  if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
177  return format (0, "current data %d before pre-data", b->current_data);
178 
179  if (b->current_data + b->current_length > fl->n_data_bytes)
180  return format (0, "%d-%d beyond end of buffer %d",
182 
183  if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
184  {
186  u8 *msg, *result;
187 
190  return format (0, "next 0x%x: %U",
192 
193  if (unique_hash)
194  {
195  if (hash_get (*unique_hash, b->next_buffer))
196  return format (0, "duplicate buffer 0x%x", b->next_buffer);
197 
198  hash_set1 (*unique_hash, b->next_buffer);
199  }
200 
201  msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
202  if (msg)
203  {
204  result = format (0, "next 0x%x: %v", b->next_buffer, msg);
205  vec_free (msg);
206  return result;
207  }
208  }
209 
210  return 0;
211 }
212 
213 u8 *
214 vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
215 {
216  return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
217  /* unique_hash */ 0);
218 }
219 
220 u8 *
222  u32 * buffers,
223  uword next_buffer_stride,
224  uword n_buffers,
225  vlib_buffer_known_state_t known_state,
226  uword follow_buffer_next)
227 {
228  uword i, *hash;
229  u32 bi, *b = buffers;
231  u8 *msg = 0, *result = 0;
232 
233  hash = hash_create (0, 0);
234  for (i = 0; i < n_buffers; i++)
235  {
236  bi = b[0];
237  b += next_buffer_stride;
238 
239  /* Buffer is not unique. */
240  if (hash_get (hash, bi))
241  {
242  msg = format (0, "not unique");
243  goto done;
244  }
245 
246  k = vlib_buffer_is_known (bi);
247  if (k != known_state)
248  {
249  msg = format (0, "is %U; expected %U",
251  format_vlib_buffer_known_state, known_state);
252  goto done;
253  }
254 
255  msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
256  if (msg)
257  goto done;
258 
259  hash_set1 (hash, bi);
260  }
261 
262 done:
263  if (msg)
264  {
265  result = format (0, "0x%x: %v", bi, msg);
266  vec_free (msg);
267  }
268  hash_free (hash);
269  return result;
270 }
271 
272 /*
273  * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
274  * and vlib_mains[0] = &vlib_global_main from the beginning of time.
275  *
276  * The only place which should ever expand vlib_mains is start_workers()
277  * in threads.c. It knows about the bootstrap vector.
278  */
279 /* *INDENT-OFF* */
280 static struct
281 {
284 } __attribute__ ((packed)) __bootstrap_vlib_main_vector
285  __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
286 {
287  .h.len = 1,
288  .vm = &vlib_global_main,
289 };
290 /* *INDENT-ON* */
291 
292 vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
293 
294 
295 /* When dubugging validate that given buffers are either known allocated
296  or known free. */
297 void
299  u32 * buffers,
300  uword n_buffers,
301  vlib_buffer_known_state_t expected_state)
302 {
303  u32 *b;
304  uword i, bi, is_free;
305 
306  if (CLIB_DEBUG == 0)
307  return;
308 
309  if (vlib_buffer_callbacks)
310  return;
311 
312  is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
313  b = buffers;
314  for (i = 0; i < n_buffers; i++)
315  {
317 
318  bi = b[0];
319  b += 1;
320  known = vlib_buffer_is_known (bi);
321  if (known != expected_state)
322  {
323  ASSERT (0);
325  (vm, "%s %U buffer 0x%x",
326  is_free ? "freeing" : "allocating",
327  format_vlib_buffer_known_state, known, bi);
328  }
329 
332  }
333 }
334 
335 void
338 {
339  uword l;
340  u32 *d;
341 
342  l = vec_len (src->buffers);
343  if (l > 0)
344  {
346  clib_memcpy (d, src->buffers, l * sizeof (d[0]));
347  vec_free (src->buffers);
348  }
349 }
350 
351 /* Add buffer free list. */
352 static u32
354  u32 n_data_bytes,
355  u32 is_public, u32 is_default, u8 * name)
356 {
359  int i;
360 
361  ASSERT (vlib_get_thread_index () == 0);
362 
363  if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0)
364  {
365  u32 default_free_free_list_index;
366 
367  /* *INDENT-OFF* */
368  default_free_free_list_index =
370  (vm,
371  /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
372  /* is_public */ 1,
373  /* is_default */ 1,
374  (u8 *) "default");
375  /* *INDENT-ON* */
376  ASSERT (default_free_free_list_index ==
378 
379  if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
380  return default_free_free_list_index;
381  }
382 
384 
385  memset (f, 0, sizeof (f[0]));
386  f->index = f - bm->buffer_free_list_pool;
387  f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
389  f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
390 
391  /* Setup free buffer template. */
394 
395  if (is_public)
396  {
398  if (!p)
400  }
401 
403 
404  for (i = 1; i < vec_len (vlib_mains); i++)
405  {
406  vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
410  ASSERT (f - bm->buffer_free_list_pool ==
411  wf - wbm->buffer_free_list_pool);
412  wf[0] = f[0];
413  wf->buffers = 0;
414  wf->n_alloc = 0;
415  }
416 
417  return f->index;
418 }
419 
420 u32
422  char *fmt, ...)
423 {
424  va_list va;
425  u8 *name;
426 
427  va_start (va, fmt);
428  name = va_format (0, fmt, &va);
429  va_end (va);
430 
431  return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
432  /* is_public */ 0,
433  /* is_default */ 0,
434  name);
435 }
436 
437 u32
439  char *fmt, ...)
440 {
441  u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
442 
443  if (i == ~0)
444  {
445  va_list va;
446  u8 *name;
447 
448  va_start (va, fmt);
449  name = va_format (0, fmt, &va);
450  va_end (va);
451 
452  i = vlib_buffer_create_free_list_helper (vm, n_data_bytes,
453  /* is_public */ 1,
454  /* is_default */ 0,
455  name);
456  }
457 
458  return i;
459 }
460 
461 static void
463 {
464  u32 i;
465 
466  for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
469  vec_free (f->name);
471  vec_free (f->buffers);
472 }
473 
474 /* Add buffer free list. */
475 void
477 {
480  u32 merge_index;
481  int i;
482 
483  ASSERT (vlib_get_thread_index () == 0);
484 
485  f = vlib_buffer_get_free_list (vm, free_list_index);
486 
487  ASSERT (vec_len (f->buffers) == f->n_alloc);
488  merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes);
489  if (merge_index != ~0 && merge_index != free_list_index)
490  {
492  (bm->buffer_free_list_pool, merge_index),
493  f);
494  }
495 
496  del_free_list (vm, f);
497 
498  /* Poison it. */
499  memset (f, 0xab, sizeof (f[0]));
500 
502 
503  for (i = 1; i < vec_len (vlib_mains); i++)
504  {
505  bm = vlib_mains[i]->buffer_main;
506  f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);;
507  memset (f, 0xab, sizeof (f[0]));
509  }
510 }
511 
512 /* Make sure free list has at least given number of free buffers. */
513 static uword
516  uword min_free_buffers)
517 {
518  vlib_buffer_t *buffers, *b;
520  int n, n_bytes, i;
521  u32 *bi;
522  u32 n_remaining, n_alloc, n_this_chunk;
523 
524  /* Already have enough free buffers on free list? */
525  n = min_free_buffers - vec_len (fl->buffers);
526  if (n <= 0)
527  return min_free_buffers;
528 
529  mfl = vlib_buffer_get_free_list (vlib_mains[0], fl->index);
530  if (vec_len (mfl->global_buffers) > 0)
531  {
532  int n_copy, n_left;
534  n_copy = clib_min (vec_len (mfl->global_buffers), n);
535  n_left = vec_len (mfl->global_buffers) - n_copy;
536  vec_add_aligned (fl->buffers, mfl->global_buffers + n_left, n_copy,
538  _vec_len (mfl->global_buffers) = n_left;
540  n = min_free_buffers - vec_len (fl->buffers);
541  if (n <= 0)
542  return min_free_buffers;
543  }
544 
545  /* Always allocate round number of buffers. */
546  n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
547 
548  /* Always allocate new buffers in reasonably large sized chunks. */
550 
551  n_remaining = n;
552  n_alloc = 0;
553  while (n_remaining > 0)
554  {
555  n_this_chunk = clib_min (n_remaining, 16);
556 
557  n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
558 
559  /* drb: removed power-of-2 ASSERT */
560  buffers =
561  vm->os_physmem_alloc_aligned (vm,
562  vm->buffer_main->
563  buffer_pools[0].physmem_region, n_bytes,
564  sizeof (vlib_buffer_t));
565  if (!buffers)
566  return n_alloc;
567 
568  /* Record chunk as being allocated so we can free it later. */
569  vec_add1 (fl->buffer_memory_allocated, buffers);
570 
571  fl->n_alloc += n_this_chunk;
572  n_alloc += n_this_chunk;
573  n_remaining -= n_this_chunk;
574 
575  b = buffers;
576  vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
577  for (i = 0; i < n_this_chunk; i++)
578  {
579  bi[i] = vlib_get_buffer_index (vm, b);
580 
581  if (CLIB_DEBUG > 0)
584  }
585 
586  memset (buffers, 0, n_bytes);
587 
588  /* Initialize all new buffers. */
589  b = buffers;
590  for (i = 0; i < n_this_chunk; i++)
591  {
594  }
595 
596  if (fl->buffer_init_function)
597  fl->buffer_init_function (vm, fl, bi, n_this_chunk);
598  }
599  return n_alloc;
600 }
601 
602 void *
604 {
606  void *rv = bm->buffer_free_callback;
607 
608  bm->buffer_free_callback = fp;
609  return rv;
610 }
611 
614  u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
615 {
618  u32 fi;
619  int i;
620  u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
621  u32 follow_buffer_next);
622 
623  cb = bm->buffer_free_callback;
624 
625  if (PREDICT_FALSE (cb != 0))
626  n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
627 
628  if (!n_buffers)
629  return;
630 
631  for (i = 0; i < n_buffers; i++)
632  {
633  vlib_buffer_t *b;
634  u32 bi = buffers[i];
635 
636  b = vlib_get_buffer (vm, bi);
638  fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
639 
640  /* The only current use of this callback: multicast recycle */
642  {
643  int j;
644 
646  (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
647 
648  for (j = 0; j < vec_len (bm->announce_list); j++)
649  {
650  if (fl == bm->announce_list[j])
651  goto already_announced;
652  }
653  vec_add1 (bm->announce_list, fl);
654  already_announced:
655  ;
656  }
657  else
658  {
659  if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
660  {
661  u32 flags, next;
662 
663  do
664  {
665  vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
666  flags = nb->flags;
667  next = nb->next_buffer;
668  if (nb->n_add_refs)
669  nb->n_add_refs--;
670  else
671  {
674  vlib_buffer_add_to_free_list (vm, fl, bi, 1);
675  }
676  bi = next;
677  }
678  while (follow_buffer_next
679  && (flags & VLIB_BUFFER_NEXT_PRESENT));
680 
681  }
682  }
683  }
684  if (vec_len (bm->announce_list))
685  {
687  for (i = 0; i < vec_len (bm->announce_list); i++)
688  {
689  fl = bm->announce_list[i];
691  }
692  _vec_len (bm->announce_list) = 0;
693  }
694 }
695 
696 static void
697 vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
698 {
699  vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
700  1);
701 }
702 
703 static void
705  u32 n_buffers)
706 {
707  vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
708  0);
709 }
710 
711 /* Copy template packet data into buffers as they are allocated. */
712 static void __attribute__ ((unused))
715  u32 * buffers, u32 n_buffers)
716 {
718  uword_to_pointer (fl->buffer_init_function_opaque,
720  uword i;
721 
722  for (i = 0; i < n_buffers; i++)
723  {
724  vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
727  b->current_length);
728  }
729 }
730 
731 void
734  void *packet_data,
735  uword n_packet_data_bytes,
736  uword min_n_buffers_each_physmem_alloc,
737  char *fmt, ...)
738 {
740  va_list va;
741  u8 *name;
743 
744  va_start (va, fmt);
745  name = va_format (0, fmt, &va);
746  va_end (va);
747 
749  bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
750  n_packet_data_bytes,
751  min_n_buffers_each_physmem_alloc,
752  name);
753 
755 
756  memset (t, 0, sizeof (t[0]));
757 
758  vec_add (t->packet_data, packet_data, n_packet_data_bytes);
759  t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
760 
762  (vm, n_packet_data_bytes,
763  /* is_public */ 1,
764  /* is_default */ 0,
765  name);
766 
767  ASSERT (t->free_list_index != 0);
770 
773 
775  fl->buffer_init_template.current_length = n_packet_data_bytes;
776  fl->buffer_init_template.flags = 0;
779 }
780 
781 void *
783  vlib_packet_template_t * t, u32 * bi_result)
784 {
785  u32 bi;
786  vlib_buffer_t *b;
787 
788  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
789  return 0;
790 
791  *bi_result = bi;
792 
793  b = vlib_get_buffer (vm, bi);
795  t->packet_data, vec_len (t->packet_data));
797 
798  return b->data;
799 }
800 
801 void
804 {
806  word l = vec_len (t->packet_data);
807  word n_alloc;
808 
809  ASSERT (l > 0);
810  ASSERT (vec_len (t->free_buffers) == 0);
811 
812  vec_validate (t->free_buffers, n - 1);
814  n, t->free_list_index);
815  _vec_len (t->free_buffers) = n_alloc;
816 }
817 
818 /* Append given data to end of buffer, possibly allocating new buffers. */
819 u32
821  u32 free_list_index,
822  u32 buffer_index, void *data, u32 n_data_bytes)
823 {
824  u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
825  vlib_buffer_t *b;
826  void *d;
827 
828  bi = buffer_index;
829  if (bi == 0
830  && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
831  goto out_of_buffers;
832 
833  d = data;
834  n_left = n_data_bytes;
835  n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
836 
837  b = vlib_get_buffer (vm, bi);
839 
840  /* Get to the end of the chain before we try to append data... */
841  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
842  b = vlib_get_buffer (vm, b->next_buffer);
843 
844  while (1)
845  {
846  u32 n;
847 
848  ASSERT (n_buffer_bytes >= b->current_length);
849  n_left_this_buffer =
850  n_buffer_bytes - (b->current_data + b->current_length);
851  n = clib_min (n_left_this_buffer, n_left);
853  b->current_length += n;
854  n_left -= n;
855  if (n_left == 0)
856  break;
857 
858  d += n;
859  if (1 !=
861  free_list_index))
862  goto out_of_buffers;
863 
865 
866  b = vlib_get_buffer (vm, b->next_buffer);
867  }
868 
869  return bi;
870 
871 out_of_buffers:
872  clib_error ("out of buffers");
873  return bi;
874 }
875 
876 u16
878  u32 free_list_index,
880  vlib_buffer_t ** last,
881  void *data, u16 data_len)
882 {
883  vlib_buffer_t *l = *last;
884  u32 n_buffer_bytes =
885  vlib_buffer_free_list_buffer_size (vm, free_list_index);
886  u16 copied = 0;
887  ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
888  while (data_len)
889  {
890  u16 max = n_buffer_bytes - l->current_length - l->current_data;
891  if (max == 0)
892  {
893  if (1 !=
895  free_list_index))
896  return copied;
897  *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
898  max = n_buffer_bytes - l->current_length - l->current_data;
899  }
900 
901  u16 len = (data_len > max) ? max : data_len;
903  data + copied, len);
904  vlib_buffer_chain_increase_length (first, l, len);
905  data_len -= len;
906  copied += len;
907  }
908  return copied;
909 }
910 
911 u8
914 {
918  uword start = pointer_to_uword (pr->mem);
919  uword size = pr->size;
920 
921  if (bm->buffer_mem_size == 0)
922  {
923  bm->buffer_mem_start = start;
924  bm->buffer_mem_size = size;
925  }
926  else if (start < bm->buffer_mem_start)
927  {
928  bm->buffer_mem_size += bm->buffer_mem_start - start;
929  bm->buffer_mem_start = start;
930  if (size > bm->buffer_mem_size)
931  bm->buffer_mem_size = size;
932  }
933  else if (start > bm->buffer_mem_start)
934  {
935  uword new_size = start - bm->buffer_mem_start + size;
936  if (new_size > bm->buffer_mem_size)
937  bm->buffer_mem_size = new_size;
938  }
939 
940  if ((u64) bm->buffer_mem_size >
941  ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
942  {
943  clib_panic ("buffer memory size out of range!");
944  }
945 
946  vec_add2 (bm->buffer_pools, p, 1);
947  p->start = start;
948  p->size = size;
949  p->physmem_region = pri;
950  return p - bm->buffer_pools;
951 }
952 
953 static u8 *
954 format_vlib_buffer_free_list (u8 * s, va_list * va)
955 {
957  u32 threadnum = va_arg (*va, u32);
958  uword bytes_alloc, bytes_free, n_free, size;
959 
960  if (!f)
961  return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
962  "Thread", "Name", "Index", "Size", "Alloc", "Free",
963  "#Alloc", "#Free");
964 
965  size = sizeof (vlib_buffer_t) + f->n_data_bytes;
966  n_free = vec_len (f->buffers);
967  bytes_alloc = size * f->n_alloc;
968  bytes_free = size * n_free;
969 
970  s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
971  f->name, f->index, f->n_data_bytes,
972  format_memory_size, bytes_alloc,
973  format_memory_size, bytes_free, f->n_alloc, n_free);
974 
975  return s;
976 }
977 
978 static clib_error_t *
980  unformat_input_t * input, vlib_cli_command_t * cmd)
981 {
982  vlib_buffer_main_t *bm;
984  vlib_main_t *curr_vm;
985  u32 vm_index = 0;
986 
988 
989  do
990  {
991  curr_vm = vlib_mains[vm_index];
992  bm = curr_vm->buffer_main;
993 
994  /* *INDENT-OFF* */
996  vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
997  }));
998  /* *INDENT-ON* */
999 
1000  vm_index++;
1001  }
1002  while (vm_index < vec_len (vlib_mains));
1003 
1004  return 0;
1005 }
1006 
1007 /* *INDENT-OFF* */
1008 VLIB_CLI_COMMAND (show_buffers_command, static) = {
1009  .path = "show buffers",
1010  .short_help = "Show packet buffer allocation",
1011  .function = show_buffers,
1012 };
1013 /* *INDENT-ON* */
1014 
1015 clib_error_t *
1017 {
1018  vlib_buffer_main_t *bm;
1020  clib_error_t *error;
1021 
1022  vec_validate (vm->buffer_main, 0);
1023  bm = vm->buffer_main;
1024 
1025  if (vlib_buffer_callbacks)
1026  {
1027  /* external plugin has registered own buffer callbacks
1028  so we just copy them and quit */
1029  vlib_buffer_main_t *bm = vm->buffer_main;
1030  clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1031  sizeof (vlib_buffer_callbacks_t));
1032  bm->callbacks_registered = 1;
1033  return 0;
1034  }
1035 
1042 
1043  /* allocate default region */
1044  error = vlib_physmem_region_alloc (vm, "buffers",
1047 
1048  if (error == 0)
1049  goto done;
1050 
1051  clib_error_free (error);
1052 
1053  /* we my be running unpriviledged, so try to allocate fake physmem */
1054  error = vlib_physmem_region_alloc (vm, "buffers (fake)",
1058 done:
1060  return error;
1061 }
1062 
1063 static clib_error_t *
1065 {
1066  u32 size_in_mb;
1067 
1068  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1069  {
1070  if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1071  vlib_buffer_physmem_sz = size_in_mb << 20;
1072  else
1073  return unformat_parse_error (input);
1074  }
1075 
1076  unformat_free (input);
1077  return 0;
1078 }
1079 
1081 
1082 
1083 /** @endcond */
1084 /*
1085  * fd.io coding-style-patch-verification: ON
1086  *
1087  * Local Variables:
1088  * eval: (c-set-style "gnu")
1089  * End:
1090  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
vlib_physmem_region_index_t physmem_region
Definition: buffer.h:413
vlib_main_t vlib_global_main
Definition: main.c:1637
#define hash_set(h, key, value)
Definition: hash.h:254
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
void vlib_buffer_merge_free_lists(vlib_buffer_free_list_t *dst, vlib_buffer_free_list_t *src)
Definition: buffer.c:336
#define clib_min(x, y)
Definition: clib.h:340
#define VLIB_PHYSMEM_F_INIT_MHEAP
Definition: physmem.h:57
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:72
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:50
#define VLIB_BUFFER_RECYCLE
Definition: buffer.h:99
static clib_error_t * vlib_buffers_configure(vlib_main_t *vm, unformat_input_t *input)
Definition: buffer.c:1064
#define PREDICT_TRUE(x)
Definition: clib.h:106
#define clib_error(format, args...)
Definition: error.h:62
vlib_buffer_callbacks_t cb
Definition: buffer.h:452
static vlib_buffer_t * vlib_buffer_chain_buffer(vlib_main_t *vm, vlib_buffer_t *first, vlib_buffer_t *last, u32 next_bi)
Definition: buffer_funcs.h:678
#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:569
static void vlib_buffer_chain_increase_length(vlib_buffer_t *first, vlib_buffer_t *last, i32 len)
Definition: buffer_funcs.h:695
vlib_buffer_t buffer_init_template
Definition: buffer.h:337
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:52
#define CLIB_LOG2_CACHE_LINE_BYTES
Definition: cache.h:49
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
clib_spinlock_t global_buffers_lock
Definition: buffer.h:359
static clib_error_t * vlib_physmem_region_alloc(vlib_main_t *vm, char *name, u32 size, u8 numa_node, u32 flags, vlib_physmem_region_index_t *idx)
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:557
void ** buffer_memory_allocated
Definition: buffer.h:364
vlib_buffer_main_t * buffer_main
Definition: main.h:106
static u32 format_get_indent(u8 *s)
Definition: format.h:72
static void vlib_buffer_free_no_next_internal(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.c:704
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:387
u32 min_n_buffers_each_physmem_alloc
Definition: buffer.h:347
static clib_error_t * show_buffers(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: buffer.c:979
vlib_main_t ** vlib_mains
Definition: buffer.c:292
uword vlib_buffer_length_in_chain_slow_path(vlib_main_t *vm, vlib_buffer_t *b_first)
Definition: buffer.c:53
void vlib_packet_template_get_packet_helper(vlib_main_t *vm, vlib_packet_template_t *t)
Definition: buffer.c:802
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:212
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:595
void vlib_buffer_delete_free_list_internal(vlib_main_t *vm, u32 free_list_index)
Definition: buffer.c:476
static u8 * format_vlib_buffer_free_list(u8 *s, va_list *va)
Definition: buffer.c:954
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:68
#define static_always_inline
Definition: clib.h:93
static u32 vlib_buffer_physmem_sz
Definition: buffer.c:50
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:782
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:438
void(* buffer_init_function)(struct vlib_main_t *vm, struct vlib_buffer_free_list_t *fl, u32 *buffers, u32 n_buffers)
Definition: buffer.h:371
void(* os_physmem_free)(struct vlib_main_t *vm, vlib_physmem_region_index_t idx, void *x)
Definition: main.h:125
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:97
void *(* os_physmem_alloc_aligned)(struct vlib_main_t *vm, vlib_physmem_region_index_t idx, uword n_bytes, uword alignment)
Definition: main.h:122
uword buffer_mem_size
Definition: buffer.h:422
u8 * format_memory_size(u8 *s, va_list *va)
Definition: std-formats.c:193
vlib_buffer_free_no_next_cb_t * vlib_buffer_free_no_next_cb
Definition: buffer.h:395
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:435
unsigned long u64
Definition: types.h:89
static vlib_buffer_t * vlib_buffer_next_contiguous(vlib_buffer_t *b, u32 buffer_bytes)
Definition: buffer.h:174
u8 * format_vlib_buffer(u8 *s, va_list *args)
Definition: buffer.c:70
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:74
#define fl(x, y)
static vlib_physmem_region_t * vlib_physmem_get_region(vlib_main_t *vm, u8 index)
Definition: physmem_funcs.h:44
int callbacks_registered
Definition: buffer.h:453
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:33
vlib_buffer_free_list_t ** announce_list
Definition: buffer.h:449
static heap_elt_t * first(heap_header_t *h)
Definition: heap.c:59
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
void * vlib_set_buffer_free_callback(vlib_main_t *vm, void *fp)
Definition: buffer.c:603
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:72
static u32 vlib_buffer_get_free_list_with_size(vlib_main_t *vm, u32 size)
Definition: buffer_funcs.h:430
static u32 vlib_buffer_create_free_list_helper(vlib_main_t *vm, u32 n_data_bytes, u32 is_public, u32 is_default, u8 *name)
Definition: buffer.c:353
struct _unformat_input_t unformat_input_t
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:195
#define hash_free(h)
Definition: hash.h:309
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:434
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:271
void(* vlib_buffer_delete_free_list_cb)(struct vlib_main_t *vm, u32 free_list_index)
Definition: buffer.h:402
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:423
static u8 * vlib_validate_buffer_helper(vlib_main_t *vm, u32 bi, uword follow_buffer_next, uword **unique_hash)
Definition: buffer.c:159
#define PREDICT_FALSE(x)
Definition: clib.h:105
static u32 vlib_buffer_free_list_buffer_size(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:465
#define VLIB_FRAME_SIZE
Definition: node.h:328
static void vlib_packet_template_buffer_init(vlib_main_t *vm, vlib_buffer_free_list_t *fl, u32 *buffers, u32 n_buffers)
Definition: buffer.c:713
static void vlib_buffer_add_to_free_list(vlib_main_t *vm, vlib_buffer_free_list_t *f, u32 buffer_index, u8 do_init)
Definition: buffer_funcs.h:848
#define uword_to_pointer(u, type)
Definition: types.h:136
static void vlib_buffer_set_known_state(u32 buffer_index, vlib_buffer_known_state_t state)
Definition: buffer_funcs.h:236
u16 vlib_buffer_chain_append_data_with_alloc(vlib_main_t *vm, u32 free_list_index, vlib_buffer_t *first, vlib_buffer_t **last, void *data, u16 data_len)
Definition: buffer.c:877
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:188
#define vec_add_aligned(V, E, N, A)
Add N elements to end of vector V (no header, specified alignment)
Definition: vec.h:605
static u32 vlib_buffer_get_free_list_index(vlib_buffer_t *b)
Definition: buffer_funcs.h:258
clib_spinlock_t buffer_known_hash_lockp
Definition: buffer.h:446
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:140
vlib_buffer_fill_free_list_cb_t * vlib_buffer_fill_free_list_cb
Definition: buffer.h:393
u32 vlib_buffer_add_data(vlib_main_t *vm, u32 free_list_index, u32 buffer_index, void *data, u32 n_data_bytes)
Definition: buffer.c:820
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
static_always_inline void vlib_buffer_free_inline(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 follow_buffer_next)
Definition: buffer.c:613
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
u8 vlib_buffer_add_physmem_region(vlib_main_t *vm, vlib_physmem_region_index_t pri)
Definition: buffer.c:912
u32(* buffer_free_callback)(struct vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 follow_buffer_next)
Definition: buffer.h:426
vlib_main_t * vm
Definition: buffer.c:283
vec_header_t h
Definition: buffer.c:282
u32 vlib_buffer_get_or_create_free_list(vlib_main_t *vm, u32 n_data_bytes, char *fmt,...)
Definition: buffer.c:438
u8 * format_vlib_buffer_contents(u8 *s, va_list *va)
Definition: buffer.c:142
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
u8 * vlib_validate_buffer(vlib_main_t *vm, u32 bi, uword follow_buffer_next)
Definition: buffer.c:214
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
#define clib_memcpy(a, b, c)
Definition: string.h:75
static u32 vlib_buffer_alloc_from_free_list(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 free_list_index)
Allocate buffers from specific freelist into supplied array.
Definition: buffer_funcs.h:284
void(* buffers_added_to_freelist_function)(struct vlib_main_t *vm, struct vlib_buffer_free_list_t *fl)
Definition: buffer.h:378
clib_error_t * vlib_buffer_main_init(struct vlib_main_t *vm)
Definition: buffer.c:1016
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:268
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:286
vlib_buffer_known_state_t
Definition: buffer_funcs.h:209
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define hash_set1(h, key)
Definition: hash.h:257
#define hash_create(elts, value_bytes)
Definition: hash.h:681
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vlib_buffer_free_cb_t * vlib_buffer_free_cb
Definition: buffer.h:394
static u8 * format_vlib_buffer_known_state(u8 *s, va_list *args)
Definition: buffer.c:114
void(* vlib_packet_template_init_cb)(struct vlib_main_t *vm, void *t, void *packet_data, uword n_packet_data_bytes, uword min_n_buffers_each_physmem_alloc, u8 *name)
Definition: buffer.h:396
vhost_vring_state_t state
Definition: vhost-user.h:82
u64 size
Definition: vhost-user.h:76
static vlib_buffer_free_list_t * vlib_buffer_get_buffer_free_list(vlib_main_t *vm, vlib_buffer_t *b, u32 *index)
Definition: buffer_funcs.h:440
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
u8 * format_vlib_buffer_and_data(u8 *s, va_list *args)
Definition: buffer.c:102
vector header structure
Definition: vec_bootstrap.h:55
u32 vlib_buffer_create_free_list(vlib_main_t *vm, u32 n_data_bytes, char *fmt,...)
Definition: buffer.c:421
u8 n_add_refs
Number of additional references to this buffer.
Definition: buffer.h:124
#define clib_max(x, y)
Definition: clib.h:333
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
#define unformat_parse_error(input)
Definition: format.h:267
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:142
unsigned short u16
Definition: types.h:57
static void vlib_buffer_set_free_list_index(vlib_buffer_t *b, u32 index)
Definition: buffer_funcs.h:264
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:534
vlib_buffer_free_list_t * buffer_free_list_pool
Definition: buffer.h:433
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
uword buffer_mem_start
Definition: buffer.h:421
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:221
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_physmem_alloc, char *fmt,...)
Definition: buffer.c:732
#define VLIB_PHYSMEM_F_FAKE
Definition: physmem.h:58
#define clib_error_free(e)
Definition: error.h:86
static void vlib_buffer_init_for_free_list(vlib_buffer_t *dst, vlib_buffer_free_list_t *fl)
Definition: buffer_funcs.h:809
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1491
static vlib_buffer_known_state_t vlib_buffer_is_known(u32 buffer_index)
Definition: buffer_funcs.h:225
static uword clib_mem_is_vec(void *v)
Predicate function, says whether the supplied vector is a clib heap object.
Definition: vec.h:201
u8 data[0]
Packet data.
Definition: buffer.h:159
uword buffer_init_function_opaque
Definition: buffer.h:380
static vlib_buffer_free_list_t * vlib_buffer_get_free_list(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:451
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
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:75
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:341
u8 vlib_physmem_region_index_t
Definition: physmem.h:43
static void del_free_list(vlib_main_t *vm, vlib_buffer_free_list_t *f)
Definition: buffer.c:462
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
u32 trace_index
Specifies index into trace buffer if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:137
uword * free_list_by_size
Definition: buffer.h:439
#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:298
#define vlib_panic_with_msg(vm, args...)
Definition: main.h:275
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword vlib_buffer_fill_free_list_internal(vlib_main_t *vm, vlib_buffer_free_list_t *fl, uword min_free_buffers)
Definition: buffer.c:514
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
static void vlib_buffer_free_internal(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.c:697
static u32 vlib_buffer_round_size(u32 size)
Definition: buffer_funcs.h:252
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128