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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * buffer.h: VLIB buffers
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #ifndef included_vlib_buffer_h
41 #define included_vlib_buffer_h
43 #include <vppinfra/types.h>
44 #include <vppinfra/cache.h>
45 #include <vppinfra/serialize.h>
46 #include <vppinfra/vector.h>
47 #include <vppinfra/lock.h>
48 #include <vlib/error.h> /* for vlib_error_t */
50 #include <vlib/config.h> /* for __PRE_DATA_SIZE */
51 #define VLIB_BUFFER_PRE_DATA_SIZE __PRE_DATA_SIZE
53 #define VLIB_BUFFER_DEFAULT_DATA_SIZE (2048)
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)
60 /* Amount of head buffer data copied to each replica head buffer */
61 #define VLIB_BUFFER_CLONE_HEAD_SIZE (256)
64 vlib buffer structure definition and a few select
65 access methods. This structure and the buffer allocation
66 mechanism should perhaps live in vnet, but it would take a lot
67 of typing to make it so.
73 #define foreach_vlib_buffer_flag \
75 _( 1, NEXT_PRESENT, "next-present") \
76 _( 2, TOTAL_LENGTH_VALID, 0) \
77 _( 3, EXT_HDR_VALID, "ext-hdr-valid")
79 /* NOTE: only buffer generic flags should be defined here, please consider
80 using user flags. i.e. src/vnet/buffer.h */
84 #define _(bit, name, v) VLIB_BUFFER_##name = (1 << (bit)),
85 foreach_vlib_buffer_flag
91 #define _(bit, name, v) VLIB_BUFFER_LOG2_##name = (bit),
92 foreach_vlib_buffer_flag
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))
99 #define VLIB_BUFFER_FLAGS_ALL (0x0f)
101 /** \brief Compile time buffer trajectory tracing option
102 Turn this on if you run into "bad monkey" contexts,
103 and you want to know exactly which nodes they've visited...
106 #ifndef VLIB_BUFFER_TRACE_TRAJECTORY
107 #define VLIB_BUFFER_TRACE_TRAJECTORY 0
108 #endif /* VLIB_BUFFER_TRACE_TRAJECTORY */
110 /** VLIB buffer representation. */
115 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
117 /** signed offset in data[], pre_data[] that we are currently
118 * processing. If negative current header points into predata area. */
121 /** Nbytes between current data and the end of this buffer. */
125 <br> VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,
126 <br> VLIB_BUFFER_IS_TRACED: trace this buffer.
127 <br> VLIB_BUFFER_NEXT_PRESENT: this is a multi-chunk buffer.
128 <br> VLIB_BUFFER_TOTAL_LENGTH_VALID: as it says
129 <br> VLIB_BUFFER_EXT_HDR_VALID: buffer contains valid external buffer manager header,
130 set to avoid adding it to a flow report
131 <br> VLIB_BUFFER_FLAG_USER(n): user-defined bit N
135 /** Generic flow identifier */
138 /** Reference count for this buffer. */
139 volatile u8 ref_count;
141 /** index of buffer pool this buffer belongs. */
142 u8 buffer_pool_index;
144 /** Error code for buffers to be enqueued to error handler. */
147 /** Next buffer for this linked-list of buffers. Only valid if
148 * VLIB_BUFFER_NEXT_PRESENT flag is set. */
151 /** The following fields can be in a union because once a packet enters
152 * the punt path, it is no longer on a feature arc */
155 /** Used by feature subgraph arcs to visit enabled feature nodes */
156 u32 current_config_index;
157 /* the reason the packet once punted */
161 /** Opaque data used by sub-graphs for their own purposes. */
164 /** part of buffer metadata which is initialized on alloc ends here. */
165 STRUCT_MARK (template_end);
167 /** start of 2nd half (2nd cacheline on systems where cacheline size is 64) */
168 CLIB_ALIGN_MARK (second_half, 64);
170 /** Specifies trace buffer handle if VLIB_PACKET_IS_TRACED flag is
174 /** Only valid for first buffer in chain. Current length plus total length
175 * given here give total number of bytes in buffer chain. */
176 u32 total_length_not_including_first_buffer;
178 /**< More opaque data, see ../vnet/vnet/buffer.h */
181 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
182 /** trace trajectory data - we use a specific cacheline for that in the
183 * buffer when it is compiled-in */
184 #define VLIB_BUFFER_TRACE_TRAJECTORY_MAX 31
185 #define VLIB_BUFFER_TRACE_TRAJECTORY_SZ 64
186 #define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b) (b)->trajectory_nb = 0
187 CLIB_ALIGN_MARK (trajectory, 64);
189 u16 trajectory_trace[VLIB_BUFFER_TRACE_TRAJECTORY_MAX];
190 #else /* VLIB_BUFFER_TRACE_TRAJECTORY */
191 #define VLIB_BUFFER_TRACE_TRAJECTORY_SZ 0
192 #define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
193 #endif /* VLIB_BUFFER_TRACE_TRAJECTORY */
195 /** start of buffer headroom */
196 CLIB_ALIGN_MARK (headroom, 64);
198 /** Space for inserting data before buffer start. Packet rewrite string
199 * will be rewritten backwards and may extend back before
200 * buffer->data[0]. Must come directly before packet data. */
201 u8 pre_data[VLIB_BUFFER_PRE_DATA_SIZE];
206 #ifdef CLIB_HAVE_VEC128
209 #ifdef CLIB_HAVE_VEC256
212 #ifdef CLIB_HAVE_VEC512
217 STATIC_ASSERT_SIZEOF (vlib_buffer_t, 128 + VLIB_BUFFER_TRACE_TRAJECTORY_SZ +
218 VLIB_BUFFER_PRE_DATA_SIZE);
219 STATIC_ASSERT (VLIB_BUFFER_PRE_DATA_SIZE % CLIB_CACHE_LINE_BYTES == 0,
220 "VLIB_BUFFER_PRE_DATA_SIZE must be divisible by cache line size");
222 #define VLIB_BUFFER_HDR_SIZE (sizeof(vlib_buffer_t) - VLIB_BUFFER_PRE_DATA_SIZE)
224 /** \brief Prefetch buffer metadata.
225 The first 64 bytes of buffer contains most header information
227 @param b - (vlib_buffer_t *) pointer to the buffer
228 @param type - LOAD, STORE. In most cases, STORE is the right answer
231 #define vlib_prefetch_buffer_header(b,type) CLIB_PREFETCH (b, 64, type)
232 #define vlib_prefetch_buffer_data(b,type) \
233 CLIB_PREFETCH (vlib_buffer_get_current(b), CLIB_CACHE_LINE_BYTES, type)
236 vlib_buffer_struct_is_sane (vlib_buffer_t * b)
238 ASSERT (sizeof (b[0]) % 64 == 0);
240 /* Rewrite data must be before and contiguous with packet data. */
241 ASSERT (b->pre_data + VLIB_BUFFER_PRE_DATA_SIZE == b->data);
245 vlib_buffer_get_va (vlib_buffer_t * b)
247 return pointer_to_uword (b->data);
250 /** \brief Get pointer to current data to process
252 @param b - (vlib_buffer_t *) pointer to the buffer
253 @return - (void *) (b->data + b->current_data)
257 vlib_buffer_get_current (vlib_buffer_t * b)
260 ASSERT ((signed) b->current_data >= (signed) -VLIB_BUFFER_PRE_DATA_SIZE);
261 return b->data + b->current_data;
265 vlib_buffer_get_current_va (vlib_buffer_t * b)
267 return vlib_buffer_get_va (b) + b->current_data;
270 /** \brief Advance current data pointer by the supplied (signed!) amount
272 @param b - (vlib_buffer_t *) pointer to the buffer
273 @param l - (word) signed increment
276 vlib_buffer_advance (vlib_buffer_t * b, word l)
278 ASSERT (b->current_length >= l);
279 b->current_data += l;
280 b->current_length -= l;
282 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0 ||
283 b->current_length >= VLIB_BUFFER_MIN_CHAIN_SEG_SIZE);
286 /** \brief Check if there is enough space in buffer to advance
288 @param b - (vlib_buffer_t *) pointer to the buffer
289 @param l - (word) size to check
290 @return - 0 if there is less space than 'l' in buffer
293 vlib_buffer_has_space (vlib_buffer_t * b, word l)
295 return b->current_length >= l;
298 /** \brief Reset current header & length to state they were in when
301 @param b - (vlib_buffer_t *) pointer to the buffer
305 vlib_buffer_reset (vlib_buffer_t * b)
307 b->current_length += clib_max (b->current_data, 0);
311 /** \brief Get pointer to buffer's opaque data array
313 @param b - (vlib_buffer_t *) pointer to the buffer
314 @return - (void *) b->opaque
317 vlib_get_buffer_opaque (vlib_buffer_t * b)
319 return (void *) b->opaque;
322 /** \brief Get pointer to buffer's opaque2 data array
324 @param b - (vlib_buffer_t *) pointer to the buffer
325 @return - (void *) b->opaque2
328 vlib_get_buffer_opaque2 (vlib_buffer_t * b)
330 return (void *) b->opaque2;
333 /** \brief Get pointer to the end of buffer's data
334 * @param b pointer to the buffer
335 * @return pointer to tail of packet's data
338 vlib_buffer_get_tail (vlib_buffer_t * b)
340 return b->data + b->current_data + b->current_length;
343 /** \brief Append uninitialized data to buffer
344 * @param b pointer to the buffer
345 * @param size number of uninitialized bytes
346 * @return pointer to beginning of uninitialized data
349 vlib_buffer_put_uninit (vlib_buffer_t * b, u16 size)
351 void *p = vlib_buffer_get_tail (b);
352 /* XXX make sure there's enough space */
353 b->current_length += size;
357 /** \brief Prepend uninitialized data to buffer
358 * @param b pointer to the buffer
359 * @param size number of uninitialized bytes
360 * @return pointer to beginning of uninitialized data
363 vlib_buffer_push_uninit (vlib_buffer_t * b, u8 size)
365 ASSERT (b->current_data + VLIB_BUFFER_PRE_DATA_SIZE >= size);
366 b->current_data -= size;
367 b->current_length += size;
369 return vlib_buffer_get_current (b);
372 /** \brief Make head room, typically for packet headers
373 * @param b pointer to the buffer
374 * @param size number of head room bytes
375 * @return pointer to start of buffer (current data)
378 vlib_buffer_make_headroom (vlib_buffer_t * b, u8 size)
380 b->current_data += size;
381 return vlib_buffer_get_current (b);
384 /** \brief Construct a trace handle from thread and pool index
385 * @param thread Thread id
386 * @param pool_index Pool index
387 * @return trace handle
390 vlib_buffer_make_trace_handle (u32 thread, u32 pool_index)
393 ASSERT (thread < 0xff);
394 ASSERT (pool_index < 0x00FFFFFF);
395 rv = (thread << 24) | (pool_index & 0x00FFFFFF);
399 /** \brief Extract the thread id from a trace handle
400 * @param trace_handle the trace handle
401 * @return the thread id
404 vlib_buffer_get_trace_thread (vlib_buffer_t * b)
406 u32 trace_handle = b->trace_handle;
408 return trace_handle >> 24;
411 /** \brief Extract the trace (pool) index from a trace handle
412 * @param trace_handle the trace handle
413 * @return the trace index
416 vlib_buffer_get_trace_index (vlib_buffer_t * b)
418 u32 trace_handle = b->trace_handle;
419 return trace_handle & 0x00FFFFFF;
422 /** \brief Retrieve bytes from buffer head
423 * @param b pointer to the buffer
424 * @param size number of bytes to pull
425 * @return pointer to start of buffer (current data)
428 vlib_buffer_pull (vlib_buffer_t * b, u8 size)
430 if (b->current_length + VLIB_BUFFER_PRE_DATA_SIZE < size)
433 void *data = vlib_buffer_get_current (b);
434 vlib_buffer_advance (b, size);
438 /* Forward declaration. */
441 #define VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ 512
445 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
446 u32 cached_buffers[VLIB_BUFFER_POOL_PER_THREAD_CACHE_SZ];
448 } vlib_buffer_pool_thread_t;
452 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
455 uword log2_page_size;
458 u32 physmem_map_index;
464 clib_spinlock_t lock;
466 /* per-thread data */
467 vlib_buffer_pool_thread_t *threads;
469 /* buffer metadata template */
470 vlib_buffer_t buffer_template;
471 } vlib_buffer_pool_t;
473 #define VLIB_BUFFER_MAX_NUMA_NODES 32
475 typedef u32 (vlib_buffer_alloc_free_callback_t) (struct vlib_main_t *vm,
476 u8 buffer_pool_index,
477 u32 *buffers, u32 n_buffers);
481 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
482 /* Virtual memory address and size of buffer memory, used for calculating
484 uword buffer_mem_start;
485 uword buffer_mem_size;
486 vlib_buffer_pool_t *buffer_pools;
488 vlib_buffer_alloc_free_callback_t *alloc_callback_fn;
489 vlib_buffer_alloc_free_callback_t *free_callback_fn;
491 u8 default_buffer_pool_index_for_numa[VLIB_BUFFER_MAX_NUMA_NODES];
494 u32 buffers_per_numa;
496 u32 default_data_size;
497 clib_mem_page_sz_t log2_page_size;
499 /* Hash table mapping buffer index into number
500 0 => allocated but free, 1 => allocated and not-free.
501 If buffer index is not in hash table then this buffer
502 has never been allocated. */
503 uword *buffer_known_hash;
504 clib_spinlock_t buffer_known_hash_lockp;
507 vlib_log_class_t log_default;
508 } vlib_buffer_main_t;
510 clib_error_t *vlib_buffer_main_init (struct vlib_main_t *vm);
512 format_function_t format_vlib_buffer_pool_all;
514 int vlib_buffer_set_alloc_free_callback (
515 struct vlib_main_t *vm, vlib_buffer_alloc_free_callback_t *alloc_callback_fn,
516 vlib_buffer_alloc_free_callback_t *free_callback_fn);
518 extern u16 __vlib_buffer_external_hdr_size;
519 #define VLIB_BUFFER_SET_EXT_HDR_SIZE(x) \
520 static void __clib_constructor \
521 vnet_buffer_set_ext_hdr_size() \
523 if (__vlib_buffer_external_hdr_size) \
524 clib_error ("buffer external header space already set"); \
525 __vlib_buffer_external_hdr_size = CLIB_CACHE_LINE_ROUND (x); \
528 #endif /* included_vlib_buffer_h */
531 * fd.io coding-style-patch-verification: ON
534 * eval: (c-set-style "gnu")