1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2014 6WIND S.A.
13 * The mbuf library provides the ability to create and destroy buffers
14 * that may be used by the RTE application to store message
15 * buffers. The message buffers are stored in a mempool, using the
16 * RTE mempool library.
18 * The preferred way to create a mbuf pool is to use
19 * rte_pktmbuf_pool_create(). However, in some situations, an
20 * application may want to have more control (ex: populate the pool with
21 * specific memory), in this case it is possible to use functions from
22 * rte_mempool. See how rte_pktmbuf_pool_create() is implemented for
25 * This library provides an API to allocate/free packet mbufs, which are
26 * used to carry network packets.
28 * To understand the concepts of packet buffers or mbufs, you
29 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
30 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
31 * http://www.kohala.com/start/tcpipiv2.html
35 #include <rte_compat.h>
36 #include <rte_common.h>
37 #include <rte_config.h>
38 #include <rte_mempool.h>
39 #include <rte_prefetch.h>
40 #include <rte_branch_prediction.h>
41 #include <rte_mbuf_ptype.h>
42 #include <rte_mbuf_core.h>
49 * Get the name of a RX offload flag
52 * The mask describing the flag.
54 * The name of this flag, or NULL if it's not a valid RX flag.
56 const char *rte_get_rx_ol_flag_name(uint64_t mask);
59 * Dump the list of RX offload flags in a buffer
62 * The mask describing the RX flags.
66 * The length of the buffer.
68 * 0 on success, (-1) on error.
70 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
73 * Get the name of a TX offload flag
76 * The mask describing the flag. Usually only one bit must be set.
77 * Several bits can be given if they belong to the same mask.
78 * Ex: RTE_MBUF_F_TX_L4_MASK.
80 * The name of this flag, or NULL if it's not a valid TX flag.
82 const char *rte_get_tx_ol_flag_name(uint64_t mask);
85 * Dump the list of TX offload flags in a buffer
88 * The mask describing the TX flags.
92 * The length of the buffer.
94 * 0 on success, (-1) on error.
96 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
99 * Prefetch the first part of the mbuf
101 * The first 64 bytes of the mbuf corresponds to fields that are used early
102 * in the receive path. If the cache line of the architecture is higher than
103 * 64B, the second part will also be prefetched.
106 * The pointer to the mbuf.
109 rte_mbuf_prefetch_part1(struct rte_mbuf *m)
111 rte_prefetch0(&m->cacheline0);
115 * Prefetch the second part of the mbuf
117 * The next 64 bytes of the mbuf corresponds to fields that are used in the
118 * transmit path. If the cache line of the architecture is higher than 64B,
119 * this function does nothing as it is expected that the full mbuf is
123 * The pointer to the mbuf.
126 rte_mbuf_prefetch_part2(struct rte_mbuf *m)
128 #if RTE_CACHE_LINE_SIZE == 64
129 rte_prefetch0(&m->cacheline1);
136 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
139 * Return the IO address of the beginning of the mbuf data
142 * The pointer to the mbuf.
144 * The IO address of the beginning of the mbuf data
146 static inline rte_iova_t
147 rte_mbuf_data_iova(const struct rte_mbuf *mb)
149 return mb->buf_iova + mb->data_off;
153 * Return the default IO address of the beginning of the mbuf data
155 * This function is used by drivers in their receive function, as it
156 * returns the location where data should be written by the NIC, taking
157 * the default headroom in account.
160 * The pointer to the mbuf.
162 * The IO address of the beginning of the mbuf data
164 static inline rte_iova_t
165 rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
167 return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
171 * Return the mbuf owning the data buffer address of an indirect mbuf.
174 * The pointer to the indirect mbuf.
176 * The address of the direct mbuf corresponding to buffer_addr.
178 static inline struct rte_mbuf *
179 rte_mbuf_from_indirect(struct rte_mbuf *mi)
181 return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
185 * Return address of buffer embedded in the given mbuf.
187 * The return value shall be same as mb->buf_addr if the mbuf is already
188 * initialized and direct. However, this API is useful if mempool of the
189 * mbuf is already known because it doesn't need to access mbuf contents in
190 * order to get the mempool pointer.
193 * The pointer to the mbuf.
195 * The pointer to the mempool of the mbuf.
197 * The pointer of the mbuf buffer.
200 rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
202 return (char *)mb + sizeof(*mb) + rte_pktmbuf_priv_size(mp);
206 * Return the default address of the beginning of the mbuf data.
209 * The pointer to the mbuf.
211 * The pointer of the beginning of the mbuf data.
214 rte_mbuf_data_addr_default(struct rte_mbuf *mb)
216 return rte_mbuf_buf_addr(mb, mb->pool) + RTE_PKTMBUF_HEADROOM;
220 * Return address of buffer embedded in the given mbuf.
222 * @note: Accessing mempool pointer of a mbuf is expensive because the
223 * pointer is stored in the 2nd cache line of mbuf. If mempool is known, it
224 * is better not to reference the mempool pointer in mbuf but calling
225 * rte_mbuf_buf_addr() would be more efficient.
228 * The pointer to the mbuf.
230 * The address of the data buffer owned by the mbuf.
233 rte_mbuf_to_baddr(struct rte_mbuf *md)
235 return rte_mbuf_buf_addr(md, md->pool);
239 * Return the starting address of the private data area embedded in
242 * Note that no check is made to ensure that a private data area
243 * actually exists in the supplied mbuf.
246 * The pointer to the mbuf.
248 * The starting address of the private data area of the given mbuf.
251 rte_mbuf_to_priv(struct rte_mbuf *m)
253 return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
257 * Private data in case of pktmbuf pool.
259 * A structure that contains some pktmbuf_pool-specific data that are
260 * appended after the mempool structure (in private data).
262 struct rte_pktmbuf_pool_private {
263 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
264 uint16_t mbuf_priv_size; /**< Size of private area in each mbuf. */
265 uint32_t flags; /**< reserved for future use. */
269 * Return the flags from private data in an mempool structure.
272 * A pointer to the mempool structure.
274 * The flags from the private data structure.
276 static inline uint32_t
277 rte_pktmbuf_priv_flags(struct rte_mempool *mp)
279 struct rte_pktmbuf_pool_private *mbp_priv;
281 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
282 return mbp_priv->flags;
286 * When set, pktmbuf mempool will hold only mbufs with pinned external
287 * buffer. The external buffer will be attached to the mbuf at the
288 * memory pool creation and will never be detached by the mbuf free calls.
289 * mbuf should not contain any room for data after the mbuf structure.
291 #define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF (1 << 0)
294 * Returns non zero if given mbuf has a pinned external buffer, or zero
295 * otherwise. The pinned external buffer is allocated at pool creation
296 * time and should not be freed on mbuf freeing.
298 * External buffer is a user-provided anonymous buffer.
300 #define RTE_MBUF_HAS_PINNED_EXTBUF(mb) \
301 (rte_pktmbuf_priv_flags(mb->pool) & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
303 #ifdef RTE_LIBRTE_MBUF_DEBUG
305 /** check mbuf type in debug mode */
306 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
308 #else /* RTE_LIBRTE_MBUF_DEBUG */
310 /** check mbuf type in debug mode */
311 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
313 #endif /* RTE_LIBRTE_MBUF_DEBUG */
315 #ifdef RTE_MBUF_REFCNT_ATOMIC
318 * Reads the value of an mbuf's refcnt.
322 * Reference count number.
324 static inline uint16_t
325 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
327 return __atomic_load_n(&m->refcnt, __ATOMIC_RELAXED);
331 * Sets an mbuf's refcnt to a defined value.
338 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
340 __atomic_store_n(&m->refcnt, new_value, __ATOMIC_RELAXED);
344 static inline uint16_t
345 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
347 return __atomic_add_fetch(&m->refcnt, (uint16_t)value,
352 * Adds given value to an mbuf's refcnt and returns its new value.
356 * Value to add/subtract
360 static inline uint16_t
361 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
364 * The atomic_add is an expensive operation, so we don't want to
365 * call it in the case where we know we are the unique holder of
366 * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
367 * operation has to be used because concurrent accesses on the
368 * reference counter can occur.
370 if (likely(rte_mbuf_refcnt_read(m) == 1)) {
372 rte_mbuf_refcnt_set(m, (uint16_t)value);
373 return (uint16_t)value;
376 return __rte_mbuf_refcnt_update(m, value);
379 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
382 static inline uint16_t
383 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
385 m->refcnt = (uint16_t)(m->refcnt + value);
390 * Adds given value to an mbuf's refcnt and returns its new value.
392 static inline uint16_t
393 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
395 return __rte_mbuf_refcnt_update(m, value);
399 * Reads the value of an mbuf's refcnt.
401 static inline uint16_t
402 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
408 * Sets an mbuf's refcnt to the defined value.
411 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
413 m->refcnt = new_value;
416 #endif /* RTE_MBUF_REFCNT_ATOMIC */
419 * Reads the refcnt of an external buffer.
422 * Shared data of the external buffer.
424 * Reference count number.
426 static inline uint16_t
427 rte_mbuf_ext_refcnt_read(const struct rte_mbuf_ext_shared_info *shinfo)
429 return __atomic_load_n(&shinfo->refcnt, __ATOMIC_RELAXED);
433 * Set refcnt of an external buffer.
436 * Shared data of the external buffer.
441 rte_mbuf_ext_refcnt_set(struct rte_mbuf_ext_shared_info *shinfo,
444 __atomic_store_n(&shinfo->refcnt, new_value, __ATOMIC_RELAXED);
448 * Add given value to refcnt of an external buffer and return its new
452 * Shared data of the external buffer.
454 * Value to add/subtract
458 static inline uint16_t
459 rte_mbuf_ext_refcnt_update(struct rte_mbuf_ext_shared_info *shinfo,
462 if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
464 rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
465 return (uint16_t)value;
468 return __atomic_add_fetch(&shinfo->refcnt, (uint16_t)value,
473 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
480 * Sanity checks on an mbuf.
482 * Check the consistency of the given mbuf. The function will cause a
483 * panic if corruption is detected.
486 * The mbuf to be checked.
488 * True if the mbuf is a packet header, false if it is a sub-segment
489 * of a packet (in this case, some fields like nb_segs are not checked)
492 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
495 * Sanity checks on a mbuf.
497 * Almost like rte_mbuf_sanity_check(), but this function gives the reason
498 * if corruption is detected rather than panic.
501 * The mbuf to be checked.
503 * True if the mbuf is a packet header, false if it is a sub-segment
504 * of a packet (in this case, some fields like nb_segs are not checked)
506 * A reference to a string pointer where to store the reason why a mbuf is
507 * considered invalid.
509 * - 0 if no issue has been found, reason is left untouched.
510 * - -1 if a problem is detected, reason then points to a string describing
511 * the reason why the mbuf is deemed invalid.
513 int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
514 const char **reason);
517 * Sanity checks on a reinitialized mbuf in debug mode.
519 * Check the consistency of the given reinitialized mbuf.
520 * The function will cause a panic if corruption is detected.
522 * Check that the mbuf is properly reinitialized (refcnt=1, next=NULL,
523 * nb_segs=1), as done by rte_pktmbuf_prefree_seg().
526 * The mbuf to be checked.
528 static __rte_always_inline void
529 __rte_mbuf_raw_sanity_check(__rte_unused const struct rte_mbuf *m)
531 RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
532 RTE_ASSERT(m->next == NULL);
533 RTE_ASSERT(m->nb_segs == 1);
534 __rte_mbuf_sanity_check(m, 0);
537 /** For backwards compatibility. */
538 #define MBUF_RAW_ALLOC_CHECK(m) __rte_mbuf_raw_sanity_check(m)
541 * Allocate an uninitialized mbuf from mempool *mp*.
543 * This function can be used by PMDs (especially in RX functions) to
544 * allocate an uninitialized mbuf. The driver is responsible of
545 * initializing all the required fields. See rte_pktmbuf_reset().
546 * For standard needs, prefer rte_pktmbuf_alloc().
548 * The caller can expect that the following fields of the mbuf structure
549 * are initialized: buf_addr, buf_iova, buf_len, refcnt=1, nb_segs=1,
550 * next=NULL, pool, priv_size. The other fields must be initialized
554 * The mempool from which mbuf is allocated.
556 * - The pointer to the new mbuf on success.
557 * - NULL if allocation failed.
559 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
563 if (rte_mempool_get(mp, (void **)&m) < 0)
565 __rte_mbuf_raw_sanity_check(m);
570 * Put mbuf back into its original mempool.
572 * The caller must ensure that the mbuf is direct and properly
573 * reinitialized (refcnt=1, next=NULL, nb_segs=1), as done by
574 * rte_pktmbuf_prefree_seg().
576 * This function should be used with care, when optimization is
577 * required. For standard needs, prefer rte_pktmbuf_free() or
578 * rte_pktmbuf_free_seg().
581 * The mbuf to be freed.
583 static __rte_always_inline void
584 rte_mbuf_raw_free(struct rte_mbuf *m)
586 RTE_ASSERT(!RTE_MBUF_CLONED(m) &&
587 (!RTE_MBUF_HAS_EXTBUF(m) || RTE_MBUF_HAS_PINNED_EXTBUF(m)));
588 __rte_mbuf_raw_sanity_check(m);
589 rte_mempool_put(m->pool, m);
593 * The packet mbuf constructor.
595 * This function initializes some fields in the mbuf structure that are
596 * not modified by the user once created (origin pool, buffer start
597 * address, and so on). This function is given as a callback function to
598 * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
600 * This function expects that the mempool private area was previously
601 * initialized with rte_pktmbuf_pool_init().
604 * The mempool from which mbufs originate.
606 * A pointer that can be used by the user to retrieve useful information
607 * for mbuf initialization. This pointer is the opaque argument passed to
608 * rte_mempool_obj_iter() or rte_mempool_create().
610 * The mbuf to initialize.
612 * The index of the mbuf in the pool table.
614 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
615 void *m, unsigned i);
618 * A packet mbuf pool constructor.
620 * This function initializes the mempool private data in the case of a
621 * pktmbuf pool. This private data is needed by the driver. The
622 * function must be called on the mempool before it is used, or it
623 * can be given as a callback function to rte_mempool_create() at
624 * pool creation. It can be extended by the user, for example, to
625 * provide another packet size.
627 * The mempool private area size must be at least equal to
628 * sizeof(struct rte_pktmbuf_pool_private).
631 * The mempool from which mbufs originate.
633 * A pointer that can be used by the user to retrieve useful information
634 * for mbuf initialization. This pointer is the opaque argument passed to
635 * rte_mempool_create().
637 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
640 * Create a mbuf pool.
642 * This function creates and initializes a packet mbuf pool. It is
643 * a wrapper to rte_mempool functions.
646 * The name of the mbuf pool.
648 * The number of elements in the mbuf pool. The optimum size (in terms
649 * of memory usage) for a mempool is when n is a power of two minus one:
652 * Size of the per-core object cache. See rte_mempool_create() for
655 * Size of application private are between the rte_mbuf structure
656 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
657 * @param data_room_size
658 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
660 * The socket identifier where the memory should be allocated. The
661 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
664 * The pointer to the new allocated mempool, on success. NULL on error
665 * with rte_errno set appropriately. Possible rte_errno values include:
666 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
667 * - E_RTE_SECONDARY - function was called from a secondary process instance
668 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
669 * - ENOSPC - the maximum number of memzones has already been allocated
670 * - EEXIST - a memzone with the same name already exists
671 * - ENOMEM - no appropriate memory area found in which to create memzone
674 rte_pktmbuf_pool_create(const char *name, unsigned n,
675 unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
679 * Create a mbuf pool with a given mempool ops name
681 * This function creates and initializes a packet mbuf pool. It is
682 * a wrapper to rte_mempool functions.
685 * The name of the mbuf pool.
687 * The number of elements in the mbuf pool. The optimum size (in terms
688 * of memory usage) for a mempool is when n is a power of two minus one:
691 * Size of the per-core object cache. See rte_mempool_create() for
694 * Size of application private are between the rte_mbuf structure
695 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
696 * @param data_room_size
697 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
699 * The socket identifier where the memory should be allocated. The
700 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
703 * The mempool ops name to be used for this mempool instead of
704 * default mempool. The value can be *NULL* to use default mempool.
706 * The pointer to the new allocated mempool, on success. NULL on error
707 * with rte_errno set appropriately. Possible rte_errno values include:
708 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
709 * - E_RTE_SECONDARY - function was called from a secondary process instance
710 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
711 * - ENOSPC - the maximum number of memzones has already been allocated
712 * - EEXIST - a memzone with the same name already exists
713 * - ENOMEM - no appropriate memory area found in which to create memzone
716 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
717 unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
718 int socket_id, const char *ops_name);
720 /** A structure that describes the pinned external buffer segment. */
721 struct rte_pktmbuf_extmem {
722 void *buf_ptr; /**< The virtual address of data buffer. */
723 rte_iova_t buf_iova; /**< The IO address of the data buffer. */
724 size_t buf_len; /**< External buffer length in bytes. */
725 uint16_t elt_size; /**< mbuf element size in bytes. */
729 * Create a mbuf pool with external pinned data buffers.
731 * This function creates and initializes a packet mbuf pool that contains
732 * only mbufs with external buffer. It is a wrapper to rte_mempool functions.
735 * The name of the mbuf pool.
737 * The number of elements in the mbuf pool. The optimum size (in terms
738 * of memory usage) for a mempool is when n is a power of two minus one:
741 * Size of the per-core object cache. See rte_mempool_create() for
744 * Size of application private are between the rte_mbuf structure
745 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
746 * @param data_room_size
747 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
749 * The socket identifier where the memory should be allocated. The
750 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
753 * Pointer to the array of structures describing the external memory
754 * for data buffers. It is caller responsibility to register this memory
755 * with rte_extmem_register() (if needed), map this memory to appropriate
756 * physical device, etc.
758 * Number of elements in the ext_mem array.
760 * The pointer to the new allocated mempool, on success. NULL on error
761 * with rte_errno set appropriately. Possible rte_errno values include:
762 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
763 * - E_RTE_SECONDARY - function was called from a secondary process instance
764 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
765 * - ENOSPC - the maximum number of memzones has already been allocated
766 * - EEXIST - a memzone with the same name already exists
767 * - ENOMEM - no appropriate memory area found in which to create memzone
771 rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n,
772 unsigned int cache_size, uint16_t priv_size,
773 uint16_t data_room_size, int socket_id,
774 const struct rte_pktmbuf_extmem *ext_mem,
775 unsigned int ext_num);
778 * Get the data room size of mbufs stored in a pktmbuf_pool
780 * The data room size is the amount of data that can be stored in a
781 * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
784 * The packet mbuf pool.
786 * The data room size of mbufs stored in this mempool.
788 static inline uint16_t
789 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
791 struct rte_pktmbuf_pool_private *mbp_priv;
793 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
794 return mbp_priv->mbuf_data_room_size;
798 * Get the application private size of mbufs stored in a pktmbuf_pool
800 * The private size of mbuf is a zone located between the rte_mbuf
801 * structure and the data buffer where an application can store data
802 * associated to a packet.
805 * The packet mbuf pool.
807 * The private size of mbufs stored in this mempool.
809 static inline uint16_t
810 rte_pktmbuf_priv_size(struct rte_mempool *mp)
812 struct rte_pktmbuf_pool_private *mbp_priv;
814 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
815 return mbp_priv->mbuf_priv_size;
819 * Reset the data_off field of a packet mbuf to its default value.
821 * The given mbuf must have only one segment, which should be empty.
824 * The packet mbuf's data_off field has to be reset.
826 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
828 m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
829 (uint16_t)m->buf_len);
833 * Reset the fields of a packet mbuf to their default values.
835 * The given mbuf must have only one segment.
838 * The packet mbuf to be reset.
840 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
846 m->vlan_tci_outer = 0;
848 m->port = RTE_MBUF_PORT_INVALID;
850 m->ol_flags &= RTE_MBUF_F_EXTERNAL;
852 rte_pktmbuf_reset_headroom(m);
855 __rte_mbuf_sanity_check(m, 1);
859 * Allocate a new mbuf from a mempool.
861 * This new mbuf contains one segment, which has a length of 0. The pointer
862 * to data is initialized to have some bytes of headroom in the buffer
863 * (if buffer size allows).
866 * The mempool from which the mbuf is allocated.
868 * - The pointer to the new mbuf on success.
869 * - NULL if allocation failed.
871 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
874 if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
875 rte_pktmbuf_reset(m);
880 * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
884 * The mempool from which mbufs are allocated.
886 * Array of pointers to mbufs
891 * - -ENOENT: Not enough entries in the mempool; no mbufs are retrieved.
893 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
894 struct rte_mbuf **mbufs, unsigned count)
899 rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
903 /* To understand duff's device on loop unwinding optimization, see
904 * https://en.wikipedia.org/wiki/Duff's_device.
905 * Here while() loop is used rather than do() while{} to avoid extra
906 * check if count is zero.
910 while (idx != count) {
911 __rte_mbuf_raw_sanity_check(mbufs[idx]);
912 rte_pktmbuf_reset(mbufs[idx]);
916 __rte_mbuf_raw_sanity_check(mbufs[idx]);
917 rte_pktmbuf_reset(mbufs[idx]);
921 __rte_mbuf_raw_sanity_check(mbufs[idx]);
922 rte_pktmbuf_reset(mbufs[idx]);
926 __rte_mbuf_raw_sanity_check(mbufs[idx]);
927 rte_pktmbuf_reset(mbufs[idx]);
936 * Initialize shared data at the end of an external buffer before attaching
937 * to a mbuf by ``rte_pktmbuf_attach_extbuf()``. This is not a mandatory
938 * initialization but a helper function to simply spare a few bytes at the
939 * end of the buffer for shared data. If shared data is allocated
940 * separately, this should not be called but application has to properly
941 * initialize the shared data according to its need.
943 * Free callback and its argument is saved and the refcnt is set to 1.
946 * The value of buf_len will be reduced to RTE_PTR_DIFF(shinfo, buf_addr)
947 * after this initialization. This shall be used for
948 * ``rte_pktmbuf_attach_extbuf()``
951 * The pointer to the external buffer.
952 * @param [in,out] buf_len
953 * The pointer to length of the external buffer. Input value must be
954 * larger than the size of ``struct rte_mbuf_ext_shared_info`` and
955 * padding for alignment. If not enough, this function will return NULL.
956 * Adjusted buffer length will be returned through this pointer.
958 * Free callback function to call when the external buffer needs to be
961 * Argument for the free callback function.
964 * A pointer to the initialized shared data on success, return NULL
967 static inline struct rte_mbuf_ext_shared_info *
968 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
969 rte_mbuf_extbuf_free_callback_t free_cb, void *fcb_opaque)
971 struct rte_mbuf_ext_shared_info *shinfo;
972 void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
975 addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
977 if (addr <= buf_addr)
980 shinfo = (struct rte_mbuf_ext_shared_info *)addr;
981 shinfo->free_cb = free_cb;
982 shinfo->fcb_opaque = fcb_opaque;
983 rte_mbuf_ext_refcnt_set(shinfo, 1);
985 *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
990 * Attach an external buffer to a mbuf.
992 * User-managed anonymous buffer can be attached to an mbuf. When attaching
993 * it, corresponding free callback function and its argument should be
994 * provided via shinfo. This callback function will be called once all the
995 * mbufs are detached from the buffer (refcnt becomes zero).
997 * The headroom length of the attaching mbuf will be set to zero and this
998 * can be properly adjusted after attachment. For example, ``rte_pktmbuf_adj()``
999 * or ``rte_pktmbuf_reset_headroom()`` might be used.
1001 * Similarly, the packet length is initialized to 0. If the buffer contains
1002 * data, the user has to adjust ``data_len`` and the ``pkt_len`` field of
1003 * the mbuf accordingly.
1005 * More mbufs can be attached to the same external buffer by
1006 * ``rte_pktmbuf_attach()`` once the external buffer has been attached by
1009 * Detachment can be done by either ``rte_pktmbuf_detach_extbuf()`` or
1010 * ``rte_pktmbuf_detach()``.
1012 * Memory for shared data must be provided and user must initialize all of
1013 * the content properly, especially free callback and refcnt. The pointer
1014 * of shared data will be stored in m->shinfo.
1015 * ``rte_pktmbuf_ext_shinfo_init_helper`` can help to simply spare a few
1016 * bytes at the end of buffer for the shared data, store free callback and
1017 * its argument and set the refcnt to 1. The following is an example:
1019 * struct rte_mbuf_ext_shared_info *shinfo =
1020 * rte_pktmbuf_ext_shinfo_init_helper(buf_addr, &buf_len,
1021 * free_cb, fcb_arg);
1022 * rte_pktmbuf_attach_extbuf(m, buf_addr, buf_iova, buf_len, shinfo);
1023 * rte_pktmbuf_reset_headroom(m);
1024 * rte_pktmbuf_adj(m, data_len);
1026 * Attaching an external buffer is quite similar to mbuf indirection in
1027 * replacing buffer addresses and length of a mbuf, but a few differences:
1028 * - When an indirect mbuf is attached, refcnt of the direct mbuf would be
1029 * 2 as long as the direct mbuf itself isn't freed after the attachment.
1030 * In such cases, the buffer area of a direct mbuf must be read-only. But
1031 * external buffer has its own refcnt and it starts from 1. Unless
1032 * multiple mbufs are attached to a mbuf having an external buffer, the
1033 * external buffer is writable.
1034 * - There's no need to allocate buffer from a mempool. Any buffer can be
1035 * attached with appropriate free callback and its IO address.
1036 * - Smaller metadata is required to maintain shared data such as refcnt.
1039 * The pointer to the mbuf.
1041 * The pointer to the external buffer.
1043 * IO address of the external buffer.
1045 * The size of the external buffer.
1047 * User-provided memory for shared data of the external buffer.
1050 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
1051 rte_iova_t buf_iova, uint16_t buf_len,
1052 struct rte_mbuf_ext_shared_info *shinfo)
1054 /* mbuf should not be read-only */
1055 RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
1056 RTE_ASSERT(shinfo->free_cb != NULL);
1058 m->buf_addr = buf_addr;
1059 m->buf_iova = buf_iova;
1060 m->buf_len = buf_len;
1065 m->ol_flags |= RTE_MBUF_F_EXTERNAL;
1070 * Detach the external buffer attached to a mbuf, same as
1071 * ``rte_pktmbuf_detach()``
1074 * The mbuf having external buffer.
1076 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1079 * Copy dynamic fields from msrc to mdst.
1082 * The destination mbuf.
1087 rte_mbuf_dynfield_copy(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1089 memcpy(&mdst->dynfield1, msrc->dynfield1, sizeof(mdst->dynfield1));
1094 __rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1096 mdst->port = msrc->port;
1097 mdst->vlan_tci = msrc->vlan_tci;
1098 mdst->vlan_tci_outer = msrc->vlan_tci_outer;
1099 mdst->tx_offload = msrc->tx_offload;
1100 mdst->hash = msrc->hash;
1101 mdst->packet_type = msrc->packet_type;
1102 rte_mbuf_dynfield_copy(mdst, msrc);
1106 * Attach packet mbuf to another packet mbuf.
1108 * If the mbuf we are attaching to isn't a direct buffer and is attached to
1109 * an external buffer, the mbuf being attached will be attached to the
1110 * external buffer instead of mbuf indirection.
1112 * Otherwise, the mbuf will be indirectly attached. After attachment we
1113 * refer the mbuf we attached as 'indirect', while mbuf we attached to as
1114 * 'direct'. The direct mbuf's reference counter is incremented.
1116 * Right now, not supported:
1117 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
1118 * - mbuf we trying to attach (mi) is used by someone else
1119 * e.g. it's reference counter is greater then 1.
1122 * The indirect packet mbuf.
1124 * The packet mbuf we're attaching to.
1126 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1128 RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1129 rte_mbuf_refcnt_read(mi) == 1);
1131 if (RTE_MBUF_HAS_EXTBUF(m)) {
1132 rte_mbuf_ext_refcnt_update(m->shinfo, 1);
1133 mi->ol_flags = m->ol_flags;
1134 mi->shinfo = m->shinfo;
1136 /* if m is not direct, get the mbuf that embeds the data */
1137 rte_mbuf_refcnt_update(rte_mbuf_from_indirect(m), 1);
1138 mi->priv_size = m->priv_size;
1139 mi->ol_flags = m->ol_flags | RTE_MBUF_F_INDIRECT;
1142 __rte_pktmbuf_copy_hdr(mi, m);
1144 mi->data_off = m->data_off;
1145 mi->data_len = m->data_len;
1146 mi->buf_iova = m->buf_iova;
1147 mi->buf_addr = m->buf_addr;
1148 mi->buf_len = m->buf_len;
1151 mi->pkt_len = mi->data_len;
1154 __rte_mbuf_sanity_check(mi, 1);
1155 __rte_mbuf_sanity_check(m, 0);
1159 * @internal used by rte_pktmbuf_detach().
1161 * Decrement the reference counter of the external buffer. When the
1162 * reference counter becomes 0, the buffer is freed by pre-registered
1166 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1168 RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1169 RTE_ASSERT(m->shinfo != NULL);
1171 if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1172 m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1176 * @internal used by rte_pktmbuf_detach().
1178 * Decrement the direct mbuf's reference counter. When the reference
1179 * counter becomes 0, the direct mbuf is freed.
1182 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1184 struct rte_mbuf *md;
1186 RTE_ASSERT(RTE_MBUF_CLONED(m));
1188 md = rte_mbuf_from_indirect(m);
1190 if (rte_mbuf_refcnt_update(md, -1) == 0) {
1193 rte_mbuf_refcnt_set(md, 1);
1194 rte_mbuf_raw_free(md);
1199 * Detach a packet mbuf from external buffer or direct buffer.
1201 * - decrement refcnt and free the external/direct buffer if refcnt
1203 * - restore original mbuf address and length values.
1204 * - reset pktmbuf data and data_len to their default values.
1206 * All other fields of the given packet mbuf will be left intact.
1208 * If the packet mbuf was allocated from the pool with pinned
1209 * external buffers the rte_pktmbuf_detach does nothing with the
1210 * mbuf of this kind, because the pinned buffers are not supposed
1214 * The indirect attached packet mbuf.
1216 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1218 struct rte_mempool *mp = m->pool;
1219 uint32_t mbuf_size, buf_len;
1222 if (RTE_MBUF_HAS_EXTBUF(m)) {
1224 * The mbuf has the external attached buffer,
1225 * we should check the type of the memory pool where
1226 * the mbuf was allocated from to detect the pinned
1229 uint32_t flags = rte_pktmbuf_priv_flags(mp);
1231 if (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) {
1233 * The pinned external buffer should not be
1234 * detached from its backing mbuf, just exit.
1238 __rte_pktmbuf_free_extbuf(m);
1240 __rte_pktmbuf_free_direct(m);
1242 priv_size = rte_pktmbuf_priv_size(mp);
1243 mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1244 buf_len = rte_pktmbuf_data_room_size(mp);
1246 m->priv_size = priv_size;
1247 m->buf_addr = (char *)m + mbuf_size;
1248 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1249 m->buf_len = (uint16_t)buf_len;
1250 rte_pktmbuf_reset_headroom(m);
1256 * @internal Handle the packet mbufs with attached pinned external buffer
1257 * on the mbuf freeing:
1259 * - return zero if reference counter in shinfo is one. It means there is
1260 * no more reference to this pinned buffer and mbuf can be returned to
1263 * - otherwise (if reference counter is not one), decrement reference
1264 * counter and return non-zero value to prevent freeing the backing mbuf.
1266 * Returns non zero if mbuf should not be freed.
1268 static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)
1270 struct rte_mbuf_ext_shared_info *shinfo;
1272 /* Clear flags, mbuf is being freed. */
1273 m->ol_flags = RTE_MBUF_F_EXTERNAL;
1276 /* Optimize for performance - do not dec/reinit */
1277 if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1))
1281 * Direct usage of add primitive to avoid
1282 * duplication of comparing with one.
1284 if (likely(__atomic_add_fetch(&shinfo->refcnt, (uint16_t)-1,
1288 /* Reinitialize counter before mbuf freeing. */
1289 rte_mbuf_ext_refcnt_set(shinfo, 1);
1294 * Decrease reference counter and unlink a mbuf segment
1296 * This function does the same than a free, except that it does not
1297 * return the segment to its pool.
1298 * It decreases the reference counter, and if it reaches 0, it is
1299 * detached from its parent for an indirect mbuf.
1302 * The mbuf to be unlinked
1304 * - (m) if it is the last reference. It can be recycled or freed.
1305 * - (NULL) if the mbuf still has remaining references on it.
1307 static __rte_always_inline struct rte_mbuf *
1308 rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1310 __rte_mbuf_sanity_check(m, 0);
1312 if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1314 if (!RTE_MBUF_DIRECT(m)) {
1315 rte_pktmbuf_detach(m);
1316 if (RTE_MBUF_HAS_EXTBUF(m) &&
1317 RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
1318 __rte_pktmbuf_pinned_extbuf_decref(m))
1322 if (m->next != NULL)
1324 if (m->nb_segs != 1)
1329 } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1331 if (!RTE_MBUF_DIRECT(m)) {
1332 rte_pktmbuf_detach(m);
1333 if (RTE_MBUF_HAS_EXTBUF(m) &&
1334 RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
1335 __rte_pktmbuf_pinned_extbuf_decref(m))
1339 if (m->next != NULL)
1341 if (m->nb_segs != 1)
1343 rte_mbuf_refcnt_set(m, 1);
1351 * Free a segment of a packet mbuf into its original mempool.
1353 * Free an mbuf, without parsing other segments in case of chained
1357 * The packet mbuf segment to be freed.
1359 static __rte_always_inline void
1360 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1362 m = rte_pktmbuf_prefree_seg(m);
1363 if (likely(m != NULL))
1364 rte_mbuf_raw_free(m);
1368 * Free a packet mbuf back into its original mempool.
1370 * Free an mbuf, and all its segments in case of chained buffers. Each
1371 * segment is added back into its original mempool.
1374 * The packet mbuf to be freed. If NULL, the function does nothing.
1376 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1378 struct rte_mbuf *m_next;
1381 __rte_mbuf_sanity_check(m, 1);
1385 rte_pktmbuf_free_seg(m);
1391 * Free a bulk of packet mbufs back into their original mempools.
1393 * Free a bulk of mbufs, and all their segments in case of chained buffers.
1394 * Each segment is added back into its original mempool.
1397 * Array of pointers to packet mbufs.
1398 * The array may contain NULL pointers.
1402 void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count);
1405 * Create a "clone" of the given packet mbuf.
1407 * Walks through all segments of the given packet mbuf, and for each of them:
1408 * - Creates a new packet mbuf from the given pool.
1409 * - Attaches newly created mbuf to the segment.
1410 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1411 * from the original packet mbuf.
1414 * The packet mbuf to be cloned.
1416 * The mempool from which the "clone" mbufs are allocated.
1418 * - The pointer to the new "clone" mbuf on success.
1419 * - NULL if allocation fails.
1422 rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
1425 * Create a full copy of a given packet mbuf.
1427 * Copies all the data from a given packet mbuf to a newly allocated
1428 * set of mbufs. The private data are is not copied.
1431 * The packet mbuf to be copied.
1433 * The mempool from which the "clone" mbufs are allocated.
1435 * The number of bytes to skip before copying.
1436 * If the mbuf does not have that many bytes, it is an error
1437 * and NULL is returned.
1439 * The upper limit on bytes to copy. Passing UINT32_MAX
1440 * means all data (after offset).
1442 * - The pointer to the new "clone" mbuf on success.
1443 * - NULL if allocation fails.
1446 rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
1447 uint32_t offset, uint32_t length);
1450 * Adds given value to the refcnt of all packet mbuf segments.
1452 * Walks through all segments of given packet mbuf and for each of them
1453 * invokes rte_mbuf_refcnt_update().
1456 * The packet mbuf whose refcnt to be updated.
1458 * The value to add to the mbuf's segments refcnt.
1460 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1462 __rte_mbuf_sanity_check(m, 1);
1465 rte_mbuf_refcnt_update(m, v);
1466 } while ((m = m->next) != NULL);
1470 * Get the headroom in a packet mbuf.
1475 * The length of the headroom.
1477 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1479 __rte_mbuf_sanity_check(m, 0);
1484 * Get the tailroom of a packet mbuf.
1489 * The length of the tailroom.
1491 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1493 __rte_mbuf_sanity_check(m, 0);
1494 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1499 * Get the last segment of the packet.
1504 * The last segment of the given mbuf.
1506 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1508 __rte_mbuf_sanity_check(m, 1);
1509 while (m->next != NULL)
1515 * A macro that returns the length of the packet.
1517 * The value can be read or assigned.
1522 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1525 * A macro that returns the length of the segment.
1527 * The value can be read or assigned.
1532 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1535 * Prepend len bytes to an mbuf data area.
1537 * Returns a pointer to the new
1538 * data start address. If there is not enough headroom in the first
1539 * segment, the function will return NULL, without modifying the mbuf.
1544 * The amount of data to prepend (in bytes).
1546 * A pointer to the start of the newly prepended data, or
1547 * NULL if there is not enough headroom space in the first segment
1549 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1552 __rte_mbuf_sanity_check(m, 1);
1554 if (unlikely(len > rte_pktmbuf_headroom(m)))
1557 /* NB: elaborating the subtraction like this instead of using
1558 * -= allows us to ensure the result type is uint16_t
1559 * avoiding compiler warnings on gcc 8.1 at least */
1560 m->data_off = (uint16_t)(m->data_off - len);
1561 m->data_len = (uint16_t)(m->data_len + len);
1562 m->pkt_len = (m->pkt_len + len);
1564 return (char *)m->buf_addr + m->data_off;
1568 * Append len bytes to an mbuf.
1570 * Append len bytes to an mbuf and return a pointer to the start address
1571 * of the added data. If there is not enough tailroom in the last
1572 * segment, the function will return NULL, without modifying the mbuf.
1577 * The amount of data to append (in bytes).
1579 * A pointer to the start of the newly appended data, or
1580 * NULL if there is not enough tailroom space in the last segment
1582 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1585 struct rte_mbuf *m_last;
1587 __rte_mbuf_sanity_check(m, 1);
1589 m_last = rte_pktmbuf_lastseg(m);
1590 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1593 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1594 m_last->data_len = (uint16_t)(m_last->data_len + len);
1595 m->pkt_len = (m->pkt_len + len);
1596 return (char*) tail;
1600 * Remove len bytes at the beginning of an mbuf.
1602 * Returns a pointer to the start address of the new data area. If the
1603 * length is greater than the length of the first segment, then the
1604 * function will fail and return NULL, without modifying the mbuf.
1609 * The amount of data to remove (in bytes).
1611 * A pointer to the new start of the data.
1613 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1615 __rte_mbuf_sanity_check(m, 1);
1617 if (unlikely(len > m->data_len))
1620 /* NB: elaborating the addition like this instead of using
1621 * += allows us to ensure the result type is uint16_t
1622 * avoiding compiler warnings on gcc 8.1 at least */
1623 m->data_len = (uint16_t)(m->data_len - len);
1624 m->data_off = (uint16_t)(m->data_off + len);
1625 m->pkt_len = (m->pkt_len - len);
1626 return (char *)m->buf_addr + m->data_off;
1630 * Remove len bytes of data at the end of the mbuf.
1632 * If the length is greater than the length of the last segment, the
1633 * function will fail and return -1 without modifying the mbuf.
1638 * The amount of data to remove (in bytes).
1643 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1645 struct rte_mbuf *m_last;
1647 __rte_mbuf_sanity_check(m, 1);
1649 m_last = rte_pktmbuf_lastseg(m);
1650 if (unlikely(len > m_last->data_len))
1653 m_last->data_len = (uint16_t)(m_last->data_len - len);
1654 m->pkt_len = (m->pkt_len - len);
1659 * Test if mbuf data is contiguous.
1664 * - 1, if all data is contiguous (one segment).
1665 * - 0, if there is several segments.
1667 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1669 __rte_mbuf_sanity_check(m, 1);
1670 return m->nb_segs == 1;
1674 * @internal used by rte_pktmbuf_read().
1676 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1677 uint32_t len, void *buf);
1680 * Read len data bytes in a mbuf at specified offset.
1682 * If the data is contiguous, return the pointer in the mbuf data, else
1683 * copy the data in the buffer provided by the user and return its
1687 * The pointer to the mbuf.
1689 * The offset of the data in the mbuf.
1691 * The amount of bytes to read.
1693 * The buffer where data is copied if it is not contiguous in mbuf
1694 * data. Its length should be at least equal to the len parameter.
1696 * The pointer to the data, either in the mbuf if it is contiguous,
1697 * or in the user buffer. If mbuf is too small, NULL is returned.
1699 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1700 uint32_t off, uint32_t len, void *buf)
1702 if (likely(off + len <= rte_pktmbuf_data_len(m)))
1703 return rte_pktmbuf_mtod_offset(m, char *, off);
1705 return __rte_pktmbuf_read(m, off, len, buf);
1709 * Chain an mbuf to another, thereby creating a segmented packet.
1711 * Note: The implementation will do a linear walk over the segments to find
1712 * the tail entry. For cases when there are many segments, it's better to
1713 * chain the entries manually.
1716 * The head of the mbuf chain (the first packet)
1718 * The mbuf to put last in the chain
1722 * - -EOVERFLOW, if the chain segment limit exceeded
1724 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1726 struct rte_mbuf *cur_tail;
1728 /* Check for number-of-segments-overflow */
1729 if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1732 /* Chain 'tail' onto the old tail */
1733 cur_tail = rte_pktmbuf_lastseg(head);
1734 cur_tail->next = tail;
1736 /* accumulate number of segments and total length.
1737 * NB: elaborating the addition like this instead of using
1738 * -= allows us to ensure the result type is uint16_t
1739 * avoiding compiler warnings on gcc 8.1 at least */
1740 head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
1741 head->pkt_len += tail->pkt_len;
1743 /* pkt_len is only set in the head */
1744 tail->pkt_len = tail->data_len;
1750 * For given input values generate raw tx_offload value.
1751 * Note that it is caller responsibility to make sure that input parameters
1752 * don't exceed maximum bit-field values.
1762 * outer_l3_len value.
1764 * outer_l2_len value.
1768 * raw tx_offload value.
1770 static __rte_always_inline uint64_t
1771 rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso,
1772 uint64_t ol3, uint64_t ol2, uint64_t unused)
1774 return il2 << RTE_MBUF_L2_LEN_OFS |
1775 il3 << RTE_MBUF_L3_LEN_OFS |
1776 il4 << RTE_MBUF_L4_LEN_OFS |
1777 tso << RTE_MBUF_TSO_SEGSZ_OFS |
1778 ol3 << RTE_MBUF_OUTL3_LEN_OFS |
1779 ol2 << RTE_MBUF_OUTL2_LEN_OFS |
1780 unused << RTE_MBUF_TXOFLD_UNUSED_OFS;
1784 * Validate general requirements for Tx offload in mbuf.
1786 * This function checks correctness and completeness of Tx offload settings.
1789 * The packet mbuf to be validated.
1791 * 0 if packet is valid
1794 rte_validate_tx_offload(const struct rte_mbuf *m)
1796 uint64_t ol_flags = m->ol_flags;
1798 /* Does packet set any of available offloads? */
1799 if (!(ol_flags & RTE_MBUF_F_TX_OFFLOAD_MASK))
1802 /* IP checksum can be counted only for IPv4 packet */
1803 if ((ol_flags & RTE_MBUF_F_TX_IP_CKSUM) && (ol_flags & RTE_MBUF_F_TX_IPV6))
1806 /* IP type not set when required */
1807 if (ol_flags & (RTE_MBUF_F_TX_L4_MASK | RTE_MBUF_F_TX_TCP_SEG))
1808 if (!(ol_flags & (RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IPV6)))
1811 /* Check requirements for TSO packet */
1812 if (ol_flags & RTE_MBUF_F_TX_TCP_SEG)
1813 if ((m->tso_segsz == 0) ||
1814 ((ol_flags & RTE_MBUF_F_TX_IPV4) &&
1815 !(ol_flags & RTE_MBUF_F_TX_IP_CKSUM)))
1818 /* RTE_MBUF_F_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1819 if ((ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM) &&
1820 !(ol_flags & RTE_MBUF_F_TX_OUTER_IPV4))
1827 * @internal used by rte_pktmbuf_linearize().
1829 int __rte_pktmbuf_linearize(struct rte_mbuf *mbuf);
1832 * Linearize data in mbuf.
1834 * This function moves the mbuf data in the first segment if there is enough
1835 * tailroom. The subsequent segments are unchained and freed.
1844 rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
1846 if (rte_pktmbuf_is_contiguous(mbuf))
1848 return __rte_pktmbuf_linearize(mbuf);
1852 * Dump an mbuf structure to a file.
1854 * Dump all fields for the given packet mbuf and all its associated
1855 * segments (in the case of a chained buffer).
1858 * A pointer to a file for output
1862 * If dump_len != 0, also dump the "dump_len" first data bytes of
1865 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1868 * Get the value of mbuf sched queue_id field.
1870 static inline uint32_t
1871 rte_mbuf_sched_queue_get(const struct rte_mbuf *m)
1873 return m->hash.sched.queue_id;
1877 * Get the value of mbuf sched traffic_class field.
1879 static inline uint8_t
1880 rte_mbuf_sched_traffic_class_get(const struct rte_mbuf *m)
1882 return m->hash.sched.traffic_class;
1886 * Get the value of mbuf sched color field.
1888 static inline uint8_t
1889 rte_mbuf_sched_color_get(const struct rte_mbuf *m)
1891 return m->hash.sched.color;
1895 * Get the values of mbuf sched queue_id, traffic_class and color.
1900 * Returns the queue id
1901 * @param traffic_class
1902 * Returns the traffic class id
1904 * Returns the colour id
1907 rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id,
1908 uint8_t *traffic_class,
1911 struct rte_mbuf_sched sched = m->hash.sched;
1913 *queue_id = sched.queue_id;
1914 *traffic_class = sched.traffic_class;
1915 *color = sched.color;
1919 * Set the mbuf sched queue_id to the defined value.
1922 rte_mbuf_sched_queue_set(struct rte_mbuf *m, uint32_t queue_id)
1924 m->hash.sched.queue_id = queue_id;
1928 * Set the mbuf sched traffic_class id to the defined value.
1931 rte_mbuf_sched_traffic_class_set(struct rte_mbuf *m, uint8_t traffic_class)
1933 m->hash.sched.traffic_class = traffic_class;
1937 * Set the mbuf sched color id to the defined value.
1940 rte_mbuf_sched_color_set(struct rte_mbuf *m, uint8_t color)
1942 m->hash.sched.color = color;
1946 * Set the mbuf sched queue_id, traffic_class and color.
1951 * Queue id value to be set
1952 * @param traffic_class
1953 * Traffic class id value to be set
1955 * Color id to be set
1958 rte_mbuf_sched_set(struct rte_mbuf *m, uint32_t queue_id,
1959 uint8_t traffic_class,
1962 m->hash.sched = (struct rte_mbuf_sched){
1963 .queue_id = queue_id,
1964 .traffic_class = traffic_class,
1974 #endif /* _RTE_MBUF_H_ */