blob: 82fe6412781666a20d1599435c8a912446aad98c [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
Damjan Marion910d3692019-01-21 11:48:34 +010046#include <vppinfra/linux/sysfs.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#include <vlib/vlib.h>
Damjan Marion374e2c52017-03-09 20:38:15 +010048#include <vlib/unix/unix.h>
Damjan Marion8973b072022-03-01 15:51:18 +010049#include <vlib/stats/stats.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Damjan Marion910d3692019-01-21 11:48:34 +010051#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384
52#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192
Damjan Marion901d16c2018-10-23 19:50:20 +020053
Damjan Marion910d3692019-01-21 11:48:34 +010054#ifdef CLIB_HAVE_VEC128
55/* Assumptions by vlib_buffer_free_inline: */
56STATIC_ASSERT_FITS_IN (vlib_buffer_t, flags, 16);
57STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16);
58STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16);
59#endif
Damjan Marion04a7f052017-07-10 15:06:17 +020060
Damjan Marion910d3692019-01-21 11:48:34 +010061u16 __vlib_buffer_external_hdr_size = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +020062
Dave Barach9b8ffd92016-07-08 08:13:45 -040063uword
64vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
65 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
Dave Barach9b8ffd92016-07-08 08:13:45 -040067 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 uword l_first = b_first->current_length;
69 uword l = 0;
70 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
71 {
72 b = vlib_get_buffer (vm, b->next_buffer);
73 l += b->current_length;
74 }
75 b_first->total_length_not_including_first_buffer = l;
76 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
77 return l + l_first;
78}
79
Dave Barach9b8ffd92016-07-08 08:13:45 -040080u8 *
Benoît Ganne43543172019-10-21 15:13:54 +020081format_vlib_buffer_no_chain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070082{
Dave Barach9b8ffd92016-07-08 08:13:45 -040083 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020084 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010085 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010086
Damjan Mariondac03522018-02-01 15:30:13 +010087#define _(bit, name, v) \
88 if (v && (b->flags & VLIB_BUFFER_##name)) \
89 a = format (a, "%s ", v);
90 foreach_vlib_buffer_flag
91#undef _
Damjan Marion36eb7c22019-01-18 20:45:30 +010092 s = format (s, "current data %d, length %d, buffer-pool %d, "
Damjan Marion910d3692019-01-21 11:48:34 +010093 "ref-count %u", b->current_data, b->current_length,
94 b->buffer_pool_index, b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010095
Mohsin Kazmi30d46e72022-01-11 11:34:56 +000096 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010097 s = format (s, ", totlen-nifb %d",
98 b->total_length_not_including_first_buffer);
99
100 if (b->flags & VLIB_BUFFER_IS_TRACED)
Dave Baracha638c182019-06-21 18:24:07 -0400101 s = format (s, ", trace handle 0x%x", b->trace_handle);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100102
Damjan Mariondac03522018-02-01 15:30:13 +0100103 if (a)
104 s = format (s, "\n%U%v", format_white_space, indent, a);
105 vec_free (a);
106
Benoît Ganne43543172019-10-21 15:13:54 +0200107 return s;
108}
109
110u8 *
111format_vlib_buffer (u8 * s, va_list * args)
112{
113 vlib_main_t *vm = vlib_get_main ();
114 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
115 u32 indent = format_get_indent (s);
116
117 s = format (s, "%U", format_vlib_buffer_no_chain, b);
118
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100119 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
120 {
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100121 u32 next_buffer = b->next_buffer;
122 b = vlib_get_buffer (vm, next_buffer);
123
Damjan Marionc47ed032017-01-25 14:18:03 +0100124 s =
Damjan Marion910d3692019-01-21 11:48:34 +0100125 format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
Damjan Marionc47ed032017-01-25 14:18:03 +0100126 format_white_space, indent, next_buffer, b->current_length,
Damjan Marion910d3692019-01-21 11:48:34 +0100127 b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100128 }
129
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 return s;
131}
132
Dave Barach9b8ffd92016-07-08 08:13:45 -0400133u8 *
134format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
137
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 s = format (s, "%U, %U",
139 format_vlib_buffer, b,
140 format_hex_bytes, vlib_buffer_get_current (b), 64);
141
142 return s;
143}
144
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145static u8 *
146format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147{
148 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
151 switch (state)
152 {
153 case VLIB_BUFFER_UNKNOWN:
154 t = "unknown";
155 break;
156
157 case VLIB_BUFFER_KNOWN_ALLOCATED:
158 t = "known-allocated";
159 break;
160
161 case VLIB_BUFFER_KNOWN_FREE:
162 t = "known-free";
163 break;
164
165 default:
166 t = "invalid";
167 break;
168 }
169
170 return format (s, "%s", t);
171}
172
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173u8 *
174format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
177 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 while (1)
180 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181 vec_add (s, vlib_buffer_get_current (b), b->current_length);
182 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 break;
184 b = vlib_get_buffer (vm, b->next_buffer);
185 }
186
187 return s;
188}
189
190static u8 *
191vlib_validate_buffer_helper (vlib_main_t * vm,
192 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194{
Damjan Marion910d3692019-01-21 11:48:34 +0100195 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Damjan Marion910d3692019-01-21 11:48:34 +0100198 if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
199 return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
200
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100203
Damjan Marion8934a042019-02-09 23:29:26 +0100204 if (b->current_data + b->current_length >
205 vlib_buffer_get_default_data_size (vm))
Damjan Marion910d3692019-01-21 11:48:34 +0100206 return format (0, "%d-%d beyond end of buffer %d", b->current_data,
Damjan Marion8934a042019-02-09 23:29:26 +0100207 b->current_length, vlib_buffer_get_default_data_size (vm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Dave Barach9b8ffd92016-07-08 08:13:45 -0400209 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 {
211 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400212 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Damjan Mariond50e3472019-01-20 00:03:56 +0100214 k = vlib_buffer_is_known (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
216 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219 if (unique_hash)
220 {
221 if (hash_get (*unique_hash, b->next_buffer))
222 return format (0, "duplicate buffer 0x%x", b->next_buffer);
223
224 hash_set1 (*unique_hash, b->next_buffer);
225 }
226
227 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
228 if (msg)
229 {
230 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
231 vec_free (msg);
232 return result;
233 }
234 }
235
236 return 0;
237}
238
239u8 *
240vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400241{
242 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
243 /* unique_hash */ 0);
244}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
246u8 *
247vlib_validate_buffers (vlib_main_t * vm,
248 u32 * buffers,
249 uword next_buffer_stride,
250 uword n_buffers,
251 vlib_buffer_known_state_t known_state,
252 uword follow_buffer_next)
253{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254 uword i, *hash;
255 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
259 hash = hash_create (0, 0);
260 for (i = 0; i < n_buffers; i++)
261 {
262 bi = b[0];
263 b += next_buffer_stride;
264
265 /* Buffer is not unique. */
266 if (hash_get (hash, bi))
267 {
268 msg = format (0, "not unique");
269 goto done;
270 }
271
Damjan Mariond50e3472019-01-20 00:03:56 +0100272 k = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 if (k != known_state)
274 {
275 msg = format (0, "is %U; expected %U",
276 format_vlib_buffer_known_state, k,
277 format_vlib_buffer_known_state, known_state);
278 goto done;
279 }
280
281 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
282 if (msg)
283 goto done;
284
285 hash_set1 (hash, bi);
286 }
287
Dave Barach9b8ffd92016-07-08 08:13:45 -0400288done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289 if (msg)
290 {
291 result = format (0, "0x%x: %v", bi, msg);
292 vec_free (msg);
293 }
294 hash_free (hash);
295 return result;
296}
297
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700298/* When debugging validate that given buffers are either known allocated
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100300void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301vlib_buffer_validate_alloc_free (vlib_main_t * vm,
302 u32 * buffers,
303 uword n_buffers,
304 vlib_buffer_known_state_t expected_state)
305{
Damjan Marion910d3692019-01-21 11:48:34 +0100306 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400307 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 uword i, bi, is_free;
309
310 if (CLIB_DEBUG == 0)
311 return;
312
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
314 b = buffers;
315 for (i = 0; i < n_buffers; i++)
316 {
317 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400318
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 bi = b[0];
320 b += 1;
Damjan Mariond50e3472019-01-20 00:03:56 +0100321 known = vlib_buffer_is_known (vm, bi);
Damjan Marion910d3692019-01-21 11:48:34 +0100322
323 if (known == VLIB_BUFFER_UNKNOWN &&
324 expected_state == VLIB_BUFFER_KNOWN_FREE)
325 known = VLIB_BUFFER_KNOWN_FREE;
326
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 if (known != expected_state)
328 {
Klement Sekerae82c73a2019-01-30 18:29:28 +0100329 clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
330 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 }
332
Damjan Marion910d3692019-01-21 11:48:34 +0100333 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
334 hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
335 VLIB_BUFFER_KNOWN_ALLOCATED);
336 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 }
338}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
Dave Barach9b8ffd92016-07-08 08:13:45 -0400340void
341vlib_packet_template_init (vlib_main_t * vm,
342 vlib_packet_template_t * t,
343 void *packet_data,
344 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100345 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100347 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100348
349 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100350 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100351 va_end (va);
352
353 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barachb7b92992018-10-17 10:38:51 -0400355 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
357 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100358 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Damjan Marion878c6092017-01-04 13:19:27 +0100359 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360}
361
362void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400363vlib_packet_template_get_packet (vlib_main_t * vm,
364 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365{
366 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
369 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
370 return 0;
371
372 *bi_result = bi;
373
374 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500375 clib_memcpy_fast (vlib_buffer_get_current (b),
376 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
379 return b->data;
380}
381
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500383int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100384vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
385 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386{
387 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 vlib_buffer_t *b;
389 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach3a63fc52019-01-07 09:15:47 -0500391 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100392 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 goto out_of_buffers;
394
395 d = data;
396 n_left = n_data_bytes;
Damjan Marion8934a042019-02-09 23:29:26 +0100397 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 b = vlib_get_buffer (vm, bi);
400 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
401
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
404 b = vlib_get_buffer (vm, b->next_buffer);
405
406 while (1)
407 {
408 u32 n;
409
410 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400411 n_left_this_buffer =
412 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500414 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
415 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 b->current_length += n;
417 n_left -= n;
418 if (n_left == 0)
419 break;
420
421 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100422 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 goto out_of_buffers;
424
425 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
426
427 b = vlib_get_buffer (vm, b->next_buffer);
428 }
429
Dave Barach3a63fc52019-01-07 09:15:47 -0500430 *buffer_index = bi;
431 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500434 clib_warning ("out of buffers");
435 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436}
437
Pierre Pfister328e99b2016-02-12 13:18:42 +0000438u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100441 vlib_buffer_t ** last, void *data,
442 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000444 vlib_buffer_t *l = *last;
Damjan Marion8934a042019-02-09 23:29:26 +0100445 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000446 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
448 while (data_len)
449 {
450 u16 max = n_buffer_bytes - l->current_length - l->current_data;
451 if (max == 0)
452 {
Damjan Marion910d3692019-01-21 11:48:34 +0100453 if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
454 first->buffer_pool_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200456 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457 max = n_buffer_bytes - l->current_length - l->current_data;
458 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000459
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500461 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
462 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400463 vlib_buffer_chain_increase_length (first, l, len);
464 data_len -= len;
465 copied += len;
466 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000467 return copied;
468}
469
Benoît Gannec16fe462020-06-15 12:24:36 +0200470static uword
471vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size)
472{
Damjan Marion0be1b762020-09-04 12:34:58 +0200473 uword alloc_size = ext_hdr_size + sizeof (vlib_buffer_t) + data_size;
Damjan Marion038dad72024-01-19 21:19:57 +0100474 alloc_size = round_pow2 (alloc_size, VLIB_BUFFER_ALIGN);
Damjan Marion0be1b762020-09-04 12:34:58 +0200475
476 return alloc_size;
Benoît Gannec16fe462020-06-15 12:24:36 +0200477}
478
Damjan Marionb592d1b2019-02-28 23:16:11 +0100479u8
Damjan Marion65dc34b2023-10-06 10:59:32 +0200480vlib_buffer_pool_create (vlib_main_t *vm, u32 data_size, u32 physmem_map_index,
481 char *fmt, ...)
Damjan Marion04a7f052017-07-10 15:06:17 +0200482{
Damjan Mariond50e3472019-01-20 00:03:56 +0100483 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100484 vlib_buffer_pool_t *bp;
Damjan Marion68b4da62018-09-30 18:26:20 +0200485 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
486 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200487 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion65dc34b2023-10-06 10:59:32 +0200488 uword page_mask = ~pow2_mask (m->log2_page_size);
489 u8 *p;
490 u32 alloc_size;
491 va_list va;
Damjan Marion910d3692019-01-21 11:48:34 +0100492
Damjan Marionb592d1b2019-02-28 23:16:11 +0100493 if (vec_len (bm->buffer_pools) >= 255)
494 return ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100495
Tianyu Li70b1cbd2021-06-23 07:35:03 +0000496 vec_add2_aligned (bm->buffer_pools, bp, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion04a7f052017-07-10 15:06:17 +0200497
498 if (bm->buffer_mem_size == 0)
499 {
500 bm->buffer_mem_start = start;
501 bm->buffer_mem_size = size;
502 }
503 else if (start < bm->buffer_mem_start)
504 {
505 bm->buffer_mem_size += bm->buffer_mem_start - start;
506 bm->buffer_mem_start = start;
507 if (size > bm->buffer_mem_size)
508 bm->buffer_mem_size = size;
509 }
510 else if (start > bm->buffer_mem_start)
511 {
512 uword new_size = start - bm->buffer_mem_start + size;
513 if (new_size > bm->buffer_mem_size)
514 bm->buffer_mem_size = new_size;
515 }
516
517 if ((u64) bm->buffer_mem_size >
518 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
519 {
520 clib_panic ("buffer memory size out of range!");
521 }
Damjan Marion149ba772017-10-12 13:09:26 +0200522
Damjan Marion910d3692019-01-21 11:48:34 +0100523 bp->start = start;
524 bp->size = size;
525 bp->index = bp - bm->buffer_pools;
526 bp->buffer_template.buffer_pool_index = bp->index;
527 bp->buffer_template.ref_count = 1;
528 bp->physmem_map_index = physmem_map_index;
Damjan Marion910d3692019-01-21 11:48:34 +0100529 bp->data_size = data_size;
530 bp->numa_node = m->numa_node;
Damjan Marion65dc34b2023-10-06 10:59:32 +0200531 bp->log2_page_size = m->log2_page_size;
532
533 va_start (va, fmt);
534 bp->name = va_format (0, fmt, &va);
535 va_end (va);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100536
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100537 vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
Damjan Marion910d3692019-01-21 11:48:34 +0100538 CLIB_CACHE_LINE_BYTES);
539
Benoît Gannec16fe462020-06-15 12:24:36 +0200540 alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size);
Damjan Marion65dc34b2023-10-06 10:59:32 +0200541 bp->alloc_size = alloc_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100542
543 /* preallocate buffer indices memory */
Damjan Marion65dc34b2023-10-06 10:59:32 +0200544 bp->buffers = clib_mem_alloc_aligned (
545 round_pow2 ((size / alloc_size) * sizeof (u32), CLIB_CACHE_LINE_BYTES),
546 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100547
548 clib_spinlock_init (&bp->lock);
549
Damjan Marion65dc34b2023-10-06 10:59:32 +0200550 p = m->base;
Damjan Marion910d3692019-01-21 11:48:34 +0100551
Damjan Marion65dc34b2023-10-06 10:59:32 +0200552 /* start with naturally aligned address */
553 p += alloc_size - (uword) p % alloc_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100554
Damjan Marion65dc34b2023-10-06 10:59:32 +0200555 /*
556 * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
557 * Allows various places to ASSERT (bi != 0). Much easier
558 * than debugging downstream crashes in successor nodes.
559 */
560 if (p == m->base)
561 p += alloc_size;
Dave Barachc74b43c2020-04-09 17:24:07 -0400562
Damjan Marion65dc34b2023-10-06 10:59:32 +0200563 for (; p < (u8 *) m->base + size - alloc_size; p += alloc_size)
564 {
565 vlib_buffer_t *b;
566 u32 bi;
Damjan Marion910d3692019-01-21 11:48:34 +0100567
Damjan Marion65dc34b2023-10-06 10:59:32 +0200568 /* skip if buffer spans across page boundary */
569 if (((uword) p & page_mask) != ((uword) (p + alloc_size) & page_mask))
570 continue;
Damjan Marion910d3692019-01-21 11:48:34 +0100571
Damjan Marion65dc34b2023-10-06 10:59:32 +0200572 b = (vlib_buffer_t *) (p + bm->ext_hdr_size);
Damjan Marionbf236632023-10-13 09:59:00 +0000573 b->template = bp->buffer_template;
Damjan Marion65dc34b2023-10-06 10:59:32 +0200574 bi = vlib_get_buffer_index (vm, b);
575 bp->buffers[bp->n_avail++] = bi;
576 vlib_get_buffer (vm, bi);
577 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100578
Damjan Marion65dc34b2023-10-06 10:59:32 +0200579 bp->n_buffers = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100580
Damjan Marionb592d1b2019-02-28 23:16:11 +0100581 return bp->index;
Damjan Marion04a7f052017-07-10 15:06:17 +0200582}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584static u8 *
Damjan Marion910d3692019-01-21 11:48:34 +0100585format_vlib_buffer_pool (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586{
Damjan Marion910d3692019-01-21 11:48:34 +0100587 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
588 vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
589 vlib_buffer_pool_thread_t *bpt;
590 u32 cached = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100591
Damjan Marion910d3692019-01-21 11:48:34 +0100592 if (!bp)
593 return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
594 "Pool Name", "Index", "NUMA", "Size", "Data Size",
595 "Total", "Avail", "Cached", "Used");
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100596
Damjan Marion910d3692019-01-21 11:48:34 +0100597 /* *INDENT-OFF* */
598 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100599 cached += bpt->n_cached;
Damjan Marion910d3692019-01-21 11:48:34 +0100600 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100601
Damjan Marion55a1f282023-10-24 23:59:48 +0200602 s = format (s, "%-20v%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u", bp->name, bp->index,
603 bp->numa_node,
604 bp->data_size + sizeof (vlib_buffer_t) +
605 vm->buffer_main->ext_hdr_size,
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100606 bp->data_size, bp->n_buffers, bp->n_avail, cached,
607 bp->n_buffers - bp->n_avail - cached);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
609 return s;
610}
611
Benoît Gannee09a2332021-03-09 15:37:49 +0100612u8 *
613format_vlib_buffer_pool_all (u8 *s, va_list *va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614{
Benoît Gannee09a2332021-03-09 15:37:49 +0100615 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
Damjan Marion910d3692019-01-21 11:48:34 +0100616 vlib_buffer_main_t *bm = vm->buffer_main;
617 vlib_buffer_pool_t *bp;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100618
Benoît Gannee09a2332021-03-09 15:37:49 +0100619 s = format (s, "%U", format_vlib_buffer_pool, vm, 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100620
Damjan Marion910d3692019-01-21 11:48:34 +0100621 vec_foreach (bp, bm->buffer_pools)
Benoît Gannee09a2332021-03-09 15:37:49 +0100622 s = format (s, "\n%U", format_vlib_buffer_pool, vm, bp);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100623
Benoît Gannee09a2332021-03-09 15:37:49 +0100624 return s;
625}
626
627static clib_error_t *
628show_buffers (vlib_main_t *vm, unformat_input_t *input,
629 vlib_cli_command_t *cmd)
630{
631 vlib_cli_output (vm, "%U", format_vlib_buffer_pool_all, vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 return 0;
633}
634
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636VLIB_CLI_COMMAND (show_buffers_command, static) = {
637 .path = "show buffers",
638 .short_help = "Show packet buffer allocation",
639 .function = show_buffers,
640};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641/* *INDENT-ON* */
642
Damjan Marion49d66f12017-07-20 18:10:35 +0200643clib_error_t *
Damjan Marion321bd102022-06-01 00:45:18 +0200644vlib_buffer_num_workers_change (vlib_main_t *vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100645{
Damjan Marion910d3692019-01-21 11:48:34 +0100646 vlib_buffer_main_t *bm = vm->buffer_main;
647 vlib_buffer_pool_t *bp;
648
Damjan Marion910d3692019-01-21 11:48:34 +0100649 vec_foreach (bp, bm->buffer_pools)
Damjan Marion321bd102022-06-01 00:45:18 +0200650 vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
651 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100652
653 return 0;
654}
655
Damjan Marion321bd102022-06-01 00:45:18 +0200656VLIB_NUM_WORKERS_CHANGE_FN (vlib_buffer_num_workers_change);
Damjan Marion910d3692019-01-21 11:48:34 +0100657
658static clib_error_t *
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100659vlib_buffer_main_init_numa_alloc (struct vlib_main_t *vm, u32 numa_node,
660 u32 * physmem_map_index,
661 clib_mem_page_sz_t log2_page_size,
662 u8 unpriv)
Damjan Marion910d3692019-01-21 11:48:34 +0100663{
664 vlib_buffer_main_t *bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100665 u32 buffers_per_numa = bm->buffers_per_numa;
Damjan Marion49d66f12017-07-20 18:10:35 +0200666 clib_error_t *error;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100667 u32 buffer_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100668 uword n_pages, pagesize;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100669 u8 *name = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200670
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100671 ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN);
Damjan Marion49d66f12017-07-20 18:10:35 +0200672
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100673 pagesize = clib_mem_page_bytes (log2_page_size);
674 buffer_size = vlib_buffer_alloc_size (bm->ext_hdr_size,
675 vlib_buffer_get_default_data_size
676 (vm));
Benoît Gannec16fe462020-06-15 12:24:36 +0200677 if (buffer_size > pagesize)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100678 return clib_error_return (0, "buffer size (%llu) is greater than page "
679 "size (%llu)", buffer_size, pagesize);
Benoît Gannec16fe462020-06-15 12:24:36 +0200680
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100681 if (buffers_per_numa == 0)
682 buffers_per_numa = unpriv ? VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV :
683 VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA;
684
685 name = format (0, "buffers-numa-%d%c", numa_node, 0);
Damjan Marion910d3692019-01-21 11:48:34 +0100686 n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
687 error = vlib_physmem_shared_map_create (vm, (char *) name,
688 n_pages * pagesize,
689 min_log2 (pagesize), numa_node,
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100690 physmem_map_index);
691 vec_free (name);
692 return error;
693}
Damjan Marion567e61d2018-10-24 17:08:26 +0200694
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100695static clib_error_t *
696vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node,
697 u8 * index)
698{
699 vlib_buffer_main_t *bm = vm->buffer_main;
700 u32 physmem_map_index;
701 clib_error_t *error;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100702
703 if (bm->log2_page_size == CLIB_MEM_PAGE_SZ_UNKNOWN)
Damjan Marion567e61d2018-10-24 17:08:26 +0200704 {
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100705 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
706 &physmem_map_index,
707 CLIB_MEM_PAGE_SZ_DEFAULT_HUGE,
708 0 /* unpriv */ );
709 if (!error)
710 goto buffer_pool_create;
711
712 /* If alloc failed, retry without hugepages */
713 vlib_log_warn (bm->log_default,
714 "numa[%u] falling back to non-hugepage backed "
715 "buffer pool (%U)", numa_node, format_clib_error, error);
Damjan Marion567e61d2018-10-24 17:08:26 +0200716 clib_error_free (error);
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100717
718 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
719 &physmem_map_index,
720 CLIB_MEM_PAGE_SZ_DEFAULT,
721 1 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200722 }
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100723 else
724 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
725 &physmem_map_index,
726 bm->log2_page_size,
727 0 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200728 if (error)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100729 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200730
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100731buffer_pool_create:
Damjan Marion65dc34b2023-10-06 10:59:32 +0200732 *index =
733 vlib_buffer_pool_create (vm, vlib_buffer_get_default_data_size (vm),
734 physmem_map_index, "default-numa-%d", numa_node);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100735
736 if (*index == (u8) ~ 0)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100737 error = clib_error_return (0, "maximum number of buffer pools reached");
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100738
739
Guanghua Zhang785daf42019-08-18 08:35:04 +0800740 return error;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100741}
742
743void
744vlib_buffer_main_alloc (vlib_main_t * vm)
745{
746 vlib_buffer_main_t *bm;
747
748 if (vm->buffer_main)
749 return;
750
751 vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
752 clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
753 bm->default_data_size = VLIB_BUFFER_DEFAULT_DATA_SIZE;
Damjan Marion910d3692019-01-21 11:48:34 +0100754}
755
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800756static u32
757buffer_get_cached (vlib_buffer_pool_t * bp)
758{
759 u32 cached = 0;
760 vlib_buffer_pool_thread_t *bpt;
761
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800762 clib_spinlock_lock (&bp->lock);
763
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800764 /* *INDENT-OFF* */
765 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100766 cached += bpt->n_cached;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800767 /* *INDENT-ON* */
768
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800769 clib_spinlock_unlock (&bp->lock);
770
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800771 return cached;
772}
773
774static vlib_buffer_pool_t *
Ole Troan849cbe22019-02-26 13:10:56 +0100775buffer_get_by_index (vlib_buffer_main_t * bm, u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800776{
777 vlib_buffer_pool_t *bp;
Ole Troan849cbe22019-02-26 13:10:56 +0100778 if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index)
779 return 0;
780 bp = vec_elt_at_index (bm->buffer_pools, index);
Steven Luong95155102019-03-05 15:29:22 -0800781
Ole Troan849cbe22019-02-26 13:10:56 +0100782 return bp;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800783}
784
785static void
Damjan Marion8973b072022-03-01 15:51:18 +0100786buffer_gauges_collect_used_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800787{
788 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100789 vlib_buffer_pool_t *bp =
790 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800791 if (!bp)
792 return;
793
Damjan Marion8973b072022-03-01 15:51:18 +0100794 d->entry->value = bp->n_buffers - bp->n_avail - buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800795}
796
797static void
Damjan Marion8973b072022-03-01 15:51:18 +0100798buffer_gauges_collect_available_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800799{
800 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100801 vlib_buffer_pool_t *bp =
802 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800803 if (!bp)
804 return;
805
Damjan Marion8973b072022-03-01 15:51:18 +0100806 d->entry->value = bp->n_avail;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800807}
808
809static void
Damjan Marion8973b072022-03-01 15:51:18 +0100810buffer_gauges_collect_cached_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800811{
812 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100813 vlib_buffer_pool_t *bp =
814 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800815 if (!bp)
816 return;
817
Damjan Marion8973b072022-03-01 15:51:18 +0100818 d->entry->value = buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800819}
820
Damjan Marion910d3692019-01-21 11:48:34 +0100821clib_error_t *
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800822vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion910d3692019-01-21 11:48:34 +0100823{
824 vlib_buffer_main_t *bm;
825 clib_error_t *err;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100826 clib_bitmap_t *bmp = 0, *bmp_has_memory = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100827 u32 numa_node;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800828 vlib_buffer_pool_t *bp;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100829 u8 *name = 0, first_valid_buffer_pool_index = ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100830
Damjan Marion5de3fec2019-02-06 14:22:32 +0100831 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100832
833 bm = vm->buffer_main;
834 bm->log_default = vlib_log_register_class ("buffer", 0);
835 bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
836
837 clib_spinlock_init (&bm->buffer_known_hash_lockp);
838
Damjan Marionb592d1b2019-02-28 23:16:11 +0100839 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
840 unformat_bitmap_list, &bmp)))
841 clib_error_free (err);
842
843 if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U",
844 unformat_bitmap_list, &bmp_has_memory)))
845 clib_error_free (err);
846
847 if (bmp && bmp_has_memory)
848 bmp = clib_bitmap_and (bmp, bmp_has_memory);
849
850 /* no info from sysfs, assuming that only numa 0 exists */
851 if (bmp == 0)
852 bmp = clib_bitmap_set (bmp, 0, 1);
853
854 if (clib_bitmap_last_set (bmp) >= VLIB_BUFFER_MAX_NUMA_NODES)
855 clib_panic ("system have more than %u NUMA nodes",
856 VLIB_BUFFER_MAX_NUMA_NODES);
Damjan Marion910d3692019-01-21 11:48:34 +0100857
858 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100859 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100860 {
861 u8 *index = bm->default_buffer_pool_index_for_numa + numa_node;
862 index[0] = ~0;
863 if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index)))
864 {
865 clib_error_report (err);
866 clib_error_free (err);
867 continue;
868 }
869
870 if (first_valid_buffer_pool_index == 0xff)
871 first_valid_buffer_pool_index = index[0];
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100872 }
Damjan Marion910d3692019-01-21 11:48:34 +0100873 /* *INDENT-ON* */
874
Damjan Marionb592d1b2019-02-28 23:16:11 +0100875 if (first_valid_buffer_pool_index == (u8) ~ 0)
876 {
877 err = clib_error_return (0, "failed to allocate buffer pool(s)");
878 goto done;
879 }
880
881 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100882 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100883 {
884 if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0)
885 bm->default_buffer_pool_index_for_numa[numa_node] =
886 first_valid_buffer_pool_index;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100887 }
Damjan Marionb592d1b2019-02-28 23:16:11 +0100888 /* *INDENT-ON* */
Damjan Marion910d3692019-01-21 11:48:34 +0100889
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800890 vec_foreach (bp, bm->buffer_pools)
891 {
Damjan Marion8973b072022-03-01 15:51:18 +0100892 vlib_stats_collector_reg_t reg = { .private_data = bp - bm->buffer_pools };
Damjan Marionb592d1b2019-02-28 23:16:11 +0100893 if (bp->n_buffers == 0)
894 continue;
895
Damjan Marion8973b072022-03-01 15:51:18 +0100896 reg.entry_index =
Damjan Marion55a1f282023-10-24 23:59:48 +0200897 vlib_stats_add_gauge ("/buffer-pools/%v/cached", bp->name);
Damjan Marion8973b072022-03-01 15:51:18 +0100898 reg.collect_fn = buffer_gauges_collect_cached_fn;
899 vlib_stats_register_collector_fn (&reg);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100900
Damjan Marion55a1f282023-10-24 23:59:48 +0200901 reg.entry_index = vlib_stats_add_gauge ("/buffer-pools/%v/used", bp->name);
Damjan Marion8973b072022-03-01 15:51:18 +0100902 reg.collect_fn = buffer_gauges_collect_used_fn;
903 vlib_stats_register_collector_fn (&reg);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100904
Damjan Marion8973b072022-03-01 15:51:18 +0100905 reg.entry_index =
Damjan Marion55a1f282023-10-24 23:59:48 +0200906 vlib_stats_add_gauge ("/buffer-pools/%v/available", bp->name);
Damjan Marion8973b072022-03-01 15:51:18 +0100907 reg.collect_fn = buffer_gauges_collect_available_fn;
908 vlib_stats_register_collector_fn (&reg);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800909 }
910
Damjan Marion910d3692019-01-21 11:48:34 +0100911done:
912 vec_free (bmp);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100913 vec_free (bmp_has_memory);
914 vec_free (name);
Damjan Marion910d3692019-01-21 11:48:34 +0100915 return err;
Damjan Marion878c6092017-01-04 13:19:27 +0100916}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100917
Damjan Marion49d66f12017-07-20 18:10:35 +0200918static clib_error_t *
919vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
920{
Damjan Marion910d3692019-01-21 11:48:34 +0100921 vlib_buffer_main_t *bm;
922
Damjan Marion5de3fec2019-02-06 14:22:32 +0100923 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100924
925 bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100926 bm->log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN;
Damjan Marion49d66f12017-07-20 18:10:35 +0200927
928 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
929 {
Damjan Marion910d3692019-01-21 11:48:34 +0100930 if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
931 ;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100932 else if (unformat (input, "page-size %U", unformat_log2_page_size,
933 &bm->log2_page_size))
934 ;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100935 else if (unformat (input, "default data-size %u",
936 &bm->default_data_size))
937 ;
Damjan Marion49d66f12017-07-20 18:10:35 +0200938 else
939 return unformat_parse_error (input);
940 }
941
942 unformat_free (input);
943 return 0;
944}
945
946VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
947
Dave Barachc74b43c2020-04-09 17:24:07 -0400948#if VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0
949u32
950vlib_buffer_alloc_may_fail (vlib_main_t * vm, u32 n_buffers)
951{
952 f64 r;
953
954 r = random_f64 (&vm->buffer_alloc_success_seed);
955
956 /* Fail this request? */
957 if (r > vm->buffer_alloc_success_rate)
958 n_buffers--;
959 /* 5% chance of returning nothing at all */
960 if (r > vm->buffer_alloc_success_rate && r > 0.95)
961 n_buffers = 0;
962
963 return n_buffers;
964}
965#endif
Damjan Marion49d66f12017-07-20 18:10:35 +0200966
Benoît Gannee09a2332021-03-09 15:37:49 +0100967__clib_export int
968vlib_buffer_set_alloc_free_callback (
969 vlib_main_t *vm, vlib_buffer_alloc_free_callback_t *alloc_callback_fn,
970 vlib_buffer_alloc_free_callback_t *free_callback_fn)
971{
972 vlib_buffer_main_t *bm = vm->buffer_main;
973 if ((alloc_callback_fn && bm->alloc_callback_fn) ||
974 (free_callback_fn && bm->free_callback_fn))
975 return 1;
976 bm->alloc_callback_fn = alloc_callback_fn;
977 bm->free_callback_fn = free_callback_fn;
978 return 0;
979}
980
Chris Luked4024f52016-09-06 09:32:36 -0400981/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400982/*
983 * fd.io coding-style-patch-verification: ON
984 *
985 * Local Variables:
986 * eval: (c-set-style "gnu")
987 * End:
988 */