blob: 0251f52bd87a42258748be5c7762a8bc41697f03 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * buffer.c: allocate/free network buffers.
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Chris Luked4024f52016-09-06 09:32:36 -040040/**
Chris Luked4024f52016-09-06 09:32:36 -040041 * @file
42 *
43 * Allocate/free network buffers.
44 */
45
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#include <vlib/vlib.h>
Damjan Marion374e2c52017-03-09 20:38:15 +010047#include <vlib/unix/unix.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Damjan Marion04a7f052017-07-10 15:06:17 +020049vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0;
Damjan Marion901d16c2018-10-23 19:50:20 +020050
51/* when running unpriviledged we are limited by RLIMIT_MEMLOCK which is
52 typically set to 16MB so setting default size for buffer memory to 14MB
53 */
54static u32 vlib_buffer_physmem_sz = 14 << 20;
Damjan Marion04a7f052017-07-10 15:06:17 +020055
Damjan Mariond1274cb2018-03-13 21:32:17 +010056vlib_buffer_main_t buffer_main;
57
Damjan Marion567e61d2018-10-24 17:08:26 +020058/* logging */
59static vlib_log_class_t buffer_log_default;
60
Dave Barach9b8ffd92016-07-08 08:13:45 -040061uword
62vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
63 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064{
Dave Barach9b8ffd92016-07-08 08:13:45 -040065 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 uword l_first = b_first->current_length;
67 uword l = 0;
68 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
69 {
70 b = vlib_get_buffer (vm, b->next_buffer);
71 l += b->current_length;
72 }
73 b_first->total_length_not_including_first_buffer = l;
74 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
75 return l + l_first;
76}
77
Dave Barach9b8ffd92016-07-08 08:13:45 -040078u8 *
79format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barach9b8ffd92016-07-08 08:13:45 -040081 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020082 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010083 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010084
Damjan Mariondac03522018-02-01 15:30:13 +010085#define _(bit, name, v) \
86 if (v && (b->flags & VLIB_BUFFER_##name)) \
87 a = format (a, "%s ", v);
88 foreach_vlib_buffer_flag
89#undef _
Damjan Marion36eb7c22019-01-18 20:45:30 +010090 s = format (s, "current data %d, length %d, buffer-pool %d, "
91 "clone-count %u", b->current_data, b->current_length,
92 b->buffer_pool_index, b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010093
94 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
95 s = format (s, ", totlen-nifb %d",
96 b->total_length_not_including_first_buffer);
97
98 if (b->flags & VLIB_BUFFER_IS_TRACED)
99 s = format (s, ", trace 0x%x", b->trace_index);
100
Damjan Mariondac03522018-02-01 15:30:13 +0100101 if (a)
102 s = format (s, "\n%U%v", format_white_space, indent, a);
103 vec_free (a);
104
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100105 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
106 {
107 vlib_main_t *vm = vlib_get_main ();
108 u32 next_buffer = b->next_buffer;
109 b = vlib_get_buffer (vm, next_buffer);
110
Damjan Marionc47ed032017-01-25 14:18:03 +0100111 s =
112 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
113 format_white_space, indent, next_buffer, b->current_length,
114 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100115 }
116
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 return s;
118}
119
Dave Barach9b8ffd92016-07-08 08:13:45 -0400120u8 *
121format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400123 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
124
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 s = format (s, "%U, %U",
126 format_vlib_buffer, b,
127 format_hex_bytes, vlib_buffer_get_current (b), 64);
128
129 return s;
130}
131
Dave Barach9b8ffd92016-07-08 08:13:45 -0400132static u8 *
133format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134{
135 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138 switch (state)
139 {
140 case VLIB_BUFFER_UNKNOWN:
141 t = "unknown";
142 break;
143
144 case VLIB_BUFFER_KNOWN_ALLOCATED:
145 t = "known-allocated";
146 break;
147
148 case VLIB_BUFFER_KNOWN_FREE:
149 t = "known-free";
150 break;
151
152 default:
153 t = "invalid";
154 break;
155 }
156
157 return format (s, "%s", t);
158}
159
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160u8 *
161format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400163 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
164 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
165
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 while (1)
167 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400168 vec_add (s, vlib_buffer_get_current (b), b->current_length);
169 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 break;
171 b = vlib_get_buffer (vm, b->next_buffer);
172 }
173
174 return s;
175}
176
177static u8 *
178vlib_validate_buffer_helper (vlib_main_t * vm,
179 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 vlib_buffer_free_list_t *fl;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Damjan Marion36eb7c22019-01-18 20:45:30 +0100185 fl = pool_elt_at_index (vm->buffer_free_list_pool,
186 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Dave Barach9b8ffd92016-07-08 08:13:45 -0400188 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191 if (b->current_data + b->current_length > fl->n_data_bytes)
192 return format (0, "%d-%d beyond end of buffer %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 b->current_data, b->current_length, fl->n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 {
197 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Steven899a84b2018-01-29 20:09:09 -0800200 k = vlib_buffer_is_known (b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
202 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
205 if (unique_hash)
206 {
207 if (hash_get (*unique_hash, b->next_buffer))
208 return format (0, "duplicate buffer 0x%x", b->next_buffer);
209
210 hash_set1 (*unique_hash, b->next_buffer);
211 }
212
213 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
214 if (msg)
215 {
216 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
217 vec_free (msg);
218 return result;
219 }
220 }
221
222 return 0;
223}
224
225u8 *
226vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400227{
228 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
229 /* unique_hash */ 0);
230}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231
232u8 *
233vlib_validate_buffers (vlib_main_t * vm,
234 u32 * buffers,
235 uword next_buffer_stride,
236 uword n_buffers,
237 vlib_buffer_known_state_t known_state,
238 uword follow_buffer_next)
239{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400240 uword i, *hash;
241 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400243 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
245 hash = hash_create (0, 0);
246 for (i = 0; i < n_buffers; i++)
247 {
248 bi = b[0];
249 b += next_buffer_stride;
250
251 /* Buffer is not unique. */
252 if (hash_get (hash, bi))
253 {
254 msg = format (0, "not unique");
255 goto done;
256 }
257
Steven899a84b2018-01-29 20:09:09 -0800258 k = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 if (k != known_state)
260 {
261 msg = format (0, "is %U; expected %U",
262 format_vlib_buffer_known_state, k,
263 format_vlib_buffer_known_state, known_state);
264 goto done;
265 }
266
267 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
268 if (msg)
269 goto done;
270
271 hash_set1 (hash, bi);
272 }
273
Dave Barach9b8ffd92016-07-08 08:13:45 -0400274done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 if (msg)
276 {
277 result = format (0, "0x%x: %v", bi, msg);
278 vec_free (msg);
279 }
280 hash_free (hash);
281 return result;
282}
283
Dave Barach80f54e22017-03-08 19:08:56 -0500284/*
285 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
286 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
287 *
288 * The only place which should ever expand vlib_mains is start_workers()
289 * in threads.c. It knows about the bootstrap vector.
290 */
291/* *INDENT-OFF* */
292static struct
293{
294 vec_header_t h;
295 vlib_main_t *vm;
296} __attribute__ ((packed)) __bootstrap_vlib_main_vector
297 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
298{
299 .h.len = 1,
300 .vm = &vlib_global_main,
301};
302/* *INDENT-ON* */
303
304vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
305
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
307/* When dubugging validate that given buffers are either known allocated
308 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100309void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310vlib_buffer_validate_alloc_free (vlib_main_t * vm,
311 u32 * buffers,
312 uword n_buffers,
313 vlib_buffer_known_state_t expected_state)
314{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400315 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 uword i, bi, is_free;
317
318 if (CLIB_DEBUG == 0)
319 return;
320
Damjan Marionc8a26c62017-11-24 20:15:23 +0100321 if (vlib_buffer_callbacks)
322 return;
323
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
325 b = buffers;
326 for (i = 0; i < n_buffers; i++)
327 {
328 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400329
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 bi = b[0];
331 b += 1;
Steven899a84b2018-01-29 20:09:09 -0800332 known = vlib_buffer_is_known (bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 if (known != expected_state)
334 {
335 ASSERT (0);
336 vlib_panic_with_msg
337 (vm, "%s %U buffer 0x%x",
338 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400339 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 }
341
342 vlib_buffer_set_known_state
Steven899a84b2018-01-29 20:09:09 -0800343 (bi, is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 }
345}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347/* Add buffer free list. */
Damjan Mariondac03522018-02-01 15:30:13 +0100348static vlib_buffer_free_list_index_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349vlib_buffer_create_free_list_helper (vlib_main_t * vm,
350 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400351 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100353 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100355 int i;
356
Damjan Marion586afd72017-04-05 19:18:20 +0200357 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100358
Damjan Mariond1274cb2018-03-13 21:32:17 +0100359 if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100360 {
Damjan Mariondac03522018-02-01 15:30:13 +0100361 vlib_buffer_free_list_index_t default_free_free_list_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100362
363 /* *INDENT-OFF* */
364 default_free_free_list_index =
365 vlib_buffer_create_free_list_helper
366 (vm,
367 /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
368 /* is_public */ 1,
369 /* is_default */ 1,
370 (u8 *) "default");
371 /* *INDENT-ON* */
372 ASSERT (default_free_free_list_index ==
373 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
374
375 if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public)
376 return default_free_free_list_index;
377 }
378
Damjan Mariond1274cb2018-03-13 21:32:17 +0100379 pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100380
Dave Barachb7b92992018-10-17 10:38:51 -0400381 clib_memset (f, 0, sizeof (f[0]));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100382 f->index = f - vm->buffer_free_list_pool;
Eyal Barice55bcd2018-11-25 15:42:47 +0200383 vec_validate (f->buffers, 0);
384 vec_reset_length (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100385 f->n_data_bytes = vlib_buffer_round_size (n_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100386 f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE;
387 f->buffer_pool_index = 0;
Chris Luke7447d072017-07-05 12:57:10 -0400388 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100389
390 /* Setup free buffer template. */
Damjan Marionc47ed032017-01-25 14:18:03 +0100391 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100392
393 if (is_public)
394 {
395 uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes);
396 if (!p)
397 hash_set (bm->free_list_by_size, f->n_data_bytes, f->index);
398 }
399
400 for (i = 1; i < vec_len (vlib_mains); i++)
401 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100402 vlib_main_t *wvm = vlib_mains[i];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100403 vlib_buffer_free_list_t *wf;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100404 pool_get_aligned (wvm->buffer_free_list_pool,
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100405 wf, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100406 ASSERT (f - vm->buffer_free_list_pool ==
407 wf - wvm->buffer_free_list_pool);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100408 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100409 wf->buffers = 0;
Eyal Barice55bcd2018-11-25 15:42:47 +0200410 vec_validate (wf->buffers, 0);
411 vec_reset_length (wf->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100412 wf->n_alloc = 0;
413 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
415 return f->index;
416}
417
Damjan Mariondac03522018-02-01 15:30:13 +0100418vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
420 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421{
422 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
425 va_start (va, fmt);
426 name = va_format (0, fmt, &va);
427 va_end (va);
428
429 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
430 /* is_public */ 0,
431 /* is_default */ 0,
432 name);
433}
434
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435static void
436del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f)
437{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100438 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (f->buffer_pool_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Damjan Mariond1274cb2018-03-13 21:32:17 +0100440 vec_add_aligned (bp->buffers, f->buffers, vec_len (f->buffers),
441 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 vec_free (f->name);
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100443 vec_free (f->buffers);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100444
445 /* Poison it. */
Dave Barachb7b92992018-10-17 10:38:51 -0400446 clib_memset (f, 0xab, sizeof (f[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447}
448
449/* Add buffer free list. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450void
Damjan Mariondac03522018-02-01 15:30:13 +0100451vlib_buffer_delete_free_list_internal (vlib_main_t * vm,
452 vlib_buffer_free_list_index_t index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100455 int i;
456
Damjan Marion586afd72017-04-05 19:18:20 +0200457 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100458
Damjan Mariondac03522018-02-01 15:30:13 +0100459 f = vlib_buffer_get_free_list (vm, index);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100460
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100461 ASSERT (vec_len (f->buffers) == f->n_alloc);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100462
463 del_free_list (vm, f);
464
Damjan Mariond1274cb2018-03-13 21:32:17 +0100465 pool_put (vm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100466
467 for (i = 1; i < vec_len (vlib_mains); i++)
468 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100469 vlib_main_t *wvm = vlib_mains[i];
470 f = vlib_buffer_get_free_list (vlib_mains[i], index);
471 del_free_list (wvm, f);
472 pool_put (wvm->buffer_free_list_pool, f);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100473 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474}
475
Damjan Mariond1274cb2018-03-13 21:32:17 +0100476static_always_inline void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200477vlib_buffer_pool_get_buffer (vlib_main_t * vm, vlib_buffer_pool_t * bp)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100478{
Damjan Marion68b4da62018-09-30 18:26:20 +0200479 return vlib_physmem_alloc_from_map (vm, bp->physmem_map_index,
480 bp->buffer_size, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100481}
482
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483/* Make sure free list has at least given number of free buffers. */
484static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100485vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
486 vlib_buffer_free_list_t * fl,
487 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100489 vlib_buffer_t *b;
490 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (fl->buffer_pool_index);
491 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 u32 *bi;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100493 u32 n_alloc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100496 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 if (n <= 0)
498 return min_free_buffers;
499
Damjan Mariond1274cb2018-03-13 21:32:17 +0100500 if (vec_len (bp->buffers) > 0)
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200501 {
502 int n_copy, n_left;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100503 clib_spinlock_lock (&bp->lock);
504 n_copy = clib_min (vec_len (bp->buffers), n);
505 n_left = vec_len (bp->buffers) - n_copy;
506 vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy,
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200507 CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100508 _vec_len (bp->buffers) = n_left;
509 clib_spinlock_unlock (&bp->lock);
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200510 n = min_free_buffers - vec_len (fl->buffers);
511 if (n <= 0)
512 return min_free_buffers;
513 }
514
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100516 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
518 /* Always allocate new buffers in reasonably large sized chunks. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100519 n = clib_max (n, fl->min_n_buffers_each_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Damjan Mariond1274cb2018-03-13 21:32:17 +0100521 clib_spinlock_lock (&bp->lock);
522 while (n_alloc < n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200524 if ((b = vlib_buffer_pool_get_buffer (vm, bp)) == 0)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100525 goto done;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526
Damjan Mariond1274cb2018-03-13 21:32:17 +0100527 n_alloc += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Damjan Mariond1274cb2018-03-13 21:32:17 +0100529 vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES);
530 bi[0] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Damjan Mariond1274cb2018-03-13 21:32:17 +0100532 if (CLIB_DEBUG > 0)
533 vlib_buffer_set_known_state (bi[0], VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Dave Barachb7b92992018-10-17 10:38:51 -0400535 clib_memset (b, 0, sizeof (vlib_buffer_t));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100536 vlib_buffer_init_for_free_list (b, fl);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 if (fl->buffer_init_function)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100539 fl->buffer_init_function (vm, fl, bi, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100541
542done:
543 clib_spinlock_unlock (&bp->lock);
544 fl->n_alloc += n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 return n_alloc;
546}
547
Dave Barach9b8ffd92016-07-08 08:13:45 -0400548void *
549vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100551 vlib_buffer_main_t *bm = &buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
554 bm->buffer_free_callback = fp;
555 return rv;
556}
557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558static_always_inline void
Damjan Marioneac3b112018-03-05 09:36:31 +0100559recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi,
560 vlib_buffer_t * b, u32 follow_buffer_next)
561{
562 vlib_buffer_free_list_t *fl;
Damjan Marion8e715292018-09-03 15:41:45 +0200563 u32 flags, next;
564
Damjan Marion36eb7c22019-01-18 20:45:30 +0100565 fl = pool_elt_at_index (vm->buffer_free_list_pool,
566 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Damjan Marioneac3b112018-03-05 09:36:31 +0100567
Damjan Marion8e715292018-09-03 15:41:45 +0200568 do
Damjan Marioneac3b112018-03-05 09:36:31 +0100569 {
Damjan Marion8e715292018-09-03 15:41:45 +0200570 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
571 flags = nb->flags;
572 next = nb->next_buffer;
573 if (nb->n_add_refs)
574 nb->n_add_refs--;
575 else
Damjan Marioneac3b112018-03-05 09:36:31 +0100576 {
Damjan Marion8e715292018-09-03 15:41:45 +0200577 vlib_buffer_validate_alloc_free (vm, &bi, 1,
578 VLIB_BUFFER_KNOWN_ALLOCATED);
579 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
Damjan Marioneac3b112018-03-05 09:36:31 +0100580 }
Damjan Marion8e715292018-09-03 15:41:45 +0200581 bi = next;
Damjan Marioneac3b112018-03-05 09:36:31 +0100582 }
Damjan Marion8e715292018-09-03 15:41:45 +0200583 while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT));
Damjan Marioneac3b112018-03-05 09:36:31 +0100584}
585
586static_always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100590 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marioneac3b112018-03-05 09:36:31 +0100591 vlib_buffer_t *p, *b0, *b1, *b2, *b3;
592 int i = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
594 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596 cb = bm->buffer_free_callback;
597
598 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400599 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 return;
603
Damjan Marioneac3b112018-03-05 09:36:31 +0100604 while (i + 11 < n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605 {
Damjan Marioneac3b112018-03-05 09:36:31 +0100606 p = vlib_get_buffer (vm, buffers[i + 8]);
607 vlib_prefetch_buffer_header (p, LOAD);
608 p = vlib_get_buffer (vm, buffers[i + 9]);
609 vlib_prefetch_buffer_header (p, LOAD);
610 p = vlib_get_buffer (vm, buffers[i + 10]);
611 vlib_prefetch_buffer_header (p, LOAD);
612 p = vlib_get_buffer (vm, buffers[i + 11]);
613 vlib_prefetch_buffer_header (p, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Damjan Marioneac3b112018-03-05 09:36:31 +0100615 b0 = vlib_get_buffer (vm, buffers[i]);
616 b1 = vlib_get_buffer (vm, buffers[i + 1]);
617 b2 = vlib_get_buffer (vm, buffers[i + 2]);
618 b3 = vlib_get_buffer (vm, buffers[i + 3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Damjan Marioneac3b112018-03-05 09:36:31 +0100620 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
621 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
622 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
623 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
Damjan Marioneac3b112018-03-05 09:36:31 +0100625 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
626 recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next);
627 recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next);
628 recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next);
Damjan Marionc47ed032017-01-25 14:18:03 +0100629
Damjan Marioneac3b112018-03-05 09:36:31 +0100630 i += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631 }
Damjan Marioneac3b112018-03-05 09:36:31 +0100632
633 while (i < n_buffers)
634 {
635 b0 = vlib_get_buffer (vm, buffers[i]);
636 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
637 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
638 i++;
639 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640}
641
Damjan Marion878c6092017-01-04 13:19:27 +0100642static void
643vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
646 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647}
648
Damjan Marion878c6092017-01-04 13:19:27 +0100649static void
650vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
651 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
654 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655}
656
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657void
658vlib_packet_template_init (vlib_main_t * vm,
659 vlib_packet_template_t * t,
660 void *packet_data,
661 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100662 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100664 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100665
666 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100667 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100668 va_end (va);
669
670 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Dave Barachb7b92992018-10-17 10:38:51 -0400672 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
674 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100675 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Damjan Marion878c6092017-01-04 13:19:27 +0100677 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678}
679
680void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400681vlib_packet_template_get_packet (vlib_main_t * vm,
682 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683{
684 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400685 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686
687 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
688 return 0;
689
690 *bi_result = bi;
691
692 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500693 clib_memcpy_fast (vlib_buffer_get_current (b),
694 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696
697 return b->data;
698}
699
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500701int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100702vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
703 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704{
705 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400706 vlib_buffer_t *b;
707 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708
Dave Barach3a63fc52019-01-07 09:15:47 -0500709 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100710 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 goto out_of_buffers;
712
713 d = data;
714 n_left = n_data_bytes;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100715 n_buffer_bytes = VLIB_BUFFER_DATA_SIZE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 b = vlib_get_buffer (vm, bi);
718 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
719
Dave Barach9b8ffd92016-07-08 08:13:45 -0400720 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
722 b = vlib_get_buffer (vm, b->next_buffer);
723
724 while (1)
725 {
726 u32 n;
727
728 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 n_left_this_buffer =
730 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500732 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
733 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 b->current_length += n;
735 n_left -= n;
736 if (n_left == 0)
737 break;
738
739 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100740 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 goto out_of_buffers;
742
743 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
744
745 b = vlib_get_buffer (vm, b->next_buffer);
746 }
747
Dave Barach3a63fc52019-01-07 09:15:47 -0500748 *buffer_index = bi;
749 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500752 clib_warning ("out of buffers");
753 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754}
755
Pierre Pfister328e99b2016-02-12 13:18:42 +0000756u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100758 vlib_buffer_free_list_index_t
759 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400760 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100761 vlib_buffer_t ** last, void *data,
762 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400763{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000764 vlib_buffer_t *l = *last;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400765 u32 n_buffer_bytes =
766 vlib_buffer_free_list_buffer_size (vm, free_list_index);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000767 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
769 while (data_len)
770 {
771 u16 max = n_buffer_bytes - l->current_length - l->current_data;
772 if (max == 0)
773 {
774 if (1 !=
775 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
776 free_list_index))
777 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200778 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779 max = n_buffer_bytes - l->current_length - l->current_data;
780 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000781
Dave Barach9b8ffd92016-07-08 08:13:45 -0400782 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500783 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
784 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400785 vlib_buffer_chain_increase_length (first, l, len);
786 data_len -= len;
787 copied += len;
788 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000789 return copied;
790}
791
Damjan Marion149ba772017-10-12 13:09:26 +0200792u8
Damjan Marion68b4da62018-09-30 18:26:20 +0200793vlib_buffer_register_physmem_map (vlib_main_t * vm, u32 physmem_map_index)
Damjan Marion04a7f052017-07-10 15:06:17 +0200794{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100795 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200796 vlib_buffer_pool_t *p;
Damjan Marion68b4da62018-09-30 18:26:20 +0200797 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
798 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200799 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200800
801 if (bm->buffer_mem_size == 0)
802 {
803 bm->buffer_mem_start = start;
804 bm->buffer_mem_size = size;
805 }
806 else if (start < bm->buffer_mem_start)
807 {
808 bm->buffer_mem_size += bm->buffer_mem_start - start;
809 bm->buffer_mem_start = start;
810 if (size > bm->buffer_mem_size)
811 bm->buffer_mem_size = size;
812 }
813 else if (start > bm->buffer_mem_start)
814 {
815 uword new_size = start - bm->buffer_mem_start + size;
816 if (new_size > bm->buffer_mem_size)
817 bm->buffer_mem_size = new_size;
818 }
819
820 if ((u64) bm->buffer_mem_size >
821 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
822 {
823 clib_panic ("buffer memory size out of range!");
824 }
Damjan Marion149ba772017-10-12 13:09:26 +0200825
826 vec_add2 (bm->buffer_pools, p, 1);
827 p->start = start;
828 p->size = size;
Damjan Marion68b4da62018-09-30 18:26:20 +0200829 p->physmem_map_index = physmem_map_index;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100830
Damjan Mariond1274cb2018-03-13 21:32:17 +0100831 ASSERT (p - bm->buffer_pools < 256);
Damjan Marion149ba772017-10-12 13:09:26 +0200832 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200833}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
Dave Barach9b8ffd92016-07-08 08:13:45 -0400835static u8 *
836format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400838 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100839 u32 threadnum = va_arg (*va, u32);
840 uword bytes_alloc, bytes_free, n_free, size;
841
842 if (!f)
843 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
844 "Thread", "Name", "Index", "Size", "Alloc", "Free",
845 "#Alloc", "#Free");
846
847 size = sizeof (vlib_buffer_t) + f->n_data_bytes;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100848 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100849 bytes_alloc = size * f->n_alloc;
850 bytes_free = size * n_free;
851
Chris Luke7447d072017-07-05 12:57:10 -0400852 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 f->name, f->index, f->n_data_bytes,
854 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400855 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856
857 return s;
858}
859
860static clib_error_t *
861show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400862 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100864 vlib_buffer_free_list_t *f;
865 vlib_main_t *curr_vm;
866 u32 vm_index = 0;
867
868 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
869
870 do
871 {
Dave Barach80f54e22017-03-08 19:08:56 -0500872 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100873
874 /* *INDENT-OFF* */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100875 pool_foreach (f, curr_vm->buffer_free_list_pool, ({
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100876 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
877 }));
878 /* *INDENT-ON* */
879
880 vm_index++;
881 }
882 while (vm_index < vec_len (vlib_mains));
883
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 return 0;
885}
886
Dave Barach9b8ffd92016-07-08 08:13:45 -0400887/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888VLIB_CLI_COMMAND (show_buffers_command, static) = {
889 .path = "show buffers",
890 .short_help = "Show packet buffer allocation",
891 .function = show_buffers,
892};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400893/* *INDENT-ON* */
894
Damjan Marion49d66f12017-07-20 18:10:35 +0200895clib_error_t *
896vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100897{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100898 vlib_buffer_main_t *bm = &buffer_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200899 clib_error_t *error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200900 u32 physmem_map_index;
901 u8 pool_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200902 int log2_page_size = 0;
903
904 buffer_log_default = vlib_log_register_class ("buffer", 0);
Damjan Marion49d66f12017-07-20 18:10:35 +0200905
Damjan Marion49d66f12017-07-20 18:10:35 +0200906 if (vlib_buffer_callbacks)
907 {
908 /* external plugin has registered own buffer callbacks
909 so we just copy them and quit */
Dave Barach178cf492018-11-13 16:34:13 -0500910 clib_memcpy_fast (&bm->cb, vlib_buffer_callbacks,
911 sizeof (vlib_buffer_callbacks_t));
Damjan Marion49d66f12017-07-20 18:10:35 +0200912 bm->callbacks_registered = 1;
913 return 0;
914 }
Damjan Marion04a7f052017-07-10 15:06:17 +0200915
Damjan Marionc8a26c62017-11-24 20:15:23 +0100916 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +0100917 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
918 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
919 bm->cb.vlib_buffer_delete_free_list_cb =
920 &vlib_buffer_delete_free_list_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -0400921 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +0200922
Damjan Marion567e61d2018-10-24 17:08:26 +0200923retry:
924 error = vlib_physmem_shared_map_create (vm, "buffers",
925 vlib_buffer_physmem_sz,
926 log2_page_size,
927 CLIB_PMALLOC_NUMA_LOCAL,
928 &physmem_map_index);
929
930 if (error && log2_page_size == 0)
931 {
932 vlib_log_warn (buffer_log_default, "%U", format_clib_error, error);
933 clib_error_free (error);
934 vlib_log_warn (buffer_log_default, "falling back to non-hugepage "
935 "backed buffer pool");
936 log2_page_size = min_log2 (clib_mem_get_page_size ());
937 goto retry;
938 }
939
940 if (error)
Damjan Marion68b4da62018-09-30 18:26:20 +0200941 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200942
Damjan Marion68b4da62018-09-30 18:26:20 +0200943 pool_index = vlib_buffer_register_physmem_map (vm, physmem_map_index);
944 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (pool_index);
945 clib_spinlock_init (&bp->lock);
946 bp->buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES +
947 sizeof (vlib_buffer_t);
Damjan Marion49d66f12017-07-20 18:10:35 +0200948
Damjan Marion68b4da62018-09-30 18:26:20 +0200949 return 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100950}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100951
Damjan Marion49d66f12017-07-20 18:10:35 +0200952static clib_error_t *
953vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
954{
955 u32 size_in_mb;
956
957 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
958 {
959 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
960 vlib_buffer_physmem_sz = size_in_mb << 20;
961 else
962 return unformat_parse_error (input);
963 }
964
965 unformat_free (input);
966 return 0;
967}
968
969VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
970
971
Chris Luked4024f52016-09-06 09:32:36 -0400972/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400973/*
974 * fd.io coding-style-patch-verification: ON
975 *
976 * Local Variables:
977 * eval: (c-set-style "gnu")
978 * End:
979 */