first public release
[dpdk.git] / lib / librte_mbuf / rte_mbuf.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <errno.h>
43 #include <ctype.h>
44 #include <sys/queue.h>
45
46 #include <rte_debug.h>
47 #include <rte_common.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
51 #include <rte_launch.h>
52 #include <rte_tailq.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_ring.h>
59 #include <rte_mempool.h>
60 #include <rte_mbuf.h>
61 #include <rte_string_fns.h>
62
63 /*
64  * ctrlmbuf constructor, given as a callback function to
65  * rte_mempool_create()
66  */
67 void
68 rte_ctrlmbuf_init(struct rte_mempool *mp,
69                   __attribute__((unused)) void *opaque_arg,
70                   void *_m,
71                   __attribute__((unused)) unsigned i)
72 {
73         struct rte_mbuf *m = _m;
74
75         memset(m, 0, mp->elt_size);
76
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));
82
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;
87 }
88
89 /*
90  * pktmbuf pool constructor, given as a callback function to
91  * rte_mempool_create()
92  */
93 void
94 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
95 {
96         struct rte_pktmbuf_pool_private *mbp_priv;
97         uint16_t roomsz;
98
99         mbp_priv = rte_mempool_get_priv(mp);
100         roomsz = (uint16_t)(uintptr_t)opaque_arg;
101
102         /* Use default data room size. */
103         if (0 == roomsz)
104                 roomsz = 2048 + RTE_PKTMBUF_HEADROOM;
105
106         mbp_priv->mbuf_data_room_size = roomsz;
107 }
108
109 /*
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.
113  */
114 void
115 rte_pktmbuf_init(struct rte_mempool *mp,
116                  __attribute__((unused)) void *opaque_arg,
117                  void *_m,
118                  __attribute__((unused)) unsigned i)
119 {
120         struct rte_mbuf *m = _m;
121         uint32_t buf_len = mp->elt_size - sizeof(struct rte_mbuf);
122
123         RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
124
125         memset(m, 0, mp->elt_size);
126
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;
132
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);
135
136         /* init some constant fields */
137         m->type = RTE_MBUF_PKT;
138         m->pool = mp;
139         m->pkt.nb_segs = 1;
140         m->pkt.in_port = 0xff;
141 }
142
143 static void
144 rte_pktmbuf_hexdump(const void *buf, unsigned int len)
145 {
146         unsigned int i, out, ofs;
147         const unsigned char *data = buf;
148 #define LINE_LEN 80
149         char line[LINE_LEN];
150
151         printf("  dump data at 0x%p, len=%u\n", data, len);
152         ofs = 0;
153         while (ofs < len) {
154                 out = rte_snprintf(line, LINE_LEN, "  %08X", ofs);
155                 for (i = 0; ofs+i < len && i < 16; i++)
156                         out += rte_snprintf(line+out, LINE_LEN - out, " %02X",
157                                         data[ofs+i]&0xff);
158                 for (; i <= 16; i++)
159                         out += rte_snprintf(line+out, LINE_LEN - out, "   ");
160                 for (i = 0; ofs < len && i < 16; i++, ofs++) {
161                         unsigned char c = data[ofs];
162                         if (!isascii(c) || !isprint(c))
163                                 c = '.';
164                         out += rte_snprintf(line+out, LINE_LEN - out, "%c", c);
165                 }
166                 printf("%s\n", line);
167         }
168 }
169
170 /* do some sanity checks on a mbuf: panic if it fails */
171 void
172 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
173                       int is_header)
174 {
175         const struct rte_mbuf *m_seg;
176         unsigned nb_segs;
177
178         if (m == NULL)
179                 rte_panic("mbuf is NULL\n");
180         if (m->type != (uint8_t)t)
181                 rte_panic("bad mbuf type\n");
182
183         /* generic checks */
184         if (m->pool == NULL)
185                 rte_panic("bad mbuf pool\n");
186         if (m->buf_physaddr == 0)
187                 rte_panic("bad phys addr\n");
188         if (m->buf_addr == NULL)
189                 rte_panic("bad virt addr\n");
190
191 #ifdef RTE_MBUF_SCATTER_GATHER
192         uint16_t cnt = rte_mbuf_refcnt_read(m);
193         if ((cnt == 0) || (cnt == UINT16_MAX))
194                 rte_panic("bad ref cnt\n");
195 #endif
196
197         /* nothing to check for ctrl messages */
198         if (m->type == RTE_MBUF_CTRL)
199                 return;
200
201         /* check pkt consistency */
202         else if (m->type == RTE_MBUF_PKT) {
203
204                 /* nothing to check for sub-segments */
205                 if (is_header == 0)
206                         return;
207
208                 nb_segs = m->pkt.nb_segs;
209                 m_seg = m;
210                 while (m_seg && nb_segs != 0) {
211                         m_seg = m_seg->pkt.next;
212                         nb_segs --;
213                 }
214                 if (nb_segs != 0)
215                         rte_panic("bad nb_segs\n");
216                 return;
217         }
218
219         rte_panic("unknown mbuf type\n");
220 }
221
222 /* dump a mbuf on console */
223 void
224 rte_pktmbuf_dump(const struct rte_mbuf *m, unsigned dump_len)
225 {
226         unsigned int len;
227         unsigned nb_segs;
228
229         __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 1);
230
231         printf("dump mbuf at 0x%p, phys=%"PRIx64", buf_len=%u\n",
232                m, (uint64_t)m->buf_physaddr, (unsigned)m->buf_len);
233         printf("  pkt_len=%"PRIx32", ol_flags=%"PRIx16", nb_segs=%u, "
234                "in_port=%u\n", m->pkt.pkt_len, m->ol_flags,
235                (unsigned)m->pkt.nb_segs, (unsigned)m->pkt.in_port);
236         nb_segs = m->pkt.nb_segs;
237
238         while (m && nb_segs != 0) {
239                 __rte_mbuf_sanity_check(m, RTE_MBUF_PKT, 0);
240
241                 printf("  segment at 0x%p, data=0x%p, data_len=%u\n",
242                        m, m->pkt.data, (unsigned)m->pkt.data_len);
243                 len = dump_len;
244                 if (len > m->pkt.data_len)
245                         len = m->pkt.data_len;
246                 if (len != 0)
247                         rte_pktmbuf_hexdump(m->pkt.data, len);
248                 dump_len -= len;
249                 m = m->pkt.next;
250                 nb_segs --;
251         }
252 }