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