blob: e1c871c2a2a771511f63322764bfbae5e2a859b1 [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>
Filip Tehlard2bbdef2019-02-22 05:05:53 -080049#include <vpp/stats/stat_segment.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
Filip Tehlarb806d0e2019-02-28 04:36:10 -080066static void
67buffer_gauges_update_cached_fn (stat_segment_directory_entry_t * e,
68 u32 index);
69
70static void
71buffer_gauges_update_available_fn (stat_segment_directory_entry_t * e,
72 u32 index);
73
74static void
75buffer_gauges_update_used_fn (stat_segment_directory_entry_t * e, u32 index);
76
Dave Barach9b8ffd92016-07-08 08:13:45 -040077uword
78vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
79 vlib_buffer_t * b_first)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barach9b8ffd92016-07-08 08:13:45 -040081 vlib_buffer_t *b = b_first;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 uword l_first = b_first->current_length;
83 uword l = 0;
84 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
85 {
86 b = vlib_get_buffer (vm, b->next_buffer);
87 l += b->current_length;
88 }
89 b_first->total_length_not_including_first_buffer = l;
90 b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
91 return l + l_first;
92}
93
Dave Barach9b8ffd92016-07-08 08:13:45 -040094u8 *
Benoît Ganne43543172019-10-21 15:13:54 +020095format_vlib_buffer_no_chain (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Dave Barach9b8ffd92016-07-08 08:13:45 -040097 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +020098 u32 indent = format_get_indent (s);
Damjan Mariondac03522018-02-01 15:30:13 +010099 u8 *a = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100100
Damjan Mariondac03522018-02-01 15:30:13 +0100101#define _(bit, name, v) \
102 if (v && (b->flags & VLIB_BUFFER_##name)) \
103 a = format (a, "%s ", v);
104 foreach_vlib_buffer_flag
105#undef _
Damjan Marion36eb7c22019-01-18 20:45:30 +0100106 s = format (s, "current data %d, length %d, buffer-pool %d, "
Damjan Marion910d3692019-01-21 11:48:34 +0100107 "ref-count %u", b->current_data, b->current_length,
108 b->buffer_pool_index, b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100109
110 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
111 s = format (s, ", totlen-nifb %d",
112 b->total_length_not_including_first_buffer);
113
114 if (b->flags & VLIB_BUFFER_IS_TRACED)
Dave Baracha638c182019-06-21 18:24:07 -0400115 s = format (s, ", trace handle 0x%x", b->trace_handle);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100116
Damjan Mariondac03522018-02-01 15:30:13 +0100117 if (a)
118 s = format (s, "\n%U%v", format_white_space, indent, a);
119 vec_free (a);
120
Benoît Ganne43543172019-10-21 15:13:54 +0200121 return s;
122}
123
124u8 *
125format_vlib_buffer (u8 * s, va_list * args)
126{
127 vlib_main_t *vm = vlib_get_main ();
128 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
129 u32 indent = format_get_indent (s);
130
131 s = format (s, "%U", format_vlib_buffer_no_chain, b);
132
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100133 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
134 {
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100135 u32 next_buffer = b->next_buffer;
136 b = vlib_get_buffer (vm, next_buffer);
137
Damjan Marionc47ed032017-01-25 14:18:03 +0100138 s =
Damjan Marion910d3692019-01-21 11:48:34 +0100139 format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
Damjan Marionc47ed032017-01-25 14:18:03 +0100140 format_white_space, indent, next_buffer, b->current_length,
Damjan Marion910d3692019-01-21 11:48:34 +0100141 b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100142 }
143
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 return s;
145}
146
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147u8 *
148format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
151
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 s = format (s, "%U, %U",
153 format_vlib_buffer, b,
154 format_hex_bytes, vlib_buffer_get_current (b), 64);
155
156 return s;
157}
158
Dave Barach9b8ffd92016-07-08 08:13:45 -0400159static u8 *
160format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161{
162 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400163 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
165 switch (state)
166 {
167 case VLIB_BUFFER_UNKNOWN:
168 t = "unknown";
169 break;
170
171 case VLIB_BUFFER_KNOWN_ALLOCATED:
172 t = "known-allocated";
173 break;
174
175 case VLIB_BUFFER_KNOWN_FREE:
176 t = "known-free";
177 break;
178
179 default:
180 t = "invalid";
181 break;
182 }
183
184 return format (s, "%s", t);
185}
186
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187u8 *
188format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400190 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
191 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
192
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 while (1)
194 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 vec_add (s, vlib_buffer_get_current (b), b->current_length);
196 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 break;
198 b = vlib_get_buffer (vm, b->next_buffer);
199 }
200
201 return s;
202}
203
204static u8 *
205vlib_validate_buffer_helper (vlib_main_t * vm,
206 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208{
Damjan Marion910d3692019-01-21 11:48:34 +0100209 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400210 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Damjan Marion910d3692019-01-21 11:48:34 +0100212 if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
213 return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
214
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100217
Damjan Marion8934a042019-02-09 23:29:26 +0100218 if (b->current_data + b->current_length >
219 vlib_buffer_get_default_data_size (vm))
Damjan Marion910d3692019-01-21 11:48:34 +0100220 return format (0, "%d-%d beyond end of buffer %d", b->current_data,
Damjan Marion8934a042019-02-09 23:29:26 +0100221 b->current_length, vlib_buffer_get_default_data_size (vm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
Dave Barach9b8ffd92016-07-08 08:13:45 -0400223 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 {
225 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Damjan Mariond50e3472019-01-20 00:03:56 +0100228 k = vlib_buffer_is_known (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
230 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
233 if (unique_hash)
234 {
235 if (hash_get (*unique_hash, b->next_buffer))
236 return format (0, "duplicate buffer 0x%x", b->next_buffer);
237
238 hash_set1 (*unique_hash, b->next_buffer);
239 }
240
241 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
242 if (msg)
243 {
244 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
245 vec_free (msg);
246 return result;
247 }
248 }
249
250 return 0;
251}
252
253u8 *
254vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255{
256 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
257 /* unique_hash */ 0);
258}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
260u8 *
261vlib_validate_buffers (vlib_main_t * vm,
262 u32 * buffers,
263 uword next_buffer_stride,
264 uword n_buffers,
265 vlib_buffer_known_state_t known_state,
266 uword follow_buffer_next)
267{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268 uword i, *hash;
269 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400271 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
273 hash = hash_create (0, 0);
274 for (i = 0; i < n_buffers; i++)
275 {
276 bi = b[0];
277 b += next_buffer_stride;
278
279 /* Buffer is not unique. */
280 if (hash_get (hash, bi))
281 {
282 msg = format (0, "not unique");
283 goto done;
284 }
285
Damjan Mariond50e3472019-01-20 00:03:56 +0100286 k = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 if (k != known_state)
288 {
289 msg = format (0, "is %U; expected %U",
290 format_vlib_buffer_known_state, k,
291 format_vlib_buffer_known_state, known_state);
292 goto done;
293 }
294
295 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
296 if (msg)
297 goto done;
298
299 hash_set1 (hash, bi);
300 }
301
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 if (msg)
304 {
305 result = format (0, "0x%x: %v", bi, msg);
306 vec_free (msg);
307 }
308 hash_free (hash);
309 return result;
310}
311
Dave Barach80f54e22017-03-08 19:08:56 -0500312/*
313 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
314 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
315 *
316 * The only place which should ever expand vlib_mains is start_workers()
317 * in threads.c. It knows about the bootstrap vector.
318 */
319/* *INDENT-OFF* */
320static struct
321{
322 vec_header_t h;
323 vlib_main_t *vm;
324} __attribute__ ((packed)) __bootstrap_vlib_main_vector
325 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
326{
327 .h.len = 1,
328 .vm = &vlib_global_main,
329};
330/* *INDENT-ON* */
331
332vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700335/* When debugging validate that given buffers are either known allocated
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100337void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338vlib_buffer_validate_alloc_free (vlib_main_t * vm,
339 u32 * buffers,
340 uword n_buffers,
341 vlib_buffer_known_state_t expected_state)
342{
Damjan Marion910d3692019-01-21 11:48:34 +0100343 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 uword i, bi, is_free;
346
347 if (CLIB_DEBUG == 0)
348 return;
349
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
351 b = buffers;
352 for (i = 0; i < n_buffers; i++)
353 {
354 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 bi = b[0];
357 b += 1;
Damjan Mariond50e3472019-01-20 00:03:56 +0100358 known = vlib_buffer_is_known (vm, bi);
Damjan Marion910d3692019-01-21 11:48:34 +0100359
360 if (known == VLIB_BUFFER_UNKNOWN &&
361 expected_state == VLIB_BUFFER_KNOWN_FREE)
362 known = VLIB_BUFFER_KNOWN_FREE;
363
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 if (known != expected_state)
365 {
Klement Sekerae82c73a2019-01-30 18:29:28 +0100366 clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
367 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 }
369
Damjan Marion910d3692019-01-21 11:48:34 +0100370 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
371 hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
372 VLIB_BUFFER_KNOWN_ALLOCATED);
373 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 }
375}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377void
378vlib_packet_template_init (vlib_main_t * vm,
379 vlib_packet_template_t * t,
380 void *packet_data,
381 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100382 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100384 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100385
386 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100387 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100388 va_end (va);
389
390 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Dave Barachb7b92992018-10-17 10:38:51 -0400392 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
394 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100395 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Damjan Marion878c6092017-01-04 13:19:27 +0100396 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397}
398
399void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400400vlib_packet_template_get_packet (vlib_main_t * vm,
401 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402{
403 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
406 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
407 return 0;
408
409 *bi_result = bi;
410
411 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500412 clib_memcpy_fast (vlib_buffer_get_current (b),
413 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400414 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
416 return b->data;
417}
418
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500420int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100421vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
422 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423{
424 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400425 vlib_buffer_t *b;
426 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Dave Barach3a63fc52019-01-07 09:15:47 -0500428 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100429 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 goto out_of_buffers;
431
432 d = data;
433 n_left = n_data_bytes;
Damjan Marion8934a042019-02-09 23:29:26 +0100434 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 b = vlib_get_buffer (vm, bi);
437 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
438
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
441 b = vlib_get_buffer (vm, b->next_buffer);
442
443 while (1)
444 {
445 u32 n;
446
447 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448 n_left_this_buffer =
449 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500451 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
452 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 b->current_length += n;
454 n_left -= n;
455 if (n_left == 0)
456 break;
457
458 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100459 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 goto out_of_buffers;
461
462 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
463
464 b = vlib_get_buffer (vm, b->next_buffer);
465 }
466
Dave Barach3a63fc52019-01-07 09:15:47 -0500467 *buffer_index = bi;
468 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Dave Barach9b8ffd92016-07-08 08:13:45 -0400470out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500471 clib_warning ("out of buffers");
472 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473}
474
Pierre Pfister328e99b2016-02-12 13:18:42 +0000475u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100478 vlib_buffer_t ** last, void *data,
479 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000481 vlib_buffer_t *l = *last;
Damjan Marion8934a042019-02-09 23:29:26 +0100482 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000483 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400484 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
485 while (data_len)
486 {
487 u16 max = n_buffer_bytes - l->current_length - l->current_data;
488 if (max == 0)
489 {
Damjan Marion910d3692019-01-21 11:48:34 +0100490 if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
491 first->buffer_pool_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200493 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400494 max = n_buffer_bytes - l->current_length - l->current_data;
495 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000496
Dave Barach9b8ffd92016-07-08 08:13:45 -0400497 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500498 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
499 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400500 vlib_buffer_chain_increase_length (first, l, len);
501 data_len -= len;
502 copied += len;
503 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000504 return copied;
505}
506
Benoît Gannec16fe462020-06-15 12:24:36 +0200507static uword
508vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size)
509{
Damjan Marion0be1b762020-09-04 12:34:58 +0200510 uword alloc_size = ext_hdr_size + sizeof (vlib_buffer_t) + data_size;
511 alloc_size = CLIB_CACHE_LINE_ROUND (alloc_size);
512
513 /* in case when we have even number of cachelines, we add one more for
514 * better cache occupancy */
515 alloc_size |= CLIB_CACHE_LINE_BYTES;
516
517 return alloc_size;
Benoît Gannec16fe462020-06-15 12:24:36 +0200518}
519
Damjan Marionb592d1b2019-02-28 23:16:11 +0100520u8
521vlib_buffer_pool_create (vlib_main_t * vm, char *name, u32 data_size,
522 u32 physmem_map_index)
Damjan Marion04a7f052017-07-10 15:06:17 +0200523{
Damjan Mariond50e3472019-01-20 00:03:56 +0100524 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100525 vlib_buffer_pool_t *bp;
Damjan Marion68b4da62018-09-30 18:26:20 +0200526 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
527 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200528 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion672ab692019-02-06 17:56:57 +0100529 uword i, j;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100530 u32 alloc_size, n_alloc_per_page;
Damjan Marion910d3692019-01-21 11:48:34 +0100531
Damjan Marionb592d1b2019-02-28 23:16:11 +0100532 if (vec_len (bm->buffer_pools) >= 255)
533 return ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100534
Damjan Marionb592d1b2019-02-28 23:16:11 +0100535 vec_add2_aligned (bm->buffer_pools, bp, 1, CLIB_LOG2_CACHE_LINE_BYTES);
Damjan Marion04a7f052017-07-10 15:06:17 +0200536
537 if (bm->buffer_mem_size == 0)
538 {
539 bm->buffer_mem_start = start;
540 bm->buffer_mem_size = size;
541 }
542 else if (start < bm->buffer_mem_start)
543 {
544 bm->buffer_mem_size += bm->buffer_mem_start - start;
545 bm->buffer_mem_start = start;
546 if (size > bm->buffer_mem_size)
547 bm->buffer_mem_size = size;
548 }
549 else if (start > bm->buffer_mem_start)
550 {
551 uword new_size = start - bm->buffer_mem_start + size;
552 if (new_size > bm->buffer_mem_size)
553 bm->buffer_mem_size = new_size;
554 }
555
556 if ((u64) bm->buffer_mem_size >
557 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
558 {
559 clib_panic ("buffer memory size out of range!");
560 }
Damjan Marion149ba772017-10-12 13:09:26 +0200561
Damjan Marion910d3692019-01-21 11:48:34 +0100562 bp->start = start;
563 bp->size = size;
564 bp->index = bp - bm->buffer_pools;
565 bp->buffer_template.buffer_pool_index = bp->index;
566 bp->buffer_template.ref_count = 1;
567 bp->physmem_map_index = physmem_map_index;
568 bp->name = format (0, "%s%c", name, 0);
569 bp->data_size = data_size;
570 bp->numa_node = m->numa_node;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100571
Damjan Marion910d3692019-01-21 11:48:34 +0100572 vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
573 CLIB_CACHE_LINE_BYTES);
574
Benoît Gannec16fe462020-06-15 12:24:36 +0200575 alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size);
Damjan Marion910d3692019-01-21 11:48:34 +0100576 n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
577
578 /* preallocate buffer indices memory */
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100579 bp->n_buffers = m->n_pages * n_alloc_per_page;
580 bp->buffers = clib_mem_alloc_aligned (bp->n_buffers * sizeof (u32),
581 CLIB_CACHE_LINE_BYTES);
Damjan Marion910d3692019-01-21 11:48:34 +0100582
583 clib_spinlock_init (&bp->lock);
584
Damjan Marion672ab692019-02-06 17:56:57 +0100585 for (j = 0; j < m->n_pages; j++)
586 for (i = 0; i < n_alloc_per_page; i++)
587 {
588 u8 *p;
589 u32 bi;
Damjan Marion910d3692019-01-21 11:48:34 +0100590
Damjan Marion672ab692019-02-06 17:56:57 +0100591 p = m->base + (j << m->log2_page_size) + i * alloc_size;
592 p += bm->ext_hdr_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100593
Dave Barachc74b43c2020-04-09 17:24:07 -0400594 /*
595 * Waste 1 buffer (maximum) so that 0 is never a valid buffer index.
596 * Allows various places to ASSERT (bi != 0). Much easier
597 * than debugging downstream crashes in successor nodes.
598 */
599 if (p == m->base)
600 continue;
601
Damjan Marion672ab692019-02-06 17:56:57 +0100602 vlib_buffer_copy_template ((vlib_buffer_t *) p, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100603
Damjan Marion672ab692019-02-06 17:56:57 +0100604 bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
Damjan Marion910d3692019-01-21 11:48:34 +0100605
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100606 bp->buffers[bp->n_avail++] = bi;
607
Damjan Marion672ab692019-02-06 17:56:57 +0100608 vlib_get_buffer (vm, bi);
609 }
Damjan Marion910d3692019-01-21 11:48:34 +0100610
Damjan Marionb592d1b2019-02-28 23:16:11 +0100611 return bp->index;
Damjan Marion04a7f052017-07-10 15:06:17 +0200612}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
Dave Barach9b8ffd92016-07-08 08:13:45 -0400614static u8 *
Damjan Marion910d3692019-01-21 11:48:34 +0100615format_vlib_buffer_pool (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616{
Damjan Marion910d3692019-01-21 11:48:34 +0100617 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
618 vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
619 vlib_buffer_pool_thread_t *bpt;
620 u32 cached = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100621
Damjan Marion910d3692019-01-21 11:48:34 +0100622 if (!bp)
623 return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
624 "Pool Name", "Index", "NUMA", "Size", "Data Size",
625 "Total", "Avail", "Cached", "Used");
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100626
Damjan Marion910d3692019-01-21 11:48:34 +0100627 /* *INDENT-OFF* */
628 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100629 cached += bpt->n_cached;
Damjan Marion910d3692019-01-21 11:48:34 +0100630 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100631
Damjan Marion910d3692019-01-21 11:48:34 +0100632 s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u",
633 bp->name, bp->index, bp->numa_node, bp->data_size +
634 sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size,
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100635 bp->data_size, bp->n_buffers, bp->n_avail, cached,
636 bp->n_buffers - bp->n_avail - cached);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637
638 return s;
639}
640
641static clib_error_t *
642show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400643 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Damjan Marion910d3692019-01-21 11:48:34 +0100645 vlib_buffer_main_t *bm = vm->buffer_main;
646 vlib_buffer_pool_t *bp;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100647
Damjan Marion910d3692019-01-21 11:48:34 +0100648 vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100649
Damjan Marion910d3692019-01-21 11:48:34 +0100650 /* *INDENT-OFF* */
651 vec_foreach (bp, bm->buffer_pools)
652 vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, bp);
653 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100654
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655 return 0;
656}
657
Dave Barach9b8ffd92016-07-08 08:13:45 -0400658/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659VLIB_CLI_COMMAND (show_buffers_command, static) = {
660 .path = "show buffers",
661 .short_help = "Show packet buffer allocation",
662 .function = show_buffers,
663};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400664/* *INDENT-ON* */
665
Damjan Marion49d66f12017-07-20 18:10:35 +0200666clib_error_t *
Damjan Marion910d3692019-01-21 11:48:34 +0100667vlib_buffer_worker_init (vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100668{
Damjan Marion910d3692019-01-21 11:48:34 +0100669 vlib_buffer_main_t *bm = vm->buffer_main;
670 vlib_buffer_pool_t *bp;
671
672 /* *INDENT-OFF* */
673 vec_foreach (bp, bm->buffer_pools)
674 {
675 clib_spinlock_lock (&bp->lock);
676 vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
677 CLIB_CACHE_LINE_BYTES);
678 clib_spinlock_unlock (&bp->lock);
679 }
680 /* *INDENT-ON* */
681
682 return 0;
683}
684
685VLIB_WORKER_INIT_FUNCTION (vlib_buffer_worker_init);
686
687static clib_error_t *
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100688vlib_buffer_main_init_numa_alloc (struct vlib_main_t *vm, u32 numa_node,
689 u32 * physmem_map_index,
690 clib_mem_page_sz_t log2_page_size,
691 u8 unpriv)
Damjan Marion910d3692019-01-21 11:48:34 +0100692{
693 vlib_buffer_main_t *bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100694 u32 buffers_per_numa = bm->buffers_per_numa;
Damjan Marion49d66f12017-07-20 18:10:35 +0200695 clib_error_t *error;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100696 u32 buffer_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100697 uword n_pages, pagesize;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100698 u8 *name = 0;
Damjan Marion567e61d2018-10-24 17:08:26 +0200699
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100700 ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN);
Damjan Marion49d66f12017-07-20 18:10:35 +0200701
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100702 pagesize = clib_mem_page_bytes (log2_page_size);
703 buffer_size = vlib_buffer_alloc_size (bm->ext_hdr_size,
704 vlib_buffer_get_default_data_size
705 (vm));
Benoît Gannec16fe462020-06-15 12:24:36 +0200706 if (buffer_size > pagesize)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100707 return clib_error_return (0, "buffer size (%llu) is greater than page "
708 "size (%llu)", buffer_size, pagesize);
Benoît Gannec16fe462020-06-15 12:24:36 +0200709
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100710 if (buffers_per_numa == 0)
711 buffers_per_numa = unpriv ? VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV :
712 VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA;
713
714 name = format (0, "buffers-numa-%d%c", numa_node, 0);
Damjan Marion910d3692019-01-21 11:48:34 +0100715 n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
716 error = vlib_physmem_shared_map_create (vm, (char *) name,
717 n_pages * pagesize,
718 min_log2 (pagesize), numa_node,
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100719 physmem_map_index);
720 vec_free (name);
721 return error;
722}
Damjan Marion567e61d2018-10-24 17:08:26 +0200723
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100724static clib_error_t *
725vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node,
726 u8 * index)
727{
728 vlib_buffer_main_t *bm = vm->buffer_main;
729 u32 physmem_map_index;
730 clib_error_t *error;
731 u8 *name = 0;
732
733 if (bm->log2_page_size == CLIB_MEM_PAGE_SZ_UNKNOWN)
Damjan Marion567e61d2018-10-24 17:08:26 +0200734 {
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100735 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
736 &physmem_map_index,
737 CLIB_MEM_PAGE_SZ_DEFAULT_HUGE,
738 0 /* unpriv */ );
739 if (!error)
740 goto buffer_pool_create;
741
742 /* If alloc failed, retry without hugepages */
743 vlib_log_warn (bm->log_default,
744 "numa[%u] falling back to non-hugepage backed "
745 "buffer pool (%U)", numa_node, format_clib_error, error);
Damjan Marion567e61d2018-10-24 17:08:26 +0200746 clib_error_free (error);
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100747
748 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
749 &physmem_map_index,
750 CLIB_MEM_PAGE_SZ_DEFAULT,
751 1 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200752 }
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100753 else
754 error = vlib_buffer_main_init_numa_alloc (vm, numa_node,
755 &physmem_map_index,
756 bm->log2_page_size,
757 0 /* unpriv */ );
Damjan Marion567e61d2018-10-24 17:08:26 +0200758 if (error)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100759 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200760
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100761buffer_pool_create:
Damjan Marion910d3692019-01-21 11:48:34 +0100762 name = format (name, "default-numa-%d%c", numa_node, 0);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100763 *index = vlib_buffer_pool_create (vm, (char *) name,
764 vlib_buffer_get_default_data_size (vm),
765 physmem_map_index);
766
767 if (*index == (u8) ~ 0)
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100768 error = clib_error_return (0, "maximum number of buffer pools reached");
Guanghua Zhang785daf42019-08-18 08:35:04 +0800769 vec_free (name);
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100770
771
Guanghua Zhang785daf42019-08-18 08:35:04 +0800772 return error;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100773}
774
775void
776vlib_buffer_main_alloc (vlib_main_t * vm)
777{
778 vlib_buffer_main_t *bm;
779
780 if (vm->buffer_main)
781 return;
782
783 vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
784 clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
785 bm->default_data_size = VLIB_BUFFER_DEFAULT_DATA_SIZE;
Damjan Marion910d3692019-01-21 11:48:34 +0100786}
787
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800788static u32
789buffer_get_cached (vlib_buffer_pool_t * bp)
790{
791 u32 cached = 0;
792 vlib_buffer_pool_thread_t *bpt;
793
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800794 clib_spinlock_lock (&bp->lock);
795
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800796 /* *INDENT-OFF* */
797 vec_foreach (bpt, bp->threads)
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100798 cached += bpt->n_cached;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800799 /* *INDENT-ON* */
800
Filip Tehlarb806d0e2019-02-28 04:36:10 -0800801 clib_spinlock_unlock (&bp->lock);
802
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800803 return cached;
804}
805
806static vlib_buffer_pool_t *
Ole Troan849cbe22019-02-26 13:10:56 +0100807buffer_get_by_index (vlib_buffer_main_t * bm, u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800808{
809 vlib_buffer_pool_t *bp;
Ole Troan849cbe22019-02-26 13:10:56 +0100810 if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index)
811 return 0;
812 bp = vec_elt_at_index (bm->buffer_pools, index);
Steven Luong95155102019-03-05 15:29:22 -0800813
Ole Troan849cbe22019-02-26 13:10:56 +0100814 return bp;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800815}
816
817static void
Ole Troan849cbe22019-02-26 13:10:56 +0100818buffer_gauges_update_used_fn (stat_segment_directory_entry_t * e, u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800819{
820 vlib_main_t *vm = vlib_get_main ();
Ole Troan849cbe22019-02-26 13:10:56 +0100821 vlib_buffer_pool_t *bp = buffer_get_by_index (vm->buffer_main, index);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800822 if (!bp)
823 return;
824
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100825 e->value = bp->n_buffers - bp->n_avail - buffer_get_cached (bp);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800826}
827
828static void
Ole Troan849cbe22019-02-26 13:10:56 +0100829buffer_gauges_update_available_fn (stat_segment_directory_entry_t * e,
830 u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800831{
832 vlib_main_t *vm = vlib_get_main ();
Ole Troan849cbe22019-02-26 13:10:56 +0100833 vlib_buffer_pool_t *bp = buffer_get_by_index (vm->buffer_main, index);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800834 if (!bp)
835 return;
836
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100837 e->value = bp->n_avail;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800838}
839
840static void
Ole Troan849cbe22019-02-26 13:10:56 +0100841buffer_gauges_update_cached_fn (stat_segment_directory_entry_t * e, u32 index)
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800842{
843 vlib_main_t *vm = vlib_get_main ();
Ole Troan849cbe22019-02-26 13:10:56 +0100844 vlib_buffer_pool_t *bp = buffer_get_by_index (vm->buffer_main, index);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800845 if (!bp)
846 return;
847
848 e->value = buffer_get_cached (bp);
849}
850
Damjan Marion910d3692019-01-21 11:48:34 +0100851clib_error_t *
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800852vlib_buffer_main_init (struct vlib_main_t * vm)
Damjan Marion910d3692019-01-21 11:48:34 +0100853{
854 vlib_buffer_main_t *bm;
855 clib_error_t *err;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100856 clib_bitmap_t *bmp = 0, *bmp_has_memory = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100857 u32 numa_node;
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800858 vlib_buffer_pool_t *bp;
Damjan Marionb592d1b2019-02-28 23:16:11 +0100859 u8 *name = 0, first_valid_buffer_pool_index = ~0;
Damjan Marion910d3692019-01-21 11:48:34 +0100860
Damjan Marion5de3fec2019-02-06 14:22:32 +0100861 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100862
863 bm = vm->buffer_main;
864 bm->log_default = vlib_log_register_class ("buffer", 0);
865 bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
866
867 clib_spinlock_init (&bm->buffer_known_hash_lockp);
868
Damjan Marionb592d1b2019-02-28 23:16:11 +0100869 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
870 unformat_bitmap_list, &bmp)))
871 clib_error_free (err);
872
873 if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U",
874 unformat_bitmap_list, &bmp_has_memory)))
875 clib_error_free (err);
876
877 if (bmp && bmp_has_memory)
878 bmp = clib_bitmap_and (bmp, bmp_has_memory);
879
880 /* no info from sysfs, assuming that only numa 0 exists */
881 if (bmp == 0)
882 bmp = clib_bitmap_set (bmp, 0, 1);
883
884 if (clib_bitmap_last_set (bmp) >= VLIB_BUFFER_MAX_NUMA_NODES)
885 clib_panic ("system have more than %u NUMA nodes",
886 VLIB_BUFFER_MAX_NUMA_NODES);
Damjan Marion910d3692019-01-21 11:48:34 +0100887
888 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100889 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100890 {
891 u8 *index = bm->default_buffer_pool_index_for_numa + numa_node;
892 index[0] = ~0;
893 if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index)))
894 {
895 clib_error_report (err);
896 clib_error_free (err);
897 continue;
898 }
899
900 if (first_valid_buffer_pool_index == 0xff)
901 first_valid_buffer_pool_index = index[0];
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100902 }
Damjan Marion910d3692019-01-21 11:48:34 +0100903 /* *INDENT-ON* */
904
Damjan Marionb592d1b2019-02-28 23:16:11 +0100905 if (first_valid_buffer_pool_index == (u8) ~ 0)
906 {
907 err = clib_error_return (0, "failed to allocate buffer pool(s)");
908 goto done;
909 }
910
911 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100912 clib_bitmap_foreach (numa_node, bmp)
Damjan Marionb592d1b2019-02-28 23:16:11 +0100913 {
914 if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0)
915 bm->default_buffer_pool_index_for_numa[numa_node] =
916 first_valid_buffer_pool_index;
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100917 }
Damjan Marionb592d1b2019-02-28 23:16:11 +0100918 /* *INDENT-ON* */
Damjan Marion910d3692019-01-21 11:48:34 +0100919
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800920 vec_foreach (bp, bm->buffer_pools)
921 {
Damjan Marionb592d1b2019-02-28 23:16:11 +0100922 if (bp->n_buffers == 0)
923 continue;
924
925 vec_reset_length (name);
926 name = format (name, "/buffer-pools/%s/cached%c", bp->name, 0);
Ole Troan849cbe22019-02-26 13:10:56 +0100927 stat_segment_register_gauge (name, buffer_gauges_update_cached_fn,
928 bp - bm->buffer_pools);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100929
930 vec_reset_length (name);
931 name = format (name, "/buffer-pools/%s/used%c", bp->name, 0);
Ole Troan849cbe22019-02-26 13:10:56 +0100932 stat_segment_register_gauge (name, buffer_gauges_update_used_fn,
933 bp - bm->buffer_pools);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100934
935 vec_reset_length (name);
936 name = format (name, "/buffer-pools/%s/available%c", bp->name, 0);
Ole Troan849cbe22019-02-26 13:10:56 +0100937 stat_segment_register_gauge (name, buffer_gauges_update_available_fn,
938 bp - bm->buffer_pools);
Filip Tehlard2bbdef2019-02-22 05:05:53 -0800939 }
940
Damjan Marion910d3692019-01-21 11:48:34 +0100941done:
942 vec_free (bmp);
Damjan Marionb592d1b2019-02-28 23:16:11 +0100943 vec_free (bmp_has_memory);
944 vec_free (name);
Damjan Marion910d3692019-01-21 11:48:34 +0100945 return err;
Damjan Marion878c6092017-01-04 13:19:27 +0100946}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100947
Damjan Marion49d66f12017-07-20 18:10:35 +0200948static clib_error_t *
949vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
950{
Damjan Marion910d3692019-01-21 11:48:34 +0100951 vlib_buffer_main_t *bm;
952
Damjan Marion5de3fec2019-02-06 14:22:32 +0100953 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100954
955 bm = vm->buffer_main;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100956 bm->log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN;
Damjan Marion49d66f12017-07-20 18:10:35 +0200957
958 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
959 {
Damjan Marion910d3692019-01-21 11:48:34 +0100960 if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
961 ;
Nathan Skrzypczak61559022020-11-23 16:25:21 +0100962 else if (unformat (input, "page-size %U", unformat_log2_page_size,
963 &bm->log2_page_size))
964 ;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100965 else if (unformat (input, "default data-size %u",
966 &bm->default_data_size))
967 ;
Damjan Marion49d66f12017-07-20 18:10:35 +0200968 else
969 return unformat_parse_error (input);
970 }
971
972 unformat_free (input);
973 return 0;
974}
975
976VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
977
Dave Barachc74b43c2020-04-09 17:24:07 -0400978#if VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0
979u32
980vlib_buffer_alloc_may_fail (vlib_main_t * vm, u32 n_buffers)
981{
982 f64 r;
983
984 r = random_f64 (&vm->buffer_alloc_success_seed);
985
986 /* Fail this request? */
987 if (r > vm->buffer_alloc_success_rate)
988 n_buffers--;
989 /* 5% chance of returning nothing at all */
990 if (r > vm->buffer_alloc_success_rate && r > 0.95)
991 n_buffers = 0;
992
993 return n_buffers;
994}
995#endif
Damjan Marion49d66f12017-07-20 18:10:35 +0200996
Chris Luked4024f52016-09-06 09:32:36 -0400997/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400998/*
999 * fd.io coding-style-patch-verification: ON
1000 *
1001 * Local Variables:
1002 * eval: (c-set-style "gnu")
1003 * End:
1004 */