blob: 2f15e6530da7937cd7d8774713124449f43ea5b9 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Chris Luked4024f52016-09-06 09:32:36 -040040/**
Chris Luked4024f52016-09-06 09:32:36 -040041 * @file
42 *
43 * Allocate/free network buffers.
44 */
45
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#include <vlib/vlib.h>
Damjan Marion374e2c52017-03-09 20:38:15 +010047#include <vlib/unix/unix.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Damjan Marion04a7f052017-07-10 15:06:17 +020049vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0;
Damjan Marion901d16c2018-10-23 19:50:20 +020050
51/* when running unpriviledged we are limited by RLIMIT_MEMLOCK which is
52 typically set to 16MB so setting default size for buffer memory to 14MB
53 */
54static u32 vlib_buffer_physmem_sz = 14 << 20;
Damjan Marion04a7f052017-07-10 15:06:17 +020055
Damjan Mariond1274cb2018-03-13 21:32:17 +010056vlib_buffer_main_t buffer_main;
57
Dave Barach9b8ffd92016-07-08 08:13:45 -040058uword
59vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
60 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061{
Dave Barach9b8ffd92016-07-08 08:13:45 -040062 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 uword l_first = b_first->current_length;
64 uword l = 0;
65 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
66 {
67 b = vlib_get_buffer (vm, b->next_buffer);
68 l += b->current_length;
69 }
70 b_first->total_length_not_including_first_buffer = l;
71 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
72 return l + l_first;
73}
74
Dave Barach9b8ffd92016-07-08 08:13:45 -040075u8 *
76format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Dave Barach9b8ffd92016-07-08 08:13:45 -040078 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020079 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010080 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010081
Damjan Mariondac03522018-02-01 15:30:13 +010082#define _(bit, name, v) \
83 if (v && (b->flags & VLIB_BUFFER_##name)) \
84 a = format (a, "%s ", v);
85 foreach_vlib_buffer_flag
86#undef _
87 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
88 b->current_data, b->current_length,
89 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010090
91 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
92 s = format (s, ", totlen-nifb %d",
93 b->total_length_not_including_first_buffer);
94
95 if (b->flags & VLIB_BUFFER_IS_TRACED)
96 s = format (s, ", trace 0x%x", b->trace_index);
97
Damjan Mariondac03522018-02-01 15:30:13 +010098 if (a)
99 s = format (s, "\n%U%v", format_white_space, indent, a);
100 vec_free (a);
101
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100102 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
103 {
104 vlib_main_t *vm = vlib_get_main ();
105 u32 next_buffer = b->next_buffer;
106 b = vlib_get_buffer (vm, next_buffer);
107
Damjan Marionc47ed032017-01-25 14:18:03 +0100108 s =
109 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
110 format_white_space, indent, next_buffer, b->current_length,
111 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100112 }
113
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 return s;
115}
116
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117u8 *
118format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400120 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 s = format (s, "%U, %U",
123 format_vlib_buffer, b,
124 format_hex_bytes, vlib_buffer_get_current (b), 64);
125
126 return s;
127}
128
Dave Barach9b8ffd92016-07-08 08:13:45 -0400129static u8 *
130format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131{
132 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400133 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 switch (state)
136 {
137 case VLIB_BUFFER_UNKNOWN:
138 t = "unknown";
139 break;
140
141 case VLIB_BUFFER_KNOWN_ALLOCATED:
142 t = "known-allocated";
143 break;
144
145 case VLIB_BUFFER_KNOWN_FREE:
146 t = "known-free";
147 break;
148
149 default:
150 t = "invalid";
151 break;
152 }
153
154 return format (s, "%s", t);
155}
156
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157u8 *
158format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
161 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
162
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 while (1)
164 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 vec_add (s, vlib_buffer_get_current (b), b->current_length);
166 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 break;
168 b = vlib_get_buffer (vm, b->next_buffer);
169 }
170
171 return s;
172}
173
174static u8 *
175vlib_validate_buffer_helper (vlib_main_t * vm,
176 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400177 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400179 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Damjan Marion072401e2017-07-13 18:53:27 +0200182 if (pool_is_free_index
Damjan Mariond1274cb2018-03-13 21:32:17 +0100183 (vm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
Damjan Marion072401e2017-07-13 18:53:27 +0200184 return format (0, "unknown free list 0x%x",
185 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
Damjan Marion072401e2017-07-13 18:53:27 +0200187 fl =
Damjan Mariond1274cb2018-03-13 21:32:17 +0100188 pool_elt_at_index (vm->buffer_free_list_pool,
Damjan Marion072401e2017-07-13 18:53:27 +0200189 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100193
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 if (b->current_data + b->current_length > fl->n_data_bytes)
195 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 {
200 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Steven899a84b2018-01-29 20:09:09 -0800203 k = vlib_buffer_is_known (b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
205 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400206 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
208 if (unique_hash)
209 {
210 if (hash_get (*unique_hash, b->next_buffer))
211 return format (0, "duplicate buffer 0x%x", b->next_buffer);
212
213 hash_set1 (*unique_hash, b->next_buffer);
214 }
215
216 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
217 if (msg)
218 {
219 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
220 vec_free (msg);
221 return result;
222 }
223 }
224
225 return 0;
226}
227
228u8 *
229vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400230{
231 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
232 /* unique_hash */ 0);
233}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
235u8 *
236vlib_validate_buffers (vlib_main_t * vm,
237 u32 * buffers,
238 uword next_buffer_stride,
239 uword n_buffers,
240 vlib_buffer_known_state_t known_state,
241 uword follow_buffer_next)
242{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400243 uword i, *hash;
244 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400246 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
248 hash = hash_create (0, 0);
249 for (i = 0; i < n_buffers; i++)
250 {
251 bi = b[0];
252 b += next_buffer_stride;
253
254 /* Buffer is not unique. */
255 if (hash_get (hash, bi))
256 {
257 msg = format (0, "not unique");
258 goto done;
259 }
260
Steven899a84b2018-01-29 20:09:09 -0800261 k = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 if (k != known_state)
263 {
264 msg = format (0, "is %U; expected %U",
265 format_vlib_buffer_known_state, k,
266 format_vlib_buffer_known_state, known_state);
267 goto done;
268 }
269
270 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
271 if (msg)
272 goto done;
273
274 hash_set1 (hash, bi);
275 }
276
Dave Barach9b8ffd92016-07-08 08:13:45 -0400277done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 if (msg)
279 {
280 result = format (0, "0x%x: %v", bi, msg);
281 vec_free (msg);
282 }
283 hash_free (hash);
284 return result;
285}
286
Dave Barach80f54e22017-03-08 19:08:56 -0500287/*
288 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
289 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
290 *
291 * The only place which should ever expand vlib_mains is start_workers()
292 * in threads.c. It knows about the bootstrap vector.
293 */
294/* *INDENT-OFF* */
295static struct
296{
297 vec_header_t h;
298 vlib_main_t *vm;
299} __attribute__ ((packed)) __bootstrap_vlib_main_vector
300 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
301{
302 .h.len = 1,
303 .vm = &vlib_global_main,
304};
305/* *INDENT-ON* */
306
307vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
308
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
310/* When dubugging validate that given buffers are either known allocated
311 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100312void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313vlib_buffer_validate_alloc_free (vlib_main_t * vm,
314 u32 * buffers,
315 uword n_buffers,
316 vlib_buffer_known_state_t expected_state)
317{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400318 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 uword i, bi, is_free;
320
321 if (CLIB_DEBUG == 0)
322 return;
323
Damjan Marionc8a26c62017-11-24 20:15:23 +0100324 if (vlib_buffer_callbacks)
325 return;
326
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
328 b = buffers;
329 for (i = 0; i < n_buffers; i++)
330 {
331 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400332
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 bi = b[0];
334 b += 1;
Steven899a84b2018-01-29 20:09:09 -0800335 known = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 if (known != expected_state)
337 {
338 ASSERT (0);
339 vlib_panic_with_msg
340 (vm, "%s %U buffer 0x%x",
341 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400342 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 }
344
345 vlib_buffer_set_known_state
Steven899a84b2018-01-29 20:09:09 -0800346 (bi, is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 }
348}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350/* Add buffer free list. */
Damjan Mariondac03522018-02-01 15:30:13 +0100351static vlib_buffer_free_list_index_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352vlib_buffer_create_free_list_helper (vlib_main_t * vm,
353 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100356 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100358 int i;
359
Damjan Marion586afd72017-04-05 19:18:20 +0200360 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100361
Damjan Mariond1274cb2018-03-13 21:32:17 +0100362 if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100363 {
Damjan Mariondac03522018-02-01 15:30:13 +0100364 vlib_buffer_free_list_index_t default_free_free_list_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100365
366 /* *INDENT-OFF* */
367 default_free_free_list_index =
368 vlib_buffer_create_free_list_helper
369 (vm,
370 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
371 /* is_public */ 1,
372 /* is_default */ 1,
373 (u8 *) "default");
374 /* *INDENT-ON* */
375 ASSERT (default_free_free_list_index ==
376 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
377
378 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
379 return default_free_free_list_index;
380 }
381
Damjan Mariond1274cb2018-03-13 21:32:17 +0100382 pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100383
Dave Barachb7b92992018-10-17 10:38:51 -0400384 clib_memset (f, 0, sizeof (f[0]));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100385 f->index = f - vm->buffer_free_list_pool;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100386 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100387 f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE;
388 f->buffer_pool_index = 0;
Chris Luke7447d072017-07-05 12:57:10 -0400389 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100390
391 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200392 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100393 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100394
395 if (is_public)
396 {
397 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
398 if (!p)
399 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
400 }
401
402 for (i = 1; i < vec_len (vlib_mains); i++)
403 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100404 vlib_main_t *wvm = vlib_mains[i];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100405 vlib_buffer_free_list_t *wf;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100406 pool_get_aligned (wvm->buffer_free_list_pool,
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100407 wf, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100408 ASSERT (f - vm->buffer_free_list_pool ==
409 wf - wvm->buffer_free_list_pool);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100410 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100411 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100412 wf->n_alloc = 0;
413 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
415 return f->index;
416}
417
Damjan Mariondac03522018-02-01 15:30:13 +0100418vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
420 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421{
422 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435static void
436del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
437{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100438 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (f->buffer_pool_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Damjan Mariond1274cb2018-03-13 21:32:17 +0100440 vec_add_aligned (bp->buffers, f->buffers, vec_len (f->buffers),
441 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 vec_free (f->name);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100443 vec_free (f->buffers);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100444
445 /* Poison it. */
Dave Barachb7b92992018-10-17 10:38:51 -0400446 clib_memset (f, 0xab, sizeof (f[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447}
448
449/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450void
Damjan Mariondac03522018-02-01 15:30:13 +0100451vlib_buffer_delete_free_list_internal (vlib_main_t * vm,
452 vlib_buffer_free_list_index_t index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100455 int i;
456
Damjan Marion586afd72017-04-05 19:18:20 +0200457 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100458
Damjan Mariondac03522018-02-01 15:30:13 +0100459 f = vlib_buffer_get_free_list (vm, index);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100460
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100461 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100462
463 del_free_list (vm, f);
464
Damjan Mariond1274cb2018-03-13 21:32:17 +0100465 pool_put (vm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100466
467 for (i = 1; i < vec_len (vlib_mains); i++)
468 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100469 vlib_main_t *wvm = vlib_mains[i];
470 f = vlib_buffer_get_free_list (vlib_mains[i], index);
471 del_free_list (wvm, f);
472 pool_put (wvm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100473 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474}
475
Damjan Mariond1274cb2018-03-13 21:32:17 +0100476static_always_inline void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200477vlib_buffer_pool_get_buffer (vlib_main_t * vm, vlib_buffer_pool_t * bp)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100478{
Damjan Marion68b4da62018-09-30 18:26:20 +0200479 return vlib_physmem_alloc_from_map (vm, bp->physmem_map_index,
480 bp->buffer_size, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100481}
482
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483/* Make sure free list has at least given number of free buffers. */
484static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100485vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
486 vlib_buffer_free_list_t * fl,
487 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100489 vlib_buffer_t *b;
490 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (fl->buffer_pool_index);
491 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 u32 *bi;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100493 u32 n_alloc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100496 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 if (n <= 0)
498 return min_free_buffers;
499
Damjan Mariond1274cb2018-03-13 21:32:17 +0100500 if (vec_len (bp->buffers) > 0)
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200501 {
502 int n_copy, n_left;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100503 clib_spinlock_lock (&bp->lock);
504 n_copy = clib_min (vec_len (bp->buffers), n);
505 n_left = vec_len (bp->buffers) - n_copy;
506 vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy,
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200507 CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100508 _vec_len (bp->buffers) = n_left;
509 clib_spinlock_unlock (&bp->lock);
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200510 n = min_free_buffers - vec_len (fl->buffers);
511 if (n <= 0)
512 return min_free_buffers;
513 }
514
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100516 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
518 /* Always allocate new buffers in reasonably large sized chunks. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100519 n = clib_max (n, fl->min_n_buffers_each_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Damjan Mariond1274cb2018-03-13 21:32:17 +0100521 clib_spinlock_lock (&bp->lock);
522 while (n_alloc < n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200524 if ((b = vlib_buffer_pool_get_buffer (vm, bp)) == 0)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100525 goto done;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526
Damjan Mariond1274cb2018-03-13 21:32:17 +0100527 n_alloc += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Damjan Mariond1274cb2018-03-13 21:32:17 +0100529 vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES);
530 bi[0] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Damjan Mariond1274cb2018-03-13 21:32:17 +0100532 if (CLIB_DEBUG > 0)
533 vlib_buffer_set_known_state (bi[0], VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Dave Barachb7b92992018-10-17 10:38:51 -0400535 clib_memset (b, 0, sizeof (vlib_buffer_t));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100536 vlib_buffer_init_for_free_list (b, fl);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 if (fl->buffer_init_function)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100539 fl->buffer_init_function (vm, fl, bi, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100541
542done:
543 clib_spinlock_unlock (&bp->lock);
544 fl->n_alloc += n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 return n_alloc;
546}
547
Dave Barach9b8ffd92016-07-08 08:13:45 -0400548void *
549vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100551 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
554 bm->buffer_free_callback = fp;
555 return rv;
556}
557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558static_always_inline void
Damjan Marioneac3b112018-03-05 09:36:31 +0100559recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi,
560 vlib_buffer_t * b, u32 follow_buffer_next)
561{
562 vlib_buffer_free_list_t *fl;
563 vlib_buffer_free_list_index_t fi;
Damjan Marion8e715292018-09-03 15:41:45 +0200564 u32 flags, next;
565
Damjan Marioneac3b112018-03-05 09:36:31 +0100566 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
567
Damjan Marion8e715292018-09-03 15:41:45 +0200568 do
Damjan Marioneac3b112018-03-05 09:36:31 +0100569 {
Damjan Marion8e715292018-09-03 15:41:45 +0200570 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
571 flags = nb->flags;
572 next = nb->next_buffer;
573 if (nb->n_add_refs)
574 nb->n_add_refs--;
575 else
Damjan Marioneac3b112018-03-05 09:36:31 +0100576 {
Damjan Marion8e715292018-09-03 15:41:45 +0200577 vlib_buffer_validate_alloc_free (vm, &bi, 1,
578 VLIB_BUFFER_KNOWN_ALLOCATED);
579 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
Damjan Marioneac3b112018-03-05 09:36:31 +0100580 }
Damjan Marion8e715292018-09-03 15:41:45 +0200581 bi = next;
Damjan Marioneac3b112018-03-05 09:36:31 +0100582 }
Damjan Marion8e715292018-09-03 15:41:45 +0200583 while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT));
Damjan Marioneac3b112018-03-05 09:36:31 +0100584}
585
586static_always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100590 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marioneac3b112018-03-05 09:36:31 +0100591 vlib_buffer_t *p, *b0, *b1, *b2, *b3;
592 int i = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
594 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596 cb = bm->buffer_free_callback;
597
598 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400599 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 return;
603
Damjan Marioneac3b112018-03-05 09:36:31 +0100604 while (i + 11 < n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605 {
Damjan Marioneac3b112018-03-05 09:36:31 +0100606 p = vlib_get_buffer (vm, buffers[i + 8]);
607 vlib_prefetch_buffer_header (p, LOAD);
608 p = vlib_get_buffer (vm, buffers[i + 9]);
609 vlib_prefetch_buffer_header (p, LOAD);
610 p = vlib_get_buffer (vm, buffers[i + 10]);
611 vlib_prefetch_buffer_header (p, LOAD);
612 p = vlib_get_buffer (vm, buffers[i + 11]);
613 vlib_prefetch_buffer_header (p, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Damjan Marioneac3b112018-03-05 09:36:31 +0100615 b0 = vlib_get_buffer (vm, buffers[i]);
616 b1 = vlib_get_buffer (vm, buffers[i + 1]);
617 b2 = vlib_get_buffer (vm, buffers[i + 2]);
618 b3 = vlib_get_buffer (vm, buffers[i + 3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Damjan Marioneac3b112018-03-05 09:36:31 +0100620 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
621 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
622 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
623 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
Damjan Marioneac3b112018-03-05 09:36:31 +0100625 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
626 recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next);
627 recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next);
628 recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next);
Damjan Marionc47ed032017-01-25 14:18:03 +0100629
Damjan Marioneac3b112018-03-05 09:36:31 +0100630 i += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631 }
Damjan Marioneac3b112018-03-05 09:36:31 +0100632
633 while (i < n_buffers)
634 {
635 b0 = vlib_get_buffer (vm, buffers[i]);
636 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
637 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
638 i++;
639 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640}
641
Damjan Marion878c6092017-01-04 13:19:27 +0100642static void
643vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
646 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647}
648
Damjan Marion878c6092017-01-04 13:19:27 +0100649static void
650vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
651 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
654 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655}
656
657/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100658static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659vlib_packet_template_buffer_init (vlib_main_t * vm,
660 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400661 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400663 vlib_packet_template_t *t =
664 uword_to_pointer (fl->buffer_init_function_opaque,
665 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 uword i;
667
668 for (i = 0; i < n_buffers; i++)
669 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400670 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400672 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
673 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 }
675}
676
Dave Barach9b8ffd92016-07-08 08:13:45 -0400677void
678vlib_packet_template_init (vlib_main_t * vm,
679 vlib_packet_template_t * t,
680 void *packet_data,
681 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100682 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100684 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100685 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400686 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100687 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100688
689 va_start (va, fmt);
690 name = va_format (0, fmt, &va);
691 va_end (va);
692
Damjan Marion878c6092017-01-04 13:19:27 +0100693 if (bm->cb.vlib_packet_template_init_cb)
694 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
695 n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100696 min_n_buffers_each_alloc, name);
Damjan Marion878c6092017-01-04 13:19:27 +0100697
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100698 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699
Dave Barachb7b92992018-10-17 10:38:51 -0400700 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701
702 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100703 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704
705 t->free_list_index = vlib_buffer_create_free_list_helper
706 (vm, n_packet_data_bytes,
707 /* is_public */ 1,
708 /* is_default */ 0,
709 name);
710
711 ASSERT (t->free_list_index != 0);
712 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100713 fl->min_n_buffers_each_alloc = t->min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714
715 fl->buffer_init_function = vlib_packet_template_buffer_init;
716 fl->buffer_init_function_opaque = pointer_to_uword (t);
717
718 fl->buffer_init_template.current_data = 0;
719 fl->buffer_init_template.current_length = n_packet_data_bytes;
720 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100721 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100722 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723}
724
725void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400726vlib_packet_template_get_packet (vlib_main_t * vm,
727 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728{
729 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400730 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731
732 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
733 return 0;
734
735 *bi_result = bi;
736
737 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100738 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400739 t->packet_data, vec_len (t->packet_data));
740 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
742 return b->data;
743}
744
Dave Barach9b8ffd92016-07-08 08:13:45 -0400745void
746vlib_packet_template_get_packet_helper (vlib_main_t * vm,
747 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100749 word n = t->min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750 word l = vec_len (t->packet_data);
751 word n_alloc;
752
753 ASSERT (l > 0);
754 ASSERT (vec_len (t->free_buffers) == 0);
755
756 vec_validate (t->free_buffers, n - 1);
757 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
758 n, t->free_list_index);
759 _vec_len (t->free_buffers) = n_alloc;
760}
761
762/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400763u32
764vlib_buffer_add_data (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100765 vlib_buffer_free_list_index_t free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400766 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767{
768 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 vlib_buffer_t *b;
770 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
772 bi = buffer_index;
Dave Barach0d65d112018-03-16 15:21:35 -0400773 if (bi == ~0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
775 goto out_of_buffers;
776
777 d = data;
778 n_left = n_data_bytes;
779 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400780
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 b = vlib_get_buffer (vm, bi);
782 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
783
Dave Barach9b8ffd92016-07-08 08:13:45 -0400784 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
786 b = vlib_get_buffer (vm, b->next_buffer);
787
788 while (1)
789 {
790 u32 n;
791
792 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400793 n_left_this_buffer =
794 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100796 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 b->current_length += n;
798 n_left -= n;
799 if (n_left == 0)
800 break;
801
802 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400803 if (1 !=
804 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
805 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806 goto out_of_buffers;
807
808 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
809
810 b = vlib_get_buffer (vm, b->next_buffer);
811 }
812
813 return bi;
814
Dave Barach9b8ffd92016-07-08 08:13:45 -0400815out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816 clib_error ("out of buffers");
817 return bi;
818}
819
Pierre Pfister328e99b2016-02-12 13:18:42 +0000820u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400821vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100822 vlib_buffer_free_list_index_t
823 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400824 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100825 vlib_buffer_t ** last, void *data,
826 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400827{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000828 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400829 u32 n_buffer_bytes =
830 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000831 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400832 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
833 while (data_len)
834 {
835 u16 max = n_buffer_bytes - l->current_length - l->current_data;
836 if (max == 0)
837 {
838 if (1 !=
839 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
840 free_list_index))
841 return copied;
842 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
843 max = n_buffer_bytes - l->current_length - l->current_data;
844 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000845
Dave Barach9b8ffd92016-07-08 08:13:45 -0400846 u16 len = (data_len > max) ? max : data_len;
847 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
848 data + copied, len);
849 vlib_buffer_chain_increase_length (first, l, len);
850 data_len -= len;
851 copied += len;
852 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000853 return copied;
854}
855
Damjan Marion149ba772017-10-12 13:09:26 +0200856u8
Damjan Marion68b4da62018-09-30 18:26:20 +0200857vlib_buffer_register_physmem_map (vlib_main_t * vm, u32 physmem_map_index)
Damjan Marion04a7f052017-07-10 15:06:17 +0200858{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100859 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200860 vlib_buffer_pool_t *p;
Damjan Marion68b4da62018-09-30 18:26:20 +0200861 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
862 uword start = pointer_to_uword (m->base);
863 uword size = m->n_pages << m->log2_page_size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200864
865 if (bm->buffer_mem_size == 0)
866 {
867 bm->buffer_mem_start = start;
868 bm->buffer_mem_size = size;
869 }
870 else if (start < bm->buffer_mem_start)
871 {
872 bm->buffer_mem_size += bm->buffer_mem_start - start;
873 bm->buffer_mem_start = start;
874 if (size > bm->buffer_mem_size)
875 bm->buffer_mem_size = size;
876 }
877 else if (start > bm->buffer_mem_start)
878 {
879 uword new_size = start - bm->buffer_mem_start + size;
880 if (new_size > bm->buffer_mem_size)
881 bm->buffer_mem_size = new_size;
882 }
883
884 if ((u64) bm->buffer_mem_size >
885 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
886 {
887 clib_panic ("buffer memory size out of range!");
888 }
Damjan Marion149ba772017-10-12 13:09:26 +0200889
890 vec_add2 (bm->buffer_pools, p, 1);
891 p->start = start;
892 p->size = size;
Damjan Marion68b4da62018-09-30 18:26:20 +0200893 p->physmem_map_index = physmem_map_index;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100894
Damjan Mariond1274cb2018-03-13 21:32:17 +0100895 ASSERT (p - bm->buffer_pools < 256);
Damjan Marion149ba772017-10-12 13:09:26 +0200896 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200897}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898
Dave Barach9b8ffd92016-07-08 08:13:45 -0400899static u8 *
900format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400902 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100903 u32 threadnum = va_arg (*va, u32);
904 uword bytes_alloc, bytes_free, n_free, size;
905
906 if (!f)
907 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
908 "Thread", "Name", "Index", "Size", "Alloc", "Free",
909 "#Alloc", "#Free");
910
911 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100912 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100913 bytes_alloc = size * f->n_alloc;
914 bytes_free = size * n_free;
915
Chris Luke7447d072017-07-05 12:57:10 -0400916 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 f->name, f->index, f->n_data_bytes,
918 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400919 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920
921 return s;
922}
923
924static clib_error_t *
925show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400926 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100928 vlib_buffer_free_list_t *f;
929 vlib_main_t *curr_vm;
930 u32 vm_index = 0;
931
932 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
933
934 do
935 {
Dave Barach80f54e22017-03-08 19:08:56 -0500936 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100937
938 /* *INDENT-OFF* */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100939 pool_foreach (f, curr_vm->buffer_free_list_pool, ({
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100940 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
941 }));
942 /* *INDENT-ON* */
943
944 vm_index++;
945 }
946 while (vm_index < vec_len (vlib_mains));
947
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 return 0;
949}
950
Dave Barach9b8ffd92016-07-08 08:13:45 -0400951/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952VLIB_CLI_COMMAND (show_buffers_command, static) = {
953 .path = "show buffers",
954 .short_help = "Show packet buffer allocation",
955 .function = show_buffers,
956};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957/* *INDENT-ON* */
958
Damjan Marion49d66f12017-07-20 18:10:35 +0200959clib_error_t *
960vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100961{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100962 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200963 clib_error_t *error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200964 u32 physmem_map_index;
965 u8 pool_index;
Damjan Marion49d66f12017-07-20 18:10:35 +0200966
Damjan Marion49d66f12017-07-20 18:10:35 +0200967 if (vlib_buffer_callbacks)
968 {
969 /* external plugin has registered own buffer callbacks
970 so we just copy them and quit */
Damjan Marion49d66f12017-07-20 18:10:35 +0200971 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
972 sizeof (vlib_buffer_callbacks_t));
973 bm->callbacks_registered = 1;
974 return 0;
975 }
Damjan Marion04a7f052017-07-10 15:06:17 +0200976
Damjan Marionc8a26c62017-11-24 20:15:23 +0100977 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +0100978 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
979 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
980 bm->cb.vlib_buffer_delete_free_list_cb =
981 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -0400982 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +0200983
Damjan Marion68b4da62018-09-30 18:26:20 +0200984 if ((error = vlib_physmem_shared_map_create (vm, "buffers",
Damjan Marionf87eb9b2018-10-23 20:33:29 +0200985 vlib_buffer_physmem_sz,
986 CLIB_PMALLOC_NUMA_LOCAL,
Damjan Marion68b4da62018-09-30 18:26:20 +0200987 &physmem_map_index)))
988 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200989
Damjan Marion68b4da62018-09-30 18:26:20 +0200990 pool_index = vlib_buffer_register_physmem_map (vm, physmem_map_index);
991 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (pool_index);
992 clib_spinlock_init (&bp->lock);
993 bp->buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES +
994 sizeof (vlib_buffer_t);
Damjan Marion49d66f12017-07-20 18:10:35 +0200995
Damjan Marion68b4da62018-09-30 18:26:20 +0200996 return 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100997}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100998
Damjan Marion49d66f12017-07-20 18:10:35 +0200999static clib_error_t *
1000vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1001{
1002 u32 size_in_mb;
1003
1004 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1005 {
1006 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1007 vlib_buffer_physmem_sz = size_in_mb << 20;
1008 else
1009 return unformat_parse_error (input);
1010 }
1011
1012 unformat_free (input);
1013 return 0;
1014}
1015
1016VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1017
1018
Chris Luked4024f52016-09-06 09:32:36 -04001019/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001020/*
1021 * fd.io coding-style-patch-verification: ON
1022 *
1023 * Local Variables:
1024 * eval: (c-set-style "gnu")
1025 * End:
1026 */