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)
118 /* define a set of marker types that can be used to refer to set points in the
120 typedef void *MARKER[0]; /**< generic marker for a point in a structure */
121 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
122 * with a single assignment */
124 * The generic rte_mbuf, containing a packet mbuf.
129 void *buf_addr; /**< Virtual address of segment buffer. */
130 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
132 /* next 8 bytes are initialised on RX descriptor rearm */
134 uint16_t buf_len; /**< Length of segment buffer. */
138 * 16-bit Reference counter.
139 * It should only be accessed using the following functions:
140 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
141 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
142 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
146 #ifdef RTE_MBUF_REFCNT
147 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
148 uint16_t refcnt; /**< Non-atomically accessed refcnt */
150 uint16_t refcnt_reserved; /**< Do not use this field */
152 uint8_t nb_segs; /**< Number of segments. */
153 uint8_t port; /**< Input port. */
155 uint64_t ol_flags; /**< Offload features. */
157 /* remaining bytes are set on RX when pulling packet from descriptor */
158 MARKER rx_descriptor_fields1;
159 uint16_t reserved2; /**< Unused field. Required for padding */
160 uint16_t data_len; /**< Amount of data in segment buffer. */
161 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
163 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order) */
165 uint32_t rss; /**< RSS hash result if RSS enabled */
169 } fdir; /**< Filter identifier if FDIR enabled */
170 uint32_t sched; /**< Hierarchical scheduler */
171 } hash; /**< hash information */
173 /* second cache line - fields only used in slow path or on TX */
174 MARKER cacheline1 __rte_cache_aligned;
175 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
176 struct rte_mbuf *next; /**< Next segment of scattered packet. */
178 /* fields to support TX offloads */
180 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
182 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
183 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
186 } __rte_cache_aligned;
189 * Given the buf_addr returns the pointer to corresponding mbuf.
191 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
194 * Given the pointer to mbuf returns an address where it's buf_addr
197 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
200 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
202 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
205 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
207 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
211 * Private data in case of pktmbuf pool.
213 * A structure that contains some pktmbuf_pool-specific data that are
214 * appended after the mempool structure (in private data).
216 struct rte_pktmbuf_pool_private {
217 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
220 #ifdef RTE_LIBRTE_MBUF_DEBUG
222 /** check mbuf type in debug mode */
223 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
225 /** check mbuf type in debug mode if mbuf pointer is not null */
226 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
228 rte_mbuf_sanity_check(m, is_h); \
231 /** MBUF asserts in debug mode */
232 #define RTE_MBUF_ASSERT(exp) \
234 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
237 #else /* RTE_LIBRTE_MBUF_DEBUG */
239 /** check mbuf type in debug mode */
240 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
242 /** check mbuf type in debug mode if mbuf pointer is not null */
243 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
245 /** MBUF asserts in debug mode */
246 #define RTE_MBUF_ASSERT(exp) do { } while (0)
248 #endif /* RTE_LIBRTE_MBUF_DEBUG */
250 #ifdef RTE_MBUF_REFCNT
251 #ifdef RTE_MBUF_REFCNT_ATOMIC
254 * Adds given value to an mbuf's refcnt and returns its new value.
258 * Value to add/subtract
262 static inline uint16_t
263 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
265 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
269 * Reads the value of an mbuf's refcnt.
273 * Reference count number.
275 static inline uint16_t
276 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
278 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
282 * Sets an mbuf's refcnt to a defined value.
289 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
291 rte_atomic16_set(&m->refcnt_atomic, new_value);
294 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
297 * Adds given value to an mbuf's refcnt and returns its new value.
299 static inline uint16_t
300 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
302 m->refcnt = (uint16_t)(m->refcnt + value);
307 * Reads the value of an mbuf's refcnt.
309 static inline uint16_t
310 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
316 * Sets an mbuf's refcnt to the defined value.
319 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
321 m->refcnt = new_value;
324 #endif /* RTE_MBUF_REFCNT_ATOMIC */
327 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
332 #else /* ! RTE_MBUF_REFCNT */
335 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
337 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
339 #endif /* RTE_MBUF_REFCNT */
343 * Sanity checks on an mbuf.
345 * Check the consistency of the given mbuf. The function will cause a
346 * panic if corruption is detected.
349 * The mbuf to be checked.
351 * True if the mbuf is a packet header, false if it is a sub-segment
352 * of a packet (in this case, some fields like nb_segs are not checked)
355 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
358 * @internal Allocate a new mbuf from mempool *mp*.
359 * The use of that function is reserved for RTE internal needs.
360 * Please use rte_pktmbuf_alloc().
363 * The mempool from which mbuf is allocated.
365 * - The pointer to the new mbuf on success.
366 * - NULL if allocation failed.
368 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
372 if (rte_mempool_get(mp, &mb) < 0)
374 m = (struct rte_mbuf *)mb;
375 #ifdef RTE_MBUF_REFCNT
376 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
377 rte_mbuf_refcnt_set(m, 1);
378 #endif /* RTE_MBUF_REFCNT */
383 * @internal Put mbuf back into its original mempool.
384 * The use of that function is reserved for RTE internal needs.
385 * Please use rte_pktmbuf_free().
388 * The mbuf to be freed.
390 static inline void __attribute__((always_inline))
391 __rte_mbuf_raw_free(struct rte_mbuf *m)
393 #ifdef RTE_MBUF_REFCNT
394 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
395 #endif /* RTE_MBUF_REFCNT */
396 rte_mempool_put(m->pool, m);
399 /* Operations on ctrl mbuf */
402 * The control mbuf constructor.
404 * This function initializes some fields in an mbuf structure that are
405 * not modified by the user once created (mbuf type, origin pool, buffer
406 * start address, and so on). This function is given as a callback function
407 * to rte_mempool_create() at pool creation time.
410 * The mempool from which the mbuf is allocated.
412 * A pointer that can be used by the user to retrieve useful information
413 * for mbuf initialization. This pointer comes from the ``init_arg``
414 * parameter of rte_mempool_create().
416 * The mbuf to initialize.
418 * The index of the mbuf in the pool table.
420 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
421 void *m, unsigned i);
424 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
426 * This new mbuf is initialized with data pointing to the beginning of
427 * buffer, and with a length of zero.
430 * The mempool from which the mbuf is allocated.
432 * - The pointer to the new mbuf on success.
433 * - NULL if allocation failed.
435 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
438 * Free a control mbuf back into its original mempool.
441 * The control mbuf to be freed.
443 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
446 * A macro that returns the pointer to the carried data.
448 * The value that can be read or assigned.
453 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
456 * A macro that returns the length of the carried data.
458 * The value that can be read or assigned.
463 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
466 * Tests if an mbuf is a control mbuf
469 * The mbuf to be tested
471 * - True (1) if the mbuf is a control mbuf
472 * - False(0) otherwise
475 rte_is_ctrlmbuf(struct rte_mbuf *m)
477 return (!!(m->ol_flags & CTRL_MBUF_FLAG));
480 /* Operations on pkt mbuf */
483 * The packet mbuf constructor.
485 * This function initializes some fields in the mbuf structure that are
486 * not modified by the user once created (origin pool, buffer start
487 * address, and so on). This function is given as a callback function to
488 * rte_mempool_create() at pool creation time.
491 * The mempool from which mbufs originate.
493 * A pointer that can be used by the user to retrieve useful information
494 * for mbuf initialization. This pointer comes from the ``init_arg``
495 * parameter of rte_mempool_create().
497 * The mbuf to initialize.
499 * The index of the mbuf in the pool table.
501 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
502 void *m, unsigned i);
506 * A packet mbuf pool constructor.
508 * This function initializes the mempool private data in the case of a
509 * pktmbuf pool. This private data is needed by the driver. The
510 * function is given as a callback function to rte_mempool_create() at
511 * pool creation. It can be extended by the user, for example, to
512 * provide another packet size.
515 * The mempool from which mbufs originate.
517 * A pointer that can be used by the user to retrieve useful information
518 * for mbuf initialization. This pointer comes from the ``init_arg``
519 * parameter of rte_mempool_create().
521 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
524 * Reset the fields of a packet mbuf to their default values.
526 * The given mbuf must have only one segment.
529 * The packet mbuf to be resetted.
531 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
541 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
542 RTE_PKTMBUF_HEADROOM : m->buf_len;
545 __rte_mbuf_sanity_check(m, 1);
549 * Allocate a new mbuf from a mempool.
551 * This new mbuf contains one segment, which has a length of 0. The pointer
552 * to data is initialized to have some bytes of headroom in the buffer
553 * (if buffer size allows).
556 * The mempool from which the mbuf is allocated.
558 * - The pointer to the new mbuf on success.
559 * - NULL if allocation failed.
561 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
564 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
565 rte_pktmbuf_reset(m);
569 #ifdef RTE_MBUF_REFCNT
572 * Attach packet mbuf to another packet mbuf.
573 * After attachment we refer the mbuf we attached as 'indirect',
574 * while mbuf we attached to as 'direct'.
575 * Right now, not supported:
576 * - attachment to indirect mbuf (e.g. - md has to be direct).
577 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
578 * - mbuf we trying to attach (mi) is used by someone else
579 * e.g. it's reference counter is greater then 1.
582 * The indirect packet mbuf.
584 * The direct packet mbuf.
587 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
589 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
590 RTE_MBUF_DIRECT(mi) &&
591 rte_mbuf_refcnt_read(mi) == 1);
593 rte_mbuf_refcnt_update(md, 1);
594 mi->buf_physaddr = md->buf_physaddr;
595 mi->buf_addr = md->buf_addr;
596 mi->buf_len = md->buf_len;
599 mi->data_off = md->data_off;
600 mi->data_len = md->data_len;
602 mi->vlan_tci = md->vlan_tci;
603 mi->l2_l3_len = md->l2_l3_len;
607 mi->pkt_len = mi->data_len;
609 mi->ol_flags = md->ol_flags;
611 __rte_mbuf_sanity_check(mi, 1);
612 __rte_mbuf_sanity_check(md, 0);
616 * Detach an indirect packet mbuf -
617 * - restore original mbuf address and length values.
618 * - reset pktmbuf data and data_len to their default values.
619 * All other fields of the given packet mbuf will be left intact.
622 * The indirect attached packet mbuf.
625 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
627 const struct rte_mempool *mp = m->pool;
628 void *buf = RTE_MBUF_TO_BADDR(m);
629 uint32_t buf_len = mp->elt_size - sizeof(*m);
630 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
633 m->buf_len = (uint16_t)buf_len;
635 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
636 RTE_PKTMBUF_HEADROOM : m->buf_len;
641 #endif /* RTE_MBUF_REFCNT */
644 static inline struct rte_mbuf* __attribute__((always_inline))
645 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
647 __rte_mbuf_sanity_check(m, 0);
649 #ifdef RTE_MBUF_REFCNT
650 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
651 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
652 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
654 rte_mbuf_refcnt_set(m, 0);
656 /* if this is an indirect mbuf, then
658 * - free attached mbuf segment
660 if (unlikely (md != m)) {
661 rte_pktmbuf_detach(m);
662 if (rte_mbuf_refcnt_update(md, -1) == 0)
663 __rte_mbuf_raw_free(md);
667 #ifdef RTE_MBUF_REFCNT
674 * Free a segment of a packet mbuf into its original mempool.
676 * Free an mbuf, without parsing other segments in case of chained
680 * The packet mbuf segment to be freed.
682 static inline void __attribute__((always_inline))
683 rte_pktmbuf_free_seg(struct rte_mbuf *m)
685 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
687 __rte_mbuf_raw_free(m);
692 * Free a packet mbuf back into its original mempool.
694 * Free an mbuf, and all its segments in case of chained buffers. Each
695 * segment is added back into its original mempool.
698 * The packet mbuf to be freed.
700 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
702 struct rte_mbuf *m_next;
704 __rte_mbuf_sanity_check(m, 1);
708 rte_pktmbuf_free_seg(m);
713 #ifdef RTE_MBUF_REFCNT
716 * Creates a "clone" of the given packet mbuf.
718 * Walks through all segments of the given packet mbuf, and for each of them:
719 * - Creates a new packet mbuf from the given pool.
720 * - Attaches newly created mbuf to the segment.
721 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
722 * from the original packet mbuf.
725 * The packet mbuf to be cloned.
727 * The mempool from which the "clone" mbufs are allocated.
729 * - The pointer to the new "clone" mbuf on success.
730 * - NULL if allocation fails.
732 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
733 struct rte_mempool *mp)
735 struct rte_mbuf *mc, *mi, **prev;
739 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
744 pktlen = md->pkt_len;
749 rte_pktmbuf_attach(mi, md);
752 } while ((md = md->next) != NULL &&
753 (mi = rte_pktmbuf_alloc(mp)) != NULL);
757 mc->pkt_len = pktlen;
759 /* Allocation of new indirect segment failed */
760 if (unlikely (mi == NULL)) {
761 rte_pktmbuf_free(mc);
765 __rte_mbuf_sanity_check(mc, 1);
770 * Adds given value to the refcnt of all packet mbuf segments.
772 * Walks through all segments of given packet mbuf and for each of them
773 * invokes rte_mbuf_refcnt_update().
776 * The packet mbuf whose refcnt to be updated.
778 * The value to add to the mbuf's segments refcnt.
780 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
782 __rte_mbuf_sanity_check(m, 1);
785 rte_mbuf_refcnt_update(m, v);
786 } while ((m = m->next) != NULL);
789 #endif /* RTE_MBUF_REFCNT */
792 * Get the headroom in a packet mbuf.
797 * The length of the headroom.
799 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
801 __rte_mbuf_sanity_check(m, 1);
806 * Get the tailroom of a packet mbuf.
811 * The length of the tailroom.
813 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
815 __rte_mbuf_sanity_check(m, 1);
816 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
821 * Get the last segment of the packet.
826 * The last segment of the given mbuf.
828 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
830 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
832 __rte_mbuf_sanity_check(m, 1);
833 while (m2->next != NULL)
839 * A macro that points to the start of the data in the mbuf.
841 * The returned pointer is cast to type t. Before using this
842 * function, the user must ensure that m_headlen(m) is large enough to
848 * The type to cast the result into.
850 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
853 * A macro that returns the length of the packet.
855 * The value can be read or assigned.
860 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
863 * A macro that returns the length of the segment.
865 * The value can be read or assigned.
870 #define rte_pktmbuf_data_len(m) ((m)->data_len)
873 * Prepend len bytes to an mbuf data area.
875 * Returns a pointer to the new
876 * data start address. If there is not enough headroom in the first
877 * segment, the function will return NULL, without modifying the mbuf.
882 * The amount of data to prepend (in bytes).
884 * A pointer to the start of the newly prepended data, or
885 * NULL if there is not enough headroom space in the first segment
887 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
890 __rte_mbuf_sanity_check(m, 1);
892 if (unlikely(len > rte_pktmbuf_headroom(m)))
896 m->data_len = (uint16_t)(m->data_len + len);
897 m->pkt_len = (m->pkt_len + len);
899 return (char *)m->buf_addr + m->data_off;
903 * Append len bytes to an mbuf.
905 * Append len bytes to an mbuf and return a pointer to the start address
906 * of the added data. If there is not enough tailroom in the last
907 * segment, the function will return NULL, without modifying the mbuf.
912 * The amount of data to append (in bytes).
914 * A pointer to the start of the newly appended data, or
915 * NULL if there is not enough tailroom space in the last segment
917 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
920 struct rte_mbuf *m_last;
922 __rte_mbuf_sanity_check(m, 1);
924 m_last = rte_pktmbuf_lastseg(m);
925 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
928 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
929 m_last->data_len = (uint16_t)(m_last->data_len + len);
930 m->pkt_len = (m->pkt_len + len);
935 * Remove len bytes at the beginning of an mbuf.
937 * Returns a pointer to the start address of the new data area. If the
938 * length is greater than the length of the first segment, then the
939 * function will fail and return NULL, without modifying the mbuf.
944 * The amount of data to remove (in bytes).
946 * A pointer to the new start of the data.
948 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
950 __rte_mbuf_sanity_check(m, 1);
952 if (unlikely(len > m->data_len))
955 m->data_len = (uint16_t)(m->data_len - len);
957 m->pkt_len = (m->pkt_len - len);
958 return (char *)m->buf_addr + m->data_off;
962 * Remove len bytes of data at the end of the mbuf.
964 * If the length is greater than the length of the last segment, the
965 * function will fail and return -1 without modifying the mbuf.
970 * The amount of data to remove (in bytes).
975 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
977 struct rte_mbuf *m_last;
979 __rte_mbuf_sanity_check(m, 1);
981 m_last = rte_pktmbuf_lastseg(m);
982 if (unlikely(len > m_last->data_len))
985 m_last->data_len = (uint16_t)(m_last->data_len - len);
986 m->pkt_len = (m->pkt_len - len);
991 * Test if mbuf data is contiguous.
996 * - 1, if all data is contiguous (one segment).
997 * - 0, if there is several segments.
999 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1001 __rte_mbuf_sanity_check(m, 1);
1002 return !!(m->nb_segs == 1);
1006 * Dump an mbuf structure to the console.
1008 * Dump all fields for the given packet mbuf and all its associated
1009 * segments (in the case of a chained buffer).
1012 * A pointer to a file for output
1016 * If dump_len != 0, also dump the "dump_len" first data bytes of
1019 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1025 #endif /* _RTE_MBUF_H_ */