4 * Copyright(c) 2010-2013 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 /** Offload features */
119 union rte_vlan_macip {
122 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
123 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
125 /**< VLAN Tag Control Identifier (CPU order). */
130 * Compare mask for vlan_macip_len.data,
131 * should be in sync with rte_vlan_macip.f layout.
133 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
134 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
135 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
136 /**< MAC+IP length. */
137 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
140 * A packet message buffer.
143 /* valid for any segment */
144 struct rte_mbuf *next; /**< Next segment of scattered packet. */
145 void* data; /**< Start address of data in segment buffer. */
146 uint16_t data_len; /**< Amount of data in segment buffer. */
148 /* these fields are valid for first segment only */
149 uint8_t nb_segs; /**< Number of segments. */
150 uint8_t in_port; /**< Input port. */
151 uint32_t pkt_len; /**< Total pkt len: sum of all segment data_len. */
153 /* offload features */
154 union rte_vlan_macip vlan_macip;
156 uint32_t rss; /**< RSS hash result if RSS enabled */
160 } fdir; /**< Filter identifier if FDIR enabled */
161 uint32_t sched; /**< Hierarchical scheduler */
162 } hash; /**< hash information */
166 * This enum indicates the mbuf type.
169 RTE_MBUF_CTRL, /**< Control mbuf. */
170 RTE_MBUF_PKT, /**< Packet mbuf. */
174 * The generic rte_mbuf, containing a packet mbuf or a control mbuf.
177 struct rte_mempool *pool; /**< Pool from which mbuf was allocated. */
178 void *buf_addr; /**< Virtual address of segment buffer. */
179 phys_addr_t buf_physaddr; /**< Physical address of segment buffer. */
180 uint16_t buf_len; /**< Length of segment buffer. */
181 #ifdef RTE_MBUF_SCATTER_GATHER
183 * 16-bit Reference counter.
184 * It should only be accessed using the following functions:
185 * rte_mbuf_refcnt_update(), rte_mbuf_refcnt_read(), and
186 * rte_mbuf_refcnt_set(). The functionality of these functions (atomic,
187 * or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
191 rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */
192 uint16_t refcnt; /**< Non-atomically accessed refcnt */
195 uint16_t refcnt_reserved; /**< Do not use this field */
197 uint8_t type; /**< Type of mbuf. */
198 uint8_t reserved; /**< Unused field. Required for padding. */
199 uint16_t ol_flags; /**< Offload features. */
202 struct rte_ctrlmbuf ctrl;
203 struct rte_pktmbuf pkt;
205 } __rte_cache_aligned;
208 * Given the buf_addr returns the pointer to corresponding mbuf.
210 #define RTE_MBUF_FROM_BADDR(ba) (((struct rte_mbuf *)(ba)) - 1)
213 * Given the pointer to mbuf returns an address where it's buf_addr
216 #define RTE_MBUF_TO_BADDR(mb) (((struct rte_mbuf *)(mb)) + 1)
219 * Returns TRUE if given mbuf is indirect, or FALSE otherwise.
221 #define RTE_MBUF_INDIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) != (mb))
224 * Returns TRUE if given mbuf is direct, or FALSE otherwise.
226 #define RTE_MBUF_DIRECT(mb) (RTE_MBUF_FROM_BADDR((mb)->buf_addr) == (mb))
230 * Private data in case of pktmbuf pool.
232 * A structure that contains some pktmbuf_pool-specific data that are
233 * appended after the mempool structure (in private data).
235 struct rte_pktmbuf_pool_private {
236 uint16_t mbuf_data_room_size; /**< Size of data space in each mbuf.*/
239 #ifdef RTE_LIBRTE_MBUF_DEBUG
241 /** check mbuf type in debug mode */
242 #define __rte_mbuf_sanity_check(m, t, is_h) rte_mbuf_sanity_check(m, t, is_h)
244 /** check mbuf type in debug mode if mbuf pointer is not null */
245 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { \
247 rte_mbuf_sanity_check(m, t, is_h); \
250 /** MBUF asserts in debug mode */
251 #define RTE_MBUF_ASSERT(exp) \
253 rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
256 #else /* RTE_LIBRTE_MBUF_DEBUG */
258 /** check mbuf type in debug mode */
259 #define __rte_mbuf_sanity_check(m, t, is_h) do { } while(0)
261 /** check mbuf type in debug mode if mbuf pointer is not null */
262 #define __rte_mbuf_sanity_check_raw(m, t, is_h) do { } while(0)
264 /** MBUF asserts in debug mode */
265 #define RTE_MBUF_ASSERT(exp) do { } while(0)
267 #endif /* RTE_LIBRTE_MBUF_DEBUG */
269 #ifdef RTE_MBUF_SCATTER_GATHER
270 #ifdef RTE_MBUF_REFCNT_ATOMIC
273 * Adds given value to an mbuf's refcnt and returns its new value.
277 * Value to add/subtract
281 static inline uint16_t
282 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
284 return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value));
288 * Reads the value of an mbuf's refcnt.
292 * Reference count number.
294 static inline uint16_t
295 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
297 return (uint16_t)(rte_atomic16_read(&m->refcnt_atomic));
301 * Sets an mbuf's refcnt to a defined value.
308 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
310 rte_atomic16_set(&m->refcnt_atomic, new_value);
313 #else /* ! RTE_MBUF_REFCNT_ATOMIC */
316 * Adds given value to an mbuf's refcnt and returns its new value.
318 static inline uint16_t
319 rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value)
321 m->refcnt = (uint16_t)(m->refcnt + value);
326 * Reads the value of an mbuf's refcnt.
328 static inline uint16_t
329 rte_mbuf_refcnt_read(const struct rte_mbuf *m)
335 * Sets an mbuf's refcnt to the defined value.
338 rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)
340 m->refcnt = new_value;
343 #endif /* RTE_MBUF_REFCNT_ATOMIC */
346 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { \
351 #else /* ! RTE_MBUF_SCATTER_GATHER */
354 #define RTE_MBUF_PREFETCH_TO_FREE(m) do { } while(0)
356 #define rte_mbuf_refcnt_set(m,v) do { } while(0)
358 #endif /* RTE_MBUF_SCATTER_GATHER */
362 * Sanity checks on an mbuf.
364 * Check the consistency of the given mbuf. The function will cause a
365 * panic if corruption is detected.
368 * The mbuf to be checked.
370 * The expected type of the mbuf.
372 * True if the mbuf is a packet header, false if it is a sub-segment
373 * of a packet (in this case, some fields like nb_segs are not checked)
376 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
380 * @internal Allocate a new mbuf from mempool *mp*.
381 * The use of that function is reserved for RTE internal needs.
382 * Please use either rte_ctrlmbuf_alloc() or rte_pktmbuf_alloc().
385 * The mempool from which mbuf is allocated.
387 * - The pointer to the new mbuf on success.
388 * - NULL if allocation failed.
390 static inline struct rte_mbuf *__rte_mbuf_raw_alloc(struct rte_mempool *mp)
394 if (rte_mempool_get(mp, &mb) < 0)
396 m = (struct rte_mbuf *)mb;
397 #ifdef RTE_MBUF_SCATTER_GATHER
398 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
399 rte_mbuf_refcnt_set(m, 1);
400 #endif /* RTE_MBUF_SCATTER_GATHER */
405 * @internal Put mbuf back into its original mempool.
406 * The use of that function is reserved for RTE internal needs.
407 * Please use either rte_ctrlmbuf_free() or rte_pktmbuf_free().
410 * The mbuf to be freed.
412 static inline void __attribute__((always_inline))
413 __rte_mbuf_raw_free(struct rte_mbuf *m)
415 #ifdef RTE_MBUF_SCATTER_GATHER
416 RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(m) == 0);
417 #endif /* RTE_MBUF_SCATTER_GATHER */
418 rte_mempool_put(m->pool, m);
421 /* Operations on ctrl mbuf */
424 * The control mbuf constructor.
426 * This function initializes some fields in an mbuf structure that are
427 * not modified by the user once created (mbuf type, origin pool, buffer
428 * start address, and so on). This function is given as a callback function
429 * to rte_mempool_create() at pool creation time.
432 * The mempool from which the mbuf is allocated.
434 * A pointer that can be used by the user to retrieve useful information
435 * for mbuf initialization. This pointer comes from the ``init_arg``
436 * parameter of rte_mempool_create().
438 * The mbuf to initialize.
440 * The index of the mbuf in the pool table.
442 void rte_ctrlmbuf_init(struct rte_mempool *mp, void *opaque_arg,
443 void *m, unsigned i);
446 * Allocate a new mbuf (type is ctrl) from mempool *mp*.
448 * This new mbuf is initialized with data pointing to the beginning of
449 * buffer, and with a length of zero.
452 * The mempool from which the mbuf is allocated.
454 * - The pointer to the new mbuf on success.
455 * - NULL if allocation failed.
457 static inline struct rte_mbuf *rte_ctrlmbuf_alloc(struct rte_mempool *mp)
460 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL) {
461 m->ctrl.data = m->buf_addr;
462 m->ctrl.data_len = 0;
463 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
469 * Free a control mbuf back into its original mempool.
472 * The control mbuf to be freed.
474 static inline void rte_ctrlmbuf_free(struct rte_mbuf *m)
476 __rte_mbuf_sanity_check(m, RTE_MBUF_CTRL, 0);
477 #ifdef RTE_MBUF_SCATTER_GATHER
478 if (rte_mbuf_refcnt_update(m, -1) == 0)
479 #endif /* RTE_MBUF_SCATTER_GATHER */
480 __rte_mbuf_raw_free(m);
484 * A macro that returns the pointer to the carried data.
486 * The value that can be read or assigned.
491 #define rte_ctrlmbuf_data(m) ((m)->ctrl.data)
494 * A macro that returns the length of the carried data.
496 * The value that can be read or assigned.
501 #define rte_ctrlmbuf_len(m) ((m)->ctrl.data_len)
503 /* Operations on pkt mbuf */
506 * The packet mbuf constructor.
508 * This function initializes some fields in the mbuf structure that are not
509 * modified by the user once created (mbuf type, origin pool, buffer start
510 * address, and so on). This function is given as a callback function to
511 * rte_mempool_create() at pool creation time.
514 * The mempool from which mbufs originate.
516 * A pointer that can be used by the user to retrieve useful information
517 * for mbuf initialization. This pointer comes from the ``init_arg``
518 * parameter of rte_mempool_create().
520 * The mbuf to initialize.
522 * The index of the mbuf in the pool table.
524 void rte_pktmbuf_init(struct rte_mempool *mp, void *opaque_arg,
525 void *m, unsigned i);
529 * A packet mbuf pool constructor.
531 * This function initializes the mempool private data in the case of a
532 * pktmbuf pool. This private data is needed by the driver. The
533 * function is given as a callback function to rte_mempool_create() at
534 * pool creation. It can be extended by the user, for example, to
535 * provide another packet size.
538 * The mempool from which mbufs originate.
540 * A pointer that can be used by the user to retrieve useful information
541 * for mbuf initialization. This pointer comes from the ``init_arg``
542 * parameter of rte_mempool_create().
544 void rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg);
547 * Reset the fields of a packet mbuf to their default values.
549 * The given mbuf must have only one segment.
552 * The packet mbuf to be resetted.
554 static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
560 m->pkt.vlan_macip.data = 0;
562 m->pkt.in_port = 0xff;
565 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
566 RTE_PKTMBUF_HEADROOM : m->buf_len;
567 m->pkt.data = (char*) m->buf_addr + buf_ofs;
570 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
574 * Allocate a new mbuf (type is pkt) from a mempool.
576 * This new mbuf contains one segment, which has a length of 0. The pointer
577 * to data is initialized to have some bytes of headroom in the buffer
578 * (if buffer size allows).
581 * The mempool from which the mbuf is allocated.
583 * - The pointer to the new mbuf on success.
584 * - NULL if allocation failed.
586 static inline struct rte_mbuf *rte_pktmbuf_alloc(struct rte_mempool *mp)
589 if ((m = __rte_mbuf_raw_alloc(mp)) != NULL)
590 rte_pktmbuf_reset(m);
594 #ifdef RTE_MBUF_SCATTER_GATHER
597 * Attach packet mbuf to another packet mbuf.
598 * After attachment we refer the mbuf we attached as 'indirect',
599 * while mbuf we attached to as 'direct'.
600 * Right now, not supported:
601 * - attachment to indirect mbuf (e.g. - md has to be direct).
602 * - attachment for already indirect mbuf (e.g. - mi has to be direct).
603 * - mbuf we trying to attach (mi) is used by someone else
604 * e.g. it's reference counter is greater then 1.
607 * The indirect packet mbuf.
609 * The direct packet mbuf.
612 static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *md)
614 RTE_MBUF_ASSERT(RTE_MBUF_DIRECT(md) &&
615 RTE_MBUF_DIRECT(mi) &&
616 rte_mbuf_refcnt_read(mi) == 1);
618 rte_mbuf_refcnt_update(md, 1);
619 mi->buf_physaddr = md->buf_physaddr;
620 mi->buf_addr = md->buf_addr;
621 mi->buf_len = md->buf_len;
626 mi->pkt.pkt_len = mi->pkt.data_len;
629 __rte_mbuf_sanity_check(mi, RTE_MBUF_PKT, 1);
630 __rte_mbuf_sanity_check(md, RTE_MBUF_PKT, 0);
634 * Detach an indirect packet mbuf -
635 * - restore original mbuf address and length values.
636 * - reset pktmbuf data and data_len to their default values.
637 * All other fields of the given packet mbuf will be left intact.
640 * The indirect attached packet mbuf.
643 static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
645 const struct rte_mempool *mp = m->pool;
646 void *buf = RTE_MBUF_TO_BADDR(m);
648 uint32_t buf_len = mp->elt_size - sizeof(*m);
649 m->buf_physaddr = rte_mempool_virt2phy(mp, m) + sizeof (*m);
652 m->buf_len = (uint16_t)buf_len;
654 buf_ofs = (RTE_PKTMBUF_HEADROOM <= m->buf_len) ?
655 RTE_PKTMBUF_HEADROOM : m->buf_len;
656 m->pkt.data = (char*) m->buf_addr + buf_ofs;
661 #endif /* RTE_MBUF_SCATTER_GATHER */
664 * Free a segment of a packet mbuf into its original mempool.
666 * Free an mbuf, without parsing other segments in case of chained
670 * The packet mbuf segment to be freed.
672 static inline void __attribute__((always_inline))
673 rte_pktmbuf_free_seg(struct rte_mbuf *m)
675 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
677 #ifdef RTE_MBUF_SCATTER_GATHER
678 if (likely (rte_mbuf_refcnt_read(m) == 1) ||
679 likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
680 struct rte_mbuf *md = RTE_MBUF_FROM_BADDR(m->buf_addr);
682 rte_mbuf_refcnt_set(m, 0);
684 /* if this is an indirect mbuf, then
686 * - free attached mbuf segment
688 if (unlikely (md != m)) {
689 rte_pktmbuf_detach(m);
690 if (rte_mbuf_refcnt_update(md, -1) == 0)
691 __rte_mbuf_raw_free(md);
694 __rte_mbuf_raw_free(m);
695 #ifdef RTE_MBUF_SCATTER_GATHER
701 * Free a packet mbuf back into its original mempool.
703 * Free an mbuf, and all its segments in case of chained buffers. Each
704 * segment is added back into its original mempool.
707 * The packet mbuf to be freed.
709 static inline void rte_pktmbuf_free(struct rte_mbuf *m)
711 struct rte_mbuf *m_next;
713 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
716 m_next = m->pkt.next;
717 rte_pktmbuf_free_seg(m);
722 #ifdef RTE_MBUF_SCATTER_GATHER
725 * Creates a "clone" of the given packet mbuf.
727 * Walks through all segments of the given packet mbuf, and for each of them:
728 * - Creates a new packet mbuf from the given pool.
729 * - Attaches newly created mbuf to the segment.
730 * Then updates pkt_len and nb_segs of the "clone" packet mbuf to match values
731 * from the original packet mbuf.
734 * The packet mbuf to be cloned.
736 * The mempool from which the "clone" mbufs are allocated.
738 * - The pointer to the new "clone" mbuf on success.
739 * - NULL if allocation fails.
741 static inline struct rte_mbuf *rte_pktmbuf_clone(struct rte_mbuf *md,
742 struct rte_mempool *mp)
744 struct rte_mbuf *mc, *mi, **prev;
748 if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
752 prev = &mi->pkt.next;
753 pktlen = md->pkt.pkt_len;
758 rte_pktmbuf_attach(mi, md);
760 prev = &mi->pkt.next;
761 } while ((md = md->pkt.next) != NULL &&
762 (mi = rte_pktmbuf_alloc(mp)) != NULL);
765 mc->pkt.nb_segs = nseg;
766 mc->pkt.pkt_len = pktlen;
768 /* Allocation of new indirect segment failed */
769 if (unlikely (mi == NULL)) {
770 rte_pktmbuf_free(mc);
774 __rte_mbuf_sanity_check(mc, RTE_MBUF_PKT, 1);
779 * Adds given value to the refcnt of all packet mbuf segments.
781 * Walks through all segments of given packet mbuf and for each of them
782 * invokes rte_mbuf_refcnt_update().
785 * The packet mbuf whose refcnt to be updated.
787 * The value to add to the mbuf's segments refcnt.
789 static inline void rte_pktmbuf_refcnt_update(struct rte_mbuf *m, int16_t v)
791 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
794 rte_mbuf_refcnt_update(m, v);
795 } while ((m = m->pkt.next) != NULL);
798 #endif /* RTE_MBUF_SCATTER_GATHER */
801 * Get the headroom in a packet mbuf.
806 * The length of the headroom.
808 static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf *m)
810 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
811 return (uint16_t) ((char*) m->pkt.data - (char*) m->buf_addr);
815 * Get the tailroom of a packet mbuf.
820 * The length of the tailroom.
822 static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf *m)
824 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
825 return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) -
830 * Get the last segment of the packet.
835 * The last segment of the given mbuf.
837 static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m)
839 struct rte_mbuf *m2 = (struct rte_mbuf *)m;
841 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
842 while (m2->pkt.next != NULL)
848 * A macro that points to the start of the data in the mbuf.
850 * The returned pointer is cast to type t. Before using this
851 * function, the user must ensure that m_headlen(m) is large enough to
857 * The type to cast the result into.
859 #define rte_pktmbuf_mtod(m, t) ((t)((m)->pkt.data))
862 * A macro that returns the length of the packet.
864 * The value can be read or assigned.
869 #define rte_pktmbuf_pkt_len(m) ((m)->pkt.pkt_len)
872 * A macro that returns the length of the segment.
874 * The value can be read or assigned.
879 #define rte_pktmbuf_data_len(m) ((m)->pkt.data_len)
882 * Prepend len bytes to an mbuf data area.
884 * Returns a pointer to the new
885 * data start address. If there is not enough headroom in the first
886 * segment, the function will return NULL, without modifying the mbuf.
891 * The amount of data to prepend (in bytes).
893 * A pointer to the start of the newly prepended data, or
894 * NULL if there is not enough headroom space in the first segment
896 static inline char *rte_pktmbuf_prepend(struct rte_mbuf *m,
899 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
901 if (unlikely(len > rte_pktmbuf_headroom(m)))
904 m->pkt.data = (char*) m->pkt.data - len;
905 m->pkt.data_len = (uint16_t)(m->pkt.data_len + len);
906 m->pkt.pkt_len = (m->pkt.pkt_len + len);
908 return (char*) m->pkt.data;
912 * Append len bytes to an mbuf.
914 * Append len bytes to an mbuf and return a pointer to the start address
915 * of the added data. If there is not enough tailroom in the last
916 * segment, the function will return NULL, without modifying the mbuf.
921 * The amount of data to append (in bytes).
923 * A pointer to the start of the newly appended data, or
924 * NULL if there is not enough tailroom space in the last segment
926 static inline char *rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len)
929 struct rte_mbuf *m_last;
931 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
933 m_last = rte_pktmbuf_lastseg(m);
934 if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
937 tail = (char*) m_last->pkt.data + m_last->pkt.data_len;
938 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len + len);
939 m->pkt.pkt_len = (m->pkt.pkt_len + len);
944 * Remove len bytes at the beginning of an mbuf.
946 * Returns a pointer to the start address of the new data area. If the
947 * length is greater than the length of the first segment, then the
948 * function will fail and return NULL, without modifying the mbuf.
953 * The amount of data to remove (in bytes).
955 * A pointer to the new start of the data.
957 static inline char *rte_pktmbuf_adj(struct rte_mbuf *m, uint16_t len)
959 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
961 if (unlikely(len > m->pkt.data_len))
964 m->pkt.data_len = (uint16_t)(m->pkt.data_len - len);
965 m->pkt.data = ((char*) m->pkt.data + len);
966 m->pkt.pkt_len = (m->pkt.pkt_len - len);
967 return (char*) m->pkt.data;
971 * Remove len bytes of data at the end of the mbuf.
973 * If the length is greater than the length of the last segment, the
974 * function will fail and return -1 without modifying the mbuf.
979 * The amount of data to remove (in bytes).
984 static inline int rte_pktmbuf_trim(struct rte_mbuf *m, uint16_t len)
986 struct rte_mbuf *m_last;
988 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
990 m_last = rte_pktmbuf_lastseg(m);
991 if (unlikely(len > m_last->pkt.data_len))
994 m_last->pkt.data_len = (uint16_t)(m_last->pkt.data_len - len);
995 m->pkt.pkt_len = (m->pkt.pkt_len - len);
1000 * Test if mbuf data is contiguous.
1005 * - 1, if all data is contiguous (one segment).
1006 * - 0, if there is several segments.
1008 static inline int rte_pktmbuf_is_contiguous(const struct rte_mbuf *m)
1010 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
1011 return !!(m->pkt.nb_segs == 1);
1015 * Dump an mbuf structure to the console.
1017 * Dump all fields for the given packet mbuf and all its associated
1018 * segments (in the case of a chained buffer).
1023 * If dump_len != 0, also dump the "dump_len" first data bytes of
1026 void rte_pktmbuf_dump(const struct rte_mbuf *m, unsigned dump_len);
1032 #endif /* _RTE_MBUF_H_ */