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 mbufs, manipulate
47 * control message buffer (ctrlmbuf), which are generic message
48 * buffers, and packet buffers (pktmbuf), which are used to carry
51 * To understand the concepts of packet buffers or mbufs, you
52 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
53 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
54 * http://www.kohala.com/start/tcpipiv2.html
56 * The main modification of this implementation is the use of mbuf for
57 * transports other than packets. mbufs can have other types.
61 #include <rte_mempool.h>
62 #include <rte_atomic.h>
63 #include <rte_prefetch.h>
64 #include <rte_branch_prediction.h>
71 * A control message buffer.
74 void *data; /**< Pointer to data. */
75 uint32_t data_len; /**< Length of data. */
80 * Packet Offload Features Flags. It also carry packet type information.
81 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
83 #define PKT_RX_VLAN_PKT 0x0001 /**< RX packet is a 802.1q VLAN packet. */
84 #define PKT_RX_RSS_HASH 0x0002 /**< RX packet with RSS hash result. */
85 #define PKT_RX_FDIR 0x0004 /**< RX packet with FDIR infos. */
86 #define PKT_RX_L4_CKSUM_BAD 0x0008 /**< L4 cksum of RX pkt. is not OK. */
87 #define PKT_RX_IP_CKSUM_BAD 0x0010 /**< IP cksum of RX pkt. is not OK. */
88 #define PKT_RX_IPV4_HDR 0x0020 /**< RX packet with IPv4 header. */
89 #define PKT_RX_IPV4_HDR_EXT 0x0040 /**< RX packet with extended IPv4 header. */
90 #define PKT_RX_IPV6_HDR 0x0080 /**< RX packet with IPv6 header. */
91 #define PKT_RX_IPV6_HDR_EXT 0x0100 /**< RX packet with extended IPv6 header. */
92 #define PKT_RX_IEEE1588_PTP 0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
93 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
95 #define PKT_TX_VLAN_PKT 0x0800 /**< TX packet is a 802.1q VLAN packet. */
96 #define PKT_TX_IP_CKSUM 0x1000 /**< IP cksum of TX pkt. computed by NIC. */
98 * Bit 14~13 used for L4 packet type with checksum enabled.
104 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
105 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
106 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
107 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
108 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
110 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
113 * Bit Mask to indicate what bits required for building TX context
115 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
117 /** Offload features */
118 union rte_vlan_macip {
121 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
122 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
124 /**< VLAN Tag Control Identifier (CPU order). */
129 * Compare mask for vlan_macip_len.data,
130 * should be in sync with rte_vlan_macip.f layout.
132 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
133 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
134 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
135 /**< MAC+IP length. */
136 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
139 * A packet message buffer.
142 /* valid for any segment */
143 struct rte_mbuf *next; /**< Next segment of scattered packet. */
144 void* data; /**< Start address of data in segment buffer. */
145 uint16_t data_len; /**< Amount of data in segment buffer. */
147 /* these fields are valid for first segment only */
148 uint8_t nb_segs; /**< Number of segments. */
149 uint8_t in_port; /**< Input port. */
150 uint32_t pkt_len; /**< Total pkt len: sum of all segment data_len. */
152 /* offload features */
153 union rte_vlan_macip vlan_macip;
155 uint32_t rss; /**< RSS hash result if RSS enabled */
159 } fdir; /**< Filter identifier if FDIR enabled */
160 uint32_t sched; /**< Hierarchical scheduler */
161 } hash; /**< hash information */
165 * This enum indicates the mbuf type.
168 RTE_MBUF_CTRL, /**< Control mbuf. */
169 RTE_MBUF_PKT, /**< Packet mbuf. */
173 * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
176 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
177 void *buf_addr; /**< Virtual address of segment buffer. */
178 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
179 uint16_t buf_len; /**< Length of segment buffer. */
180 #ifdef RTE_MBUF_SCATTER_GATHER
182 * 16-bit Reference counter.
183 * It should only be accessed using the following functions:
184 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
185 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
186 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
190 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
191 uint16_t refcnt; /**< Non-atomically accessed refcnt */
194 uint16_t refcnt_reserved; /**< Do not use this field */
196 uint8_t type; /**< Type of mbuf. */
197 uint8_t reserved; /**< Unused field. Required for padding. */
198 uint16_t ol_flags; /**< Offload features. */
201 struct rte_ctrlmbuf ctrl;
202 struct rte_pktmbuf pkt;
207 uint16_t metadata16[0];
208 uint32_t metadata32[0];
209 uint64_t metadata64[0];
211 } __rte_cache_aligned;
213 #define RTE_MBUF_METADATA_UINT8(mbuf, offset) \
214 (mbuf->metadata[offset])
215 #define RTE_MBUF_METADATA_UINT16(mbuf, offset) \
216 (mbuf->metadata16[offset/sizeof(uint16_t)])
217 #define RTE_MBUF_METADATA_UINT32(mbuf, offset) \
218 (mbuf->metadata32[offset/sizeof(uint32_t)])
219 #define RTE_MBUF_METADATA_UINT64(mbuf, offset) \
220 (mbuf->metadata64[offset/sizeof(uint64_t)])
222 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset) \
223 (&mbuf->metadata[offset])
224 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset) \
225 (&mbuf->metadata16[offset/sizeof(uint16_t)])
226 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset) \
227 (&mbuf->metadata32[offset/sizeof(uint32_t)])
228 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset) \
229 (&mbuf->metadata64[offset/sizeof(uint64_t)])
232 * Given the buf_addr returns the pointer to corresponding mbuf.
234 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
237 * Given the pointer to mbuf returns an address where it's buf_addr
240 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
243 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
245 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
248 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
250 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
254 * Private data in case of pktmbuf pool.
256 * A structure that contains some pktmbuf_pool-specific data that are
257 * appended after the mempool structure (in private data).
259 struct rte_pktmbuf_pool_private {
260 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
263 #ifdef RTE_LIBRTE_MBUF_DEBUG
265 /** check mbuf type in debug mode */
266 #define __rte_mbuf_sanity_check(m, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
268 /** check mbuf type in debug mode if mbuf pointer is not null */
269 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { \
271 rte_mbuf_sanity_check(m, t, is_h); \
274 /** MBUF asserts in debug mode */
275 #define RTE_MBUF_ASSERT(exp) \
277 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
280 #else /* RTE_LIBRTE_MBUF_DEBUG */
282 /** check mbuf type in debug mode */
283 #define __rte_mbuf_sanity_check(m, t, is_h) do { } while(0)
285 /** check mbuf type in debug mode if mbuf pointer is not null */
286 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { } while(0)
288 /** MBUF asserts in debug mode */
289 #define RTE_MBUF_ASSERT(exp) do { } while(0)
291 #endif /* RTE_LIBRTE_MBUF_DEBUG */
293 #ifdef RTE_MBUF_SCATTER_GATHER
294 #ifdef RTE_MBUF_REFCNT_ATOMIC
297 * Adds given value to an mbuf's refcnt and returns its new value.
301 * Value to add/subtract
305 static inline uint16_t
306 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
308 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
312 * Reads the value of an mbuf's refcnt.
316 * Reference count number.
318 static inline uint16_t
319 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
321 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
325 * Sets an mbuf's refcnt to a defined value.
332 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
334 rte_atomic16_set(&m->refcnt_atomic, new_value);
337 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
340 * Adds given value to an mbuf's refcnt and returns its new value.
342 static inline uint16_t
343 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
345 m->refcnt = (uint16_t)(m->refcnt + value);
350 * Reads the value of an mbuf's refcnt.
352 static inline uint16_t
353 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
359 * Sets an mbuf's refcnt to the defined value.
362 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
364 m->refcnt = new_value;
367 #endif /* RTE_MBUF_REFCNT_ATOMIC */
370 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
375 #else /* ! RTE_MBUF_SCATTER_GATHER */
378 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
380 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
382 #endif /* RTE_MBUF_SCATTER_GATHER */
386 * Sanity checks on an mbuf.
388 * Check the consistency of the given mbuf. The function will cause a
389 * panic if corruption is detected.
392 * The mbuf to be checked.
394 * The expected type of the mbuf.
396 * True if the mbuf is a packet header, false if it is a sub-segment
397 * of a packet (in this case, some fields like nb_segs are not checked)
400 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
404 * @internal Allocate a new mbuf from mempool *mp*.
405 * The use of that function is reserved for RTE internal needs.
406 * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
409 * The mempool from which mbuf is allocated.
411 * - The pointer to the new mbuf on success.
412 * - NULL if allocation failed.
414 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
418 if (rte_mempool_get(mp, &mb) < 0)
420 m = (struct rte_mbuf *)mb;
421 #ifdef RTE_MBUF_SCATTER_GATHER
422 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
423 rte_mbuf_refcnt_set(m, 1);
424 #endif /* RTE_MBUF_SCATTER_GATHER */
429 * @internal Put mbuf back into its original mempool.
430 * The use of that function is reserved for RTE internal needs.
431 * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
434 * The mbuf to be freed.
436 static inline void __attribute__((always_inline))
437 __rte_mbuf_raw_free(struct rte_mbuf *m)
439 #ifdef RTE_MBUF_SCATTER_GATHER
440 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
441 #endif /* RTE_MBUF_SCATTER_GATHER */
442 rte_mempool_put(m->pool, m);
445 /* Operations on ctrl mbuf */
448 * The control mbuf constructor.
450 * This function initializes some fields in an mbuf structure that are
451 * not modified by the user once created (mbuf type, origin pool, buffer
452 * start address, and so on). This function is given as a callback function
453 * to rte_mempool_create() at pool creation time.
456 * The mempool from which the mbuf is allocated.
458 * A pointer that can be used by the user to retrieve useful information
459 * for mbuf initialization. This pointer comes from the ``init_arg``
460 * parameter of rte_mempool_create().
462 * The mbuf to initialize.
464 * The index of the mbuf in the pool table.
466 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
467 void *m, unsigned i);
470 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
472 * This new mbuf is initialized with data pointing to the beginning of
473 * buffer, and with a length of zero.
476 * The mempool from which the mbuf is allocated.
478 * - The pointer to the new mbuf on success.
479 * - NULL if allocation failed.
481 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
484 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
485 m->ctrl.data = m->buf_addr;
486 m->ctrl.data_len = 0;
487 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
493 * Free a control mbuf back into its original mempool.
496 * The control mbuf to be freed.
498 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
500 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
501 #ifdef RTE_MBUF_SCATTER_GATHER
502 if (rte_mbuf_refcnt_update(m, -1) == 0)
503 #endif /* RTE_MBUF_SCATTER_GATHER */
504 __rte_mbuf_raw_free(m);
508 * A macro that returns the pointer to the carried data.
510 * The value that can be read or assigned.
515 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
518 * A macro that returns the length of the carried data.
520 * The value that can be read or assigned.
525 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
527 /* Operations on pkt mbuf */
530 * The packet mbuf constructor.
532 * This function initializes some fields in the mbuf structure that are not
533 * modified by the user once created (mbuf type, origin pool, buffer start
534 * address, and so on). This function is given as a callback function to
535 * rte_mempool_create() at pool creation time.
538 * The mempool from which mbufs originate.
540 * A pointer that can be used by the user to retrieve useful information
541 * for mbuf initialization. This pointer comes from the ``init_arg``
542 * parameter of rte_mempool_create().
544 * The mbuf to initialize.
546 * The index of the mbuf in the pool table.
548 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
549 void *m, unsigned i);
553 * A packet mbuf pool constructor.
555 * This function initializes the mempool private data in the case of a
556 * pktmbuf pool. This private data is needed by the driver. The
557 * function is given as a callback function to rte_mempool_create() at
558 * pool creation. It can be extended by the user, for example, to
559 * provide another packet size.
562 * The mempool from which mbufs originate.
564 * A pointer that can be used by the user to retrieve useful information
565 * for mbuf initialization. This pointer comes from the ``init_arg``
566 * parameter of rte_mempool_create().
568 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
571 * Reset the fields of a packet mbuf to their default values.
573 * The given mbuf must have only one segment.
576 * The packet mbuf to be resetted.
578 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
584 m->pkt.vlan_macip.data = 0;
586 m->pkt.in_port = 0xff;
589 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
590 RTE_PKTMBUF_HEADROOM : m->buf_len;
591 m->pkt.data = (char*) m->buf_addr + buf_ofs;
594 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
598 * Allocate a new mbuf (type is pkt) from a mempool.
600 * This new mbuf contains one segment, which has a length of 0. The pointer
601 * to data is initialized to have some bytes of headroom in the buffer
602 * (if buffer size allows).
605 * The mempool from which the mbuf is allocated.
607 * - The pointer to the new mbuf on success.
608 * - NULL if allocation failed.
610 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
613 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
614 rte_pktmbuf_reset(m);
618 #ifdef RTE_MBUF_SCATTER_GATHER
621 * Attach packet mbuf to another packet mbuf.
622 * After attachment we refer the mbuf we attached as 'indirect',
623 * while mbuf we attached to as 'direct'.
624 * Right now, not supported:
625 * - attachment to indirect mbuf (e.g. - md has to be direct).
626 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
627 * - mbuf we trying to attach (mi) is used by someone else
628 * e.g. it's reference counter is greater then 1.
631 * The indirect packet mbuf.
633 * The direct packet mbuf.
636 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
638 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
639 RTE_MBUF_DIRECT(mi) &&
640 rte_mbuf_refcnt_read(mi) == 1);
642 rte_mbuf_refcnt_update(md, 1);
643 mi->buf_physaddr = md->buf_physaddr;
644 mi->buf_addr = md->buf_addr;
645 mi->buf_len = md->buf_len;
650 mi->pkt.pkt_len = mi->pkt.data_len;
652 mi->ol_flags = md->ol_flags;
654 __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
655 __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
659 * Detach an indirect packet mbuf -
660 * - restore original mbuf address and length values.
661 * - reset pktmbuf data and data_len to their default values.
662 * All other fields of the given packet mbuf will be left intact.
665 * The indirect attached packet mbuf.
668 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
670 const struct rte_mempool *mp = m->pool;
671 void *buf = RTE_MBUF_TO_BADDR(m);
673 uint32_t buf_len = mp->elt_size - sizeof(*m);
674 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
677 m->buf_len = (uint16_t)buf_len;
679 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
680 RTE_PKTMBUF_HEADROOM : m->buf_len;
681 m->pkt.data = (char*) m->buf_addr + buf_ofs;
686 #endif /* RTE_MBUF_SCATTER_GATHER */
689 static inline struct rte_mbuf* __attribute__((always_inline))
690 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
692 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
694 #ifdef RTE_MBUF_SCATTER_GATHER
695 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
696 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
697 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
699 rte_mbuf_refcnt_set(m, 0);
701 /* if this is an indirect mbuf, then
703 * - free attached mbuf segment
705 if (unlikely (md != m)) {
706 rte_pktmbuf_detach(m);
707 if (rte_mbuf_refcnt_update(md, -1) == 0)
708 __rte_mbuf_raw_free(md);
712 #ifdef RTE_MBUF_SCATTER_GATHER
719 * Free a segment of a packet mbuf into its original mempool.
721 * Free an mbuf, without parsing other segments in case of chained
725 * The packet mbuf segment to be freed.
727 static inline void __attribute__((always_inline))
728 rte_pktmbuf_free_seg(struct rte_mbuf *m)
730 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
731 __rte_mbuf_raw_free(m);
735 * Free a packet mbuf back into its original mempool.
737 * Free an mbuf, and all its segments in case of chained buffers. Each
738 * segment is added back into its original mempool.
741 * The packet mbuf to be freed.
743 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
745 struct rte_mbuf *m_next;
747 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
750 m_next = m->pkt.next;
751 rte_pktmbuf_free_seg(m);
756 #ifdef RTE_MBUF_SCATTER_GATHER
759 * Creates a "clone" of the given packet mbuf.
761 * Walks through all segments of the given packet mbuf, and for each of them:
762 * - Creates a new packet mbuf from the given pool.
763 * - Attaches newly created mbuf to the segment.
764 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
765 * from the original packet mbuf.
768 * The packet mbuf to be cloned.
770 * The mempool from which the "clone" mbufs are allocated.
772 * - The pointer to the new "clone" mbuf on success.
773 * - NULL if allocation fails.
775 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
776 struct rte_mempool *mp)
778 struct rte_mbuf *mc, *mi, **prev;
782 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
786 prev = &mi->pkt.next;
787 pktlen = md->pkt.pkt_len;
792 rte_pktmbuf_attach(mi, md);
794 prev = &mi->pkt.next;
795 } while ((md = md->pkt.next) != NULL &&
796 (mi = rte_pktmbuf_alloc(mp)) != NULL);
799 mc->pkt.nb_segs = nseg;
800 mc->pkt.pkt_len = pktlen;
802 /* Allocation of new indirect segment failed */
803 if (unlikely (mi == NULL)) {
804 rte_pktmbuf_free(mc);
808 __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
813 * Adds given value to the refcnt of all packet mbuf segments.
815 * Walks through all segments of given packet mbuf and for each of them
816 * invokes rte_mbuf_refcnt_update().
819 * The packet mbuf whose refcnt to be updated.
821 * The value to add to the mbuf's segments refcnt.
823 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
825 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
828 rte_mbuf_refcnt_update(m, v);
829 } while ((m = m->pkt.next) != NULL);
832 #endif /* RTE_MBUF_SCATTER_GATHER */
835 * Get the headroom in a packet mbuf.
840 * The length of the headroom.
842 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
844 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
845 return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
849 * Get the tailroom of a packet mbuf.
854 * The length of the tailroom.
856 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
858 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
859 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
864 * Get the last segment of the packet.
869 * The last segment of the given mbuf.
871 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
873 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
875 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
876 while (m2->pkt.next != NULL)
882 * A macro that points to the start of the data in the mbuf.
884 * The returned pointer is cast to type t. Before using this
885 * function, the user must ensure that m_headlen(m) is large enough to
891 * The type to cast the result into.
893 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
896 * A macro that returns the length of the packet.
898 * The value can be read or assigned.
903 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
906 * A macro that returns the length of the segment.
908 * The value can be read or assigned.
913 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
916 * Prepend len bytes to an mbuf data area.
918 * Returns a pointer to the new
919 * data start address. If there is not enough headroom in the first
920 * segment, the function will return NULL, without modifying the mbuf.
925 * The amount of data to prepend (in bytes).
927 * A pointer to the start of the newly prepended data, or
928 * NULL if there is not enough headroom space in the first segment
930 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
933 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
935 if (unlikely(len > rte_pktmbuf_headroom(m)))
938 m->pkt.data = (char*) m->pkt.data - len;
939 m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
940 m->pkt.pkt_len = (m->pkt.pkt_len + len);
942 return (char*) m->pkt.data;
946 * Append len bytes to an mbuf.
948 * Append len bytes to an mbuf and return a pointer to the start address
949 * of the added data. If there is not enough tailroom in the last
950 * segment, the function will return NULL, without modifying the mbuf.
955 * The amount of data to append (in bytes).
957 * A pointer to the start of the newly appended data, or
958 * NULL if there is not enough tailroom space in the last segment
960 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
963 struct rte_mbuf *m_last;
965 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
967 m_last = rte_pktmbuf_lastseg(m);
968 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
971 tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
972 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
973 m->pkt.pkt_len = (m->pkt.pkt_len + len);
978 * Remove len bytes at the beginning of an mbuf.
980 * Returns a pointer to the start address of the new data area. If the
981 * length is greater than the length of the first segment, then the
982 * function will fail and return NULL, without modifying the mbuf.
987 * The amount of data to remove (in bytes).
989 * A pointer to the new start of the data.
991 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
993 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
995 if (unlikely(len > m->pkt.data_len))
998 m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
999 m->pkt.data = ((char*) m->pkt.data + len);
1000 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1001 return (char*) m->pkt.data;
1005 * Remove len bytes of data at the end of the mbuf.
1007 * If the length is greater than the length of the last segment, the
1008 * function will fail and return -1 without modifying the mbuf.
1013 * The amount of data to remove (in bytes).
1018 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1020 struct rte_mbuf *m_last;
1022 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1024 m_last = rte_pktmbuf_lastseg(m);
1025 if (unlikely(len > m_last->pkt.data_len))
1028 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
1029 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1034 * Test if mbuf data is contiguous.
1039 * - 1, if all data is contiguous (one segment).
1040 * - 0, if there is several segments.
1042 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1044 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1045 return !!(m->pkt.nb_segs == 1);
1049 * Dump an mbuf structure to the console.
1051 * Dump all fields for the given packet mbuf and all its associated
1052 * segments (in the case of a chained buffer).
1055 * A pointer to a file for output
1059 * If dump_len != 0, also dump the "dump_len" first data bytes of
1062 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1068 #endif /* _RTE_MBUF_H_ */