4 * Copyright(c) 2010-2012 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.
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 mbufs, manipulate
48 * control message buffer (ctrlmbuf), which are generic message
49 * buffers, and packet buffers (pktmbuf), which are used to carry
52 * To understand the concepts of packet buffers or mbufs, you
53 * should read "TCP/IP Illustrated, Volume 2: The Implementation,
54 * Addison-Wesley, 1995, ISBN 0-201-63354-X from Richard Stevens"
55 * http://www.kohala.com/start/tcpipiv2.html
57 * The main modification of this implementation is the use of mbuf for
58 * transports other than packets. mbufs can have other types.
62 #include <rte_mempool.h>
63 #include <rte_atomic.h>
64 #include <rte_prefetch.h>
65 #include <rte_branch_prediction.h>
72 * A control message buffer.
75 void *data; /**< Pointer to data. */
76 uint32_t data_len; /**< Length of data. */
81 * Packet Offload Features Flags. It also carry packet type information.
82 * Critical resources. Both rx/tx shared these bits. Be cautious on any change
84 #define PKT_RX_VLAN_PKT 0x0001 /**< RX packet is a 802.1q VLAN packet. */
85 #define PKT_RX_RSS_HASH 0x0002 /**< RX packet with RSS hash result. */
86 #define PKT_RX_FDIR 0x0004 /**< RX packet with FDIR infos. */
87 #define PKT_RX_L4_CKSUM_BAD 0x0008 /**< L4 cksum of RX pkt. is not OK. */
88 #define PKT_RX_IP_CKSUM_BAD 0x0010 /**< IP cksum of RX pkt. is not OK. */
89 #define PKT_RX_IPV4_HDR 0x0020 /**< RX packet with IPv4 header. */
90 #define PKT_RX_IPV4_HDR_EXT 0x0040 /**< RX packet with extended IPv4 header. */
91 #define PKT_RX_IPV6_HDR 0x0080 /**< RX packet with IPv6 header. */
92 #define PKT_RX_IPV6_HDR_EXT 0x0100 /**< RX packet with extended IPv6 header. */
93 #define PKT_RX_IEEE1588_PTP 0x0200 /**< RX IEEE1588 L2 Ethernet PT Packet. */
94 #define PKT_RX_IEEE1588_TMST 0x0400 /**< RX IEEE1588 L2/L4 timestamped packet.*/
96 #define PKT_TX_VLAN_PKT 0x0800 /**< TX packet is a 802.1q VLAN packet. */
97 #define PKT_TX_IP_CKSUM 0x1000 /**< IP cksum of TX pkt. computed by NIC. */
99 * Bit 14~13 used for L4 packet type with checksum enabled.
105 #define PKT_TX_L4_MASK 0x6000 /**< Mask bits for L4 checksum offload request. */
106 #define PKT_TX_L4_NO_CKSUM 0x0000 /**< Disable L4 cksum of TX pkt. */
107 #define PKT_TX_TCP_CKSUM 0x2000 /**< TCP cksum of TX pkt. computed by NIC. */
108 #define PKT_TX_SCTP_CKSUM 0x4000 /**< SCTP cksum of TX pkt. computed by NIC. */
109 #define PKT_TX_UDP_CKSUM 0x6000 /**< UDP cksum of TX pkt. computed by NIC. */
111 #define PKT_TX_IEEE1588_TMST 0x8000 /**< TX IEEE1588 packet to timestamp. */
114 * Bit Mask to indicate what bits required for building TX context
116 #define PKT_TX_OFFLOAD_MASK (PKT_TX_VLAN_PKT | PKT_TX_IP_CKSUM | PKT_TX_L4_MASK)
118 /* Compare mask for vlan_macip_lens, used for context build up */
119 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
120 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
121 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
122 /** MAC+IP length. */
123 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
126 * A packet message buffer.
129 /* valid for any segment */
130 struct rte_mbuf *next; /**< Next segment of scattered packet. */
131 void* data; /**< Start address of data in segment buffer. */
132 uint16_t data_len; /**< Amount of data in segment buffer. */
134 /* these fields are valid for first segment only */
135 uint8_t nb_segs; /**< Number of segments. */
136 uint8_t in_port; /**< Input port. */
137 uint32_t pkt_len; /**< Total pkt len: sum of all segment data_len. */
139 /* offload features */
140 uint16_t vlan_tci; /**< VLAN Tag Control Identifier (CPU order). */
141 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
142 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
144 uint32_t rss; /**< RSS hash result if RSS enabled */
148 } fdir; /**< Filter identifier if FDIR enabled */
149 } hash; /**< hash information */
153 * This enum indicates the mbuf type.
156 RTE_MBUF_CTRL, /**< Control mbuf. */
157 RTE_MBUF_PKT, /**< Packet mbuf. */
161 * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
164 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
165 void *buf_addr; /**< Virtual address of segment buffer. */
166 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
167 uint16_t buf_len; /**< Length of segment buffer. */
168 #ifdef RTE_MBUF_SCATTER_GATHER
170 * 16-bit Reference counter.
171 * It should only be accessed using the following functions:
172 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
173 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
174 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
178 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
179 uint16_t refcnt; /**< Non-atomically accessed refcnt */
182 uint16_t refcnt_reserved; /**< Do not use this field */
184 uint8_t type; /**< Type of mbuf. */
185 uint8_t reserved; /**< Unused field. Required for padding. */
186 uint16_t ol_flags; /**< Offload features. */
189 struct rte_ctrlmbuf ctrl;
190 struct rte_pktmbuf pkt;
192 } __rte_cache_aligned;
195 * Given the buf_addr returns the pointer to corresponding mbuf.
197 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
200 * Given the pointer to mbuf returns an address where it's buf_addr
203 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
206 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
208 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
211 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
213 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
217 * Private data in case of pktmbuf pool.
219 * A structure that contains some pktmbuf_pool-specific data that are
220 * appended after the mempool structure (in private data).
222 struct rte_pktmbuf_pool_private {
223 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
226 #ifdef RTE_LIBRTE_MBUF_DEBUG
228 /** check mbuf type in debug mode */
229 #define __rte_mbuf_sanity_check(m, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
231 /** check mbuf type in debug mode if mbuf pointer is not null */
232 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { \
234 rte_mbuf_sanity_check(m, t, is_h); \
237 /** MBUF asserts in debug mode */
238 #define RTE_MBUF_ASSERT(exp) \
240 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
243 #else /* RTE_LIBRTE_MBUF_DEBUG */
245 /** check mbuf type in debug mode */
246 #define __rte_mbuf_sanity_check(m, t, is_h) do { } while(0)
248 /** check mbuf type in debug mode if mbuf pointer is not null */
249 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { } while(0)
251 /** MBUF asserts in debug mode */
252 #define RTE_MBUF_ASSERT(exp) do { } while(0)
254 #endif /* RTE_LIBRTE_MBUF_DEBUG */
256 #ifdef RTE_MBUF_SCATTER_GATHER
257 #ifdef RTE_MBUF_REFCNT_ATOMIC
260 * Adds given value to an mbuf's refcnt and returns its new value.
264 * Value to add/subtract
268 static inline uint16_t
269 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
271 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
275 * Reads the value of an mbuf's refcnt.
279 * Reference count number.
281 static inline uint16_t
282 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
284 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
288 * Sets an mbuf's refcnt to a defined value.
295 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
297 rte_atomic16_set(&m->refcnt_atomic, new_value);
300 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
303 * Adds given value to an mbuf's refcnt and returns its new value.
305 static inline uint16_t
306 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
308 m->refcnt = (uint16_t)(m->refcnt + value);
313 * Reads the value of an mbuf's refcnt.
315 static inline uint16_t
316 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
322 * Sets an mbuf's refcnt to the defined value.
325 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
327 m->refcnt = new_value;
330 #endif /* RTE_MBUF_REFCNT_ATOMIC */
333 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
338 #else /* ! RTE_MBUF_SCATTER_GATHER */
341 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
343 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
345 #endif /* RTE_MBUF_SCATTER_GATHER */
349 * Sanity checks on an mbuf.
351 * Check the consistency of the given mbuf. The function will cause a
352 * panic if corruption is detected.
355 * The mbuf to be checked.
357 * The expected type of the mbuf.
359 * True if the mbuf is a packet header, false if it is a sub-segment
360 * of a packet (in this case, some fields like nb_segs are not checked)
363 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
367 * @internal Allocate a new mbuf from mempool *mp*.
368 * The use of that function is reserved for RTE internal needs.
369 * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
372 * The mempool from which mbuf is allocated.
374 * - The pointer to the new mbuf on success.
375 * - NULL if allocation failed.
377 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
381 if (rte_mempool_get(mp, &mb) < 0)
383 m = (struct rte_mbuf *)mb;
384 #ifdef RTE_MBUF_SCATTER_GATHER
385 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
386 rte_mbuf_refcnt_set(m, 1);
387 #endif /* RTE_MBUF_SCATTER_GATHER */
392 * @internal Put mbuf back into its original mempool.
393 * The use of that function is reserved for RTE internal needs.
394 * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
397 * The mbuf to be freed.
399 static inline void __rte_mbuf_raw_free(struct rte_mbuf *m)
401 #ifdef RTE_MBUF_SCATTER_GATHER
402 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
403 #endif /* RTE_MBUF_SCATTER_GATHER */
404 rte_mempool_put(m->pool, m);
407 /* Operations on ctrl mbuf */
410 * The control mbuf constructor.
412 * This function initializes some fields in an mbuf structure that are
413 * not modified by the user once created (mbuf type, origin pool, buffer
414 * start address, and so on). This function is given as a callback function
415 * to rte_mempool_create() at pool creation time.
418 * The mempool from which the mbuf is allocated.
420 * A pointer that can be used by the user to retrieve useful information
421 * for mbuf initialization. This pointer comes from the ``init_arg``
422 * parameter of rte_mempool_create().
424 * The mbuf to initialize.
426 * The index of the mbuf in the pool table.
428 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
429 void *m, unsigned i);
432 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
434 * This new mbuf is initialized with data pointing to the beginning of
435 * buffer, and with a length of zero.
438 * The mempool from which the mbuf is allocated.
440 * - The pointer to the new mbuf on success.
441 * - NULL if allocation failed.
443 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
446 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
447 m->ctrl.data = m->buf_addr;
448 m->ctrl.data_len = 0;
449 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
455 * Free a control mbuf back into its original mempool.
458 * The control mbuf to be freed.
460 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
462 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
463 #ifdef RTE_MBUF_SCATTER_GATHER
464 if (rte_mbuf_refcnt_update(m, -1) == 0)
465 #endif /* RTE_MBUF_SCATTER_GATHER */
466 __rte_mbuf_raw_free(m);
470 * A macro that returns the pointer to the carried data.
472 * The value that can be read or assigned.
477 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
480 * A macro that returns the length of the carried data.
482 * The value that can be read or assigned.
487 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
489 /* Operations on pkt mbuf */
492 * The packet mbuf constructor.
494 * This function initializes some fields in the mbuf structure that are not
495 * modified by the user once created (mbuf type, origin pool, buffer start
496 * address, and so on). This function is given as a callback function to
497 * rte_mempool_create() at pool creation time.
500 * The mempool from which mbufs originate.
502 * A pointer that can be used by the user to retrieve useful information
503 * for mbuf initialization. This pointer comes from the ``init_arg``
504 * parameter of rte_mempool_create().
506 * The mbuf to initialize.
508 * The index of the mbuf in the pool table.
510 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
511 void *m, unsigned i);
515 * A packet mbuf pool constructor.
517 * This function initializes the mempool private data in the case of a
518 * pktmbuf pool. This private data is needed by the driver. The
519 * function is given as a callback function to rte_mempool_create() at
520 * pool creation. It can be extended by the user, for example, to
521 * provide another packet size.
524 * The mempool from which mbufs originate.
526 * A pointer that can be used by the user to retrieve useful information
527 * for mbuf initialization. This pointer comes from the ``init_arg``
528 * parameter of rte_mempool_create().
530 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
533 * Reset the fields of a packet mbuf to their default values.
535 * The given mbuf must have only one segment.
538 * The packet mbuf to be resetted.
540 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
550 m->pkt.in_port = 0xff;
553 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
554 RTE_PKTMBUF_HEADROOM : m->buf_len;
555 m->pkt.data = (char*) m->buf_addr + buf_ofs;
558 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
562 * Allocate a new mbuf (type is pkt) from a mempool.
564 * This new mbuf contains one segment, which has a length of 0. The pointer
565 * to data is initialized to have some bytes of headroom in the buffer
566 * (if buffer size allows).
569 * The mempool from which the mbuf is allocated.
571 * - The pointer to the new mbuf on success.
572 * - NULL if allocation failed.
574 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
577 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
578 rte_pktmbuf_reset(m);
582 #ifdef RTE_MBUF_SCATTER_GATHER
585 * Attach packet mbuf to another packet mbuf.
586 * After attachment we refer the mbuf we attached as 'indirect',
587 * while mbuf we attached to as 'direct'.
588 * Right now, not supported:
589 * - attachment to indirect mbuf (e.g. - md has to be direct).
590 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
591 * - mbuf we trying to attach (mi) is used by someone else
592 * e.g. it's reference counter is greater then 1.
595 * The indirect packet mbuf.
597 * The direct packet mbuf.
600 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
602 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
603 RTE_MBUF_DIRECT(mi) &&
604 rte_mbuf_refcnt_read(mi) == 1);
606 rte_mbuf_refcnt_update(md, 1);
607 mi->buf_physaddr = md->buf_physaddr;
608 mi->buf_addr = md->buf_addr;
609 mi->buf_len = md->buf_len;
614 mi->pkt.pkt_len = mi->pkt.data_len;
617 __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
618 __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
622 * Detach an indirect packet mbuf -
623 * - restore original mbuf address and length values.
624 * - reset pktmbuf data and data_len to their default values.
625 * All other fields of the given packet mbuf will be left intact.
628 * The indirect attached packet mbuf.
631 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
633 const struct rte_mempool *mp = m->pool;
634 void *buf = RTE_MBUF_TO_BADDR(m);
636 uint32_t buf_len = mp->elt_size - sizeof(*m);
637 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
640 m->buf_len = (uint16_t)buf_len;
642 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
643 RTE_PKTMBUF_HEADROOM : m->buf_len;
644 m->pkt.data = (char*) m->buf_addr + buf_ofs;
649 #endif /* RTE_MBUF_SCATTER_GATHER */
652 * Free a segment of a packet mbuf into its original mempool.
654 * Free an mbuf, without parsing other segments in case of chained
658 * The packet mbuf segment to be freed.
660 static inline void rte_pktmbuf_free_seg(struct rte_mbuf *m)
662 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
664 #ifdef RTE_MBUF_SCATTER_GATHER
665 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
666 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
667 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
669 rte_mbuf_refcnt_set(m, 0);
671 /* if this is an indirect mbuf, then
673 * - free attached mbuf segment
675 if (unlikely (md != m)) {
676 rte_pktmbuf_detach(m);
677 if (rte_mbuf_refcnt_update(md, -1) == 0)
678 __rte_mbuf_raw_free(md);
681 __rte_mbuf_raw_free(m);
682 #ifdef RTE_MBUF_SCATTER_GATHER
688 * Free a packet mbuf back into its original mempool.
690 * Free an mbuf, and all its segments in case of chained buffers. Each
691 * segment is added back into its original mempool.
694 * The packet mbuf to be freed.
696 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
698 struct rte_mbuf *m_next;
700 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
703 m_next = m->pkt.next;
704 rte_pktmbuf_free_seg(m);
709 #ifdef RTE_MBUF_SCATTER_GATHER
712 * Creates a "clone" of the given packet mbuf.
714 * Walks through all segments of the given packet mbuf, and for each of them:
715 * - Creates a new packet mbuf from the given pool.
716 * - Attaches newly created mbuf to the segment.
717 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
718 * from the original packet mbuf.
721 * The packet mbuf to be cloned.
723 * The mempool from which the "clone" mbufs are allocated.
725 * - The pointer to the new "clone" mbuf on success.
726 * - NULL if allocation fails.
728 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
729 struct rte_mempool *mp)
731 struct rte_mbuf *mc, *mi, **prev;
735 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
739 prev = &mi->pkt.next;
740 pktlen = md->pkt.pkt_len;
745 rte_pktmbuf_attach(mi, md);
747 prev = &mi->pkt.next;
748 } while ((md = md->pkt.next) != NULL &&
749 (mi = rte_pktmbuf_alloc(mp)) != NULL);
752 mc->pkt.nb_segs = nseg;
753 mc->pkt.pkt_len = pktlen;
755 /* Allocation of new indirect segment failed */
756 if (unlikely (mi == NULL)) {
757 rte_pktmbuf_free(mc);
761 __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
766 * Adds given value to the refcnt of all packet mbuf segments.
768 * Walks through all segments of given packet mbuf and for each of them
769 * invokes rte_mbuf_refcnt_update().
772 * The packet mbuf whose refcnt to be updated.
774 * The value to add to the mbuf's segments refcnt.
776 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
778 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
781 rte_mbuf_refcnt_update(m, v);
782 } while ((m = m->pkt.next) != NULL);
785 #endif /* RTE_MBUF_SCATTER_GATHER */
788 * Get the headroom in a packet mbuf.
793 * The length of the headroom.
795 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
797 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
798 return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
802 * Get the tailroom of a packet mbuf.
807 * The length of the tailroom.
809 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
811 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
812 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
817 * Get the last segment of the packet.
822 * The last segment of the given mbuf.
824 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
826 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
828 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
829 while (m2->pkt.next != NULL)
835 * A macro that points to the start of the data in the mbuf.
837 * The returned pointer is cast to type t. Before using this
838 * function, the user must ensure that m_headlen(m) is large enough to
844 * The type to cast the result into.
846 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
849 * A macro that returns the length of the packet.
851 * The value can be read or assigned.
856 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
859 * A macro that returns the length of the segment.
861 * The value can be read or assigned.
866 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
869 * Prepend len bytes to an mbuf data area.
871 * Returns a pointer to the new
872 * data start address. If there is not enough headroom in the first
873 * segment, the function will return NULL, without modifying the mbuf.
878 * The amount of data to prepend (in bytes).
880 * A pointer to the start of the newly prepended data, or
881 * NULL if there is not enough headroom space in the first segment
883 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
886 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
888 if (unlikely(len > rte_pktmbuf_headroom(m)))
891 m->pkt.data = (char*) m->pkt.data - len;
892 m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
893 m->pkt.pkt_len = (m->pkt.pkt_len + len);
895 return (char*) m->pkt.data;
899 * Append len bytes to an mbuf.
901 * Append len bytes to an mbuf and return a pointer to the start address
902 * of the added data. If there is not enough tailroom in the last
903 * segment, the function will return NULL, without modifying the mbuf.
908 * The amount of data to append (in bytes).
910 * A pointer to the start of the newly appended data, or
911 * NULL if there is not enough tailroom space in the last segment
913 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
916 struct rte_mbuf *m_last;
918 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
920 m_last = rte_pktmbuf_lastseg(m);
921 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
924 tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
925 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
926 m->pkt.pkt_len = (m->pkt.pkt_len + len);
931 * Remove len bytes at the beginning of an mbuf.
933 * Returns a pointer to the start address of the new data area. If the
934 * length is greater than the length of the first segment, then the
935 * function will fail and return NULL, without modifying the mbuf.
940 * The amount of data to remove (in bytes).
942 * A pointer to the new start of the data.
944 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
946 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
948 if (unlikely(len > m->pkt.data_len))
951 m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
952 m->pkt.data = ((char*) m->pkt.data + len);
953 m->pkt.pkt_len = (m->pkt.pkt_len - len);
954 return (char*) m->pkt.data;
958 * Remove len bytes of data at the end of the mbuf.
960 * If the length is greater than the length of the last segment, the
961 * function will fail and return -1 without modifying the mbuf.
966 * The amount of data to remove (in bytes).
971 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
973 struct rte_mbuf *m_last;
975 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
977 m_last = rte_pktmbuf_lastseg(m);
978 if (unlikely(len > m_last->pkt.data_len))
981 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
982 m->pkt.pkt_len = (m->pkt.pkt_len - len);
987 * Test if mbuf data is contiguous.
992 * - 1, if all data is contiguous (one segment).
993 * - 0, if there is several segments.
995 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
997 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
998 return !!(m->pkt.nb_segs == 1);
1002 * Dump an mbuf structure to the console.
1004 * Dump all fields for the given packet mbuf and all its associated
1005 * segments (in the case of a chained buffer).
1010 * If dump_len != 0, also dump the "dump_len" first data bytes of
1013 void rte_pktmbuf_dump(const struct rte_mbuf *m, unsigned dump_len);
1019 #endif /* _RTE_MBUF_H_ */