blob: 304d1abd8eab8e47ed91c2758a6822372543d001 [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 +010061/* Make sure that buffer template size is not accidentally changed */
62STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
63
64u16 __vlib_buffer_external_hdr_size = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +020065
Dave Barach9b8ffd92016-07-08 08:13:45 -040066uword
67vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
68 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Dave Barach9b8ffd92016-07-08 08:13:45 -040070 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 uword l_first = b_first->current_length;
72 uword l = 0;
73 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
74 {
75 b = vlib_get_buffer (vm, b->next_buffer);
76 l += b->current_length;
77 }
78 b_first->total_length_not_including_first_buffer = l;
79 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
80 return l + l_first;
81}
82
Dave Barach9b8ffd92016-07-08 08:13:45 -040083u8 *
Benoît Ganne43543172019-10-21 15:13:54 +020084format_vlib_buffer_no_chain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070085{
Dave Barach9b8ffd92016-07-08 08:13:45 -040086 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020087 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010088 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010089
Damjan Mariondac03522018-02-01 15:30:13 +010090#define _(bit, name, v) \
91 if (v && (b->flags & VLIB_BUFFER_##name)) \
92 a = format (a, "%s ", v);
93 foreach_vlib_buffer_flag
94#undef _
Damjan Marion36eb7c22019-01-18 20:45:30 +010095 s = format (s, "current data %d, length %d, buffer-pool %d, "
Damjan Marion910d3692019-01-21 11:48:34 +010096 "ref-count %u", b->current_data, b->current_length,
97 b->buffer_pool_index, b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +010098
Mohsin Kazmi30d46e72022-01-11 11:34:56 +000099 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100100 s = format (s, ", totlen-nifb %d",
101 b->total_length_not_including_first_buffer);
102
103 if (b->flags & VLIB_BUFFER_IS_TRACED)
Dave Baracha638c182019-06-21 18:24:07 -0400104 s = format (s, ", trace handle 0x%x", b->trace_handle);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100105
Damjan Mariondac03522018-02-01 15:30:13 +0100106 if (a)
107 s = format (s, "\n%U%v", format_white_space, indent, a);
108 vec_free (a);
109
Benoît Ganne43543172019-10-21 15:13:54 +0200110 return s;
111}
112
113u8 *
114format_vlib_buffer (u8 * s, va_list * args)
115{
116 vlib_main_t *vm = vlib_get_main ();
117 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
118 u32 indent = format_get_indent (s);
119
120 s = format (s, "%U", format_vlib_buffer_no_chain, b);
121
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100122 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
123 {
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100124 u32 next_buffer = b->next_buffer;
125 b = vlib_get_buffer (vm, next_buffer);
126
Damjan Marionc47ed032017-01-25 14:18:03 +0100127 s =
Damjan Marion910d3692019-01-21 11:48:34 +0100128 format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
Damjan Marionc47ed032017-01-25 14:18:03 +0100129 format_white_space, indent, next_buffer, b->current_length,
Damjan Marion910d3692019-01-21 11:48:34 +0100130 b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100131 }
132
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 return s;
134}
135
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136u8 *
137format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
140
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 s = format (s, "%U, %U",
142 format_vlib_buffer, b,
143 format_hex_bytes, vlib_buffer_get_current (b), 64);
144
145 return s;
146}
147
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148static u8 *
149format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
151 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
154 switch (state)
155 {
156 case VLIB_BUFFER_UNKNOWN:
157 t = "unknown";
158 break;
159
160 case VLIB_BUFFER_KNOWN_ALLOCATED:
161 t = "known-allocated";
162 break;
163
164 case VLIB_BUFFER_KNOWN_FREE:
165 t = "known-free";
166 break;
167
168 default:
169 t = "invalid";
170 break;
171 }
172
173 return format (s, "%s", t);
174}
175
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176u8 *
177format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400179 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
180 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
181
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 while (1)
183 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400184 vec_add (s, vlib_buffer_get_current (b), b->current_length);
185 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 break;
187 b = vlib_get_buffer (vm, b->next_buffer);
188 }
189
190 return s;
191}
192
193static u8 *
194vlib_validate_buffer_helper (vlib_main_t * vm,
195 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
Damjan Marion910d3692019-01-21 11:48:34 +0100198 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400199 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
Damjan Marion910d3692019-01-21 11:48:34 +0100201 if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
202 return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
203
Dave Barach9b8ffd92016-07-08 08:13:45 -0400204 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100206
Damjan Marion8934a042019-02-09 23:29:26 +0100207 if (b->current_data + b->current_length >
208 vlib_buffer_get_default_data_size (vm))
Damjan Marion910d3692019-01-21 11:48:34 +0100209 return format (0, "%d-%d beyond end of buffer %d", b->current_data,
Damjan Marion8934a042019-02-09 23:29:26 +0100210 b->current_length, vlib_buffer_get_default_data_size (vm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Dave Barach9b8ffd92016-07-08 08:13:45 -0400212 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 {
214 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Damjan Mariond50e3472019-01-20 00:03:56 +0100217 k = vlib_buffer_is_known (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
219 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400220 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
222 if (unique_hash)
223 {
224 if (hash_get (*unique_hash, b->next_buffer))
225 return format (0, "duplicate buffer 0x%x", b->next_buffer);
226
227 hash_set1 (*unique_hash, b->next_buffer);
228 }
229
230 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
231 if (msg)
232 {
233 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
234 vec_free (msg);
235 return result;
236 }
237 }
238
239 return 0;
240}
241
242u8 *
243vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400244{
245 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
246 /* unique_hash */ 0);
247}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
249u8 *
250vlib_validate_buffers (vlib_main_t * vm,
251 u32 * buffers,
252 uword next_buffer_stride,
253 uword n_buffers,
254 vlib_buffer_known_state_t known_state,
255 uword follow_buffer_next)
256{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257 uword i, *hash;
258 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400260 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
262 hash = hash_create (0, 0);
263 for (i = 0; i < n_buffers; i++)
264 {
265 bi = b[0];
266 b += next_buffer_stride;
267
268 /* Buffer is not unique. */
269 if (hash_get (hash, bi))
270 {
271 msg = format (0, "not unique");
272 goto done;
273 }
274
Damjan Mariond50e3472019-01-20 00:03:56 +0100275 k = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 if (k != known_state)
277 {
278 msg = format (0, "is %U; expected %U",
279 format_vlib_buffer_known_state, k,
280 format_vlib_buffer_known_state, known_state);
281 goto done;
282 }
283
284 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
285 if (msg)
286 goto done;
287
288 hash_set1 (hash, bi);
289 }
290
Dave Barach9b8ffd92016-07-08 08:13:45 -0400291done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 if (msg)
293 {
294 result = format (0, "0x%x: %v", bi, msg);
295 vec_free (msg);
296 }
297 hash_free (hash);
298 return result;
299}
300
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700301/* When debugging validate that given buffers are either known allocated
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 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{
Damjan Marion910d3692019-01-21 11:48:34 +0100309 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400310 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 uword i, bi, is_free;
312
313 if (CLIB_DEBUG == 0)
314 return;
315
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
317 b = buffers;
318 for (i = 0; i < n_buffers; i++)
319 {
320 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400321
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 bi = b[0];
323 b += 1;
Damjan Mariond50e3472019-01-20 00:03:56 +0100324 known = vlib_buffer_is_known (vm, bi);
Damjan Marion910d3692019-01-21 11:48:34 +0100325
326 if (known == VLIB_BUFFER_UNKNOWN &&
327 expected_state == VLIB_BUFFER_KNOWN_FREE)
328 known = VLIB_BUFFER_KNOWN_FREE;
329
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 if (known != expected_state)
331 {
Klement Sekerae82c73a2019-01-30 18:29:28 +0100332 clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
333 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 }
335
Damjan Marion910d3692019-01-21 11:48:34 +0100336 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
337 hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
338 VLIB_BUFFER_KNOWN_ALLOCATED);
339 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 }
341}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343void
344vlib_packet_template_init (vlib_main_t * vm,
345 vlib_packet_template_t * t,
346 void *packet_data,
347 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100348 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100350 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100351
352 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100353 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100354 va_end (va);
355
356 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
Dave Barachb7b92992018-10-17 10:38:51 -0400358 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
360 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100361 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Damjan Marion878c6092017-01-04 13:19:27 +0100362 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363}
364
365void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400366vlib_packet_template_get_packet (vlib_main_t * vm,
367 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368{
369 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400370 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371
372 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
373 return 0;
374
375 *bi_result = bi;
376
377 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500378 clib_memcpy_fast (vlib_buffer_get_current (b),
379 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400380 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
382 return b->data;
383}
384
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500386int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100387vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
388 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389{
390 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400391 vlib_buffer_t *b;
392 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
Dave Barach3a63fc52019-01-07 09:15:47 -0500394 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100395 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 goto out_of_buffers;
397
398 d = data;
399 n_left = n_data_bytes;
Damjan Marion8934a042019-02-09 23:29:26 +0100400 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400401
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 b = vlib_get_buffer (vm, bi);
403 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
404
Dave Barach9b8ffd92016-07-08 08:13:45 -0400405 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
407 b = vlib_get_buffer (vm, b->next_buffer);
408
409 while (1)
410 {
411 u32 n;
412
413 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400414 n_left_this_buffer =
415 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500417 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
418 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 b->current_length += n;
420 n_left -= n;
421 if (n_left == 0)
422 break;
423
424 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100425 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 goto out_of_buffers;
427
428 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
429
430 b = vlib_get_buffer (vm, b->next_buffer);
431 }
432
Dave Barach3a63fc52019-01-07 09:15:47 -0500433 *buffer_index = bi;
434 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Dave Barach9b8ffd92016-07-08 08:13:45 -0400436out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500437 clib_warning ("out of buffers");
438 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439}
440
Pierre Pfister328e99b2016-02-12 13:18:42 +0000441u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100444 vlib_buffer_t ** last, void *data,
445 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000447 vlib_buffer_t *l = *last;
Damjan Marion8934a042019-02-09 23:29:26 +0100448 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000449 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
451 while (data_len)
452 {
453 u16 max = n_buffer_bytes - l->current_length - l->current_data;
454 if (max == 0)
455 {
Damjan Marion910d3692019-01-21 11:48:34 +0100456 if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
457 first->buffer_pool_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400458 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200459 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460 max = n_buffer_bytes - l->current_length - l->current_data;
461 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000462
Dave Barach9b8ffd92016-07-08 08:13:45 -0400463 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500464 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
465 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400466 vlib_buffer_chain_increase_length (first, l, len);
467 data_len -= len;
468 copied += len;
469 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000470 return copied;
471}
472
Benoît Gannec16fe462020-06-15 12:24:36 +0200473static uword
474vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size)
475{
Damjan Marion0be1b762020-09-04 12:34:58 +0200476 uword alloc_size = ext_hdr_size + sizeof (vlib_buffer_t) + data_size;
477 alloc_size = CLIB_CACHE_LINE_ROUND (alloc_size);
478
479 /* in case when we have even number of cachelines, we add one more for
480 * better cache occupancy */
481 alloc_size |= CLIB_CACHE_LINE_BYTES;
482
483 return alloc_size;
Benoît Gannec16fe462020-06-15 12:24:36 +0200484}
485
Damjan Marionb592d1b2019-02-28 23:16:11 +0100486u8
487vlib_buffer_pool_create (vlib_main_t * vm, char *name, u32 data_size,
488 u32 physmem_map_index)
Damjan Marion04a7f052017-07-10 15:06:17 +0200489{
Damjan Mariond50e3472019-01-20 00:03:56 +0100490 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100491 vlib_buffer_pool_t *bp;
Damjan Marion68b4da62018-09-30 18:26:20 +0200492 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
493 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200494 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion672ab692019-02-06 17:56:57 +0100495 uword i, j;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100496 u32 alloc_size, n_alloc_per_page;
Damjan Marion910d3692019-01-21 11:48:34 +0100497
Damjan Marionb592d1b2019-02-28 23:16:11 +0100498 if (vec_len (bm->buffer_pools) >= 255)
499 return ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100500
Tianyu Li70b1cbd2021-06-23 07:35:03 +0000501 vec_add2_aligned (bm->buffer_pools, bp, 1, CLIB_CACHE_LINE_BYTES);
Damjan Marion04a7f052017-07-10 15:06:17 +0200502
503 if (bm->buffer_mem_size == 0)
504 {
505 bm->buffer_mem_start = start;
506 bm->buffer_mem_size = size;
507 }
508 else if (start < bm->buffer_mem_start)
509 {
510 bm->buffer_mem_size += bm->buffer_mem_start - start;
511 bm->buffer_mem_start = start;
512 if (size > bm->buffer_mem_size)
513 bm->buffer_mem_size = size;
514 }
515 else if (start > bm->buffer_mem_start)
516 {
517 uword new_size = start - bm->buffer_mem_start + size;
518 if (new_size > bm->buffer_mem_size)
519 bm->buffer_mem_size = new_size;
520 }
521
522 if ((u64) bm->buffer_mem_size >
523 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
524 {
525 clib_panic ("buffer memory size out of range!");
526 }
Damjan Marion149ba772017-10-12 13:09:26 +0200527
Damjan Marion910d3692019-01-21 11:48:34 +0100528 bp->start = start;
529 bp->size = size;
530 bp->index = bp - bm->buffer_pools;
531 bp->buffer_template.buffer_pool_index = bp->index;
532 bp->buffer_template.ref_count = 1;
533 bp->physmem_map_index = physmem_map_index;
534 bp->name = format (0, "%s%c", name, 0);
535 bp->data_size = data_size;
536 bp->numa_node = m->numa_node;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100537
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100538 vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
Damjan Marion910d3692019-01-21 11:48:34 +0100539 CLIB_CACHE_LINE_BYTES);
540
Benoît Gannec16fe462020-06-15 12:24:36 +0200541 alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size);
Damjan Marion910d3692019-01-21 11:48:34 +0100542 n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
543
544 /* preallocate buffer indices memory */
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100545 bp->n_buffers = m->n_pages * n_alloc_per_page;
546 bp->buffers = clib_mem_alloc_aligned (bp->n_buffers * sizeof (u32),
547 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100548
549 clib_spinlock_init (&bp->lock);
550
Damjan Marion672ab692019-02-06 17:56:57 +0100551 for (j = 0; j < m->n_pages; j++)
552 for (i = 0; i < n_alloc_per_page; i++)
553 {
554 u8 *p;
555 u32 bi;
Damjan Marion910d3692019-01-21 11:48:34 +0100556
Damjan Marion672ab692019-02-06 17:56:57 +0100557 p = m->base + (j << m->log2_page_size) + i * alloc_size;
558 p += bm->ext_hdr_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100559
Dave Barachc74b43c2020-04-09 17:24:07 -0400560 /*
561 * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
562 * Allows various places to ASSERT (bi != 0). Much easier
563 * than debugging downstream crashes in successor nodes.
564 */
565 if (p == m->base)
566 continue;
567
Damjan Marion672ab692019-02-06 17:56:57 +0100568 vlib_buffer_copy_template ((vlib_buffer_t *) p, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100569
Damjan Marion672ab692019-02-06 17:56:57 +0100570 bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
Damjan Marion910d3692019-01-21 11:48:34 +0100571
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100572 bp->buffers[bp->n_avail++] = bi;
573
Damjan Marion672ab692019-02-06 17:56:57 +0100574 vlib_get_buffer (vm, bi);
575 }
Damjan Marion910d3692019-01-21 11:48:34 +0100576
Damjan Marionb592d1b2019-02-28 23:16:11 +0100577 return bp->index;
Damjan Marion04a7f052017-07-10 15:06:17 +0200578}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580static u8 *
Damjan Marion910d3692019-01-21 11:48:34 +0100581format_vlib_buffer_pool (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582{
Damjan Marion910d3692019-01-21 11:48:34 +0100583 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
584 vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
585 vlib_buffer_pool_thread_t *bpt;
586 u32 cached = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100587
Damjan Marion910d3692019-01-21 11:48:34 +0100588 if (!bp)
589 return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
590 "Pool Name", "Index", "NUMA", "Size", "Data Size",
591 "Total", "Avail", "Cached", "Used");
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100592
Damjan Marion910d3692019-01-21 11:48:34 +0100593 /* *INDENT-OFF* */
594 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100595 cached += bpt->n_cached;
Damjan Marion910d3692019-01-21 11:48:34 +0100596 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100597
Damjan Marion910d3692019-01-21 11:48:34 +0100598 s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u",
599 bp->name, bp->index, bp->numa_node, bp->data_size +
600 sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size,
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100601 bp->data_size, bp->n_buffers, bp->n_avail, cached,
602 bp->n_buffers - bp->n_avail - cached);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 return s;
605}
606
Benoît Gannee09a2332021-03-09 15:37:49 +0100607u8 *
608format_vlib_buffer_pool_all (u8 *s, va_list *va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609{
Benoît Gannee09a2332021-03-09 15:37:49 +0100610 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
Damjan Marion910d3692019-01-21 11:48:34 +0100611 vlib_buffer_main_t *bm = vm->buffer_main;
612 vlib_buffer_pool_t *bp;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100613
Benoît Gannee09a2332021-03-09 15:37:49 +0100614 s = format (s, "%U", format_vlib_buffer_pool, vm, 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100615
Damjan Marion910d3692019-01-21 11:48:34 +0100616 vec_foreach (bp, bm->buffer_pools)
Benoît Gannee09a2332021-03-09 15:37:49 +0100617 s = format (s, "\n%U", format_vlib_buffer_pool, vm, bp);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100618
Benoît Gannee09a2332021-03-09 15:37:49 +0100619 return s;
620}
621
622static clib_error_t *
623show_buffers (vlib_main_t *vm, unformat_input_t *input,
624 vlib_cli_command_t *cmd)
625{
626 vlib_cli_output (vm, "%U", format_vlib_buffer_pool_all, vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 return 0;
628}
629
Dave Barach9b8ffd92016-07-08 08:13:45 -0400630/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631VLIB_CLI_COMMAND (show_buffers_command, static) = {
632 .path = "show buffers",
633 .short_help = "Show packet buffer allocation",
634 .function = show_buffers,
635};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636/* *INDENT-ON* */
637
Damjan Marion49d66f12017-07-20 18:10:35 +0200638clib_error_t *
Damjan Marion321bd102022-06-01 00:45:18 +0200639vlib_buffer_num_workers_change (vlib_main_t *vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100640{
Damjan Marion910d3692019-01-21 11:48:34 +0100641 vlib_buffer_main_t *bm = vm->buffer_main;
642 vlib_buffer_pool_t *bp;
643
Damjan Marion910d3692019-01-21 11:48:34 +0100644 vec_foreach (bp, bm->buffer_pools)
Damjan Marion321bd102022-06-01 00:45:18 +0200645 vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1,
646 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100647
648 return 0;
649}
650
Damjan Marion321bd102022-06-01 00:45:18 +0200651VLIB_NUM_WORKERS_CHANGE_FN (vlib_buffer_num_workers_change);
Damjan Marion910d3692019-01-21 11:48:34 +0100652
653static clib_error_t *
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100654vlib_buffer_main_init_numa_alloc (struct vlib_main_t *vm, u32 numa_node,
655 u32 * physmem_map_index,
656 clib_mem_page_sz_t log2_page_size,
657 u8 unpriv)
Damjan Marion910d3692019-01-21 11:48:34 +0100658{
659 vlib_buffer_main_t *bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100660 u32 buffers_per_numa = bm->buffers_per_numa;
Damjan Marion49d66f12017-07-20 18:10:35 +0200661 clib_error_t *error;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100662 u32 buffer_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100663 uword n_pages, pagesize;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100664 u8 *name = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200665
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100666 ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN);
Damjan Marion49d66f12017-07-20 18:10:35 +0200667
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100668 pagesize = clib_mem_page_bytes (log2_page_size);
669 buffer_size = vlib_buffer_alloc_size (bm->ext_hdr_size,
670 vlib_buffer_get_default_data_size
671 (vm));
Benoît Gannec16fe462020-06-15 12:24:36 +0200672 if (buffer_size > pagesize)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100673 return clib_error_return (0, "buffer size (%llu) is greater than page "
674 "size (%llu)", buffer_size, pagesize);
Benoît Gannec16fe462020-06-15 12:24:36 +0200675
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100676 if (buffers_per_numa == 0)
677 buffers_per_numa = unpriv ? VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV :
678 VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA;
679
680 name = format (0, "buffers-numa-%d%c", numa_node, 0);
Damjan Marion910d3692019-01-21 11:48:34 +0100681 n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
682 error = vlib_physmem_shared_map_create (vm, (char *) name,
683 n_pages * pagesize,
684 min_log2 (pagesize), numa_node,
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100685 physmem_map_index);
686 vec_free (name);
687 return error;
688}
Damjan Marion567e61d2018-10-24 17:08:26 +0200689
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100690static clib_error_t *
691vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node,
692 u8 * index)
693{
694 vlib_buffer_main_t *bm = vm->buffer_main;
695 u32 physmem_map_index;
696 clib_error_t *error;
697 u8 *name = 0;
698
699 if (bm->log2_page_size == CLIB_MEM_PAGE_SZ_UNKNOWN)
Damjan Marion567e61d2018-10-24 17:08:26 +0200700 {
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100701 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
702 &physmem_map_index,
703 CLIB_MEM_PAGE_SZ_DEFAULT_HUGE,
704 0 /* unpriv */ );
705 if (!error)
706 goto buffer_pool_create;
707
708 /* If alloc failed, retry without hugepages */
709 vlib_log_warn (bm->log_default,
710 "numa[%u] falling back to non-hugepage backed "
711 "buffer pool (%U)", numa_node, format_clib_error, error);
Damjan Marion567e61d2018-10-24 17:08:26 +0200712 clib_error_free (error);
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100713
714 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
715 &physmem_map_index,
716 CLIB_MEM_PAGE_SZ_DEFAULT,
717 1 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200718 }
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100719 else
720 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
721 &physmem_map_index,
722 bm->log2_page_size,
723 0 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200724 if (error)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100725 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200726
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100727buffer_pool_create:
Damjan Marion910d3692019-01-21 11:48:34 +0100728 name = format (name, "default-numa-%d%c", numa_node, 0);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100729 *index = vlib_buffer_pool_create (vm, (char *) name,
730 vlib_buffer_get_default_data_size (vm),
731 physmem_map_index);
732
733 if (*index == (u8) ~ 0)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100734 error = clib_error_return (0, "maximum number of buffer pools reached");
Guanghua Zhang785daf42019-08-18 08:35:04 +0800735 vec_free (name);
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100736
737
Guanghua Zhang785daf42019-08-18 08:35:04 +0800738 return error;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100739}
740
741void
742vlib_buffer_main_alloc (vlib_main_t * vm)
743{
744 vlib_buffer_main_t *bm;
745
746 if (vm->buffer_main)
747 return;
748
749 vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
750 clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
751 bm->default_data_size = VLIB_BUFFER_DEFAULT_DATA_SIZE;
Damjan Marion910d3692019-01-21 11:48:34 +0100752}
753
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800754static u32
755buffer_get_cached (vlib_buffer_pool_t * bp)
756{
757 u32 cached = 0;
758 vlib_buffer_pool_thread_t *bpt;
759
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800760 clib_spinlock_lock (&bp->lock);
761
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800762 /* *INDENT-OFF* */
763 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100764 cached += bpt->n_cached;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800765 /* *INDENT-ON* */
766
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800767 clib_spinlock_unlock (&bp->lock);
768
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800769 return cached;
770}
771
772static vlib_buffer_pool_t *
Ole Troan849cbe22019-02-26 13:10:56 +0100773buffer_get_by_index (vlib_buffer_main_t * bm, u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800774{
775 vlib_buffer_pool_t *bp;
Ole Troan849cbe22019-02-26 13:10:56 +0100776 if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index)
777 return 0;
778 bp = vec_elt_at_index (bm->buffer_pools, index);
Steven Luong95155102019-03-05 15:29:22 -0800779
Ole Troan849cbe22019-02-26 13:10:56 +0100780 return bp;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800781}
782
783static void
Damjan Marion8973b072022-03-01 15:51:18 +0100784buffer_gauges_collect_used_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800785{
786 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100787 vlib_buffer_pool_t *bp =
788 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800789 if (!bp)
790 return;
791
Damjan Marion8973b072022-03-01 15:51:18 +0100792 d->entry->value = bp->n_buffers - bp->n_avail - buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800793}
794
795static void
Damjan Marion8973b072022-03-01 15:51:18 +0100796buffer_gauges_collect_available_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800797{
798 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100799 vlib_buffer_pool_t *bp =
800 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800801 if (!bp)
802 return;
803
Damjan Marion8973b072022-03-01 15:51:18 +0100804 d->entry->value = bp->n_avail;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800805}
806
807static void
Damjan Marion8973b072022-03-01 15:51:18 +0100808buffer_gauges_collect_cached_fn (vlib_stats_collector_data_t *d)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800809{
810 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8973b072022-03-01 15:51:18 +0100811 vlib_buffer_pool_t *bp =
812 buffer_get_by_index (vm->buffer_main, d->private_data);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800813 if (!bp)
814 return;
815
Damjan Marion8973b072022-03-01 15:51:18 +0100816 d->entry->value = buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800817}
818
Damjan Marion910d3692019-01-21 11:48:34 +0100819clib_error_t *
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800820vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion910d3692019-01-21 11:48:34 +0100821{
822 vlib_buffer_main_t *bm;
823 clib_error_t *err;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100824 clib_bitmap_t *bmp = 0, *bmp_has_memory = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100825 u32 numa_node;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800826 vlib_buffer_pool_t *bp;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100827 u8 *name = 0, first_valid_buffer_pool_index = ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100828
Damjan Marion5de3fec2019-02-06 14:22:32 +0100829 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100830
831 bm = vm->buffer_main;
832 bm->log_default = vlib_log_register_class ("buffer", 0);
833 bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
834
835 clib_spinlock_init (&bm->buffer_known_hash_lockp);
836
Damjan Marionb592d1b2019-02-28 23:16:11 +0100837 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
838 unformat_bitmap_list, &bmp)))
839 clib_error_free (err);
840
841 if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U",
842 unformat_bitmap_list, &bmp_has_memory)))
843 clib_error_free (err);
844
845 if (bmp && bmp_has_memory)
846 bmp = clib_bitmap_and (bmp, bmp_has_memory);
847
848 /* no info from sysfs, assuming that only numa 0 exists */
849 if (bmp == 0)
850 bmp = clib_bitmap_set (bmp, 0, 1);
851
852 if (clib_bitmap_last_set (bmp) >= VLIB_BUFFER_MAX_NUMA_NODES)
853 clib_panic ("system have more than %u NUMA nodes",
854 VLIB_BUFFER_MAX_NUMA_NODES);
Damjan Marion910d3692019-01-21 11:48:34 +0100855
856 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100857 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100858 {
859 u8 *index = bm->default_buffer_pool_index_for_numa + numa_node;
860 index[0] = ~0;
861 if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index)))
862 {
863 clib_error_report (err);
864 clib_error_free (err);
865 continue;
866 }
867
868 if (first_valid_buffer_pool_index == 0xff)
869 first_valid_buffer_pool_index = index[0];
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100870 }
Damjan Marion910d3692019-01-21 11:48:34 +0100871 /* *INDENT-ON* */
872
Damjan Marionb592d1b2019-02-28 23:16:11 +0100873 if (first_valid_buffer_pool_index == (u8) ~ 0)
874 {
875 err = clib_error_return (0, "failed to allocate buffer pool(s)");
876 goto done;
877 }
878
879 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100880 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100881 {
882 if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0)
883 bm->default_buffer_pool_index_for_numa[numa_node] =
884 first_valid_buffer_pool_index;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100885 }
Damjan Marionb592d1b2019-02-28 23:16:11 +0100886 /* *INDENT-ON* */
Damjan Marion910d3692019-01-21 11:48:34 +0100887
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800888 vec_foreach (bp, bm->buffer_pools)
889 {
Damjan Marion8973b072022-03-01 15:51:18 +0100890 vlib_stats_collector_reg_t reg = { .private_data = bp - bm->buffer_pools };
Damjan Marionb592d1b2019-02-28 23:16:11 +0100891 if (bp->n_buffers == 0)
892 continue;
893
Damjan Marion8973b072022-03-01 15:51:18 +0100894 reg.entry_index =
895 vlib_stats_add_gauge ("/buffer-pools/%s/cached", bp->name);
896 reg.collect_fn = buffer_gauges_collect_cached_fn;
897 vlib_stats_register_collector_fn (&reg);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100898
Damjan Marion8973b072022-03-01 15:51:18 +0100899 reg.entry_index = vlib_stats_add_gauge ("/buffer-pools/%s/used", bp->name);
900 reg.collect_fn = buffer_gauges_collect_used_fn;
901 vlib_stats_register_collector_fn (&reg);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100902
Damjan Marion8973b072022-03-01 15:51:18 +0100903 reg.entry_index =
904 vlib_stats_add_gauge ("/buffer-pools/%s/available", bp->name);
905 reg.collect_fn = buffer_gauges_collect_available_fn;
906 vlib_stats_register_collector_fn (&reg);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800907 }
908
Damjan Marion910d3692019-01-21 11:48:34 +0100909done:
910 vec_free (bmp);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100911 vec_free (bmp_has_memory);
912 vec_free (name);
Damjan Marion910d3692019-01-21 11:48:34 +0100913 return err;
Damjan Marion878c6092017-01-04 13:19:27 +0100914}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100915
Damjan Marion49d66f12017-07-20 18:10:35 +0200916static clib_error_t *
917vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
918{
Damjan Marion910d3692019-01-21 11:48:34 +0100919 vlib_buffer_main_t *bm;
920
Damjan Marion5de3fec2019-02-06 14:22:32 +0100921 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100922
923 bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100924 bm->log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN;
Damjan Marion49d66f12017-07-20 18:10:35 +0200925
926 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
927 {
Damjan Marion910d3692019-01-21 11:48:34 +0100928 if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
929 ;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100930 else if (unformat (input, "page-size %U", unformat_log2_page_size,
931 &bm->log2_page_size))
932 ;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100933 else if (unformat (input, "default data-size %u",
934 &bm->default_data_size))
935 ;
Damjan Marion49d66f12017-07-20 18:10:35 +0200936 else
937 return unformat_parse_error (input);
938 }
939
940 unformat_free (input);
941 return 0;
942}
943
944VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
945
Dave Barachc74b43c2020-04-09 17:24:07 -0400946#if VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0
947u32
948vlib_buffer_alloc_may_fail (vlib_main_t * vm, u32 n_buffers)
949{
950 f64 r;
951
952 r = random_f64 (&vm->buffer_alloc_success_seed);
953
954 /* Fail this request? */
955 if (r > vm->buffer_alloc_success_rate)
956 n_buffers--;
957 /* 5% chance of returning nothing at all */
958 if (r > vm->buffer_alloc_success_rate && r > 0.95)
959 n_buffers = 0;
960
961 return n_buffers;
962}
963#endif
Damjan Marion49d66f12017-07-20 18:10:35 +0200964
Benoît Gannee09a2332021-03-09 15:37:49 +0100965__clib_export int
966vlib_buffer_set_alloc_free_callback (
967 vlib_main_t *vm, vlib_buffer_alloc_free_callback_t *alloc_callback_fn,
968 vlib_buffer_alloc_free_callback_t *free_callback_fn)
969{
970 vlib_buffer_main_t *bm = vm->buffer_main;
971 if ((alloc_callback_fn && bm->alloc_callback_fn) ||
972 (free_callback_fn && bm->free_callback_fn))
973 return 1;
974 bm->alloc_callback_fn = alloc_callback_fn;
975 bm->free_callback_fn = free_callback_fn;
976 return 0;
977}
978
Chris Luked4024f52016-09-06 09:32:36 -0400979/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400980/*
981 * fd.io coding-style-patch-verification: ON
982 *
983 * Local Variables:
984 * eval: (c-set-style "gnu")
985 * End:
986 */