blob: 6b20a542ca11318cd34f72ce067849d587b43a29 [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 Mariondac03522018-02-01 15:30:13 +010074 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010075
Damjan Mariondac03522018-02-01 15:30:13 +010076#define _(bit, name, v) \
77 if (v && (b->flags & VLIB_BUFFER_##name)) \
78 a = format (a, "%s ", v);
79 foreach_vlib_buffer_flag
80#undef _
81 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
82 b->current_data, b->current_length,
83 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010084
85 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
86 s = format (s, ", totlen-nifb %d",
87 b->total_length_not_including_first_buffer);
88
89 if (b->flags & VLIB_BUFFER_IS_TRACED)
90 s = format (s, ", trace 0x%x", b->trace_index);
91
Damjan Mariondac03522018-02-01 15:30:13 +010092 if (a)
93 s = format (s, "\n%U%v", format_white_space, indent, a);
94 vec_free (a);
95
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010096 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
97 {
98 vlib_main_t *vm = vlib_get_main ();
99 u32 next_buffer = b->next_buffer;
100 b = vlib_get_buffer (vm, next_buffer);
101
Damjan Marionc47ed032017-01-25 14:18:03 +0100102 s =
103 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
104 format_white_space, indent, next_buffer, b->current_length,
105 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100106 }
107
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 return s;
109}
110
Dave Barach9b8ffd92016-07-08 08:13:45 -0400111u8 *
112format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
115
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 s = format (s, "%U, %U",
117 format_vlib_buffer, b,
118 format_hex_bytes, vlib_buffer_get_current (b), 64);
119
120 return s;
121}
122
Dave Barach9b8ffd92016-07-08 08:13:45 -0400123static u8 *
124format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
126 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400127 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
129 switch (state)
130 {
131 case VLIB_BUFFER_UNKNOWN:
132 t = "unknown";
133 break;
134
135 case VLIB_BUFFER_KNOWN_ALLOCATED:
136 t = "known-allocated";
137 break;
138
139 case VLIB_BUFFER_KNOWN_FREE:
140 t = "known-free";
141 break;
142
143 default:
144 t = "invalid";
145 break;
146 }
147
148 return format (s, "%s", t);
149}
150
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151u8 *
152format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
155 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
156
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 while (1)
158 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400159 vec_add (s, vlib_buffer_get_current (b), b->current_length);
160 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 break;
162 b = vlib_get_buffer (vm, b->next_buffer);
163 }
164
165 return s;
166}
167
168static u8 *
169vlib_validate_buffer_helper (vlib_main_t * vm,
170 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400171 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
174 vlib_buffer_main_t *bm = vm->buffer_main;
175 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Damjan Marion072401e2017-07-13 18:53:27 +0200177 if (pool_is_free_index
178 (bm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
179 return format (0, "unknown free list 0x%x",
180 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Damjan Marion072401e2017-07-13 18:53:27 +0200182 fl =
183 pool_elt_at_index (bm->buffer_free_list_pool,
184 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach9b8ffd92016-07-08 08:13:45 -0400186 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100188
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 if (b->current_data + b->current_length > fl->n_data_bytes)
190 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 {
195 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Steven899a84b2018-01-29 20:09:09 -0800198 k = vlib_buffer_is_known (b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
200 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
203 if (unique_hash)
204 {
205 if (hash_get (*unique_hash, b->next_buffer))
206 return format (0, "duplicate buffer 0x%x", b->next_buffer);
207
208 hash_set1 (*unique_hash, b->next_buffer);
209 }
210
211 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
212 if (msg)
213 {
214 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
215 vec_free (msg);
216 return result;
217 }
218 }
219
220 return 0;
221}
222
223u8 *
224vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400225{
226 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
227 /* unique_hash */ 0);
228}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
230u8 *
231vlib_validate_buffers (vlib_main_t * vm,
232 u32 * buffers,
233 uword next_buffer_stride,
234 uword n_buffers,
235 vlib_buffer_known_state_t known_state,
236 uword follow_buffer_next)
237{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400238 uword i, *hash;
239 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400241 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
243 hash = hash_create (0, 0);
244 for (i = 0; i < n_buffers; i++)
245 {
246 bi = b[0];
247 b += next_buffer_stride;
248
249 /* Buffer is not unique. */
250 if (hash_get (hash, bi))
251 {
252 msg = format (0, "not unique");
253 goto done;
254 }
255
Steven899a84b2018-01-29 20:09:09 -0800256 k = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 if (k != known_state)
258 {
259 msg = format (0, "is %U; expected %U",
260 format_vlib_buffer_known_state, k,
261 format_vlib_buffer_known_state, known_state);
262 goto done;
263 }
264
265 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
266 if (msg)
267 goto done;
268
269 hash_set1 (hash, bi);
270 }
271
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 if (msg)
274 {
275 result = format (0, "0x%x: %v", bi, msg);
276 vec_free (msg);
277 }
278 hash_free (hash);
279 return result;
280}
281
Dave Barach80f54e22017-03-08 19:08:56 -0500282/*
283 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
284 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
285 *
286 * The only place which should ever expand vlib_mains is start_workers()
287 * in threads.c. It knows about the bootstrap vector.
288 */
289/* *INDENT-OFF* */
290static struct
291{
292 vec_header_t h;
293 vlib_main_t *vm;
294} __attribute__ ((packed)) __bootstrap_vlib_main_vector
295 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
296{
297 .h.len = 1,
298 .vm = &vlib_global_main,
299};
300/* *INDENT-ON* */
301
302vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
303
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
305/* When dubugging validate that given buffers are either known allocated
306 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100307void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308vlib_buffer_validate_alloc_free (vlib_main_t * vm,
309 u32 * buffers,
310 uword n_buffers,
311 vlib_buffer_known_state_t expected_state)
312{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 uword i, bi, is_free;
315
316 if (CLIB_DEBUG == 0)
317 return;
318
Damjan Marionc8a26c62017-11-24 20:15:23 +0100319 if (vlib_buffer_callbacks)
320 return;
321
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
323 b = buffers;
324 for (i = 0; i < n_buffers; i++)
325 {
326 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400327
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 bi = b[0];
329 b += 1;
Steven899a84b2018-01-29 20:09:09 -0800330 known = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 if (known != expected_state)
332 {
333 ASSERT (0);
334 vlib_panic_with_msg
335 (vm, "%s %U buffer 0x%x",
336 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400337 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 }
339
340 vlib_buffer_set_known_state
Steven899a84b2018-01-29 20:09:09 -0800341 (bi, is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 }
343}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100345void
346vlib_buffer_merge_free_lists (vlib_buffer_free_list_t * dst,
347 vlib_buffer_free_list_t * src)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348{
349 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350 u32 *d;
351
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100352 l = vec_len (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 if (l > 0)
354 {
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100355 vec_add2_aligned (dst->buffers, d, l, CLIB_CACHE_LINE_BYTES);
356 clib_memcpy (d, src->buffers, l * sizeof (d[0]));
357 vec_free (src->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 }
359}
360
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361/* Add buffer free list. */
Damjan Mariondac03522018-02-01 15:30:13 +0100362static vlib_buffer_free_list_index_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363vlib_buffer_create_free_list_helper (vlib_main_t * vm,
364 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 vlib_buffer_main_t *bm = vm->buffer_main;
368 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100369 int i;
370
Damjan Marion586afd72017-04-05 19:18:20 +0200371 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100372
373 if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0)
374 {
Damjan Mariondac03522018-02-01 15:30:13 +0100375 vlib_buffer_free_list_index_t default_free_free_list_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100376
377 /* *INDENT-OFF* */
378 default_free_free_list_index =
379 vlib_buffer_create_free_list_helper
380 (vm,
381 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
382 /* is_public */ 1,
383 /* is_default */ 1,
384 (u8 *) "default");
385 /* *INDENT-ON* */
386 ASSERT (default_free_free_list_index ==
387 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
388
389 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
390 return default_free_free_list_index;
391 }
392
393 pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
394
395 memset (f, 0, sizeof (f[0]));
396 f->index = f - bm->buffer_free_list_pool;
397 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500398 f->min_n_buffers_each_physmem_alloc = VLIB_FRAME_SIZE;
Chris Luke7447d072017-07-05 12:57:10 -0400399 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100400
401 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200402 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100403 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100404
405 if (is_public)
406 {
407 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
408 if (!p)
409 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
410 }
411
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200412 clib_spinlock_init (&f->global_buffers_lock);
413
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100414 for (i = 1; i < vec_len (vlib_mains); i++)
415 {
416 vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main;
417 vlib_buffer_free_list_t *wf;
418 pool_get_aligned (wbm->buffer_free_list_pool,
419 wf, CLIB_CACHE_LINE_BYTES);
420 ASSERT (f - bm->buffer_free_list_pool ==
421 wf - wbm->buffer_free_list_pool);
422 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100423 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100424 wf->n_alloc = 0;
425 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
427 return f->index;
428}
429
Damjan Mariondac03522018-02-01 15:30:13 +0100430vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400431vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
432 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433{
434 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
437 va_start (va, fmt);
438 name = va_format (0, fmt, &va);
439 va_end (va);
440
441 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
442 /* is_public */ 0,
443 /* is_default */ 0,
444 name);
445}
446
Damjan Mariondac03522018-02-01 15:30:13 +0100447vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
449 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450{
451 u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes);
452
453 if (i == ~0)
454 {
455 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400456 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457
458 va_start (va, fmt);
459 name = va_format (0, fmt, &va);
460 va_end (va);
461
462 i = vlib_buffer_create_free_list_helper (vm, n_data_bytes,
463 /* is_public */ 1,
464 /* is_default */ 0,
465 name);
466 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400467
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 return i;
469}
470
471static void
472del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
473{
474 u32 i;
475
476 for (i = 0; i < vec_len (f->buffer_memory_allocated); i++)
Damjan Marion149ba772017-10-12 13:09:26 +0200477 vm->os_physmem_free (vm, vm->buffer_main->buffer_pools[0].physmem_region,
Damjan Marion49d66f12017-07-20 18:10:35 +0200478 f->buffer_memory_allocated[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 vec_free (f->name);
480 vec_free (f->buffer_memory_allocated);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100481 vec_free (f->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482}
483
484/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400485void
Damjan Mariondac03522018-02-01 15:30:13 +0100486vlib_buffer_delete_free_list_internal (vlib_main_t * vm,
487 vlib_buffer_free_list_index_t index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400489 vlib_buffer_main_t *bm = vm->buffer_main;
490 vlib_buffer_free_list_t *f;
Damjan Mariondac03522018-02-01 15:30:13 +0100491 vlib_buffer_free_list_index_t merge_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100492 int i;
493
Damjan Marion586afd72017-04-05 19:18:20 +0200494 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100495
Damjan Mariondac03522018-02-01 15:30:13 +0100496 f = vlib_buffer_get_free_list (vm, index);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100497
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100498 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100499 merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes);
Damjan Mariondac03522018-02-01 15:30:13 +0100500 if (merge_index != (vlib_buffer_free_list_index_t) ~ 0
501 && merge_index != index)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100502 {
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100503 vlib_buffer_merge_free_lists (pool_elt_at_index
504 (bm->buffer_free_list_pool, merge_index),
505 f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100506 }
507
508 del_free_list (vm, f);
509
510 /* Poison it. */
511 memset (f, 0xab, sizeof (f[0]));
512
513 pool_put (bm->buffer_free_list_pool, f);
514
515 for (i = 1; i < vec_len (vlib_mains); i++)
516 {
517 bm = vlib_mains[i]->buffer_main;
Damjan Mariondac03522018-02-01 15:30:13 +0100518 f = vlib_buffer_get_free_list (vlib_mains[i], index);;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100519 memset (f, 0xab, sizeof (f[0]));
520 pool_put (bm->buffer_free_list_pool, f);
521 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522}
523
524/* Make sure free list has at least given number of free buffers. */
525static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100526vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
527 vlib_buffer_free_list_t * fl,
528 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 vlib_buffer_t *buffers, *b;
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200531 vlib_buffer_free_list_t *mfl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532 int n, n_bytes, i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533 u32 *bi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 u32 n_remaining, n_alloc, n_this_chunk;
535
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100537 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 if (n <= 0)
539 return min_free_buffers;
540
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200541 mfl = vlib_buffer_get_free_list (vlib_mains[0], fl->index);
542 if (vec_len (mfl->global_buffers) > 0)
543 {
544 int n_copy, n_left;
545 clib_spinlock_lock (&mfl->global_buffers_lock);
546 n_copy = clib_min (vec_len (mfl->global_buffers), n);
547 n_left = vec_len (mfl->global_buffers) - n_copy;
548 vec_add_aligned (fl->buffers, mfl->global_buffers + n_left, n_copy,
549 CLIB_CACHE_LINE_BYTES);
550 _vec_len (mfl->global_buffers) = n_left;
551 clib_spinlock_unlock (&mfl->global_buffers_lock);
552 n = min_free_buffers - vec_len (fl->buffers);
553 if (n <= 0)
554 return min_free_buffers;
555 }
556
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100558 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559
560 /* Always allocate new buffers in reasonably large sized chunks. */
561 n = clib_max (n, fl->min_n_buffers_each_physmem_alloc);
562
563 n_remaining = n;
564 n_alloc = 0;
565 while (n_remaining > 0)
566 {
567 n_this_chunk = clib_min (n_remaining, 16);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400568
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes);
570
571 /* drb: removed power-of-2 ASSERT */
Damjan Marion49d66f12017-07-20 18:10:35 +0200572 buffers =
Damjan Marion149ba772017-10-12 13:09:26 +0200573 vm->os_physmem_alloc_aligned (vm,
574 vm->buffer_main->
575 buffer_pools[0].physmem_region, n_bytes,
576 sizeof (vlib_buffer_t));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577 if (!buffers)
578 return n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
580 /* Record chunk as being allocated so we can free it later. */
581 vec_add1 (fl->buffer_memory_allocated, buffers);
582
583 fl->n_alloc += n_this_chunk;
584 n_alloc += n_this_chunk;
585 n_remaining -= n_this_chunk;
586
587 b = buffers;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100588 vec_add2_aligned (fl->buffers, bi, n_this_chunk, CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590 {
591 bi[i] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 if (CLIB_DEBUG > 0)
Steven899a84b2018-01-29 20:09:09 -0800594 vlib_buffer_set_known_state (bi[i], VLIB_BUFFER_KNOWN_FREE);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400595 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
596 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
598 memset (buffers, 0, n_bytes);
599
600 /* Initialize all new buffers. */
601 b = buffers;
602 for (i = 0; i < n_this_chunk; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603 {
604 vlib_buffer_init_for_free_list (b, fl);
605 b = vlib_buffer_next_contiguous (b, fl->n_data_bytes);
606 }
607
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 if (fl->buffer_init_function)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400609 fl->buffer_init_function (vm, fl, bi, n_this_chunk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 }
611 return n_alloc;
612}
613
Dave Barach9b8ffd92016-07-08 08:13:45 -0400614void *
615vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
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 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
620 bm->buffer_free_callback = fp;
621 return rv;
622}
623
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624static_always_inline void
625vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400628 vlib_buffer_main_t *bm = vm->buffer_main;
629 vlib_buffer_free_list_t *fl;
Damjan Mariondac03522018-02-01 15:30:13 +0100630 vlib_buffer_free_list_index_t fi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
633 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635 cb = bm->buffer_free_callback;
636
637 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
Dave Barach9b8ffd92016-07-08 08:13:45 -0400640 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 return;
642
Damjan Marionc47ed032017-01-25 14:18:03 +0100643 for (i = 0; i < n_buffers; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100645 vlib_buffer_t *b;
646 u32 bi = buffers[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
Damjan Marionc47ed032017-01-25 14:18:03 +0100648 b = vlib_get_buffer (vm, bi);
Florin Coras2f9b0c02017-09-11 20:54:15 -0400649 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Damjan Marionc47ed032017-01-25 14:18:03 +0100650 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Damjan Marionc47ed032017-01-25 14:18:03 +0100652 /* The only current use of this callback: multicast recycle */
653 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100655 int j;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Damjan Marionc47ed032017-01-25 14:18:03 +0100657 vlib_buffer_add_to_free_list
658 (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0);
659
660 for (j = 0; j < vec_len (bm->announce_list); j++)
661 {
662 if (fl == bm->announce_list[j])
663 goto already_announced;
664 }
665 vec_add1 (bm->announce_list, fl);
666 already_announced:
667 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 }
669 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400670 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100671 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
672 {
673 u32 flags, next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Damjan Marionc47ed032017-01-25 14:18:03 +0100675 do
676 {
677 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
678 flags = nb->flags;
679 next = nb->next_buffer;
680 if (nb->n_add_refs)
681 nb->n_add_refs--;
682 else
683 {
684 vlib_buffer_validate_alloc_free (vm, &bi, 1,
685 VLIB_BUFFER_KNOWN_ALLOCATED);
686 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
687 }
688 bi = next;
689 }
690 while (follow_buffer_next
691 && (flags & VLIB_BUFFER_NEXT_PRESENT));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Damjan Marionc47ed032017-01-25 14:18:03 +0100693 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100696 if (vec_len (bm->announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400698 vlib_buffer_free_list_t *fl;
Damjan Marionc47ed032017-01-25 14:18:03 +0100699 for (i = 0; i < vec_len (bm->announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400700 {
Damjan Marionc47ed032017-01-25 14:18:03 +0100701 fl = bm->announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400702 fl->buffers_added_to_freelist_function (vm, fl);
703 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100704 _vec_len (bm->announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 }
706}
707
Damjan Marion878c6092017-01-04 13:19:27 +0100708static void
709vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400711 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
712 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713}
714
Damjan Marion878c6092017-01-04 13:19:27 +0100715static void
716vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
717 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
720 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721}
722
723/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100724static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725vlib_packet_template_buffer_init (vlib_main_t * vm,
726 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 vlib_packet_template_t *t =
730 uword_to_pointer (fl->buffer_init_function_opaque,
731 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 uword i;
733
734 for (i = 0; i < n_buffers; i++)
735 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400736 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400738 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
739 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740 }
741}
742
Dave Barach9b8ffd92016-07-08 08:13:45 -0400743void
744vlib_packet_template_init (vlib_main_t * vm,
745 vlib_packet_template_t * t,
746 void *packet_data,
747 uword n_packet_data_bytes,
748 uword min_n_buffers_each_physmem_alloc,
749 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750{
Damjan Marion878c6092017-01-04 13:19:27 +0100751 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100752 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400753 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100754 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100755
756 va_start (va, fmt);
757 name = va_format (0, fmt, &va);
758 va_end (va);
759
Damjan Marion878c6092017-01-04 13:19:27 +0100760 if (bm->cb.vlib_packet_template_init_cb)
761 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
762 n_packet_data_bytes,
763 min_n_buffers_each_physmem_alloc,
764 name);
765
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100766 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
768 memset (t, 0, sizeof (t[0]));
769
770 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
771 t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc;
772
773 t->free_list_index = vlib_buffer_create_free_list_helper
774 (vm, n_packet_data_bytes,
775 /* is_public */ 1,
776 /* is_default */ 0,
777 name);
778
779 ASSERT (t->free_list_index != 0);
780 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
781 fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc;
782
783 fl->buffer_init_function = vlib_packet_template_buffer_init;
784 fl->buffer_init_function_opaque = pointer_to_uword (t);
785
786 fl->buffer_init_template.current_data = 0;
787 fl->buffer_init_template.current_length = n_packet_data_bytes;
788 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100789 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100790 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791}
792
793void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400794vlib_packet_template_get_packet (vlib_main_t * vm,
795 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796{
797 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400798 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
800 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
801 return 0;
802
803 *bi_result = bi;
804
805 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100806 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400807 t->packet_data, vec_len (t->packet_data));
808 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
810 return b->data;
811}
812
Dave Barach9b8ffd92016-07-08 08:13:45 -0400813void
814vlib_packet_template_get_packet_helper (vlib_main_t * vm,
815 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816{
817 word n = t->min_n_buffers_each_physmem_alloc;
818 word l = vec_len (t->packet_data);
819 word n_alloc;
820
821 ASSERT (l > 0);
822 ASSERT (vec_len (t->free_buffers) == 0);
823
824 vec_validate (t->free_buffers, n - 1);
825 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
826 n, t->free_list_index);
827 _vec_len (t->free_buffers) = n_alloc;
828}
829
830/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400831u32
832vlib_buffer_add_data (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100833 vlib_buffer_free_list_index_t free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400834 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835{
836 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400837 vlib_buffer_t *b;
838 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
840 bi = buffer_index;
841 if (bi == 0
842 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
843 goto out_of_buffers;
844
845 d = data;
846 n_left = n_data_bytes;
847 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400848
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 b = vlib_get_buffer (vm, bi);
850 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
851
Dave Barach9b8ffd92016-07-08 08:13:45 -0400852 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
854 b = vlib_get_buffer (vm, b->next_buffer);
855
856 while (1)
857 {
858 u32 n;
859
860 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400861 n_left_this_buffer =
862 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100864 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 b->current_length += n;
866 n_left -= n;
867 if (n_left == 0)
868 break;
869
870 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871 if (1 !=
872 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
873 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 goto out_of_buffers;
875
876 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
877
878 b = vlib_get_buffer (vm, b->next_buffer);
879 }
880
881 return bi;
882
Dave Barach9b8ffd92016-07-08 08:13:45 -0400883out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 clib_error ("out of buffers");
885 return bi;
886}
887
Pierre Pfister328e99b2016-02-12 13:18:42 +0000888u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400889vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100890 vlib_buffer_free_list_index_t
891 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400892 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100893 vlib_buffer_t ** last, void *data,
894 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400895{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000896 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897 u32 n_buffer_bytes =
898 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000899 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400900 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
901 while (data_len)
902 {
903 u16 max = n_buffer_bytes - l->current_length - l->current_data;
904 if (max == 0)
905 {
906 if (1 !=
907 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
908 free_list_index))
909 return copied;
910 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
911 max = n_buffer_bytes - l->current_length - l->current_data;
912 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000913
Dave Barach9b8ffd92016-07-08 08:13:45 -0400914 u16 len = (data_len > max) ? max : data_len;
915 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
916 data + copied, len);
917 vlib_buffer_chain_increase_length (first, l, len);
918 data_len -= len;
919 copied += len;
920 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000921 return copied;
922}
923
Damjan Marion149ba772017-10-12 13:09:26 +0200924u8
925vlib_buffer_add_physmem_region (vlib_main_t * vm,
926 vlib_physmem_region_index_t pri)
Damjan Marion04a7f052017-07-10 15:06:17 +0200927{
928 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200929 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri);
930 vlib_buffer_pool_t *p;
931 uword start = pointer_to_uword (pr->mem);
932 uword size = pr->size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200933
934 if (bm->buffer_mem_size == 0)
935 {
936 bm->buffer_mem_start = start;
937 bm->buffer_mem_size = size;
938 }
939 else if (start < bm->buffer_mem_start)
940 {
941 bm->buffer_mem_size += bm->buffer_mem_start - start;
942 bm->buffer_mem_start = start;
943 if (size > bm->buffer_mem_size)
944 bm->buffer_mem_size = size;
945 }
946 else if (start > bm->buffer_mem_start)
947 {
948 uword new_size = start - bm->buffer_mem_start + size;
949 if (new_size > bm->buffer_mem_size)
950 bm->buffer_mem_size = new_size;
951 }
952
953 if ((u64) bm->buffer_mem_size >
954 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
955 {
956 clib_panic ("buffer memory size out of range!");
957 }
Damjan Marion149ba772017-10-12 13:09:26 +0200958
959 vec_add2 (bm->buffer_pools, p, 1);
960 p->start = start;
961 p->size = size;
962 p->physmem_region = pri;
963 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200964}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965
Dave Barach9b8ffd92016-07-08 08:13:45 -0400966static u8 *
967format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400969 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100970 u32 threadnum = va_arg (*va, u32);
971 uword bytes_alloc, bytes_free, n_free, size;
972
973 if (!f)
974 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
975 "Thread", "Name", "Index", "Size", "Alloc", "Free",
976 "#Alloc", "#Free");
977
978 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100979 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100980 bytes_alloc = size * f->n_alloc;
981 bytes_free = size * n_free;
982
Chris Luke7447d072017-07-05 12:57:10 -0400983 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 f->name, f->index, f->n_data_bytes,
985 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400986 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987
988 return s;
989}
990
991static clib_error_t *
992show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400993 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100995 vlib_buffer_main_t *bm;
996 vlib_buffer_free_list_t *f;
997 vlib_main_t *curr_vm;
998 u32 vm_index = 0;
999
1000 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
1001
1002 do
1003 {
Dave Barach80f54e22017-03-08 19:08:56 -05001004 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001005 bm = curr_vm->buffer_main;
1006
1007 /* *INDENT-OFF* */
1008 pool_foreach (f, bm->buffer_free_list_pool, ({
1009 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
1010 }));
1011 /* *INDENT-ON* */
1012
1013 vm_index++;
1014 }
1015 while (vm_index < vec_len (vlib_mains));
1016
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 return 0;
1018}
1019
Dave Barach9b8ffd92016-07-08 08:13:45 -04001020/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021VLIB_CLI_COMMAND (show_buffers_command, static) = {
1022 .path = "show buffers",
1023 .short_help = "Show packet buffer allocation",
1024 .function = show_buffers,
1025};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001026/* *INDENT-ON* */
1027
Damjan Marion49d66f12017-07-20 18:10:35 +02001028clib_error_t *
1029vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001030{
Damjan Marion49d66f12017-07-20 18:10:35 +02001031 vlib_buffer_main_t *bm;
Damjan Marion149ba772017-10-12 13:09:26 +02001032 vlib_physmem_region_index_t pri;
Damjan Marion49d66f12017-07-20 18:10:35 +02001033 clib_error_t *error;
1034
1035 vec_validate (vm->buffer_main, 0);
1036 bm = vm->buffer_main;
1037
1038 if (vlib_buffer_callbacks)
1039 {
1040 /* external plugin has registered own buffer callbacks
1041 so we just copy them and quit */
1042 vlib_buffer_main_t *bm = vm->buffer_main;
1043 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1044 sizeof (vlib_buffer_callbacks_t));
1045 bm->callbacks_registered = 1;
1046 return 0;
1047 }
Damjan Marion04a7f052017-07-10 15:06:17 +02001048
Damjan Marionc8a26c62017-11-24 20:15:23 +01001049 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +01001050 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1051 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1052 bm->cb.vlib_buffer_delete_free_list_cb =
1053 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001054 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +02001055
1056 /* allocate default region */
1057 error = vlib_physmem_region_alloc (vm, "buffers",
1058 vlib_buffer_physmem_sz, 0,
Damjan Marion149ba772017-10-12 13:09:26 +02001059 VLIB_PHYSMEM_F_INIT_MHEAP, &pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001060
1061 if (error == 0)
Damjan Marion149ba772017-10-12 13:09:26 +02001062 goto done;
Damjan Marion49d66f12017-07-20 18:10:35 +02001063
1064 clib_error_free (error);
1065
1066 /* we my be running unpriviledged, so try to allocate fake physmem */
1067 error = vlib_physmem_region_alloc (vm, "buffers (fake)",
1068 vlib_buffer_physmem_sz, 0,
1069 VLIB_PHYSMEM_F_FAKE |
Damjan Marion149ba772017-10-12 13:09:26 +02001070 VLIB_PHYSMEM_F_INIT_MHEAP, &pri);
1071done:
1072 vlib_buffer_add_physmem_region (vm, pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001073 return error;
Damjan Marion878c6092017-01-04 13:19:27 +01001074}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001075
Damjan Marion49d66f12017-07-20 18:10:35 +02001076static clib_error_t *
1077vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1078{
1079 u32 size_in_mb;
1080
1081 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1082 {
1083 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1084 vlib_buffer_physmem_sz = size_in_mb << 20;
1085 else
1086 return unformat_parse_error (input);
1087 }
1088
1089 unformat_free (input);
1090 return 0;
1091}
1092
1093VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1094
1095
Chris Luked4024f52016-09-06 09:32:36 -04001096/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001097/*
1098 * fd.io coding-style-patch-verification: ON
1099 *
1100 * Local Variables:
1101 * eval: (c-set-style "gnu")
1102 * End:
1103 */