blob: f00e8854ac1139227cf2d8902667a3b86aff23e3 [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 Marion49d66f12017-07-20 18:10:35 +020050static u32 vlib_buffer_physmem_sz = 32 << 20;
Damjan Marion04a7f052017-07-10 15:06:17 +020051
Dave Barach9b8ffd92016-07-08 08:13:45 -040052uword
53vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
54 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055{
Dave Barach9b8ffd92016-07-08 08:13:45 -040056 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 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 }
64 b_first->total_length_not_including_first_buffer = l;
65 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
66 return l + l_first;
67}
68
Dave Barach9b8ffd92016-07-08 08:13:45 -040069u8 *
70format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Dave Barach9b8ffd92016-07-08 08:13:45 -040072 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020073 u32 indent = format_get_indent (s);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010074
Damjan Marionc47ed032017-01-25 14:18:03 +010075 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
Damjan Marion072401e2017-07-13 18:53:27 +020076 b->current_data, b->current_length,
77 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010078
79 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
80 s = format (s, ", totlen-nifb %d",
81 b->total_length_not_including_first_buffer);
82
83 if (b->flags & VLIB_BUFFER_IS_TRACED)
84 s = format (s, ", trace 0x%x", b->trace_index);
85
86 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
87 {
88 vlib_main_t *vm = vlib_get_main ();
89 u32 next_buffer = b->next_buffer;
90 b = vlib_get_buffer (vm, next_buffer);
91
Damjan Marionc47ed032017-01-25 14:18:03 +010092 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);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010096 }
97
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 return s;
99}
100
Dave Barach9b8ffd92016-07-08 08:13:45 -0400101u8 *
102format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400104 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
105
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 s = format (s, "%U, %U",
107 format_vlib_buffer, b,
108 format_hex_bytes, vlib_buffer_get_current (b), 64);
109
110 return s;
111}
112
Dave Barach9b8ffd92016-07-08 08:13:45 -0400113static u8 *
114format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
116 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119 switch (state)
120 {
121 case VLIB_BUFFER_UNKNOWN:
122 t = "unknown";
123 break;
124
125 case VLIB_BUFFER_KNOWN_ALLOCATED:
126 t = "known-allocated";
127 break;
128
129 case VLIB_BUFFER_KNOWN_FREE:
130 t = "known-free";
131 break;
132
133 default:
134 t = "invalid";
135 break;
136 }
137
138 return format (s, "%s", t);
139}
140
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141u8 *
142format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400144 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
145 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
146
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 while (1)
148 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149 vec_add (s, vlib_buffer_get_current (b), b->current_length);
150 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 break;
152 b = vlib_get_buffer (vm, b->next_buffer);
153 }
154
155 return s;
156}
157
158static u8 *
159vlib_validate_buffer_helper (vlib_main_t * vm,
160 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400163 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
164 vlib_buffer_main_t *bm = vm->buffer_main;
165 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Damjan Marion072401e2017-07-13 18:53:27 +0200167 if (pool_is_free_index
168 (bm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
169 return format (0, "unknown free list 0x%x",
170 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Damjan Marion072401e2017-07-13 18:53:27 +0200172 fl =
173 pool_elt_at_index (bm->buffer_free_list_pool,
174 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 if (b->current_data + b->current_length > fl->n_data_bytes)
180 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 {
185 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400186 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 k = vlib_buffer_is_known (vm, b->next_buffer);
189 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
190 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
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
213u8 *
214vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215{
216 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
217 /* unique_hash */ 0);
218}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
220u8 *
221vlib_validate_buffers (vlib_main_t * vm,
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{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400228 uword i, *hash;
229 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
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",
250 format_vlib_buffer_known_state, k,
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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400262done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 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
Dave Barach80f54e22017-03-08 19:08:56 -0500272/*
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* */
280static struct
281{
282 vec_header_t h;
283 vlib_main_t *vm;
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
292vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
293
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
295/* When dubugging validate that given buffers are either known allocated
296 or known free. */
Damjan Marionc47ed032017-01-25 14:18:03 +0100297static void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298vlib_buffer_validate_alloc_free (vlib_main_t * vm,
299 u32 * buffers,
300 uword n_buffers,
301 vlib_buffer_known_state_t expected_state)
302{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400303 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 uword i, bi, is_free;
305
306 if (CLIB_DEBUG == 0)
307 return;
308
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
310 b = buffers;
311 for (i = 0; i < n_buffers; i++)
312 {
313 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 bi = b[0];
316 b += 1;
317 known = vlib_buffer_is_known (vm, bi);
318 if (known != expected_state)
319 {
320 ASSERT (0);
321 vlib_panic_with_msg
322 (vm, "%s %U buffer 0x%x",
323 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400324 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 }
326
327 vlib_buffer_set_known_state
328 (vm, bi,
329 is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
330 }
331}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100333void
334vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst,
335 vlib_buffer_free_list_t * src)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336{
337 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 u32 *d;
339
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100340 l = vec_len (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 if (l > 0)
342 {
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100343 vec_add2_aligned (dst->buffers, d, l, CLIB_CACHE_LINE_BYTES);
344 clib_memcpy (d, src->buffers, l * sizeof (d[0]));
345 vec_free (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 }
347}
348
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349/* Add buffer free list. */
350static u32
351vlib_buffer_create_free_list_helper (vlib_main_t * vm,
352 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 vlib_buffer_main_t *bm = vm->buffer_main;
356 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100357 int i;
358
Damjan Marion586afd72017-04-05 19:18:20 +0200359 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100360
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 =
367 vlib_buffer_create_free_list_helper
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 ==
375 VLIB_BUFFER_DEFAULT_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
381 pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
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);
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 f->min_n_buffers_each_physmem_alloc = VLIB_FRAME_SIZE;
Chris Luke7447d072017-07-05 12:57:10 -0400387 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100388
389 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200390 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100391 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100392
393 if (is_public)
394 {
395 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
396 if (!p)
397 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
398 }
399
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200400 clib_spinlock_init (&f->global_buffers_lock);
401
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100402 for (i = 1; i < vec_len (vlib_mains); i++)
403 {
404 vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
405 vlib_buffer_free_list_t *wf;
406 pool_get_aligned (wbm->buffer_free_list_pool,
407 wf, CLIB_CACHE_LINE_BYTES);
408 ASSERT (f - bm->buffer_free_list_pool ==
409 wf - wbm->buffer_free_list_pool);
410 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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418u32
419vlib_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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435u32
436vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
437 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438{
439 u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
440
441 if (i == ~0)
442 {
443 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
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 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 return i;
457}
458
459static void
460del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
461{
462 u32 i;
463
464 for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
Damjan Marion149ba772017-10-12 13:09:26 +0200465 vm->os_physmem_free (vm, vm->buffer_main->buffer_pools[0].physmem_region,
Damjan Marion49d66f12017-07-20 18:10:35 +0200466 f->buffer_memory_allocated[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 vec_free (f->name);
468 vec_free (f->buffer_memory_allocated);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100469 vec_free (f->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470}
471
472/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473void
Damjan Marion878c6092017-01-04 13:19:27 +0100474vlib_buffer_delete_free_list_internal (vlib_main_t * vm, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 vlib_buffer_main_t *bm = vm->buffer_main;
477 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 u32 merge_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100479 int i;
480
Damjan Marion586afd72017-04-05 19:18:20 +0200481 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100482
483 f = vlib_buffer_get_free_list (vm, free_list_index);
484
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100485 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100486 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 {
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100489 vlib_buffer_merge_free_lists (pool_elt_at_index
490 (bm->buffer_free_list_pool, merge_index),
491 f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100492 }
493
494 del_free_list (vm, f);
495
496 /* Poison it. */
497 memset (f, 0xab, sizeof (f[0]));
498
499 pool_put (bm->buffer_free_list_pool, f);
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]));
506 pool_put (bm->buffer_free_list_pool, f);
507 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508}
509
510/* Make sure free list has at least given number of free buffers. */
511static uword
512fill_free_list (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400513 vlib_buffer_free_list_t * fl, uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400515 vlib_buffer_t *buffers, *b;
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200516 vlib_buffer_free_list_t *mfl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 int n, n_bytes, i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400518 u32 *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 u32 n_remaining, n_alloc, n_this_chunk;
520
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100522 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 if (n <= 0)
524 return min_free_buffers;
525
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200526 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;
530 clib_spinlock_lock (&mfl->global_buffers_lock);
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,
534 CLIB_CACHE_LINE_BYTES);
535 _vec_len (mfl->global_buffers) = n_left;
536 clib_spinlock_unlock (&mfl->global_buffers_lock);
537 n = min_free_buffers - vec_len (fl->buffers);
538 if (n <= 0)
539 return min_free_buffers;
540 }
541
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100543 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
545 /* Always allocate new buffers in reasonably large sized chunks. */
546 n = clib_max (n, fl->min_n_buffers_each_physmem_alloc);
547
548 n_remaining = n;
549 n_alloc = 0;
550 while (n_remaining > 0)
551 {
552 n_this_chunk = clib_min (n_remaining, 16);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
555
556 /* drb: removed power-of-2 ASSERT */
Damjan Marion49d66f12017-07-20 18:10:35 +0200557 buffers =
Damjan Marion149ba772017-10-12 13:09:26 +0200558 vm->os_physmem_alloc_aligned (vm,
559 vm->buffer_main->
560 buffer_pools[0].physmem_region, n_bytes,
561 sizeof (vlib_buffer_t));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400562 if (!buffers)
563 return n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
565 /* Record chunk as being allocated so we can free it later. */
566 vec_add1 (fl->buffer_memory_allocated, buffers);
567
568 fl->n_alloc += n_this_chunk;
569 n_alloc += n_this_chunk;
570 n_remaining -= n_this_chunk;
571
572 b = buffers;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100573 vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400575 {
576 bi[i] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
Dave Barach9b8ffd92016-07-08 08:13:45 -0400578 if (CLIB_DEBUG > 0)
579 vlib_buffer_set_known_state (vm, bi[i], VLIB_BUFFER_KNOWN_FREE);
580 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
581 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
583 memset (buffers, 0, n_bytes);
584
585 /* Initialize all new buffers. */
586 b = buffers;
587 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 {
589 vlib_buffer_init_for_free_list (b, fl);
590 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
591 }
592
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593 if (fl->buffer_init_function)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400594 fl->buffer_init_function (vm, fl, bi, n_this_chunk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 }
596 return n_alloc;
597}
598
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599static u32
600alloc_from_free_list (vlib_main_t * vm,
601 vlib_buffer_free_list_t * free_list,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400602 u32 * alloc_buffers, u32 n_alloc_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603{
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100604 u32 *dst, *src;
605 uword len;
606 uword n_filled;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 dst = alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
610 n_filled = fill_free_list (vm, free_list, n_alloc_buffers);
611 if (n_filled == 0)
612 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400613
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100614 len = vec_len (free_list->buffers);
615 ASSERT (len >= n_alloc_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100617 src = free_list->buffers + len - n_alloc_buffers;
618 clib_memcpy (dst, src, n_alloc_buffers * sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100620 _vec_len (free_list->buffers) -= n_alloc_buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
622 /* Verify that buffers are known free. */
623 vlib_buffer_validate_alloc_free (vm, alloc_buffers,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400624 n_alloc_buffers, VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
626 return n_alloc_buffers;
627}
628
Damjan Marion878c6092017-01-04 13:19:27 +0100629
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630/* Allocate a given number of buffers into given array.
631 Returns number actually allocated which will be either zero or
632 number requested. */
Damjan Marion878c6092017-01-04 13:19:27 +0100633static u32
634vlib_buffer_alloc_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637
638 return alloc_from_free_list
639 (vm,
640 pool_elt_at_index (bm->buffer_free_list_pool,
641 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX),
642 buffers, n_buffers);
643}
644
Damjan Marion878c6092017-01-04 13:19:27 +0100645static u32
646vlib_buffer_alloc_from_free_list_internal (vlib_main_t * vm,
647 u32 * buffers,
648 u32 n_buffers, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400650 vlib_buffer_main_t *bm = vm->buffer_main;
651 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index);
653 return alloc_from_free_list (vm, f, buffers, n_buffers);
654}
655
Dave Barach9b8ffd92016-07-08 08:13:45 -0400656void *
657vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400659 vlib_buffer_main_t *bm = vm->buffer_main;
660 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
662 bm->buffer_free_callback = fp;
663 return rv;
664}
665
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666static_always_inline void
667vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400668 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400670 vlib_buffer_main_t *bm = vm->buffer_main;
671 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100672 u32 fi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400674 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
675 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677 cb = bm->buffer_free_callback;
678
679 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
Dave Barach9b8ffd92016-07-08 08:13:45 -0400682 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 return;
684
Damjan Marionc47ed032017-01-25 14:18:03 +0100685 for (i = 0; i < n_buffers; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100687 vlib_buffer_t *b;
688 u32 bi = buffers[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Damjan Marionc47ed032017-01-25 14:18:03 +0100690 b = vlib_get_buffer (vm, bi);
Florin Coras2f9b0c02017-09-11 20:54:15 -0400691 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Damjan Marionc47ed032017-01-25 14:18:03 +0100692 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
Damjan Marionc47ed032017-01-25 14:18:03 +0100694 /* The only current use of this callback: multicast recycle */
695 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100697 int j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
Damjan Marionc47ed032017-01-25 14:18:03 +0100699 vlib_buffer_add_to_free_list
700 (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
701
702 for (j = 0; j < vec_len (bm->announce_list); j++)
703 {
704 if (fl == bm->announce_list[j])
705 goto already_announced;
706 }
707 vec_add1 (bm->announce_list, fl);
708 already_announced:
709 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 }
711 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400712 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100713 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
714 {
715 u32 flags, next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
Damjan Marionc47ed032017-01-25 14:18:03 +0100717 do
718 {
719 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
720 flags = nb->flags;
721 next = nb->next_buffer;
722 if (nb->n_add_refs)
723 nb->n_add_refs--;
724 else
725 {
726 vlib_buffer_validate_alloc_free (vm, &bi, 1,
727 VLIB_BUFFER_KNOWN_ALLOCATED);
728 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
729 }
730 bi = next;
731 }
732 while (follow_buffer_next
733 && (flags & VLIB_BUFFER_NEXT_PRESENT));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734
Damjan Marionc47ed032017-01-25 14:18:03 +0100735 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100738 if (vec_len (bm->announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400740 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100741 for (i = 0; i < vec_len (bm->announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100743 fl = bm->announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400744 fl->buffers_added_to_freelist_function (vm, fl);
745 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100746 _vec_len (bm->announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 }
748}
749
Damjan Marion878c6092017-01-04 13:19:27 +0100750static void
751vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400753 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
754 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755}
756
Damjan Marion878c6092017-01-04 13:19:27 +0100757static void
758vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
759 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400761 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
762 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763}
764
765/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100766static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767vlib_packet_template_buffer_init (vlib_main_t * vm,
768 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400771 vlib_packet_template_t *t =
772 uword_to_pointer (fl->buffer_init_function_opaque,
773 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774 uword i;
775
776 for (i = 0; i < n_buffers; i++)
777 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400780 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
781 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 }
783}
784
Dave Barach9b8ffd92016-07-08 08:13:45 -0400785void
786vlib_packet_template_init (vlib_main_t * vm,
787 vlib_packet_template_t * t,
788 void *packet_data,
789 uword n_packet_data_bytes,
790 uword min_n_buffers_each_physmem_alloc,
791 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792{
Damjan Marion878c6092017-01-04 13:19:27 +0100793 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100794 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400795 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100796 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100797
798 va_start (va, fmt);
799 name = va_format (0, fmt, &va);
800 va_end (va);
801
Damjan Marion878c6092017-01-04 13:19:27 +0100802 if (bm->cb.vlib_packet_template_init_cb)
803 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
804 n_packet_data_bytes,
805 min_n_buffers_each_physmem_alloc,
806 name);
807
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100808 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
810 memset (t, 0, sizeof (t[0]));
811
812 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
813 t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
814
815 t->free_list_index = vlib_buffer_create_free_list_helper
816 (vm, n_packet_data_bytes,
817 /* is_public */ 1,
818 /* is_default */ 0,
819 name);
820
821 ASSERT (t->free_list_index != 0);
822 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
823 fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc;
824
825 fl->buffer_init_function = vlib_packet_template_buffer_init;
826 fl->buffer_init_function_opaque = pointer_to_uword (t);
827
828 fl->buffer_init_template.current_data = 0;
829 fl->buffer_init_template.current_length = n_packet_data_bytes;
830 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100831 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100832 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833}
834
835void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836vlib_packet_template_get_packet (vlib_main_t * vm,
837 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838{
839 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400840 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841
842 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
843 return 0;
844
845 *bi_result = bi;
846
847 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100848 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400849 t->packet_data, vec_len (t->packet_data));
850 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851
852 return b->data;
853}
854
Dave Barach9b8ffd92016-07-08 08:13:45 -0400855void
856vlib_packet_template_get_packet_helper (vlib_main_t * vm,
857 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858{
859 word n = t->min_n_buffers_each_physmem_alloc;
860 word l = vec_len (t->packet_data);
861 word n_alloc;
862
863 ASSERT (l > 0);
864 ASSERT (vec_len (t->free_buffers) == 0);
865
866 vec_validate (t->free_buffers, n - 1);
867 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
868 n, t->free_list_index);
869 _vec_len (t->free_buffers) = n_alloc;
870}
871
872/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400873u32
874vlib_buffer_add_data (vlib_main_t * vm,
875 u32 free_list_index,
876 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877{
878 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400879 vlib_buffer_t *b;
880 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881
882 bi = buffer_index;
883 if (bi == 0
884 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
885 goto out_of_buffers;
886
887 d = data;
888 n_left = n_data_bytes;
889 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400890
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891 b = vlib_get_buffer (vm, bi);
892 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
893
Dave Barach9b8ffd92016-07-08 08:13:45 -0400894 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
896 b = vlib_get_buffer (vm, b->next_buffer);
897
898 while (1)
899 {
900 u32 n;
901
902 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400903 n_left_this_buffer =
904 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100906 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 b->current_length += n;
908 n_left -= n;
909 if (n_left == 0)
910 break;
911
912 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400913 if (1 !=
914 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
915 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 goto out_of_buffers;
917
918 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
919
920 b = vlib_get_buffer (vm, b->next_buffer);
921 }
922
923 return bi;
924
Dave Barach9b8ffd92016-07-08 08:13:45 -0400925out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926 clib_error ("out of buffers");
927 return bi;
928}
929
Pierre Pfister328e99b2016-02-12 13:18:42 +0000930u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400931vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
932 u32 free_list_index,
933 vlib_buffer_t * first,
934 vlib_buffer_t ** last,
935 void *data, u16 data_len)
936{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000937 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400938 u32 n_buffer_bytes =
939 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000940 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400941 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
942 while (data_len)
943 {
944 u16 max = n_buffer_bytes - l->current_length - l->current_data;
945 if (max == 0)
946 {
947 if (1 !=
948 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
949 free_list_index))
950 return copied;
951 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
952 max = n_buffer_bytes - l->current_length - l->current_data;
953 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000954
Dave Barach9b8ffd92016-07-08 08:13:45 -0400955 u16 len = (data_len > max) ? max : data_len;
956 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
957 data + copied, len);
958 vlib_buffer_chain_increase_length (first, l, len);
959 data_len -= len;
960 copied += len;
961 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000962 return copied;
963}
964
Damjan Marion149ba772017-10-12 13:09:26 +0200965u8
966vlib_buffer_add_physmem_region (vlib_main_t * vm,
967 vlib_physmem_region_index_t pri)
Damjan Marion04a7f052017-07-10 15:06:17 +0200968{
969 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200970 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri);
971 vlib_buffer_pool_t *p;
972 uword start = pointer_to_uword (pr->mem);
973 uword size = pr->size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200974
975 if (bm->buffer_mem_size == 0)
976 {
977 bm->buffer_mem_start = start;
978 bm->buffer_mem_size = size;
979 }
980 else if (start < bm->buffer_mem_start)
981 {
982 bm->buffer_mem_size += bm->buffer_mem_start - start;
983 bm->buffer_mem_start = start;
984 if (size > bm->buffer_mem_size)
985 bm->buffer_mem_size = size;
986 }
987 else if (start > bm->buffer_mem_start)
988 {
989 uword new_size = start - bm->buffer_mem_start + size;
990 if (new_size > bm->buffer_mem_size)
991 bm->buffer_mem_size = new_size;
992 }
993
994 if ((u64) bm->buffer_mem_size >
995 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
996 {
997 clib_panic ("buffer memory size out of range!");
998 }
Damjan Marion149ba772017-10-12 13:09:26 +0200999
1000 vec_add2 (bm->buffer_pools, p, 1);
1001 p->start = start;
1002 p->size = size;
1003 p->physmem_region = pri;
1004 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +02001005}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006
Dave Barach9b8ffd92016-07-08 08:13:45 -04001007static u8 *
1008format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001010 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001011 u32 threadnum = va_arg (*va, u32);
1012 uword bytes_alloc, bytes_free, n_free, size;
1013
1014 if (!f)
1015 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
1016 "Thread", "Name", "Index", "Size", "Alloc", "Free",
1017 "#Alloc", "#Free");
1018
1019 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +01001020 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001021 bytes_alloc = size * f->n_alloc;
1022 bytes_free = size * n_free;
1023
Chris Luke7447d072017-07-05 12:57:10 -04001024 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 f->name, f->index, f->n_data_bytes,
1026 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001027 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028
1029 return s;
1030}
1031
1032static clib_error_t *
1033show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001034 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001036 vlib_buffer_main_t *bm;
1037 vlib_buffer_free_list_t *f;
1038 vlib_main_t *curr_vm;
1039 u32 vm_index = 0;
1040
1041 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
1042
1043 do
1044 {
Dave Barach80f54e22017-03-08 19:08:56 -05001045 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001046 bm = curr_vm->buffer_main;
1047
1048 /* *INDENT-OFF* */
1049 pool_foreach (f, bm->buffer_free_list_pool, ({
1050 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
1051 }));
1052 /* *INDENT-ON* */
1053
1054 vm_index++;
1055 }
1056 while (vm_index < vec_len (vlib_mains));
1057
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 return 0;
1059}
1060
Dave Barach9b8ffd92016-07-08 08:13:45 -04001061/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062VLIB_CLI_COMMAND (show_buffers_command, static) = {
1063 .path = "show buffers",
1064 .short_help = "Show packet buffer allocation",
1065 .function = show_buffers,
1066};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001067/* *INDENT-ON* */
1068
Damjan Marion49d66f12017-07-20 18:10:35 +02001069clib_error_t *
1070vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001071{
Damjan Marion49d66f12017-07-20 18:10:35 +02001072 vlib_buffer_main_t *bm;
Damjan Marion149ba772017-10-12 13:09:26 +02001073 vlib_physmem_region_index_t pri;
Damjan Marion49d66f12017-07-20 18:10:35 +02001074 clib_error_t *error;
1075
1076 vec_validate (vm->buffer_main, 0);
1077 bm = vm->buffer_main;
1078
1079 if (vlib_buffer_callbacks)
1080 {
1081 /* external plugin has registered own buffer callbacks
1082 so we just copy them and quit */
1083 vlib_buffer_main_t *bm = vm->buffer_main;
1084 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1085 sizeof (vlib_buffer_callbacks_t));
1086 bm->callbacks_registered = 1;
1087 return 0;
1088 }
Damjan Marion04a7f052017-07-10 15:06:17 +02001089
Damjan Marion878c6092017-01-04 13:19:27 +01001090 bm->cb.vlib_buffer_alloc_cb = &vlib_buffer_alloc_internal;
1091 bm->cb.vlib_buffer_alloc_from_free_list_cb =
1092 &vlib_buffer_alloc_from_free_list_internal;
1093 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1094 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1095 bm->cb.vlib_buffer_delete_free_list_cb =
1096 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001097 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +02001098
1099 /* allocate default region */
1100 error = vlib_physmem_region_alloc (vm, "buffers",
1101 vlib_buffer_physmem_sz, 0,
Damjan Marion149ba772017-10-12 13:09:26 +02001102 VLIB_PHYSMEM_F_INIT_MHEAP, &pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001103
1104 if (error == 0)
Damjan Marion149ba772017-10-12 13:09:26 +02001105 goto done;
Damjan Marion49d66f12017-07-20 18:10:35 +02001106
1107 clib_error_free (error);
1108
1109 /* we my be running unpriviledged, so try to allocate fake physmem */
1110 error = vlib_physmem_region_alloc (vm, "buffers (fake)",
1111 vlib_buffer_physmem_sz, 0,
1112 VLIB_PHYSMEM_F_FAKE |
Damjan Marion149ba772017-10-12 13:09:26 +02001113 VLIB_PHYSMEM_F_INIT_MHEAP, &pri);
1114done:
1115 vlib_buffer_add_physmem_region (vm, pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001116 return error;
Damjan Marion878c6092017-01-04 13:19:27 +01001117}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001118
Damjan Marion49d66f12017-07-20 18:10:35 +02001119static clib_error_t *
1120vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1121{
1122 u32 size_in_mb;
1123
1124 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1125 {
1126 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1127 vlib_buffer_physmem_sz = size_in_mb << 20;
1128 else
1129 return unformat_parse_error (input);
1130 }
1131
1132 unformat_free (input);
1133 return 0;
1134}
1135
1136VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1137
1138
Chris Luked4024f52016-09-06 09:32:36 -04001139/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001140/*
1141 * fd.io coding-style-patch-verification: ON
1142 *
1143 * Local Variables:
1144 * eval: (c-set-style "gnu")
1145 * End:
1146 */