blob: 1c1cd28e95f65de0286c77004a511d52f2d46d71 [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
50
51/** \brief Translate buffer index into buffer pointer
52
53 @param vm - (vlib_main_t *) vlib main data structure pointer
54 @param buffer_index - (u32) buffer index
55 @return - (vlib_buffer_t *) buffer pointer
Dave Barach9b8ffd92016-07-08 08:13:45 -040056*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070057always_inline vlib_buffer_t *
58vlib_get_buffer (vlib_main_t * vm, u32 buffer_index)
59{
Damjan Mariond50e3472019-01-20 00:03:56 +010060 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +020061 uword offset = ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES;
62 ASSERT (offset < bm->buffer_mem_size);
63
64 return uword_to_pointer (bm->buffer_mem_start + offset, void *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065}
66
Damjan Marione58041f2019-01-18 19:56:09 +010067static_always_inline void
Damjan Marion64d557c2019-01-18 20:03:41 +010068vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices)
69{
70 clib_memcpy_fast (dst, src, n_indices * sizeof (u32));
71}
72
Damjan Marion9a8a12a2019-01-23 16:52:10 +010073STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64);
Damjan Marion64d557c2019-01-18 20:03:41 +010074static_always_inline void
Damjan Marione58041f2019-01-18 19:56:09 +010075vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt)
76{
Damjan Marion22f23ae2019-01-24 15:36:57 +010077#if defined CLIB_HAVE_VEC512
78 b->as_u8x64[0] = bt->as_u8x64[0];
79#elif defined (CLIB_HAVE_VEC256)
Damjan Marion9a8a12a2019-01-23 16:52:10 +010080 b->as_u8x32[0] = bt->as_u8x32[0];
81 b->as_u8x32[1] = bt->as_u8x32[1];
82#elif defined (CLIB_HAVE_VEC128)
83 b->as_u8x16[0] = bt->as_u8x16[0];
84 b->as_u8x16[1] = bt->as_u8x16[1];
85 b->as_u8x16[2] = bt->as_u8x16[2];
86 b->as_u8x16[3] = bt->as_u8x16[3];
87#else
88 clib_memcpy_fast (b, bt, 64);
89#endif
Damjan Marione58041f2019-01-18 19:56:09 +010090}
91
Damjan Marionafe56de2018-05-17 12:44:00 +020092/** \brief Translate array of buffer indices into buffer pointers with offset
93
94 @param vm - (vlib_main_t *) vlib main data structure pointer
95 @param bi - (u32 *) array of buffer indices
96 @param b - (void **) array to store buffer pointers
97 @param count - (uword) number of elements
98 @param offset - (i32) offset applied to each pointer
99*/
100static_always_inline void
101vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count,
102 i32 offset)
103{
Damjan Mariond50e3472019-01-20 00:03:56 +0100104 uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
Damjan Marionafe56de2018-05-17 12:44:00 +0200105#ifdef CLIB_HAVE_VEC256
Damjan Mariond50e3472019-01-20 00:03:56 +0100106 u64x4 off = u64x4_splat (buffer_mem_start + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200107 /* if count is not const, compiler will not unroll while loop
108 se we maintain two-in-parallel variant */
109 while (count >= 8)
110 {
111 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
112 u64x4 b1 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi + 4));
113 /* shift and add to get vlib_buffer_t pointer */
114 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
115 u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4);
116 b += 8;
117 bi += 8;
118 count -= 8;
119 }
120#endif
121 while (count >= 4)
122 {
123#ifdef CLIB_HAVE_VEC256
124 u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi));
125 /* shift and add to get vlib_buffer_t pointer */
126 u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
Sirshak Das759226e2018-08-22 08:46:52 +0800127#elif defined (CLIB_HAVE_VEC128)
Damjan Mariond50e3472019-01-20 00:03:56 +0100128 u64x2 off = u64x2_splat (buffer_mem_start + offset);
Damjan Marion5df580e2018-07-27 01:47:57 +0200129 u32x4 bi4 = u32x4_load_unaligned (bi);
130 u64x2 b0 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800131#if defined (__aarch64__)
132 u64x2 b1 = u32x4_extend_to_u64x2_high ((u32x4) bi4);
133#else
Damjan Marion5df580e2018-07-27 01:47:57 +0200134 bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1);
135 u64x2 b1 = u32x4_extend_to_u64x2 ((u32x4) bi4);
Sirshak Das759226e2018-08-22 08:46:52 +0800136#endif
Damjan Marion5df580e2018-07-27 01:47:57 +0200137 u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b);
138 u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2);
Damjan Marionafe56de2018-05-17 12:44:00 +0200139#else
140 b[0] = ((u8 *) vlib_get_buffer (vm, bi[0])) + offset;
141 b[1] = ((u8 *) vlib_get_buffer (vm, bi[1])) + offset;
142 b[2] = ((u8 *) vlib_get_buffer (vm, bi[2])) + offset;
143 b[3] = ((u8 *) vlib_get_buffer (vm, bi[3])) + offset;
144#endif
145 b += 4;
146 bi += 4;
147 count -= 4;
148 }
149 while (count)
150 {
151 b[0] = ((u8 *) vlib_get_buffer (vm, bi[0])) + offset;
152 b += 1;
153 bi += 1;
154 count -= 1;
155 }
156}
157
158/** \brief Translate array of buffer indices into buffer pointers
159
160 @param vm - (vlib_main_t *) vlib main data structure pointer
161 @param bi - (u32 *) array of buffer indices
162 @param b - (vlib_buffer_t **) array to store buffer pointers
163 @param count - (uword) number of elements
164*/
165
166static_always_inline void
167vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count)
168{
169 vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0);
170}
171
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172/** \brief Translate buffer pointer into buffer index
173
174 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400175 @param p - (void *) buffer pointer
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 @return - (u32) buffer index
Dave Barach9b8ffd92016-07-08 08:13:45 -0400177*/
Damjan Marion04a7f052017-07-10 15:06:17 +0200178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179always_inline u32
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180vlib_get_buffer_index (vlib_main_t * vm, void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181{
Damjan Mariond50e3472019-01-20 00:03:56 +0100182 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion04a7f052017-07-10 15:06:17 +0200183 uword offset = pointer_to_uword (p) - bm->buffer_mem_start;
184 ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start);
185 ASSERT (offset < bm->buffer_mem_size);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400186 ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 return offset >> CLIB_LOG2_CACHE_LINE_BYTES;
188}
189
Damjan Marionafe56de2018-05-17 12:44:00 +0200190/** \brief Translate array of buffer pointers into buffer indices with offset
191
192 @param vm - (vlib_main_t *) vlib main data structure pointer
193 @param b - (void **) array of buffer pointers
194 @param bi - (u32 *) array to store buffer indices
195 @param count - (uword) number of elements
196 @param offset - (i32) offset applied to each pointer
197*/
198static_always_inline void
199vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi,
200 uword count, i32 offset)
201{
202#ifdef CLIB_HAVE_VEC256
203 u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 };
Damjan Mariond50e3472019-01-20 00:03:56 +0100204 u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200205
206 while (count >= 8)
207 {
208 /* load 4 pointers into 256-bit register */
209 u64x4 v0 = u64x4_load_unaligned (b);
210 u64x4 v1 = u64x4_load_unaligned (b + 4);
211 u32x8 v2, v3;
212
213 v0 -= off4;
214 v1 -= off4;
215
216 v0 >>= CLIB_LOG2_CACHE_LINE_BYTES;
217 v1 >>= CLIB_LOG2_CACHE_LINE_BYTES;
218
219 /* permute 256-bit register so lower u32s of each buffer index are
220 * placed into lower 128-bits */
221 v2 = u32x8_permute ((u32x8) v0, mask);
222 v3 = u32x8_permute ((u32x8) v1, mask);
223
224 /* extract lower 128-bits and save them to the array of buffer indices */
225 u32x4_store_unaligned (u32x8_extract_lo (v2), bi);
226 u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4);
227 bi += 8;
228 b += 8;
229 count -= 8;
230 }
231#endif
232 while (count >= 4)
233 {
234 /* equivalent non-nector implementation */
235 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
236 bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset);
237 bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset);
238 bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset);
239 bi += 4;
240 b += 4;
241 count -= 4;
242 }
243 while (count)
244 {
Zhiyong Yang462072a2018-05-30 22:12:57 -0400245 bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset);
Damjan Marionafe56de2018-05-17 12:44:00 +0200246 bi += 1;
247 b += 1;
248 count -= 1;
249 }
250}
251
252/** \brief Translate array of buffer pointers into buffer indices
253
254 @param vm - (vlib_main_t *) vlib main data structure pointer
255 @param b - (vlib_buffer_t **) array of buffer pointers
256 @param bi - (u32 *) array to store buffer indices
257 @param count - (uword) number of elements
258*/
259static_always_inline void
260vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi,
261 uword count)
262{
263 vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0);
264}
265
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266/** \brief Get next buffer in buffer linklist, or zero for end of list.
267
268 @param vm - (vlib_main_t *) vlib main data structure pointer
269 @param b - (void *) buffer pointer
270 @return - (vlib_buffer_t *) next buffer, or NULL
Dave Barach9b8ffd92016-07-08 08:13:45 -0400271*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272always_inline vlib_buffer_t *
273vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b)
274{
275 return (b->flags & VLIB_BUFFER_NEXT_PRESENT
Dave Barach9b8ffd92016-07-08 08:13:45 -0400276 ? vlib_get_buffer (vm, b->next_buffer) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277}
278
Dave Barach9b8ffd92016-07-08 08:13:45 -0400279uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm,
280 vlib_buffer_t * b_first);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
282/** \brief Get length in bytes of the buffer chain
283
284 @param vm - (vlib_main_t *) vlib main data structure pointer
285 @param b - (void *) buffer pointer
286 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400287*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288always_inline uword
289vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b)
290{
Damjan Marion072401e2017-07-13 18:53:27 +0200291 uword len = b->current_length;
292
293 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
294 return len;
295
296 if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
297 return len + b->total_length_not_including_first_buffer;
298
299 return vlib_buffer_length_in_chain_slow_path (vm, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300}
301
302/** \brief Get length in bytes of the buffer index buffer chain
303
304 @param vm - (vlib_main_t *) vlib main data structure pointer
305 @param bi - (u32) buffer index
306 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400307*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308always_inline uword
309vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi)
310{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400311 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 return vlib_buffer_length_in_chain (vm, b);
313}
314
315/** \brief Copy buffer contents to memory
316
317 @param vm - (vlib_main_t *) vlib main data structure pointer
Chris Luked4024f52016-09-06 09:32:36 -0400318 @param buffer_index - (u32) buffer index
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 @param contents - (u8 *) memory, <strong>must be large enough</strong>
320 @return - (uword) length of buffer chain
Dave Barach9b8ffd92016-07-08 08:13:45 -0400321*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322always_inline uword
323vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents)
324{
325 uword content_len = 0;
326 uword l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400327 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328
329 while (1)
330 {
331 b = vlib_get_buffer (vm, buffer_index);
332 l = b->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500333 clib_memcpy_fast (contents + content_len, b->data + b->current_data, l);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 content_len += l;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400335 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 break;
337 buffer_index = b->next_buffer;
338 }
339
340 return content_len;
341}
342
Damjan Marion8f499362018-10-22 13:07:02 +0200343always_inline uword
344vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345{
Damjan Marion68b4da62018-09-30 18:26:20 +0200346 return vlib_physmem_get_pa (vm, b->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347}
348
Damjan Marion8f499362018-10-22 13:07:02 +0200349always_inline uword
350vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b)
351{
352 return vlib_buffer_get_pa (vm, b) + b->current_data;
353}
354
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355/** \brief Prefetch buffer metadata by buffer index
356 The first 64 bytes of buffer contains most header information
357
358 @param vm - (vlib_main_t *) vlib main data structure pointer
359 @param bi - (u32) buffer index
360 @param type - LOAD, STORE. In most cases, STORE is the right answer
361*/
362/* Prefetch buffer header given index. */
363#define vlib_prefetch_buffer_with_index(vm,bi,type) \
364 do { \
365 vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \
366 vlib_prefetch_buffer_header (_b, type); \
367 } while (0)
368
Dave Barach9b8ffd92016-07-08 08:13:45 -0400369typedef enum
370{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 /* Index is unknown. */
372 VLIB_BUFFER_UNKNOWN,
373
374 /* Index is known and free/allocated. */
375 VLIB_BUFFER_KNOWN_FREE,
376 VLIB_BUFFER_KNOWN_ALLOCATED,
377} vlib_buffer_known_state_t;
378
Damjan Marionc8a26c62017-11-24 20:15:23 +0100379void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers,
380 uword n_buffers,
381 vlib_buffer_known_state_t
382 expected_state);
383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384always_inline vlib_buffer_known_state_t
Damjan Mariond50e3472019-01-20 00:03:56 +0100385vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386{
Damjan Mariond50e3472019-01-20 00:03:56 +0100387 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Damjan Marion6b0f5892017-07-27 04:01:24 -0400389 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 uword *p = hash_get (bm->buffer_known_hash, buffer_index);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400391 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 return p ? p[0] : VLIB_BUFFER_UNKNOWN;
393}
394
395always_inline void
Damjan Mariond50e3472019-01-20 00:03:56 +0100396vlib_buffer_set_known_state (vlib_main_t * vm, u32 buffer_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 vlib_buffer_known_state_t state)
398{
Damjan Mariond50e3472019-01-20 00:03:56 +0100399 vlib_buffer_main_t *bm = vm->buffer_main;
Steven899a84b2018-01-29 20:09:09 -0800400
Damjan Marion6b0f5892017-07-27 04:01:24 -0400401 clib_spinlock_lock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 hash_set (bm->buffer_known_hash, buffer_index, state);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400403 clib_spinlock_unlock (&bm->buffer_known_hash_lockp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404}
405
406/* Validates sanity of a single buffer.
407 Returns format'ed vector with error message if any. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400408u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index,
409 uword follow_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411always_inline u32
412vlib_buffer_round_size (u32 size)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400413{
414 return round_pow2 (size, sizeof (vlib_buffer_t));
415}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416
417/** \brief Allocate buffers from specific freelist into supplied array
418
419 @param vm - (vlib_main_t *) vlib main data structure pointer
420 @param buffers - (u32 * ) buffer index array
421 @param n_buffers - (u32) number of buffers requested
Dave Barach9b8ffd92016-07-08 08:13:45 -0400422 @return - (u32) number of buffers actually allocated, may be
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 less than the number requested or zero
424*/
Damjan Marion878c6092017-01-04 13:19:27 +0100425always_inline u32
426vlib_buffer_alloc_from_free_list (vlib_main_t * vm,
427 u32 * buffers,
Damjan Mariondac03522018-02-01 15:30:13 +0100428 u32 n_buffers,
429 vlib_buffer_free_list_index_t index)
Damjan Marion878c6092017-01-04 13:19:27 +0100430{
Damjan Marionc8a26c62017-11-24 20:15:23 +0100431 vlib_buffer_free_list_t *fl;
Damjan Mariond50e3472019-01-20 00:03:56 +0100432 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100433 u32 *src;
434 uword len;
Damjan Marion878c6092017-01-04 13:19:27 +0100435
Damjan Marionc8a26c62017-11-24 20:15:23 +0100436 ASSERT (bm->cb.vlib_buffer_fill_free_list_cb);
Damjan Marion878c6092017-01-04 13:19:27 +0100437
Damjan Mariond1274cb2018-03-13 21:32:17 +0100438 fl = pool_elt_at_index (vm->buffer_free_list_pool, index);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100439
440 len = vec_len (fl->buffers);
441
442 if (PREDICT_FALSE (len < n_buffers))
443 {
444 bm->cb.vlib_buffer_fill_free_list_cb (vm, fl, n_buffers);
Damjan Marionc22fcba2018-03-06 18:46:54 +0100445 if (PREDICT_FALSE ((len = vec_len (fl->buffers)) == 0))
446 return 0;
Damjan Marionc8a26c62017-11-24 20:15:23 +0100447
448 /* even if fill free list didn't manage to refill free list
449 we should give what we have */
450 n_buffers = clib_min (len, n_buffers);
451
452 /* following code is intentionaly duplicated to allow compiler
453 to optimize fast path when n_buffers is constant value */
454 src = fl->buffers + len - n_buffers;
Damjan Marion64d557c2019-01-18 20:03:41 +0100455 vlib_buffer_copy_indices (buffers, src, n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100456 _vec_len (fl->buffers) -= n_buffers;
457
458 /* Verify that buffers are known free. */
459 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
460 VLIB_BUFFER_KNOWN_FREE);
461
462 return n_buffers;
463 }
464
465 src = fl->buffers + len - n_buffers;
Damjan Marion64d557c2019-01-18 20:03:41 +0100466 vlib_buffer_copy_indices (buffers, src, n_buffers);
Damjan Marionc8a26c62017-11-24 20:15:23 +0100467 _vec_len (fl->buffers) -= n_buffers;
468
469 /* Verify that buffers are known free. */
470 vlib_buffer_validate_alloc_free (vm, buffers, n_buffers,
471 VLIB_BUFFER_KNOWN_FREE);
472
473 return n_buffers;
474}
475
476/** \brief Allocate buffers into supplied array
477
478 @param vm - (vlib_main_t *) vlib main data structure pointer
479 @param buffers - (u32 * ) buffer index array
480 @param n_buffers - (u32) number of buffers requested
481 @return - (u32) number of buffers actually allocated, may be
482 less than the number requested or zero
483*/
484always_inline u32
485vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers)
486{
487 return vlib_buffer_alloc_from_free_list (vm, buffers, n_buffers,
488 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Damjan Marion878c6092017-01-04 13:19:27 +0100489}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490
Damjan Marionc58408c2018-01-18 14:54:04 +0100491/** \brief Allocate buffers into ring
492
493 @param vm - (vlib_main_t *) vlib main data structure pointer
494 @param buffers - (u32 * ) buffer index ring
495 @param start - (u32) first slot in the ring
496 @param ring_size - (u32) ring size
497 @param n_buffers - (u32) number of buffers requested
498 @return - (u32) number of buffers actually allocated, may be
499 less than the number requested or zero
500*/
501always_inline u32
502vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start,
503 u32 ring_size, u32 n_buffers)
504{
505 u32 n_alloc;
506
507 ASSERT (n_buffers <= ring_size);
508
509 if (PREDICT_TRUE (start + n_buffers <= ring_size))
510 return vlib_buffer_alloc (vm, ring + start, n_buffers);
511
512 n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start);
513
514 if (PREDICT_TRUE (n_alloc == ring_size - start))
515 n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc);
516
517 return n_alloc;
518}
519
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520/** \brief Free buffers
521 Frees the entire buffer chain for each buffer
522
523 @param vm - (vlib_main_t *) vlib main data structure pointer
524 @param buffers - (u32 * ) buffer index array
525 @param n_buffers - (u32) number of buffers to free
526
527*/
Damjan Marion878c6092017-01-04 13:19:27 +0100528always_inline void
529vlib_buffer_free (vlib_main_t * vm,
530 /* pointer to first buffer */
531 u32 * buffers,
532 /* number of buffers to free */
533 u32 n_buffers)
534{
Damjan Mariond50e3472019-01-20 00:03:56 +0100535 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion878c6092017-01-04 13:19:27 +0100536
537 ASSERT (bm->cb.vlib_buffer_free_cb);
538
539 return bm->cb.vlib_buffer_free_cb (vm, buffers, n_buffers);
540}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
542/** \brief Free buffers, does not free the buffer chain for each buffer
543
544 @param vm - (vlib_main_t *) vlib main data structure pointer
545 @param buffers - (u32 * ) buffer index array
546 @param n_buffers - (u32) number of buffers to free
547
548*/
Damjan Marion878c6092017-01-04 13:19:27 +0100549always_inline void
550vlib_buffer_free_no_next (vlib_main_t * vm,
551 /* pointer to first buffer */
552 u32 * buffers,
553 /* number of buffers to free */
554 u32 n_buffers)
555{
Damjan Mariond50e3472019-01-20 00:03:56 +0100556 vlib_buffer_main_t *bm = vm->buffer_main;
Damjan Marion878c6092017-01-04 13:19:27 +0100557
558 ASSERT (bm->cb.vlib_buffer_free_no_next_cb);
559
560 return bm->cb.vlib_buffer_free_no_next_cb (vm, buffers, n_buffers);
561}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
563/** \brief Free one buffer
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 Shorthand to free a single buffer chain.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
566 @param vm - (vlib_main_t *) vlib main data structure pointer
567 @param buffer_index - (u32) buffer index to free
568*/
569always_inline void
570vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index)
571{
572 vlib_buffer_free (vm, &buffer_index, /* n_buffers */ 1);
573}
574
Damjan Mariona3731492018-02-25 22:50:39 +0100575/** \brief Free buffers from ring
576
577 @param vm - (vlib_main_t *) vlib main data structure pointer
578 @param buffers - (u32 * ) buffer index ring
579 @param start - (u32) first slot in the ring
580 @param ring_size - (u32) ring size
581 @param n_buffers - (u32) number of buffers
582*/
583always_inline void
584vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start,
585 u32 ring_size, u32 n_buffers)
586{
587 ASSERT (n_buffers <= ring_size);
588
589 if (PREDICT_TRUE (start + n_buffers <= ring_size))
590 {
591 vlib_buffer_free (vm, ring + start, n_buffers);
592 }
593 else
594 {
595 vlib_buffer_free (vm, ring + start, ring_size - start);
596 vlib_buffer_free (vm, ring, n_buffers - (ring_size - start));
597 }
598}
599
Damjan Marioncef1db92018-03-28 18:27:38 +0200600/** \brief Free buffers from ring without freeing tail buffers
601
602 @param vm - (vlib_main_t *) vlib main data structure pointer
603 @param buffers - (u32 * ) buffer index ring
604 @param start - (u32) first slot in the ring
605 @param ring_size - (u32) ring size
606 @param n_buffers - (u32) number of buffers
607*/
608always_inline void
609vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start,
610 u32 ring_size, u32 n_buffers)
611{
612 ASSERT (n_buffers <= ring_size);
613
614 if (PREDICT_TRUE (start + n_buffers <= ring_size))
615 {
Damjan Marion4a973932018-06-09 19:29:16 +0200616 vlib_buffer_free_no_next (vm, ring + start, n_buffers);
Damjan Marioncef1db92018-03-28 18:27:38 +0200617 }
618 else
619 {
620 vlib_buffer_free_no_next (vm, ring + start, ring_size - start);
621 vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start));
622 }
623}
Damjan Mariona3731492018-02-25 22:50:39 +0100624
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625/* Add/delete buffer free lists. */
Damjan Mariondac03522018-02-01 15:30:13 +0100626vlib_buffer_free_list_index_t vlib_buffer_create_free_list (vlib_main_t * vm,
627 u32 n_data_bytes,
628 char *fmt, ...);
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100629
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630/* Append given data to end of buffer, possibly allocating new buffers. */
Damjan Marionab9b7ec2019-01-18 20:24:44 +0100631int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data,
632 u32 n_data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100634/* duplicate all buffers in chain */
635always_inline vlib_buffer_t *
636vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b)
637{
638 vlib_buffer_t *s, *d, *fd;
639 uword n_alloc, n_buffers = 1;
Damjan Marion67655492016-11-15 12:50:28 +0100640 u32 flag_mask = VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100641 int i;
642
643 s = b;
644 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
645 {
646 n_buffers++;
647 s = vlib_get_buffer (vm, s->next_buffer);
648 }
Neale Ranns9d676af2017-03-15 01:28:31 -0700649 u32 new_buffers[n_buffers];
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100650
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100651 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
Dave Barach26cd8c12017-02-23 17:11:26 -0500652
653 /* No guarantee that we'll get all the buffers we asked for */
654 if (PREDICT_FALSE (n_alloc < n_buffers))
655 {
656 if (n_alloc > 0)
657 vlib_buffer_free (vm, new_buffers, n_alloc);
Dave Barach26cd8c12017-02-23 17:11:26 -0500658 return 0;
659 }
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100660
661 /* 1st segment */
662 s = b;
663 fd = d = vlib_get_buffer (vm, new_buffers[0]);
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100664 d->current_data = s->current_data;
665 d->current_length = s->current_length;
Damjan Marion67655492016-11-15 12:50:28 +0100666 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100667 d->total_length_not_including_first_buffer =
668 s->total_length_not_including_first_buffer;
Dave Barach178cf492018-11-13 16:34:13 -0500669 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
670 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
671 clib_memcpy_fast (vlib_buffer_get_current (d),
672 vlib_buffer_get_current (s), s->current_length);
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100673
674 /* next segments */
675 for (i = 1; i < n_buffers; i++)
676 {
677 /* previous */
678 d->next_buffer = new_buffers[i];
679 /* current */
680 s = vlib_get_buffer (vm, s->next_buffer);
681 d = vlib_get_buffer (vm, new_buffers[i]);
682 d->current_data = s->current_data;
683 d->current_length = s->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500684 clib_memcpy_fast (vlib_buffer_get_current (d),
685 vlib_buffer_get_current (s), s->current_length);
Damjan Marion67655492016-11-15 12:50:28 +0100686 d->flags = s->flags & flag_mask;
Damjan Marion05ab8cb2016-11-03 20:16:04 +0100687 }
688
689 return fd;
690}
691
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800692/** \brief Create a maximum of 256 clones of buffer and store them
693 in the supplied array
Damjan Marionc47ed032017-01-25 14:18:03 +0100694
695 @param vm - (vlib_main_t *) vlib main data structure pointer
696 @param src_buffer - (u32) source buffer index
697 @param buffers - (u32 * ) buffer index array
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800698 @param n_buffers - (u16) number of buffer clones requested (<=256)
Damjan Marionc47ed032017-01-25 14:18:03 +0100699 @param head_end_offset - (u16) offset relative to current position
700 where packet head ends
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800701 @return - (u16) number of buffers actually cloned, may be
Damjan Marionc47ed032017-01-25 14:18:03 +0100702 less than the number requested or zero
703*/
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800704always_inline u16
705vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
706 u16 n_buffers, u16 head_end_offset)
Damjan Marionc47ed032017-01-25 14:18:03 +0100707{
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800708 u16 i;
Damjan Marionc47ed032017-01-25 14:18:03 +0100709 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
710
711 ASSERT (s->n_add_refs == 0);
712 ASSERT (n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800713 ASSERT (n_buffers <= 256);
Damjan Marionc47ed032017-01-25 14:18:03 +0100714
715 if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2)
716 {
717 buffers[0] = src_buffer;
718 for (i = 1; i < n_buffers; i++)
719 {
720 vlib_buffer_t *d;
721 d = vlib_buffer_copy (vm, s);
722 if (d == 0)
723 return i;
724 buffers[i] = vlib_get_buffer_index (vm, d);
725
726 }
727 return n_buffers;
728 }
729
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800730 if (PREDICT_FALSE (n_buffers == 1))
Damjan Marionc47ed032017-01-25 14:18:03 +0100731 {
732 buffers[0] = src_buffer;
733 return 1;
734 }
735
Damjan Marion36eb7c22019-01-18 20:45:30 +0100736 n_buffers = vlib_buffer_alloc (vm, buffers, n_buffers);
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800737
Damjan Marionc47ed032017-01-25 14:18:03 +0100738 for (i = 0; i < n_buffers; i++)
739 {
740 vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]);
741 d->current_data = s->current_data;
742 d->current_length = head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +0200743 d->total_length_not_including_first_buffer = s->current_length -
Damjan Marionc47ed032017-01-25 14:18:03 +0100744 head_end_offset;
Yoann Desmouceaux1977a342018-05-29 13:38:44 +0200745 if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT))
746 {
747 d->total_length_not_including_first_buffer +=
748 s->total_length_not_including_first_buffer;
749 }
Damjan Marionc47ed032017-01-25 14:18:03 +0100750 d->flags = s->flags | VLIB_BUFFER_NEXT_PRESENT;
751 d->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Dave Barach178cf492018-11-13 16:34:13 -0500752 clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque));
753 clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2));
754 clib_memcpy_fast (vlib_buffer_get_current (d),
755 vlib_buffer_get_current (s), head_end_offset);
Damjan Marionc47ed032017-01-25 14:18:03 +0100756 d->next_buffer = src_buffer;
757 }
758 vlib_buffer_advance (s, head_end_offset);
759 s->n_add_refs = n_buffers - 1;
760 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
761 {
762 s = vlib_get_buffer (vm, s->next_buffer);
763 s->n_add_refs = n_buffers - 1;
764 }
765
766 return n_buffers;
767}
768
Neale Ranns8f36e4a2018-01-11 09:02:01 -0800769/** \brief Create multiple clones of buffer and store them
770 in the supplied array
771
772 @param vm - (vlib_main_t *) vlib main data structure pointer
773 @param src_buffer - (u32) source buffer index
774 @param buffers - (u32 * ) buffer index array
775 @param n_buffers - (u16) number of buffer clones requested (<=256)
776 @param head_end_offset - (u16) offset relative to current position
777 where packet head ends
778 @return - (u16) number of buffers actually cloned, may be
779 less than the number requested or zero
780*/
781always_inline u16
782vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers,
783 u16 n_buffers, u16 head_end_offset)
784{
785 vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer);
786 u16 n_cloned = 0;
787
788 while (n_buffers > 256)
789 {
790 vlib_buffer_t *copy;
791 copy = vlib_buffer_copy (vm, s);
792 n_cloned += vlib_buffer_clone_256 (vm,
793 vlib_get_buffer_index (vm, copy),
794 (buffers + n_cloned),
795 256, head_end_offset);
796 n_buffers -= 256;
797 }
798 n_cloned += vlib_buffer_clone_256 (vm, src_buffer,
799 buffers + n_cloned,
800 n_buffers, head_end_offset);
801
802 return n_cloned;
803}
804
Damjan Marionc47ed032017-01-25 14:18:03 +0100805/** \brief Attach cloned tail to the buffer
806
807 @param vm - (vlib_main_t *) vlib main data structure pointer
808 @param head - (vlib_buffer_t *) head buffer
809 @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head
810*/
811
812always_inline void
813vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head,
814 vlib_buffer_t * tail)
815{
816 ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Marionc47ed032017-01-25 14:18:03 +0100817
818 head->flags |= VLIB_BUFFER_NEXT_PRESENT;
819 head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
820 head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
821 head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID);
822 head->next_buffer = vlib_get_buffer_index (vm, tail);
823 head->total_length_not_including_first_buffer = tail->current_length +
824 tail->total_length_not_including_first_buffer;
825
826next_segment:
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000827 clib_atomic_add_fetch (&tail->n_add_refs, 1);
Damjan Marionc47ed032017-01-25 14:18:03 +0100828
829 if (tail->flags & VLIB_BUFFER_NEXT_PRESENT)
830 {
831 tail = vlib_get_buffer (vm, tail->next_buffer);
832 goto next_segment;
833 }
834}
835
Pierre Pfister328e99b2016-02-12 13:18:42 +0000836/* Initializes the buffer as an empty packet with no chained buffers. */
837always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400838vlib_buffer_chain_init (vlib_buffer_t * first)
Pierre Pfister328e99b2016-02-12 13:18:42 +0000839{
840 first->total_length_not_including_first_buffer = 0;
841 first->current_length = 0;
842 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
843 first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
Pierre Pfister328e99b2016-02-12 13:18:42 +0000844}
845
846/* The provided next_bi buffer index is appended to the end of the packet. */
847always_inline vlib_buffer_t *
Eyal Barib688fb12018-11-12 16:13:49 +0200848vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi)
Pierre Pfister328e99b2016-02-12 13:18:42 +0000849{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000851 last->next_buffer = next_bi;
852 last->flags |= VLIB_BUFFER_NEXT_PRESENT;
853 next_buffer->current_length = 0;
854 next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
Pierre Pfister328e99b2016-02-12 13:18:42 +0000855 return next_buffer;
856}
857
858/* Increases or decreases the packet length.
859 * It does not allocate or deallocate new buffers.
860 * Therefore, the added length must be compatible
861 * with the last buffer. */
862always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400863vlib_buffer_chain_increase_length (vlib_buffer_t * first,
864 vlib_buffer_t * last, i32 len)
Pierre Pfister328e99b2016-02-12 13:18:42 +0000865{
866 last->current_length += len;
867 if (first != last)
868 first->total_length_not_including_first_buffer += len;
Pierre Pfister328e99b2016-02-12 13:18:42 +0000869}
870
871/* Copy data to the end of the packet and increases its length.
872 * It does not allocate new buffers.
873 * Returns the number of copied bytes. */
874always_inline u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400875vlib_buffer_chain_append_data (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100876 vlib_buffer_free_list_index_t free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400877 vlib_buffer_t * first,
878 vlib_buffer_t * last, void *data, u16 data_len)
Pierre Pfister328e99b2016-02-12 13:18:42 +0000879{
Damjan Marion32353822019-01-20 02:24:53 +0100880 u32 n_buffer_bytes = VLIB_BUFFER_DATA_SIZE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400881 ASSERT (n_buffer_bytes >= last->current_length + last->current_data);
882 u16 len = clib_min (data_len,
883 n_buffer_bytes - last->current_length -
884 last->current_data);
Dave Barach178cf492018-11-13 16:34:13 -0500885 clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length,
886 data, len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400887 vlib_buffer_chain_increase_length (first, last, len);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000888 return len;
889}
890
891/* Copy data to the end of the packet and increases its length.
892 * Allocates additional buffers from the free list if necessary.
893 * Returns the number of copied bytes.
894 * 'last' value is modified whenever new buffers are allocated and
895 * chained and points to the last buffer in the chain. */
896u16
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm,
Damjan Mariondac03522018-02-01 15:30:13 +0100898 vlib_buffer_free_list_index_t
899 free_list_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400900 vlib_buffer_t * first,
Damjan Mariondac03522018-02-01 15:30:13 +0100901 vlib_buffer_t ** last, void *data,
902 u16 data_len);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400903void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first);
Pierre Pfister328e99b2016-02-12 13:18:42 +0000904
Dave Barach9b8ffd92016-07-08 08:13:45 -0400905format_function_t format_vlib_buffer, format_vlib_buffer_and_data,
906 format_vlib_buffer_contents;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907
Dave Barach9b8ffd92016-07-08 08:13:45 -0400908typedef struct
909{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910 /* Vector of packet data. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400911 u8 *packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
Damjan Mariond1274cb2018-03-13 21:32:17 +0100913 /* Number of buffers to allocate in each call to allocator. */
914 u32 min_n_buffers_each_alloc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915
916 /* Buffer free list for this template. */
Damjan Mariondac03522018-02-01 15:30:13 +0100917 vlib_buffer_free_list_index_t free_list_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918
Dave Barach9b8ffd92016-07-08 08:13:45 -0400919 u32 *free_buffers;
Damjan Marion671e60e2018-12-30 18:09:59 +0100920
921 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922} vlib_packet_template_t;
923
924void vlib_packet_template_get_packet_helper (vlib_main_t * vm,
925 vlib_packet_template_t * t);
926
927void vlib_packet_template_init (vlib_main_t * vm,
928 vlib_packet_template_t * t,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400929 void *packet_data,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 uword n_packet_data_bytes,
Damjan Mariond1274cb2018-03-13 21:32:17 +0100931 uword min_n_buffers_each_alloc,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400932 char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933
Dave Barach9b8ffd92016-07-08 08:13:45 -0400934void *vlib_packet_template_get_packet (vlib_main_t * vm,
935 vlib_packet_template_t * t,
936 u32 * bi_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
938always_inline void
939vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t)
940{
941 vec_free (t->packet_data);
942}
943
Damjan Mariond50e3472019-01-20 00:03:56 +0100944static_always_inline vlib_buffer_pool_t *
945vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index)
946{
947 vlib_buffer_main_t *bm = vm->buffer_main;
948 return vec_elt_at_index (bm->buffer_pools, buffer_pool_index);
949}
950
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951always_inline void
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100952vlib_buffer_add_to_free_list (vlib_main_t * vm,
953 vlib_buffer_free_list_t * f,
954 u32 buffer_index, u8 do_init)
955{
Damjan Mariond50e3472019-01-20 00:03:56 +0100956 vlib_buffer_pool_t *bp = vlib_buffer_pool_get (vm, f->buffer_pool_index);
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100957 vlib_buffer_t *b;
958 b = vlib_get_buffer (vm, buffer_index);
959 if (PREDICT_TRUE (do_init))
Damjan Marion64344302019-01-20 00:11:22 +0100960 clib_memset (b, 0, STRUCT_OFFSET_OF (vlib_buffer_t, template_end));
Damjan Marionbd69a5f2017-02-05 23:44:42 +0100961 vec_add1_aligned (f->buffers, buffer_index, CLIB_CACHE_LINE_BYTES);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400962
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200963 if (vec_len (f->buffers) > 4 * VLIB_FRAME_SIZE)
Damjan Marion6b0f5892017-07-27 04:01:24 -0400964 {
Damjan Mariond1274cb2018-03-13 21:32:17 +0100965 clib_spinlock_lock (&bp->lock);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400966 /* keep last stored buffers, as they are more likely hot in the cache */
Damjan Mariond1274cb2018-03-13 21:32:17 +0100967 vec_add_aligned (bp->buffers, f->buffers, VLIB_FRAME_SIZE,
Damjan Marionb6a8ed72017-08-29 00:15:35 +0200968 CLIB_CACHE_LINE_BYTES);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400969 vec_delete (f->buffers, VLIB_FRAME_SIZE, 0);
Klement Sekera75e974b2017-11-09 09:12:12 +0100970 f->n_alloc -= VLIB_FRAME_SIZE;
Damjan Mariond1274cb2018-03-13 21:32:17 +0100971 clib_spinlock_unlock (&bp->lock);
Damjan Marion6b0f5892017-07-27 04:01:24 -0400972 }
Damjan Marion8a6a3b22017-01-17 14:12:42 +0100973}
974
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975#if CLIB_DEBUG > 0
Damjan Marion6a7acc22016-12-19 16:28:36 +0100976extern u32 *vlib_buffer_state_validation_lock;
977extern uword *vlib_buffer_state_validation_hash;
978extern void *vlib_buffer_state_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979#endif
980
Dave Barach9b8ffd92016-07-08 08:13:45 -0400981static inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982vlib_validate_buffer_in_use (vlib_buffer_t * b, u32 expected)
983{
984#if CLIB_DEBUG > 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400985 uword *p;
986 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987
988 oldheap = clib_mem_set_heap (vlib_buffer_state_heap);
989
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000990 while (clib_atomic_test_and_set (vlib_buffer_state_validation_lock))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991 ;
992
993 p = hash_get (vlib_buffer_state_validation_hash, b);
994
995 /* If we don't know about b, declare it to be in the expected state */
996 if (!p)
997 {
998 hash_set (vlib_buffer_state_validation_hash, b, expected);
999 goto out;
1000 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001001
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 if (p[0] != expected)
1003 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001004 void cj_stop (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005 u32 bi;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001006 vlib_main_t *vm = &vlib_global_main;
1007
1008 cj_stop ();
1009
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010 bi = vlib_get_buffer_index (vm, b);
1011
1012 clib_mem_set_heap (oldheap);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001013 clib_warning ("%.6f buffer %llx (%d): %s, not %s",
1014 vlib_time_now (vm), bi,
1015 p[0] ? "busy" : "free", expected ? "busy" : "free");
1016 os_panic ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001018out:
1019 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020 *vlib_buffer_state_validation_lock = 0;
1021 clib_mem_set_heap (oldheap);
1022#endif
1023}
1024
Dave Barach9b8ffd92016-07-08 08:13:45 -04001025static inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026vlib_validate_buffer_set_in_use (vlib_buffer_t * b, u32 expected)
1027{
1028#if CLIB_DEBUG > 0
Dave Barach9b8ffd92016-07-08 08:13:45 -04001029 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030
1031 oldheap = clib_mem_set_heap (vlib_buffer_state_heap);
1032
Sirshak Das2f6d7bb2018-10-03 22:53:51 +00001033 while (clib_atomic_test_and_set (vlib_buffer_state_validation_lock))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034 ;
1035
1036 hash_set (vlib_buffer_state_validation_hash, b, expected);
1037
Dave Barach9b8ffd92016-07-08 08:13:45 -04001038 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039 *vlib_buffer_state_validation_lock = 0;
1040 clib_mem_set_heap (oldheap);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001041#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042}
1043
Klement Sekera75e7d132017-09-20 08:26:30 +02001044/** minimum data size of first buffer in a buffer chain */
1045#define VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE (256)
1046
1047/**
1048 * @brief compress buffer chain in a way where the first buffer is at least
1049 * VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE long
1050 *
1051 * @param[in] vm - vlib_main
1052 * @param[in,out] first - first buffer in chain
1053 * @param[in,out] discard_vector - vector of buffer indexes which were removed
1054 * from the chain
1055 */
1056always_inline void
1057vlib_buffer_chain_compress (vlib_main_t * vm,
1058 vlib_buffer_t * first, u32 ** discard_vector)
1059{
1060 if (first->current_length >= VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE ||
1061 !(first->flags & VLIB_BUFFER_NEXT_PRESENT))
1062 {
1063 /* this is already big enough or not a chain */
1064 return;
1065 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001066
1067 u32 want_first_size = clib_min (VLIB_BUFFER_CHAIN_MIN_FIRST_DATA_SIZE,
Damjan Marion32353822019-01-20 02:24:53 +01001068 VLIB_BUFFER_DATA_SIZE -
Klement Sekera75e7d132017-09-20 08:26:30 +02001069 first->current_data);
1070 do
1071 {
1072 vlib_buffer_t *second = vlib_get_buffer (vm, first->next_buffer);
1073 u32 need = want_first_size - first->current_length;
1074 u32 amount_to_copy = clib_min (need, second->current_length);
Dave Barach178cf492018-11-13 16:34:13 -05001075 clib_memcpy_fast (((u8 *) vlib_buffer_get_current (first)) +
1076 first->current_length,
1077 vlib_buffer_get_current (second), amount_to_copy);
Klement Sekera75e7d132017-09-20 08:26:30 +02001078 first->current_length += amount_to_copy;
Klement Sekera69db1a62018-12-11 16:55:33 +01001079 second->current_data += amount_to_copy;
1080 second->current_length -= amount_to_copy;
Klement Sekera75e7d132017-09-20 08:26:30 +02001081 if (first->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)
1082 {
1083 first->total_length_not_including_first_buffer -= amount_to_copy;
1084 }
1085 if (!second->current_length)
1086 {
1087 vec_add1 (*discard_vector, first->next_buffer);
1088 if (second->flags & VLIB_BUFFER_NEXT_PRESENT)
1089 {
1090 first->next_buffer = second->next_buffer;
1091 }
1092 else
1093 {
1094 first->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1095 }
1096 second->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1097 }
1098 }
1099 while ((first->current_length < want_first_size) &&
1100 (first->flags & VLIB_BUFFER_NEXT_PRESENT));
1101}
1102
Eyal Barid3d42412018-11-05 13:29:25 +02001103/**
1104 * @brief linearize buffer chain - the first buffer is filled, if needed,
1105 * buffers are allocated and filled, returns free space in last buffer or
1106 * negative on failure
1107 *
1108 * @param[in] vm - vlib_main
1109 * @param[in,out] first - first buffer in chain
1110 */
1111always_inline int
1112vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * first)
1113{
1114 vlib_buffer_t *b = first;
Damjan Marion32353822019-01-20 02:24:53 +01001115 u32 buf_len = VLIB_BUFFER_DATA_SIZE;
Eyal Barid3d42412018-11-05 13:29:25 +02001116 // free buffer chain starting from the second buffer
1117 int free_count = (b->flags & VLIB_BUFFER_NEXT_PRESENT) != 0;
1118 u32 chain_to_free = b->next_buffer;
1119
1120 u32 len = vlib_buffer_length_in_chain (vm, b);
1121 u32 free_len = buf_len - b->current_data - b->current_length;
1122 int alloc_len = clib_max (len - free_len, 0); //use the free len in the first buffer
1123 int n_buffers = (alloc_len + buf_len - 1) / buf_len;
1124 u32 new_buffers[n_buffers];
1125
1126 u32 n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers);
1127 if (n_alloc != n_buffers)
1128 {
1129 vlib_buffer_free_no_next (vm, new_buffers, n_alloc);
1130 return -1;
1131 }
1132
1133 vlib_buffer_t *s = b;
1134 while (s->flags & VLIB_BUFFER_NEXT_PRESENT)
1135 {
1136 s = vlib_get_buffer (vm, s->next_buffer);
1137 int d_free_len = buf_len - b->current_data - b->current_length;
1138 ASSERT (d_free_len >= 0);
1139 // chain buf and split write
1140 u32 copy_len = clib_min (d_free_len, s->current_length);
1141 u8 *d = vlib_buffer_put_uninit (b, copy_len);
1142 clib_memcpy (d, vlib_buffer_get_current (s), copy_len);
1143 int rest = s->current_length - copy_len;
1144 if (rest > 0)
1145 {
1146 //prev buf is full
1147 ASSERT (vlib_buffer_get_tail (b) == b->data + buf_len);
1148 ASSERT (n_buffers > 0);
1149 b = vlib_buffer_chain_buffer (vm, b, new_buffers[--n_buffers]);
1150 //make full use of the new buffers
1151 b->current_data = 0;
1152 d = vlib_buffer_put_uninit (b, rest);
1153 clib_memcpy (d, vlib_buffer_get_current (s) + copy_len, rest);
1154 }
1155 }
1156 vlib_buffer_free (vm, &chain_to_free, free_count);
1157 b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
1158 if (b == first) /* no buffers addeed */
1159 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
1160 ASSERT (len == vlib_buffer_length_in_chain (vm, first));
1161 ASSERT (n_buffers == 0);
1162 return buf_len - b->current_data - b->current_length;
1163}
1164
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165#endif /* included_vlib_buffer_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001166
1167/*
1168 * fd.io coding-style-patch-verification: ON
1169 *
1170 * Local Variables:
1171 * eval: (c-set-style "gnu")
1172 * End:
1173 */