blob: 880cd95a0676423ea57f1356a81a34f11fec9ce6 [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 Marionc8a26c62017-11-24 20:15:23 +0100297void
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
Damjan Marionc8a26c62017-11-24 20:15:23 +0100309 if (vlib_buffer_callbacks)
310 return;
311
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
313 b = buffers;
314 for (i = 0; i < n_buffers; i++)
315 {
316 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 bi = b[0];
319 b += 1;
320 known = vlib_buffer_is_known (vm, bi);
321 if (known != expected_state)
322 {
323 ASSERT (0);
324 vlib_panic_with_msg
325 (vm, "%s %U buffer 0x%x",
326 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400327 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 }
329
330 vlib_buffer_set_known_state
331 (vm, bi,
332 is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
333 }
334}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100336void
337vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst,
338 vlib_buffer_free_list_t * src)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339{
340 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400341 u32 *d;
342
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100343 l = vec_len (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 if (l > 0)
345 {
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100346 vec_add2_aligned (dst->buffers, d, l, CLIB_CACHE_LINE_BYTES);
347 clib_memcpy (d, src->buffers, l * sizeof (d[0]));
348 vec_free (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 }
350}
351
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352/* Add buffer free list. */
353static u32
354vlib_buffer_create_free_list_helper (vlib_main_t * vm,
355 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 vlib_buffer_main_t *bm = vm->buffer_main;
359 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100360 int i;
361
Damjan Marion586afd72017-04-05 19:18:20 +0200362 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100363
364 if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0)
365 {
366 u32 default_free_free_list_index;
367
368 /* *INDENT-OFF* */
369 default_free_free_list_index =
370 vlib_buffer_create_free_list_helper
371 (vm,
372 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
373 /* is_public */ 1,
374 /* is_default */ 1,
375 (u8 *) "default");
376 /* *INDENT-ON* */
377 ASSERT (default_free_free_list_index ==
378 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
379
380 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
381 return default_free_free_list_index;
382 }
383
384 pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
385
386 memset (f, 0, sizeof (f[0]));
387 f->index = f - bm->buffer_free_list_pool;
388 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500389 f->min_n_buffers_each_physmem_alloc = VLIB_FRAME_SIZE;
Chris Luke7447d072017-07-05 12:57:10 -0400390 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100391
392 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200393 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100394 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100395
396 if (is_public)
397 {
398 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
399 if (!p)
400 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
401 }
402
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200403 clib_spinlock_init (&f->global_buffers_lock);
404
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100405 for (i = 1; i < vec_len (vlib_mains); i++)
406 {
407 vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
408 vlib_buffer_free_list_t *wf;
409 pool_get_aligned (wbm->buffer_free_list_pool,
410 wf, CLIB_CACHE_LINE_BYTES);
411 ASSERT (f - bm->buffer_free_list_pool ==
412 wf - wbm->buffer_free_list_pool);
413 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100414 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100415 wf->n_alloc = 0;
416 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
418 return f->index;
419}
420
Dave Barach9b8ffd92016-07-08 08:13:45 -0400421u32
422vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
423 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424{
425 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400426 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
428 va_start (va, fmt);
429 name = va_format (0, fmt, &va);
430 va_end (va);
431
432 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
433 /* is_public */ 0,
434 /* is_default */ 0,
435 name);
436}
437
Dave Barach9b8ffd92016-07-08 08:13:45 -0400438u32
439vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
440 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441{
442 u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
443
444 if (i == ~0)
445 {
446 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
449 va_start (va, fmt);
450 name = va_format (0, fmt, &va);
451 va_end (va);
452
453 i = vlib_buffer_create_free_list_helper (vm, n_data_bytes,
454 /* is_public */ 1,
455 /* is_default */ 0,
456 name);
457 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400458
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 return i;
460}
461
462static void
463del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
464{
465 u32 i;
466
467 for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
Damjan Marion149ba772017-10-12 13:09:26 +0200468 vm->os_physmem_free (vm, vm->buffer_main->buffer_pools[0].physmem_region,
Damjan Marion49d66f12017-07-20 18:10:35 +0200469 f->buffer_memory_allocated[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470 vec_free (f->name);
471 vec_free (f->buffer_memory_allocated);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100472 vec_free (f->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473}
474
475/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476void
Damjan Marion878c6092017-01-04 13:19:27 +0100477vlib_buffer_delete_free_list_internal (vlib_main_t * vm, u32 free_list_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400479 vlib_buffer_main_t *bm = vm->buffer_main;
480 vlib_buffer_free_list_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 u32 merge_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100482 int i;
483
Damjan Marion586afd72017-04-05 19:18:20 +0200484 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100485
486 f = vlib_buffer_get_free_list (vm, free_list_index);
487
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100488 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100489 merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes);
490 if (merge_index != ~0 && merge_index != free_list_index)
491 {
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100492 vlib_buffer_merge_free_lists (pool_elt_at_index
493 (bm->buffer_free_list_pool, merge_index),
494 f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100495 }
496
497 del_free_list (vm, f);
498
499 /* Poison it. */
500 memset (f, 0xab, sizeof (f[0]));
501
502 pool_put (bm->buffer_free_list_pool, f);
503
504 for (i = 1; i < vec_len (vlib_mains); i++)
505 {
506 bm = vlib_mains[i]->buffer_main;
507 f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);;
508 memset (f, 0xab, sizeof (f[0]));
509 pool_put (bm->buffer_free_list_pool, f);
510 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511}
512
513/* Make sure free list has at least given number of free buffers. */
514static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100515vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
516 vlib_buffer_free_list_t * fl,
517 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400519 vlib_buffer_t *buffers, *b;
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200520 vlib_buffer_free_list_t *mfl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 int n, n_bytes, i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522 u32 *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 u32 n_remaining, n_alloc, n_this_chunk;
524
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100526 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 if (n <= 0)
528 return min_free_buffers;
529
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200530 mfl = vlib_buffer_get_free_list (vlib_mains[0], fl->index);
531 if (vec_len (mfl->global_buffers) > 0)
532 {
533 int n_copy, n_left;
534 clib_spinlock_lock (&mfl->global_buffers_lock);
535 n_copy = clib_min (vec_len (mfl->global_buffers), n);
536 n_left = vec_len (mfl->global_buffers) - n_copy;
537 vec_add_aligned (fl->buffers, mfl->global_buffers + n_left, n_copy,
538 CLIB_CACHE_LINE_BYTES);
539 _vec_len (mfl->global_buffers) = n_left;
540 clib_spinlock_unlock (&mfl->global_buffers_lock);
541 n = min_free_buffers - vec_len (fl->buffers);
542 if (n <= 0)
543 return min_free_buffers;
544 }
545
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100547 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
549 /* Always allocate new buffers in reasonably large sized chunks. */
550 n = clib_max (n, fl->min_n_buffers_each_physmem_alloc);
551
552 n_remaining = n;
553 n_alloc = 0;
554 while (n_remaining > 0)
555 {
556 n_this_chunk = clib_min (n_remaining, 16);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
559
560 /* drb: removed power-of-2 ASSERT */
Damjan Marion49d66f12017-07-20 18:10:35 +0200561 buffers =
Damjan Marion149ba772017-10-12 13:09:26 +0200562 vm->os_physmem_alloc_aligned (vm,
563 vm->buffer_main->
564 buffer_pools[0].physmem_region, n_bytes,
565 sizeof (vlib_buffer_t));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400566 if (!buffers)
567 return n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
569 /* Record chunk as being allocated so we can free it later. */
570 vec_add1 (fl->buffer_memory_allocated, buffers);
571
572 fl->n_alloc += n_this_chunk;
573 n_alloc += n_this_chunk;
574 n_remaining -= n_this_chunk;
575
576 b = buffers;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100577 vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579 {
580 bi[i] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Dave Barach9b8ffd92016-07-08 08:13:45 -0400582 if (CLIB_DEBUG > 0)
583 vlib_buffer_set_known_state (vm, bi[i], VLIB_BUFFER_KNOWN_FREE);
584 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
585 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
587 memset (buffers, 0, n_bytes);
588
589 /* Initialize all new buffers. */
590 b = buffers;
591 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400592 {
593 vlib_buffer_init_for_free_list (b, fl);
594 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
595 }
596
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 if (fl->buffer_init_function)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400598 fl->buffer_init_function (vm, fl, bi, n_this_chunk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 }
600 return n_alloc;
601}
602
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603void *
604vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400606 vlib_buffer_main_t *bm = vm->buffer_main;
607 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
609 bm->buffer_free_callback = fp;
610 return rv;
611}
612
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613static_always_inline void
614vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400615 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400617 vlib_buffer_main_t *bm = vm->buffer_main;
618 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100619 u32 fi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400621 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
622 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624 cb = bm->buffer_free_callback;
625
626 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400627 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 return;
631
Damjan Marionc47ed032017-01-25 14:18:03 +0100632 for (i = 0; i < n_buffers; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100634 vlib_buffer_t *b;
635 u32 bi = buffers[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Damjan Marionc47ed032017-01-25 14:18:03 +0100637 b = vlib_get_buffer (vm, bi);
Florin Coras2f9b0c02017-09-11 20:54:15 -0400638 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Damjan Marionc47ed032017-01-25 14:18:03 +0100639 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640
Damjan Marionc47ed032017-01-25 14:18:03 +0100641 /* The only current use of this callback: multicast recycle */
642 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100644 int j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
Damjan Marionc47ed032017-01-25 14:18:03 +0100646 vlib_buffer_add_to_free_list
647 (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
648
649 for (j = 0; j < vec_len (bm->announce_list); j++)
650 {
651 if (fl == bm->announce_list[j])
652 goto already_announced;
653 }
654 vec_add1 (bm->announce_list, fl);
655 already_announced:
656 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 }
658 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400659 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100660 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
661 {
662 u32 flags, next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Damjan Marionc47ed032017-01-25 14:18:03 +0100664 do
665 {
666 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
667 flags = nb->flags;
668 next = nb->next_buffer;
669 if (nb->n_add_refs)
670 nb->n_add_refs--;
671 else
672 {
673 vlib_buffer_validate_alloc_free (vm, &bi, 1,
674 VLIB_BUFFER_KNOWN_ALLOCATED);
675 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
676 }
677 bi = next;
678 }
679 while (follow_buffer_next
680 && (flags & VLIB_BUFFER_NEXT_PRESENT));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
Damjan Marionc47ed032017-01-25 14:18:03 +0100682 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100685 if (vec_len (bm->announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400687 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100688 for (i = 0; i < vec_len (bm->announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100690 fl = bm->announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400691 fl->buffers_added_to_freelist_function (vm, fl);
692 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100693 _vec_len (bm->announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 }
695}
696
Damjan Marion878c6092017-01-04 13:19:27 +0100697static void
698vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400700 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
701 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702}
703
Damjan Marion878c6092017-01-04 13:19:27 +0100704static void
705vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
706 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400708 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
709 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710}
711
712/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100713static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714vlib_packet_template_buffer_init (vlib_main_t * vm,
715 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400718 vlib_packet_template_t *t =
719 uword_to_pointer (fl->buffer_init_function_opaque,
720 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 uword i;
722
723 for (i = 0; i < n_buffers; i++)
724 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
728 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 }
730}
731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732void
733vlib_packet_template_init (vlib_main_t * vm,
734 vlib_packet_template_t * t,
735 void *packet_data,
736 uword n_packet_data_bytes,
737 uword min_n_buffers_each_physmem_alloc,
738 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739{
Damjan Marion878c6092017-01-04 13:19:27 +0100740 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100741 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400742 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100743 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100744
745 va_start (va, fmt);
746 name = va_format (0, fmt, &va);
747 va_end (va);
748
Damjan Marion878c6092017-01-04 13:19:27 +0100749 if (bm->cb.vlib_packet_template_init_cb)
750 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
751 n_packet_data_bytes,
752 min_n_buffers_each_physmem_alloc,
753 name);
754
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100755 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756
757 memset (t, 0, sizeof (t[0]));
758
759 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
760 t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
761
762 t->free_list_index = vlib_buffer_create_free_list_helper
763 (vm, n_packet_data_bytes,
764 /* is_public */ 1,
765 /* is_default */ 0,
766 name);
767
768 ASSERT (t->free_list_index != 0);
769 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
770 fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc;
771
772 fl->buffer_init_function = vlib_packet_template_buffer_init;
773 fl->buffer_init_function_opaque = pointer_to_uword (t);
774
775 fl->buffer_init_template.current_data = 0;
776 fl->buffer_init_template.current_length = n_packet_data_bytes;
777 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100778 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100779 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780}
781
782void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400783vlib_packet_template_get_packet (vlib_main_t * vm,
784 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785{
786 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400787 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
789 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
790 return 0;
791
792 *bi_result = bi;
793
794 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100795 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 t->packet_data, vec_len (t->packet_data));
797 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
799 return b->data;
800}
801
Dave Barach9b8ffd92016-07-08 08:13:45 -0400802void
803vlib_packet_template_get_packet_helper (vlib_main_t * vm,
804 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805{
806 word n = t->min_n_buffers_each_physmem_alloc;
807 word l = vec_len (t->packet_data);
808 word n_alloc;
809
810 ASSERT (l > 0);
811 ASSERT (vec_len (t->free_buffers) == 0);
812
813 vec_validate (t->free_buffers, n - 1);
814 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
815 n, t->free_list_index);
816 _vec_len (t->free_buffers) = n_alloc;
817}
818
819/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400820u32
821vlib_buffer_add_data (vlib_main_t * vm,
822 u32 free_list_index,
823 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824{
825 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 vlib_buffer_t *b;
827 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828
829 bi = buffer_index;
830 if (bi == 0
831 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
832 goto out_of_buffers;
833
834 d = data;
835 n_left = n_data_bytes;
836 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400837
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838 b = vlib_get_buffer (vm, bi);
839 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
840
Dave Barach9b8ffd92016-07-08 08:13:45 -0400841 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
843 b = vlib_get_buffer (vm, b->next_buffer);
844
845 while (1)
846 {
847 u32 n;
848
849 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 n_left_this_buffer =
851 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100853 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854 b->current_length += n;
855 n_left -= n;
856 if (n_left == 0)
857 break;
858
859 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400860 if (1 !=
861 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
862 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 goto out_of_buffers;
864
865 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
866
867 b = vlib_get_buffer (vm, b->next_buffer);
868 }
869
870 return bi;
871
Dave Barach9b8ffd92016-07-08 08:13:45 -0400872out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700873 clib_error ("out of buffers");
874 return bi;
875}
876
Pierre Pfister328e99b2016-02-12 13:18:42 +0000877u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400878vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
879 u32 free_list_index,
880 vlib_buffer_t * first,
881 vlib_buffer_t ** last,
882 void *data, u16 data_len)
883{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000884 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400885 u32 n_buffer_bytes =
886 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000887 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400888 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
889 while (data_len)
890 {
891 u16 max = n_buffer_bytes - l->current_length - l->current_data;
892 if (max == 0)
893 {
894 if (1 !=
895 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
896 free_list_index))
897 return copied;
898 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
899 max = n_buffer_bytes - l->current_length - l->current_data;
900 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000901
Dave Barach9b8ffd92016-07-08 08:13:45 -0400902 u16 len = (data_len > max) ? max : data_len;
903 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
904 data + copied, len);
905 vlib_buffer_chain_increase_length (first, l, len);
906 data_len -= len;
907 copied += len;
908 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000909 return copied;
910}
911
Damjan Marion149ba772017-10-12 13:09:26 +0200912u8
913vlib_buffer_add_physmem_region (vlib_main_t * vm,
914 vlib_physmem_region_index_t pri)
Damjan Marion04a7f052017-07-10 15:06:17 +0200915{
916 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200917 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri);
918 vlib_buffer_pool_t *p;
919 uword start = pointer_to_uword (pr->mem);
920 uword size = pr->size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200921
922 if (bm->buffer_mem_size == 0)
923 {
924 bm->buffer_mem_start = start;
925 bm->buffer_mem_size = size;
926 }
927 else if (start < bm->buffer_mem_start)
928 {
929 bm->buffer_mem_size += bm->buffer_mem_start - start;
930 bm->buffer_mem_start = start;
931 if (size > bm->buffer_mem_size)
932 bm->buffer_mem_size = size;
933 }
934 else if (start > bm->buffer_mem_start)
935 {
936 uword new_size = start - bm->buffer_mem_start + size;
937 if (new_size > bm->buffer_mem_size)
938 bm->buffer_mem_size = new_size;
939 }
940
941 if ((u64) bm->buffer_mem_size >
942 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
943 {
944 clib_panic ("buffer memory size out of range!");
945 }
Damjan Marion149ba772017-10-12 13:09:26 +0200946
947 vec_add2 (bm->buffer_pools, p, 1);
948 p->start = start;
949 p->size = size;
950 p->physmem_region = pri;
951 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200952}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
Dave Barach9b8ffd92016-07-08 08:13:45 -0400954static u8 *
955format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100958 u32 threadnum = va_arg (*va, u32);
959 uword bytes_alloc, bytes_free, n_free, size;
960
961 if (!f)
962 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
963 "Thread", "Name", "Index", "Size", "Alloc", "Free",
964 "#Alloc", "#Free");
965
966 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100967 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100968 bytes_alloc = size * f->n_alloc;
969 bytes_free = size * n_free;
970
Chris Luke7447d072017-07-05 12:57:10 -0400971 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 f->name, f->index, f->n_data_bytes,
973 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400974 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
976 return s;
977}
978
979static clib_error_t *
980show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400981 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100983 vlib_buffer_main_t *bm;
984 vlib_buffer_free_list_t *f;
985 vlib_main_t *curr_vm;
986 u32 vm_index = 0;
987
988 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
989
990 do
991 {
Dave Barach80f54e22017-03-08 19:08:56 -0500992 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100993 bm = curr_vm->buffer_main;
994
995 /* *INDENT-OFF* */
996 pool_foreach (f, bm->buffer_free_list_pool, ({
997 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
998 }));
999 /* *INDENT-ON* */
1000
1001 vm_index++;
1002 }
1003 while (vm_index < vec_len (vlib_mains));
1004
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005 return 0;
1006}
1007
Dave Barach9b8ffd92016-07-08 08:13:45 -04001008/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009VLIB_CLI_COMMAND (show_buffers_command, static) = {
1010 .path = "show buffers",
1011 .short_help = "Show packet buffer allocation",
1012 .function = show_buffers,
1013};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001014/* *INDENT-ON* */
1015
Damjan Marion49d66f12017-07-20 18:10:35 +02001016clib_error_t *
1017vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001018{
Damjan Marion49d66f12017-07-20 18:10:35 +02001019 vlib_buffer_main_t *bm;
Damjan Marion149ba772017-10-12 13:09:26 +02001020 vlib_physmem_region_index_t pri;
Damjan Marion49d66f12017-07-20 18:10:35 +02001021 clib_error_t *error;
1022
1023 vec_validate (vm->buffer_main, 0);
1024 bm = vm->buffer_main;
1025
1026 if (vlib_buffer_callbacks)
1027 {
1028 /* external plugin has registered own buffer callbacks
1029 so we just copy them and quit */
1030 vlib_buffer_main_t *bm = vm->buffer_main;
1031 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1032 sizeof (vlib_buffer_callbacks_t));
1033 bm->callbacks_registered = 1;
1034 return 0;
1035 }
Damjan Marion04a7f052017-07-10 15:06:17 +02001036
Damjan Marionc8a26c62017-11-24 20:15:23 +01001037 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +01001038 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1039 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1040 bm->cb.vlib_buffer_delete_free_list_cb =
1041 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001042 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +02001043
1044 /* allocate default region */
1045 error = vlib_physmem_region_alloc (vm, "buffers",
1046 vlib_buffer_physmem_sz, 0,
Damjan Marion149ba772017-10-12 13:09:26 +02001047 VLIB_PHYSMEM_F_INIT_MHEAP, &pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001048
1049 if (error == 0)
Damjan Marion149ba772017-10-12 13:09:26 +02001050 goto done;
Damjan Marion49d66f12017-07-20 18:10:35 +02001051
1052 clib_error_free (error);
1053
1054 /* we my be running unpriviledged, so try to allocate fake physmem */
1055 error = vlib_physmem_region_alloc (vm, "buffers (fake)",
1056 vlib_buffer_physmem_sz, 0,
1057 VLIB_PHYSMEM_F_FAKE |
Damjan Marion149ba772017-10-12 13:09:26 +02001058 VLIB_PHYSMEM_F_INIT_MHEAP, &pri);
1059done:
1060 vlib_buffer_add_physmem_region (vm, pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001061 return error;
Damjan Marion878c6092017-01-04 13:19:27 +01001062}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001063
Damjan Marion49d66f12017-07-20 18:10:35 +02001064static clib_error_t *
1065vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1066{
1067 u32 size_in_mb;
1068
1069 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1070 {
1071 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1072 vlib_buffer_physmem_sz = size_in_mb << 20;
1073 else
1074 return unformat_parse_error (input);
1075 }
1076
1077 unformat_free (input);
1078 return 0;
1079}
1080
1081VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1082
1083
Chris Luked4024f52016-09-06 09:32:36 -04001084/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001085/*
1086 * fd.io coding-style-patch-verification: ON
1087 *
1088 * Local Variables:
1089 * eval: (c-set-style "gnu")
1090 * End:
1091 */