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 */
95 * Bit 14~13 used for L4 packet type with checksum enabled.
101 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
102 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
103 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
104 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
105 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
107 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
110 * Bit Mask to indicate what bits required for building TX context
112 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
115 * The generic rte_mbuf, containing a packet mbuf.
118 void *buf_addr; /**< Virtual address of segment buffer. */
119 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
121 /* next 8 bytes are initialised on RX descriptor rearm */
122 uint16_t buf_len; /**< Length of segment buffer. */
125 #ifdef RTE_MBUF_REFCNT
127 * 16-bit Reference counter.
128 * It should only be accessed using the following functions:
129 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
130 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
131 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
135 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
136 uint16_t refcnt; /**< Non-atomically accessed refcnt */
139 uint16_t refcnt_reserved; /**< Do not use this field */
141 uint8_t nb_segs; /**< Number of segments. */
142 uint8_t port; /**< Input port. */
144 uint64_t ol_flags; /**< Offload features. */
146 /* remaining bytes are set on RX when pulling packet from descriptor */
147 uint16_t reserved2; /**< Unused field. Required for padding */
148 uint16_t data_len; /**< Amount of data in segment buffer. */
149 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
151 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
153 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
154 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
157 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order). */
159 uint32_t rss; /**< RSS hash result if RSS enabled */
163 } fdir; /**< Filter identifier if FDIR enabled */
164 uint32_t sched; /**< Hierarchical scheduler */
165 } hash; /**< hash information */
167 /* fields only used in slow path or on TX */
168 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
169 struct rte_mbuf *next; /**< Next segment of scattered packet. */
173 uint16_t metadata16[0];
174 uint32_t metadata32[0];
175 uint64_t metadata64[0];
176 } __rte_cache_aligned;
177 } __rte_cache_aligned;
179 #define RTE_MBUF_METADATA_UINT8(mbuf, offset) \
180 (mbuf->metadata[offset])
181 #define RTE_MBUF_METADATA_UINT16(mbuf, offset) \
182 (mbuf->metadata16[offset/sizeof(uint16_t)])
183 #define RTE_MBUF_METADATA_UINT32(mbuf, offset) \
184 (mbuf->metadata32[offset/sizeof(uint32_t)])
185 #define RTE_MBUF_METADATA_UINT64(mbuf, offset) \
186 (mbuf->metadata64[offset/sizeof(uint64_t)])
188 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset) \
189 (&mbuf->metadata[offset])
190 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset) \
191 (&mbuf->metadata16[offset/sizeof(uint16_t)])
192 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset) \
193 (&mbuf->metadata32[offset/sizeof(uint32_t)])
194 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset) \
195 (&mbuf->metadata64[offset/sizeof(uint64_t)])
198 * Given the buf_addr returns the pointer to corresponding mbuf.
200 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
203 * Given the pointer to mbuf returns an address where it's buf_addr
206 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
209 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
211 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
214 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
216 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
220 * Private data in case of pktmbuf pool.
222 * A structure that contains some pktmbuf_pool-specific data that are
223 * appended after the mempool structure (in private data).
225 struct rte_pktmbuf_pool_private {
226 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
229 #ifdef RTE_LIBRTE_MBUF_DEBUG
231 /** check mbuf type in debug mode */
232 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
234 /** check mbuf type in debug mode if mbuf pointer is not null */
235 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
237 rte_mbuf_sanity_check(m, is_h); \
240 /** MBUF asserts in debug mode */
241 #define RTE_MBUF_ASSERT(exp) \
243 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
246 #else /* RTE_LIBRTE_MBUF_DEBUG */
248 /** check mbuf type in debug mode */
249 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
251 /** check mbuf type in debug mode if mbuf pointer is not null */
252 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
254 /** MBUF asserts in debug mode */
255 #define RTE_MBUF_ASSERT(exp) do { } while (0)
257 #endif /* RTE_LIBRTE_MBUF_DEBUG */
259 #ifdef RTE_MBUF_REFCNT
260 #ifdef RTE_MBUF_REFCNT_ATOMIC
263 * Adds given value to an mbuf's refcnt and returns its new value.
267 * Value to add/subtract
271 static inline uint16_t
272 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
274 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
278 * Reads the value of an mbuf's refcnt.
282 * Reference count number.
284 static inline uint16_t
285 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
287 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
291 * Sets an mbuf's refcnt to a defined value.
298 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
300 rte_atomic16_set(&m->refcnt_atomic, new_value);
303 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
306 * Adds given value to an mbuf's refcnt and returns its new value.
308 static inline uint16_t
309 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
311 m->refcnt = (uint16_t)(m->refcnt + value);
316 * Reads the value of an mbuf's refcnt.
318 static inline uint16_t
319 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
325 * Sets an mbuf's refcnt to the defined value.
328 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
330 m->refcnt = new_value;
333 #endif /* RTE_MBUF_REFCNT_ATOMIC */
336 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
341 #else /* ! RTE_MBUF_REFCNT */
344 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
346 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
348 #endif /* RTE_MBUF_REFCNT */
352 * Sanity checks on an mbuf.
354 * Check the consistency of the given mbuf. The function will cause a
355 * panic if corruption is detected.
358 * The mbuf to be checked.
360 * True if the mbuf is a packet header, false if it is a sub-segment
361 * of a packet (in this case, some fields like nb_segs are not checked)
364 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
367 * @internal Allocate a new mbuf from mempool *mp*.
368 * The use of that function is reserved for RTE internal needs.
369 * Please use rte_pktmbuf_alloc().
372 * The mempool from which mbuf is allocated.
374 * - The pointer to the new mbuf on success.
375 * - NULL if allocation failed.
377 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
381 if (rte_mempool_get(mp, &mb) < 0)
383 m = (struct rte_mbuf *)mb;
384 #ifdef RTE_MBUF_REFCNT
385 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
386 rte_mbuf_refcnt_set(m, 1);
387 #endif /* RTE_MBUF_REFCNT */
392 * @internal Put mbuf back into its original mempool.
393 * The use of that function is reserved for RTE internal needs.
394 * Please use rte_pktmbuf_free().
397 * The mbuf to be freed.
399 static inline void __attribute__((always_inline))
400 __rte_mbuf_raw_free(struct rte_mbuf *m)
402 #ifdef RTE_MBUF_REFCNT
403 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
404 #endif /* RTE_MBUF_REFCNT */
405 rte_mempool_put(m->pool, m);
408 /* Operations on ctrl mbuf */
411 * The control mbuf constructor.
413 * This function initializes some fields in an mbuf structure that are
414 * not modified by the user once created (mbuf type, origin pool, buffer
415 * start address, and so on). This function is given as a callback function
416 * to rte_mempool_create() at pool creation time.
419 * The mempool from which the mbuf is allocated.
421 * A pointer that can be used by the user to retrieve useful information
422 * for mbuf initialization. This pointer comes from the ``init_arg``
423 * parameter of rte_mempool_create().
425 * The mbuf to initialize.
427 * The index of the mbuf in the pool table.
429 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
430 void *m, unsigned i);
433 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
435 * This new mbuf is initialized with data pointing to the beginning of
436 * buffer, and with a length of zero.
439 * The mempool from which the mbuf is allocated.
441 * - The pointer to the new mbuf on success.
442 * - NULL if allocation failed.
444 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
447 * Free a control mbuf back into its original mempool.
450 * The control mbuf to be freed.
452 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
455 * A macro that returns the pointer to the carried data.
457 * The value that can be read or assigned.
462 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
465 * A macro that returns the length of the carried data.
467 * The value that can be read or assigned.
472 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
474 /* Operations on pkt mbuf */
477 * The packet mbuf constructor.
479 * This function initializes some fields in the mbuf structure that are
480 * not modified by the user once created (origin pool, buffer start
481 * address, and so on). This function is given as a callback function to
482 * rte_mempool_create() at pool creation time.
485 * The mempool from which mbufs originate.
487 * A pointer that can be used by the user to retrieve useful information
488 * for mbuf initialization. This pointer comes from the ``init_arg``
489 * parameter of rte_mempool_create().
491 * The mbuf to initialize.
493 * The index of the mbuf in the pool table.
495 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
496 void *m, unsigned i);
500 * A packet mbuf pool constructor.
502 * This function initializes the mempool private data in the case of a
503 * pktmbuf pool. This private data is needed by the driver. The
504 * function is given as a callback function to rte_mempool_create() at
505 * pool creation. It can be extended by the user, for example, to
506 * provide another packet size.
509 * The mempool from which mbufs originate.
511 * A pointer that can be used by the user to retrieve useful information
512 * for mbuf initialization. This pointer comes from the ``init_arg``
513 * parameter of rte_mempool_create().
515 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
518 * Reset the fields of a packet mbuf to their default values.
520 * The given mbuf must have only one segment.
523 * The packet mbuf to be resetted.
525 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
535 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
536 RTE_PKTMBUF_HEADROOM : m->buf_len;
539 __rte_mbuf_sanity_check(m, 1);
543 * Allocate a new mbuf from a mempool.
545 * This new mbuf contains one segment, which has a length of 0. The pointer
546 * to data is initialized to have some bytes of headroom in the buffer
547 * (if buffer size allows).
550 * The mempool from which the mbuf is allocated.
552 * - The pointer to the new mbuf on success.
553 * - NULL if allocation failed.
555 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
558 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
559 rte_pktmbuf_reset(m);
563 #ifdef RTE_MBUF_REFCNT
566 * Attach packet mbuf to another packet mbuf.
567 * After attachment we refer the mbuf we attached as 'indirect',
568 * while mbuf we attached to as 'direct'.
569 * Right now, not supported:
570 * - attachment to indirect mbuf (e.g. - md has to be direct).
571 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
572 * - mbuf we trying to attach (mi) is used by someone else
573 * e.g. it's reference counter is greater then 1.
576 * The indirect packet mbuf.
578 * The direct packet mbuf.
581 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
583 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
584 RTE_MBUF_DIRECT(mi) &&
585 rte_mbuf_refcnt_read(mi) == 1);
587 rte_mbuf_refcnt_update(md, 1);
588 mi->buf_physaddr = md->buf_physaddr;
589 mi->buf_addr = md->buf_addr;
590 mi->buf_len = md->buf_len;
593 mi->data_off = md->data_off;
594 mi->data_len = md->data_len;
596 mi->vlan_tci = md->vlan_tci;
597 mi->l2_l3_len = md->l2_l3_len;
601 mi->pkt_len = mi->data_len;
603 mi->ol_flags = md->ol_flags;
605 __rte_mbuf_sanity_check(mi, 1);
606 __rte_mbuf_sanity_check(md, 0);
610 * Detach an indirect packet mbuf -
611 * - restore original mbuf address and length values.
612 * - reset pktmbuf data and data_len to their default values.
613 * All other fields of the given packet mbuf will be left intact.
616 * The indirect attached packet mbuf.
619 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
621 const struct rte_mempool *mp = m->pool;
622 void *buf = RTE_MBUF_TO_BADDR(m);
623 uint32_t buf_len = mp->elt_size - sizeof(*m);
624 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
627 m->buf_len = (uint16_t)buf_len;
629 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
630 RTE_PKTMBUF_HEADROOM : m->buf_len;
635 #endif /* RTE_MBUF_REFCNT */
638 static inline struct rte_mbuf* __attribute__((always_inline))
639 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
641 __rte_mbuf_sanity_check(m, 0);
643 #ifdef RTE_MBUF_REFCNT
644 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
645 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
646 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
648 rte_mbuf_refcnt_set(m, 0);
650 /* if this is an indirect mbuf, then
652 * - free attached mbuf segment
654 if (unlikely (md != m)) {
655 rte_pktmbuf_detach(m);
656 if (rte_mbuf_refcnt_update(md, -1) == 0)
657 __rte_mbuf_raw_free(md);
661 #ifdef RTE_MBUF_REFCNT
668 * Free a segment of a packet mbuf into its original mempool.
670 * Free an mbuf, without parsing other segments in case of chained
674 * The packet mbuf segment to be freed.
676 static inline void __attribute__((always_inline))
677 rte_pktmbuf_free_seg(struct rte_mbuf *m)
679 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
680 __rte_mbuf_raw_free(m);
684 * Free a packet mbuf back into its original mempool.
686 * Free an mbuf, and all its segments in case of chained buffers. Each
687 * segment is added back into its original mempool.
690 * The packet mbuf to be freed.
692 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
694 struct rte_mbuf *m_next;
696 __rte_mbuf_sanity_check(m, 1);
700 rte_pktmbuf_free_seg(m);
705 #ifdef RTE_MBUF_REFCNT
708 * Creates a "clone" of the given packet mbuf.
710 * Walks through all segments of the given packet mbuf, and for each of them:
711 * - Creates a new packet mbuf from the given pool.
712 * - Attaches newly created mbuf to the segment.
713 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
714 * from the original packet mbuf.
717 * The packet mbuf to be cloned.
719 * The mempool from which the "clone" mbufs are allocated.
721 * - The pointer to the new "clone" mbuf on success.
722 * - NULL if allocation fails.
724 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
725 struct rte_mempool *mp)
727 struct rte_mbuf *mc, *mi, **prev;
731 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
736 pktlen = md->pkt_len;
741 rte_pktmbuf_attach(mi, md);
744 } while ((md = md->next) != NULL &&
745 (mi = rte_pktmbuf_alloc(mp)) != NULL);
749 mc->pkt_len = pktlen;
751 /* Allocation of new indirect segment failed */
752 if (unlikely (mi == NULL)) {
753 rte_pktmbuf_free(mc);
757 __rte_mbuf_sanity_check(mc, 1);
762 * Adds given value to the refcnt of all packet mbuf segments.
764 * Walks through all segments of given packet mbuf and for each of them
765 * invokes rte_mbuf_refcnt_update().
768 * The packet mbuf whose refcnt to be updated.
770 * The value to add to the mbuf's segments refcnt.
772 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
774 __rte_mbuf_sanity_check(m, 1);
777 rte_mbuf_refcnt_update(m, v);
778 } while ((m = m->next) != NULL);
781 #endif /* RTE_MBUF_REFCNT */
784 * Get the headroom in a packet mbuf.
789 * The length of the headroom.
791 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
793 __rte_mbuf_sanity_check(m, 1);
798 * Get the tailroom of a packet mbuf.
803 * The length of the tailroom.
805 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
807 __rte_mbuf_sanity_check(m, 1);
808 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
813 * Get the last segment of the packet.
818 * The last segment of the given mbuf.
820 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
822 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
824 __rte_mbuf_sanity_check(m, 1);
825 while (m2->next != NULL)
831 * A macro that points to the start of the data in the mbuf.
833 * The returned pointer is cast to type t. Before using this
834 * function, the user must ensure that m_headlen(m) is large enough to
840 * The type to cast the result into.
842 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
845 * A macro that returns the length of the packet.
847 * The value can be read or assigned.
852 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
855 * A macro that returns the length of the segment.
857 * The value can be read or assigned.
862 #define rte_pktmbuf_data_len(m) ((m)->data_len)
865 * Prepend len bytes to an mbuf data area.
867 * Returns a pointer to the new
868 * data start address. If there is not enough headroom in the first
869 * segment, the function will return NULL, without modifying the mbuf.
874 * The amount of data to prepend (in bytes).
876 * A pointer to the start of the newly prepended data, or
877 * NULL if there is not enough headroom space in the first segment
879 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
882 __rte_mbuf_sanity_check(m, 1);
884 if (unlikely(len > rte_pktmbuf_headroom(m)))
888 m->data_len = (uint16_t)(m->data_len + len);
889 m->pkt_len = (m->pkt_len + len);
891 return (char *)m->buf_addr + m->data_off;
895 * Append len bytes to an mbuf.
897 * Append len bytes to an mbuf and return a pointer to the start address
898 * of the added data. If there is not enough tailroom in the last
899 * segment, the function will return NULL, without modifying the mbuf.
904 * The amount of data to append (in bytes).
906 * A pointer to the start of the newly appended data, or
907 * NULL if there is not enough tailroom space in the last segment
909 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
912 struct rte_mbuf *m_last;
914 __rte_mbuf_sanity_check(m, 1);
916 m_last = rte_pktmbuf_lastseg(m);
917 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
920 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
921 m_last->data_len = (uint16_t)(m_last->data_len + len);
922 m->pkt_len = (m->pkt_len + len);
927 * Remove len bytes at the beginning of an mbuf.
929 * Returns a pointer to the start address of the new data area. If the
930 * length is greater than the length of the first segment, then the
931 * function will fail and return NULL, without modifying the mbuf.
936 * The amount of data to remove (in bytes).
938 * A pointer to the new start of the data.
940 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
942 __rte_mbuf_sanity_check(m, 1);
944 if (unlikely(len > m->data_len))
947 m->data_len = (uint16_t)(m->data_len - len);
949 m->pkt_len = (m->pkt_len - len);
950 return (char *)m->buf_addr + m->data_off;
954 * Remove len bytes of data at the end of the mbuf.
956 * If the length is greater than the length of the last segment, the
957 * function will fail and return -1 without modifying the mbuf.
962 * The amount of data to remove (in bytes).
967 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
969 struct rte_mbuf *m_last;
971 __rte_mbuf_sanity_check(m, 1);
973 m_last = rte_pktmbuf_lastseg(m);
974 if (unlikely(len > m_last->data_len))
977 m_last->data_len = (uint16_t)(m_last->data_len - len);
978 m->pkt_len = (m->pkt_len - len);
983 * Test if mbuf data is contiguous.
988 * - 1, if all data is contiguous (one segment).
989 * - 0, if there is several segments.
991 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
993 __rte_mbuf_sanity_check(m, 1);
994 return !!(m->nb_segs == 1);
998 * Dump an mbuf structure to the console.
1000 * Dump all fields for the given packet mbuf and all its associated
1001 * segments (in the case of a chained buffer).
1004 * A pointer to a file for output
1008 * If dump_len != 0, also dump the "dump_len" first data bytes of
1011 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1017 #endif /* _RTE_MBUF_H_ */