blob: 37ddcd4a206405a2ae65995e21a99a2b4f12f25e [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
46/** \file
47 vlib buffer access methods.
48*/
49
Damjan Marion24dcbe42019-01-31 12:29:39 +010050always_inline void
51vlib_buffer_validate (vlib_main_t * vm, vlib_buffer_t * b)
52{
53 vlib_buffer_main_t *bm = vm->buffer_main;
54 vlib_buffer_pool_t *bp;
55
56 /* reference count in allocated buffer always must be 1 or higher */
57 ASSERT (b->ref_count > 0);
58
59 /* verify that buffer pointer is from buffer memory range */
60 ASSERT (pointer_to_uword (b) >= bm->buffer_mem_start);
61 ASSERT (pointer_to_uword (b) < bm->buffer_mem_start + bm->buffer_mem_size -
62 VLIB_BUFFER_DATA_SIZE);
63
64 /* verify that buffer pool index is valid */
65 bp = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index);
66 ASSERT (pointer_to_uword (b) >= bp->start);
67 ASSERT (pointer_to_uword (b) < bp->start + bp->size -
68 VLIB_BUFFER_DATA_SIZE);
69}
70
71always_inline void *
72vlib_buffer_ptr_from_index (uword buffer_mem_start, u32 buffer_index,
73 uword offset)
74{
75 offset += ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES;
76 return uword_to_pointer (buffer_mem_start + offset, vlib_buffer_t *);
77}
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79/** \brief Translate buffer index into buffer pointer
80
81 @param vm - (vlib_main_t *) vlib main data structure pointer
82 @param buffer_index - (u32) buffer index
83 @return - (vlib_buffer_t *) buffer pointer
Dave Barach9b8ffd92016-07-08 08:13:45 -040084*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070085always_inline vlib_buffer_t *
86vlib_get_buffer (vlib_main_t * vm, u32 buffer_index)
87{
Damjan Mariond50e3472019-01-20 00:03:56 +010088 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion24dcbe42019-01-31 12:29:39 +010089 vlib_buffer_t *b;
Damjan Marion04a7f052017-07-10 15:06:17 +020090
Damjan Marion24dcbe42019-01-31 12:29:39 +010091 b = vlib_buffer_ptr_from_index (bm->buffer_mem_start, buffer_index, 0);
92 vlib_buffer_validate (vm, b);
93 return b;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094}
95
Damjan Marione58041f2019-01-18 19:56:09 +010096static_always_inline void
Damjan Marion64d557c2019-01-18 20:03:41 +010097vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices)
98{
99 clib_memcpy_fast (dst, src, n_indices * sizeof (u32));
100}
101
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100102STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +0100103static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +0100104vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
105{
Damjan Marion22f23ae2019-01-24 15:36:57 +0100106#if defined CLIB_HAVE_VEC512
107 b->as_u8x64[0] = bt->as_u8x64[0];
108#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +0100109 b->as_u8x32[0] = bt->as_u8x32[0];
110 b->as_u8x32[1] = bt->as_u8x32[1];
111#elif defined (CLIB_HAVE_VEC128)
112 b->as_u8x16[0] = bt->as_u8x16[0];
113 b->as_u8x16[1] = bt->as_u8x16[1];
114 b->as_u8x16[2] = bt->as_u8x16[2];
115 b->as_u8x16[3] = bt->as_u8x16[3];
116#else
117 clib_memcpy_fast (b, bt, 64);
118#endif
Damjan Marione58041f2019-01-18 19:56:09 +0100119}
120
Damjan Marion910d3692019-01-21 11:48:34 +0100121always_inline u8
122vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node)
123{
124 ASSERT (numa_node < vm->buffer_main->n_numa_nodes);
125 return numa_node;
126}
127
Damjan Marionafe56de2018-05-17 12:44:00 +0200128/** \brief Translate array of buffer indices into buffer pointers with offset
129
130 @param vm - (vlib_main_t *) vlib main data structure pointer
131 @param bi - (u32 *) array of buffer indices
132 @param b - (void **) array to store buffer pointers
133 @param count - (uword) number of elements
134 @param offset - (i32) offset applied to each pointer
135*/
136static_always_inline void
137vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count,
138 i32 offset)
139{
Damjan Mariond50e3472019-01-20 00:03:56 +0100140 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marionafe56de2018-05-17 12:44:00 +0200141#ifdef CLIB_HAVE_VEC256
Damjan Mariond50e3472019-01-20 00:03:56 +0100142 u64x4 off = u64x4_splat (buffer_mem_start + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200143 /* if count is not const, compiler will not unroll while loop
144 se we maintain two-in-parallel variant */
145 while (count >= 8)
146 {
147 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
148 u64x4 b1 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi + 4));
149 /* shift and add to get vlib_buffer_t pointer */
150 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
151 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
152 b += 8;
153 bi += 8;
154 count -= 8;
155 }
156#endif
157 while (count >= 4)
158 {
159#ifdef CLIB_HAVE_VEC256
160 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
161 /* shift and add to get vlib_buffer_t pointer */
162 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800163#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100164 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200165 u32x4 bi4 = u32x4_load_unaligned (bi);
166 u64x2 b0 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800167#if defined (__aarch64__)
168 u64x2 b1 = u32x4_extend_to_u64x2_high ((u32x4) bi4);
169#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200170 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
171 u64x2 b1 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800172#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200173 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
174 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200175#else
Damjan Marion24dcbe42019-01-31 12:29:39 +0100176 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
177 b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset);
178 b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset);
179 b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200180#endif
181 b += 4;
182 bi += 4;
183 count -= 4;
184 }
185 while (count)
186 {
Damjan Marion24dcbe42019-01-31 12:29:39 +0100187 b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200188 b += 1;
189 bi += 1;
190 count -= 1;
191 }
192}
193
194/** \brief Translate array of buffer indices into buffer pointers
195
196 @param vm - (vlib_main_t *) vlib main data structure pointer
197 @param bi - (u32 *) array of buffer indices
198 @param b - (vlib_buffer_t **) array to store buffer pointers
199 @param count - (uword) number of elements
200*/
201
202static_always_inline void
203vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
204{
205 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
206}
207
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208/** \brief Translate buffer pointer into buffer index
209
210 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400211 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200214
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400216vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217{
Damjan Mariond50e3472019-01-20 00:03:56 +0100218 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200219 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
220 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
221 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400222 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
224}
225
Damjan Marionafe56de2018-05-17 12:44:00 +0200226/** \brief Translate array of buffer pointers into buffer indices with offset
227
228 @param vm - (vlib_main_t *) vlib main data structure pointer
229 @param b - (void **) array of buffer pointers
230 @param bi - (u32 *) array to store buffer indices
231 @param count - (uword) number of elements
232 @param offset - (i32) offset applied to each pointer
233*/
234static_always_inline void
235vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
236 uword count, i32 offset)
237{
238#ifdef CLIB_HAVE_VEC256
239 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100240 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200241
242 while (count >= 8)
243 {
244 /* load 4 pointers into 256-bit register */
245 u64x4 v0 = u64x4_load_unaligned (b);
246 u64x4 v1 = u64x4_load_unaligned (b + 4);
247 u32x8 v2, v3;
248
249 v0 -= off4;
250 v1 -= off4;
251
252 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
253 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
254
255 /* permute 256-bit register so lower u32s of each buffer index are
256 * placed into lower 128-bits */
257 v2 = u32x8_permute ((u32x8) v0, mask);
258 v3 = u32x8_permute ((u32x8) v1, mask);
259
260 /* extract lower 128-bits and save them to the array of buffer indices */
261 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
262 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
263 bi += 8;
264 b += 8;
265 count -= 8;
266 }
267#endif
268 while (count >= 4)
269 {
270 /* equivalent non-nector implementation */
271 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
272 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
273 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
274 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
275 bi += 4;
276 b += 4;
277 count -= 4;
278 }
279 while (count)
280 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400281 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200282 bi += 1;
283 b += 1;
284 count -= 1;
285 }
286}
287
288/** \brief Translate array of buffer pointers into buffer indices
289
290 @param vm - (vlib_main_t *) vlib main data structure pointer
291 @param b - (vlib_buffer_t **) array of buffer pointers
292 @param bi - (u32 *) array to store buffer indices
293 @param count - (uword) number of elements
294*/
295static_always_inline void
296vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
297 uword count)
298{
299 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
300}
301
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302/** \brief Get next buffer in buffer linklist, or zero for end of list.
303
304 @param vm - (vlib_main_t *) vlib main data structure pointer
305 @param b - (void *) buffer pointer
306 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400307*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308always_inline vlib_buffer_t *
309vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
310{
311 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400312 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313}
314
Dave Barach9b8ffd92016-07-08 08:13:45 -0400315uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
316 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
318/** \brief Get length in bytes of the buffer chain
319
320 @param vm - (vlib_main_t *) vlib main data structure pointer
321 @param b - (void *) buffer pointer
322 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400323*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324always_inline uword
325vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
326{
Damjan Marion072401e2017-07-13 18:53:27 +0200327 uword len = b->current_length;
328
329 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
330 return len;
331
332 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
333 return len + b->total_length_not_including_first_buffer;
334
335 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336}
337
338/** \brief Get length in bytes of the buffer index buffer chain
339
340 @param vm - (vlib_main_t *) vlib main data structure pointer
341 @param bi - (u32) buffer index
342 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344always_inline uword
345vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
346{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 return vlib_buffer_length_in_chain (vm, b);
349}
350
351/** \brief Copy buffer contents to memory
352
353 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400354 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 @param contents - (u8 *) memory, <strong>must be large enough</strong>
356 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358always_inline uword
359vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
360{
361 uword content_len = 0;
362 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400363 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
365 while (1)
366 {
367 b = vlib_get_buffer (vm, buffer_index);
368 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500369 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 break;
373 buffer_index = b->next_buffer;
374 }
375
376 return content_len;
377}
378
Damjan Marion8f499362018-10-22 13:07:02 +0200379always_inline uword
380vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381{
Damjan Marion68b4da62018-09-30 18:26:20 +0200382 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383}
384
Damjan Marion8f499362018-10-22 13:07:02 +0200385always_inline uword
386vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
387{
388 return vlib_buffer_get_pa (vm, b) + b->current_data;
389}
390
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391/** \brief Prefetch buffer metadata by buffer index
392 The first 64 bytes of buffer contains most header information
393
394 @param vm - (vlib_main_t *) vlib main data structure pointer
395 @param bi - (u32) buffer index
396 @param type - LOAD, STORE. In most cases, STORE is the right answer
397*/
398/* Prefetch buffer header given index. */
399#define vlib_prefetch_buffer_with_index(vm,bi,type) \
400 do { \
401 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
402 vlib_prefetch_buffer_header (_b, type); \
403 } while (0)
404
Dave Barach9b8ffd92016-07-08 08:13:45 -0400405typedef enum
406{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 /* Index is unknown. */
408 VLIB_BUFFER_UNKNOWN,
409
410 /* Index is known and free/allocated. */
411 VLIB_BUFFER_KNOWN_FREE,
412 VLIB_BUFFER_KNOWN_ALLOCATED,
413} vlib_buffer_known_state_t;
414
Damjan Marionc8a26c62017-11-24 20:15:23 +0100415void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
416 uword n_buffers,
417 vlib_buffer_known_state_t
418 expected_state);
419
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100421vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422{
Damjan Mariond50e3472019-01-20 00:03:56 +0100423 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Damjan Marion6b0f5892017-07-27 04:01:24 -0400425 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400426 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400427 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
429}
430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431/* Validates sanity of a single buffer.
432 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
434 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Damjan Marion910d3692019-01-21 11:48:34 +0100436static_always_inline vlib_buffer_pool_t *
437vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400438{
Damjan Marion910d3692019-01-21 11:48:34 +0100439 vlib_buffer_main_t *bm = vm->buffer_main;
440 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Damjan Marion910d3692019-01-21 11:48:34 +0100443static_always_inline uword
444vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers,
445 u32 n_buffers)
446{
447 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
448 u32 len;
449
450 ASSERT (bp->buffers);
451
452 clib_spinlock_lock (&bp->lock);
453 len = vec_len (bp->buffers);
454 if (PREDICT_TRUE (n_buffers < len))
455 {
456 len -= n_buffers;
457 vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers);
458 _vec_len (bp->buffers) = len;
459 clib_spinlock_unlock (&bp->lock);
460 return n_buffers;
461 }
462 else
463 {
464 vlib_buffer_copy_indices (buffers, bp->buffers, len);
465 _vec_len (bp->buffers) = 0;
466 clib_spinlock_unlock (&bp->lock);
467 return len;
468 }
469}
470
471
472/** \brief Allocate buffers from specific pool into supplied array
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
474 @param vm - (vlib_main_t *) vlib main data structure pointer
475 @param buffers - (u32 * ) buffer index array
476 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 less than the number requested or zero
479*/
Damjan Marion910d3692019-01-21 11:48:34 +0100480
Damjan Marion878c6092017-01-04 13:19:27 +0100481always_inline u32
Damjan Marion910d3692019-01-21 11:48:34 +0100482vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
483 u8 buffer_pool_index)
Damjan Marion878c6092017-01-04 13:19:27 +0100484{
Damjan Mariond50e3472019-01-20 00:03:56 +0100485 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion910d3692019-01-21 11:48:34 +0100486 vlib_buffer_pool_t *bp;
487 vlib_buffer_pool_thread_t *bpt;
488 u32 *src, *dst, len, n_left;
Damjan Marion878c6092017-01-04 13:19:27 +0100489
Damjan Marion910d3692019-01-21 11:48:34 +0100490 bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
491 bpt = vec_elt_at_index (bp->threads, vm->thread_index);
Damjan Marion878c6092017-01-04 13:19:27 +0100492
Damjan Marion910d3692019-01-21 11:48:34 +0100493 dst = buffers;
494 n_left = n_buffers;
495 len = vec_len (bpt->cached_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100496
Damjan Marion910d3692019-01-21 11:48:34 +0100497 /* per-thread cache contains enough buffers */
498 if (len >= n_buffers)
Damjan Marionc8a26c62017-11-24 20:15:23 +0100499 {
Damjan Marion910d3692019-01-21 11:48:34 +0100500 src = bpt->cached_buffers + len - n_buffers;
501 vlib_buffer_copy_indices (dst, src, n_buffers);
502 _vec_len (bpt->cached_buffers) -= n_buffers;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100503
Damjan Marion910d3692019-01-21 11:48:34 +0100504 if (CLIB_DEBUG > 0)
505 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
506 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100507 return n_buffers;
508 }
509
Damjan Marion910d3692019-01-21 11:48:34 +0100510 /* take everything available in the cache */
511 if (len)
512 {
513 vlib_buffer_copy_indices (dst, bpt->cached_buffers, len);
514 _vec_len (bpt->cached_buffers) = 0;
515 dst += len;
516 n_left -= len;
517 }
518
519 len = round_pow2 (n_left, 32);
520 vec_validate_aligned (bpt->cached_buffers, len - 1, CLIB_CACHE_LINE_BYTES);
521 len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers,
522 len);
523 _vec_len (bpt->cached_buffers) = len;
524
525 if (len)
526 {
527 u32 n_copy = clib_min (len, n_left);
528 src = bpt->cached_buffers + len - n_copy;
529 vlib_buffer_copy_indices (dst, src, n_copy);
530 _vec_len (bpt->cached_buffers) -= n_copy;
531 n_left -= n_copy;
532 }
533
534 n_buffers -= n_left;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100535
536 /* Verify that buffers are known free. */
Damjan Marion910d3692019-01-21 11:48:34 +0100537 if (CLIB_DEBUG > 0)
538 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
539 VLIB_BUFFER_KNOWN_FREE);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100540
541 return n_buffers;
542}
543
Damjan Marion910d3692019-01-21 11:48:34 +0100544/** \brief Allocate buffers from specific numa node into supplied array
545
546 @param vm - (vlib_main_t *) vlib main data structure pointer
547 @param buffers - (u32 * ) buffer index array
548 @param n_buffers - (u32) number of buffers requested
549 @param numa_node - (u32) numa node
550 @return - (u32) number of buffers actually allocated, may be
551 less than the number requested or zero
552*/
553always_inline u32
554vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
555 u32 numa_node)
556{
557 u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node);
558 return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index);
559}
560
Damjan Marionc8a26c62017-11-24 20:15:23 +0100561/** \brief Allocate buffers into supplied array
562
563 @param vm - (vlib_main_t *) vlib main data structure pointer
564 @param buffers - (u32 * ) buffer index array
565 @param n_buffers - (u32) number of buffers requested
566 @return - (u32) number of buffers actually allocated, may be
567 less than the number requested or zero
568*/
Damjan Marion910d3692019-01-21 11:48:34 +0100569
Damjan Marionc8a26c62017-11-24 20:15:23 +0100570always_inline u32
571vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
572{
Damjan Marion910d3692019-01-21 11:48:34 +0100573 return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node);
Damjan Marion878c6092017-01-04 13:19:27 +0100574}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Damjan Marionc58408c2018-01-18 14:54:04 +0100576/** \brief Allocate buffers into ring
577
578 @param vm - (vlib_main_t *) vlib main data structure pointer
579 @param buffers - (u32 * ) buffer index ring
580 @param start - (u32) first slot in the ring
581 @param ring_size - (u32) ring size
582 @param n_buffers - (u32) number of buffers requested
583 @return - (u32) number of buffers actually allocated, may be
584 less than the number requested or zero
585*/
586always_inline u32
587vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
588 u32 ring_size, u32 n_buffers)
589{
590 u32 n_alloc;
591
592 ASSERT (n_buffers <= ring_size);
593
594 if (PREDICT_TRUE (start + n_buffers <= ring_size))
595 return vlib_buffer_alloc (vm, ring + start, n_buffers);
596
597 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
598
599 if (PREDICT_TRUE (n_alloc == ring_size - start))
600 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
601
602 return n_alloc;
603}
604
Damjan Marion910d3692019-01-21 11:48:34 +0100605/** \brief Allocate buffers into ring from specific buffer pool
606
607 @param vm - (vlib_main_t *) vlib main data structure pointer
608 @param buffers - (u32 * ) buffer index ring
609 @param start - (u32) first slot in the ring
610 @param ring_size - (u32) ring size
611 @param n_buffers - (u32) number of buffers requested
612 @return - (u32) number of buffers actually allocated, may be
613 less than the number requested or zero
614*/
615always_inline u32
616vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start,
617 u32 ring_size, u32 n_buffers,
618 u8 buffer_pool_index)
619{
620 u32 n_alloc;
621
622 ASSERT (n_buffers <= ring_size);
623
624 if (PREDICT_TRUE (start + n_buffers <= ring_size))
625 return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers,
626 buffer_pool_index);
627
628 n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start,
629 buffer_pool_index);
630
631 if (PREDICT_TRUE (n_alloc == ring_size - start))
632 n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc,
633 buffer_pool_index);
634
635 return n_alloc;
636}
637
638static void
639vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index,
640 u32 * buffers, u32 n_buffers)
641{
642 vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
643 vlib_buffer_pool_thread_t *bpt =
644 vec_elt_at_index (bp->threads, vm->thread_index);
645
646 vec_add_aligned (bpt->cached_buffers, buffers, n_buffers,
647 CLIB_CACHE_LINE_BYTES);
648
649 if (vec_len (bpt->cached_buffers) > 4 * VLIB_FRAME_SIZE)
650 {
651 clib_spinlock_lock (&bp->lock);
652 /* keep last stored buffers, as they are more likely hot in the cache */
653 vec_add_aligned (bp->buffers, bpt->cached_buffers, VLIB_FRAME_SIZE,
654 CLIB_CACHE_LINE_BYTES);
655 vec_delete (bpt->cached_buffers, VLIB_FRAME_SIZE, 0);
656 bpt->n_alloc -= VLIB_FRAME_SIZE;
657 clib_spinlock_unlock (&bp->lock);
658 }
659}
660
661static_always_inline void
662vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers,
663 int maybe_next)
664{
665 const int queue_size = 128;
666 vlib_buffer_pool_t *bp = 0;
667 u8 buffer_pool_index = ~0;
668 u32 n_queue = 0, queue[queue_size + 4];
669 vlib_buffer_t bt = { };
670#if defined(CLIB_HAVE_VEC128) && !__aarch64__
671 vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 };
672 vlib_buffer_t bpi_vec = {.buffer_pool_index = ~0 };
673 vlib_buffer_t flags_refs_mask = {
674 .flags = VLIB_BUFFER_NEXT_PRESENT,
675 .ref_count = ~0
676 };
677#endif
678
679 while (n_buffers)
680 {
681 vlib_buffer_t *b[8];
682 u32 bi, sum = 0, flags, next;
683
684 if (n_buffers < 12)
685 goto one_by_one;
686
687 vlib_get_buffers (vm, buffers, b, 4);
688 vlib_get_buffers (vm, buffers + 8, b + 4, 4);
689
690 vlib_prefetch_buffer_header (b[4], LOAD);
691 vlib_prefetch_buffer_header (b[5], LOAD);
692 vlib_prefetch_buffer_header (b[6], LOAD);
693 vlib_prefetch_buffer_header (b[7], LOAD);
694
695#if defined(CLIB_HAVE_VEC128) && !__aarch64__
696 u8x16 p0, p1, p2, p3, r;
697 p0 = u8x16_load_unaligned (b[0]);
698 p1 = u8x16_load_unaligned (b[1]);
699 p2 = u8x16_load_unaligned (b[2]);
700 p3 = u8x16_load_unaligned (b[3]);
701
702 r = p0 ^ bpi_vec.as_u8x16[0];
703 r |= p1 ^ bpi_vec.as_u8x16[0];
704 r |= p2 ^ bpi_vec.as_u8x16[0];
705 r |= p3 ^ bpi_vec.as_u8x16[0];
706 r &= bpi_mask.as_u8x16[0];
707 r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0];
708
709 sum = !u8x16_is_all_zero (r);
710#else
711 sum |= b[0]->flags;
712 sum |= b[1]->flags;
713 sum |= b[2]->flags;
714 sum |= b[3]->flags;
715 sum &= VLIB_BUFFER_NEXT_PRESENT;
716 sum += b[0]->ref_count - 1;
717 sum += b[1]->ref_count - 1;
718 sum += b[2]->ref_count - 1;
719 sum += b[3]->ref_count - 1;
720 sum |= b[0]->buffer_pool_index ^ buffer_pool_index;
721 sum |= b[1]->buffer_pool_index ^ buffer_pool_index;
722 sum |= b[2]->buffer_pool_index ^ buffer_pool_index;
723 sum |= b[3]->buffer_pool_index ^ buffer_pool_index;
724#endif
725
726 if (sum)
727 goto one_by_one;
728
729 vlib_buffer_copy_indices (queue + n_queue, buffers, 4);
730 vlib_buffer_copy_template (b[0], &bt);
731 vlib_buffer_copy_template (b[1], &bt);
732 vlib_buffer_copy_template (b[2], &bt);
733 vlib_buffer_copy_template (b[3], &bt);
734 n_queue += 4;
735
736 if (CLIB_DEBUG > 0)
737 vlib_buffer_validate_alloc_free (vm, buffers, 4,
738 VLIB_BUFFER_KNOWN_ALLOCATED);
739
Damjan Marion24dcbe42019-01-31 12:29:39 +0100740 vlib_buffer_validate (vm, b[0]);
741 vlib_buffer_validate (vm, b[1]);
742 vlib_buffer_validate (vm, b[2]);
743 vlib_buffer_validate (vm, b[3]);
744
Damjan Marion910d3692019-01-21 11:48:34 +0100745 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
746 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
747 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
748 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
749
750 if (n_queue >= queue_size)
751 {
752 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
753 n_queue = 0;
754 }
755 buffers += 4;
756 n_buffers -= 4;
757 continue;
758
759 one_by_one:
760 bi = buffers[0];
761
762 next_in_chain:
763 b[0] = vlib_get_buffer (vm, bi);
764 flags = b[0]->flags;
765 next = b[0]->next_buffer;
766
767 if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index))
768 {
769 buffer_pool_index = b[0]->buffer_pool_index;
770#if defined(CLIB_HAVE_VEC128) && !__aarch64__
771 bpi_vec.buffer_pool_index = buffer_pool_index;
772#endif
773 bp = vlib_get_buffer_pool (vm, buffer_pool_index);
774 vlib_buffer_copy_template (&bt, &bp->buffer_template);
775
776 if (n_queue)
777 {
778 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
779 n_queue = 0;
780 }
781 }
782
Damjan Marion24dcbe42019-01-31 12:29:39 +0100783 vlib_buffer_validate (vm, b[0]);
Damjan Marion910d3692019-01-21 11:48:34 +0100784
785 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
786
787 if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0)
788 {
789 if (CLIB_DEBUG > 0)
790 vlib_buffer_validate_alloc_free (vm, &bi, 1,
791 VLIB_BUFFER_KNOWN_ALLOCATED);
792 vlib_buffer_copy_template (b[0], &bt);
793 queue[n_queue++] = bi;
794 }
795
796 if (n_queue == queue_size)
797 {
798 vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size);
799 n_queue = 0;
800 }
801
802 if (flags & VLIB_BUFFER_NEXT_PRESENT)
803 {
804 bi = next;
805 goto next_in_chain;
806 }
807
808 buffers++;
809 n_buffers--;
810 }
811
812 if (n_queue)
813 vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue);
814}
815
816
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817/** \brief Free buffers
818 Frees the entire buffer chain for each buffer
819
820 @param vm - (vlib_main_t *) vlib main data structure pointer
821 @param buffers - (u32 * ) buffer index array
822 @param n_buffers - (u32) number of buffers to free
823
824*/
Damjan Marion878c6092017-01-04 13:19:27 +0100825always_inline void
826vlib_buffer_free (vlib_main_t * vm,
827 /* pointer to first buffer */
828 u32 * buffers,
829 /* number of buffers to free */
830 u32 n_buffers)
831{
Damjan Marion910d3692019-01-21 11:48:34 +0100832 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1);
Damjan Marion878c6092017-01-04 13:19:27 +0100833}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
835/** \brief Free buffers, does not free the buffer chain for each buffer
836
837 @param vm - (vlib_main_t *) vlib main data structure pointer
838 @param buffers - (u32 * ) buffer index array
839 @param n_buffers - (u32) number of buffers to free
840
841*/
Damjan Marion878c6092017-01-04 13:19:27 +0100842always_inline void
843vlib_buffer_free_no_next (vlib_main_t * vm,
844 /* pointer to first buffer */
845 u32 * buffers,
846 /* number of buffers to free */
847 u32 n_buffers)
848{
Damjan Marion910d3692019-01-21 11:48:34 +0100849 vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0);
Damjan Marion878c6092017-01-04 13:19:27 +0100850}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851
852/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
855 @param vm - (vlib_main_t *) vlib main data structure pointer
856 @param buffer_index - (u32) buffer index to free
857*/
858always_inline void
859vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
860{
Damjan Marion910d3692019-01-21 11:48:34 +0100861 vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862}
863
Damjan Mariona3731492018-02-25 22:50:39 +0100864/** \brief Free buffers from ring
865
866 @param vm - (vlib_main_t *) vlib main data structure pointer
867 @param buffers - (u32 * ) buffer index ring
868 @param start - (u32) first slot in the ring
869 @param ring_size - (u32) ring size
870 @param n_buffers - (u32) number of buffers
871*/
872always_inline void
873vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
874 u32 ring_size, u32 n_buffers)
875{
876 ASSERT (n_buffers <= ring_size);
877
878 if (PREDICT_TRUE (start + n_buffers <= ring_size))
879 {
880 vlib_buffer_free (vm, ring + start, n_buffers);
881 }
882 else
883 {
884 vlib_buffer_free (vm, ring + start, ring_size - start);
885 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
886 }
887}
888
Damjan Marioncef1db92018-03-28 18:27:38 +0200889/** \brief Free buffers from ring without freeing tail buffers
890
891 @param vm - (vlib_main_t *) vlib main data structure pointer
892 @param buffers - (u32 * ) buffer index ring
893 @param start - (u32) first slot in the ring
894 @param ring_size - (u32) ring size
895 @param n_buffers - (u32) number of buffers
896*/
897always_inline void
898vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
899 u32 ring_size, u32 n_buffers)
900{
901 ASSERT (n_buffers <= ring_size);
902
903 if (PREDICT_TRUE (start + n_buffers <= ring_size))
904 {
Damjan Marion4a973932018-06-09 19:29:16 +0200905 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +0200906 }
907 else
908 {
909 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
910 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
911 }
912}
Damjan Mariona3731492018-02-25 22:50:39 +0100913
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100915int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
916 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100918/* duplicate all buffers in chain */
919always_inline vlib_buffer_t *
920vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
921{
922 vlib_buffer_t *s, *d, *fd;
923 uword n_alloc, n_buffers = 1;
Damjan Marion67655492016-11-15 12:50:28 +0100924 u32 flag_mask = VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100925 int i;
926
927 s = b;
928 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
929 {
930 n_buffers++;
931 s = vlib_get_buffer (vm, s->next_buffer);
932 }
Neale Ranns9d676af2017-03-15 01:28:31 -0700933 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100934
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100935 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -0500936
937 /* No guarantee that we'll get all the buffers we asked for */
938 if (PREDICT_FALSE (n_alloc < n_buffers))
939 {
940 if (n_alloc > 0)
941 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -0500942 return 0;
943 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100944
945 /* 1st segment */
946 s = b;
947 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100948 d->current_data = s->current_data;
949 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +0100950 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100951 d->total_length_not_including_first_buffer =
952 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500953 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
954 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
955 clib_memcpy_fast (vlib_buffer_get_current (d),
956 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100957
958 /* next segments */
959 for (i = 1; i < n_buffers; i++)
960 {
961 /* previous */
962 d->next_buffer = new_buffers[i];
963 /* current */
964 s = vlib_get_buffer (vm, s->next_buffer);
965 d = vlib_get_buffer (vm, new_buffers[i]);
966 d->current_data = s->current_data;
967 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500968 clib_memcpy_fast (vlib_buffer_get_current (d),
969 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +0100970 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100971 }
972
973 return fd;
974}
975
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800976/** \brief Create a maximum of 256 clones of buffer and store them
977 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +0100978
979 @param vm - (vlib_main_t *) vlib main data structure pointer
980 @param src_buffer - (u32) source buffer index
981 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800982 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +0100983 @param head_end_offset - (u16) offset relative to current position
984 where packet head ends
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800985 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +0100986 less than the number requested or zero
987*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800988always_inline u16
989vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
990 u16 n_buffers, u16 head_end_offset)
Damjan Marionc47ed032017-01-25 14:18:03 +0100991{
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800992 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +0100993 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
994
Damjan Marion910d3692019-01-21 11:48:34 +0100995 ASSERT (s->ref_count == 1);
Damjan Marionc47ed032017-01-25 14:18:03 +0100996 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800997 ASSERT (n_buffers <= 256);
Damjan Marionc47ed032017-01-25 14:18:03 +0100998
999 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
1000 {
1001 buffers[0] = src_buffer;
1002 for (i = 1; i < n_buffers; i++)
1003 {
1004 vlib_buffer_t *d;
1005 d = vlib_buffer_copy (vm, s);
1006 if (d == 0)
1007 return i;
1008 buffers[i] = vlib_get_buffer_index (vm, d);
1009
1010 }
1011 return n_buffers;
1012 }
1013
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001014 if (PREDICT_FALSE (n_buffers == 1))
Damjan Marionc47ed032017-01-25 14:18:03 +01001015 {
1016 buffers[0] = src_buffer;
1017 return 1;
1018 }
1019
Damjan Marion910d3692019-01-21 11:48:34 +01001020 n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers,
1021 s->buffer_pool_index);
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001022
Damjan Marionc47ed032017-01-25 14:18:03 +01001023 for (i = 0; i < n_buffers; i++)
1024 {
1025 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
1026 d->current_data = s->current_data;
1027 d->current_length = head_end_offset;
Damjan Marion910d3692019-01-21 11:48:34 +01001028 ASSERT (d->buffer_pool_index == s->buffer_pool_index);
1029
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001030 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +01001031 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +02001032 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
1033 {
1034 d->total_length_not_including_first_buffer +=
1035 s->total_length_not_including_first_buffer;
1036 }
Damjan Marionc47ed032017-01-25 14:18:03 +01001037 d->flags = s->flags | VLIB_BUFFER_NEXT_PRESENT;
1038 d->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Dave Barach178cf492018-11-13 16:34:13 -05001039 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
1040 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
1041 clib_memcpy_fast (vlib_buffer_get_current (d),
1042 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +01001043 d->next_buffer = src_buffer;
1044 }
1045 vlib_buffer_advance (s, head_end_offset);
Damjan Marion910d3692019-01-21 11:48:34 +01001046 s->ref_count = n_buffers;
Damjan Marionc47ed032017-01-25 14:18:03 +01001047 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1048 {
1049 s = vlib_get_buffer (vm, s->next_buffer);
Damjan Marion910d3692019-01-21 11:48:34 +01001050 s->ref_count = n_buffers;
Damjan Marionc47ed032017-01-25 14:18:03 +01001051 }
1052
1053 return n_buffers;
1054}
1055
Neale Ranns8f36e4a2018-01-11 09:02:01 -08001056/** \brief Create multiple clones of buffer and store them
1057 in the supplied array
1058
1059 @param vm - (vlib_main_t *) vlib main data structure pointer
1060 @param src_buffer - (u32) source buffer index
1061 @param buffers - (u32 * ) buffer index array
1062 @param n_buffers - (u16) number of buffer clones requested (<=256)
1063 @param head_end_offset - (u16) offset relative to current position
1064 where packet head ends
1065 @return - (u16) number of buffers actually cloned, may be
1066 less than the number requested or zero
1067*/
1068always_inline u16
1069vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
1070 u16 n_buffers, u16 head_end_offset)
1071{
1072 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
1073 u16 n_cloned = 0;
1074
1075 while (n_buffers > 256)
1076 {
1077 vlib_buffer_t *copy;
1078 copy = vlib_buffer_copy (vm, s);
1079 n_cloned += vlib_buffer_clone_256 (vm,
1080 vlib_get_buffer_index (vm, copy),
1081 (buffers + n_cloned),
1082 256, head_end_offset);
1083 n_buffers -= 256;
1084 }
1085 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
1086 buffers + n_cloned,
1087 n_buffers, head_end_offset);
1088
1089 return n_cloned;
1090}
1091
Damjan Marionc47ed032017-01-25 14:18:03 +01001092/** \brief Attach cloned tail to the buffer
1093
1094 @param vm - (vlib_main_t *) vlib main data structure pointer
1095 @param head - (vlib_buffer_t *) head buffer
1096 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
1097*/
1098
1099always_inline void
1100vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
1101 vlib_buffer_t * tail)
1102{
1103 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marion910d3692019-01-21 11:48:34 +01001104 ASSERT (head->buffer_pool_index == tail->buffer_pool_index);
Damjan Marionc47ed032017-01-25 14:18:03 +01001105
1106 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
1107 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1108 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
1109 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
1110 head->next_buffer = vlib_get_buffer_index (vm, tail);
1111 head->total_length_not_including_first_buffer = tail->current_length +
1112 tail->total_length_not_including_first_buffer;
1113
1114next_segment:
Damjan Marion910d3692019-01-21 11:48:34 +01001115 clib_atomic_add_fetch (&tail->ref_count, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +01001116
1117 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
1118 {
1119 tail = vlib_get_buffer (vm, tail->next_buffer);
1120 goto next_segment;
1121 }
1122}
1123
Pierre Pfister328e99b2016-02-12 13:18:42 +00001124/* Initializes the buffer as an empty packet with no chained buffers. */
1125always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001126vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001127{
1128 first->total_length_not_including_first_buffer = 0;
1129 first->current_length = 0;
1130 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1131 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001132}
1133
1134/* The provided next_bi buffer index is appended to the end of the packet. */
1135always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +02001136vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001137{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001138 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001139 last->next_buffer = next_bi;
1140 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
1141 next_buffer->current_length = 0;
1142 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001143 return next_buffer;
1144}
1145
1146/* Increases or decreases the packet length.
1147 * It does not allocate or deallocate new buffers.
1148 * Therefore, the added length must be compatible
1149 * with the last buffer. */
1150always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001151vlib_buffer_chain_increase_length (vlib_buffer_t * first,
1152 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001153{
1154 last->current_length += len;
1155 if (first != last)
1156 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001157}
1158
1159/* Copy data to the end of the packet and increases its length.
1160 * It does not allocate new buffers.
1161 * Returns the number of copied bytes. */
1162always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001163vlib_buffer_chain_append_data (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001164 vlib_buffer_t * first,
1165 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +00001166{
Damjan Marion32353822019-01-20 02:24:53 +01001167 u32 n_buffer_bytes = VLIB_BUFFER_DATA_SIZE;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001168 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
1169 u16 len = clib_min (data_len,
1170 n_buffer_bytes - last->current_length -
1171 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -05001172 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
1173 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001174 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001175 return len;
1176}
1177
1178/* Copy data to the end of the packet and increases its length.
1179 * Allocates additional buffers from the free list if necessary.
1180 * Returns the number of copied bytes.
1181 * 'last' value is modified whenever new buffers are allocated and
1182 * chained and points to the last buffer in the chain. */
1183u16
Dave Barach9b8ffd92016-07-08 08:13:45 -04001184vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001185 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +01001186 vlib_buffer_t ** last, void *data,
1187 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001188void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001189
Dave Barach9b8ffd92016-07-08 08:13:45 -04001190format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
1191 format_vlib_buffer_contents;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
Dave Barach9b8ffd92016-07-08 08:13:45 -04001193typedef struct
1194{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001196 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197
Damjan Mariond1274cb2018-03-13 21:32:17 +01001198 /* Number of buffers to allocate in each call to allocator. */
1199 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001200
Damjan Marion671e60e2018-12-30 18:09:59 +01001201 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202} vlib_packet_template_t;
1203
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204void vlib_packet_template_init (vlib_main_t * vm,
1205 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001206 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +01001208 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001209 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210
Dave Barach9b8ffd92016-07-08 08:13:45 -04001211void *vlib_packet_template_get_packet (vlib_main_t * vm,
1212 vlib_packet_template_t * t,
1213 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
1215always_inline void
1216vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
1217{
1218 vec_free (t->packet_data);
1219}
1220
Klement Sekera75e7d132017-09-20 08:26:30 +02001221/**
1222 * @brief compress buffer chain in a way where the first buffer is at least
Damjan Marion910d3692019-01-21 11:48:34 +01001223 * VLIB_BUFFER_CLONE_HEAD_SIZE long
Klement Sekera75e7d132017-09-20 08:26:30 +02001224 *
1225 * @param[in] vm - vlib_main
1226 * @param[in,out] first - first buffer in chain
1227 * @param[in,out] discard_vector - vector of buffer indexes which were removed
1228 * from the chain
1229 */
1230always_inline void
1231vlib_buffer_chain_compress (vlib_main_t * vm,
1232 vlib_buffer_t * first, u32 ** discard_vector)
1233{
Damjan Marion910d3692019-01-21 11:48:34 +01001234 if (first->current_length >= VLIB_BUFFER_CLONE_HEAD_SIZE ||
Klement Sekera75e7d132017-09-20 08:26:30 +02001235 !(first->flags & VLIB_BUFFER_NEXT_PRESENT))
1236 {
1237 /* this is already big enough or not a chain */
1238 return;
1239 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001240
Damjan Marion910d3692019-01-21 11:48:34 +01001241 u32 want_first_size = clib_min (VLIB_BUFFER_CLONE_HEAD_SIZE,
Damjan Marion32353822019-01-20 02:24:53 +01001242 VLIB_BUFFER_DATA_SIZE -
Klement Sekera75e7d132017-09-20 08:26:30 +02001243 first->current_data);
1244 do
1245 {
1246 vlib_buffer_t *second = vlib_get_buffer (vm, first->next_buffer);
1247 u32 need = want_first_size - first->current_length;
1248 u32 amount_to_copy = clib_min (need, second->current_length);
Dave Barach178cf492018-11-13 16:34:13 -05001249 clib_memcpy_fast (((u8 *) vlib_buffer_get_current (first)) +
1250 first->current_length,
1251 vlib_buffer_get_current (second), amount_to_copy);
Klement Sekera75e7d132017-09-20 08:26:30 +02001252 first->current_length += amount_to_copy;
Klement Sekera69db1a62018-12-11 16:55:33 +01001253 second->current_data += amount_to_copy;
1254 second->current_length -= amount_to_copy;
Klement Sekera75e7d132017-09-20 08:26:30 +02001255 if (first->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
1256 {
1257 first->total_length_not_including_first_buffer -= amount_to_copy;
1258 }
1259 if (!second->current_length)
1260 {
1261 vec_add1 (*discard_vector, first->next_buffer);
1262 if (second->flags & VLIB_BUFFER_NEXT_PRESENT)
1263 {
1264 first->next_buffer = second->next_buffer;
1265 }
1266 else
1267 {
1268 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1269 }
1270 second->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1271 }
1272 }
1273 while ((first->current_length < want_first_size) &&
1274 (first->flags & VLIB_BUFFER_NEXT_PRESENT));
1275}
1276
Eyal Barid3d42412018-11-05 13:29:25 +02001277/**
1278 * @brief linearize buffer chain - the first buffer is filled, if needed,
1279 * buffers are allocated and filled, returns free space in last buffer or
1280 * negative on failure
1281 *
1282 * @param[in] vm - vlib_main
1283 * @param[in,out] first - first buffer in chain
1284 */
1285always_inline int
1286vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * first)
1287{
1288 vlib_buffer_t *b = first;
Damjan Marion32353822019-01-20 02:24:53 +01001289 u32 buf_len = VLIB_BUFFER_DATA_SIZE;
Eyal Barid3d42412018-11-05 13:29:25 +02001290 // free buffer chain starting from the second buffer
1291 int free_count = (b->flags & VLIB_BUFFER_NEXT_PRESENT) != 0;
1292 u32 chain_to_free = b->next_buffer;
1293
1294 u32 len = vlib_buffer_length_in_chain (vm, b);
1295 u32 free_len = buf_len - b->current_data - b->current_length;
1296 int alloc_len = clib_max (len - free_len, 0); //use the free len in the first buffer
1297 int n_buffers = (alloc_len + buf_len - 1) / buf_len;
1298 u32 new_buffers[n_buffers];
1299
1300 u32 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
1301 if (n_alloc != n_buffers)
1302 {
1303 vlib_buffer_free_no_next (vm, new_buffers, n_alloc);
1304 return -1;
1305 }
1306
1307 vlib_buffer_t *s = b;
1308 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1309 {
1310 s = vlib_get_buffer (vm, s->next_buffer);
1311 int d_free_len = buf_len - b->current_data - b->current_length;
1312 ASSERT (d_free_len >= 0);
1313 // chain buf and split write
1314 u32 copy_len = clib_min (d_free_len, s->current_length);
1315 u8 *d = vlib_buffer_put_uninit (b, copy_len);
1316 clib_memcpy (d, vlib_buffer_get_current (s), copy_len);
1317 int rest = s->current_length - copy_len;
1318 if (rest > 0)
1319 {
1320 //prev buf is full
1321 ASSERT (vlib_buffer_get_tail (b) == b->data + buf_len);
1322 ASSERT (n_buffers > 0);
1323 b = vlib_buffer_chain_buffer (vm, b, new_buffers[--n_buffers]);
1324 //make full use of the new buffers
1325 b->current_data = 0;
1326 d = vlib_buffer_put_uninit (b, rest);
1327 clib_memcpy (d, vlib_buffer_get_current (s) + copy_len, rest);
1328 }
1329 }
1330 vlib_buffer_free (vm, &chain_to_free, free_count);
1331 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1332 if (b == first) /* no buffers addeed */
1333 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1334 ASSERT (len == vlib_buffer_length_in_chain (vm, first));
1335 ASSERT (n_buffers == 0);
1336 return buf_len - b->current_data - b->current_length;
1337}
1338
Ed Warnickecb9cada2015-12-08 15:45:58 -07001339#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001340
1341/*
1342 * fd.io coding-style-patch-verification: ON
1343 *
1344 * Local Variables:
1345 * eval: (c-set-style "gnu")
1346 * End:
1347 */