blob: 708539957c9fd6b109b2257790c72f2d90cfe8ce [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_funcs.h: VLIB buffer related functions/inlines
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
40#ifndef included_vlib_buffer_funcs_h
41#define included_vlib_buffer_funcs_h
42
43#include <vppinfra/hash.h>
Dave Barachc3a06552018-10-01 09:25:32 -040044#include <vppinfra/fifo.h>
Damjan Marion4c53ff42021-10-28 23:03:04 +020045#include <vppinfra/vector/index_to_ptr.h>
Klement Sekeraf883f6a2019-02-13 11:01:32 +010046#include <vlib/buffer.h>
47#include <vlib/physmem_funcs.h>
48#include <vlib/main.h>
49#include <vlib/node.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
51/** \file
52 vlib buffer access methods.
53*/
54
Damjan Marion1c229712021-04-21 12:55:15 +020055typedef void (vlib_buffer_enqueue_to_next_fn_t) (vlib_main_t *vm,
56 vlib_node_runtime_t *node,
57 u32 *buffers, u16 *nexts,
58 uword count);
Mohammed Hawaricd758e62022-05-31 18:11:05 +020059typedef void (vlib_buffer_enqueue_to_next_with_aux_fn_t) (
60 vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u32 *aux_data,
61 u16 *nexts, uword count);
Damjan Marion1c229712021-04-21 12:55:15 +020062typedef void (vlib_buffer_enqueue_to_single_next_fn_t) (
63 vlib_main_t *vm, vlib_node_runtime_t *node, u32 *ers, u16 next_index,
64 u32 count);
65
Mohammed Hawaricd758e62022-05-31 18:11:05 +020066typedef void (vlib_buffer_enqueue_to_single_next_with_aux_fn_t) (
67 vlib_main_t *vm, vlib_node_runtime_t *node, u32 *ers, u32 *aux_data,
68 u16 next_index, u32 count);
69
Damjan Marion1c229712021-04-21 12:55:15 +020070typedef u32 (vlib_buffer_enqueue_to_thread_fn_t) (
Damjan Marion9e7a0b42021-05-14 14:50:01 +020071 vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
72 u32 *buffer_indices, u16 *thread_indices, u32 n_packets,
73 int drop_on_congestion);
Damjan Marioneee099e2021-05-01 14:56:13 +020074
Mohammed Hawarie7149262022-05-18 10:08:47 +020075typedef u32 (vlib_buffer_enqueue_to_thread_with_aux_fn_t) (
76 vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
77 u32 *buffer_indices, u32 *aux, u16 *thread_indices, u32 n_packets,
78 int drop_on_congestion);
Damjan Marioneee099e2021-05-01 14:56:13 +020079
Damjan Marion1c229712021-04-21 12:55:15 +020080typedef struct
81{
82 vlib_buffer_enqueue_to_next_fn_t *buffer_enqueue_to_next_fn;
Mohammed Hawaricd758e62022-05-31 18:11:05 +020083 vlib_buffer_enqueue_to_next_with_aux_fn_t
84 *buffer_enqueue_to_next_with_aux_fn;
Damjan Marion1c229712021-04-21 12:55:15 +020085 vlib_buffer_enqueue_to_single_next_fn_t *buffer_enqueue_to_single_next_fn;
Mohammed Hawaricd758e62022-05-31 18:11:05 +020086 vlib_buffer_enqueue_to_single_next_with_aux_fn_t
87 *buffer_enqueue_to_single_next_with_aux_fn;
Damjan Marion1c229712021-04-21 12:55:15 +020088 vlib_buffer_enqueue_to_thread_fn_t *buffer_enqueue_to_thread_fn;
Mohammed Hawarie7149262022-05-18 10:08:47 +020089 vlib_buffer_enqueue_to_thread_with_aux_fn_t
90 *buffer_enqueue_to_thread_with_aux_fn;
Damjan Marion1c229712021-04-21 12:55:15 +020091} vlib_buffer_func_main_t;
92
93extern vlib_buffer_func_main_t vlib_buffer_func_main;
94
Damjan Marion24dcbe42019-01-31 12:29:39 +010095always_inline void
96vlib_buffer_validate (vlib_main_t * vm, vlib_buffer_t * b)
97{
98 vlib_buffer_main_t *bm = vm->buffer_main;
99 vlib_buffer_pool_t *bp;
100
101 /* reference count in allocated buffer always must be 1 or higher */
102 ASSERT (b->ref_count > 0);
103
Damjan Marion24dcbe42019-01-31 12:29:39 +0100104 /* verify that buffer pool index is valid */
105 bp = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index);
106 ASSERT (pointer_to_uword (b) >= bp->start);
107 ASSERT (pointer_to_uword (b) < bp->start + bp->size -
Damjan Marion5de3fec2019-02-06 14:22:32 +0100108 (bp->data_size + sizeof (vlib_buffer_t)));
Damjan Marion24dcbe42019-01-31 12:29:39 +0100109}
110
111always_inline void *
112vlib_buffer_ptr_from_index (uword buffer_mem_start, u32 buffer_index,
113 uword offset)
114{
115 offset += ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES;
116 return uword_to_pointer (buffer_mem_start + offset, vlib_buffer_t *);
117}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119/** \brief Translate buffer index into buffer pointer
120
121 @param vm - (vlib_main_t *) vlib main data structure pointer
122 @param buffer_index - (u32) buffer index
123 @return - (vlib_buffer_t *) buffer pointer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400124*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125always_inline vlib_buffer_t *
126vlib_get_buffer (vlib_main_t * vm, u32 buffer_index)
127{
Damjan Mariond50e3472019-01-20 00:03:56 +0100128 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion24dcbe42019-01-31 12:29:39 +0100129 vlib_buffer_t *b;
Damjan Marion04a7f052017-07-10 15:06:17 +0200130
Damjan Marion24dcbe42019-01-31 12:29:39 +0100131 b = vlib_buffer_ptr_from_index (bm->buffer_mem_start, buffer_index, 0);
132 vlib_buffer_validate (vm, b);
133 return b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134}
135
Damjan Marion5de3fec2019-02-06 14:22:32 +0100136static_always_inline u32
Damjan Marion8934a042019-02-09 23:29:26 +0100137vlib_buffer_get_default_data_size (vlib_main_t * vm)
Damjan Marion5de3fec2019-02-06 14:22:32 +0100138{
139 return vm->buffer_main->default_data_size;
140}
141
Damjan Marione58041f2019-01-18 19:56:09 +0100142static_always_inline void
Damjan Marion64d557c2019-01-18 20:03:41 +0100143vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices)
144{
Damjan Marion856d0622021-04-21 21:11:35 +0200145 clib_memcpy_u32 (dst, src, n_indices);
Damjan Marion64d557c2019-01-18 20:03:41 +0100146}
147
Damjan Marionb9250a92020-03-02 19:02:15 +0100148always_inline void
149vlib_buffer_copy_indices_from_ring (u32 * dst, u32 * ring, u32 start,
150 u32 ring_size, u32 n_buffers)
151{
152 ASSERT (n_buffers <= ring_size);
153
154 if (PREDICT_TRUE (start + n_buffers <= ring_size))
155 {
156 vlib_buffer_copy_indices (dst, ring + start, n_buffers);
157 }
158 else
159 {
160 u32 n = ring_size - start;
161 vlib_buffer_copy_indices (dst, ring + start, n);
162 vlib_buffer_copy_indices (dst + n, ring, n_buffers - n);
163 }
164}
165
Benoît Ganne72f49212020-03-19 11:41:07 +0100166always_inline void
167vlib_buffer_copy_indices_to_ring (u32 * ring, u32 * src, u32 start,
168 u32 ring_size, u32 n_buffers)
169{
170 ASSERT (n_buffers <= ring_size);
171
172 if (PREDICT_TRUE (start + n_buffers <= ring_size))
173 {
174 vlib_buffer_copy_indices (ring + start, src, n_buffers);
175 }
176 else
177 {
178 u32 n = ring_size - start;
179 vlib_buffer_copy_indices (ring + start, src, n);
180 vlib_buffer_copy_indices (ring, src + n, n_buffers - n);
181 }
182}
183
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100184STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +0100185static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +0100186vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
187{
Damjan Marion22f23ae2019-01-24 15:36:57 +0100188#if defined CLIB_HAVE_VEC512
189 b->as_u8x64[0] = bt->as_u8x64[0];
190#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100191 b->as_u8x32[0] = bt->as_u8x32[0];
192 b->as_u8x32[1] = bt->as_u8x32[1];
193#elif defined (CLIB_HAVE_VEC128)
194 b->as_u8x16[0] = bt->as_u8x16[0];
195 b->as_u8x16[1] = bt->as_u8x16[1];
196 b->as_u8x16[2] = bt->as_u8x16[2];
197 b->as_u8x16[3] = bt->as_u8x16[3];
198#else
199 clib_memcpy_fast (b, bt, 64);
200#endif
Damjan Marione58041f2019-01-18 19:56:09 +0100201}
202
Damjan Marion910d3692019-01-21 11:48:34 +0100203always_inline u8
204vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
205{
Damjan Marionb592d1b2019-02-28 23:16:11 +0100206 ASSERT (numa_node < VLIB_BUFFER_MAX_NUMA_NODES);
207 return vm->buffer_main->default_buffer_pool_index_for_numa[numa_node];
Damjan Marion910d3692019-01-21 11:48:34 +0100208}
209
Damjan Marionafe56de2018-05-17 12:44:00 +0200210/** \brief Translate array of buffer indices into buffer pointers with offset
211
212 @param vm - (vlib_main_t *) vlib main data structure pointer
213 @param bi - (u32 *) array of buffer indices
214 @param b - (void **) array to store buffer pointers
215 @param count - (uword) number of elements
216 @param offset - (i32) offset applied to each pointer
217*/
218static_always_inline void
Damjan Marion4c53ff42021-10-28 23:03:04 +0200219vlib_get_buffers_with_offset (vlib_main_t *vm, u32 *bi, void **b, u32 count,
Damjan Marionafe56de2018-05-17 12:44:00 +0200220 i32 offset)
221{
Damjan Mariond50e3472019-01-20 00:03:56 +0100222 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marion4c53ff42021-10-28 23:03:04 +0200223 void *base = (void *) (buffer_mem_start + offset);
224 int objsize = __builtin_object_size (b, 0);
225 const int sh = CLIB_LOG2_CACHE_LINE_BYTES;
226
227 if (COMPILE_TIME_CONST (count) == 0 && objsize >= 64 * sizeof (b[0]) &&
228 (objsize & ((8 * sizeof (b[0])) - 1)) == 0)
Damjan Marion54208852021-04-21 15:25:47 +0200229 {
Damjan Marion4c53ff42021-10-28 23:03:04 +0200230 u32 n = round_pow2 (count, 8);
231 ASSERT (objsize >= count);
232 CLIB_ASSUME (objsize >= count);
233 while (n >= 64)
234 {
235 clib_index_to_ptr_u32 (bi, base, sh, b, 64);
236 b += 64;
237 bi += 64;
238 n -= 64;
239 }
240
241 while (n)
242 {
243 clib_index_to_ptr_u32 (bi, base, sh, b, 8);
244 b += 8;
245 bi += 8;
246 n -= 8;
247 }
Damjan Marion54208852021-04-21 15:25:47 +0200248 }
Damjan Marion4c53ff42021-10-28 23:03:04 +0200249 else
250 clib_index_to_ptr_u32 (bi, base, sh, b, count);
Damjan Marionafe56de2018-05-17 12:44:00 +0200251}
252
253/** \brief Translate array of buffer indices into buffer pointers
254
255 @param vm - (vlib_main_t *) vlib main data structure pointer
256 @param bi - (u32 *) array of buffer indices
257 @param b - (vlib_buffer_t **) array to store buffer pointers
258 @param count - (uword) number of elements
259*/
260
261static_always_inline void
Damjan Marion4c53ff42021-10-28 23:03:04 +0200262vlib_get_buffers (vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, u32 count)
Damjan Marionafe56de2018-05-17 12:44:00 +0200263{
264 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
265}
266
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267/** \brief Translate buffer pointer into buffer index
268
269 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400270 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200273
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400275vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276{
Damjan Mariond50e3472019-01-20 00:03:56 +0100277 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200278 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
279 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
280 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400281 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
283}
284
Damjan Marionafe56de2018-05-17 12:44:00 +0200285/** \brief Translate array of buffer pointers into buffer indices with offset
286
287 @param vm - (vlib_main_t *) vlib main data structure pointer
288 @param b - (void **) array of buffer pointers
289 @param bi - (u32 *) array to store buffer indices
290 @param count - (uword) number of elements
291 @param offset - (i32) offset applied to each pointer
292*/
293static_always_inline void
294vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
295 uword count, i32 offset)
296{
297#ifdef CLIB_HAVE_VEC256
298 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100299 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200300
301 while (count >= 8)
302 {
303 /* load 4 pointers into 256-bit register */
304 u64x4 v0 = u64x4_load_unaligned (b);
305 u64x4 v1 = u64x4_load_unaligned (b + 4);
306 u32x8 v2, v3;
307
308 v0 -= off4;
309 v1 -= off4;
310
311 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
312 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
313
314 /* permute 256-bit register so lower u32s of each buffer index are
315 * placed into lower 128-bits */
316 v2 = u32x8_permute ((u32x8) v0, mask);
317 v3 = u32x8_permute ((u32x8) v1, mask);
318
319 /* extract lower 128-bits and save them to the array of buffer indices */
320 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
321 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
322 bi += 8;
323 b += 8;
324 count -= 8;
325 }
326#endif
327 while (count >= 4)
328 {
329 /* equivalent non-nector implementation */
330 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
331 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
332 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
333 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
334 bi += 4;
335 b += 4;
336 count -= 4;
337 }
338 while (count)
339 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400340 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200341 bi += 1;
342 b += 1;
343 count -= 1;
344 }
345}
346
347/** \brief Translate array of buffer pointers into buffer indices
348
349 @param vm - (vlib_main_t *) vlib main data structure pointer
350 @param b - (vlib_buffer_t **) array of buffer pointers
351 @param bi - (u32 *) array to store buffer indices
352 @param count - (uword) number of elements
353*/
354static_always_inline void
355vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
356 uword count)
357{
358 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
359}
360
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361/** \brief Get next buffer in buffer linklist, or zero for end of list.
362
363 @param vm - (vlib_main_t *) vlib main data structure pointer
364 @param b - (void *) buffer pointer
365 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400366*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367always_inline vlib_buffer_t *
368vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
369{
370 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372}
373
Dave Barach9b8ffd92016-07-08 08:13:45 -0400374uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
375 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
377/** \brief Get length in bytes of the buffer chain
378
379 @param vm - (vlib_main_t *) vlib main data structure pointer
380 @param b - (void *) buffer pointer
381 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400382*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383always_inline uword
384vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
385{
Damjan Marion072401e2017-07-13 18:53:27 +0200386 uword len = b->current_length;
387
388 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
389 return len;
390
391 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
392 return len + b->total_length_not_including_first_buffer;
393
394 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395}
396
397/** \brief Get length in bytes of the buffer index buffer chain
398
399 @param vm - (vlib_main_t *) vlib main data structure pointer
400 @param bi - (u32) buffer index
401 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403always_inline uword
404vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
405{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 return vlib_buffer_length_in_chain (vm, b);
408}
409
410/** \brief Copy buffer contents to memory
411
412 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400413 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 @param contents - (u8 *) memory, <strong>must be large enough</strong>
415 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417always_inline uword
418vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
419{
420 uword content_len = 0;
421 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400422 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
424 while (1)
425 {
426 b = vlib_get_buffer (vm, buffer_index);
427 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500428 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400430 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 break;
432 buffer_index = b->next_buffer;
433 }
434
435 return content_len;
436}
437
Damjan Marion8f499362018-10-22 13:07:02 +0200438always_inline uword
439vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440{
Damjan Marion68b4da62018-09-30 18:26:20 +0200441 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442}
443
Damjan Marion8f499362018-10-22 13:07:02 +0200444always_inline uword
445vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
446{
447 return vlib_buffer_get_pa (vm, b) + b->current_data;
448}
449
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450/** \brief Prefetch buffer metadata by buffer index
451 The first 64 bytes of buffer contains most header information
452
453 @param vm - (vlib_main_t *) vlib main data structure pointer
454 @param bi - (u32) buffer index
455 @param type - LOAD, STORE. In most cases, STORE is the right answer
456*/
457/* Prefetch buffer header given index. */
458#define vlib_prefetch_buffer_with_index(vm,bi,type) \
459 do { \
460 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
461 vlib_prefetch_buffer_header (_b, type); \
462 } while (0)
463
Dave Barach9b8ffd92016-07-08 08:13:45 -0400464typedef enum
465{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 /* Index is unknown. */
467 VLIB_BUFFER_UNKNOWN,
468
469 /* Index is known and free/allocated. */
470 VLIB_BUFFER_KNOWN_FREE,
471 VLIB_BUFFER_KNOWN_ALLOCATED,
472} vlib_buffer_known_state_t;
473
Damjan Marionc8a26c62017-11-24 20:15:23 +0100474void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
475 uword n_buffers,
476 vlib_buffer_known_state_t
477 expected_state);
478
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100480vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Damjan Mariond50e3472019-01-20 00:03:56 +0100482 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
Damjan Marion6b0f5892017-07-27 04:01:24 -0400484 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400485 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400486 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
488}
489
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490/* Validates sanity of a single buffer.
491 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
493 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100495u8 *vlib_validate_buffers (vlib_main_t * vm,
496 u32 * buffers,
497 uword next_buffer_stride,
498 uword n_buffers,
499 vlib_buffer_known_state_t known_state,
500 uword follow_buffer_next);
501
Damjan Marion910d3692019-01-21 11:48:34 +0100502static_always_inline vlib_buffer_pool_t *
503vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400504{
Damjan Marion910d3692019-01-21 11:48:34 +0100505 vlib_buffer_main_t *bm = vm->buffer_main;
506 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400507}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Damjan Mariona2185122020-04-08 00:52:53 +0200509static_always_inline __clib_warn_unused_result uword
Damjan Marion910d3692019-01-21 11:48:34 +0100510vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
511 u32 n_buffers)
512{
513 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
514 u32 len;
515
516 ASSERT (bp->buffers);
517
518 clib_spinlock_lock (&bp->lock);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100519 len = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100520 if (PREDICT_TRUE (n_buffers < len))
521 {
522 len -= n_buffers;
523 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100524 bp->n_avail = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100525 clib_spinlock_unlock (&bp->lock);
526 return n_buffers;
527 }
528 else
529 {
530 vlib_buffer_copy_indices (buffers, bp->buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100531 bp->n_avail = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100532 clib_spinlock_unlock (&bp->lock);
533 return len;
534 }
535}
536
537
538/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
540 @param vm - (vlib_main_t *) vlib main data structure pointer
541 @param buffers - (u32 * ) buffer index array
542 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400543 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 less than the number requested or zero
545*/
Damjan Marion910d3692019-01-21 11:48:34 +0100546
Damjan Mariona2185122020-04-08 00:52:53 +0200547always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100548vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
549 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100550{
Damjan Mariond50e3472019-01-20 00:03:56 +0100551 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100552 vlib_buffer_pool_t *bp;
553 vlib_buffer_pool_thread_t *bpt;
554 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100555
Dave Barachc74b43c2020-04-09 17:24:07 -0400556 /* If buffer allocation fault injection is configured */
557 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
558 {
559 u32 vlib_buffer_alloc_may_fail (vlib_main_t *, u32);
560
561 /* See how many buffers we're willing to allocate */
562 n_buffers = vlib_buffer_alloc_may_fail (vm, n_buffers);
563 if (n_buffers == 0)
564 return (n_buffers);
565 }
566
Damjan Marion910d3692019-01-21 11:48:34 +0100567 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
568 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100569
Damjan Marion910d3692019-01-21 11:48:34 +0100570 dst = buffers;
571 n_left = n_buffers;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100572 len = bpt->n_cached;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100573
Damjan Marion910d3692019-01-21 11:48:34 +0100574 /* per-thread cache contains enough buffers */
575 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100576 {
Damjan Marion910d3692019-01-21 11:48:34 +0100577 src = bpt->cached_buffers + len - n_buffers;
578 vlib_buffer_copy_indices (dst, src, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100579 bpt->n_cached -= n_buffers;
Benoît Gannee09a2332021-03-09 15:37:49 +0100580 goto done;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100581 }
582
583 /* alloc bigger than cache - take buffers directly from main pool */
584 if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ)
585 {
586 n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers,
587 n_buffers);
Benoît Gannee09a2332021-03-09 15:37:49 +0100588 goto done;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100589 }
590
Damjan Marion910d3692019-01-21 11:48:34 +0100591 /* take everything available in the cache */
592 if (len)
593 {
594 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100595 bpt->n_cached = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100596 dst += len;
597 n_left -= len;
598 }
599
600 len = round_pow2 (n_left, 32);
Damjan Marion910d3692019-01-21 11:48:34 +0100601 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
602 len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100603 bpt->n_cached = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100604
605 if (len)
606 {
607 u32 n_copy = clib_min (len, n_left);
608 src = bpt->cached_buffers + len - n_copy;
609 vlib_buffer_copy_indices (dst, src, n_copy);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100610 bpt->n_cached -= n_copy;
Damjan Marion910d3692019-01-21 11:48:34 +0100611 n_left -= n_copy;
612 }
613
614 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100615
Benoît Gannee09a2332021-03-09 15:37:49 +0100616done:
Damjan Marionc8a26c62017-11-24 20:15:23 +0100617 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100618 if (CLIB_DEBUG > 0)
619 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
620 VLIB_BUFFER_KNOWN_FREE);
Benoît Gannee09a2332021-03-09 15:37:49 +0100621 if (PREDICT_FALSE (bm->alloc_callback_fn != 0))
622 bm->alloc_callback_fn (vm, buffer_pool_index, buffers, n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100623 return n_buffers;
624}
625
Damjan Marion910d3692019-01-21 11:48:34 +0100626/** \brief Allocate buffers from specific numa node into supplied array
627
628 @param vm - (vlib_main_t *) vlib main data structure pointer
629 @param buffers - (u32 * ) buffer index array
630 @param n_buffers - (u32) number of buffers requested
631 @param numa_node - (u32) numa node
632 @return - (u32) number of buffers actually allocated, may be
633 less than the number requested or zero
634*/
Damjan Mariona2185122020-04-08 00:52:53 +0200635always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100636vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
637 u32 numa_node)
638{
639 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
640 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
641}
642
Damjan Marionc8a26c62017-11-24 20:15:23 +0100643/** \brief Allocate buffers into supplied array
644
645 @param vm - (vlib_main_t *) vlib main data structure pointer
646 @param buffers - (u32 * ) buffer index array
647 @param n_buffers - (u32) number of buffers requested
648 @return - (u32) number of buffers actually allocated, may be
649 less than the number requested or zero
650*/
Damjan Marion910d3692019-01-21 11:48:34 +0100651
Damjan Mariona2185122020-04-08 00:52:53 +0200652always_inline __clib_warn_unused_result u32
Damjan Marionc8a26c62017-11-24 20:15:23 +0100653vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
654{
Damjan Marion910d3692019-01-21 11:48:34 +0100655 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100656}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657
Damjan Marionc58408c2018-01-18 14:54:04 +0100658/** \brief Allocate buffers into ring
659
660 @param vm - (vlib_main_t *) vlib main data structure pointer
661 @param buffers - (u32 * ) buffer index ring
662 @param start - (u32) first slot in the ring
663 @param ring_size - (u32) ring size
664 @param n_buffers - (u32) number of buffers requested
665 @return - (u32) number of buffers actually allocated, may be
666 less than the number requested or zero
667*/
Damjan Mariona2185122020-04-08 00:52:53 +0200668always_inline __clib_warn_unused_result u32
Damjan Marionc58408c2018-01-18 14:54:04 +0100669vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
670 u32 ring_size, u32 n_buffers)
671{
672 u32 n_alloc;
673
674 ASSERT (n_buffers <= ring_size);
675
676 if (PREDICT_TRUE (start + n_buffers <= ring_size))
677 return vlib_buffer_alloc (vm, ring + start, n_buffers);
678
679 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
680
681 if (PREDICT_TRUE (n_alloc == ring_size - start))
682 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
683
684 return n_alloc;
685}
686
Damjan Marion910d3692019-01-21 11:48:34 +0100687/** \brief Allocate buffers into ring from specific buffer pool
688
689 @param vm - (vlib_main_t *) vlib main data structure pointer
690 @param buffers - (u32 * ) buffer index ring
691 @param start - (u32) first slot in the ring
692 @param ring_size - (u32) ring size
693 @param n_buffers - (u32) number of buffers requested
694 @return - (u32) number of buffers actually allocated, may be
695 less than the number requested or zero
696*/
Damjan Mariona2185122020-04-08 00:52:53 +0200697always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100698vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
699 u32 ring_size, u32 n_buffers,
700 u8 buffer_pool_index)
701{
702 u32 n_alloc;
703
704 ASSERT (n_buffers <= ring_size);
705
706 if (PREDICT_TRUE (start + n_buffers <= ring_size))
707 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
708 buffer_pool_index);
709
710 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
711 buffer_pool_index);
712
713 if (PREDICT_TRUE (n_alloc == ring_size - start))
714 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
715 buffer_pool_index);
716
717 return n_alloc;
718}
719
Damjan Marionf646d742019-01-31 18:50:04 +0100720static_always_inline void
Damjan Marion910d3692019-01-21 11:48:34 +0100721vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
722 u32 * buffers, u32 n_buffers)
723{
Benoît Gannee09a2332021-03-09 15:37:49 +0100724 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100725 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100726 vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads,
727 vm->thread_index);
728 u32 n_cached, n_empty;
Damjan Marion910d3692019-01-21 11:48:34 +0100729
Damjan Marionf646d742019-01-31 18:50:04 +0100730 if (CLIB_DEBUG > 0)
731 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
732 VLIB_BUFFER_KNOWN_ALLOCATED);
Benoît Gannee09a2332021-03-09 15:37:49 +0100733 if (PREDICT_FALSE (bm->free_callback_fn != 0))
734 bm->free_callback_fn (vm, buffer_pool_index, buffers, n_buffers);
Damjan Marionf646d742019-01-31 18:50:04 +0100735
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100736 n_cached = bpt->n_cached;
737 n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached;
738 if (n_buffers <= n_empty)
Damjan Marion910d3692019-01-21 11:48:34 +0100739 {
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100740 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
741 buffers, n_buffers);
742 bpt->n_cached = n_cached + n_buffers;
743 return;
Damjan Marion910d3692019-01-21 11:48:34 +0100744 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100745
746 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
747 buffers + n_buffers - n_empty, n_empty);
748 bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ;
749
750 clib_spinlock_lock (&bp->lock);
751 vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers,
752 n_buffers - n_empty);
753 bp->n_avail += n_buffers - n_empty;
754 clib_spinlock_unlock (&bp->lock);
Damjan Marion910d3692019-01-21 11:48:34 +0100755}
756
757static_always_inline void
758vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
759 int maybe_next)
760{
761 const int queue_size = 128;
762 vlib_buffer_pool_t *bp = 0;
763 u8 buffer_pool_index = ~0;
764 u32 n_queue = 0, queue[queue_size + 4];
765 vlib_buffer_t bt = { };
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800766#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100767 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
Radu Nicolaud82349e2021-03-16 12:45:01 +0000768 vlib_buffer_t bpi_vec = {};
Damjan Marion910d3692019-01-21 11:48:34 +0100769 vlib_buffer_t flags_refs_mask = {
770 .flags = VLIB_BUFFER_NEXT_PRESENT,
Damjan Marion0e209242019-03-13 12:04:10 +0100771 .ref_count = ~1
Damjan Marion910d3692019-01-21 11:48:34 +0100772 };
773#endif
774
Radu Nicolaud82349e2021-03-16 12:45:01 +0000775 if (PREDICT_FALSE (n_buffers == 0))
776 return;
777
778 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[0]);
779 buffer_pool_index = b->buffer_pool_index;
780 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
781 vlib_buffer_copy_template (&bt, &bp->buffer_template);
782#if defined(CLIB_HAVE_VEC128)
783 bpi_vec.buffer_pool_index = buffer_pool_index;
784#endif
785
Damjan Marion910d3692019-01-21 11:48:34 +0100786 while (n_buffers)
787 {
788 vlib_buffer_t *b[8];
789 u32 bi, sum = 0, flags, next;
790
Radu Nicolaud82349e2021-03-16 12:45:01 +0000791 if (n_buffers < 4)
Damjan Marion910d3692019-01-21 11:48:34 +0100792 goto one_by_one;
793
794 vlib_get_buffers (vm, buffers, b, 4);
Damjan Marion910d3692019-01-21 11:48:34 +0100795
Radu Nicolaud82349e2021-03-16 12:45:01 +0000796 if (n_buffers >= 12)
797 {
798 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
799 vlib_prefetch_buffer_header (b[4], LOAD);
800 vlib_prefetch_buffer_header (b[5], LOAD);
801 vlib_prefetch_buffer_header (b[6], LOAD);
802 vlib_prefetch_buffer_header (b[7], LOAD);
803 }
Damjan Marion910d3692019-01-21 11:48:34 +0100804
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800805#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100806 u8x16 p0, p1, p2, p3, r;
807 p0 = u8x16_load_unaligned (b[0]);
808 p1 = u8x16_load_unaligned (b[1]);
809 p2 = u8x16_load_unaligned (b[2]);
810 p3 = u8x16_load_unaligned (b[3]);
811
812 r = p0 ^ bpi_vec.as_u8x16[0];
813 r |= p1 ^ bpi_vec.as_u8x16[0];
814 r |= p2 ^ bpi_vec.as_u8x16[0];
815 r |= p3 ^ bpi_vec.as_u8x16[0];
816 r &= bpi_mask.as_u8x16[0];
817 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
818
819 sum = !u8x16_is_all_zero (r);
820#else
821 sum |= b[0]->flags;
822 sum |= b[1]->flags;
823 sum |= b[2]->flags;
824 sum |= b[3]->flags;
825 sum &= VLIB_BUFFER_NEXT_PRESENT;
826 sum += b[0]->ref_count - 1;
827 sum += b[1]->ref_count - 1;
828 sum += b[2]->ref_count - 1;
829 sum += b[3]->ref_count - 1;
830 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
831 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
832 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
833 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
834#endif
835
836 if (sum)
837 goto one_by_one;
838
839 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
840 vlib_buffer_copy_template (b[0], &bt);
841 vlib_buffer_copy_template (b[1], &bt);
842 vlib_buffer_copy_template (b[2], &bt);
843 vlib_buffer_copy_template (b[3], &bt);
844 n_queue += 4;
845
Damjan Marion24dcbe42019-01-31 12:29:39 +0100846 vlib_buffer_validate (vm, b[0]);
847 vlib_buffer_validate (vm, b[1]);
848 vlib_buffer_validate (vm, b[2]);
849 vlib_buffer_validate (vm, b[3]);
850
Damjan Marion910d3692019-01-21 11:48:34 +0100851 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
852 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
853 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
854 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
855
856 if (n_queue >= queue_size)
857 {
858 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
859 n_queue = 0;
860 }
861 buffers += 4;
862 n_buffers -= 4;
863 continue;
864
865 one_by_one:
866 bi = buffers[0];
867
868 next_in_chain:
869 b[0] = vlib_get_buffer (vm, bi);
870 flags = b[0]->flags;
871 next = b[0]->next_buffer;
872
873 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
874 {
Damjan Marion910d3692019-01-21 11:48:34 +0100875
876 if (n_queue)
877 {
878 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
879 n_queue = 0;
880 }
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500881
882 buffer_pool_index = b[0]->buffer_pool_index;
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800883#if defined(CLIB_HAVE_VEC128)
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500884 bpi_vec.buffer_pool_index = buffer_pool_index;
885#endif
886 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
887 vlib_buffer_copy_template (&bt, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100888 }
889
Damjan Marion24dcbe42019-01-31 12:29:39 +0100890 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100891
892 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
893
894 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
895 {
Damjan Marion910d3692019-01-21 11:48:34 +0100896 vlib_buffer_copy_template (b[0], &bt);
897 queue[n_queue++] = bi;
898 }
899
900 if (n_queue == queue_size)
901 {
902 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
903 n_queue = 0;
904 }
905
Damjan Marion5fed42a2019-06-11 20:12:23 +0200906 if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT))
Damjan Marion910d3692019-01-21 11:48:34 +0100907 {
908 bi = next;
909 goto next_in_chain;
910 }
911
912 buffers++;
913 n_buffers--;
914 }
915
916 if (n_queue)
917 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
918}
919
920
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921/** \brief Free buffers
922 Frees the entire buffer chain for each buffer
923
924 @param vm - (vlib_main_t *) vlib main data structure pointer
925 @param buffers - (u32 * ) buffer index array
926 @param n_buffers - (u32) number of buffers to free
927
928*/
Damjan Marion878c6092017-01-04 13:19:27 +0100929always_inline void
930vlib_buffer_free (vlib_main_t * vm,
931 /* pointer to first buffer */
932 u32 * buffers,
933 /* number of buffers to free */
934 u32 n_buffers)
935{
Damjan Marion910d3692019-01-21 11:48:34 +0100936 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +0100937}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938
939/** \brief Free buffers, does not free the buffer chain for each buffer
940
941 @param vm - (vlib_main_t *) vlib main data structure pointer
942 @param buffers - (u32 * ) buffer index array
943 @param n_buffers - (u32) number of buffers to free
944
945*/
Damjan Marion878c6092017-01-04 13:19:27 +0100946always_inline void
947vlib_buffer_free_no_next (vlib_main_t * vm,
948 /* pointer to first buffer */
949 u32 * buffers,
950 /* number of buffers to free */
951 u32 n_buffers)
952{
Damjan Marion910d3692019-01-21 11:48:34 +0100953 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +0100954}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
956/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958
959 @param vm - (vlib_main_t *) vlib main data structure pointer
960 @param buffer_index - (u32) buffer index to free
961*/
962always_inline void
963vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
964{
Damjan Marion910d3692019-01-21 11:48:34 +0100965 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966}
967
Damjan Mariona3731492018-02-25 22:50:39 +0100968/** \brief Free buffers from ring
969
970 @param vm - (vlib_main_t *) vlib main data structure pointer
971 @param buffers - (u32 * ) buffer index ring
972 @param start - (u32) first slot in the ring
973 @param ring_size - (u32) ring size
974 @param n_buffers - (u32) number of buffers
975*/
976always_inline void
977vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
978 u32 ring_size, u32 n_buffers)
979{
980 ASSERT (n_buffers <= ring_size);
981
982 if (PREDICT_TRUE (start + n_buffers <= ring_size))
983 {
984 vlib_buffer_free (vm, ring + start, n_buffers);
985 }
986 else
987 {
988 vlib_buffer_free (vm, ring + start, ring_size - start);
989 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
990 }
991}
992
Damjan Marioncef1db92018-03-28 18:27:38 +0200993/** \brief Free buffers from ring without freeing tail buffers
994
995 @param vm - (vlib_main_t *) vlib main data structure pointer
996 @param buffers - (u32 * ) buffer index ring
997 @param start - (u32) first slot in the ring
998 @param ring_size - (u32) ring size
999 @param n_buffers - (u32) number of buffers
1000*/
1001always_inline void
1002vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
1003 u32 ring_size, u32 n_buffers)
1004{
1005 ASSERT (n_buffers <= ring_size);
1006
1007 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1008 {
Damjan Marion4a973932018-06-09 19:29:16 +02001009 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +02001010 }
1011 else
1012 {
1013 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
1014 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
1015 }
1016}
Damjan Mariona3731492018-02-25 22:50:39 +01001017
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001019int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
1020 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
John Lo8ed2d522019-08-07 19:30:29 -04001022/* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */
1023#define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \
1024 (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \
1025 VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL)
1026
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001027/* duplicate all buffers in chain */
1028always_inline vlib_buffer_t *
1029vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
1030{
1031 vlib_buffer_t *s, *d, *fd;
1032 uword n_alloc, n_buffers = 1;
John Lo8ed2d522019-08-07 19:30:29 -04001033 u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001034 int i;
1035
1036 s = b;
1037 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1038 {
1039 n_buffers++;
1040 s = vlib_get_buffer (vm, s->next_buffer);
1041 }
Neale Ranns9d676af2017-03-15 01:28:31 -07001042 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001043
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001044 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -05001045
1046 /* No guarantee that we'll get all the buffers we asked for */
1047 if (PREDICT_FALSE (n_alloc < n_buffers))
1048 {
1049 if (n_alloc > 0)
1050 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -05001051 return 0;
1052 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001053
1054 /* 1st segment */
1055 s = b;
1056 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001057 d->current_data = s->current_data;
1058 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +01001059 d->flags = s->flags & flag_mask;
John Lo66317802019-08-13 18:18:21 -04001060 d->trace_handle = s->trace_handle;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001061 d->total_length_not_including_first_buffer =
1062 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -05001063 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1064 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1065 clib_memcpy_fast (vlib_buffer_get_current (d),
1066 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001067
1068 /* next segments */
1069 for (i = 1; i < n_buffers; i++)
1070 {
1071 /* previous */
1072 d->next_buffer = new_buffers[i];
1073 /* current */
1074 s = vlib_get_buffer (vm, s->next_buffer);
1075 d = vlib_get_buffer (vm, new_buffers[i]);
1076 d->current_data = s->current_data;
1077 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -05001078 clib_memcpy_fast (vlib_buffer_get_current (d),
1079 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +01001080 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001081 }
1082
1083 return fd;
1084}
1085
Ole Troanda7f7b62019-03-11 13:15:54 +01001086/* duplicate first buffer in chain */
1087always_inline vlib_buffer_t *
1088vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di)
1089{
1090 vlib_buffer_t *d;
1091
1092 if ((vlib_buffer_alloc (vm, di, 1)) != 1)
1093 return 0;
1094
1095 d = vlib_get_buffer (vm, *di);
1096 /* 1st segment */
1097 d->current_data = b->current_data;
1098 d->current_length = b->current_length;
1099 clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque));
1100 clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2));
1101 clib_memcpy_fast (vlib_buffer_get_current (d),
1102 vlib_buffer_get_current (b), b->current_length);
1103
1104 return d;
1105}
1106
John Lof545caa2019-04-01 11:30:07 -04001107/* \brief Move packet from current position to offset position in buffer.
1108 Only work for small packet using one buffer with room to fit the move
1109 @param vm - (vlib_main_t *) vlib main data structure pointer
1110 @param b - (vlib_buffer_t *) pointer to buffer
1111 @param offset - (i16) position to move the packet in buffer
1112 */
1113always_inline void
1114vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset)
1115{
1116 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
1117 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1118 ASSERT (offset + b->current_length <
1119 vlib_buffer_get_default_data_size (vm));
1120
1121 u8 *source = vlib_buffer_get_current (b);
1122 b->current_data = offset;
1123 u8 *destination = vlib_buffer_get_current (b);
1124 u16 length = b->current_length;
1125
1126 if (source + length <= destination) /* no overlap */
1127 clib_memcpy_fast (destination, source, length);
1128 else
1129 memmove (destination, source, length);
1130}
1131
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001132/** \brief Create a maximum of 256 clones of buffer and store them
1133 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +01001134
1135 @param vm - (vlib_main_t *) vlib main data structure pointer
1136 @param src_buffer - (u32) source buffer index
1137 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001138 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +01001139 @param head_end_offset - (u16) offset relative to current position
1140 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001141 @param offset - (i16) copy packet head at current position if 0,
1142 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001143 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +01001144 less than the number requested or zero
1145*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001146always_inline u16
1147vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
John Lof545caa2019-04-01 11:30:07 -04001148 u16 n_buffers, u16 head_end_offset, i16 offset)
Damjan Marionc47ed032017-01-25 14:18:03 +01001149{
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001150 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +01001151 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1152
Damjan Marion910d3692019-01-21 11:48:34 +01001153 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001154 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001155 ASSERT (n_buffers <= 256);
John Lof545caa2019-04-01 11:30:07 -04001156 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1157 ASSERT ((offset + head_end_offset) <
1158 vlib_buffer_get_default_data_size (vm));
Damjan Marionc47ed032017-01-25 14:18:03 +01001159
1160 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1161 {
1162 buffers[0] = src_buffer;
John Lof545caa2019-04-01 11:30:07 -04001163 if (offset)
1164 vlib_buffer_move (vm, s, offset);
1165
Damjan Marionc47ed032017-01-25 14:18:03 +01001166 for (i = 1; i < n_buffers; i++)
1167 {
1168 vlib_buffer_t *d;
1169 d = vlib_buffer_copy (vm, s);
1170 if (d == 0)
1171 return i;
1172 buffers[i] = vlib_get_buffer_index (vm, d);
1173
1174 }
1175 return n_buffers;
1176 }
1177
John Lof545caa2019-04-01 11:30:07 -04001178 if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0)))
Damjan Marionc47ed032017-01-25 14:18:03 +01001179 {
1180 buffers[0] = src_buffer;
1181 return 1;
1182 }
1183
Damjan Marion910d3692019-01-21 11:48:34 +01001184 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1185 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001186
Damjan Marionc47ed032017-01-25 14:18:03 +01001187 for (i = 0; i < n_buffers; i++)
1188 {
1189 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
John Lof545caa2019-04-01 11:30:07 -04001190 if (offset)
1191 d->current_data = offset;
1192 else
1193 d->current_data = s->current_data;
1194
Damjan Marionc47ed032017-01-25 14:18:03 +01001195 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001196 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1197
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001198 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001199 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001200 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1201 {
1202 d->total_length_not_including_first_buffer +=
1203 s->total_length_not_including_first_buffer;
1204 }
John Lo8ed2d522019-08-07 19:30:29 -04001205 d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) |
1206 VLIB_BUFFER_NEXT_PRESENT;
John Lo66317802019-08-13 18:18:21 -04001207 d->trace_handle = s->trace_handle;
Dave Barach178cf492018-11-13 16:34:13 -05001208 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1209 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1210 clib_memcpy_fast (vlib_buffer_get_current (d),
1211 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001212 d->next_buffer = src_buffer;
1213 }
1214 vlib_buffer_advance (s, head_end_offset);
Dave Barach95e19252020-04-07 10:52:43 -04001215 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001216 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1217 {
1218 s = vlib_get_buffer (vm, s->next_buffer);
Dave Barach95e19252020-04-07 10:52:43 -04001219 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001220 }
1221
1222 return n_buffers;
1223}
1224
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001225/** \brief Create multiple clones of buffer and store them
1226 in the supplied array
1227
1228 @param vm - (vlib_main_t *) vlib main data structure pointer
1229 @param src_buffer - (u32) source buffer index
1230 @param buffers - (u32 * ) buffer index array
1231 @param n_buffers - (u16) number of buffer clones requested (<=256)
1232 @param head_end_offset - (u16) offset relative to current position
1233 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001234 @param offset - (i16) copy packet head at current position if 0,
1235 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001236 @return - (u16) number of buffers actually cloned, may be
1237 less than the number requested or zero
1238*/
1239always_inline u16
John Lof545caa2019-04-01 11:30:07 -04001240vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1241 u16 n_buffers, u16 head_end_offset, i16 offset)
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001242{
1243 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1244 u16 n_cloned = 0;
1245
1246 while (n_buffers > 256)
1247 {
1248 vlib_buffer_t *copy;
1249 copy = vlib_buffer_copy (vm, s);
1250 n_cloned += vlib_buffer_clone_256 (vm,
1251 vlib_get_buffer_index (vm, copy),
1252 (buffers + n_cloned),
John Lof545caa2019-04-01 11:30:07 -04001253 256, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001254 n_buffers -= 256;
1255 }
1256 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1257 buffers + n_cloned,
John Lof545caa2019-04-01 11:30:07 -04001258 n_buffers, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001259
1260 return n_cloned;
1261}
1262
John Lof545caa2019-04-01 11:30:07 -04001263/** \brief Create multiple clones of buffer and store them
1264 in the supplied array
1265
1266 @param vm - (vlib_main_t *) vlib main data structure pointer
1267 @param src_buffer - (u32) source buffer index
1268 @param buffers - (u32 * ) buffer index array
1269 @param n_buffers - (u16) number of buffer clones requested (<=256)
1270 @param head_end_offset - (u16) offset relative to current position
1271 where packet head ends
1272 @return - (u16) number of buffers actually cloned, may be
1273 less than the number requested or zero
1274*/
1275always_inline u16
1276vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1277 u16 n_buffers, u16 head_end_offset)
1278{
1279 return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers,
1280 head_end_offset, 0);
1281}
1282
Damjan Marionc47ed032017-01-25 14:18:03 +01001283/** \brief Attach cloned tail to the buffer
1284
1285 @param vm - (vlib_main_t *) vlib main data structure pointer
1286 @param head - (vlib_buffer_t *) head buffer
1287 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1288*/
1289
1290always_inline void
1291vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1292 vlib_buffer_t * tail)
1293{
1294 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001295 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001296
1297 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1298 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1299 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1300 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1301 head->next_buffer = vlib_get_buffer_index (vm, tail);
1302 head->total_length_not_including_first_buffer = tail->current_length +
1303 tail->total_length_not_including_first_buffer;
1304
1305next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001306 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001307
1308 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1309 {
1310 tail = vlib_get_buffer (vm, tail->next_buffer);
1311 goto next_segment;
1312 }
1313}
1314
Pierre Pfister328e99b2016-02-12 13:18:42 +00001315/* Initializes the buffer as an empty packet with no chained buffers. */
1316always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001317vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001318{
1319 first->total_length_not_including_first_buffer = 0;
1320 first->current_length = 0;
1321 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1322 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001323}
1324
1325/* The provided next_bi buffer index is appended to the end of the packet. */
1326always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001327vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001328{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001329 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001330 last->next_buffer = next_bi;
1331 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1332 next_buffer->current_length = 0;
1333 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001334 return next_buffer;
1335}
1336
1337/* Increases or decreases the packet length.
1338 * It does not allocate or deallocate new buffers.
1339 * Therefore, the added length must be compatible
1340 * with the last buffer. */
1341always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001342vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1343 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001344{
1345 last->current_length += len;
1346 if (first != last)
1347 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001348}
1349
1350/* Copy data to the end of the packet and increases its length.
1351 * It does not allocate new buffers.
1352 * Returns the number of copied bytes. */
1353always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001354vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001355 vlib_buffer_t * first,
1356 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001357{
Damjan Marion8934a042019-02-09 23:29:26 +01001358 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001359 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1360 u16 len = clib_min (data_len,
1361 n_buffer_bytes - last->current_length -
1362 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001363 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1364 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001365 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001366 return len;
1367}
1368
1369/* Copy data to the end of the packet and increases its length.
1370 * Allocates additional buffers from the free list if necessary.
1371 * Returns the number of copied bytes.
1372 * 'last' value is modified whenever new buffers are allocated and
1373 * chained and points to the last buffer in the chain. */
1374u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001375vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001376 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001377 vlib_buffer_t ** last, void *data,
1378 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001379void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001380
Dave Barach9b8ffd92016-07-08 08:13:45 -04001381format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
Benoît Ganne43543172019-10-21 15:13:54 +02001382 format_vlib_buffer_contents, format_vlib_buffer_no_chain;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001383
Dave Barach9b8ffd92016-07-08 08:13:45 -04001384typedef struct
1385{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001386 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001387 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388
Damjan Mariond1274cb2018-03-13 21:32:17 +01001389 /* Number of buffers to allocate in each call to allocator. */
1390 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391
Damjan Marion671e60e2018-12-30 18:09:59 +01001392 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393} vlib_packet_template_t;
1394
Ed Warnickecb9cada2015-12-08 15:45:58 -07001395void vlib_packet_template_init (vlib_main_t * vm,
1396 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001397 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001398 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001399 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001400 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401
Dave Barach9b8ffd92016-07-08 08:13:45 -04001402void *vlib_packet_template_get_packet (vlib_main_t * vm,
1403 vlib_packet_template_t * t,
1404 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405
1406always_inline void
1407vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1408{
1409 vec_free (t->packet_data);
1410}
1411
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001412always_inline u32
1413vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +02001414{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001415 return b->data + vlib_buffer_get_default_data_size (vm) -
1416 ((u8 *) vlib_buffer_get_current (b) + b->current_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001417}
1418
Benoît Ganne1a195522021-05-27 17:43:34 +02001419#define VLIB_BUFFER_LINEARIZE_MAX 64
1420
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001421always_inline u32
1422vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b)
Eyal Barid3d42412018-11-05 13:29:25 +02001423{
Benoît Ganne1a195522021-05-27 17:43:34 +02001424 vlib_buffer_t *dst_b;
1425 u32 n_buffers = 1, to_free = 0;
1426 u16 rem_len, dst_len, data_size, src_len = 0;
1427 u8 *dst, *src = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001428
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001429 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
1430 return 1;
Eyal Barid3d42412018-11-05 13:29:25 +02001431
Benoît Ganne1a195522021-05-27 17:43:34 +02001432 ASSERT (1 == b->ref_count);
1433 if (PREDICT_FALSE (1 != b->ref_count))
1434 return 0;
1435
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001436 data_size = vlib_buffer_get_default_data_size (vm);
Benoît Ganne1a195522021-05-27 17:43:34 +02001437 rem_len = vlib_buffer_length_in_chain (vm, b) - b->current_length;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001438
Benoît Ganne1a195522021-05-27 17:43:34 +02001439 dst_b = b;
1440 dst = vlib_buffer_get_tail (dst_b);
1441 dst_len = vlib_buffer_space_left_at_end (vm, dst_b);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001442
Benoît Ganne1a195522021-05-27 17:43:34 +02001443 b->total_length_not_including_first_buffer -= dst_len;
1444
1445 while (rem_len > 0)
Eyal Barid3d42412018-11-05 13:29:25 +02001446 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001447 u16 copy_len;
Eyal Barid3d42412018-11-05 13:29:25 +02001448
Benoît Ganne1a195522021-05-27 17:43:34 +02001449 while (0 == src_len)
Eyal Barid3d42412018-11-05 13:29:25 +02001450 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001451 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
1452 if (PREDICT_FALSE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
1453 break; /* malformed chained buffer */
1454
1455 b = vlib_get_buffer (vm, b->next_buffer);
1456 src = vlib_buffer_get_current (b);
1457 src_len = b->current_length;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001458 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001459
Benoît Ganne1a195522021-05-27 17:43:34 +02001460 if (0 == dst_len)
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001461 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001462 ASSERT (dst_b->flags & VLIB_BUFFER_NEXT_PRESENT);
1463 if (PREDICT_FALSE (!(dst_b->flags & VLIB_BUFFER_NEXT_PRESENT)))
1464 break; /* malformed chained buffer */
1465
1466 vlib_buffer_t *next_dst_b = vlib_get_buffer (vm, dst_b->next_buffer);
1467
1468 if (PREDICT_TRUE (1 == next_dst_b->ref_count))
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001469 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001470 /* normal case: buffer is not cloned, just use it */
1471 dst_b = next_dst_b;
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001472 }
1473 else
1474 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001475 /* cloned buffer, build a new dest chain from there */
1476 vlib_buffer_t *bufs[VLIB_BUFFER_LINEARIZE_MAX];
1477 u32 bis[VLIB_BUFFER_LINEARIZE_MAX + 1];
1478 const int n = (rem_len + data_size - 1) / data_size;
1479 int n_alloc;
1480 int i;
1481
1482 ASSERT (n <= VLIB_BUFFER_LINEARIZE_MAX);
1483 if (PREDICT_FALSE (n > VLIB_BUFFER_LINEARIZE_MAX))
1484 return 0;
1485
1486 n_alloc = vlib_buffer_alloc (vm, bis, n);
1487 if (PREDICT_FALSE (n_alloc != n))
1488 {
1489 vlib_buffer_free (vm, bis, n_alloc);
1490 return 0;
1491 }
1492
1493 vlib_get_buffers (vm, bis, bufs, n);
1494
1495 for (i = 0; i < n - 1; i++)
1496 {
1497 bufs[i]->flags |= VLIB_BUFFER_NEXT_PRESENT;
1498 bufs[i]->next_buffer = bis[i + 1];
1499 }
1500
1501 to_free = dst_b->next_buffer;
1502 dst_b->next_buffer = bis[0];
1503 dst_b = bufs[0];
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001504 }
Benoît Ganne1a195522021-05-27 17:43:34 +02001505
1506 n_buffers++;
1507
1508 dst_b->current_data = clib_min (0, dst_b->current_data);
1509 dst_b->current_length = 0;
1510
1511 dst = dst_b->data + dst_b->current_data;
1512 dst_len = data_size - dst_b->current_data;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001513 }
1514
Benoît Ganne1a195522021-05-27 17:43:34 +02001515 copy_len = clib_min (src_len, dst_len);
1516
1517 if (PREDICT_TRUE (src == dst))
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001518 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001519 /* nothing to do */
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001520 }
Benoît Ganne1a195522021-05-27 17:43:34 +02001521 else if (src + copy_len > dst && dst + copy_len > src)
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001522 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001523 /* src and dst overlap */
1524 ASSERT (b == dst_b);
1525 memmove (dst, src, copy_len);
1526 }
1527 else
1528 {
1529 clib_memcpy_fast (dst, src, copy_len);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001530 }
1531
Benoît Ganne1a195522021-05-27 17:43:34 +02001532 dst_b->current_length += copy_len;
1533
1534 dst += copy_len;
1535 src += copy_len;
1536 dst_len -= copy_len;
1537 src_len -= copy_len;
1538 rem_len -= copy_len;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001539 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001540
Benoît Ganne1a195522021-05-27 17:43:34 +02001541 /* in case of a malformed chain buffer, we'll exit early from the loop. */
1542 ASSERT (0 == rem_len);
1543 b->total_length_not_including_first_buffer -= rem_len;
1544
1545 if (to_free)
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001546 vlib_buffer_free_one (vm, to_free);
Benoît Ganne1a195522021-05-27 17:43:34 +02001547
1548 if (dst_b->flags & VLIB_BUFFER_NEXT_PRESENT)
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001549 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001550 /* the resulting chain is smaller than the original, cut it there */
1551 dst_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1552 vlib_buffer_free_one (vm, dst_b->next_buffer);
1553 if (1 == n_buffers)
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001554 {
Benoît Ganne1a195522021-05-27 17:43:34 +02001555 /* no longer a chained buffer */
1556 dst_b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1557 dst_b->total_length_not_including_first_buffer = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001558 }
1559 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001560
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001561 return n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001562}
1563
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001565
1566/*
1567 * fd.io coding-style-patch-verification: ON
1568 *
1569 * Local Variables:
1570 * eval: (c-set-style "gnu")
1571 * End:
1572 */