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