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_EIP_CKSUM_BAD 0x0000 /**< External IP header checksum error. */
89 #define PKT_RX_OVERSIZE 0x0000 /**< Num of desc of an RX pkt oversize. */
90 #define PKT_RX_HBUF_OVERFLOW 0x0000 /**< Header buffer overflow. */
91 #define PKT_RX_RECIP_ERR 0x0000 /**< Hardware processing error. */
92 #define PKT_RX_MAC_ERR 0x0000 /**< MAC error. */
93 #define PKT_RX_IPV4_HDR 0x0020 /**< RX packet with IPv4 header. */
94 #define PKT_RX_IPV4_HDR_EXT 0x0040 /**< RX packet with extended IPv4 header. */
95 #define PKT_RX_IPV6_HDR 0x0080 /**< RX packet with IPv6 header. */
96 #define PKT_RX_IPV6_HDR_EXT 0x0100 /**< RX packet with extended IPv6 header. */
97 #define PKT_RX_IEEE1588_PTP 0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
98 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
100 #define PKT_TX_VLAN_PKT 0x0800 /**< TX packet is a 802.1q VLAN packet. */
101 #define PKT_TX_IP_CKSUM 0x1000 /**< IP cksum of TX pkt. computed by NIC. */
102 #define PKT_TX_IPV4_CSUM 0x1000 /**< Alias of PKT_TX_IP_CKSUM. */
103 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
104 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR /**< IPv6 packet */
106 * Bit 14~13 used for L4 packet type with checksum enabled.
112 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
113 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
114 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
115 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
116 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
118 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
121 * Bit Mask to indicate what bits required for building TX context
123 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
125 /** Offload features */
126 union rte_vlan_macip {
129 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
130 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
132 /**< VLAN Tag Control Identifier (CPU order). */
137 * Compare mask for vlan_macip_len.data,
138 * should be in sync with rte_vlan_macip.f layout.
140 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
141 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
142 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
143 /**< MAC+IP length. */
144 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
147 * A packet message buffer.
150 /* valid for any segment */
151 struct rte_mbuf *next; /**< Next segment of scattered packet. */
152 void* data; /**< Start address of data in segment buffer. */
153 uint16_t data_len; /**< Amount of data in segment buffer. */
155 /* these fields are valid for first segment only */
156 uint8_t nb_segs; /**< Number of segments. */
157 uint8_t in_port; /**< Input port. */
158 uint32_t pkt_len; /**< Total pkt len: sum of all segment data_len. */
160 /* offload features */
161 union rte_vlan_macip vlan_macip;
163 uint32_t rss; /**< RSS hash result if RSS enabled */
167 } fdir; /**< Filter identifier if FDIR enabled */
168 uint32_t sched; /**< Hierarchical scheduler */
169 } hash; /**< hash information */
173 * This enum indicates the mbuf type.
176 RTE_MBUF_CTRL, /**< Control mbuf. */
177 RTE_MBUF_PKT, /**< Packet mbuf. */
181 * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
184 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
185 void *buf_addr; /**< Virtual address of segment buffer. */
186 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
187 uint16_t buf_len; /**< Length of segment buffer. */
188 #ifdef RTE_MBUF_SCATTER_GATHER
190 * 16-bit Reference counter.
191 * It should only be accessed using the following functions:
192 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
193 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
194 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
198 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
199 uint16_t refcnt; /**< Non-atomically accessed refcnt */
202 uint16_t refcnt_reserved; /**< Do not use this field */
204 uint8_t type; /**< Type of mbuf. */
205 uint8_t reserved; /**< Unused field. Required for padding. */
206 uint16_t ol_flags; /**< Offload features. */
209 struct rte_ctrlmbuf ctrl;
210 struct rte_pktmbuf pkt;
215 uint16_t metadata16[0];
216 uint32_t metadata32[0];
217 uint64_t metadata64[0];
219 } __rte_cache_aligned;
221 #define RTE_MBUF_METADATA_UINT8(mbuf, offset) \
222 (mbuf->metadata[offset])
223 #define RTE_MBUF_METADATA_UINT16(mbuf, offset) \
224 (mbuf->metadata16[offset/sizeof(uint16_t)])
225 #define RTE_MBUF_METADATA_UINT32(mbuf, offset) \
226 (mbuf->metadata32[offset/sizeof(uint32_t)])
227 #define RTE_MBUF_METADATA_UINT64(mbuf, offset) \
228 (mbuf->metadata64[offset/sizeof(uint64_t)])
230 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset) \
231 (&mbuf->metadata[offset])
232 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset) \
233 (&mbuf->metadata16[offset/sizeof(uint16_t)])
234 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset) \
235 (&mbuf->metadata32[offset/sizeof(uint32_t)])
236 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset) \
237 (&mbuf->metadata64[offset/sizeof(uint64_t)])
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, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
276 /** check mbuf type in debug mode if mbuf pointer is not null */
277 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { \
279 rte_mbuf_sanity_check(m, t, 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, t, 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, t, 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_SCATTER_GATHER
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_SCATTER_GATHER */
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_SCATTER_GATHER */
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 * The expected type of the mbuf.
404 * True if the mbuf is a packet header, false if it is a sub-segment
405 * of a packet (in this case, some fields like nb_segs are not checked)
408 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
412 * @internal Allocate a new mbuf from mempool *mp*.
413 * The use of that function is reserved for RTE internal needs.
414 * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
417 * The mempool from which mbuf is allocated.
419 * - The pointer to the new mbuf on success.
420 * - NULL if allocation failed.
422 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
426 if (rte_mempool_get(mp, &mb) < 0)
428 m = (struct rte_mbuf *)mb;
429 #ifdef RTE_MBUF_SCATTER_GATHER
430 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
431 rte_mbuf_refcnt_set(m, 1);
432 #endif /* RTE_MBUF_SCATTER_GATHER */
437 * @internal Put mbuf back into its original mempool.
438 * The use of that function is reserved for RTE internal needs.
439 * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
442 * The mbuf to be freed.
444 static inline void __attribute__((always_inline))
445 __rte_mbuf_raw_free(struct rte_mbuf *m)
447 #ifdef RTE_MBUF_SCATTER_GATHER
448 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
449 #endif /* RTE_MBUF_SCATTER_GATHER */
450 rte_mempool_put(m->pool, m);
453 /* Operations on ctrl mbuf */
456 * The control mbuf constructor.
458 * This function initializes some fields in an mbuf structure that are
459 * not modified by the user once created (mbuf type, origin pool, buffer
460 * start address, and so on). This function is given as a callback function
461 * to rte_mempool_create() at pool creation time.
464 * The mempool from which the mbuf is allocated.
466 * A pointer that can be used by the user to retrieve useful information
467 * for mbuf initialization. This pointer comes from the ``init_arg``
468 * parameter of rte_mempool_create().
470 * The mbuf to initialize.
472 * The index of the mbuf in the pool table.
474 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
475 void *m, unsigned i);
478 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
480 * This new mbuf is initialized with data pointing to the beginning of
481 * buffer, and with a length of zero.
484 * The mempool from which the mbuf is allocated.
486 * - The pointer to the new mbuf on success.
487 * - NULL if allocation failed.
489 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
492 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
493 m->ctrl.data = m->buf_addr;
494 m->ctrl.data_len = 0;
495 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
501 * Free a control mbuf back into its original mempool.
504 * The control mbuf to be freed.
506 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
508 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
509 #ifdef RTE_MBUF_SCATTER_GATHER
510 if (rte_mbuf_refcnt_update(m, -1) == 0)
511 #endif /* RTE_MBUF_SCATTER_GATHER */
512 __rte_mbuf_raw_free(m);
516 * A macro that returns the pointer to the carried data.
518 * The value that can be read or assigned.
523 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
526 * A macro that returns the length of the carried data.
528 * The value that can be read or assigned.
533 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
535 /* Operations on pkt mbuf */
538 * The packet mbuf constructor.
540 * This function initializes some fields in the mbuf structure that are not
541 * modified by the user once created (mbuf type, origin pool, buffer start
542 * address, and so on). This function is given as a callback function to
543 * rte_mempool_create() at pool creation time.
546 * The mempool from which mbufs originate.
548 * A pointer that can be used by the user to retrieve useful information
549 * for mbuf initialization. This pointer comes from the ``init_arg``
550 * parameter of rte_mempool_create().
552 * The mbuf to initialize.
554 * The index of the mbuf in the pool table.
556 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
557 void *m, unsigned i);
561 * A packet mbuf pool constructor.
563 * This function initializes the mempool private data in the case of a
564 * pktmbuf pool. This private data is needed by the driver. The
565 * function is given as a callback function to rte_mempool_create() at
566 * pool creation. It can be extended by the user, for example, to
567 * provide another packet size.
570 * The mempool from which mbufs originate.
572 * A pointer that can be used by the user to retrieve useful information
573 * for mbuf initialization. This pointer comes from the ``init_arg``
574 * parameter of rte_mempool_create().
576 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
579 * Reset the fields of a packet mbuf to their default values.
581 * The given mbuf must have only one segment.
584 * The packet mbuf to be resetted.
586 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
592 m->pkt.vlan_macip.data = 0;
594 m->pkt.in_port = 0xff;
597 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
598 RTE_PKTMBUF_HEADROOM : m->buf_len;
599 m->pkt.data = (char*) m->buf_addr + buf_ofs;
602 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
606 * Allocate a new mbuf (type is pkt) from a mempool.
608 * This new mbuf contains one segment, which has a length of 0. The pointer
609 * to data is initialized to have some bytes of headroom in the buffer
610 * (if buffer size allows).
613 * The mempool from which the mbuf is allocated.
615 * - The pointer to the new mbuf on success.
616 * - NULL if allocation failed.
618 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
621 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
622 rte_pktmbuf_reset(m);
626 #ifdef RTE_MBUF_SCATTER_GATHER
629 * Attach packet mbuf to another packet mbuf.
630 * After attachment we refer the mbuf we attached as 'indirect',
631 * while mbuf we attached to as 'direct'.
632 * Right now, not supported:
633 * - attachment to indirect mbuf (e.g. - md has to be direct).
634 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
635 * - mbuf we trying to attach (mi) is used by someone else
636 * e.g. it's reference counter is greater then 1.
639 * The indirect packet mbuf.
641 * The direct packet mbuf.
644 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
646 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
647 RTE_MBUF_DIRECT(mi) &&
648 rte_mbuf_refcnt_read(mi) == 1);
650 rte_mbuf_refcnt_update(md, 1);
651 mi->buf_physaddr = md->buf_physaddr;
652 mi->buf_addr = md->buf_addr;
653 mi->buf_len = md->buf_len;
658 mi->pkt.pkt_len = mi->pkt.data_len;
660 mi->ol_flags = md->ol_flags;
662 __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
663 __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
667 * Detach an indirect packet mbuf -
668 * - restore original mbuf address and length values.
669 * - reset pktmbuf data and data_len to their default values.
670 * All other fields of the given packet mbuf will be left intact.
673 * The indirect attached packet mbuf.
676 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
678 const struct rte_mempool *mp = m->pool;
679 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 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
688 RTE_PKTMBUF_HEADROOM : m->buf_len;
689 m->pkt.data = (char*) m->buf_addr + buf_ofs;
694 #endif /* RTE_MBUF_SCATTER_GATHER */
697 static inline struct rte_mbuf* __attribute__((always_inline))
698 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
700 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
702 #ifdef RTE_MBUF_SCATTER_GATHER
703 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
704 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
705 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
707 rte_mbuf_refcnt_set(m, 0);
709 /* if this is an indirect mbuf, then
711 * - free attached mbuf segment
713 if (unlikely (md != m)) {
714 rte_pktmbuf_detach(m);
715 if (rte_mbuf_refcnt_update(md, -1) == 0)
716 __rte_mbuf_raw_free(md);
720 #ifdef RTE_MBUF_SCATTER_GATHER
727 * Free a segment of a packet mbuf into its original mempool.
729 * Free an mbuf, without parsing other segments in case of chained
733 * The packet mbuf segment to be freed.
735 static inline void __attribute__((always_inline))
736 rte_pktmbuf_free_seg(struct rte_mbuf *m)
738 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
739 __rte_mbuf_raw_free(m);
743 * Free a packet mbuf back into its original mempool.
745 * Free an mbuf, and all its segments in case of chained buffers. Each
746 * segment is added back into its original mempool.
749 * The packet mbuf to be freed.
751 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
753 struct rte_mbuf *m_next;
755 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
758 m_next = m->pkt.next;
759 rte_pktmbuf_free_seg(m);
764 #ifdef RTE_MBUF_SCATTER_GATHER
767 * Creates a "clone" of the given packet mbuf.
769 * Walks through all segments of the given packet mbuf, and for each of them:
770 * - Creates a new packet mbuf from the given pool.
771 * - Attaches newly created mbuf to the segment.
772 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
773 * from the original packet mbuf.
776 * The packet mbuf to be cloned.
778 * The mempool from which the "clone" mbufs are allocated.
780 * - The pointer to the new "clone" mbuf on success.
781 * - NULL if allocation fails.
783 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
784 struct rte_mempool *mp)
786 struct rte_mbuf *mc, *mi, **prev;
790 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
794 prev = &mi->pkt.next;
795 pktlen = md->pkt.pkt_len;
800 rte_pktmbuf_attach(mi, md);
802 prev = &mi->pkt.next;
803 } while ((md = md->pkt.next) != NULL &&
804 (mi = rte_pktmbuf_alloc(mp)) != NULL);
807 mc->pkt.nb_segs = nseg;
808 mc->pkt.pkt_len = pktlen;
810 /* Allocation of new indirect segment failed */
811 if (unlikely (mi == NULL)) {
812 rte_pktmbuf_free(mc);
816 __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
821 * Adds given value to the refcnt of all packet mbuf segments.
823 * Walks through all segments of given packet mbuf and for each of them
824 * invokes rte_mbuf_refcnt_update().
827 * The packet mbuf whose refcnt to be updated.
829 * The value to add to the mbuf's segments refcnt.
831 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
833 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
836 rte_mbuf_refcnt_update(m, v);
837 } while ((m = m->pkt.next) != NULL);
840 #endif /* RTE_MBUF_SCATTER_GATHER */
843 * Get the headroom in a packet mbuf.
848 * The length of the headroom.
850 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
852 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
853 return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
857 * Get the tailroom of a packet mbuf.
862 * The length of the tailroom.
864 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
866 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
867 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
872 * Get the last segment of the packet.
877 * The last segment of the given mbuf.
879 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
881 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
883 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
884 while (m2->pkt.next != NULL)
890 * A macro that points to the start of the data in the mbuf.
892 * The returned pointer is cast to type t. Before using this
893 * function, the user must ensure that m_headlen(m) is large enough to
899 * The type to cast the result into.
901 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
904 * A macro that returns the length of the packet.
906 * The value can be read or assigned.
911 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
914 * A macro that returns the length of the segment.
916 * The value can be read or assigned.
921 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
924 * Prepend len bytes to an mbuf data area.
926 * Returns a pointer to the new
927 * data start address. If there is not enough headroom in the first
928 * segment, the function will return NULL, without modifying the mbuf.
933 * The amount of data to prepend (in bytes).
935 * A pointer to the start of the newly prepended data, or
936 * NULL if there is not enough headroom space in the first segment
938 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
941 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
943 if (unlikely(len > rte_pktmbuf_headroom(m)))
946 m->pkt.data = (char*) m->pkt.data - len;
947 m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
948 m->pkt.pkt_len = (m->pkt.pkt_len + len);
950 return (char*) m->pkt.data;
954 * Append len bytes to an mbuf.
956 * Append len bytes to an mbuf and return a pointer to the start address
957 * of the added data. If there is not enough tailroom in the last
958 * segment, the function will return NULL, without modifying the mbuf.
963 * The amount of data to append (in bytes).
965 * A pointer to the start of the newly appended data, or
966 * NULL if there is not enough tailroom space in the last segment
968 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
971 struct rte_mbuf *m_last;
973 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
975 m_last = rte_pktmbuf_lastseg(m);
976 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
979 tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
980 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
981 m->pkt.pkt_len = (m->pkt.pkt_len + len);
986 * Remove len bytes at the beginning of an mbuf.
988 * Returns a pointer to the start address of the new data area. If the
989 * length is greater than the length of the first segment, then the
990 * function will fail and return NULL, without modifying the mbuf.
995 * The amount of data to remove (in bytes).
997 * A pointer to the new start of the data.
999 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1001 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1003 if (unlikely(len > m->pkt.data_len))
1006 m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
1007 m->pkt.data = ((char*) m->pkt.data + len);
1008 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1009 return (char*) m->pkt.data;
1013 * Remove len bytes of data at the end of the mbuf.
1015 * If the length is greater than the length of the last segment, the
1016 * function will fail and return -1 without modifying the mbuf.
1021 * The amount of data to remove (in bytes).
1026 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1028 struct rte_mbuf *m_last;
1030 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1032 m_last = rte_pktmbuf_lastseg(m);
1033 if (unlikely(len > m_last->pkt.data_len))
1036 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
1037 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1042 * Test if mbuf data is contiguous.
1047 * - 1, if all data is contiguous (one segment).
1048 * - 0, if there is several segments.
1050 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1052 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1053 return !!(m->pkt.nb_segs == 1);
1057 * Dump an mbuf structure to the console.
1059 * Dump all fields for the given packet mbuf and all its associated
1060 * segments (in the case of a chained buffer).
1063 * A pointer to a file for output
1067 * If dump_len != 0, also dump the "dump_len" first data bytes of
1070 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1076 #endif /* _RTE_MBUF_H_ */