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 * The preferred way to create a mbuf pool is to use
48 * rte_pktmbuf_pool_create(). However, in some situations, an
49 * application may want to have more control (ex: populate the pool with
50 * specific memory), in this case it is possible to use functions from
51 * rte_mempool. See how rte_pktmbuf_pool_create() is implemented for
54 * This library provides an API to allocate/free packet mbufs, which are
55 * used to carry network packets.
57 * To understand the concepts of packet buffers or mbufs, you
58 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
59 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
60 * http://www.kohala.com/start/tcpipiv2.html
64 #include <rte_compat.h>
65 #include <rte_common.h>
66 #include <rte_config.h>
67 #include <rte_mempool.h>
68 #include <rte_memory.h>
69 #include <rte_atomic.h>
70 #include <rte_prefetch.h>
71 #include <rte_branch_prediction.h>
72 #include <rte_mbuf_ptype.h>
79 * Packet Offload Features Flags. It also carry packet type information.
80 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
82 * - RX flags start at bit position zero, and get added to the left of previous
84 * - The most-significant 3 bits are reserved for generic mbuf flags
85 * - TX flags therefore start at bit position 60 (i.e. 63-3), and new flags get
86 * added to the right of the previously defined flags i.e. they should count
87 * downwards, not upwards.
89 * Keep these flags synchronized with rte_get_rx_ol_flag_name() and
90 * rte_get_tx_ol_flag_name().
94 * The RX packet is a 802.1q VLAN packet, and the tci has been
95 * saved in in mbuf->vlan_tci.
96 * If the flag PKT_RX_VLAN_STRIPPED is also present, the VLAN
97 * header has been stripped from mbuf data, else it is still
100 #define PKT_RX_VLAN (1ULL << 0)
102 #define PKT_RX_RSS_HASH (1ULL << 1) /**< RX packet with RSS hash result. */
103 #define PKT_RX_FDIR (1ULL << 2) /**< RX packet with FDIR match indicate. */
107 * Checking this flag alone is deprecated: check the 2 bits of
108 * PKT_RX_L4_CKSUM_MASK.
109 * This flag was set when the L4 checksum of a packet was detected as
110 * wrong by the hardware.
112 #define PKT_RX_L4_CKSUM_BAD (1ULL << 3)
116 * Checking this flag alone is deprecated: check the 2 bits of
117 * PKT_RX_IP_CKSUM_MASK.
118 * This flag was set when the IP checksum of a packet was detected as
119 * wrong by the hardware.
121 #define PKT_RX_IP_CKSUM_BAD (1ULL << 4)
123 #define PKT_RX_EIP_CKSUM_BAD (1ULL << 5) /**< External IP header checksum error. */
126 * A vlan has been stripped by the hardware and its tci is saved in
127 * mbuf->vlan_tci. This can only happen if vlan stripping is enabled
128 * in the RX configuration of the PMD.
129 * When PKT_RX_VLAN_STRIPPED is set, PKT_RX_VLAN must also be set.
131 #define PKT_RX_VLAN_STRIPPED (1ULL << 6)
134 * Mask of bits used to determine the status of RX IP checksum.
135 * - PKT_RX_IP_CKSUM_UNKNOWN: no information about the RX IP checksum
136 * - PKT_RX_IP_CKSUM_BAD: the IP checksum in the packet is wrong
137 * - PKT_RX_IP_CKSUM_GOOD: the IP checksum in the packet is valid
138 * - PKT_RX_IP_CKSUM_NONE: the IP checksum is not correct in the packet
139 * data, but the integrity of the IP header is verified.
141 #define PKT_RX_IP_CKSUM_MASK ((1ULL << 4) | (1ULL << 7))
143 #define PKT_RX_IP_CKSUM_UNKNOWN 0
144 #define PKT_RX_IP_CKSUM_BAD (1ULL << 4)
145 #define PKT_RX_IP_CKSUM_GOOD (1ULL << 7)
146 #define PKT_RX_IP_CKSUM_NONE ((1ULL << 4) | (1ULL << 7))
149 * Mask of bits used to determine the status of RX L4 checksum.
150 * - PKT_RX_L4_CKSUM_UNKNOWN: no information about the RX L4 checksum
151 * - PKT_RX_L4_CKSUM_BAD: the L4 checksum in the packet is wrong
152 * - PKT_RX_L4_CKSUM_GOOD: the L4 checksum in the packet is valid
153 * - PKT_RX_L4_CKSUM_NONE: the L4 checksum is not correct in the packet
154 * data, but the integrity of the L4 data is verified.
156 #define PKT_RX_L4_CKSUM_MASK ((1ULL << 3) | (1ULL << 8))
158 #define PKT_RX_L4_CKSUM_UNKNOWN 0
159 #define PKT_RX_L4_CKSUM_BAD (1ULL << 3)
160 #define PKT_RX_L4_CKSUM_GOOD (1ULL << 8)
161 #define PKT_RX_L4_CKSUM_NONE ((1ULL << 3) | (1ULL << 8))
163 #define PKT_RX_IEEE1588_PTP (1ULL << 9) /**< RX IEEE1588 L2 Ethernet PT Packet. */
164 #define PKT_RX_IEEE1588_TMST (1ULL << 10) /**< RX IEEE1588 L2/L4 timestamped packet.*/
165 #define PKT_RX_FDIR_ID (1ULL << 13) /**< FD id reported if FDIR match. */
166 #define PKT_RX_FDIR_FLX (1ULL << 14) /**< Flexible bytes reported if FDIR match. */
169 * The 2 vlans have been stripped by the hardware and their tci are
170 * saved in mbuf->vlan_tci (inner) and mbuf->vlan_tci_outer (outer).
171 * This can only happen if vlan stripping is enabled in the RX
172 * configuration of the PMD. If this flag is set,
173 * When PKT_RX_QINQ_STRIPPED is set, the flags (PKT_RX_VLAN |
174 * PKT_RX_VLAN_STRIPPED | PKT_RX_QINQ) must also be set.
176 #define PKT_RX_QINQ_STRIPPED (1ULL << 15)
179 * When packets are coalesced by a hardware or virtual driver, this flag
180 * can be set in the RX mbuf, meaning that the m->tso_segsz field is
181 * valid and is set to the segment size of original packets.
183 #define PKT_RX_LRO (1ULL << 16)
186 * Indicate that the timestamp field in the mbuf is valid.
188 #define PKT_RX_TIMESTAMP (1ULL << 17)
191 * Indicate that security offload processing was applied on the RX packet.
193 #define PKT_RX_SEC_OFFLOAD (1ULL << 18)
196 * Indicate that security offload processing failed on the RX packet.
198 #define PKT_RX_SEC_OFFLOAD_FAILED (1ULL << 19)
201 * The RX packet is a double VLAN, and the outer tci has been
202 * saved in in mbuf->vlan_tci_outer.
203 * If the flag PKT_RX_QINQ_STRIPPED is also present, both VLANs
204 * headers have been stripped from mbuf data, else they are still
207 #define PKT_RX_QINQ (1ULL << 20)
209 /* add new RX flags here */
211 /* add new TX flags here */
214 * UDP Fragmentation Offload flag. This flag is used for enabling UDP
215 * fragmentation in SW or in HW. When use UFO, mbuf->tso_segsz is used
216 * to store the MSS of UDP fragments.
218 #define PKT_TX_UDP_SEG (1ULL << 42)
221 * Request security offload processing on the TX packet.
223 #define PKT_TX_SEC_OFFLOAD (1ULL << 43)
226 * Offload the MACsec. This flag must be set by the application to enable
227 * this offload feature for a packet to be transmitted.
229 #define PKT_TX_MACSEC (1ULL << 44)
232 * Bits 45:48 used for the tunnel type.
233 * When doing Tx offload like TSO or checksum, the HW needs to configure the
234 * tunnel type into the HW descriptors.
236 #define PKT_TX_TUNNEL_VXLAN (0x1ULL << 45)
237 #define PKT_TX_TUNNEL_GRE (0x2ULL << 45)
238 #define PKT_TX_TUNNEL_IPIP (0x3ULL << 45)
239 #define PKT_TX_TUNNEL_GENEVE (0x4ULL << 45)
240 /**< TX packet with MPLS-in-UDP RFC 7510 header. */
241 #define PKT_TX_TUNNEL_MPLSINUDP (0x5ULL << 45)
242 /* add new TX TUNNEL type here */
243 #define PKT_TX_TUNNEL_MASK (0xFULL << 45)
246 * Second VLAN insertion (QinQ) flag.
248 #define PKT_TX_QINQ (1ULL << 49) /**< TX packet with double VLAN inserted. */
249 /* this old name is deprecated */
250 #define PKT_TX_QINQ_PKT PKT_TX_QINQ
253 * TCP segmentation offload. To enable this offload feature for a
254 * packet to be transmitted on hardware supporting TSO:
255 * - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag implies
257 * - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
258 * - if it's IPv4, set the PKT_TX_IP_CKSUM flag and write the IP checksum
260 * - fill the mbuf offload information: l2_len, l3_len, l4_len, tso_segsz
261 * - calculate the pseudo header checksum without taking ip_len in account,
262 * and set it in the TCP header. Refer to rte_ipv4_phdr_cksum() and
263 * rte_ipv6_phdr_cksum() that can be used as helpers.
265 #define PKT_TX_TCP_SEG (1ULL << 50)
267 #define PKT_TX_IEEE1588_TMST (1ULL << 51) /**< TX IEEE1588 packet to timestamp. */
270 * Bits 52+53 used for L4 packet type with checksum enabled: 00: Reserved,
271 * 01: TCP checksum, 10: SCTP checksum, 11: UDP checksum. To use hardware
272 * L4 checksum offload, the user needs to:
273 * - fill l2_len and l3_len in mbuf
274 * - set the flags PKT_TX_TCP_CKSUM, PKT_TX_SCTP_CKSUM or PKT_TX_UDP_CKSUM
275 * - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
276 * - calculate the pseudo header checksum and set it in the L4 header (only
277 * for TCP or UDP). See rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum().
278 * For SCTP, set the crc field to 0.
280 #define PKT_TX_L4_NO_CKSUM (0ULL << 52) /**< Disable L4 cksum of TX pkt. */
281 #define PKT_TX_TCP_CKSUM (1ULL << 52) /**< TCP cksum of TX pkt. computed by NIC. */
282 #define PKT_TX_SCTP_CKSUM (2ULL << 52) /**< SCTP cksum of TX pkt. computed by NIC. */
283 #define PKT_TX_UDP_CKSUM (3ULL << 52) /**< UDP cksum of TX pkt. computed by NIC. */
284 #define PKT_TX_L4_MASK (3ULL << 52) /**< Mask for L4 cksum offload request. */
287 * Offload the IP checksum in the hardware. The flag PKT_TX_IPV4 should
288 * also be set by the application, although a PMD will only check
290 * - set the IP checksum field in the packet to 0
291 * - fill the mbuf offload information: l2_len, l3_len
293 #define PKT_TX_IP_CKSUM (1ULL << 54)
296 * Packet is IPv4. This flag must be set when using any offload feature
297 * (TSO, L3 or L4 checksum) to tell the NIC that the packet is an IPv4
298 * packet. If the packet is a tunneled packet, this flag is related to
301 #define PKT_TX_IPV4 (1ULL << 55)
304 * Packet is IPv6. This flag must be set when using an offload feature
305 * (TSO or L4 checksum) to tell the NIC that the packet is an IPv6
306 * packet. If the packet is a tunneled packet, this flag is related to
309 #define PKT_TX_IPV6 (1ULL << 56)
312 * TX packet is a 802.1q VLAN packet.
314 #define PKT_TX_VLAN (1ULL << 57)
315 /* this old name is deprecated */
316 #define PKT_TX_VLAN_PKT PKT_TX_VLAN
319 * Offload the IP checksum of an external header in the hardware. The
320 * flag PKT_TX_OUTER_IPV4 should also be set by the application, alto ugh
321 * a PMD will only check PKT_TX_IP_CKSUM. The IP checksum field in the
322 * packet must be set to 0.
323 * - set the outer IP checksum field in the packet to 0
324 * - fill the mbuf offload information: outer_l2_len, outer_l3_len
326 #define PKT_TX_OUTER_IP_CKSUM (1ULL << 58)
329 * Packet outer header is IPv4. This flag must be set when using any
330 * outer offload feature (L3 or L4 checksum) to tell the NIC that the
331 * outer header of the tunneled packet is an IPv4 packet.
333 #define PKT_TX_OUTER_IPV4 (1ULL << 59)
336 * Packet outer header is IPv6. This flag must be set when using any
337 * outer offload feature (L4 checksum) to tell the NIC that the outer
338 * header of the tunneled packet is an IPv6 packet.
340 #define PKT_TX_OUTER_IPV6 (1ULL << 60)
343 * Bitmask of all supported packet Tx offload features flags,
344 * which can be set for packet.
346 #define PKT_TX_OFFLOAD_MASK ( \
349 PKT_TX_OUTER_IP_CKSUM | \
351 PKT_TX_IEEE1588_TMST | \
354 PKT_TX_TUNNEL_MASK | \
358 #define __RESERVED (1ULL << 61) /**< reserved for future mbuf use */
360 #define IND_ATTACHED_MBUF (1ULL << 62) /**< Indirect attached mbuf */
362 /* Use final bit of flags to indicate a control mbuf */
363 #define CTRL_MBUF_FLAG (1ULL << 63) /**< Mbuf contains control data */
365 /** Alignment constraint of mbuf private area. */
366 #define RTE_MBUF_PRIV_ALIGN 8
369 * Get the name of a RX offload flag
372 * The mask describing the flag.
374 * The name of this flag, or NULL if it's not a valid RX flag.
376 const char *rte_get_rx_ol_flag_name(uint64_t mask);
379 * Dump the list of RX offload flags in a buffer
382 * The mask describing the RX flags.
386 * The length of the buffer.
388 * 0 on success, (-1) on error.
390 int rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
393 * Get the name of a TX offload flag
396 * The mask describing the flag. Usually only one bit must be set.
397 * Several bits can be given if they belong to the same mask.
398 * Ex: PKT_TX_L4_MASK.
400 * The name of this flag, or NULL if it's not a valid TX flag.
402 const char *rte_get_tx_ol_flag_name(uint64_t mask);
405 * Dump the list of TX offload flags in a buffer
408 * The mask describing the TX flags.
412 * The length of the buffer.
414 * 0 on success, (-1) on error.
416 int rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen);
419 * Some NICs need at least 2KB buffer to RX standard Ethernet frame without
420 * splitting it into multiple segments.
421 * So, for mbufs that planned to be involved into RX/TX, the recommended
422 * minimal buffer length is 2KB + RTE_PKTMBUF_HEADROOM.
424 #define RTE_MBUF_DEFAULT_DATAROOM 2048
425 #define RTE_MBUF_DEFAULT_BUF_SIZE \
426 (RTE_MBUF_DEFAULT_DATAROOM + RTE_PKTMBUF_HEADROOM)
428 /* define a set of marker types that can be used to refer to set points in the
431 typedef void *MARKER[0]; /**< generic marker for a point in a structure */
433 typedef uint8_t MARKER8[0]; /**< generic marker with 1B alignment */
435 typedef uint64_t MARKER64[0]; /**< marker that allows us to overwrite 8 bytes
436 * with a single assignment */
439 * The generic rte_mbuf, containing a packet mbuf.
444 void *buf_addr; /**< Virtual address of segment buffer. */
446 * Physical address of segment buffer.
447 * Force alignment to 8-bytes, so as to ensure we have the exact
448 * same mbuf cacheline0 layout for 32-bit and 64-bit. This makes
449 * working on vector drivers easier.
454 rte_iova_t buf_physaddr; /**< deprecated */
455 } __rte_aligned(sizeof(rte_iova_t));
457 /* next 8 bytes are initialised on RX descriptor rearm */
462 * Reference counter. Its size should at least equal to the size
463 * of port field (16 bits), to support zero-copy broadcast.
464 * It should only be accessed using the following functions:
465 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
466 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
467 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
472 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
473 uint16_t refcnt; /**< Non-atomically accessed refcnt */
475 uint16_t nb_segs; /**< Number of segments. */
477 /** Input port (16 bits to support more than 256 virtual ports). */
480 uint64_t ol_flags; /**< Offload features. */
482 /* remaining bytes are set on RX when pulling packet from descriptor */
483 MARKER rx_descriptor_fields1;
486 * The packet type, which is the combination of outer/inner L2, L3, L4
487 * and tunnel types. The packet_type is about data really present in the
488 * mbuf. Example: if vlan stripping is enabled, a received vlan packet
489 * would have RTE_PTYPE_L2_ETHER and not RTE_PTYPE_L2_VLAN because the
490 * vlan is stripped from the data.
494 uint32_t packet_type; /**< L2/L3/L4 and tunnel information. */
496 uint32_t l2_type:4; /**< (Outer) L2 type. */
497 uint32_t l3_type:4; /**< (Outer) L3 type. */
498 uint32_t l4_type:4; /**< (Outer) L4 type. */
499 uint32_t tun_type:4; /**< Tunnel type. */
502 uint8_t inner_esp_next_proto;
503 /**< ESP next protocol type, valid if
504 * RTE_PTYPE_TUNNEL_ESP tunnel type is set
509 uint8_t inner_l2_type:4;
510 /**< Inner L2 type. */
511 uint8_t inner_l3_type:4;
512 /**< Inner L3 type. */
515 uint32_t inner_l4_type:4; /**< Inner L4 type. */
519 uint32_t pkt_len; /**< Total pkt len: sum of all segments. */
520 uint16_t data_len; /**< Amount of data in segment buffer. */
521 /** VLAN TCI (CPU order), valid if PKT_RX_VLAN is set. */
525 uint32_t rss; /**< RSS hash result if RSS enabled */
534 /**< Second 4 flexible bytes */
537 /**< First 4 flexible bytes or FD ID, dependent on
538 PKT_RX_FDIR_* flag in ol_flags. */
539 } fdir; /**< Filter identifier if FDIR enabled */
543 } sched; /**< Hierarchical scheduler */
544 uint32_t usr; /**< User defined tags. See rte_distributor_process() */
545 } hash; /**< hash information */
547 /** Outer VLAN TCI (CPU order), valid if PKT_RX_QINQ is set. */
548 uint16_t vlan_tci_outer;
550 uint16_t buf_len; /**< Length of segment buffer. */
552 /** Valid if PKT_RX_TIMESTAMP is set. The unit and time reference
553 * are not normalized but are always the same for a given port.
557 /* second cache line - fields only used in slow path or on TX */
558 MARKER cacheline1 __rte_cache_min_aligned;
562 void *userdata; /**< Can be used for external metadata */
563 uint64_t udata64; /**< Allow 8-byte userdata on 32-bit */
566 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
567 struct rte_mbuf *next; /**< Next segment of scattered packet. */
569 /* fields to support TX offloads */
572 uint64_t tx_offload; /**< combined for easy fetch */
576 /**< L2 (MAC) Header Length for non-tunneling pkt.
577 * Outer_L4_len + ... + Inner_L2_len for tunneling pkt.
579 uint64_t l3_len:9; /**< L3 (IP) Header Length. */
580 uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
581 uint64_t tso_segsz:16; /**< TCP TSO segment size */
583 /* fields for TX offloading of tunnels */
584 uint64_t outer_l3_len:9; /**< Outer L3 (IP) Hdr Length. */
585 uint64_t outer_l2_len:7; /**< Outer L2 (MAC) Hdr Length. */
587 /* uint64_t unused:8; */
591 /** Size of the application private data. In case of an indirect
592 * mbuf, it stores the direct mbuf private data size. */
595 /** Timesync flags for use with IEEE1588. */
598 /** Sequence number. See also rte_reorder_insert(). */
601 } __rte_cache_aligned;
603 /**< Maximum number of nb_segs allowed. */
604 #define RTE_MBUF_MAX_NB_SEGS UINT16_MAX
607 * Prefetch the first part of the mbuf
609 * The first 64 bytes of the mbuf corresponds to fields that are used early
610 * in the receive path. If the cache line of the architecture is higher than
611 * 64B, the second part will also be prefetched.
614 * The pointer to the mbuf.
617 rte_mbuf_prefetch_part1(struct rte_mbuf *m)
619 rte_prefetch0(&m->cacheline0);
623 * Prefetch the second part of the mbuf
625 * The next 64 bytes of the mbuf corresponds to fields that are used in the
626 * transmit path. If the cache line of the architecture is higher than 64B,
627 * this function does nothing as it is expected that the full mbuf is
631 * The pointer to the mbuf.
634 rte_mbuf_prefetch_part2(struct rte_mbuf *m)
636 #if RTE_CACHE_LINE_SIZE == 64
637 rte_prefetch0(&m->cacheline1);
644 static inline uint16_t rte_pktmbuf_priv_size(struct rte_mempool *mp);
647 * Return the IO address of the beginning of the mbuf data
650 * The pointer to the mbuf.
652 * The IO address of the beginning of the mbuf data
654 static inline rte_iova_t
655 rte_mbuf_data_iova(const struct rte_mbuf *mb)
657 return mb->buf_iova + mb->data_off;
661 static inline phys_addr_t
662 rte_mbuf_data_dma_addr(const struct rte_mbuf *mb)
664 return rte_mbuf_data_iova(mb);
668 * Return the default IO address of the beginning of the mbuf data
670 * This function is used by drivers in their receive function, as it
671 * returns the location where data should be written by the NIC, taking
672 * the default headroom in account.
675 * The pointer to the mbuf.
677 * The IO address of the beginning of the mbuf data
679 static inline rte_iova_t
680 rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
682 return mb->buf_iova + RTE_PKTMBUF_HEADROOM;
686 static inline phys_addr_t
687 rte_mbuf_data_dma_addr_default(const struct rte_mbuf *mb)
689 return rte_mbuf_data_iova_default(mb);
693 * Return the mbuf owning the data buffer address of an indirect mbuf.
696 * The pointer to the indirect mbuf.
698 * The address of the direct mbuf corresponding to buffer_addr.
700 static inline struct rte_mbuf *
701 rte_mbuf_from_indirect(struct rte_mbuf *mi)
703 return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
707 * Return the buffer address embedded in the given mbuf.
710 * The pointer to the mbuf.
712 * The address of the data buffer owned by the mbuf.
715 rte_mbuf_to_baddr(struct rte_mbuf *md)
718 buffer_addr = (char *)md + sizeof(*md) + rte_pktmbuf_priv_size(md->pool);
723 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
725 #define RTE_MBUF_INDIRECT(mb) ((mb)->ol_flags & IND_ATTACHED_MBUF)
728 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
730 #define RTE_MBUF_DIRECT(mb) (!RTE_MBUF_INDIRECT(mb))
733 * Private data in case of pktmbuf pool.
735 * A structure that contains some pktmbuf_pool-specific data that are
736 * appended after the mempool structure (in private data).
738 struct rte_pktmbuf_pool_private {
739 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf. */
740 uint16_t mbuf_priv_size; /**< Size of private area in each mbuf. */
743 #ifdef RTE_LIBRTE_MBUF_DEBUG
745 /** check mbuf type in debug mode */
746 #define __rte_mbuf_sanity_check(m, is_h) rte_mbuf_sanity_check(m, is_h)
748 #else /* RTE_LIBRTE_MBUF_DEBUG */
750 /** check mbuf type in debug mode */
751 #define __rte_mbuf_sanity_check(m, is_h) do { } while (0)
753 #endif /* RTE_LIBRTE_MBUF_DEBUG */
755 #ifdef RTE_MBUF_REFCNT_ATOMIC
758 * Reads the value of an mbuf's refcnt.
762 * Reference count number.
764 static inline uint16_t
765 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
767 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
771 * Sets an mbuf's refcnt to a defined value.
778 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
780 rte_atomic16_set(&m->refcnt_atomic, new_value);
784 static inline uint16_t
785 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
787 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
791 * Adds given value to an mbuf's refcnt and returns its new value.
795 * Value to add/subtract
799 static inline uint16_t
800 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
803 * The atomic_add is an expensive operation, so we don't want to
804 * call it in the case where we know we are the uniq holder of
805 * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic
806 * operation has to be used because concurrent accesses on the
807 * reference counter can occur.
809 if (likely(rte_mbuf_refcnt_read(m) == 1)) {
810 rte_mbuf_refcnt_set(m, 1 + value);
814 return __rte_mbuf_refcnt_update(m, value);
817 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
820 static inline uint16_t
821 __rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
823 m->refcnt = (uint16_t)(m->refcnt + value);
828 * Adds given value to an mbuf's refcnt and returns its new value.
830 static inline uint16_t
831 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
833 return __rte_mbuf_refcnt_update(m, value);
837 * Reads the value of an mbuf's refcnt.
839 static inline uint16_t
840 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
846 * Sets an mbuf's refcnt to the defined value.
849 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
851 m->refcnt = new_value;
854 #endif /* RTE_MBUF_REFCNT_ATOMIC */
857 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
864 * Sanity checks on an mbuf.
866 * Check the consistency of the given mbuf. The function will cause a
867 * panic if corruption is detected.
870 * The mbuf to be checked.
872 * True if the mbuf is a packet header, false if it is a sub-segment
873 * of a packet (in this case, some fields like nb_segs are not checked)
876 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header);
878 #define MBUF_RAW_ALLOC_CHECK(m) do { \
879 RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1); \
880 RTE_ASSERT((m)->next == NULL); \
881 RTE_ASSERT((m)->nb_segs == 1); \
882 __rte_mbuf_sanity_check(m, 0); \
886 * Allocate an uninitialized mbuf from mempool *mp*.
888 * This function can be used by PMDs (especially in RX functions) to
889 * allocate an uninitialized mbuf. The driver is responsible of
890 * initializing all the required fields. See rte_pktmbuf_reset().
891 * For standard needs, prefer rte_pktmbuf_alloc().
893 * The caller can expect that the following fields of the mbuf structure
894 * are initialized: buf_addr, buf_iova, buf_len, refcnt=1, nb_segs=1,
895 * next=NULL, pool, priv_size. The other fields must be initialized
899 * The mempool from which mbuf is allocated.
901 * - The pointer to the new mbuf on success.
902 * - NULL if allocation failed.
904 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
908 if (rte_mempool_get(mp, (void **)&m) < 0)
910 MBUF_RAW_ALLOC_CHECK(m);
915 * Put mbuf back into its original mempool.
917 * The caller must ensure that the mbuf is direct and properly
918 * reinitialized (refcnt=1, next=NULL, nb_segs=1), as done by
919 * rte_pktmbuf_prefree_seg().
921 * This function should be used with care, when optimization is
922 * required. For standard needs, prefer rte_pktmbuf_free() or
923 * rte_pktmbuf_free_seg().
926 * The mbuf to be freed.
928 static __rte_always_inline void
929 rte_mbuf_raw_free(struct rte_mbuf *m)
931 RTE_ASSERT(RTE_MBUF_DIRECT(m));
932 RTE_ASSERT(rte_mbuf_refcnt_read(m) == 1);
933 RTE_ASSERT(m->next == NULL);
934 RTE_ASSERT(m->nb_segs == 1);
935 __rte_mbuf_sanity_check(m, 0);
936 rte_mempool_put(m->pool, m);
939 /* compat with older versions */
942 __rte_mbuf_raw_free(struct rte_mbuf *m)
944 rte_mbuf_raw_free(m);
947 /* Operations on ctrl mbuf */
950 * The control mbuf constructor.
952 * This function initializes some fields in an mbuf structure that are
953 * not modified by the user once created (mbuf type, origin pool, buffer
954 * start address, and so on). This function is given as a callback function
955 * to rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
958 * The mempool from which the mbuf is allocated.
960 * A pointer that can be used by the user to retrieve useful information
961 * for mbuf initialization. This pointer is the opaque argument passed to
962 * rte_mempool_obj_iter() or rte_mempool_create().
964 * The mbuf to initialize.
966 * The index of the mbuf in the pool table.
968 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
969 void *m, unsigned i);
972 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
974 * This new mbuf is initialized with data pointing to the beginning of
975 * buffer, and with a length of zero.
978 * The mempool from which the mbuf is allocated.
980 * - The pointer to the new mbuf on success.
981 * - NULL if allocation failed.
983 #define rte_ctrlmbuf_alloc(mp) rte_pktmbuf_alloc(mp)
986 * Free a control mbuf back into its original mempool.
989 * The control mbuf to be freed.
991 #define rte_ctrlmbuf_free(m) rte_pktmbuf_free(m)
994 * A macro that returns the pointer to the carried data.
996 * The value that can be read or assigned.
1001 #define rte_ctrlmbuf_data(m) ((char *)((m)->buf_addr) + (m)->data_off)
1004 * A macro that returns the length of the carried data.
1006 * The value that can be read or assigned.
1011 #define rte_ctrlmbuf_len(m) rte_pktmbuf_data_len(m)
1014 * Tests if an mbuf is a control mbuf
1017 * The mbuf to be tested
1019 * - True (1) if the mbuf is a control mbuf
1020 * - False(0) otherwise
1023 rte_is_ctrlmbuf(struct rte_mbuf *m)
1025 return !!(m->ol_flags & CTRL_MBUF_FLAG);
1028 /* Operations on pkt mbuf */
1031 * The packet mbuf constructor.
1033 * This function initializes some fields in the mbuf structure that are
1034 * not modified by the user once created (origin pool, buffer start
1035 * address, and so on). This function is given as a callback function to
1036 * rte_mempool_obj_iter() or rte_mempool_create() at pool creation time.
1039 * The mempool from which mbufs originate.
1041 * A pointer that can be used by the user to retrieve useful information
1042 * for mbuf initialization. This pointer is the opaque argument passed to
1043 * rte_mempool_obj_iter() or rte_mempool_create().
1045 * The mbuf to initialize.
1047 * The index of the mbuf in the pool table.
1049 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
1050 void *m, unsigned i);
1054 * A packet mbuf pool constructor.
1056 * This function initializes the mempool private data in the case of a
1057 * pktmbuf pool. This private data is needed by the driver. The
1058 * function must be called on the mempool before it is used, or it
1059 * can be given as a callback function to rte_mempool_create() at
1060 * pool creation. It can be extended by the user, for example, to
1061 * provide another packet size.
1064 * The mempool from which mbufs originate.
1066 * A pointer that can be used by the user to retrieve useful information
1067 * for mbuf initialization. This pointer is the opaque argument passed to
1068 * rte_mempool_create().
1070 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
1073 * Create a mbuf pool.
1075 * This function creates and initializes a packet mbuf pool. It is
1076 * a wrapper to rte_mempool functions.
1079 * The name of the mbuf pool.
1081 * The number of elements in the mbuf pool. The optimum size (in terms
1082 * of memory usage) for a mempool is when n is a power of two minus one:
1085 * Size of the per-core object cache. See rte_mempool_create() for
1088 * Size of application private are between the rte_mbuf structure
1089 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
1090 * @param data_room_size
1091 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
1093 * The socket identifier where the memory should be allocated. The
1094 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
1097 * The pointer to the new allocated mempool, on success. NULL on error
1098 * with rte_errno set appropriately. Possible rte_errno values include:
1099 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
1100 * - E_RTE_SECONDARY - function was called from a secondary process instance
1101 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
1102 * - ENOSPC - the maximum number of memzones has already been allocated
1103 * - EEXIST - a memzone with the same name already exists
1104 * - ENOMEM - no appropriate memory area found in which to create memzone
1106 struct rte_mempool *
1107 rte_pktmbuf_pool_create(const char *name, unsigned n,
1108 unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
1112 * Create a mbuf pool with a given mempool ops name
1114 * This function creates and initializes a packet mbuf pool. It is
1115 * a wrapper to rte_mempool functions.
1118 * The name of the mbuf pool.
1120 * The number of elements in the mbuf pool. The optimum size (in terms
1121 * of memory usage) for a mempool is when n is a power of two minus one:
1124 * Size of the per-core object cache. See rte_mempool_create() for
1127 * Size of application private are between the rte_mbuf structure
1128 * and the data buffer. This value must be aligned to RTE_MBUF_PRIV_ALIGN.
1129 * @param data_room_size
1130 * Size of data buffer in each mbuf, including RTE_PKTMBUF_HEADROOM.
1132 * The socket identifier where the memory should be allocated. The
1133 * value can be *SOCKET_ID_ANY* if there is no NUMA constraint for the
1136 * The mempool ops name to be used for this mempool instead of
1137 * default mempool. The value can be *NULL* to use default mempool.
1139 * The pointer to the new allocated mempool, on success. NULL on error
1140 * with rte_errno set appropriately. Possible rte_errno values include:
1141 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
1142 * - E_RTE_SECONDARY - function was called from a secondary process instance
1143 * - EINVAL - cache size provided is too large, or priv_size is not aligned.
1144 * - ENOSPC - the maximum number of memzones has already been allocated
1145 * - EEXIST - a memzone with the same name already exists
1146 * - ENOMEM - no appropriate memory area found in which to create memzone
1148 struct rte_mempool * __rte_experimental
1149 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
1150 unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
1151 int socket_id, const char *ops_name);
1154 * Get the data room size of mbufs stored in a pktmbuf_pool
1156 * The data room size is the amount of data that can be stored in a
1157 * mbuf including the headroom (RTE_PKTMBUF_HEADROOM).
1160 * The packet mbuf pool.
1162 * The data room size of mbufs stored in this mempool.
1164 static inline uint16_t
1165 rte_pktmbuf_data_room_size(struct rte_mempool *mp)
1167 struct rte_pktmbuf_pool_private *mbp_priv;
1169 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1170 return mbp_priv->mbuf_data_room_size;
1174 * Get the application private size of mbufs stored in a pktmbuf_pool
1176 * The private size of mbuf is a zone located between the rte_mbuf
1177 * structure and the data buffer where an application can store data
1178 * associated to a packet.
1181 * The packet mbuf pool.
1183 * The private size of mbufs stored in this mempool.
1185 static inline uint16_t
1186 rte_pktmbuf_priv_size(struct rte_mempool *mp)
1188 struct rte_pktmbuf_pool_private *mbp_priv;
1190 mbp_priv = (struct rte_pktmbuf_pool_private *)rte_mempool_get_priv(mp);
1191 return mbp_priv->mbuf_priv_size;
1195 * Reset the data_off field of a packet mbuf to its default value.
1197 * The given mbuf must have only one segment, which should be empty.
1200 * The packet mbuf's data_off field has to be reset.
1202 static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf *m)
1204 m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
1208 * Reset the fields of a packet mbuf to their default values.
1210 * The given mbuf must have only one segment.
1213 * The packet mbuf to be resetted.
1215 #define MBUF_INVALID_PORT UINT16_MAX
1217 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
1223 m->vlan_tci_outer = 0;
1225 m->port = MBUF_INVALID_PORT;
1229 rte_pktmbuf_reset_headroom(m);
1232 __rte_mbuf_sanity_check(m, 1);
1236 * Allocate a new mbuf from a mempool.
1238 * This new mbuf contains one segment, which has a length of 0. The pointer
1239 * to data is initialized to have some bytes of headroom in the buffer
1240 * (if buffer size allows).
1243 * The mempool from which the mbuf is allocated.
1245 * - The pointer to the new mbuf on success.
1246 * - NULL if allocation failed.
1248 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
1251 if ((m = rte_mbuf_raw_alloc(mp)) != NULL)
1252 rte_pktmbuf_reset(m);
1257 * Allocate a bulk of mbufs, initialize refcnt and reset the fields to default
1261 * The mempool from which mbufs are allocated.
1263 * Array of pointers to mbufs
1268 * - -ENOENT: Not enough entries in the mempool; no mbufs are retrieved.
1270 static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
1271 struct rte_mbuf **mbufs, unsigned count)
1276 rc = rte_mempool_get_bulk(pool, (void **)mbufs, count);
1280 /* To understand duff's device on loop unwinding optimization, see
1281 * https://en.wikipedia.org/wiki/Duff's_device.
1282 * Here while() loop is used rather than do() while{} to avoid extra
1283 * check if count is zero.
1285 switch (count % 4) {
1287 while (idx != count) {
1288 MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1289 rte_pktmbuf_reset(mbufs[idx]);
1293 MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1294 rte_pktmbuf_reset(mbufs[idx]);
1298 MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1299 rte_pktmbuf_reset(mbufs[idx]);
1303 MBUF_RAW_ALLOC_CHECK(mbufs[idx]);
1304 rte_pktmbuf_reset(mbufs[idx]);
1313 * Attach packet mbuf to another packet mbuf.
1315 * After attachment we refer the mbuf we attached as 'indirect',
1316 * while mbuf we attached to as 'direct'.
1317 * The direct mbuf's reference counter is incremented.
1319 * Right now, not supported:
1320 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
1321 * - mbuf we trying to attach (mi) is used by someone else
1322 * e.g. it's reference counter is greater then 1.
1325 * The indirect packet mbuf.
1327 * The packet mbuf we're attaching to.
1329 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
1331 struct rte_mbuf *md;
1333 RTE_ASSERT(RTE_MBUF_DIRECT(mi) &&
1334 rte_mbuf_refcnt_read(mi) == 1);
1336 /* if m is not direct, get the mbuf that embeds the data */
1337 if (RTE_MBUF_DIRECT(m))
1340 md = rte_mbuf_from_indirect(m);
1342 rte_mbuf_refcnt_update(md, 1);
1343 mi->priv_size = m->priv_size;
1344 mi->buf_iova = m->buf_iova;
1345 mi->buf_addr = m->buf_addr;
1346 mi->buf_len = m->buf_len;
1348 mi->data_off = m->data_off;
1349 mi->data_len = m->data_len;
1351 mi->vlan_tci = m->vlan_tci;
1352 mi->vlan_tci_outer = m->vlan_tci_outer;
1353 mi->tx_offload = m->tx_offload;
1357 mi->pkt_len = mi->data_len;
1359 mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
1360 mi->packet_type = m->packet_type;
1361 mi->timestamp = m->timestamp;
1363 __rte_mbuf_sanity_check(mi, 1);
1364 __rte_mbuf_sanity_check(m, 0);
1368 * Detach an indirect packet mbuf.
1370 * - restore original mbuf address and length values.
1371 * - reset pktmbuf data and data_len to their default values.
1372 * - decrement the direct mbuf's reference counter. When the
1373 * reference counter becomes 0, the direct mbuf is freed.
1375 * All other fields of the given packet mbuf will be left intact.
1378 * The indirect attached packet mbuf.
1380 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
1382 struct rte_mbuf *md = rte_mbuf_from_indirect(m);
1383 struct rte_mempool *mp = m->pool;
1384 uint32_t mbuf_size, buf_len, priv_size;
1386 priv_size = rte_pktmbuf_priv_size(mp);
1387 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1388 buf_len = rte_pktmbuf_data_room_size(mp);
1390 m->priv_size = priv_size;
1391 m->buf_addr = (char *)m + mbuf_size;
1392 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1393 m->buf_len = (uint16_t)buf_len;
1394 rte_pktmbuf_reset_headroom(m);
1398 if (rte_mbuf_refcnt_update(md, -1) == 0) {
1401 rte_mbuf_refcnt_set(md, 1);
1402 rte_mbuf_raw_free(md);
1407 * Decrease reference counter and unlink a mbuf segment
1409 * This function does the same than a free, except that it does not
1410 * return the segment to its pool.
1411 * It decreases the reference counter, and if it reaches 0, it is
1412 * detached from its parent for an indirect mbuf.
1415 * The mbuf to be unlinked
1417 * - (m) if it is the last reference. It can be recycled or freed.
1418 * - (NULL) if the mbuf still has remaining references on it.
1420 static __rte_always_inline struct rte_mbuf *
1421 rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1423 __rte_mbuf_sanity_check(m, 0);
1425 if (likely(rte_mbuf_refcnt_read(m) == 1)) {
1427 if (RTE_MBUF_INDIRECT(m))
1428 rte_pktmbuf_detach(m);
1430 if (m->next != NULL) {
1437 } else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
1439 if (RTE_MBUF_INDIRECT(m))
1440 rte_pktmbuf_detach(m);
1442 if (m->next != NULL) {
1446 rte_mbuf_refcnt_set(m, 1);
1453 /* deprecated, replaced by rte_pktmbuf_prefree_seg() */
1455 static inline struct rte_mbuf *
1456 __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
1458 return rte_pktmbuf_prefree_seg(m);
1462 * Free a segment of a packet mbuf into its original mempool.
1464 * Free an mbuf, without parsing other segments in case of chained
1468 * The packet mbuf segment to be freed.
1470 static __rte_always_inline void
1471 rte_pktmbuf_free_seg(struct rte_mbuf *m)
1473 m = rte_pktmbuf_prefree_seg(m);
1474 if (likely(m != NULL))
1475 rte_mbuf_raw_free(m);
1479 * Free a packet mbuf back into its original mempool.
1481 * Free an mbuf, and all its segments in case of chained buffers. Each
1482 * segment is added back into its original mempool.
1485 * The packet mbuf to be freed. If NULL, the function does nothing.
1487 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
1489 struct rte_mbuf *m_next;
1492 __rte_mbuf_sanity_check(m, 1);
1496 rte_pktmbuf_free_seg(m);
1502 * Creates a "clone" of the given packet mbuf.
1504 * Walks through all segments of the given packet mbuf, and for each of them:
1505 * - Creates a new packet mbuf from the given pool.
1506 * - Attaches newly created mbuf to the segment.
1507 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
1508 * from the original packet mbuf.
1511 * The packet mbuf to be cloned.
1513 * The mempool from which the "clone" mbufs are allocated.
1515 * - The pointer to the new "clone" mbuf on success.
1516 * - NULL if allocation fails.
1518 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
1519 struct rte_mempool *mp)
1521 struct rte_mbuf *mc, *mi, **prev;
1525 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
1530 pktlen = md->pkt_len;
1535 rte_pktmbuf_attach(mi, md);
1538 } while ((md = md->next) != NULL &&
1539 (mi = rte_pktmbuf_alloc(mp)) != NULL);
1543 mc->pkt_len = pktlen;
1545 /* Allocation of new indirect segment failed */
1546 if (unlikely (mi == NULL)) {
1547 rte_pktmbuf_free(mc);
1551 __rte_mbuf_sanity_check(mc, 1);
1556 * Adds given value to the refcnt of all packet mbuf segments.
1558 * Walks through all segments of given packet mbuf and for each of them
1559 * invokes rte_mbuf_refcnt_update().
1562 * The packet mbuf whose refcnt to be updated.
1564 * The value to add to the mbuf's segments refcnt.
1566 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
1568 __rte_mbuf_sanity_check(m, 1);
1571 rte_mbuf_refcnt_update(m, v);
1572 } while ((m = m->next) != NULL);
1576 * Get the headroom in a packet mbuf.
1581 * The length of the headroom.
1583 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
1585 __rte_mbuf_sanity_check(m, 0);
1590 * Get the tailroom of a packet mbuf.
1595 * The length of the tailroom.
1597 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
1599 __rte_mbuf_sanity_check(m, 0);
1600 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
1605 * Get the last segment of the packet.
1610 * The last segment of the given mbuf.
1612 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
1614 __rte_mbuf_sanity_check(m, 1);
1615 while (m->next != NULL)
1621 * A macro that points to an offset into the data in the mbuf.
1623 * The returned pointer is cast to type t. Before using this
1624 * function, the user must ensure that the first segment is large
1625 * enough to accommodate its data.
1630 * The offset into the mbuf data.
1632 * The type to cast the result into.
1634 #define rte_pktmbuf_mtod_offset(m, t, o) \
1635 ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
1638 * A macro that points to the start of the data in the mbuf.
1640 * The returned pointer is cast to type t. Before using this
1641 * function, the user must ensure that the first segment is large
1642 * enough to accommodate its data.
1647 * The type to cast the result into.
1649 #define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
1652 * A macro that returns the IO address that points to an offset of the
1653 * start of the data in the mbuf
1658 * The offset into the data to calculate address from.
1660 #define rte_pktmbuf_iova_offset(m, o) \
1661 (rte_iova_t)((m)->buf_iova + (m)->data_off + (o))
1664 #define rte_pktmbuf_mtophys_offset(m, o) \
1665 rte_pktmbuf_iova_offset(m, o)
1668 * A macro that returns the IO address that points to the start of the
1674 #define rte_pktmbuf_iova(m) rte_pktmbuf_iova_offset(m, 0)
1677 #define rte_pktmbuf_mtophys(m) rte_pktmbuf_iova(m)
1680 * A macro that returns the length of the packet.
1682 * The value can be read or assigned.
1687 #define rte_pktmbuf_pkt_len(m) ((m)->pkt_len)
1690 * A macro that returns the length of the segment.
1692 * The value can be read or assigned.
1697 #define rte_pktmbuf_data_len(m) ((m)->data_len)
1700 * Prepend len bytes to an mbuf data area.
1702 * Returns a pointer to the new
1703 * data start address. If there is not enough headroom in the first
1704 * segment, the function will return NULL, without modifying the mbuf.
1709 * The amount of data to prepend (in bytes).
1711 * A pointer to the start of the newly prepended data, or
1712 * NULL if there is not enough headroom space in the first segment
1714 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
1717 __rte_mbuf_sanity_check(m, 1);
1719 if (unlikely(len > rte_pktmbuf_headroom(m)))
1723 m->data_len = (uint16_t)(m->data_len + len);
1724 m->pkt_len = (m->pkt_len + len);
1726 return (char *)m->buf_addr + m->data_off;
1730 * Append len bytes to an mbuf.
1732 * Append len bytes to an mbuf and return a pointer to the start address
1733 * of the added data. If there is not enough tailroom in the last
1734 * segment, the function will return NULL, without modifying the mbuf.
1739 * The amount of data to append (in bytes).
1741 * A pointer to the start of the newly appended data, or
1742 * NULL if there is not enough tailroom space in the last segment
1744 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
1747 struct rte_mbuf *m_last;
1749 __rte_mbuf_sanity_check(m, 1);
1751 m_last = rte_pktmbuf_lastseg(m);
1752 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
1755 tail = (char *)m_last->buf_addr + m_last->data_off + m_last->data_len;
1756 m_last->data_len = (uint16_t)(m_last->data_len + len);
1757 m->pkt_len = (m->pkt_len + len);
1758 return (char*) tail;
1762 * Remove len bytes at the beginning of an mbuf.
1764 * Returns a pointer to the start address of the new data area. If the
1765 * length is greater than the length of the first segment, then the
1766 * function will fail and return NULL, without modifying the mbuf.
1771 * The amount of data to remove (in bytes).
1773 * A pointer to the new start of the data.
1775 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
1777 __rte_mbuf_sanity_check(m, 1);
1779 if (unlikely(len > m->data_len))
1782 m->data_len = (uint16_t)(m->data_len - len);
1784 m->pkt_len = (m->pkt_len - len);
1785 return (char *)m->buf_addr + m->data_off;
1789 * Remove len bytes of data at the end of the mbuf.
1791 * If the length is greater than the length of the last segment, the
1792 * function will fail and return -1 without modifying the mbuf.
1797 * The amount of data to remove (in bytes).
1802 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
1804 struct rte_mbuf *m_last;
1806 __rte_mbuf_sanity_check(m, 1);
1808 m_last = rte_pktmbuf_lastseg(m);
1809 if (unlikely(len > m_last->data_len))
1812 m_last->data_len = (uint16_t)(m_last->data_len - len);
1813 m->pkt_len = (m->pkt_len - len);
1818 * Test if mbuf data is contiguous.
1823 * - 1, if all data is contiguous (one segment).
1824 * - 0, if there is several segments.
1826 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1828 __rte_mbuf_sanity_check(m, 1);
1829 return !!(m->nb_segs == 1);
1833 * @internal used by rte_pktmbuf_read().
1835 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
1836 uint32_t len, void *buf);
1839 * Read len data bytes in a mbuf at specified offset.
1841 * If the data is contiguous, return the pointer in the mbuf data, else
1842 * copy the data in the buffer provided by the user and return its
1846 * The pointer to the mbuf.
1848 * The offset of the data in the mbuf.
1850 * The amount of bytes to read.
1852 * The buffer where data is copied if it is not contiguous in mbuf
1853 * data. Its length should be at least equal to the len parameter.
1855 * The pointer to the data, either in the mbuf if it is contiguous,
1856 * or in the user buffer. If mbuf is too small, NULL is returned.
1858 static inline const void *rte_pktmbuf_read(const struct rte_mbuf *m,
1859 uint32_t off, uint32_t len, void *buf)
1861 if (likely(off + len <= rte_pktmbuf_data_len(m)))
1862 return rte_pktmbuf_mtod_offset(m, char *, off);
1864 return __rte_pktmbuf_read(m, off, len, buf);
1868 * Chain an mbuf to another, thereby creating a segmented packet.
1870 * Note: The implementation will do a linear walk over the segments to find
1871 * the tail entry. For cases when there are many segments, it's better to
1872 * chain the entries manually.
1875 * The head of the mbuf chain (the first packet)
1877 * The mbuf to put last in the chain
1881 * - -EOVERFLOW, if the chain segment limit exceeded
1883 static inline int rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail)
1885 struct rte_mbuf *cur_tail;
1887 /* Check for number-of-segments-overflow */
1888 if (head->nb_segs + tail->nb_segs > RTE_MBUF_MAX_NB_SEGS)
1891 /* Chain 'tail' onto the old tail */
1892 cur_tail = rte_pktmbuf_lastseg(head);
1893 cur_tail->next = tail;
1895 /* accumulate number of segments and total length. */
1896 head->nb_segs += tail->nb_segs;
1897 head->pkt_len += tail->pkt_len;
1899 /* pkt_len is only set in the head */
1900 tail->pkt_len = tail->data_len;
1906 * Validate general requirements for Tx offload in mbuf.
1908 * This function checks correctness and completeness of Tx offload settings.
1911 * The packet mbuf to be validated.
1913 * 0 if packet is valid
1916 rte_validate_tx_offload(const struct rte_mbuf *m)
1918 uint64_t ol_flags = m->ol_flags;
1919 uint64_t inner_l3_offset = m->l2_len;
1921 /* Does packet set any of available offloads? */
1922 if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
1925 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
1926 inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
1928 /* Headers are fragmented */
1929 if (rte_pktmbuf_data_len(m) < inner_l3_offset + m->l3_len + m->l4_len)
1932 /* IP checksum can be counted only for IPv4 packet */
1933 if ((ol_flags & PKT_TX_IP_CKSUM) && (ol_flags & PKT_TX_IPV6))
1936 /* IP type not set when required */
1937 if (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG))
1938 if (!(ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6)))
1941 /* Check requirements for TSO packet */
1942 if (ol_flags & PKT_TX_TCP_SEG)
1943 if ((m->tso_segsz == 0) ||
1944 ((ol_flags & PKT_TX_IPV4) &&
1945 !(ol_flags & PKT_TX_IP_CKSUM)))
1948 /* PKT_TX_OUTER_IP_CKSUM set for non outer IPv4 packet. */
1949 if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) &&
1950 !(ol_flags & PKT_TX_OUTER_IPV4))
1957 * Linearize data in mbuf.
1959 * This function moves the mbuf data in the first segment if there is enough
1960 * tailroom. The subsequent segments are unchained and freed.
1969 rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
1971 int seg_len, copy_len;
1973 struct rte_mbuf *m_next;
1976 if (rte_pktmbuf_is_contiguous(mbuf))
1979 /* Extend first segment to the total packet length */
1980 copy_len = rte_pktmbuf_pkt_len(mbuf) - rte_pktmbuf_data_len(mbuf);
1982 if (unlikely(copy_len > rte_pktmbuf_tailroom(mbuf)))
1985 buffer = rte_pktmbuf_mtod_offset(mbuf, char *, mbuf->data_len);
1986 mbuf->data_len = (uint16_t)(mbuf->pkt_len);
1988 /* Append data from next segments to the first one */
1993 seg_len = rte_pktmbuf_data_len(m);
1994 rte_memcpy(buffer, rte_pktmbuf_mtod(m, char *), seg_len);
1997 rte_pktmbuf_free_seg(m);
2008 * Dump an mbuf structure to a file.
2010 * Dump all fields for the given packet mbuf and all its associated
2011 * segments (in the case of a chained buffer).
2014 * A pointer to a file for output
2018 * If dump_len != 0, also dump the "dump_len" first data bytes of
2021 void rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len);
2027 #endif /* _RTE_MBUF_H_ */