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. */
105 * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
106 * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
107 * L4 checksum offload, the user needs to:
108 * - fill l2_len and l3_len in mbuf
109 * - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
110 * - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
111 * - calculate the pseudo header checksum and set it in the L4 header (only
112 * for TCP or UDP). For SCTP, set the crc field to 0.
114 #define PKT_TX_L4_NO_CKSUM (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
115 #define PKT_TX_TCP_CKSUM (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
116 #define PKT_TX_SCTP_CKSUM (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
117 #define PKT_TX_UDP_CKSUM (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
118 #define PKT_TX_L4_MASK (3ULL << 52) /**< Mask for L4 cksum offload request. */
120 #define PKT_TX_IP_CKSUM (1ULL << 54) /**< IP cksum of TX pkt. computed by NIC. */
121 #define PKT_TX_IPV4_CSUM PKT_TX_IP_CKSUM /**< Alias of PKT_TX_IP_CKSUM. */
123 /** Tell the NIC it's an IPv4 packet. Required for L4 checksum offload. */
124 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR
126 /** Tell the NIC it's an IPv6 packet. Required for L4 checksum offload. */
127 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR
129 #define PKT_TX_VLAN_PKT (1ULL << 55) /**< TX packet is a 802.1q VLAN packet. */
131 /* Use final bit of flags to indicate a control mbuf */
132 #define CTRL_MBUF_FLAG (1ULL << 63) /**< Mbuf contains control data */
134 /* define a set of marker types that can be used to refer to set points in the
136 typedef void *MARKER[0]; /**< generic marker for a point in a structure */
137 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
138 * with a single assignment */
140 * The generic rte_mbuf, containing a packet mbuf.
145 void *buf_addr; /**< Virtual address of segment buffer. */
146 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
148 /* next 8 bytes are initialised on RX descriptor rearm */
150 uint16_t buf_len; /**< Length of segment buffer. */
154 * 16-bit Reference counter.
155 * It should only be accessed using the following functions:
156 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
157 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
158 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
162 #ifdef RTE_MBUF_REFCNT
163 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
164 uint16_t refcnt; /**< Non-atomically accessed refcnt */
166 uint16_t refcnt_reserved; /**< Do not use this field */
168 uint8_t nb_segs; /**< Number of segments. */
169 uint8_t port; /**< Input port. */
171 uint64_t ol_flags; /**< Offload features. */
173 /* remaining bytes are set on RX when pulling packet from descriptor */
174 MARKER rx_descriptor_fields1;
177 * The packet type, which is used to indicate ordinary packet and also
178 * tunneled packet format, i.e. each number is represented a type of
181 uint16_t packet_type;
183 uint16_t data_len; /**< Amount of data in segment buffer. */
184 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
185 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order) */
188 uint32_t rss; /**< RSS hash result if RSS enabled */
196 /**< Second 4 flexible bytes */
199 /**< First 4 flexible bytes or FD ID, dependent on
200 PKT_RX_FDIR_* flag in ol_flags. */
201 } fdir; /**< Filter identifier if FDIR enabled */
202 uint32_t sched; /**< Hierarchical scheduler */
203 uint32_t usr; /**< User defined tags. See @rte_distributor_process */
204 } hash; /**< hash information */
206 /* second cache line - fields only used in slow path or on TX */
207 MARKER cacheline1 __rte_cache_aligned;
210 void *userdata; /**< Can be used for external metadata */
211 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
214 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
215 struct rte_mbuf *next; /**< Next segment of scattered packet. */
217 /* fields to support TX offloads */
219 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
221 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
222 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
226 /* fields for TX offloading of tunnels */
228 uint16_t inner_l2_l3_len;
229 /**< combined inner l2/l3 lengths as single var */
231 uint16_t inner_l3_len:9;
232 /**< inner L3 (IP) Header Length. */
233 uint16_t inner_l2_len:7;
234 /**< inner L2 (MAC) Header Length. */
237 } __rte_cache_aligned;
240 * Given the buf_addr returns the pointer to corresponding mbuf.
242 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
245 * Given the pointer to mbuf returns an address where it's buf_addr
248 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
251 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
253 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
256 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
258 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
262 * Private data in case of pktmbuf pool.
264 * A structure that contains some pktmbuf_pool-specific data that are
265 * appended after the mempool structure (in private data).
267 struct rte_pktmbuf_pool_private {
268 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
271 #ifdef RTE_LIBRTE_MBUF_DEBUG
273 /** check mbuf type in debug mode */
274 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
276 /** check mbuf type in debug mode if mbuf pointer is not null */
277 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
279 rte_mbuf_sanity_check(m, is_h); \
282 /** MBUF asserts in debug mode */
283 #define RTE_MBUF_ASSERT(exp) \
285 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
288 #else /* RTE_LIBRTE_MBUF_DEBUG */
290 /** check mbuf type in debug mode */
291 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
293 /** check mbuf type in debug mode if mbuf pointer is not null */
294 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
296 /** MBUF asserts in debug mode */
297 #define RTE_MBUF_ASSERT(exp) do { } while (0)
299 #endif /* RTE_LIBRTE_MBUF_DEBUG */
301 #ifdef RTE_MBUF_REFCNT
302 #ifdef RTE_MBUF_REFCNT_ATOMIC
305 * Adds given value to an mbuf's refcnt and returns its new value.
309 * Value to add/subtract
313 static inline uint16_t
314 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
316 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
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 (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
333 * Sets an mbuf's refcnt to a defined value.
340 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
342 rte_atomic16_set(&m->refcnt_atomic, new_value);
345 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
348 * Adds given value to an mbuf's refcnt and returns its new value.
350 static inline uint16_t
351 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
353 m->refcnt = (uint16_t)(m->refcnt + value);
358 * Reads the value of an mbuf's refcnt.
360 static inline uint16_t
361 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
367 * Sets an mbuf's refcnt to the defined value.
370 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
372 m->refcnt = new_value;
375 #endif /* RTE_MBUF_REFCNT_ATOMIC */
378 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
383 #else /* ! RTE_MBUF_REFCNT */
386 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
388 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
390 #endif /* RTE_MBUF_REFCNT */
394 * Sanity checks on an mbuf.
396 * Check the consistency of the given mbuf. The function will cause a
397 * panic if corruption is detected.
400 * The mbuf to be checked.
402 * True if the mbuf is a packet header, false if it is a sub-segment
403 * of a packet (in this case, some fields like nb_segs are not checked)
406 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
409 * @internal Allocate a new mbuf from mempool *mp*.
410 * The use of that function is reserved for RTE internal needs.
411 * Please use rte_pktmbuf_alloc().
414 * The mempool from which mbuf is allocated.
416 * - The pointer to the new mbuf on success.
417 * - NULL if allocation failed.
419 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
423 if (rte_mempool_get(mp, &mb) < 0)
425 m = (struct rte_mbuf *)mb;
426 #ifdef RTE_MBUF_REFCNT
427 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
428 rte_mbuf_refcnt_set(m, 1);
429 #endif /* RTE_MBUF_REFCNT */
434 * @internal Put mbuf back into its original mempool.
435 * The use of that function is reserved for RTE internal needs.
436 * Please use rte_pktmbuf_free().
439 * The mbuf to be freed.
441 static inline void __attribute__((always_inline))
442 __rte_mbuf_raw_free(struct rte_mbuf *m)
444 #ifdef RTE_MBUF_REFCNT
445 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
446 #endif /* RTE_MBUF_REFCNT */
447 rte_mempool_put(m->pool, m);
450 /* Operations on ctrl mbuf */
453 * The control mbuf constructor.
455 * This function initializes some fields in an mbuf structure that are
456 * not modified by the user once created (mbuf type, origin pool, buffer
457 * start address, and so on). This function is given as a callback function
458 * to rte_mempool_create() at pool creation time.
461 * The mempool from which the mbuf is allocated.
463 * A pointer that can be used by the user to retrieve useful information
464 * for mbuf initialization. This pointer comes from the ``init_arg``
465 * parameter of rte_mempool_create().
467 * The mbuf to initialize.
469 * The index of the mbuf in the pool table.
471 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
472 void *m, unsigned i);
475 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
477 * This new mbuf is initialized with data pointing to the beginning of
478 * buffer, and with a length of zero.
481 * The mempool from which the mbuf is allocated.
483 * - The pointer to the new mbuf on success.
484 * - NULL if allocation failed.
486 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
489 * Free a control mbuf back into its original mempool.
492 * The control mbuf to be freed.
494 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
497 * A macro that returns the pointer to the carried data.
499 * The value that can be read or assigned.
504 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
507 * A macro that returns the length of the carried data.
509 * The value that can be read or assigned.
514 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
517 * Tests if an mbuf is a control mbuf
520 * The mbuf to be tested
522 * - True (1) if the mbuf is a control mbuf
523 * - False(0) otherwise
526 rte_is_ctrlmbuf(struct rte_mbuf *m)
528 return (!!(m->ol_flags & CTRL_MBUF_FLAG));
531 /* Operations on pkt mbuf */
534 * The packet mbuf constructor.
536 * This function initializes some fields in the mbuf structure that are
537 * not modified by the user once created (origin pool, buffer start
538 * address, and so on). This function is given as a callback function to
539 * rte_mempool_create() at pool creation time.
542 * The mempool from which mbufs originate.
544 * A pointer that can be used by the user to retrieve useful information
545 * for mbuf initialization. This pointer comes from the ``init_arg``
546 * parameter of rte_mempool_create().
548 * The mbuf to initialize.
550 * The index of the mbuf in the pool table.
552 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
553 void *m, unsigned i);
557 * A packet mbuf pool constructor.
559 * This function initializes the mempool private data in the case of a
560 * pktmbuf pool. This private data is needed by the driver. The
561 * function is given as a callback function to rte_mempool_create() at
562 * pool creation. It can be extended by the user, for example, to
563 * provide another packet size.
566 * The mempool from which mbufs originate.
568 * A pointer that can be used by the user to retrieve useful information
569 * for mbuf initialization. This pointer comes from the ``init_arg``
570 * parameter of rte_mempool_create().
572 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
575 * Reset the fields of a packet mbuf to their default values.
577 * The given mbuf must have only one segment.
580 * The packet mbuf to be resetted.
582 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
587 m->inner_l2_l3_len = 0;
594 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
595 RTE_PKTMBUF_HEADROOM : m->buf_len;
598 __rte_mbuf_sanity_check(m, 1);
602 * Allocate a new mbuf from a mempool.
604 * This new mbuf contains one segment, which has a length of 0. The pointer
605 * to data is initialized to have some bytes of headroom in the buffer
606 * (if buffer size allows).
609 * The mempool from which the mbuf is allocated.
611 * - The pointer to the new mbuf on success.
612 * - NULL if allocation failed.
614 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
617 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
618 rte_pktmbuf_reset(m);
622 #ifdef RTE_MBUF_REFCNT
625 * Attach packet mbuf to another packet mbuf.
626 * After attachment we refer the mbuf we attached as 'indirect',
627 * while mbuf we attached to as 'direct'.
628 * Right now, not supported:
629 * - attachment to indirect mbuf (e.g. - md has to be direct).
630 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
631 * - mbuf we trying to attach (mi) is used by someone else
632 * e.g. it's reference counter is greater then 1.
635 * The indirect packet mbuf.
637 * The direct packet mbuf.
640 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
642 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
643 RTE_MBUF_DIRECT(mi) &&
644 rte_mbuf_refcnt_read(mi) == 1);
646 rte_mbuf_refcnt_update(md, 1);
647 mi->buf_physaddr = md->buf_physaddr;
648 mi->buf_addr = md->buf_addr;
649 mi->buf_len = md->buf_len;
652 mi->data_off = md->data_off;
653 mi->data_len = md->data_len;
655 mi->vlan_tci = md->vlan_tci;
656 mi->l2_l3_len = md->l2_l3_len;
657 mi->inner_l2_l3_len = md->inner_l2_l3_len;
661 mi->pkt_len = mi->data_len;
663 mi->ol_flags = md->ol_flags;
664 mi->packet_type = md->packet_type;
666 __rte_mbuf_sanity_check(mi, 1);
667 __rte_mbuf_sanity_check(md, 0);
671 * Detach an indirect packet mbuf -
672 * - restore original mbuf address and length values.
673 * - reset pktmbuf data and data_len to their default values.
674 * All other fields of the given packet mbuf will be left intact.
677 * The indirect attached packet mbuf.
680 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
682 const struct rte_mempool *mp = m->pool;
683 void *buf = RTE_MBUF_TO_BADDR(m);
684 uint32_t buf_len = mp->elt_size - sizeof(*m);
685 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
688 m->buf_len = (uint16_t)buf_len;
690 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
691 RTE_PKTMBUF_HEADROOM : m->buf_len;
696 #endif /* RTE_MBUF_REFCNT */
699 static inline struct rte_mbuf* __attribute__((always_inline))
700 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
702 __rte_mbuf_sanity_check(m, 0);
704 #ifdef RTE_MBUF_REFCNT
705 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
706 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
707 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
709 rte_mbuf_refcnt_set(m, 0);
711 /* if this is an indirect mbuf, then
713 * - free attached mbuf segment
715 if (unlikely (md != m)) {
716 rte_pktmbuf_detach(m);
717 if (rte_mbuf_refcnt_update(md, -1) == 0)
718 __rte_mbuf_raw_free(md);
722 #ifdef RTE_MBUF_REFCNT
729 * Free a segment of a packet mbuf into its original mempool.
731 * Free an mbuf, without parsing other segments in case of chained
735 * The packet mbuf segment to be freed.
737 static inline void __attribute__((always_inline))
738 rte_pktmbuf_free_seg(struct rte_mbuf *m)
740 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
742 __rte_mbuf_raw_free(m);
747 * Free a packet mbuf back into its original mempool.
749 * Free an mbuf, and all its segments in case of chained buffers. Each
750 * segment is added back into its original mempool.
753 * The packet mbuf to be freed.
755 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
757 struct rte_mbuf *m_next;
759 __rte_mbuf_sanity_check(m, 1);
763 rte_pktmbuf_free_seg(m);
768 #ifdef RTE_MBUF_REFCNT
771 * Creates a "clone" of the given packet mbuf.
773 * Walks through all segments of the given packet mbuf, and for each of them:
774 * - Creates a new packet mbuf from the given pool.
775 * - Attaches newly created mbuf to the segment.
776 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
777 * from the original packet mbuf.
780 * The packet mbuf to be cloned.
782 * The mempool from which the "clone" mbufs are allocated.
784 * - The pointer to the new "clone" mbuf on success.
785 * - NULL if allocation fails.
787 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
788 struct rte_mempool *mp)
790 struct rte_mbuf *mc, *mi, **prev;
794 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
799 pktlen = md->pkt_len;
804 rte_pktmbuf_attach(mi, md);
807 } while ((md = md->next) != NULL &&
808 (mi = rte_pktmbuf_alloc(mp)) != NULL);
812 mc->pkt_len = pktlen;
814 /* Allocation of new indirect segment failed */
815 if (unlikely (mi == NULL)) {
816 rte_pktmbuf_free(mc);
820 __rte_mbuf_sanity_check(mc, 1);
825 * Adds given value to the refcnt of all packet mbuf segments.
827 * Walks through all segments of given packet mbuf and for each of them
828 * invokes rte_mbuf_refcnt_update().
831 * The packet mbuf whose refcnt to be updated.
833 * The value to add to the mbuf's segments refcnt.
835 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
837 __rte_mbuf_sanity_check(m, 1);
840 rte_mbuf_refcnt_update(m, v);
841 } while ((m = m->next) != NULL);
844 #endif /* RTE_MBUF_REFCNT */
847 * Get the headroom in a packet mbuf.
852 * The length of the headroom.
854 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
856 __rte_mbuf_sanity_check(m, 1);
861 * Get the tailroom of a packet mbuf.
866 * The length of the tailroom.
868 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
870 __rte_mbuf_sanity_check(m, 1);
871 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
876 * Get the last segment of the packet.
881 * The last segment of the given mbuf.
883 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
885 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
887 __rte_mbuf_sanity_check(m, 1);
888 while (m2->next != NULL)
894 * A macro that points to the start of the data in the mbuf.
896 * The returned pointer is cast to type t. Before using this
897 * function, the user must ensure that m_headlen(m) is large enough to
903 * The type to cast the result into.
905 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
908 * A macro that returns the length of the packet.
910 * The value can be read or assigned.
915 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
918 * A macro that returns the length of the segment.
920 * The value can be read or assigned.
925 #define rte_pktmbuf_data_len(m) ((m)->data_len)
928 * Prepend len bytes to an mbuf data area.
930 * Returns a pointer to the new
931 * data start address. If there is not enough headroom in the first
932 * segment, the function will return NULL, without modifying the mbuf.
937 * The amount of data to prepend (in bytes).
939 * A pointer to the start of the newly prepended data, or
940 * NULL if there is not enough headroom space in the first segment
942 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
945 __rte_mbuf_sanity_check(m, 1);
947 if (unlikely(len > rte_pktmbuf_headroom(m)))
951 m->data_len = (uint16_t)(m->data_len + len);
952 m->pkt_len = (m->pkt_len + len);
954 return (char *)m->buf_addr + m->data_off;
958 * Append len bytes to an mbuf.
960 * Append len bytes to an mbuf and return a pointer to the start address
961 * of the added data. If there is not enough tailroom in the last
962 * segment, the function will return NULL, without modifying the mbuf.
967 * The amount of data to append (in bytes).
969 * A pointer to the start of the newly appended data, or
970 * NULL if there is not enough tailroom space in the last segment
972 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
975 struct rte_mbuf *m_last;
977 __rte_mbuf_sanity_check(m, 1);
979 m_last = rte_pktmbuf_lastseg(m);
980 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
983 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
984 m_last->data_len = (uint16_t)(m_last->data_len + len);
985 m->pkt_len = (m->pkt_len + len);
990 * Remove len bytes at the beginning of an mbuf.
992 * Returns a pointer to the start address of the new data area. If the
993 * length is greater than the length of the first segment, then the
994 * function will fail and return NULL, without modifying the mbuf.
999 * The amount of data to remove (in bytes).
1001 * A pointer to the new start of the data.
1003 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1005 __rte_mbuf_sanity_check(m, 1);
1007 if (unlikely(len > m->data_len))
1010 m->data_len = (uint16_t)(m->data_len - len);
1012 m->pkt_len = (m->pkt_len - len);
1013 return (char *)m->buf_addr + m->data_off;
1017 * Remove len bytes of data at the end of the mbuf.
1019 * If the length is greater than the length of the last segment, the
1020 * function will fail and return -1 without modifying the mbuf.
1025 * The amount of data to remove (in bytes).
1030 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1032 struct rte_mbuf *m_last;
1034 __rte_mbuf_sanity_check(m, 1);
1036 m_last = rte_pktmbuf_lastseg(m);
1037 if (unlikely(len > m_last->data_len))
1040 m_last->data_len = (uint16_t)(m_last->data_len - len);
1041 m->pkt_len = (m->pkt_len - len);
1046 * Test if mbuf data is contiguous.
1051 * - 1, if all data is contiguous (one segment).
1052 * - 0, if there is several segments.
1054 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1056 __rte_mbuf_sanity_check(m, 1);
1057 return !!(m->nb_segs == 1);
1061 * Dump an mbuf structure to the console.
1063 * Dump all fields for the given packet mbuf and all its associated
1064 * segments (in the case of a chained buffer).
1067 * A pointer to a file for output
1071 * If dump_len != 0, also dump the "dump_len" first data bytes of
1074 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1080 #endif /* _RTE_MBUF_H_ */