blob: 3f90f5a08604f9fa57cd3948a18144471c273f35 [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
Damjan Mariond1274cb2018-03-13 21:32:17 +010052vlib_buffer_main_t buffer_main;
53
Dave Barach9b8ffd92016-07-08 08:13:45 -040054uword
55vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
56 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barach9b8ffd92016-07-08 08:13:45 -040058 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 uword l_first = b_first->current_length;
60 uword l = 0;
61 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
62 {
63 b = vlib_get_buffer (vm, b->next_buffer);
64 l += b->current_length;
65 }
66 b_first->total_length_not_including_first_buffer = l;
67 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
68 return l + l_first;
69}
70
Dave Barach9b8ffd92016-07-08 08:13:45 -040071u8 *
72format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Dave Barach9b8ffd92016-07-08 08:13:45 -040074 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020075 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010076 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010077
Damjan Mariondac03522018-02-01 15:30:13 +010078#define _(bit, name, v) \
79 if (v && (b->flags & VLIB_BUFFER_##name)) \
80 a = format (a, "%s ", v);
81 foreach_vlib_buffer_flag
82#undef _
83 s = format (s, "current data %d, length %d, free-list %d, clone-count %u",
84 b->current_data, b->current_length,
85 vlib_buffer_get_free_list_index (b), b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010086
87 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
88 s = format (s, ", totlen-nifb %d",
89 b->total_length_not_including_first_buffer);
90
91 if (b->flags & VLIB_BUFFER_IS_TRACED)
92 s = format (s, ", trace 0x%x", b->trace_index);
93
Damjan Mariondac03522018-02-01 15:30:13 +010094 if (a)
95 s = format (s, "\n%U%v", format_white_space, indent, a);
96 vec_free (a);
97
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010098 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
99 {
100 vlib_main_t *vm = vlib_get_main ();
101 u32 next_buffer = b->next_buffer;
102 b = vlib_get_buffer (vm, next_buffer);
103
Damjan Marionc47ed032017-01-25 14:18:03 +0100104 s =
105 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
106 format_white_space, indent, next_buffer, b->current_length,
107 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100108 }
109
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 return s;
111}
112
Dave Barach9b8ffd92016-07-08 08:13:45 -0400113u8 *
114format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400116 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
117
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 s = format (s, "%U, %U",
119 format_vlib_buffer, b,
120 format_hex_bytes, vlib_buffer_get_current (b), 64);
121
122 return s;
123}
124
Dave Barach9b8ffd92016-07-08 08:13:45 -0400125static u8 *
126format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
128 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400129 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131 switch (state)
132 {
133 case VLIB_BUFFER_UNKNOWN:
134 t = "unknown";
135 break;
136
137 case VLIB_BUFFER_KNOWN_ALLOCATED:
138 t = "known-allocated";
139 break;
140
141 case VLIB_BUFFER_KNOWN_FREE:
142 t = "known-free";
143 break;
144
145 default:
146 t = "invalid";
147 break;
148 }
149
150 return format (s, "%s", t);
151}
152
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153u8 *
154format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
157 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
158
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 while (1)
160 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 vec_add (s, vlib_buffer_get_current (b), b->current_length);
162 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 break;
164 b = vlib_get_buffer (vm, b->next_buffer);
165 }
166
167 return s;
168}
169
170static u8 *
171vlib_validate_buffer_helper (vlib_main_t * vm,
172 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Damjan Marion072401e2017-07-13 18:53:27 +0200178 if (pool_is_free_index
Damjan Mariond1274cb2018-03-13 21:32:17 +0100179 (vm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b)))
Damjan Marion072401e2017-07-13 18:53:27 +0200180 return format (0, "unknown free list 0x%x",
181 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Damjan Marion072401e2017-07-13 18:53:27 +0200183 fl =
Damjan Mariond1274cb2018-03-13 21:32:17 +0100184 pool_elt_at_index (vm->buffer_free_list_pool,
Damjan Marion072401e2017-07-13 18:53:27 +0200185 vlib_buffer_get_free_list_index (b));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100189
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 if (b->current_data + b->current_length > fl->n_data_bytes)
191 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Dave Barach9b8ffd92016-07-08 08:13:45 -0400194 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 {
196 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Steven899a84b2018-01-29 20:09:09 -0800199 k = vlib_buffer_is_known (b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
201 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204 if (unique_hash)
205 {
206 if (hash_get (*unique_hash, b->next_buffer))
207 return format (0, "duplicate buffer 0x%x", b->next_buffer);
208
209 hash_set1 (*unique_hash, b->next_buffer);
210 }
211
212 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
213 if (msg)
214 {
215 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
216 vec_free (msg);
217 return result;
218 }
219 }
220
221 return 0;
222}
223
224u8 *
225vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226{
227 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
228 /* unique_hash */ 0);
229}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231u8 *
232vlib_validate_buffers (vlib_main_t * vm,
233 u32 * buffers,
234 uword next_buffer_stride,
235 uword n_buffers,
236 vlib_buffer_known_state_t known_state,
237 uword follow_buffer_next)
238{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400239 uword i, *hash;
240 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400242 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
244 hash = hash_create (0, 0);
245 for (i = 0; i < n_buffers; i++)
246 {
247 bi = b[0];
248 b += next_buffer_stride;
249
250 /* Buffer is not unique. */
251 if (hash_get (hash, bi))
252 {
253 msg = format (0, "not unique");
254 goto done;
255 }
256
Steven899a84b2018-01-29 20:09:09 -0800257 k = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 if (k != known_state)
259 {
260 msg = format (0, "is %U; expected %U",
261 format_vlib_buffer_known_state, k,
262 format_vlib_buffer_known_state, known_state);
263 goto done;
264 }
265
266 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
267 if (msg)
268 goto done;
269
270 hash_set1 (hash, bi);
271 }
272
Dave Barach9b8ffd92016-07-08 08:13:45 -0400273done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 if (msg)
275 {
276 result = format (0, "0x%x: %v", bi, msg);
277 vec_free (msg);
278 }
279 hash_free (hash);
280 return result;
281}
282
Dave Barach80f54e22017-03-08 19:08:56 -0500283/*
284 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
285 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
286 *
287 * The only place which should ever expand vlib_mains is start_workers()
288 * in threads.c. It knows about the bootstrap vector.
289 */
290/* *INDENT-OFF* */
291static struct
292{
293 vec_header_t h;
294 vlib_main_t *vm;
295} __attribute__ ((packed)) __bootstrap_vlib_main_vector
296 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
297{
298 .h.len = 1,
299 .vm = &vlib_global_main,
300};
301/* *INDENT-ON* */
302
303vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
304
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
306/* When dubugging validate that given buffers are either known allocated
307 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100308void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309vlib_buffer_validate_alloc_free (vlib_main_t * vm,
310 u32 * buffers,
311 uword n_buffers,
312 vlib_buffer_known_state_t expected_state)
313{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 uword i, bi, is_free;
316
317 if (CLIB_DEBUG == 0)
318 return;
319
Damjan Marionc8a26c62017-11-24 20:15:23 +0100320 if (vlib_buffer_callbacks)
321 return;
322
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
324 b = buffers;
325 for (i = 0; i < n_buffers; i++)
326 {
327 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 bi = b[0];
330 b += 1;
Steven899a84b2018-01-29 20:09:09 -0800331 known = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 if (known != expected_state)
333 {
334 ASSERT (0);
335 vlib_panic_with_msg
336 (vm, "%s %U buffer 0x%x",
337 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400338 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 }
340
341 vlib_buffer_set_known_state
Steven899a84b2018-01-29 20:09:09 -0800342 (bi, is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 }
344}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346/* Add buffer free list. */
Damjan Mariondac03522018-02-01 15:30:13 +0100347static vlib_buffer_free_list_index_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348vlib_buffer_create_free_list_helper (vlib_main_t * vm,
349 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100352 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100354 int i;
355
Damjan Marion586afd72017-04-05 19:18:20 +0200356 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100357
Damjan Mariond1274cb2018-03-13 21:32:17 +0100358 if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100359 {
Damjan Mariondac03522018-02-01 15:30:13 +0100360 vlib_buffer_free_list_index_t default_free_free_list_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100361
362 /* *INDENT-OFF* */
363 default_free_free_list_index =
364 vlib_buffer_create_free_list_helper
365 (vm,
366 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
367 /* is_public */ 1,
368 /* is_default */ 1,
369 (u8 *) "default");
370 /* *INDENT-ON* */
371 ASSERT (default_free_free_list_index ==
372 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
373
374 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
375 return default_free_free_list_index;
376 }
377
Damjan Mariond1274cb2018-03-13 21:32:17 +0100378 pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100379
380 memset (f, 0, sizeof (f[0]));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100381 f->index = f - vm->buffer_free_list_pool;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100382 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100383 f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE;
384 f->buffer_pool_index = 0;
Chris Luke7447d072017-07-05 12:57:10 -0400385 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100386
387 /* Setup free buffer template. */
Damjan Marion072401e2017-07-13 18:53:27 +0200388 vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index);
Damjan Marionc47ed032017-01-25 14:18:03 +0100389 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100390
391 if (is_public)
392 {
393 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
394 if (!p)
395 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
396 }
397
398 for (i = 1; i < vec_len (vlib_mains); i++)
399 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100400 vlib_main_t *wvm = vlib_mains[i];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100401 vlib_buffer_free_list_t *wf;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100402 pool_get_aligned (wvm->buffer_free_list_pool,
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100403 wf, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100404 ASSERT (f - vm->buffer_free_list_pool ==
405 wf - wvm->buffer_free_list_pool);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100406 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100407 wf->buffers = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100408 wf->n_alloc = 0;
409 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
411 return f->index;
412}
413
Damjan Mariondac03522018-02-01 15:30:13 +0100414vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
416 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417{
418 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
421 va_start (va, fmt);
422 name = va_format (0, fmt, &va);
423 va_end (va);
424
425 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
426 /* is_public */ 0,
427 /* is_default */ 0,
428 name);
429}
430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431static void
432del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
433{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100434 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (f->buffer_pool_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Damjan Mariond1274cb2018-03-13 21:32:17 +0100436 vec_add_aligned (bp->buffers, f->buffers, vec_len (f->buffers),
437 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 vec_free (f->name);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100439 vec_free (f->buffers);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100440
441 /* Poison it. */
442 memset (f, 0xab, sizeof (f[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443}
444
445/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446void
Damjan Mariondac03522018-02-01 15:30:13 +0100447vlib_buffer_delete_free_list_internal (vlib_main_t * vm,
448 vlib_buffer_free_list_index_t index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100451 int i;
452
Damjan Marion586afd72017-04-05 19:18:20 +0200453 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100454
Damjan Mariondac03522018-02-01 15:30:13 +0100455 f = vlib_buffer_get_free_list (vm, index);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100456
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100457 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100458
459 del_free_list (vm, f);
460
Damjan Mariond1274cb2018-03-13 21:32:17 +0100461 pool_put (vm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100462
463 for (i = 1; i < vec_len (vlib_mains); i++)
464 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100465 vlib_main_t *wvm = vlib_mains[i];
466 f = vlib_buffer_get_free_list (vlib_mains[i], index);
467 del_free_list (wvm, f);
468 pool_put (wvm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100469 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470}
471
Damjan Mariond1274cb2018-03-13 21:32:17 +0100472static_always_inline void *
473vlib_buffer_pool_get_buffer (vlib_buffer_pool_t * bp)
474{
475 uword slot, page, addr;
476
477 if (PREDICT_FALSE (bp->n_elts == bp->n_used))
478 {
479 clib_spinlock_unlock (&bp->lock);
480 return 0;
481 }
482 slot = bp->next_clear;
483 bp->bitmap = clib_bitmap_set (bp->bitmap, slot, 1);
484 bp->next_clear = clib_bitmap_next_clear (bp->bitmap, slot + 1);
485 bp->n_used++;
486
487 page = slot / bp->buffers_per_page;
488 slot -= page * bp->buffers_per_page;
489
490 addr = bp->start + (page << bp->log2_page_size) + slot * bp->buffer_size;
491
492 return uword_to_pointer (addr, void *);
493}
494
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495/* Make sure free list has at least given number of free buffers. */
496static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100497vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
498 vlib_buffer_free_list_t * fl,
499 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100501 vlib_buffer_t *b;
502 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (fl->buffer_pool_index);
503 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400504 u32 *bi;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100505 u32 n_alloc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100508 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 if (n <= 0)
510 return min_free_buffers;
511
Damjan Mariond1274cb2018-03-13 21:32:17 +0100512 if (vec_len (bp->buffers) > 0)
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200513 {
514 int n_copy, n_left;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100515 clib_spinlock_lock (&bp->lock);
516 n_copy = clib_min (vec_len (bp->buffers), n);
517 n_left = vec_len (bp->buffers) - n_copy;
518 vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy,
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200519 CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100520 _vec_len (bp->buffers) = n_left;
521 clib_spinlock_unlock (&bp->lock);
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200522 n = min_free_buffers - vec_len (fl->buffers);
523 if (n <= 0)
524 return min_free_buffers;
525 }
526
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100528 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530 /* Always allocate new buffers in reasonably large sized chunks. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100531 n = clib_max (n, fl->min_n_buffers_each_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Damjan Mariond1274cb2018-03-13 21:32:17 +0100533 clib_spinlock_lock (&bp->lock);
534 while (n_alloc < n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100536 if ((b = vlib_buffer_pool_get_buffer (bp)) == 0)
537 goto done;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400538
Damjan Mariond1274cb2018-03-13 21:32:17 +0100539 n_alloc += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540
Damjan Mariond1274cb2018-03-13 21:32:17 +0100541 vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES);
542 bi[0] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Damjan Mariond1274cb2018-03-13 21:32:17 +0100544 if (CLIB_DEBUG > 0)
545 vlib_buffer_set_known_state (bi[0], VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Damjan Mariond1274cb2018-03-13 21:32:17 +0100547 memset (b, 0, sizeof (vlib_buffer_t));
548 vlib_buffer_init_for_free_list (b, fl);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400549
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 if (fl->buffer_init_function)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100551 fl->buffer_init_function (vm, fl, bi, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100553
554done:
555 clib_spinlock_unlock (&bp->lock);
556 fl->n_alloc += n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 return n_alloc;
558}
559
Dave Barach9b8ffd92016-07-08 08:13:45 -0400560void *
561vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100563 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
566 bm->buffer_free_callback = fp;
567 return rv;
568}
569
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570static_always_inline void
Damjan Marioneac3b112018-03-05 09:36:31 +0100571recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi,
572 vlib_buffer_t * b, u32 follow_buffer_next)
573{
574 vlib_buffer_free_list_t *fl;
575 vlib_buffer_free_list_index_t fi;
576 fl = vlib_buffer_get_buffer_free_list (vm, b, &fi);
577
578 /* The only current use of this callback:
579 * multicast recycle */
580 if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0))
581 {
582 int j;
583
584 vlib_buffer_add_to_free_list (vm, fl, bi,
585 (b->flags & VLIB_BUFFER_RECYCLE) == 0);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100586 for (j = 0; j < vec_len (vm->buffer_announce_list); j++)
Damjan Marioneac3b112018-03-05 09:36:31 +0100587 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100588 if (fl == vm->buffer_announce_list[j])
Damjan Marioneac3b112018-03-05 09:36:31 +0100589 goto already_announced;
590 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100591 vec_add1 (vm->buffer_announce_list, fl);
Damjan Marioneac3b112018-03-05 09:36:31 +0100592 already_announced:
593 ;
594 }
595 else
596 {
597 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0))
598 {
599 u32 flags, next;
600
601 do
602 {
603 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
604 flags = nb->flags;
605 next = nb->next_buffer;
606 if (nb->n_add_refs)
607 nb->n_add_refs--;
608 else
609 {
610 vlib_buffer_validate_alloc_free (vm, &bi, 1,
611 VLIB_BUFFER_KNOWN_ALLOCATED);
612 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
613 }
614 bi = next;
615 }
616 while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT));
617
618 }
619 }
620}
621
622static_always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400624 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100626 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marioneac3b112018-03-05 09:36:31 +0100627 vlib_buffer_t *p, *b0, *b1, *b2, *b3;
628 int i = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
630 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 cb = bm->buffer_free_callback;
633
634 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 return;
639
Damjan Marioneac3b112018-03-05 09:36:31 +0100640 while (i + 11 < n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 {
Damjan Marioneac3b112018-03-05 09:36:31 +0100642 p = vlib_get_buffer (vm, buffers[i + 8]);
643 vlib_prefetch_buffer_header (p, LOAD);
644 p = vlib_get_buffer (vm, buffers[i + 9]);
645 vlib_prefetch_buffer_header (p, LOAD);
646 p = vlib_get_buffer (vm, buffers[i + 10]);
647 vlib_prefetch_buffer_header (p, LOAD);
648 p = vlib_get_buffer (vm, buffers[i + 11]);
649 vlib_prefetch_buffer_header (p, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650
Damjan Marioneac3b112018-03-05 09:36:31 +0100651 b0 = vlib_get_buffer (vm, buffers[i]);
652 b1 = vlib_get_buffer (vm, buffers[i + 1]);
653 b2 = vlib_get_buffer (vm, buffers[i + 2]);
654 b3 = vlib_get_buffer (vm, buffers[i + 3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655
Damjan Marioneac3b112018-03-05 09:36:31 +0100656 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
657 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
658 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
659 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660
Damjan Marioneac3b112018-03-05 09:36:31 +0100661 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
662 recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next);
663 recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next);
664 recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next);
Damjan Marionc47ed032017-01-25 14:18:03 +0100665
Damjan Marioneac3b112018-03-05 09:36:31 +0100666 i += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667 }
Damjan Marioneac3b112018-03-05 09:36:31 +0100668
669 while (i < n_buffers)
670 {
671 b0 = vlib_get_buffer (vm, buffers[i]);
672 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
673 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
674 i++;
675 }
676
Damjan Mariond1274cb2018-03-13 21:32:17 +0100677 if (vec_len (vm->buffer_announce_list))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400679 vlib_buffer_free_list_t *fl;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100680 for (i = 0; i < vec_len (vm->buffer_announce_list); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400681 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100682 fl = vm->buffer_announce_list[i];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400683 fl->buffers_added_to_freelist_function (vm, fl);
684 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100685 _vec_len (vm->buffer_announce_list) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 }
687}
688
Damjan Marion878c6092017-01-04 13:19:27 +0100689static void
690vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400692 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
693 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694}
695
Damjan Marion878c6092017-01-04 13:19:27 +0100696static void
697vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
698 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 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702}
703
704/* Copy template packet data into buffers as they are allocated. */
Damjan Marion878c6092017-01-04 13:19:27 +0100705static void __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706vlib_packet_template_buffer_init (vlib_main_t * vm,
707 vlib_buffer_free_list_t * fl,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400708 u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 vlib_packet_template_t *t =
711 uword_to_pointer (fl->buffer_init_function_opaque,
712 vlib_packet_template_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 uword i;
714
715 for (i = 0; i < n_buffers; i++)
716 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 ASSERT (b->current_length == vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 clib_memcpy (vlib_buffer_get_current (b), t->packet_data,
720 b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 }
722}
723
Dave Barach9b8ffd92016-07-08 08:13:45 -0400724void
725vlib_packet_template_init (vlib_main_t * vm,
726 vlib_packet_template_t * t,
727 void *packet_data,
728 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100729 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100731 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100732 va_list va;
Chris Luke7447d072017-07-05 12:57:10 -0400733 u8 *name;
Damjan Marion878c6092017-01-04 13:19:27 +0100734 vlib_buffer_free_list_t *fl;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100735
736 va_start (va, fmt);
737 name = va_format (0, fmt, &va);
738 va_end (va);
739
Damjan Marion878c6092017-01-04 13:19:27 +0100740 if (bm->cb.vlib_packet_template_init_cb)
741 bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data,
742 n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100743 min_n_buffers_each_alloc, name);
Damjan Marion878c6092017-01-04 13:19:27 +0100744
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100745 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746
747 memset (t, 0, sizeof (t[0]));
748
749 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100750 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
752 t->free_list_index = vlib_buffer_create_free_list_helper
753 (vm, n_packet_data_bytes,
754 /* is_public */ 1,
755 /* is_default */ 0,
756 name);
757
758 ASSERT (t->free_list_index != 0);
759 fl = vlib_buffer_get_free_list (vm, t->free_list_index);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100760 fl->min_n_buffers_each_alloc = t->min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
762 fl->buffer_init_function = vlib_packet_template_buffer_init;
763 fl->buffer_init_function_opaque = pointer_to_uword (t);
764
765 fl->buffer_init_template.current_data = 0;
766 fl->buffer_init_template.current_length = n_packet_data_bytes;
767 fl->buffer_init_template.flags = 0;
Damjan Marionc47ed032017-01-25 14:18:03 +0100768 fl->buffer_init_template.n_add_refs = 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100769 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770}
771
772void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400773vlib_packet_template_get_packet (vlib_main_t * vm,
774 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775{
776 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400777 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778
779 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
780 return 0;
781
782 *bi_result = bi;
783
784 b = vlib_get_buffer (vm, bi);
Damjan Marionf1213b82016-03-13 02:22:06 +0100785 clib_memcpy (vlib_buffer_get_current (b),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400786 t->packet_data, vec_len (t->packet_data));
787 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
789 return b->data;
790}
791
Dave Barach9b8ffd92016-07-08 08:13:45 -0400792void
793vlib_packet_template_get_packet_helper (vlib_main_t * vm,
794 vlib_packet_template_t * t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100796 word n = t->min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 word l = vec_len (t->packet_data);
798 word n_alloc;
799
800 ASSERT (l > 0);
801 ASSERT (vec_len (t->free_buffers) == 0);
802
803 vec_validate (t->free_buffers, n - 1);
804 n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers,
805 n, t->free_list_index);
806 _vec_len (t->free_buffers) = n_alloc;
807}
808
809/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400810u32
811vlib_buffer_add_data (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100812 vlib_buffer_free_list_index_t free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400813 u32 buffer_index, void *data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814{
815 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400816 vlib_buffer_t *b;
817 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818
819 bi = buffer_index;
Dave Barach0d65d112018-03-16 15:21:35 -0400820 if (bi == ~0
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index))
822 goto out_of_buffers;
823
824 d = data;
825 n_left = n_data_bytes;
826 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400827
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 b = vlib_get_buffer (vm, bi);
829 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
830
Dave Barach9b8ffd92016-07-08 08:13:45 -0400831 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
833 b = vlib_get_buffer (vm, b->next_buffer);
834
835 while (1)
836 {
837 u32 n;
838
839 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400840 n_left_this_buffer =
841 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842 n = clib_min (n_left_this_buffer, n_left);
Damjan Marionf1213b82016-03-13 02:22:06 +0100843 clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844 b->current_length += n;
845 n_left -= n;
846 if (n_left == 0)
847 break;
848
849 d += n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 if (1 !=
851 vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1,
852 free_list_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 goto out_of_buffers;
854
855 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
856
857 b = vlib_get_buffer (vm, b->next_buffer);
858 }
859
860 return bi;
861
Dave Barach9b8ffd92016-07-08 08:13:45 -0400862out_of_buffers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 clib_error ("out of buffers");
864 return bi;
865}
866
Pierre Pfister328e99b2016-02-12 13:18:42 +0000867u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400868vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100869 vlib_buffer_free_list_index_t
870 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100872 vlib_buffer_t ** last, void *data,
873 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400874{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000875 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400876 u32 n_buffer_bytes =
877 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000878 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400879 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
880 while (data_len)
881 {
882 u16 max = n_buffer_bytes - l->current_length - l->current_data;
883 if (max == 0)
884 {
885 if (1 !=
886 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
887 free_list_index))
888 return copied;
889 *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer);
890 max = n_buffer_bytes - l->current_length - l->current_data;
891 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000892
Dave Barach9b8ffd92016-07-08 08:13:45 -0400893 u16 len = (data_len > max) ? max : data_len;
894 clib_memcpy (vlib_buffer_get_current (l) + l->current_length,
895 data + copied, len);
896 vlib_buffer_chain_increase_length (first, l, len);
897 data_len -= len;
898 copied += len;
899 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000900 return copied;
901}
902
Damjan Marion149ba772017-10-12 13:09:26 +0200903u8
Damjan Mariond1274cb2018-03-13 21:32:17 +0100904vlib_buffer_pool_create (vlib_main_t * vm, vlib_physmem_region_index_t pri,
905 u16 buffer_size)
Damjan Marion04a7f052017-07-10 15:06:17 +0200906{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100907 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200908 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri);
909 vlib_buffer_pool_t *p;
910 uword start = pointer_to_uword (pr->mem);
911 uword size = pr->size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200912
913 if (bm->buffer_mem_size == 0)
914 {
915 bm->buffer_mem_start = start;
916 bm->buffer_mem_size = size;
917 }
918 else if (start < bm->buffer_mem_start)
919 {
920 bm->buffer_mem_size += bm->buffer_mem_start - start;
921 bm->buffer_mem_start = start;
922 if (size > bm->buffer_mem_size)
923 bm->buffer_mem_size = size;
924 }
925 else if (start > bm->buffer_mem_start)
926 {
927 uword new_size = start - bm->buffer_mem_start + size;
928 if (new_size > bm->buffer_mem_size)
929 bm->buffer_mem_size = new_size;
930 }
931
932 if ((u64) bm->buffer_mem_size >
933 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
934 {
935 clib_panic ("buffer memory size out of range!");
936 }
Damjan Marion149ba772017-10-12 13:09:26 +0200937
938 vec_add2 (bm->buffer_pools, p, 1);
939 p->start = start;
940 p->size = size;
941 p->physmem_region = pri;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100942
943 if (buffer_size == 0)
944 goto done;
945
946 p->log2_page_size = pr->log2_page_size;
947 p->buffer_size = buffer_size;
948 p->buffers_per_page = (1 << pr->log2_page_size) / p->buffer_size;
949 p->n_elts = p->buffers_per_page * pr->n_pages;
950 p->n_used = 0;
951 clib_spinlock_init (&p->lock);
952done:
953 ASSERT (p - bm->buffer_pools < 256);
Damjan Marion149ba772017-10-12 13:09:26 +0200954 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200955}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957static u8 *
958format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400960 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100961 u32 threadnum = va_arg (*va, u32);
962 uword bytes_alloc, bytes_free, n_free, size;
963
964 if (!f)
965 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
966 "Thread", "Name", "Index", "Size", "Alloc", "Free",
967 "#Alloc", "#Free");
968
969 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100970 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100971 bytes_alloc = size * f->n_alloc;
972 bytes_free = size * n_free;
973
Chris Luke7447d072017-07-05 12:57:10 -0400974 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 f->name, f->index, f->n_data_bytes,
976 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400977 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978
979 return s;
980}
981
982static clib_error_t *
983show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400984 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100986 vlib_buffer_free_list_t *f;
987 vlib_main_t *curr_vm;
988 u32 vm_index = 0;
989
990 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
991
992 do
993 {
Dave Barach80f54e22017-03-08 19:08:56 -0500994 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100995
996 /* *INDENT-OFF* */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100997 pool_foreach (f, curr_vm->buffer_free_list_pool, ({
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100998 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
999 }));
1000 /* *INDENT-ON* */
1001
1002 vm_index++;
1003 }
1004 while (vm_index < vec_len (vlib_mains));
1005
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 return 0;
1007}
1008
Dave Barach9b8ffd92016-07-08 08:13:45 -04001009/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010VLIB_CLI_COMMAND (show_buffers_command, static) = {
1011 .path = "show buffers",
1012 .short_help = "Show packet buffer allocation",
1013 .function = show_buffers,
1014};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001015/* *INDENT-ON* */
1016
Damjan Marion49d66f12017-07-20 18:10:35 +02001017clib_error_t *
1018vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001019{
Damjan Mariond1274cb2018-03-13 21:32:17 +01001020 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +02001021 vlib_physmem_region_index_t pri;
Damjan Marion49d66f12017-07-20 18:10:35 +02001022 clib_error_t *error;
1023
Damjan Marion49d66f12017-07-20 18:10:35 +02001024 if (vlib_buffer_callbacks)
1025 {
1026 /* external plugin has registered own buffer callbacks
1027 so we just copy them and quit */
Damjan Marion49d66f12017-07-20 18:10:35 +02001028 clib_memcpy (&bm->cb, vlib_buffer_callbacks,
1029 sizeof (vlib_buffer_callbacks_t));
1030 bm->callbacks_registered = 1;
1031 return 0;
1032 }
Damjan Marion04a7f052017-07-10 15:06:17 +02001033
Damjan Marionc8a26c62017-11-24 20:15:23 +01001034 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +01001035 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
1036 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
1037 bm->cb.vlib_buffer_delete_free_list_cb =
1038 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -04001039 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +02001040
1041 /* allocate default region */
1042 error = vlib_physmem_region_alloc (vm, "buffers",
1043 vlib_buffer_physmem_sz, 0,
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001044 VLIB_PHYSMEM_F_SHARED |
Damjan Mariond1274cb2018-03-13 21:32:17 +01001045 VLIB_PHYSMEM_F_HUGETLB, &pri);
Damjan Marion49d66f12017-07-20 18:10:35 +02001046
1047 if (error == 0)
Damjan Marion149ba772017-10-12 13:09:26 +02001048 goto done;
Damjan Marion49d66f12017-07-20 18:10:35 +02001049
1050 clib_error_free (error);
1051
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001052 error = vlib_physmem_region_alloc (vm, "buffers",
Damjan Marion49d66f12017-07-20 18:10:35 +02001053 vlib_buffer_physmem_sz, 0,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001054 VLIB_PHYSMEM_F_SHARED, &pri);
Damjan Marion149ba772017-10-12 13:09:26 +02001055done:
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001056 if (error == 0)
Damjan Mariond1274cb2018-03-13 21:32:17 +01001057 vlib_buffer_pool_create (vm, pri, sizeof (vlib_buffer_t) +
1058 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
Damjan Marion49d66f12017-07-20 18:10:35 +02001059 return error;
Damjan Marion878c6092017-01-04 13:19:27 +01001060}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +01001061
Damjan Marion49d66f12017-07-20 18:10:35 +02001062static clib_error_t *
1063vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
1064{
1065 u32 size_in_mb;
1066
1067 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1068 {
1069 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
1070 vlib_buffer_physmem_sz = size_in_mb << 20;
1071 else
1072 return unformat_parse_error (input);
1073 }
1074
1075 unformat_free (input);
1076 return 0;
1077}
1078
1079VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
1080
1081
Chris Luked4024f52016-09-06 09:32:36 -04001082/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001083/*
1084 * fd.io coding-style-patch-verification: ON
1085 *
1086 * Local Variables:
1087 * eval: (c-set-style "gnu")
1088 * End:
1089 */