blob: 1dd107a72a041fcfddce8ce1a5f9b8be16343c04 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- This is broken by doc revisioning.
-->
<link rel="shortcut icon" href="../../../../img/favicon.ico">
<title>toc - Apache Mynewt</title>
<link href="../../../../css/bootstrap-3.0.3.min.css" rel="stylesheet">
<link rel="stylesheet" href="../../../../css/highlight.css">
<link href="../../../../css/base.css" rel="stylesheet">
<link href="../../../../css/custom.css" rel="stylesheet">
<link href="../../../../css/v2.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<script>
(function(i, s, o, g, r, a, m) {
i["GoogleAnalyticsObject"] = r;
(i[r] =
i[r] ||
function() {
(i[r].q = i[r].q || []).push(arguments);
}),
(i[r].l = 1 * new Date());
(a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
})(window, document, "script", "//www.google-analytics.com/analytics.js", "ga");
ga("create", "UA-72162311-1", "auto");
ga("send", "pageview");
</script>
</head>
<body class="toc">
<div class="container">
<div class="row v2-main-banner">
<a class="logo-cell" href="/">
<img class="logo" src="/img/logo.png">
</a>
<div class="tagline-cell">
<h4 class="tagline">An OS to build, deploy and securely manage billions of devices</h4>
</div>
<div class="news-cell">
<div class="well">
<h4>Latest News:</h4> <a href="/download">Apache Mynewt 1.12.0, Apache NimBLE 1.7.0 </a> released (April 4, 2024)
</div>
</div>
</div>
</div>
<nav id="navbar" class="navbar navbar-inverse affix-top" data-spy="affix" data-offset-top="150" role="navigation">
<div class="container">
<!-- Collapsed navigation -->
<div class="navbar-header">
<!-- Expander button -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Expanded navigation -->
<div class="navbar-collapse collapse">
<!-- Main navigation -->
<ul class="nav navbar-nav navbar-right">
<li
class=""
>
<a href="/"><i class="fa fa-home" style="font-size: larger;"></i></a>
</li>
<li
class="important"
>
<a href="/quick-start/">Quick Start</a>
</li>
<li
class=""
>
<a href="/about/">About</a>
</li>
<li
class=""
>
<a href="/talks/">Talks</a>
</li>
<li
class="active"
>
<a href="/documentation/">Documentation</a>
</li>
<li
class=""
>
<a href="/download/">Download</a>
</li>
<li
class=""
>
<a href="/community/">Community</a>
</li>
<li
class=""
>
<a href="/events/">Events</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-3 v2-sidebar sidebar-container"><div id="docSidebar" class="hidden-print" role="complementary">
<div class="top">
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search documentation" />
</div>
</form>
</div>
</div>
<ul class="toc-nav">
<li class="doc-version"><select class="form-control" onchange="if (this.value) window.location.href=this.value">
<option value="/latest">
Version: master
</option>
<option value="/v1_12_0/" >
Version: 1.12.0
</option>
<option value="/v1_11_0/" >
Version: 1.11.0
</option>
<option value="/v1_10_0/" >
Version: 1.10.0
</option>
<option value="/v1_9_0/" >
Version: 1.9.0
</option>
<option value="/v1_8_0/" >
Version: 1.8.0
</option>
<option value="/v1_7_0/" >
Version: 1.7.0
</option>
<option value="/v1_6_0/" >
Version: 1.6.0
</option>
<option value="/v1_5_0/" >
Version: 1.5.0
</option>
<option value="/v1_4_0/" >
Version: 1.4.0
</option>
<option value="/v1_3_0/os/introduction" >
Version: 1.3.0
</option>
<option value="/v1_2_0/os/introduction" >
Version: 1.2.0
</option>
<option value="/v1_1_0/os/introduction" >
Version: 1.1.0
</option>
<option value="/v1_0_0/os/introduction" selected="selected" >
Version: 1.0.0
</option>
<option value="/v0_9_0/os/introduction" >
Version: 0.9.0
</option>
</select></li>
<li ><a href="../../../introduction/">Mynewt Documentation</a>
<ul>
<li ><a href="../../../get_started/get_started/">Basic Setup</a>
</li>
<li >
<a href="../../../get_started/vocabulary/">Concepts</a>
</li>
<li ><a href="../../../tutorials/tutorials/">Tutorials</a>
</li>
<li ><a href="../../../os_user_guide/">OS User Guide</a>
<ul>
<li ><a href="../../mynewt_os/">OS Core</a>
<ul>
<li><a href="
../../os_started/
">Functions</a>
</li>
<li ><a href="../../context_switch/context_switch/">Scheduler</a>
</li>
<li ><a href="../../cputime/os_cputime/">CPU Time</a>
</li>
<li ><a href="../../time/os_time/">OS Time</a>
</li>
<li ><a href="../../task/task/">Tasks</a>
</li>
<li ><a href="../../event_queue/event_queue/">Event Queues</a>
</li>
<li ><a href="../../semaphore/semaphore/">Semaphores</a>
</li>
<li ><a href="../../mutex/mutex/">Mutexes</a>
</li>
<li ><a href="../../memory_pool/memory_pool/">Memory Pools</a>
</li>
<li ><a href="../../heap/heap/">Heap</a>
</li>
<li><a href="
./
">Memory Buffers</a>
<ul>
<li class="active"><a href="./">Mbuf</a>
<ul>
<li><a href="
../OS_MBUF_PKTHDR/
">Functions/Macros</a>
</li>
</ul>
</li>
<li ><a href="../../msys/msys/">Msys</a>
</li>
<li ><a href="../../mqueue/mqueue/">MQueue</a>
</li>
</ul>
</li>
<li ><a href="../../sanity/sanity/">Sanity</a>
</li>
<li ><a href="../../callout/callout/">Callouts</a>
</li>
</ul>
</li>
<li ><a href="../../porting/port_os/">Porting to your Platform</a>
</li>
<li ><a href="../../../modules/console/console/">Console</a>
</li>
<li ><a href="../../../modules/shell/shell/">Shell</a>
</li>
<li ><a href="../../../modules/split/split/">Split Images</a>
</li>
<li ><a href="../../../modules/bootloader/bootloader/">Bootloader</a>
</li>
<li><a href="
../../../modules/fs/fs/fs/
">File System</a>
</li>
<li ><a href="../../../modules/hal/hal/">Hardware Abstraction Layer</a>
</li>
<li ><a href="../../../modules/drivers/driver/">Drivers</a>
</li>
<li ><a href="../../../modules/testutil/testutil/">Test Utilities</a>
</li>
<li ><a href="../../../modules/devmgmt/newtmgr/">Device Management with Newt Manager</a>
</li>
<li ><a href="../../../modules/imgmgr/imgmgr/">Image Manager</a>
</li>
<li >
<a href="../../../modules/baselibc/">Baselibc library</a>
</li>
<li ><a href="../../../modules/json/json/">JSON</a>
</li>
<li ><a href="../../../modules/fcb/fcb/">Flash Circular Buffer</a>
</li>
<li ><a href="../../../modules/stats/stats/">Stats</a>
</li>
<li ><a href="../../../modules/logs/logs/">Logs</a>
</li>
<li ><a href="../../../modules/sysinitconfig/sysinitconfig/">System Configuration And Initialization</a>
</li>
</ul>
</li>
<li><a href="
../../../../network/ble/ble_intro/
">BLE User Guide</a>
</li>
<li ><a href="../../../../newt/newt_intro/">Newt Tool Guide</a>
</li>
<li ><a href="../../../../newtmgr/overview/">Newt Manager Guide</a>
</li>
<li >
<a href="../../../../known_issues/">Known Issues</a>
</li>
</ul>
</li>
<li><a href="
../../../../faq/go_env/
">Appendix</a>
</li>
</ul>
</div></div>
<div class="col-md-9" role="main">
<div class="doc-header">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="/documentation/">Docs</a></li>
<li>&raquo; Mbuf</li>
<li>&raquo; Memory Buffers</li>
<li>&raquo; <a href="os/core_os/mynewt_os/">OS Core</a></li>
<li>&raquo; <a href="os/os_user_guide/">OS User Guide</a></li>
<li>&raquo; <a href="os/introduction/">Mynewt Documentation</a></li>
</ul>
</div>
</div>
<div class="alert alert-warning">
<p>
Version 1.0.0 is not the most recent version of the Apache Mynewt
documentation. Click <a href="/latest">here</a> to read the latest
version.
</p>
</div>
<h1 id="mbufs">Mbufs</h1>
<p>The mbuf (short for memory buffer) is a common concept in networking stacks. The mbuf is used to hold packet data as it traverses the stack. The mbuf also generally stores header information or other networking stack information that is carried around with the packet. The mbuf and its associated library of functions were developed to make common networking stack operations (like stripping and adding protocol headers) efficient and as copy-free as possible.</p>
<p>In its simplest form, an mbuf is a memory block with some space reserved for internal information and a pointer which is used to "chain" memory blocks together in order to create a "packet". This is a very important aspect of the mbuf: the ability to chain mbufs together to create larger "packets" (chains of mbufs).</p>
<h3 id="why-use-mbufs">Why use mbufs?</h3>
<p>The main reason is to conserve memory. Consider a networking protocol that generally sends small packets but occasionally sends large ones. The Bluetooth Low Energy (BLE) protocol is one such example. A flat buffer would need to be sized so that the maximum packet size could be contained by the buffer. With the mbuf, a number of mbufs can be chained together so that the occasional large packet can be handled while leaving more packet buffers available to the networking stack for smaller packets.</p>
<h3 id="packet-header-mbuf">Packet Header mbuf</h3>
<p>Not all mbufs are created equal. The first mbuf in a chain of mbufs is a special mbuf called a "packet header mbuf". The reason that this mbuf is special is that it contains the length of all the data contained by the chain of mbufs (the packet length, in other words). The packet header mbuf may also contain a user defined structure (called a "user header") so that networking protocol specific information can be conveyed to various layers of the networking stack. Any mbufs that are part of the packet (i.e. in the mbuf chain but not the first one) are "normal" (i.e. non-packet header) mbufs. A normal mbuf does not have any packet header or user packet header structures in them; they only contain the basic mbuf header (<code>struct os_mbuf</code>). Figure 1 illustrates these two types of mbufs. Note that the numbers/text in parentheses denote the size of the structures/elements (in bytes) and that <code>MBLEN</code> is the memory block length of the memory pool used by the mbuf pool.</p>
<p><img alt="Packet header mbuf" src="../pics/mbuf_fig1.png" /></p>
<h3 id="normal-mbuf">Normal mbuf</h3>
<p>Now let's take a deeper dive into the mbuf structure. Figure 2 illustrates a normal mbuf and breaks out the various fields in the <code>os_mbuf</code> structure. </p>
<ul>
<li>The <code>om_data</code> field is a pointer to where the data starts inside the data buffer. Typically, mbufs that are allocated from the mbuf pool (discussed later) have their <code>om_data</code> pointer set to the start of the data buffer but there are cases where this may not be desirable (added a protocol header to a packet, for example). </li>
<li>The <code>om_flags</code> field is a set of flags used internally by the mbuf library. Currently, no flags have been defined. </li>
<li>The <code>om_pkthdr_len</code> field is the total length of all packet headers in the mbuf. For normal mbufs this is set to 0 as there is no packet or user packet headers. For packet header mbufs, this would be set to the length of the packet header structure (16) plus the size of the user packet header (if any). Note that it is this field which differentiates packet header mbufs from normal mbufs (i.e. if <code>om_pkthdr_len</code> is zero, this is a normal mbuf; otherwise it is a packet header mbuf). </li>
<li>The <code>om_len</code> field contains the amount of user data in the data buffer. When initially allocated, this field is 0 as there is no user data in the mbuf. </li>
<li>The <code>omp_pool</code> field is a pointer to the pool from which this mbuf has been allocated. This is used internally by the mbuf library. </li>
<li>The <code>omp_next</code> field is a linked list element which is used to chain mbufs.</li>
</ul>
<p>Figure 2 also shows a normal mbuf with actual values in the <code>os_mbuf</code> structure. This mbuf starts at address 0x1000 and is 256 bytes in total length. In this example, the user has copied 33 bytes into the data buffer starting at address 0x1010 (this is where <code>om_data</code> points). Note that the packet header length in this mbuf is 0 as it is not a packet header mbuf.</p>
<p><img alt="OS mbuf structure" src="../pics/mbuf_fig2.png" /></p>
<p>Figure 3 illustrates the packet header mbuf along with some chained mbufs (i.e a "packet"). In this example, the user header structure is defined to be 8 bytes. Note that in figure 3 we show a number of different mbufs with varying <code>om_data</code> pointers and lengths since we want to show various examples of valid mbufs. For all the mbufs (both packet header and normal ones) the total length of the memory block is 128 bytes.</p>
<p><img alt="Packet" src="../pics/mbuf_fig3.png" /></p>
<h3 id="mbuf-pools">Mbuf pools</h3>
<p>Mbufs are collected into "mbuf pools" much like memory blocks. The mbuf pool itself contains a pointer to a memory pool. The memory blocks in this memory pool are the actual mbufs; both normal and packet header mbufs. Thus, the memory block (and corresponding memory pool) must be sized correctly. In other words, the memory blocks which make up the memory pool used by the mbuf pool must be at least: <code>sizeof(struct os_mbuf)</code> + <code>sizeof(struct os_mbuf_pkthdr)</code> + <code>sizeof(struct user_defined_header)</code> + <em>desired minimum data buffer length</em>. For example, if the developer wants mbufs to contain at least 64 bytes of user data and they have a user header of 12 bytes, the size of the memory block would be (at least): 64 + 12 + 16 + 8, or 100 bytes. Yes, this is a fair amount of overhead. However, the flexibility provided by the mbuf library usually outweighs overhead concerns.</p>
<h3 id="create-mbuf-pool">Create mbuf pool</h3>
<p>Creating an mbuf pool is fairly simple: create a memory pool and then create the mbuf pool using that memory pool. Once the developer has determined the size of the user data needed per mbuf (this is based on the application/networking stack and is outside the scope of this discussion) and the size of the user header (if any), the memory blocks can be sized. In the example shown below, the application requires 64 bytes of user data per mbuf and also allocates a user header (called <code>struct user_hdr</code>). Note that we do not show the user header data structure as there really is no need; all we need to do is to account for it when creating the memory pool. In the example, we use the macro <code>MBUF_PKTHDR_OVERHEAD</code> to denote the amount of packet header overhead per mbuf and <code>MBUF_MEMBLOCK_OVERHEAD</code> to denote the total amount of overhead required per memory block. The macro <code>MBUF_BUF_SIZE</code> is used to denote the amount of payload that the application requires (aligned on a 32-bit boundary in this case). All this leads to the total memory block size required, denoted by the macro <code>MBUF_MEMBLOCK_OVERHEAD</code>.</p>
<div class="codehilite" style="background: #ffffff"><pre style="line-height: 125%;"><span></span><code><span style="color: #633820">#define MBUF_PKTHDR_OVERHEAD sizeof(struct os_mbuf_pkthdr) + sizeof(struct user_hdr)</span>
<span style="color: #633820">#define MBUF_MEMBLOCK_OVERHEAD sizeof(struct os_mbuf) + MBUF_PKTHDR_OVERHEAD</span>
<span style="color: #633820">#define MBUF_NUM_MBUFS (32)</span>
<span style="color: #633820">#define MBUF_PAYLOAD_SIZE (64)</span>
<span style="color: #633820">#define MBUF_BUF_SIZE OS_ALIGN(MBUF_PAYLOAD_SIZE, 4)</span>
<span style="color: #633820">#define MBUF_MEMBLOCK_SIZE (MBUF_BUF_SIZE + MBUF_MEMBLOCK_OVERHEAD)</span>
<span style="color: #633820">#define MBUF_MEMPOOL_SIZE OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)</span>
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mbuf_pool</span> <span style="color: #000000">g_mbuf_pool</span>;
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mempool</span> <span style="color: #000000">g_mbuf_mempool</span>;
<span style="color: #000000">os_membuf_t</span> <span style="color: #000000">g_mbuf_buffer</span>[<span style="color: #000000">MBUF_MEMPOOL_SIZE</span>];
<span style="color: #A90D91">void</span>
<span style="color: #000000">create_mbuf_pool</span>(<span style="color: #A90D91">void</span>)
{
<span style="color: #A90D91">int</span> <span style="color: #000000">rc</span>;
<span style="color: #000000">rc</span> <span style="color: #000000">=</span> <span style="color: #000000">os_mempool_init</span>(<span style="color: #000000">&amp;g_mbuf_mempool</span>, <span style="color: #000000">MBUF_NUM_MBUFS</span>,
<span style="color: #000000">MBUF_MEMBLOCK_SIZE</span>, <span style="color: #000000">&amp;g_mbuf_buffer</span>[<span style="color: #1C01CE">0</span>], <span style="color: #C41A16">&quot;mbuf_pool&quot;</span>);
<span style="color: #000000">assert</span>(<span style="color: #000000">rc</span> <span style="color: #000000">==</span> <span style="color: #1C01CE">0</span>);
<span style="color: #000000">rc</span> <span style="color: #000000">=</span> <span style="color: #000000">os_mbuf_pool_init</span>(<span style="color: #000000">&amp;g_mbuf_pool</span>, <span style="color: #000000">&amp;g_mbuf_mempool</span>, <span style="color: #000000">MBUF_MEMBLOCK_SIZE</span>,
<span style="color: #000000">MBUF_NUM_MBUFS</span>);
<span style="color: #000000">assert</span>(<span style="color: #000000">rc</span> <span style="color: #000000">==</span> <span style="color: #1C01CE">0</span>);
}
</code></pre></div>
<h3 id="using-mbufs">Using mbufs</h3>
<p>The following examples illustrate typical mbuf usage. There are two basic mbuf allocation API: <code>os_mbuf_get()</code> and <code>os_mbuf_get_pkthdr()</code>. The first API obtains a normal mbuf whereas the latter obtains a packet header mbuf. Typically, application developers use <code>os_mbuf_get_pkthdr()</code> and rarely, if ever, need to call <code>os_mbuf_get()</code> as the rest of the mbuf API (e.g. <code>os_mbuf_append()</code>, <code>os_mbuf_copyinto()</code>, etc.) typically deal with allocating and chaining mbufs. It is recommended to use the provided API to copy data into/out of mbuf chains and/or manipulate mbufs.</p>
<p>In <code>example1</code>, the developer creates a packet and then sends the packet to a networking interface. The code sample also provides an example of copying data out of an mbuf as well as use of the "pullup" api (another very common mbuf api).</p>
<div class="codehilite" style="background: #ffffff"><pre style="line-height: 125%;"><span></span><code><span style="color: #A90D91">void</span>
<span style="color: #000000">mbuf_usage_example1</span>(<span style="color: #A90D91">uint8_t</span> <span style="color: #000000">*mydata</span>, <span style="color: #A90D91">int</span> <span style="color: #000000">mydata_length</span>)
{
<span style="color: #A90D91">int</span> <span style="color: #000000">rc</span>;
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mbuf</span> <span style="color: #000000">*om</span>;
<span style="color: #177500">/* get a packet header mbuf */</span>
<span style="color: #000000">om</span> <span style="color: #000000">=</span> <span style="color: #000000">os_mbuf_get_pkthdr</span>(<span style="color: #000000">&amp;g_mbuf_pool</span>, <span style="color: #A90D91">sizeof</span>(<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">user_hdr</span>));
<span style="color: #A90D91">if</span> (<span style="color: #000000">om</span>) {
<span style="color: #177500">/* </span>
<span style="color: #177500"> * Copy user data into mbuf. NOTE: if mydata_length is greater than the</span>
<span style="color: #177500"> * mbuf payload size (64 bytes using above example), mbufs are allocated</span>
<span style="color: #177500"> * and chained together to accommodate the total packet length.</span>
<span style="color: #177500"> */</span>
<span style="color: #000000">rc</span> <span style="color: #000000">=</span> <span style="color: #000000">os_mbuf_copyinto</span>(<span style="color: #000000">om</span>, <span style="color: #1C01CE">0</span>, <span style="color: #000000">mydata</span>, <span style="color: #000000">len</span>);
<span style="color: #A90D91">if</span> (<span style="color: #000000">rc</span>) {
<span style="color: #177500">/* Error! Could not allocate enough mbufs for total packet length */</span>
<span style="color: #A90D91">return</span> <span style="color: #1C01CE">-1</span>;
}
<span style="color: #177500">/* Send packet to networking interface */</span>
<span style="color: #000000">send_pkt</span>(<span style="color: #000000">om</span>);
}
}
</code></pre></div>
<p>In <code>example2</code> we show use of the pullup api as this illustrates some of the typical pitfalls developers encounter when using mbufs. The first pitfall is one of alignment/padding. Depending on the processor and/or compiler, the <code>sizeof()</code> a structure may vary. Thus, the size of <code>my_protocol_header</code> may be different inside the packet data of the mbuf than the size of the structure on the stack or as a global variable, for instance. While some networking protcols may align protocol information on convenient processor boundaries many others try to conserve bytes "on the air" (i.e inside the packet data). Typical methods used to deal with this are "packing" the structure (i.e. force compiler to not pad) or creating protocol headers that do not require padding. <code>example2</code> assumes that one of these methods was used when defining the <code>my_protocol_header</code> structure.</p>
<p>Another common pitfall occurs around endianness. A network protocol may be little endian or big endian; it all depends on the protocol specification. Processors also have an endianness; this means that the developer has to be careful that the processor endianness and the protocol endianness are handled correctly. In <code>example2</code>, some common networking functions are used: <code>ntohs()</code> and <code>ntohl()</code>. These are shorthand for "network order to host order, short" and "network order to host order, long". Basically, these functions convert data of a certain size (i.e. 16 bits, 32 bits, etc) to the endianness of the host. Network byte order is big-endian (most significant byte first), so these functions convert big-endian byte order to host order (thus, the implementation of these functions is host dependent). Note that the BLE networking stack "on the air" format is least signigicant byte first (i.e. little-endian), so a <code>bletoh</code> function would have to take little-endian format and convert to host format.</p>
<p>A long story short: the developer must take care when copying structure data to/from mbufs and flat buffers!</p>
<p>A final note: these examples assume the same mbuf struture and definitions used in the first example. </p>
<div class="codehilite" style="background: #ffffff"><pre style="line-height: 125%;"><span></span><code><span style="color: #A90D91">void</span>
<span style="color: #000000">mbuf_usage_example2</span>(<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">mbuf</span> <span style="color: #000000">*rxpkt</span>)
{
<span style="color: #A90D91">int</span> <span style="color: #000000">rc</span>;
<span style="color: #A90D91">uint8_t</span> <span style="color: #000000">packet_data</span>[<span style="color: #1C01CE">16</span>];
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">mbuf</span> <span style="color: #000000">*om</span>;
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">my_protocol_header</span> <span style="color: #000000">*phdr</span>;
<span style="color: #177500">/* Make sure that &quot;my_protocol_header&quot; bytes are contiguous in mbuf */</span>
<span style="color: #000000">om</span> <span style="color: #000000">=</span> <span style="color: #000000">os_mbuf_pullup</span>(<span style="color: #000000">&amp;g_mbuf_pool</span>, <span style="color: #A90D91">sizeof</span>(<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">my_protocol_header</span>));
<span style="color: #A90D91">if</span> (<span style="color: #000000">!om</span>) {
<span style="color: #177500">/* Not able to pull up data into contiguous area */</span>
<span style="color: #A90D91">return</span> <span style="color: #1C01CE">-1</span>;
}
<span style="color: #177500">/* </span>
<span style="color: #177500"> * Get the protocol information from the packet. In this example we presume that we</span>
<span style="color: #177500"> * are interested in protocol types that are equal to MY_PROTOCOL_TYPE, are not zero</span>
<span style="color: #177500"> * length, and have had some time in flight.</span>
<span style="color: #177500"> */</span>
<span style="color: #000000">phdr</span> <span style="color: #000000">=</span> <span style="color: #000000">OS_MBUF_DATA</span>(<span style="color: #000000">om</span>, <span style="color: #A90D91">struct</span> <span style="color: #3F6E75">my_protocol_header</span> <span style="color: #000000">*</span>);
<span style="color: #000000">type</span> <span style="color: #000000">=</span> <span style="color: #000000">ntohs</span>(<span style="color: #000000">phdr-&gt;prot_type</span>);
<span style="color: #000000">length</span> <span style="color: #000000">=</span> <span style="color: #000000">ntohs</span>(<span style="color: #000000">phdr-&gt;prot_length</span>);
<span style="color: #000000">time_in_flight</span> <span style="color: #000000">=</span> <span style="color: #000000">ntohl</span>(<span style="color: #000000">phdr-&gt;prot_tif</span>);
<span style="color: #A90D91">if</span> ((<span style="color: #000000">type</span> <span style="color: #000000">==</span> <span style="color: #000000">MY_PROTOCOL_TYPE</span>) <span style="color: #000000">&amp;&amp;</span> (<span style="color: #000000">length</span> <span style="color: #000000">&gt;</span> <span style="color: #1C01CE">0</span>) <span style="color: #000000">&amp;&amp;</span> (<span style="color: #000000">time_in_flight</span> <span style="color: #000000">&gt;</span> <span style="color: #1C01CE">0</span>)) {
<span style="color: #000000">rc</span> <span style="color: #000000">=</span> <span style="color: #000000">os_mbuf_copydata</span>(<span style="color: #000000">rxpkt</span>, <span style="color: #A90D91">sizeof</span>(<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">my_protocol_header</span>), <span style="color: #1C01CE">16</span>, <span style="color: #000000">packet_data</span>);
<span style="color: #A90D91">if</span> (<span style="color: #000000">!rc</span>) {
<span style="color: #177500">/* Success! Perform operations on packet data */</span>
<span style="color: #000000">&lt;</span>... <span style="color: #000000">user</span> <span style="color: #000000">code</span> <span style="color: #000000">here</span> ...<span style="color: #000000">&gt;</span>
}
}
<span style="color: #177500">/* Free passed in packet (mbuf chain) since we don&#39;t need it anymore */</span>
<span style="color: #000000">os_mbuf_free_chain</span>(<span style="color: #000000">om</span>);
}
</code></pre></div>
<p><br></p>
<h3 id="data-structures">Data Structures</h3>
<div class="codehilite" style="background: #ffffff"><pre style="line-height: 125%;"><span></span><code><span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mbuf_pool</span> {
<span style="color: #A90D91">uint16_t</span> <span style="color: #000000">omp_databuf_len</span>;
<span style="color: #A90D91">uint16_t</span> <span style="color: #000000">omp_mbuf_count</span>;
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mempool</span> <span style="color: #000000">*omp_pool</span>;
<span style="color: #000000">STAILQ_ENTRY</span>(<span style="color: #000000">os_mbuf_pool</span>) <span style="color: #000000">omp_next</span>;
};
</code></pre></div>
<table>
<thead>
<tr>
<th><strong>Element</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>omp_databuf_len</td>
<td>The length, in bytes, of the "data buffer" of the mbuf. The data buffer of the mbuf is everything except the os_mbuf structure (which is present in all types of mbufs)</td>
</tr>
<tr>
<td>omp_mbuf_count</td>
<td>Total number of mbufs in the pool when allocated. This is NOT the number of free mbufs in the pool!</td>
</tr>
<tr>
<td>omp_pool</td>
<td>The memory pool from which the mbufs are allocated</td>
</tr>
<tr>
<td>omp_next</td>
<td>This is a linked list pointer which chains memory pools. It is used by the system memory pool library</td>
</tr>
</tbody>
</table>
<p><br></p>
<div class="codehilite" style="background: #ffffff"><pre style="line-height: 125%;"><span></span><code><span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mbuf_pkthdr</span> {
<span style="color: #A90D91">uint16_t</span> <span style="color: #000000">omp_len</span>;
<span style="color: #A90D91">uint16_t</span> <span style="color: #000000">omp_flags</span>;
<span style="color: #000000">STAILQ_ENTRY</span>(<span style="color: #000000">os_mbuf_pkthdr</span>) <span style="color: #000000">omp_next</span>;
};
</code></pre></div>
<table>
<thead>
<tr>
<th><strong>Element</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>omp_len</td>
<td>Length, in bytes, of the "packet". This is the sum of the user data in all the mbufs chained to the packet header mbuf (including the packet header mbuf)</td>
</tr>
<tr>
<td>omp_flags</td>
<td>Packet header flags.</td>
</tr>
<tr>
<td>omp_next</td>
<td>Linked list pointer to chain "packets". This can be used to add mbuf chains to a queue or linked list and is there for convenience.</td>
</tr>
</tbody>
</table>
<p><br></p>
<div class="codehilite" style="background: #ffffff"><pre style="line-height: 125%;"><span></span><code><span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mbuf</span> {
<span style="color: #A90D91">uint8_t</span> <span style="color: #000000">*om_data</span>;
<span style="color: #A90D91">uint8_t</span> <span style="color: #000000">om_flags</span>;
<span style="color: #A90D91">uint8_t</span> <span style="color: #000000">om_pkthdr_len</span>;
<span style="color: #A90D91">uint16_t</span> <span style="color: #000000">om_len</span>;
<span style="color: #A90D91">struct</span> <span style="color: #3F6E75">os_mbuf_pool</span> <span style="color: #000000">*om_omp</span>;
<span style="color: #000000">SLIST_ENTRY</span>(<span style="color: #000000">os_mbuf</span>) <span style="color: #000000">om_next</span>;
<span style="color: #A90D91">uint8_t</span> <span style="color: #000000">om_databuf</span>[<span style="color: #1C01CE">0</span>];
};
</code></pre></div>
<table>
<thead>
<tr>
<th><strong>Element</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>om_data</td>
<td>Pointer to start of user data in mbuf data buffer</td>
</tr>
<tr>
<td>om_flags</td>
<td>mbuf flags field. Currently all flags unused.</td>
</tr>
<tr>
<td>om_pkthdr_len</td>
<td>The total length of all packet headers in the mbuf (mbuf packet header plus user packet header), in bytes</td>
</tr>
<tr>
<td>om_len</td>
<td>The length of the user data contained in this mbuf, in bytes</td>
</tr>
<tr>
<td>om_omp</td>
<td>Memory pool pointer. This is the mbuf pool from which this mbuf was allocated.</td>
</tr>
<tr>
<td>om_next</td>
<td>Pointer to next mbuf in packet chain</td>
</tr>
<tr>
<td>om_databuf</td>
<td>mbuf data buffer (accessor to start of mbuf data buffer). Note that the mbuf data buffer refers to the start of either the user data in normal mbufs or the start of the os mbuf packet header for packet header mbufs</td>
</tr>
</tbody>
</table>
<h3 id="list-of-functionsmacros">List of Functions/Macros</h3>
<p>The functions/macros available in mbuf are:</p>
<table>
<thead>
<tr>
<th><strong>Function/Macro</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="../OS_MBUF_PKTHDR/">OS_MBUF_PKTHDR</a></td>
<td>Get a pointer to the os mbuf packet header of an mbuf.</td>
</tr>
<tr>
<td><a href="../OS_MBUF_PKTHDR_TO_MBUF/">OS_MBUF_PKTHDR_TO_MBUF</a></td>
<td>Get a pointer to the mbuf given a pointer to the os mbuf packet header.</td>
</tr>
<tr>
<td><a href="../OS_MBUF_PKTLEN/">OS_MBUF_PKTLEN</a></td>
<td>Get the length of an entire mbuf chain.</td>
</tr>
<tr>
<td><a href="../OS_MBUF_DATA/">OS_MBUF_DATA</a></td>
<td>Cast the data pointer of an mbuf to a given type.</td>
</tr>
<tr>
<td><a href="../OS_MBUF_USRHDR/">OS_MBUF_USRHDR</a></td>
<td>Get a pointer to the user packet header of an mbuf.</td>
</tr>
<tr>
<td><a href="../OS_MBUF_USRHDR_LEN/">OS_MBUF_USRHDR_LEN</a></td>
<td>Retrieve the length of the user packet header in an mbuf.</td>
</tr>
<tr>
<td><a href="../OS_MBUF_LEADINGSPACE/">OS_MBUF_LEADINGSPACE</a></td>
<td>Get the amount of leading space in an mbuf (in bytes).</td>
</tr>
<tr>
<td><a href="../OS_MBUF_TRAILINGSPACE/">OS_MBUF_TRAILINGSPACE</a></td>
<td>Get the amount of trailing space in an mbuf (in bytes).</td>
</tr>
<tr>
<td><a href="../os_mbuf_adj/">os_mbuf_adj</a></td>
<td>Trims the given number of bytes from either the head (if positive) or tail (if negative) of an mbuf chain.</td>
</tr>
<tr>
<td><a href="../os_mbuf_append/">os_mbuf_append</a></td>
<td>Appends a data buffer of the given length to the end of an mbuf chain.</td>
</tr>
<tr>
<td><a href="../os_mbuf_concat/">os_mbuf_concat</a></td>
<td>Attaches a second mbuf chain onto the end of the first.</td>
</tr>
<tr>
<td><a href="../os_mbuf_copydata/">os_mbuf_copydata</a></td>
<td>Copy data from an mbuf chain.</td>
</tr>
<tr>
<td><a href="../os_mbuf_copyinto/">os_mbuf_copyinto</a></td>
<td>Copies the contents of a flat buffer into an mbuf chain.</td>
</tr>
<tr>
<td><a href="../os_mbuf_dup/">os_mbuf_dup</a></td>
<td>Duplicate a chain of mbufs.</td>
</tr>
<tr>
<td><a href="../os_mbuf_extend/">os_mbuf_extend</a></td>
<td>Increases the length of an mbuf chain by the specified amount.</td>
</tr>
<tr>
<td><a href="../os_mbuf_free_chain/">os_mbuf_free_chain</a></td>
<td>Frees a chain of mbufs.</td>
</tr>
<tr>
<td><a href="../os_mbuf_get/">os_mbuf_get</a></td>
<td>Get an mbuf from the mbuf pool.</td>
</tr>
<tr>
<td><a href="../os_mbuf_get_pkthdr/">os_mbuf_get_pkthdr</a></td>
<td>Allocates a packet header mbuf from the given mbuf pool. Adds a user header to the packet header mbuf.</td>
</tr>
<tr>
<td><a href="../os_mbuf_memcmp/">os_mbuf_memcmp</a></td>
<td>Performs a memory compare of the specified region of an mbuf chain against a flat buffer.</td>
</tr>
<tr>
<td><a href="../os_mbuf_off/">os_mbuf_off</a></td>
<td>Given an offset in the packet, return the mbuf and the offset in that mbuf where byte 'off' is located.</td>
</tr>
<tr>
<td><a href="../os_mbuf_pool_init/">os_mbuf_pool_init</a></td>
<td>nitialize an mbuf pool.</td>
</tr>
<tr>
<td><a href="../os_mbuf_prepend/">os_mbuf_prepend</a></td>
<td>Increases the length of an mbuf chain by adding data to the front.</td>
</tr>
<tr>
<td><a href="../os_mbuf_pullup/">os_mbuf_pullup</a></td>
<td>Rearrange an mbuf chain so that the given length of bytes are contiguous and in the data area of an mbuf.</td>
</tr>
</tbody>
</table>
<div class="row">
<ul class="nav nav-pills" style="margin-bottom: 10px">
<li>
</li>
<li class="pull-right">
</li>
</ul>
</div>
<footer class="row">
<div class="col-xs-12">
<p class="copyright">Apache Mynewt (incubating) is available under Apache License, version 2.0.</p>
</div>
<div class="col-xs-12">
<div class="logos">
<a href="https://www.apache.org/">
<img src="/img/asf_logo_wide_small.png" alt="Apache" title="Apache">
</a>
<p>
Copyright © 2015-2021 The Apache Software Foundation.<br>
<small class="footnote">
Apache Mynewt, Mynewt, Apache, the Apache feather logo, and the Apache Mynewt
project logo are either registered trademarks or trademarks of the Apache
Software Foundation in the United States and other countries.
</small>
</p>
<a href="">
<img src="https://www.countit.com/images/add_to_slack.png" alt="Slack Icon" title="Join our Slack Community" />
</a>
</div>
</div>
<a href="https://www.apache.org/licenses/">
<button class="button-footer-asf">
License
</button>
</a>
<a href="https://www.apache.org/foundation/sponsorship.html">
<button class="button-footer-asf">
Sponsorship
</button>
</a>
<a href="https://www.apache.org/foundation/thanks.html">
<button class="button-footer-asf">
Thanks
</button>
</a>
<a href="https://www.apache.org/security/">
<button class="button-footer-asf">
Security
</button>
</a>
<a href="https://apache.org/events/current-event">
<button class="button-footer-asf">
ASF Events
</button>
</a>
</footer>
</div>
</div>
</div>
<script src="../../../../js/jquery-1.10.2.min.js"></script>
<script src="../../../../js/bootstrap-3.0.3.min.js"></script>
<script src="../../../../js/highlight.pack.js"></script>
<script src="../../../../js/base.js"></script>
<script src="../../../../js/custom.js"></script>
<script src="search/main.js"></script>
</body>
</html>