rte_mbuf.c revision a41e6ff1
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_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#include <rte_errno.h>
62
63/*
64 * ctrlmbuf constructor, given as a callback function to
65 * rte_mempool_create()
66 */
67void
68rte_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 */
82void
83rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
84{
85	struct rte_pktmbuf_pool_private *user_mbp_priv, *mbp_priv;
86	struct rte_pktmbuf_pool_private default_mbp_priv;
87	uint16_t roomsz;
88
89	RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
90
91	/* if no structure is provided, assume no mbuf private area */
92	user_mbp_priv = opaque_arg;
93	if (user_mbp_priv == NULL) {
94		default_mbp_priv.mbuf_priv_size = 0;
95		if (mp->elt_size > sizeof(struct rte_mbuf))
96			roomsz = mp->elt_size - sizeof(struct rte_mbuf);
97		else
98			roomsz = 0;
99		default_mbp_priv.mbuf_data_room_size = roomsz;
100		user_mbp_priv = &default_mbp_priv;
101	}
102
103	RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf) +
104		user_mbp_priv->mbuf_data_room_size +
105		user_mbp_priv->mbuf_priv_size);
106
107	mbp_priv = rte_mempool_get_priv(mp);
108	memcpy(mbp_priv, user_mbp_priv, sizeof(*mbp_priv));
109}
110
111/*
112 * pktmbuf constructor, given as a callback function to
113 * rte_mempool_create().
114 * Set the fields of a packet mbuf to their default values.
115 */
116void
117rte_pktmbuf_init(struct rte_mempool *mp,
118		 __attribute__((unused)) void *opaque_arg,
119		 void *_m,
120		 __attribute__((unused)) unsigned i)
121{
122	struct rte_mbuf *m = _m;
123	uint32_t mbuf_size, buf_len, priv_size;
124
125	priv_size = rte_pktmbuf_priv_size(mp);
126	mbuf_size = sizeof(struct rte_mbuf) + priv_size;
127	buf_len = rte_pktmbuf_data_room_size(mp);
128
129	RTE_ASSERT(RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) == priv_size);
130	RTE_ASSERT(mp->elt_size >= mbuf_size);
131	RTE_ASSERT(buf_len <= UINT16_MAX);
132
133	memset(m, 0, mp->elt_size);
134
135	/* start of buffer is after mbuf structure and priv data */
136	m->priv_size = priv_size;
137	m->buf_addr = (char *)m + mbuf_size;
138	m->buf_physaddr = rte_mempool_virt2phy(mp, m) + mbuf_size;
139	m->buf_len = (uint16_t)buf_len;
140
141	/* keep some headroom between start of buffer and data */
142	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
143
144	/* init some constant fields */
145	m->pool = mp;
146	m->nb_segs = 1;
147	m->port = 0xff;
148}
149
150/* helper to create a mbuf pool */
151struct rte_mempool *
152rte_pktmbuf_pool_create(const char *name, unsigned n,
153	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
154	int socket_id)
155{
156	struct rte_mempool *mp;
157	struct rte_pktmbuf_pool_private mbp_priv;
158	unsigned elt_size;
159	int ret;
160
161	if (RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) != priv_size) {
162		RTE_LOG(ERR, MBUF, "mbuf priv_size=%u is not aligned\n",
163			priv_size);
164		rte_errno = EINVAL;
165		return NULL;
166	}
167	elt_size = sizeof(struct rte_mbuf) + (unsigned)priv_size +
168		(unsigned)data_room_size;
169	mbp_priv.mbuf_data_room_size = data_room_size;
170	mbp_priv.mbuf_priv_size = priv_size;
171
172	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
173		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
174	if (mp == NULL)
175		return NULL;
176
177	rte_errno = rte_mempool_set_ops_byname(mp,
178			RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL);
179	if (rte_errno != 0) {
180		RTE_LOG(ERR, MBUF, "error setting mempool handler\n");
181		return NULL;
182	}
183	rte_pktmbuf_pool_init(mp, &mbp_priv);
184
185	ret = rte_mempool_populate_default(mp);
186	if (ret < 0) {
187		rte_mempool_free(mp);
188		rte_errno = -ret;
189		return NULL;
190	}
191
192	rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
193
194	return mp;
195}
196
197/* do some sanity checks on a mbuf: panic if it fails */
198void
199rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
200{
201	const struct rte_mbuf *m_seg;
202	unsigned nb_segs;
203
204	if (m == NULL)
205		rte_panic("mbuf is NULL\n");
206
207	/* generic checks */
208	if (m->pool == NULL)
209		rte_panic("bad mbuf pool\n");
210	if (m->buf_physaddr == 0)
211		rte_panic("bad phys addr\n");
212	if (m->buf_addr == NULL)
213		rte_panic("bad virt addr\n");
214
215	uint16_t cnt = rte_mbuf_refcnt_read(m);
216	if ((cnt == 0) || (cnt == UINT16_MAX))
217		rte_panic("bad ref cnt\n");
218
219	/* nothing to check for sub-segments */
220	if (is_header == 0)
221		return;
222
223	nb_segs = m->nb_segs;
224	m_seg = m;
225	while (m_seg && nb_segs != 0) {
226		m_seg = m_seg->next;
227		nb_segs--;
228	}
229	if (nb_segs != 0)
230		rte_panic("bad nb_segs\n");
231}
232
233/* dump a mbuf on console */
234void
235rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
236{
237	unsigned int len;
238	unsigned nb_segs;
239
240	__rte_mbuf_sanity_check(m, 1);
241
242	fprintf(f, "dump mbuf at %p, phys=%"PRIx64", buf_len=%u\n",
243	       m, (uint64_t)m->buf_physaddr, (unsigned)m->buf_len);
244	fprintf(f, "  pkt_len=%"PRIu32", ol_flags=%"PRIx64", nb_segs=%u, "
245	       "in_port=%u\n", m->pkt_len, m->ol_flags,
246	       (unsigned)m->nb_segs, (unsigned)m->port);
247	nb_segs = m->nb_segs;
248
249	while (m && nb_segs != 0) {
250		__rte_mbuf_sanity_check(m, 0);
251
252		fprintf(f, "  segment at %p, data=%p, data_len=%u\n",
253			m, rte_pktmbuf_mtod(m, void *), (unsigned)m->data_len);
254		len = dump_len;
255		if (len > m->data_len)
256			len = m->data_len;
257		if (len != 0)
258			rte_hexdump(f, NULL, rte_pktmbuf_mtod(m, void *), len);
259		dump_len -= len;
260		m = m->next;
261		nb_segs --;
262	}
263}
264
265/*
266 * Get the name of a RX offload flag. Must be kept synchronized with flag
267 * definitions in rte_mbuf.h.
268 */
269const char *rte_get_rx_ol_flag_name(uint64_t mask)
270{
271	switch (mask) {
272	case PKT_RX_VLAN_PKT: return "PKT_RX_VLAN_PKT";
273	case PKT_RX_RSS_HASH: return "PKT_RX_RSS_HASH";
274	case PKT_RX_FDIR: return "PKT_RX_FDIR";
275	case PKT_RX_L4_CKSUM_BAD: return "PKT_RX_L4_CKSUM_BAD";
276	case PKT_RX_IP_CKSUM_BAD: return "PKT_RX_IP_CKSUM_BAD";
277	case PKT_RX_EIP_CKSUM_BAD: return "PKT_RX_EIP_CKSUM_BAD";
278	case PKT_RX_VLAN_STRIPPED: return "PKT_RX_VLAN_STRIPPED";
279	case PKT_RX_IEEE1588_PTP: return "PKT_RX_IEEE1588_PTP";
280	case PKT_RX_IEEE1588_TMST: return "PKT_RX_IEEE1588_TMST";
281	case PKT_RX_QINQ_STRIPPED: return "PKT_RX_QINQ_STRIPPED";
282	default: return NULL;
283	}
284}
285
286/*
287 * Get the name of a TX offload flag. Must be kept synchronized with flag
288 * definitions in rte_mbuf.h.
289 */
290const char *rte_get_tx_ol_flag_name(uint64_t mask)
291{
292	switch (mask) {
293	case PKT_TX_VLAN_PKT: return "PKT_TX_VLAN_PKT";
294	case PKT_TX_IP_CKSUM: return "PKT_TX_IP_CKSUM";
295	case PKT_TX_TCP_CKSUM: return "PKT_TX_TCP_CKSUM";
296	case PKT_TX_SCTP_CKSUM: return "PKT_TX_SCTP_CKSUM";
297	case PKT_TX_UDP_CKSUM: return "PKT_TX_UDP_CKSUM";
298	case PKT_TX_IEEE1588_TMST: return "PKT_TX_IEEE1588_TMST";
299	case PKT_TX_TCP_SEG: return "PKT_TX_TCP_SEG";
300	case PKT_TX_IPV4: return "PKT_TX_IPV4";
301	case PKT_TX_IPV6: return "PKT_TX_IPV6";
302	case PKT_TX_OUTER_IP_CKSUM: return "PKT_TX_OUTER_IP_CKSUM";
303	case PKT_TX_OUTER_IPV4: return "PKT_TX_OUTER_IPV4";
304	case PKT_TX_OUTER_IPV6: return "PKT_TX_OUTER_IPV6";
305	default: return NULL;
306	}
307}
308