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 #define PKT_RX_VLAN_PKT 0x0001 /**< RX packet is a 802.1q VLAN packet. */
73 #define PKT_RX_RSS_HASH 0x0002 /**< RX packet with RSS hash result. */
74 #define PKT_RX_FDIR 0x0004 /**< RX packet with FDIR infos. */
75 #define PKT_RX_L4_CKSUM_BAD 0x0008 /**< L4 cksum of RX pkt. is not OK. */
76 #define PKT_RX_IP_CKSUM_BAD 0x0010 /**< IP cksum of RX pkt. is not OK. */
77 #define PKT_RX_EIP_CKSUM_BAD 0x0000 /**< External IP header checksum error. */
78 #define PKT_RX_OVERSIZE 0x0000 /**< Num of desc of an RX pkt oversize. */
79 #define PKT_RX_HBUF_OVERFLOW 0x0000 /**< Header buffer overflow. */
80 #define PKT_RX_RECIP_ERR 0x0000 /**< Hardware processing error. */
81 #define PKT_RX_MAC_ERR 0x0000 /**< MAC error. */
82 #define PKT_RX_IPV4_HDR 0x0020 /**< RX packet with IPv4 header. */
83 #define PKT_RX_IPV4_HDR_EXT 0x0040 /**< RX packet with extended IPv4 header. */
84 #define PKT_RX_IPV6_HDR 0x0080 /**< RX packet with IPv6 header. */
85 #define PKT_RX_IPV6_HDR_EXT 0x0100 /**< RX packet with extended IPv6 header. */
86 #define PKT_RX_IEEE1588_PTP 0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
87 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
89 #define PKT_TX_VLAN_PKT 0x0800 /**< TX packet is a 802.1q VLAN packet. */
90 #define PKT_TX_IP_CKSUM 0x1000 /**< IP cksum of TX pkt. computed by NIC. */
91 #define PKT_TX_IPV4_CSUM 0x1000 /**< Alias of PKT_TX_IP_CKSUM. */
92 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
93 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR /**< IPv6 packet */
96 * Bit 14~13 used for L4 packet type with checksum enabled.
102 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
103 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
104 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
105 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
106 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
108 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
110 /* Use final bit of flags to indicate a control mbuf */
111 #define CTRL_MBUF_FLAG (1ULL << 63)
114 * Bit Mask to indicate what bits required for building TX context
116 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
119 * The generic rte_mbuf, containing a packet mbuf.
122 void *buf_addr; /**< Virtual address of segment buffer. */
123 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
125 /* next 8 bytes are initialised on RX descriptor rearm */
126 uint16_t buf_len; /**< Length of segment buffer. */
129 #ifdef RTE_MBUF_REFCNT
131 * 16-bit Reference counter.
132 * It should only be accessed using the following functions:
133 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
134 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
135 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
139 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
140 uint16_t refcnt; /**< Non-atomically accessed refcnt */
143 uint16_t refcnt_reserved; /**< Do not use this field */
145 uint8_t nb_segs; /**< Number of segments. */
146 uint8_t port; /**< Input port. */
148 uint64_t ol_flags; /**< Offload features. */
150 /* remaining bytes are set on RX when pulling packet from descriptor */
151 uint16_t reserved2; /**< Unused field. Required for padding */
152 uint16_t data_len; /**< Amount of data in segment buffer. */
153 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
155 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
157 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
158 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
161 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order). */
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 */
171 /* fields only used in slow path or on TX */
172 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
173 struct rte_mbuf *next; /**< Next segment of scattered packet. */
177 uint16_t metadata16[0];
178 uint32_t metadata32[0];
179 uint64_t metadata64[0];
180 } __rte_cache_aligned;
181 } __rte_cache_aligned;
183 #define RTE_MBUF_METADATA_UINT8(mbuf, offset) \
184 (mbuf->metadata[offset])
185 #define RTE_MBUF_METADATA_UINT16(mbuf, offset) \
186 (mbuf->metadata16[offset/sizeof(uint16_t)])
187 #define RTE_MBUF_METADATA_UINT32(mbuf, offset) \
188 (mbuf->metadata32[offset/sizeof(uint32_t)])
189 #define RTE_MBUF_METADATA_UINT64(mbuf, offset) \
190 (mbuf->metadata64[offset/sizeof(uint64_t)])
192 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset) \
193 (&mbuf->metadata[offset])
194 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset) \
195 (&mbuf->metadata16[offset/sizeof(uint16_t)])
196 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset) \
197 (&mbuf->metadata32[offset/sizeof(uint32_t)])
198 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset) \
199 (&mbuf->metadata64[offset/sizeof(uint64_t)])
202 * Given the buf_addr returns the pointer to corresponding mbuf.
204 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
207 * Given the pointer to mbuf returns an address where it's buf_addr
210 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
213 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
215 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
218 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
220 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
224 * Private data in case of pktmbuf pool.
226 * A structure that contains some pktmbuf_pool-specific data that are
227 * appended after the mempool structure (in private data).
229 struct rte_pktmbuf_pool_private {
230 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
233 #ifdef RTE_LIBRTE_MBUF_DEBUG
235 /** check mbuf type in debug mode */
236 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
238 /** check mbuf type in debug mode if mbuf pointer is not null */
239 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
241 rte_mbuf_sanity_check(m, is_h); \
244 /** MBUF asserts in debug mode */
245 #define RTE_MBUF_ASSERT(exp) \
247 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
250 #else /* RTE_LIBRTE_MBUF_DEBUG */
252 /** check mbuf type in debug mode */
253 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
255 /** check mbuf type in debug mode if mbuf pointer is not null */
256 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
258 /** MBUF asserts in debug mode */
259 #define RTE_MBUF_ASSERT(exp) do { } while (0)
261 #endif /* RTE_LIBRTE_MBUF_DEBUG */
263 #ifdef RTE_MBUF_REFCNT
264 #ifdef RTE_MBUF_REFCNT_ATOMIC
267 * Adds given value to an mbuf's refcnt and returns its new value.
271 * Value to add/subtract
275 static inline uint16_t
276 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
278 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
282 * Reads the value of an mbuf's refcnt.
286 * Reference count number.
288 static inline uint16_t
289 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
291 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
295 * Sets an mbuf's refcnt to a defined value.
302 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
304 rte_atomic16_set(&m->refcnt_atomic, new_value);
307 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
310 * Adds given value to an mbuf's refcnt and returns its new value.
312 static inline uint16_t
313 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
315 m->refcnt = (uint16_t)(m->refcnt + value);
320 * Reads the value of an mbuf's refcnt.
322 static inline uint16_t
323 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
329 * Sets an mbuf's refcnt to the defined value.
332 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
334 m->refcnt = new_value;
337 #endif /* RTE_MBUF_REFCNT_ATOMIC */
340 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
345 #else /* ! RTE_MBUF_REFCNT */
348 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
350 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
352 #endif /* RTE_MBUF_REFCNT */
356 * Sanity checks on an mbuf.
358 * Check the consistency of the given mbuf. The function will cause a
359 * panic if corruption is detected.
362 * The mbuf to be checked.
364 * True if the mbuf is a packet header, false if it is a sub-segment
365 * of a packet (in this case, some fields like nb_segs are not checked)
368 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
371 * @internal Allocate a new mbuf from mempool *mp*.
372 * The use of that function is reserved for RTE internal needs.
373 * Please use rte_pktmbuf_alloc().
376 * The mempool from which mbuf is allocated.
378 * - The pointer to the new mbuf on success.
379 * - NULL if allocation failed.
381 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
385 if (rte_mempool_get(mp, &mb) < 0)
387 m = (struct rte_mbuf *)mb;
388 #ifdef RTE_MBUF_REFCNT
389 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
390 rte_mbuf_refcnt_set(m, 1);
391 #endif /* RTE_MBUF_REFCNT */
396 * @internal Put mbuf back into its original mempool.
397 * The use of that function is reserved for RTE internal needs.
398 * Please use rte_pktmbuf_free().
401 * The mbuf to be freed.
403 static inline void __attribute__((always_inline))
404 __rte_mbuf_raw_free(struct rte_mbuf *m)
406 #ifdef RTE_MBUF_REFCNT
407 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
408 #endif /* RTE_MBUF_REFCNT */
409 rte_mempool_put(m->pool, m);
412 /* Operations on ctrl mbuf */
415 * The control mbuf constructor.
417 * This function initializes some fields in an mbuf structure that are
418 * not modified by the user once created (mbuf type, origin pool, buffer
419 * start address, and so on). This function is given as a callback function
420 * to rte_mempool_create() at pool creation time.
423 * The mempool from which the mbuf is allocated.
425 * A pointer that can be used by the user to retrieve useful information
426 * for mbuf initialization. This pointer comes from the ``init_arg``
427 * parameter of rte_mempool_create().
429 * The mbuf to initialize.
431 * The index of the mbuf in the pool table.
433 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
434 void *m, unsigned i);
437 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
439 * This new mbuf is initialized with data pointing to the beginning of
440 * buffer, and with a length of zero.
443 * The mempool from which the mbuf is allocated.
445 * - The pointer to the new mbuf on success.
446 * - NULL if allocation failed.
448 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
451 * Free a control mbuf back into its original mempool.
454 * The control mbuf to be freed.
456 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
459 * A macro that returns the pointer to the carried data.
461 * The value that can be read or assigned.
466 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
469 * A macro that returns the length of the carried data.
471 * The value that can be read or assigned.
476 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
479 * Tests if an mbuf is a control mbuf
482 * The mbuf to be tested
484 * - True (1) if the mbuf is a control mbuf
485 * - False(0) otherwise
488 rte_is_ctrlmbuf(struct rte_mbuf *m)
490 return (!!(m->ol_flags & CTRL_MBUF_FLAG));
493 /* Operations on pkt mbuf */
496 * The packet mbuf constructor.
498 * This function initializes some fields in the mbuf structure that are
499 * not modified by the user once created (origin pool, buffer start
500 * address, and so on). This function is given as a callback function to
501 * rte_mempool_create() at pool creation time.
504 * The mempool from which mbufs originate.
506 * A pointer that can be used by the user to retrieve useful information
507 * for mbuf initialization. This pointer comes from the ``init_arg``
508 * parameter of rte_mempool_create().
510 * The mbuf to initialize.
512 * The index of the mbuf in the pool table.
514 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
515 void *m, unsigned i);
519 * A packet mbuf pool constructor.
521 * This function initializes the mempool private data in the case of a
522 * pktmbuf pool. This private data is needed by the driver. The
523 * function is given as a callback function to rte_mempool_create() at
524 * pool creation. It can be extended by the user, for example, to
525 * provide another packet size.
528 * The mempool from which mbufs originate.
530 * A pointer that can be used by the user to retrieve useful information
531 * for mbuf initialization. This pointer comes from the ``init_arg``
532 * parameter of rte_mempool_create().
534 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
537 * Reset the fields of a packet mbuf to their default values.
539 * The given mbuf must have only one segment.
542 * The packet mbuf to be resetted.
544 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
554 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
555 RTE_PKTMBUF_HEADROOM : m->buf_len;
558 __rte_mbuf_sanity_check(m, 1);
562 * Allocate a new mbuf from a mempool.
564 * This new mbuf contains one segment, which has a length of 0. The pointer
565 * to data is initialized to have some bytes of headroom in the buffer
566 * (if buffer size allows).
569 * The mempool from which the mbuf is allocated.
571 * - The pointer to the new mbuf on success.
572 * - NULL if allocation failed.
574 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
577 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
578 rte_pktmbuf_reset(m);
582 #ifdef RTE_MBUF_REFCNT
585 * Attach packet mbuf to another packet mbuf.
586 * After attachment we refer the mbuf we attached as 'indirect',
587 * while mbuf we attached to as 'direct'.
588 * Right now, not supported:
589 * - attachment to indirect mbuf (e.g. - md has to be direct).
590 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
591 * - mbuf we trying to attach (mi) is used by someone else
592 * e.g. it's reference counter is greater then 1.
595 * The indirect packet mbuf.
597 * The direct packet mbuf.
600 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
602 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
603 RTE_MBUF_DIRECT(mi) &&
604 rte_mbuf_refcnt_read(mi) == 1);
606 rte_mbuf_refcnt_update(md, 1);
607 mi->buf_physaddr = md->buf_physaddr;
608 mi->buf_addr = md->buf_addr;
609 mi->buf_len = md->buf_len;
612 mi->data_off = md->data_off;
613 mi->data_len = md->data_len;
615 mi->vlan_tci = md->vlan_tci;
616 mi->l2_l3_len = md->l2_l3_len;
620 mi->pkt_len = mi->data_len;
622 mi->ol_flags = md->ol_flags;
624 __rte_mbuf_sanity_check(mi, 1);
625 __rte_mbuf_sanity_check(md, 0);
629 * Detach an indirect packet mbuf -
630 * - restore original mbuf address and length values.
631 * - reset pktmbuf data and data_len to their default values.
632 * All other fields of the given packet mbuf will be left intact.
635 * The indirect attached packet mbuf.
638 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
640 const struct rte_mempool *mp = m->pool;
641 void *buf = RTE_MBUF_TO_BADDR(m);
642 uint32_t buf_len = mp->elt_size - sizeof(*m);
643 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
646 m->buf_len = (uint16_t)buf_len;
648 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
649 RTE_PKTMBUF_HEADROOM : m->buf_len;
654 #endif /* RTE_MBUF_REFCNT */
657 static inline struct rte_mbuf* __attribute__((always_inline))
658 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
660 __rte_mbuf_sanity_check(m, 0);
662 #ifdef RTE_MBUF_REFCNT
663 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
664 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
665 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
667 rte_mbuf_refcnt_set(m, 0);
669 /* if this is an indirect mbuf, then
671 * - free attached mbuf segment
673 if (unlikely (md != m)) {
674 rte_pktmbuf_detach(m);
675 if (rte_mbuf_refcnt_update(md, -1) == 0)
676 __rte_mbuf_raw_free(md);
680 #ifdef RTE_MBUF_REFCNT
687 * Free a segment of a packet mbuf into its original mempool.
689 * Free an mbuf, without parsing other segments in case of chained
693 * The packet mbuf segment to be freed.
695 static inline void __attribute__((always_inline))
696 rte_pktmbuf_free_seg(struct rte_mbuf *m)
698 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
699 __rte_mbuf_raw_free(m);
703 * Free a packet mbuf back into its original mempool.
705 * Free an mbuf, and all its segments in case of chained buffers. Each
706 * segment is added back into its original mempool.
709 * The packet mbuf to be freed.
711 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
713 struct rte_mbuf *m_next;
715 __rte_mbuf_sanity_check(m, 1);
719 rte_pktmbuf_free_seg(m);
724 #ifdef RTE_MBUF_REFCNT
727 * Creates a "clone" of the given packet mbuf.
729 * Walks through all segments of the given packet mbuf, and for each of them:
730 * - Creates a new packet mbuf from the given pool.
731 * - Attaches newly created mbuf to the segment.
732 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
733 * from the original packet mbuf.
736 * The packet mbuf to be cloned.
738 * The mempool from which the "clone" mbufs are allocated.
740 * - The pointer to the new "clone" mbuf on success.
741 * - NULL if allocation fails.
743 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
744 struct rte_mempool *mp)
746 struct rte_mbuf *mc, *mi, **prev;
750 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
755 pktlen = md->pkt_len;
760 rte_pktmbuf_attach(mi, md);
763 } while ((md = md->next) != NULL &&
764 (mi = rte_pktmbuf_alloc(mp)) != NULL);
768 mc->pkt_len = pktlen;
770 /* Allocation of new indirect segment failed */
771 if (unlikely (mi == NULL)) {
772 rte_pktmbuf_free(mc);
776 __rte_mbuf_sanity_check(mc, 1);
781 * Adds given value to the refcnt of all packet mbuf segments.
783 * Walks through all segments of given packet mbuf and for each of them
784 * invokes rte_mbuf_refcnt_update().
787 * The packet mbuf whose refcnt to be updated.
789 * The value to add to the mbuf's segments refcnt.
791 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
793 __rte_mbuf_sanity_check(m, 1);
796 rte_mbuf_refcnt_update(m, v);
797 } while ((m = m->next) != NULL);
800 #endif /* RTE_MBUF_REFCNT */
803 * Get the headroom in a packet mbuf.
808 * The length of the headroom.
810 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
812 __rte_mbuf_sanity_check(m, 1);
817 * Get the tailroom of a packet mbuf.
822 * The length of the tailroom.
824 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
826 __rte_mbuf_sanity_check(m, 1);
827 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
832 * Get the last segment of the packet.
837 * The last segment of the given mbuf.
839 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
841 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
843 __rte_mbuf_sanity_check(m, 1);
844 while (m2->next != NULL)
850 * A macro that points to the start of the data in the mbuf.
852 * The returned pointer is cast to type t. Before using this
853 * function, the user must ensure that m_headlen(m) is large enough to
859 * The type to cast the result into.
861 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
864 * A macro that returns the length of the packet.
866 * The value can be read or assigned.
871 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
874 * A macro that returns the length of the segment.
876 * The value can be read or assigned.
881 #define rte_pktmbuf_data_len(m) ((m)->data_len)
884 * Prepend len bytes to an mbuf data area.
886 * Returns a pointer to the new
887 * data start address. If there is not enough headroom in the first
888 * segment, the function will return NULL, without modifying the mbuf.
893 * The amount of data to prepend (in bytes).
895 * A pointer to the start of the newly prepended data, or
896 * NULL if there is not enough headroom space in the first segment
898 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
901 __rte_mbuf_sanity_check(m, 1);
903 if (unlikely(len > rte_pktmbuf_headroom(m)))
907 m->data_len = (uint16_t)(m->data_len + len);
908 m->pkt_len = (m->pkt_len + len);
910 return (char *)m->buf_addr + m->data_off;
914 * Append len bytes to an mbuf.
916 * Append len bytes to an mbuf and return a pointer to the start address
917 * of the added data. If there is not enough tailroom in the last
918 * segment, the function will return NULL, without modifying the mbuf.
923 * The amount of data to append (in bytes).
925 * A pointer to the start of the newly appended data, or
926 * NULL if there is not enough tailroom space in the last segment
928 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
931 struct rte_mbuf *m_last;
933 __rte_mbuf_sanity_check(m, 1);
935 m_last = rte_pktmbuf_lastseg(m);
936 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
939 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
940 m_last->data_len = (uint16_t)(m_last->data_len + len);
941 m->pkt_len = (m->pkt_len + len);
946 * Remove len bytes at the beginning of an mbuf.
948 * Returns a pointer to the start address of the new data area. If the
949 * length is greater than the length of the first segment, then the
950 * function will fail and return NULL, without modifying the mbuf.
955 * The amount of data to remove (in bytes).
957 * A pointer to the new start of the data.
959 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
961 __rte_mbuf_sanity_check(m, 1);
963 if (unlikely(len > m->data_len))
966 m->data_len = (uint16_t)(m->data_len - len);
968 m->pkt_len = (m->pkt_len - len);
969 return (char *)m->buf_addr + m->data_off;
973 * Remove len bytes of data at the end of the mbuf.
975 * If the length is greater than the length of the last segment, the
976 * function will fail and return -1 without modifying the mbuf.
981 * The amount of data to remove (in bytes).
986 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
988 struct rte_mbuf *m_last;
990 __rte_mbuf_sanity_check(m, 1);
992 m_last = rte_pktmbuf_lastseg(m);
993 if (unlikely(len > m_last->data_len))
996 m_last->data_len = (uint16_t)(m_last->data_len - len);
997 m->pkt_len = (m->pkt_len - len);
1002 * Test if mbuf data is contiguous.
1007 * - 1, if all data is contiguous (one segment).
1008 * - 0, if there is several segments.
1010 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1012 __rte_mbuf_sanity_check(m, 1);
1013 return !!(m->nb_segs == 1);
1017 * Dump an mbuf structure to the console.
1019 * Dump all fields for the given packet mbuf and all its associated
1020 * segments (in the case of a chained buffer).
1023 * A pointer to a file for output
1027 * If dump_len != 0, also dump the "dump_len" first data bytes of
1030 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1036 #endif /* _RTE_MBUF_H_ */