4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * The mbuf library provides the ability to create and destroy buffers
43 * that may be used by the RTE application to store message
44 * buffers. The message buffers are stored in a mempool, using the
45 * RTE mempool library.
47 * This library provide an API to allocate/free packet mbufs, which are
48 * used to carry network packets.
50 * To understand the concepts of packet buffers or mbufs, you
51 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
52 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
53 * http://www.kohala.com/start/tcpipiv2.html
57 #include <rte_mempool.h>
58 #include <rte_atomic.h>
59 #include <rte_prefetch.h>
60 #include <rte_branch_prediction.h>
66 /* deprecated feature, renamed in RTE_MBUF_REFCNT */
67 #pragma GCC poison RTE_MBUF_SCATTER_GATHER
70 * Packet Offload Features Flags. It also carry packet type information.
71 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
73 * - RX flags start at bit position zero, and get added to the left of previous
75 * - The most-significant 8 bits are reserved for generic mbuf flags
76 * - TX flags therefore start at bit position 55 (i.e. 63-8), and new flags get
77 * added to the right of the previously defined flags
79 * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
80 * rte_get_tx_ol_flag_name().
82 #define PKT_RX_VLAN_PKT (1ULL << 0) /**< RX packet is a 802.1q VLAN packet. */
83 #define PKT_RX_RSS_HASH (1ULL << 1) /**< RX packet with RSS hash result. */
84 #define PKT_RX_FDIR (1ULL << 2) /**< RX packet with FDIR match indicate. */
85 #define PKT_RX_L4_CKSUM_BAD (1ULL << 3) /**< L4 cksum of RX pkt. is not OK. */
86 #define PKT_RX_IP_CKSUM_BAD (1ULL << 4) /**< IP cksum of RX pkt. is not OK. */
87 #define PKT_RX_EIP_CKSUM_BAD (0ULL << 0) /**< External IP header checksum error. */
88 #define PKT_RX_OVERSIZE (0ULL << 0) /**< Num of desc of an RX pkt oversize. */
89 #define PKT_RX_HBUF_OVERFLOW (0ULL << 0) /**< Header buffer overflow. */
90 #define PKT_RX_RECIP_ERR (0ULL << 0) /**< Hardware processing error. */
91 #define PKT_RX_MAC_ERR (0ULL << 0) /**< MAC error. */
92 #define PKT_RX_IPV4_HDR (1ULL << 5) /**< RX packet with IPv4 header. */
93 #define PKT_RX_IPV4_HDR_EXT (1ULL << 6) /**< RX packet with extended IPv4 header. */
94 #define PKT_RX_IPV6_HDR (1ULL << 7) /**< RX packet with IPv6 header. */
95 #define PKT_RX_IPV6_HDR_EXT (1ULL << 8) /**< RX packet with extended IPv6 header. */
96 #define PKT_RX_IEEE1588_PTP (1ULL << 9) /**< RX IEEE1588 L2 Ethernet PT Packet. */
97 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
98 #define PKT_RX_TUNNEL_IPV4_HDR (1ULL << 11) /**< RX tunnel packet with IPv4 header.*/
99 #define PKT_RX_TUNNEL_IPV6_HDR (1ULL << 12) /**< RX tunnel packet with IPv6 header. */
100 #define PKT_RX_FDIR_ID (1ULL << 13) /**< FD id reported if FDIR match. */
101 #define PKT_RX_FDIR_FLX (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
102 /* add new RX flags here */
104 /* add new TX flags here */
105 #define PKT_TX_VXLAN_CKSUM (1ULL << 50) /**< TX checksum of VXLAN computed by NIC */
106 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
109 * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
110 * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
111 * L4 checksum offload, the user needs to:
112 * - fill l2_len and l3_len in mbuf
113 * - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
114 * - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
115 * - calculate the pseudo header checksum and set it in the L4 header (only
116 * for TCP or UDP). See rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum().
117 * For SCTP, set the crc field to 0.
119 #define PKT_TX_L4_NO_CKSUM (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
120 #define PKT_TX_TCP_CKSUM (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
121 #define PKT_TX_SCTP_CKSUM (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
122 #define PKT_TX_UDP_CKSUM (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
123 #define PKT_TX_L4_MASK (3ULL << 52) /**< Mask for L4 cksum offload request. */
125 #define PKT_TX_IP_CKSUM (1ULL << 54) /**< IP cksum of TX pkt. computed by NIC. */
126 #define PKT_TX_IPV4_CSUM PKT_TX_IP_CKSUM /**< Alias of PKT_TX_IP_CKSUM. */
128 /** Tell the NIC it's an IPv4 packet. Required for L4 checksum offload. */
129 #define PKT_TX_IPV4 PKT_RX_IPV4_HDR
131 /** Tell the NIC it's an IPv6 packet. Required for L4 checksum offload. */
132 #define PKT_TX_IPV6 PKT_RX_IPV6_HDR
134 #define PKT_TX_VLAN_PKT (1ULL << 55) /**< TX packet is a 802.1q VLAN packet. */
137 * TCP segmentation offload. To enable this offload feature for a
138 * packet to be transmitted on hardware supporting TSO:
139 * - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag implies
141 * - if it's IPv4, set the PKT_TX_IP_CKSUM flag and write the IP checksum
143 * - fill the mbuf offload information: l2_len, l3_len, l4_len, tso_segsz
144 * - calculate the pseudo header checksum without taking ip_len in accound,
145 * and set it in the TCP header. Refer to rte_ipv4_phdr_cksum() and
146 * rte_ipv6_phdr_cksum() that can be used as helpers.
148 #define PKT_TX_TCP_SEG (1ULL << 49)
150 /* Use final bit of flags to indicate a control mbuf */
151 #define CTRL_MBUF_FLAG (1ULL << 63) /**< Mbuf contains control data */
154 * Get the name of a RX offload flag
157 * The mask describing the flag.
159 * The name of this flag, or NULL if it's not a valid RX flag.
161 const char *rte_get_rx_ol_flag_name(uint64_t mask);
164 * Get the name of a TX offload flag
167 * The mask describing the flag. Usually only one bit must be set.
168 * Several bits can be given if they belong to the same mask.
169 * Ex: PKT_TX_L4_MASK.
171 * The name of this flag, or NULL if it's not a valid TX flag.
173 const char *rte_get_tx_ol_flag_name(uint64_t mask);
175 /* define a set of marker types that can be used to refer to set points in the
177 typedef void *MARKER[0]; /**< generic marker for a point in a structure */
178 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
179 * with a single assignment */
181 * The generic rte_mbuf, containing a packet mbuf.
186 void *buf_addr; /**< Virtual address of segment buffer. */
187 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
189 /* next 8 bytes are initialised on RX descriptor rearm */
191 uint16_t buf_len; /**< Length of segment buffer. */
195 * 16-bit Reference counter.
196 * It should only be accessed using the following functions:
197 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
198 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
199 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
203 #ifdef RTE_MBUF_REFCNT
204 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
205 uint16_t refcnt; /**< Non-atomically accessed refcnt */
207 uint16_t refcnt_reserved; /**< Do not use this field */
209 uint8_t nb_segs; /**< Number of segments. */
210 uint8_t port; /**< Input port. */
212 uint64_t ol_flags; /**< Offload features. */
214 /* remaining bytes are set on RX when pulling packet from descriptor */
215 MARKER rx_descriptor_fields1;
218 * The packet type, which is used to indicate ordinary packet and also
219 * tunneled packet format, i.e. each number is represented a type of
222 uint16_t packet_type;
224 uint16_t data_len; /**< Amount of data in segment buffer. */
225 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
226 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order) */
229 uint32_t rss; /**< RSS hash result if RSS enabled */
237 /**< Second 4 flexible bytes */
240 /**< First 4 flexible bytes or FD ID, dependent on
241 PKT_RX_FDIR_* flag in ol_flags. */
242 } fdir; /**< Filter identifier if FDIR enabled */
243 uint32_t sched; /**< Hierarchical scheduler */
244 uint32_t usr; /**< User defined tags. See @rte_distributor_process */
245 } hash; /**< hash information */
247 /* second cache line - fields only used in slow path or on TX */
248 MARKER cacheline1 __rte_cache_aligned;
251 void *userdata; /**< Can be used for external metadata */
252 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
255 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
256 struct rte_mbuf *next; /**< Next segment of scattered packet. */
258 /* fields to support TX offloads */
260 uint64_t tx_offload; /**< combined for easy fetch */
262 uint64_t l2_len:7; /**< L2 (MAC) Header Length. */
263 uint64_t l3_len:9; /**< L3 (IP) Header Length. */
264 uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
265 uint64_t tso_segsz:16; /**< TCP TSO segment size */
267 /* fields for TX offloading of tunnels */
268 uint64_t inner_l3_len:9; /**< inner L3 (IP) Hdr Length. */
269 uint64_t inner_l2_len:7; /**< inner L2 (MAC) Hdr Length. */
271 /* uint64_t unused:8; */
274 } __rte_cache_aligned;
277 * Given the buf_addr returns the pointer to corresponding mbuf.
279 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
282 * Given the pointer to mbuf returns an address where it's buf_addr
285 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
288 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
290 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
293 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
295 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
299 * Private data in case of pktmbuf pool.
301 * A structure that contains some pktmbuf_pool-specific data that are
302 * appended after the mempool structure (in private data).
304 struct rte_pktmbuf_pool_private {
305 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
308 #ifdef RTE_LIBRTE_MBUF_DEBUG
310 /** check mbuf type in debug mode */
311 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
313 /** check mbuf type in debug mode if mbuf pointer is not null */
314 #define __rte_mbuf_sanity_check_raw(m, is_h) do { \
316 rte_mbuf_sanity_check(m, is_h); \
319 /** MBUF asserts in debug mode */
320 #define RTE_MBUF_ASSERT(exp) \
322 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
325 #else /* RTE_LIBRTE_MBUF_DEBUG */
327 /** check mbuf type in debug mode */
328 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
330 /** check mbuf type in debug mode if mbuf pointer is not null */
331 #define __rte_mbuf_sanity_check_raw(m, is_h) do { } while (0)
333 /** MBUF asserts in debug mode */
334 #define RTE_MBUF_ASSERT(exp) do { } while (0)
336 #endif /* RTE_LIBRTE_MBUF_DEBUG */
338 #ifdef RTE_MBUF_REFCNT
339 #ifdef RTE_MBUF_REFCNT_ATOMIC
342 * Adds given value to an mbuf's refcnt and returns its new value.
346 * Value to add/subtract
350 static inline uint16_t
351 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
353 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
357 * Reads the value of an mbuf's refcnt.
361 * Reference count number.
363 static inline uint16_t
364 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
366 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
370 * Sets an mbuf's refcnt to a defined value.
377 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
379 rte_atomic16_set(&m->refcnt_atomic, new_value);
382 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
385 * Adds given value to an mbuf's refcnt and returns its new value.
387 static inline uint16_t
388 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
390 m->refcnt = (uint16_t)(m->refcnt + value);
395 * Reads the value of an mbuf's refcnt.
397 static inline uint16_t
398 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
404 * Sets an mbuf's refcnt to the defined value.
407 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
409 m->refcnt = new_value;
412 #endif /* RTE_MBUF_REFCNT_ATOMIC */
415 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
420 #else /* ! RTE_MBUF_REFCNT */
423 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
425 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
427 #endif /* RTE_MBUF_REFCNT */
431 * Sanity checks on an mbuf.
433 * Check the consistency of the given mbuf. The function will cause a
434 * panic if corruption is detected.
437 * The mbuf to be checked.
439 * True if the mbuf is a packet header, false if it is a sub-segment
440 * of a packet (in this case, some fields like nb_segs are not checked)
443 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
446 * @internal Allocate a new mbuf from mempool *mp*.
447 * The use of that function is reserved for RTE internal needs.
448 * Please use rte_pktmbuf_alloc().
451 * The mempool from which mbuf is allocated.
453 * - The pointer to the new mbuf on success.
454 * - NULL if allocation failed.
456 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
460 if (rte_mempool_get(mp, &mb) < 0)
462 m = (struct rte_mbuf *)mb;
463 #ifdef RTE_MBUF_REFCNT
464 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
465 rte_mbuf_refcnt_set(m, 1);
466 #endif /* RTE_MBUF_REFCNT */
471 * @internal Put mbuf back into its original mempool.
472 * The use of that function is reserved for RTE internal needs.
473 * Please use rte_pktmbuf_free().
476 * The mbuf to be freed.
478 static inline void __attribute__((always_inline))
479 __rte_mbuf_raw_free(struct rte_mbuf *m)
481 #ifdef RTE_MBUF_REFCNT
482 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
483 #endif /* RTE_MBUF_REFCNT */
484 rte_mempool_put(m->pool, m);
487 /* Operations on ctrl mbuf */
490 * The control mbuf constructor.
492 * This function initializes some fields in an mbuf structure that are
493 * not modified by the user once created (mbuf type, origin pool, buffer
494 * start address, and so on). This function is given as a callback function
495 * to rte_mempool_create() at pool creation time.
498 * The mempool from which the mbuf is allocated.
500 * A pointer that can be used by the user to retrieve useful information
501 * for mbuf initialization. This pointer comes from the ``init_arg``
502 * parameter of rte_mempool_create().
504 * The mbuf to initialize.
506 * The index of the mbuf in the pool table.
508 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
509 void *m, unsigned i);
512 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
514 * This new mbuf is initialized with data pointing to the beginning of
515 * buffer, and with a length of zero.
518 * The mempool from which the mbuf is allocated.
520 * - The pointer to the new mbuf on success.
521 * - NULL if allocation failed.
523 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
526 * Free a control mbuf back into its original mempool.
529 * The control mbuf to be freed.
531 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
534 * A macro that returns the pointer to the carried data.
536 * The value that can be read or assigned.
541 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
544 * A macro that returns the length of the carried data.
546 * The value that can be read or assigned.
551 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
554 * Tests if an mbuf is a control mbuf
557 * The mbuf to be tested
559 * - True (1) if the mbuf is a control mbuf
560 * - False(0) otherwise
563 rte_is_ctrlmbuf(struct rte_mbuf *m)
565 return (!!(m->ol_flags & CTRL_MBUF_FLAG));
568 /* Operations on pkt mbuf */
571 * The packet mbuf constructor.
573 * This function initializes some fields in the mbuf structure that are
574 * not modified by the user once created (origin pool, buffer start
575 * address, and so on). This function is given as a callback function to
576 * rte_mempool_create() at pool creation time.
579 * The mempool from which mbufs originate.
581 * A pointer that can be used by the user to retrieve useful information
582 * for mbuf initialization. This pointer comes from the ``init_arg``
583 * parameter of rte_mempool_create().
585 * The mbuf to initialize.
587 * The index of the mbuf in the pool table.
589 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
590 void *m, unsigned i);
594 * A packet mbuf pool constructor.
596 * This function initializes the mempool private data in the case of a
597 * pktmbuf pool. This private data is needed by the driver. The
598 * function is given as a callback function to rte_mempool_create() at
599 * pool creation. It can be extended by the user, for example, to
600 * provide another packet size.
603 * The mempool from which mbufs originate.
605 * A pointer that can be used by the user to retrieve useful information
606 * for mbuf initialization. This pointer comes from the ``init_arg``
607 * parameter of rte_mempool_create().
609 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
612 * Reset the fields of a packet mbuf to their default values.
614 * The given mbuf must have only one segment.
617 * The packet mbuf to be resetted.
619 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
630 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
631 RTE_PKTMBUF_HEADROOM : m->buf_len;
634 __rte_mbuf_sanity_check(m, 1);
638 * Allocate a new mbuf from a mempool.
640 * This new mbuf contains one segment, which has a length of 0. The pointer
641 * to data is initialized to have some bytes of headroom in the buffer
642 * (if buffer size allows).
645 * The mempool from which the mbuf is allocated.
647 * - The pointer to the new mbuf on success.
648 * - NULL if allocation failed.
650 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
653 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
654 rte_pktmbuf_reset(m);
658 #ifdef RTE_MBUF_REFCNT
661 * Attach packet mbuf to another packet mbuf.
662 * After attachment we refer the mbuf we attached as 'indirect',
663 * while mbuf we attached to as 'direct'.
664 * Right now, not supported:
665 * - attachment to indirect mbuf (e.g. - md has to be direct).
666 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
667 * - mbuf we trying to attach (mi) is used by someone else
668 * e.g. it's reference counter is greater then 1.
671 * The indirect packet mbuf.
673 * The direct packet mbuf.
676 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
678 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
679 RTE_MBUF_DIRECT(mi) &&
680 rte_mbuf_refcnt_read(mi) == 1);
682 rte_mbuf_refcnt_update(md, 1);
683 mi->buf_physaddr = md->buf_physaddr;
684 mi->buf_addr = md->buf_addr;
685 mi->buf_len = md->buf_len;
688 mi->data_off = md->data_off;
689 mi->data_len = md->data_len;
691 mi->vlan_tci = md->vlan_tci;
692 mi->tx_offload = md->tx_offload;
696 mi->pkt_len = mi->data_len;
698 mi->ol_flags = md->ol_flags;
699 mi->packet_type = md->packet_type;
701 __rte_mbuf_sanity_check(mi, 1);
702 __rte_mbuf_sanity_check(md, 0);
706 * Detach an indirect packet mbuf -
707 * - restore original mbuf address and length values.
708 * - reset pktmbuf data and data_len to their default values.
709 * All other fields of the given packet mbuf will be left intact.
712 * The indirect attached packet mbuf.
715 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
717 const struct rte_mempool *mp = m->pool;
718 void *buf = RTE_MBUF_TO_BADDR(m);
719 uint32_t buf_len = mp->elt_size - sizeof(*m);
720 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
723 m->buf_len = (uint16_t)buf_len;
725 m->data_off = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
726 RTE_PKTMBUF_HEADROOM : m->buf_len;
731 #endif /* RTE_MBUF_REFCNT */
734 static inline struct rte_mbuf* __attribute__((always_inline))
735 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
737 __rte_mbuf_sanity_check(m, 0);
739 #ifdef RTE_MBUF_REFCNT
740 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
741 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
742 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
744 rte_mbuf_refcnt_set(m, 0);
746 /* if this is an indirect mbuf, then
748 * - free attached mbuf segment
750 if (unlikely (md != m)) {
751 rte_pktmbuf_detach(m);
752 if (rte_mbuf_refcnt_update(md, -1) == 0)
753 __rte_mbuf_raw_free(md);
757 #ifdef RTE_MBUF_REFCNT
764 * Free a segment of a packet mbuf into its original mempool.
766 * Free an mbuf, without parsing other segments in case of chained
770 * The packet mbuf segment to be freed.
772 static inline void __attribute__((always_inline))
773 rte_pktmbuf_free_seg(struct rte_mbuf *m)
775 if (likely(NULL != (m = __rte_pktmbuf_prefree_seg(m)))) {
777 __rte_mbuf_raw_free(m);
782 * Free a packet mbuf back into its original mempool.
784 * Free an mbuf, and all its segments in case of chained buffers. Each
785 * segment is added back into its original mempool.
788 * The packet mbuf to be freed.
790 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
792 struct rte_mbuf *m_next;
794 __rte_mbuf_sanity_check(m, 1);
798 rte_pktmbuf_free_seg(m);
803 #ifdef RTE_MBUF_REFCNT
806 * Creates a "clone" of the given packet mbuf.
808 * Walks through all segments of the given packet mbuf, and for each of them:
809 * - Creates a new packet mbuf from the given pool.
810 * - Attaches newly created mbuf to the segment.
811 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
812 * from the original packet mbuf.
815 * The packet mbuf to be cloned.
817 * The mempool from which the "clone" mbufs are allocated.
819 * - The pointer to the new "clone" mbuf on success.
820 * - NULL if allocation fails.
822 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
823 struct rte_mempool *mp)
825 struct rte_mbuf *mc, *mi, **prev;
829 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
834 pktlen = md->pkt_len;
839 rte_pktmbuf_attach(mi, md);
842 } while ((md = md->next) != NULL &&
843 (mi = rte_pktmbuf_alloc(mp)) != NULL);
847 mc->pkt_len = pktlen;
849 /* Allocation of new indirect segment failed */
850 if (unlikely (mi == NULL)) {
851 rte_pktmbuf_free(mc);
855 __rte_mbuf_sanity_check(mc, 1);
860 * Adds given value to the refcnt of all packet mbuf segments.
862 * Walks through all segments of given packet mbuf and for each of them
863 * invokes rte_mbuf_refcnt_update().
866 * The packet mbuf whose refcnt to be updated.
868 * The value to add to the mbuf's segments refcnt.
870 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
872 __rte_mbuf_sanity_check(m, 1);
875 rte_mbuf_refcnt_update(m, v);
876 } while ((m = m->next) != NULL);
879 #endif /* RTE_MBUF_REFCNT */
882 * Get the headroom in a packet mbuf.
887 * The length of the headroom.
889 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
891 __rte_mbuf_sanity_check(m, 1);
896 * Get the tailroom of a packet mbuf.
901 * The length of the tailroom.
903 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
905 __rte_mbuf_sanity_check(m, 1);
906 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
911 * Get the last segment of the packet.
916 * The last segment of the given mbuf.
918 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
920 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
922 __rte_mbuf_sanity_check(m, 1);
923 while (m2->next != NULL)
929 * A macro that points to the start of the data in the mbuf.
931 * The returned pointer is cast to type t. Before using this
932 * function, the user must ensure that m_headlen(m) is large enough to
938 * The type to cast the result into.
940 #define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off))
943 * A macro that returns the length of the packet.
945 * The value can be read or assigned.
950 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
953 * A macro that returns the length of the segment.
955 * The value can be read or assigned.
960 #define rte_pktmbuf_data_len(m) ((m)->data_len)
963 * Prepend len bytes to an mbuf data area.
965 * Returns a pointer to the new
966 * data start address. If there is not enough headroom in the first
967 * segment, the function will return NULL, without modifying the mbuf.
972 * The amount of data to prepend (in bytes).
974 * A pointer to the start of the newly prepended data, or
975 * NULL if there is not enough headroom space in the first segment
977 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
980 __rte_mbuf_sanity_check(m, 1);
982 if (unlikely(len > rte_pktmbuf_headroom(m)))
986 m->data_len = (uint16_t)(m->data_len + len);
987 m->pkt_len = (m->pkt_len + len);
989 return (char *)m->buf_addr + m->data_off;
993 * Append len bytes to an mbuf.
995 * Append len bytes to an mbuf and return a pointer to the start address
996 * of the added data. If there is not enough tailroom in the last
997 * segment, the function will return NULL, without modifying the mbuf.
1002 * The amount of data to append (in bytes).
1004 * A pointer to the start of the newly appended data, or
1005 * NULL if there is not enough tailroom space in the last segment
1007 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1010 struct rte_mbuf *m_last;
1012 __rte_mbuf_sanity_check(m, 1);
1014 m_last = rte_pktmbuf_lastseg(m);
1015 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1018 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1019 m_last->data_len = (uint16_t)(m_last->data_len + len);
1020 m->pkt_len = (m->pkt_len + len);
1021 return (char*) tail;
1025 * Remove len bytes at the beginning of an mbuf.
1027 * Returns a pointer to the start address of the new data area. If the
1028 * length is greater than the length of the first segment, then the
1029 * function will fail and return NULL, without modifying the mbuf.
1034 * The amount of data to remove (in bytes).
1036 * A pointer to the new start of the data.
1038 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1040 __rte_mbuf_sanity_check(m, 1);
1042 if (unlikely(len > m->data_len))
1045 m->data_len = (uint16_t)(m->data_len - len);
1047 m->pkt_len = (m->pkt_len - len);
1048 return (char *)m->buf_addr + m->data_off;
1052 * Remove len bytes of data at the end of the mbuf.
1054 * If the length is greater than the length of the last segment, the
1055 * function will fail and return -1 without modifying the mbuf.
1060 * The amount of data to remove (in bytes).
1065 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1067 struct rte_mbuf *m_last;
1069 __rte_mbuf_sanity_check(m, 1);
1071 m_last = rte_pktmbuf_lastseg(m);
1072 if (unlikely(len > m_last->data_len))
1075 m_last->data_len = (uint16_t)(m_last->data_len - len);
1076 m->pkt_len = (m->pkt_len - len);
1081 * Test if mbuf data is contiguous.
1086 * - 1, if all data is contiguous (one segment).
1087 * - 0, if there is several segments.
1089 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1091 __rte_mbuf_sanity_check(m, 1);
1092 return !!(m->nb_segs == 1);
1096 * Dump an mbuf structure to the console.
1098 * Dump all fields for the given packet mbuf and all its associated
1099 * segments (in the case of a chained buffer).
1102 * A pointer to a file for output
1106 * If dump_len != 0, also dump the "dump_len" first data bytes of
1109 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
1115 #endif /* _RTE_MBUF_H_ */