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_launch.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_atomic.h>
54 #include <rte_branch_prediction.h>
55 #include <rte_mempool.h>
57 #include <rte_string_fns.h>
58 #include <rte_hexdump.h>
59 #include <rte_errno.h>
60 #include <rte_memcpy.h>
63 * ctrlmbuf constructor, given as a callback function to
64 * rte_mempool_obj_iter() or rte_mempool_create()
67 rte_ctrlmbuf_init(struct rte_mempool *mp,
68 __attribute__((unused)) void *opaque_arg,
70 __attribute__((unused)) unsigned i)
72 struct rte_mbuf *m = _m;
73 rte_pktmbuf_init(mp, opaque_arg, _m, i);
74 m->ol_flags |= CTRL_MBUF_FLAG;
78 * pktmbuf pool constructor, given as a callback function to
79 * rte_mempool_create(), or called directly if using
80 * rte_mempool_create_empty()/rte_mempool_populate()
83 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
85 struct rte_pktmbuf_pool_private *user_mbp_priv, *mbp_priv;
86 struct rte_pktmbuf_pool_private default_mbp_priv;
89 RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
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);
99 default_mbp_priv.mbuf_data_room_size = roomsz;
100 user_mbp_priv = &default_mbp_priv;
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);
107 mbp_priv = rte_mempool_get_priv(mp);
108 memcpy(mbp_priv, user_mbp_priv, sizeof(*mbp_priv));
112 * pktmbuf constructor, given as a callback function to
113 * rte_mempool_obj_iter() or rte_mempool_create().
114 * Set the fields of a packet mbuf to their default values.
117 rte_pktmbuf_init(struct rte_mempool *mp,
118 __attribute__((unused)) void *opaque_arg,
120 __attribute__((unused)) unsigned i)
122 struct rte_mbuf *m = _m;
123 uint32_t mbuf_size, buf_len, priv_size;
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);
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);
133 memset(m, 0, mbuf_size);
134 /* start of buffer is after mbuf structure and priv data */
135 m->priv_size = priv_size;
136 m->buf_addr = (char *)m + mbuf_size;
137 m->buf_physaddr = rte_mempool_virt2iova(m) + mbuf_size;
138 m->buf_len = (uint16_t)buf_len;
140 /* keep some headroom between start of buffer and data */
141 m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
143 /* init some constant fields */
146 m->port = MBUF_INVALID_PORT;
147 rte_mbuf_refcnt_set(m, 1);
151 /* helper to create a mbuf pool */
153 rte_pktmbuf_pool_create(const char *name, unsigned n,
154 unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
157 struct rte_mempool *mp;
158 struct rte_pktmbuf_pool_private mbp_priv;
159 const char *mp_ops_name;
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 mp_ops_name = rte_eal_mbuf_default_mempool_ops();
180 ret = rte_mempool_set_ops_byname(mp, mp_ops_name, 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: return "PKT_RX_VLAN";
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 case PKT_RX_SEC_OFFLOAD: return "PKT_RX_SEC_OFFLOAD";
327 case PKT_RX_SEC_OFFLOAD_FAILED: return "PKT_RX_SEC_OFFLOAD_FAILED";
328 default: return NULL;
335 const char *default_name;
338 /* write the list of rx ol flags in buffer buf */
340 rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
342 const struct flag_mask rx_flags[] = {
343 { PKT_RX_VLAN, PKT_RX_VLAN, NULL },
344 { PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, NULL },
345 { PKT_RX_FDIR, PKT_RX_FDIR, NULL },
346 { PKT_RX_L4_CKSUM_BAD, PKT_RX_L4_CKSUM_MASK, NULL },
347 { PKT_RX_L4_CKSUM_GOOD, PKT_RX_L4_CKSUM_MASK, NULL },
348 { PKT_RX_L4_CKSUM_NONE, PKT_RX_L4_CKSUM_MASK, NULL },
349 { PKT_RX_L4_CKSUM_UNKNOWN, PKT_RX_L4_CKSUM_MASK,
350 "PKT_RX_L4_CKSUM_UNKNOWN" },
351 { PKT_RX_IP_CKSUM_BAD, PKT_RX_IP_CKSUM_MASK, NULL },
352 { PKT_RX_IP_CKSUM_GOOD, PKT_RX_IP_CKSUM_MASK, NULL },
353 { PKT_RX_IP_CKSUM_NONE, PKT_RX_IP_CKSUM_MASK, NULL },
354 { PKT_RX_IP_CKSUM_UNKNOWN, PKT_RX_IP_CKSUM_MASK,
355 "PKT_RX_IP_CKSUM_UNKNOWN" },
356 { PKT_RX_EIP_CKSUM_BAD, PKT_RX_EIP_CKSUM_BAD, NULL },
357 { PKT_RX_VLAN_STRIPPED, PKT_RX_VLAN_STRIPPED, NULL },
358 { PKT_RX_IEEE1588_PTP, PKT_RX_IEEE1588_PTP, NULL },
359 { PKT_RX_IEEE1588_TMST, PKT_RX_IEEE1588_TMST, NULL },
360 { PKT_RX_QINQ_STRIPPED, PKT_RX_QINQ_STRIPPED, NULL },
361 { PKT_RX_LRO, PKT_RX_LRO, NULL },
362 { PKT_RX_TIMESTAMP, PKT_RX_TIMESTAMP, NULL },
363 { PKT_RX_SEC_OFFLOAD, PKT_RX_SEC_OFFLOAD, NULL },
364 { PKT_RX_SEC_OFFLOAD_FAILED, PKT_RX_SEC_OFFLOAD_FAILED, NULL },
365 { PKT_RX_QINQ, PKT_RX_QINQ, NULL },
375 for (i = 0; i < RTE_DIM(rx_flags); i++) {
376 if ((mask & rx_flags[i].mask) != rx_flags[i].flag)
378 name = rte_get_rx_ol_flag_name(rx_flags[i].flag);
380 name = rx_flags[i].default_name;
381 ret = snprintf(buf, buflen, "%s ", name);
384 if ((size_t)ret >= buflen)
394 * Get the name of a TX offload flag. Must be kept synchronized with flag
395 * definitions in rte_mbuf.h.
397 const char *rte_get_tx_ol_flag_name(uint64_t mask)
400 case PKT_TX_VLAN_PKT: return "PKT_TX_VLAN_PKT";
401 case PKT_TX_IP_CKSUM: return "PKT_TX_IP_CKSUM";
402 case PKT_TX_TCP_CKSUM: return "PKT_TX_TCP_CKSUM";
403 case PKT_TX_SCTP_CKSUM: return "PKT_TX_SCTP_CKSUM";
404 case PKT_TX_UDP_CKSUM: return "PKT_TX_UDP_CKSUM";
405 case PKT_TX_IEEE1588_TMST: return "PKT_TX_IEEE1588_TMST";
406 case PKT_TX_TCP_SEG: return "PKT_TX_TCP_SEG";
407 case PKT_TX_IPV4: return "PKT_TX_IPV4";
408 case PKT_TX_IPV6: return "PKT_TX_IPV6";
409 case PKT_TX_OUTER_IP_CKSUM: return "PKT_TX_OUTER_IP_CKSUM";
410 case PKT_TX_OUTER_IPV4: return "PKT_TX_OUTER_IPV4";
411 case PKT_TX_OUTER_IPV6: return "PKT_TX_OUTER_IPV6";
412 case PKT_TX_TUNNEL_VXLAN: return "PKT_TX_TUNNEL_VXLAN";
413 case PKT_TX_TUNNEL_GRE: return "PKT_TX_TUNNEL_GRE";
414 case PKT_TX_TUNNEL_IPIP: return "PKT_TX_TUNNEL_IPIP";
415 case PKT_TX_TUNNEL_GENEVE: return "PKT_TX_TUNNEL_GENEVE";
416 case PKT_TX_TUNNEL_MPLSINUDP: return "PKT_TX_TUNNEL_MPLSINUDP";
417 case PKT_TX_MACSEC: return "PKT_TX_MACSEC";
418 case PKT_TX_SEC_OFFLOAD: return "PKT_TX_SEC_OFFLOAD";
419 default: return NULL;
423 /* write the list of tx ol flags in buffer buf */
425 rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
427 const struct flag_mask tx_flags[] = {
428 { PKT_TX_VLAN_PKT, PKT_TX_VLAN_PKT, NULL },
429 { PKT_TX_IP_CKSUM, PKT_TX_IP_CKSUM, NULL },
430 { PKT_TX_TCP_CKSUM, PKT_TX_L4_MASK, NULL },
431 { PKT_TX_SCTP_CKSUM, PKT_TX_L4_MASK, NULL },
432 { PKT_TX_UDP_CKSUM, PKT_TX_L4_MASK, NULL },
433 { PKT_TX_L4_NO_CKSUM, PKT_TX_L4_MASK, "PKT_TX_L4_NO_CKSUM" },
434 { PKT_TX_IEEE1588_TMST, PKT_TX_IEEE1588_TMST, NULL },
435 { PKT_TX_TCP_SEG, PKT_TX_TCP_SEG, NULL },
436 { PKT_TX_IPV4, PKT_TX_IPV4, NULL },
437 { PKT_TX_IPV6, PKT_TX_IPV6, NULL },
438 { PKT_TX_OUTER_IP_CKSUM, PKT_TX_OUTER_IP_CKSUM, NULL },
439 { PKT_TX_OUTER_IPV4, PKT_TX_OUTER_IPV4, NULL },
440 { PKT_TX_OUTER_IPV6, PKT_TX_OUTER_IPV6, NULL },
441 { PKT_TX_TUNNEL_VXLAN, PKT_TX_TUNNEL_MASK,
442 "PKT_TX_TUNNEL_NONE" },
443 { PKT_TX_TUNNEL_GRE, PKT_TX_TUNNEL_MASK,
444 "PKT_TX_TUNNEL_NONE" },
445 { PKT_TX_TUNNEL_IPIP, PKT_TX_TUNNEL_MASK,
446 "PKT_TX_TUNNEL_NONE" },
447 { PKT_TX_TUNNEL_GENEVE, PKT_TX_TUNNEL_MASK,
448 "PKT_TX_TUNNEL_NONE" },
449 { PKT_TX_TUNNEL_MPLSINUDP, PKT_TX_TUNNEL_MASK,
450 "PKT_TX_TUNNEL_NONE" },
451 { PKT_TX_MACSEC, PKT_TX_MACSEC, NULL },
452 { PKT_TX_SEC_OFFLOAD, PKT_TX_SEC_OFFLOAD, NULL },
462 for (i = 0; i < RTE_DIM(tx_flags); i++) {
463 if ((mask & tx_flags[i].mask) != tx_flags[i].flag)
465 name = rte_get_tx_ol_flag_name(tx_flags[i].flag);
467 name = tx_flags[i].default_name;
468 ret = snprintf(buf, buflen, "%s ", name);
471 if ((size_t)ret >= buflen)