mbuf: remove build option to disable refcnt
[dpdk.git] / lib / librte_mbuf / rte_mbuf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 #include <rte_hexdump.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         rte_pktmbuf_init(mp, opaque_arg, _m, i);
75         m->ol_flags |= CTRL_MBUF_FLAG;
76 }
77
78 /*
79  * pktmbuf pool constructor, given as a callback function to
80  * rte_mempool_create()
81  */
82 void
83 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
84 {
85         struct rte_pktmbuf_pool_private *mbp_priv;
86         uint16_t roomsz;
87
88         mbp_priv = rte_mempool_get_priv(mp);
89         roomsz = (uint16_t)(uintptr_t)opaque_arg;
90
91         /* Use default data room size. */
92         if (0 == roomsz)
93                 roomsz = 2048 + RTE_PKTMBUF_HEADROOM;
94
95         mbp_priv->mbuf_data_room_size = roomsz;
96 }
97
98 /*
99  * pktmbuf constructor, given as a callback function to
100  * rte_mempool_create().
101  * Set the fields of a packet mbuf to their default values.
102  */
103 void
104 rte_pktmbuf_init(struct rte_mempool *mp,
105                  __attribute__((unused)) void *opaque_arg,
106                  void *_m,
107                  __attribute__((unused)) unsigned i)
108 {
109         struct rte_mbuf *m = _m;
110         uint32_t buf_len = mp->elt_size - sizeof(struct rte_mbuf);
111
112         RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
113
114         memset(m, 0, mp->elt_size);
115
116         /* start of buffer is just after mbuf structure */
117         m->buf_addr = (char *)m + sizeof(struct rte_mbuf);
118         m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
119                         sizeof(struct rte_mbuf);
120         m->buf_len = (uint16_t)buf_len;
121
122         /* keep some headroom between start of buffer and data */
123         m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
124
125         /* init some constant fields */
126         m->pool = mp;
127         m->nb_segs = 1;
128         m->port = 0xff;
129 }
130
131 /* do some sanity checks on a mbuf: panic if it fails */
132 void
133 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
134 {
135         const struct rte_mbuf *m_seg;
136         unsigned nb_segs;
137
138         if (m == NULL)
139                 rte_panic("mbuf is NULL\n");
140
141         /* generic checks */
142         if (m->pool == NULL)
143                 rte_panic("bad mbuf pool\n");
144         if (m->buf_physaddr == 0)
145                 rte_panic("bad phys addr\n");
146         if (m->buf_addr == NULL)
147                 rte_panic("bad virt addr\n");
148
149         uint16_t cnt = rte_mbuf_refcnt_read(m);
150         if ((cnt == 0) || (cnt == UINT16_MAX))
151                 rte_panic("bad ref cnt\n");
152
153         /* nothing to check for sub-segments */
154         if (is_header == 0)
155                 return;
156
157         nb_segs = m->nb_segs;
158         m_seg = m;
159         while (m_seg && nb_segs != 0) {
160                 m_seg = m_seg->next;
161                 nb_segs--;
162         }
163         if (nb_segs != 0)
164                 rte_panic("bad nb_segs\n");
165 }
166
167 /* dump a mbuf on console */
168 void
169 rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
170 {
171         unsigned int len;
172         unsigned nb_segs;
173
174         __rte_mbuf_sanity_check(m, 1);
175
176         fprintf(f, "dump mbuf at 0x%p, phys=%"PRIx64", buf_len=%u\n",
177                m, (uint64_t)m->buf_physaddr, (unsigned)m->buf_len);
178         fprintf(f, "  pkt_len=%"PRIu32", ol_flags=%"PRIx64", nb_segs=%u, "
179                "in_port=%u\n", m->pkt_len, m->ol_flags,
180                (unsigned)m->nb_segs, (unsigned)m->port);
181         nb_segs = m->nb_segs;
182
183         while (m && nb_segs != 0) {
184                 __rte_mbuf_sanity_check(m, 0);
185
186                 fprintf(f, "  segment at 0x%p, data=0x%p, data_len=%u\n",
187                         m, rte_pktmbuf_mtod(m, void *), (unsigned)m->data_len);
188                 len = dump_len;
189                 if (len > m->data_len)
190                         len = m->data_len;
191                 if (len != 0)
192                         rte_hexdump(f, NULL, rte_pktmbuf_mtod(m, void *), len);
193                 dump_len -= len;
194                 m = m->next;
195                 nb_segs --;
196         }
197 }
198
199 /*
200  * Get the name of a RX offload flag. Must be kept synchronized with flag
201  * definitions in rte_mbuf.h.
202  */
203 const char *rte_get_rx_ol_flag_name(uint64_t mask)
204 {
205         switch (mask) {
206         case PKT_RX_VLAN_PKT: return "PKT_RX_VLAN_PKT";
207         case PKT_RX_RSS_HASH: return "PKT_RX_RSS_HASH";
208         case PKT_RX_FDIR: return "PKT_RX_FDIR";
209         case PKT_RX_L4_CKSUM_BAD: return "PKT_RX_L4_CKSUM_BAD";
210         case PKT_RX_IP_CKSUM_BAD: return "PKT_RX_IP_CKSUM_BAD";
211         /* case PKT_RX_EIP_CKSUM_BAD: return "PKT_RX_EIP_CKSUM_BAD"; */
212         /* case PKT_RX_OVERSIZE: return "PKT_RX_OVERSIZE"; */
213         /* case PKT_RX_HBUF_OVERFLOW: return "PKT_RX_HBUF_OVERFLOW"; */
214         /* case PKT_RX_RECIP_ERR: return "PKT_RX_RECIP_ERR"; */
215         /* case PKT_RX_MAC_ERR: return "PKT_RX_MAC_ERR"; */
216         case PKT_RX_IPV4_HDR: return "PKT_RX_IPV4_HDR";
217         case PKT_RX_IPV4_HDR_EXT: return "PKT_RX_IPV4_HDR_EXT";
218         case PKT_RX_IPV6_HDR: return "PKT_RX_IPV6_HDR";
219         case PKT_RX_IPV6_HDR_EXT: return "PKT_RX_IPV6_HDR_EXT";
220         case PKT_RX_IEEE1588_PTP: return "PKT_RX_IEEE1588_PTP";
221         case PKT_RX_IEEE1588_TMST: return "PKT_RX_IEEE1588_TMST";
222         case PKT_RX_TUNNEL_IPV4_HDR: return "PKT_RX_TUNNEL_IPV4_HDR";
223         case PKT_RX_TUNNEL_IPV6_HDR: return "PKT_RX_TUNNEL_IPV6_HDR";
224         default: return NULL;
225         }
226 }
227
228 /*
229  * Get the name of a TX offload flag. Must be kept synchronized with flag
230  * definitions in rte_mbuf.h.
231  */
232 const char *rte_get_tx_ol_flag_name(uint64_t mask)
233 {
234         switch (mask) {
235         case PKT_TX_VLAN_PKT: return "PKT_TX_VLAN_PKT";
236         case PKT_TX_IP_CKSUM: return "PKT_TX_IP_CKSUM";
237         case PKT_TX_TCP_CKSUM: return "PKT_TX_TCP_CKSUM";
238         case PKT_TX_SCTP_CKSUM: return "PKT_TX_SCTP_CKSUM";
239         case PKT_TX_UDP_CKSUM: return "PKT_TX_UDP_CKSUM";
240         case PKT_TX_IEEE1588_TMST: return "PKT_TX_IEEE1588_TMST";
241         case PKT_TX_TCP_SEG: return "PKT_TX_TCP_SEG";
242         case PKT_TX_IPV4: return "PKT_TX_IPV4";
243         case PKT_TX_IPV6: return "PKT_TX_IPV6";
244         case PKT_TX_OUTER_IP_CKSUM: return "PKT_TX_OUTER_IP_CKSUM";
245         case PKT_TX_OUTER_IPV4: return "PKT_TX_OUTER_IPV4";
246         case PKT_TX_OUTER_IPV6: return "PKT_TX_OUTER_IPV6";
247         default: return NULL;
248         }
249 }