blob: 58abdb18841773750237a3bc15e83a334924137b [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 Marion24dcbe42019-01-31 12:29:39 +010054always_inline void
55vlib_buffer_validate (vlib_main_t * vm, vlib_buffer_t * b)
56{
57 vlib_buffer_main_t *bm = vm->buffer_main;
58 vlib_buffer_pool_t *bp;
59
60 /* reference count in allocated buffer always must be 1 or higher */
61 ASSERT (b->ref_count > 0);
62
Damjan Marion24dcbe42019-01-31 12:29:39 +010063 /* verify that buffer pool index is valid */
64 bp = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index);
65 ASSERT (pointer_to_uword (b) >= bp->start);
66 ASSERT (pointer_to_uword (b) < bp->start + bp->size -
Damjan Marion5de3fec2019-02-06 14:22:32 +010067 (bp->data_size + sizeof (vlib_buffer_t)));
Damjan Marion24dcbe42019-01-31 12:29:39 +010068}
69
70always_inline void *
71vlib_buffer_ptr_from_index (uword buffer_mem_start, u32 buffer_index,
72 uword offset)
73{
74 offset += ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES;
75 return uword_to_pointer (buffer_mem_start + offset, vlib_buffer_t *);
76}
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78/** \brief Translate buffer index into buffer pointer
79
80 @param vm - (vlib_main_t *) vlib main data structure pointer
81 @param buffer_index - (u32) buffer index
82 @return - (vlib_buffer_t *) buffer pointer
Dave Barach9b8ffd92016-07-08 08:13:45 -040083*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070084always_inline vlib_buffer_t *
85vlib_get_buffer (vlib_main_t * vm, u32 buffer_index)
86{
Damjan Mariond50e3472019-01-20 00:03:56 +010087 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion24dcbe42019-01-31 12:29:39 +010088 vlib_buffer_t *b;
Damjan Marion04a7f052017-07-10 15:06:17 +020089
Damjan Marion24dcbe42019-01-31 12:29:39 +010090 b = vlib_buffer_ptr_from_index (bm->buffer_mem_start, buffer_index, 0);
91 vlib_buffer_validate (vm, b);
92 return b;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093}
94
Damjan Marion5de3fec2019-02-06 14:22:32 +010095static_always_inline u32
Damjan Marion8934a042019-02-09 23:29:26 +010096vlib_buffer_get_default_data_size (vlib_main_t * vm)
Damjan Marion5de3fec2019-02-06 14:22:32 +010097{
98 return vm->buffer_main->default_data_size;
99}
100
Damjan Marione58041f2019-01-18 19:56:09 +0100101static_always_inline void
Damjan Marion64d557c2019-01-18 20:03:41 +0100102vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices)
103{
Damjan Marion3e94c412019-03-13 12:41:54 +0100104#if defined(CLIB_HAVE_VEC512)
105 while (n_indices >= 16)
106 {
107 u32x16_store_unaligned (u32x16_load_unaligned (src), dst);
108 dst += 16;
109 src += 16;
110 n_indices -= 16;
111 }
112#endif
113
114#if defined(CLIB_HAVE_VEC256)
115 while (n_indices >= 8)
116 {
117 u32x8_store_unaligned (u32x8_load_unaligned (src), dst);
118 dst += 8;
119 src += 8;
120 n_indices -= 8;
121 }
122#endif
123
124#if defined(CLIB_HAVE_VEC128)
125 while (n_indices >= 4)
126 {
127 u32x4_store_unaligned (u32x4_load_unaligned (src), dst);
128 dst += 4;
129 src += 4;
130 n_indices -= 4;
131 }
132#endif
133
134 while (n_indices)
135 {
136 dst[0] = src[0];
137 dst += 1;
138 src += 1;
139 n_indices -= 1;
140 }
Damjan Marion64d557c2019-01-18 20:03:41 +0100141}
142
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100143STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +0100144static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +0100145vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
146{
Damjan Marion22f23ae2019-01-24 15:36:57 +0100147#if defined CLIB_HAVE_VEC512
148 b->as_u8x64[0] = bt->as_u8x64[0];
149#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100150 b->as_u8x32[0] = bt->as_u8x32[0];
151 b->as_u8x32[1] = bt->as_u8x32[1];
152#elif defined (CLIB_HAVE_VEC128)
153 b->as_u8x16[0] = bt->as_u8x16[0];
154 b->as_u8x16[1] = bt->as_u8x16[1];
155 b->as_u8x16[2] = bt->as_u8x16[2];
156 b->as_u8x16[3] = bt->as_u8x16[3];
157#else
158 clib_memcpy_fast (b, bt, 64);
159#endif
Damjan Marione58041f2019-01-18 19:56:09 +0100160}
161
Damjan Marion910d3692019-01-21 11:48:34 +0100162always_inline u8
163vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
164{
Damjan Marionb592d1b2019-02-28 23:16:11 +0100165 ASSERT (numa_node < VLIB_BUFFER_MAX_NUMA_NODES);
166 return vm->buffer_main->default_buffer_pool_index_for_numa[numa_node];
Damjan Marion910d3692019-01-21 11:48:34 +0100167}
168
Damjan Marionafe56de2018-05-17 12:44:00 +0200169/** \brief Translate array of buffer indices into buffer pointers with offset
170
171 @param vm - (vlib_main_t *) vlib main data structure pointer
172 @param bi - (u32 *) array of buffer indices
173 @param b - (void **) array to store buffer pointers
174 @param count - (uword) number of elements
175 @param offset - (i32) offset applied to each pointer
176*/
177static_always_inline void
178vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count,
179 i32 offset)
180{
Damjan Mariond50e3472019-01-20 00:03:56 +0100181 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marionafe56de2018-05-17 12:44:00 +0200182#ifdef CLIB_HAVE_VEC256
Damjan Mariond50e3472019-01-20 00:03:56 +0100183 u64x4 off = u64x4_splat (buffer_mem_start + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200184 /* if count is not const, compiler will not unroll while loop
185 se we maintain two-in-parallel variant */
186 while (count >= 8)
187 {
188 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
189 u64x4 b1 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi + 4));
190 /* shift and add to get vlib_buffer_t pointer */
191 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
192 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
193 b += 8;
194 bi += 8;
195 count -= 8;
196 }
197#endif
198 while (count >= 4)
199 {
200#ifdef CLIB_HAVE_VEC256
201 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
202 /* shift and add to get vlib_buffer_t pointer */
203 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800204#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100205 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200206 u32x4 bi4 = u32x4_load_unaligned (bi);
207 u64x2 b0 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800208#if defined (__aarch64__)
209 u64x2 b1 = u32x4_extend_to_u64x2_high ((u32x4) bi4);
210#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200211 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
212 u64x2 b1 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800213#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200214 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
215 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200216#else
Damjan Marion24dcbe42019-01-31 12:29:39 +0100217 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
218 b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset);
219 b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset);
220 b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200221#endif
222 b += 4;
223 bi += 4;
224 count -= 4;
225 }
226 while (count)
227 {
Damjan Marion24dcbe42019-01-31 12:29:39 +0100228 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200229 b += 1;
230 bi += 1;
231 count -= 1;
232 }
233}
234
235/** \brief Translate array of buffer indices into buffer pointers
236
237 @param vm - (vlib_main_t *) vlib main data structure pointer
238 @param bi - (u32 *) array of buffer indices
239 @param b - (vlib_buffer_t **) array to store buffer pointers
240 @param count - (uword) number of elements
241*/
242
243static_always_inline void
244vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
245{
246 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
247}
248
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249/** \brief Translate buffer pointer into buffer index
250
251 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400252 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200255
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258{
Damjan Mariond50e3472019-01-20 00:03:56 +0100259 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200260 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
261 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
262 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400263 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
265}
266
Damjan Marionafe56de2018-05-17 12:44:00 +0200267/** \brief Translate array of buffer pointers into buffer indices with offset
268
269 @param vm - (vlib_main_t *) vlib main data structure pointer
270 @param b - (void **) array of buffer pointers
271 @param bi - (u32 *) array to store buffer indices
272 @param count - (uword) number of elements
273 @param offset - (i32) offset applied to each pointer
274*/
275static_always_inline void
276vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
277 uword count, i32 offset)
278{
279#ifdef CLIB_HAVE_VEC256
280 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100281 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200282
283 while (count >= 8)
284 {
285 /* load 4 pointers into 256-bit register */
286 u64x4 v0 = u64x4_load_unaligned (b);
287 u64x4 v1 = u64x4_load_unaligned (b + 4);
288 u32x8 v2, v3;
289
290 v0 -= off4;
291 v1 -= off4;
292
293 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
294 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
295
296 /* permute 256-bit register so lower u32s of each buffer index are
297 * placed into lower 128-bits */
298 v2 = u32x8_permute ((u32x8) v0, mask);
299 v3 = u32x8_permute ((u32x8) v1, mask);
300
301 /* extract lower 128-bits and save them to the array of buffer indices */
302 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
303 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
304 bi += 8;
305 b += 8;
306 count -= 8;
307 }
308#endif
309 while (count >= 4)
310 {
311 /* equivalent non-nector implementation */
312 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
313 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
314 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
315 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
316 bi += 4;
317 b += 4;
318 count -= 4;
319 }
320 while (count)
321 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400322 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200323 bi += 1;
324 b += 1;
325 count -= 1;
326 }
327}
328
329/** \brief Translate array of buffer pointers into buffer indices
330
331 @param vm - (vlib_main_t *) vlib main data structure pointer
332 @param b - (vlib_buffer_t **) array of buffer pointers
333 @param bi - (u32 *) array to store buffer indices
334 @param count - (uword) number of elements
335*/
336static_always_inline void
337vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
338 uword count)
339{
340 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
341}
342
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343/** \brief Get next buffer in buffer linklist, or zero for end of list.
344
345 @param vm - (vlib_main_t *) vlib main data structure pointer
346 @param b - (void *) buffer pointer
347 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400348*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349always_inline vlib_buffer_t *
350vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
351{
352 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354}
355
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
357 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
359/** \brief Get length in bytes of the buffer chain
360
361 @param vm - (vlib_main_t *) vlib main data structure pointer
362 @param b - (void *) buffer pointer
363 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400364*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365always_inline uword
366vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
367{
Damjan Marion072401e2017-07-13 18:53:27 +0200368 uword len = b->current_length;
369
370 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
371 return len;
372
373 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
374 return len + b->total_length_not_including_first_buffer;
375
376 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377}
378
379/** \brief Get length in bytes of the buffer index buffer chain
380
381 @param vm - (vlib_main_t *) vlib main data structure pointer
382 @param bi - (u32) buffer index
383 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400384*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385always_inline uword
386vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
387{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 return vlib_buffer_length_in_chain (vm, b);
390}
391
392/** \brief Copy buffer contents to memory
393
394 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400395 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 @param contents - (u8 *) memory, <strong>must be large enough</strong>
397 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399always_inline uword
400vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
401{
402 uword content_len = 0;
403 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
406 while (1)
407 {
408 b = vlib_get_buffer (vm, buffer_index);
409 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500410 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 break;
414 buffer_index = b->next_buffer;
415 }
416
417 return content_len;
418}
419
Damjan Marion8f499362018-10-22 13:07:02 +0200420always_inline uword
421vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422{
Damjan Marion68b4da62018-09-30 18:26:20 +0200423 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424}
425
Damjan Marion8f499362018-10-22 13:07:02 +0200426always_inline uword
427vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
428{
429 return vlib_buffer_get_pa (vm, b) + b->current_data;
430}
431
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432/** \brief Prefetch buffer metadata by buffer index
433 The first 64 bytes of buffer contains most header information
434
435 @param vm - (vlib_main_t *) vlib main data structure pointer
436 @param bi - (u32) buffer index
437 @param type - LOAD, STORE. In most cases, STORE is the right answer
438*/
439/* Prefetch buffer header given index. */
440#define vlib_prefetch_buffer_with_index(vm,bi,type) \
441 do { \
442 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
443 vlib_prefetch_buffer_header (_b, type); \
444 } while (0)
445
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446typedef enum
447{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 /* Index is unknown. */
449 VLIB_BUFFER_UNKNOWN,
450
451 /* Index is known and free/allocated. */
452 VLIB_BUFFER_KNOWN_FREE,
453 VLIB_BUFFER_KNOWN_ALLOCATED,
454} vlib_buffer_known_state_t;
455
Damjan Marionc8a26c62017-11-24 20:15:23 +0100456void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
457 uword n_buffers,
458 vlib_buffer_known_state_t
459 expected_state);
460
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100462vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463{
Damjan Mariond50e3472019-01-20 00:03:56 +0100464 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Damjan Marion6b0f5892017-07-27 04:01:24 -0400466 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400467 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400468 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
470}
471
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472/* Validates sanity of a single buffer.
473 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
475 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100477u8 *vlib_validate_buffers (vlib_main_t * vm,
478 u32 * buffers,
479 uword next_buffer_stride,
480 uword n_buffers,
481 vlib_buffer_known_state_t known_state,
482 uword follow_buffer_next);
483
Damjan Marion910d3692019-01-21 11:48:34 +0100484static_always_inline vlib_buffer_pool_t *
485vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400486{
Damjan Marion910d3692019-01-21 11:48:34 +0100487 vlib_buffer_main_t *bm = vm->buffer_main;
488 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400489}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490
Damjan Marion910d3692019-01-21 11:48:34 +0100491static_always_inline uword
492vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
493 u32 n_buffers)
494{
495 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
496 u32 len;
497
498 ASSERT (bp->buffers);
499
500 clib_spinlock_lock (&bp->lock);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100501 len = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100502 if (PREDICT_TRUE (n_buffers < len))
503 {
504 len -= n_buffers;
505 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100506 bp->n_avail = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100507 clib_spinlock_unlock (&bp->lock);
508 return n_buffers;
509 }
510 else
511 {
512 vlib_buffer_copy_indices (buffers, bp->buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100513 bp->n_avail = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100514 clib_spinlock_unlock (&bp->lock);
515 return len;
516 }
517}
518
519
520/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
522 @param vm - (vlib_main_t *) vlib main data structure pointer
523 @param buffers - (u32 * ) buffer index array
524 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400525 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 less than the number requested or zero
527*/
Damjan Marion910d3692019-01-21 11:48:34 +0100528
Damjan Marion878c6092017-01-04 13:19:27 +0100529always_inline u32
Damjan Marion910d3692019-01-21 11:48:34 +0100530vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
531 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100532{
Damjan Mariond50e3472019-01-20 00:03:56 +0100533 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100534 vlib_buffer_pool_t *bp;
535 vlib_buffer_pool_thread_t *bpt;
536 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100537
Damjan Marion910d3692019-01-21 11:48:34 +0100538 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
539 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100540
Damjan Marion910d3692019-01-21 11:48:34 +0100541 dst = buffers;
542 n_left = n_buffers;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100543 len = bpt->n_cached;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100544
Damjan Marion910d3692019-01-21 11:48:34 +0100545 /* per-thread cache contains enough buffers */
546 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100547 {
Damjan Marion910d3692019-01-21 11:48:34 +0100548 src = bpt->cached_buffers + len - n_buffers;
549 vlib_buffer_copy_indices (dst, src, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100550 bpt->n_cached -= n_buffers;
551
552 if (CLIB_DEBUG > 0)
553 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
554 VLIB_BUFFER_KNOWN_FREE);
555 return n_buffers;
556 }
557
558 /* alloc bigger than cache - take buffers directly from main pool */
559 if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ)
560 {
561 n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers,
562 n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100563
Damjan Marion910d3692019-01-21 11:48:34 +0100564 if (CLIB_DEBUG > 0)
565 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
566 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100567 return n_buffers;
568 }
569
Damjan Marion910d3692019-01-21 11:48:34 +0100570 /* take everything available in the cache */
571 if (len)
572 {
573 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100574 bpt->n_cached = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100575 dst += len;
576 n_left -= len;
577 }
578
579 len = round_pow2 (n_left, 32);
Damjan Marion910d3692019-01-21 11:48:34 +0100580 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
581 len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100582 bpt->n_cached = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100583
584 if (len)
585 {
586 u32 n_copy = clib_min (len, n_left);
587 src = bpt->cached_buffers + len - n_copy;
588 vlib_buffer_copy_indices (dst, src, n_copy);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100589 bpt->n_cached -= n_copy;
Damjan Marion910d3692019-01-21 11:48:34 +0100590 n_left -= n_copy;
591 }
592
593 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100594
595 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100596 if (CLIB_DEBUG > 0)
597 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
598 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100599
600 return n_buffers;
601}
602
Damjan Marion910d3692019-01-21 11:48:34 +0100603/** \brief Allocate buffers from specific numa node into supplied array
604
605 @param vm - (vlib_main_t *) vlib main data structure pointer
606 @param buffers - (u32 * ) buffer index array
607 @param n_buffers - (u32) number of buffers requested
608 @param numa_node - (u32) numa node
609 @return - (u32) number of buffers actually allocated, may be
610 less than the number requested or zero
611*/
612always_inline u32
613vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
614 u32 numa_node)
615{
616 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
617 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
618}
619
Damjan Marionc8a26c62017-11-24 20:15:23 +0100620/** \brief Allocate buffers into supplied array
621
622 @param vm - (vlib_main_t *) vlib main data structure pointer
623 @param buffers - (u32 * ) buffer index array
624 @param n_buffers - (u32) number of buffers requested
625 @return - (u32) number of buffers actually allocated, may be
626 less than the number requested or zero
627*/
Damjan Marion910d3692019-01-21 11:48:34 +0100628
Damjan Marionc8a26c62017-11-24 20:15:23 +0100629always_inline u32
630vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
631{
Damjan Marion910d3692019-01-21 11:48:34 +0100632 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100633}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
Damjan Marionc58408c2018-01-18 14:54:04 +0100635/** \brief Allocate buffers into ring
636
637 @param vm - (vlib_main_t *) vlib main data structure pointer
638 @param buffers - (u32 * ) buffer index ring
639 @param start - (u32) first slot in the ring
640 @param ring_size - (u32) ring size
641 @param n_buffers - (u32) number of buffers requested
642 @return - (u32) number of buffers actually allocated, may be
643 less than the number requested or zero
644*/
645always_inline u32
646vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
647 u32 ring_size, u32 n_buffers)
648{
649 u32 n_alloc;
650
651 ASSERT (n_buffers <= ring_size);
652
653 if (PREDICT_TRUE (start + n_buffers <= ring_size))
654 return vlib_buffer_alloc (vm, ring + start, n_buffers);
655
656 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
657
658 if (PREDICT_TRUE (n_alloc == ring_size - start))
659 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
660
661 return n_alloc;
662}
663
Damjan Marion910d3692019-01-21 11:48:34 +0100664/** \brief Allocate buffers into ring from specific buffer pool
665
666 @param vm - (vlib_main_t *) vlib main data structure pointer
667 @param buffers - (u32 * ) buffer index ring
668 @param start - (u32) first slot in the ring
669 @param ring_size - (u32) ring size
670 @param n_buffers - (u32) number of buffers requested
671 @return - (u32) number of buffers actually allocated, may be
672 less than the number requested or zero
673*/
674always_inline u32
675vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
676 u32 ring_size, u32 n_buffers,
677 u8 buffer_pool_index)
678{
679 u32 n_alloc;
680
681 ASSERT (n_buffers <= ring_size);
682
683 if (PREDICT_TRUE (start + n_buffers <= ring_size))
684 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
685 buffer_pool_index);
686
687 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
688 buffer_pool_index);
689
690 if (PREDICT_TRUE (n_alloc == ring_size - start))
691 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
692 buffer_pool_index);
693
694 return n_alloc;
695}
696
Damjan Marionf646d742019-01-31 18:50:04 +0100697static_always_inline void
Damjan Marion910d3692019-01-21 11:48:34 +0100698vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
699 u32 * buffers, u32 n_buffers)
700{
701 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100702 vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads,
703 vm->thread_index);
704 u32 n_cached, n_empty;
Damjan Marion910d3692019-01-21 11:48:34 +0100705
Damjan Marionf646d742019-01-31 18:50:04 +0100706 if (CLIB_DEBUG > 0)
707 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
708 VLIB_BUFFER_KNOWN_ALLOCATED);
709
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100710 n_cached = bpt->n_cached;
711 n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached;
712 if (n_buffers <= n_empty)
Damjan Marion910d3692019-01-21 11:48:34 +0100713 {
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100714 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
715 buffers, n_buffers);
716 bpt->n_cached = n_cached + n_buffers;
717 return;
Damjan Marion910d3692019-01-21 11:48:34 +0100718 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100719
720 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
721 buffers + n_buffers - n_empty, n_empty);
722 bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ;
723
724 clib_spinlock_lock (&bp->lock);
725 vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers,
726 n_buffers - n_empty);
727 bp->n_avail += n_buffers - n_empty;
728 clib_spinlock_unlock (&bp->lock);
Damjan Marion910d3692019-01-21 11:48:34 +0100729}
730
731static_always_inline void
732vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
733 int maybe_next)
734{
735 const int queue_size = 128;
736 vlib_buffer_pool_t *bp = 0;
737 u8 buffer_pool_index = ~0;
738 u32 n_queue = 0, queue[queue_size + 4];
739 vlib_buffer_t bt = { };
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800740#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100741 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
742 vlib_buffer_t bpi_vec = {.buffer_pool_index = ~0 };
743 vlib_buffer_t flags_refs_mask = {
744 .flags = VLIB_BUFFER_NEXT_PRESENT,
Damjan Marion0e209242019-03-13 12:04:10 +0100745 .ref_count = ~1
Damjan Marion910d3692019-01-21 11:48:34 +0100746 };
747#endif
748
749 while (n_buffers)
750 {
751 vlib_buffer_t *b[8];
752 u32 bi, sum = 0, flags, next;
753
754 if (n_buffers < 12)
755 goto one_by_one;
756
757 vlib_get_buffers (vm, buffers, b, 4);
758 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
759
760 vlib_prefetch_buffer_header (b[4], LOAD);
761 vlib_prefetch_buffer_header (b[5], LOAD);
762 vlib_prefetch_buffer_header (b[6], LOAD);
763 vlib_prefetch_buffer_header (b[7], LOAD);
764
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800765#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100766 u8x16 p0, p1, p2, p3, r;
767 p0 = u8x16_load_unaligned (b[0]);
768 p1 = u8x16_load_unaligned (b[1]);
769 p2 = u8x16_load_unaligned (b[2]);
770 p3 = u8x16_load_unaligned (b[3]);
771
772 r = p0 ^ bpi_vec.as_u8x16[0];
773 r |= p1 ^ bpi_vec.as_u8x16[0];
774 r |= p2 ^ bpi_vec.as_u8x16[0];
775 r |= p3 ^ bpi_vec.as_u8x16[0];
776 r &= bpi_mask.as_u8x16[0];
777 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
778
779 sum = !u8x16_is_all_zero (r);
780#else
781 sum |= b[0]->flags;
782 sum |= b[1]->flags;
783 sum |= b[2]->flags;
784 sum |= b[3]->flags;
785 sum &= VLIB_BUFFER_NEXT_PRESENT;
786 sum += b[0]->ref_count - 1;
787 sum += b[1]->ref_count - 1;
788 sum += b[2]->ref_count - 1;
789 sum += b[3]->ref_count - 1;
790 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
791 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
792 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
793 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
794#endif
795
796 if (sum)
797 goto one_by_one;
798
799 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
800 vlib_buffer_copy_template (b[0], &bt);
801 vlib_buffer_copy_template (b[1], &bt);
802 vlib_buffer_copy_template (b[2], &bt);
803 vlib_buffer_copy_template (b[3], &bt);
804 n_queue += 4;
805
Damjan Marion24dcbe42019-01-31 12:29:39 +0100806 vlib_buffer_validate (vm, b[0]);
807 vlib_buffer_validate (vm, b[1]);
808 vlib_buffer_validate (vm, b[2]);
809 vlib_buffer_validate (vm, b[3]);
810
Damjan Marion910d3692019-01-21 11:48:34 +0100811 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
812 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
813 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
814 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
815
816 if (n_queue >= queue_size)
817 {
818 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
819 n_queue = 0;
820 }
821 buffers += 4;
822 n_buffers -= 4;
823 continue;
824
825 one_by_one:
826 bi = buffers[0];
827
828 next_in_chain:
829 b[0] = vlib_get_buffer (vm, bi);
830 flags = b[0]->flags;
831 next = b[0]->next_buffer;
832
833 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
834 {
Damjan Marion910d3692019-01-21 11:48:34 +0100835
836 if (n_queue)
837 {
838 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
839 n_queue = 0;
840 }
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500841
842 buffer_pool_index = b[0]->buffer_pool_index;
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800843#if defined(CLIB_HAVE_VEC128)
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500844 bpi_vec.buffer_pool_index = buffer_pool_index;
845#endif
846 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
847 vlib_buffer_copy_template (&bt, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100848 }
849
Damjan Marion24dcbe42019-01-31 12:29:39 +0100850 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100851
852 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
853
854 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
855 {
Damjan Marion910d3692019-01-21 11:48:34 +0100856 vlib_buffer_copy_template (b[0], &bt);
857 queue[n_queue++] = bi;
858 }
859
860 if (n_queue == queue_size)
861 {
862 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
863 n_queue = 0;
864 }
865
Damjan Marion5fed42a2019-06-11 20:12:23 +0200866 if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT))
Damjan Marion910d3692019-01-21 11:48:34 +0100867 {
868 bi = next;
869 goto next_in_chain;
870 }
871
872 buffers++;
873 n_buffers--;
874 }
875
876 if (n_queue)
877 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
878}
879
880
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881/** \brief Free buffers
882 Frees the entire buffer chain for each buffer
883
884 @param vm - (vlib_main_t *) vlib main data structure pointer
885 @param buffers - (u32 * ) buffer index array
886 @param n_buffers - (u32) number of buffers to free
887
888*/
Damjan Marion878c6092017-01-04 13:19:27 +0100889always_inline void
890vlib_buffer_free (vlib_main_t * vm,
891 /* pointer to first buffer */
892 u32 * buffers,
893 /* number of buffers to free */
894 u32 n_buffers)
895{
Damjan Marion910d3692019-01-21 11:48:34 +0100896 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +0100897}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898
899/** \brief Free buffers, does not free the buffer chain for each buffer
900
901 @param vm - (vlib_main_t *) vlib main data structure pointer
902 @param buffers - (u32 * ) buffer index array
903 @param n_buffers - (u32) number of buffers to free
904
905*/
Damjan Marion878c6092017-01-04 13:19:27 +0100906always_inline void
907vlib_buffer_free_no_next (vlib_main_t * vm,
908 /* pointer to first buffer */
909 u32 * buffers,
910 /* number of buffers to free */
911 u32 n_buffers)
912{
Damjan Marion910d3692019-01-21 11:48:34 +0100913 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +0100914}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915
916/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400917 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918
919 @param vm - (vlib_main_t *) vlib main data structure pointer
920 @param buffer_index - (u32) buffer index to free
921*/
922always_inline void
923vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
924{
Damjan Marion910d3692019-01-21 11:48:34 +0100925 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926}
927
Damjan Mariona3731492018-02-25 22:50:39 +0100928/** \brief Free buffers from ring
929
930 @param vm - (vlib_main_t *) vlib main data structure pointer
931 @param buffers - (u32 * ) buffer index ring
932 @param start - (u32) first slot in the ring
933 @param ring_size - (u32) ring size
934 @param n_buffers - (u32) number of buffers
935*/
936always_inline void
937vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
938 u32 ring_size, u32 n_buffers)
939{
940 ASSERT (n_buffers <= ring_size);
941
942 if (PREDICT_TRUE (start + n_buffers <= ring_size))
943 {
944 vlib_buffer_free (vm, ring + start, n_buffers);
945 }
946 else
947 {
948 vlib_buffer_free (vm, ring + start, ring_size - start);
949 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
950 }
951}
952
Damjan Marioncef1db92018-03-28 18:27:38 +0200953/** \brief Free buffers from ring without freeing tail buffers
954
955 @param vm - (vlib_main_t *) vlib main data structure pointer
956 @param buffers - (u32 * ) buffer index ring
957 @param start - (u32) first slot in the ring
958 @param ring_size - (u32) ring size
959 @param n_buffers - (u32) number of buffers
960*/
961always_inline void
962vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
963 u32 ring_size, u32 n_buffers)
964{
965 ASSERT (n_buffers <= ring_size);
966
967 if (PREDICT_TRUE (start + n_buffers <= ring_size))
968 {
Damjan Marion4a973932018-06-09 19:29:16 +0200969 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +0200970 }
971 else
972 {
973 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
974 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
975 }
976}
Damjan Mariona3731492018-02-25 22:50:39 +0100977
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100979int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
980 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981
John Lo8ed2d522019-08-07 19:30:29 -0400982/* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */
983#define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \
984 (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \
985 VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL)
986
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100987/* duplicate all buffers in chain */
988always_inline vlib_buffer_t *
989vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
990{
991 vlib_buffer_t *s, *d, *fd;
992 uword n_alloc, n_buffers = 1;
John Lo8ed2d522019-08-07 19:30:29 -0400993 u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100994 int i;
995
996 s = b;
997 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
998 {
999 n_buffers++;
1000 s = vlib_get_buffer (vm, s->next_buffer);
1001 }
Neale Ranns9d676af2017-03-15 01:28:31 -07001002 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001003
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001004 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -05001005
1006 /* No guarantee that we'll get all the buffers we asked for */
1007 if (PREDICT_FALSE (n_alloc < n_buffers))
1008 {
1009 if (n_alloc > 0)
1010 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -05001011 return 0;
1012 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001013
1014 /* 1st segment */
1015 s = b;
1016 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001017 d->current_data = s->current_data;
1018 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +01001019 d->flags = s->flags & flag_mask;
John Lo66317802019-08-13 18:18:21 -04001020 d->trace_handle = s->trace_handle;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001021 d->total_length_not_including_first_buffer =
1022 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -05001023 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1024 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1025 clib_memcpy_fast (vlib_buffer_get_current (d),
1026 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001027
1028 /* next segments */
1029 for (i = 1; i < n_buffers; i++)
1030 {
1031 /* previous */
1032 d->next_buffer = new_buffers[i];
1033 /* current */
1034 s = vlib_get_buffer (vm, s->next_buffer);
1035 d = vlib_get_buffer (vm, new_buffers[i]);
1036 d->current_data = s->current_data;
1037 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -05001038 clib_memcpy_fast (vlib_buffer_get_current (d),
1039 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +01001040 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001041 }
1042
1043 return fd;
1044}
1045
Ole Troanda7f7b62019-03-11 13:15:54 +01001046/* duplicate first buffer in chain */
1047always_inline vlib_buffer_t *
1048vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di)
1049{
1050 vlib_buffer_t *d;
1051
1052 if ((vlib_buffer_alloc (vm, di, 1)) != 1)
1053 return 0;
1054
1055 d = vlib_get_buffer (vm, *di);
1056 /* 1st segment */
1057 d->current_data = b->current_data;
1058 d->current_length = b->current_length;
1059 clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque));
1060 clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2));
1061 clib_memcpy_fast (vlib_buffer_get_current (d),
1062 vlib_buffer_get_current (b), b->current_length);
1063
1064 return d;
1065}
1066
John Lof545caa2019-04-01 11:30:07 -04001067/* \brief Move packet from current position to offset position in buffer.
1068 Only work for small packet using one buffer with room to fit the move
1069 @param vm - (vlib_main_t *) vlib main data structure pointer
1070 @param b - (vlib_buffer_t *) pointer to buffer
1071 @param offset - (i16) position to move the packet in buffer
1072 */
1073always_inline void
1074vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset)
1075{
1076 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
1077 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1078 ASSERT (offset + b->current_length <
1079 vlib_buffer_get_default_data_size (vm));
1080
1081 u8 *source = vlib_buffer_get_current (b);
1082 b->current_data = offset;
1083 u8 *destination = vlib_buffer_get_current (b);
1084 u16 length = b->current_length;
1085
1086 if (source + length <= destination) /* no overlap */
1087 clib_memcpy_fast (destination, source, length);
1088 else
1089 memmove (destination, source, length);
1090}
1091
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001092/** \brief Create a maximum of 256 clones of buffer and store them
1093 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +01001094
1095 @param vm - (vlib_main_t *) vlib main data structure pointer
1096 @param src_buffer - (u32) source buffer index
1097 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001098 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +01001099 @param head_end_offset - (u16) offset relative to current position
1100 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001101 @param offset - (i16) copy packet head at current position if 0,
1102 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001103 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +01001104 less than the number requested or zero
1105*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001106always_inline u16
1107vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
John Lof545caa2019-04-01 11:30:07 -04001108 u16 n_buffers, u16 head_end_offset, i16 offset)
Damjan Marionc47ed032017-01-25 14:18:03 +01001109{
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001110 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +01001111 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1112
Damjan Marion910d3692019-01-21 11:48:34 +01001113 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001114 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001115 ASSERT (n_buffers <= 256);
John Lof545caa2019-04-01 11:30:07 -04001116 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1117 ASSERT ((offset + head_end_offset) <
1118 vlib_buffer_get_default_data_size (vm));
Damjan Marionc47ed032017-01-25 14:18:03 +01001119
1120 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1121 {
1122 buffers[0] = src_buffer;
John Lof545caa2019-04-01 11:30:07 -04001123 if (offset)
1124 vlib_buffer_move (vm, s, offset);
1125
Damjan Marionc47ed032017-01-25 14:18:03 +01001126 for (i = 1; i < n_buffers; i++)
1127 {
1128 vlib_buffer_t *d;
1129 d = vlib_buffer_copy (vm, s);
1130 if (d == 0)
1131 return i;
1132 buffers[i] = vlib_get_buffer_index (vm, d);
1133
1134 }
1135 return n_buffers;
1136 }
1137
John Lof545caa2019-04-01 11:30:07 -04001138 if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0)))
Damjan Marionc47ed032017-01-25 14:18:03 +01001139 {
1140 buffers[0] = src_buffer;
1141 return 1;
1142 }
1143
Damjan Marion910d3692019-01-21 11:48:34 +01001144 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1145 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001146
Damjan Marionc47ed032017-01-25 14:18:03 +01001147 for (i = 0; i < n_buffers; i++)
1148 {
1149 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
John Lof545caa2019-04-01 11:30:07 -04001150 if (offset)
1151 d->current_data = offset;
1152 else
1153 d->current_data = s->current_data;
1154
Damjan Marionc47ed032017-01-25 14:18:03 +01001155 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001156 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1157
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001158 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001159 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001160 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1161 {
1162 d->total_length_not_including_first_buffer +=
1163 s->total_length_not_including_first_buffer;
1164 }
John Lo8ed2d522019-08-07 19:30:29 -04001165 d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) |
1166 VLIB_BUFFER_NEXT_PRESENT;
John Lo66317802019-08-13 18:18:21 -04001167 d->trace_handle = s->trace_handle;
Dave Barach178cf492018-11-13 16:34:13 -05001168 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1169 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1170 clib_memcpy_fast (vlib_buffer_get_current (d),
1171 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001172 d->next_buffer = src_buffer;
1173 }
1174 vlib_buffer_advance (s, head_end_offset);
Damjan Marion910d3692019-01-21 11:48:34 +01001175 s->ref_count = n_buffers;
Damjan Marionc47ed032017-01-25 14:18:03 +01001176 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1177 {
1178 s = vlib_get_buffer (vm, s->next_buffer);
Damjan Marion910d3692019-01-21 11:48:34 +01001179 s->ref_count = n_buffers;
Damjan Marionc47ed032017-01-25 14:18:03 +01001180 }
1181
1182 return n_buffers;
1183}
1184
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001185/** \brief Create multiple clones of buffer and store them
1186 in the supplied array
1187
1188 @param vm - (vlib_main_t *) vlib main data structure pointer
1189 @param src_buffer - (u32) source buffer index
1190 @param buffers - (u32 * ) buffer index array
1191 @param n_buffers - (u16) number of buffer clones requested (<=256)
1192 @param head_end_offset - (u16) offset relative to current position
1193 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001194 @param offset - (i16) copy packet head at current position if 0,
1195 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001196 @return - (u16) number of buffers actually cloned, may be
1197 less than the number requested or zero
1198*/
1199always_inline u16
John Lof545caa2019-04-01 11:30:07 -04001200vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1201 u16 n_buffers, u16 head_end_offset, i16 offset)
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001202{
1203 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1204 u16 n_cloned = 0;
1205
1206 while (n_buffers > 256)
1207 {
1208 vlib_buffer_t *copy;
1209 copy = vlib_buffer_copy (vm, s);
1210 n_cloned += vlib_buffer_clone_256 (vm,
1211 vlib_get_buffer_index (vm, copy),
1212 (buffers + n_cloned),
John Lof545caa2019-04-01 11:30:07 -04001213 256, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001214 n_buffers -= 256;
1215 }
1216 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1217 buffers + n_cloned,
John Lof545caa2019-04-01 11:30:07 -04001218 n_buffers, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001219
1220 return n_cloned;
1221}
1222
John Lof545caa2019-04-01 11:30:07 -04001223/** \brief Create multiple clones of buffer and store them
1224 in the supplied array
1225
1226 @param vm - (vlib_main_t *) vlib main data structure pointer
1227 @param src_buffer - (u32) source buffer index
1228 @param buffers - (u32 * ) buffer index array
1229 @param n_buffers - (u16) number of buffer clones requested (<=256)
1230 @param head_end_offset - (u16) offset relative to current position
1231 where packet head ends
1232 @return - (u16) number of buffers actually cloned, may be
1233 less than the number requested or zero
1234*/
1235always_inline u16
1236vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1237 u16 n_buffers, u16 head_end_offset)
1238{
1239 return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers,
1240 head_end_offset, 0);
1241}
1242
Damjan Marionc47ed032017-01-25 14:18:03 +01001243/** \brief Attach cloned tail to the buffer
1244
1245 @param vm - (vlib_main_t *) vlib main data structure pointer
1246 @param head - (vlib_buffer_t *) head buffer
1247 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1248*/
1249
1250always_inline void
1251vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1252 vlib_buffer_t * tail)
1253{
1254 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001255 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001256
1257 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1258 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1259 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1260 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1261 head->next_buffer = vlib_get_buffer_index (vm, tail);
1262 head->total_length_not_including_first_buffer = tail->current_length +
1263 tail->total_length_not_including_first_buffer;
1264
1265next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001266 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001267
1268 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1269 {
1270 tail = vlib_get_buffer (vm, tail->next_buffer);
1271 goto next_segment;
1272 }
1273}
1274
Pierre Pfister328e99b2016-02-12 13:18:42 +00001275/* Initializes the buffer as an empty packet with no chained buffers. */
1276always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001277vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001278{
1279 first->total_length_not_including_first_buffer = 0;
1280 first->current_length = 0;
1281 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1282 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001283}
1284
1285/* The provided next_bi buffer index is appended to the end of the packet. */
1286always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001287vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001288{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001289 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001290 last->next_buffer = next_bi;
1291 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1292 next_buffer->current_length = 0;
1293 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001294 return next_buffer;
1295}
1296
1297/* Increases or decreases the packet length.
1298 * It does not allocate or deallocate new buffers.
1299 * Therefore, the added length must be compatible
1300 * with the last buffer. */
1301always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001302vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1303 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001304{
1305 last->current_length += len;
1306 if (first != last)
1307 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001308}
1309
1310/* Copy data to the end of the packet and increases its length.
1311 * It does not allocate new buffers.
1312 * Returns the number of copied bytes. */
1313always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001314vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001315 vlib_buffer_t * first,
1316 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001317{
Damjan Marion8934a042019-02-09 23:29:26 +01001318 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001319 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1320 u16 len = clib_min (data_len,
1321 n_buffer_bytes - last->current_length -
1322 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001323 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1324 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001325 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001326 return len;
1327}
1328
1329/* Copy data to the end of the packet and increases its length.
1330 * Allocates additional buffers from the free list if necessary.
1331 * Returns the number of copied bytes.
1332 * 'last' value is modified whenever new buffers are allocated and
1333 * chained and points to the last buffer in the chain. */
1334u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001335vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001336 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001337 vlib_buffer_t ** last, void *data,
1338 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001339void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001340
Dave Barach9b8ffd92016-07-08 08:13:45 -04001341format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
Benoît Ganne43543172019-10-21 15:13:54 +02001342 format_vlib_buffer_contents, format_vlib_buffer_no_chain;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343
Dave Barach9b8ffd92016-07-08 08:13:45 -04001344typedef struct
1345{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001347 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001348
Damjan Mariond1274cb2018-03-13 21:32:17 +01001349 /* Number of buffers to allocate in each call to allocator. */
1350 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351
Damjan Marion671e60e2018-12-30 18:09:59 +01001352 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001353} vlib_packet_template_t;
1354
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355void vlib_packet_template_init (vlib_main_t * vm,
1356 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001357 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001359 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001360 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361
Dave Barach9b8ffd92016-07-08 08:13:45 -04001362void *vlib_packet_template_get_packet (vlib_main_t * vm,
1363 vlib_packet_template_t * t,
1364 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365
1366always_inline void
1367vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1368{
1369 vec_free (t->packet_data);
1370}
1371
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001372always_inline u32
1373vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +02001374{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001375 return b->data + vlib_buffer_get_default_data_size (vm) -
1376 ((u8 *) vlib_buffer_get_current (b) + b->current_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001377}
1378
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001379always_inline u32
1380vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b)
Eyal Barid3d42412018-11-05 13:29:25 +02001381{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001382 vlib_buffer_t *db = b, *sb, *first = b;
1383 int is_cloned = 0;
1384 u32 bytes_left = 0, data_size;
1385 u16 src_left, dst_left, n_buffers = 1;
1386 u8 *dp, *sp;
1387 u32 to_free = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001388
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001389 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
1390 return 1;
Eyal Barid3d42412018-11-05 13:29:25 +02001391
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001392 data_size = vlib_buffer_get_default_data_size (vm);
1393
1394 dst_left = vlib_buffer_space_left_at_end (vm, b);
1395
1396 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Eyal Barid3d42412018-11-05 13:29:25 +02001397 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001398 b = vlib_get_buffer (vm, b->next_buffer);
1399 if (b->ref_count > 1)
1400 is_cloned = 1;
1401 bytes_left += b->current_length;
1402 n_buffers++;
Eyal Barid3d42412018-11-05 13:29:25 +02001403 }
1404
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001405 /* if buffer is cloned, create completely new chain - unless everything fits
1406 * into one buffer */
1407 if (is_cloned && bytes_left >= dst_left)
Eyal Barid3d42412018-11-05 13:29:25 +02001408 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001409 u32 len = 0;
1410 u32 space_needed = bytes_left - dst_left;
1411 u32 tail;
1412
1413 if (vlib_buffer_alloc (vm, &tail, 1) == 0)
1414 return 0;
1415
1416 ++n_buffers;
1417 len += data_size;
1418 b = vlib_get_buffer (vm, tail);
1419
1420 while (len < space_needed)
Eyal Barid3d42412018-11-05 13:29:25 +02001421 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001422 u32 bi;
1423 if (vlib_buffer_alloc (vm, &bi, 1) == 0)
1424 {
1425 vlib_buffer_free_one (vm, tail);
1426 return 0;
1427 }
1428 b->flags = VLIB_BUFFER_NEXT_PRESENT;
1429 b->next_buffer = bi;
1430 b = vlib_get_buffer (vm, bi);
1431 len += data_size;
1432 n_buffers++;
1433 }
1434 sb = vlib_get_buffer (vm, first->next_buffer);
1435 to_free = first->next_buffer;
1436 first->next_buffer = tail;
1437 }
1438 else
1439 sb = vlib_get_buffer (vm, first->next_buffer);
1440
1441 src_left = sb->current_length;
1442 sp = vlib_buffer_get_current (sb);
1443 dp = vlib_buffer_get_tail (db);
1444
1445 while (bytes_left)
1446 {
1447 u16 bytes_to_copy;
1448
1449 if (dst_left == 0)
1450 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001451 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1452 ASSERT (db->flags & VLIB_BUFFER_NEXT_PRESENT);
1453 db = vlib_get_buffer (vm, db->next_buffer);
1454 dst_left = data_size;
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001455 if (db->current_data > 0)
1456 {
1457 db->current_data = 0;
1458 }
1459 else
1460 {
1461 dst_left += -db->current_data;
1462 }
1463 dp = vlib_buffer_get_current (db);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001464 }
1465
1466 while (src_left == 0)
1467 {
1468 ASSERT (sb->flags & VLIB_BUFFER_NEXT_PRESENT);
1469 sb = vlib_get_buffer (vm, sb->next_buffer);
1470 src_left = sb->current_length;
1471 sp = vlib_buffer_get_current (sb);
1472 }
1473
1474 bytes_to_copy = clib_min (dst_left, src_left);
1475
1476 if (dp != sp)
1477 {
1478 if (sb == db)
1479 bytes_to_copy = clib_min (bytes_to_copy, sp - dp);
1480
1481 clib_memcpy_fast (dp, sp, bytes_to_copy);
1482 }
1483
1484 src_left -= bytes_to_copy;
1485 dst_left -= bytes_to_copy;
1486 dp += bytes_to_copy;
1487 sp += bytes_to_copy;
1488 bytes_left -= bytes_to_copy;
1489 }
1490 if (db != first)
1491 db->current_data = 0;
1492 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1493
1494 if (is_cloned && to_free)
1495 vlib_buffer_free_one (vm, to_free);
1496 else
1497 {
1498 if (db->flags & VLIB_BUFFER_NEXT_PRESENT)
1499 vlib_buffer_free_one (vm, db->next_buffer);
1500 db->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1501 b = first;
1502 n_buffers = 1;
1503 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1504 {
1505 b = vlib_get_buffer (vm, b->next_buffer);
1506 ++n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001507 }
1508 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001509
1510 first->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1511
1512 return n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001513}
1514
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001516
1517/*
1518 * fd.io coding-style-patch-verification: ON
1519 *
1520 * Local Variables:
1521 * eval: (c-set-style "gnu")
1522 * End:
1523 */