/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * buffer.h: VLIB buffers * * Copyright (c) 2008 Eliot Dresselhaus * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef included_vlib_buffer_h #define included_vlib_buffer_h #include #include #include #include #include #include /* for vlib_error_t */ #include /* for __PRE_DATA_SIZE */ #define VLIB_BUFFER_PRE_DATA_SIZE __PRE_DATA_SIZE #define VLIB_BUFFER_DEFAULT_DATA_SIZE (2048) /* Minimum buffer chain segment size. Does not apply to last buffer in chain. Dataplane code can safely asume that specified amount of data is not split into 2 chained buffers */ #define VLIB_BUFFER_MIN_CHAIN_SEG_SIZE (128) /* Amount of head buffer data copied to each replica head buffer */ #define VLIB_BUFFER_CLONE_HEAD_SIZE (256) /** \file vlib buffer structure definition and a few select access methods. This structure and the buffer allocation mechanism should perhaps live in vnet, but it would take a lot of typing to make it so. */ /** * Buffer Flags */ #define foreach_vlib_buffer_flag \ _( 0, IS_TRACED, 0) \ _( 1, NEXT_PRESENT, "next-present") \ _( 2, TOTAL_LENGTH_VALID, 0) \ _( 3, EXT_HDR_VALID, "ext-hdr-valid") /* NOTE: only buffer generic flags should be defined here, please consider using user flags. i.e. src/vnet/buffer.h */ enum { #define _(bit, name, v) VLIB_BUFFER_##name = (1 << (bit)), foreach_vlib_buffer_flag #undef _ }; enum { #define _(bit, name, v) VLIB_BUFFER_LOG2_##name = (bit), foreach_vlib_buffer_flag #undef _ }; /* User defined buffer flags. */ #define LOG2_VLIB_BUFFER_FLAG_USER(n) (32 - (n)) #define VLIB_BUFFER_FLAG_USER(n) (1 << LOG2_VLIB_BUFFER_FLAG_USER(n)) #define VLIB_BUFFER_FLAGS_ALL (0x0f) /** VLIB buffer representation. */ typedef union { struct { CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); /** signed offset in data[], pre_data[] that we are currently * processing. If negative current header points into predata area. */ i16 current_data; /** Nbytes between current data and the end of this buffer. */ u16 current_length; /** buffer flags:
VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,
VLIB_BUFFER_IS_TRACED: trace this buffer.
VLIB_BUFFER_NEXT_PRESENT: this is a multi-chunk buffer.
VLIB_BUFFER_TOTAL_LENGTH_VALID: as it says
VLIB_BUFFER_EXT_HDR_VALID: buffer contains valid external buffer manager header, set to avoid adding it to a flow report
VLIB_BUFFER_FLAG_USER(n): user-defined bit N */ u32 flags; /** Generic flow identifier */ u32 flow_id; /** Reference count for this buffer. */ volatile u8 ref_count; /** index of buffer pool this buffer belongs. */ u8 buffer_pool_index; /** Error code for buffers to be enqueued to error handler. */ vlib_error_t error; /** Next buffer for this linked-list of buffers. Only valid if * VLIB_BUFFER_NEXT_PRESENT flag is set. */ u32 next_buffer; /** The following fields can be in a union because once a packet enters * the punt path, it is no longer on a feature arc */ union { /** Used by feature subgraph arcs to visit enabled feature nodes */ u32 current_config_index; /* the reason the packet once punted */ u32 punt_reason; }; /** Opaque data used by sub-graphs for their own purposes. */ u32 opaque[10]; /** part of buffer metadata which is initialized on alloc ends here. */ STRUCT_MARK (template_end); /** start of 2nd cache line */ CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); /** Specifies trace buffer handle if VLIB_PACKET_IS_TRACED flag is * set. */ u32 trace_handle; /** Only valid for first buffer in chain. Current length plus total length * given here give total number of bytes in buffer chain. */ u32 total_length_not_including_first_buffer; /**< More opaque data, see ../vnet/vnet/buffer.h */ u32 opaque2[14]; /** start of third cache line */ CLIB_CACHE_LINE_ALIGN_MARK (cacheline2); /** Space for inserting data before buffer start. Packet rewrite string * will be rewritten backwards and may extend back before * buffer->data[0]. Must come directly before packet data. */ u8 pre_data[VLIB_BUFFER_PRE_DATA_SIZE]; /** Packet data */ u8 data[]; }; #ifdef CLIB_HAVE_VEC128 u8x16 as_u8x16[4]; #endif #ifdef CLIB_HAVE_VEC256 u8x32 as_u8x32[2]; #endif #ifdef CLIB_HAVE_VEC512 u8x64 as_u8x64[1]; #endif } vlib_buffer_t; #define VLIB_BUFFER_HDR_SIZE (sizeof(vlib_buffer_t) - VLIB_BUFFER_PRE_DATA_SIZE) /** \brief Prefetch buffer metadata. The first 64 bytes of buffer contains most header information @param b - (vlib_buffer_t *) pointer to the buffer @param type - LOAD, STORE. In most cases, STORE is the right answer */ #define vlib_prefetch_buffer_header(b,type) CLIB_PREFETCH (b, 64, type) #define vlib_prefetch_buffer_data(b,type) \ CLIB_PREFETCH (vlib_buffer_get_current(b), CLIB_CACHE_LINE_BYTES, type) always_inline void vlib_buffer_struct_is_sane (vlib_buffer_t * b) { ASSERT (sizeof (b[0]) % 64 == 0); /* Rewrite data must be before and contiguous with packet data. */ ASSERT (b->pre_data + VLIB_BUFFER_PRE_DATA_SIZE == b->data); } always_inline uword vlib_buffer_get_va (vlib_buffer_t * b) { return pointer_to_uword (b->data); } /** \brief Get pointer to current data to process @param b - (vlib_buffer_t *) pointer to the buffer @return - (void *) (b->data + b->current_data) */ always_inline void * vlib_buffer_get_current (vlib_buffer_t * b) { /* Check bounds. */ ASSERT ((signed) b->current_data >= (signed) -VLIB_BUFFER_PRE_DATA_SIZE); return b->data + b->current_data; } always_inline uword vlib_buffer_get_current_va (vlib_buffer_t * b) { return vlib_buffer_get_va (b) + b->current_data; } /** \brief Advance current data pointer by the supplied (signed!) amount @param b - (vlib_buffer_t *) pointer to the buffer @param l - (word) signed