blob: 8ab9c4e53ad49a68d8dabfbadee125932ae7eb65 [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) (
63 vlib_main_t *vm, u32 frame_queue_index, u32 *buffer_indices,
64 u16 *thread_indices, u32 n_packets, int drop_on_congestion);
65typedef struct
66{
67 vlib_buffer_enqueue_to_next_fn_t *buffer_enqueue_to_next_fn;
68 vlib_buffer_enqueue_to_single_next_fn_t *buffer_enqueue_to_single_next_fn;
69 vlib_buffer_enqueue_to_thread_fn_t *buffer_enqueue_to_thread_fn;
70} vlib_buffer_func_main_t;
71
72extern vlib_buffer_func_main_t vlib_buffer_func_main;
73
Damjan Marion24dcbe42019-01-31 12:29:39 +010074always_inline void
75vlib_buffer_validate (vlib_main_t * vm, vlib_buffer_t * b)
76{
77 vlib_buffer_main_t *bm = vm->buffer_main;
78 vlib_buffer_pool_t *bp;
79
80 /* reference count in allocated buffer always must be 1 or higher */
81 ASSERT (b->ref_count > 0);
82
Damjan Marion24dcbe42019-01-31 12:29:39 +010083 /* verify that buffer pool index is valid */
84 bp = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index);
85 ASSERT (pointer_to_uword (b) >= bp->start);
86 ASSERT (pointer_to_uword (b) < bp->start + bp->size -
Damjan Marion5de3fec2019-02-06 14:22:32 +010087 (bp->data_size + sizeof (vlib_buffer_t)));
Damjan Marion24dcbe42019-01-31 12:29:39 +010088}
89
90always_inline void *
91vlib_buffer_ptr_from_index (uword buffer_mem_start, u32 buffer_index,
92 uword offset)
93{
94 offset += ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES;
95 return uword_to_pointer (buffer_mem_start + offset, vlib_buffer_t *);
96}
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98/** \brief Translate buffer index into buffer pointer
99
100 @param vm - (vlib_main_t *) vlib main data structure pointer
101 @param buffer_index - (u32) buffer index
102 @return - (vlib_buffer_t *) buffer pointer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400103*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104always_inline vlib_buffer_t *
105vlib_get_buffer (vlib_main_t * vm, u32 buffer_index)
106{
Damjan Mariond50e3472019-01-20 00:03:56 +0100107 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion24dcbe42019-01-31 12:29:39 +0100108 vlib_buffer_t *b;
Damjan Marion04a7f052017-07-10 15:06:17 +0200109
Damjan Marion24dcbe42019-01-31 12:29:39 +0100110 b = vlib_buffer_ptr_from_index (bm->buffer_mem_start, buffer_index, 0);
111 vlib_buffer_validate (vm, b);
112 return b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113}
114
Damjan Marion5de3fec2019-02-06 14:22:32 +0100115static_always_inline u32
Damjan Marion8934a042019-02-09 23:29:26 +0100116vlib_buffer_get_default_data_size (vlib_main_t * vm)
Damjan Marion5de3fec2019-02-06 14:22:32 +0100117{
118 return vm->buffer_main->default_data_size;
119}
120
Damjan Marione58041f2019-01-18 19:56:09 +0100121static_always_inline void
Damjan Marion64d557c2019-01-18 20:03:41 +0100122vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices)
123{
Damjan Marion3e94c412019-03-13 12:41:54 +0100124#if defined(CLIB_HAVE_VEC512)
125 while (n_indices >= 16)
126 {
127 u32x16_store_unaligned (u32x16_load_unaligned (src), dst);
128 dst += 16;
129 src += 16;
130 n_indices -= 16;
131 }
132#endif
133
134#if defined(CLIB_HAVE_VEC256)
135 while (n_indices >= 8)
136 {
137 u32x8_store_unaligned (u32x8_load_unaligned (src), dst);
138 dst += 8;
139 src += 8;
140 n_indices -= 8;
141 }
142#endif
143
144#if defined(CLIB_HAVE_VEC128)
145 while (n_indices >= 4)
146 {
147 u32x4_store_unaligned (u32x4_load_unaligned (src), dst);
148 dst += 4;
149 src += 4;
150 n_indices -= 4;
151 }
152#endif
153
154 while (n_indices)
155 {
156 dst[0] = src[0];
157 dst += 1;
158 src += 1;
159 n_indices -= 1;
160 }
Damjan Marion64d557c2019-01-18 20:03:41 +0100161}
162
Damjan Marionb9250a92020-03-02 19:02:15 +0100163always_inline void
164vlib_buffer_copy_indices_from_ring (u32 * dst, u32 * ring, u32 start,
165 u32 ring_size, u32 n_buffers)
166{
167 ASSERT (n_buffers <= ring_size);
168
169 if (PREDICT_TRUE (start + n_buffers <= ring_size))
170 {
171 vlib_buffer_copy_indices (dst, ring + start, n_buffers);
172 }
173 else
174 {
175 u32 n = ring_size - start;
176 vlib_buffer_copy_indices (dst, ring + start, n);
177 vlib_buffer_copy_indices (dst + n, ring, n_buffers - n);
178 }
179}
180
Benoît Ganne72f49212020-03-19 11:41:07 +0100181always_inline void
182vlib_buffer_copy_indices_to_ring (u32 * ring, u32 * src, u32 start,
183 u32 ring_size, u32 n_buffers)
184{
185 ASSERT (n_buffers <= ring_size);
186
187 if (PREDICT_TRUE (start + n_buffers <= ring_size))
188 {
189 vlib_buffer_copy_indices (ring + start, src, n_buffers);
190 }
191 else
192 {
193 u32 n = ring_size - start;
194 vlib_buffer_copy_indices (ring + start, src, n);
195 vlib_buffer_copy_indices (ring, src + n, n_buffers - n);
196 }
197}
198
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100199STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +0100200static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +0100201vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
202{
Damjan Marion22f23ae2019-01-24 15:36:57 +0100203#if defined CLIB_HAVE_VEC512
204 b->as_u8x64[0] = bt->as_u8x64[0];
205#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100206 b->as_u8x32[0] = bt->as_u8x32[0];
207 b->as_u8x32[1] = bt->as_u8x32[1];
208#elif defined (CLIB_HAVE_VEC128)
209 b->as_u8x16[0] = bt->as_u8x16[0];
210 b->as_u8x16[1] = bt->as_u8x16[1];
211 b->as_u8x16[2] = bt->as_u8x16[2];
212 b->as_u8x16[3] = bt->as_u8x16[3];
213#else
214 clib_memcpy_fast (b, bt, 64);
215#endif
Damjan Marione58041f2019-01-18 19:56:09 +0100216}
217
Damjan Marion910d3692019-01-21 11:48:34 +0100218always_inline u8
219vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
220{
Damjan Marionb592d1b2019-02-28 23:16:11 +0100221 ASSERT (numa_node < VLIB_BUFFER_MAX_NUMA_NODES);
222 return vm->buffer_main->default_buffer_pool_index_for_numa[numa_node];
Damjan Marion910d3692019-01-21 11:48:34 +0100223}
224
Damjan Marionafe56de2018-05-17 12:44:00 +0200225/** \brief Translate array of buffer indices into buffer pointers with offset
226
227 @param vm - (vlib_main_t *) vlib main data structure pointer
228 @param bi - (u32 *) array of buffer indices
229 @param b - (void **) array to store buffer pointers
230 @param count - (uword) number of elements
231 @param offset - (i32) offset applied to each pointer
232*/
233static_always_inline void
234vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count,
235 i32 offset)
236{
Damjan Mariond50e3472019-01-20 00:03:56 +0100237 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marion54208852021-04-21 15:25:47 +0200238#ifdef CLIB_HAVE_VEC512
239 u64x8 of8 = u64x8_splat (buffer_mem_start + offset);
240 u64x4 off = u64x8_extract_lo (of8);
Damjan Marionafe56de2018-05-17 12:44:00 +0200241 /* if count is not const, compiler will not unroll while loop
242 se we maintain two-in-parallel variant */
Damjan Marion54208852021-04-21 15:25:47 +0200243 while (count >= 32)
244 {
245 u64x8 b0 = u64x8_from_u32x8 (u32x8_load_unaligned (bi));
246 u64x8 b1 = u64x8_from_u32x8 (u32x8_load_unaligned (bi + 8));
247 u64x8 b2 = u64x8_from_u32x8 (u32x8_load_unaligned (bi + 16));
248 u64x8 b3 = u64x8_from_u32x8 (u32x8_load_unaligned (bi + 24));
249 /* shift and add to get vlib_buffer_t pointer */
250 u64x8_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b);
251 u64x8_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b + 8);
252 u64x8_store_unaligned ((b2 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b + 16);
253 u64x8_store_unaligned ((b3 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b + 24);
254 b += 32;
255 bi += 32;
256 count -= 32;
257 }
Damjan Marionafe56de2018-05-17 12:44:00 +0200258 while (count >= 8)
259 {
Damjan Marion54208852021-04-21 15:25:47 +0200260 u64x8 b0 = u64x8_from_u32x8 (u32x8_load_unaligned (bi));
Damjan Marionafe56de2018-05-17 12:44:00 +0200261 /* shift and add to get vlib_buffer_t pointer */
Damjan Marion54208852021-04-21 15:25:47 +0200262 u64x8_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + of8, b);
Damjan Marionafe56de2018-05-17 12:44:00 +0200263 b += 8;
264 bi += 8;
265 count -= 8;
266 }
Damjan Marion54208852021-04-21 15:25:47 +0200267#elif defined CLIB_HAVE_VEC256
268 u64x4 off = u64x4_splat (buffer_mem_start + offset);
269 /* if count is not const, compiler will not unroll while loop
270 se we maintain two-in-parallel variant */
271 while (count >= 32)
272 {
273 u64x4 b0 = u64x4_from_u32x4 (u32x4_load_unaligned (bi));
274 u64x4 b1 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 4));
275 u64x4 b2 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 8));
276 u64x4 b3 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 12));
277 u64x4 b4 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 16));
278 u64x4 b5 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 20));
279 u64x4 b6 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 24));
280 u64x4 b7 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 28));
281 /* shift and add to get vlib_buffer_t pointer */
282 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
283 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
284 u64x4_store_unaligned ((b2 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 8);
285 u64x4_store_unaligned ((b3 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 12);
286 u64x4_store_unaligned ((b4 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 16);
287 u64x4_store_unaligned ((b5 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 20);
288 u64x4_store_unaligned ((b6 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 24);
289 u64x4_store_unaligned ((b7 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 28);
290 b += 32;
291 bi += 32;
292 count -= 32;
293 }
Damjan Marionafe56de2018-05-17 12:44:00 +0200294#endif
295 while (count >= 4)
296 {
297#ifdef CLIB_HAVE_VEC256
Damjan Marion90d05bc2020-08-31 17:18:26 +0200298 u64x4 b0 = u64x4_from_u32x4 (u32x4_load_unaligned (bi));
Damjan Marionafe56de2018-05-17 12:44:00 +0200299 /* shift and add to get vlib_buffer_t pointer */
300 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800301#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100302 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200303 u32x4 bi4 = u32x4_load_unaligned (bi);
Damjan Marion90d05bc2020-08-31 17:18:26 +0200304 u64x2 b0 = u64x2_from_u32x4 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800305#if defined (__aarch64__)
Damjan Marion90d05bc2020-08-31 17:18:26 +0200306 u64x2 b1 = u64x2_from_u32x4_high ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800307#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200308 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
Damjan Marion90d05bc2020-08-31 17:18:26 +0200309 u64x2 b1 = u64x2_from_u32x4 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800310#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200311 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
312 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200313#else
Damjan Marion24dcbe42019-01-31 12:29:39 +0100314 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
315 b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset);
316 b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset);
317 b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200318#endif
319 b += 4;
320 bi += 4;
321 count -= 4;
322 }
323 while (count)
324 {
Damjan Marion24dcbe42019-01-31 12:29:39 +0100325 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200326 b += 1;
327 bi += 1;
328 count -= 1;
329 }
330}
331
332/** \brief Translate array of buffer indices into buffer pointers
333
334 @param vm - (vlib_main_t *) vlib main data structure pointer
335 @param bi - (u32 *) array of buffer indices
336 @param b - (vlib_buffer_t **) array to store buffer pointers
337 @param count - (uword) number of elements
338*/
339
340static_always_inline void
341vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
342{
343 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
344}
345
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346/** \brief Translate buffer pointer into buffer index
347
348 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400349 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400351*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200352
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355{
Damjan Mariond50e3472019-01-20 00:03:56 +0100356 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200357 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
358 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
359 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
362}
363
Damjan Marionafe56de2018-05-17 12:44:00 +0200364/** \brief Translate array of buffer pointers into buffer indices with offset
365
366 @param vm - (vlib_main_t *) vlib main data structure pointer
367 @param b - (void **) array of buffer pointers
368 @param bi - (u32 *) array to store buffer indices
369 @param count - (uword) number of elements
370 @param offset - (i32) offset applied to each pointer
371*/
372static_always_inline void
373vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
374 uword count, i32 offset)
375{
376#ifdef CLIB_HAVE_VEC256
377 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100378 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200379
380 while (count >= 8)
381 {
382 /* load 4 pointers into 256-bit register */
383 u64x4 v0 = u64x4_load_unaligned (b);
384 u64x4 v1 = u64x4_load_unaligned (b + 4);
385 u32x8 v2, v3;
386
387 v0 -= off4;
388 v1 -= off4;
389
390 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
391 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
392
393 /* permute 256-bit register so lower u32s of each buffer index are
394 * placed into lower 128-bits */
395 v2 = u32x8_permute ((u32x8) v0, mask);
396 v3 = u32x8_permute ((u32x8) v1, mask);
397
398 /* extract lower 128-bits and save them to the array of buffer indices */
399 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
400 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
401 bi += 8;
402 b += 8;
403 count -= 8;
404 }
405#endif
406 while (count >= 4)
407 {
408 /* equivalent non-nector implementation */
409 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
410 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
411 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
412 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
413 bi += 4;
414 b += 4;
415 count -= 4;
416 }
417 while (count)
418 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400419 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200420 bi += 1;
421 b += 1;
422 count -= 1;
423 }
424}
425
426/** \brief Translate array of buffer pointers into buffer indices
427
428 @param vm - (vlib_main_t *) vlib main data structure pointer
429 @param b - (vlib_buffer_t **) array of buffer pointers
430 @param bi - (u32 *) array to store buffer indices
431 @param count - (uword) number of elements
432*/
433static_always_inline void
434vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
435 uword count)
436{
437 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
438}
439
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440/** \brief Get next buffer in buffer linklist, or zero for end of list.
441
442 @param vm - (vlib_main_t *) vlib main data structure pointer
443 @param b - (void *) buffer pointer
444 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400445*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446always_inline vlib_buffer_t *
447vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
448{
449 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451}
452
Dave Barach9b8ffd92016-07-08 08:13:45 -0400453uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
454 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
456/** \brief Get length in bytes of the buffer chain
457
458 @param vm - (vlib_main_t *) vlib main data structure pointer
459 @param b - (void *) buffer pointer
460 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462always_inline uword
463vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
464{
Damjan Marion072401e2017-07-13 18:53:27 +0200465 uword len = b->current_length;
466
467 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
468 return len;
469
470 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
471 return len + b->total_length_not_including_first_buffer;
472
473 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474}
475
476/** \brief Get length in bytes of the buffer index buffer chain
477
478 @param vm - (vlib_main_t *) vlib main data structure pointer
479 @param bi - (u32) buffer index
480 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400481*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482always_inline uword
483vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
484{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400485 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486 return vlib_buffer_length_in_chain (vm, b);
487}
488
489/** \brief Copy buffer contents to memory
490
491 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400492 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 @param contents - (u8 *) memory, <strong>must be large enough</strong>
494 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400495*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496always_inline uword
497vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
498{
499 uword content_len = 0;
500 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400501 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502
503 while (1)
504 {
505 b = vlib_get_buffer (vm, buffer_index);
506 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500507 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 break;
511 buffer_index = b->next_buffer;
512 }
513
514 return content_len;
515}
516
Damjan Marion8f499362018-10-22 13:07:02 +0200517always_inline uword
518vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519{
Damjan Marion68b4da62018-09-30 18:26:20 +0200520 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521}
522
Damjan Marion8f499362018-10-22 13:07:02 +0200523always_inline uword
524vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
525{
526 return vlib_buffer_get_pa (vm, b) + b->current_data;
527}
528
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529/** \brief Prefetch buffer metadata by buffer index
530 The first 64 bytes of buffer contains most header information
531
532 @param vm - (vlib_main_t *) vlib main data structure pointer
533 @param bi - (u32) buffer index
534 @param type - LOAD, STORE. In most cases, STORE is the right answer
535*/
536/* Prefetch buffer header given index. */
537#define vlib_prefetch_buffer_with_index(vm,bi,type) \
538 do { \
539 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
540 vlib_prefetch_buffer_header (_b, type); \
541 } while (0)
542
Dave Barach9b8ffd92016-07-08 08:13:45 -0400543typedef enum
544{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 /* Index is unknown. */
546 VLIB_BUFFER_UNKNOWN,
547
548 /* Index is known and free/allocated. */
549 VLIB_BUFFER_KNOWN_FREE,
550 VLIB_BUFFER_KNOWN_ALLOCATED,
551} vlib_buffer_known_state_t;
552
Damjan Marionc8a26c62017-11-24 20:15:23 +0100553void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
554 uword n_buffers,
555 vlib_buffer_known_state_t
556 expected_state);
557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100559vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560{
Damjan Mariond50e3472019-01-20 00:03:56 +0100561 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
Damjan Marion6b0f5892017-07-27 04:01:24 -0400563 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400565 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
567}
568
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569/* Validates sanity of a single buffer.
570 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400571u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
572 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100574u8 *vlib_validate_buffers (vlib_main_t * vm,
575 u32 * buffers,
576 uword next_buffer_stride,
577 uword n_buffers,
578 vlib_buffer_known_state_t known_state,
579 uword follow_buffer_next);
580
Damjan Marion910d3692019-01-21 11:48:34 +0100581static_always_inline vlib_buffer_pool_t *
582vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400583{
Damjan Marion910d3692019-01-21 11:48:34 +0100584 vlib_buffer_main_t *bm = vm->buffer_main;
585 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400586}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Damjan Mariona2185122020-04-08 00:52:53 +0200588static_always_inline __clib_warn_unused_result uword
Damjan Marion910d3692019-01-21 11:48:34 +0100589vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
590 u32 n_buffers)
591{
592 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
593 u32 len;
594
595 ASSERT (bp->buffers);
596
597 clib_spinlock_lock (&bp->lock);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100598 len = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100599 if (PREDICT_TRUE (n_buffers < len))
600 {
601 len -= n_buffers;
602 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100603 bp->n_avail = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100604 clib_spinlock_unlock (&bp->lock);
605 return n_buffers;
606 }
607 else
608 {
609 vlib_buffer_copy_indices (buffers, bp->buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100610 bp->n_avail = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100611 clib_spinlock_unlock (&bp->lock);
612 return len;
613 }
614}
615
616
617/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
619 @param vm - (vlib_main_t *) vlib main data structure pointer
620 @param buffers - (u32 * ) buffer index array
621 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400622 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 less than the number requested or zero
624*/
Damjan Marion910d3692019-01-21 11:48:34 +0100625
Damjan Mariona2185122020-04-08 00:52:53 +0200626always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100627vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
628 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100629{
Damjan Mariond50e3472019-01-20 00:03:56 +0100630 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100631 vlib_buffer_pool_t *bp;
632 vlib_buffer_pool_thread_t *bpt;
633 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100634
Dave Barachc74b43c2020-04-09 17:24:07 -0400635 /* If buffer allocation fault injection is configured */
636 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
637 {
638 u32 vlib_buffer_alloc_may_fail (vlib_main_t *, u32);
639
640 /* See how many buffers we're willing to allocate */
641 n_buffers = vlib_buffer_alloc_may_fail (vm, n_buffers);
642 if (n_buffers == 0)
643 return (n_buffers);
644 }
645
Damjan Marion910d3692019-01-21 11:48:34 +0100646 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
647 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100648
Damjan Marion910d3692019-01-21 11:48:34 +0100649 dst = buffers;
650 n_left = n_buffers;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100651 len = bpt->n_cached;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100652
Damjan Marion910d3692019-01-21 11:48:34 +0100653 /* per-thread cache contains enough buffers */
654 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100655 {
Damjan Marion910d3692019-01-21 11:48:34 +0100656 src = bpt->cached_buffers + len - n_buffers;
657 vlib_buffer_copy_indices (dst, src, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100658 bpt->n_cached -= n_buffers;
659
660 if (CLIB_DEBUG > 0)
661 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
662 VLIB_BUFFER_KNOWN_FREE);
663 return n_buffers;
664 }
665
666 /* alloc bigger than cache - take buffers directly from main pool */
667 if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ)
668 {
669 n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers,
670 n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100671
Damjan Marion910d3692019-01-21 11:48:34 +0100672 if (CLIB_DEBUG > 0)
673 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
674 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100675 return n_buffers;
676 }
677
Damjan Marion910d3692019-01-21 11:48:34 +0100678 /* take everything available in the cache */
679 if (len)
680 {
681 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100682 bpt->n_cached = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100683 dst += len;
684 n_left -= len;
685 }
686
687 len = round_pow2 (n_left, 32);
Damjan Marion910d3692019-01-21 11:48:34 +0100688 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
689 len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100690 bpt->n_cached = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100691
692 if (len)
693 {
694 u32 n_copy = clib_min (len, n_left);
695 src = bpt->cached_buffers + len - n_copy;
696 vlib_buffer_copy_indices (dst, src, n_copy);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100697 bpt->n_cached -= n_copy;
Damjan Marion910d3692019-01-21 11:48:34 +0100698 n_left -= n_copy;
699 }
700
701 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100702
703 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100704 if (CLIB_DEBUG > 0)
705 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
706 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100707
708 return n_buffers;
709}
710
Damjan Marion910d3692019-01-21 11:48:34 +0100711/** \brief Allocate buffers from specific numa node into supplied array
712
713 @param vm - (vlib_main_t *) vlib main data structure pointer
714 @param buffers - (u32 * ) buffer index array
715 @param n_buffers - (u32) number of buffers requested
716 @param numa_node - (u32) numa node
717 @return - (u32) number of buffers actually allocated, may be
718 less than the number requested or zero
719*/
Damjan Mariona2185122020-04-08 00:52:53 +0200720always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100721vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
722 u32 numa_node)
723{
724 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
725 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
726}
727
Damjan Marionc8a26c62017-11-24 20:15:23 +0100728/** \brief Allocate buffers into supplied array
729
730 @param vm - (vlib_main_t *) vlib main data structure pointer
731 @param buffers - (u32 * ) buffer index array
732 @param n_buffers - (u32) number of buffers requested
733 @return - (u32) number of buffers actually allocated, may be
734 less than the number requested or zero
735*/
Damjan Marion910d3692019-01-21 11:48:34 +0100736
Damjan Mariona2185122020-04-08 00:52:53 +0200737always_inline __clib_warn_unused_result u32
Damjan Marionc8a26c62017-11-24 20:15:23 +0100738vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
739{
Damjan Marion910d3692019-01-21 11:48:34 +0100740 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100741}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Damjan Marionc58408c2018-01-18 14:54:04 +0100743/** \brief Allocate buffers into ring
744
745 @param vm - (vlib_main_t *) vlib main data structure pointer
746 @param buffers - (u32 * ) buffer index ring
747 @param start - (u32) first slot in the ring
748 @param ring_size - (u32) ring size
749 @param n_buffers - (u32) number of buffers requested
750 @return - (u32) number of buffers actually allocated, may be
751 less than the number requested or zero
752*/
Damjan Mariona2185122020-04-08 00:52:53 +0200753always_inline __clib_warn_unused_result u32
Damjan Marionc58408c2018-01-18 14:54:04 +0100754vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
755 u32 ring_size, u32 n_buffers)
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 (vm, ring + start, n_buffers);
763
764 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
765
766 if (PREDICT_TRUE (n_alloc == ring_size - start))
767 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
768
769 return n_alloc;
770}
771
Damjan Marion910d3692019-01-21 11:48:34 +0100772/** \brief Allocate buffers into ring from specific buffer pool
773
774 @param vm - (vlib_main_t *) vlib main data structure pointer
775 @param buffers - (u32 * ) buffer index ring
776 @param start - (u32) first slot in the ring
777 @param ring_size - (u32) ring size
778 @param n_buffers - (u32) number of buffers requested
779 @return - (u32) number of buffers actually allocated, may be
780 less than the number requested or zero
781*/
Damjan Mariona2185122020-04-08 00:52:53 +0200782always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100783vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
784 u32 ring_size, u32 n_buffers,
785 u8 buffer_pool_index)
786{
787 u32 n_alloc;
788
789 ASSERT (n_buffers <= ring_size);
790
791 if (PREDICT_TRUE (start + n_buffers <= ring_size))
792 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
793 buffer_pool_index);
794
795 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
796 buffer_pool_index);
797
798 if (PREDICT_TRUE (n_alloc == ring_size - start))
799 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
800 buffer_pool_index);
801
802 return n_alloc;
803}
804
Damjan Marionf646d742019-01-31 18:50:04 +0100805static_always_inline void
Damjan Marion910d3692019-01-21 11:48:34 +0100806vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
807 u32 * buffers, u32 n_buffers)
808{
809 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100810 vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads,
811 vm->thread_index);
812 u32 n_cached, n_empty;
Damjan Marion910d3692019-01-21 11:48:34 +0100813
Damjan Marionf646d742019-01-31 18:50:04 +0100814 if (CLIB_DEBUG > 0)
815 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
816 VLIB_BUFFER_KNOWN_ALLOCATED);
817
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100818 n_cached = bpt->n_cached;
819 n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached;
820 if (n_buffers <= n_empty)
Damjan Marion910d3692019-01-21 11:48:34 +0100821 {
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100822 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
823 buffers, n_buffers);
824 bpt->n_cached = n_cached + n_buffers;
825 return;
Damjan Marion910d3692019-01-21 11:48:34 +0100826 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100827
828 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
829 buffers + n_buffers - n_empty, n_empty);
830 bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ;
831
832 clib_spinlock_lock (&bp->lock);
833 vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers,
834 n_buffers - n_empty);
835 bp->n_avail += n_buffers - n_empty;
836 clib_spinlock_unlock (&bp->lock);
Damjan Marion910d3692019-01-21 11:48:34 +0100837}
838
839static_always_inline void
840vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
841 int maybe_next)
842{
843 const int queue_size = 128;
844 vlib_buffer_pool_t *bp = 0;
845 u8 buffer_pool_index = ~0;
846 u32 n_queue = 0, queue[queue_size + 4];
847 vlib_buffer_t bt = { };
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800848#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100849 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
Radu Nicolaud82349e2021-03-16 12:45:01 +0000850 vlib_buffer_t bpi_vec = {};
Damjan Marion910d3692019-01-21 11:48:34 +0100851 vlib_buffer_t flags_refs_mask = {
852 .flags = VLIB_BUFFER_NEXT_PRESENT,
Damjan Marion0e209242019-03-13 12:04:10 +0100853 .ref_count = ~1
Damjan Marion910d3692019-01-21 11:48:34 +0100854 };
855#endif
856
Radu Nicolaud82349e2021-03-16 12:45:01 +0000857 if (PREDICT_FALSE (n_buffers == 0))
858 return;
859
860 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[0]);
861 buffer_pool_index = b->buffer_pool_index;
862 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
863 vlib_buffer_copy_template (&bt, &bp->buffer_template);
864#if defined(CLIB_HAVE_VEC128)
865 bpi_vec.buffer_pool_index = buffer_pool_index;
866#endif
867
Damjan Marion910d3692019-01-21 11:48:34 +0100868 while (n_buffers)
869 {
870 vlib_buffer_t *b[8];
871 u32 bi, sum = 0, flags, next;
872
Radu Nicolaud82349e2021-03-16 12:45:01 +0000873 if (n_buffers < 4)
Damjan Marion910d3692019-01-21 11:48:34 +0100874 goto one_by_one;
875
876 vlib_get_buffers (vm, buffers, b, 4);
Damjan Marion910d3692019-01-21 11:48:34 +0100877
Radu Nicolaud82349e2021-03-16 12:45:01 +0000878 if (n_buffers >= 12)
879 {
880 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
881 vlib_prefetch_buffer_header (b[4], LOAD);
882 vlib_prefetch_buffer_header (b[5], LOAD);
883 vlib_prefetch_buffer_header (b[6], LOAD);
884 vlib_prefetch_buffer_header (b[7], LOAD);
885 }
Damjan Marion910d3692019-01-21 11:48:34 +0100886
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800887#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100888 u8x16 p0, p1, p2, p3, r;
889 p0 = u8x16_load_unaligned (b[0]);
890 p1 = u8x16_load_unaligned (b[1]);
891 p2 = u8x16_load_unaligned (b[2]);
892 p3 = u8x16_load_unaligned (b[3]);
893
894 r = p0 ^ bpi_vec.as_u8x16[0];
895 r |= p1 ^ bpi_vec.as_u8x16[0];
896 r |= p2 ^ bpi_vec.as_u8x16[0];
897 r |= p3 ^ bpi_vec.as_u8x16[0];
898 r &= bpi_mask.as_u8x16[0];
899 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
900
901 sum = !u8x16_is_all_zero (r);
902#else
903 sum |= b[0]->flags;
904 sum |= b[1]->flags;
905 sum |= b[2]->flags;
906 sum |= b[3]->flags;
907 sum &= VLIB_BUFFER_NEXT_PRESENT;
908 sum += b[0]->ref_count - 1;
909 sum += b[1]->ref_count - 1;
910 sum += b[2]->ref_count - 1;
911 sum += b[3]->ref_count - 1;
912 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
913 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
914 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
915 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
916#endif
917
918 if (sum)
919 goto one_by_one;
920
921 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
922 vlib_buffer_copy_template (b[0], &bt);
923 vlib_buffer_copy_template (b[1], &bt);
924 vlib_buffer_copy_template (b[2], &bt);
925 vlib_buffer_copy_template (b[3], &bt);
926 n_queue += 4;
927
Damjan Marion24dcbe42019-01-31 12:29:39 +0100928 vlib_buffer_validate (vm, b[0]);
929 vlib_buffer_validate (vm, b[1]);
930 vlib_buffer_validate (vm, b[2]);
931 vlib_buffer_validate (vm, b[3]);
932
Damjan Marion910d3692019-01-21 11:48:34 +0100933 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
934 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
935 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
936 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
937
938 if (n_queue >= queue_size)
939 {
940 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
941 n_queue = 0;
942 }
943 buffers += 4;
944 n_buffers -= 4;
945 continue;
946
947 one_by_one:
948 bi = buffers[0];
949
950 next_in_chain:
951 b[0] = vlib_get_buffer (vm, bi);
952 flags = b[0]->flags;
953 next = b[0]->next_buffer;
954
955 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
956 {
Damjan Marion910d3692019-01-21 11:48:34 +0100957
958 if (n_queue)
959 {
960 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
961 n_queue = 0;
962 }
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500963
964 buffer_pool_index = b[0]->buffer_pool_index;
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800965#if defined(CLIB_HAVE_VEC128)
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500966 bpi_vec.buffer_pool_index = buffer_pool_index;
967#endif
968 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
969 vlib_buffer_copy_template (&bt, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100970 }
971
Damjan Marion24dcbe42019-01-31 12:29:39 +0100972 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100973
974 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
975
976 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
977 {
Damjan Marion910d3692019-01-21 11:48:34 +0100978 vlib_buffer_copy_template (b[0], &bt);
979 queue[n_queue++] = bi;
980 }
981
982 if (n_queue == queue_size)
983 {
984 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
985 n_queue = 0;
986 }
987
Damjan Marion5fed42a2019-06-11 20:12:23 +0200988 if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT))
Damjan Marion910d3692019-01-21 11:48:34 +0100989 {
990 bi = next;
991 goto next_in_chain;
992 }
993
994 buffers++;
995 n_buffers--;
996 }
997
998 if (n_queue)
999 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
1000}
1001
1002
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003/** \brief Free buffers
1004 Frees the entire buffer chain for each buffer
1005
1006 @param vm - (vlib_main_t *) vlib main data structure pointer
1007 @param buffers - (u32 * ) buffer index array
1008 @param n_buffers - (u32) number of buffers to free
1009
1010*/
Damjan Marion878c6092017-01-04 13:19:27 +01001011always_inline void
1012vlib_buffer_free (vlib_main_t * vm,
1013 /* pointer to first buffer */
1014 u32 * buffers,
1015 /* number of buffers to free */
1016 u32 n_buffers)
1017{
Damjan Marion910d3692019-01-21 11:48:34 +01001018 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +01001019}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020
1021/** \brief Free buffers, does not free the buffer chain for each buffer
1022
1023 @param vm - (vlib_main_t *) vlib main data structure pointer
1024 @param buffers - (u32 * ) buffer index array
1025 @param n_buffers - (u32) number of buffers to free
1026
1027*/
Damjan Marion878c6092017-01-04 13:19:27 +01001028always_inline void
1029vlib_buffer_free_no_next (vlib_main_t * vm,
1030 /* pointer to first buffer */
1031 u32 * buffers,
1032 /* number of buffers to free */
1033 u32 n_buffers)
1034{
Damjan Marion910d3692019-01-21 11:48:34 +01001035 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +01001036}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037
1038/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -04001039 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
1041 @param vm - (vlib_main_t *) vlib main data structure pointer
1042 @param buffer_index - (u32) buffer index to free
1043*/
1044always_inline void
1045vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
1046{
Damjan Marion910d3692019-01-21 11:48:34 +01001047 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048}
1049
Damjan Mariona3731492018-02-25 22:50:39 +01001050/** \brief Free buffers from ring
1051
1052 @param vm - (vlib_main_t *) vlib main data structure pointer
1053 @param buffers - (u32 * ) buffer index ring
1054 @param start - (u32) first slot in the ring
1055 @param ring_size - (u32) ring size
1056 @param n_buffers - (u32) number of buffers
1057*/
1058always_inline void
1059vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
1060 u32 ring_size, u32 n_buffers)
1061{
1062 ASSERT (n_buffers <= ring_size);
1063
1064 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1065 {
1066 vlib_buffer_free (vm, ring + start, n_buffers);
1067 }
1068 else
1069 {
1070 vlib_buffer_free (vm, ring + start, ring_size - start);
1071 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
1072 }
1073}
1074
Damjan Marioncef1db92018-03-28 18:27:38 +02001075/** \brief Free buffers from ring without freeing tail buffers
1076
1077 @param vm - (vlib_main_t *) vlib main data structure pointer
1078 @param buffers - (u32 * ) buffer index ring
1079 @param start - (u32) first slot in the ring
1080 @param ring_size - (u32) ring size
1081 @param n_buffers - (u32) number of buffers
1082*/
1083always_inline void
1084vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
1085 u32 ring_size, u32 n_buffers)
1086{
1087 ASSERT (n_buffers <= ring_size);
1088
1089 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1090 {
Damjan Marion4a973932018-06-09 19:29:16 +02001091 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +02001092 }
1093 else
1094 {
1095 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
1096 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
1097 }
1098}
Damjan Mariona3731492018-02-25 22:50:39 +01001099
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001101int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
1102 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103
John Lo8ed2d522019-08-07 19:30:29 -04001104/* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */
1105#define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \
1106 (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \
1107 VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL)
1108
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001109/* duplicate all buffers in chain */
1110always_inline vlib_buffer_t *
1111vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
1112{
1113 vlib_buffer_t *s, *d, *fd;
1114 uword n_alloc, n_buffers = 1;
John Lo8ed2d522019-08-07 19:30:29 -04001115 u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001116 int i;
1117
1118 s = b;
1119 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1120 {
1121 n_buffers++;
1122 s = vlib_get_buffer (vm, s->next_buffer);
1123 }
Neale Ranns9d676af2017-03-15 01:28:31 -07001124 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001125
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001126 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -05001127
1128 /* No guarantee that we'll get all the buffers we asked for */
1129 if (PREDICT_FALSE (n_alloc < n_buffers))
1130 {
1131 if (n_alloc > 0)
1132 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -05001133 return 0;
1134 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001135
1136 /* 1st segment */
1137 s = b;
1138 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001139 d->current_data = s->current_data;
1140 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +01001141 d->flags = s->flags & flag_mask;
John Lo66317802019-08-13 18:18:21 -04001142 d->trace_handle = s->trace_handle;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001143 d->total_length_not_including_first_buffer =
1144 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -05001145 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1146 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1147 clib_memcpy_fast (vlib_buffer_get_current (d),
1148 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001149
1150 /* next segments */
1151 for (i = 1; i < n_buffers; i++)
1152 {
1153 /* previous */
1154 d->next_buffer = new_buffers[i];
1155 /* current */
1156 s = vlib_get_buffer (vm, s->next_buffer);
1157 d = vlib_get_buffer (vm, new_buffers[i]);
1158 d->current_data = s->current_data;
1159 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -05001160 clib_memcpy_fast (vlib_buffer_get_current (d),
1161 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +01001162 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001163 }
1164
1165 return fd;
1166}
1167
Ole Troanda7f7b62019-03-11 13:15:54 +01001168/* duplicate first buffer in chain */
1169always_inline vlib_buffer_t *
1170vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di)
1171{
1172 vlib_buffer_t *d;
1173
1174 if ((vlib_buffer_alloc (vm, di, 1)) != 1)
1175 return 0;
1176
1177 d = vlib_get_buffer (vm, *di);
1178 /* 1st segment */
1179 d->current_data = b->current_data;
1180 d->current_length = b->current_length;
1181 clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque));
1182 clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2));
1183 clib_memcpy_fast (vlib_buffer_get_current (d),
1184 vlib_buffer_get_current (b), b->current_length);
1185
1186 return d;
1187}
1188
John Lof545caa2019-04-01 11:30:07 -04001189/* \brief Move packet from current position to offset position in buffer.
1190 Only work for small packet using one buffer with room to fit the move
1191 @param vm - (vlib_main_t *) vlib main data structure pointer
1192 @param b - (vlib_buffer_t *) pointer to buffer
1193 @param offset - (i16) position to move the packet in buffer
1194 */
1195always_inline void
1196vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset)
1197{
1198 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
1199 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1200 ASSERT (offset + b->current_length <
1201 vlib_buffer_get_default_data_size (vm));
1202
1203 u8 *source = vlib_buffer_get_current (b);
1204 b->current_data = offset;
1205 u8 *destination = vlib_buffer_get_current (b);
1206 u16 length = b->current_length;
1207
1208 if (source + length <= destination) /* no overlap */
1209 clib_memcpy_fast (destination, source, length);
1210 else
1211 memmove (destination, source, length);
1212}
1213
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001214/** \brief Create a maximum of 256 clones of buffer and store them
1215 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +01001216
1217 @param vm - (vlib_main_t *) vlib main data structure pointer
1218 @param src_buffer - (u32) source buffer index
1219 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001220 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +01001221 @param head_end_offset - (u16) offset relative to current position
1222 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001223 @param offset - (i16) copy packet head at current position if 0,
1224 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001225 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +01001226 less than the number requested or zero
1227*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001228always_inline u16
1229vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
John Lof545caa2019-04-01 11:30:07 -04001230 u16 n_buffers, u16 head_end_offset, i16 offset)
Damjan Marionc47ed032017-01-25 14:18:03 +01001231{
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001232 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +01001233 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1234
Damjan Marion910d3692019-01-21 11:48:34 +01001235 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001236 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001237 ASSERT (n_buffers <= 256);
John Lof545caa2019-04-01 11:30:07 -04001238 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1239 ASSERT ((offset + head_end_offset) <
1240 vlib_buffer_get_default_data_size (vm));
Damjan Marionc47ed032017-01-25 14:18:03 +01001241
1242 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1243 {
1244 buffers[0] = src_buffer;
John Lof545caa2019-04-01 11:30:07 -04001245 if (offset)
1246 vlib_buffer_move (vm, s, offset);
1247
Damjan Marionc47ed032017-01-25 14:18:03 +01001248 for (i = 1; i < n_buffers; i++)
1249 {
1250 vlib_buffer_t *d;
1251 d = vlib_buffer_copy (vm, s);
1252 if (d == 0)
1253 return i;
1254 buffers[i] = vlib_get_buffer_index (vm, d);
1255
1256 }
1257 return n_buffers;
1258 }
1259
John Lof545caa2019-04-01 11:30:07 -04001260 if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0)))
Damjan Marionc47ed032017-01-25 14:18:03 +01001261 {
1262 buffers[0] = src_buffer;
1263 return 1;
1264 }
1265
Damjan Marion910d3692019-01-21 11:48:34 +01001266 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1267 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001268
Damjan Marionc47ed032017-01-25 14:18:03 +01001269 for (i = 0; i < n_buffers; i++)
1270 {
1271 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
John Lof545caa2019-04-01 11:30:07 -04001272 if (offset)
1273 d->current_data = offset;
1274 else
1275 d->current_data = s->current_data;
1276
Damjan Marionc47ed032017-01-25 14:18:03 +01001277 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001278 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1279
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001280 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001281 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001282 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1283 {
1284 d->total_length_not_including_first_buffer +=
1285 s->total_length_not_including_first_buffer;
1286 }
John Lo8ed2d522019-08-07 19:30:29 -04001287 d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) |
1288 VLIB_BUFFER_NEXT_PRESENT;
John Lo66317802019-08-13 18:18:21 -04001289 d->trace_handle = s->trace_handle;
Dave Barach178cf492018-11-13 16:34:13 -05001290 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1291 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1292 clib_memcpy_fast (vlib_buffer_get_current (d),
1293 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001294 d->next_buffer = src_buffer;
1295 }
1296 vlib_buffer_advance (s, head_end_offset);
Dave Barach95e19252020-04-07 10:52:43 -04001297 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001298 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1299 {
1300 s = vlib_get_buffer (vm, s->next_buffer);
Dave Barach95e19252020-04-07 10:52:43 -04001301 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001302 }
1303
1304 return n_buffers;
1305}
1306
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001307/** \brief Create multiple clones of buffer and store them
1308 in the supplied array
1309
1310 @param vm - (vlib_main_t *) vlib main data structure pointer
1311 @param src_buffer - (u32) source buffer index
1312 @param buffers - (u32 * ) buffer index array
1313 @param n_buffers - (u16) number of buffer clones requested (<=256)
1314 @param head_end_offset - (u16) offset relative to current position
1315 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001316 @param offset - (i16) copy packet head at current position if 0,
1317 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001318 @return - (u16) number of buffers actually cloned, may be
1319 less than the number requested or zero
1320*/
1321always_inline u16
John Lof545caa2019-04-01 11:30:07 -04001322vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1323 u16 n_buffers, u16 head_end_offset, i16 offset)
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001324{
1325 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1326 u16 n_cloned = 0;
1327
1328 while (n_buffers > 256)
1329 {
1330 vlib_buffer_t *copy;
1331 copy = vlib_buffer_copy (vm, s);
1332 n_cloned += vlib_buffer_clone_256 (vm,
1333 vlib_get_buffer_index (vm, copy),
1334 (buffers + n_cloned),
John Lof545caa2019-04-01 11:30:07 -04001335 256, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001336 n_buffers -= 256;
1337 }
1338 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1339 buffers + n_cloned,
John Lof545caa2019-04-01 11:30:07 -04001340 n_buffers, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001341
1342 return n_cloned;
1343}
1344
John Lof545caa2019-04-01 11:30:07 -04001345/** \brief Create multiple clones of buffer and store them
1346 in the supplied array
1347
1348 @param vm - (vlib_main_t *) vlib main data structure pointer
1349 @param src_buffer - (u32) source buffer index
1350 @param buffers - (u32 * ) buffer index array
1351 @param n_buffers - (u16) number of buffer clones requested (<=256)
1352 @param head_end_offset - (u16) offset relative to current position
1353 where packet head ends
1354 @return - (u16) number of buffers actually cloned, may be
1355 less than the number requested or zero
1356*/
1357always_inline u16
1358vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1359 u16 n_buffers, u16 head_end_offset)
1360{
1361 return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers,
1362 head_end_offset, 0);
1363}
1364
Damjan Marionc47ed032017-01-25 14:18:03 +01001365/** \brief Attach cloned tail to the buffer
1366
1367 @param vm - (vlib_main_t *) vlib main data structure pointer
1368 @param head - (vlib_buffer_t *) head buffer
1369 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1370*/
1371
1372always_inline void
1373vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1374 vlib_buffer_t * tail)
1375{
1376 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001377 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001378
1379 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1380 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1381 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1382 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1383 head->next_buffer = vlib_get_buffer_index (vm, tail);
1384 head->total_length_not_including_first_buffer = tail->current_length +
1385 tail->total_length_not_including_first_buffer;
1386
1387next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001388 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001389
1390 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1391 {
1392 tail = vlib_get_buffer (vm, tail->next_buffer);
1393 goto next_segment;
1394 }
1395}
1396
Pierre Pfister328e99b2016-02-12 13:18:42 +00001397/* Initializes the buffer as an empty packet with no chained buffers. */
1398always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001399vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001400{
1401 first->total_length_not_including_first_buffer = 0;
1402 first->current_length = 0;
1403 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1404 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001405}
1406
1407/* The provided next_bi buffer index is appended to the end of the packet. */
1408always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001409vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001410{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001411 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001412 last->next_buffer = next_bi;
1413 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1414 next_buffer->current_length = 0;
1415 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001416 return next_buffer;
1417}
1418
1419/* Increases or decreases the packet length.
1420 * It does not allocate or deallocate new buffers.
1421 * Therefore, the added length must be compatible
1422 * with the last buffer. */
1423always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001424vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1425 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001426{
1427 last->current_length += len;
1428 if (first != last)
1429 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001430}
1431
1432/* Copy data to the end of the packet and increases its length.
1433 * It does not allocate new buffers.
1434 * Returns the number of copied bytes. */
1435always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001436vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001437 vlib_buffer_t * first,
1438 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001439{
Damjan Marion8934a042019-02-09 23:29:26 +01001440 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001441 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1442 u16 len = clib_min (data_len,
1443 n_buffer_bytes - last->current_length -
1444 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001445 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1446 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001447 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001448 return len;
1449}
1450
1451/* Copy data to the end of the packet and increases its length.
1452 * Allocates additional buffers from the free list if necessary.
1453 * Returns the number of copied bytes.
1454 * 'last' value is modified whenever new buffers are allocated and
1455 * chained and points to the last buffer in the chain. */
1456u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001457vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001458 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001459 vlib_buffer_t ** last, void *data,
1460 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001461void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001462
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
Benoît Ganne43543172019-10-21 15:13:54 +02001464 format_vlib_buffer_contents, format_vlib_buffer_no_chain;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465
Dave Barach9b8ffd92016-07-08 08:13:45 -04001466typedef struct
1467{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001470
Damjan Mariond1274cb2018-03-13 21:32:17 +01001471 /* Number of buffers to allocate in each call to allocator. */
1472 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473
Damjan Marion671e60e2018-12-30 18:09:59 +01001474 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001475} vlib_packet_template_t;
1476
Ed Warnickecb9cada2015-12-08 15:45:58 -07001477void vlib_packet_template_init (vlib_main_t * vm,
1478 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001479 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001481 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001482 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001483
Dave Barach9b8ffd92016-07-08 08:13:45 -04001484void *vlib_packet_template_get_packet (vlib_main_t * vm,
1485 vlib_packet_template_t * t,
1486 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487
1488always_inline void
1489vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1490{
1491 vec_free (t->packet_data);
1492}
1493
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001494always_inline u32
1495vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +02001496{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001497 return b->data + vlib_buffer_get_default_data_size (vm) -
1498 ((u8 *) vlib_buffer_get_current (b) + b->current_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001499}
1500
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001501always_inline u32
1502vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b)
Eyal Barid3d42412018-11-05 13:29:25 +02001503{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001504 vlib_buffer_t *db = b, *sb, *first = b;
1505 int is_cloned = 0;
1506 u32 bytes_left = 0, data_size;
1507 u16 src_left, dst_left, n_buffers = 1;
1508 u8 *dp, *sp;
1509 u32 to_free = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001510
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001511 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
1512 return 1;
Eyal Barid3d42412018-11-05 13:29:25 +02001513
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001514 data_size = vlib_buffer_get_default_data_size (vm);
1515
1516 dst_left = vlib_buffer_space_left_at_end (vm, b);
1517
1518 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Eyal Barid3d42412018-11-05 13:29:25 +02001519 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001520 b = vlib_get_buffer (vm, b->next_buffer);
1521 if (b->ref_count > 1)
1522 is_cloned = 1;
1523 bytes_left += b->current_length;
1524 n_buffers++;
Eyal Barid3d42412018-11-05 13:29:25 +02001525 }
1526
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001527 /* if buffer is cloned, create completely new chain - unless everything fits
1528 * into one buffer */
1529 if (is_cloned && bytes_left >= dst_left)
Eyal Barid3d42412018-11-05 13:29:25 +02001530 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001531 u32 len = 0;
1532 u32 space_needed = bytes_left - dst_left;
1533 u32 tail;
1534
1535 if (vlib_buffer_alloc (vm, &tail, 1) == 0)
1536 return 0;
1537
1538 ++n_buffers;
1539 len += data_size;
1540 b = vlib_get_buffer (vm, tail);
1541
1542 while (len < space_needed)
Eyal Barid3d42412018-11-05 13:29:25 +02001543 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001544 u32 bi;
1545 if (vlib_buffer_alloc (vm, &bi, 1) == 0)
1546 {
1547 vlib_buffer_free_one (vm, tail);
1548 return 0;
1549 }
1550 b->flags = VLIB_BUFFER_NEXT_PRESENT;
1551 b->next_buffer = bi;
1552 b = vlib_get_buffer (vm, bi);
1553 len += data_size;
1554 n_buffers++;
1555 }
1556 sb = vlib_get_buffer (vm, first->next_buffer);
1557 to_free = first->next_buffer;
1558 first->next_buffer = tail;
1559 }
1560 else
1561 sb = vlib_get_buffer (vm, first->next_buffer);
1562
1563 src_left = sb->current_length;
1564 sp = vlib_buffer_get_current (sb);
1565 dp = vlib_buffer_get_tail (db);
1566
1567 while (bytes_left)
1568 {
1569 u16 bytes_to_copy;
1570
1571 if (dst_left == 0)
1572 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001573 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1574 ASSERT (db->flags & VLIB_BUFFER_NEXT_PRESENT);
1575 db = vlib_get_buffer (vm, db->next_buffer);
1576 dst_left = data_size;
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001577 if (db->current_data > 0)
1578 {
1579 db->current_data = 0;
1580 }
1581 else
1582 {
1583 dst_left += -db->current_data;
1584 }
1585 dp = vlib_buffer_get_current (db);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001586 }
1587
1588 while (src_left == 0)
1589 {
1590 ASSERT (sb->flags & VLIB_BUFFER_NEXT_PRESENT);
1591 sb = vlib_get_buffer (vm, sb->next_buffer);
1592 src_left = sb->current_length;
1593 sp = vlib_buffer_get_current (sb);
1594 }
1595
1596 bytes_to_copy = clib_min (dst_left, src_left);
1597
1598 if (dp != sp)
1599 {
1600 if (sb == db)
1601 bytes_to_copy = clib_min (bytes_to_copy, sp - dp);
1602
1603 clib_memcpy_fast (dp, sp, bytes_to_copy);
1604 }
1605
1606 src_left -= bytes_to_copy;
1607 dst_left -= bytes_to_copy;
1608 dp += bytes_to_copy;
1609 sp += bytes_to_copy;
1610 bytes_left -= bytes_to_copy;
1611 }
1612 if (db != first)
1613 db->current_data = 0;
1614 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1615
1616 if (is_cloned && to_free)
1617 vlib_buffer_free_one (vm, to_free);
1618 else
1619 {
1620 if (db->flags & VLIB_BUFFER_NEXT_PRESENT)
1621 vlib_buffer_free_one (vm, db->next_buffer);
1622 db->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1623 b = first;
1624 n_buffers = 1;
1625 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1626 {
1627 b = vlib_get_buffer (vm, b->next_buffer);
1628 ++n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001629 }
1630 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001631
1632 first->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1633
1634 return n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001635}
1636
Ed Warnickecb9cada2015-12-08 15:45:58 -07001637#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001638
1639/*
1640 * fd.io coding-style-patch-verification: ON
1641 *
1642 * Local Variables:
1643 * eval: (c-set-style "gnu")
1644 * End:
1645 */