mbuf: replace data pointer by an offset
[dpdk.git] / lib / librte_mbuf / rte_mbuf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <ctype.h>
42 #include <sys/queue.h>
43
44 #include <rte_debug.h>
45 #include <rte_common.h>
46 #include <rte_log.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_launch.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_mempool.h>
58 #include <rte_mbuf.h>
59 #include <rte_string_fns.h>
60 #include <rte_hexdump.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         rte_pktmbuf_init(mp, opaque_arg, _m, i);
73 }
74
75 /*
76  * pktmbuf pool constructor, given as a callback function to
77  * rte_mempool_create()
78  */
79 void
80 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
81 {
82         struct rte_pktmbuf_pool_private *mbp_priv;
83         uint16_t roomsz;
84
85         mbp_priv = rte_mempool_get_priv(mp);
86         roomsz = (uint16_t)(uintptr_t)opaque_arg;
87
88         /* Use default data room size. */
89         if (0 == roomsz)
90                 roomsz = 2048 + RTE_PKTMBUF_HEADROOM;
91
92         mbp_priv->mbuf_data_room_size = roomsz;
93 }
94
95 /*
96  * pktmbuf constructor, given as a callback function to
97  * rte_mempool_create().
98  * Set the fields of a packet mbuf to their default values.
99  */
100 void
101 rte_pktmbuf_init(struct rte_mempool *mp,
102                  __attribute__((unused)) void *opaque_arg,
103                  void *_m,
104                  __attribute__((unused)) unsigned i)
105 {
106         struct rte_mbuf *m = _m;
107         uint32_t buf_len = mp->elt_size - sizeof(struct rte_mbuf);
108
109         RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
110
111         memset(m, 0, mp->elt_size);
112
113         /* start of buffer is just after mbuf structure */
114         m->buf_addr = (char *)m + sizeof(struct rte_mbuf);
115         m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
116                         sizeof(struct rte_mbuf);
117         m->buf_len = (uint16_t)buf_len;
118
119         /* keep some headroom between start of buffer and data */
120         m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
121
122         /* init some constant fields */
123         m->pool = mp;
124         m->nb_segs = 1;
125         m->port = 0xff;
126 }
127
128 /* do some sanity checks on a mbuf: panic if it fails */
129 void
130 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
131 {
132         const struct rte_mbuf *m_seg;
133         unsigned nb_segs;
134
135         if (m == NULL)
136                 rte_panic("mbuf is NULL\n");
137
138         /* generic checks */
139         if (m->pool == NULL)
140                 rte_panic("bad mbuf pool\n");
141         if (m->buf_physaddr == 0)
142                 rte_panic("bad phys addr\n");
143         if (m->buf_addr == NULL)
144                 rte_panic("bad virt addr\n");
145
146 #ifdef RTE_MBUF_REFCNT
147         uint16_t cnt = rte_mbuf_refcnt_read(m);
148         if ((cnt == 0) || (cnt == UINT16_MAX))
149                 rte_panic("bad ref cnt\n");
150 #endif
151
152         /* nothing to check for sub-segments */
153         if (is_header == 0)
154                 return;
155
156         nb_segs = m->nb_segs;
157         m_seg = m;
158         while (m_seg && nb_segs != 0) {
159                 m_seg = m_seg->next;
160                 nb_segs--;
161         }
162         if (nb_segs != 0)
163                 rte_panic("bad nb_segs\n");
164 }
165
166 /* dump a mbuf on console */
167 void
168 rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
169 {
170         unsigned int len;
171         unsigned nb_segs;
172
173         __rte_mbuf_sanity_check(m, 1);
174
175         fprintf(f, "dump mbuf at 0x%p, phys=%"PRIx64", buf_len=%u\n",
176                m, (uint64_t)m->buf_physaddr, (unsigned)m->buf_len);
177         fprintf(f, "  pkt_len=%"PRIu32", ol_flags=%"PRIx16", nb_segs=%u, "
178                "in_port=%u\n", m->pkt_len, m->ol_flags,
179                (unsigned)m->nb_segs, (unsigned)m->port);
180         nb_segs = m->nb_segs;
181
182         while (m && nb_segs != 0) {
183                 __rte_mbuf_sanity_check(m, 0);
184
185                 fprintf(f, "  segment at 0x%p, data=0x%p, data_len=%u\n",
186                         m, rte_pktmbuf_mtod(m, void *), (unsigned)m->data_len);
187                 len = dump_len;
188                 if (len > m->data_len)
189                         len = m->data_len;
190                 if (len != 0)
191                         rte_hexdump(f, NULL, rte_pktmbuf_mtod(m, void *), len);
192                 dump_len -= len;
193                 m = m->next;
194                 nb_segs --;
195         }
196 }