Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Barach | c3a0655 | 2018-10-01 09:25:32 -0400 | [diff] [blame] | 44 | #include <vppinfra/fifo.h> |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 45 | #include <vlib/buffer.h> |
| 46 | #include <vlib/physmem_funcs.h> |
| 47 | #include <vlib/main.h> |
| 48 | #include <vlib/node.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
| 50 | /** \file |
| 51 | vlib buffer access methods. |
| 52 | */ |
| 53 | |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 54 | always_inline void |
| 55 | vlib_buffer_validate (vlib_main_t * vm, vlib_buffer_t * b) |
| 56 | { |
| 57 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 58 | vlib_buffer_pool_t *bp; |
| 59 | |
| 60 | /* reference count in allocated buffer always must be 1 or higher */ |
| 61 | ASSERT (b->ref_count > 0); |
| 62 | |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 63 | /* verify that buffer pool index is valid */ |
| 64 | bp = vec_elt_at_index (bm->buffer_pools, b->buffer_pool_index); |
| 65 | ASSERT (pointer_to_uword (b) >= bp->start); |
| 66 | ASSERT (pointer_to_uword (b) < bp->start + bp->size - |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 67 | (bp->data_size + sizeof (vlib_buffer_t))); |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | always_inline void * |
| 71 | vlib_buffer_ptr_from_index (uword buffer_mem_start, u32 buffer_index, |
| 72 | uword offset) |
| 73 | { |
| 74 | offset += ((uword) buffer_index) << CLIB_LOG2_CACHE_LINE_BYTES; |
| 75 | return uword_to_pointer (buffer_mem_start + offset, vlib_buffer_t *); |
| 76 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | /** \brief Translate buffer index into buffer pointer |
| 79 | |
| 80 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 81 | @param buffer_index - (u32) buffer index |
| 82 | @return - (vlib_buffer_t *) buffer pointer |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 83 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | always_inline vlib_buffer_t * |
| 85 | vlib_get_buffer (vlib_main_t * vm, u32 buffer_index) |
| 86 | { |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 87 | vlib_buffer_main_t *bm = vm->buffer_main; |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 88 | vlib_buffer_t *b; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 89 | |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 90 | b = vlib_buffer_ptr_from_index (bm->buffer_mem_start, buffer_index, 0); |
| 91 | vlib_buffer_validate (vm, b); |
| 92 | return b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 95 | static_always_inline u32 |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 96 | vlib_buffer_get_default_data_size (vlib_main_t * vm) |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 97 | { |
| 98 | return vm->buffer_main->default_data_size; |
| 99 | } |
| 100 | |
Damjan Marion | e58041f | 2019-01-18 19:56:09 +0100 | [diff] [blame] | 101 | static_always_inline void |
Damjan Marion | 64d557c | 2019-01-18 20:03:41 +0100 | [diff] [blame] | 102 | vlib_buffer_copy_indices (u32 * dst, u32 * src, u32 n_indices) |
| 103 | { |
Damjan Marion | 3e94c41 | 2019-03-13 12:41:54 +0100 | [diff] [blame] | 104 | #if defined(CLIB_HAVE_VEC512) |
| 105 | while (n_indices >= 16) |
| 106 | { |
| 107 | u32x16_store_unaligned (u32x16_load_unaligned (src), dst); |
| 108 | dst += 16; |
| 109 | src += 16; |
| 110 | n_indices -= 16; |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | #if defined(CLIB_HAVE_VEC256) |
| 115 | while (n_indices >= 8) |
| 116 | { |
| 117 | u32x8_store_unaligned (u32x8_load_unaligned (src), dst); |
| 118 | dst += 8; |
| 119 | src += 8; |
| 120 | n_indices -= 8; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #if defined(CLIB_HAVE_VEC128) |
| 125 | while (n_indices >= 4) |
| 126 | { |
| 127 | u32x4_store_unaligned (u32x4_load_unaligned (src), dst); |
| 128 | dst += 4; |
| 129 | src += 4; |
| 130 | n_indices -= 4; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | while (n_indices) |
| 135 | { |
| 136 | dst[0] = src[0]; |
| 137 | dst += 1; |
| 138 | src += 1; |
| 139 | n_indices -= 1; |
| 140 | } |
Damjan Marion | 64d557c | 2019-01-18 20:03:41 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 143 | STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64); |
Damjan Marion | 64d557c | 2019-01-18 20:03:41 +0100 | [diff] [blame] | 144 | static_always_inline void |
Damjan Marion | e58041f | 2019-01-18 19:56:09 +0100 | [diff] [blame] | 145 | vlib_buffer_copy_template (vlib_buffer_t * b, vlib_buffer_t * bt) |
| 146 | { |
Damjan Marion | 22f23ae | 2019-01-24 15:36:57 +0100 | [diff] [blame] | 147 | #if defined CLIB_HAVE_VEC512 |
| 148 | b->as_u8x64[0] = bt->as_u8x64[0]; |
| 149 | #elif defined (CLIB_HAVE_VEC256) |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 150 | b->as_u8x32[0] = bt->as_u8x32[0]; |
| 151 | b->as_u8x32[1] = bt->as_u8x32[1]; |
| 152 | #elif defined (CLIB_HAVE_VEC128) |
| 153 | b->as_u8x16[0] = bt->as_u8x16[0]; |
| 154 | b->as_u8x16[1] = bt->as_u8x16[1]; |
| 155 | b->as_u8x16[2] = bt->as_u8x16[2]; |
| 156 | b->as_u8x16[3] = bt->as_u8x16[3]; |
| 157 | #else |
| 158 | clib_memcpy_fast (b, bt, 64); |
| 159 | #endif |
Damjan Marion | e58041f | 2019-01-18 19:56:09 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 162 | always_inline u8 |
| 163 | vlib_buffer_pool_get_default_for_numa (vlib_main_t * vm, u32 numa_node) |
| 164 | { |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 165 | ASSERT (numa_node < VLIB_BUFFER_MAX_NUMA_NODES); |
| 166 | return vm->buffer_main->default_buffer_pool_index_for_numa[numa_node]; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 169 | /** \brief Translate array of buffer indices into buffer pointers with offset |
| 170 | |
| 171 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 172 | @param bi - (u32 *) array of buffer indices |
| 173 | @param b - (void **) array to store buffer pointers |
| 174 | @param count - (uword) number of elements |
| 175 | @param offset - (i32) offset applied to each pointer |
| 176 | */ |
| 177 | static_always_inline void |
| 178 | vlib_get_buffers_with_offset (vlib_main_t * vm, u32 * bi, void **b, int count, |
| 179 | i32 offset) |
| 180 | { |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 181 | uword buffer_mem_start = vm->buffer_main->buffer_mem_start; |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 182 | #ifdef CLIB_HAVE_VEC256 |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 183 | u64x4 off = u64x4_splat (buffer_mem_start + offset); |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 184 | /* if count is not const, compiler will not unroll while loop |
| 185 | se we maintain two-in-parallel variant */ |
| 186 | while (count >= 8) |
| 187 | { |
| 188 | u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi)); |
| 189 | u64x4 b1 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi + 4)); |
| 190 | /* shift and add to get vlib_buffer_t pointer */ |
| 191 | u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b); |
| 192 | u64x4_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 4); |
| 193 | b += 8; |
| 194 | bi += 8; |
| 195 | count -= 8; |
| 196 | } |
| 197 | #endif |
| 198 | while (count >= 4) |
| 199 | { |
| 200 | #ifdef CLIB_HAVE_VEC256 |
| 201 | u64x4 b0 = u32x4_extend_to_u64x4 (u32x4_load_unaligned (bi)); |
| 202 | /* shift and add to get vlib_buffer_t pointer */ |
| 203 | u64x4_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b); |
Sirshak Das | 759226e | 2018-08-22 08:46:52 +0800 | [diff] [blame] | 204 | #elif defined (CLIB_HAVE_VEC128) |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 205 | u64x2 off = u64x2_splat (buffer_mem_start + offset); |
Damjan Marion | 5df580e | 2018-07-27 01:47:57 +0200 | [diff] [blame] | 206 | u32x4 bi4 = u32x4_load_unaligned (bi); |
| 207 | u64x2 b0 = u32x4_extend_to_u64x2 ((u32x4) bi4); |
Sirshak Das | 759226e | 2018-08-22 08:46:52 +0800 | [diff] [blame] | 208 | #if defined (__aarch64__) |
| 209 | u64x2 b1 = u32x4_extend_to_u64x2_high ((u32x4) bi4); |
| 210 | #else |
Damjan Marion | 5df580e | 2018-07-27 01:47:57 +0200 | [diff] [blame] | 211 | bi4 = u32x4_shuffle (bi4, 2, 3, 0, 1); |
| 212 | u64x2 b1 = u32x4_extend_to_u64x2 ((u32x4) bi4); |
Sirshak Das | 759226e | 2018-08-22 08:46:52 +0800 | [diff] [blame] | 213 | #endif |
Damjan Marion | 5df580e | 2018-07-27 01:47:57 +0200 | [diff] [blame] | 214 | u64x2_store_unaligned ((b0 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b); |
| 215 | u64x2_store_unaligned ((b1 << CLIB_LOG2_CACHE_LINE_BYTES) + off, b + 2); |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 216 | #else |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 217 | b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset); |
| 218 | b[1] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[1], offset); |
| 219 | b[2] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[2], offset); |
| 220 | b[3] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[3], offset); |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 221 | #endif |
| 222 | b += 4; |
| 223 | bi += 4; |
| 224 | count -= 4; |
| 225 | } |
| 226 | while (count) |
| 227 | { |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 228 | b[0] = vlib_buffer_ptr_from_index (buffer_mem_start, bi[0], offset); |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 229 | b += 1; |
| 230 | bi += 1; |
| 231 | count -= 1; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** \brief Translate array of buffer indices into buffer pointers |
| 236 | |
| 237 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 238 | @param bi - (u32 *) array of buffer indices |
| 239 | @param b - (vlib_buffer_t **) array to store buffer pointers |
| 240 | @param count - (uword) number of elements |
| 241 | */ |
| 242 | |
| 243 | static_always_inline void |
| 244 | vlib_get_buffers (vlib_main_t * vm, u32 * bi, vlib_buffer_t ** b, int count) |
| 245 | { |
| 246 | vlib_get_buffers_with_offset (vm, bi, (void **) b, count, 0); |
| 247 | } |
| 248 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | /** \brief Translate buffer pointer into buffer index |
| 250 | |
| 251 | @param vm - (vlib_main_t *) vlib main data structure pointer |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 252 | @param p - (void *) buffer pointer |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | @return - (u32) buffer index |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 254 | */ |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 255 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | always_inline u32 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 257 | vlib_get_buffer_index (vlib_main_t * vm, void *p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | { |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 259 | vlib_buffer_main_t *bm = vm->buffer_main; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 260 | uword offset = pointer_to_uword (p) - bm->buffer_mem_start; |
| 261 | ASSERT (pointer_to_uword (p) >= bm->buffer_mem_start); |
| 262 | ASSERT (offset < bm->buffer_mem_size); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | ASSERT ((offset % (1 << CLIB_LOG2_CACHE_LINE_BYTES)) == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | return offset >> CLIB_LOG2_CACHE_LINE_BYTES; |
| 265 | } |
| 266 | |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 267 | /** \brief Translate array of buffer pointers into buffer indices with offset |
| 268 | |
| 269 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 270 | @param b - (void **) array of buffer pointers |
| 271 | @param bi - (u32 *) array to store buffer indices |
| 272 | @param count - (uword) number of elements |
| 273 | @param offset - (i32) offset applied to each pointer |
| 274 | */ |
| 275 | static_always_inline void |
| 276 | vlib_get_buffer_indices_with_offset (vlib_main_t * vm, void **b, u32 * bi, |
| 277 | uword count, i32 offset) |
| 278 | { |
| 279 | #ifdef CLIB_HAVE_VEC256 |
| 280 | u32x8 mask = { 0, 2, 4, 6, 1, 3, 5, 7 }; |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 281 | u64x4 off4 = u64x4_splat (vm->buffer_main->buffer_mem_start - offset); |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 282 | |
| 283 | while (count >= 8) |
| 284 | { |
| 285 | /* load 4 pointers into 256-bit register */ |
| 286 | u64x4 v0 = u64x4_load_unaligned (b); |
| 287 | u64x4 v1 = u64x4_load_unaligned (b + 4); |
| 288 | u32x8 v2, v3; |
| 289 | |
| 290 | v0 -= off4; |
| 291 | v1 -= off4; |
| 292 | |
| 293 | v0 >>= CLIB_LOG2_CACHE_LINE_BYTES; |
| 294 | v1 >>= CLIB_LOG2_CACHE_LINE_BYTES; |
| 295 | |
| 296 | /* permute 256-bit register so lower u32s of each buffer index are |
| 297 | * placed into lower 128-bits */ |
| 298 | v2 = u32x8_permute ((u32x8) v0, mask); |
| 299 | v3 = u32x8_permute ((u32x8) v1, mask); |
| 300 | |
| 301 | /* extract lower 128-bits and save them to the array of buffer indices */ |
| 302 | u32x4_store_unaligned (u32x8_extract_lo (v2), bi); |
| 303 | u32x4_store_unaligned (u32x8_extract_lo (v3), bi + 4); |
| 304 | bi += 8; |
| 305 | b += 8; |
| 306 | count -= 8; |
| 307 | } |
| 308 | #endif |
| 309 | while (count >= 4) |
| 310 | { |
| 311 | /* equivalent non-nector implementation */ |
| 312 | bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset); |
| 313 | bi[1] = vlib_get_buffer_index (vm, ((u8 *) b[1]) + offset); |
| 314 | bi[2] = vlib_get_buffer_index (vm, ((u8 *) b[2]) + offset); |
| 315 | bi[3] = vlib_get_buffer_index (vm, ((u8 *) b[3]) + offset); |
| 316 | bi += 4; |
| 317 | b += 4; |
| 318 | count -= 4; |
| 319 | } |
| 320 | while (count) |
| 321 | { |
Zhiyong Yang | 462072a | 2018-05-30 22:12:57 -0400 | [diff] [blame] | 322 | bi[0] = vlib_get_buffer_index (vm, ((u8 *) b[0]) + offset); |
Damjan Marion | afe56de | 2018-05-17 12:44:00 +0200 | [diff] [blame] | 323 | bi += 1; |
| 324 | b += 1; |
| 325 | count -= 1; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** \brief Translate array of buffer pointers into buffer indices |
| 330 | |
| 331 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 332 | @param b - (vlib_buffer_t **) array of buffer pointers |
| 333 | @param bi - (u32 *) array to store buffer indices |
| 334 | @param count - (uword) number of elements |
| 335 | */ |
| 336 | static_always_inline void |
| 337 | vlib_get_buffer_indices (vlib_main_t * vm, vlib_buffer_t ** b, u32 * bi, |
| 338 | uword count) |
| 339 | { |
| 340 | vlib_get_buffer_indices_with_offset (vm, (void **) b, bi, count, 0); |
| 341 | } |
| 342 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | /** \brief Get next buffer in buffer linklist, or zero for end of list. |
| 344 | |
| 345 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 346 | @param b - (void *) buffer pointer |
| 347 | @return - (vlib_buffer_t *) next buffer, or NULL |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 348 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | always_inline vlib_buffer_t * |
| 350 | vlib_get_next_buffer (vlib_main_t * vm, vlib_buffer_t * b) |
| 351 | { |
| 352 | return (b->flags & VLIB_BUFFER_NEXT_PRESENT |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 353 | ? vlib_get_buffer (vm, b->next_buffer) : 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 356 | uword vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm, |
| 357 | vlib_buffer_t * b_first); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
| 359 | /** \brief Get length in bytes of the buffer chain |
| 360 | |
| 361 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 362 | @param b - (void *) buffer pointer |
| 363 | @return - (uword) length of buffer chain |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 364 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | always_inline uword |
| 366 | vlib_buffer_length_in_chain (vlib_main_t * vm, vlib_buffer_t * b) |
| 367 | { |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 368 | uword len = b->current_length; |
| 369 | |
| 370 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0)) |
| 371 | return len; |
| 372 | |
| 373 | if (PREDICT_TRUE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID)) |
| 374 | return len + b->total_length_not_including_first_buffer; |
| 375 | |
| 376 | return vlib_buffer_length_in_chain_slow_path (vm, b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** \brief Get length in bytes of the buffer index buffer chain |
| 380 | |
| 381 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 382 | @param bi - (u32) buffer index |
| 383 | @return - (uword) length of buffer chain |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 384 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | always_inline uword |
| 386 | vlib_buffer_index_length_in_chain (vlib_main_t * vm, u32 bi) |
| 387 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 388 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | return vlib_buffer_length_in_chain (vm, b); |
| 390 | } |
| 391 | |
| 392 | /** \brief Copy buffer contents to memory |
| 393 | |
| 394 | @param vm - (vlib_main_t *) vlib main data structure pointer |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 395 | @param buffer_index - (u32) buffer index |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | @param contents - (u8 *) memory, <strong>must be large enough</strong> |
| 397 | @return - (uword) length of buffer chain |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 398 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | always_inline uword |
| 400 | vlib_buffer_contents (vlib_main_t * vm, u32 buffer_index, u8 * contents) |
| 401 | { |
| 402 | uword content_len = 0; |
| 403 | uword l; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 404 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | |
| 406 | while (1) |
| 407 | { |
| 408 | b = vlib_get_buffer (vm, buffer_index); |
| 409 | l = b->current_length; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 410 | clib_memcpy_fast (contents + content_len, b->data + b->current_data, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | content_len += l; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 412 | if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | break; |
| 414 | buffer_index = b->next_buffer; |
| 415 | } |
| 416 | |
| 417 | return content_len; |
| 418 | } |
| 419 | |
Damjan Marion | 8f49936 | 2018-10-22 13:07:02 +0200 | [diff] [blame] | 420 | always_inline uword |
| 421 | vlib_buffer_get_pa (vlib_main_t * vm, vlib_buffer_t * b) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | { |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 423 | return vlib_physmem_get_pa (vm, b->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Damjan Marion | 8f49936 | 2018-10-22 13:07:02 +0200 | [diff] [blame] | 426 | always_inline uword |
| 427 | vlib_buffer_get_current_pa (vlib_main_t * vm, vlib_buffer_t * b) |
| 428 | { |
| 429 | return vlib_buffer_get_pa (vm, b) + b->current_data; |
| 430 | } |
| 431 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | /** \brief Prefetch buffer metadata by buffer index |
| 433 | The first 64 bytes of buffer contains most header information |
| 434 | |
| 435 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 436 | @param bi - (u32) buffer index |
| 437 | @param type - LOAD, STORE. In most cases, STORE is the right answer |
| 438 | */ |
| 439 | /* Prefetch buffer header given index. */ |
| 440 | #define vlib_prefetch_buffer_with_index(vm,bi,type) \ |
| 441 | do { \ |
| 442 | vlib_buffer_t * _b = vlib_get_buffer (vm, bi); \ |
| 443 | vlib_prefetch_buffer_header (_b, type); \ |
| 444 | } while (0) |
| 445 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 446 | typedef enum |
| 447 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | /* Index is unknown. */ |
| 449 | VLIB_BUFFER_UNKNOWN, |
| 450 | |
| 451 | /* Index is known and free/allocated. */ |
| 452 | VLIB_BUFFER_KNOWN_FREE, |
| 453 | VLIB_BUFFER_KNOWN_ALLOCATED, |
| 454 | } vlib_buffer_known_state_t; |
| 455 | |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 456 | void vlib_buffer_validate_alloc_free (vlib_main_t * vm, u32 * buffers, |
| 457 | uword n_buffers, |
| 458 | vlib_buffer_known_state_t |
| 459 | expected_state); |
| 460 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | always_inline vlib_buffer_known_state_t |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 462 | vlib_buffer_is_known (vlib_main_t * vm, u32 buffer_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | { |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 464 | vlib_buffer_main_t *bm = vm->buffer_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
Damjan Marion | 6b0f589 | 2017-07-27 04:01:24 -0400 | [diff] [blame] | 466 | clib_spinlock_lock (&bm->buffer_known_hash_lockp); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 467 | uword *p = hash_get (bm->buffer_known_hash, buffer_index); |
Damjan Marion | 6b0f589 | 2017-07-27 04:01:24 -0400 | [diff] [blame] | 468 | clib_spinlock_unlock (&bm->buffer_known_hash_lockp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | return p ? p[0] : VLIB_BUFFER_UNKNOWN; |
| 470 | } |
| 471 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | /* Validates sanity of a single buffer. |
| 473 | Returns format'ed vector with error message if any. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 474 | u8 *vlib_validate_buffer (vlib_main_t * vm, u32 buffer_index, |
| 475 | uword follow_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 476 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 477 | static_always_inline vlib_buffer_pool_t * |
| 478 | vlib_get_buffer_pool (vlib_main_t * vm, u8 buffer_pool_index) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 479 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 480 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 481 | return vec_elt_at_index (bm->buffer_pools, buffer_pool_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 482 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 484 | static_always_inline uword |
| 485 | vlib_buffer_pool_get (vlib_main_t * vm, u8 buffer_pool_index, u32 * buffers, |
| 486 | u32 n_buffers) |
| 487 | { |
| 488 | vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index); |
| 489 | u32 len; |
| 490 | |
| 491 | ASSERT (bp->buffers); |
| 492 | |
| 493 | clib_spinlock_lock (&bp->lock); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 494 | len = bp->n_avail; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 495 | if (PREDICT_TRUE (n_buffers < len)) |
| 496 | { |
| 497 | len -= n_buffers; |
| 498 | vlib_buffer_copy_indices (buffers, bp->buffers + len, n_buffers); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 499 | bp->n_avail = len; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 500 | clib_spinlock_unlock (&bp->lock); |
| 501 | return n_buffers; |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | vlib_buffer_copy_indices (buffers, bp->buffers, len); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 506 | bp->n_avail = 0; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 507 | clib_spinlock_unlock (&bp->lock); |
| 508 | return len; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /** \brief Allocate buffers from specific pool into supplied array |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | |
| 515 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 516 | @param buffers - (u32 * ) buffer index array |
| 517 | @param n_buffers - (u32) number of buffers requested |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 518 | @return - (u32) number of buffers actually allocated, may be |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | less than the number requested or zero |
| 520 | */ |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 521 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 522 | always_inline u32 |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 523 | vlib_buffer_alloc_from_pool (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 524 | u8 buffer_pool_index) |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 525 | { |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 526 | vlib_buffer_main_t *bm = vm->buffer_main; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 527 | vlib_buffer_pool_t *bp; |
| 528 | vlib_buffer_pool_thread_t *bpt; |
| 529 | u32 *src, *dst, len, n_left; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 530 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 531 | bp = vec_elt_at_index (bm->buffer_pools, buffer_pool_index); |
| 532 | bpt = vec_elt_at_index (bp->threads, vm->thread_index); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 533 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 534 | dst = buffers; |
| 535 | n_left = n_buffers; |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 536 | len = bpt->n_cached; |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 537 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 538 | /* per-thread cache contains enough buffers */ |
| 539 | if (len >= n_buffers) |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 540 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 541 | src = bpt->cached_buffers + len - n_buffers; |
| 542 | vlib_buffer_copy_indices (dst, src, n_buffers); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 543 | bpt->n_cached -= n_buffers; |
| 544 | |
| 545 | if (CLIB_DEBUG > 0) |
| 546 | vlib_buffer_validate_alloc_free (vm, buffers, n_buffers, |
| 547 | VLIB_BUFFER_KNOWN_FREE); |
| 548 | return n_buffers; |
| 549 | } |
| 550 | |
| 551 | /* alloc bigger than cache - take buffers directly from main pool */ |
| 552 | if (n_buffers >= VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ) |
| 553 | { |
| 554 | n_buffers = vlib_buffer_pool_get (vm, buffer_pool_index, buffers, |
| 555 | n_buffers); |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 556 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 557 | if (CLIB_DEBUG > 0) |
| 558 | vlib_buffer_validate_alloc_free (vm, buffers, n_buffers, |
| 559 | VLIB_BUFFER_KNOWN_FREE); |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 560 | return n_buffers; |
| 561 | } |
| 562 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 563 | /* take everything available in the cache */ |
| 564 | if (len) |
| 565 | { |
| 566 | vlib_buffer_copy_indices (dst, bpt->cached_buffers, len); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 567 | bpt->n_cached = 0; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 568 | dst += len; |
| 569 | n_left -= len; |
| 570 | } |
| 571 | |
| 572 | len = round_pow2 (n_left, 32); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 573 | len = vlib_buffer_pool_get (vm, buffer_pool_index, bpt->cached_buffers, |
| 574 | len); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 575 | bpt->n_cached = len; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 576 | |
| 577 | if (len) |
| 578 | { |
| 579 | u32 n_copy = clib_min (len, n_left); |
| 580 | src = bpt->cached_buffers + len - n_copy; |
| 581 | vlib_buffer_copy_indices (dst, src, n_copy); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 582 | bpt->n_cached -= n_copy; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 583 | n_left -= n_copy; |
| 584 | } |
| 585 | |
| 586 | n_buffers -= n_left; |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 587 | |
| 588 | /* Verify that buffers are known free. */ |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 589 | if (CLIB_DEBUG > 0) |
| 590 | vlib_buffer_validate_alloc_free (vm, buffers, n_buffers, |
| 591 | VLIB_BUFFER_KNOWN_FREE); |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 592 | |
| 593 | return n_buffers; |
| 594 | } |
| 595 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 596 | /** \brief Allocate buffers from specific numa node into supplied array |
| 597 | |
| 598 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 599 | @param buffers - (u32 * ) buffer index array |
| 600 | @param n_buffers - (u32) number of buffers requested |
| 601 | @param numa_node - (u32) numa node |
| 602 | @return - (u32) number of buffers actually allocated, may be |
| 603 | less than the number requested or zero |
| 604 | */ |
| 605 | always_inline u32 |
| 606 | vlib_buffer_alloc_on_numa (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 607 | u32 numa_node) |
| 608 | { |
| 609 | u8 index = vlib_buffer_pool_get_default_for_numa (vm, numa_node); |
| 610 | return vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, index); |
| 611 | } |
| 612 | |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 613 | /** \brief Allocate buffers into supplied array |
| 614 | |
| 615 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 616 | @param buffers - (u32 * ) buffer index array |
| 617 | @param n_buffers - (u32) number of buffers requested |
| 618 | @return - (u32) number of buffers actually allocated, may be |
| 619 | less than the number requested or zero |
| 620 | */ |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 621 | |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 622 | always_inline u32 |
| 623 | vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
| 624 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 625 | return vlib_buffer_alloc_on_numa (vm, buffers, n_buffers, vm->numa_node); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 626 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 627 | |
Damjan Marion | c58408c | 2018-01-18 14:54:04 +0100 | [diff] [blame] | 628 | /** \brief Allocate buffers into ring |
| 629 | |
| 630 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 631 | @param buffers - (u32 * ) buffer index ring |
| 632 | @param start - (u32) first slot in the ring |
| 633 | @param ring_size - (u32) ring size |
| 634 | @param n_buffers - (u32) number of buffers requested |
| 635 | @return - (u32) number of buffers actually allocated, may be |
| 636 | less than the number requested or zero |
| 637 | */ |
| 638 | always_inline u32 |
| 639 | vlib_buffer_alloc_to_ring (vlib_main_t * vm, u32 * ring, u32 start, |
| 640 | u32 ring_size, u32 n_buffers) |
| 641 | { |
| 642 | u32 n_alloc; |
| 643 | |
| 644 | ASSERT (n_buffers <= ring_size); |
| 645 | |
| 646 | if (PREDICT_TRUE (start + n_buffers <= ring_size)) |
| 647 | return vlib_buffer_alloc (vm, ring + start, n_buffers); |
| 648 | |
| 649 | n_alloc = vlib_buffer_alloc (vm, ring + start, ring_size - start); |
| 650 | |
| 651 | if (PREDICT_TRUE (n_alloc == ring_size - start)) |
| 652 | n_alloc += vlib_buffer_alloc (vm, ring, n_buffers - n_alloc); |
| 653 | |
| 654 | return n_alloc; |
| 655 | } |
| 656 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 657 | /** \brief Allocate buffers into ring from specific buffer pool |
| 658 | |
| 659 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 660 | @param buffers - (u32 * ) buffer index ring |
| 661 | @param start - (u32) first slot in the ring |
| 662 | @param ring_size - (u32) ring size |
| 663 | @param n_buffers - (u32) number of buffers requested |
| 664 | @return - (u32) number of buffers actually allocated, may be |
| 665 | less than the number requested or zero |
| 666 | */ |
| 667 | always_inline u32 |
| 668 | vlib_buffer_alloc_to_ring_from_pool (vlib_main_t * vm, u32 * ring, u32 start, |
| 669 | u32 ring_size, u32 n_buffers, |
| 670 | u8 buffer_pool_index) |
| 671 | { |
| 672 | u32 n_alloc; |
| 673 | |
| 674 | ASSERT (n_buffers <= ring_size); |
| 675 | |
| 676 | if (PREDICT_TRUE (start + n_buffers <= ring_size)) |
| 677 | return vlib_buffer_alloc_from_pool (vm, ring + start, n_buffers, |
| 678 | buffer_pool_index); |
| 679 | |
| 680 | n_alloc = vlib_buffer_alloc_from_pool (vm, ring + start, ring_size - start, |
| 681 | buffer_pool_index); |
| 682 | |
| 683 | if (PREDICT_TRUE (n_alloc == ring_size - start)) |
| 684 | n_alloc += vlib_buffer_alloc_from_pool (vm, ring, n_buffers - n_alloc, |
| 685 | buffer_pool_index); |
| 686 | |
| 687 | return n_alloc; |
| 688 | } |
| 689 | |
Damjan Marion | f646d74 | 2019-01-31 18:50:04 +0100 | [diff] [blame] | 690 | static_always_inline void |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 691 | vlib_buffer_pool_put (vlib_main_t * vm, u8 buffer_pool_index, |
| 692 | u32 * buffers, u32 n_buffers) |
| 693 | { |
| 694 | vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 695 | vlib_buffer_pool_thread_t *bpt = vec_elt_at_index (bp->threads, |
| 696 | vm->thread_index); |
| 697 | u32 n_cached, n_empty; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 698 | |
Damjan Marion | f646d74 | 2019-01-31 18:50:04 +0100 | [diff] [blame] | 699 | if (CLIB_DEBUG > 0) |
| 700 | vlib_buffer_validate_alloc_free (vm, buffers, n_buffers, |
| 701 | VLIB_BUFFER_KNOWN_ALLOCATED); |
| 702 | |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 703 | n_cached = bpt->n_cached; |
| 704 | n_empty = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ - n_cached; |
| 705 | if (n_buffers <= n_empty) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 706 | { |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 707 | vlib_buffer_copy_indices (bpt->cached_buffers + n_cached, |
| 708 | buffers, n_buffers); |
| 709 | bpt->n_cached = n_cached + n_buffers; |
| 710 | return; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 711 | } |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 712 | |
| 713 | vlib_buffer_copy_indices (bpt->cached_buffers + n_cached, |
| 714 | buffers + n_buffers - n_empty, n_empty); |
| 715 | bpt->n_cached = VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ; |
| 716 | |
| 717 | clib_spinlock_lock (&bp->lock); |
| 718 | vlib_buffer_copy_indices (bp->buffers + bp->n_avail, buffers, |
| 719 | n_buffers - n_empty); |
| 720 | bp->n_avail += n_buffers - n_empty; |
| 721 | clib_spinlock_unlock (&bp->lock); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static_always_inline void |
| 725 | vlib_buffer_free_inline (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 726 | int maybe_next) |
| 727 | { |
| 728 | const int queue_size = 128; |
| 729 | vlib_buffer_pool_t *bp = 0; |
| 730 | u8 buffer_pool_index = ~0; |
| 731 | u32 n_queue = 0, queue[queue_size + 4]; |
| 732 | vlib_buffer_t bt = { }; |
Lijian.Zhang | e6a47cf | 2019-03-12 18:32:39 +0800 | [diff] [blame] | 733 | #if defined(CLIB_HAVE_VEC128) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 734 | vlib_buffer_t bpi_mask = {.buffer_pool_index = ~0 }; |
| 735 | vlib_buffer_t bpi_vec = {.buffer_pool_index = ~0 }; |
| 736 | vlib_buffer_t flags_refs_mask = { |
| 737 | .flags = VLIB_BUFFER_NEXT_PRESENT, |
Damjan Marion | 0e20924 | 2019-03-13 12:04:10 +0100 | [diff] [blame] | 738 | .ref_count = ~1 |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 739 | }; |
| 740 | #endif |
| 741 | |
| 742 | while (n_buffers) |
| 743 | { |
| 744 | vlib_buffer_t *b[8]; |
| 745 | u32 bi, sum = 0, flags, next; |
| 746 | |
| 747 | if (n_buffers < 12) |
| 748 | goto one_by_one; |
| 749 | |
| 750 | vlib_get_buffers (vm, buffers, b, 4); |
| 751 | vlib_get_buffers (vm, buffers + 8, b + 4, 4); |
| 752 | |
| 753 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 754 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 755 | vlib_prefetch_buffer_header (b[6], LOAD); |
| 756 | vlib_prefetch_buffer_header (b[7], LOAD); |
| 757 | |
Lijian.Zhang | e6a47cf | 2019-03-12 18:32:39 +0800 | [diff] [blame] | 758 | #if defined(CLIB_HAVE_VEC128) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 759 | u8x16 p0, p1, p2, p3, r; |
| 760 | p0 = u8x16_load_unaligned (b[0]); |
| 761 | p1 = u8x16_load_unaligned (b[1]); |
| 762 | p2 = u8x16_load_unaligned (b[2]); |
| 763 | p3 = u8x16_load_unaligned (b[3]); |
| 764 | |
| 765 | r = p0 ^ bpi_vec.as_u8x16[0]; |
| 766 | r |= p1 ^ bpi_vec.as_u8x16[0]; |
| 767 | r |= p2 ^ bpi_vec.as_u8x16[0]; |
| 768 | r |= p3 ^ bpi_vec.as_u8x16[0]; |
| 769 | r &= bpi_mask.as_u8x16[0]; |
| 770 | r |= (p0 | p1 | p2 | p3) & flags_refs_mask.as_u8x16[0]; |
| 771 | |
| 772 | sum = !u8x16_is_all_zero (r); |
| 773 | #else |
| 774 | sum |= b[0]->flags; |
| 775 | sum |= b[1]->flags; |
| 776 | sum |= b[2]->flags; |
| 777 | sum |= b[3]->flags; |
| 778 | sum &= VLIB_BUFFER_NEXT_PRESENT; |
| 779 | sum += b[0]->ref_count - 1; |
| 780 | sum += b[1]->ref_count - 1; |
| 781 | sum += b[2]->ref_count - 1; |
| 782 | sum += b[3]->ref_count - 1; |
| 783 | sum |= b[0]->buffer_pool_index ^ buffer_pool_index; |
| 784 | sum |= b[1]->buffer_pool_index ^ buffer_pool_index; |
| 785 | sum |= b[2]->buffer_pool_index ^ buffer_pool_index; |
| 786 | sum |= b[3]->buffer_pool_index ^ buffer_pool_index; |
| 787 | #endif |
| 788 | |
| 789 | if (sum) |
| 790 | goto one_by_one; |
| 791 | |
| 792 | vlib_buffer_copy_indices (queue + n_queue, buffers, 4); |
| 793 | vlib_buffer_copy_template (b[0], &bt); |
| 794 | vlib_buffer_copy_template (b[1], &bt); |
| 795 | vlib_buffer_copy_template (b[2], &bt); |
| 796 | vlib_buffer_copy_template (b[3], &bt); |
| 797 | n_queue += 4; |
| 798 | |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 799 | vlib_buffer_validate (vm, b[0]); |
| 800 | vlib_buffer_validate (vm, b[1]); |
| 801 | vlib_buffer_validate (vm, b[2]); |
| 802 | vlib_buffer_validate (vm, b[3]); |
| 803 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 804 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]); |
| 805 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]); |
| 806 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]); |
| 807 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]); |
| 808 | |
| 809 | if (n_queue >= queue_size) |
| 810 | { |
| 811 | vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue); |
| 812 | n_queue = 0; |
| 813 | } |
| 814 | buffers += 4; |
| 815 | n_buffers -= 4; |
| 816 | continue; |
| 817 | |
| 818 | one_by_one: |
| 819 | bi = buffers[0]; |
| 820 | |
| 821 | next_in_chain: |
| 822 | b[0] = vlib_get_buffer (vm, bi); |
| 823 | flags = b[0]->flags; |
| 824 | next = b[0]->next_buffer; |
| 825 | |
| 826 | if (PREDICT_FALSE (buffer_pool_index != b[0]->buffer_pool_index)) |
| 827 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 828 | |
| 829 | if (n_queue) |
| 830 | { |
| 831 | vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue); |
| 832 | n_queue = 0; |
| 833 | } |
Lollita Liu | 8f6c1dd | 2019-02-14 05:02:44 -0500 | [diff] [blame] | 834 | |
| 835 | buffer_pool_index = b[0]->buffer_pool_index; |
Lijian.Zhang | e6a47cf | 2019-03-12 18:32:39 +0800 | [diff] [blame] | 836 | #if defined(CLIB_HAVE_VEC128) |
Lollita Liu | 8f6c1dd | 2019-02-14 05:02:44 -0500 | [diff] [blame] | 837 | bpi_vec.buffer_pool_index = buffer_pool_index; |
| 838 | #endif |
| 839 | bp = vlib_get_buffer_pool (vm, buffer_pool_index); |
| 840 | vlib_buffer_copy_template (&bt, &bp->buffer_template); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Damjan Marion | 24dcbe4 | 2019-01-31 12:29:39 +0100 | [diff] [blame] | 843 | vlib_buffer_validate (vm, b[0]); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 844 | |
| 845 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]); |
| 846 | |
| 847 | if (clib_atomic_sub_fetch (&b[0]->ref_count, 1) == 0) |
| 848 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 849 | vlib_buffer_copy_template (b[0], &bt); |
| 850 | queue[n_queue++] = bi; |
| 851 | } |
| 852 | |
| 853 | if (n_queue == queue_size) |
| 854 | { |
| 855 | vlib_buffer_pool_put (vm, buffer_pool_index, queue, queue_size); |
| 856 | n_queue = 0; |
| 857 | } |
| 858 | |
Damjan Marion | 5fed42a | 2019-06-11 20:12:23 +0200 | [diff] [blame] | 859 | if (maybe_next && (flags & VLIB_BUFFER_NEXT_PRESENT)) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 860 | { |
| 861 | bi = next; |
| 862 | goto next_in_chain; |
| 863 | } |
| 864 | |
| 865 | buffers++; |
| 866 | n_buffers--; |
| 867 | } |
| 868 | |
| 869 | if (n_queue) |
| 870 | vlib_buffer_pool_put (vm, buffer_pool_index, queue, n_queue); |
| 871 | } |
| 872 | |
| 873 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 874 | /** \brief Free buffers |
| 875 | Frees the entire buffer chain for each buffer |
| 876 | |
| 877 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 878 | @param buffers - (u32 * ) buffer index array |
| 879 | @param n_buffers - (u32) number of buffers to free |
| 880 | |
| 881 | */ |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 882 | always_inline void |
| 883 | vlib_buffer_free (vlib_main_t * vm, |
| 884 | /* pointer to first buffer */ |
| 885 | u32 * buffers, |
| 886 | /* number of buffers to free */ |
| 887 | u32 n_buffers) |
| 888 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 889 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 1); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 890 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 891 | |
| 892 | /** \brief Free buffers, does not free the buffer chain for each buffer |
| 893 | |
| 894 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 895 | @param buffers - (u32 * ) buffer index array |
| 896 | @param n_buffers - (u32) number of buffers to free |
| 897 | |
| 898 | */ |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 899 | always_inline void |
| 900 | vlib_buffer_free_no_next (vlib_main_t * vm, |
| 901 | /* pointer to first buffer */ |
| 902 | u32 * buffers, |
| 903 | /* number of buffers to free */ |
| 904 | u32 n_buffers) |
| 905 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 906 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* maybe next */ 0); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 907 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 908 | |
| 909 | /** \brief Free one buffer |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 910 | Shorthand to free a single buffer chain. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 911 | |
| 912 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 913 | @param buffer_index - (u32) buffer index to free |
| 914 | */ |
| 915 | always_inline void |
| 916 | vlib_buffer_free_one (vlib_main_t * vm, u32 buffer_index) |
| 917 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 918 | vlib_buffer_free_inline (vm, &buffer_index, 1, /* maybe next */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Damjan Marion | a373149 | 2018-02-25 22:50:39 +0100 | [diff] [blame] | 921 | /** \brief Free buffers from ring |
| 922 | |
| 923 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 924 | @param buffers - (u32 * ) buffer index ring |
| 925 | @param start - (u32) first slot in the ring |
| 926 | @param ring_size - (u32) ring size |
| 927 | @param n_buffers - (u32) number of buffers |
| 928 | */ |
| 929 | always_inline void |
| 930 | vlib_buffer_free_from_ring (vlib_main_t * vm, u32 * ring, u32 start, |
| 931 | u32 ring_size, u32 n_buffers) |
| 932 | { |
| 933 | ASSERT (n_buffers <= ring_size); |
| 934 | |
| 935 | if (PREDICT_TRUE (start + n_buffers <= ring_size)) |
| 936 | { |
| 937 | vlib_buffer_free (vm, ring + start, n_buffers); |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | vlib_buffer_free (vm, ring + start, ring_size - start); |
| 942 | vlib_buffer_free (vm, ring, n_buffers - (ring_size - start)); |
| 943 | } |
| 944 | } |
| 945 | |
Damjan Marion | cef1db9 | 2018-03-28 18:27:38 +0200 | [diff] [blame] | 946 | /** \brief Free buffers from ring without freeing tail buffers |
| 947 | |
| 948 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 949 | @param buffers - (u32 * ) buffer index ring |
| 950 | @param start - (u32) first slot in the ring |
| 951 | @param ring_size - (u32) ring size |
| 952 | @param n_buffers - (u32) number of buffers |
| 953 | */ |
| 954 | always_inline void |
| 955 | vlib_buffer_free_from_ring_no_next (vlib_main_t * vm, u32 * ring, u32 start, |
| 956 | u32 ring_size, u32 n_buffers) |
| 957 | { |
| 958 | ASSERT (n_buffers <= ring_size); |
| 959 | |
| 960 | if (PREDICT_TRUE (start + n_buffers <= ring_size)) |
| 961 | { |
Damjan Marion | 4a97393 | 2018-06-09 19:29:16 +0200 | [diff] [blame] | 962 | vlib_buffer_free_no_next (vm, ring + start, n_buffers); |
Damjan Marion | cef1db9 | 2018-03-28 18:27:38 +0200 | [diff] [blame] | 963 | } |
| 964 | else |
| 965 | { |
| 966 | vlib_buffer_free_no_next (vm, ring + start, ring_size - start); |
| 967 | vlib_buffer_free_no_next (vm, ring, n_buffers - (ring_size - start)); |
| 968 | } |
| 969 | } |
Damjan Marion | a373149 | 2018-02-25 22:50:39 +0100 | [diff] [blame] | 970 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 971 | /* Append given data to end of buffer, possibly allocating new buffers. */ |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 972 | int vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data, |
| 973 | u32 n_data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | |
John Lo | 8ed2d52 | 2019-08-07 19:30:29 -0400 | [diff] [blame] | 975 | /* Define vlib_buffer and vnet_buffer flags bits preserved for copy/clone */ |
| 976 | #define VLIB_BUFFER_COPY_CLONE_FLAGS_MASK \ |
| 977 | (VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID | \ |
| 978 | VLIB_BUFFER_IS_TRACED | ~VLIB_BUFFER_FLAGS_ALL) |
| 979 | |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 980 | /* duplicate all buffers in chain */ |
| 981 | always_inline vlib_buffer_t * |
| 982 | vlib_buffer_copy (vlib_main_t * vm, vlib_buffer_t * b) |
| 983 | { |
| 984 | vlib_buffer_t *s, *d, *fd; |
| 985 | uword n_alloc, n_buffers = 1; |
John Lo | 8ed2d52 | 2019-08-07 19:30:29 -0400 | [diff] [blame] | 986 | u32 flag_mask = VLIB_BUFFER_COPY_CLONE_FLAGS_MASK; |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 987 | int i; |
| 988 | |
| 989 | s = b; |
| 990 | while (s->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 991 | { |
| 992 | n_buffers++; |
| 993 | s = vlib_get_buffer (vm, s->next_buffer); |
| 994 | } |
Neale Ranns | 9d676af | 2017-03-15 01:28:31 -0700 | [diff] [blame] | 995 | u32 new_buffers[n_buffers]; |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 996 | |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 997 | n_alloc = vlib_buffer_alloc (vm, new_buffers, n_buffers); |
Dave Barach | 26cd8c1 | 2017-02-23 17:11:26 -0500 | [diff] [blame] | 998 | |
| 999 | /* No guarantee that we'll get all the buffers we asked for */ |
| 1000 | if (PREDICT_FALSE (n_alloc < n_buffers)) |
| 1001 | { |
| 1002 | if (n_alloc > 0) |
| 1003 | vlib_buffer_free (vm, new_buffers, n_alloc); |
Dave Barach | 26cd8c1 | 2017-02-23 17:11:26 -0500 | [diff] [blame] | 1004 | return 0; |
| 1005 | } |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 1006 | |
| 1007 | /* 1st segment */ |
| 1008 | s = b; |
| 1009 | fd = d = vlib_get_buffer (vm, new_buffers[0]); |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 1010 | d->current_data = s->current_data; |
| 1011 | d->current_length = s->current_length; |
Damjan Marion | 6765549 | 2016-11-15 12:50:28 +0100 | [diff] [blame] | 1012 | d->flags = s->flags & flag_mask; |
John Lo | 6631780 | 2019-08-13 18:18:21 -0400 | [diff] [blame] | 1013 | d->trace_handle = s->trace_handle; |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 1014 | d->total_length_not_including_first_buffer = |
| 1015 | s->total_length_not_including_first_buffer; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1016 | clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque)); |
| 1017 | clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2)); |
| 1018 | clib_memcpy_fast (vlib_buffer_get_current (d), |
| 1019 | vlib_buffer_get_current (s), s->current_length); |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 1020 | |
| 1021 | /* next segments */ |
| 1022 | for (i = 1; i < n_buffers; i++) |
| 1023 | { |
| 1024 | /* previous */ |
| 1025 | d->next_buffer = new_buffers[i]; |
| 1026 | /* current */ |
| 1027 | s = vlib_get_buffer (vm, s->next_buffer); |
| 1028 | d = vlib_get_buffer (vm, new_buffers[i]); |
| 1029 | d->current_data = s->current_data; |
| 1030 | d->current_length = s->current_length; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1031 | clib_memcpy_fast (vlib_buffer_get_current (d), |
| 1032 | vlib_buffer_get_current (s), s->current_length); |
Damjan Marion | 6765549 | 2016-11-15 12:50:28 +0100 | [diff] [blame] | 1033 | d->flags = s->flags & flag_mask; |
Damjan Marion | 05ab8cb | 2016-11-03 20:16:04 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | return fd; |
| 1037 | } |
| 1038 | |
Ole Troan | da7f7b6 | 2019-03-11 13:15:54 +0100 | [diff] [blame] | 1039 | /* duplicate first buffer in chain */ |
| 1040 | always_inline vlib_buffer_t * |
| 1041 | vlib_buffer_copy_no_chain (vlib_main_t * vm, vlib_buffer_t * b, u32 * di) |
| 1042 | { |
| 1043 | vlib_buffer_t *d; |
| 1044 | |
| 1045 | if ((vlib_buffer_alloc (vm, di, 1)) != 1) |
| 1046 | return 0; |
| 1047 | |
| 1048 | d = vlib_get_buffer (vm, *di); |
| 1049 | /* 1st segment */ |
| 1050 | d->current_data = b->current_data; |
| 1051 | d->current_length = b->current_length; |
| 1052 | clib_memcpy_fast (d->opaque, b->opaque, sizeof (b->opaque)); |
| 1053 | clib_memcpy_fast (d->opaque2, b->opaque2, sizeof (b->opaque2)); |
| 1054 | clib_memcpy_fast (vlib_buffer_get_current (d), |
| 1055 | vlib_buffer_get_current (b), b->current_length); |
| 1056 | |
| 1057 | return d; |
| 1058 | } |
| 1059 | |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1060 | /* \brief Move packet from current position to offset position in buffer. |
| 1061 | Only work for small packet using one buffer with room to fit the move |
| 1062 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 1063 | @param b - (vlib_buffer_t *) pointer to buffer |
| 1064 | @param offset - (i16) position to move the packet in buffer |
| 1065 | */ |
| 1066 | always_inline void |
| 1067 | vlib_buffer_move (vlib_main_t * vm, vlib_buffer_t * b, i16 offset) |
| 1068 | { |
| 1069 | ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0); |
| 1070 | ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0); |
| 1071 | ASSERT (offset + b->current_length < |
| 1072 | vlib_buffer_get_default_data_size (vm)); |
| 1073 | |
| 1074 | u8 *source = vlib_buffer_get_current (b); |
| 1075 | b->current_data = offset; |
| 1076 | u8 *destination = vlib_buffer_get_current (b); |
| 1077 | u16 length = b->current_length; |
| 1078 | |
| 1079 | if (source + length <= destination) /* no overlap */ |
| 1080 | clib_memcpy_fast (destination, source, length); |
| 1081 | else |
| 1082 | memmove (destination, source, length); |
| 1083 | } |
| 1084 | |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1085 | /** \brief Create a maximum of 256 clones of buffer and store them |
| 1086 | in the supplied array |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1087 | |
| 1088 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 1089 | @param src_buffer - (u32) source buffer index |
| 1090 | @param buffers - (u32 * ) buffer index array |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1091 | @param n_buffers - (u16) number of buffer clones requested (<=256) |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1092 | @param head_end_offset - (u16) offset relative to current position |
| 1093 | where packet head ends |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1094 | @param offset - (i16) copy packet head at current position if 0, |
| 1095 | else at offset position to change headroom space as specified |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1096 | @return - (u16) number of buffers actually cloned, may be |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1097 | less than the number requested or zero |
| 1098 | */ |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1099 | always_inline u16 |
| 1100 | vlib_buffer_clone_256 (vlib_main_t * vm, u32 src_buffer, u32 * buffers, |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1101 | u16 n_buffers, u16 head_end_offset, i16 offset) |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1102 | { |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1103 | u16 i; |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1104 | vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer); |
| 1105 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1106 | ASSERT (s->ref_count == 1); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1107 | ASSERT (n_buffers); |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1108 | ASSERT (n_buffers <= 256); |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1109 | ASSERT (offset + VLIB_BUFFER_PRE_DATA_SIZE >= 0); |
| 1110 | ASSERT ((offset + head_end_offset) < |
| 1111 | vlib_buffer_get_default_data_size (vm)); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1112 | |
| 1113 | if (s->current_length <= head_end_offset + CLIB_CACHE_LINE_BYTES * 2) |
| 1114 | { |
| 1115 | buffers[0] = src_buffer; |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1116 | if (offset) |
| 1117 | vlib_buffer_move (vm, s, offset); |
| 1118 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1119 | for (i = 1; i < n_buffers; i++) |
| 1120 | { |
| 1121 | vlib_buffer_t *d; |
| 1122 | d = vlib_buffer_copy (vm, s); |
| 1123 | if (d == 0) |
| 1124 | return i; |
| 1125 | buffers[i] = vlib_get_buffer_index (vm, d); |
| 1126 | |
| 1127 | } |
| 1128 | return n_buffers; |
| 1129 | } |
| 1130 | |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1131 | if (PREDICT_FALSE ((n_buffers == 1) && (offset == 0))) |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1132 | { |
| 1133 | buffers[0] = src_buffer; |
| 1134 | return 1; |
| 1135 | } |
| 1136 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1137 | n_buffers = vlib_buffer_alloc_from_pool (vm, buffers, n_buffers, |
| 1138 | s->buffer_pool_index); |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1139 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1140 | for (i = 0; i < n_buffers; i++) |
| 1141 | { |
| 1142 | vlib_buffer_t *d = vlib_get_buffer (vm, buffers[i]); |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1143 | if (offset) |
| 1144 | d->current_data = offset; |
| 1145 | else |
| 1146 | d->current_data = s->current_data; |
| 1147 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1148 | d->current_length = head_end_offset; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1149 | ASSERT (d->buffer_pool_index == s->buffer_pool_index); |
| 1150 | |
Yoann Desmouceaux | 1977a34 | 2018-05-29 13:38:44 +0200 | [diff] [blame] | 1151 | d->total_length_not_including_first_buffer = s->current_length - |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1152 | head_end_offset; |
Yoann Desmouceaux | 1977a34 | 2018-05-29 13:38:44 +0200 | [diff] [blame] | 1153 | if (PREDICT_FALSE (s->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 1154 | { |
| 1155 | d->total_length_not_including_first_buffer += |
| 1156 | s->total_length_not_including_first_buffer; |
| 1157 | } |
John Lo | 8ed2d52 | 2019-08-07 19:30:29 -0400 | [diff] [blame] | 1158 | d->flags = (s->flags & VLIB_BUFFER_COPY_CLONE_FLAGS_MASK) | |
| 1159 | VLIB_BUFFER_NEXT_PRESENT; |
John Lo | 6631780 | 2019-08-13 18:18:21 -0400 | [diff] [blame] | 1160 | d->trace_handle = s->trace_handle; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1161 | clib_memcpy_fast (d->opaque, s->opaque, sizeof (s->opaque)); |
| 1162 | clib_memcpy_fast (d->opaque2, s->opaque2, sizeof (s->opaque2)); |
| 1163 | clib_memcpy_fast (vlib_buffer_get_current (d), |
| 1164 | vlib_buffer_get_current (s), head_end_offset); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1165 | d->next_buffer = src_buffer; |
| 1166 | } |
| 1167 | vlib_buffer_advance (s, head_end_offset); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1168 | s->ref_count = n_buffers; |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1169 | while (s->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1170 | { |
| 1171 | s = vlib_get_buffer (vm, s->next_buffer); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1172 | s->ref_count = n_buffers; |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | return n_buffers; |
| 1176 | } |
| 1177 | |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1178 | /** \brief Create multiple clones of buffer and store them |
| 1179 | in the supplied array |
| 1180 | |
| 1181 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 1182 | @param src_buffer - (u32) source buffer index |
| 1183 | @param buffers - (u32 * ) buffer index array |
| 1184 | @param n_buffers - (u16) number of buffer clones requested (<=256) |
| 1185 | @param head_end_offset - (u16) offset relative to current position |
| 1186 | where packet head ends |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1187 | @param offset - (i16) copy packet head at current position if 0, |
| 1188 | else at offset position to change headroom space as specified |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1189 | @return - (u16) number of buffers actually cloned, may be |
| 1190 | less than the number requested or zero |
| 1191 | */ |
| 1192 | always_inline u16 |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1193 | vlib_buffer_clone_at_offset (vlib_main_t * vm, u32 src_buffer, u32 * buffers, |
| 1194 | u16 n_buffers, u16 head_end_offset, i16 offset) |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1195 | { |
| 1196 | vlib_buffer_t *s = vlib_get_buffer (vm, src_buffer); |
| 1197 | u16 n_cloned = 0; |
| 1198 | |
| 1199 | while (n_buffers > 256) |
| 1200 | { |
| 1201 | vlib_buffer_t *copy; |
| 1202 | copy = vlib_buffer_copy (vm, s); |
| 1203 | n_cloned += vlib_buffer_clone_256 (vm, |
| 1204 | vlib_get_buffer_index (vm, copy), |
| 1205 | (buffers + n_cloned), |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1206 | 256, head_end_offset, offset); |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1207 | n_buffers -= 256; |
| 1208 | } |
| 1209 | n_cloned += vlib_buffer_clone_256 (vm, src_buffer, |
| 1210 | buffers + n_cloned, |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1211 | n_buffers, head_end_offset, offset); |
Neale Ranns | 8f36e4a | 2018-01-11 09:02:01 -0800 | [diff] [blame] | 1212 | |
| 1213 | return n_cloned; |
| 1214 | } |
| 1215 | |
John Lo | f545caa | 2019-04-01 11:30:07 -0400 | [diff] [blame] | 1216 | /** \brief Create multiple clones of buffer and store them |
| 1217 | in the supplied array |
| 1218 | |
| 1219 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 1220 | @param src_buffer - (u32) source buffer index |
| 1221 | @param buffers - (u32 * ) buffer index array |
| 1222 | @param n_buffers - (u16) number of buffer clones requested (<=256) |
| 1223 | @param head_end_offset - (u16) offset relative to current position |
| 1224 | where packet head ends |
| 1225 | @return - (u16) number of buffers actually cloned, may be |
| 1226 | less than the number requested or zero |
| 1227 | */ |
| 1228 | always_inline u16 |
| 1229 | vlib_buffer_clone (vlib_main_t * vm, u32 src_buffer, u32 * buffers, |
| 1230 | u16 n_buffers, u16 head_end_offset) |
| 1231 | { |
| 1232 | return vlib_buffer_clone_at_offset (vm, src_buffer, buffers, n_buffers, |
| 1233 | head_end_offset, 0); |
| 1234 | } |
| 1235 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1236 | /** \brief Attach cloned tail to the buffer |
| 1237 | |
| 1238 | @param vm - (vlib_main_t *) vlib main data structure pointer |
| 1239 | @param head - (vlib_buffer_t *) head buffer |
| 1240 | @param tail - (Vlib buffer_t *) tail buffer to clone and attach to head |
| 1241 | */ |
| 1242 | |
| 1243 | always_inline void |
| 1244 | vlib_buffer_attach_clone (vlib_main_t * vm, vlib_buffer_t * head, |
| 1245 | vlib_buffer_t * tail) |
| 1246 | { |
| 1247 | ASSERT ((head->flags & VLIB_BUFFER_NEXT_PRESENT) == 0); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1248 | ASSERT (head->buffer_pool_index == tail->buffer_pool_index); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1249 | |
| 1250 | head->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1251 | head->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 1252 | head->flags &= ~VLIB_BUFFER_EXT_HDR_VALID; |
| 1253 | head->flags |= (tail->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID); |
| 1254 | head->next_buffer = vlib_get_buffer_index (vm, tail); |
| 1255 | head->total_length_not_including_first_buffer = tail->current_length + |
| 1256 | tail->total_length_not_including_first_buffer; |
| 1257 | |
| 1258 | next_segment: |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 1259 | clib_atomic_add_fetch (&tail->ref_count, 1); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (tail->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1262 | { |
| 1263 | tail = vlib_get_buffer (vm, tail->next_buffer); |
| 1264 | goto next_segment; |
| 1265 | } |
| 1266 | } |
| 1267 | |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1268 | /* Initializes the buffer as an empty packet with no chained buffers. */ |
| 1269 | always_inline void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1270 | vlib_buffer_chain_init (vlib_buffer_t * first) |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1271 | { |
| 1272 | first->total_length_not_including_first_buffer = 0; |
| 1273 | first->current_length = 0; |
| 1274 | first->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 1275 | first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | /* The provided next_bi buffer index is appended to the end of the packet. */ |
| 1279 | always_inline vlib_buffer_t * |
Eyal Bari | b688fb1 | 2018-11-12 16:13:49 +0200 | [diff] [blame] | 1280 | vlib_buffer_chain_buffer (vlib_main_t * vm, vlib_buffer_t * last, u32 next_bi) |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1281 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1282 | vlib_buffer_t *next_buffer = vlib_get_buffer (vm, next_bi); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1283 | last->next_buffer = next_bi; |
| 1284 | last->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1285 | next_buffer->current_length = 0; |
| 1286 | next_buffer->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1287 | return next_buffer; |
| 1288 | } |
| 1289 | |
| 1290 | /* Increases or decreases the packet length. |
| 1291 | * It does not allocate or deallocate new buffers. |
| 1292 | * Therefore, the added length must be compatible |
| 1293 | * with the last buffer. */ |
| 1294 | always_inline void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1295 | vlib_buffer_chain_increase_length (vlib_buffer_t * first, |
| 1296 | vlib_buffer_t * last, i32 len) |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1297 | { |
| 1298 | last->current_length += len; |
| 1299 | if (first != last) |
| 1300 | first->total_length_not_including_first_buffer += len; |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | /* Copy data to the end of the packet and increases its length. |
| 1304 | * It does not allocate new buffers. |
| 1305 | * Returns the number of copied bytes. */ |
| 1306 | always_inline u16 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1307 | vlib_buffer_chain_append_data (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1308 | vlib_buffer_t * first, |
| 1309 | vlib_buffer_t * last, void *data, u16 data_len) |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1310 | { |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 1311 | u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1312 | ASSERT (n_buffer_bytes >= last->current_length + last->current_data); |
| 1313 | u16 len = clib_min (data_len, |
| 1314 | n_buffer_bytes - last->current_length - |
| 1315 | last->current_data); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1316 | clib_memcpy_fast (vlib_buffer_get_current (last) + last->current_length, |
| 1317 | data, len); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1318 | vlib_buffer_chain_increase_length (first, last, len); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1319 | return len; |
| 1320 | } |
| 1321 | |
| 1322 | /* Copy data to the end of the packet and increases its length. |
| 1323 | * Allocates additional buffers from the free list if necessary. |
| 1324 | * Returns the number of copied bytes. |
| 1325 | * 'last' value is modified whenever new buffers are allocated and |
| 1326 | * chained and points to the last buffer in the chain. */ |
| 1327 | u16 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1328 | vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1329 | vlib_buffer_t * first, |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 1330 | vlib_buffer_t ** last, void *data, |
| 1331 | u16 data_len); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1332 | void vlib_buffer_chain_validate (vlib_main_t * vm, vlib_buffer_t * first); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1333 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1334 | format_function_t format_vlib_buffer, format_vlib_buffer_and_data, |
Benoît Ganne | 4354317 | 2019-10-21 15:13:54 +0200 | [diff] [blame^] | 1335 | format_vlib_buffer_contents, format_vlib_buffer_no_chain; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1336 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1337 | typedef struct |
| 1338 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1339 | /* Vector of packet data. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1340 | u8 *packet_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1341 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 1342 | /* Number of buffers to allocate in each call to allocator. */ |
| 1343 | u32 min_n_buffers_each_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1344 | |
Damjan Marion | 671e60e | 2018-12-30 18:09:59 +0100 | [diff] [blame] | 1345 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1346 | } vlib_packet_template_t; |
| 1347 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1348 | void vlib_packet_template_init (vlib_main_t * vm, |
| 1349 | vlib_packet_template_t * t, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1350 | void *packet_data, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1351 | uword n_packet_data_bytes, |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 1352 | uword min_n_buffers_each_alloc, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1353 | char *fmt, ...); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1354 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1355 | void *vlib_packet_template_get_packet (vlib_main_t * vm, |
| 1356 | vlib_packet_template_t * t, |
| 1357 | u32 * bi_result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1358 | |
| 1359 | always_inline void |
| 1360 | vlib_packet_template_free (vlib_main_t * vm, vlib_packet_template_t * t) |
| 1361 | { |
| 1362 | vec_free (t->packet_data); |
| 1363 | } |
| 1364 | |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1365 | always_inline u32 |
| 1366 | vlib_buffer_space_left_at_end (vlib_main_t * vm, vlib_buffer_t * b) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1367 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1368 | return b->data + vlib_buffer_get_default_data_size (vm) - |
| 1369 | ((u8 *) vlib_buffer_get_current (b) + b->current_length); |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1372 | always_inline u32 |
| 1373 | vlib_buffer_chain_linearize (vlib_main_t * vm, vlib_buffer_t * b) |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1374 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1375 | vlib_buffer_t *db = b, *sb, *first = b; |
| 1376 | int is_cloned = 0; |
| 1377 | u32 bytes_left = 0, data_size; |
| 1378 | u16 src_left, dst_left, n_buffers = 1; |
| 1379 | u8 *dp, *sp; |
| 1380 | u32 to_free = 0; |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1381 | |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1382 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0)) |
| 1383 | return 1; |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1384 | |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1385 | data_size = vlib_buffer_get_default_data_size (vm); |
| 1386 | |
| 1387 | dst_left = vlib_buffer_space_left_at_end (vm, b); |
| 1388 | |
| 1389 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1390 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1391 | b = vlib_get_buffer (vm, b->next_buffer); |
| 1392 | if (b->ref_count > 1) |
| 1393 | is_cloned = 1; |
| 1394 | bytes_left += b->current_length; |
| 1395 | n_buffers++; |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1398 | /* if buffer is cloned, create completely new chain - unless everything fits |
| 1399 | * into one buffer */ |
| 1400 | if (is_cloned && bytes_left >= dst_left) |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1401 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1402 | u32 len = 0; |
| 1403 | u32 space_needed = bytes_left - dst_left; |
| 1404 | u32 tail; |
| 1405 | |
| 1406 | if (vlib_buffer_alloc (vm, &tail, 1) == 0) |
| 1407 | return 0; |
| 1408 | |
| 1409 | ++n_buffers; |
| 1410 | len += data_size; |
| 1411 | b = vlib_get_buffer (vm, tail); |
| 1412 | |
| 1413 | while (len < space_needed) |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1414 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1415 | u32 bi; |
| 1416 | if (vlib_buffer_alloc (vm, &bi, 1) == 0) |
| 1417 | { |
| 1418 | vlib_buffer_free_one (vm, tail); |
| 1419 | return 0; |
| 1420 | } |
| 1421 | b->flags = VLIB_BUFFER_NEXT_PRESENT; |
| 1422 | b->next_buffer = bi; |
| 1423 | b = vlib_get_buffer (vm, bi); |
| 1424 | len += data_size; |
| 1425 | n_buffers++; |
| 1426 | } |
| 1427 | sb = vlib_get_buffer (vm, first->next_buffer); |
| 1428 | to_free = first->next_buffer; |
| 1429 | first->next_buffer = tail; |
| 1430 | } |
| 1431 | else |
| 1432 | sb = vlib_get_buffer (vm, first->next_buffer); |
| 1433 | |
| 1434 | src_left = sb->current_length; |
| 1435 | sp = vlib_buffer_get_current (sb); |
| 1436 | dp = vlib_buffer_get_tail (db); |
| 1437 | |
| 1438 | while (bytes_left) |
| 1439 | { |
| 1440 | u16 bytes_to_copy; |
| 1441 | |
| 1442 | if (dst_left == 0) |
| 1443 | { |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1444 | db->current_length = dp - (u8 *) vlib_buffer_get_current (db); |
| 1445 | ASSERT (db->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 1446 | db = vlib_get_buffer (vm, db->next_buffer); |
| 1447 | dst_left = data_size; |
Klement Sekera | c09b7fd | 2019-06-04 21:14:26 +0200 | [diff] [blame] | 1448 | if (db->current_data > 0) |
| 1449 | { |
| 1450 | db->current_data = 0; |
| 1451 | } |
| 1452 | else |
| 1453 | { |
| 1454 | dst_left += -db->current_data; |
| 1455 | } |
| 1456 | dp = vlib_buffer_get_current (db); |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | while (src_left == 0) |
| 1460 | { |
| 1461 | ASSERT (sb->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 1462 | sb = vlib_get_buffer (vm, sb->next_buffer); |
| 1463 | src_left = sb->current_length; |
| 1464 | sp = vlib_buffer_get_current (sb); |
| 1465 | } |
| 1466 | |
| 1467 | bytes_to_copy = clib_min (dst_left, src_left); |
| 1468 | |
| 1469 | if (dp != sp) |
| 1470 | { |
| 1471 | if (sb == db) |
| 1472 | bytes_to_copy = clib_min (bytes_to_copy, sp - dp); |
| 1473 | |
| 1474 | clib_memcpy_fast (dp, sp, bytes_to_copy); |
| 1475 | } |
| 1476 | |
| 1477 | src_left -= bytes_to_copy; |
| 1478 | dst_left -= bytes_to_copy; |
| 1479 | dp += bytes_to_copy; |
| 1480 | sp += bytes_to_copy; |
| 1481 | bytes_left -= bytes_to_copy; |
| 1482 | } |
| 1483 | if (db != first) |
| 1484 | db->current_data = 0; |
| 1485 | db->current_length = dp - (u8 *) vlib_buffer_get_current (db); |
| 1486 | |
| 1487 | if (is_cloned && to_free) |
| 1488 | vlib_buffer_free_one (vm, to_free); |
| 1489 | else |
| 1490 | { |
| 1491 | if (db->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1492 | vlib_buffer_free_one (vm, db->next_buffer); |
| 1493 | db->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 1494 | b = first; |
| 1495 | n_buffers = 1; |
| 1496 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1497 | { |
| 1498 | b = vlib_get_buffer (vm, b->next_buffer); |
| 1499 | ++n_buffers; |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1500 | } |
| 1501 | } |
Klement Sekera | f883f6a | 2019-02-13 11:01:32 +0100 | [diff] [blame] | 1502 | |
| 1503 | first->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 1504 | |
| 1505 | return n_buffers; |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1508 | #endif /* included_vlib_buffer_funcs_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1509 | |
| 1510 | /* |
| 1511 | * fd.io coding-style-patch-verification: ON |
| 1512 | * |
| 1513 | * Local Variables: |
| 1514 | * eval: (c-set-style "gnu") |
| 1515 | * End: |
| 1516 | */ |