4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * The mbuf library provides the ability to create and destroy buffers
42 * that may be used by the RTE application to store message
43 * buffers. The message buffers are stored in a mempool, using the
44 * RTE mempool library.
46 * This library provide an API to allocate/free packet mbufs, which are
47 * used to carry network packets.
49 * To understand the concepts of packet buffers or mbufs, you
50 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
51 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
52 * http://www.kohala.com/start/tcpipiv2.html
56 #include <rte_mempool.h>
57 #include <rte_atomic.h>
58 #include <rte_prefetch.h>
59 #include <rte_branch_prediction.h>
65 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
66 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
69 * Packet Offload Features Flags. It also carry packet type information.
70 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
72 #define PKT_RX_VLAN_PKT 0x0001 /**< RX packet is a 802.1q VLAN packet. */
73 #define PKT_RX_RSS_HASH 0x0002 /**< RX packet with RSS hash result. */
74 #define PKT_RX_FDIR 0x0004 /**< RX packet with FDIR infos. */
75 #define PKT_RX_L4_CKSUM_BAD 0x0008 /**< L4 cksum of RX pkt. is not OK. */
76 #define PKT_RX_IP_CKSUM_BAD 0x0010 /**< IP cksum of RX pkt. is not OK. */
77 #define PKT_RX_EIP_CKSUM_BAD 0x0000 /**< External IP header checksum error. */
78 #define PKT_RX_OVERSIZE 0x0000 /**< Num of desc of an RX pkt oversize. */
79 #define PKT_RX_HBUF_OVERFLOW 0x0000 /**< Header buffer overflow. */
80 #define PKT_RX_RECIP_ERR 0x0000 /**< Hardware processing error. */
81 #define PKT_RX_MAC_ERR 0x0000 /**< MAC error. */
82 #define PKT_RX_IPV4_HDR 0x0020 /**< RX packet with IPv4 header. */
83 #define PKT_RX_IPV4_HDR_EXT 0x0040 /**< RX packet with extended IPv4 header. */
84 #define PKT_RX_IPV6_HDR 0x0080 /**< RX packet with IPv6 header. */
85 #define PKT_RX_IPV6_HDR_EXT 0x0100 /**< RX packet with extended IPv6 header. */
86 #define PKT_RX_IEEE1588_PTP 0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
87 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
89 #define PKT_TX_VLAN_PKT 0x0800 /**< TX packet is a 802.1q VLAN packet. */
90 #define PKT_TX_IP_CKSUM 0x1000 /**< IP cksum of TX pkt. computed by NIC. */
91 #define PKT_TX_IPV4_CSUM 0x1000 /**< Alias of PKT_TX_IP_CKSUM. */
92 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR /**< IPv4 with no IP checksum offload. */
93 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR /**< IPv6 packet */
96 * Bit 14~13 used for L4 packet type with checksum enabled.
102 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
103 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
104 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
105 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
106 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
108 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
110 /* Use final bit of flags to indicate a control mbuf */
111 #define CTRL_MBUF_FLAG (1ULL << 63)
114 * Bit Mask to indicate what bits required for building TX context
116 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
119 * The generic rte_mbuf, containing a packet mbuf.
122 void *buf_addr; /**< Virtual address of segment buffer. */
123 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
125 /* next 8 bytes are initialised on RX descriptor rearm */
126 uint16_t buf_len; /**< Length of segment buffer. */
130 * 16-bit Reference counter.
131 * It should only be accessed using the following functions:
132 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
133 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
134 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
138 #ifdef RTE_MBUF_REFCNT
139 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
140 uint16_t refcnt; /**< Non-atomically accessed refcnt */
142 uint16_t refcnt_reserved; /**< Do not use this field */
144 uint8_t nb_segs; /**< Number of segments. */
145 uint8_t port; /**< Input port. */
147 uint64_t ol_flags; /**< Offload features. */
149 /* remaining bytes are set on RX when pulling packet from descriptor */
150 uint16_t reserved2; /**< Unused field. Required for padding */
151 uint16_t data_len; /**< Amount of data in segment buffer. */
152 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
154 uint16_t l2_l3_len; /**< combined l2/l3 lengths as single var */
156 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
157 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
160 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order) */
162 uint32_t rss; /**< RSS hash result if RSS enabled */
166 } fdir; /**< Filter identifier if FDIR enabled */
167 uint32_t sched; /**< Hierarchical scheduler */
168 } hash; /**< hash information */
170 /* fields only used in slow path or on TX */
171 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
172 struct rte_mbuf *next; /**< Next segment of scattered packet. */
174 } __rte_cache_aligned;
177 * Given the buf_addr returns the pointer to corresponding mbuf.
179 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
182 * Given the pointer to mbuf returns an address where it's buf_addr
185 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
188 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
190 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
193 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
195 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
199 * Private data in case of pktmbuf pool.
201 * A structure that contains some pktmbuf_pool-specific data that are
202 * appended after the mempool structure (in private data).
204 struct rte_pktmbuf_pool_private {
205 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
208 #ifdef RTE_LIBRTE_MBUF_DEBUG
210 /** check mbuf type in debug mode */
211 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
213 /** check mbuf type in debug mode if mbuf pointer is not null */
214 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
216 rte_mbuf_sanity_check(m, is_h); \
219 /** MBUF asserts in debug mode */
220 #define RTE_MBUF_ASSERT(exp) \
222 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
225 #else /* RTE_LIBRTE_MBUF_DEBUG */
227 /** check mbuf type in debug mode */
228 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
230 /** check mbuf type in debug mode if mbuf pointer is not null */
231 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
233 /** MBUF asserts in debug mode */
234 #define RTE_MBUF_ASSERT(exp) do { } while (0)
236 #endif /* RTE_LIBRTE_MBUF_DEBUG */
238 #ifdef RTE_MBUF_REFCNT
239 #ifdef RTE_MBUF_REFCNT_ATOMIC
242 * Adds given value to an mbuf's refcnt and returns its new value.
246 * Value to add/subtract
250 static inline uint16_t
251 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
253 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
257 * Reads the value of an mbuf's refcnt.
261 * Reference count number.
263 static inline uint16_t
264 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
266 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
270 * Sets an mbuf's refcnt to a defined value.
277 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
279 rte_atomic16_set(&m->refcnt_atomic, new_value);
282 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
285 * Adds given value to an mbuf's refcnt and returns its new value.
287 static inline uint16_t
288 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
290 m->refcnt = (uint16_t)(m->refcnt + value);
295 * Reads the value of an mbuf's refcnt.
297 static inline uint16_t
298 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
304 * Sets an mbuf's refcnt to the defined value.
307 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
309 m->refcnt = new_value;
312 #endif /* RTE_MBUF_REFCNT_ATOMIC */
315 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
320 #else /* ! RTE_MBUF_REFCNT */
323 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
325 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
327 #endif /* RTE_MBUF_REFCNT */
331 * Sanity checks on an mbuf.
333 * Check the consistency of the given mbuf. The function will cause a
334 * panic if corruption is detected.
337 * The mbuf to be checked.
339 * True if the mbuf is a packet header, false if it is a sub-segment
340 * of a packet (in this case, some fields like nb_segs are not checked)
343 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
346 * @internal Allocate a new mbuf from mempool *mp*.
347 * The use of that function is reserved for RTE internal needs.
348 * Please use rte_pktmbuf_alloc().
351 * The mempool from which mbuf is allocated.
353 * - The pointer to the new mbuf on success.
354 * - NULL if allocation failed.
356 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
360 if (rte_mempool_get(mp, &mb) < 0)
362 m = (struct rte_mbuf *)mb;
363 #ifdef RTE_MBUF_REFCNT
364 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
365 rte_mbuf_refcnt_set(m, 1);
366 #endif /* RTE_MBUF_REFCNT */
371 * @internal Put mbuf back into its original mempool.
372 * The use of that function is reserved for RTE internal needs.
373 * Please use rte_pktmbuf_free().
376 * The mbuf to be freed.
378 static inline void __attribute__((always_inline))
379 __rte_mbuf_raw_free(struct rte_mbuf *m)
381 #ifdef RTE_MBUF_REFCNT
382 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
383 #endif /* RTE_MBUF_REFCNT */
384 rte_mempool_put(m->pool, m);
387 /* Operations on ctrl mbuf */
390 * The control mbuf constructor.
392 * This function initializes some fields in an mbuf structure that are
393 * not modified by the user once created (mbuf type, origin pool, buffer
394 * start address, and so on). This function is given as a callback function
395 * to rte_mempool_create() at pool creation time.
398 * The mempool from which the mbuf is allocated.
400 * A pointer that can be used by the user to retrieve useful information
401 * for mbuf initialization. This pointer comes from the ``init_arg``
402 * parameter of rte_mempool_create().
404 * The mbuf to initialize.
406 * The index of the mbuf in the pool table.
408 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
409 void *m, unsigned i);
412 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
414 * This new mbuf is initialized with data pointing to the beginning of
415 * buffer, and with a length of zero.
418 * The mempool from which the mbuf is allocated.
420 * - The pointer to the new mbuf on success.
421 * - NULL if allocation failed.
423 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
426 * Free a control mbuf back into its original mempool.
429 * The control mbuf to be freed.
431 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
434 * A macro that returns the pointer to the carried data.
436 * The value that can be read or assigned.
441 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
444 * A macro that returns the length of the carried data.
446 * The value that can be read or assigned.
451 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
454 * Tests if an mbuf is a control mbuf
457 * The mbuf to be tested
459 * - True (1) if the mbuf is a control mbuf
460 * - False(0) otherwise
463 rte_is_ctrlmbuf(struct rte_mbuf *m)
465 return (!!(m->ol_flags & CTRL_MBUF_FLAG));
468 /* Operations on pkt mbuf */
471 * The packet mbuf constructor.
473 * This function initializes some fields in the mbuf structure that are
474 * not modified by the user once created (origin pool, buffer start
475 * address, and so on). This function is given as a callback function to
476 * rte_mempool_create() at pool creation time.
479 * The mempool from which mbufs originate.
481 * A pointer that can be used by the user to retrieve useful information
482 * for mbuf initialization. This pointer comes from the ``init_arg``
483 * parameter of rte_mempool_create().
485 * The mbuf to initialize.
487 * The index of the mbuf in the pool table.
489 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
490 void *m, unsigned i);
494 * A packet mbuf pool constructor.
496 * This function initializes the mempool private data in the case of a
497 * pktmbuf pool. This private data is needed by the driver. The
498 * function is given as a callback function to rte_mempool_create() at
499 * pool creation. It can be extended by the user, for example, to
500 * provide another packet size.
503 * The mempool from which mbufs originate.
505 * A pointer that can be used by the user to retrieve useful information
506 * for mbuf initialization. This pointer comes from the ``init_arg``
507 * parameter of rte_mempool_create().
509 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
512 * Reset the fields of a packet mbuf to their default values.
514 * The given mbuf must have only one segment.
517 * The packet mbuf to be resetted.
519 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
529 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
530 RTE_PKTMBUF_HEADROOM : m->buf_len;
533 __rte_mbuf_sanity_check(m, 1);
537 * Allocate a new mbuf from a mempool.
539 * This new mbuf contains one segment, which has a length of 0. The pointer
540 * to data is initialized to have some bytes of headroom in the buffer
541 * (if buffer size allows).
544 * The mempool from which the mbuf is allocated.
546 * - The pointer to the new mbuf on success.
547 * - NULL if allocation failed.
549 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
552 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
553 rte_pktmbuf_reset(m);
557 #ifdef RTE_MBUF_REFCNT
560 * Attach packet mbuf to another packet mbuf.
561 * After attachment we refer the mbuf we attached as 'indirect',
562 * while mbuf we attached to as 'direct'.
563 * Right now, not supported:
564 * - attachment to indirect mbuf (e.g. - md has to be direct).
565 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
566 * - mbuf we trying to attach (mi) is used by someone else
567 * e.g. it's reference counter is greater then 1.
570 * The indirect packet mbuf.
572 * The direct packet mbuf.
575 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
577 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
578 RTE_MBUF_DIRECT(mi) &&
579 rte_mbuf_refcnt_read(mi) == 1);
581 rte_mbuf_refcnt_update(md, 1);
582 mi->buf_physaddr = md->buf_physaddr;
583 mi->buf_addr = md->buf_addr;
584 mi->buf_len = md->buf_len;
587 mi->data_off = md->data_off;
588 mi->data_len = md->data_len;
590 mi->vlan_tci = md->vlan_tci;
591 mi->l2_l3_len = md->l2_l3_len;
595 mi->pkt_len = mi->data_len;
597 mi->ol_flags = md->ol_flags;
599 __rte_mbuf_sanity_check(mi, 1);
600 __rte_mbuf_sanity_check(md, 0);
604 * Detach an indirect packet mbuf -
605 * - restore original mbuf address and length values.
606 * - reset pktmbuf data and data_len to their default values.
607 * All other fields of the given packet mbuf will be left intact.
610 * The indirect attached packet mbuf.
613 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
615 const struct rte_mempool *mp = m->pool;
616 void *buf = RTE_MBUF_TO_BADDR(m);
617 uint32_t buf_len = mp->elt_size - sizeof(*m);
618 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
621 m->buf_len = (uint16_t)buf_len;
623 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
624 RTE_PKTMBUF_HEADROOM : m->buf_len;
629 #endif /* RTE_MBUF_REFCNT */
632 static inline struct rte_mbuf* __attribute__((always_inline))
633 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
635 __rte_mbuf_sanity_check(m, 0);
637 #ifdef RTE_MBUF_REFCNT
638 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
639 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
640 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
642 rte_mbuf_refcnt_set(m, 0);
644 /* if this is an indirect mbuf, then
646 * - free attached mbuf segment
648 if (unlikely (md != m)) {
649 rte_pktmbuf_detach(m);
650 if (rte_mbuf_refcnt_update(md, -1) == 0)
651 __rte_mbuf_raw_free(md);
655 #ifdef RTE_MBUF_REFCNT
662 * Free a segment of a packet mbuf into its original mempool.
664 * Free an mbuf, without parsing other segments in case of chained
668 * The packet mbuf segment to be freed.
670 static inline void __attribute__((always_inline))
671 rte_pktmbuf_free_seg(struct rte_mbuf *m)
673 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m))))
674 __rte_mbuf_raw_free(m);
678 * Free a packet mbuf back into its original mempool.
680 * Free an mbuf, and all its segments in case of chained buffers. Each
681 * segment is added back into its original mempool.
684 * The packet mbuf to be freed.
686 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
688 struct rte_mbuf *m_next;
690 __rte_mbuf_sanity_check(m, 1);
694 rte_pktmbuf_free_seg(m);
699 #ifdef RTE_MBUF_REFCNT
702 * Creates a "clone" of the given packet mbuf.
704 * Walks through all segments of the given packet mbuf, and for each of them:
705 * - Creates a new packet mbuf from the given pool.
706 * - Attaches newly created mbuf to the segment.
707 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
708 * from the original packet mbuf.
711 * The packet mbuf to be cloned.
713 * The mempool from which the "clone" mbufs are allocated.
715 * - The pointer to the new "clone" mbuf on success.
716 * - NULL if allocation fails.
718 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
719 struct rte_mempool *mp)
721 struct rte_mbuf *mc, *mi, **prev;
725 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
730 pktlen = md->pkt_len;
735 rte_pktmbuf_attach(mi, md);
738 } while ((md = md->next) != NULL &&
739 (mi = rte_pktmbuf_alloc(mp)) != NULL);
743 mc->pkt_len = pktlen;
745 /* Allocation of new indirect segment failed */
746 if (unlikely (mi == NULL)) {
747 rte_pktmbuf_free(mc);
751 __rte_mbuf_sanity_check(mc, 1);
756 * Adds given value to the refcnt of all packet mbuf segments.
758 * Walks through all segments of given packet mbuf and for each of them
759 * invokes rte_mbuf_refcnt_update().
762 * The packet mbuf whose refcnt to be updated.
764 * The value to add to the mbuf's segments refcnt.
766 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
768 __rte_mbuf_sanity_check(m, 1);
771 rte_mbuf_refcnt_update(m, v);
772 } while ((m = m->next) != NULL);
775 #endif /* RTE_MBUF_REFCNT */
778 * Get the headroom in a packet mbuf.
783 * The length of the headroom.
785 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
787 __rte_mbuf_sanity_check(m, 1);
792 * Get the tailroom of a packet mbuf.
797 * The length of the tailroom.
799 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
801 __rte_mbuf_sanity_check(m, 1);
802 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
807 * Get the last segment of the packet.
812 * The last segment of the given mbuf.
814 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
816 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
818 __rte_mbuf_sanity_check(m, 1);
819 while (m2->next != NULL)
825 * A macro that points to the start of the data in the mbuf.
827 * The returned pointer is cast to type t. Before using this
828 * function, the user must ensure that m_headlen(m) is large enough to
834 * The type to cast the result into.
836 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
839 * A macro that returns the length of the packet.
841 * The value can be read or assigned.
846 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
849 * A macro that returns the length of the segment.
851 * The value can be read or assigned.
856 #define rte_pktmbuf_data_len(m) ((m)->data_len)
859 * Prepend len bytes to an mbuf data area.
861 * Returns a pointer to the new
862 * data start address. If there is not enough headroom in the first
863 * segment, the function will return NULL, without modifying the mbuf.
868 * The amount of data to prepend (in bytes).
870 * A pointer to the start of the newly prepended data, or
871 * NULL if there is not enough headroom space in the first segment
873 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
876 __rte_mbuf_sanity_check(m, 1);
878 if (unlikely(len > rte_pktmbuf_headroom(m)))
882 m->data_len = (uint16_t)(m->data_len + len);
883 m->pkt_len = (m->pkt_len + len);
885 return (char *)m->buf_addr + m->data_off;
889 * Append len bytes to an mbuf.
891 * Append len bytes to an mbuf and return a pointer to the start address
892 * of the added data. If there is not enough tailroom in the last
893 * segment, the function will return NULL, without modifying the mbuf.
898 * The amount of data to append (in bytes).
900 * A pointer to the start of the newly appended data, or
901 * NULL if there is not enough tailroom space in the last segment
903 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
906 struct rte_mbuf *m_last;
908 __rte_mbuf_sanity_check(m, 1);
910 m_last = rte_pktmbuf_lastseg(m);
911 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
914 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
915 m_last->data_len = (uint16_t)(m_last->data_len + len);
916 m->pkt_len = (m->pkt_len + len);
921 * Remove len bytes at the beginning of an mbuf.
923 * Returns a pointer to the start address of the new data area. If the
924 * length is greater than the length of the first segment, then the
925 * function will fail and return NULL, without modifying the mbuf.
930 * The amount of data to remove (in bytes).
932 * A pointer to the new start of the data.
934 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
936 __rte_mbuf_sanity_check(m, 1);
938 if (unlikely(len > m->data_len))
941 m->data_len = (uint16_t)(m->data_len - len);
943 m->pkt_len = (m->pkt_len - len);
944 return (char *)m->buf_addr + m->data_off;
948 * Remove len bytes of data at the end of the mbuf.
950 * If the length is greater than the length of the last segment, the
951 * function will fail and return -1 without modifying the mbuf.
956 * The amount of data to remove (in bytes).
961 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
963 struct rte_mbuf *m_last;
965 __rte_mbuf_sanity_check(m, 1);
967 m_last = rte_pktmbuf_lastseg(m);
968 if (unlikely(len > m_last->data_len))
971 m_last->data_len = (uint16_t)(m_last->data_len - len);
972 m->pkt_len = (m->pkt_len - len);
977 * Test if mbuf data is contiguous.
982 * - 1, if all data is contiguous (one segment).
983 * - 0, if there is several segments.
985 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
987 __rte_mbuf_sanity_check(m, 1);
988 return !!(m->nb_segs == 1);
992 * Dump an mbuf structure to the console.
994 * Dump all fields for the given packet mbuf and all its associated
995 * segments (in the case of a chained buffer).
998 * A pointer to a file for output
1002 * If dump_len != 0, also dump the "dump_len" first data bytes of
1005 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1011 #endif /* _RTE_MBUF_H_ */