1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2014 6WIND S.A.
14 #include <sys/queue.h>
16 #include <rte_compat.h>
17 #include <rte_debug.h>
18 #include <rte_common.h>
20 #include <rte_memory.h>
21 #include <rte_launch.h>
23 #include <rte_per_lcore.h>
24 #include <rte_lcore.h>
25 #include <rte_atomic.h>
26 #include <rte_branch_prediction.h>
27 #include <rte_mempool.h>
29 #include <rte_mbuf_pool_ops.h>
30 #include <rte_string_fns.h>
31 #include <rte_hexdump.h>
32 #include <rte_errno.h>
33 #include <rte_memcpy.h>
36 * pktmbuf pool constructor, given as a callback function to
37 * rte_mempool_create(), or called directly if using
38 * rte_mempool_create_empty()/rte_mempool_populate()
41 rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
43 struct rte_pktmbuf_pool_private *user_mbp_priv, *mbp_priv;
44 struct rte_pktmbuf_pool_private default_mbp_priv;
47 RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
49 /* if no structure is provided, assume no mbuf private area */
50 user_mbp_priv = opaque_arg;
51 if (user_mbp_priv == NULL) {
52 default_mbp_priv.mbuf_priv_size = 0;
53 if (mp->elt_size > sizeof(struct rte_mbuf))
54 roomsz = mp->elt_size - sizeof(struct rte_mbuf);
57 default_mbp_priv.mbuf_data_room_size = roomsz;
58 user_mbp_priv = &default_mbp_priv;
61 RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf) +
62 user_mbp_priv->mbuf_data_room_size +
63 user_mbp_priv->mbuf_priv_size);
65 mbp_priv = rte_mempool_get_priv(mp);
66 memcpy(mbp_priv, user_mbp_priv, sizeof(*mbp_priv));
70 * pktmbuf constructor, given as a callback function to
71 * rte_mempool_obj_iter() or rte_mempool_create().
72 * Set the fields of a packet mbuf to their default values.
75 rte_pktmbuf_init(struct rte_mempool *mp,
76 __attribute__((unused)) void *opaque_arg,
78 __attribute__((unused)) unsigned i)
80 struct rte_mbuf *m = _m;
81 uint32_t mbuf_size, buf_len, priv_size;
83 priv_size = rte_pktmbuf_priv_size(mp);
84 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
85 buf_len = rte_pktmbuf_data_room_size(mp);
87 RTE_ASSERT(RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) == priv_size);
88 RTE_ASSERT(mp->elt_size >= mbuf_size);
89 RTE_ASSERT(buf_len <= UINT16_MAX);
91 memset(m, 0, mbuf_size);
92 /* start of buffer is after mbuf structure and priv data */
93 m->priv_size = priv_size;
94 m->buf_addr = (char *)m + mbuf_size;
95 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
96 m->buf_len = (uint16_t)buf_len;
98 /* keep some headroom between start of buffer and data */
99 m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
101 /* init some constant fields */
104 m->port = MBUF_INVALID_PORT;
105 rte_mbuf_refcnt_set(m, 1);
109 /* Helper to create a mbuf pool with given mempool ops name*/
111 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
112 unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
113 int socket_id, const char *ops_name)
115 struct rte_mempool *mp;
116 struct rte_pktmbuf_pool_private mbp_priv;
117 const char *mp_ops_name = ops_name;
121 if (RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) != priv_size) {
122 RTE_LOG(ERR, MBUF, "mbuf priv_size=%u is not aligned\n",
127 elt_size = sizeof(struct rte_mbuf) + (unsigned)priv_size +
128 (unsigned)data_room_size;
129 mbp_priv.mbuf_data_room_size = data_room_size;
130 mbp_priv.mbuf_priv_size = priv_size;
132 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
133 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
137 if (mp_ops_name == NULL)
138 mp_ops_name = rte_mbuf_best_mempool_ops();
139 ret = rte_mempool_set_ops_byname(mp, mp_ops_name, NULL);
141 RTE_LOG(ERR, MBUF, "error setting mempool handler\n");
142 rte_mempool_free(mp);
146 rte_pktmbuf_pool_init(mp, &mbp_priv);
148 ret = rte_mempool_populate_default(mp);
150 rte_mempool_free(mp);
155 rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
160 /* helper to create a mbuf pool */
162 rte_pktmbuf_pool_create(const char *name, unsigned int n,
163 unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
166 return rte_pktmbuf_pool_create_by_ops(name, n, cache_size, priv_size,
167 data_room_size, socket_id, NULL);
170 /* do some sanity checks on a mbuf: panic if it fails */
172 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
174 unsigned int nb_segs, pkt_len;
177 rte_panic("mbuf is NULL\n");
181 rte_panic("bad mbuf pool\n");
182 if (m->buf_iova == 0)
183 rte_panic("bad IO addr\n");
184 if (m->buf_addr == NULL)
185 rte_panic("bad virt addr\n");
187 uint16_t cnt = rte_mbuf_refcnt_read(m);
188 if ((cnt == 0) || (cnt == UINT16_MAX))
189 rte_panic("bad ref cnt\n");
191 /* nothing to check for sub-segments */
195 /* data_len is supposed to be not more than pkt_len */
196 if (m->data_len > m->pkt_len)
197 rte_panic("bad data_len\n");
199 nb_segs = m->nb_segs;
200 pkt_len = m->pkt_len;
204 pkt_len -= m->data_len;
205 } while ((m = m->next) != NULL);
208 rte_panic("bad nb_segs\n");
210 rte_panic("bad pkt_len\n");
213 /* dump a mbuf on console */
215 rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
218 unsigned int nb_segs;
220 __rte_mbuf_sanity_check(m, 1);
222 fprintf(f, "dump mbuf at %p, iova=%"PRIx64", buf_len=%u\n",
223 m, (uint64_t)m->buf_iova, (unsigned)m->buf_len);
224 fprintf(f, " pkt_len=%"PRIu32", ol_flags=%"PRIx64", nb_segs=%u, "
225 "in_port=%u\n", m->pkt_len, m->ol_flags,
226 (unsigned)m->nb_segs, (unsigned)m->port);
227 nb_segs = m->nb_segs;
229 while (m && nb_segs != 0) {
230 __rte_mbuf_sanity_check(m, 0);
232 fprintf(f, " segment at %p, data=%p, data_len=%u\n",
233 m, rte_pktmbuf_mtod(m, void *), (unsigned)m->data_len);
235 if (len > m->data_len)
238 rte_hexdump(f, NULL, rte_pktmbuf_mtod(m, void *), len);
245 /* read len data bytes in a mbuf at specified offset (internal) */
246 const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
247 uint32_t len, void *buf)
249 const struct rte_mbuf *seg = m;
250 uint32_t buf_off = 0, copy_len;
252 if (off + len > rte_pktmbuf_pkt_len(m))
255 while (off >= rte_pktmbuf_data_len(seg)) {
256 off -= rte_pktmbuf_data_len(seg);
260 if (off + len <= rte_pktmbuf_data_len(seg))
261 return rte_pktmbuf_mtod_offset(seg, char *, off);
263 /* rare case: header is split among several segments */
265 copy_len = rte_pktmbuf_data_len(seg) - off;
268 rte_memcpy((char *)buf + buf_off,
269 rte_pktmbuf_mtod_offset(seg, char *, off), copy_len);
280 * Get the name of a RX offload flag. Must be kept synchronized with flag
281 * definitions in rte_mbuf.h.
283 const char *rte_get_rx_ol_flag_name(uint64_t mask)
286 case PKT_RX_VLAN: return "PKT_RX_VLAN";
287 case PKT_RX_RSS_HASH: return "PKT_RX_RSS_HASH";
288 case PKT_RX_FDIR: return "PKT_RX_FDIR";
289 case PKT_RX_L4_CKSUM_BAD: return "PKT_RX_L4_CKSUM_BAD";
290 case PKT_RX_L4_CKSUM_GOOD: return "PKT_RX_L4_CKSUM_GOOD";
291 case PKT_RX_L4_CKSUM_NONE: return "PKT_RX_L4_CKSUM_NONE";
292 case PKT_RX_IP_CKSUM_BAD: return "PKT_RX_IP_CKSUM_BAD";
293 case PKT_RX_IP_CKSUM_GOOD: return "PKT_RX_IP_CKSUM_GOOD";
294 case PKT_RX_IP_CKSUM_NONE: return "PKT_RX_IP_CKSUM_NONE";
295 case PKT_RX_EIP_CKSUM_BAD: return "PKT_RX_EIP_CKSUM_BAD";
296 case PKT_RX_VLAN_STRIPPED: return "PKT_RX_VLAN_STRIPPED";
297 case PKT_RX_IEEE1588_PTP: return "PKT_RX_IEEE1588_PTP";
298 case PKT_RX_IEEE1588_TMST: return "PKT_RX_IEEE1588_TMST";
299 case PKT_RX_QINQ_STRIPPED: return "PKT_RX_QINQ_STRIPPED";
300 case PKT_RX_LRO: return "PKT_RX_LRO";
301 case PKT_RX_TIMESTAMP: return "PKT_RX_TIMESTAMP";
302 case PKT_RX_SEC_OFFLOAD: return "PKT_RX_SEC_OFFLOAD";
303 case PKT_RX_SEC_OFFLOAD_FAILED: return "PKT_RX_SEC_OFFLOAD_FAILED";
304 default: return NULL;
311 const char *default_name;
314 /* write the list of rx ol flags in buffer buf */
316 rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
318 const struct flag_mask rx_flags[] = {
319 { PKT_RX_VLAN, PKT_RX_VLAN, NULL },
320 { PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, NULL },
321 { PKT_RX_FDIR, PKT_RX_FDIR, NULL },
322 { PKT_RX_L4_CKSUM_BAD, PKT_RX_L4_CKSUM_MASK, NULL },
323 { PKT_RX_L4_CKSUM_GOOD, PKT_RX_L4_CKSUM_MASK, NULL },
324 { PKT_RX_L4_CKSUM_NONE, PKT_RX_L4_CKSUM_MASK, NULL },
325 { PKT_RX_L4_CKSUM_UNKNOWN, PKT_RX_L4_CKSUM_MASK,
326 "PKT_RX_L4_CKSUM_UNKNOWN" },
327 { PKT_RX_IP_CKSUM_BAD, PKT_RX_IP_CKSUM_MASK, NULL },
328 { PKT_RX_IP_CKSUM_GOOD, PKT_RX_IP_CKSUM_MASK, NULL },
329 { PKT_RX_IP_CKSUM_NONE, PKT_RX_IP_CKSUM_MASK, NULL },
330 { PKT_RX_IP_CKSUM_UNKNOWN, PKT_RX_IP_CKSUM_MASK,
331 "PKT_RX_IP_CKSUM_UNKNOWN" },
332 { PKT_RX_EIP_CKSUM_BAD, PKT_RX_EIP_CKSUM_BAD, NULL },
333 { PKT_RX_VLAN_STRIPPED, PKT_RX_VLAN_STRIPPED, NULL },
334 { PKT_RX_IEEE1588_PTP, PKT_RX_IEEE1588_PTP, NULL },
335 { PKT_RX_IEEE1588_TMST, PKT_RX_IEEE1588_TMST, NULL },
336 { PKT_RX_QINQ_STRIPPED, PKT_RX_QINQ_STRIPPED, NULL },
337 { PKT_RX_LRO, PKT_RX_LRO, NULL },
338 { PKT_RX_TIMESTAMP, PKT_RX_TIMESTAMP, NULL },
339 { PKT_RX_SEC_OFFLOAD, PKT_RX_SEC_OFFLOAD, NULL },
340 { PKT_RX_SEC_OFFLOAD_FAILED, PKT_RX_SEC_OFFLOAD_FAILED, NULL },
341 { PKT_RX_QINQ, PKT_RX_QINQ, NULL },
351 for (i = 0; i < RTE_DIM(rx_flags); i++) {
352 if ((mask & rx_flags[i].mask) != rx_flags[i].flag)
354 name = rte_get_rx_ol_flag_name(rx_flags[i].flag);
356 name = rx_flags[i].default_name;
357 ret = snprintf(buf, buflen, "%s ", name);
360 if ((size_t)ret >= buflen)
370 * Get the name of a TX offload flag. Must be kept synchronized with flag
371 * definitions in rte_mbuf.h.
373 const char *rte_get_tx_ol_flag_name(uint64_t mask)
376 case PKT_TX_VLAN_PKT: return "PKT_TX_VLAN_PKT";
377 case PKT_TX_IP_CKSUM: return "PKT_TX_IP_CKSUM";
378 case PKT_TX_TCP_CKSUM: return "PKT_TX_TCP_CKSUM";
379 case PKT_TX_SCTP_CKSUM: return "PKT_TX_SCTP_CKSUM";
380 case PKT_TX_UDP_CKSUM: return "PKT_TX_UDP_CKSUM";
381 case PKT_TX_IEEE1588_TMST: return "PKT_TX_IEEE1588_TMST";
382 case PKT_TX_TCP_SEG: return "PKT_TX_TCP_SEG";
383 case PKT_TX_IPV4: return "PKT_TX_IPV4";
384 case PKT_TX_IPV6: return "PKT_TX_IPV6";
385 case PKT_TX_OUTER_IP_CKSUM: return "PKT_TX_OUTER_IP_CKSUM";
386 case PKT_TX_OUTER_IPV4: return "PKT_TX_OUTER_IPV4";
387 case PKT_TX_OUTER_IPV6: return "PKT_TX_OUTER_IPV6";
388 case PKT_TX_TUNNEL_VXLAN: return "PKT_TX_TUNNEL_VXLAN";
389 case PKT_TX_TUNNEL_GRE: return "PKT_TX_TUNNEL_GRE";
390 case PKT_TX_TUNNEL_IPIP: return "PKT_TX_TUNNEL_IPIP";
391 case PKT_TX_TUNNEL_GENEVE: return "PKT_TX_TUNNEL_GENEVE";
392 case PKT_TX_TUNNEL_MPLSINUDP: return "PKT_TX_TUNNEL_MPLSINUDP";
393 case PKT_TX_TUNNEL_VXLAN_GPE: return "PKT_TX_TUNNEL_VXLAN_GPE";
394 case PKT_TX_TUNNEL_IP: return "PKT_TX_TUNNEL_IP";
395 case PKT_TX_TUNNEL_UDP: return "PKT_TX_TUNNEL_UDP";
396 case PKT_TX_MACSEC: return "PKT_TX_MACSEC";
397 case PKT_TX_SEC_OFFLOAD: return "PKT_TX_SEC_OFFLOAD";
398 default: return NULL;
402 /* write the list of tx ol flags in buffer buf */
404 rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
406 const struct flag_mask tx_flags[] = {
407 { PKT_TX_VLAN_PKT, PKT_TX_VLAN_PKT, NULL },
408 { PKT_TX_IP_CKSUM, PKT_TX_IP_CKSUM, NULL },
409 { PKT_TX_TCP_CKSUM, PKT_TX_L4_MASK, NULL },
410 { PKT_TX_SCTP_CKSUM, PKT_TX_L4_MASK, NULL },
411 { PKT_TX_UDP_CKSUM, PKT_TX_L4_MASK, NULL },
412 { PKT_TX_L4_NO_CKSUM, PKT_TX_L4_MASK, "PKT_TX_L4_NO_CKSUM" },
413 { PKT_TX_IEEE1588_TMST, PKT_TX_IEEE1588_TMST, NULL },
414 { PKT_TX_TCP_SEG, PKT_TX_TCP_SEG, NULL },
415 { PKT_TX_IPV4, PKT_TX_IPV4, NULL },
416 { PKT_TX_IPV6, PKT_TX_IPV6, NULL },
417 { PKT_TX_OUTER_IP_CKSUM, PKT_TX_OUTER_IP_CKSUM, NULL },
418 { PKT_TX_OUTER_IPV4, PKT_TX_OUTER_IPV4, NULL },
419 { PKT_TX_OUTER_IPV6, PKT_TX_OUTER_IPV6, NULL },
420 { PKT_TX_TUNNEL_VXLAN, PKT_TX_TUNNEL_MASK,
421 "PKT_TX_TUNNEL_NONE" },
422 { PKT_TX_TUNNEL_GRE, PKT_TX_TUNNEL_MASK,
423 "PKT_TX_TUNNEL_NONE" },
424 { PKT_TX_TUNNEL_IPIP, PKT_TX_TUNNEL_MASK,
425 "PKT_TX_TUNNEL_NONE" },
426 { PKT_TX_TUNNEL_GENEVE, PKT_TX_TUNNEL_MASK,
427 "PKT_TX_TUNNEL_NONE" },
428 { PKT_TX_TUNNEL_MPLSINUDP, PKT_TX_TUNNEL_MASK,
429 "PKT_TX_TUNNEL_NONE" },
430 { PKT_TX_TUNNEL_VXLAN_GPE, PKT_TX_TUNNEL_MASK,
431 "PKT_TX_TUNNEL_NONE" },
432 { PKT_TX_TUNNEL_IP, PKT_TX_TUNNEL_MASK,
433 "PKT_TX_TUNNEL_NONE" },
434 { PKT_TX_TUNNEL_UDP, PKT_TX_TUNNEL_MASK,
435 "PKT_TX_TUNNEL_NONE" },
436 { PKT_TX_MACSEC, PKT_TX_MACSEC, NULL },
437 { PKT_TX_SEC_OFFLOAD, PKT_TX_SEC_OFFLOAD, NULL },
447 for (i = 0; i < RTE_DIM(tx_flags); i++) {
448 if ((mask & tx_flags[i].mask) != tx_flags[i].flag)
450 name = rte_get_tx_ol_flag_name(tx_flags[i].flag);
452 name = tx_flags[i].default_name;
453 ret = snprintf(buf, buflen, "%s ", name);
456 if ((size_t)ret >= buflen)