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 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
119 void *buf_addr; /**< Virtual address of segment buffer. */
120 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
121 uint16_t buf_len; /**< Length of segment buffer. */
122 #ifdef RTE_MBUF_REFCNT
124 * 16-bit Reference counter.
125 * It should only be accessed using the following functions:
126 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
127 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
128 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
132 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
133 uint16_t refcnt; /**< Non-atomically accessed refcnt */
136 uint16_t refcnt_reserved; /**< Do not use this field */
138 uint16_t reserved; /**< Unused field. Required for padding */
139 uint16_t ol_flags; /**< Offload features. */
141 /* valid for any segment */
142 struct rte_mbuf *next; /**< Next segment of scattered packet. */
143 void* data; /**< Start address of data in segment buffer. */
144 uint16_t data_len; /**< Amount of data in segment buffer. */
146 /* these fields are valid for first segment only */
147 uint8_t nb_segs; /**< Number of segments. */
148 uint8_t port; /**< Input port. */
149 uint32_t pkt_len; /**< Total pkt len: sum of all segment data_len. */
151 /* offload features, valid for first segment only */
153 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
155 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
156 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
159 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order). */
161 uint32_t rss; /**< RSS hash result if RSS enabled */
165 } fdir; /**< Filter identifier if FDIR enabled */
166 uint32_t sched; /**< Hierarchical scheduler */
167 } hash; /**< hash information */
171 uint16_t metadata16[0];
172 uint32_t metadata32[0];
173 uint64_t metadata64[0];
175 } __rte_cache_aligned;
177 #define RTE_MBUF_METADATA_UINT8(mbuf, offset) \
178 (mbuf->metadata[offset])
179 #define RTE_MBUF_METADATA_UINT16(mbuf, offset) \
180 (mbuf->metadata16[offset/sizeof(uint16_t)])
181 #define RTE_MBUF_METADATA_UINT32(mbuf, offset) \
182 (mbuf->metadata32[offset/sizeof(uint32_t)])
183 #define RTE_MBUF_METADATA_UINT64(mbuf, offset) \
184 (mbuf->metadata64[offset/sizeof(uint64_t)])
186 #define RTE_MBUF_METADATA_UINT8_PTR(mbuf, offset) \
187 (&mbuf->metadata[offset])
188 #define RTE_MBUF_METADATA_UINT16_PTR(mbuf, offset) \
189 (&mbuf->metadata16[offset/sizeof(uint16_t)])
190 #define RTE_MBUF_METADATA_UINT32_PTR(mbuf, offset) \
191 (&mbuf->metadata32[offset/sizeof(uint32_t)])
192 #define RTE_MBUF_METADATA_UINT64_PTR(mbuf, offset) \
193 (&mbuf->metadata64[offset/sizeof(uint64_t)])
196 * Given the buf_addr returns the pointer to corresponding mbuf.
198 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
201 * Given the pointer to mbuf returns an address where it's buf_addr
204 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
207 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
209 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
212 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
214 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
218 * Private data in case of pktmbuf pool.
220 * A structure that contains some pktmbuf_pool-specific data that are
221 * appended after the mempool structure (in private data).
223 struct rte_pktmbuf_pool_private {
224 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
227 #ifdef RTE_LIBRTE_MBUF_DEBUG
229 /** check mbuf type in debug mode */
230 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
232 /** check mbuf type in debug mode if mbuf pointer is not null */
233 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
235 rte_mbuf_sanity_check(m, is_h); \
238 /** MBUF asserts in debug mode */
239 #define RTE_MBUF_ASSERT(exp) \
241 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
244 #else /* RTE_LIBRTE_MBUF_DEBUG */
246 /** check mbuf type in debug mode */
247 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
249 /** check mbuf type in debug mode if mbuf pointer is not null */
250 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
252 /** MBUF asserts in debug mode */
253 #define RTE_MBUF_ASSERT(exp) do { } while (0)
255 #endif /* RTE_LIBRTE_MBUF_DEBUG */
257 #ifdef RTE_MBUF_REFCNT
258 #ifdef RTE_MBUF_REFCNT_ATOMIC
261 * Adds given value to an mbuf's refcnt and returns its new value.
265 * Value to add/subtract
269 static inline uint16_t
270 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
272 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
276 * Reads the value of an mbuf's refcnt.
280 * Reference count number.
282 static inline uint16_t
283 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
285 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
289 * Sets an mbuf's refcnt to a defined value.
296 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
298 rte_atomic16_set(&m->refcnt_atomic, new_value);
301 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
304 * Adds given value to an mbuf's refcnt and returns its new value.
306 static inline uint16_t
307 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
309 m->refcnt = (uint16_t)(m->refcnt + value);
314 * Reads the value of an mbuf's refcnt.
316 static inline uint16_t
317 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
323 * Sets an mbuf's refcnt to the defined value.
326 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
328 m->refcnt = new_value;
331 #endif /* RTE_MBUF_REFCNT_ATOMIC */
334 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
339 #else /* ! RTE_MBUF_REFCNT */
342 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
344 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
346 #endif /* RTE_MBUF_REFCNT */
350 * Sanity checks on an mbuf.
352 * Check the consistency of the given mbuf. The function will cause a
353 * panic if corruption is detected.
356 * The mbuf to be checked.
358 * True if the mbuf is a packet header, false if it is a sub-segment
359 * of a packet (in this case, some fields like nb_segs are not checked)
362 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
365 * @internal Allocate a new mbuf from mempool *mp*.
366 * The use of that function is reserved for RTE internal needs.
367 * Please use rte_pktmbuf_alloc().
370 * The mempool from which mbuf is allocated.
372 * - The pointer to the new mbuf on success.
373 * - NULL if allocation failed.
375 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
379 if (rte_mempool_get(mp, &mb) < 0)
381 m = (struct rte_mbuf *)mb;
382 #ifdef RTE_MBUF_REFCNT
383 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
384 rte_mbuf_refcnt_set(m, 1);
385 #endif /* RTE_MBUF_REFCNT */
390 * @internal Put mbuf back into its original mempool.
391 * The use of that function is reserved for RTE internal needs.
392 * Please use rte_pktmbuf_free().
395 * The mbuf to be freed.
397 static inline void __attribute__((always_inline))
398 __rte_mbuf_raw_free(struct rte_mbuf *m)
400 #ifdef RTE_MBUF_REFCNT
401 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
402 #endif /* RTE_MBUF_REFCNT */
403 rte_mempool_put(m->pool, m);
406 /* Operations on ctrl mbuf */
409 * The control mbuf constructor.
411 * This function initializes some fields in an mbuf structure that are
412 * not modified by the user once created (mbuf type, origin pool, buffer
413 * start address, and so on). This function is given as a callback function
414 * to rte_mempool_create() at pool creation time.
417 * The mempool from which the mbuf is allocated.
419 * A pointer that can be used by the user to retrieve useful information
420 * for mbuf initialization. This pointer comes from the ``init_arg``
421 * parameter of rte_mempool_create().
423 * The mbuf to initialize.
425 * The index of the mbuf in the pool table.
427 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
428 void *m, unsigned i);
431 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
433 * This new mbuf is initialized with data pointing to the beginning of
434 * buffer, and with a length of zero.
437 * The mempool from which the mbuf is allocated.
439 * - The pointer to the new mbuf on success.
440 * - NULL if allocation failed.
442 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
445 * Free a control mbuf back into its original mempool.
448 * The control mbuf to be freed.
450 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
453 * A macro that returns the pointer to the carried data.
455 * The value that can be read or assigned.
460 #define rte_ctrlmbuf_data(m) ((m)->data)
463 * A macro that returns the length of the carried data.
465 * The value that can be read or assigned.
470 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
472 /* Operations on pkt mbuf */
475 * The packet mbuf constructor.
477 * This function initializes some fields in the mbuf structure that are
478 * not modified by the user once created (origin pool, buffer start
479 * address, and so on). This function is given as a callback function to
480 * rte_mempool_create() at pool creation time.
483 * The mempool from which mbufs originate.
485 * A pointer that can be used by the user to retrieve useful information
486 * for mbuf initialization. This pointer comes from the ``init_arg``
487 * parameter of rte_mempool_create().
489 * The mbuf to initialize.
491 * The index of the mbuf in the pool table.
493 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
494 void *m, unsigned i);
498 * A packet mbuf pool constructor.
500 * This function initializes the mempool private data in the case of a
501 * pktmbuf pool. This private data is needed by the driver. The
502 * function is given as a callback function to rte_mempool_create() at
503 * pool creation. It can be extended by the user, for example, to
504 * provide another packet size.
507 * The mempool from which mbufs originate.
509 * A pointer that can be used by the user to retrieve useful information
510 * for mbuf initialization. This pointer comes from the ``init_arg``
511 * parameter of rte_mempool_create().
513 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
516 * Reset the fields of a packet mbuf to their default values.
518 * The given mbuf must have only one segment.
521 * The packet mbuf to be resetted.
523 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
535 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
536 RTE_PKTMBUF_HEADROOM : m->buf_len;
537 m->data = (char*) m->buf_addr + buf_ofs;
540 __rte_mbuf_sanity_check(m, 1);
544 * Allocate a new mbuf from a mempool.
546 * This new mbuf contains one segment, which has a length of 0. The pointer
547 * to data is initialized to have some bytes of headroom in the buffer
548 * (if buffer size allows).
551 * The mempool from which the mbuf is allocated.
553 * - The pointer to the new mbuf on success.
554 * - NULL if allocation failed.
556 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
559 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
560 rte_pktmbuf_reset(m);
564 #ifdef RTE_MBUF_REFCNT
567 * Attach packet mbuf to another packet mbuf.
568 * After attachment we refer the mbuf we attached as 'indirect',
569 * while mbuf we attached to as 'direct'.
570 * Right now, not supported:
571 * - attachment to indirect mbuf (e.g. - md has to be direct).
572 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
573 * - mbuf we trying to attach (mi) is used by someone else
574 * e.g. it's reference counter is greater then 1.
577 * The indirect packet mbuf.
579 * The direct packet mbuf.
582 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
584 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
585 RTE_MBUF_DIRECT(mi) &&
586 rte_mbuf_refcnt_read(mi) == 1);
588 rte_mbuf_refcnt_update(md, 1);
589 mi->buf_physaddr = md->buf_physaddr;
590 mi->buf_addr = md->buf_addr;
591 mi->buf_len = md->buf_len;
595 mi->data_len = md->data_len;
597 mi->vlan_tci = md->vlan_tci;
598 mi->l2_l3_len = md->l2_l3_len;
602 mi->pkt_len = mi->data_len;
604 mi->ol_flags = md->ol_flags;
606 __rte_mbuf_sanity_check(mi, 1);
607 __rte_mbuf_sanity_check(md, 0);
611 * Detach an indirect packet mbuf -
612 * - restore original mbuf address and length values.
613 * - reset pktmbuf data and data_len to their default values.
614 * All other fields of the given packet mbuf will be left intact.
617 * The indirect attached packet mbuf.
620 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
622 const struct rte_mempool *mp = m->pool;
623 void *buf = RTE_MBUF_TO_BADDR(m);
625 uint32_t buf_len = mp->elt_size - sizeof(*m);
626 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
629 m->buf_len = (uint16_t)buf_len;
631 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
632 RTE_PKTMBUF_HEADROOM : m->buf_len;
633 m->data = (char*) m->buf_addr + buf_ofs;
638 #endif /* RTE_MBUF_REFCNT */
641 static inline struct rte_mbuf* __attribute__((always_inline))
642 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
644 __rte_mbuf_sanity_check(m, 0);
646 #ifdef RTE_MBUF_REFCNT
647 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
648 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
649 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
651 rte_mbuf_refcnt_set(m, 0);
653 /* if this is an indirect mbuf, then
655 * - free attached mbuf segment
657 if (unlikely (md != m)) {
658 rte_pktmbuf_detach(m);
659 if (rte_mbuf_refcnt_update(md, -1) == 0)
660 __rte_mbuf_raw_free(md);
664 #ifdef RTE_MBUF_REFCNT
671 * Free a segment of a packet mbuf into its original mempool.
673 * Free an mbuf, without parsing other segments in case of chained
677 * The packet mbuf segment to be freed.
679 static inline void __attribute__((always_inline))
680 rte_pktmbuf_free_seg(struct rte_mbuf *m)
682 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
683 __rte_mbuf_raw_free(m);
687 * Free a packet mbuf back into its original mempool.
689 * Free an mbuf, and all its segments in case of chained buffers. Each
690 * segment is added back into its original mempool.
693 * The packet mbuf to be freed.
695 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
697 struct rte_mbuf *m_next;
699 __rte_mbuf_sanity_check(m, 1);
703 rte_pktmbuf_free_seg(m);
708 #ifdef RTE_MBUF_REFCNT
711 * Creates a "clone" of the given packet mbuf.
713 * Walks through all segments of the given packet mbuf, and for each of them:
714 * - Creates a new packet mbuf from the given pool.
715 * - Attaches newly created mbuf to the segment.
716 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
717 * from the original packet mbuf.
720 * The packet mbuf to be cloned.
722 * The mempool from which the "clone" mbufs are allocated.
724 * - The pointer to the new "clone" mbuf on success.
725 * - NULL if allocation fails.
727 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
728 struct rte_mempool *mp)
730 struct rte_mbuf *mc, *mi, **prev;
734 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
739 pktlen = md->pkt_len;
744 rte_pktmbuf_attach(mi, md);
747 } while ((md = md->next) != NULL &&
748 (mi = rte_pktmbuf_alloc(mp)) != NULL);
752 mc->pkt_len = pktlen;
754 /* Allocation of new indirect segment failed */
755 if (unlikely (mi == NULL)) {
756 rte_pktmbuf_free(mc);
760 __rte_mbuf_sanity_check(mc, 1);
765 * Adds given value to the refcnt of all packet mbuf segments.
767 * Walks through all segments of given packet mbuf and for each of them
768 * invokes rte_mbuf_refcnt_update().
771 * The packet mbuf whose refcnt to be updated.
773 * The value to add to the mbuf's segments refcnt.
775 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
777 __rte_mbuf_sanity_check(m, 1);
780 rte_mbuf_refcnt_update(m, v);
781 } while ((m = m->next) != NULL);
784 #endif /* RTE_MBUF_REFCNT */
787 * Get the headroom in a packet mbuf.
792 * The length of the headroom.
794 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
796 __rte_mbuf_sanity_check(m, 1);
797 return (uint16_t) ((char*) m->data - (char*) m->buf_addr);
801 * Get the tailroom of a packet mbuf.
806 * The length of the tailroom.
808 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
810 __rte_mbuf_sanity_check(m, 1);
811 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
816 * Get the last segment of the packet.
821 * The last segment of the given mbuf.
823 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
825 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
827 __rte_mbuf_sanity_check(m, 1);
828 while (m2->next != NULL)
834 * A macro that points to the start of the data in the mbuf.
836 * The returned pointer is cast to type t. Before using this
837 * function, the user must ensure that m_headlen(m) is large enough to
843 * The type to cast the result into.
845 #define rte_pktmbuf_mtod(m, t) ((t)((m)->data))
848 * A macro that returns the length of the packet.
850 * The value can be read or assigned.
855 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
858 * A macro that returns the length of the segment.
860 * The value can be read or assigned.
865 #define rte_pktmbuf_data_len(m) ((m)->data_len)
868 * Prepend len bytes to an mbuf data area.
870 * Returns a pointer to the new
871 * data start address. If there is not enough headroom in the first
872 * segment, the function will return NULL, without modifying the mbuf.
877 * The amount of data to prepend (in bytes).
879 * A pointer to the start of the newly prepended data, or
880 * NULL if there is not enough headroom space in the first segment
882 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
885 __rte_mbuf_sanity_check(m, 1);
887 if (unlikely(len > rte_pktmbuf_headroom(m)))
890 m->data = (char*) m->data - len;
891 m->data_len = (uint16_t)(m->data_len + len);
892 m->pkt_len = (m->pkt_len + len);
894 return (char*) m->data;
898 * Append len bytes to an mbuf.
900 * Append len bytes to an mbuf and return a pointer to the start address
901 * of the added data. If there is not enough tailroom in the last
902 * segment, the function will return NULL, without modifying the mbuf.
907 * The amount of data to append (in bytes).
909 * A pointer to the start of the newly appended data, or
910 * NULL if there is not enough tailroom space in the last segment
912 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
915 struct rte_mbuf *m_last;
917 __rte_mbuf_sanity_check(m, 1);
919 m_last = rte_pktmbuf_lastseg(m);
920 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
923 tail = (char*) m_last->data + m_last->data_len;
924 m_last->data_len = (uint16_t)(m_last->data_len + len);
925 m->pkt_len = (m->pkt_len + len);
930 * Remove len bytes at the beginning of an mbuf.
932 * Returns a pointer to the start address of the new data area. If the
933 * length is greater than the length of the first segment, then the
934 * function will fail and return NULL, without modifying the mbuf.
939 * The amount of data to remove (in bytes).
941 * A pointer to the new start of the data.
943 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
945 __rte_mbuf_sanity_check(m, 1);
947 if (unlikely(len > m->data_len))
950 m->data_len = (uint16_t)(m->data_len - len);
951 m->data = ((char*) m->data + len);
952 m->pkt_len = (m->pkt_len - len);
953 return (char*) m->data;
957 * Remove len bytes of data at the end of the mbuf.
959 * If the length is greater than the length of the last segment, the
960 * function will fail and return -1 without modifying the mbuf.
965 * The amount of data to remove (in bytes).
970 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
972 struct rte_mbuf *m_last;
974 __rte_mbuf_sanity_check(m, 1);
976 m_last = rte_pktmbuf_lastseg(m);
977 if (unlikely(len > m_last->data_len))
980 m_last->data_len = (uint16_t)(m_last->data_len - len);
981 m->pkt_len = (m->pkt_len - len);
986 * Test if mbuf data is contiguous.
991 * - 1, if all data is contiguous (one segment).
992 * - 0, if there is several segments.
994 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
996 __rte_mbuf_sanity_check(m, 1);
997 return !!(m->nb_segs == 1);
1001 * Dump an mbuf structure to the console.
1003 * Dump all fields for the given packet mbuf and all its associated
1004 * segments (in the case of a chained buffer).
1007 * A pointer to a file for output
1011 * If dump_len != 0, also dump the "dump_len" first data bytes of
1014 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1020 #endif /* _RTE_MBUF_H_ */