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>
70 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
71 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
74 * A control message buffer.
77 void *data; /**< Pointer to data. */
78 uint32_t data_len; /**< Length of data. */
83 * Packet Offload Features Flags. It also carry packet type information.
84 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
86 #define PKT_RX_VLAN_PKT 0x0001 /**< RX packet is a 802.1q VLAN packet. */
87 #define PKT_RX_RSS_HASH 0x0002 /**< RX packet with RSS hash result. */
88 #define PKT_RX_FDIR 0x0004 /**< RX packet with FDIR infos. */
89 #define PKT_RX_L4_CKSUM_BAD 0x0008 /**< L4 cksum of RX pkt. is not OK. */
90 #define PKT_RX_IP_CKSUM_BAD 0x0010 /**< IP cksum of RX pkt. is not OK. */
91 #define PKT_RX_EIP_CKSUM_BAD 0x0000 /**< External IP header checksum error. */
92 #define PKT_RX_OVERSIZE 0x0000 /**< Num of desc of an RX pkt oversize. */
93 #define PKT_RX_HBUF_OVERFLOW 0x0000 /**< Header buffer overflow. */
94 #define PKT_RX_RECIP_ERR 0x0000 /**< Hardware processing error. */
95 #define PKT_RX_MAC_ERR 0x0000 /**< MAC error. */
96 #define PKT_RX_IPV4_HDR 0x0020 /**< RX packet with IPv4 header. */
97 #define PKT_RX_IPV4_HDR_EXT 0x0040 /**< RX packet with extended IPv4 header. */
98 #define PKT_RX_IPV6_HDR 0x0080 /**< RX packet with IPv6 header. */
99 #define PKT_RX_IPV6_HDR_EXT 0x0100 /**< RX packet with extended IPv6 header. */
100 #define PKT_RX_IEEE1588_PTP 0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
101 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
103 #define PKT_TX_VLAN_PKT 0x0800 /**< TX packet is a 802.1q VLAN packet. */
104 #define PKT_TX_IP_CKSUM 0x1000 /**< IP cksum of TX pkt. computed by NIC. */
105 #define PKT_TX_IPV4_CSUM 0x1000 /**< Alias of PKT_TX_IP_CKSUM. */
106 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
107 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR /**< IPv6 packet */
109 * Bit 14~13 used for L4 packet type with checksum enabled.
115 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
116 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
117 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
118 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
119 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
121 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
124 * Bit Mask to indicate what bits required for building TX context
126 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
128 /** Offload features */
129 union rte_vlan_macip {
132 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
133 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
135 /**< VLAN Tag Control Identifier (CPU order). */
140 * Compare mask for vlan_macip_len.data,
141 * should be in sync with rte_vlan_macip.f layout.
143 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
144 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
145 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
146 /**< MAC+IP length. */
147 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
150 * A packet message buffer.
153 /* valid for any segment */
154 struct rte_mbuf *next; /**< Next segment of scattered packet. */
155 void* data; /**< Start address of data in segment buffer. */
156 uint16_t data_len; /**< Amount of data in segment buffer. */
158 /* these fields are valid for first segment only */
159 uint8_t nb_segs; /**< Number of segments. */
160 uint8_t in_port; /**< Input port. */
161 uint32_t pkt_len; /**< Total pkt len: sum of all segment data_len. */
163 /* offload features */
164 union rte_vlan_macip vlan_macip;
166 uint32_t rss; /**< RSS hash result if RSS enabled */
170 } fdir; /**< Filter identifier if FDIR enabled */
171 uint32_t sched; /**< Hierarchical scheduler */
172 } hash; /**< hash information */
176 * This enum indicates the mbuf type.
179 RTE_MBUF_CTRL, /**< Control mbuf. */
180 RTE_MBUF_PKT, /**< Packet mbuf. */
184 * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
187 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
188 void *buf_addr; /**< Virtual address of segment buffer. */
189 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
190 uint16_t buf_len; /**< Length of segment buffer. */
191 #ifdef RTE_MBUF_REFCNT
193 * 16-bit Reference counter.
194 * It should only be accessed using the following functions:
195 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
196 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
197 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
201 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
202 uint16_t refcnt; /**< Non-atomically accessed refcnt */
205 uint16_t refcnt_reserved; /**< Do not use this field */
207 uint8_t type; /**< Type of mbuf. */
208 uint8_t reserved; /**< Unused field. Required for padding. */
209 uint16_t ol_flags; /**< Offload features. */
212 struct rte_ctrlmbuf ctrl;
213 struct rte_pktmbuf pkt;
218 uint16_t metadata16[0];
219 uint32_t metadata32[0];
220 uint64_t metadata64[0];
222 } __rte_cache_aligned;
224 #define RTE_MBUF_METADATA_UINT8(mbuf, offset) \
225 (mbuf->metadata[offset])
226 #define RTE_MBUF_METADATA_UINT16(mbuf, offset) \
227 (mbuf->metadata16[offset/sizeof(uint16_t)])
228 #define RTE_MBUF_METADATA_UINT32(mbuf, offset) \
229 (mbuf->metadata32[offset/sizeof(uint32_t)])
230 #define RTE_MBUF_METADATA_UINT64(mbuf, offset) \
231 (mbuf->metadata64[offset/sizeof(uint64_t)])
233 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset) \
234 (&mbuf->metadata[offset])
235 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset) \
236 (&mbuf->metadata16[offset/sizeof(uint16_t)])
237 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset) \
238 (&mbuf->metadata32[offset/sizeof(uint32_t)])
239 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset) \
240 (&mbuf->metadata64[offset/sizeof(uint64_t)])
243 * Given the buf_addr returns the pointer to corresponding mbuf.
245 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
248 * Given the pointer to mbuf returns an address where it's buf_addr
251 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
254 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
256 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
259 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
261 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
265 * Private data in case of pktmbuf pool.
267 * A structure that contains some pktmbuf_pool-specific data that are
268 * appended after the mempool structure (in private data).
270 struct rte_pktmbuf_pool_private {
271 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
274 #ifdef RTE_LIBRTE_MBUF_DEBUG
276 /** check mbuf type in debug mode */
277 #define __rte_mbuf_sanity_check(m, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
279 /** check mbuf type in debug mode if mbuf pointer is not null */
280 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { \
282 rte_mbuf_sanity_check(m, t, is_h); \
285 /** MBUF asserts in debug mode */
286 #define RTE_MBUF_ASSERT(exp) \
288 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
291 #else /* RTE_LIBRTE_MBUF_DEBUG */
293 /** check mbuf type in debug mode */
294 #define __rte_mbuf_sanity_check(m, t, is_h) do { } while(0)
296 /** check mbuf type in debug mode if mbuf pointer is not null */
297 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { } while(0)
299 /** MBUF asserts in debug mode */
300 #define RTE_MBUF_ASSERT(exp) do { } while(0)
302 #endif /* RTE_LIBRTE_MBUF_DEBUG */
304 #ifdef RTE_MBUF_REFCNT
305 #ifdef RTE_MBUF_REFCNT_ATOMIC
308 * Adds given value to an mbuf's refcnt and returns its new value.
312 * Value to add/subtract
316 static inline uint16_t
317 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
319 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
323 * Reads the value of an mbuf's refcnt.
327 * Reference count number.
329 static inline uint16_t
330 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
332 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
336 * Sets an mbuf's refcnt to a defined value.
343 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
345 rte_atomic16_set(&m->refcnt_atomic, new_value);
348 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
351 * Adds given value to an mbuf's refcnt and returns its new value.
353 static inline uint16_t
354 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
356 m->refcnt = (uint16_t)(m->refcnt + value);
361 * Reads the value of an mbuf's refcnt.
363 static inline uint16_t
364 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
370 * Sets an mbuf's refcnt to the defined value.
373 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
375 m->refcnt = new_value;
378 #endif /* RTE_MBUF_REFCNT_ATOMIC */
381 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
386 #else /* ! RTE_MBUF_REFCNT */
389 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
391 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
393 #endif /* RTE_MBUF_REFCNT */
397 * Sanity checks on an mbuf.
399 * Check the consistency of the given mbuf. The function will cause a
400 * panic if corruption is detected.
403 * The mbuf to be checked.
405 * The expected type of the mbuf.
407 * True if the mbuf is a packet header, false if it is a sub-segment
408 * of a packet (in this case, some fields like nb_segs are not checked)
411 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
415 * @internal Allocate a new mbuf from mempool *mp*.
416 * The use of that function is reserved for RTE internal needs.
417 * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
420 * The mempool from which mbuf is allocated.
422 * - The pointer to the new mbuf on success.
423 * - NULL if allocation failed.
425 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
429 if (rte_mempool_get(mp, &mb) < 0)
431 m = (struct rte_mbuf *)mb;
432 #ifdef RTE_MBUF_REFCNT
433 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
434 rte_mbuf_refcnt_set(m, 1);
435 #endif /* RTE_MBUF_REFCNT */
440 * @internal Put mbuf back into its original mempool.
441 * The use of that function is reserved for RTE internal needs.
442 * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
445 * The mbuf to be freed.
447 static inline void __attribute__((always_inline))
448 __rte_mbuf_raw_free(struct rte_mbuf *m)
450 #ifdef RTE_MBUF_REFCNT
451 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
452 #endif /* RTE_MBUF_REFCNT */
453 rte_mempool_put(m->pool, m);
456 /* Operations on ctrl mbuf */
459 * The control mbuf constructor.
461 * This function initializes some fields in an mbuf structure that are
462 * not modified by the user once created (mbuf type, origin pool, buffer
463 * start address, and so on). This function is given as a callback function
464 * to rte_mempool_create() at pool creation time.
467 * The mempool from which the mbuf is allocated.
469 * A pointer that can be used by the user to retrieve useful information
470 * for mbuf initialization. This pointer comes from the ``init_arg``
471 * parameter of rte_mempool_create().
473 * The mbuf to initialize.
475 * The index of the mbuf in the pool table.
477 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
478 void *m, unsigned i);
481 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
483 * This new mbuf is initialized with data pointing to the beginning of
484 * buffer, and with a length of zero.
487 * The mempool from which the mbuf is allocated.
489 * - The pointer to the new mbuf on success.
490 * - NULL if allocation failed.
492 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
495 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
496 m->ctrl.data = m->buf_addr;
497 m->ctrl.data_len = 0;
498 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
504 * Free a control mbuf back into its original mempool.
507 * The control mbuf to be freed.
509 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
511 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
512 #ifdef RTE_MBUF_REFCNT
513 if (rte_mbuf_refcnt_update(m, -1) == 0)
514 #endif /* RTE_MBUF_REFCNT */
515 __rte_mbuf_raw_free(m);
519 * A macro that returns the pointer to the carried data.
521 * The value that can be read or assigned.
526 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
529 * A macro that returns the length of the carried data.
531 * The value that can be read or assigned.
536 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
538 /* Operations on pkt mbuf */
541 * The packet mbuf constructor.
543 * This function initializes some fields in the mbuf structure that are not
544 * modified by the user once created (mbuf type, origin pool, buffer start
545 * address, and so on). This function is given as a callback function to
546 * rte_mempool_create() at pool creation time.
549 * The mempool from which mbufs originate.
551 * A pointer that can be used by the user to retrieve useful information
552 * for mbuf initialization. This pointer comes from the ``init_arg``
553 * parameter of rte_mempool_create().
555 * The mbuf to initialize.
557 * The index of the mbuf in the pool table.
559 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
560 void *m, unsigned i);
564 * A packet mbuf pool constructor.
566 * This function initializes the mempool private data in the case of a
567 * pktmbuf pool. This private data is needed by the driver. The
568 * function is given as a callback function to rte_mempool_create() at
569 * pool creation. It can be extended by the user, for example, to
570 * provide another packet size.
573 * The mempool from which mbufs originate.
575 * A pointer that can be used by the user to retrieve useful information
576 * for mbuf initialization. This pointer comes from the ``init_arg``
577 * parameter of rte_mempool_create().
579 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
582 * Reset the fields of a packet mbuf to their default values.
584 * The given mbuf must have only one segment.
587 * The packet mbuf to be resetted.
589 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
595 m->pkt.vlan_macip.data = 0;
597 m->pkt.in_port = 0xff;
600 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
601 RTE_PKTMBUF_HEADROOM : m->buf_len;
602 m->pkt.data = (char*) m->buf_addr + buf_ofs;
605 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
609 * Allocate a new mbuf (type is pkt) from a mempool.
611 * This new mbuf contains one segment, which has a length of 0. The pointer
612 * to data is initialized to have some bytes of headroom in the buffer
613 * (if buffer size allows).
616 * The mempool from which the mbuf is allocated.
618 * - The pointer to the new mbuf on success.
619 * - NULL if allocation failed.
621 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
624 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
625 rte_pktmbuf_reset(m);
629 #ifdef RTE_MBUF_REFCNT
632 * Attach packet mbuf to another packet mbuf.
633 * After attachment we refer the mbuf we attached as 'indirect',
634 * while mbuf we attached to as 'direct'.
635 * Right now, not supported:
636 * - attachment to indirect mbuf (e.g. - md has to be direct).
637 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
638 * - mbuf we trying to attach (mi) is used by someone else
639 * e.g. it's reference counter is greater then 1.
642 * The indirect packet mbuf.
644 * The direct packet mbuf.
647 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
649 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
650 RTE_MBUF_DIRECT(mi) &&
651 rte_mbuf_refcnt_read(mi) == 1);
653 rte_mbuf_refcnt_update(md, 1);
654 mi->buf_physaddr = md->buf_physaddr;
655 mi->buf_addr = md->buf_addr;
656 mi->buf_len = md->buf_len;
661 mi->pkt.pkt_len = mi->pkt.data_len;
663 mi->ol_flags = md->ol_flags;
665 __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
666 __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
670 * Detach an indirect packet mbuf -
671 * - restore original mbuf address and length values.
672 * - reset pktmbuf data and data_len to their default values.
673 * All other fields of the given packet mbuf will be left intact.
676 * The indirect attached packet mbuf.
679 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
681 const struct rte_mempool *mp = m->pool;
682 void *buf = RTE_MBUF_TO_BADDR(m);
684 uint32_t buf_len = mp->elt_size - sizeof(*m);
685 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
688 m->buf_len = (uint16_t)buf_len;
690 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
691 RTE_PKTMBUF_HEADROOM : m->buf_len;
692 m->pkt.data = (char*) m->buf_addr + buf_ofs;
697 #endif /* RTE_MBUF_REFCNT */
700 static inline struct rte_mbuf* __attribute__((always_inline))
701 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
703 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
705 #ifdef RTE_MBUF_REFCNT
706 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
707 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
708 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
710 rte_mbuf_refcnt_set(m, 0);
712 /* if this is an indirect mbuf, then
714 * - free attached mbuf segment
716 if (unlikely (md != m)) {
717 rte_pktmbuf_detach(m);
718 if (rte_mbuf_refcnt_update(md, -1) == 0)
719 __rte_mbuf_raw_free(md);
723 #ifdef RTE_MBUF_REFCNT
730 * Free a segment of a packet mbuf into its original mempool.
732 * Free an mbuf, without parsing other segments in case of chained
736 * The packet mbuf segment to be freed.
738 static inline void __attribute__((always_inline))
739 rte_pktmbuf_free_seg(struct rte_mbuf *m)
741 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
742 __rte_mbuf_raw_free(m);
746 * Free a packet mbuf back into its original mempool.
748 * Free an mbuf, and all its segments in case of chained buffers. Each
749 * segment is added back into its original mempool.
752 * The packet mbuf to be freed.
754 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
756 struct rte_mbuf *m_next;
758 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
761 m_next = m->pkt.next;
762 rte_pktmbuf_free_seg(m);
767 #ifdef RTE_MBUF_REFCNT
770 * Creates a "clone" of the given packet mbuf.
772 * Walks through all segments of the given packet mbuf, and for each of them:
773 * - Creates a new packet mbuf from the given pool.
774 * - Attaches newly created mbuf to the segment.
775 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
776 * from the original packet mbuf.
779 * The packet mbuf to be cloned.
781 * The mempool from which the "clone" mbufs are allocated.
783 * - The pointer to the new "clone" mbuf on success.
784 * - NULL if allocation fails.
786 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
787 struct rte_mempool *mp)
789 struct rte_mbuf *mc, *mi, **prev;
793 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
797 prev = &mi->pkt.next;
798 pktlen = md->pkt.pkt_len;
803 rte_pktmbuf_attach(mi, md);
805 prev = &mi->pkt.next;
806 } while ((md = md->pkt.next) != NULL &&
807 (mi = rte_pktmbuf_alloc(mp)) != NULL);
810 mc->pkt.nb_segs = nseg;
811 mc->pkt.pkt_len = pktlen;
813 /* Allocation of new indirect segment failed */
814 if (unlikely (mi == NULL)) {
815 rte_pktmbuf_free(mc);
819 __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
824 * Adds given value to the refcnt of all packet mbuf segments.
826 * Walks through all segments of given packet mbuf and for each of them
827 * invokes rte_mbuf_refcnt_update().
830 * The packet mbuf whose refcnt to be updated.
832 * The value to add to the mbuf's segments refcnt.
834 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
836 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
839 rte_mbuf_refcnt_update(m, v);
840 } while ((m = m->pkt.next) != NULL);
843 #endif /* RTE_MBUF_REFCNT */
846 * Get the headroom in a packet mbuf.
851 * The length of the headroom.
853 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
855 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
856 return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
860 * Get the tailroom of a packet mbuf.
865 * The length of the tailroom.
867 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
869 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
870 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
875 * Get the last segment of the packet.
880 * The last segment of the given mbuf.
882 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
884 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
886 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
887 while (m2->pkt.next != NULL)
893 * A macro that points to the start of the data in the mbuf.
895 * The returned pointer is cast to type t. Before using this
896 * function, the user must ensure that m_headlen(m) is large enough to
902 * The type to cast the result into.
904 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
907 * A macro that returns the length of the packet.
909 * The value can be read or assigned.
914 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
917 * A macro that returns the length of the segment.
919 * The value can be read or assigned.
924 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
927 * Prepend len bytes to an mbuf data area.
929 * Returns a pointer to the new
930 * data start address. If there is not enough headroom in the first
931 * segment, the function will return NULL, without modifying the mbuf.
936 * The amount of data to prepend (in bytes).
938 * A pointer to the start of the newly prepended data, or
939 * NULL if there is not enough headroom space in the first segment
941 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
944 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
946 if (unlikely(len > rte_pktmbuf_headroom(m)))
949 m->pkt.data = (char*) m->pkt.data - len;
950 m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
951 m->pkt.pkt_len = (m->pkt.pkt_len + len);
953 return (char*) m->pkt.data;
957 * Append len bytes to an mbuf.
959 * Append len bytes to an mbuf and return a pointer to the start address
960 * of the added data. If there is not enough tailroom in the last
961 * segment, the function will return NULL, without modifying the mbuf.
966 * The amount of data to append (in bytes).
968 * A pointer to the start of the newly appended data, or
969 * NULL if there is not enough tailroom space in the last segment
971 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
974 struct rte_mbuf *m_last;
976 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
978 m_last = rte_pktmbuf_lastseg(m);
979 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
982 tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
983 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
984 m->pkt.pkt_len = (m->pkt.pkt_len + len);
989 * Remove len bytes at the beginning of an mbuf.
991 * Returns a pointer to the start address of the new data area. If the
992 * length is greater than the length of the first segment, then the
993 * function will fail and return NULL, without modifying the mbuf.
998 * The amount of data to remove (in bytes).
1000 * A pointer to the new start of the data.
1002 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1004 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1006 if (unlikely(len > m->pkt.data_len))
1009 m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
1010 m->pkt.data = ((char*) m->pkt.data + len);
1011 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1012 return (char*) m->pkt.data;
1016 * Remove len bytes of data at the end of the mbuf.
1018 * If the length is greater than the length of the last segment, the
1019 * function will fail and return -1 without modifying the mbuf.
1024 * The amount of data to remove (in bytes).
1029 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1031 struct rte_mbuf *m_last;
1033 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1035 m_last = rte_pktmbuf_lastseg(m);
1036 if (unlikely(len > m_last->pkt.data_len))
1039 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
1040 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1045 * Test if mbuf data is contiguous.
1050 * - 1, if all data is contiguous (one segment).
1051 * - 0, if there is several segments.
1053 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1055 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1056 return !!(m->pkt.nb_segs == 1);
1060 * Dump an mbuf structure to the console.
1062 * Dump all fields for the given packet mbuf and all its associated
1063 * segments (in the case of a chained buffer).
1066 * A pointer to a file for output
1070 * If dump_len != 0, also dump the "dump_len" first data bytes of
1073 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1079 #endif /* _RTE_MBUF_H_ */