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.
43 #include <sys/queue.h>
45 #include <rte_debug.h>
46 #include <rte_common.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_launch.h>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_atomic.h>
56 #include <rte_branch_prediction.h>
58 #include <rte_mempool.h>
60 #include <rte_string_fns.h>
61 #include <rte_hexdump.h>
64 * ctrlmbuf constructor, given as a callback function to
65 * rte_mempool_create()
68 rte_ctrlmbuf_init(struct rte_mempool *mp,
69 __attribute__((unused)) void *opaque_arg,
71 __attribute__((unused)) unsigned i)
73 struct rte_mbuf *m = _m;
75 memset(m, 0, mp->elt_size);
77 /* start of buffer is just after mbuf structure */
78 m->buf_addr = (char *)m + sizeof(struct rte_mbuf);
79 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
80 sizeof(struct rte_mbuf);
81 m->buf_len = (uint16_t) (mp->elt_size - sizeof(struct rte_mbuf));
83 /* init some constant fields */
84 m->type = RTE_MBUF_CTRL;
85 m->ctrl.data = (char *)m->buf_addr;
86 m->pool = (struct rte_mempool *)mp;
90 * pktmbuf pool constructor, given as a callback function to
91 * rte_mempool_create()
94 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
96 struct rte_pktmbuf_pool_private *mbp_priv;
99 mbp_priv = rte_mempool_get_priv(mp);
100 roomsz = (uint16_t)(uintptr_t)opaque_arg;
102 /* Use default data room size. */
104 roomsz = 2048 + RTE_PKTMBUF_HEADROOM;
106 mbp_priv->mbuf_data_room_size = roomsz;
110 * pktmbuf constructor, given as a callback function to
111 * rte_mempool_create().
112 * Set the fields of a packet mbuf to their default values.
115 rte_pktmbuf_init(struct rte_mempool *mp,
116 __attribute__((unused)) void *opaque_arg,
118 __attribute__((unused)) unsigned i)
120 struct rte_mbuf *m = _m;
121 uint32_t buf_len = mp->elt_size - sizeof(struct rte_mbuf);
123 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
125 memset(m, 0, mp->elt_size);
127 /* start of buffer is just after mbuf structure */
128 m->buf_addr = (char *)m + sizeof(struct rte_mbuf);
129 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
130 sizeof(struct rte_mbuf);
131 m->buf_len = (uint16_t)buf_len;
133 /* keep some headroom between start of buffer and data */
134 m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
136 /* init some constant fields */
137 m->type = RTE_MBUF_PKT;
140 m->pkt.in_port = 0xff;
143 /* do some sanity checks on a mbuf: panic if it fails */
145 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
148 const struct rte_mbuf *m_seg;
152 rte_panic("mbuf is NULL\n");
153 if (m->type != (uint8_t)t)
154 rte_panic("bad mbuf type\n");
158 rte_panic("bad mbuf pool\n");
159 if (m->buf_physaddr == 0)
160 rte_panic("bad phys addr\n");
161 if (m->buf_addr == NULL)
162 rte_panic("bad virt addr\n");
164 #ifdef RTE_MBUF_SCATTER_GATHER
165 uint16_t cnt = rte_mbuf_refcnt_read(m);
166 if ((cnt == 0) || (cnt == UINT16_MAX))
167 rte_panic("bad ref cnt\n");
170 /* nothing to check for ctrl messages */
171 if (m->type == RTE_MBUF_CTRL)
174 /* check pkt consistency */
175 else if (m->type == RTE_MBUF_PKT) {
177 /* nothing to check for sub-segments */
181 nb_segs = m->pkt.nb_segs;
183 while (m_seg && nb_segs != 0) {
184 m_seg = m_seg->pkt.next;
188 rte_panic("bad nb_segs\n");
192 rte_panic("unknown mbuf type\n");
195 /* dump a mbuf on console */
197 rte_pktmbuf_dump(const struct rte_mbuf *m, unsigned dump_len)
202 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
204 printf("dump mbuf at 0x%p, phys=%"PRIx64", buf_len=%u\n",
205 m, (uint64_t)m->buf_physaddr, (unsigned)m->buf_len);
206 printf(" pkt_len=%"PRIu32", ol_flags=%"PRIx16", nb_segs=%u, "
207 "in_port=%u\n", m->pkt.pkt_len, m->ol_flags,
208 (unsigned)m->pkt.nb_segs, (unsigned)m->pkt.in_port);
209 nb_segs = m->pkt.nb_segs;
211 while (m && nb_segs != 0) {
212 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
214 printf(" segment at 0x%p, data=0x%p, data_len=%u\n",
215 m, m->pkt.data, (unsigned)m->pkt.data_len);
217 if (len > m->pkt.data_len)
218 len = m->pkt.data_len;
220 rte_hexdump(NULL, m->pkt.data, len);