xref: /dpdk/doc/guides/prog_guide/mbuf_lib.rst (revision ba9e05cb6b002016b01adf4e8700f206f3d04fd6)
1fc1f2750SBernard Iremonger..  BSD LICENSE
2fc1f2750SBernard Iremonger    Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
3fc1f2750SBernard Iremonger    All rights reserved.
4fc1f2750SBernard Iremonger
5fc1f2750SBernard Iremonger    Redistribution and use in source and binary forms, with or without
6fc1f2750SBernard Iremonger    modification, are permitted provided that the following conditions
7fc1f2750SBernard Iremonger    are met:
8fc1f2750SBernard Iremonger
9fc1f2750SBernard Iremonger    * Redistributions of source code must retain the above copyright
10fc1f2750SBernard Iremonger    notice, this list of conditions and the following disclaimer.
11fc1f2750SBernard Iremonger    * Redistributions in binary form must reproduce the above copyright
12fc1f2750SBernard Iremonger    notice, this list of conditions and the following disclaimer in
13fc1f2750SBernard Iremonger    the documentation and/or other materials provided with the
14fc1f2750SBernard Iremonger    distribution.
15fc1f2750SBernard Iremonger    * Neither the name of Intel Corporation nor the names of its
16fc1f2750SBernard Iremonger    contributors may be used to endorse or promote products derived
17fc1f2750SBernard Iremonger    from this software without specific prior written permission.
18fc1f2750SBernard Iremonger
19fc1f2750SBernard Iremonger    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20fc1f2750SBernard Iremonger    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21fc1f2750SBernard Iremonger    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22fc1f2750SBernard Iremonger    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23fc1f2750SBernard Iremonger    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24fc1f2750SBernard Iremonger    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25fc1f2750SBernard Iremonger    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26fc1f2750SBernard Iremonger    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27fc1f2750SBernard Iremonger    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28fc1f2750SBernard Iremonger    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29fc1f2750SBernard Iremonger    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30fc1f2750SBernard Iremonger
31fc1f2750SBernard Iremonger.. _Mbuf_Library:
32fc1f2750SBernard Iremonger
33fc1f2750SBernard IremongerMbuf Library
34fc1f2750SBernard Iremonger============
35fc1f2750SBernard Iremonger
36fc1f2750SBernard IremongerThe mbuf library provides the ability to allocate and free buffers (mbufs)
3748624fd9SSiobhan Butlerthat may be used by the DPDK application to store message buffers.
38fc1f2750SBernard IremongerThe message buffers are stored in a mempool, using the :ref:`Mempool Library <Mempool_Library>`.
39fc1f2750SBernard Iremonger
4014b5e699SBruce RichardsonA rte_mbuf struct can carry network packet buffers
4114b5e699SBruce Richardsonor generic control buffers (indicated by the CTRL_MBUF_FLAG).
42fc1f2750SBernard IremongerThis can be extended to other types.
4314b5e699SBruce RichardsonThe rte_mbuf header structure is kept as small as possible and currently uses
4414b5e699SBruce Richardsonjust two cache lines, with the most frequently used fields being on the first
4514b5e699SBruce Richardsonof the two cache lines.
46fc1f2750SBernard Iremonger
47fc1f2750SBernard IremongerDesign of Packet Buffers
48fc1f2750SBernard Iremonger------------------------
49fc1f2750SBernard Iremonger
50fc1f2750SBernard IremongerFor the storage of the packet data (including protocol headers), two approaches were considered:
51fc1f2750SBernard Iremonger
52fc1f2750SBernard Iremonger#.  Embed metadata within a single memory buffer the structure followed by a fixed size area for the packet data.
53fc1f2750SBernard Iremonger
54fc1f2750SBernard Iremonger#.  Use separate memory buffers for the metadata structure and for the packet data.
55fc1f2750SBernard Iremonger
56fc1f2750SBernard IremongerThe advantage of the first method is that it only needs one operation to allocate/free the whole memory representation of a packet.
57fc1f2750SBernard IremongerOn the other hand, the second method is more flexible and allows
58fc1f2750SBernard Iremongerthe complete separation of the allocation of metadata structures from the allocation of packet data buffers.
59fc1f2750SBernard Iremonger
6048624fd9SSiobhan ButlerThe first method was chosen for the DPDK.
61fc1f2750SBernard IremongerThe metadata contains control information such as message type, length,
6214b5e699SBruce Richardsonoffset to the start of the data and a pointer for additional mbuf structures allowing buffer chaining.
63fc1f2750SBernard Iremonger
64fc1f2750SBernard IremongerMessage buffers that are used to carry network packets can handle buffer chaining
65fc1f2750SBernard Iremongerwhere multiple buffers are required to hold the complete packet.
6614b5e699SBruce RichardsonThis is the case for jumbo frames that are composed of many mbufs linked together through their next field.
67fc1f2750SBernard Iremonger
68fc1f2750SBernard IremongerFor a newly allocated mbuf, the area at which the data begins in the message buffer is
69fc1f2750SBernard IremongerRTE_PKTMBUF_HEADROOM bytes after the beginning of the buffer, which is cache aligned.
70fc1f2750SBernard IremongerMessage buffers may be used to carry control information, packets, events,
71fc1f2750SBernard Iremongerand so on between different entities in the system.
7214b5e699SBruce RichardsonMessage buffers may also use their buffer pointers to point to other message buffer data sections or other structures.
73fc1f2750SBernard Iremonger
74fc1f2750SBernard IremongerFigure 8 and Figure 9 show some of these scenarios.
75fc1f2750SBernard Iremonger
76fc1f2750SBernard Iremonger.. _pg_figure_8:
77fc1f2750SBernard Iremonger
78fc1f2750SBernard Iremonger**Figure 8. An mbuf with One Segment**
79fc1f2750SBernard Iremonger
80fc1f2750SBernard Iremonger.. image22_png  has been replaced
81fc1f2750SBernard Iremonger
82fc1f2750SBernard Iremonger|mbuf1|
83fc1f2750SBernard Iremonger
84fc1f2750SBernard Iremonger.. _pg_figure_9:
85fc1f2750SBernard Iremonger
86fc1f2750SBernard Iremonger**Figure 9. An mbuf with Three Segments**
87fc1f2750SBernard Iremonger
88fc1f2750SBernard Iremonger.. image23_png has been replaced
89fc1f2750SBernard Iremonger
90fc1f2750SBernard Iremonger|mbuf2|
91fc1f2750SBernard Iremonger
92fc1f2750SBernard IremongerThe Buffer Manager implements a fairly standard set of buffer access functions to manipulate network packets.
93fc1f2750SBernard Iremonger
94fc1f2750SBernard IremongerBuffers Stored in Memory Pools
95fc1f2750SBernard Iremonger------------------------------
96fc1f2750SBernard Iremonger
97fc1f2750SBernard IremongerThe Buffer Manager uses the :ref:`Mempool Library <Mempool_Library>` to allocate buffers.
98fc1f2750SBernard IremongerTherefore, it ensures that the packet header is interleaved optimally across the channels and ranks for L3 processing.
99fc1f2750SBernard IremongerAn mbuf contains a field indicating the pool that it originated from.
100fc1f2750SBernard IremongerWhen calling rte_ctrlmbuf_free(m) or rte_pktmbuf_free(m), the mbuf returns to its original pool.
101fc1f2750SBernard Iremonger
102fc1f2750SBernard IremongerConstructors
103fc1f2750SBernard Iremonger------------
104fc1f2750SBernard Iremonger
105fc1f2750SBernard IremongerPacket and control mbuf constructors are provided by the API.
106fc1f2750SBernard IremongerThe rte_pktmbuf_init() and rte_ctrlmbuf_init() functions initialize some fields in the mbuf structure that
107fc1f2750SBernard Iremongerare not modified by the user once created (mbuf type, origin pool, buffer start address, and so on).
108fc1f2750SBernard IremongerThis function is given as a callback function to the rte_mempool_create() function at pool creation time.
109fc1f2750SBernard Iremonger
110fc1f2750SBernard IremongerAllocating and Freeing mbufs
111fc1f2750SBernard Iremonger----------------------------
112fc1f2750SBernard Iremonger
113fc1f2750SBernard IremongerAllocating a new mbuf requires the user to specify the mempool from which the mbuf should be taken.
11414b5e699SBruce RichardsonFor any newly-allocated mbuf, it contains one segment, with a length of 0.
11514b5e699SBruce RichardsonThe offset to data is initialized to have some bytes of headroom in the buffer (RTE_PKTMBUF_HEADROOM).
116fc1f2750SBernard Iremonger
117fc1f2750SBernard IremongerFreeing a mbuf means returning it into its original mempool.
118fc1f2750SBernard IremongerThe content of an mbuf is not modified when it is stored in a pool (as a free mbuf).
119fc1f2750SBernard IremongerFields initialized by the constructor do not need to be re-initialized at mbuf allocation.
120fc1f2750SBernard Iremonger
121fc1f2750SBernard IremongerWhen freeing a packet mbuf that contains several segments, all of them are freed and returned to their original mempool.
122fc1f2750SBernard Iremonger
123fc1f2750SBernard IremongerManipulating mbufs
124fc1f2750SBernard Iremonger------------------
125fc1f2750SBernard Iremonger
126fc1f2750SBernard IremongerThis library provides some functions for manipulating the data in a packet mbuf. For instance:
127fc1f2750SBernard Iremonger
128fc1f2750SBernard Iremonger    *  Get data length
129fc1f2750SBernard Iremonger
130fc1f2750SBernard Iremonger    *  Get a pointer to the start of data
131fc1f2750SBernard Iremonger
132fc1f2750SBernard Iremonger    *  Prepend data before data
133fc1f2750SBernard Iremonger
134fc1f2750SBernard Iremonger    *   Append data after data
135fc1f2750SBernard Iremonger
136fc1f2750SBernard Iremonger    *   Remove data at the beginning of the buffer (rte_pktmbuf_adj())
137fc1f2750SBernard Iremonger
13848624fd9SSiobhan Butler    *   Remove data at the end of the buffer (rte_pktmbuf_trim()) Refer to the *DPDK API Reference* for details.
139fc1f2750SBernard Iremonger
140fc1f2750SBernard IremongerMeta Information
141fc1f2750SBernard Iremonger----------------
142fc1f2750SBernard Iremonger
143fc1f2750SBernard IremongerSome information is retrieved by the network driver and stored in an mbuf to make processing easier.
144fc1f2750SBernard IremongerFor instance, the VLAN, the RSS hash result (see :ref:`Poll Mode Driver <Poll_Mode_Driver>`)
145fc1f2750SBernard Iremongerand a flag indicating that the checksum was computed by hardware.
146fc1f2750SBernard Iremonger
147fc1f2750SBernard IremongerAn mbuf also contains the input port (where it comes from), and the number of segment mbufs in the chain.
148fc1f2750SBernard Iremonger
149fc1f2750SBernard IremongerFor chained buffers, only the first mbuf of the chain stores this meta information.
150fc1f2750SBernard Iremonger
151fc1f2750SBernard IremongerDirect and Indirect Buffers
152fc1f2750SBernard Iremonger---------------------------
153fc1f2750SBernard Iremonger
154fc1f2750SBernard IremongerA direct buffer is a buffer that is completely separate and self-contained.
15514b5e699SBruce RichardsonAn indirect buffer behaves like a direct buffer but for the fact that the buffer pointer and
15614b5e699SBruce Richardsondata offset in it refer to data in another direct buffer.
157fc1f2750SBernard IremongerThis is useful in situations where packets need to be duplicated or fragmented,
158fc1f2750SBernard Iremongersince indirect buffers provide the means to reuse the same packet data across multiple buffers.
159fc1f2750SBernard Iremonger
160fc1f2750SBernard IremongerA buffer becomes indirect when it is "attached" to a direct buffer using the rte_pktmbuf_attach() function.
161fc1f2750SBernard IremongerEach buffer has a reference counter field and whenever an indirect buffer is attached to the direct buffer,
162fc1f2750SBernard Iremongerthe reference counter on the direct buffer is incremented.
163fc1f2750SBernard IremongerSimilarly, whenever the indirect buffer is detached, the reference counter on the direct buffer is decremented.
164fc1f2750SBernard IremongerIf the resulting reference counter is equal to 0, the direct buffer is freed since it is no longer in use.
165fc1f2750SBernard Iremonger
166fc1f2750SBernard IremongerThere are a few things to remember when dealing with indirect buffers.
167fc1f2750SBernard IremongerFirst of all, it is not possible to attach an indirect buffer to another indirect buffer.
168fc1f2750SBernard IremongerSecondly, for a buffer to become indirect, its reference counter must be equal to 1,
169fc1f2750SBernard Iremongerthat is, it must not be already referenced by another indirect buffer.
170fc1f2750SBernard IremongerFinally, it is not possible to reattach an indirect buffer to the direct buffer (unless it is detached first).
171fc1f2750SBernard Iremonger
172fc1f2750SBernard IremongerWhile the attach/detach operations can be invoked directly using the recommended rte_pktmbuf_attach() and rte_pktmbuf_detach() functions,
173fc1f2750SBernard Iremongerit is suggested to use the higher-level rte_pktmbuf_clone() function,
174fc1f2750SBernard Iremongerwhich takes care of the correct initialization of an indirect buffer and can clone buffers with multiple segments.
175fc1f2750SBernard Iremonger
176fc1f2750SBernard IremongerSince indirect buffers are not supposed to actually hold any data,
177fc1f2750SBernard Iremongerthe memory pool for indirect buffers should be configured to indicate the reduced memory consumption.
178fc1f2750SBernard IremongerExamples of the initialization of a memory pool for indirect buffers (as well as use case examples for indirect buffers)
179fc1f2750SBernard Iremongercan be found in several of the sample applications, for example, the IPv4 Multicast sample application.
180fc1f2750SBernard Iremonger
181fc1f2750SBernard IremongerDebug
182fc1f2750SBernard Iremonger-----
183fc1f2750SBernard Iremonger
184fc1f2750SBernard IremongerIn debug mode (CONFIG_RTE_MBUF_DEBUG is enabled),
185fc1f2750SBernard Iremongerthe functions of the mbuf library perform sanity checks before any operation (such as, buffer corruption, bad type, and so on).
186fc1f2750SBernard Iremonger
187fc1f2750SBernard IremongerUse Cases
188fc1f2750SBernard Iremonger---------
189fc1f2750SBernard Iremonger
190fc1f2750SBernard IremongerAll networking application should use mbufs to transport network packets.
191fc1f2750SBernard Iremonger
192*ba9e05cbSJohn McNamara.. |mbuf1| image:: img/mbuf1.*
193fc1f2750SBernard Iremonger
194*ba9e05cbSJohn McNamara.. |mbuf2| image:: img/mbuf2.*
195