blob: 643f2c503424525131e2fc9f19ce58f2ab5f2d3e [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 Marion567e61d2018-10-24 17:08:26 +020056/* logging */
57static vlib_log_class_t buffer_log_default;
58
Dave Barach9b8ffd92016-07-08 08:13:45 -040059uword
60vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
61 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
Dave Barach9b8ffd92016-07-08 08:13:45 -040063 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 uword l_first = b_first->current_length;
65 uword l = 0;
66 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
67 {
68 b = vlib_get_buffer (vm, b->next_buffer);
69 l += b->current_length;
70 }
71 b_first->total_length_not_including_first_buffer = l;
72 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
73 return l + l_first;
74}
75
Dave Barach9b8ffd92016-07-08 08:13:45 -040076u8 *
77format_vlib_buffer (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
Dave Barach9b8ffd92016-07-08 08:13:45 -040079 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020080 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010081 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010082
Damjan Mariondac03522018-02-01 15:30:13 +010083#define _(bit, name, v) \
84 if (v && (b->flags & VLIB_BUFFER_##name)) \
85 a = format (a, "%s ", v);
86 foreach_vlib_buffer_flag
87#undef _
Damjan Marion36eb7c22019-01-18 20:45:30 +010088 s = format (s, "current data %d, length %d, buffer-pool %d, "
89 "clone-count %u", b->current_data, b->current_length,
90 b->buffer_pool_index, b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010091
92 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
93 s = format (s, ", totlen-nifb %d",
94 b->total_length_not_including_first_buffer);
95
96 if (b->flags & VLIB_BUFFER_IS_TRACED)
97 s = format (s, ", trace 0x%x", b->trace_index);
98
Damjan Mariondac03522018-02-01 15:30:13 +010099 if (a)
100 s = format (s, "\n%U%v", format_white_space, indent, a);
101 vec_free (a);
102
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100103 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
104 {
105 vlib_main_t *vm = vlib_get_main ();
106 u32 next_buffer = b->next_buffer;
107 b = vlib_get_buffer (vm, next_buffer);
108
Damjan Marionc47ed032017-01-25 14:18:03 +0100109 s =
110 format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u",
111 format_white_space, indent, next_buffer, b->current_length,
112 b->n_add_refs);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100113 }
114
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 return s;
116}
117
Dave Barach9b8ffd92016-07-08 08:13:45 -0400118u8 *
119format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
122
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 s = format (s, "%U, %U",
124 format_vlib_buffer, b,
125 format_hex_bytes, vlib_buffer_get_current (b), 64);
126
127 return s;
128}
129
Dave Barach9b8ffd92016-07-08 08:13:45 -0400130static u8 *
131format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132{
133 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400134 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
136 switch (state)
137 {
138 case VLIB_BUFFER_UNKNOWN:
139 t = "unknown";
140 break;
141
142 case VLIB_BUFFER_KNOWN_ALLOCATED:
143 t = "known-allocated";
144 break;
145
146 case VLIB_BUFFER_KNOWN_FREE:
147 t = "known-free";
148 break;
149
150 default:
151 t = "invalid";
152 break;
153 }
154
155 return format (s, "%s", t);
156}
157
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158u8 *
159format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
162 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
163
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 while (1)
165 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400166 vec_add (s, vlib_buffer_get_current (b), b->current_length);
167 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 break;
169 b = vlib_get_buffer (vm, b->next_buffer);
170 }
171
172 return s;
173}
174
175static u8 *
176vlib_validate_buffer_helper (vlib_main_t * vm,
177 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400178 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100184
Damjan Marion32353822019-01-20 02:24:53 +0100185 if (b->current_data + b->current_length > VLIB_BUFFER_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 return format (0, "%d-%d beyond end of buffer %d",
Damjan Marion32353822019-01-20 02:24:53 +0100187 b->current_data, b->current_length, VLIB_BUFFER_DATA_SIZE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Dave Barach9b8ffd92016-07-08 08:13:45 -0400189 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 {
191 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Damjan Mariond50e3472019-01-20 00:03:56 +0100194 k = vlib_buffer_is_known (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
196 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
199 if (unique_hash)
200 {
201 if (hash_get (*unique_hash, b->next_buffer))
202 return format (0, "duplicate buffer 0x%x", b->next_buffer);
203
204 hash_set1 (*unique_hash, b->next_buffer);
205 }
206
207 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
208 if (msg)
209 {
210 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
211 vec_free (msg);
212 return result;
213 }
214 }
215
216 return 0;
217}
218
219u8 *
220vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221{
222 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
223 /* unique_hash */ 0);
224}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
226u8 *
227vlib_validate_buffers (vlib_main_t * vm,
228 u32 * buffers,
229 uword next_buffer_stride,
230 uword n_buffers,
231 vlib_buffer_known_state_t known_state,
232 uword follow_buffer_next)
233{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400234 uword i, *hash;
235 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400237 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
239 hash = hash_create (0, 0);
240 for (i = 0; i < n_buffers; i++)
241 {
242 bi = b[0];
243 b += next_buffer_stride;
244
245 /* Buffer is not unique. */
246 if (hash_get (hash, bi))
247 {
248 msg = format (0, "not unique");
249 goto done;
250 }
251
Damjan Mariond50e3472019-01-20 00:03:56 +0100252 k = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 if (k != known_state)
254 {
255 msg = format (0, "is %U; expected %U",
256 format_vlib_buffer_known_state, k,
257 format_vlib_buffer_known_state, known_state);
258 goto done;
259 }
260
261 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
262 if (msg)
263 goto done;
264
265 hash_set1 (hash, bi);
266 }
267
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 if (msg)
270 {
271 result = format (0, "0x%x: %v", bi, msg);
272 vec_free (msg);
273 }
274 hash_free (hash);
275 return result;
276}
277
Dave Barach80f54e22017-03-08 19:08:56 -0500278/*
279 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
280 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
281 *
282 * The only place which should ever expand vlib_mains is start_workers()
283 * in threads.c. It knows about the bootstrap vector.
284 */
285/* *INDENT-OFF* */
286static struct
287{
288 vec_header_t h;
289 vlib_main_t *vm;
290} __attribute__ ((packed)) __bootstrap_vlib_main_vector
291 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
292{
293 .h.len = 1,
294 .vm = &vlib_global_main,
295};
296/* *INDENT-ON* */
297
298vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
299
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
301/* When dubugging validate that given buffers are either known allocated
302 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100303void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304vlib_buffer_validate_alloc_free (vlib_main_t * vm,
305 u32 * buffers,
306 uword n_buffers,
307 vlib_buffer_known_state_t expected_state)
308{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400309 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 uword i, bi, is_free;
311
312 if (CLIB_DEBUG == 0)
313 return;
314
Damjan Marionc8a26c62017-11-24 20:15:23 +0100315 if (vlib_buffer_callbacks)
316 return;
317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
319 b = buffers;
320 for (i = 0; i < n_buffers; i++)
321 {
322 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400323
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 bi = b[0];
325 b += 1;
Damjan Mariond50e3472019-01-20 00:03:56 +0100326 known = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 if (known != expected_state)
328 {
329 ASSERT (0);
330 vlib_panic_with_msg
331 (vm, "%s %U buffer 0x%x",
332 is_free ? "freeing" : "allocating",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400333 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 }
335
Damjan Mariond50e3472019-01-20 00:03:56 +0100336 vlib_buffer_set_known_state (vm, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
337 VLIB_BUFFER_KNOWN_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 }
339}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341/* Add buffer free list. */
Damjan Mariondac03522018-02-01 15:30:13 +0100342static vlib_buffer_free_list_index_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343vlib_buffer_create_free_list_helper (vlib_main_t * vm,
344 u32 n_data_bytes,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345 u32 is_public, u32 is_default, u8 * name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346{
Damjan Mariond50e3472019-01-20 00:03:56 +0100347 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400348 vlib_buffer_free_list_t *f;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100349 int i;
350
Damjan Marion586afd72017-04-05 19:18:20 +0200351 ASSERT (vlib_get_thread_index () == 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100352
Damjan Mariond1274cb2018-03-13 21:32:17 +0100353 if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100354 {
Damjan Mariondac03522018-02-01 15:30:13 +0100355 vlib_buffer_free_list_index_t default_free_free_list_index;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100356
357 /* *INDENT-OFF* */
358 default_free_free_list_index =
359 vlib_buffer_create_free_list_helper
360 (vm,
Damjan Marion32353822019-01-20 02:24:53 +0100361 /* default buffer size */ VLIB_BUFFER_DATA_SIZE,
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100362 /* is_public */ 1,
363 /* is_default */ 1,
364 (u8 *) "default");
365 /* *INDENT-ON* */
366 ASSERT (default_free_free_list_index ==
367 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
368
Damjan Marion32353822019-01-20 02:24:53 +0100369 if (n_data_bytes == VLIB_BUFFER_DATA_SIZE && is_public)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100370 return default_free_free_list_index;
371 }
372
Damjan Mariond1274cb2018-03-13 21:32:17 +0100373 pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100374
Dave Barachb7b92992018-10-17 10:38:51 -0400375 clib_memset (f, 0, sizeof (f[0]));
Damjan Mariond1274cb2018-03-13 21:32:17 +0100376 f->index = f - vm->buffer_free_list_pool;
Eyal Barice55bcd2018-11-25 15:42:47 +0200377 vec_validate (f->buffers, 0);
378 vec_reset_length (f->buffers);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100379 f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE;
380 f->buffer_pool_index = 0;
Chris Luke7447d072017-07-05 12:57:10 -0400381 f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100382
383 /* Setup free buffer template. */
Damjan Marionc47ed032017-01-25 14:18:03 +0100384 f->buffer_init_template.n_add_refs = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100385
386 if (is_public)
387 {
Damjan Marion32353822019-01-20 02:24:53 +0100388 uword *p = hash_get (bm->free_list_by_size, VLIB_BUFFER_DATA_SIZE);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100389 if (!p)
Damjan Marion32353822019-01-20 02:24:53 +0100390 hash_set (bm->free_list_by_size, VLIB_BUFFER_DATA_SIZE, f->index);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100391 }
392
393 for (i = 1; i < vec_len (vlib_mains); i++)
394 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100395 vlib_main_t *wvm = vlib_mains[i];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100396 vlib_buffer_free_list_t *wf;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100397 pool_get_aligned (wvm->buffer_free_list_pool,
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100398 wf, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100399 ASSERT (f - vm->buffer_free_list_pool ==
400 wf - wvm->buffer_free_list_pool);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100401 wf[0] = f[0];
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100402 wf->buffers = 0;
Eyal Barice55bcd2018-11-25 15:42:47 +0200403 vec_validate (wf->buffers, 0);
404 vec_reset_length (wf->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100405 wf->n_alloc = 0;
406 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
408 return f->index;
409}
410
Damjan Mariondac03522018-02-01 15:30:13 +0100411vlib_buffer_free_list_index_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes,
413 char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414{
415 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
418 va_start (va, fmt);
419 name = va_format (0, fmt, &va);
420 va_end (va);
421
422 return vlib_buffer_create_free_list_helper (vm, n_data_bytes,
423 /* is_public */ 0,
424 /* is_default */ 0,
425 name);
426}
427
Damjan Mariond1274cb2018-03-13 21:32:17 +0100428static_always_inline void *
Damjan Marion68b4da62018-09-30 18:26:20 +0200429vlib_buffer_pool_get_buffer (vlib_main_t * vm, vlib_buffer_pool_t * bp)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100430{
Damjan Marion68b4da62018-09-30 18:26:20 +0200431 return vlib_physmem_alloc_from_map (vm, bp->physmem_map_index,
432 bp->buffer_size, CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100433}
434
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435/* Make sure free list has at least given number of free buffers. */
436static uword
Damjan Marionc8a26c62017-11-24 20:15:23 +0100437vlib_buffer_fill_free_list_internal (vlib_main_t * vm,
438 vlib_buffer_free_list_t * fl,
439 uword min_free_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440{
Damjan Mariond1274cb2018-03-13 21:32:17 +0100441 vlib_buffer_t *b;
Damjan Mariond50e3472019-01-20 00:03:56 +0100442 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (vm, fl->buffer_pool_index);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100443 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 u32 *bi;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100445 u32 n_alloc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 /* Already have enough free buffers on free list? */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100448 n = min_free_buffers - vec_len (fl->buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 if (n <= 0)
450 return min_free_buffers;
451
Damjan Mariond1274cb2018-03-13 21:32:17 +0100452 if (vec_len (bp->buffers) > 0)
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200453 {
454 int n_copy, n_left;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100455 clib_spinlock_lock (&bp->lock);
456 n_copy = clib_min (vec_len (bp->buffers), n);
457 n_left = vec_len (bp->buffers) - n_copy;
458 vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy,
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200459 CLIB_CACHE_LINE_BYTES);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100460 _vec_len (bp->buffers) = n_left;
461 clib_spinlock_unlock (&bp->lock);
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200462 n = min_free_buffers - vec_len (fl->buffers);
463 if (n <= 0)
464 return min_free_buffers;
465 }
466
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 /* Always allocate round number of buffers. */
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100468 n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
470 /* Always allocate new buffers in reasonably large sized chunks. */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100471 n = clib_max (n, fl->min_n_buffers_each_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Damjan Mariond1274cb2018-03-13 21:32:17 +0100473 clib_spinlock_lock (&bp->lock);
474 while (n_alloc < n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 {
Damjan Marion68b4da62018-09-30 18:26:20 +0200476 if ((b = vlib_buffer_pool_get_buffer (vm, bp)) == 0)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100477 goto done;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400478
Damjan Mariond1274cb2018-03-13 21:32:17 +0100479 n_alloc += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
Damjan Mariond1274cb2018-03-13 21:32:17 +0100481 vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES);
482 bi[0] = vlib_get_buffer_index (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
Damjan Mariond1274cb2018-03-13 21:32:17 +0100484 if (CLIB_DEBUG > 0)
Damjan Mariond50e3472019-01-20 00:03:56 +0100485 vlib_buffer_set_known_state (vm, bi[0], VLIB_BUFFER_KNOWN_FREE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
Dave Barachb7b92992018-10-17 10:38:51 -0400487 clib_memset (b, 0, sizeof (vlib_buffer_t));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400488
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 if (fl->buffer_init_function)
Damjan Mariond1274cb2018-03-13 21:32:17 +0100490 fl->buffer_init_function (vm, fl, bi, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 }
Damjan Mariond1274cb2018-03-13 21:32:17 +0100492
493done:
494 clib_spinlock_unlock (&bp->lock);
495 fl->n_alloc += n_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 return n_alloc;
497}
498
Dave Barach9b8ffd92016-07-08 08:13:45 -0400499void *
500vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501{
Damjan Mariond50e3472019-01-20 00:03:56 +0100502 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400503 void *rv = bm->buffer_free_callback;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
505 bm->buffer_free_callback = fp;
506 return rv;
507}
508
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509static_always_inline void
Damjan Marioneac3b112018-03-05 09:36:31 +0100510recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi,
511 vlib_buffer_t * b, u32 follow_buffer_next)
512{
513 vlib_buffer_free_list_t *fl;
Damjan Marion8e715292018-09-03 15:41:45 +0200514 u32 flags, next;
515
Damjan Marion36eb7c22019-01-18 20:45:30 +0100516 fl = pool_elt_at_index (vm->buffer_free_list_pool,
517 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Damjan Marioneac3b112018-03-05 09:36:31 +0100518
Damjan Marion8e715292018-09-03 15:41:45 +0200519 do
Damjan Marioneac3b112018-03-05 09:36:31 +0100520 {
Damjan Marion8e715292018-09-03 15:41:45 +0200521 vlib_buffer_t *nb = vlib_get_buffer (vm, bi);
522 flags = nb->flags;
523 next = nb->next_buffer;
524 if (nb->n_add_refs)
525 nb->n_add_refs--;
526 else
Damjan Marioneac3b112018-03-05 09:36:31 +0100527 {
Damjan Marion8e715292018-09-03 15:41:45 +0200528 vlib_buffer_validate_alloc_free (vm, &bi, 1,
529 VLIB_BUFFER_KNOWN_ALLOCATED);
530 vlib_buffer_add_to_free_list (vm, fl, bi, 1);
Damjan Marioneac3b112018-03-05 09:36:31 +0100531 }
Damjan Marion8e715292018-09-03 15:41:45 +0200532 bi = next;
Damjan Marioneac3b112018-03-05 09:36:31 +0100533 }
Damjan Marion8e715292018-09-03 15:41:45 +0200534 while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT));
Damjan Marioneac3b112018-03-05 09:36:31 +0100535}
536
537static_always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538vlib_buffer_free_inline (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 u32 * buffers, u32 n_buffers, u32 follow_buffer_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540{
Damjan Mariond50e3472019-01-20 00:03:56 +0100541 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marioneac3b112018-03-05 09:36:31 +0100542 vlib_buffer_t *p, *b0, *b1, *b2, *b3;
543 int i = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544 u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
545 u32 follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 cb = bm->buffer_free_callback;
548
549 if (PREDICT_FALSE (cb != 0))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552 if (!n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 return;
554
Damjan Marioneac3b112018-03-05 09:36:31 +0100555 while (i + 11 < n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 {
Damjan Marioneac3b112018-03-05 09:36:31 +0100557 p = vlib_get_buffer (vm, buffers[i + 8]);
558 vlib_prefetch_buffer_header (p, LOAD);
559 p = vlib_get_buffer (vm, buffers[i + 9]);
560 vlib_prefetch_buffer_header (p, LOAD);
561 p = vlib_get_buffer (vm, buffers[i + 10]);
562 vlib_prefetch_buffer_header (p, LOAD);
563 p = vlib_get_buffer (vm, buffers[i + 11]);
564 vlib_prefetch_buffer_header (p, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Damjan Marioneac3b112018-03-05 09:36:31 +0100566 b0 = vlib_get_buffer (vm, buffers[i]);
567 b1 = vlib_get_buffer (vm, buffers[i + 1]);
568 b2 = vlib_get_buffer (vm, buffers[i + 2]);
569 b3 = vlib_get_buffer (vm, buffers[i + 3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Damjan Marioneac3b112018-03-05 09:36:31 +0100571 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
572 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
573 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
574 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Damjan Marioneac3b112018-03-05 09:36:31 +0100576 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
577 recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next);
578 recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next);
579 recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next);
Damjan Marionc47ed032017-01-25 14:18:03 +0100580
Damjan Marioneac3b112018-03-05 09:36:31 +0100581 i += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 }
Damjan Marioneac3b112018-03-05 09:36:31 +0100583
584 while (i < n_buffers)
585 {
586 b0 = vlib_get_buffer (vm, buffers[i]);
587 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
588 recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next);
589 i++;
590 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591}
592
Damjan Marion878c6092017-01-04 13:19:27 +0100593static void
594vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
597 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598}
599
Damjan Marion878c6092017-01-04 13:19:27 +0100600static void
601vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers,
602 u32 n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400604 vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */
605 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606}
607
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608void
609vlib_packet_template_init (vlib_main_t * vm,
610 vlib_packet_template_t * t,
611 void *packet_data,
612 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100613 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100615 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100616
617 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100618 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100619 va_end (va);
620
621 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
Dave Barachb7b92992018-10-17 10:38:51 -0400623 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
625 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100626 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
Damjan Marion878c6092017-01-04 13:19:27 +0100628 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629}
630
631void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632vlib_packet_template_get_packet (vlib_main_t * vm,
633 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634{
635 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637
638 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
639 return 0;
640
641 *bi_result = bi;
642
643 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500644 clib_memcpy_fast (vlib_buffer_get_current (b),
645 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
648 return b->data;
649}
650
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500652int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100653vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
654 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655{
656 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 vlib_buffer_t *b;
658 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Dave Barach3a63fc52019-01-07 09:15:47 -0500660 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100661 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662 goto out_of_buffers;
663
664 d = data;
665 n_left = n_data_bytes;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100666 n_buffer_bytes = VLIB_BUFFER_DATA_SIZE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400667
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 b = vlib_get_buffer (vm, bi);
669 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
670
Dave Barach9b8ffd92016-07-08 08:13:45 -0400671 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
673 b = vlib_get_buffer (vm, b->next_buffer);
674
675 while (1)
676 {
677 u32 n;
678
679 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 n_left_this_buffer =
681 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500683 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
684 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685 b->current_length += n;
686 n_left -= n;
687 if (n_left == 0)
688 break;
689
690 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100691 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 goto out_of_buffers;
693
694 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
695
696 b = vlib_get_buffer (vm, b->next_buffer);
697 }
698
Dave Barach3a63fc52019-01-07 09:15:47 -0500699 *buffer_index = bi;
700 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701
Dave Barach9b8ffd92016-07-08 08:13:45 -0400702out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500703 clib_warning ("out of buffers");
704 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705}
706
Pierre Pfister328e99b2016-02-12 13:18:42 +0000707u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400708vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100709 vlib_buffer_free_list_index_t
710 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400711 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100712 vlib_buffer_t ** last, void *data,
713 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000715 vlib_buffer_t *l = *last;
Damjan Marion32353822019-01-20 02:24:53 +0100716 u32 n_buffer_bytes = VLIB_BUFFER_DATA_SIZE;
Pierre Pfister328e99b2016-02-12 13:18:42 +0000717 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400718 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
719 while (data_len)
720 {
721 u16 max = n_buffer_bytes - l->current_length - l->current_data;
722 if (max == 0)
723 {
724 if (1 !=
725 vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1,
726 free_list_index))
727 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200728 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 max = n_buffer_bytes - l->current_length - l->current_data;
730 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500733 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
734 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400735 vlib_buffer_chain_increase_length (first, l, len);
736 data_len -= len;
737 copied += len;
738 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000739 return copied;
740}
741
Damjan Marion149ba772017-10-12 13:09:26 +0200742u8
Damjan Marion68b4da62018-09-30 18:26:20 +0200743vlib_buffer_register_physmem_map (vlib_main_t * vm, u32 physmem_map_index)
Damjan Marion04a7f052017-07-10 15:06:17 +0200744{
Damjan Mariond50e3472019-01-20 00:03:56 +0100745 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion149ba772017-10-12 13:09:26 +0200746 vlib_buffer_pool_t *p;
Damjan Marion68b4da62018-09-30 18:26:20 +0200747 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
748 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200749 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion04a7f052017-07-10 15:06:17 +0200750
751 if (bm->buffer_mem_size == 0)
752 {
753 bm->buffer_mem_start = start;
754 bm->buffer_mem_size = size;
755 }
756 else if (start < bm->buffer_mem_start)
757 {
758 bm->buffer_mem_size += bm->buffer_mem_start - start;
759 bm->buffer_mem_start = start;
760 if (size > bm->buffer_mem_size)
761 bm->buffer_mem_size = size;
762 }
763 else if (start > bm->buffer_mem_start)
764 {
765 uword new_size = start - bm->buffer_mem_start + size;
766 if (new_size > bm->buffer_mem_size)
767 bm->buffer_mem_size = new_size;
768 }
769
770 if ((u64) bm->buffer_mem_size >
771 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
772 {
773 clib_panic ("buffer memory size out of range!");
774 }
Damjan Marion149ba772017-10-12 13:09:26 +0200775
776 vec_add2 (bm->buffer_pools, p, 1);
777 p->start = start;
778 p->size = size;
Damjan Marion68b4da62018-09-30 18:26:20 +0200779 p->physmem_map_index = physmem_map_index;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100780
Damjan Mariond1274cb2018-03-13 21:32:17 +0100781 ASSERT (p - bm->buffer_pools < 256);
Damjan Marion149ba772017-10-12 13:09:26 +0200782 return p - bm->buffer_pools;
Damjan Marion04a7f052017-07-10 15:06:17 +0200783}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784
Dave Barach9b8ffd92016-07-08 08:13:45 -0400785static u8 *
786format_vlib_buffer_free_list (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400788 vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100789 u32 threadnum = va_arg (*va, u32);
790 uword bytes_alloc, bytes_free, n_free, size;
791
792 if (!f)
793 return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s",
794 "Thread", "Name", "Index", "Size", "Alloc", "Free",
795 "#Alloc", "#Free");
796
Damjan Marion32353822019-01-20 02:24:53 +0100797 size = sizeof (vlib_buffer_t) + VLIB_BUFFER_DATA_SIZE;
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100798 n_free = vec_len (f->buffers);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100799 bytes_alloc = size * f->n_alloc;
800 bytes_free = size * n_free;
801
Chris Luke7447d072017-07-05 12:57:10 -0400802 s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum,
Damjan Marion32353822019-01-20 02:24:53 +0100803 f->name, f->index, VLIB_BUFFER_DATA_SIZE,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804 format_memory_size, bytes_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400805 format_memory_size, bytes_free, f->n_alloc, n_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
807 return s;
808}
809
810static clib_error_t *
811show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400812 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100814 vlib_buffer_free_list_t *f;
815 vlib_main_t *curr_vm;
816 u32 vm_index = 0;
817
818 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0);
819
820 do
821 {
Dave Barach80f54e22017-03-08 19:08:56 -0500822 curr_vm = vlib_mains[vm_index];
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100823
824 /* *INDENT-OFF* */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100825 pool_foreach (f, curr_vm->buffer_free_list_pool, ({
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100826 vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index);
827 }));
828 /* *INDENT-ON* */
829
830 vm_index++;
831 }
832 while (vm_index < vec_len (vlib_mains));
833
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834 return 0;
835}
836
Dave Barach9b8ffd92016-07-08 08:13:45 -0400837/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838VLIB_CLI_COMMAND (show_buffers_command, static) = {
839 .path = "show buffers",
840 .short_help = "Show packet buffer allocation",
841 .function = show_buffers,
842};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400843/* *INDENT-ON* */
844
Damjan Marion49d66f12017-07-20 18:10:35 +0200845clib_error_t *
846vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100847{
Damjan Mariond50e3472019-01-20 00:03:56 +0100848 vlib_buffer_main_t *bm;
Damjan Marion49d66f12017-07-20 18:10:35 +0200849 clib_error_t *error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200850 u32 physmem_map_index;
851 u8 pool_index;
Damjan Marion567e61d2018-10-24 17:08:26 +0200852 int log2_page_size = 0;
853
854 buffer_log_default = vlib_log_register_class ("buffer", 0);
Damjan Marion49d66f12017-07-20 18:10:35 +0200855
Damjan Mariond50e3472019-01-20 00:03:56 +0100856 bm = vm->buffer_main = clib_mem_alloc (sizeof (bm[0]));
857 clib_memset (bm, 0, sizeof (bm[0]));
858
Damjan Marion49d66f12017-07-20 18:10:35 +0200859 if (vlib_buffer_callbacks)
860 {
861 /* external plugin has registered own buffer callbacks
862 so we just copy them and quit */
Dave Barach178cf492018-11-13 16:34:13 -0500863 clib_memcpy_fast (&bm->cb, vlib_buffer_callbacks,
864 sizeof (vlib_buffer_callbacks_t));
Damjan Marion49d66f12017-07-20 18:10:35 +0200865 bm->callbacks_registered = 1;
866 return 0;
867 }
Damjan Marion04a7f052017-07-10 15:06:17 +0200868
Damjan Marionc8a26c62017-11-24 20:15:23 +0100869 bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal;
Damjan Marion878c6092017-01-04 13:19:27 +0100870 bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal;
871 bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal;
Damjan Marion6b0f5892017-07-27 04:01:24 -0400872 clib_spinlock_init (&bm->buffer_known_hash_lockp);
Damjan Marion49d66f12017-07-20 18:10:35 +0200873
Damjan Marion567e61d2018-10-24 17:08:26 +0200874retry:
875 error = vlib_physmem_shared_map_create (vm, "buffers",
876 vlib_buffer_physmem_sz,
877 log2_page_size,
878 CLIB_PMALLOC_NUMA_LOCAL,
879 &physmem_map_index);
880
881 if (error && log2_page_size == 0)
882 {
883 vlib_log_warn (buffer_log_default, "%U", format_clib_error, error);
884 clib_error_free (error);
885 vlib_log_warn (buffer_log_default, "falling back to non-hugepage "
886 "backed buffer pool");
887 log2_page_size = min_log2 (clib_mem_get_page_size ());
888 goto retry;
889 }
890
891 if (error)
Damjan Marion68b4da62018-09-30 18:26:20 +0200892 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200893
Damjan Marion68b4da62018-09-30 18:26:20 +0200894 pool_index = vlib_buffer_register_physmem_map (vm, physmem_map_index);
Damjan Mariond50e3472019-01-20 00:03:56 +0100895 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (vm, pool_index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200896 clib_spinlock_init (&bp->lock);
Damjan Marion32353822019-01-20 02:24:53 +0100897 bp->buffer_size = VLIB_BUFFER_DATA_SIZE + sizeof (vlib_buffer_t);
Damjan Marion49d66f12017-07-20 18:10:35 +0200898
Damjan Marion68b4da62018-09-30 18:26:20 +0200899 return 0;
Damjan Marion878c6092017-01-04 13:19:27 +0100900}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100901
Damjan Marion49d66f12017-07-20 18:10:35 +0200902static clib_error_t *
903vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
904{
905 u32 size_in_mb;
906
907 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
908 {
909 if (unformat (input, "memory-size-in-mb %d", &size_in_mb))
910 vlib_buffer_physmem_sz = size_in_mb << 20;
911 else
912 return unformat_parse_error (input);
913 }
914
915 unformat_free (input);
916 return 0;
917}
918
919VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
920
921
Chris Luked4024f52016-09-06 09:32:36 -0400922/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400923/*
924 * fd.io coding-style-patch-verification: ON
925 *
926 * Local Variables:
927 * eval: (c-set-style "gnu")
928 * End:
929 */