4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * The mbuf library provides the ability to create and destroy buffers
42 * that may be used by the RTE application to store message
43 * buffers. The message buffers are stored in a mempool, using the
44 * RTE mempool library.
46 * This library provide an API to allocate/free packet mbufs, which are
47 * used to carry network packets.
49 * To understand the concepts of packet buffers or mbufs, you
50 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
51 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
52 * http://www.kohala.com/start/tcpipiv2.html
56 #include <rte_mempool.h>
57 #include <rte_atomic.h>
58 #include <rte_prefetch.h>
59 #include <rte_branch_prediction.h>
65 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
66 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
69 * Packet Offload Features Flags. It also carry packet type information.
70 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
72 * - RX flags start at bit position zero, and get added to the left of previous
74 * - The most-significant 8 bits are reserved for generic mbuf flags
75 * - TX flags therefore start at bit position 55 (i.e. 63-8), and new flags get
76 * added to the right of the previously defined flags
78 #define PKT_RX_VLAN_PKT (1ULL << 0) /**< RX packet is a 802.1q VLAN packet. */
79 #define PKT_RX_RSS_HASH (1ULL << 1) /**< RX packet with RSS hash result. */
80 #define PKT_RX_FDIR (1ULL << 2) /**< RX packet with FDIR match indicate. */
81 #define PKT_RX_L4_CKSUM_BAD (1ULL << 3) /**< L4 cksum of RX pkt. is not OK. */
82 #define PKT_RX_IP_CKSUM_BAD (1ULL << 4) /**< IP cksum of RX pkt. is not OK. */
83 #define PKT_RX_EIP_CKSUM_BAD (0ULL << 0) /**< External IP header checksum error. */
84 #define PKT_RX_OVERSIZE (0ULL << 0) /**< Num of desc of an RX pkt oversize. */
85 #define PKT_RX_HBUF_OVERFLOW (0ULL << 0) /**< Header buffer overflow. */
86 #define PKT_RX_RECIP_ERR (0ULL << 0) /**< Hardware processing error. */
87 #define PKT_RX_MAC_ERR (0ULL << 0) /**< MAC error. */
88 #define PKT_RX_IPV4_HDR (1ULL << 5) /**< RX packet with IPv4 header. */
89 #define PKT_RX_IPV4_HDR_EXT (1ULL << 6) /**< RX packet with extended IPv4 header. */
90 #define PKT_RX_IPV6_HDR (1ULL << 7) /**< RX packet with IPv6 header. */
91 #define PKT_RX_IPV6_HDR_EXT (1ULL << 8) /**< RX packet with extended IPv6 header. */
92 #define PKT_RX_IEEE1588_PTP (1ULL << 9) /**< RX IEEE1588 L2 Ethernet PT Packet. */
93 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
94 #define PKT_RX_TUNNEL_IPV4_HDR (1ULL << 11) /**< RX tunnel packet with IPv4 header.*/
95 #define PKT_RX_TUNNEL_IPV6_HDR (1ULL << 12) /**< RX tunnel packet with IPv6 header. */
96 #define PKT_RX_FDIR_ID (1ULL << 13) /**< FD id reported if FDIR match. */
97 #define PKT_RX_FDIR_FLX (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
98 /* add new RX flags here */
100 /* add new TX flags here */
101 #define PKT_TX_VXLAN_CKSUM (1ULL << 50) /**< TX checksum of VXLAN computed by NIC */
102 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
104 * Bits 52+53 used for L4 packet type with checksum enabled.
110 #define PKT_TX_L4_NO_CKSUM (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
111 #define PKT_TX_TCP_CKSUM (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
112 #define PKT_TX_SCTP_CKSUM (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
113 #define PKT_TX_UDP_CKSUM (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
114 #define PKT_TX_L4_MASK (3ULL << 52) /**< Mask for L4 cksum offload request. */
116 #define PKT_TX_IP_CKSUM (1ULL << 54) /**< IP cksum of TX pkt. computed by NIC. */
117 #define PKT_TX_IPV4_CSUM PKT_TX_IP_CKSUM /**< Alias of PKT_TX_IP_CKSUM. */
118 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
119 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR /**< IPv6 packet */
121 #define PKT_TX_VLAN_PKT (1ULL << 55) /**< TX packet is a 802.1q VLAN packet. */
123 /* Use final bit of flags to indicate a control mbuf */
124 #define CTRL_MBUF_FLAG (1ULL << 63) /**< Mbuf contains control data */
127 * Bit Mask to indicate what bits required for building TX context
129 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
131 /* define a set of marker types that can be used to refer to set points in the
133 typedef void *MARKER[0]; /**< generic marker for a point in a structure */
134 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
135 * with a single assignment */
137 * The generic rte_mbuf, containing a packet mbuf.
142 void *buf_addr; /**< Virtual address of segment buffer. */
143 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
145 /* next 8 bytes are initialised on RX descriptor rearm */
147 uint16_t buf_len; /**< Length of segment buffer. */
151 * 16-bit Reference counter.
152 * It should only be accessed using the following functions:
153 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
154 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
155 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
159 #ifdef RTE_MBUF_REFCNT
160 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
161 uint16_t refcnt; /**< Non-atomically accessed refcnt */
163 uint16_t refcnt_reserved; /**< Do not use this field */
165 uint8_t nb_segs; /**< Number of segments. */
166 uint8_t port; /**< Input port. */
168 uint64_t ol_flags; /**< Offload features. */
170 /* remaining bytes are set on RX when pulling packet from descriptor */
171 MARKER rx_descriptor_fields1;
174 * The packet type, which is used to indicate ordinary packet and also
175 * tunneled packet format, i.e. each number is represented a type of
178 uint16_t packet_type;
180 uint16_t data_len; /**< Amount of data in segment buffer. */
181 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
182 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order) */
185 uint32_t rss; /**< RSS hash result if RSS enabled */
193 /**< Second 4 flexible bytes */
196 /**< First 4 flexible bytes or FD ID, dependent on
197 PKT_RX_FDIR_* flag in ol_flags. */
198 } fdir; /**< Filter identifier if FDIR enabled */
199 uint32_t sched; /**< Hierarchical scheduler */
200 uint32_t usr; /**< User defined tags. See @rte_distributor_process */
201 } hash; /**< hash information */
203 /* second cache line - fields only used in slow path or on TX */
204 MARKER cacheline1 __rte_cache_aligned;
207 void *userdata; /**< Can be used for external metadata */
208 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
211 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
212 struct rte_mbuf *next; /**< Next segment of scattered packet. */
214 /* fields to support TX offloads */
216 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
218 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
219 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
223 /* fields for TX offloading of tunnels */
225 uint16_t inner_l2_l3_len;
226 /**< combined inner l2/l3 lengths as single var */
228 uint16_t inner_l3_len:9;
229 /**< inner L3 (IP) Header Length. */
230 uint16_t inner_l2_len:7;
231 /**< inner L2 (MAC) Header Length. */
234 } __rte_cache_aligned;
237 * Given the buf_addr returns the pointer to corresponding mbuf.
239 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
242 * Given the pointer to mbuf returns an address where it's buf_addr
245 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
248 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
250 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
253 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
255 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
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.*/
268 #ifdef RTE_LIBRTE_MBUF_DEBUG
270 /** check mbuf type in debug mode */
271 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
273 /** check mbuf type in debug mode if mbuf pointer is not null */
274 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
276 rte_mbuf_sanity_check(m, is_h); \
279 /** MBUF asserts in debug mode */
280 #define RTE_MBUF_ASSERT(exp) \
282 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
285 #else /* RTE_LIBRTE_MBUF_DEBUG */
287 /** check mbuf type in debug mode */
288 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
290 /** check mbuf type in debug mode if mbuf pointer is not null */
291 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
293 /** MBUF asserts in debug mode */
294 #define RTE_MBUF_ASSERT(exp) do { } while (0)
296 #endif /* RTE_LIBRTE_MBUF_DEBUG */
298 #ifdef RTE_MBUF_REFCNT
299 #ifdef RTE_MBUF_REFCNT_ATOMIC
302 * Adds given value to an mbuf's refcnt and returns its new value.
306 * Value to add/subtract
310 static inline uint16_t
311 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
313 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
317 * Reads the value of an mbuf's refcnt.
321 * Reference count number.
323 static inline uint16_t
324 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
326 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
330 * Sets an mbuf's refcnt to a defined value.
337 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
339 rte_atomic16_set(&m->refcnt_atomic, new_value);
342 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
345 * Adds given value to an mbuf's refcnt and returns its new value.
347 static inline uint16_t
348 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
350 m->refcnt = (uint16_t)(m->refcnt + value);
355 * Reads the value of an mbuf's refcnt.
357 static inline uint16_t
358 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
364 * Sets an mbuf's refcnt to the defined value.
367 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
369 m->refcnt = new_value;
372 #endif /* RTE_MBUF_REFCNT_ATOMIC */
375 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
380 #else /* ! RTE_MBUF_REFCNT */
383 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
385 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
387 #endif /* RTE_MBUF_REFCNT */
391 * Sanity checks on an mbuf.
393 * Check the consistency of the given mbuf. The function will cause a
394 * panic if corruption is detected.
397 * The mbuf to be checked.
399 * True if the mbuf is a packet header, false if it is a sub-segment
400 * of a packet (in this case, some fields like nb_segs are not checked)
403 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
406 * @internal Allocate a new mbuf from mempool *mp*.
407 * The use of that function is reserved for RTE internal needs.
408 * Please use rte_pktmbuf_alloc().
411 * The mempool from which mbuf is allocated.
413 * - The pointer to the new mbuf on success.
414 * - NULL if allocation failed.
416 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
420 if (rte_mempool_get(mp, &mb) < 0)
422 m = (struct rte_mbuf *)mb;
423 #ifdef RTE_MBUF_REFCNT
424 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
425 rte_mbuf_refcnt_set(m, 1);
426 #endif /* RTE_MBUF_REFCNT */
431 * @internal Put mbuf back into its original mempool.
432 * The use of that function is reserved for RTE internal needs.
433 * Please use rte_pktmbuf_free().
436 * The mbuf to be freed.
438 static inline void __attribute__((always_inline))
439 __rte_mbuf_raw_free(struct rte_mbuf *m)
441 #ifdef RTE_MBUF_REFCNT
442 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
443 #endif /* RTE_MBUF_REFCNT */
444 rte_mempool_put(m->pool, m);
447 /* Operations on ctrl mbuf */
450 * The control mbuf constructor.
452 * This function initializes some fields in an mbuf structure that are
453 * not modified by the user once created (mbuf type, origin pool, buffer
454 * start address, and so on). This function is given as a callback function
455 * to rte_mempool_create() at pool creation time.
458 * The mempool from which the mbuf is allocated.
460 * A pointer that can be used by the user to retrieve useful information
461 * for mbuf initialization. This pointer comes from the ``init_arg``
462 * parameter of rte_mempool_create().
464 * The mbuf to initialize.
466 * The index of the mbuf in the pool table.
468 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
469 void *m, unsigned i);
472 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
474 * This new mbuf is initialized with data pointing to the beginning of
475 * buffer, and with a length of zero.
478 * The mempool from which the mbuf is allocated.
480 * - The pointer to the new mbuf on success.
481 * - NULL if allocation failed.
483 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
486 * Free a control mbuf back into its original mempool.
489 * The control mbuf to be freed.
491 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
494 * A macro that returns the pointer to the carried data.
496 * The value that can be read or assigned.
501 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
504 * A macro that returns the length of the carried data.
506 * The value that can be read or assigned.
511 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
514 * Tests if an mbuf is a control mbuf
517 * The mbuf to be tested
519 * - True (1) if the mbuf is a control mbuf
520 * - False(0) otherwise
523 rte_is_ctrlmbuf(struct rte_mbuf *m)
525 return (!!(m->ol_flags & CTRL_MBUF_FLAG));
528 /* Operations on pkt mbuf */
531 * The packet mbuf constructor.
533 * This function initializes some fields in the mbuf structure that are
534 * not modified by the user once created (origin pool, buffer start
535 * address, and so on). This function is given as a callback function to
536 * rte_mempool_create() at pool creation time.
539 * The mempool from which mbufs originate.
541 * A pointer that can be used by the user to retrieve useful information
542 * for mbuf initialization. This pointer comes from the ``init_arg``
543 * parameter of rte_mempool_create().
545 * The mbuf to initialize.
547 * The index of the mbuf in the pool table.
549 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
550 void *m, unsigned i);
554 * A packet mbuf pool constructor.
556 * This function initializes the mempool private data in the case of a
557 * pktmbuf pool. This private data is needed by the driver. The
558 * function is given as a callback function to rte_mempool_create() at
559 * pool creation. It can be extended by the user, for example, to
560 * provide another packet size.
563 * The mempool from which mbufs originate.
565 * A pointer that can be used by the user to retrieve useful information
566 * for mbuf initialization. This pointer comes from the ``init_arg``
567 * parameter of rte_mempool_create().
569 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
572 * Reset the fields of a packet mbuf to their default values.
574 * The given mbuf must have only one segment.
577 * The packet mbuf to be resetted.
579 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
584 m->inner_l2_l3_len = 0;
591 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
592 RTE_PKTMBUF_HEADROOM : m->buf_len;
595 __rte_mbuf_sanity_check(m, 1);
599 * Allocate a new mbuf from a mempool.
601 * This new mbuf contains one segment, which has a length of 0. The pointer
602 * to data is initialized to have some bytes of headroom in the buffer
603 * (if buffer size allows).
606 * The mempool from which the mbuf is allocated.
608 * - The pointer to the new mbuf on success.
609 * - NULL if allocation failed.
611 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
614 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
615 rte_pktmbuf_reset(m);
619 #ifdef RTE_MBUF_REFCNT
622 * Attach packet mbuf to another packet mbuf.
623 * After attachment we refer the mbuf we attached as 'indirect',
624 * while mbuf we attached to as 'direct'.
625 * Right now, not supported:
626 * - attachment to indirect mbuf (e.g. - md has to be direct).
627 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
628 * - mbuf we trying to attach (mi) is used by someone else
629 * e.g. it's reference counter is greater then 1.
632 * The indirect packet mbuf.
634 * The direct packet mbuf.
637 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
639 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
640 RTE_MBUF_DIRECT(mi) &&
641 rte_mbuf_refcnt_read(mi) == 1);
643 rte_mbuf_refcnt_update(md, 1);
644 mi->buf_physaddr = md->buf_physaddr;
645 mi->buf_addr = md->buf_addr;
646 mi->buf_len = md->buf_len;
649 mi->data_off = md->data_off;
650 mi->data_len = md->data_len;
652 mi->vlan_tci = md->vlan_tci;
653 mi->l2_l3_len = md->l2_l3_len;
654 mi->inner_l2_l3_len = md->inner_l2_l3_len;
658 mi->pkt_len = mi->data_len;
660 mi->ol_flags = md->ol_flags;
661 mi->packet_type = md->packet_type;
663 __rte_mbuf_sanity_check(mi, 1);
664 __rte_mbuf_sanity_check(md, 0);
668 * Detach an indirect packet mbuf -
669 * - restore original mbuf address and length values.
670 * - reset pktmbuf data and data_len to their default values.
671 * All other fields of the given packet mbuf will be left intact.
674 * The indirect attached packet mbuf.
677 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
679 const struct rte_mempool *mp = m->pool;
680 void *buf = RTE_MBUF_TO_BADDR(m);
681 uint32_t buf_len = mp->elt_size - sizeof(*m);
682 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
685 m->buf_len = (uint16_t)buf_len;
687 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
688 RTE_PKTMBUF_HEADROOM : m->buf_len;
693 #endif /* RTE_MBUF_REFCNT */
696 static inline struct rte_mbuf* __attribute__((always_inline))
697 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
699 __rte_mbuf_sanity_check(m, 0);
701 #ifdef RTE_MBUF_REFCNT
702 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
703 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
704 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
706 rte_mbuf_refcnt_set(m, 0);
708 /* if this is an indirect mbuf, then
710 * - free attached mbuf segment
712 if (unlikely (md != m)) {
713 rte_pktmbuf_detach(m);
714 if (rte_mbuf_refcnt_update(md, -1) == 0)
715 __rte_mbuf_raw_free(md);
719 #ifdef RTE_MBUF_REFCNT
726 * Free a segment of a packet mbuf into its original mempool.
728 * Free an mbuf, without parsing other segments in case of chained
732 * The packet mbuf segment to be freed.
734 static inline void __attribute__((always_inline))
735 rte_pktmbuf_free_seg(struct rte_mbuf *m)
737 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
739 __rte_mbuf_raw_free(m);
744 * Free a packet mbuf back into its original mempool.
746 * Free an mbuf, and all its segments in case of chained buffers. Each
747 * segment is added back into its original mempool.
750 * The packet mbuf to be freed.
752 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
754 struct rte_mbuf *m_next;
756 __rte_mbuf_sanity_check(m, 1);
760 rte_pktmbuf_free_seg(m);
765 #ifdef RTE_MBUF_REFCNT
768 * Creates a "clone" of the given packet mbuf.
770 * Walks through all segments of the given packet mbuf, and for each of them:
771 * - Creates a new packet mbuf from the given pool.
772 * - Attaches newly created mbuf to the segment.
773 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
774 * from the original packet mbuf.
777 * The packet mbuf to be cloned.
779 * The mempool from which the "clone" mbufs are allocated.
781 * - The pointer to the new "clone" mbuf on success.
782 * - NULL if allocation fails.
784 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
785 struct rte_mempool *mp)
787 struct rte_mbuf *mc, *mi, **prev;
791 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
796 pktlen = md->pkt_len;
801 rte_pktmbuf_attach(mi, md);
804 } while ((md = md->next) != NULL &&
805 (mi = rte_pktmbuf_alloc(mp)) != NULL);
809 mc->pkt_len = pktlen;
811 /* Allocation of new indirect segment failed */
812 if (unlikely (mi == NULL)) {
813 rte_pktmbuf_free(mc);
817 __rte_mbuf_sanity_check(mc, 1);
822 * Adds given value to the refcnt of all packet mbuf segments.
824 * Walks through all segments of given packet mbuf and for each of them
825 * invokes rte_mbuf_refcnt_update().
828 * The packet mbuf whose refcnt to be updated.
830 * The value to add to the mbuf's segments refcnt.
832 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
834 __rte_mbuf_sanity_check(m, 1);
837 rte_mbuf_refcnt_update(m, v);
838 } while ((m = m->next) != NULL);
841 #endif /* RTE_MBUF_REFCNT */
844 * Get the headroom in a packet mbuf.
849 * The length of the headroom.
851 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
853 __rte_mbuf_sanity_check(m, 1);
858 * Get the tailroom of a packet mbuf.
863 * The length of the tailroom.
865 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
867 __rte_mbuf_sanity_check(m, 1);
868 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
873 * Get the last segment of the packet.
878 * The last segment of the given mbuf.
880 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
882 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
884 __rte_mbuf_sanity_check(m, 1);
885 while (m2->next != NULL)
891 * A macro that points to the start of the data in the mbuf.
893 * The returned pointer is cast to type t. Before using this
894 * function, the user must ensure that m_headlen(m) is large enough to
900 * The type to cast the result into.
902 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
905 * A macro that returns the length of the packet.
907 * The value can be read or assigned.
912 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
915 * A macro that returns the length of the segment.
917 * The value can be read or assigned.
922 #define rte_pktmbuf_data_len(m) ((m)->data_len)
925 * Prepend len bytes to an mbuf data area.
927 * Returns a pointer to the new
928 * data start address. If there is not enough headroom in the first
929 * segment, the function will return NULL, without modifying the mbuf.
934 * The amount of data to prepend (in bytes).
936 * A pointer to the start of the newly prepended data, or
937 * NULL if there is not enough headroom space in the first segment
939 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
942 __rte_mbuf_sanity_check(m, 1);
944 if (unlikely(len > rte_pktmbuf_headroom(m)))
948 m->data_len = (uint16_t)(m->data_len + len);
949 m->pkt_len = (m->pkt_len + len);
951 return (char *)m->buf_addr + m->data_off;
955 * Append len bytes to an mbuf.
957 * Append len bytes to an mbuf and return a pointer to the start address
958 * of the added data. If there is not enough tailroom in the last
959 * segment, the function will return NULL, without modifying the mbuf.
964 * The amount of data to append (in bytes).
966 * A pointer to the start of the newly appended data, or
967 * NULL if there is not enough tailroom space in the last segment
969 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
972 struct rte_mbuf *m_last;
974 __rte_mbuf_sanity_check(m, 1);
976 m_last = rte_pktmbuf_lastseg(m);
977 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
980 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
981 m_last->data_len = (uint16_t)(m_last->data_len + len);
982 m->pkt_len = (m->pkt_len + len);
987 * Remove len bytes at the beginning of an mbuf.
989 * Returns a pointer to the start address of the new data area. If the
990 * length is greater than the length of the first segment, then the
991 * function will fail and return NULL, without modifying the mbuf.
996 * The amount of data to remove (in bytes).
998 * A pointer to the new start of the data.
1000 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1002 __rte_mbuf_sanity_check(m, 1);
1004 if (unlikely(len > m->data_len))
1007 m->data_len = (uint16_t)(m->data_len - len);
1009 m->pkt_len = (m->pkt_len - len);
1010 return (char *)m->buf_addr + m->data_off;
1014 * Remove len bytes of data at the end of the mbuf.
1016 * If the length is greater than the length of the last segment, the
1017 * function will fail and return -1 without modifying the mbuf.
1022 * The amount of data to remove (in bytes).
1027 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1029 struct rte_mbuf *m_last;
1031 __rte_mbuf_sanity_check(m, 1);
1033 m_last = rte_pktmbuf_lastseg(m);
1034 if (unlikely(len > m_last->data_len))
1037 m_last->data_len = (uint16_t)(m_last->data_len - len);
1038 m->pkt_len = (m->pkt_len - len);
1043 * Test if mbuf data is contiguous.
1048 * - 1, if all data is contiguous (one segment).
1049 * - 0, if there is several segments.
1051 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1053 __rte_mbuf_sanity_check(m, 1);
1054 return !!(m->nb_segs == 1);
1058 * Dump an mbuf structure to the console.
1060 * Dump all fields for the given packet mbuf and all its associated
1061 * segments (in the case of a chained buffer).
1064 * A pointer to a file for output
1068 * If dump_len != 0, also dump the "dump_len" first data bytes of
1071 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1077 #endif /* _RTE_MBUF_H_ */