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