blob: e1dcf7ed82776dc6ef6316d93a6343befb269f41 [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>
Damjan Marion4b661402024-02-29 16:14:27 +010047#include <vppinfra/bitmap.h>
48#include <vppinfra/unix.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070049#include <vlib/vlib.h>
Damjan Marion374e2c52017-03-09 20:38:15 +010050#include <vlib/unix/unix.h>
Damjan Marion8973b072022-03-01 15:51:18 +010051#include <vlib/stats/stats.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Damjan Marion910d3692019-01-21 11:48:34 +010053#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384
54#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192
Damjan Marion901d16c2018-10-23 19:50:20 +020055
Damjan Marion910d3692019-01-21 11:48:34 +010056#ifdef CLIB_HAVE_VEC128
57/* Assumptions by vlib_buffer_free_inline: */
58STATIC_ASSERT_FITS_IN (vlib_buffer_t, flags, 16);
59STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16);
60STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16);
61#endif
Damjan Marion04a7f052017-07-10 15:06:17 +020062
Damjan Marion910d3692019-01-21 11:48:34 +010063u16 __vlib_buffer_external_hdr_size = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +020064
Dave Barach9b8ffd92016-07-08 08:13:45 -040065uword
66vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
67 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barach9b8ffd92016-07-08 08:13:45 -040069 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 uword l_first = b_first->current_length;
71 uword l = 0;
72 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
73 {
74 b = vlib_get_buffer (vm, b->next_buffer);
75 l += b->current_length;
76 }
77 b_first->total_length_not_including_first_buffer = l;
78 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
79 return l + l_first;
80}
81
Dave Barach9b8ffd92016-07-08 08:13:45 -040082u8 *
Benoît Ganne43543172019-10-21 15:13:54 +020083format_vlib_buffer_no_chain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070084{
Dave Barach9b8ffd92016-07-08 08:13:45 -040085 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020086 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010087 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010088
Damjan Mariondac03522018-02-01 15:30:13 +010089#define _(bit, name, v) \
90 if (v && (b->flags & VLIB_BUFFER_##name)) \
91 a = format (a, "%s ", v);
92 foreach_vlib_buffer_flag
93#undef _
Damjan Marion36eb7c22019-01-18 20:45:30 +010094 s = format (s, "current data %d, length %d, buffer-pool %d, "
Damjan Marion910d3692019-01-21 11:48:34 +010095 "ref-count %u", b->current_data, b->current_length,
96 b->buffer_pool_index, b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010097
Mohsin Kazmi30d46e72022-01-11 11:34:56 +000098 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010099 s = format (s, ", totlen-nifb %d",
100 b->total_length_not_including_first_buffer);
101
102 if (b->flags & VLIB_BUFFER_IS_TRACED)
Dave Baracha638c182019-06-21 18:24:07 -0400103 s = format (s, ", trace handle 0x%x", b->trace_handle);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100104
Damjan Mariondac03522018-02-01 15:30:13 +0100105 if (a)
106 s = format (s, "\n%U%v", format_white_space, indent, a);
107 vec_free (a);
108
Benoît Ganne43543172019-10-21 15:13:54 +0200109 return s;
110}
111
112u8 *
113format_vlib_buffer (u8 * s, va_list * args)
114{
115 vlib_main_t *vm = vlib_get_main ();
116 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
117 u32 indent = format_get_indent (s);
118
119 s = format (s, "%U", format_vlib_buffer_no_chain, b);
120
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100121 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
122 {
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100123 u32 next_buffer = b->next_buffer;
124 b = vlib_get_buffer (vm, next_buffer);
125
Damjan Marionc47ed032017-01-25 14:18:03 +0100126 s =
Damjan Marion910d3692019-01-21 11:48:34 +0100127 format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
Damjan Marionc47ed032017-01-25 14:18:03 +0100128 format_white_space, indent, next_buffer, b->current_length,
Damjan Marion910d3692019-01-21 11:48:34 +0100129 b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100130 }
131
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 return s;
133}
134
Dave Barach9b8ffd92016-07-08 08:13:45 -0400135u8 *
136format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400138 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
139
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 s = format (s, "%U, %U",
141 format_vlib_buffer, b,
142 format_hex_bytes, vlib_buffer_get_current (b), 64);
143
144 return s;
145}
146
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147static u8 *
148format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
150 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153 switch (state)
154 {
155 case VLIB_BUFFER_UNKNOWN:
156 t = "unknown";
157 break;
158
159 case VLIB_BUFFER_KNOWN_ALLOCATED:
160 t = "known-allocated";
161 break;
162
163 case VLIB_BUFFER_KNOWN_FREE:
164 t = "known-free";
165 break;
166
167 default:
168 t = "invalid";
169 break;
170 }
171
172 return format (s, "%s", t);
173}
174
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175u8 *
176format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400178 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
179 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 while (1)
182 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 vec_add (s, vlib_buffer_get_current (b), b->current_length);
184 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 break;
186 b = vlib_get_buffer (vm, b->next_buffer);
187 }
188
189 return s;
190}
191
192static u8 *
193vlib_validate_buffer_helper (vlib_main_t * vm,
194 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196{
Damjan Marion910d3692019-01-21 11:48:34 +0100197 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Damjan Marion910d3692019-01-21 11:48:34 +0100200 if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
201 return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
202
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100205
Damjan Marion8934a042019-02-09 23:29:26 +0100206 if (b->current_data + b->current_length >
207 vlib_buffer_get_default_data_size (vm))
Damjan Marion910d3692019-01-21 11:48:34 +0100208 return format (0, "%d-%d beyond end of buffer %d", b->current_data,
Damjan Marion8934a042019-02-09 23:29:26 +0100209 b->current_length, vlib_buffer_get_default_data_size (vm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Dave Barach9b8ffd92016-07-08 08:13:45 -0400211 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 {
213 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400214 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Damjan Mariond50e3472019-01-20 00:03:56 +0100216 k = vlib_buffer_is_known (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
218 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400219 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
221 if (unique_hash)
222 {
223 if (hash_get (*unique_hash, b->next_buffer))
224 return format (0, "duplicate buffer 0x%x", b->next_buffer);
225
226 hash_set1 (*unique_hash, b->next_buffer);
227 }
228
229 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
230 if (msg)
231 {
232 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
233 vec_free (msg);
234 return result;
235 }
236 }
237
238 return 0;
239}
240
241u8 *
242vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400243{
244 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
245 /* unique_hash */ 0);
246}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
248u8 *
249vlib_validate_buffers (vlib_main_t * vm,
250 u32 * buffers,
251 uword next_buffer_stride,
252 uword n_buffers,
253 vlib_buffer_known_state_t known_state,
254 uword follow_buffer_next)
255{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400256 uword i, *hash;
257 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400259 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
261 hash = hash_create (0, 0);
262 for (i = 0; i < n_buffers; i++)
263 {
264 bi = b[0];
265 b += next_buffer_stride;
266
267 /* Buffer is not unique. */
268 if (hash_get (hash, bi))
269 {
270 msg = format (0, "not unique");
271 goto done;
272 }
273
Damjan Mariond50e3472019-01-20 00:03:56 +0100274 k = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 if (k != known_state)
276 {
277 msg = format (0, "is %U; expected %U",
278 format_vlib_buffer_known_state, k,
279 format_vlib_buffer_known_state, known_state);
280 goto done;
281 }
282
283 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
284 if (msg)
285 goto done;
286
287 hash_set1 (hash, bi);
288 }
289
Dave Barach9b8ffd92016-07-08 08:13:45 -0400290done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 if (msg)
292 {
293 result = format (0, "0x%x: %v", bi, msg);
294 vec_free (msg);
295 }
296 hash_free (hash);
297 return result;
298}
299
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700300/* When debugging validate that given buffers are either known allocated
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100302void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303vlib_buffer_validate_alloc_free (vlib_main_t * vm,
304 u32 * buffers,
305 uword n_buffers,
306 vlib_buffer_known_state_t expected_state)
307{
Damjan Marion910d3692019-01-21 11:48:34 +0100308 vlib_buffer_main_t *bm = vm->buffer_main;
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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
316 b = buffers;
317 for (i = 0; i < n_buffers; i++)
318 {
319 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400320
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 bi = b[0];
322 b += 1;
Damjan Mariond50e3472019-01-20 00:03:56 +0100323 known = vlib_buffer_is_known (vm, bi);
Damjan Marion910d3692019-01-21 11:48:34 +0100324
325 if (known == VLIB_BUFFER_UNKNOWN &&
326 expected_state == VLIB_BUFFER_KNOWN_FREE)
327 known = VLIB_BUFFER_KNOWN_FREE;
328
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 if (known != expected_state)
330 {
Klement Sekerae82c73a2019-01-30 18:29:28 +0100331 clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
332 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 }
334
Damjan Marion910d3692019-01-21 11:48:34 +0100335 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
336 hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
337 VLIB_BUFFER_KNOWN_ALLOCATED);
338 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 }
340}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
Dave Barach9b8ffd92016-07-08 08:13:45 -0400342void
343vlib_packet_template_init (vlib_main_t * vm,
344 vlib_packet_template_t * t,
345 void *packet_data,
346 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100347 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100349 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100350
351 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100352 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100353 va_end (va);
354
355 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Dave Barachb7b92992018-10-17 10:38:51 -0400357 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
359 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100360 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Damjan Marion878c6092017-01-04 13:19:27 +0100361 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362}
363
364void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365vlib_packet_template_get_packet (vlib_main_t * vm,
366 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367{
368 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400369 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
371 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
372 return 0;
373
374 *bi_result = bi;
375
376 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500377 clib_memcpy_fast (vlib_buffer_get_current (b),
378 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400379 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
381 return b->data;
382}
383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500385int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100386vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
387 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388{
389 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 vlib_buffer_t *b;
391 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Dave Barach3a63fc52019-01-07 09:15:47 -0500393 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100394 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 goto out_of_buffers;
396
397 d = data;
398 n_left = n_data_bytes;
Damjan Marion8934a042019-02-09 23:29:26 +0100399 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400400
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 b = vlib_get_buffer (vm, bi);
402 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
403
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
406 b = vlib_get_buffer (vm, b->next_buffer);
407
408 while (1)
409 {
410 u32 n;
411
412 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400413 n_left_this_buffer =
414 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500416 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
417 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 b->current_length += n;
419 n_left -= n;
420 if (n_left == 0)
421 break;
422
423 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100424 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 goto out_of_buffers;
426
427 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
428
429 b = vlib_get_buffer (vm, b->next_buffer);
430 }
431
Dave Barach3a63fc52019-01-07 09:15:47 -0500432 *buffer_index = bi;
433 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500436 clib_warning ("out of buffers");
437 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438}
439
Pierre Pfister328e99b2016-02-12 13:18:42 +0000440u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100443 vlib_buffer_t ** last, void *data,
444 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400445{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000446 vlib_buffer_t *l = *last;
Damjan Marion8934a042019-02-09 23:29:26 +0100447 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000448 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
450 while (data_len)
451 {
452 u16 max = n_buffer_bytes - l->current_length - l->current_data;
453 if (max == 0)
454 {
Damjan Marion910d3692019-01-21 11:48:34 +0100455 if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
456 first->buffer_pool_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200458 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 max = n_buffer_bytes - l->current_length - l->current_data;
460 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000461
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500463 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
464 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465 vlib_buffer_chain_increase_length (first, l, len);
466 data_len -= len;
467 copied += len;
468 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000469 return copied;
470}
471
Benoît Gannec16fe462020-06-15 12:24:36 +0200472static uword
473vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size)
474{
Damjan Marion0be1b762020-09-04 12:34:58 +0200475 uword alloc_size = ext_hdr_size + sizeof (vlib_buffer_t) + data_size;
Damjan Marion038dad72024-01-19 21:19:57 +0100476 alloc_size = round_pow2 (alloc_size, VLIB_BUFFER_ALIGN);
Damjan Marion0be1b762020-09-04 12:34:58 +0200477
Vratko Polak04fd51c2024-02-06 12:45:59 +0100478 /* in case when we have even number of 'cachelines', we add one more for
479 * better cache occupancy */
480 alloc_size |= VLIB_BUFFER_ALIGN;
481
Damjan Marion0be1b762020-09-04 12:34:58 +0200482 return alloc_size;
Benoît Gannec16fe462020-06-15 12:24:36 +0200483}
484
Damjan Marionb592d1b2019-02-28 23:16:11 +0100485u8
Damjan Marion65dc34b2023-10-06 10:59:32 +0200486vlib_buffer_pool_create (vlib_main_t *vm, u32 data_size, u32 physmem_map_index,
487 char *fmt, ...)
Damjan Marion04a7f052017-07-10 15:06:17 +0200488{
Damjan Mariond50e3472019-01-20 00:03:56 +0100489 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100490 vlib_buffer_pool_t *bp;
Damjan Marion68b4da62018-09-30 18:26:20 +0200491 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
492 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200493 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion65dc34b2023-10-06 10:59:32 +0200494 uword page_mask = ~pow2_mask (m->log2_page_size);
495 u8 *p;
496 u32 alloc_size;
497 va_list va;
Damjan Marion910d3692019-01-21 11:48:34 +0100498
Damjan Marionb592d1b2019-02-28 23:16:11 +0100499 if (vec_len (bm->buffer_pools) >= 255)
500 return ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100501
Tianyu Li70b1cbd2021-06-23 07:35:03 +0000502 vec_add2_aligned (bm->buffer_pools, bp, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion04a7f052017-07-10 15:06:17 +0200503
504 if (bm->buffer_mem_size == 0)
505 {
506 bm->buffer_mem_start = start;
507 bm->buffer_mem_size = size;
508 }
509 else if (start < bm->buffer_mem_start)
510 {
511 bm->buffer_mem_size += bm->buffer_mem_start - start;
512 bm->buffer_mem_start = start;
513 if (size > bm->buffer_mem_size)
514 bm->buffer_mem_size = size;
515 }
516 else if (start > bm->buffer_mem_start)
517 {
518 uword new_size = start - bm->buffer_mem_start + size;
519 if (new_size > bm->buffer_mem_size)
520 bm->buffer_mem_size = new_size;
521 }
522
523 if ((u64) bm->buffer_mem_size >
524 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
525 {
526 clib_panic ("buffer memory size out of range!");
527 }
Damjan Marion149ba772017-10-12 13:09:26 +0200528
Damjan Marion910d3692019-01-21 11:48:34 +0100529 bp->start = start;
530 bp->size = size;
531 bp->index = bp - bm->buffer_pools;
532 bp->buffer_template.buffer_pool_index = bp->index;
533 bp->buffer_template.ref_count = 1;
534 bp->physmem_map_index = physmem_map_index;
Damjan Marion910d3692019-01-21 11:48:34 +0100535 bp->data_size = data_size;
536 bp->numa_node = m->numa_node;
Damjan Marion65dc34b2023-10-06 10:59:32 +0200537 bp->log2_page_size = m->log2_page_size;
538
539 va_start (va, fmt);
540 bp->name = va_format (0, fmt, &va);
541 va_end (va);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100542
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100543 vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
Damjan Marion910d3692019-01-21 11:48:34 +0100544 CLIB_CACHE_LINE_BYTES);
545
Benoît Gannec16fe462020-06-15 12:24:36 +0200546 alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size);
Damjan Marion65dc34b2023-10-06 10:59:32 +0200547 bp->alloc_size = alloc_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100548
549 /* preallocate buffer indices memory */
Damjan Marion65dc34b2023-10-06 10:59:32 +0200550 bp->buffers = clib_mem_alloc_aligned (
551 round_pow2 ((size / alloc_size) * sizeof (u32), CLIB_CACHE_LINE_BYTES),
552 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100553
554 clib_spinlock_init (&bp->lock);
555
Damjan Marion65dc34b2023-10-06 10:59:32 +0200556 p = m->base;
Damjan Marion910d3692019-01-21 11:48:34 +0100557
Damjan Marion65dc34b2023-10-06 10:59:32 +0200558 /* start with naturally aligned address */
559 p += alloc_size - (uword) p % alloc_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100560
Damjan Marion65dc34b2023-10-06 10:59:32 +0200561 /*
562 * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
563 * Allows various places to ASSERT (bi != 0). Much easier
564 * than debugging downstream crashes in successor nodes.
565 */
566 if (p == m->base)
567 p += alloc_size;
Dave Barachc74b43c2020-04-09 17:24:07 -0400568
Damjan Marion65dc34b2023-10-06 10:59:32 +0200569 for (; p < (u8 *) m->base + size - alloc_size; p += alloc_size)
570 {
571 vlib_buffer_t *b;
572 u32 bi;
Damjan Marion910d3692019-01-21 11:48:34 +0100573
Damjan Marion65dc34b2023-10-06 10:59:32 +0200574 /* skip if buffer spans across page boundary */
575 if (((uword) p & page_mask) != ((uword) (p + alloc_size) & page_mask))
576 continue;
Damjan Marion910d3692019-01-21 11:48:34 +0100577
Damjan Marion65dc34b2023-10-06 10:59:32 +0200578 b = (vlib_buffer_t *) (p + bm->ext_hdr_size);
Damjan Marionbf236632023-10-13 09:59:00 +0000579 b->template = bp->buffer_template;
Damjan Marion65dc34b2023-10-06 10:59:32 +0200580 bi = vlib_get_buffer_index (vm, b);
581 bp->buffers[bp->n_avail++] = bi;
582 vlib_get_buffer (vm, bi);
583 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100584
Damjan Marion65dc34b2023-10-06 10:59:32 +0200585 bp->n_buffers = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100586
Damjan Marionb592d1b2019-02-28 23:16:11 +0100587 return bp->index;
Damjan Marion04a7f052017-07-10 15:06:17 +0200588}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590static u8 *
Damjan Marion910d3692019-01-21 11:48:34 +0100591format_vlib_buffer_pool (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592{
Damjan Marion910d3692019-01-21 11:48:34 +0100593 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
594 vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
595 vlib_buffer_pool_thread_t *bpt;
596 u32 cached = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100597
Damjan Marion910d3692019-01-21 11:48:34 +0100598 if (!bp)
599 return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
600 "Pool Name", "Index", "NUMA", "Size", "Data Size",
601 "Total", "Avail", "Cached", "Used");
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100602
Damjan Marion910d3692019-01-21 11:48:34 +0100603 /* *INDENT-OFF* */
604 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100605 cached += bpt->n_cached;
Damjan Marion910d3692019-01-21 11:48:34 +0100606 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100607
Damjan Marion55a1f282023-10-24 23:59:48 +0200608 s = format (s, "%-20v%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u", bp->name, bp->index,
609 bp->numa_node,
610 bp->data_size + sizeof (vlib_buffer_t) +
611 vm->buffer_main->ext_hdr_size,
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100612 bp->data_size, bp->n_buffers, bp->n_avail, cached,
613 bp->n_buffers - bp->n_avail - cached);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
615 return s;
616}
617
Benoît Gannee09a2332021-03-09 15:37:49 +0100618u8 *
619format_vlib_buffer_pool_all (u8 *s, va_list *va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620{
Benoît Gannee09a2332021-03-09 15:37:49 +0100621 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
Damjan Marion910d3692019-01-21 11:48:34 +0100622 vlib_buffer_main_t *bm = vm->buffer_main;
623 vlib_buffer_pool_t *bp;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100624
Benoît Gannee09a2332021-03-09 15:37:49 +0100625 s = format (s, "%U", format_vlib_buffer_pool, vm, 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100626
Damjan Marion910d3692019-01-21 11:48:34 +0100627 vec_foreach (bp, bm->buffer_pools)
Benoît Gannee09a2332021-03-09 15:37:49 +0100628 s = format (s, "\n%U", format_vlib_buffer_pool, vm, bp);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100629
Benoît Gannee09a2332021-03-09 15:37:49 +0100630 return s;
631}
632
633static clib_error_t *
634show_buffers (vlib_main_t *vm, unformat_input_t *input,
635 vlib_cli_command_t *cmd)
636{
637 vlib_cli_output (vm, "%U", format_vlib_buffer_pool_all, vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 return 0;
639}
640
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642VLIB_CLI_COMMAND (show_buffers_command, static) = {
643 .path = "show buffers",
644 .short_help = "Show packet buffer allocation",
645 .function = show_buffers,
646};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400647/* *INDENT-ON* */
648
Damjan Marion49d66f12017-07-20 18:10:35 +0200649clib_error_t *
Damjan Marion321bd102022-06-01 00:45:18 +0200650vlib_buffer_num_workers_change (vlib_main_t *vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100651{
Damjan Marion910d3692019-01-21 11:48:34 +0100652 vlib_buffer_main_t *bm = vm->buffer_main;
653 vlib_buffer_pool_t *bp;
654
Damjan Marion910d3692019-01-21 11:48:34 +0100655 vec_foreach (bp, bm->buffer_pools)
Damjan Marion321bd102022-06-01 00:45:18 +0200656 vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
657 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100658
659 return 0;
660}
661
Damjan Marion321bd102022-06-01 00:45:18 +0200662VLIB_NUM_WORKERS_CHANGE_FN (vlib_buffer_num_workers_change);
Damjan Marion910d3692019-01-21 11:48:34 +0100663
664static clib_error_t *
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100665vlib_buffer_main_init_numa_alloc (struct vlib_main_t *vm, u32 numa_node,
666 u32 * physmem_map_index,
667 clib_mem_page_sz_t log2_page_size,
668 u8 unpriv)
Damjan Marion910d3692019-01-21 11:48:34 +0100669{
670 vlib_buffer_main_t *bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100671 u32 buffers_per_numa = bm->buffers_per_numa;
Damjan Marion49d66f12017-07-20 18:10:35 +0200672 clib_error_t *error;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100673 u32 buffer_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100674 uword n_pages, pagesize;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100675 u8 *name = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200676
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100677 ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN);
Damjan Marion49d66f12017-07-20 18:10:35 +0200678
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100679 pagesize = clib_mem_page_bytes (log2_page_size);
680 buffer_size = vlib_buffer_alloc_size (bm->ext_hdr_size,
681 vlib_buffer_get_default_data_size
682 (vm));
Benoît Gannec16fe462020-06-15 12:24:36 +0200683 if (buffer_size > pagesize)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100684 return clib_error_return (0, "buffer size (%llu) is greater than page "
685 "size (%llu)", buffer_size, pagesize);
Benoît Gannec16fe462020-06-15 12:24:36 +0200686
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100687 if (buffers_per_numa == 0)
688 buffers_per_numa = unpriv ? VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV :
689 VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA;
690
691 name = format (0, "buffers-numa-%d%c", numa_node, 0);
Damjan Marion910d3692019-01-21 11:48:34 +0100692 n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
693 error = vlib_physmem_shared_map_create (vm, (char *) name,
694 n_pages * pagesize,
695 min_log2 (pagesize), numa_node,
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100696 physmem_map_index);
697 vec_free (name);
698 return error;
699}
Damjan Marion567e61d2018-10-24 17:08:26 +0200700
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100701static clib_error_t *
702vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node,
703 u8 * index)
704{
705 vlib_buffer_main_t *bm = vm->buffer_main;
706 u32 physmem_map_index;
707 clib_error_t *error;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100708
709 if (bm->log2_page_size == CLIB_MEM_PAGE_SZ_UNKNOWN)
Damjan Marion567e61d2018-10-24 17:08:26 +0200710 {
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100711 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
712 &physmem_map_index,
713 CLIB_MEM_PAGE_SZ_DEFAULT_HUGE,
714 0 /* unpriv */ );
715 if (!error)
716 goto buffer_pool_create;
717
718 /* If alloc failed, retry without hugepages */
719 vlib_log_warn (bm->log_default,
720 "numa[%u] falling back to non-hugepage backed "
721 "buffer pool (%U)", numa_node, format_clib_error, error);
Damjan Marion567e61d2018-10-24 17:08:26 +0200722 clib_error_free (error);
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100723
724 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
725 &physmem_map_index,
726 CLIB_MEM_PAGE_SZ_DEFAULT,
727 1 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200728 }
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100729 else
730 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
731 &physmem_map_index,
732 bm->log2_page_size,
733 0 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200734 if (error)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100735 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200736
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100737buffer_pool_create:
Damjan Marion65dc34b2023-10-06 10:59:32 +0200738 *index =
739 vlib_buffer_pool_create (vm, vlib_buffer_get_default_data_size (vm),
740 physmem_map_index, "default-numa-%d", numa_node);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100741
742 if (*index == (u8) ~ 0)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100743 error = clib_error_return (0, "maximum number of buffer pools reached");
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100744
745
Guanghua Zhang785daf42019-08-18 08:35:04 +0800746 return error;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100747}
748
749void
750vlib_buffer_main_alloc (vlib_main_t * vm)
751{
752 vlib_buffer_main_t *bm;
753
754 if (vm->buffer_main)
755 return;
756
757 vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
758 clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
759 bm->default_data_size = VLIB_BUFFER_DEFAULT_DATA_SIZE;
Damjan Marion910d3692019-01-21 11:48:34 +0100760}
761
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800762static u32
763buffer_get_cached (vlib_buffer_pool_t * bp)
764{
765 u32 cached = 0;
766 vlib_buffer_pool_thread_t *bpt;
767
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800768 clib_spinlock_lock (&bp->lock);
769
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800770 /* *INDENT-OFF* */
771 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100772 cached += bpt->n_cached;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800773 /* *INDENT-ON* */
774
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800775 clib_spinlock_unlock (&bp->lock);
776
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800777 return cached;
778}
779
780static vlib_buffer_pool_t *
Ole Troan849cbe22019-02-26 13:10:56 +0100781buffer_get_by_index (vlib_buffer_main_t * bm, u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800782{
783 vlib_buffer_pool_t *bp;
Ole Troan849cbe22019-02-26 13:10:56 +0100784 if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index)
785 return 0;
786 bp = vec_elt_at_index (bm->buffer_pools, index);
Steven Luong95155102019-03-05 15:29:22 -0800787
Ole Troan849cbe22019-02-26 13:10:56 +0100788 return bp;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800789}
790
791static void
Damjan Marion8973b072022-03-01 15:51:18 +0100792buffer_gauges_collect_used_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800793{
794 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100795 vlib_buffer_pool_t *bp =
796 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800797 if (!bp)
798 return;
799
Damjan Marion8973b072022-03-01 15:51:18 +0100800 d->entry->value = bp->n_buffers - bp->n_avail - buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800801}
802
803static void
Damjan Marion8973b072022-03-01 15:51:18 +0100804buffer_gauges_collect_available_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800805{
806 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100807 vlib_buffer_pool_t *bp =
808 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800809 if (!bp)
810 return;
811
Damjan Marion8973b072022-03-01 15:51:18 +0100812 d->entry->value = bp->n_avail;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800813}
814
815static void
Damjan Marion8973b072022-03-01 15:51:18 +0100816buffer_gauges_collect_cached_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800817{
818 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100819 vlib_buffer_pool_t *bp =
820 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800821 if (!bp)
822 return;
823
Damjan Marion8973b072022-03-01 15:51:18 +0100824 d->entry->value = buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800825}
826
Damjan Marion910d3692019-01-21 11:48:34 +0100827clib_error_t *
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800828vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion910d3692019-01-21 11:48:34 +0100829{
830 vlib_buffer_main_t *bm;
831 clib_error_t *err;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100832 clib_bitmap_t *bmp = 0, *bmp_has_memory = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100833 u32 numa_node;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800834 vlib_buffer_pool_t *bp;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100835 u8 *name = 0, first_valid_buffer_pool_index = ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100836
Damjan Marion5de3fec2019-02-06 14:22:32 +0100837 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100838
839 bm = vm->buffer_main;
840 bm->log_default = vlib_log_register_class ("buffer", 0);
841 bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
842
843 clib_spinlock_init (&bm->buffer_known_hash_lockp);
844
Damjan Marion4b661402024-02-29 16:14:27 +0100845 bmp = os_get_online_cpu_node_bitmap ();
Damjan Marionb592d1b2019-02-28 23:16:11 +0100846
847 if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U",
848 unformat_bitmap_list, &bmp_has_memory)))
849 clib_error_free (err);
850
851 if (bmp && bmp_has_memory)
852 bmp = clib_bitmap_and (bmp, bmp_has_memory);
853
854 /* no info from sysfs, assuming that only numa 0 exists */
855 if (bmp == 0)
856 bmp = clib_bitmap_set (bmp, 0, 1);
857
858 if (clib_bitmap_last_set (bmp) >= VLIB_BUFFER_MAX_NUMA_NODES)
859 clib_panic ("system have more than %u NUMA nodes",
860 VLIB_BUFFER_MAX_NUMA_NODES);
Damjan Marion910d3692019-01-21 11:48:34 +0100861
862 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100863 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100864 {
865 u8 *index = bm->default_buffer_pool_index_for_numa + numa_node;
866 index[0] = ~0;
867 if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index)))
868 {
869 clib_error_report (err);
870 clib_error_free (err);
871 continue;
872 }
873
874 if (first_valid_buffer_pool_index == 0xff)
875 first_valid_buffer_pool_index = index[0];
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100876 }
Damjan Marion910d3692019-01-21 11:48:34 +0100877 /* *INDENT-ON* */
878
Damjan Marionb592d1b2019-02-28 23:16:11 +0100879 if (first_valid_buffer_pool_index == (u8) ~ 0)
880 {
881 err = clib_error_return (0, "failed to allocate buffer pool(s)");
882 goto done;
883 }
884
885 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100886 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100887 {
888 if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0)
889 bm->default_buffer_pool_index_for_numa[numa_node] =
890 first_valid_buffer_pool_index;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100891 }
Damjan Marionb592d1b2019-02-28 23:16:11 +0100892 /* *INDENT-ON* */
Damjan Marion910d3692019-01-21 11:48:34 +0100893
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800894 vec_foreach (bp, bm->buffer_pools)
895 {
Damjan Marion8973b072022-03-01 15:51:18 +0100896 vlib_stats_collector_reg_t reg = { .private_data = bp - bm->buffer_pools };
Damjan Marionb592d1b2019-02-28 23:16:11 +0100897 if (bp->n_buffers == 0)
898 continue;
899
Damjan Marion8973b072022-03-01 15:51:18 +0100900 reg.entry_index =
Damjan Marion55a1f282023-10-24 23:59:48 +0200901 vlib_stats_add_gauge ("/buffer-pools/%v/cached", bp->name);
Damjan Marion8973b072022-03-01 15:51:18 +0100902 reg.collect_fn = buffer_gauges_collect_cached_fn;
903 vlib_stats_register_collector_fn (&reg);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100904
Damjan Marion55a1f282023-10-24 23:59:48 +0200905 reg.entry_index = vlib_stats_add_gauge ("/buffer-pools/%v/used", bp->name);
Damjan Marion8973b072022-03-01 15:51:18 +0100906 reg.collect_fn = buffer_gauges_collect_used_fn;
907 vlib_stats_register_collector_fn (&reg);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100908
Damjan Marion8973b072022-03-01 15:51:18 +0100909 reg.entry_index =
Damjan Marion55a1f282023-10-24 23:59:48 +0200910 vlib_stats_add_gauge ("/buffer-pools/%v/available", bp->name);
Damjan Marion8973b072022-03-01 15:51:18 +0100911 reg.collect_fn = buffer_gauges_collect_available_fn;
912 vlib_stats_register_collector_fn (&reg);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800913 }
914
Damjan Marion910d3692019-01-21 11:48:34 +0100915done:
916 vec_free (bmp);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100917 vec_free (bmp_has_memory);
918 vec_free (name);
Damjan Marion910d3692019-01-21 11:48:34 +0100919 return err;
Damjan Marion878c6092017-01-04 13:19:27 +0100920}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100921
Damjan Marion49d66f12017-07-20 18:10:35 +0200922static clib_error_t *
923vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
924{
Damjan Marion910d3692019-01-21 11:48:34 +0100925 vlib_buffer_main_t *bm;
926
Damjan Marion5de3fec2019-02-06 14:22:32 +0100927 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100928
929 bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100930 bm->log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN;
Damjan Marion49d66f12017-07-20 18:10:35 +0200931
932 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
933 {
Damjan Marion910d3692019-01-21 11:48:34 +0100934 if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
935 ;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100936 else if (unformat (input, "page-size %U", unformat_log2_page_size,
937 &bm->log2_page_size))
938 ;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100939 else if (unformat (input, "default data-size %u",
940 &bm->default_data_size))
941 ;
Damjan Marion49d66f12017-07-20 18:10:35 +0200942 else
943 return unformat_parse_error (input);
944 }
945
946 unformat_free (input);
947 return 0;
948}
949
950VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
951
Dave Barachc74b43c2020-04-09 17:24:07 -0400952#if VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0
953u32
954vlib_buffer_alloc_may_fail (vlib_main_t * vm, u32 n_buffers)
955{
956 f64 r;
957
958 r = random_f64 (&vm->buffer_alloc_success_seed);
959
960 /* Fail this request? */
961 if (r > vm->buffer_alloc_success_rate)
962 n_buffers--;
963 /* 5% chance of returning nothing at all */
964 if (r > vm->buffer_alloc_success_rate && r > 0.95)
965 n_buffers = 0;
966
967 return n_buffers;
968}
969#endif
Damjan Marion49d66f12017-07-20 18:10:35 +0200970
Benoît Gannee09a2332021-03-09 15:37:49 +0100971__clib_export int
972vlib_buffer_set_alloc_free_callback (
973 vlib_main_t *vm, vlib_buffer_alloc_free_callback_t *alloc_callback_fn,
974 vlib_buffer_alloc_free_callback_t *free_callback_fn)
975{
976 vlib_buffer_main_t *bm = vm->buffer_main;
977 if ((alloc_callback_fn && bm->alloc_callback_fn) ||
978 (free_callback_fn && bm->free_callback_fn))
979 return 1;
980 bm->alloc_callback_fn = alloc_callback_fn;
981 bm->free_callback_fn = free_callback_fn;
982 return 0;
983}
984
Chris Luked4024f52016-09-06 09:32:36 -0400985/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400986/*
987 * fd.io coding-style-patch-verification: ON
988 *
989 * Local Variables:
990 * eval: (c-set-style "gnu")
991 * End:
992 */