blob: e4427d6c3826bccbd997187247f0410cdb8be21d [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 Marionafe56de2018-05-17 12:44:00 +0200238#ifdef CLIB_HAVE_VEC256
Damjan Mariond50e3472019-01-20 00:03:56 +0100239 u64x4 off = u64x4_splat (buffer_mem_start + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200240 /* if count is not const, compiler will not unroll while loop
241 se we maintain two-in-parallel variant */
242 while (count >= 8)
243 {
Damjan Marion90d05bc2020-08-31 17:18:26 +0200244 u64x4 b0 = u64x4_from_u32x4 (u32x4_load_unaligned (bi));
245 u64x4 b1 = u64x4_from_u32x4 (u32x4_load_unaligned (bi + 4));
Damjan Marionafe56de2018-05-17 12:44:00 +0200246 /* shift and add to get vlib_buffer_t pointer */
247 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
248 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
249 b += 8;
250 bi += 8;
251 count -= 8;
252 }
253#endif
254 while (count >= 4)
255 {
256#ifdef CLIB_HAVE_VEC256
Damjan Marion90d05bc2020-08-31 17:18:26 +0200257 u64x4 b0 = u64x4_from_u32x4 (u32x4_load_unaligned (bi));
Damjan Marionafe56de2018-05-17 12:44:00 +0200258 /* shift and add to get vlib_buffer_t pointer */
259 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800260#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100261 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200262 u32x4 bi4 = u32x4_load_unaligned (bi);
Damjan Marion90d05bc2020-08-31 17:18:26 +0200263 u64x2 b0 = u64x2_from_u32x4 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800264#if defined (__aarch64__)
Damjan Marion90d05bc2020-08-31 17:18:26 +0200265 u64x2 b1 = u64x2_from_u32x4_high ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800266#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200267 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
Damjan Marion90d05bc2020-08-31 17:18:26 +0200268 u64x2 b1 = u64x2_from_u32x4 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800269#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200270 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
271 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200272#else
Damjan Marion24dcbe42019-01-31 12:29:39 +0100273 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
274 b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset);
275 b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset);
276 b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200277#endif
278 b += 4;
279 bi += 4;
280 count -= 4;
281 }
282 while (count)
283 {
Damjan Marion24dcbe42019-01-31 12:29:39 +0100284 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200285 b += 1;
286 bi += 1;
287 count -= 1;
288 }
289}
290
291/** \brief Translate array of buffer indices into buffer pointers
292
293 @param vm - (vlib_main_t *) vlib main data structure pointer
294 @param bi - (u32 *) array of buffer indices
295 @param b - (vlib_buffer_t **) array to store buffer pointers
296 @param count - (uword) number of elements
297*/
298
299static_always_inline void
300vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
301{
302 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
303}
304
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305/** \brief Translate buffer pointer into buffer index
306
307 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400308 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400310*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200311
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314{
Damjan Mariond50e3472019-01-20 00:03:56 +0100315 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200316 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
317 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
318 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400319 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
321}
322
Damjan Marionafe56de2018-05-17 12:44:00 +0200323/** \brief Translate array of buffer pointers into buffer indices with offset
324
325 @param vm - (vlib_main_t *) vlib main data structure pointer
326 @param b - (void **) array of buffer pointers
327 @param bi - (u32 *) array to store buffer indices
328 @param count - (uword) number of elements
329 @param offset - (i32) offset applied to each pointer
330*/
331static_always_inline void
332vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
333 uword count, i32 offset)
334{
335#ifdef CLIB_HAVE_VEC256
336 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100337 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200338
339 while (count >= 8)
340 {
341 /* load 4 pointers into 256-bit register */
342 u64x4 v0 = u64x4_load_unaligned (b);
343 u64x4 v1 = u64x4_load_unaligned (b + 4);
344 u32x8 v2, v3;
345
346 v0 -= off4;
347 v1 -= off4;
348
349 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
350 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
351
352 /* permute 256-bit register so lower u32s of each buffer index are
353 * placed into lower 128-bits */
354 v2 = u32x8_permute ((u32x8) v0, mask);
355 v3 = u32x8_permute ((u32x8) v1, mask);
356
357 /* extract lower 128-bits and save them to the array of buffer indices */
358 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
359 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
360 bi += 8;
361 b += 8;
362 count -= 8;
363 }
364#endif
365 while (count >= 4)
366 {
367 /* equivalent non-nector implementation */
368 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
369 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
370 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
371 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
372 bi += 4;
373 b += 4;
374 count -= 4;
375 }
376 while (count)
377 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400378 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200379 bi += 1;
380 b += 1;
381 count -= 1;
382 }
383}
384
385/** \brief Translate array of buffer pointers into buffer indices
386
387 @param vm - (vlib_main_t *) vlib main data structure pointer
388 @param b - (vlib_buffer_t **) array of buffer pointers
389 @param bi - (u32 *) array to store buffer indices
390 @param count - (uword) number of elements
391*/
392static_always_inline void
393vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
394 uword count)
395{
396 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
397}
398
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399/** \brief Get next buffer in buffer linklist, or zero for end of list.
400
401 @param vm - (vlib_main_t *) vlib main data structure pointer
402 @param b - (void *) buffer pointer
403 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405always_inline vlib_buffer_t *
406vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
407{
408 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400409 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410}
411
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
413 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
415/** \brief Get length in bytes of the buffer chain
416
417 @param vm - (vlib_main_t *) vlib main data structure pointer
418 @param b - (void *) buffer pointer
419 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421always_inline uword
422vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
423{
Damjan Marion072401e2017-07-13 18:53:27 +0200424 uword len = b->current_length;
425
426 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
427 return len;
428
429 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
430 return len + b->total_length_not_including_first_buffer;
431
432 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433}
434
435/** \brief Get length in bytes of the buffer index buffer chain
436
437 @param vm - (vlib_main_t *) vlib main data structure pointer
438 @param bi - (u32) buffer index
439 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441always_inline uword
442vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
443{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 return vlib_buffer_length_in_chain (vm, b);
446}
447
448/** \brief Copy buffer contents to memory
449
450 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400451 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 @param contents - (u8 *) memory, <strong>must be large enough</strong>
453 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455always_inline uword
456vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
457{
458 uword content_len = 0;
459 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
462 while (1)
463 {
464 b = vlib_get_buffer (vm, buffer_index);
465 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500466 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400468 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469 break;
470 buffer_index = b->next_buffer;
471 }
472
473 return content_len;
474}
475
Damjan Marion8f499362018-10-22 13:07:02 +0200476always_inline uword
477vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478{
Damjan Marion68b4da62018-09-30 18:26:20 +0200479 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480}
481
Damjan Marion8f499362018-10-22 13:07:02 +0200482always_inline uword
483vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
484{
485 return vlib_buffer_get_pa (vm, b) + b->current_data;
486}
487
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488/** \brief Prefetch buffer metadata by buffer index
489 The first 64 bytes of buffer contains most header information
490
491 @param vm - (vlib_main_t *) vlib main data structure pointer
492 @param bi - (u32) buffer index
493 @param type - LOAD, STORE. In most cases, STORE is the right answer
494*/
495/* Prefetch buffer header given index. */
496#define vlib_prefetch_buffer_with_index(vm,bi,type) \
497 do { \
498 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
499 vlib_prefetch_buffer_header (_b, type); \
500 } while (0)
501
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502typedef enum
503{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 /* Index is unknown. */
505 VLIB_BUFFER_UNKNOWN,
506
507 /* Index is known and free/allocated. */
508 VLIB_BUFFER_KNOWN_FREE,
509 VLIB_BUFFER_KNOWN_ALLOCATED,
510} vlib_buffer_known_state_t;
511
Damjan Marionc8a26c62017-11-24 20:15:23 +0100512void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
513 uword n_buffers,
514 vlib_buffer_known_state_t
515 expected_state);
516
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100518vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519{
Damjan Mariond50e3472019-01-20 00:03:56 +0100520 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
Damjan Marion6b0f5892017-07-27 04:01:24 -0400522 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400523 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400524 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
526}
527
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528/* Validates sanity of a single buffer.
529 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
531 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100533u8 *vlib_validate_buffers (vlib_main_t * vm,
534 u32 * buffers,
535 uword next_buffer_stride,
536 uword n_buffers,
537 vlib_buffer_known_state_t known_state,
538 uword follow_buffer_next);
539
Damjan Marion910d3692019-01-21 11:48:34 +0100540static_always_inline vlib_buffer_pool_t *
541vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542{
Damjan Marion910d3692019-01-21 11:48:34 +0100543 vlib_buffer_main_t *bm = vm->buffer_main;
544 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400545}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Damjan Mariona2185122020-04-08 00:52:53 +0200547static_always_inline __clib_warn_unused_result uword
Damjan Marion910d3692019-01-21 11:48:34 +0100548vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
549 u32 n_buffers)
550{
551 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
552 u32 len;
553
554 ASSERT (bp->buffers);
555
556 clib_spinlock_lock (&bp->lock);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100557 len = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100558 if (PREDICT_TRUE (n_buffers < len))
559 {
560 len -= n_buffers;
561 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100562 bp->n_avail = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100563 clib_spinlock_unlock (&bp->lock);
564 return n_buffers;
565 }
566 else
567 {
568 vlib_buffer_copy_indices (buffers, bp->buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100569 bp->n_avail = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100570 clib_spinlock_unlock (&bp->lock);
571 return len;
572 }
573}
574
575
576/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
578 @param vm - (vlib_main_t *) vlib main data structure pointer
579 @param buffers - (u32 * ) buffer index array
580 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400581 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 less than the number requested or zero
583*/
Damjan Marion910d3692019-01-21 11:48:34 +0100584
Damjan Mariona2185122020-04-08 00:52:53 +0200585always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100586vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
587 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100588{
Damjan Mariond50e3472019-01-20 00:03:56 +0100589 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100590 vlib_buffer_pool_t *bp;
591 vlib_buffer_pool_thread_t *bpt;
592 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100593
Dave Barachc74b43c2020-04-09 17:24:07 -0400594 /* If buffer allocation fault injection is configured */
595 if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
596 {
597 u32 vlib_buffer_alloc_may_fail (vlib_main_t *, u32);
598
599 /* See how many buffers we're willing to allocate */
600 n_buffers = vlib_buffer_alloc_may_fail (vm, n_buffers);
601 if (n_buffers == 0)
602 return (n_buffers);
603 }
604
Damjan Marion910d3692019-01-21 11:48:34 +0100605 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
606 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100607
Damjan Marion910d3692019-01-21 11:48:34 +0100608 dst = buffers;
609 n_left = n_buffers;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100610 len = bpt->n_cached;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100611
Damjan Marion910d3692019-01-21 11:48:34 +0100612 /* per-thread cache contains enough buffers */
613 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100614 {
Damjan Marion910d3692019-01-21 11:48:34 +0100615 src = bpt->cached_buffers + len - n_buffers;
616 vlib_buffer_copy_indices (dst, src, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100617 bpt->n_cached -= n_buffers;
618
619 if (CLIB_DEBUG > 0)
620 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
621 VLIB_BUFFER_KNOWN_FREE);
622 return n_buffers;
623 }
624
625 /* alloc bigger than cache - take buffers directly from main pool */
626 if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ)
627 {
628 n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers,
629 n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100630
Damjan Marion910d3692019-01-21 11:48:34 +0100631 if (CLIB_DEBUG > 0)
632 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
633 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100634 return n_buffers;
635 }
636
Damjan Marion910d3692019-01-21 11:48:34 +0100637 /* take everything available in the cache */
638 if (len)
639 {
640 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100641 bpt->n_cached = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100642 dst += len;
643 n_left -= len;
644 }
645
646 len = round_pow2 (n_left, 32);
Damjan Marion910d3692019-01-21 11:48:34 +0100647 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
648 len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100649 bpt->n_cached = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100650
651 if (len)
652 {
653 u32 n_copy = clib_min (len, n_left);
654 src = bpt->cached_buffers + len - n_copy;
655 vlib_buffer_copy_indices (dst, src, n_copy);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100656 bpt->n_cached -= n_copy;
Damjan Marion910d3692019-01-21 11:48:34 +0100657 n_left -= n_copy;
658 }
659
660 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100661
662 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100663 if (CLIB_DEBUG > 0)
664 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
665 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100666
667 return n_buffers;
668}
669
Damjan Marion910d3692019-01-21 11:48:34 +0100670/** \brief Allocate buffers from specific numa node into supplied array
671
672 @param vm - (vlib_main_t *) vlib main data structure pointer
673 @param buffers - (u32 * ) buffer index array
674 @param n_buffers - (u32) number of buffers requested
675 @param numa_node - (u32) numa node
676 @return - (u32) number of buffers actually allocated, may be
677 less than the number requested or zero
678*/
Damjan Mariona2185122020-04-08 00:52:53 +0200679always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100680vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
681 u32 numa_node)
682{
683 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
684 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
685}
686
Damjan Marionc8a26c62017-11-24 20:15:23 +0100687/** \brief Allocate buffers into supplied array
688
689 @param vm - (vlib_main_t *) vlib main data structure pointer
690 @param buffers - (u32 * ) buffer index array
691 @param n_buffers - (u32) number of buffers requested
692 @return - (u32) number of buffers actually allocated, may be
693 less than the number requested or zero
694*/
Damjan Marion910d3692019-01-21 11:48:34 +0100695
Damjan Mariona2185122020-04-08 00:52:53 +0200696always_inline __clib_warn_unused_result u32
Damjan Marionc8a26c62017-11-24 20:15:23 +0100697vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
698{
Damjan Marion910d3692019-01-21 11:48:34 +0100699 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100700}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701
Damjan Marionc58408c2018-01-18 14:54:04 +0100702/** \brief Allocate buffers into ring
703
704 @param vm - (vlib_main_t *) vlib main data structure pointer
705 @param buffers - (u32 * ) buffer index ring
706 @param start - (u32) first slot in the ring
707 @param ring_size - (u32) ring size
708 @param n_buffers - (u32) number of buffers requested
709 @return - (u32) number of buffers actually allocated, may be
710 less than the number requested or zero
711*/
Damjan Mariona2185122020-04-08 00:52:53 +0200712always_inline __clib_warn_unused_result u32
Damjan Marionc58408c2018-01-18 14:54:04 +0100713vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
714 u32 ring_size, u32 n_buffers)
715{
716 u32 n_alloc;
717
718 ASSERT (n_buffers <= ring_size);
719
720 if (PREDICT_TRUE (start + n_buffers <= ring_size))
721 return vlib_buffer_alloc (vm, ring + start, n_buffers);
722
723 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
724
725 if (PREDICT_TRUE (n_alloc == ring_size - start))
726 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
727
728 return n_alloc;
729}
730
Damjan Marion910d3692019-01-21 11:48:34 +0100731/** \brief Allocate buffers into ring from specific buffer pool
732
733 @param vm - (vlib_main_t *) vlib main data structure pointer
734 @param buffers - (u32 * ) buffer index ring
735 @param start - (u32) first slot in the ring
736 @param ring_size - (u32) ring size
737 @param n_buffers - (u32) number of buffers requested
738 @return - (u32) number of buffers actually allocated, may be
739 less than the number requested or zero
740*/
Damjan Mariona2185122020-04-08 00:52:53 +0200741always_inline __clib_warn_unused_result u32
Damjan Marion910d3692019-01-21 11:48:34 +0100742vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
743 u32 ring_size, u32 n_buffers,
744 u8 buffer_pool_index)
745{
746 u32 n_alloc;
747
748 ASSERT (n_buffers <= ring_size);
749
750 if (PREDICT_TRUE (start + n_buffers <= ring_size))
751 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
752 buffer_pool_index);
753
754 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
755 buffer_pool_index);
756
757 if (PREDICT_TRUE (n_alloc == ring_size - start))
758 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
759 buffer_pool_index);
760
761 return n_alloc;
762}
763
Damjan Marionf646d742019-01-31 18:50:04 +0100764static_always_inline void
Damjan Marion910d3692019-01-21 11:48:34 +0100765vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
766 u32 * buffers, u32 n_buffers)
767{
768 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100769 vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads,
770 vm->thread_index);
771 u32 n_cached, n_empty;
Damjan Marion910d3692019-01-21 11:48:34 +0100772
Damjan Marionf646d742019-01-31 18:50:04 +0100773 if (CLIB_DEBUG > 0)
774 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
775 VLIB_BUFFER_KNOWN_ALLOCATED);
776
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100777 n_cached = bpt->n_cached;
778 n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached;
779 if (n_buffers <= n_empty)
Damjan Marion910d3692019-01-21 11:48:34 +0100780 {
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100781 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
782 buffers, n_buffers);
783 bpt->n_cached = n_cached + n_buffers;
784 return;
Damjan Marion910d3692019-01-21 11:48:34 +0100785 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100786
787 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
788 buffers + n_buffers - n_empty, n_empty);
789 bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ;
790
791 clib_spinlock_lock (&bp->lock);
792 vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers,
793 n_buffers - n_empty);
794 bp->n_avail += n_buffers - n_empty;
795 clib_spinlock_unlock (&bp->lock);
Damjan Marion910d3692019-01-21 11:48:34 +0100796}
797
798static_always_inline void
799vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
800 int maybe_next)
801{
802 const int queue_size = 128;
803 vlib_buffer_pool_t *bp = 0;
804 u8 buffer_pool_index = ~0;
805 u32 n_queue = 0, queue[queue_size + 4];
806 vlib_buffer_t bt = { };
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800807#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100808 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
Radu Nicolaud82349e2021-03-16 12:45:01 +0000809 vlib_buffer_t bpi_vec = {};
Damjan Marion910d3692019-01-21 11:48:34 +0100810 vlib_buffer_t flags_refs_mask = {
811 .flags = VLIB_BUFFER_NEXT_PRESENT,
Damjan Marion0e209242019-03-13 12:04:10 +0100812 .ref_count = ~1
Damjan Marion910d3692019-01-21 11:48:34 +0100813 };
814#endif
815
Radu Nicolaud82349e2021-03-16 12:45:01 +0000816 if (PREDICT_FALSE (n_buffers == 0))
817 return;
818
819 vlib_buffer_t *b = vlib_get_buffer (vm, buffers[0]);
820 buffer_pool_index = b->buffer_pool_index;
821 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
822 vlib_buffer_copy_template (&bt, &bp->buffer_template);
823#if defined(CLIB_HAVE_VEC128)
824 bpi_vec.buffer_pool_index = buffer_pool_index;
825#endif
826
Damjan Marion910d3692019-01-21 11:48:34 +0100827 while (n_buffers)
828 {
829 vlib_buffer_t *b[8];
830 u32 bi, sum = 0, flags, next;
831
Radu Nicolaud82349e2021-03-16 12:45:01 +0000832 if (n_buffers < 4)
Damjan Marion910d3692019-01-21 11:48:34 +0100833 goto one_by_one;
834
835 vlib_get_buffers (vm, buffers, b, 4);
Damjan Marion910d3692019-01-21 11:48:34 +0100836
Radu Nicolaud82349e2021-03-16 12:45:01 +0000837 if (n_buffers >= 12)
838 {
839 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
840 vlib_prefetch_buffer_header (b[4], LOAD);
841 vlib_prefetch_buffer_header (b[5], LOAD);
842 vlib_prefetch_buffer_header (b[6], LOAD);
843 vlib_prefetch_buffer_header (b[7], LOAD);
844 }
Damjan Marion910d3692019-01-21 11:48:34 +0100845
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800846#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100847 u8x16 p0, p1, p2, p3, r;
848 p0 = u8x16_load_unaligned (b[0]);
849 p1 = u8x16_load_unaligned (b[1]);
850 p2 = u8x16_load_unaligned (b[2]);
851 p3 = u8x16_load_unaligned (b[3]);
852
853 r = p0 ^ bpi_vec.as_u8x16[0];
854 r |= p1 ^ bpi_vec.as_u8x16[0];
855 r |= p2 ^ bpi_vec.as_u8x16[0];
856 r |= p3 ^ bpi_vec.as_u8x16[0];
857 r &= bpi_mask.as_u8x16[0];
858 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
859
860 sum = !u8x16_is_all_zero (r);
861#else
862 sum |= b[0]->flags;
863 sum |= b[1]->flags;
864 sum |= b[2]->flags;
865 sum |= b[3]->flags;
866 sum &= VLIB_BUFFER_NEXT_PRESENT;
867 sum += b[0]->ref_count - 1;
868 sum += b[1]->ref_count - 1;
869 sum += b[2]->ref_count - 1;
870 sum += b[3]->ref_count - 1;
871 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
872 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
873 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
874 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
875#endif
876
877 if (sum)
878 goto one_by_one;
879
880 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
881 vlib_buffer_copy_template (b[0], &bt);
882 vlib_buffer_copy_template (b[1], &bt);
883 vlib_buffer_copy_template (b[2], &bt);
884 vlib_buffer_copy_template (b[3], &bt);
885 n_queue += 4;
886
Damjan Marion24dcbe42019-01-31 12:29:39 +0100887 vlib_buffer_validate (vm, b[0]);
888 vlib_buffer_validate (vm, b[1]);
889 vlib_buffer_validate (vm, b[2]);
890 vlib_buffer_validate (vm, b[3]);
891
Damjan Marion910d3692019-01-21 11:48:34 +0100892 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
893 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
894 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
895 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
896
897 if (n_queue >= queue_size)
898 {
899 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
900 n_queue = 0;
901 }
902 buffers += 4;
903 n_buffers -= 4;
904 continue;
905
906 one_by_one:
907 bi = buffers[0];
908
909 next_in_chain:
910 b[0] = vlib_get_buffer (vm, bi);
911 flags = b[0]->flags;
912 next = b[0]->next_buffer;
913
914 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
915 {
Damjan Marion910d3692019-01-21 11:48:34 +0100916
917 if (n_queue)
918 {
919 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
920 n_queue = 0;
921 }
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500922
923 buffer_pool_index = b[0]->buffer_pool_index;
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800924#if defined(CLIB_HAVE_VEC128)
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500925 bpi_vec.buffer_pool_index = buffer_pool_index;
926#endif
927 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
928 vlib_buffer_copy_template (&bt, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100929 }
930
Damjan Marion24dcbe42019-01-31 12:29:39 +0100931 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100932
933 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
934
935 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
936 {
Damjan Marion910d3692019-01-21 11:48:34 +0100937 vlib_buffer_copy_template (b[0], &bt);
938 queue[n_queue++] = bi;
939 }
940
941 if (n_queue == queue_size)
942 {
943 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
944 n_queue = 0;
945 }
946
Damjan Marion5fed42a2019-06-11 20:12:23 +0200947 if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT))
Damjan Marion910d3692019-01-21 11:48:34 +0100948 {
949 bi = next;
950 goto next_in_chain;
951 }
952
953 buffers++;
954 n_buffers--;
955 }
956
957 if (n_queue)
958 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
959}
960
961
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962/** \brief Free buffers
963 Frees the entire buffer chain for each buffer
964
965 @param vm - (vlib_main_t *) vlib main data structure pointer
966 @param buffers - (u32 * ) buffer index array
967 @param n_buffers - (u32) number of buffers to free
968
969*/
Damjan Marion878c6092017-01-04 13:19:27 +0100970always_inline void
971vlib_buffer_free (vlib_main_t * vm,
972 /* pointer to first buffer */
973 u32 * buffers,
974 /* number of buffers to free */
975 u32 n_buffers)
976{
Damjan Marion910d3692019-01-21 11:48:34 +0100977 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +0100978}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
980/** \brief Free buffers, does not free the buffer chain for each buffer
981
982 @param vm - (vlib_main_t *) vlib main data structure pointer
983 @param buffers - (u32 * ) buffer index array
984 @param n_buffers - (u32) number of buffers to free
985
986*/
Damjan Marion878c6092017-01-04 13:19:27 +0100987always_inline void
988vlib_buffer_free_no_next (vlib_main_t * vm,
989 /* pointer to first buffer */
990 u32 * buffers,
991 /* number of buffers to free */
992 u32 n_buffers)
993{
Damjan Marion910d3692019-01-21 11:48:34 +0100994 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +0100995}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996
997/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400998 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999
1000 @param vm - (vlib_main_t *) vlib main data structure pointer
1001 @param buffer_index - (u32) buffer index to free
1002*/
1003always_inline void
1004vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
1005{
Damjan Marion910d3692019-01-21 11:48:34 +01001006 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007}
1008
Damjan Mariona3731492018-02-25 22:50:39 +01001009/** \brief Free buffers from ring
1010
1011 @param vm - (vlib_main_t *) vlib main data structure pointer
1012 @param buffers - (u32 * ) buffer index ring
1013 @param start - (u32) first slot in the ring
1014 @param ring_size - (u32) ring size
1015 @param n_buffers - (u32) number of buffers
1016*/
1017always_inline void
1018vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
1019 u32 ring_size, u32 n_buffers)
1020{
1021 ASSERT (n_buffers <= ring_size);
1022
1023 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1024 {
1025 vlib_buffer_free (vm, ring + start, n_buffers);
1026 }
1027 else
1028 {
1029 vlib_buffer_free (vm, ring + start, ring_size - start);
1030 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
1031 }
1032}
1033
Damjan Marioncef1db92018-03-28 18:27:38 +02001034/** \brief Free buffers from ring without freeing tail buffers
1035
1036 @param vm - (vlib_main_t *) vlib main data structure pointer
1037 @param buffers - (u32 * ) buffer index ring
1038 @param start - (u32) first slot in the ring
1039 @param ring_size - (u32) ring size
1040 @param n_buffers - (u32) number of buffers
1041*/
1042always_inline void
1043vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
1044 u32 ring_size, u32 n_buffers)
1045{
1046 ASSERT (n_buffers <= ring_size);
1047
1048 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1049 {
Damjan Marion4a973932018-06-09 19:29:16 +02001050 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +02001051 }
1052 else
1053 {
1054 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
1055 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
1056 }
1057}
Damjan Mariona3731492018-02-25 22:50:39 +01001058
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001060int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
1061 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062
John Lo8ed2d522019-08-07 19:30:29 -04001063/* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */
1064#define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \
1065 (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \
1066 VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL)
1067
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001068/* duplicate all buffers in chain */
1069always_inline vlib_buffer_t *
1070vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
1071{
1072 vlib_buffer_t *s, *d, *fd;
1073 uword n_alloc, n_buffers = 1;
John Lo8ed2d522019-08-07 19:30:29 -04001074 u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001075 int i;
1076
1077 s = b;
1078 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1079 {
1080 n_buffers++;
1081 s = vlib_get_buffer (vm, s->next_buffer);
1082 }
Neale Ranns9d676af2017-03-15 01:28:31 -07001083 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001084
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001085 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -05001086
1087 /* No guarantee that we'll get all the buffers we asked for */
1088 if (PREDICT_FALSE (n_alloc < n_buffers))
1089 {
1090 if (n_alloc > 0)
1091 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -05001092 return 0;
1093 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001094
1095 /* 1st segment */
1096 s = b;
1097 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001098 d->current_data = s->current_data;
1099 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +01001100 d->flags = s->flags & flag_mask;
John Lo66317802019-08-13 18:18:21 -04001101 d->trace_handle = s->trace_handle;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001102 d->total_length_not_including_first_buffer =
1103 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -05001104 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1105 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1106 clib_memcpy_fast (vlib_buffer_get_current (d),
1107 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001108
1109 /* next segments */
1110 for (i = 1; i < n_buffers; i++)
1111 {
1112 /* previous */
1113 d->next_buffer = new_buffers[i];
1114 /* current */
1115 s = vlib_get_buffer (vm, s->next_buffer);
1116 d = vlib_get_buffer (vm, new_buffers[i]);
1117 d->current_data = s->current_data;
1118 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -05001119 clib_memcpy_fast (vlib_buffer_get_current (d),
1120 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +01001121 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001122 }
1123
1124 return fd;
1125}
1126
Ole Troanda7f7b62019-03-11 13:15:54 +01001127/* duplicate first buffer in chain */
1128always_inline vlib_buffer_t *
1129vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di)
1130{
1131 vlib_buffer_t *d;
1132
1133 if ((vlib_buffer_alloc (vm, di, 1)) != 1)
1134 return 0;
1135
1136 d = vlib_get_buffer (vm, *di);
1137 /* 1st segment */
1138 d->current_data = b->current_data;
1139 d->current_length = b->current_length;
1140 clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque));
1141 clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2));
1142 clib_memcpy_fast (vlib_buffer_get_current (d),
1143 vlib_buffer_get_current (b), b->current_length);
1144
1145 return d;
1146}
1147
John Lof545caa2019-04-01 11:30:07 -04001148/* \brief Move packet from current position to offset position in buffer.
1149 Only work for small packet using one buffer with room to fit the move
1150 @param vm - (vlib_main_t *) vlib main data structure pointer
1151 @param b - (vlib_buffer_t *) pointer to buffer
1152 @param offset - (i16) position to move the packet in buffer
1153 */
1154always_inline void
1155vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset)
1156{
1157 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
1158 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1159 ASSERT (offset + b->current_length <
1160 vlib_buffer_get_default_data_size (vm));
1161
1162 u8 *source = vlib_buffer_get_current (b);
1163 b->current_data = offset;
1164 u8 *destination = vlib_buffer_get_current (b);
1165 u16 length = b->current_length;
1166
1167 if (source + length <= destination) /* no overlap */
1168 clib_memcpy_fast (destination, source, length);
1169 else
1170 memmove (destination, source, length);
1171}
1172
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001173/** \brief Create a maximum of 256 clones of buffer and store them
1174 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +01001175
1176 @param vm - (vlib_main_t *) vlib main data structure pointer
1177 @param src_buffer - (u32) source buffer index
1178 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001179 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +01001180 @param head_end_offset - (u16) offset relative to current position
1181 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001182 @param offset - (i16) copy packet head at current position if 0,
1183 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001184 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +01001185 less than the number requested or zero
1186*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001187always_inline u16
1188vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
John Lof545caa2019-04-01 11:30:07 -04001189 u16 n_buffers, u16 head_end_offset, i16 offset)
Damjan Marionc47ed032017-01-25 14:18:03 +01001190{
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001191 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +01001192 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1193
Damjan Marion910d3692019-01-21 11:48:34 +01001194 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001195 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001196 ASSERT (n_buffers <= 256);
John Lof545caa2019-04-01 11:30:07 -04001197 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1198 ASSERT ((offset + head_end_offset) <
1199 vlib_buffer_get_default_data_size (vm));
Damjan Marionc47ed032017-01-25 14:18:03 +01001200
1201 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1202 {
1203 buffers[0] = src_buffer;
John Lof545caa2019-04-01 11:30:07 -04001204 if (offset)
1205 vlib_buffer_move (vm, s, offset);
1206
Damjan Marionc47ed032017-01-25 14:18:03 +01001207 for (i = 1; i < n_buffers; i++)
1208 {
1209 vlib_buffer_t *d;
1210 d = vlib_buffer_copy (vm, s);
1211 if (d == 0)
1212 return i;
1213 buffers[i] = vlib_get_buffer_index (vm, d);
1214
1215 }
1216 return n_buffers;
1217 }
1218
John Lof545caa2019-04-01 11:30:07 -04001219 if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0)))
Damjan Marionc47ed032017-01-25 14:18:03 +01001220 {
1221 buffers[0] = src_buffer;
1222 return 1;
1223 }
1224
Damjan Marion910d3692019-01-21 11:48:34 +01001225 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1226 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001227
Damjan Marionc47ed032017-01-25 14:18:03 +01001228 for (i = 0; i < n_buffers; i++)
1229 {
1230 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
John Lof545caa2019-04-01 11:30:07 -04001231 if (offset)
1232 d->current_data = offset;
1233 else
1234 d->current_data = s->current_data;
1235
Damjan Marionc47ed032017-01-25 14:18:03 +01001236 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001237 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1238
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001239 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001240 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001241 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1242 {
1243 d->total_length_not_including_first_buffer +=
1244 s->total_length_not_including_first_buffer;
1245 }
John Lo8ed2d522019-08-07 19:30:29 -04001246 d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) |
1247 VLIB_BUFFER_NEXT_PRESENT;
John Lo66317802019-08-13 18:18:21 -04001248 d->trace_handle = s->trace_handle;
Dave Barach178cf492018-11-13 16:34:13 -05001249 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1250 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1251 clib_memcpy_fast (vlib_buffer_get_current (d),
1252 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001253 d->next_buffer = src_buffer;
1254 }
1255 vlib_buffer_advance (s, head_end_offset);
Dave Barach95e19252020-04-07 10:52:43 -04001256 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001257 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1258 {
1259 s = vlib_get_buffer (vm, s->next_buffer);
Dave Barach95e19252020-04-07 10:52:43 -04001260 s->ref_count = n_buffers ? n_buffers : s->ref_count;
Damjan Marionc47ed032017-01-25 14:18:03 +01001261 }
1262
1263 return n_buffers;
1264}
1265
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001266/** \brief Create multiple clones of buffer and store them
1267 in the supplied array
1268
1269 @param vm - (vlib_main_t *) vlib main data structure pointer
1270 @param src_buffer - (u32) source buffer index
1271 @param buffers - (u32 * ) buffer index array
1272 @param n_buffers - (u16) number of buffer clones requested (<=256)
1273 @param head_end_offset - (u16) offset relative to current position
1274 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001275 @param offset - (i16) copy packet head at current position if 0,
1276 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001277 @return - (u16) number of buffers actually cloned, may be
1278 less than the number requested or zero
1279*/
1280always_inline u16
John Lof545caa2019-04-01 11:30:07 -04001281vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1282 u16 n_buffers, u16 head_end_offset, i16 offset)
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001283{
1284 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1285 u16 n_cloned = 0;
1286
1287 while (n_buffers > 256)
1288 {
1289 vlib_buffer_t *copy;
1290 copy = vlib_buffer_copy (vm, s);
1291 n_cloned += vlib_buffer_clone_256 (vm,
1292 vlib_get_buffer_index (vm, copy),
1293 (buffers + n_cloned),
John Lof545caa2019-04-01 11:30:07 -04001294 256, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001295 n_buffers -= 256;
1296 }
1297 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1298 buffers + n_cloned,
John Lof545caa2019-04-01 11:30:07 -04001299 n_buffers, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001300
1301 return n_cloned;
1302}
1303
John Lof545caa2019-04-01 11:30:07 -04001304/** \brief Create multiple clones of buffer and store them
1305 in the supplied array
1306
1307 @param vm - (vlib_main_t *) vlib main data structure pointer
1308 @param src_buffer - (u32) source buffer index
1309 @param buffers - (u32 * ) buffer index array
1310 @param n_buffers - (u16) number of buffer clones requested (<=256)
1311 @param head_end_offset - (u16) offset relative to current position
1312 where packet head ends
1313 @return - (u16) number of buffers actually cloned, may be
1314 less than the number requested or zero
1315*/
1316always_inline u16
1317vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1318 u16 n_buffers, u16 head_end_offset)
1319{
1320 return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers,
1321 head_end_offset, 0);
1322}
1323
Damjan Marionc47ed032017-01-25 14:18:03 +01001324/** \brief Attach cloned tail to the buffer
1325
1326 @param vm - (vlib_main_t *) vlib main data structure pointer
1327 @param head - (vlib_buffer_t *) head buffer
1328 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1329*/
1330
1331always_inline void
1332vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1333 vlib_buffer_t * tail)
1334{
1335 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001336 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001337
1338 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1339 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1340 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1341 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1342 head->next_buffer = vlib_get_buffer_index (vm, tail);
1343 head->total_length_not_including_first_buffer = tail->current_length +
1344 tail->total_length_not_including_first_buffer;
1345
1346next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001347 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001348
1349 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1350 {
1351 tail = vlib_get_buffer (vm, tail->next_buffer);
1352 goto next_segment;
1353 }
1354}
1355
Pierre Pfister328e99b2016-02-12 13:18:42 +00001356/* Initializes the buffer as an empty packet with no chained buffers. */
1357always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001358vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001359{
1360 first->total_length_not_including_first_buffer = 0;
1361 first->current_length = 0;
1362 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1363 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001364}
1365
1366/* The provided next_bi buffer index is appended to the end of the packet. */
1367always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001368vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001369{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001370 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001371 last->next_buffer = next_bi;
1372 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1373 next_buffer->current_length = 0;
1374 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001375 return next_buffer;
1376}
1377
1378/* Increases or decreases the packet length.
1379 * It does not allocate or deallocate new buffers.
1380 * Therefore, the added length must be compatible
1381 * with the last buffer. */
1382always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001383vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1384 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001385{
1386 last->current_length += len;
1387 if (first != last)
1388 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001389}
1390
1391/* Copy data to the end of the packet and increases its length.
1392 * It does not allocate new buffers.
1393 * Returns the number of copied bytes. */
1394always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001395vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001396 vlib_buffer_t * first,
1397 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001398{
Damjan Marion8934a042019-02-09 23:29:26 +01001399 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001400 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1401 u16 len = clib_min (data_len,
1402 n_buffer_bytes - last->current_length -
1403 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001404 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1405 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001406 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001407 return len;
1408}
1409
1410/* Copy data to the end of the packet and increases its length.
1411 * Allocates additional buffers from the free list if necessary.
1412 * Returns the number of copied bytes.
1413 * 'last' value is modified whenever new buffers are allocated and
1414 * chained and points to the last buffer in the chain. */
1415u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001416vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001417 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001418 vlib_buffer_t ** last, void *data,
1419 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001420void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001421
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
Benoît Ganne43543172019-10-21 15:13:54 +02001423 format_vlib_buffer_contents, format_vlib_buffer_no_chain;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424
Dave Barach9b8ffd92016-07-08 08:13:45 -04001425typedef struct
1426{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001427 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001428 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001429
Damjan Mariond1274cb2018-03-13 21:32:17 +01001430 /* Number of buffers to allocate in each call to allocator. */
1431 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
Damjan Marion671e60e2018-12-30 18:09:59 +01001433 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001434} vlib_packet_template_t;
1435
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436void vlib_packet_template_init (vlib_main_t * vm,
1437 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001440 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001441 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442
Dave Barach9b8ffd92016-07-08 08:13:45 -04001443void *vlib_packet_template_get_packet (vlib_main_t * vm,
1444 vlib_packet_template_t * t,
1445 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
1447always_inline void
1448vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1449{
1450 vec_free (t->packet_data);
1451}
1452
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001453always_inline u32
1454vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +02001455{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001456 return b->data + vlib_buffer_get_default_data_size (vm) -
1457 ((u8 *) vlib_buffer_get_current (b) + b->current_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001458}
1459
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001460always_inline u32
1461vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b)
Eyal Barid3d42412018-11-05 13:29:25 +02001462{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001463 vlib_buffer_t *db = b, *sb, *first = b;
1464 int is_cloned = 0;
1465 u32 bytes_left = 0, data_size;
1466 u16 src_left, dst_left, n_buffers = 1;
1467 u8 *dp, *sp;
1468 u32 to_free = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001469
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001470 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
1471 return 1;
Eyal Barid3d42412018-11-05 13:29:25 +02001472
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001473 data_size = vlib_buffer_get_default_data_size (vm);
1474
1475 dst_left = vlib_buffer_space_left_at_end (vm, b);
1476
1477 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Eyal Barid3d42412018-11-05 13:29:25 +02001478 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001479 b = vlib_get_buffer (vm, b->next_buffer);
1480 if (b->ref_count > 1)
1481 is_cloned = 1;
1482 bytes_left += b->current_length;
1483 n_buffers++;
Eyal Barid3d42412018-11-05 13:29:25 +02001484 }
1485
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001486 /* if buffer is cloned, create completely new chain - unless everything fits
1487 * into one buffer */
1488 if (is_cloned && bytes_left >= dst_left)
Eyal Barid3d42412018-11-05 13:29:25 +02001489 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001490 u32 len = 0;
1491 u32 space_needed = bytes_left - dst_left;
1492 u32 tail;
1493
1494 if (vlib_buffer_alloc (vm, &tail, 1) == 0)
1495 return 0;
1496
1497 ++n_buffers;
1498 len += data_size;
1499 b = vlib_get_buffer (vm, tail);
1500
1501 while (len < space_needed)
Eyal Barid3d42412018-11-05 13:29:25 +02001502 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001503 u32 bi;
1504 if (vlib_buffer_alloc (vm, &bi, 1) == 0)
1505 {
1506 vlib_buffer_free_one (vm, tail);
1507 return 0;
1508 }
1509 b->flags = VLIB_BUFFER_NEXT_PRESENT;
1510 b->next_buffer = bi;
1511 b = vlib_get_buffer (vm, bi);
1512 len += data_size;
1513 n_buffers++;
1514 }
1515 sb = vlib_get_buffer (vm, first->next_buffer);
1516 to_free = first->next_buffer;
1517 first->next_buffer = tail;
1518 }
1519 else
1520 sb = vlib_get_buffer (vm, first->next_buffer);
1521
1522 src_left = sb->current_length;
1523 sp = vlib_buffer_get_current (sb);
1524 dp = vlib_buffer_get_tail (db);
1525
1526 while (bytes_left)
1527 {
1528 u16 bytes_to_copy;
1529
1530 if (dst_left == 0)
1531 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001532 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1533 ASSERT (db->flags & VLIB_BUFFER_NEXT_PRESENT);
1534 db = vlib_get_buffer (vm, db->next_buffer);
1535 dst_left = data_size;
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001536 if (db->current_data > 0)
1537 {
1538 db->current_data = 0;
1539 }
1540 else
1541 {
1542 dst_left += -db->current_data;
1543 }
1544 dp = vlib_buffer_get_current (db);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001545 }
1546
1547 while (src_left == 0)
1548 {
1549 ASSERT (sb->flags & VLIB_BUFFER_NEXT_PRESENT);
1550 sb = vlib_get_buffer (vm, sb->next_buffer);
1551 src_left = sb->current_length;
1552 sp = vlib_buffer_get_current (sb);
1553 }
1554
1555 bytes_to_copy = clib_min (dst_left, src_left);
1556
1557 if (dp != sp)
1558 {
1559 if (sb == db)
1560 bytes_to_copy = clib_min (bytes_to_copy, sp - dp);
1561
1562 clib_memcpy_fast (dp, sp, bytes_to_copy);
1563 }
1564
1565 src_left -= bytes_to_copy;
1566 dst_left -= bytes_to_copy;
1567 dp += bytes_to_copy;
1568 sp += bytes_to_copy;
1569 bytes_left -= bytes_to_copy;
1570 }
1571 if (db != first)
1572 db->current_data = 0;
1573 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1574
1575 if (is_cloned && to_free)
1576 vlib_buffer_free_one (vm, to_free);
1577 else
1578 {
1579 if (db->flags & VLIB_BUFFER_NEXT_PRESENT)
1580 vlib_buffer_free_one (vm, db->next_buffer);
1581 db->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1582 b = first;
1583 n_buffers = 1;
1584 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1585 {
1586 b = vlib_get_buffer (vm, b->next_buffer);
1587 ++n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001588 }
1589 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001590
1591 first->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1592
1593 return n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001594}
1595
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001597
1598/*
1599 * fd.io coding-style-patch-verification: ON
1600 *
1601 * Local Variables:
1602 * eval: (c-set-style "gnu")
1603 * End:
1604 */