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_memory.h>
40 #include <rte_prefetch.h>
41 #include <rte_branch_prediction.h>
42 #include <rte_byteorder.h>
43 #include <rte_mbuf_ptype.h>
44 #include <rte_mbuf_core.h>
51 * Get the name of a RX offload flag
54 * The mask describing the flag.
56 * The name of this flag, or NULL if it's not a valid RX flag.
58 const char *rte_get_rx_ol_flag_name(uint64_t mask);
61 * Dump the list of RX offload flags in a buffer
64 * The mask describing the RX flags.
68 * The length of the buffer.
70 * 0 on success, (-1) on error.
72 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
75 * Get the name of a TX offload flag
78 * The mask describing the flag. Usually only one bit must be set.
79 * Several bits can be given if they belong to the same mask.
82 * The name of this flag, or NULL if it's not a valid TX flag.
84 const char *rte_get_tx_ol_flag_name(uint64_t mask);
87 * Dump the list of TX offload flags in a buffer
90 * The mask describing the TX flags.
94 * The length of the buffer.
96 * 0 on success, (-1) on error.
98 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
101 * Prefetch the first part of the mbuf
103 * The first 64 bytes of the mbuf corresponds to fields that are used early
104 * in the receive path. If the cache line of the architecture is higher than
105 * 64B, the second part will also be prefetched.
108 * The pointer to the mbuf.
111 rte_mbuf_prefetch_part1(struct rte_mbuf *m)
113 rte_prefetch0(&m->cacheline0);
117 * Prefetch the second part of the mbuf
119 * The next 64 bytes of the mbuf corresponds to fields that are used in the
120 * transmit path. If the cache line of the architecture is higher than 64B,
121 * this function does nothing as it is expected that the full mbuf is
125 * The pointer to the mbuf.
128 rte_mbuf_prefetch_part2(struct rte_mbuf *m)
130 #if RTE_CACHE_LINE_SIZE == 64
131 rte_prefetch0(&m->cacheline1);
138 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
141 * Return the IO address of the beginning of the mbuf data
144 * The pointer to the mbuf.
146 * The IO address of the beginning of the mbuf data
148 static inline rte_iova_t
149 rte_mbuf_data_iova(const struct rte_mbuf *mb)
151 return mb->buf_iova + mb->data_off;
155 * Return the default IO address of the beginning of the mbuf data
157 * This function is used by drivers in their receive function, as it
158 * returns the location where data should be written by the NIC, taking
159 * the default headroom in account.
162 * The pointer to the mbuf.
164 * The IO address of the beginning of the mbuf data
166 static inline rte_iova_t
167 rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
169 return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
173 * Return the mbuf owning the data buffer address of an indirect mbuf.
176 * The pointer to the indirect mbuf.
178 * The address of the direct mbuf corresponding to buffer_addr.
180 static inline struct rte_mbuf *
181 rte_mbuf_from_indirect(struct rte_mbuf *mi)
183 return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
187 * Return address of buffer embedded in the given mbuf.
189 * The return value shall be same as mb->buf_addr if the mbuf is already
190 * initialized and direct. However, this API is useful if mempool of the
191 * mbuf is already known because it doesn't need to access mbuf contents in
192 * order to get the mempool pointer.
195 * The pointer to the mbuf.
197 * The pointer to the mempool of the mbuf.
199 * The pointer of the mbuf buffer.
202 rte_mbuf_buf_addr(struct rte_mbuf *mb, struct rte_mempool *mp)
204 return (char *)mb + sizeof(*mb) + rte_pktmbuf_priv_size(mp);
208 * Return the default address of the beginning of the mbuf data.
211 * The pointer to the mbuf.
213 * The pointer of the beginning of the mbuf data.
216 rte_mbuf_data_addr_default(struct rte_mbuf *mb)
218 return rte_mbuf_buf_addr(mb, mb->pool) + RTE_PKTMBUF_HEADROOM;
222 * Return address of buffer embedded in the given mbuf.
224 * @note: Accessing mempool pointer of a mbuf is expensive because the
225 * pointer is stored in the 2nd cache line of mbuf. If mempool is known, it
226 * is better not to reference the mempool pointer in mbuf but calling
227 * rte_mbuf_buf_addr() would be more efficient.
230 * The pointer to the mbuf.
232 * The address of the data buffer owned by the mbuf.
235 rte_mbuf_to_baddr(struct rte_mbuf *md)
237 return rte_mbuf_buf_addr(md, md->pool);
241 * Return the starting address of the private data area embedded in
244 * Note that no check is made to ensure that a private data area
245 * actually exists in the supplied mbuf.
248 * The pointer to the mbuf.
250 * The starting address of the private data area of the given mbuf.
253 rte_mbuf_to_priv(struct rte_mbuf *m)
255 return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
259 * Private data in case of pktmbuf pool.
261 * A structure that contains some pktmbuf_pool-specific data that are
262 * appended after the mempool structure (in private data).
264 struct rte_pktmbuf_pool_private {
265 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
266 uint16_t mbuf_priv_size; /**< Size of private area in each mbuf. */
267 uint32_t flags; /**< reserved for future use. */
271 * Return the flags from private data in an mempool structure.
274 * A pointer to the mempool structure.
276 * The flags from the private data structure.
278 static inline uint32_t
279 rte_pktmbuf_priv_flags(struct rte_mempool *mp)
281 struct rte_pktmbuf_pool_private *mbp_priv;
283 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
284 return mbp_priv->flags;
288 * When set, pktmbuf mempool will hold only mbufs with pinned external
289 * buffer. The external buffer will be attached to the mbuf at the
290 * memory pool creation and will never be detached by the mbuf free calls.
291 * mbuf should not contain any room for data after the mbuf structure.
293 #define RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF (1 << 0)
296 * Returns non zero if given mbuf has a pinned external buffer, or zero
297 * otherwise. The pinned external buffer is allocated at pool creation
298 * time and should not be freed on mbuf freeing.
300 * External buffer is a user-provided anonymous buffer.
302 #define RTE_MBUF_HAS_PINNED_EXTBUF(mb) \
303 (rte_pktmbuf_priv_flags(mb->pool) & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
305 #ifdef RTE_LIBRTE_MBUF_DEBUG
307 /** check mbuf type in debug mode */
308 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
310 #else /* RTE_LIBRTE_MBUF_DEBUG */
312 /** check mbuf type in debug mode */
313 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
315 #endif /* RTE_LIBRTE_MBUF_DEBUG */
317 #ifdef RTE_MBUF_REFCNT_ATOMIC
320 * Reads the value of an mbuf's refcnt.
324 * Reference count number.
326 static inline uint16_t
327 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
329 return __atomic_load_n(&m->refcnt, __ATOMIC_RELAXED);
333 * Sets an mbuf's refcnt to a defined value.
340 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
342 __atomic_store_n(&m->refcnt, new_value, __ATOMIC_RELAXED);
346 static inline uint16_t
347 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
349 return __atomic_add_fetch(&m->refcnt, (uint16_t)value,
354 * Adds given value to an mbuf's refcnt and returns its new value.
358 * Value to add/subtract
362 static inline uint16_t
363 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
366 * The atomic_add is an expensive operation, so we don't want to
367 * call it in the case where we know we are the unique holder of
368 * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
369 * operation has to be used because concurrent accesses on the
370 * reference counter can occur.
372 if (likely(rte_mbuf_refcnt_read(m) == 1)) {
374 rte_mbuf_refcnt_set(m, (uint16_t)value);
375 return (uint16_t)value;
378 return __rte_mbuf_refcnt_update(m, value);
381 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
384 static inline uint16_t
385 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
387 m->refcnt = (uint16_t)(m->refcnt + value);
392 * Adds given value to an mbuf's refcnt and returns its new value.
394 static inline uint16_t
395 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
397 return __rte_mbuf_refcnt_update(m, value);
401 * Reads the value of an mbuf's refcnt.
403 static inline uint16_t
404 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
410 * Sets an mbuf's refcnt to the defined value.
413 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
415 m->refcnt = new_value;
418 #endif /* RTE_MBUF_REFCNT_ATOMIC */
421 * Reads the refcnt of an external buffer.
424 * Shared data of the external buffer.
426 * Reference count number.
428 static inline uint16_t
429 rte_mbuf_ext_refcnt_read(const struct rte_mbuf_ext_shared_info *shinfo)
431 return __atomic_load_n(&shinfo->refcnt, __ATOMIC_RELAXED);
435 * Set refcnt of an external buffer.
438 * Shared data of the external buffer.
443 rte_mbuf_ext_refcnt_set(struct rte_mbuf_ext_shared_info *shinfo,
446 __atomic_store_n(&shinfo->refcnt, new_value, __ATOMIC_RELAXED);
450 * Add given value to refcnt of an external buffer and return its new
454 * Shared data of the external buffer.
456 * Value to add/subtract
460 static inline uint16_t
461 rte_mbuf_ext_refcnt_update(struct rte_mbuf_ext_shared_info *shinfo,
464 if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1)) {
466 rte_mbuf_ext_refcnt_set(shinfo, (uint16_t)value);
467 return (uint16_t)value;
470 return __atomic_add_fetch(&shinfo->refcnt, (uint16_t)value,
475 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
482 * Sanity checks on an mbuf.
484 * Check the consistency of the given mbuf. The function will cause a
485 * panic if corruption is detected.
488 * The mbuf to be checked.
490 * True if the mbuf is a packet header, false if it is a sub-segment
491 * of a packet (in this case, some fields like nb_segs are not checked)
494 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
497 * Sanity checks on a mbuf.
499 * Almost like rte_mbuf_sanity_check(), but this function gives the reason
500 * if corruption is detected rather than panic.
503 * The mbuf to be checked.
505 * True if the mbuf is a packet header, false if it is a sub-segment
506 * of a packet (in this case, some fields like nb_segs are not checked)
508 * A reference to a string pointer where to store the reason why a mbuf is
509 * considered invalid.
511 * - 0 if no issue has been found, reason is left untouched.
512 * - -1 if a problem is detected, reason then points to a string describing
513 * the reason why the mbuf is deemed invalid.
515 int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
516 const char **reason);
519 * Sanity checks on a reinitialized mbuf in debug mode.
521 * Check the consistency of the given reinitialized mbuf.
522 * The function will cause a panic if corruption is detected.
524 * Check that the mbuf is properly reinitialized (refcnt=1, next=NULL,
525 * nb_segs=1), as done by rte_pktmbuf_prefree_seg().
528 * The mbuf to be checked.
530 static __rte_always_inline void
531 __rte_mbuf_raw_sanity_check(__rte_unused const struct rte_mbuf *m)
533 RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
534 RTE_ASSERT(m->next == NULL);
535 RTE_ASSERT(m->nb_segs == 1);
536 __rte_mbuf_sanity_check(m, 0);
539 /** For backwards compatibility. */
540 #define MBUF_RAW_ALLOC_CHECK(m) __rte_mbuf_raw_sanity_check(m)
543 * Allocate an uninitialized mbuf from mempool *mp*.
545 * This function can be used by PMDs (especially in RX functions) to
546 * allocate an uninitialized mbuf. The driver is responsible of
547 * initializing all the required fields. See rte_pktmbuf_reset().
548 * For standard needs, prefer rte_pktmbuf_alloc().
550 * The caller can expect that the following fields of the mbuf structure
551 * are initialized: buf_addr, buf_iova, buf_len, refcnt=1, nb_segs=1,
552 * next=NULL, pool, priv_size. The other fields must be initialized
556 * The mempool from which mbuf is allocated.
558 * - The pointer to the new mbuf on success.
559 * - NULL if allocation failed.
561 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
565 if (rte_mempool_get(mp, (void **)&m) < 0)
567 __rte_mbuf_raw_sanity_check(m);
572 * Put mbuf back into its original mempool.
574 * The caller must ensure that the mbuf is direct and properly
575 * reinitialized (refcnt=1, next=NULL, nb_segs=1), as done by
576 * rte_pktmbuf_prefree_seg().
578 * This function should be used with care, when optimization is
579 * required. For standard needs, prefer rte_pktmbuf_free() or
580 * rte_pktmbuf_free_seg().
583 * The mbuf to be freed.
585 static __rte_always_inline void
586 rte_mbuf_raw_free(struct rte_mbuf *m)
588 RTE_ASSERT(!RTE_MBUF_CLONED(m) &&
589 (!RTE_MBUF_HAS_EXTBUF(m) || RTE_MBUF_HAS_PINNED_EXTBUF(m)));
590 __rte_mbuf_raw_sanity_check(m);
591 rte_mempool_put(m->pool, m);
595 * The packet mbuf constructor.
597 * This function initializes some fields in the mbuf structure that are
598 * not modified by the user once created (origin pool, buffer start
599 * address, and so on). This function is given as a callback function to
600 * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
602 * This function expects that the mempool private area was previously
603 * initialized with rte_pktmbuf_pool_init().
606 * The mempool from which mbufs originate.
608 * A pointer that can be used by the user to retrieve useful information
609 * for mbuf initialization. This pointer is the opaque argument passed to
610 * rte_mempool_obj_iter() or rte_mempool_create().
612 * The mbuf to initialize.
614 * The index of the mbuf in the pool table.
616 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
617 void *m, unsigned i);
620 * A packet mbuf pool constructor.
622 * This function initializes the mempool private data in the case of a
623 * pktmbuf pool. This private data is needed by the driver. The
624 * function must be called on the mempool before it is used, or it
625 * can be given as a callback function to rte_mempool_create() at
626 * pool creation. It can be extended by the user, for example, to
627 * provide another packet size.
629 * The mempool private area size must be at least equal to
630 * sizeof(struct rte_pktmbuf_pool_private).
633 * The mempool from which mbufs originate.
635 * A pointer that can be used by the user to retrieve useful information
636 * for mbuf initialization. This pointer is the opaque argument passed to
637 * rte_mempool_create().
639 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
642 * Create a mbuf pool.
644 * This function creates and initializes a packet mbuf pool. It is
645 * a wrapper to rte_mempool functions.
648 * The name of the mbuf pool.
650 * The number of elements in the mbuf pool. The optimum size (in terms
651 * of memory usage) for a mempool is when n is a power of two minus one:
654 * Size of the per-core object cache. See rte_mempool_create() for
657 * Size of application private are between the rte_mbuf structure
658 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
659 * @param data_room_size
660 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
662 * The socket identifier where the memory should be allocated. The
663 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
666 * The pointer to the new allocated mempool, on success. NULL on error
667 * with rte_errno set appropriately. Possible rte_errno values include:
668 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
669 * - E_RTE_SECONDARY - function was called from a secondary process instance
670 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
671 * - ENOSPC - the maximum number of memzones has already been allocated
672 * - EEXIST - a memzone with the same name already exists
673 * - ENOMEM - no appropriate memory area found in which to create memzone
676 rte_pktmbuf_pool_create(const char *name, unsigned n,
677 unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
681 * Create a mbuf pool with a given mempool ops name
683 * This function creates and initializes a packet mbuf pool. It is
684 * a wrapper to rte_mempool functions.
687 * The name of the mbuf pool.
689 * The number of elements in the mbuf pool. The optimum size (in terms
690 * of memory usage) for a mempool is when n is a power of two minus one:
693 * Size of the per-core object cache. See rte_mempool_create() for
696 * Size of application private are between the rte_mbuf structure
697 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
698 * @param data_room_size
699 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
701 * The socket identifier where the memory should be allocated. The
702 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
705 * The mempool ops name to be used for this mempool instead of
706 * default mempool. The value can be *NULL* to use default mempool.
708 * The pointer to the new allocated mempool, on success. NULL on error
709 * with rte_errno set appropriately. Possible rte_errno values include:
710 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
711 * - E_RTE_SECONDARY - function was called from a secondary process instance
712 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
713 * - ENOSPC - the maximum number of memzones has already been allocated
714 * - EEXIST - a memzone with the same name already exists
715 * - ENOMEM - no appropriate memory area found in which to create memzone
718 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
719 unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
720 int socket_id, const char *ops_name);
722 /** A structure that describes the pinned external buffer segment. */
723 struct rte_pktmbuf_extmem {
724 void *buf_ptr; /**< The virtual address of data buffer. */
725 rte_iova_t buf_iova; /**< The IO address of the data buffer. */
726 size_t buf_len; /**< External buffer length in bytes. */
727 uint16_t elt_size; /**< mbuf element size in bytes. */
731 * Create a mbuf pool with external pinned data buffers.
733 * This function creates and initializes a packet mbuf pool that contains
734 * only mbufs with external buffer. It is a wrapper to rte_mempool functions.
737 * The name of the mbuf pool.
739 * The number of elements in the mbuf pool. The optimum size (in terms
740 * of memory usage) for a mempool is when n is a power of two minus one:
743 * Size of the per-core object cache. See rte_mempool_create() for
746 * Size of application private are between the rte_mbuf structure
747 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
748 * @param data_room_size
749 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
751 * The socket identifier where the memory should be allocated. The
752 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
755 * Pointer to the array of structures describing the external memory
756 * for data buffers. It is caller responsibility to register this memory
757 * with rte_extmem_register() (if needed), map this memory to appropriate
758 * physical device, etc.
760 * Number of elements in the ext_mem array.
762 * The pointer to the new allocated mempool, on success. NULL on error
763 * with rte_errno set appropriately. Possible rte_errno values include:
764 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
765 * - E_RTE_SECONDARY - function was called from a secondary process instance
766 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
767 * - ENOSPC - the maximum number of memzones has already been allocated
768 * - EEXIST - a memzone with the same name already exists
769 * - ENOMEM - no appropriate memory area found in which to create memzone
773 rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n,
774 unsigned int cache_size, uint16_t priv_size,
775 uint16_t data_room_size, int socket_id,
776 const struct rte_pktmbuf_extmem *ext_mem,
777 unsigned int ext_num);
780 * Get the data room size of mbufs stored in a pktmbuf_pool
782 * The data room size is the amount of data that can be stored in a
783 * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
786 * The packet mbuf pool.
788 * The data room size of mbufs stored in this mempool.
790 static inline uint16_t
791 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
793 struct rte_pktmbuf_pool_private *mbp_priv;
795 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
796 return mbp_priv->mbuf_data_room_size;
800 * Get the application private size of mbufs stored in a pktmbuf_pool
802 * The private size of mbuf is a zone located between the rte_mbuf
803 * structure and the data buffer where an application can store data
804 * associated to a packet.
807 * The packet mbuf pool.
809 * The private size of mbufs stored in this mempool.
811 static inline uint16_t
812 rte_pktmbuf_priv_size(struct rte_mempool *mp)
814 struct rte_pktmbuf_pool_private *mbp_priv;
816 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
817 return mbp_priv->mbuf_priv_size;
821 * Reset the data_off field of a packet mbuf to its default value.
823 * The given mbuf must have only one segment, which should be empty.
826 * The packet mbuf's data_off field has to be reset.
828 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
830 m->data_off = (uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM,
831 (uint16_t)m->buf_len);
835 * Reset the fields of a packet mbuf to their default values.
837 * The given mbuf must have only one segment.
840 * The packet mbuf to be reset.
842 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
848 m->vlan_tci_outer = 0;
850 m->port = RTE_MBUF_PORT_INVALID;
852 m->ol_flags &= EXT_ATTACHED_MBUF;
854 rte_pktmbuf_reset_headroom(m);
857 __rte_mbuf_sanity_check(m, 1);
861 * Allocate a new mbuf from a mempool.
863 * This new mbuf contains one segment, which has a length of 0. The pointer
864 * to data is initialized to have some bytes of headroom in the buffer
865 * (if buffer size allows).
868 * The mempool from which the mbuf is allocated.
870 * - The pointer to the new mbuf on success.
871 * - NULL if allocation failed.
873 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
876 if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
877 rte_pktmbuf_reset(m);
882 * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
886 * The mempool from which mbufs are allocated.
888 * Array of pointers to mbufs
893 * - -ENOENT: Not enough entries in the mempool; no mbufs are retrieved.
895 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
896 struct rte_mbuf **mbufs, unsigned count)
901 rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
905 /* To understand duff's device on loop unwinding optimization, see
906 * https://en.wikipedia.org/wiki/Duff's_device.
907 * Here while() loop is used rather than do() while{} to avoid extra
908 * check if count is zero.
912 while (idx != count) {
913 __rte_mbuf_raw_sanity_check(mbufs[idx]);
914 rte_pktmbuf_reset(mbufs[idx]);
918 __rte_mbuf_raw_sanity_check(mbufs[idx]);
919 rte_pktmbuf_reset(mbufs[idx]);
923 __rte_mbuf_raw_sanity_check(mbufs[idx]);
924 rte_pktmbuf_reset(mbufs[idx]);
928 __rte_mbuf_raw_sanity_check(mbufs[idx]);
929 rte_pktmbuf_reset(mbufs[idx]);
938 * Initialize shared data at the end of an external buffer before attaching
939 * to a mbuf by ``rte_pktmbuf_attach_extbuf()``. This is not a mandatory
940 * initialization but a helper function to simply spare a few bytes at the
941 * end of the buffer for shared data. If shared data is allocated
942 * separately, this should not be called but application has to properly
943 * initialize the shared data according to its need.
945 * Free callback and its argument is saved and the refcnt is set to 1.
948 * The value of buf_len will be reduced to RTE_PTR_DIFF(shinfo, buf_addr)
949 * after this initialization. This shall be used for
950 * ``rte_pktmbuf_attach_extbuf()``
953 * The pointer to the external buffer.
954 * @param [in,out] buf_len
955 * The pointer to length of the external buffer. Input value must be
956 * larger than the size of ``struct rte_mbuf_ext_shared_info`` and
957 * padding for alignment. If not enough, this function will return NULL.
958 * Adjusted buffer length will be returned through this pointer.
960 * Free callback function to call when the external buffer needs to be
963 * Argument for the free callback function.
966 * A pointer to the initialized shared data on success, return NULL
969 static inline struct rte_mbuf_ext_shared_info *
970 rte_pktmbuf_ext_shinfo_init_helper(void *buf_addr, uint16_t *buf_len,
971 rte_mbuf_extbuf_free_callback_t free_cb, void *fcb_opaque)
973 struct rte_mbuf_ext_shared_info *shinfo;
974 void *buf_end = RTE_PTR_ADD(buf_addr, *buf_len);
977 addr = RTE_PTR_ALIGN_FLOOR(RTE_PTR_SUB(buf_end, sizeof(*shinfo)),
979 if (addr <= buf_addr)
982 shinfo = (struct rte_mbuf_ext_shared_info *)addr;
983 shinfo->free_cb = free_cb;
984 shinfo->fcb_opaque = fcb_opaque;
985 rte_mbuf_ext_refcnt_set(shinfo, 1);
987 *buf_len = (uint16_t)RTE_PTR_DIFF(shinfo, buf_addr);
992 * Attach an external buffer to a mbuf.
994 * User-managed anonymous buffer can be attached to an mbuf. When attaching
995 * it, corresponding free callback function and its argument should be
996 * provided via shinfo. This callback function will be called once all the
997 * mbufs are detached from the buffer (refcnt becomes zero).
999 * The headroom length of the attaching mbuf will be set to zero and this
1000 * can be properly adjusted after attachment. For example, ``rte_pktmbuf_adj()``
1001 * or ``rte_pktmbuf_reset_headroom()`` might be used.
1003 * Similarly, the packet length is initialized to 0. If the buffer contains
1004 * data, the user has to adjust ``data_len`` and the ``pkt_len`` field of
1005 * the mbuf accordingly.
1007 * More mbufs can be attached to the same external buffer by
1008 * ``rte_pktmbuf_attach()`` once the external buffer has been attached by
1011 * Detachment can be done by either ``rte_pktmbuf_detach_extbuf()`` or
1012 * ``rte_pktmbuf_detach()``.
1014 * Memory for shared data must be provided and user must initialize all of
1015 * the content properly, especially free callback and refcnt. The pointer
1016 * of shared data will be stored in m->shinfo.
1017 * ``rte_pktmbuf_ext_shinfo_init_helper`` can help to simply spare a few
1018 * bytes at the end of buffer for the shared data, store free callback and
1019 * its argument and set the refcnt to 1. The following is an example:
1021 * struct rte_mbuf_ext_shared_info *shinfo =
1022 * rte_pktmbuf_ext_shinfo_init_helper(buf_addr, &buf_len,
1023 * free_cb, fcb_arg);
1024 * rte_pktmbuf_attach_extbuf(m, buf_addr, buf_iova, buf_len, shinfo);
1025 * rte_pktmbuf_reset_headroom(m);
1026 * rte_pktmbuf_adj(m, data_len);
1028 * Attaching an external buffer is quite similar to mbuf indirection in
1029 * replacing buffer addresses and length of a mbuf, but a few differences:
1030 * - When an indirect mbuf is attached, refcnt of the direct mbuf would be
1031 * 2 as long as the direct mbuf itself isn't freed after the attachment.
1032 * In such cases, the buffer area of a direct mbuf must be read-only. But
1033 * external buffer has its own refcnt and it starts from 1. Unless
1034 * multiple mbufs are attached to a mbuf having an external buffer, the
1035 * external buffer is writable.
1036 * - There's no need to allocate buffer from a mempool. Any buffer can be
1037 * attached with appropriate free callback and its IO address.
1038 * - Smaller metadata is required to maintain shared data such as refcnt.
1041 * The pointer to the mbuf.
1043 * The pointer to the external buffer.
1045 * IO address of the external buffer.
1047 * The size of the external buffer.
1049 * User-provided memory for shared data of the external buffer.
1052 rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
1053 rte_iova_t buf_iova, uint16_t buf_len,
1054 struct rte_mbuf_ext_shared_info *shinfo)
1056 /* mbuf should not be read-only */
1057 RTE_ASSERT(RTE_MBUF_DIRECT(m) && rte_mbuf_refcnt_read(m) == 1);
1058 RTE_ASSERT(shinfo->free_cb != NULL);
1060 m->buf_addr = buf_addr;
1061 m->buf_iova = buf_iova;
1062 m->buf_len = buf_len;
1067 m->ol_flags |= EXT_ATTACHED_MBUF;
1072 * Detach the external buffer attached to a mbuf, same as
1073 * ``rte_pktmbuf_detach()``
1076 * The mbuf having external buffer.
1078 #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
1081 * Copy dynamic fields from msrc to mdst.
1084 * The destination mbuf.
1089 rte_mbuf_dynfield_copy(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1091 memcpy(&mdst->dynfield1, msrc->dynfield1, sizeof(mdst->dynfield1));
1096 __rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
1098 mdst->port = msrc->port;
1099 mdst->vlan_tci = msrc->vlan_tci;
1100 mdst->vlan_tci_outer = msrc->vlan_tci_outer;
1101 mdst->tx_offload = msrc->tx_offload;
1102 mdst->hash = msrc->hash;
1103 mdst->packet_type = msrc->packet_type;
1104 rte_mbuf_dynfield_copy(mdst, msrc);
1108 * Attach packet mbuf to another packet mbuf.
1110 * If the mbuf we are attaching to isn't a direct buffer and is attached to
1111 * an external buffer, the mbuf being attached will be attached to the
1112 * external buffer instead of mbuf indirection.
1114 * Otherwise, the mbuf will be indirectly attached. After attachment we
1115 * refer the mbuf we attached as 'indirect', while mbuf we attached to as
1116 * 'direct'. The direct mbuf's reference counter is incremented.
1118 * Right now, not supported:
1119 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
1120 * - mbuf we trying to attach (mi) is used by someone else
1121 * e.g. it's reference counter is greater then 1.
1124 * The indirect packet mbuf.
1126 * The packet mbuf we're attaching to.
1128 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1130 RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1131 rte_mbuf_refcnt_read(mi) == 1);
1133 if (RTE_MBUF_HAS_EXTBUF(m)) {
1134 rte_mbuf_ext_refcnt_update(m->shinfo, 1);
1135 mi->ol_flags = m->ol_flags;
1136 mi->shinfo = m->shinfo;
1138 /* if m is not direct, get the mbuf that embeds the data */
1139 rte_mbuf_refcnt_update(rte_mbuf_from_indirect(m), 1);
1140 mi->priv_size = m->priv_size;
1141 mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1144 __rte_pktmbuf_copy_hdr(mi, m);
1146 mi->data_off = m->data_off;
1147 mi->data_len = m->data_len;
1148 mi->buf_iova = m->buf_iova;
1149 mi->buf_addr = m->buf_addr;
1150 mi->buf_len = m->buf_len;
1153 mi->pkt_len = mi->data_len;
1156 __rte_mbuf_sanity_check(mi, 1);
1157 __rte_mbuf_sanity_check(m, 0);
1161 * @internal used by rte_pktmbuf_detach().
1163 * Decrement the reference counter of the external buffer. When the
1164 * reference counter becomes 0, the buffer is freed by pre-registered
1168 __rte_pktmbuf_free_extbuf(struct rte_mbuf *m)
1170 RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
1171 RTE_ASSERT(m->shinfo != NULL);
1173 if (rte_mbuf_ext_refcnt_update(m->shinfo, -1) == 0)
1174 m->shinfo->free_cb(m->buf_addr, m->shinfo->fcb_opaque);
1178 * @internal used by rte_pktmbuf_detach().
1180 * Decrement the direct mbuf's reference counter. When the reference
1181 * counter becomes 0, the direct mbuf is freed.
1184 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
1186 struct rte_mbuf *md;
1188 RTE_ASSERT(RTE_MBUF_CLONED(m));
1190 md = rte_mbuf_from_indirect(m);
1192 if (rte_mbuf_refcnt_update(md, -1) == 0) {
1195 rte_mbuf_refcnt_set(md, 1);
1196 rte_mbuf_raw_free(md);
1201 * Detach a packet mbuf from external buffer or direct buffer.
1203 * - decrement refcnt and free the external/direct buffer if refcnt
1205 * - restore original mbuf address and length values.
1206 * - reset pktmbuf data and data_len to their default values.
1208 * All other fields of the given packet mbuf will be left intact.
1210 * If the packet mbuf was allocated from the pool with pinned
1211 * external buffers the rte_pktmbuf_detach does nothing with the
1212 * mbuf of this kind, because the pinned buffers are not supposed
1216 * The indirect attached packet mbuf.
1218 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1220 struct rte_mempool *mp = m->pool;
1221 uint32_t mbuf_size, buf_len;
1224 if (RTE_MBUF_HAS_EXTBUF(m)) {
1226 * The mbuf has the external attached buffer,
1227 * we should check the type of the memory pool where
1228 * the mbuf was allocated from to detect the pinned
1231 uint32_t flags = rte_pktmbuf_priv_flags(mp);
1233 if (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) {
1235 * The pinned external buffer should not be
1236 * detached from its backing mbuf, just exit.
1240 __rte_pktmbuf_free_extbuf(m);
1242 __rte_pktmbuf_free_direct(m);
1244 priv_size = rte_pktmbuf_priv_size(mp);
1245 mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
1246 buf_len = rte_pktmbuf_data_room_size(mp);
1248 m->priv_size = priv_size;
1249 m->buf_addr = (char *)m + mbuf_size;
1250 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1251 m->buf_len = (uint16_t)buf_len;
1252 rte_pktmbuf_reset_headroom(m);
1258 * @internal Handle the packet mbufs with attached pinned external buffer
1259 * on the mbuf freeing:
1261 * - return zero if reference counter in shinfo is one. It means there is
1262 * no more reference to this pinned buffer and mbuf can be returned to
1265 * - otherwise (if reference counter is not one), decrement reference
1266 * counter and return non-zero value to prevent freeing the backing mbuf.
1268 * Returns non zero if mbuf should not be freed.
1270 static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)
1272 struct rte_mbuf_ext_shared_info *shinfo;
1274 /* Clear flags, mbuf is being freed. */
1275 m->ol_flags = EXT_ATTACHED_MBUF;
1278 /* Optimize for performance - do not dec/reinit */
1279 if (likely(rte_mbuf_ext_refcnt_read(shinfo) == 1))
1283 * Direct usage of add primitive to avoid
1284 * duplication of comparing with one.
1286 if (likely(__atomic_add_fetch(&shinfo->refcnt, (uint16_t)-1,
1290 /* Reinitialize counter before mbuf freeing. */
1291 rte_mbuf_ext_refcnt_set(shinfo, 1);
1296 * Decrease reference counter and unlink a mbuf segment
1298 * This function does the same than a free, except that it does not
1299 * return the segment to its pool.
1300 * It decreases the reference counter, and if it reaches 0, it is
1301 * detached from its parent for an indirect mbuf.
1304 * The mbuf to be unlinked
1306 * - (m) if it is the last reference. It can be recycled or freed.
1307 * - (NULL) if the mbuf still has remaining references on it.
1309 static __rte_always_inline struct rte_mbuf *
1310 rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1312 __rte_mbuf_sanity_check(m, 0);
1314 if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1316 if (!RTE_MBUF_DIRECT(m)) {
1317 rte_pktmbuf_detach(m);
1318 if (RTE_MBUF_HAS_EXTBUF(m) &&
1319 RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
1320 __rte_pktmbuf_pinned_extbuf_decref(m))
1324 if (m->next != NULL) {
1331 } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1333 if (!RTE_MBUF_DIRECT(m)) {
1334 rte_pktmbuf_detach(m);
1335 if (RTE_MBUF_HAS_EXTBUF(m) &&
1336 RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
1337 __rte_pktmbuf_pinned_extbuf_decref(m))
1341 if (m->next != NULL) {
1345 rte_mbuf_refcnt_set(m, 1);
1353 * Free a segment of a packet mbuf into its original mempool.
1355 * Free an mbuf, without parsing other segments in case of chained
1359 * The packet mbuf segment to be freed.
1361 static __rte_always_inline void
1362 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1364 m = rte_pktmbuf_prefree_seg(m);
1365 if (likely(m != NULL))
1366 rte_mbuf_raw_free(m);
1370 * Free a packet mbuf back into its original mempool.
1372 * Free an mbuf, and all its segments in case of chained buffers. Each
1373 * segment is added back into its original mempool.
1376 * The packet mbuf to be freed. If NULL, the function does nothing.
1378 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1380 struct rte_mbuf *m_next;
1383 __rte_mbuf_sanity_check(m, 1);
1387 rte_pktmbuf_free_seg(m);
1393 * Free a bulk of packet mbufs back into their original mempools.
1395 * Free a bulk of mbufs, and all their segments in case of chained buffers.
1396 * Each segment is added back into its original mempool.
1399 * Array of pointers to packet mbufs.
1400 * The array may contain NULL pointers.
1404 void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count);
1407 * Create a "clone" of the given packet mbuf.
1409 * Walks through all segments of the given packet mbuf, and for each of them:
1410 * - Creates a new packet mbuf from the given pool.
1411 * - Attaches newly created mbuf to the segment.
1412 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1413 * from the original packet mbuf.
1416 * The packet mbuf to be cloned.
1418 * The mempool from which the "clone" mbufs are allocated.
1420 * - The pointer to the new "clone" mbuf on success.
1421 * - NULL if allocation fails.
1424 rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
1427 * Create a full copy of a given packet mbuf.
1429 * Copies all the data from a given packet mbuf to a newly allocated
1430 * set of mbufs. The private data are is not copied.
1433 * The packet mbuf to be copied.
1435 * The mempool from which the "clone" mbufs are allocated.
1437 * The number of bytes to skip before copying.
1438 * If the mbuf does not have that many bytes, it is an error
1439 * and NULL is returned.
1441 * The upper limit on bytes to copy. Passing UINT32_MAX
1442 * means all data (after offset).
1444 * - The pointer to the new "clone" mbuf on success.
1445 * - NULL if allocation fails.
1448 rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
1449 uint32_t offset, uint32_t length);
1452 * Adds given value to the refcnt of all packet mbuf segments.
1454 * Walks through all segments of given packet mbuf and for each of them
1455 * invokes rte_mbuf_refcnt_update().
1458 * The packet mbuf whose refcnt to be updated.
1460 * The value to add to the mbuf's segments refcnt.
1462 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1464 __rte_mbuf_sanity_check(m, 1);
1467 rte_mbuf_refcnt_update(m, v);
1468 } while ((m = m->next) != NULL);
1472 * Get the headroom in a packet mbuf.
1477 * The length of the headroom.
1479 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1481 __rte_mbuf_sanity_check(m, 0);
1486 * Get the tailroom of a packet mbuf.
1491 * The length of the tailroom.
1493 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1495 __rte_mbuf_sanity_check(m, 0);
1496 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1501 * Get the last segment of the packet.
1506 * The last segment of the given mbuf.
1508 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1510 __rte_mbuf_sanity_check(m, 1);
1511 while (m->next != NULL)
1517 * A macro that returns the length of the packet.
1519 * The value can be read or assigned.
1524 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1527 * A macro that returns the length of the segment.
1529 * The value can be read or assigned.
1534 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1537 * Prepend len bytes to an mbuf data area.
1539 * Returns a pointer to the new
1540 * data start address. If there is not enough headroom in the first
1541 * segment, the function will return NULL, without modifying the mbuf.
1546 * The amount of data to prepend (in bytes).
1548 * A pointer to the start of the newly prepended data, or
1549 * NULL if there is not enough headroom space in the first segment
1551 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1554 __rte_mbuf_sanity_check(m, 1);
1556 if (unlikely(len > rte_pktmbuf_headroom(m)))
1559 /* NB: elaborating the subtraction like this instead of using
1560 * -= allows us to ensure the result type is uint16_t
1561 * avoiding compiler warnings on gcc 8.1 at least */
1562 m->data_off = (uint16_t)(m->data_off - len);
1563 m->data_len = (uint16_t)(m->data_len + len);
1564 m->pkt_len = (m->pkt_len + len);
1566 return (char *)m->buf_addr + m->data_off;
1570 * Append len bytes to an mbuf.
1572 * Append len bytes to an mbuf and return a pointer to the start address
1573 * of the added data. If there is not enough tailroom in the last
1574 * segment, the function will return NULL, without modifying the mbuf.
1579 * The amount of data to append (in bytes).
1581 * A pointer to the start of the newly appended data, or
1582 * NULL if there is not enough tailroom space in the last segment
1584 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1587 struct rte_mbuf *m_last;
1589 __rte_mbuf_sanity_check(m, 1);
1591 m_last = rte_pktmbuf_lastseg(m);
1592 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1595 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1596 m_last->data_len = (uint16_t)(m_last->data_len + len);
1597 m->pkt_len = (m->pkt_len + len);
1598 return (char*) tail;
1602 * Remove len bytes at the beginning of an mbuf.
1604 * Returns a pointer to the start address of the new data area. If the
1605 * length is greater than the length of the first segment, then the
1606 * function will fail and return NULL, without modifying the mbuf.
1611 * The amount of data to remove (in bytes).
1613 * A pointer to the new start of the data.
1615 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1617 __rte_mbuf_sanity_check(m, 1);
1619 if (unlikely(len > m->data_len))
1622 /* NB: elaborating the addition like this instead of using
1623 * += allows us to ensure the result type is uint16_t
1624 * avoiding compiler warnings on gcc 8.1 at least */
1625 m->data_len = (uint16_t)(m->data_len - len);
1626 m->data_off = (uint16_t)(m->data_off + len);
1627 m->pkt_len = (m->pkt_len - len);
1628 return (char *)m->buf_addr + m->data_off;
1632 * Remove len bytes of data at the end of the mbuf.
1634 * If the length is greater than the length of the last segment, the
1635 * function will fail and return -1 without modifying the mbuf.
1640 * The amount of data to remove (in bytes).
1645 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1647 struct rte_mbuf *m_last;
1649 __rte_mbuf_sanity_check(m, 1);
1651 m_last = rte_pktmbuf_lastseg(m);
1652 if (unlikely(len > m_last->data_len))
1655 m_last->data_len = (uint16_t)(m_last->data_len - len);
1656 m->pkt_len = (m->pkt_len - len);
1661 * Test if mbuf data is contiguous.
1666 * - 1, if all data is contiguous (one segment).
1667 * - 0, if there is several segments.
1669 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1671 __rte_mbuf_sanity_check(m, 1);
1672 return m->nb_segs == 1;
1676 * @internal used by rte_pktmbuf_read().
1678 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1679 uint32_t len, void *buf);
1682 * Read len data bytes in a mbuf at specified offset.
1684 * If the data is contiguous, return the pointer in the mbuf data, else
1685 * copy the data in the buffer provided by the user and return its
1689 * The pointer to the mbuf.
1691 * The offset of the data in the mbuf.
1693 * The amount of bytes to read.
1695 * The buffer where data is copied if it is not contiguous in mbuf
1696 * data. Its length should be at least equal to the len parameter.
1698 * The pointer to the data, either in the mbuf if it is contiguous,
1699 * or in the user buffer. If mbuf is too small, NULL is returned.
1701 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1702 uint32_t off, uint32_t len, void *buf)
1704 if (likely(off + len <= rte_pktmbuf_data_len(m)))
1705 return rte_pktmbuf_mtod_offset(m, char *, off);
1707 return __rte_pktmbuf_read(m, off, len, buf);
1711 * Chain an mbuf to another, thereby creating a segmented packet.
1713 * Note: The implementation will do a linear walk over the segments to find
1714 * the tail entry. For cases when there are many segments, it's better to
1715 * chain the entries manually.
1718 * The head of the mbuf chain (the first packet)
1720 * The mbuf to put last in the chain
1724 * - -EOVERFLOW, if the chain segment limit exceeded
1726 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1728 struct rte_mbuf *cur_tail;
1730 /* Check for number-of-segments-overflow */
1731 if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1734 /* Chain 'tail' onto the old tail */
1735 cur_tail = rte_pktmbuf_lastseg(head);
1736 cur_tail->next = tail;
1738 /* accumulate number of segments and total length.
1739 * NB: elaborating the addition like this instead of using
1740 * -= allows us to ensure the result type is uint16_t
1741 * avoiding compiler warnings on gcc 8.1 at least */
1742 head->nb_segs = (uint16_t)(head->nb_segs + tail->nb_segs);
1743 head->pkt_len += tail->pkt_len;
1745 /* pkt_len is only set in the head */
1746 tail->pkt_len = tail->data_len;
1752 * For given input values generate raw tx_offload value.
1753 * Note that it is caller responsibility to make sure that input parameters
1754 * don't exceed maximum bit-field values.
1764 * outer_l3_len value.
1766 * outer_l2_len value.
1770 * raw tx_offload value.
1772 static __rte_always_inline uint64_t
1773 rte_mbuf_tx_offload(uint64_t il2, uint64_t il3, uint64_t il4, uint64_t tso,
1774 uint64_t ol3, uint64_t ol2, uint64_t unused)
1776 return il2 << RTE_MBUF_L2_LEN_OFS |
1777 il3 << RTE_MBUF_L3_LEN_OFS |
1778 il4 << RTE_MBUF_L4_LEN_OFS |
1779 tso << RTE_MBUF_TSO_SEGSZ_OFS |
1780 ol3 << RTE_MBUF_OUTL3_LEN_OFS |
1781 ol2 << RTE_MBUF_OUTL2_LEN_OFS |
1782 unused << RTE_MBUF_TXOFLD_UNUSED_OFS;
1786 * Validate general requirements for Tx offload in mbuf.
1788 * This function checks correctness and completeness of Tx offload settings.
1791 * The packet mbuf to be validated.
1793 * 0 if packet is valid
1796 rte_validate_tx_offload(const struct rte_mbuf *m)
1798 uint64_t ol_flags = m->ol_flags;
1800 /* Does packet set any of available offloads? */
1801 if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
1804 /* IP checksum can be counted only for IPv4 packet */
1805 if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
1808 /* IP type not set when required */
1809 if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
1810 if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
1813 /* Check requirements for TSO packet */
1814 if (ol_flags & PKT_TX_TCP_SEG)
1815 if ((m->tso_segsz == 0) ||
1816 ((ol_flags & PKT_TX_IPV4) &&
1817 !(ol_flags & PKT_TX_IP_CKSUM)))
1820 /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1821 if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
1822 !(ol_flags & PKT_TX_OUTER_IPV4))
1829 * @internal used by rte_pktmbuf_linearize().
1831 int __rte_pktmbuf_linearize(struct rte_mbuf *mbuf);
1834 * Linearize data in mbuf.
1836 * This function moves the mbuf data in the first segment if there is enough
1837 * tailroom. The subsequent segments are unchained and freed.
1846 rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
1848 if (rte_pktmbuf_is_contiguous(mbuf))
1850 return __rte_pktmbuf_linearize(mbuf);
1854 * Dump an mbuf structure to a file.
1856 * Dump all fields for the given packet mbuf and all its associated
1857 * segments (in the case of a chained buffer).
1860 * A pointer to a file for output
1864 * If dump_len != 0, also dump the "dump_len" first data bytes of
1867 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1870 * Get the value of mbuf sched queue_id field.
1872 static inline uint32_t
1873 rte_mbuf_sched_queue_get(const struct rte_mbuf *m)
1875 return m->hash.sched.queue_id;
1879 * Get the value of mbuf sched traffic_class field.
1881 static inline uint8_t
1882 rte_mbuf_sched_traffic_class_get(const struct rte_mbuf *m)
1884 return m->hash.sched.traffic_class;
1888 * Get the value of mbuf sched color field.
1890 static inline uint8_t
1891 rte_mbuf_sched_color_get(const struct rte_mbuf *m)
1893 return m->hash.sched.color;
1897 * Get the values of mbuf sched queue_id, traffic_class and color.
1902 * Returns the queue id
1903 * @param traffic_class
1904 * Returns the traffic class id
1906 * Returns the colour id
1909 rte_mbuf_sched_get(const struct rte_mbuf *m, uint32_t *queue_id,
1910 uint8_t *traffic_class,
1913 struct rte_mbuf_sched sched = m->hash.sched;
1915 *queue_id = sched.queue_id;
1916 *traffic_class = sched.traffic_class;
1917 *color = sched.color;
1921 * Set the mbuf sched queue_id to the defined value.
1924 rte_mbuf_sched_queue_set(struct rte_mbuf *m, uint32_t queue_id)
1926 m->hash.sched.queue_id = queue_id;
1930 * Set the mbuf sched traffic_class id to the defined value.
1933 rte_mbuf_sched_traffic_class_set(struct rte_mbuf *m, uint8_t traffic_class)
1935 m->hash.sched.traffic_class = traffic_class;
1939 * Set the mbuf sched color id to the defined value.
1942 rte_mbuf_sched_color_set(struct rte_mbuf *m, uint8_t color)
1944 m->hash.sched.color = color;
1948 * Set the mbuf sched queue_id, traffic_class and color.
1953 * Queue id value to be set
1954 * @param traffic_class
1955 * Traffic class id value to be set
1957 * Color id to be set
1960 rte_mbuf_sched_set(struct rte_mbuf *m, uint32_t queue_id,
1961 uint8_t traffic_class,
1964 m->hash.sched = (struct rte_mbuf_sched){
1965 .queue_id = queue_id,
1966 .traffic_class = traffic_class,
1976 #endif /* _RTE_MBUF_H_ */