blob: 8b8a39117763ab59e4bb1b5679d0ccb640adae48 [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>
Klement Sekeraf883f6a2019-02-13 11:01:32 +010045#include <vlib/buffer.h>
46#include <vlib/physmem_funcs.h>
47#include <vlib/main.h>
48#include <vlib/node.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
50/** \file
51 vlib buffer access methods.
52*/
53
Damjan Marion1c229712021-04-21 12:55:15 +020054typedef void (vlib_buffer_enqueue_to_next_fn_t) (vlib_main_t *vm,
55 vlib_node_runtime_t *node,
56 u32 *buffers, u16 *nexts,
57 uword count);
58typedef void (vlib_buffer_enqueue_to_single_next_fn_t) (
59 vlib_main_t *vm, vlib_node_runtime_t *node, u32 *ers, u16 next_index,
60 u32 count);
61
62typedef u32 (vlib_buffer_enqueue_to_thread_fn_t) (
Damjan Marion9e7a0b42021-05-14 14:50:01 +020063 vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
64 u32 *buffer_indices, u16 *thread_indices, u32 n_packets,
65 int drop_on_congestion);
Damjan Marioneee099e2021-05-01 14:56:13 +020066
67typedef u32 (vlib_frame_queue_dequeue_fn_t) (vlib_main_t *vm,
68 vlib_frame_queue_main_t *fqm);
69
Damjan Marion1c229712021-04-21 12:55:15 +020070typedef struct
71{
72 vlib_buffer_enqueue_to_next_fn_t *buffer_enqueue_to_next_fn;
73 vlib_buffer_enqueue_to_single_next_fn_t *buffer_enqueue_to_single_next_fn;
74 vlib_buffer_enqueue_to_thread_fn_t *buffer_enqueue_to_thread_fn;
Damjan Marioneee099e2021-05-01 14:56:13 +020075 vlib_frame_queue_dequeue_fn_t *frame_queue_dequeue_fn;
Damjan Marion1c229712021-04-21 12:55:15 +020076} vlib_buffer_func_main_t;
77
78extern vlib_buffer_func_main_t vlib_buffer_func_main;
79
Damjan Marion24dcbe42019-01-31 12:29:39 +010080always_inline void
81vlib_buffer_validate (vlib_main_t * vm, vlib_buffer_t * b)
82{
83 vlib_buffer_main_t *bm = vm->buffer_main;
84 vlib_buffer_pool_t *bp;
85
86 /* reference count in allocated buffer always must be 1 or higher */
87 ASSERT (b->ref_count > 0);
88
Damjan Marion24dcbe42019-01-31 12:29:39 +010089 /* verify that buffer pool index is valid */
90 bp = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index);
91 ASSERT (pointer_to_uword (b) >= bp->start);
92 ASSERT (pointer_to_uword (b) < bp->start + bp->size -
Damjan Marion5de3fec2019-02-06 14:22:32 +010093 (bp->data_size + sizeof (vlib_buffer_t)));
Damjan Marion24dcbe42019-01-31 12:29:39 +010094}
95
96always_inline void *
97vlib_buffer_ptr_from_index (uword buffer_mem_start, u32 buffer_index,
98 uword offset)
99{
100 offset += ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES;
101 return uword_to_pointer (buffer_mem_start + offset, vlib_buffer_t *);
102}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
104/** \brief Translate buffer index into buffer pointer
105
106 @param vm - (vlib_main_t *) vlib main data structure pointer
107 @param buffer_index - (u32) buffer index
108 @return - (vlib_buffer_t *) buffer pointer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400109*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110always_inline vlib_buffer_t *
111vlib_get_buffer (vlib_main_t * vm, u32 buffer_index)
112{
Damjan Mariond50e3472019-01-20 00:03:56 +0100113 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion24dcbe42019-01-31 12:29:39 +0100114 vlib_buffer_t *b;
Damjan Marion04a7f052017-07-10 15:06:17 +0200115
Damjan Marion24dcbe42019-01-31 12:29:39 +0100116 b = vlib_buffer_ptr_from_index (bm->buffer_mem_start, buffer_index, 0);
117 vlib_buffer_validate (vm, b);
118 return b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119}
120
Damjan Marion5de3fec2019-02-06 14:22:32 +0100121static_always_inline u32
Damjan Marion8934a042019-02-09 23:29:26 +0100122vlib_buffer_get_default_data_size (vlib_main_t * vm)
Damjan Marion5de3fec2019-02-06 14:22:32 +0100123{
124 return vm->buffer_main->default_data_size;
125}
126
Damjan Marione58041f2019-01-18 19:56:09 +0100127static_always_inline void
Damjan Marion64d557c2019-01-18 20:03:41 +0100128vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices)
129{
Damjan Marion856d0622021-04-21 21:11:35 +0200130 clib_memcpy_u32 (dst, src, n_indices);
Damjan Marion64d557c2019-01-18 20:03:41 +0100131}
132
Damjan Marionb9250a92020-03-02 19:02:15 +0100133always_inline void
134vlib_buffer_copy_indices_from_ring (u32 * dst, u32 * ring, u32 start,
135 u32 ring_size, u32 n_buffers)
136{
137 ASSERT (n_buffers <= ring_size);
138
139 if (PREDICT_TRUE (start + n_buffers <= ring_size))
140 {
141 vlib_buffer_copy_indices (dst, ring + start, n_buffers);
142 }
143 else
144 {
145 u32 n = ring_size - start;
146 vlib_buffer_copy_indices (dst, ring + start, n);
147 vlib_buffer_copy_indices (dst + n, ring, n_buffers - n);
148 }
149}
150
Benoît Ganne72f49212020-03-19 11:41:07 +0100151always_inline void
152vlib_buffer_copy_indices_to_ring (u32 * ring, u32 * src, u32 start,
153 u32 ring_size, u32 n_buffers)
154{
155 ASSERT (n_buffers <= ring_size);
156
157 if (PREDICT_TRUE (start + n_buffers <= ring_size))
158 {
159 vlib_buffer_copy_indices (ring + start, src, n_buffers);
160 }
161 else
162 {
163 u32 n = ring_size - start;
164 vlib_buffer_copy_indices (ring + start, src, n);
165 vlib_buffer_copy_indices (ring, src + n, n_buffers - n);
166 }
167}
168
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100169STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +0100170static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +0100171vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
172{
Damjan Marion22f23ae2019-01-24 15:36:57 +0100173#if defined CLIB_HAVE_VEC512
174 b->as_u8x64[0] = bt->as_u8x64[0];
175#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100176 b->as_u8x32[0] = bt->as_u8x32[0];
177 b->as_u8x32[1] = bt->as_u8x32[1];
178#elif defined (CLIB_HAVE_VEC128)
179 b->as_u8x16[0] = bt->as_u8x16[0];
180 b->as_u8x16[1] = bt->as_u8x16[1];
181 b->as_u8x16[2] = bt->as_u8x16[2];
182 b->as_u8x16[3] = bt->as_u8x16[3];
183#else
184 clib_memcpy_fast (b, bt, 64);
185#endif
Damjan Marione58041f2019-01-18 19:56:09 +0100186}
187
Damjan Marion910d3692019-01-21 11:48:34 +0100188always_inline u8
189vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
190{
Damjan Marionb592d1b2019-02-28 23:16:11 +0100191 ASSERT (numa_node < VLIB_BUFFER_MAX_NUMA_NODES);
192 return vm->buffer_main->default_buffer_pool_index_for_numa[numa_node];
Damjan Marion910d3692019-01-21 11:48:34 +0100193}
194
Damjan Marionafe56de2018-05-17 12:44:00 +0200195/** \brief Translate array of buffer indices into buffer pointers with offset
196
197 @param vm - (vlib_main_t *) vlib main data structure pointer
198 @param bi - (u32 *) array of buffer indices
199 @param b - (void **) array to store buffer pointers
200 @param count - (uword) number of elements
201 @param offset - (i32) offset applied to each pointer
202*/
203static_always_inline void
204vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count,
205 i32 offset)
206{
Damjan Mariond50e3472019-01-20 00:03:56 +0100207 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marion54208852021-04-21 15:25:47 +0200208#ifdef CLIB_HAVE_VEC512
209 u64x8 of8 = u64x8_splat (buffer_mem_start + offset);
210 u64x4 off = u64x8_extract_lo (of8);
Damjan Marionafe56de2018-05-17 12:44:00 +0200211 /* if count is not const, compiler will not unroll while loop
212 se we maintain two-in-parallel variant */
Damjan Marion54208852021-04-21 15:25:47 +0200213 while (count >= 32)
214 {
215 u64x8 b0 = u64x8_from_u32x8 (u32x8_load_unaligned (bi));
216 u64x8 b1 = u64x8_from_u32x8 (u32x8_load_unaligned (bi + 8));
217 u64x8 b2 = u64x8_from_u32x8 (u32x8_load_unaligned (bi + 16));
218 u64x8 b3 = u64x8_from_u32x8 (u32x8_load_unaligned (bi + 24));
219 /* shift and add to get vlib_buffer_t pointer */
220 u64x8_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b);
221 u64x8_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b + 8);
222 u64x8_store_unaligned ((b2 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b + 16);
223 u64x8_store_unaligned ((b3 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b + 24);
224 b += 32;
225 bi += 32;
226 count -= 32;
227 }
Damjan Marionafe56de2018-05-17 12:44:00 +0200228 while (count >= 8)
229 {
Damjan Marion54208852021-04-21 15:25:47 +0200230 u64x8 b0 = u64x8_from_u32x8 (u32x8_load_unaligned (bi));
Damjan Marionafe56de2018-05-17 12:44:00 +0200231 /* shift and add to get vlib_buffer_t pointer */
Damjan Marion54208852021-04-21 15:25:47 +0200232 u64x8_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b);
Damjan Marionafe56de2018-05-17 12:44:00 +0200233 b += 8;
234 bi += 8;
235 count -= 8;
236 }
Damjan Marion54208852021-04-21 15:25:47 +0200237#elif defined CLIB_HAVE_VEC256
238 u64x4 off = u64x4_splat (buffer_mem_start + offset);
239 /* if count is not const, compiler will not unroll while loop
240 se we maintain two-in-parallel variant */
241 while (count >= 32)
242 {
243 u64x4 b0 = u64x4_from_u32x4 (u32x4_load_unaligned (bi));
244 u64x4 b1 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 4));
245 u64x4 b2 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 8));
246 u64x4 b3 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 12));
247 u64x4 b4 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 16));
248 u64x4 b5 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 20));
249 u64x4 b6 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 24));
250 u64x4 b7 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 28));
251 /* shift and add to get vlib_buffer_t pointer */
252 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
253 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
254 u64x4_store_unaligned ((b2 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 8);
255 u64x4_store_unaligned ((b3 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 12);
256 u64x4_store_unaligned ((b4 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 16);
257 u64x4_store_unaligned ((b5 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 20);
258 u64x4_store_unaligned ((b6 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 24);
259 u64x4_store_unaligned ((b7 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 28);
260 b += 32;
261 bi += 32;
262 count -= 32;
263 }
Damjan Marionafe56de2018-05-17 12:44:00 +0200264#endif
265 while (count >= 4)
266 {
267#ifdef CLIB_HAVE_VEC256
Damjan Marion90d05bc2020-08-31 17:18:26 +0200268 u64x4 b0 = u64x4_from_u32x4 (u32x4_load_unaligned (bi));
Damjan Marionafe56de2018-05-17 12:44:00 +0200269 /* shift and add to get vlib_buffer_t pointer */
270 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800271#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100272 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200273 u32x4 bi4 = u32x4_load_unaligned (bi);
Damjan Marion90d05bc2020-08-31 17:18:26 +0200274 u64x2 b0 = u64x2_from_u32x4 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800275#if defined (__aarch64__)
Damjan Marion90d05bc2020-08-31 17:18:26 +0200276 u64x2 b1 = u64x2_from_u32x4_high ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800277#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200278 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
Damjan Marion90d05bc2020-08-31 17:18:26 +0200279 u64x2 b1 = u64x2_from_u32x4 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800280#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200281 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
282 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200283#else
Damjan Marion24dcbe42019-01-31 12:29:39 +0100284 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
285 b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset);
286 b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset);
287 b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200288#endif
289 b += 4;
290 bi += 4;
291 count -= 4;
292 }
293 while (count)
294 {
Damjan Marion24dcbe42019-01-31 12:29:39 +0100295 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200296 b += 1;
297 bi += 1;
298 count -= 1;
299 }
300}
301
302/** \brief Translate array of buffer indices into buffer pointers
303
304 @param vm - (vlib_main_t *) vlib main data structure pointer
305 @param bi - (u32 *) array of buffer indices
306 @param b - (vlib_buffer_t **) array to store buffer pointers
307 @param count - (uword) number of elements
308*/
309
310static_always_inline void
311vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
312{
313 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
314}
315
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316/** \brief Translate buffer pointer into buffer index
317
318 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400319 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400321*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200322
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400324vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325{
Damjan Mariond50e3472019-01-20 00:03:56 +0100326 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200327 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
328 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
329 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400330 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
332}
333
Damjan Marionafe56de2018-05-17 12:44:00 +0200334/** \brief Translate array of buffer pointers into buffer indices with offset
335
336 @param vm - (vlib_main_t *) vlib main data structure pointer
337 @param b - (void **) array of buffer pointers
338 @param bi - (u32 *) array to store buffer indices
339 @param count - (uword) number of elements
340 @param offset - (i32) offset applied to each pointer
341*/
342static_always_inline void
343vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
344 uword count, i32 offset)
345{
346#ifdef CLIB_HAVE_VEC256
347 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100348 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200349
350 while (count >= 8)
351 {
352 /* load 4 pointers into 256-bit register */
353 u64x4 v0 = u64x4_load_unaligned (b);
354 u64x4 v1 = u64x4_load_unaligned (b + 4);
355 u32x8 v2, v3;
356
357 v0 -= off4;
358 v1 -= off4;
359
360 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
361 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
362
363 /* permute 256-bit register so lower u32s of each buffer index are
364 * placed into lower 128-bits */
365 v2 = u32x8_permute ((u32x8) v0, mask);
366 v3 = u32x8_permute ((u32x8) v1, mask);
367
368 /* extract lower 128-bits and save them to the array of buffer indices */
369 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
370 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
371 bi += 8;
372 b += 8;
373 count -= 8;
374 }
375#endif
376 while (count >= 4)
377 {
378 /* equivalent non-nector implementation */
379 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
380 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
381 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
382 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
383 bi += 4;
384 b += 4;
385 count -= 4;
386 }
387 while (count)
388 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400389 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200390 bi += 1;
391 b += 1;
392 count -= 1;
393 }
394}
395
396/** \brief Translate array of buffer pointers into buffer indices
397
398 @param vm - (vlib_main_t *) vlib main data structure pointer
399 @param b - (vlib_buffer_t **) array of buffer pointers
400 @param bi - (u32 *) array to store buffer indices
401 @param count - (uword) number of elements
402*/
403static_always_inline void
404vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
405 uword count)
406{
407 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
408}
409
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410/** \brief Get next buffer in buffer linklist, or zero for end of list.
411
412 @param vm - (vlib_main_t *) vlib main data structure pointer
413 @param b - (void *) buffer pointer
414 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416always_inline vlib_buffer_t *
417vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
418{
419 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421}
422
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
424 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
426/** \brief Get length in bytes of the buffer chain
427
428 @param vm - (vlib_main_t *) vlib main data structure pointer
429 @param b - (void *) buffer pointer
430 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400431*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432always_inline uword
433vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
434{
Damjan Marion072401e2017-07-13 18:53:27 +0200435 uword len = b->current_length;
436
437 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
438 return len;
439
440 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
441 return len + b->total_length_not_including_first_buffer;
442
443 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444}
445
446/** \brief Get length in bytes of the buffer index buffer chain
447
448 @param vm - (vlib_main_t *) vlib main data structure pointer
449 @param bi - (u32) buffer index
450 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452always_inline uword
453vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
454{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 return vlib_buffer_length_in_chain (vm, b);
457}
458
459/** \brief Copy buffer contents to memory
460
461 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400462 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 @param contents - (u8 *) memory, <strong>must be large enough</strong>
464 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466always_inline uword
467vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
468{
469 uword content_len = 0;
470 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
473 while (1)
474 {
475 b = vlib_get_buffer (vm, buffer_index);
476 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500477 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400479 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 break;
481 buffer_index = b->next_buffer;
482 }
483
484 return content_len;
485}
486
Damjan Marion8f499362018-10-22 13:07:02 +0200487always_inline uword
488vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489{
Damjan Marion68b4da62018-09-30 18:26:20 +0200490 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491}
492
Damjan Marion8f499362018-10-22 13:07:02 +0200493always_inline uword
494vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
495{
496 return vlib_buffer_get_pa (vm, b) + b->current_data;
497}
498
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499/** \brief Prefetch buffer metadata by buffer index
500 The first 64 bytes of buffer contains most header information
501
502 @param vm - (vlib_main_t *) vlib main data structure pointer
503 @param bi - (u32) buffer index
504 @param type - LOAD, STORE. In most cases, STORE is the right answer
505*/
506/* Prefetch buffer header given index. */
507#define vlib_prefetch_buffer_with_index(vm,bi,type) \
508 do { \
509 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
510 vlib_prefetch_buffer_header (_b, type); \
511 } while (0)
512
Dave Barach9b8ffd92016-07-08 08:13:45 -0400513typedef enum
514{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 /* Index is unknown. */
516 VLIB_BUFFER_UNKNOWN,
517
518 /* Index is known and free/allocated. */
519 VLIB_BUFFER_KNOWN_FREE,
520 VLIB_BUFFER_KNOWN_ALLOCATED,
521} vlib_buffer_known_state_t;
522
Damjan Marionc8a26c62017-11-24 20:15:23 +0100523void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
524 uword n_buffers,
525 vlib_buffer_known_state_t
526 expected_state);
527
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100529vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530{
Damjan Mariond50e3472019-01-20 00:03:56 +0100531 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Damjan Marion6b0f5892017-07-27 04:01:24 -0400533 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400534 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400535 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
537}
538
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539/* Validates sanity of a single buffer.
540 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400541u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
542 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100544u8 *vlib_validate_buffers (vlib_main_t * vm,
545 u32 * buffers,
546 uword next_buffer_stride,
547 uword n_buffers,
548 vlib_buffer_known_state_t known_state,
549 uword follow_buffer_next);
550
Damjan Marion910d3692019-01-21 11:48:34 +0100551static_always_inline vlib_buffer_pool_t *
552vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553{
Damjan Marion910d3692019-01-21 11:48:34 +0100554 vlib_buffer_main_t *bm = vm->buffer_main;
555 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400556}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
Damjan Mariona2185122020-04-08 00:52:53 +0200558static_always_inline __clib_warn_unused_result uword
Damjan Marion910d3692019-01-21 11:48:34 +0100559vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
560 u32 n_buffers)
561{
562 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
563 u32 len;
564
565 ASSERT (bp->buffers);
566
567 clib_spinlock_lock (&bp->lock);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100568 len = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100569 if (PREDICT_TRUE (n_buffers < len))
570 {
571 len -= n_buffers;
572 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100573 bp->n_avail = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100574 clib_spinlock_unlock (&bp->lock);
575 return n_buffers;
576 }
577 else
578 {
579 vlib_buffer_copy_indices (buffers, bp->buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100580 bp->n_avail = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100581 clib_spinlock_unlock (&bp->lock);
582 return len;
583 }
584}
585
586
587/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
589 @param vm - (vlib_main_t *) vlib main data structure pointer
590 @param buffers - (u32 * ) buffer index array
591 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400592 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593 less than the number requested or zero
594*/
Damjan Marion910d3692019-01-21 11:48:34 +0100595
Damjan Mariona2185122020-04-08 00:52:53 +0200596always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100597vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
598 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100599{
Damjan Mariond50e3472019-01-20 00:03:56 +0100600 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100601 vlib_buffer_pool_t *bp;
602 vlib_buffer_pool_thread_t *bpt;
603 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100604
Dave Barachc74b43c2020-04-09 17:24:07 -0400605 /* If buffer allocation fault injection is configured */
606 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
607 {
608 u32 vlib_buffer_alloc_may_fail (vlib_main_t *, u32);
609
610 /* See how many buffers we're willing to allocate */
611 n_buffers = vlib_buffer_alloc_may_fail (vm, n_buffers);
612 if (n_buffers == 0)
613 return (n_buffers);
614 }
615
Damjan Marion910d3692019-01-21 11:48:34 +0100616 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
617 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100618
Damjan Marion910d3692019-01-21 11:48:34 +0100619 dst = buffers;
620 n_left = n_buffers;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100621 len = bpt->n_cached;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100622
Damjan Marion910d3692019-01-21 11:48:34 +0100623 /* per-thread cache contains enough buffers */
624 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100625 {
Damjan Marion910d3692019-01-21 11:48:34 +0100626 src = bpt->cached_buffers + len - n_buffers;
627 vlib_buffer_copy_indices (dst, src, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100628 bpt->n_cached -= n_buffers;
629
630 if (CLIB_DEBUG > 0)
631 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
632 VLIB_BUFFER_KNOWN_FREE);
633 return n_buffers;
634 }
635
636 /* alloc bigger than cache - take buffers directly from main pool */
637 if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ)
638 {
639 n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers,
640 n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100641
Damjan Marion910d3692019-01-21 11:48:34 +0100642 if (CLIB_DEBUG > 0)
643 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
644 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100645 return n_buffers;
646 }
647
Damjan Marion910d3692019-01-21 11:48:34 +0100648 /* take everything available in the cache */
649 if (len)
650 {
651 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100652 bpt->n_cached = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100653 dst += len;
654 n_left -= len;
655 }
656
657 len = round_pow2 (n_left, 32);
Damjan Marion910d3692019-01-21 11:48:34 +0100658 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
659 len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100660 bpt->n_cached = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100661
662 if (len)
663 {
664 u32 n_copy = clib_min (len, n_left);
665 src = bpt->cached_buffers + len - n_copy;
666 vlib_buffer_copy_indices (dst, src, n_copy);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100667 bpt->n_cached -= n_copy;
Damjan Marion910d3692019-01-21 11:48:34 +0100668 n_left -= n_copy;
669 }
670
671 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100672
673 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100674 if (CLIB_DEBUG > 0)
675 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
676 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100677
678 return n_buffers;
679}
680
Damjan Marion910d3692019-01-21 11:48:34 +0100681/** \brief Allocate buffers from specific numa node into supplied array
682
683 @param vm - (vlib_main_t *) vlib main data structure pointer
684 @param buffers - (u32 * ) buffer index array
685 @param n_buffers - (u32) number of buffers requested
686 @param numa_node - (u32) numa node
687 @return - (u32) number of buffers actually allocated, may be
688 less than the number requested or zero
689*/
Damjan Mariona2185122020-04-08 00:52:53 +0200690always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100691vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
692 u32 numa_node)
693{
694 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
695 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
696}
697
Damjan Marionc8a26c62017-11-24 20:15:23 +0100698/** \brief Allocate buffers into supplied array
699
700 @param vm - (vlib_main_t *) vlib main data structure pointer
701 @param buffers - (u32 * ) buffer index array
702 @param n_buffers - (u32) number of buffers requested
703 @return - (u32) number of buffers actually allocated, may be
704 less than the number requested or zero
705*/
Damjan Marion910d3692019-01-21 11:48:34 +0100706
Damjan Mariona2185122020-04-08 00:52:53 +0200707always_inline __clib_warn_unused_result u32
Damjan Marionc8a26c62017-11-24 20:15:23 +0100708vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
709{
Damjan Marion910d3692019-01-21 11:48:34 +0100710 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100711}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712
Damjan Marionc58408c2018-01-18 14:54:04 +0100713/** \brief Allocate buffers into ring
714
715 @param vm - (vlib_main_t *) vlib main data structure pointer
716 @param buffers - (u32 * ) buffer index ring
717 @param start - (u32) first slot in the ring
718 @param ring_size - (u32) ring size
719 @param n_buffers - (u32) number of buffers requested
720 @return - (u32) number of buffers actually allocated, may be
721 less than the number requested or zero
722*/
Damjan Mariona2185122020-04-08 00:52:53 +0200723always_inline __clib_warn_unused_result u32
Damjan Marionc58408c2018-01-18 14:54:04 +0100724vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
725 u32 ring_size, u32 n_buffers)
726{
727 u32 n_alloc;
728
729 ASSERT (n_buffers <= ring_size);
730
731 if (PREDICT_TRUE (start + n_buffers <= ring_size))
732 return vlib_buffer_alloc (vm, ring + start, n_buffers);
733
734 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
735
736 if (PREDICT_TRUE (n_alloc == ring_size - start))
737 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
738
739 return n_alloc;
740}
741
Damjan Marion910d3692019-01-21 11:48:34 +0100742/** \brief Allocate buffers into ring from specific buffer pool
743
744 @param vm - (vlib_main_t *) vlib main data structure pointer
745 @param buffers - (u32 * ) buffer index ring
746 @param start - (u32) first slot in the ring
747 @param ring_size - (u32) ring size
748 @param n_buffers - (u32) number of buffers requested
749 @return - (u32) number of buffers actually allocated, may be
750 less than the number requested or zero
751*/
Damjan Mariona2185122020-04-08 00:52:53 +0200752always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100753vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
754 u32 ring_size, u32 n_buffers,
755 u8 buffer_pool_index)
756{
757 u32 n_alloc;
758
759 ASSERT (n_buffers <= ring_size);
760
761 if (PREDICT_TRUE (start + n_buffers <= ring_size))
762 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
763 buffer_pool_index);
764
765 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
766 buffer_pool_index);
767
768 if (PREDICT_TRUE (n_alloc == ring_size - start))
769 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
770 buffer_pool_index);
771
772 return n_alloc;
773}
774
Damjan Marionf646d742019-01-31 18:50:04 +0100775static_always_inline void
Damjan Marion910d3692019-01-21 11:48:34 +0100776vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
777 u32 * buffers, u32 n_buffers)
778{
779 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100780 vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads,
781 vm->thread_index);
782 u32 n_cached, n_empty;
Damjan Marion910d3692019-01-21 11:48:34 +0100783
Damjan Marionf646d742019-01-31 18:50:04 +0100784 if (CLIB_DEBUG > 0)
785 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
786 VLIB_BUFFER_KNOWN_ALLOCATED);
787
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100788 n_cached = bpt->n_cached;
789 n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached;
790 if (n_buffers <= n_empty)
Damjan Marion910d3692019-01-21 11:48:34 +0100791 {
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100792 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
793 buffers, n_buffers);
794 bpt->n_cached = n_cached + n_buffers;
795 return;
Damjan Marion910d3692019-01-21 11:48:34 +0100796 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100797
798 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
799 buffers + n_buffers - n_empty, n_empty);
800 bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ;
801
802 clib_spinlock_lock (&bp->lock);
803 vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers,
804 n_buffers - n_empty);
805 bp->n_avail += n_buffers - n_empty;
806 clib_spinlock_unlock (&bp->lock);
Damjan Marion910d3692019-01-21 11:48:34 +0100807}
808
809static_always_inline void
810vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
811 int maybe_next)
812{
813 const int queue_size = 128;
814 vlib_buffer_pool_t *bp = 0;
815 u8 buffer_pool_index = ~0;
816 u32 n_queue = 0, queue[queue_size + 4];
817 vlib_buffer_t bt = { };
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800818#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100819 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
Radu Nicolaud82349e2021-03-16 12:45:01 +0000820 vlib_buffer_t bpi_vec = {};
Damjan Marion910d3692019-01-21 11:48:34 +0100821 vlib_buffer_t flags_refs_mask = {
822 .flags = VLIB_BUFFER_NEXT_PRESENT,
Damjan Marion0e209242019-03-13 12:04:10 +0100823 .ref_count = ~1
Damjan Marion910d3692019-01-21 11:48:34 +0100824 };
825#endif
826
Radu Nicolaud82349e2021-03-16 12:45:01 +0000827 if (PREDICT_FALSE (n_buffers == 0))
828 return;
829
830 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[0]);
831 buffer_pool_index = b->buffer_pool_index;
832 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
833 vlib_buffer_copy_template (&bt, &bp->buffer_template);
834#if defined(CLIB_HAVE_VEC128)
835 bpi_vec.buffer_pool_index = buffer_pool_index;
836#endif
837
Damjan Marion910d3692019-01-21 11:48:34 +0100838 while (n_buffers)
839 {
840 vlib_buffer_t *b[8];
841 u32 bi, sum = 0, flags, next;
842
Radu Nicolaud82349e2021-03-16 12:45:01 +0000843 if (n_buffers < 4)
Damjan Marion910d3692019-01-21 11:48:34 +0100844 goto one_by_one;
845
846 vlib_get_buffers (vm, buffers, b, 4);
Damjan Marion910d3692019-01-21 11:48:34 +0100847
Radu Nicolaud82349e2021-03-16 12:45:01 +0000848 if (n_buffers >= 12)
849 {
850 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
851 vlib_prefetch_buffer_header (b[4], LOAD);
852 vlib_prefetch_buffer_header (b[5], LOAD);
853 vlib_prefetch_buffer_header (b[6], LOAD);
854 vlib_prefetch_buffer_header (b[7], LOAD);
855 }
Damjan Marion910d3692019-01-21 11:48:34 +0100856
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800857#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100858 u8x16 p0, p1, p2, p3, r;
859 p0 = u8x16_load_unaligned (b[0]);
860 p1 = u8x16_load_unaligned (b[1]);
861 p2 = u8x16_load_unaligned (b[2]);
862 p3 = u8x16_load_unaligned (b[3]);
863
864 r = p0 ^ bpi_vec.as_u8x16[0];
865 r |= p1 ^ bpi_vec.as_u8x16[0];
866 r |= p2 ^ bpi_vec.as_u8x16[0];
867 r |= p3 ^ bpi_vec.as_u8x16[0];
868 r &= bpi_mask.as_u8x16[0];
869 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
870
871 sum = !u8x16_is_all_zero (r);
872#else
873 sum |= b[0]->flags;
874 sum |= b[1]->flags;
875 sum |= b[2]->flags;
876 sum |= b[3]->flags;
877 sum &= VLIB_BUFFER_NEXT_PRESENT;
878 sum += b[0]->ref_count - 1;
879 sum += b[1]->ref_count - 1;
880 sum += b[2]->ref_count - 1;
881 sum += b[3]->ref_count - 1;
882 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
883 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
884 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
885 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
886#endif
887
888 if (sum)
889 goto one_by_one;
890
891 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
892 vlib_buffer_copy_template (b[0], &bt);
893 vlib_buffer_copy_template (b[1], &bt);
894 vlib_buffer_copy_template (b[2], &bt);
895 vlib_buffer_copy_template (b[3], &bt);
896 n_queue += 4;
897
Damjan Marion24dcbe42019-01-31 12:29:39 +0100898 vlib_buffer_validate (vm, b[0]);
899 vlib_buffer_validate (vm, b[1]);
900 vlib_buffer_validate (vm, b[2]);
901 vlib_buffer_validate (vm, b[3]);
902
Damjan Marion910d3692019-01-21 11:48:34 +0100903 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
904 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
905 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
906 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
907
908 if (n_queue >= queue_size)
909 {
910 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
911 n_queue = 0;
912 }
913 buffers += 4;
914 n_buffers -= 4;
915 continue;
916
917 one_by_one:
918 bi = buffers[0];
919
920 next_in_chain:
921 b[0] = vlib_get_buffer (vm, bi);
922 flags = b[0]->flags;
923 next = b[0]->next_buffer;
924
925 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
926 {
Damjan Marion910d3692019-01-21 11:48:34 +0100927
928 if (n_queue)
929 {
930 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
931 n_queue = 0;
932 }
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500933
934 buffer_pool_index = b[0]->buffer_pool_index;
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800935#if defined(CLIB_HAVE_VEC128)
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500936 bpi_vec.buffer_pool_index = buffer_pool_index;
937#endif
938 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
939 vlib_buffer_copy_template (&bt, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100940 }
941
Damjan Marion24dcbe42019-01-31 12:29:39 +0100942 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100943
944 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
945
946 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
947 {
Damjan Marion910d3692019-01-21 11:48:34 +0100948 vlib_buffer_copy_template (b[0], &bt);
949 queue[n_queue++] = bi;
950 }
951
952 if (n_queue == queue_size)
953 {
954 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
955 n_queue = 0;
956 }
957
Damjan Marion5fed42a2019-06-11 20:12:23 +0200958 if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT))
Damjan Marion910d3692019-01-21 11:48:34 +0100959 {
960 bi = next;
961 goto next_in_chain;
962 }
963
964 buffers++;
965 n_buffers--;
966 }
967
968 if (n_queue)
969 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
970}
971
972
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973/** \brief Free buffers
974 Frees the entire buffer chain for each buffer
975
976 @param vm - (vlib_main_t *) vlib main data structure pointer
977 @param buffers - (u32 * ) buffer index array
978 @param n_buffers - (u32) number of buffers to free
979
980*/
Damjan Marion878c6092017-01-04 13:19:27 +0100981always_inline void
982vlib_buffer_free (vlib_main_t * vm,
983 /* pointer to first buffer */
984 u32 * buffers,
985 /* number of buffers to free */
986 u32 n_buffers)
987{
Damjan Marion910d3692019-01-21 11:48:34 +0100988 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +0100989}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990
991/** \brief Free buffers, does not free the buffer chain for each buffer
992
993 @param vm - (vlib_main_t *) vlib main data structure pointer
994 @param buffers - (u32 * ) buffer index array
995 @param n_buffers - (u32) number of buffers to free
996
997*/
Damjan Marion878c6092017-01-04 13:19:27 +0100998always_inline void
999vlib_buffer_free_no_next (vlib_main_t * vm,
1000 /* pointer to first buffer */
1001 u32 * buffers,
1002 /* number of buffers to free */
1003 u32 n_buffers)
1004{
Damjan Marion910d3692019-01-21 11:48:34 +01001005 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +01001006}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007
1008/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -04001009 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010
1011 @param vm - (vlib_main_t *) vlib main data structure pointer
1012 @param buffer_index - (u32) buffer index to free
1013*/
1014always_inline void
1015vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
1016{
Damjan Marion910d3692019-01-21 11:48:34 +01001017 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018}
1019
Damjan Mariona3731492018-02-25 22:50:39 +01001020/** \brief Free buffers from ring
1021
1022 @param vm - (vlib_main_t *) vlib main data structure pointer
1023 @param buffers - (u32 * ) buffer index ring
1024 @param start - (u32) first slot in the ring
1025 @param ring_size - (u32) ring size
1026 @param n_buffers - (u32) number of buffers
1027*/
1028always_inline void
1029vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
1030 u32 ring_size, u32 n_buffers)
1031{
1032 ASSERT (n_buffers <= ring_size);
1033
1034 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1035 {
1036 vlib_buffer_free (vm, ring + start, n_buffers);
1037 }
1038 else
1039 {
1040 vlib_buffer_free (vm, ring + start, ring_size - start);
1041 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
1042 }
1043}
1044
Damjan Marioncef1db92018-03-28 18:27:38 +02001045/** \brief Free buffers from ring without freeing tail buffers
1046
1047 @param vm - (vlib_main_t *) vlib main data structure pointer
1048 @param buffers - (u32 * ) buffer index ring
1049 @param start - (u32) first slot in the ring
1050 @param ring_size - (u32) ring size
1051 @param n_buffers - (u32) number of buffers
1052*/
1053always_inline void
1054vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
1055 u32 ring_size, u32 n_buffers)
1056{
1057 ASSERT (n_buffers <= ring_size);
1058
1059 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1060 {
Damjan Marion4a973932018-06-09 19:29:16 +02001061 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +02001062 }
1063 else
1064 {
1065 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
1066 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
1067 }
1068}
Damjan Mariona3731492018-02-25 22:50:39 +01001069
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001071int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
1072 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
John Lo8ed2d522019-08-07 19:30:29 -04001074/* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */
1075#define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \
1076 (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \
1077 VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL)
1078
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001079/* duplicate all buffers in chain */
1080always_inline vlib_buffer_t *
1081vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
1082{
1083 vlib_buffer_t *s, *d, *fd;
1084 uword n_alloc, n_buffers = 1;
John Lo8ed2d522019-08-07 19:30:29 -04001085 u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001086 int i;
1087
1088 s = b;
1089 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1090 {
1091 n_buffers++;
1092 s = vlib_get_buffer (vm, s->next_buffer);
1093 }
Neale Ranns9d676af2017-03-15 01:28:31 -07001094 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001095
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001096 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -05001097
1098 /* No guarantee that we'll get all the buffers we asked for */
1099 if (PREDICT_FALSE (n_alloc < n_buffers))
1100 {
1101 if (n_alloc > 0)
1102 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -05001103 return 0;
1104 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001105
1106 /* 1st segment */
1107 s = b;
1108 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001109 d->current_data = s->current_data;
1110 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +01001111 d->flags = s->flags & flag_mask;
John Lo66317802019-08-13 18:18:21 -04001112 d->trace_handle = s->trace_handle;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001113 d->total_length_not_including_first_buffer =
1114 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -05001115 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1116 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1117 clib_memcpy_fast (vlib_buffer_get_current (d),
1118 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001119
1120 /* next segments */
1121 for (i = 1; i < n_buffers; i++)
1122 {
1123 /* previous */
1124 d->next_buffer = new_buffers[i];
1125 /* current */
1126 s = vlib_get_buffer (vm, s->next_buffer);
1127 d = vlib_get_buffer (vm, new_buffers[i]);
1128 d->current_data = s->current_data;
1129 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -05001130 clib_memcpy_fast (vlib_buffer_get_current (d),
1131 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +01001132 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001133 }
1134
1135 return fd;
1136}
1137
Ole Troanda7f7b62019-03-11 13:15:54 +01001138/* duplicate first buffer in chain */
1139always_inline vlib_buffer_t *
1140vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di)
1141{
1142 vlib_buffer_t *d;
1143
1144 if ((vlib_buffer_alloc (vm, di, 1)) != 1)
1145 return 0;
1146
1147 d = vlib_get_buffer (vm, *di);
1148 /* 1st segment */
1149 d->current_data = b->current_data;
1150 d->current_length = b->current_length;
1151 clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque));
1152 clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2));
1153 clib_memcpy_fast (vlib_buffer_get_current (d),
1154 vlib_buffer_get_current (b), b->current_length);
1155
1156 return d;
1157}
1158
John Lof545caa2019-04-01 11:30:07 -04001159/* \brief Move packet from current position to offset position in buffer.
1160 Only work for small packet using one buffer with room to fit the move
1161 @param vm - (vlib_main_t *) vlib main data structure pointer
1162 @param b - (vlib_buffer_t *) pointer to buffer
1163 @param offset - (i16) position to move the packet in buffer
1164 */
1165always_inline void
1166vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset)
1167{
1168 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
1169 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1170 ASSERT (offset + b->current_length <
1171 vlib_buffer_get_default_data_size (vm));
1172
1173 u8 *source = vlib_buffer_get_current (b);
1174 b->current_data = offset;
1175 u8 *destination = vlib_buffer_get_current (b);
1176 u16 length = b->current_length;
1177
1178 if (source + length <= destination) /* no overlap */
1179 clib_memcpy_fast (destination, source, length);
1180 else
1181 memmove (destination, source, length);
1182}
1183
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001184/** \brief Create a maximum of 256 clones of buffer and store them
1185 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +01001186
1187 @param vm - (vlib_main_t *) vlib main data structure pointer
1188 @param src_buffer - (u32) source buffer index
1189 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001190 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +01001191 @param head_end_offset - (u16) offset relative to current position
1192 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001193 @param offset - (i16) copy packet head at current position if 0,
1194 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001195 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +01001196 less than the number requested or zero
1197*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001198always_inline u16
1199vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
John Lof545caa2019-04-01 11:30:07 -04001200 u16 n_buffers, u16 head_end_offset, i16 offset)
Damjan Marionc47ed032017-01-25 14:18:03 +01001201{
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001202 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +01001203 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1204
Damjan Marion910d3692019-01-21 11:48:34 +01001205 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001206 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001207 ASSERT (n_buffers <= 256);
John Lof545caa2019-04-01 11:30:07 -04001208 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1209 ASSERT ((offset + head_end_offset) <
1210 vlib_buffer_get_default_data_size (vm));
Damjan Marionc47ed032017-01-25 14:18:03 +01001211
1212 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1213 {
1214 buffers[0] = src_buffer;
John Lof545caa2019-04-01 11:30:07 -04001215 if (offset)
1216 vlib_buffer_move (vm, s, offset);
1217
Damjan Marionc47ed032017-01-25 14:18:03 +01001218 for (i = 1; i < n_buffers; i++)
1219 {
1220 vlib_buffer_t *d;
1221 d = vlib_buffer_copy (vm, s);
1222 if (d == 0)
1223 return i;
1224 buffers[i] = vlib_get_buffer_index (vm, d);
1225
1226 }
1227 return n_buffers;
1228 }
1229
John Lof545caa2019-04-01 11:30:07 -04001230 if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0)))
Damjan Marionc47ed032017-01-25 14:18:03 +01001231 {
1232 buffers[0] = src_buffer;
1233 return 1;
1234 }
1235
Damjan Marion910d3692019-01-21 11:48:34 +01001236 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1237 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001238
Damjan Marionc47ed032017-01-25 14:18:03 +01001239 for (i = 0; i < n_buffers; i++)
1240 {
1241 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
John Lof545caa2019-04-01 11:30:07 -04001242 if (offset)
1243 d->current_data = offset;
1244 else
1245 d->current_data = s->current_data;
1246
Damjan Marionc47ed032017-01-25 14:18:03 +01001247 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001248 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1249
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001250 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001251 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001252 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1253 {
1254 d->total_length_not_including_first_buffer +=
1255 s->total_length_not_including_first_buffer;
1256 }
John Lo8ed2d522019-08-07 19:30:29 -04001257 d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) |
1258 VLIB_BUFFER_NEXT_PRESENT;
John Lo66317802019-08-13 18:18:21 -04001259 d->trace_handle = s->trace_handle;
Dave Barach178cf492018-11-13 16:34:13 -05001260 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1261 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1262 clib_memcpy_fast (vlib_buffer_get_current (d),
1263 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001264 d->next_buffer = src_buffer;
1265 }
1266 vlib_buffer_advance (s, head_end_offset);
Dave Barach95e19252020-04-07 10:52:43 -04001267 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001268 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1269 {
1270 s = vlib_get_buffer (vm, s->next_buffer);
Dave Barach95e19252020-04-07 10:52:43 -04001271 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001272 }
1273
1274 return n_buffers;
1275}
1276
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001277/** \brief Create multiple clones of buffer and store them
1278 in the supplied array
1279
1280 @param vm - (vlib_main_t *) vlib main data structure pointer
1281 @param src_buffer - (u32) source buffer index
1282 @param buffers - (u32 * ) buffer index array
1283 @param n_buffers - (u16) number of buffer clones requested (<=256)
1284 @param head_end_offset - (u16) offset relative to current position
1285 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001286 @param offset - (i16) copy packet head at current position if 0,
1287 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001288 @return - (u16) number of buffers actually cloned, may be
1289 less than the number requested or zero
1290*/
1291always_inline u16
John Lof545caa2019-04-01 11:30:07 -04001292vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1293 u16 n_buffers, u16 head_end_offset, i16 offset)
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001294{
1295 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1296 u16 n_cloned = 0;
1297
1298 while (n_buffers > 256)
1299 {
1300 vlib_buffer_t *copy;
1301 copy = vlib_buffer_copy (vm, s);
1302 n_cloned += vlib_buffer_clone_256 (vm,
1303 vlib_get_buffer_index (vm, copy),
1304 (buffers + n_cloned),
John Lof545caa2019-04-01 11:30:07 -04001305 256, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001306 n_buffers -= 256;
1307 }
1308 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1309 buffers + n_cloned,
John Lof545caa2019-04-01 11:30:07 -04001310 n_buffers, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001311
1312 return n_cloned;
1313}
1314
John Lof545caa2019-04-01 11:30:07 -04001315/** \brief Create multiple clones of buffer and store them
1316 in the supplied array
1317
1318 @param vm - (vlib_main_t *) vlib main data structure pointer
1319 @param src_buffer - (u32) source buffer index
1320 @param buffers - (u32 * ) buffer index array
1321 @param n_buffers - (u16) number of buffer clones requested (<=256)
1322 @param head_end_offset - (u16) offset relative to current position
1323 where packet head ends
1324 @return - (u16) number of buffers actually cloned, may be
1325 less than the number requested or zero
1326*/
1327always_inline u16
1328vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1329 u16 n_buffers, u16 head_end_offset)
1330{
1331 return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers,
1332 head_end_offset, 0);
1333}
1334
Damjan Marionc47ed032017-01-25 14:18:03 +01001335/** \brief Attach cloned tail to the buffer
1336
1337 @param vm - (vlib_main_t *) vlib main data structure pointer
1338 @param head - (vlib_buffer_t *) head buffer
1339 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1340*/
1341
1342always_inline void
1343vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1344 vlib_buffer_t * tail)
1345{
1346 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001347 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001348
1349 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1350 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1351 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1352 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1353 head->next_buffer = vlib_get_buffer_index (vm, tail);
1354 head->total_length_not_including_first_buffer = tail->current_length +
1355 tail->total_length_not_including_first_buffer;
1356
1357next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001358 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001359
1360 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1361 {
1362 tail = vlib_get_buffer (vm, tail->next_buffer);
1363 goto next_segment;
1364 }
1365}
1366
Pierre Pfister328e99b2016-02-12 13:18:42 +00001367/* Initializes the buffer as an empty packet with no chained buffers. */
1368always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001369vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001370{
1371 first->total_length_not_including_first_buffer = 0;
1372 first->current_length = 0;
1373 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1374 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001375}
1376
1377/* The provided next_bi buffer index is appended to the end of the packet. */
1378always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001379vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001380{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001381 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001382 last->next_buffer = next_bi;
1383 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1384 next_buffer->current_length = 0;
1385 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001386 return next_buffer;
1387}
1388
1389/* Increases or decreases the packet length.
1390 * It does not allocate or deallocate new buffers.
1391 * Therefore, the added length must be compatible
1392 * with the last buffer. */
1393always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001394vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1395 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001396{
1397 last->current_length += len;
1398 if (first != last)
1399 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001400}
1401
1402/* Copy data to the end of the packet and increases its length.
1403 * It does not allocate new buffers.
1404 * Returns the number of copied bytes. */
1405always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001406vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001407 vlib_buffer_t * first,
1408 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001409{
Damjan Marion8934a042019-02-09 23:29:26 +01001410 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001411 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1412 u16 len = clib_min (data_len,
1413 n_buffer_bytes - last->current_length -
1414 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001415 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1416 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001417 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001418 return len;
1419}
1420
1421/* Copy data to the end of the packet and increases its length.
1422 * Allocates additional buffers from the free list if necessary.
1423 * Returns the number of copied bytes.
1424 * 'last' value is modified whenever new buffers are allocated and
1425 * chained and points to the last buffer in the chain. */
1426u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001427vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001428 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001429 vlib_buffer_t ** last, void *data,
1430 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001431void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001432
Dave Barach9b8ffd92016-07-08 08:13:45 -04001433format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
Benoît Ganne43543172019-10-21 15:13:54 +02001434 format_vlib_buffer_contents, format_vlib_buffer_no_chain;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001435
Dave Barach9b8ffd92016-07-08 08:13:45 -04001436typedef struct
1437{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001439 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440
Damjan Mariond1274cb2018-03-13 21:32:17 +01001441 /* Number of buffers to allocate in each call to allocator. */
1442 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
Damjan Marion671e60e2018-12-30 18:09:59 +01001444 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445} vlib_packet_template_t;
1446
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447void vlib_packet_template_init (vlib_main_t * vm,
1448 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001449 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001451 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001452 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001453
Dave Barach9b8ffd92016-07-08 08:13:45 -04001454void *vlib_packet_template_get_packet (vlib_main_t * vm,
1455 vlib_packet_template_t * t,
1456 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001457
1458always_inline void
1459vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1460{
1461 vec_free (t->packet_data);
1462}
1463
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001464always_inline u32
1465vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +02001466{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001467 return b->data + vlib_buffer_get_default_data_size (vm) -
1468 ((u8 *) vlib_buffer_get_current (b) + b->current_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001469}
1470
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001471always_inline u32
1472vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b)
Eyal Barid3d42412018-11-05 13:29:25 +02001473{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001474 vlib_buffer_t *db = b, *sb, *first = b;
1475 int is_cloned = 0;
1476 u32 bytes_left = 0, data_size;
1477 u16 src_left, dst_left, n_buffers = 1;
1478 u8 *dp, *sp;
1479 u32 to_free = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001480
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001481 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
1482 return 1;
Eyal Barid3d42412018-11-05 13:29:25 +02001483
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001484 data_size = vlib_buffer_get_default_data_size (vm);
1485
1486 dst_left = vlib_buffer_space_left_at_end (vm, b);
1487
1488 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Eyal Barid3d42412018-11-05 13:29:25 +02001489 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001490 b = vlib_get_buffer (vm, b->next_buffer);
1491 if (b->ref_count > 1)
1492 is_cloned = 1;
1493 bytes_left += b->current_length;
1494 n_buffers++;
Eyal Barid3d42412018-11-05 13:29:25 +02001495 }
1496
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001497 /* if buffer is cloned, create completely new chain - unless everything fits
1498 * into one buffer */
1499 if (is_cloned && bytes_left >= dst_left)
Eyal Barid3d42412018-11-05 13:29:25 +02001500 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001501 u32 len = 0;
1502 u32 space_needed = bytes_left - dst_left;
1503 u32 tail;
1504
1505 if (vlib_buffer_alloc (vm, &tail, 1) == 0)
1506 return 0;
1507
1508 ++n_buffers;
1509 len += data_size;
1510 b = vlib_get_buffer (vm, tail);
1511
1512 while (len < space_needed)
Eyal Barid3d42412018-11-05 13:29:25 +02001513 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001514 u32 bi;
1515 if (vlib_buffer_alloc (vm, &bi, 1) == 0)
1516 {
1517 vlib_buffer_free_one (vm, tail);
1518 return 0;
1519 }
1520 b->flags = VLIB_BUFFER_NEXT_PRESENT;
1521 b->next_buffer = bi;
1522 b = vlib_get_buffer (vm, bi);
1523 len += data_size;
1524 n_buffers++;
1525 }
1526 sb = vlib_get_buffer (vm, first->next_buffer);
1527 to_free = first->next_buffer;
1528 first->next_buffer = tail;
1529 }
1530 else
1531 sb = vlib_get_buffer (vm, first->next_buffer);
1532
1533 src_left = sb->current_length;
1534 sp = vlib_buffer_get_current (sb);
1535 dp = vlib_buffer_get_tail (db);
1536
1537 while (bytes_left)
1538 {
1539 u16 bytes_to_copy;
1540
1541 if (dst_left == 0)
1542 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001543 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1544 ASSERT (db->flags & VLIB_BUFFER_NEXT_PRESENT);
1545 db = vlib_get_buffer (vm, db->next_buffer);
1546 dst_left = data_size;
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001547 if (db->current_data > 0)
1548 {
1549 db->current_data = 0;
1550 }
1551 else
1552 {
1553 dst_left += -db->current_data;
1554 }
1555 dp = vlib_buffer_get_current (db);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001556 }
1557
1558 while (src_left == 0)
1559 {
1560 ASSERT (sb->flags & VLIB_BUFFER_NEXT_PRESENT);
1561 sb = vlib_get_buffer (vm, sb->next_buffer);
1562 src_left = sb->current_length;
1563 sp = vlib_buffer_get_current (sb);
1564 }
1565
1566 bytes_to_copy = clib_min (dst_left, src_left);
1567
1568 if (dp != sp)
1569 {
1570 if (sb == db)
1571 bytes_to_copy = clib_min (bytes_to_copy, sp - dp);
1572
1573 clib_memcpy_fast (dp, sp, bytes_to_copy);
1574 }
1575
1576 src_left -= bytes_to_copy;
1577 dst_left -= bytes_to_copy;
1578 dp += bytes_to_copy;
1579 sp += bytes_to_copy;
1580 bytes_left -= bytes_to_copy;
1581 }
1582 if (db != first)
1583 db->current_data = 0;
1584 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1585
1586 if (is_cloned && to_free)
1587 vlib_buffer_free_one (vm, to_free);
1588 else
1589 {
1590 if (db->flags & VLIB_BUFFER_NEXT_PRESENT)
1591 vlib_buffer_free_one (vm, db->next_buffer);
1592 db->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1593 b = first;
1594 n_buffers = 1;
1595 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1596 {
1597 b = vlib_get_buffer (vm, b->next_buffer);
1598 ++n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001599 }
1600 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001601
1602 first->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1603
1604 return n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001605}
1606
Ed Warnickecb9cada2015-12-08 15:45:58 -07001607#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001608
1609/*
1610 * fd.io coding-style-patch-verification: ON
1611 *
1612 * Local Variables:
1613 * eval: (c-set-style "gnu")
1614 * End:
1615 */