FD.io VPP  v17.10-9-gd594711
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  uword 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 
188  k = vlib_buffer_is_known (vm, b->next_buffer);
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 (vm, 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 static 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  is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
310  b = buffers;
311  for (i = 0; i < n_buffers; i++)
312  {
314 
315  bi = b[0];
316  b += 1;
317  known = vlib_buffer_is_known (vm, bi);
318  if (known != expected_state)
319  {
320  ASSERT (0);
322  (vm, "%s %U buffer 0x%x",
323  is_free ? "freeing" : "allocating",
324  format_vlib_buffer_known_state, known, bi);
325  }
326 
328  (vm, bi,
330  }
331 }
332 
333 void
336 {
337  uword l;
338  u32 *d;
339 
340  l = vec_len (src->buffers);
341  if (l > 0)
342  {
344  clib_memcpy (d, src->buffers, l * sizeof (d[0]));
345  vec_free (src->buffers);
346  }
347 }
348 
349 /* Add buffer free list. */
350 static u32
352  u32 n_data_bytes,
353  u32 is_public, u32 is_default, u8 * name)
354 {
357  int i;
358 
359  ASSERT (vlib_get_thread_index () == 0);
360 
361  if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0)
362  {
363  u32 default_free_free_list_index;
364 
365  /* *INDENT-OFF* */
366  default_free_free_list_index =
368  (vm,
369  /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
370  /* is_public */ 1,
371  /* is_default */ 1,
372  (u8 *) "default");
373  /* *INDENT-ON* */
374  ASSERT (default_free_free_list_index ==
376 
377  if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
378  return default_free_free_list_index;
379  }
380 
382 
383  memset (f, 0, sizeof (f[0]));
384  f->index = f - bm->buffer_free_list_pool;
385  f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
387  f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
388 
389  /* Setup free buffer template. */
392 
393  if (is_public)
394  {
396  if (!p)
398  }
399 
401 
402  for (i = 1; i < vec_len (vlib_mains); i++)
403  {
404  vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
408  ASSERT (f - bm->buffer_free_list_pool ==
409  wf - wbm->buffer_free_list_pool);
410  wf[0] = f[0];
411  wf->buffers = 0;
412  wf->n_alloc = 0;
413  }
414 
415  return f->index;
416 }
417 
418 u32
420  char *fmt, ...)
421 {
422  va_list va;
423  u8 *name;
424 
425  va_start (va, fmt);
426  name = va_format (0, fmt, &va);
427  va_end (va);
428 
429  return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
430  /* is_public */ 0,
431  /* is_default */ 0,
432  name);
433 }
434 
435 u32
437  char *fmt, ...)
438 {
439  u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
440 
441  if (i == ~0)
442  {
443  va_list va;
444  u8 *name;
445 
446  va_start (va, fmt);
447  name = va_format (0, fmt, &va);
448  va_end (va);
449 
450  i = vlib_buffer_create_free_list_helper (vm, n_data_bytes,
451  /* is_public */ 1,
452  /* is_default */ 0,
453  name);
454  }
455 
456  return i;
457 }
458 
459 static void
461 {
462  u32 i;
463 
464  for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
467  vec_free (f->name);
469  vec_free (f->buffers);
470 }
471 
472 /* Add buffer free list. */
473 void
475 {
478  u32 merge_index;
479  int i;
480 
481  ASSERT (vlib_get_thread_index () == 0);
482 
483  f = vlib_buffer_get_free_list (vm, free_list_index);
484 
485  ASSERT (vec_len (f->buffers) == f->n_alloc);
486  merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes);
487  if (merge_index != ~0 && merge_index != free_list_index)
488  {
490  (bm->buffer_free_list_pool, merge_index),
491  f);
492  }
493 
494  del_free_list (vm, f);
495 
496  /* Poison it. */
497  memset (f, 0xab, sizeof (f[0]));
498 
500 
501  for (i = 1; i < vec_len (vlib_mains); i++)
502  {
503  bm = vlib_mains[i]->buffer_main;
504  f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);;
505  memset (f, 0xab, sizeof (f[0]));
507  }
508 }
509 
510 /* Make sure free list has at least given number of free buffers. */
511 static uword
513  vlib_buffer_free_list_t * fl, uword min_free_buffers)
514 {
515  vlib_buffer_t *buffers, *b;
517  int n, n_bytes, i;
518  u32 *bi;
519  u32 n_remaining, n_alloc, n_this_chunk;
520 
521  /* Already have enough free buffers on free list? */
522  n = min_free_buffers - vec_len (fl->buffers);
523  if (n <= 0)
524  return min_free_buffers;
525 
526  mfl = vlib_buffer_get_free_list (vlib_mains[0], fl->index);
527  if (vec_len (mfl->global_buffers) > 0)
528  {
529  int n_copy, n_left;
531  n_copy = clib_min (vec_len (mfl->global_buffers), n);
532  n_left = vec_len (mfl->global_buffers) - n_copy;
533  vec_add_aligned (fl->buffers, mfl->global_buffers + n_left, n_copy,
535  _vec_len (mfl->global_buffers) = n_left;
537  n = min_free_buffers - vec_len (fl->buffers);
538  if (n <= 0)
539  return min_free_buffers;
540  }
541 
542  /* Always allocate round number of buffers. */
543  n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
544 
545  /* Always allocate new buffers in reasonably large sized chunks. */
547 
548  n_remaining = n;
549  n_alloc = 0;
550  while (n_remaining > 0)
551  {
552  n_this_chunk = clib_min (n_remaining, 16);
553 
554  n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
555 
556  /* drb: removed power-of-2 ASSERT */
557  buffers =
559  n_bytes, sizeof (vlib_buffer_t));
560  if (!buffers)
561  return n_alloc;
562 
563  /* Record chunk as being allocated so we can free it later. */
564  vec_add1 (fl->buffer_memory_allocated, buffers);
565 
566  fl->n_alloc += n_this_chunk;
567  n_alloc += n_this_chunk;
568  n_remaining -= n_this_chunk;
569 
570  b = buffers;
571  vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
572  for (i = 0; i < n_this_chunk; i++)
573  {
574  bi[i] = vlib_get_buffer_index (vm, b);
575 
576  if (CLIB_DEBUG > 0)
579  }
580 
581  memset (buffers, 0, n_bytes);
582 
583  /* Initialize all new buffers. */
584  b = buffers;
585  for (i = 0; i < n_this_chunk; i++)
586  {
589  }
590 
591  if (fl->buffer_init_function)
592  fl->buffer_init_function (vm, fl, bi, n_this_chunk);
593  }
594  return n_alloc;
595 }
596 
597 static u32
599  vlib_buffer_free_list_t * free_list,
600  u32 * alloc_buffers, u32 n_alloc_buffers)
601 {
602  u32 *dst, *src;
603  uword len;
604  uword n_filled;
605 
606  dst = alloc_buffers;
607 
608  n_filled = fill_free_list (vm, free_list, n_alloc_buffers);
609  if (n_filled == 0)
610  return 0;
611 
612  len = vec_len (free_list->buffers);
613  ASSERT (len >= n_alloc_buffers);
614 
615  src = free_list->buffers + len - n_alloc_buffers;
616  clib_memcpy (dst, src, n_alloc_buffers * sizeof (u32));
617 
618  _vec_len (free_list->buffers) -= n_alloc_buffers;
619 
620  /* Verify that buffers are known free. */
621  vlib_buffer_validate_alloc_free (vm, alloc_buffers,
622  n_alloc_buffers, VLIB_BUFFER_KNOWN_FREE);
623 
624  return n_alloc_buffers;
625 }
626 
627 
628 /* Allocate a given number of buffers into given array.
629  Returns number actually allocated which will be either zero or
630  number requested. */
631 static u32
633 {
635 
636  return alloc_from_free_list
637  (vm,
640  buffers, n_buffers);
641 }
642 
643 static u32
645  u32 * buffers,
646  u32 n_buffers, u32 free_list_index)
647 {
650  f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index);
651  return alloc_from_free_list (vm, f, buffers, n_buffers);
652 }
653 
654 void *
656 {
658  void *rv = bm->buffer_free_callback;
659 
660  bm->buffer_free_callback = fp;
661  return rv;
662 }
663 
666  u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
667 {
670  u32 fi;
671  int i;
672  u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
673  u32 follow_buffer_next);
674 
675  cb = bm->buffer_free_callback;
676 
677  if (PREDICT_FALSE (cb != 0))
678  n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
679 
680  if (!n_buffers)
681  return;
682 
683  for (i = 0; i < n_buffers; i++)
684  {
685  vlib_buffer_t *b;
686  u32 bi = buffers[i];
687 
688  b = vlib_get_buffer (vm, bi);
690  fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
691 
692  /* The only current use of this callback: multicast recycle */
694  {
695  int j;
696 
698  (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
699 
700  for (j = 0; j < vec_len (bm->announce_list); j++)
701  {
702  if (fl == bm->announce_list[j])
703  goto already_announced;
704  }
705  vec_add1 (bm->announce_list, fl);
706  already_announced:
707  ;
708  }
709  else
710  {
711  if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
712  {
713  u32 flags, next;
714 
715  do
716  {
717  vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
718  flags = nb->flags;
719  next = nb->next_buffer;
720  if (nb->n_add_refs)
721  nb->n_add_refs--;
722  else
723  {
726  vlib_buffer_add_to_free_list (vm, fl, bi, 1);
727  }
728  bi = next;
729  }
730  while (follow_buffer_next
731  && (flags & VLIB_BUFFER_NEXT_PRESENT));
732 
733  }
734  }
735  }
736  if (vec_len (bm->announce_list))
737  {
739  for (i = 0; i < vec_len (bm->announce_list); i++)
740  {
741  fl = bm->announce_list[i];
743  }
744  _vec_len (bm->announce_list) = 0;
745  }
746 }
747 
748 static void
749 vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
750 {
751  vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
752  1);
753 }
754 
755 static void
757  u32 n_buffers)
758 {
759  vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
760  0);
761 }
762 
763 /* Copy template packet data into buffers as they are allocated. */
764 static void __attribute__ ((unused))
767  u32 * buffers, u32 n_buffers)
768 {
770  uword_to_pointer (fl->buffer_init_function_opaque,
772  uword i;
773 
774  for (i = 0; i < n_buffers; i++)
775  {
776  vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
779  b->current_length);
780  }
781 }
782 
783 void
786  void *packet_data,
787  uword n_packet_data_bytes,
788  uword min_n_buffers_each_physmem_alloc,
789  char *fmt, ...)
790 {
792  va_list va;
793  u8 *name;
795 
796  va_start (va, fmt);
797  name = va_format (0, fmt, &va);
798  va_end (va);
799 
801  bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
802  n_packet_data_bytes,
803  min_n_buffers_each_physmem_alloc,
804  name);
805 
807 
808  memset (t, 0, sizeof (t[0]));
809 
810  vec_add (t->packet_data, packet_data, n_packet_data_bytes);
811  t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
812 
814  (vm, n_packet_data_bytes,
815  /* is_public */ 1,
816  /* is_default */ 0,
817  name);
818 
819  ASSERT (t->free_list_index != 0);
822 
825 
827  fl->buffer_init_template.current_length = n_packet_data_bytes;
828  fl->buffer_init_template.flags = 0;
831 }
832 
833 void *
835  vlib_packet_template_t * t, u32 * bi_result)
836 {
837  u32 bi;
838  vlib_buffer_t *b;
839 
840  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
841  return 0;
842 
843  *bi_result = bi;
844 
845  b = vlib_get_buffer (vm, bi);
847  t->packet_data, vec_len (t->packet_data));
849 
850  return b->data;
851 }
852 
853 void
856 {
858  word l = vec_len (t->packet_data);
859  word n_alloc;
860 
861  ASSERT (l > 0);
862  ASSERT (vec_len (t->free_buffers) == 0);
863 
864  vec_validate (t->free_buffers, n - 1);
866  n, t->free_list_index);
867  _vec_len (t->free_buffers) = n_alloc;
868 }
869 
870 /* Append given data to end of buffer, possibly allocating new buffers. */
871 u32
873  u32 free_list_index,
874  u32 buffer_index, void *data, u32 n_data_bytes)
875 {
876  u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
877  vlib_buffer_t *b;
878  void *d;
879 
880  bi = buffer_index;
881  if (bi == 0
882  && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
883  goto out_of_buffers;
884 
885  d = data;
886  n_left = n_data_bytes;
887  n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
888 
889  b = vlib_get_buffer (vm, bi);
891 
892  /* Get to the end of the chain before we try to append data... */
893  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
894  b = vlib_get_buffer (vm, b->next_buffer);
895 
896  while (1)
897  {
898  u32 n;
899 
900  ASSERT (n_buffer_bytes >= b->current_length);
901  n_left_this_buffer =
902  n_buffer_bytes - (b->current_data + b->current_length);
903  n = clib_min (n_left_this_buffer, n_left);
905  b->current_length += n;
906  n_left -= n;
907  if (n_left == 0)
908  break;
909 
910  d += n;
911  if (1 !=
913  free_list_index))
914  goto out_of_buffers;
915 
917 
918  b = vlib_get_buffer (vm, b->next_buffer);
919  }
920 
921  return bi;
922 
923 out_of_buffers:
924  clib_error ("out of buffers");
925  return bi;
926 }
927 
928 u16
930  u32 free_list_index,
932  vlib_buffer_t ** last,
933  void *data, u16 data_len)
934 {
935  vlib_buffer_t *l = *last;
936  u32 n_buffer_bytes =
937  vlib_buffer_free_list_buffer_size (vm, free_list_index);
938  u16 copied = 0;
939  ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
940  while (data_len)
941  {
942  u16 max = n_buffer_bytes - l->current_length - l->current_data;
943  if (max == 0)
944  {
945  if (1 !=
947  free_list_index))
948  return copied;
949  *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
950  max = n_buffer_bytes - l->current_length - l->current_data;
951  }
952 
953  u16 len = (data_len > max) ? max : data_len;
955  data + copied, len);
956  vlib_buffer_chain_increase_length (first, l, len);
957  data_len -= len;
958  copied += len;
959  }
960  return copied;
961 }
962 
963 void
965 {
967 
968  if (bm->buffer_mem_size == 0)
969  {
970  bm->buffer_mem_start = start;
971  bm->buffer_mem_size = size;
972  }
973  else if (start < bm->buffer_mem_start)
974  {
975  bm->buffer_mem_size += bm->buffer_mem_start - start;
976  bm->buffer_mem_start = start;
977  if (size > bm->buffer_mem_size)
978  bm->buffer_mem_size = size;
979  }
980  else if (start > bm->buffer_mem_start)
981  {
982  uword new_size = start - bm->buffer_mem_start + size;
983  if (new_size > bm->buffer_mem_size)
984  bm->buffer_mem_size = new_size;
985  }
986 
987  if ((u64) bm->buffer_mem_size >
988  ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
989  {
990  clib_panic ("buffer memory size out of range!");
991  }
992 }
993 
994 static u8 *
995 format_vlib_buffer_free_list (u8 * s, va_list * va)
996 {
998  u32 threadnum = va_arg (*va, u32);
999  uword bytes_alloc, bytes_free, n_free, size;
1000 
1001  if (!f)
1002  return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
1003  "Thread", "Name", "Index", "Size", "Alloc", "Free",
1004  "#Alloc", "#Free");
1005 
1006  size = sizeof (vlib_buffer_t) + f->n_data_bytes;
1007  n_free = vec_len (f->buffers);
1008  bytes_alloc = size * f->n_alloc;
1009  bytes_free = size * n_free;
1010 
1011  s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
1012  f->name, f->index, f->n_data_bytes,
1013  format_memory_size, bytes_alloc,
1014  format_memory_size, bytes_free, f->n_alloc, n_free);
1015 
1016  return s;
1017 }
1018 
1019 static clib_error_t *
1021  unformat_input_t * input, vlib_cli_command_t * cmd)
1022 {
1023  vlib_buffer_main_t *bm;
1025  vlib_main_t *curr_vm;
1026  u32 vm_index = 0;
1027 
1029 
1030  do
1031  {
1032  curr_vm = vlib_mains[vm_index];
1033  bm = curr_vm->buffer_main;
1034 
1035  /* *INDENT-OFF* */
1037  vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
1038  }));
1039  /* *INDENT-ON* */
1040 
1041  vm_index++;
1042  }
1043  while (vm_index < vec_len (vlib_mains));
1044 
1045  return 0;
1046 }
1047 
1048 /* *INDENT-OFF* */
1049 VLIB_CLI_COMMAND (show_buffers_command, static) = {
1050  .path = "show buffers",
1051  .short_help = "Show packet buffer allocation",
1052  .function = show_buffers,
1053 };
1054 /* *INDENT-ON* */
1055 
1056 clib_error_t *
1058 {
1059  vlib_buffer_main_t *bm;
1060  clib_error_t *error;
1061 
1062  vec_validate (vm->buffer_main, 0);
1063  bm = vm->buffer_main;
1064 
1065  if (vlib_buffer_callbacks)
1066  {
1067  /* external plugin has registered own buffer callbacks
1068  so we just copy them and quit */
1069  vlib_buffer_main_t *bm = vm->buffer_main;
1070  clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1071  sizeof (vlib_buffer_callbacks_t));
1072  bm->callbacks_registered = 1;
1073  return 0;
1074  }
1075 
1084 
1085  /* allocate default region */
1086  error = vlib_physmem_region_alloc (vm, "buffers",
1090  &bm->physmem_region);
1091 
1092  if (error == 0)
1093  return 0;
1094 
1095  clib_error_free (error);
1096 
1097  /* we my be running unpriviledged, so try to allocate fake physmem */
1098  error = vlib_physmem_region_alloc (vm, "buffers (fake)",
1103  &bm->physmem_region);
1104  return error;
1105 }
1106 
1107 static clib_error_t *
1109 {
1110  u32 size_in_mb;
1111 
1112  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1113  {
1114  if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1115  vlib_buffer_physmem_sz = size_in_mb << 20;
1116  else
1117  return unformat_parse_error (input);
1118  }
1119 
1120  unformat_free (input);
1121  return 0;
1122 }
1123 
1125 
1126 
1127 /** @endcond */
1128 /*
1129  * fd.io coding-style-patch-verification: ON
1130  *
1131  * Local Variables:
1132  * eval: (c-set-style "gnu")
1133  * End:
1134  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
vlib_main_t vlib_global_main
Definition: main.c:1650
#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:334
#define clib_min(x, y)
Definition: clib.h:332
#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:1108
void vlib_buffer_add_mem_range(vlib_main_t *vm, uword start, uword size)
Definition: buffer.c:964
static void vlib_buffer_set_known_state(vlib_main_t *vm, u32 buffer_index, vlib_buffer_known_state_t state)
Definition: buffer_funcs.h:230
#define PREDICT_TRUE(x)
Definition: clib.h:98
#define clib_error(format, args...)
Definition: error.h:62
vlib_buffer_callbacks_t cb
Definition: buffer.h:440
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:639
#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:656
vlib_buffer_t buffer_init_template
Definition: buffer.h:335
#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:357
static u32 alloc_from_free_list(vlib_main_t *vm, vlib_buffer_free_list_t *free_list, u32 *alloc_buffers, u32 n_alloc_buffers)
Definition: buffer.c:598
static u32 vlib_buffer_alloc_internal(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.c:632
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
void ** buffer_memory_allocated
Definition: buffer.h:362
vlib_buffer_main_t * buffer_main
Definition: main.h:106
static void vlib_buffer_free_no_next_internal(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.c:756
static uword fill_free_list(vlib_main_t *vm, vlib_buffer_free_list_t *fl, uword min_free_buffers)
Definition: buffer.c:512
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:345
static clib_error_t * show_buffers(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: buffer.c:1020
static vlib_buffer_known_state_t vlib_buffer_is_known(vlib_main_t *vm, u32 buffer_index)
Definition: buffer_funcs.h:219
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:854
#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:474
static u8 * format_vlib_buffer_free_list(u8 *s, va_list *va)
Definition: buffer.c:995
#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:85
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:834
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:437
void(* buffer_init_function)(struct vlib_main_t *vm, struct vlib_buffer_free_list_t *fl, u32 *buffers, u32 n_buffers)
Definition: buffer.h:369
void(* os_physmem_free)(struct vlib_main_t *vm, vlib_physmem_region_index_t idx, void *x)
Definition: main.h:125
static uword format_get_indent(u8 *s)
Definition: format.h:72
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:410
u8 * format_memory_size(u8 *s, va_list *va)
Definition: std-formats.c:193
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:423
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:172
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)
int callbacks_registered
Definition: buffer.h:441
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:33
vlib_buffer_free_list_t ** announce_list
Definition: buffer.h:437
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:458
void * vlib_set_buffer_free_callback(vlib_main_t *vm, void *fp)
Definition: buffer.c:655
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:391
static u32 vlib_buffer_alloc_from_free_list_internal(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 free_list_index)
Definition: buffer.c:644
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:351
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:193
#define hash_free(h)
Definition: hash.h:286
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:422
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:270
void(* vlib_buffer_delete_free_list_cb)(struct vlib_main_t *vm, u32 free_list_index)
Definition: buffer.h:398
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:97
static u32 vlib_buffer_free_list_buffer_size(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:426
#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:765
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:809
void(* vlib_buffer_free_cb)(struct vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.h:388
void(* vlib_buffer_free_no_next_cb)(struct vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.h:390
#define uword_to_pointer(u, type)
Definition: types.h:136
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:929
#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:270
clib_spinlock_t buffer_known_hash_lockp
Definition: buffer.h:434
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:140
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:872
#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:665
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
u32(* buffer_free_callback)(struct vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 follow_buffer_next)
Definition: buffer.h:414
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:436
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:69
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:296
void(* buffers_added_to_freelist_function)(struct vlib_main_t *vm, struct vlib_buffer_free_list_t *fl)
Definition: buffer.h:376
clib_error_t * vlib_buffer_main_init(struct vlib_main_t *vm)
Definition: buffer.c:1057
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:267
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:278
vlib_buffer_known_state_t
Definition: buffer_funcs.h:208
#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:658
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u32(* vlib_buffer_alloc_cb)(struct vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Definition: buffer.h:383
u32(* vlib_buffer_alloc_from_free_list_cb)(struct vlib_main_t *vm, u32 *buffers, u32 n_buffers, u32 free_list_index)
Definition: buffer.h:385
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:392
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:401
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
static 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
u32 vlib_buffer_create_free_list(vlib_main_t *vm, u32 n_data_bytes, char *fmt,...)
Definition: buffer.c:419
vlib_physmem_region_index_t physmem_region
Definition: buffer.h:411
u8 n_add_refs
Number of additional references to this buffer.
Definition: buffer.h:124
#define clib_max(x, y)
Definition: clib.h:325
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:141
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:276
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:517
vlib_buffer_free_list_t * buffer_free_list_pool
Definition: buffer.h:421
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
uword buffer_mem_start
Definition: buffer.h:409
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:784
#define VLIB_PHYSMEM_F_FAKE
Definition: physmem.h:59
#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:770
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1488
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:157
uword buffer_init_function_opaque
Definition: buffer.h:378
static vlib_buffer_free_list_t * vlib_buffer_get_free_list(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:412
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
#define VLIB_PHYSMEM_F_HAVE_BUFFERS
Definition: physmem.h:58
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:254
static void del_free_list(vlib_main_t *vm, vlib_buffer_free_list_t *f)
Definition: buffer.c:460
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:136
uword * free_list_by_size
Definition: buffer.h:427
#define clib_panic(format, args...)
Definition: error.h:72
#define vlib_panic_with_msg(vm, args...)
Definition: main.h:272
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
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:749
static u32 vlib_buffer_round_size(u32 size)
Definition: buffer_funcs.h:264
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128