blob: c44d8e49d40c48648015d993184c49ba676d2a11 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
Damjan Marion910d3692019-01-21 11:48:34 +010050#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384
51#define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192
Damjan Marion901d16c2018-10-23 19:50:20 +020052
Damjan Marion910d3692019-01-21 11:48:34 +010053#ifdef CLIB_HAVE_VEC128
54/* Assumptions by vlib_buffer_free_inline: */
55STATIC_ASSERT_FITS_IN (vlib_buffer_t, flags, 16);
56STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16);
57STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16);
58#endif
Damjan Marion04a7f052017-07-10 15:06:17 +020059
Damjan Marion910d3692019-01-21 11:48:34 +010060/* Make sure that buffer template size is not accidentally changed */
61STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
62
63u16 __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 *
83format_vlib_buffer (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
98 if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
99 s = format (s, ", totlen-nifb %d",
100 b->total_length_not_including_first_buffer);
101
102 if (b->flags & VLIB_BUFFER_IS_TRACED)
103 s = format (s, ", trace 0x%x", b->trace_index);
104
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
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100109 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
110 {
111 vlib_main_t *vm = vlib_get_main ();
112 u32 next_buffer = b->next_buffer;
113 b = vlib_get_buffer (vm, next_buffer);
114
Damjan Marionc47ed032017-01-25 14:18:03 +0100115 s =
Damjan Marion910d3692019-01-21 11:48:34 +0100116 format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u",
Damjan Marionc47ed032017-01-25 14:18:03 +0100117 format_white_space, indent, next_buffer, b->current_length,
Damjan Marion910d3692019-01-21 11:48:34 +0100118 b->ref_count);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100119 }
120
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 return s;
122}
123
Dave Barach9b8ffd92016-07-08 08:13:45 -0400124u8 *
125format_vlib_buffer_and_data (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400127 vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *);
128
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 s = format (s, "%U, %U",
130 format_vlib_buffer, b,
131 format_hex_bytes, vlib_buffer_get_current (b), 64);
132
133 return s;
134}
135
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136static u8 *
137format_vlib_buffer_known_state (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138{
139 vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400140 char *t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
142 switch (state)
143 {
144 case VLIB_BUFFER_UNKNOWN:
145 t = "unknown";
146 break;
147
148 case VLIB_BUFFER_KNOWN_ALLOCATED:
149 t = "known-allocated";
150 break;
151
152 case VLIB_BUFFER_KNOWN_FREE:
153 t = "known-free";
154 break;
155
156 default:
157 t = "invalid";
158 break;
159 }
160
161 return format (s, "%s", t);
162}
163
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164u8 *
165format_vlib_buffer_contents (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
168 vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *);
169
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 while (1)
171 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400172 vec_add (s, vlib_buffer_get_current (b), b->current_length);
173 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 break;
175 b = vlib_get_buffer (vm, b->next_buffer);
176 }
177
178 return s;
179}
180
181static u8 *
182vlib_validate_buffer_helper (vlib_main_t * vm,
183 u32 bi,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400184 uword follow_buffer_next, uword ** unique_hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185{
Damjan Marion910d3692019-01-21 11:48:34 +0100186 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Damjan Marion910d3692019-01-21 11:48:34 +0100189 if (vec_len (bm->buffer_pools) <= b->buffer_pool_index)
190 return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index);
191
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192 if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 return format (0, "current data %d before pre-data", b->current_data);
Damjan Marion878c6092017-01-04 13:19:27 +0100194
Damjan Marion8934a042019-02-09 23:29:26 +0100195 if (b->current_data + b->current_length >
196 vlib_buffer_get_default_data_size (vm))
Damjan Marion910d3692019-01-21 11:48:34 +0100197 return format (0, "%d-%d beyond end of buffer %d", b->current_data,
Damjan Marion8934a042019-02-09 23:29:26 +0100198 b->current_length, vlib_buffer_get_default_data_size (vm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 {
202 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203 u8 *msg, *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Damjan Mariond50e3472019-01-20 00:03:56 +0100205 k = vlib_buffer_is_known (vm, b->next_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 if (k != VLIB_BUFFER_KNOWN_ALLOCATED)
207 return format (0, "next 0x%x: %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400208 b->next_buffer, format_vlib_buffer_known_state, k);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
210 if (unique_hash)
211 {
212 if (hash_get (*unique_hash, b->next_buffer))
213 return format (0, "duplicate buffer 0x%x", b->next_buffer);
214
215 hash_set1 (*unique_hash, b->next_buffer);
216 }
217
218 msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next);
219 if (msg)
220 {
221 result = format (0, "next 0x%x: %v", b->next_buffer, msg);
222 vec_free (msg);
223 return result;
224 }
225 }
226
227 return 0;
228}
229
230u8 *
231vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400232{
233 return vlib_validate_buffer_helper (vm, bi, follow_buffer_next,
234 /* unique_hash */ 0);
235}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
237u8 *
238vlib_validate_buffers (vlib_main_t * vm,
239 u32 * buffers,
240 uword next_buffer_stride,
241 uword n_buffers,
242 vlib_buffer_known_state_t known_state,
243 uword follow_buffer_next)
244{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400245 uword i, *hash;
246 u32 bi, *b = buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 vlib_buffer_known_state_t k;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400248 u8 *msg = 0, *result = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 hash = hash_create (0, 0);
251 for (i = 0; i < n_buffers; i++)
252 {
253 bi = b[0];
254 b += next_buffer_stride;
255
256 /* Buffer is not unique. */
257 if (hash_get (hash, bi))
258 {
259 msg = format (0, "not unique");
260 goto done;
261 }
262
Damjan Mariond50e3472019-01-20 00:03:56 +0100263 k = vlib_buffer_is_known (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 if (k != known_state)
265 {
266 msg = format (0, "is %U; expected %U",
267 format_vlib_buffer_known_state, k,
268 format_vlib_buffer_known_state, known_state);
269 goto done;
270 }
271
272 msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash);
273 if (msg)
274 goto done;
275
276 hash_set1 (hash, bi);
277 }
278
Dave Barach9b8ffd92016-07-08 08:13:45 -0400279done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 if (msg)
281 {
282 result = format (0, "0x%x: %v", bi, msg);
283 vec_free (msg);
284 }
285 hash_free (hash);
286 return result;
287}
288
Dave Barach80f54e22017-03-08 19:08:56 -0500289/*
290 * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1
291 * and vlib_mains[0] = &vlib_global_main from the beginning of time.
292 *
293 * The only place which should ever expand vlib_mains is start_workers()
294 * in threads.c. It knows about the bootstrap vector.
295 */
296/* *INDENT-OFF* */
297static struct
298{
299 vec_header_t h;
300 vlib_main_t *vm;
301} __attribute__ ((packed)) __bootstrap_vlib_main_vector
302 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
303{
304 .h.len = 1,
305 .vm = &vlib_global_main,
306};
307/* *INDENT-ON* */
308
309vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
310
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312/* When dubugging validate that given buffers are either known allocated
313 or known free. */
Damjan Marionc8a26c62017-11-24 20:15:23 +0100314void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315vlib_buffer_validate_alloc_free (vlib_main_t * vm,
316 u32 * buffers,
317 uword n_buffers,
318 vlib_buffer_known_state_t expected_state)
319{
Damjan Marion910d3692019-01-21 11:48:34 +0100320 vlib_buffer_main_t *bm = vm->buffer_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400321 u32 *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 uword i, bi, is_free;
323
324 if (CLIB_DEBUG == 0)
325 return;
326
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED;
328 b = buffers;
329 for (i = 0; i < n_buffers; i++)
330 {
331 vlib_buffer_known_state_t known;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400332
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 bi = b[0];
334 b += 1;
Damjan Mariond50e3472019-01-20 00:03:56 +0100335 known = vlib_buffer_is_known (vm, bi);
Damjan Marion910d3692019-01-21 11:48:34 +0100336
337 if (known == VLIB_BUFFER_UNKNOWN &&
338 expected_state == VLIB_BUFFER_KNOWN_FREE)
339 known = VLIB_BUFFER_KNOWN_FREE;
340
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 if (known != expected_state)
342 {
Klement Sekerae82c73a2019-01-30 18:29:28 +0100343 clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating",
344 format_vlib_buffer_known_state, known, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 }
346
Damjan Marion910d3692019-01-21 11:48:34 +0100347 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
348 hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE :
349 VLIB_BUFFER_KNOWN_ALLOCATED);
350 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 }
352}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354void
355vlib_packet_template_init (vlib_main_t * vm,
356 vlib_packet_template_t * t,
357 void *packet_data,
358 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100359 uword min_n_buffers_each_alloc, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360{
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100361 va_list va;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100362
363 va_start (va, fmt);
Damjan Marion671e60e2018-12-30 18:09:59 +0100364 t->name = va_format (0, fmt, &va);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100365 va_end (va);
366
367 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Dave Barachb7b92992018-10-17 10:38:51 -0400369 clib_memset (t, 0, sizeof (t[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
371 vec_add (t->packet_data, packet_data, n_packet_data_bytes);
Damjan Mariond1274cb2018-03-13 21:32:17 +0100372 t->min_n_buffers_each_alloc = min_n_buffers_each_alloc;
Damjan Marion878c6092017-01-04 13:19:27 +0100373 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374}
375
376void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377vlib_packet_template_get_packet (vlib_main_t * vm,
378 vlib_packet_template_t * t, u32 * bi_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379{
380 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400381 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
383 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
384 return 0;
385
386 *bi_result = bi;
387
388 b = vlib_get_buffer (vm, bi);
Dave Barach178cf492018-11-13 16:34:13 -0500389 clib_memcpy_fast (vlib_buffer_get_current (b),
390 t->packet_data, vec_len (t->packet_data));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400391 b->current_length = vec_len (t->packet_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
393 return b->data;
394}
395
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396/* Append given data to end of buffer, possibly allocating new buffers. */
Dave Barach3a63fc52019-01-07 09:15:47 -0500397int
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100398vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
399 u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400{
401 u32 n_buffer_bytes, n_left, n_left_this_buffer, bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402 vlib_buffer_t *b;
403 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
Dave Barach3a63fc52019-01-07 09:15:47 -0500405 bi = *buffer_index;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100406 if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 goto out_of_buffers;
408
409 d = data;
410 n_left = n_data_bytes;
Damjan Marion8934a042019-02-09 23:29:26 +0100411 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 b = vlib_get_buffer (vm, bi);
414 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
415
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416 /* Get to the end of the chain before we try to append data... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
418 b = vlib_get_buffer (vm, b->next_buffer);
419
420 while (1)
421 {
422 u32 n;
423
424 ASSERT (n_buffer_bytes >= b->current_length);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400425 n_left_this_buffer =
426 n_buffer_bytes - (b->current_data + b->current_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 n = clib_min (n_left_this_buffer, n_left);
Dave Barach178cf492018-11-13 16:34:13 -0500428 clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d,
429 n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 b->current_length += n;
431 n_left -= n;
432 if (n_left == 0)
433 break;
434
435 d += n;
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100436 if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 goto out_of_buffers;
438
439 b->flags |= VLIB_BUFFER_NEXT_PRESENT;
440
441 b = vlib_get_buffer (vm, b->next_buffer);
442 }
443
Dave Barach3a63fc52019-01-07 09:15:47 -0500444 *buffer_index = bi;
445 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447out_of_buffers:
Dave Barach3a63fc52019-01-07 09:15:47 -0500448 clib_warning ("out of buffers");
449 return 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450}
451
Pierre Pfister328e99b2016-02-12 13:18:42 +0000452u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400453vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100455 vlib_buffer_t ** last, void *data,
456 u16 data_len)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457{
Pierre Pfister328e99b2016-02-12 13:18:42 +0000458 vlib_buffer_t *l = *last;
Damjan Marion8934a042019-02-09 23:29:26 +0100459 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000460 u16 copied = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461 ASSERT (n_buffer_bytes >= l->current_length + l->current_data);
462 while (data_len)
463 {
464 u16 max = n_buffer_bytes - l->current_length - l->current_data;
465 if (max == 0)
466 {
Damjan Marion910d3692019-01-21 11:48:34 +0100467 if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1,
468 first->buffer_pool_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400469 return copied;
Eyal Barib688fb12018-11-12 16:13:49 +0200470 *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 max = n_buffer_bytes - l->current_length - l->current_data;
472 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000473
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 u16 len = (data_len > max) ? max : data_len;
Dave Barach178cf492018-11-13 16:34:13 -0500475 clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length,
476 data + copied, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 vlib_buffer_chain_increase_length (first, l, len);
478 data_len -= len;
479 copied += len;
480 }
Pierre Pfister328e99b2016-02-12 13:18:42 +0000481 return copied;
482}
483
Damjan Marion910d3692019-01-21 11:48:34 +0100484clib_error_t *
485vlib_buffer_pool_create (vlib_main_t * vm, u8 index, char *name,
486 u32 data_size, u32 physmem_map_index)
Damjan Marion04a7f052017-07-10 15:06:17 +0200487{
Damjan Mariond50e3472019-01-20 00:03:56 +0100488 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100489 vlib_buffer_pool_t *bp;
Damjan Marion68b4da62018-09-30 18:26:20 +0200490 vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index);
491 uword start = pointer_to_uword (m->base);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200492 uword size = (uword) m->n_pages << m->log2_page_size;
Damjan Marion672ab692019-02-06 17:56:57 +0100493 uword i, j;
Damjan Marion910d3692019-01-21 11:48:34 +0100494 u32 alloc_size, n_alloc_per_page;;
495
496 vec_validate_aligned (bm->buffer_pools, index, CLIB_CACHE_LINE_BYTES);
497 bp = vec_elt_at_index (bm->buffer_pools, index);
498
499 if (bp->start)
500 return clib_error_return (0, "buffer with index %u already exists",
501 index);
502
503 if (index >= 255)
504 return clib_error_return (0, "buffer index must be < 255", index);
Damjan Marion04a7f052017-07-10 15:06:17 +0200505
506 if (bm->buffer_mem_size == 0)
507 {
508 bm->buffer_mem_start = start;
509 bm->buffer_mem_size = size;
510 }
511 else if (start < bm->buffer_mem_start)
512 {
513 bm->buffer_mem_size += bm->buffer_mem_start - start;
514 bm->buffer_mem_start = start;
515 if (size > bm->buffer_mem_size)
516 bm->buffer_mem_size = size;
517 }
518 else if (start > bm->buffer_mem_start)
519 {
520 uword new_size = start - bm->buffer_mem_start + size;
521 if (new_size > bm->buffer_mem_size)
522 bm->buffer_mem_size = new_size;
523 }
524
525 if ((u64) bm->buffer_mem_size >
526 ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES)))
527 {
528 clib_panic ("buffer memory size out of range!");
529 }
Damjan Marion149ba772017-10-12 13:09:26 +0200530
Damjan Marion910d3692019-01-21 11:48:34 +0100531 bp->start = start;
532 bp->size = size;
533 bp->index = bp - bm->buffer_pools;
534 bp->buffer_template.buffer_pool_index = bp->index;
535 bp->buffer_template.ref_count = 1;
536 bp->physmem_map_index = physmem_map_index;
537 bp->name = format (0, "%s%c", name, 0);
538 bp->data_size = data_size;
539 bp->numa_node = m->numa_node;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100540
Damjan Marion910d3692019-01-21 11:48:34 +0100541 vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
542 CLIB_CACHE_LINE_BYTES);
543
544 alloc_size = data_size + sizeof (vlib_buffer_t) + bm->ext_hdr_size;
545 n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size;
546
547 /* preallocate buffer indices memory */
548 vec_validate_aligned (bp->buffers, m->n_pages * n_alloc_per_page,
549 CLIB_CACHE_LINE_BYTES);
550 vec_reset_length (bp->buffers);
551
552 clib_spinlock_init (&bp->lock);
553
Damjan Marion672ab692019-02-06 17:56:57 +0100554 for (j = 0; j < m->n_pages; j++)
555 for (i = 0; i < n_alloc_per_page; i++)
556 {
557 u8 *p;
558 u32 bi;
Damjan Marion910d3692019-01-21 11:48:34 +0100559
Damjan Marion672ab692019-02-06 17:56:57 +0100560 p = m->base + (j << m->log2_page_size) + i * alloc_size;
561 p += bm->ext_hdr_size;
Damjan Marion910d3692019-01-21 11:48:34 +0100562
Damjan Marion672ab692019-02-06 17:56:57 +0100563 vlib_buffer_copy_template ((vlib_buffer_t *) p, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100564
Damjan Marion672ab692019-02-06 17:56:57 +0100565 bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p);
Damjan Marion910d3692019-01-21 11:48:34 +0100566
Damjan Marion672ab692019-02-06 17:56:57 +0100567 vec_add1_aligned (bp->buffers, bi, CLIB_CACHE_LINE_BYTES);
568 vlib_get_buffer (vm, bi);
569 }
Damjan Marion910d3692019-01-21 11:48:34 +0100570
Damjan Marion672ab692019-02-06 17:56:57 +0100571 bp->n_buffers = vec_len (bp->buffers);
Damjan Marion910d3692019-01-21 11:48:34 +0100572
573 return 0;
Damjan Marion04a7f052017-07-10 15:06:17 +0200574}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Dave Barach9b8ffd92016-07-08 08:13:45 -0400576static u8 *
Damjan Marion910d3692019-01-21 11:48:34 +0100577format_vlib_buffer_pool (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578{
Damjan Marion910d3692019-01-21 11:48:34 +0100579 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
580 vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *);
581 vlib_buffer_pool_thread_t *bpt;
582 u32 cached = 0;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100583
Damjan Marion910d3692019-01-21 11:48:34 +0100584 if (!bp)
585 return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s",
586 "Pool Name", "Index", "NUMA", "Size", "Data Size",
587 "Total", "Avail", "Cached", "Used");
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100588
Damjan Marion910d3692019-01-21 11:48:34 +0100589 /* *INDENT-OFF* */
590 vec_foreach (bpt, bp->threads)
591 cached += vec_len (bpt->cached_buffers);
592 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100593
Damjan Marion910d3692019-01-21 11:48:34 +0100594 s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u",
595 bp->name, bp->index, bp->numa_node, bp->data_size +
596 sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size,
597 bp->data_size, bp->n_buffers, vec_len (bp->buffers), cached,
598 bp->n_buffers - vec_len (bp->buffers) - cached);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
600 return s;
601}
602
603static clib_error_t *
604show_buffers (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400605 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606{
Damjan Marion910d3692019-01-21 11:48:34 +0100607 vlib_buffer_main_t *bm = vm->buffer_main;
608 vlib_buffer_pool_t *bp;
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100609
Damjan Marion910d3692019-01-21 11:48:34 +0100610 vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, 0);
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100611
Damjan Marion910d3692019-01-21 11:48:34 +0100612 /* *INDENT-OFF* */
613 vec_foreach (bp, bm->buffer_pools)
614 vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, bp);
615 /* *INDENT-ON* */
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100616
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 return 0;
618}
619
Dave Barach9b8ffd92016-07-08 08:13:45 -0400620/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621VLIB_CLI_COMMAND (show_buffers_command, static) = {
622 .path = "show buffers",
623 .short_help = "Show packet buffer allocation",
624 .function = show_buffers,
625};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626/* *INDENT-ON* */
627
Damjan Marion49d66f12017-07-20 18:10:35 +0200628clib_error_t *
Damjan Marion910d3692019-01-21 11:48:34 +0100629vlib_buffer_worker_init (vlib_main_t * vm)
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100630{
Damjan Marion910d3692019-01-21 11:48:34 +0100631 vlib_buffer_main_t *bm = vm->buffer_main;
632 vlib_buffer_pool_t *bp;
633
634 /* *INDENT-OFF* */
635 vec_foreach (bp, bm->buffer_pools)
636 {
637 clib_spinlock_lock (&bp->lock);
638 vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1,
639 CLIB_CACHE_LINE_BYTES);
640 clib_spinlock_unlock (&bp->lock);
641 }
642 /* *INDENT-ON* */
643
644 return 0;
645}
646
647VLIB_WORKER_INIT_FUNCTION (vlib_buffer_worker_init);
648
649static clib_error_t *
650vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node)
651{
652 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200653 clib_error_t *error;
Damjan Marion68b4da62018-09-30 18:26:20 +0200654 u32 physmem_map_index;
Damjan Marion910d3692019-01-21 11:48:34 +0100655 uword n_pages, pagesize;
656 u32 buffers_per_numa;
657 u32 buffer_size = CLIB_CACHE_LINE_ROUND (bm->ext_hdr_size +
658 sizeof (vlib_buffer_t) +
Damjan Marion8934a042019-02-09 23:29:26 +0100659 vlib_buffer_get_default_data_size
660 (vm));
Damjan Marion910d3692019-01-21 11:48:34 +0100661 u8 *name;
Damjan Marion567e61d2018-10-24 17:08:26 +0200662
Damjan Marion910d3692019-01-21 11:48:34 +0100663 pagesize = clib_mem_get_default_hugepage_size ();
664 name = format (0, "buffers-numa-%d%c", numa_node, 0);
Damjan Marion49d66f12017-07-20 18:10:35 +0200665
Damjan Marion910d3692019-01-21 11:48:34 +0100666 buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
667 VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA;
Damjan Marion49d66f12017-07-20 18:10:35 +0200668
Damjan Marion567e61d2018-10-24 17:08:26 +0200669retry:
Damjan Marion910d3692019-01-21 11:48:34 +0100670 n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1;
671 error = vlib_physmem_shared_map_create (vm, (char *) name,
672 n_pages * pagesize,
673 min_log2 (pagesize), numa_node,
Damjan Marion567e61d2018-10-24 17:08:26 +0200674 &physmem_map_index);
675
Damjan Marion910d3692019-01-21 11:48:34 +0100676 if (error && pagesize != clib_mem_get_page_size ())
Damjan Marion567e61d2018-10-24 17:08:26 +0200677 {
Damjan Marion910d3692019-01-21 11:48:34 +0100678 vlib_log_warn (bm->log_default, "%U", format_clib_error, error);
Damjan Marion567e61d2018-10-24 17:08:26 +0200679 clib_error_free (error);
Damjan Marion910d3692019-01-21 11:48:34 +0100680 vlib_log_warn (bm->log_default, "falling back to non-hugepage "
Damjan Marion567e61d2018-10-24 17:08:26 +0200681 "backed buffer pool");
Damjan Marion910d3692019-01-21 11:48:34 +0100682 pagesize = clib_mem_get_page_size ();
683 buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa :
684 VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV;
Damjan Marion567e61d2018-10-24 17:08:26 +0200685 goto retry;
686 }
687
688 if (error)
Damjan Marion68b4da62018-09-30 18:26:20 +0200689 return error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200690
Damjan Marion910d3692019-01-21 11:48:34 +0100691 vec_reset_length (name);
692 name = format (name, "default-numa-%d%c", numa_node, 0);
Damjan Marion49d66f12017-07-20 18:10:35 +0200693
Damjan Marion910d3692019-01-21 11:48:34 +0100694 return vlib_buffer_pool_create (vm, numa_node, (char *) name,
Damjan Marion8934a042019-02-09 23:29:26 +0100695 vlib_buffer_get_default_data_size (vm),
Damjan Marion5de3fec2019-02-06 14:22:32 +0100696 physmem_map_index);
697}
698
699void
700vlib_buffer_main_alloc (vlib_main_t * vm)
701{
702 vlib_buffer_main_t *bm;
703
704 if (vm->buffer_main)
705 return;
706
707 vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0]));
708 clib_memset (vm->buffer_main, 0, sizeof (bm[0]));
709 bm->default_data_size = VLIB_BUFFER_DEFAULT_DATA_SIZE;
Damjan Marion910d3692019-01-21 11:48:34 +0100710}
711
712clib_error_t *
713vlib_buffer_main_init (struct vlib_main_t *vm)
714{
715 vlib_buffer_main_t *bm;
716 clib_error_t *err;
717 clib_bitmap_t *bmp = 0;
718 u32 numa_node;
719
Damjan Marion5de3fec2019-02-06 14:22:32 +0100720 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100721
722 bm = vm->buffer_main;
723 bm->log_default = vlib_log_register_class ("buffer", 0);
724 bm->ext_hdr_size = __vlib_buffer_external_hdr_size;
725
726 clib_spinlock_init (&bm->buffer_known_hash_lockp);
727
728 err = clib_sysfs_read ("/sys/devices/system/node/possible", "%U",
729 unformat_bitmap_list, &bmp);
730 if (err)
731 {
732 /* no info from sysfs, assuming that only numa 0 exists */
733 clib_error_free (err);
734 bmp = clib_bitmap_set (bmp, 0, 1);
735 }
736
737 /* *INDENT-OFF* */
738 clib_bitmap_foreach (numa_node, bmp, {
739 if ((err = vlib_buffer_main_init_numa_node(vm, numa_node)))
740 goto done;
741 });
742 /* *INDENT-ON* */
743
744 bm->n_numa_nodes = clib_bitmap_last_set (bmp) + 1;
745
746done:
747 vec_free (bmp);
748 return err;
Damjan Marion878c6092017-01-04 13:19:27 +0100749}
Damjan Marion1cd8f3c2016-11-24 02:07:32 +0100750
Damjan Marion49d66f12017-07-20 18:10:35 +0200751static clib_error_t *
752vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input)
753{
Damjan Marion910d3692019-01-21 11:48:34 +0100754 vlib_buffer_main_t *bm;
755
Damjan Marion5de3fec2019-02-06 14:22:32 +0100756 vlib_buffer_main_alloc (vm);
Damjan Marion910d3692019-01-21 11:48:34 +0100757
758 bm = vm->buffer_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200759
760 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
761 {
Damjan Marion910d3692019-01-21 11:48:34 +0100762 if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa))
763 ;
Damjan Marion5de3fec2019-02-06 14:22:32 +0100764 else if (unformat (input, "default data-size %u",
765 &bm->default_data_size))
766 ;
Damjan Marion49d66f12017-07-20 18:10:35 +0200767 else
768 return unformat_parse_error (input);
769 }
770
771 unformat_free (input);
772 return 0;
773}
774
775VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers");
776
777
Chris Luked4024f52016-09-06 09:32:36 -0400778/** @endcond */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779/*
780 * fd.io coding-style-patch-verification: ON
781 *
782 * Local Variables:
783 * eval: (c-set-style "gnu")
784 * End:
785 */