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.h: VLIB buffers |
| 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_h |
| 41 | #define included_vlib_buffer_h |
| 42 | |
| 43 | #include <vppinfra/types.h> |
| 44 | #include <vppinfra/cache.h> |
| 45 | #include <vppinfra/serialize.h> |
| 46 | #include <vppinfra/vector.h> |
Damjan Marion | 6b0f589 | 2017-07-27 04:01:24 -0400 | [diff] [blame] | 47 | #include <vppinfra/lock.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | #include <vlib/error.h> /* for vlib_error_t */ |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 49 | |
Damjan Marion | 038dad7 | 2024-01-19 21:19:57 +0100 | [diff] [blame] | 50 | #include <vlib/config.h> /* for VLIB_BUFFER_PRE_DATA_SIZE */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 52 | #define VLIB_BUFFER_DEFAULT_DATA_SIZE (2048) |
| 53 | |
Damjan Marion | bd0da97 | 2018-10-31 10:59:02 +0100 | [diff] [blame] | 54 | /* Minimum buffer chain segment size. Does not apply to last buffer in chain. |
| 55 | Dataplane code can safely asume that specified amount of data is not split |
| 56 | into 2 chained buffers */ |
| 57 | #define VLIB_BUFFER_MIN_CHAIN_SEG_SIZE (128) |
| 58 | |
| 59 | /* Amount of head buffer data copied to each replica head buffer */ |
| 60 | #define VLIB_BUFFER_CLONE_HEAD_SIZE (256) |
| 61 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | /** \file |
| 63 | vlib buffer structure definition and a few select |
| 64 | access methods. This structure and the buffer allocation |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | mechanism should perhaps live in vnet, but it would take a lot |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | of typing to make it so. |
| 67 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 68 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 69 | /** |
| 70 | * Buffer Flags |
| 71 | */ |
| 72 | #define foreach_vlib_buffer_flag \ |
Damjan Marion | 36eb7c2 | 2019-01-18 20:45:30 +0100 | [diff] [blame] | 73 | _( 0, IS_TRACED, 0) \ |
Benoît Ganne | 4354317 | 2019-10-21 15:13:54 +0200 | [diff] [blame] | 74 | _( 1, NEXT_PRESENT, "next-present") \ |
Damjan Marion | 36eb7c2 | 2019-01-18 20:45:30 +0100 | [diff] [blame] | 75 | _( 2, TOTAL_LENGTH_VALID, 0) \ |
| 76 | _( 3, EXT_HDR_VALID, "ext-hdr-valid") |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 77 | |
| 78 | /* NOTE: only buffer generic flags should be defined here, please consider |
| 79 | using user flags. i.e. src/vnet/buffer.h */ |
| 80 | |
| 81 | enum |
| 82 | { |
| 83 | #define _(bit, name, v) VLIB_BUFFER_##name = (1 << (bit)), |
| 84 | foreach_vlib_buffer_flag |
| 85 | #undef _ |
| 86 | }; |
| 87 | |
| 88 | enum |
| 89 | { |
| 90 | #define _(bit, name, v) VLIB_BUFFER_LOG2_##name = (bit), |
| 91 | foreach_vlib_buffer_flag |
| 92 | #undef _ |
| 93 | }; |
| 94 | |
| 95 | /* User defined buffer flags. */ |
| 96 | #define LOG2_VLIB_BUFFER_FLAG_USER(n) (32 - (n)) |
| 97 | #define VLIB_BUFFER_FLAG_USER(n) (1 << LOG2_VLIB_BUFFER_FLAG_USER(n)) |
Damjan Marion | 36eb7c2 | 2019-01-18 20:45:30 +0100 | [diff] [blame] | 98 | #define VLIB_BUFFER_FLAGS_ALL (0x0f) |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 99 | |
Benoît Ganne | f89bbbe | 2021-03-04 14:31:03 +0100 | [diff] [blame] | 100 | /** \brief Compile time buffer trajectory tracing option |
| 101 | Turn this on if you run into "bad monkey" contexts, |
| 102 | and you want to know exactly which nodes they've visited... |
| 103 | See vlib/main.c... |
| 104 | */ |
| 105 | #ifndef VLIB_BUFFER_TRACE_TRAJECTORY |
| 106 | #define VLIB_BUFFER_TRACE_TRAJECTORY 0 |
| 107 | #endif /* VLIB_BUFFER_TRACE_TRAJECTORY */ |
| 108 | |
Damjan Marion | bf23663 | 2023-10-13 09:59:00 +0000 | [diff] [blame] | 109 | #define vlib_buffer_template_fields \ |
| 110 | /** signed offset in data[], pre_data[] that we are currently \ |
| 111 | * processing. If negative current header points into predata area. */ \ |
| 112 | i16 current_data; \ |
| 113 | \ |
| 114 | /** Nbytes between current data and the end of this buffer. */ \ |
| 115 | u16 current_length; \ |
| 116 | /** buffer flags: \ |
| 117 | <br> VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list \ |
| 118 | index, <br> VLIB_BUFFER_IS_TRACED: trace this buffer. <br> \ |
| 119 | VLIB_BUFFER_NEXT_PRESENT: this is a multi-chunk buffer. <br> \ |
| 120 | VLIB_BUFFER_TOTAL_LENGTH_VALID: as it says <br> \ |
| 121 | VLIB_BUFFER_EXT_HDR_VALID: buffer contains valid external buffer manager \ |
| 122 | header, set to avoid adding it to a flow report <br> \ |
| 123 | VLIB_BUFFER_FLAG_USER(n): user-defined bit N \ |
| 124 | */ \ |
| 125 | u32 flags; \ |
| 126 | \ |
| 127 | /** Generic flow identifier */ \ |
| 128 | u32 flow_id; \ |
| 129 | \ |
| 130 | /** Reference count for this buffer. */ \ |
| 131 | volatile u8 ref_count; \ |
| 132 | \ |
| 133 | /** index of buffer pool this buffer belongs. */ \ |
| 134 | u8 buffer_pool_index; \ |
| 135 | \ |
| 136 | /** Error code for buffers to be enqueued to error handler. */ \ |
| 137 | vlib_error_t error; \ |
| 138 | \ |
| 139 | /** Next buffer for this linked-list of buffers. Only valid if \ |
| 140 | * VLIB_BUFFER_NEXT_PRESENT flag is set. */ \ |
| 141 | u32 next_buffer; \ |
| 142 | \ |
| 143 | /** The following fields can be in a union because once a packet enters \ |
| 144 | * the punt path, it is no longer on a feature arc */ \ |
| 145 | union \ |
| 146 | { \ |
| 147 | /** Used by feature subgraph arcs to visit enabled feature nodes */ \ |
| 148 | u32 current_config_index; \ |
| 149 | /* the reason the packet once punted */ \ |
| 150 | u32 punt_reason; \ |
| 151 | }; \ |
| 152 | \ |
| 153 | /** Opaque data used by sub-graphs for their own purposes. */ \ |
| 154 | u32 opaque[10]; |
| 155 | |
| 156 | typedef struct |
| 157 | { |
| 158 | CLIB_ALIGN_MARK (align_mark, 64); |
| 159 | vlib_buffer_template_fields |
| 160 | } vlib_buffer_template_t; |
| 161 | |
| 162 | STATIC_ASSERT_SIZEOF (vlib_buffer_template_t, 64); |
| 163 | |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 164 | /** VLIB buffer representation. */ |
| 165 | typedef union |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 166 | { |
Damjan Marion | bf23663 | 2023-10-13 09:59:00 +0000 | [diff] [blame] | 167 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 168 | struct |
| 169 | { |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 170 | union |
| 171 | { |
Damjan Marion | bf23663 | 2023-10-13 09:59:00 +0000 | [diff] [blame] | 172 | struct |
| 173 | { |
| 174 | vlib_buffer_template_fields |
| 175 | }; |
| 176 | vlib_buffer_template_t template; |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 177 | }; |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 178 | |
Damjan Marion | bf23663 | 2023-10-13 09:59:00 +0000 | [diff] [blame] | 179 | /* Data above is initialized or zeroed on alloc, data bellow is not |
| 180 | * and it is app responsibility to ensure data is valid */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | |
Damjan Marion | 6807b77 | 2020-11-13 10:46:32 +0100 | [diff] [blame] | 182 | /** start of 2nd half (2nd cacheline on systems where cacheline size is 64) */ |
| 183 | CLIB_ALIGN_MARK (second_half, 64); |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 184 | |
Dave Barach | a638c18 | 2019-06-21 18:24:07 -0400 | [diff] [blame] | 185 | /** Specifies trace buffer handle if VLIB_PACKET_IS_TRACED flag is |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 186 | * set. */ |
Dave Barach | a638c18 | 2019-06-21 18:24:07 -0400 | [diff] [blame] | 187 | u32 trace_handle; |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 188 | |
| 189 | /** Only valid for first buffer in chain. Current length plus total length |
| 190 | * given here give total number of bytes in buffer chain. */ |
| 191 | u32 total_length_not_including_first_buffer; |
| 192 | |
| 193 | /**< More opaque data, see ../vnet/vnet/buffer.h */ |
| 194 | u32 opaque2[14]; |
| 195 | |
Benoît Ganne | f89bbbe | 2021-03-04 14:31:03 +0100 | [diff] [blame] | 196 | #if VLIB_BUFFER_TRACE_TRAJECTORY > 0 |
| 197 | /** trace trajectory data - we use a specific cacheline for that in the |
| 198 | * buffer when it is compiled-in */ |
| 199 | #define VLIB_BUFFER_TRACE_TRAJECTORY_MAX 31 |
| 200 | #define VLIB_BUFFER_TRACE_TRAJECTORY_SZ 64 |
| 201 | #define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b) (b)->trajectory_nb = 0 |
| 202 | CLIB_ALIGN_MARK (trajectory, 64); |
| 203 | u16 trajectory_nb; |
| 204 | u16 trajectory_trace[VLIB_BUFFER_TRACE_TRAJECTORY_MAX]; |
| 205 | #else /* VLIB_BUFFER_TRACE_TRAJECTORY */ |
| 206 | #define VLIB_BUFFER_TRACE_TRAJECTORY_SZ 0 |
| 207 | #define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b) |
| 208 | #endif /* VLIB_BUFFER_TRACE_TRAJECTORY */ |
| 209 | |
Damjan Marion | 6807b77 | 2020-11-13 10:46:32 +0100 | [diff] [blame] | 210 | /** start of buffer headroom */ |
| 211 | CLIB_ALIGN_MARK (headroom, 64); |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 212 | |
| 213 | /** Space for inserting data before buffer start. Packet rewrite string |
| 214 | * will be rewritten backwards and may extend back before |
| 215 | * buffer->data[0]. Must come directly before packet data. */ |
| 216 | u8 pre_data[VLIB_BUFFER_PRE_DATA_SIZE]; |
| 217 | |
| 218 | /** Packet data */ |
Benoît Ganne | 049d0b4 | 2020-04-24 11:48:04 +0200 | [diff] [blame] | 219 | u8 data[]; |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 220 | }; |
| 221 | #ifdef CLIB_HAVE_VEC128 |
| 222 | u8x16 as_u8x16[4]; |
| 223 | #endif |
| 224 | #ifdef CLIB_HAVE_VEC256 |
Damjan Marion | 22f23ae | 2019-01-24 15:36:57 +0100 | [diff] [blame] | 225 | u8x32 as_u8x32[2]; |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 226 | #endif |
| 227 | #ifdef CLIB_HAVE_VEC512 |
Damjan Marion | 22f23ae | 2019-01-24 15:36:57 +0100 | [diff] [blame] | 228 | u8x64 as_u8x64[1]; |
Damjan Marion | 9a8a12a | 2019-01-23 16:52:10 +0100 | [diff] [blame] | 229 | #endif |
| 230 | } vlib_buffer_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
Benoît Ganne | f89bbbe | 2021-03-04 14:31:03 +0100 | [diff] [blame] | 232 | STATIC_ASSERT_SIZEOF (vlib_buffer_t, 128 + VLIB_BUFFER_TRACE_TRAJECTORY_SZ + |
| 233 | VLIB_BUFFER_PRE_DATA_SIZE); |
Damjan Marion | 6807b77 | 2020-11-13 10:46:32 +0100 | [diff] [blame] | 234 | STATIC_ASSERT (VLIB_BUFFER_PRE_DATA_SIZE % CLIB_CACHE_LINE_BYTES == 0, |
| 235 | "VLIB_BUFFER_PRE_DATA_SIZE must be divisible by cache line size"); |
| 236 | |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 237 | #define VLIB_BUFFER_HDR_SIZE (sizeof(vlib_buffer_t) - VLIB_BUFFER_PRE_DATA_SIZE) |
Damjan Marion | 2d725c6 | 2023-11-13 12:18:24 +0000 | [diff] [blame] | 238 | #define VLIB_BUFFER_INVALID_INDEX 0xffffffff |
Damjan Marion | 1901020 | 2016-03-24 17:17:47 +0100 | [diff] [blame] | 239 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | /** \brief Prefetch buffer metadata. |
| 241 | The first 64 bytes of buffer contains most header information |
| 242 | |
| 243 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 244 | @param type - LOAD, STORE. In most cases, STORE is the right answer |
| 245 | */ |
| 246 | |
| 247 | #define vlib_prefetch_buffer_header(b,type) CLIB_PREFETCH (b, 64, type) |
Damjan Marion | d30bf01 | 2018-11-18 23:48:43 +0100 | [diff] [blame] | 248 | #define vlib_prefetch_buffer_data(b,type) \ |
| 249 | CLIB_PREFETCH (vlib_buffer_get_current(b), CLIB_CACHE_LINE_BYTES, type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | always_inline void |
| 252 | vlib_buffer_struct_is_sane (vlib_buffer_t * b) |
| 253 | { |
| 254 | ASSERT (sizeof (b[0]) % 64 == 0); |
| 255 | |
| 256 | /* Rewrite data must be before and contiguous with packet data. */ |
| 257 | ASSERT (b->pre_data + VLIB_BUFFER_PRE_DATA_SIZE == b->data); |
| 258 | } |
| 259 | |
Damjan Marion | 8f49936 | 2018-10-22 13:07:02 +0200 | [diff] [blame] | 260 | always_inline uword |
| 261 | vlib_buffer_get_va (vlib_buffer_t * b) |
| 262 | { |
| 263 | return pointer_to_uword (b->data); |
| 264 | } |
| 265 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | /** \brief Get pointer to current data to process |
| 267 | |
| 268 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 269 | @return - (void *) (b->data + b->current_data) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 270 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
| 272 | always_inline void * |
| 273 | vlib_buffer_get_current (vlib_buffer_t * b) |
| 274 | { |
| 275 | /* Check bounds. */ |
| 276 | ASSERT ((signed) b->current_data >= (signed) -VLIB_BUFFER_PRE_DATA_SIZE); |
| 277 | return b->data + b->current_data; |
| 278 | } |
| 279 | |
Damjan Marion | 8f49936 | 2018-10-22 13:07:02 +0200 | [diff] [blame] | 280 | always_inline uword |
| 281 | vlib_buffer_get_current_va (vlib_buffer_t * b) |
| 282 | { |
| 283 | return vlib_buffer_get_va (b) + b->current_data; |
| 284 | } |
| 285 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | /** \brief Advance current data pointer by the supplied (signed!) amount |
| 287 | |
| 288 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 289 | @param l - (word) signed increment |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 290 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | always_inline void |
| 292 | vlib_buffer_advance (vlib_buffer_t * b, word l) |
| 293 | { |
| 294 | ASSERT (b->current_length >= l); |
| 295 | b->current_data += l; |
| 296 | b->current_length -= l; |
Damjan Marion | bd0da97 | 2018-10-31 10:59:02 +0100 | [diff] [blame] | 297 | |
| 298 | ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0 || |
| 299 | b->current_length >= VLIB_BUFFER_MIN_CHAIN_SEG_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Filip Tehlar | 816f437 | 2017-04-26 16:09:06 +0200 | [diff] [blame] | 302 | /** \brief Check if there is enough space in buffer to advance |
| 303 | |
| 304 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 305 | @param l - (word) size to check |
| 306 | @return - 0 if there is less space than 'l' in buffer |
| 307 | */ |
| 308 | always_inline u8 |
| 309 | vlib_buffer_has_space (vlib_buffer_t * b, word l) |
| 310 | { |
| 311 | return b->current_length >= l; |
| 312 | } |
| 313 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | /** \brief Reset current header & length to state they were in when |
| 315 | packet was received. |
| 316 | |
| 317 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 318 | */ |
| 319 | |
| 320 | always_inline void |
| 321 | vlib_buffer_reset (vlib_buffer_t * b) |
| 322 | { |
| 323 | b->current_length += clib_max (b->current_data, 0); |
| 324 | b->current_data = 0; |
| 325 | } |
| 326 | |
| 327 | /** \brief Get pointer to buffer's opaque data array |
| 328 | |
| 329 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 330 | @return - (void *) b->opaque |
| 331 | */ |
| 332 | always_inline void * |
| 333 | vlib_get_buffer_opaque (vlib_buffer_t * b) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 334 | { |
| 335 | return (void *) b->opaque; |
| 336 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | |
| 338 | /** \brief Get pointer to buffer's opaque2 data array |
| 339 | |
| 340 | @param b - (vlib_buffer_t *) pointer to the buffer |
| 341 | @return - (void *) b->opaque2 |
| 342 | */ |
| 343 | always_inline void * |
| 344 | vlib_get_buffer_opaque2 (vlib_buffer_t * b) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 345 | { |
| 346 | return (void *) b->opaque2; |
| 347 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 349 | /** \brief Get pointer to the end of buffer's data |
| 350 | * @param b pointer to the buffer |
| 351 | * @return pointer to tail of packet's data |
| 352 | */ |
| 353 | always_inline u8 * |
| 354 | vlib_buffer_get_tail (vlib_buffer_t * b) |
| 355 | { |
| 356 | return b->data + b->current_data + b->current_length; |
| 357 | } |
| 358 | |
| 359 | /** \brief Append uninitialized data to buffer |
| 360 | * @param b pointer to the buffer |
| 361 | * @param size number of uninitialized bytes |
| 362 | * @return pointer to beginning of uninitialized data |
| 363 | */ |
| 364 | always_inline void * |
Eyal Bari | d3d4241 | 2018-11-05 13:29:25 +0200 | [diff] [blame] | 365 | vlib_buffer_put_uninit (vlib_buffer_t * b, u16 size) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 366 | { |
| 367 | void *p = vlib_buffer_get_tail (b); |
| 368 | /* XXX make sure there's enough space */ |
| 369 | b->current_length += size; |
| 370 | return p; |
| 371 | } |
| 372 | |
| 373 | /** \brief Prepend uninitialized data to buffer |
| 374 | * @param b pointer to the buffer |
| 375 | * @param size number of uninitialized bytes |
| 376 | * @return pointer to beginning of uninitialized data |
| 377 | */ |
| 378 | always_inline void * |
| 379 | vlib_buffer_push_uninit (vlib_buffer_t * b, u8 size) |
| 380 | { |
| 381 | ASSERT (b->current_data + VLIB_BUFFER_PRE_DATA_SIZE >= size); |
| 382 | b->current_data -= size; |
| 383 | b->current_length += size; |
| 384 | |
| 385 | return vlib_buffer_get_current (b); |
| 386 | } |
| 387 | |
| 388 | /** \brief Make head room, typically for packet headers |
| 389 | * @param b pointer to the buffer |
| 390 | * @param size number of head room bytes |
| 391 | * @return pointer to start of buffer (current data) |
| 392 | */ |
| 393 | always_inline void * |
| 394 | vlib_buffer_make_headroom (vlib_buffer_t * b, u8 size) |
| 395 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 396 | b->current_data += size; |
| 397 | return vlib_buffer_get_current (b); |
| 398 | } |
| 399 | |
Dave Barach | a638c18 | 2019-06-21 18:24:07 -0400 | [diff] [blame] | 400 | /** \brief Construct a trace handle from thread and pool index |
| 401 | * @param thread Thread id |
| 402 | * @param pool_index Pool index |
| 403 | * @return trace handle |
| 404 | */ |
| 405 | always_inline u32 |
| 406 | vlib_buffer_make_trace_handle (u32 thread, u32 pool_index) |
| 407 | { |
| 408 | u32 rv; |
| 409 | ASSERT (thread < 0xff); |
| 410 | ASSERT (pool_index < 0x00FFFFFF); |
| 411 | rv = (thread << 24) | (pool_index & 0x00FFFFFF); |
| 412 | return rv; |
| 413 | } |
| 414 | |
| 415 | /** \brief Extract the thread id from a trace handle |
| 416 | * @param trace_handle the trace handle |
| 417 | * @return the thread id |
| 418 | */ |
| 419 | always_inline u32 |
| 420 | vlib_buffer_get_trace_thread (vlib_buffer_t * b) |
| 421 | { |
| 422 | u32 trace_handle = b->trace_handle; |
| 423 | |
| 424 | return trace_handle >> 24; |
| 425 | } |
| 426 | |
| 427 | /** \brief Extract the trace (pool) index from a trace handle |
| 428 | * @param trace_handle the trace handle |
| 429 | * @return the trace index |
| 430 | */ |
| 431 | always_inline u32 |
| 432 | vlib_buffer_get_trace_index (vlib_buffer_t * b) |
| 433 | { |
| 434 | u32 trace_handle = b->trace_handle; |
| 435 | return trace_handle & 0x00FFFFFF; |
| 436 | } |
| 437 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 438 | /** \brief Retrieve bytes from buffer head |
| 439 | * @param b pointer to the buffer |
| 440 | * @param size number of bytes to pull |
| 441 | * @return pointer to start of buffer (current data) |
| 442 | */ |
| 443 | always_inline void * |
| 444 | vlib_buffer_pull (vlib_buffer_t * b, u8 size) |
| 445 | { |
| 446 | if (b->current_length + VLIB_BUFFER_PRE_DATA_SIZE < size) |
| 447 | return 0; |
| 448 | |
| 449 | void *data = vlib_buffer_get_current (b); |
| 450 | vlib_buffer_advance (b, size); |
| 451 | return data; |
| 452 | } |
| 453 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | /* Forward declaration. */ |
| 455 | struct vlib_main_t; |
| 456 | |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 457 | #define VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ 512 |
| 458 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 459 | typedef struct |
| 460 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 461 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 462 | u32 cached_buffers[VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ]; |
| 463 | u32 n_cached; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 464 | } vlib_buffer_pool_thread_t; |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 465 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 466 | typedef struct |
| 467 | { |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 468 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 469 | uword start; |
| 470 | uword size; |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 471 | u8 log2_page_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 472 | u8 index; |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 473 | u8 numa_node; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 474 | u32 physmem_map_index; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 475 | u32 data_size; |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 476 | u32 alloc_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 477 | u32 n_buffers; |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 478 | u32 n_avail; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 479 | u32 *buffers; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 480 | u8 *name; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 481 | clib_spinlock_t lock; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 482 | |
| 483 | /* per-thread data */ |
| 484 | vlib_buffer_pool_thread_t *threads; |
| 485 | |
| 486 | /* buffer metadata template */ |
Damjan Marion | bf23663 | 2023-10-13 09:59:00 +0000 | [diff] [blame] | 487 | vlib_buffer_template_t buffer_template; |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 488 | } vlib_buffer_pool_t; |
| 489 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 490 | #define VLIB_BUFFER_MAX_NUMA_NODES 32 |
| 491 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 492 | typedef u32 (vlib_buffer_alloc_free_callback_t) (struct vlib_main_t *vm, |
| 493 | u8 buffer_pool_index, |
| 494 | u32 *buffers, u32 n_buffers); |
| 495 | |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 496 | typedef struct |
| 497 | { |
| 498 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 499 | /* Virtual memory address and size of buffer memory, used for calculating |
| 500 | buffer index */ |
| 501 | uword buffer_mem_start; |
| 502 | uword buffer_mem_size; |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 503 | vlib_buffer_pool_t *buffer_pools; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 504 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 505 | vlib_buffer_alloc_free_callback_t *alloc_callback_fn; |
| 506 | vlib_buffer_alloc_free_callback_t *free_callback_fn; |
| 507 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 508 | u8 default_buffer_pool_index_for_numa[VLIB_BUFFER_MAX_NUMA_NODES]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 510 | /* config */ |
Lukas Stockner | 63ff7a6 | 2024-06-04 16:14:55 +0200 | [diff] [blame] | 511 | u32 default_buffers_per_numa; |
| 512 | u32 buffers_per_numa[VLIB_BUFFER_MAX_NUMA_NODES]; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 513 | u16 ext_hdr_size; |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 514 | u32 default_data_size; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 515 | clib_mem_page_sz_t log2_page_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 516 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 517 | /* Hash table mapping buffer index into number |
| 518 | 0 => allocated but free, 1 => allocated and not-free. |
| 519 | If buffer index is not in hash table then this buffer |
| 520 | has never been allocated. */ |
| 521 | uword *buffer_known_hash; |
| 522 | clib_spinlock_t buffer_known_hash_lockp; |
| 523 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 524 | /* logging */ |
| 525 | vlib_log_class_t log_default; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | } vlib_buffer_main_t; |
| 527 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 528 | clib_error_t *vlib_buffer_main_init (struct vlib_main_t *vm); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 529 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 530 | format_function_t format_vlib_buffer_pool_all; |
| 531 | |
| 532 | int vlib_buffer_set_alloc_free_callback ( |
| 533 | struct vlib_main_t *vm, vlib_buffer_alloc_free_callback_t *alloc_callback_fn, |
| 534 | vlib_buffer_alloc_free_callback_t *free_callback_fn); |
| 535 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 536 | extern u16 __vlib_buffer_external_hdr_size; |
| 537 | #define VLIB_BUFFER_SET_EXT_HDR_SIZE(x) \ |
| 538 | static void __clib_constructor \ |
| 539 | vnet_buffer_set_ext_hdr_size() \ |
| 540 | { \ |
| 541 | if (__vlib_buffer_external_hdr_size) \ |
| 542 | clib_error ("buffer external header space already set"); \ |
| 543 | __vlib_buffer_external_hdr_size = CLIB_CACHE_LINE_ROUND (x); \ |
| 544 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 545 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 546 | #endif /* included_vlib_buffer_h */ |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 547 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 548 | /* |
| 549 | * fd.io coding-style-patch-verification: ON |
| 550 | * |
| 551 | * Local Variables: |
| 552 | * eval: (c-set-style "gnu") |
| 553 | * End: |
| 554 | */ |