blob: 7103227a9655f290b95c77f13af1c4d23b0bd326 [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 Marionb9250a92020-03-02 19:02:15 +0100143always_inline void
144vlib_buffer_copy_indices_from_ring (u32 * dst, u32 * ring, u32 start,
145 u32 ring_size, u32 n_buffers)
146{
147 ASSERT (n_buffers <= ring_size);
148
149 if (PREDICT_TRUE (start + n_buffers <= ring_size))
150 {
151 vlib_buffer_copy_indices (dst, ring + start, n_buffers);
152 }
153 else
154 {
155 u32 n = ring_size - start;
156 vlib_buffer_copy_indices (dst, ring + start, n);
157 vlib_buffer_copy_indices (dst + n, ring, n_buffers - n);
158 }
159}
160
Benoît Ganne72f49212020-03-19 11:41:07 +0100161always_inline void
162vlib_buffer_copy_indices_to_ring (u32 * ring, u32 * src, u32 start,
163 u32 ring_size, u32 n_buffers)
164{
165 ASSERT (n_buffers <= ring_size);
166
167 if (PREDICT_TRUE (start + n_buffers <= ring_size))
168 {
169 vlib_buffer_copy_indices (ring + start, src, n_buffers);
170 }
171 else
172 {
173 u32 n = ring_size - start;
174 vlib_buffer_copy_indices (ring + start, src, n);
175 vlib_buffer_copy_indices (ring, src + n, n_buffers - n);
176 }
177}
178
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100179STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +0100180static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +0100181vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
182{
Damjan Marion22f23ae2019-01-24 15:36:57 +0100183#if defined CLIB_HAVE_VEC512
184 b->as_u8x64[0] = bt->as_u8x64[0];
185#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100186 b->as_u8x32[0] = bt->as_u8x32[0];
187 b->as_u8x32[1] = bt->as_u8x32[1];
188#elif defined (CLIB_HAVE_VEC128)
189 b->as_u8x16[0] = bt->as_u8x16[0];
190 b->as_u8x16[1] = bt->as_u8x16[1];
191 b->as_u8x16[2] = bt->as_u8x16[2];
192 b->as_u8x16[3] = bt->as_u8x16[3];
193#else
194 clib_memcpy_fast (b, bt, 64);
195#endif
Damjan Marione58041f2019-01-18 19:56:09 +0100196}
197
Damjan Marion910d3692019-01-21 11:48:34 +0100198always_inline u8
199vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
200{
Damjan Marionb592d1b2019-02-28 23:16:11 +0100201 ASSERT (numa_node < VLIB_BUFFER_MAX_NUMA_NODES);
202 return vm->buffer_main->default_buffer_pool_index_for_numa[numa_node];
Damjan Marion910d3692019-01-21 11:48:34 +0100203}
204
Damjan Marionafe56de2018-05-17 12:44:00 +0200205/** \brief Translate array of buffer indices into buffer pointers with offset
206
207 @param vm - (vlib_main_t *) vlib main data structure pointer
208 @param bi - (u32 *) array of buffer indices
209 @param b - (void **) array to store buffer pointers
210 @param count - (uword) number of elements
211 @param offset - (i32) offset applied to each pointer
212*/
213static_always_inline void
214vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count,
215 i32 offset)
216{
Damjan Mariond50e3472019-01-20 00:03:56 +0100217 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marionafe56de2018-05-17 12:44:00 +0200218#ifdef CLIB_HAVE_VEC256
Damjan Mariond50e3472019-01-20 00:03:56 +0100219 u64x4 off = u64x4_splat (buffer_mem_start + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200220 /* if count is not const, compiler will not unroll while loop
221 se we maintain two-in-parallel variant */
222 while (count >= 8)
223 {
224 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
225 u64x4 b1 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi + 4));
226 /* shift and add to get vlib_buffer_t pointer */
227 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
228 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
229 b += 8;
230 bi += 8;
231 count -= 8;
232 }
233#endif
234 while (count >= 4)
235 {
236#ifdef CLIB_HAVE_VEC256
237 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
238 /* shift and add to get vlib_buffer_t pointer */
239 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800240#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100241 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200242 u32x4 bi4 = u32x4_load_unaligned (bi);
243 u64x2 b0 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800244#if defined (__aarch64__)
245 u64x2 b1 = u32x4_extend_to_u64x2_high ((u32x4) bi4);
246#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200247 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
248 u64x2 b1 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800249#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200250 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
251 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200252#else
Damjan Marion24dcbe42019-01-31 12:29:39 +0100253 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
254 b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset);
255 b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset);
256 b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200257#endif
258 b += 4;
259 bi += 4;
260 count -= 4;
261 }
262 while (count)
263 {
Damjan Marion24dcbe42019-01-31 12:29:39 +0100264 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200265 b += 1;
266 bi += 1;
267 count -= 1;
268 }
269}
270
271/** \brief Translate array of buffer indices into buffer pointers
272
273 @param vm - (vlib_main_t *) vlib main data structure pointer
274 @param bi - (u32 *) array of buffer indices
275 @param b - (vlib_buffer_t **) array to store buffer pointers
276 @param count - (uword) number of elements
277*/
278
279static_always_inline void
280vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
281{
282 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
283}
284
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285/** \brief Translate buffer pointer into buffer index
286
287 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400288 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400290*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200291
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400293vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294{
Damjan Mariond50e3472019-01-20 00:03:56 +0100295 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200296 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
297 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
298 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400299 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
301}
302
Damjan Marionafe56de2018-05-17 12:44:00 +0200303/** \brief Translate array of buffer pointers into buffer indices with offset
304
305 @param vm - (vlib_main_t *) vlib main data structure pointer
306 @param b - (void **) array of buffer pointers
307 @param bi - (u32 *) array to store buffer indices
308 @param count - (uword) number of elements
309 @param offset - (i32) offset applied to each pointer
310*/
311static_always_inline void
312vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
313 uword count, i32 offset)
314{
315#ifdef CLIB_HAVE_VEC256
316 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100317 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200318
319 while (count >= 8)
320 {
321 /* load 4 pointers into 256-bit register */
322 u64x4 v0 = u64x4_load_unaligned (b);
323 u64x4 v1 = u64x4_load_unaligned (b + 4);
324 u32x8 v2, v3;
325
326 v0 -= off4;
327 v1 -= off4;
328
329 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
330 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
331
332 /* permute 256-bit register so lower u32s of each buffer index are
333 * placed into lower 128-bits */
334 v2 = u32x8_permute ((u32x8) v0, mask);
335 v3 = u32x8_permute ((u32x8) v1, mask);
336
337 /* extract lower 128-bits and save them to the array of buffer indices */
338 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
339 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
340 bi += 8;
341 b += 8;
342 count -= 8;
343 }
344#endif
345 while (count >= 4)
346 {
347 /* equivalent non-nector implementation */
348 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
349 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
350 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
351 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
352 bi += 4;
353 b += 4;
354 count -= 4;
355 }
356 while (count)
357 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400358 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200359 bi += 1;
360 b += 1;
361 count -= 1;
362 }
363}
364
365/** \brief Translate array of buffer pointers into buffer indices
366
367 @param vm - (vlib_main_t *) vlib main data structure pointer
368 @param b - (vlib_buffer_t **) array of buffer pointers
369 @param bi - (u32 *) array to store buffer indices
370 @param count - (uword) number of elements
371*/
372static_always_inline void
373vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
374 uword count)
375{
376 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
377}
378
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379/** \brief Get next buffer in buffer linklist, or zero for end of list.
380
381 @param vm - (vlib_main_t *) vlib main data structure pointer
382 @param b - (void *) buffer pointer
383 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400384*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385always_inline vlib_buffer_t *
386vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
387{
388 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400389 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390}
391
Dave Barach9b8ffd92016-07-08 08:13:45 -0400392uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
393 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
395/** \brief Get length in bytes of the buffer chain
396
397 @param vm - (vlib_main_t *) vlib main data structure pointer
398 @param b - (void *) buffer pointer
399 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400400*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401always_inline uword
402vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
403{
Damjan Marion072401e2017-07-13 18:53:27 +0200404 uword len = b->current_length;
405
406 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
407 return len;
408
409 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
410 return len + b->total_length_not_including_first_buffer;
411
412 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413}
414
415/** \brief Get length in bytes of the buffer index buffer chain
416
417 @param vm - (vlib_main_t *) vlib main data structure pointer
418 @param bi - (u32) buffer index
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_index_length_in_chain (vlib_main_t * vm, u32 bi)
423{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400424 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 return vlib_buffer_length_in_chain (vm, b);
426}
427
428/** \brief Copy buffer contents to memory
429
430 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400431 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 @param contents - (u8 *) memory, <strong>must be large enough</strong>
433 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400434*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435always_inline uword
436vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
437{
438 uword content_len = 0;
439 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
442 while (1)
443 {
444 b = vlib_get_buffer (vm, buffer_index);
445 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500446 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 break;
450 buffer_index = b->next_buffer;
451 }
452
453 return content_len;
454}
455
Damjan Marion8f499362018-10-22 13:07:02 +0200456always_inline uword
457vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458{
Damjan Marion68b4da62018-09-30 18:26:20 +0200459 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460}
461
Damjan Marion8f499362018-10-22 13:07:02 +0200462always_inline uword
463vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
464{
465 return vlib_buffer_get_pa (vm, b) + b->current_data;
466}
467
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468/** \brief Prefetch buffer metadata by buffer index
469 The first 64 bytes of buffer contains most header information
470
471 @param vm - (vlib_main_t *) vlib main data structure pointer
472 @param bi - (u32) buffer index
473 @param type - LOAD, STORE. In most cases, STORE is the right answer
474*/
475/* Prefetch buffer header given index. */
476#define vlib_prefetch_buffer_with_index(vm,bi,type) \
477 do { \
478 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
479 vlib_prefetch_buffer_header (_b, type); \
480 } while (0)
481
Dave Barach9b8ffd92016-07-08 08:13:45 -0400482typedef enum
483{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 /* Index is unknown. */
485 VLIB_BUFFER_UNKNOWN,
486
487 /* Index is known and free/allocated. */
488 VLIB_BUFFER_KNOWN_FREE,
489 VLIB_BUFFER_KNOWN_ALLOCATED,
490} vlib_buffer_known_state_t;
491
Damjan Marionc8a26c62017-11-24 20:15:23 +0100492void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
493 uword n_buffers,
494 vlib_buffer_known_state_t
495 expected_state);
496
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100498vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499{
Damjan Mariond50e3472019-01-20 00:03:56 +0100500 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
Damjan Marion6b0f5892017-07-27 04:01:24 -0400502 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400503 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400504 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
506}
507
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508/* Validates sanity of a single buffer.
509 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400510u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
511 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100513u8 *vlib_validate_buffers (vlib_main_t * vm,
514 u32 * buffers,
515 uword next_buffer_stride,
516 uword n_buffers,
517 vlib_buffer_known_state_t known_state,
518 uword follow_buffer_next);
519
Damjan Marion910d3692019-01-21 11:48:34 +0100520static_always_inline vlib_buffer_pool_t *
521vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522{
Damjan Marion910d3692019-01-21 11:48:34 +0100523 vlib_buffer_main_t *bm = vm->buffer_main;
524 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400525}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
Damjan Marion910d3692019-01-21 11:48:34 +0100527static_always_inline uword
528vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
529 u32 n_buffers)
530{
531 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
532 u32 len;
533
534 ASSERT (bp->buffers);
535
536 clib_spinlock_lock (&bp->lock);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100537 len = bp->n_avail;
Damjan Marion910d3692019-01-21 11:48:34 +0100538 if (PREDICT_TRUE (n_buffers < len))
539 {
540 len -= n_buffers;
541 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100542 bp->n_avail = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100543 clib_spinlock_unlock (&bp->lock);
544 return n_buffers;
545 }
546 else
547 {
548 vlib_buffer_copy_indices (buffers, bp->buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100549 bp->n_avail = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100550 clib_spinlock_unlock (&bp->lock);
551 return len;
552 }
553}
554
555
556/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
558 @param vm - (vlib_main_t *) vlib main data structure pointer
559 @param buffers - (u32 * ) buffer index array
560 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400561 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 less than the number requested or zero
563*/
Damjan Marion910d3692019-01-21 11:48:34 +0100564
Damjan Marion878c6092017-01-04 13:19:27 +0100565always_inline u32
Damjan Marion910d3692019-01-21 11:48:34 +0100566vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
567 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100568{
Damjan Mariond50e3472019-01-20 00:03:56 +0100569 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100570 vlib_buffer_pool_t *bp;
571 vlib_buffer_pool_thread_t *bpt;
572 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100573
Damjan Marion910d3692019-01-21 11:48:34 +0100574 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
575 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100576
Damjan Marion910d3692019-01-21 11:48:34 +0100577 dst = buffers;
578 n_left = n_buffers;
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100579 len = bpt->n_cached;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100580
Damjan Marion910d3692019-01-21 11:48:34 +0100581 /* per-thread cache contains enough buffers */
582 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100583 {
Damjan Marion910d3692019-01-21 11:48:34 +0100584 src = bpt->cached_buffers + len - n_buffers;
585 vlib_buffer_copy_indices (dst, src, n_buffers);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100586 bpt->n_cached -= n_buffers;
587
588 if (CLIB_DEBUG > 0)
589 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
590 VLIB_BUFFER_KNOWN_FREE);
591 return n_buffers;
592 }
593
594 /* alloc bigger than cache - take buffers directly from main pool */
595 if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ)
596 {
597 n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers,
598 n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100599
Damjan Marion910d3692019-01-21 11:48:34 +0100600 if (CLIB_DEBUG > 0)
601 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
602 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100603 return n_buffers;
604 }
605
Damjan Marion910d3692019-01-21 11:48:34 +0100606 /* take everything available in the cache */
607 if (len)
608 {
609 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100610 bpt->n_cached = 0;
Damjan Marion910d3692019-01-21 11:48:34 +0100611 dst += len;
612 n_left -= len;
613 }
614
615 len = round_pow2 (n_left, 32);
Damjan Marion910d3692019-01-21 11:48:34 +0100616 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
617 len);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100618 bpt->n_cached = len;
Damjan Marion910d3692019-01-21 11:48:34 +0100619
620 if (len)
621 {
622 u32 n_copy = clib_min (len, n_left);
623 src = bpt->cached_buffers + len - n_copy;
624 vlib_buffer_copy_indices (dst, src, n_copy);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100625 bpt->n_cached -= n_copy;
Damjan Marion910d3692019-01-21 11:48:34 +0100626 n_left -= n_copy;
627 }
628
629 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100630
631 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100632 if (CLIB_DEBUG > 0)
633 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
634 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100635
636 return n_buffers;
637}
638
Damjan Marion910d3692019-01-21 11:48:34 +0100639/** \brief Allocate buffers from specific numa node into supplied array
640
641 @param vm - (vlib_main_t *) vlib main data structure pointer
642 @param buffers - (u32 * ) buffer index array
643 @param n_buffers - (u32) number of buffers requested
644 @param numa_node - (u32) numa node
645 @return - (u32) number of buffers actually allocated, may be
646 less than the number requested or zero
647*/
648always_inline u32
649vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
650 u32 numa_node)
651{
652 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
653 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
654}
655
Damjan Marionc8a26c62017-11-24 20:15:23 +0100656/** \brief Allocate buffers into supplied array
657
658 @param vm - (vlib_main_t *) vlib main data structure pointer
659 @param buffers - (u32 * ) buffer index array
660 @param n_buffers - (u32) number of buffers requested
661 @return - (u32) number of buffers actually allocated, may be
662 less than the number requested or zero
663*/
Damjan Marion910d3692019-01-21 11:48:34 +0100664
Damjan Marionc8a26c62017-11-24 20:15:23 +0100665always_inline u32
666vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
667{
Damjan Marion910d3692019-01-21 11:48:34 +0100668 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100669}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
Damjan Marionc58408c2018-01-18 14:54:04 +0100671/** \brief Allocate buffers into ring
672
673 @param vm - (vlib_main_t *) vlib main data structure pointer
674 @param buffers - (u32 * ) buffer index ring
675 @param start - (u32) first slot in the ring
676 @param ring_size - (u32) ring size
677 @param n_buffers - (u32) number of buffers requested
678 @return - (u32) number of buffers actually allocated, may be
679 less than the number requested or zero
680*/
681always_inline u32
682vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
683 u32 ring_size, u32 n_buffers)
684{
685 u32 n_alloc;
686
687 ASSERT (n_buffers <= ring_size);
688
689 if (PREDICT_TRUE (start + n_buffers <= ring_size))
690 return vlib_buffer_alloc (vm, ring + start, n_buffers);
691
692 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
693
694 if (PREDICT_TRUE (n_alloc == ring_size - start))
695 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
696
697 return n_alloc;
698}
699
Damjan Marion910d3692019-01-21 11:48:34 +0100700/** \brief Allocate buffers into ring from specific buffer pool
701
702 @param vm - (vlib_main_t *) vlib main data structure pointer
703 @param buffers - (u32 * ) buffer index ring
704 @param start - (u32) first slot in the ring
705 @param ring_size - (u32) ring size
706 @param n_buffers - (u32) number of buffers requested
707 @return - (u32) number of buffers actually allocated, may be
708 less than the number requested or zero
709*/
710always_inline u32
711vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
712 u32 ring_size, u32 n_buffers,
713 u8 buffer_pool_index)
714{
715 u32 n_alloc;
716
717 ASSERT (n_buffers <= ring_size);
718
719 if (PREDICT_TRUE (start + n_buffers <= ring_size))
720 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
721 buffer_pool_index);
722
723 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
724 buffer_pool_index);
725
726 if (PREDICT_TRUE (n_alloc == ring_size - start))
727 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
728 buffer_pool_index);
729
730 return n_alloc;
731}
732
Damjan Marionf646d742019-01-31 18:50:04 +0100733static_always_inline void
Damjan Marion910d3692019-01-21 11:48:34 +0100734vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
735 u32 * buffers, u32 n_buffers)
736{
737 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100738 vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads,
739 vm->thread_index);
740 u32 n_cached, n_empty;
Damjan Marion910d3692019-01-21 11:48:34 +0100741
Damjan Marionf646d742019-01-31 18:50:04 +0100742 if (CLIB_DEBUG > 0)
743 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
744 VLIB_BUFFER_KNOWN_ALLOCATED);
745
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100746 n_cached = bpt->n_cached;
747 n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached;
748 if (n_buffers <= n_empty)
Damjan Marion910d3692019-01-21 11:48:34 +0100749 {
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100750 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
751 buffers, n_buffers);
752 bpt->n_cached = n_cached + n_buffers;
753 return;
Damjan Marion910d3692019-01-21 11:48:34 +0100754 }
Damjan Marionb6e8b1a2019-03-12 18:14:15 +0100755
756 vlib_buffer_copy_indices (bpt->cached_buffers + n_cached,
757 buffers + n_buffers - n_empty, n_empty);
758 bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ;
759
760 clib_spinlock_lock (&bp->lock);
761 vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers,
762 n_buffers - n_empty);
763 bp->n_avail += n_buffers - n_empty;
764 clib_spinlock_unlock (&bp->lock);
Damjan Marion910d3692019-01-21 11:48:34 +0100765}
766
767static_always_inline void
768vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
769 int maybe_next)
770{
771 const int queue_size = 128;
772 vlib_buffer_pool_t *bp = 0;
773 u8 buffer_pool_index = ~0;
774 u32 n_queue = 0, queue[queue_size + 4];
775 vlib_buffer_t bt = { };
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800776#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100777 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
778 vlib_buffer_t bpi_vec = {.buffer_pool_index = ~0 };
779 vlib_buffer_t flags_refs_mask = {
780 .flags = VLIB_BUFFER_NEXT_PRESENT,
Damjan Marion0e209242019-03-13 12:04:10 +0100781 .ref_count = ~1
Damjan Marion910d3692019-01-21 11:48:34 +0100782 };
783#endif
784
785 while (n_buffers)
786 {
787 vlib_buffer_t *b[8];
788 u32 bi, sum = 0, flags, next;
789
790 if (n_buffers < 12)
791 goto one_by_one;
792
793 vlib_get_buffers (vm, buffers, b, 4);
794 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
795
796 vlib_prefetch_buffer_header (b[4], LOAD);
797 vlib_prefetch_buffer_header (b[5], LOAD);
798 vlib_prefetch_buffer_header (b[6], LOAD);
799 vlib_prefetch_buffer_header (b[7], LOAD);
800
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800801#if defined(CLIB_HAVE_VEC128)
Damjan Marion910d3692019-01-21 11:48:34 +0100802 u8x16 p0, p1, p2, p3, r;
803 p0 = u8x16_load_unaligned (b[0]);
804 p1 = u8x16_load_unaligned (b[1]);
805 p2 = u8x16_load_unaligned (b[2]);
806 p3 = u8x16_load_unaligned (b[3]);
807
808 r = p0 ^ bpi_vec.as_u8x16[0];
809 r |= p1 ^ bpi_vec.as_u8x16[0];
810 r |= p2 ^ bpi_vec.as_u8x16[0];
811 r |= p3 ^ bpi_vec.as_u8x16[0];
812 r &= bpi_mask.as_u8x16[0];
813 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
814
815 sum = !u8x16_is_all_zero (r);
816#else
817 sum |= b[0]->flags;
818 sum |= b[1]->flags;
819 sum |= b[2]->flags;
820 sum |= b[3]->flags;
821 sum &= VLIB_BUFFER_NEXT_PRESENT;
822 sum += b[0]->ref_count - 1;
823 sum += b[1]->ref_count - 1;
824 sum += b[2]->ref_count - 1;
825 sum += b[3]->ref_count - 1;
826 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
827 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
828 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
829 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
830#endif
831
832 if (sum)
833 goto one_by_one;
834
835 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
836 vlib_buffer_copy_template (b[0], &bt);
837 vlib_buffer_copy_template (b[1], &bt);
838 vlib_buffer_copy_template (b[2], &bt);
839 vlib_buffer_copy_template (b[3], &bt);
840 n_queue += 4;
841
Damjan Marion24dcbe42019-01-31 12:29:39 +0100842 vlib_buffer_validate (vm, b[0]);
843 vlib_buffer_validate (vm, b[1]);
844 vlib_buffer_validate (vm, b[2]);
845 vlib_buffer_validate (vm, b[3]);
846
Damjan Marion910d3692019-01-21 11:48:34 +0100847 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
848 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
849 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
850 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
851
852 if (n_queue >= queue_size)
853 {
854 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
855 n_queue = 0;
856 }
857 buffers += 4;
858 n_buffers -= 4;
859 continue;
860
861 one_by_one:
862 bi = buffers[0];
863
864 next_in_chain:
865 b[0] = vlib_get_buffer (vm, bi);
866 flags = b[0]->flags;
867 next = b[0]->next_buffer;
868
869 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
870 {
Damjan Marion910d3692019-01-21 11:48:34 +0100871
872 if (n_queue)
873 {
874 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
875 n_queue = 0;
876 }
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500877
878 buffer_pool_index = b[0]->buffer_pool_index;
Lijian.Zhange6a47cf2019-03-12 18:32:39 +0800879#if defined(CLIB_HAVE_VEC128)
Lollita Liu8f6c1dd2019-02-14 05:02:44 -0500880 bpi_vec.buffer_pool_index = buffer_pool_index;
881#endif
882 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
883 vlib_buffer_copy_template (&bt, &bp->buffer_template);
Damjan Marion910d3692019-01-21 11:48:34 +0100884 }
885
Damjan Marion24dcbe42019-01-31 12:29:39 +0100886 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100887
888 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
889
890 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
891 {
Damjan Marion910d3692019-01-21 11:48:34 +0100892 vlib_buffer_copy_template (b[0], &bt);
893 queue[n_queue++] = bi;
894 }
895
896 if (n_queue == queue_size)
897 {
898 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
899 n_queue = 0;
900 }
901
Damjan Marion5fed42a2019-06-11 20:12:23 +0200902 if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT))
Damjan Marion910d3692019-01-21 11:48:34 +0100903 {
904 bi = next;
905 goto next_in_chain;
906 }
907
908 buffers++;
909 n_buffers--;
910 }
911
912 if (n_queue)
913 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
914}
915
916
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917/** \brief Free buffers
918 Frees the entire buffer chain for each buffer
919
920 @param vm - (vlib_main_t *) vlib main data structure pointer
921 @param buffers - (u32 * ) buffer index array
922 @param n_buffers - (u32) number of buffers to free
923
924*/
Damjan Marion878c6092017-01-04 13:19:27 +0100925always_inline void
926vlib_buffer_free (vlib_main_t * vm,
927 /* pointer to first buffer */
928 u32 * buffers,
929 /* number of buffers to free */
930 u32 n_buffers)
931{
Damjan Marion910d3692019-01-21 11:48:34 +0100932 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +0100933}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
935/** \brief Free buffers, does not free the buffer chain for each buffer
936
937 @param vm - (vlib_main_t *) vlib main data structure pointer
938 @param buffers - (u32 * ) buffer index array
939 @param n_buffers - (u32) number of buffers to free
940
941*/
Damjan Marion878c6092017-01-04 13:19:27 +0100942always_inline void
943vlib_buffer_free_no_next (vlib_main_t * vm,
944 /* pointer to first buffer */
945 u32 * buffers,
946 /* number of buffers to free */
947 u32 n_buffers)
948{
Damjan Marion910d3692019-01-21 11:48:34 +0100949 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +0100950}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
952/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400953 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
955 @param vm - (vlib_main_t *) vlib main data structure pointer
956 @param buffer_index - (u32) buffer index to free
957*/
958always_inline void
959vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
960{
Damjan Marion910d3692019-01-21 11:48:34 +0100961 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962}
963
Damjan Mariona3731492018-02-25 22:50:39 +0100964/** \brief Free buffers from ring
965
966 @param vm - (vlib_main_t *) vlib main data structure pointer
967 @param buffers - (u32 * ) buffer index ring
968 @param start - (u32) first slot in the ring
969 @param ring_size - (u32) ring size
970 @param n_buffers - (u32) number of buffers
971*/
972always_inline void
973vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
974 u32 ring_size, u32 n_buffers)
975{
976 ASSERT (n_buffers <= ring_size);
977
978 if (PREDICT_TRUE (start + n_buffers <= ring_size))
979 {
980 vlib_buffer_free (vm, ring + start, n_buffers);
981 }
982 else
983 {
984 vlib_buffer_free (vm, ring + start, ring_size - start);
985 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
986 }
987}
988
Damjan Marioncef1db92018-03-28 18:27:38 +0200989/** \brief Free buffers from ring without freeing tail buffers
990
991 @param vm - (vlib_main_t *) vlib main data structure pointer
992 @param buffers - (u32 * ) buffer index ring
993 @param start - (u32) first slot in the ring
994 @param ring_size - (u32) ring size
995 @param n_buffers - (u32) number of buffers
996*/
997always_inline void
998vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
999 u32 ring_size, u32 n_buffers)
1000{
1001 ASSERT (n_buffers <= ring_size);
1002
1003 if (PREDICT_TRUE (start + n_buffers <= ring_size))
1004 {
Damjan Marion4a973932018-06-09 19:29:16 +02001005 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +02001006 }
1007 else
1008 {
1009 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
1010 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
1011 }
1012}
Damjan Mariona3731492018-02-25 22:50:39 +01001013
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001015int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
1016 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017
John Lo8ed2d522019-08-07 19:30:29 -04001018/* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */
1019#define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \
1020 (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \
1021 VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL)
1022
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001023/* duplicate all buffers in chain */
1024always_inline vlib_buffer_t *
1025vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
1026{
1027 vlib_buffer_t *s, *d, *fd;
1028 uword n_alloc, n_buffers = 1;
John Lo8ed2d522019-08-07 19:30:29 -04001029 u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001030 int i;
1031
1032 s = b;
1033 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1034 {
1035 n_buffers++;
1036 s = vlib_get_buffer (vm, s->next_buffer);
1037 }
Neale Ranns9d676af2017-03-15 01:28:31 -07001038 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001039
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001040 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -05001041
1042 /* No guarantee that we'll get all the buffers we asked for */
1043 if (PREDICT_FALSE (n_alloc < n_buffers))
1044 {
1045 if (n_alloc > 0)
1046 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -05001047 return 0;
1048 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001049
1050 /* 1st segment */
1051 s = b;
1052 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001053 d->current_data = s->current_data;
1054 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +01001055 d->flags = s->flags & flag_mask;
John Lo66317802019-08-13 18:18:21 -04001056 d->trace_handle = s->trace_handle;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001057 d->total_length_not_including_first_buffer =
1058 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -05001059 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1060 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1061 clib_memcpy_fast (vlib_buffer_get_current (d),
1062 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001063
1064 /* next segments */
1065 for (i = 1; i < n_buffers; i++)
1066 {
1067 /* previous */
1068 d->next_buffer = new_buffers[i];
1069 /* current */
1070 s = vlib_get_buffer (vm, s->next_buffer);
1071 d = vlib_get_buffer (vm, new_buffers[i]);
1072 d->current_data = s->current_data;
1073 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -05001074 clib_memcpy_fast (vlib_buffer_get_current (d),
1075 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +01001076 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +01001077 }
1078
1079 return fd;
1080}
1081
Ole Troanda7f7b62019-03-11 13:15:54 +01001082/* duplicate first buffer in chain */
1083always_inline vlib_buffer_t *
1084vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di)
1085{
1086 vlib_buffer_t *d;
1087
1088 if ((vlib_buffer_alloc (vm, di, 1)) != 1)
1089 return 0;
1090
1091 d = vlib_get_buffer (vm, *di);
1092 /* 1st segment */
1093 d->current_data = b->current_data;
1094 d->current_length = b->current_length;
1095 clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque));
1096 clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2));
1097 clib_memcpy_fast (vlib_buffer_get_current (d),
1098 vlib_buffer_get_current (b), b->current_length);
1099
1100 return d;
1101}
1102
John Lof545caa2019-04-01 11:30:07 -04001103/* \brief Move packet from current position to offset position in buffer.
1104 Only work for small packet using one buffer with room to fit the move
1105 @param vm - (vlib_main_t *) vlib main data structure pointer
1106 @param b - (vlib_buffer_t *) pointer to buffer
1107 @param offset - (i16) position to move the packet in buffer
1108 */
1109always_inline void
1110vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset)
1111{
1112 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
1113 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1114 ASSERT (offset + b->current_length <
1115 vlib_buffer_get_default_data_size (vm));
1116
1117 u8 *source = vlib_buffer_get_current (b);
1118 b->current_data = offset;
1119 u8 *destination = vlib_buffer_get_current (b);
1120 u16 length = b->current_length;
1121
1122 if (source + length <= destination) /* no overlap */
1123 clib_memcpy_fast (destination, source, length);
1124 else
1125 memmove (destination, source, length);
1126}
1127
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001128/** \brief Create a maximum of 256 clones of buffer and store them
1129 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +01001130
1131 @param vm - (vlib_main_t *) vlib main data structure pointer
1132 @param src_buffer - (u32) source buffer index
1133 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001134 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +01001135 @param head_end_offset - (u16) offset relative to current position
1136 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001137 @param offset - (i16) copy packet head at current position if 0,
1138 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001139 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +01001140 less than the number requested or zero
1141*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001142always_inline u16
1143vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
John Lof545caa2019-04-01 11:30:07 -04001144 u16 n_buffers, u16 head_end_offset, i16 offset)
Damjan Marionc47ed032017-01-25 14:18:03 +01001145{
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001146 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +01001147 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1148
Damjan Marion910d3692019-01-21 11:48:34 +01001149 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001150 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001151 ASSERT (n_buffers <= 256);
John Lof545caa2019-04-01 11:30:07 -04001152 ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0);
1153 ASSERT ((offset + head_end_offset) <
1154 vlib_buffer_get_default_data_size (vm));
Damjan Marionc47ed032017-01-25 14:18:03 +01001155
1156 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1157 {
1158 buffers[0] = src_buffer;
John Lof545caa2019-04-01 11:30:07 -04001159 if (offset)
1160 vlib_buffer_move (vm, s, offset);
1161
Damjan Marionc47ed032017-01-25 14:18:03 +01001162 for (i = 1; i < n_buffers; i++)
1163 {
1164 vlib_buffer_t *d;
1165 d = vlib_buffer_copy (vm, s);
1166 if (d == 0)
1167 return i;
1168 buffers[i] = vlib_get_buffer_index (vm, d);
1169
1170 }
1171 return n_buffers;
1172 }
1173
John Lof545caa2019-04-01 11:30:07 -04001174 if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0)))
Damjan Marionc47ed032017-01-25 14:18:03 +01001175 {
1176 buffers[0] = src_buffer;
1177 return 1;
1178 }
1179
Damjan Marion910d3692019-01-21 11:48:34 +01001180 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1181 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001182
Damjan Marionc47ed032017-01-25 14:18:03 +01001183 for (i = 0; i < n_buffers; i++)
1184 {
1185 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
John Lof545caa2019-04-01 11:30:07 -04001186 if (offset)
1187 d->current_data = offset;
1188 else
1189 d->current_data = s->current_data;
1190
Damjan Marionc47ed032017-01-25 14:18:03 +01001191 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001192 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1193
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001194 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001195 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001196 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1197 {
1198 d->total_length_not_including_first_buffer +=
1199 s->total_length_not_including_first_buffer;
1200 }
John Lo8ed2d522019-08-07 19:30:29 -04001201 d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) |
1202 VLIB_BUFFER_NEXT_PRESENT;
John Lo66317802019-08-13 18:18:21 -04001203 d->trace_handle = s->trace_handle;
Dave Barach178cf492018-11-13 16:34:13 -05001204 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1205 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1206 clib_memcpy_fast (vlib_buffer_get_current (d),
1207 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001208 d->next_buffer = src_buffer;
1209 }
1210 vlib_buffer_advance (s, head_end_offset);
Damjan Marion910d3692019-01-21 11:48:34 +01001211 s->ref_count = n_buffers;
Damjan Marionc47ed032017-01-25 14:18:03 +01001212 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1213 {
1214 s = vlib_get_buffer (vm, s->next_buffer);
Damjan Marion910d3692019-01-21 11:48:34 +01001215 s->ref_count = n_buffers;
Damjan Marionc47ed032017-01-25 14:18:03 +01001216 }
1217
1218 return n_buffers;
1219}
1220
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001221/** \brief Create multiple clones of buffer and store them
1222 in the supplied array
1223
1224 @param vm - (vlib_main_t *) vlib main data structure pointer
1225 @param src_buffer - (u32) source buffer index
1226 @param buffers - (u32 * ) buffer index array
1227 @param n_buffers - (u16) number of buffer clones requested (<=256)
1228 @param head_end_offset - (u16) offset relative to current position
1229 where packet head ends
John Lof545caa2019-04-01 11:30:07 -04001230 @param offset - (i16) copy packet head at current position if 0,
1231 else at offset position to change headroom space as specified
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001232 @return - (u16) number of buffers actually cloned, may be
1233 less than the number requested or zero
1234*/
1235always_inline u16
John Lof545caa2019-04-01 11:30:07 -04001236vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1237 u16 n_buffers, u16 head_end_offset, i16 offset)
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001238{
1239 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1240 u16 n_cloned = 0;
1241
1242 while (n_buffers > 256)
1243 {
1244 vlib_buffer_t *copy;
1245 copy = vlib_buffer_copy (vm, s);
1246 n_cloned += vlib_buffer_clone_256 (vm,
1247 vlib_get_buffer_index (vm, copy),
1248 (buffers + n_cloned),
John Lof545caa2019-04-01 11:30:07 -04001249 256, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001250 n_buffers -= 256;
1251 }
1252 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1253 buffers + n_cloned,
John Lof545caa2019-04-01 11:30:07 -04001254 n_buffers, head_end_offset, offset);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001255
1256 return n_cloned;
1257}
1258
John Lof545caa2019-04-01 11:30:07 -04001259/** \brief Create multiple clones of buffer and store them
1260 in the supplied array
1261
1262 @param vm - (vlib_main_t *) vlib main data structure pointer
1263 @param src_buffer - (u32) source buffer index
1264 @param buffers - (u32 * ) buffer index array
1265 @param n_buffers - (u16) number of buffer clones requested (<=256)
1266 @param head_end_offset - (u16) offset relative to current position
1267 where packet head ends
1268 @return - (u16) number of buffers actually cloned, may be
1269 less than the number requested or zero
1270*/
1271always_inline u16
1272vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1273 u16 n_buffers, u16 head_end_offset)
1274{
1275 return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers,
1276 head_end_offset, 0);
1277}
1278
Damjan Marionc47ed032017-01-25 14:18:03 +01001279/** \brief Attach cloned tail to the buffer
1280
1281 @param vm - (vlib_main_t *) vlib main data structure pointer
1282 @param head - (vlib_buffer_t *) head buffer
1283 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1284*/
1285
1286always_inline void
1287vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1288 vlib_buffer_t * tail)
1289{
1290 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001291 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001292
1293 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1294 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1295 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1296 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1297 head->next_buffer = vlib_get_buffer_index (vm, tail);
1298 head->total_length_not_including_first_buffer = tail->current_length +
1299 tail->total_length_not_including_first_buffer;
1300
1301next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001302 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001303
1304 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1305 {
1306 tail = vlib_get_buffer (vm, tail->next_buffer);
1307 goto next_segment;
1308 }
1309}
1310
Pierre Pfister328e99b2016-02-12 13:18:42 +00001311/* Initializes the buffer as an empty packet with no chained buffers. */
1312always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001313vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001314{
1315 first->total_length_not_including_first_buffer = 0;
1316 first->current_length = 0;
1317 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1318 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001319}
1320
1321/* The provided next_bi buffer index is appended to the end of the packet. */
1322always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001323vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001324{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001325 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001326 last->next_buffer = next_bi;
1327 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1328 next_buffer->current_length = 0;
1329 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001330 return next_buffer;
1331}
1332
1333/* Increases or decreases the packet length.
1334 * It does not allocate or deallocate new buffers.
1335 * Therefore, the added length must be compatible
1336 * with the last buffer. */
1337always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001338vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1339 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001340{
1341 last->current_length += len;
1342 if (first != last)
1343 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001344}
1345
1346/* Copy data to the end of the packet and increases its length.
1347 * It does not allocate new buffers.
1348 * Returns the number of copied bytes. */
1349always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001350vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001351 vlib_buffer_t * first,
1352 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001353{
Damjan Marion8934a042019-02-09 23:29:26 +01001354 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001355 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1356 u16 len = clib_min (data_len,
1357 n_buffer_bytes - last->current_length -
1358 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001359 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1360 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001361 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001362 return len;
1363}
1364
1365/* Copy data to the end of the packet and increases its length.
1366 * Allocates additional buffers from the free list if necessary.
1367 * Returns the number of copied bytes.
1368 * 'last' value is modified whenever new buffers are allocated and
1369 * chained and points to the last buffer in the chain. */
1370u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001371vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001372 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001373 vlib_buffer_t ** last, void *data,
1374 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001375void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001376
Dave Barach9b8ffd92016-07-08 08:13:45 -04001377format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
Benoît Ganne43543172019-10-21 15:13:54 +02001378 format_vlib_buffer_contents, format_vlib_buffer_no_chain;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001379
Dave Barach9b8ffd92016-07-08 08:13:45 -04001380typedef struct
1381{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001383 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001384
Damjan Mariond1274cb2018-03-13 21:32:17 +01001385 /* Number of buffers to allocate in each call to allocator. */
1386 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001387
Damjan Marion671e60e2018-12-30 18:09:59 +01001388 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001389} vlib_packet_template_t;
1390
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391void vlib_packet_template_init (vlib_main_t * vm,
1392 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001393 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001395 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001396 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001397
Dave Barach9b8ffd92016-07-08 08:13:45 -04001398void *vlib_packet_template_get_packet (vlib_main_t * vm,
1399 vlib_packet_template_t * t,
1400 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401
1402always_inline void
1403vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1404{
1405 vec_free (t->packet_data);
1406}
1407
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001408always_inline u32
1409vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b)
Klement Sekera75e7d132017-09-20 08:26:30 +02001410{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001411 return b->data + vlib_buffer_get_default_data_size (vm) -
1412 ((u8 *) vlib_buffer_get_current (b) + b->current_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001413}
1414
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001415always_inline u32
1416vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b)
Eyal Barid3d42412018-11-05 13:29:25 +02001417{
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001418 vlib_buffer_t *db = b, *sb, *first = b;
1419 int is_cloned = 0;
1420 u32 bytes_left = 0, data_size;
1421 u16 src_left, dst_left, n_buffers = 1;
1422 u8 *dp, *sp;
1423 u32 to_free = 0;
Eyal Barid3d42412018-11-05 13:29:25 +02001424
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001425 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
1426 return 1;
Eyal Barid3d42412018-11-05 13:29:25 +02001427
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001428 data_size = vlib_buffer_get_default_data_size (vm);
1429
1430 dst_left = vlib_buffer_space_left_at_end (vm, b);
1431
1432 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
Eyal Barid3d42412018-11-05 13:29:25 +02001433 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001434 b = vlib_get_buffer (vm, b->next_buffer);
1435 if (b->ref_count > 1)
1436 is_cloned = 1;
1437 bytes_left += b->current_length;
1438 n_buffers++;
Eyal Barid3d42412018-11-05 13:29:25 +02001439 }
1440
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001441 /* if buffer is cloned, create completely new chain - unless everything fits
1442 * into one buffer */
1443 if (is_cloned && bytes_left >= dst_left)
Eyal Barid3d42412018-11-05 13:29:25 +02001444 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001445 u32 len = 0;
1446 u32 space_needed = bytes_left - dst_left;
1447 u32 tail;
1448
1449 if (vlib_buffer_alloc (vm, &tail, 1) == 0)
1450 return 0;
1451
1452 ++n_buffers;
1453 len += data_size;
1454 b = vlib_get_buffer (vm, tail);
1455
1456 while (len < space_needed)
Eyal Barid3d42412018-11-05 13:29:25 +02001457 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001458 u32 bi;
1459 if (vlib_buffer_alloc (vm, &bi, 1) == 0)
1460 {
1461 vlib_buffer_free_one (vm, tail);
1462 return 0;
1463 }
1464 b->flags = VLIB_BUFFER_NEXT_PRESENT;
1465 b->next_buffer = bi;
1466 b = vlib_get_buffer (vm, bi);
1467 len += data_size;
1468 n_buffers++;
1469 }
1470 sb = vlib_get_buffer (vm, first->next_buffer);
1471 to_free = first->next_buffer;
1472 first->next_buffer = tail;
1473 }
1474 else
1475 sb = vlib_get_buffer (vm, first->next_buffer);
1476
1477 src_left = sb->current_length;
1478 sp = vlib_buffer_get_current (sb);
1479 dp = vlib_buffer_get_tail (db);
1480
1481 while (bytes_left)
1482 {
1483 u16 bytes_to_copy;
1484
1485 if (dst_left == 0)
1486 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001487 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1488 ASSERT (db->flags & VLIB_BUFFER_NEXT_PRESENT);
1489 db = vlib_get_buffer (vm, db->next_buffer);
1490 dst_left = data_size;
Klement Sekerac09b7fd2019-06-04 21:14:26 +02001491 if (db->current_data > 0)
1492 {
1493 db->current_data = 0;
1494 }
1495 else
1496 {
1497 dst_left += -db->current_data;
1498 }
1499 dp = vlib_buffer_get_current (db);
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001500 }
1501
1502 while (src_left == 0)
1503 {
1504 ASSERT (sb->flags & VLIB_BUFFER_NEXT_PRESENT);
1505 sb = vlib_get_buffer (vm, sb->next_buffer);
1506 src_left = sb->current_length;
1507 sp = vlib_buffer_get_current (sb);
1508 }
1509
1510 bytes_to_copy = clib_min (dst_left, src_left);
1511
1512 if (dp != sp)
1513 {
1514 if (sb == db)
1515 bytes_to_copy = clib_min (bytes_to_copy, sp - dp);
1516
1517 clib_memcpy_fast (dp, sp, bytes_to_copy);
1518 }
1519
1520 src_left -= bytes_to_copy;
1521 dst_left -= bytes_to_copy;
1522 dp += bytes_to_copy;
1523 sp += bytes_to_copy;
1524 bytes_left -= bytes_to_copy;
1525 }
1526 if (db != first)
1527 db->current_data = 0;
1528 db->current_length = dp - (u8 *) vlib_buffer_get_current (db);
1529
1530 if (is_cloned && to_free)
1531 vlib_buffer_free_one (vm, to_free);
1532 else
1533 {
1534 if (db->flags & VLIB_BUFFER_NEXT_PRESENT)
1535 vlib_buffer_free_one (vm, db->next_buffer);
1536 db->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1537 b = first;
1538 n_buffers = 1;
1539 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1540 {
1541 b = vlib_get_buffer (vm, b->next_buffer);
1542 ++n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001543 }
1544 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001545
1546 first->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1547
1548 return n_buffers;
Eyal Barid3d42412018-11-05 13:29:25 +02001549}
1550
Ed Warnickecb9cada2015-12-08 15:45:58 -07001551#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001552
1553/*
1554 * fd.io coding-style-patch-verification: ON
1555 *
1556 * Local Variables:
1557 * eval: (c-set-style "gnu")
1558 * End:
1559 */