4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
43 #include <sys/queue.h>
45 #include <rte_debug.h>
46 #include <rte_common.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_launch.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_mempool.h>
58 #include <rte_string_fns.h>
59 #include <rte_hexdump.h>
60 #include <rte_errno.h>
61 #include <rte_memcpy.h>
64 * ctrlmbuf constructor, given as a callback function to
65 * rte_mempool_obj_iter() or rte_mempool_create()
68 rte_ctrlmbuf_init(struct rte_mempool *mp,
69 __attribute__((unused)) void *opaque_arg,
71 __attribute__((unused)) unsigned i)
73 struct rte_mbuf *m = _m;
74 rte_pktmbuf_init(mp, opaque_arg, _m, i);
75 m->ol_flags |= CTRL_MBUF_FLAG;
79 * pktmbuf pool constructor, given as a callback function to
80 * rte_mempool_create(), or called directly if using
81 * rte_mempool_create_empty()/rte_mempool_populate()
84 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
86 struct rte_pktmbuf_pool_private *user_mbp_priv, *mbp_priv;
87 struct rte_pktmbuf_pool_private default_mbp_priv;
90 RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
92 /* if no structure is provided, assume no mbuf private area */
93 user_mbp_priv = opaque_arg;
94 if (user_mbp_priv == NULL) {
95 default_mbp_priv.mbuf_priv_size = 0;
96 if (mp->elt_size > sizeof(struct rte_mbuf))
97 roomsz = mp->elt_size - sizeof(struct rte_mbuf);
100 default_mbp_priv.mbuf_data_room_size = roomsz;
101 user_mbp_priv = &default_mbp_priv;
104 RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf) +
105 user_mbp_priv->mbuf_data_room_size +
106 user_mbp_priv->mbuf_priv_size);
108 mbp_priv = rte_mempool_get_priv(mp);
109 memcpy(mbp_priv, user_mbp_priv, sizeof(*mbp_priv));
113 * pktmbuf constructor, given as a callback function to
114 * rte_mempool_obj_iter() or rte_mempool_create().
115 * Set the fields of a packet mbuf to their default values.
118 rte_pktmbuf_init(struct rte_mempool *mp,
119 __attribute__((unused)) void *opaque_arg,
121 __attribute__((unused)) unsigned i)
123 struct rte_mbuf *m = _m;
124 uint32_t mbuf_size, buf_len, priv_size;
126 priv_size = rte_pktmbuf_priv_size(mp);
127 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
128 buf_len = rte_pktmbuf_data_room_size(mp);
130 RTE_ASSERT(RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) == priv_size);
131 RTE_ASSERT(mp->elt_size >= mbuf_size);
132 RTE_ASSERT(buf_len <= UINT16_MAX);
134 memset(m, 0, mbuf_size);
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;
141 /* keep some headroom between start of buffer and data */
142 m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
144 /* init some constant fields */
148 rte_mbuf_refcnt_set(m, 1);
152 /* helper to create a mbuf pool */
154 rte_pktmbuf_pool_create(const char *name, unsigned n,
155 unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
158 struct rte_mempool *mp;
159 struct rte_pktmbuf_pool_private mbp_priv;
163 if (RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) != priv_size) {
164 RTE_LOG(ERR, MBUF, "mbuf priv_size=%u is not aligned\n",
169 elt_size = sizeof(struct rte_mbuf) + (unsigned)priv_size +
170 (unsigned)data_room_size;
171 mbp_priv.mbuf_data_room_size = data_room_size;
172 mbp_priv.mbuf_priv_size = priv_size;
174 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
175 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
179 ret = rte_mempool_set_ops_byname(mp,
180 RTE_MBUF_DEFAULT_MEMPOOL_OPS, NULL);
182 RTE_LOG(ERR, MBUF, "error setting mempool handler\n");
183 rte_mempool_free(mp);
187 rte_pktmbuf_pool_init(mp, &mbp_priv);
189 ret = rte_mempool_populate_default(mp);
191 rte_mempool_free(mp);
196 rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
201 /* do some sanity checks on a mbuf: panic if it fails */
203 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
205 const struct rte_mbuf *m_seg;
209 rte_panic("mbuf is NULL\n");
213 rte_panic("bad mbuf pool\n");
214 if (m->buf_physaddr == 0)
215 rte_panic("bad phys addr\n");
216 if (m->buf_addr == NULL)
217 rte_panic("bad virt addr\n");
219 uint16_t cnt = rte_mbuf_refcnt_read(m);
220 if ((cnt == 0) || (cnt == UINT16_MAX))
221 rte_panic("bad ref cnt\n");
223 /* nothing to check for sub-segments */
227 nb_segs = m->nb_segs;
229 while (m_seg && nb_segs != 0) {
234 rte_panic("bad nb_segs\n");
237 /* dump a mbuf on console */
239 rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
244 __rte_mbuf_sanity_check(m, 1);
246 fprintf(f, "dump mbuf at %p, phys=%"PRIx64", buf_len=%u\n",
247 m, (uint64_t)m->buf_physaddr, (unsigned)m->buf_len);
248 fprintf(f, " pkt_len=%"PRIu32", ol_flags=%"PRIx64", nb_segs=%u, "
249 "in_port=%u\n", m->pkt_len, m->ol_flags,
250 (unsigned)m->nb_segs, (unsigned)m->port);
251 nb_segs = m->nb_segs;
253 while (m && nb_segs != 0) {
254 __rte_mbuf_sanity_check(m, 0);
256 fprintf(f, " segment at %p, data=%p, data_len=%u\n",
257 m, rte_pktmbuf_mtod(m, void *), (unsigned)m->data_len);
259 if (len > m->data_len)
262 rte_hexdump(f, NULL, rte_pktmbuf_mtod(m, void *), len);
269 /* read len data bytes in a mbuf at specified offset (internal) */
270 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
271 uint32_t len, void *buf)
273 const struct rte_mbuf *seg = m;
274 uint32_t buf_off = 0, copy_len;
276 if (off + len > rte_pktmbuf_pkt_len(m))
279 while (off >= rte_pktmbuf_data_len(seg)) {
280 off -= rte_pktmbuf_data_len(seg);
284 if (off + len <= rte_pktmbuf_data_len(seg))
285 return rte_pktmbuf_mtod_offset(seg, char *, off);
287 /* rare case: header is split among several segments */
289 copy_len = rte_pktmbuf_data_len(seg) - off;
292 rte_memcpy((char *)buf + buf_off,
293 rte_pktmbuf_mtod_offset(seg, char *, off), copy_len);
304 * Get the name of a RX offload flag. Must be kept synchronized with flag
305 * definitions in rte_mbuf.h.
307 const char *rte_get_rx_ol_flag_name(uint64_t mask)
310 case PKT_RX_VLAN_PKT: return "PKT_RX_VLAN_PKT";
311 case PKT_RX_RSS_HASH: return "PKT_RX_RSS_HASH";
312 case PKT_RX_FDIR: return "PKT_RX_FDIR";
313 case PKT_RX_L4_CKSUM_BAD: return "PKT_RX_L4_CKSUM_BAD";
314 case PKT_RX_L4_CKSUM_GOOD: return "PKT_RX_L4_CKSUM_GOOD";
315 case PKT_RX_L4_CKSUM_NONE: return "PKT_RX_L4_CKSUM_NONE";
316 case PKT_RX_IP_CKSUM_BAD: return "PKT_RX_IP_CKSUM_BAD";
317 case PKT_RX_IP_CKSUM_GOOD: return "PKT_RX_IP_CKSUM_GOOD";
318 case PKT_RX_IP_CKSUM_NONE: return "PKT_RX_IP_CKSUM_NONE";
319 case PKT_RX_EIP_CKSUM_BAD: return "PKT_RX_EIP_CKSUM_BAD";
320 case PKT_RX_VLAN_STRIPPED: return "PKT_RX_VLAN_STRIPPED";
321 case PKT_RX_IEEE1588_PTP: return "PKT_RX_IEEE1588_PTP";
322 case PKT_RX_IEEE1588_TMST: return "PKT_RX_IEEE1588_TMST";
323 case PKT_RX_QINQ_STRIPPED: return "PKT_RX_QINQ_STRIPPED";
324 case PKT_RX_LRO: return "PKT_RX_LRO";
325 case PKT_RX_TIMESTAMP: return "PKT_RX_TIMESTAMP";
326 default: return NULL;
333 const char *default_name;
336 /* write the list of rx ol flags in buffer buf */
338 rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
340 const struct flag_mask rx_flags[] = {
341 { PKT_RX_VLAN_PKT, PKT_RX_VLAN_PKT, NULL },
342 { PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, NULL },
343 { PKT_RX_FDIR, PKT_RX_FDIR, NULL },
344 { PKT_RX_L4_CKSUM_BAD, PKT_RX_L4_CKSUM_MASK, NULL },
345 { PKT_RX_L4_CKSUM_GOOD, PKT_RX_L4_CKSUM_MASK, NULL },
346 { PKT_RX_L4_CKSUM_NONE, PKT_RX_L4_CKSUM_MASK, NULL },
347 { PKT_RX_L4_CKSUM_UNKNOWN, PKT_RX_L4_CKSUM_MASK,
348 "PKT_RX_L4_CKSUM_UNKNOWN" },
349 { PKT_RX_IP_CKSUM_BAD, PKT_RX_IP_CKSUM_MASK, NULL },
350 { PKT_RX_IP_CKSUM_GOOD, PKT_RX_IP_CKSUM_MASK, NULL },
351 { PKT_RX_IP_CKSUM_NONE, PKT_RX_IP_CKSUM_MASK, NULL },
352 { PKT_RX_IP_CKSUM_UNKNOWN, PKT_RX_IP_CKSUM_MASK,
353 "PKT_RX_IP_CKSUM_UNKNOWN" },
354 { PKT_RX_EIP_CKSUM_BAD, PKT_RX_EIP_CKSUM_BAD, NULL },
355 { PKT_RX_VLAN_STRIPPED, PKT_RX_VLAN_STRIPPED, NULL },
356 { PKT_RX_IEEE1588_PTP, PKT_RX_IEEE1588_PTP, NULL },
357 { PKT_RX_IEEE1588_TMST, PKT_RX_IEEE1588_TMST, NULL },
358 { PKT_RX_QINQ_STRIPPED, PKT_RX_QINQ_STRIPPED, NULL },
359 { PKT_RX_LRO, PKT_RX_LRO, NULL },
360 { PKT_RX_TIMESTAMP, PKT_RX_TIMESTAMP, NULL },
370 for (i = 0; i < RTE_DIM(rx_flags); i++) {
371 if ((mask & rx_flags[i].mask) != rx_flags[i].flag)
373 name = rte_get_rx_ol_flag_name(rx_flags[i].flag);
375 name = rx_flags[i].default_name;
376 ret = snprintf(buf, buflen, "%s ", name);
379 if ((size_t)ret >= buflen)
389 * Get the name of a TX offload flag. Must be kept synchronized with flag
390 * definitions in rte_mbuf.h.
392 const char *rte_get_tx_ol_flag_name(uint64_t mask)
395 case PKT_TX_VLAN_PKT: return "PKT_TX_VLAN_PKT";
396 case PKT_TX_IP_CKSUM: return "PKT_TX_IP_CKSUM";
397 case PKT_TX_TCP_CKSUM: return "PKT_TX_TCP_CKSUM";
398 case PKT_TX_SCTP_CKSUM: return "PKT_TX_SCTP_CKSUM";
399 case PKT_TX_UDP_CKSUM: return "PKT_TX_UDP_CKSUM";
400 case PKT_TX_IEEE1588_TMST: return "PKT_TX_IEEE1588_TMST";
401 case PKT_TX_TCP_SEG: return "PKT_TX_TCP_SEG";
402 case PKT_TX_IPV4: return "PKT_TX_IPV4";
403 case PKT_TX_IPV6: return "PKT_TX_IPV6";
404 case PKT_TX_OUTER_IP_CKSUM: return "PKT_TX_OUTER_IP_CKSUM";
405 case PKT_TX_OUTER_IPV4: return "PKT_TX_OUTER_IPV4";
406 case PKT_TX_OUTER_IPV6: return "PKT_TX_OUTER_IPV6";
407 case PKT_TX_TUNNEL_VXLAN: return "PKT_TX_TUNNEL_VXLAN";
408 case PKT_TX_TUNNEL_GRE: return "PKT_TX_TUNNEL_GRE";
409 case PKT_TX_TUNNEL_IPIP: return "PKT_TX_TUNNEL_IPIP";
410 case PKT_TX_TUNNEL_GENEVE: return "PKT_TX_TUNNEL_GENEVE";
411 case PKT_TX_TUNNEL_MPLSINUDP: return "PKT_TX_TUNNEL_MPLSINUDP";
412 case PKT_TX_MACSEC: return "PKT_TX_MACSEC";
413 default: return NULL;
417 /* write the list of tx ol flags in buffer buf */
419 rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
421 const struct flag_mask tx_flags[] = {
422 { PKT_TX_VLAN_PKT, PKT_TX_VLAN_PKT, NULL },
423 { PKT_TX_IP_CKSUM, PKT_TX_IP_CKSUM, NULL },
424 { PKT_TX_TCP_CKSUM, PKT_TX_L4_MASK, NULL },
425 { PKT_TX_SCTP_CKSUM, PKT_TX_L4_MASK, NULL },
426 { PKT_TX_UDP_CKSUM, PKT_TX_L4_MASK, NULL },
427 { PKT_TX_L4_NO_CKSUM, PKT_TX_L4_MASK, "PKT_TX_L4_NO_CKSUM" },
428 { PKT_TX_IEEE1588_TMST, PKT_TX_IEEE1588_TMST, NULL },
429 { PKT_TX_TCP_SEG, PKT_TX_TCP_SEG, NULL },
430 { PKT_TX_IPV4, PKT_TX_IPV4, NULL },
431 { PKT_TX_IPV6, PKT_TX_IPV6, NULL },
432 { PKT_TX_OUTER_IP_CKSUM, PKT_TX_OUTER_IP_CKSUM, NULL },
433 { PKT_TX_OUTER_IPV4, PKT_TX_OUTER_IPV4, NULL },
434 { PKT_TX_OUTER_IPV6, PKT_TX_OUTER_IPV6, NULL },
435 { PKT_TX_TUNNEL_VXLAN, PKT_TX_TUNNEL_MASK,
436 "PKT_TX_TUNNEL_NONE" },
437 { PKT_TX_TUNNEL_GRE, PKT_TX_TUNNEL_MASK,
438 "PKT_TX_TUNNEL_NONE" },
439 { PKT_TX_TUNNEL_IPIP, PKT_TX_TUNNEL_MASK,
440 "PKT_TX_TUNNEL_NONE" },
441 { PKT_TX_TUNNEL_GENEVE, PKT_TX_TUNNEL_MASK,
442 "PKT_TX_TUNNEL_NONE" },
443 { PKT_TX_TUNNEL_MPLSINUDP, PKT_TX_TUNNEL_MASK,
444 "PKT_TX_TUNNEL_NONE" },
445 { PKT_TX_MACSEC, PKT_TX_MACSEC, NULL },
455 for (i = 0; i < RTE_DIM(tx_flags); i++) {
456 if ((mask & tx_flags[i].mask) != tx_flags[i].flag)
458 name = rte_get_tx_ol_flag_name(tx_flags[i].flag);
460 name = tx_flags[i].default_name;
461 ret = snprintf(buf, buflen, "%s ", name);
464 if ((size_t)ret >= buflen)