xref: /dpdk/lib/mbuf/rte_mbuf.c (revision ae67895b507bb6af22263c79ba0d5c374b396485)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation.
399a2dd95SBruce Richardson  * Copyright 2014 6WIND S.A.
499a2dd95SBruce Richardson  */
599a2dd95SBruce Richardson 
699a2dd95SBruce Richardson #include <string.h>
799a2dd95SBruce Richardson #include <stdio.h>
899a2dd95SBruce Richardson #include <stdint.h>
999a2dd95SBruce Richardson #include <inttypes.h>
1099a2dd95SBruce Richardson #include <errno.h>
1199a2dd95SBruce Richardson 
1299a2dd95SBruce Richardson #include <rte_debug.h>
1399a2dd95SBruce Richardson #include <rte_common.h>
1499a2dd95SBruce Richardson #include <rte_log.h>
1599a2dd95SBruce Richardson #include <rte_branch_prediction.h>
1699a2dd95SBruce Richardson #include <rte_mempool.h>
1799a2dd95SBruce Richardson #include <rte_mbuf.h>
1899a2dd95SBruce Richardson #include <rte_mbuf_pool_ops.h>
1999a2dd95SBruce Richardson #include <rte_hexdump.h>
2099a2dd95SBruce Richardson #include <rte_errno.h>
2199a2dd95SBruce Richardson #include <rte_memcpy.h>
2299a2dd95SBruce Richardson 
23e906c931SStephen Hemminger #include "mbuf_log.h"
24e906c931SStephen Hemminger 
25e906c931SStephen Hemminger RTE_LOG_REGISTER_DEFAULT(mbuf_logtype, INFO);
26e906c931SStephen Hemminger 
2799a2dd95SBruce Richardson /*
2899a2dd95SBruce Richardson  * pktmbuf pool constructor, given as a callback function to
2999a2dd95SBruce Richardson  * rte_mempool_create(), or called directly if using
3099a2dd95SBruce Richardson  * rte_mempool_create_empty()/rte_mempool_populate()
3199a2dd95SBruce Richardson  */
3299a2dd95SBruce Richardson void
rte_pktmbuf_pool_init(struct rte_mempool * mp,void * opaque_arg)3399a2dd95SBruce Richardson rte_pktmbuf_pool_init(struct rte_mempool *mp, void *opaque_arg)
3499a2dd95SBruce Richardson {
3599a2dd95SBruce Richardson 	struct rte_pktmbuf_pool_private *user_mbp_priv, *mbp_priv;
3699a2dd95SBruce Richardson 	struct rte_pktmbuf_pool_private default_mbp_priv;
3799a2dd95SBruce Richardson 	uint16_t roomsz;
3899a2dd95SBruce Richardson 
39bd611385SOlivier Matz 	RTE_ASSERT(mp->private_data_size >=
40bd611385SOlivier Matz 		   sizeof(struct rte_pktmbuf_pool_private));
4199a2dd95SBruce Richardson 	RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf));
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson 	/* if no structure is provided, assume no mbuf private area */
4499a2dd95SBruce Richardson 	user_mbp_priv = opaque_arg;
4599a2dd95SBruce Richardson 	if (user_mbp_priv == NULL) {
4699a2dd95SBruce Richardson 		memset(&default_mbp_priv, 0, sizeof(default_mbp_priv));
4799a2dd95SBruce Richardson 		if (mp->elt_size > sizeof(struct rte_mbuf))
4899a2dd95SBruce Richardson 			roomsz = mp->elt_size - sizeof(struct rte_mbuf);
4999a2dd95SBruce Richardson 		else
5099a2dd95SBruce Richardson 			roomsz = 0;
5199a2dd95SBruce Richardson 		default_mbp_priv.mbuf_data_room_size = roomsz;
5299a2dd95SBruce Richardson 		user_mbp_priv = &default_mbp_priv;
5399a2dd95SBruce Richardson 	}
5499a2dd95SBruce Richardson 
5599a2dd95SBruce Richardson 	RTE_ASSERT(mp->elt_size >= sizeof(struct rte_mbuf) +
5699a2dd95SBruce Richardson 		((user_mbp_priv->flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) ?
5799a2dd95SBruce Richardson 			sizeof(struct rte_mbuf_ext_shared_info) :
5899a2dd95SBruce Richardson 			user_mbp_priv->mbuf_data_room_size) +
5999a2dd95SBruce Richardson 		user_mbp_priv->mbuf_priv_size);
6099a2dd95SBruce Richardson 	RTE_ASSERT((user_mbp_priv->flags &
6199a2dd95SBruce Richardson 		    ~RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) == 0);
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson 	mbp_priv = rte_mempool_get_priv(mp);
6499a2dd95SBruce Richardson 	memcpy(mbp_priv, user_mbp_priv, sizeof(*mbp_priv));
6599a2dd95SBruce Richardson }
6699a2dd95SBruce Richardson 
6799a2dd95SBruce Richardson /*
6899a2dd95SBruce Richardson  * pktmbuf constructor, given as a callback function to
6999a2dd95SBruce Richardson  * rte_mempool_obj_iter() or rte_mempool_create().
7099a2dd95SBruce Richardson  * Set the fields of a packet mbuf to their default values.
7199a2dd95SBruce Richardson  */
7299a2dd95SBruce Richardson void
rte_pktmbuf_init(struct rte_mempool * mp,__rte_unused void * opaque_arg,void * _m,__rte_unused unsigned i)7399a2dd95SBruce Richardson rte_pktmbuf_init(struct rte_mempool *mp,
7499a2dd95SBruce Richardson 		 __rte_unused void *opaque_arg,
7599a2dd95SBruce Richardson 		 void *_m,
7699a2dd95SBruce Richardson 		 __rte_unused unsigned i)
7799a2dd95SBruce Richardson {
7899a2dd95SBruce Richardson 	struct rte_mbuf *m = _m;
7999a2dd95SBruce Richardson 	uint32_t mbuf_size, buf_len, priv_size;
8099a2dd95SBruce Richardson 
81bd611385SOlivier Matz 	RTE_ASSERT(mp->private_data_size >=
82bd611385SOlivier Matz 		   sizeof(struct rte_pktmbuf_pool_private));
83bd611385SOlivier Matz 
8499a2dd95SBruce Richardson 	priv_size = rte_pktmbuf_priv_size(mp);
8599a2dd95SBruce Richardson 	mbuf_size = sizeof(struct rte_mbuf) + priv_size;
8699a2dd95SBruce Richardson 	buf_len = rte_pktmbuf_data_room_size(mp);
8799a2dd95SBruce Richardson 
8899a2dd95SBruce Richardson 	RTE_ASSERT(RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) == priv_size);
8999a2dd95SBruce Richardson 	RTE_ASSERT(mp->elt_size >= mbuf_size);
9099a2dd95SBruce Richardson 	RTE_ASSERT(buf_len <= UINT16_MAX);
9199a2dd95SBruce Richardson 
9299a2dd95SBruce Richardson 	memset(m, 0, mbuf_size);
9399a2dd95SBruce Richardson 	/* start of buffer is after mbuf structure and priv data */
9499a2dd95SBruce Richardson 	m->priv_size = priv_size;
9599a2dd95SBruce Richardson 	m->buf_addr = (char *)m + mbuf_size;
96e811e2d7SShijith Thotton 	rte_mbuf_iova_set(m, rte_mempool_virt2iova(m) + mbuf_size);
9799a2dd95SBruce Richardson 	m->buf_len = (uint16_t)buf_len;
9899a2dd95SBruce Richardson 
9999a2dd95SBruce Richardson 	/* keep some headroom between start of buffer and data */
10099a2dd95SBruce Richardson 	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	/* init some constant fields */
10399a2dd95SBruce Richardson 	m->pool = mp;
10499a2dd95SBruce Richardson 	m->nb_segs = 1;
10599a2dd95SBruce Richardson 	m->port = RTE_MBUF_PORT_INVALID;
10699a2dd95SBruce Richardson 	rte_mbuf_refcnt_set(m, 1);
10799a2dd95SBruce Richardson 	m->next = NULL;
10899a2dd95SBruce Richardson }
10999a2dd95SBruce Richardson 
11099a2dd95SBruce Richardson /*
11199a2dd95SBruce Richardson  * @internal The callback routine called when reference counter in shinfo
11299a2dd95SBruce Richardson  * for mbufs with pinned external buffer reaches zero. It means there is
11399a2dd95SBruce Richardson  * no more reference to buffer backing mbuf and this one should be freed.
11499a2dd95SBruce Richardson  * This routine is called for the regular (not with pinned external or
11599a2dd95SBruce Richardson  * indirect buffer) mbufs on detaching from the mbuf with pinned external
11699a2dd95SBruce Richardson  * buffer.
11799a2dd95SBruce Richardson  */
11899a2dd95SBruce Richardson static void
rte_pktmbuf_free_pinned_extmem(void * addr,void * opaque)11999a2dd95SBruce Richardson rte_pktmbuf_free_pinned_extmem(void *addr, void *opaque)
12099a2dd95SBruce Richardson {
12199a2dd95SBruce Richardson 	struct rte_mbuf *m = opaque;
12299a2dd95SBruce Richardson 
12399a2dd95SBruce Richardson 	RTE_SET_USED(addr);
12499a2dd95SBruce Richardson 	RTE_ASSERT(RTE_MBUF_HAS_EXTBUF(m));
12599a2dd95SBruce Richardson 	RTE_ASSERT(RTE_MBUF_HAS_PINNED_EXTBUF(m));
12699a2dd95SBruce Richardson 	RTE_ASSERT(m->shinfo->fcb_opaque == m);
12799a2dd95SBruce Richardson 
12899a2dd95SBruce Richardson 	rte_mbuf_ext_refcnt_set(m->shinfo, 1);
129daa02b5cSOlivier Matz 	m->ol_flags = RTE_MBUF_F_EXTERNAL;
130efc6f910SOlivier Matz 	if (m->next != NULL)
13199a2dd95SBruce Richardson 		m->next = NULL;
132efc6f910SOlivier Matz 	if (m->nb_segs != 1)
13399a2dd95SBruce Richardson 		m->nb_segs = 1;
13499a2dd95SBruce Richardson 	rte_mbuf_raw_free(m);
13599a2dd95SBruce Richardson }
13699a2dd95SBruce Richardson 
13799a2dd95SBruce Richardson /** The context to initialize the mbufs with pinned external buffers. */
13899a2dd95SBruce Richardson struct rte_pktmbuf_extmem_init_ctx {
13999a2dd95SBruce Richardson 	const struct rte_pktmbuf_extmem *ext_mem; /* descriptor array. */
14099a2dd95SBruce Richardson 	unsigned int ext_num; /* number of descriptors in array. */
14199a2dd95SBruce Richardson 	unsigned int ext; /* loop descriptor index. */
14299a2dd95SBruce Richardson 	size_t off; /* loop buffer offset. */
14399a2dd95SBruce Richardson };
14499a2dd95SBruce Richardson 
14599a2dd95SBruce Richardson /**
14699a2dd95SBruce Richardson  * @internal Packet mbuf constructor for pools with pinned external memory.
14799a2dd95SBruce Richardson  *
14899a2dd95SBruce Richardson  * This function initializes some fields in the mbuf structure that are
14999a2dd95SBruce Richardson  * not modified by the user once created (origin pool, buffer start
15099a2dd95SBruce Richardson  * address, and so on). This function is given as a callback function to
15199a2dd95SBruce Richardson  * rte_mempool_obj_iter() called from rte_mempool_create_extmem().
15299a2dd95SBruce Richardson  *
15399a2dd95SBruce Richardson  * @param mp
15499a2dd95SBruce Richardson  *   The mempool from which mbufs originate.
15599a2dd95SBruce Richardson  * @param opaque_arg
15699a2dd95SBruce Richardson  *   A pointer to the rte_pktmbuf_extmem_init_ctx - initialization
15799a2dd95SBruce Richardson  *   context structure
15899a2dd95SBruce Richardson  * @param m
15999a2dd95SBruce Richardson  *   The mbuf to initialize.
16099a2dd95SBruce Richardson  * @param i
16199a2dd95SBruce Richardson  *   The index of the mbuf in the pool table.
16299a2dd95SBruce Richardson  */
16399a2dd95SBruce Richardson static void
__rte_pktmbuf_init_extmem(struct rte_mempool * mp,void * opaque_arg,void * _m,__rte_unused unsigned int i)16499a2dd95SBruce Richardson __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
16599a2dd95SBruce Richardson 			  void *opaque_arg,
16699a2dd95SBruce Richardson 			  void *_m,
16799a2dd95SBruce Richardson 			  __rte_unused unsigned int i)
16899a2dd95SBruce Richardson {
16999a2dd95SBruce Richardson 	struct rte_mbuf *m = _m;
17099a2dd95SBruce Richardson 	struct rte_pktmbuf_extmem_init_ctx *ctx = opaque_arg;
17199a2dd95SBruce Richardson 	const struct rte_pktmbuf_extmem *ext_mem;
17299a2dd95SBruce Richardson 	uint32_t mbuf_size, buf_len, priv_size;
17399a2dd95SBruce Richardson 	struct rte_mbuf_ext_shared_info *shinfo;
17499a2dd95SBruce Richardson 
17599a2dd95SBruce Richardson 	priv_size = rte_pktmbuf_priv_size(mp);
17699a2dd95SBruce Richardson 	mbuf_size = sizeof(struct rte_mbuf) + priv_size;
17799a2dd95SBruce Richardson 	buf_len = rte_pktmbuf_data_room_size(mp);
17899a2dd95SBruce Richardson 
17999a2dd95SBruce Richardson 	RTE_ASSERT(RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) == priv_size);
18099a2dd95SBruce Richardson 	RTE_ASSERT(mp->elt_size >= mbuf_size);
18199a2dd95SBruce Richardson 	RTE_ASSERT(buf_len <= UINT16_MAX);
18299a2dd95SBruce Richardson 
18399a2dd95SBruce Richardson 	memset(m, 0, mbuf_size);
18499a2dd95SBruce Richardson 	m->priv_size = priv_size;
18599a2dd95SBruce Richardson 	m->buf_len = (uint16_t)buf_len;
18699a2dd95SBruce Richardson 
18799a2dd95SBruce Richardson 	/* set the data buffer pointers to external memory */
18899a2dd95SBruce Richardson 	ext_mem = ctx->ext_mem + ctx->ext;
18999a2dd95SBruce Richardson 
19099a2dd95SBruce Richardson 	RTE_ASSERT(ctx->ext < ctx->ext_num);
19199a2dd95SBruce Richardson 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
19299a2dd95SBruce Richardson 
19399a2dd95SBruce Richardson 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
194e811e2d7SShijith Thotton 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
195e811e2d7SShijith Thotton 								 (ext_mem->buf_iova + ctx->off));
19699a2dd95SBruce Richardson 
19799a2dd95SBruce Richardson 	ctx->off += ext_mem->elt_size;
19899a2dd95SBruce Richardson 	if (ctx->off + ext_mem->elt_size > ext_mem->buf_len) {
19999a2dd95SBruce Richardson 		ctx->off = 0;
20099a2dd95SBruce Richardson 		++ctx->ext;
20199a2dd95SBruce Richardson 	}
20299a2dd95SBruce Richardson 	/* keep some headroom between start of buffer and data */
20399a2dd95SBruce Richardson 	m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
20499a2dd95SBruce Richardson 
20599a2dd95SBruce Richardson 	/* init some constant fields */
20699a2dd95SBruce Richardson 	m->pool = mp;
20799a2dd95SBruce Richardson 	m->nb_segs = 1;
20899a2dd95SBruce Richardson 	m->port = RTE_MBUF_PORT_INVALID;
209daa02b5cSOlivier Matz 	m->ol_flags = RTE_MBUF_F_EXTERNAL;
21099a2dd95SBruce Richardson 	rte_mbuf_refcnt_set(m, 1);
21199a2dd95SBruce Richardson 	m->next = NULL;
21299a2dd95SBruce Richardson 
21399a2dd95SBruce Richardson 	/* init external buffer shared info items */
21499a2dd95SBruce Richardson 	shinfo = RTE_PTR_ADD(m, mbuf_size);
21599a2dd95SBruce Richardson 	m->shinfo = shinfo;
21699a2dd95SBruce Richardson 	shinfo->free_cb = rte_pktmbuf_free_pinned_extmem;
21799a2dd95SBruce Richardson 	shinfo->fcb_opaque = m;
21899a2dd95SBruce Richardson 	rte_mbuf_ext_refcnt_set(shinfo, 1);
21999a2dd95SBruce Richardson }
22099a2dd95SBruce Richardson 
22199a2dd95SBruce Richardson /* Helper to create a mbuf pool with given mempool ops name*/
22299a2dd95SBruce Richardson struct rte_mempool *
rte_pktmbuf_pool_create_by_ops(const char * name,unsigned int n,unsigned int cache_size,uint16_t priv_size,uint16_t data_room_size,int socket_id,const char * ops_name)22399a2dd95SBruce Richardson rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
22499a2dd95SBruce Richardson 	unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
22599a2dd95SBruce Richardson 	int socket_id, const char *ops_name)
22699a2dd95SBruce Richardson {
22799a2dd95SBruce Richardson 	struct rte_mempool *mp;
22899a2dd95SBruce Richardson 	struct rte_pktmbuf_pool_private mbp_priv;
22999a2dd95SBruce Richardson 	const char *mp_ops_name = ops_name;
23099a2dd95SBruce Richardson 	unsigned elt_size;
23199a2dd95SBruce Richardson 	int ret;
23299a2dd95SBruce Richardson 
23399a2dd95SBruce Richardson 	if (RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) != priv_size) {
234*ae67895bSDavid Marchand 		MBUF_LOG(ERR, "mbuf priv_size=%u is not aligned",
23599a2dd95SBruce Richardson 			priv_size);
23699a2dd95SBruce Richardson 		rte_errno = EINVAL;
23799a2dd95SBruce Richardson 		return NULL;
23899a2dd95SBruce Richardson 	}
23999a2dd95SBruce Richardson 	elt_size = sizeof(struct rte_mbuf) + (unsigned)priv_size +
24099a2dd95SBruce Richardson 		(unsigned)data_room_size;
24199a2dd95SBruce Richardson 	memset(&mbp_priv, 0, sizeof(mbp_priv));
24299a2dd95SBruce Richardson 	mbp_priv.mbuf_data_room_size = data_room_size;
24399a2dd95SBruce Richardson 	mbp_priv.mbuf_priv_size = priv_size;
24499a2dd95SBruce Richardson 
24599a2dd95SBruce Richardson 	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
24699a2dd95SBruce Richardson 		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
24799a2dd95SBruce Richardson 	if (mp == NULL)
24899a2dd95SBruce Richardson 		return NULL;
24999a2dd95SBruce Richardson 
25099a2dd95SBruce Richardson 	if (mp_ops_name == NULL)
25199a2dd95SBruce Richardson 		mp_ops_name = rte_mbuf_best_mempool_ops();
25299a2dd95SBruce Richardson 	ret = rte_mempool_set_ops_byname(mp, mp_ops_name, NULL);
25399a2dd95SBruce Richardson 	if (ret != 0) {
254*ae67895bSDavid Marchand 		MBUF_LOG(ERR, "error setting mempool handler");
25599a2dd95SBruce Richardson 		rte_mempool_free(mp);
25699a2dd95SBruce Richardson 		rte_errno = -ret;
25799a2dd95SBruce Richardson 		return NULL;
25899a2dd95SBruce Richardson 	}
25999a2dd95SBruce Richardson 	rte_pktmbuf_pool_init(mp, &mbp_priv);
26099a2dd95SBruce Richardson 
26199a2dd95SBruce Richardson 	ret = rte_mempool_populate_default(mp);
26299a2dd95SBruce Richardson 	if (ret < 0) {
26399a2dd95SBruce Richardson 		rte_mempool_free(mp);
26499a2dd95SBruce Richardson 		rte_errno = -ret;
26599a2dd95SBruce Richardson 		return NULL;
26699a2dd95SBruce Richardson 	}
26799a2dd95SBruce Richardson 
26899a2dd95SBruce Richardson 	rte_mempool_obj_iter(mp, rte_pktmbuf_init, NULL);
26999a2dd95SBruce Richardson 
27099a2dd95SBruce Richardson 	return mp;
27199a2dd95SBruce Richardson }
27299a2dd95SBruce Richardson 
27399a2dd95SBruce Richardson /* helper to create a mbuf pool */
27499a2dd95SBruce Richardson struct rte_mempool *
rte_pktmbuf_pool_create(const char * name,unsigned int n,unsigned int cache_size,uint16_t priv_size,uint16_t data_room_size,int socket_id)27599a2dd95SBruce Richardson rte_pktmbuf_pool_create(const char *name, unsigned int n,
27699a2dd95SBruce Richardson 	unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
27799a2dd95SBruce Richardson 	int socket_id)
27899a2dd95SBruce Richardson {
27999a2dd95SBruce Richardson 	return rte_pktmbuf_pool_create_by_ops(name, n, cache_size, priv_size,
28099a2dd95SBruce Richardson 			data_room_size, socket_id, NULL);
28199a2dd95SBruce Richardson }
28299a2dd95SBruce Richardson 
28399a2dd95SBruce Richardson /* Helper to create a mbuf pool with pinned external data buffers. */
28499a2dd95SBruce Richardson struct rte_mempool *
rte_pktmbuf_pool_create_extbuf(const char * name,unsigned int n,unsigned int cache_size,uint16_t priv_size,uint16_t data_room_size,int socket_id,const struct rte_pktmbuf_extmem * ext_mem,unsigned int ext_num)28599a2dd95SBruce Richardson rte_pktmbuf_pool_create_extbuf(const char *name, unsigned int n,
28699a2dd95SBruce Richardson 	unsigned int cache_size, uint16_t priv_size,
28799a2dd95SBruce Richardson 	uint16_t data_room_size, int socket_id,
28899a2dd95SBruce Richardson 	const struct rte_pktmbuf_extmem *ext_mem,
28999a2dd95SBruce Richardson 	unsigned int ext_num)
29099a2dd95SBruce Richardson {
29199a2dd95SBruce Richardson 	struct rte_mempool *mp;
29299a2dd95SBruce Richardson 	struct rte_pktmbuf_pool_private mbp_priv;
29399a2dd95SBruce Richardson 	struct rte_pktmbuf_extmem_init_ctx init_ctx;
29499a2dd95SBruce Richardson 	const char *mp_ops_name;
29599a2dd95SBruce Richardson 	unsigned int elt_size;
29699a2dd95SBruce Richardson 	unsigned int i, n_elts = 0;
29799a2dd95SBruce Richardson 	int ret;
29899a2dd95SBruce Richardson 
29999a2dd95SBruce Richardson 	if (RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) != priv_size) {
300*ae67895bSDavid Marchand 		MBUF_LOG(ERR, "mbuf priv_size=%u is not aligned",
30199a2dd95SBruce Richardson 			priv_size);
30299a2dd95SBruce Richardson 		rte_errno = EINVAL;
30399a2dd95SBruce Richardson 		return NULL;
30499a2dd95SBruce Richardson 	}
30599a2dd95SBruce Richardson 	/* Check the external memory descriptors. */
30699a2dd95SBruce Richardson 	for (i = 0; i < ext_num; i++) {
30799a2dd95SBruce Richardson 		const struct rte_pktmbuf_extmem *extm = ext_mem + i;
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson 		if (!extm->elt_size || !extm->buf_len || !extm->buf_ptr) {
310*ae67895bSDavid Marchand 			MBUF_LOG(ERR, "invalid extmem descriptor");
31199a2dd95SBruce Richardson 			rte_errno = EINVAL;
31299a2dd95SBruce Richardson 			return NULL;
31399a2dd95SBruce Richardson 		}
31499a2dd95SBruce Richardson 		if (data_room_size > extm->elt_size) {
315*ae67895bSDavid Marchand 			MBUF_LOG(ERR, "ext elt_size=%u is too small",
31699a2dd95SBruce Richardson 				priv_size);
31799a2dd95SBruce Richardson 			rte_errno = EINVAL;
31899a2dd95SBruce Richardson 			return NULL;
31999a2dd95SBruce Richardson 		}
32099a2dd95SBruce Richardson 		n_elts += extm->buf_len / extm->elt_size;
32199a2dd95SBruce Richardson 	}
32299a2dd95SBruce Richardson 	/* Check whether enough external memory provided. */
32399a2dd95SBruce Richardson 	if (n_elts < n) {
324*ae67895bSDavid Marchand 		MBUF_LOG(ERR, "not enough extmem");
32599a2dd95SBruce Richardson 		rte_errno = ENOMEM;
32699a2dd95SBruce Richardson 		return NULL;
32799a2dd95SBruce Richardson 	}
32899a2dd95SBruce Richardson 	elt_size = sizeof(struct rte_mbuf) +
32999a2dd95SBruce Richardson 		   (unsigned int)priv_size +
33099a2dd95SBruce Richardson 		   sizeof(struct rte_mbuf_ext_shared_info);
33199a2dd95SBruce Richardson 
33299a2dd95SBruce Richardson 	memset(&mbp_priv, 0, sizeof(mbp_priv));
33399a2dd95SBruce Richardson 	mbp_priv.mbuf_data_room_size = data_room_size;
33499a2dd95SBruce Richardson 	mbp_priv.mbuf_priv_size = priv_size;
33599a2dd95SBruce Richardson 	mbp_priv.flags = RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF;
33699a2dd95SBruce Richardson 
33799a2dd95SBruce Richardson 	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
33899a2dd95SBruce Richardson 		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
33999a2dd95SBruce Richardson 	if (mp == NULL)
34099a2dd95SBruce Richardson 		return NULL;
34199a2dd95SBruce Richardson 
34299a2dd95SBruce Richardson 	mp_ops_name = rte_mbuf_best_mempool_ops();
34399a2dd95SBruce Richardson 	ret = rte_mempool_set_ops_byname(mp, mp_ops_name, NULL);
34499a2dd95SBruce Richardson 	if (ret != 0) {
345*ae67895bSDavid Marchand 		MBUF_LOG(ERR, "error setting mempool handler");
34699a2dd95SBruce Richardson 		rte_mempool_free(mp);
34799a2dd95SBruce Richardson 		rte_errno = -ret;
34899a2dd95SBruce Richardson 		return NULL;
34999a2dd95SBruce Richardson 	}
35099a2dd95SBruce Richardson 	rte_pktmbuf_pool_init(mp, &mbp_priv);
35199a2dd95SBruce Richardson 
35299a2dd95SBruce Richardson 	ret = rte_mempool_populate_default(mp);
35399a2dd95SBruce Richardson 	if (ret < 0) {
35499a2dd95SBruce Richardson 		rte_mempool_free(mp);
35599a2dd95SBruce Richardson 		rte_errno = -ret;
35699a2dd95SBruce Richardson 		return NULL;
35799a2dd95SBruce Richardson 	}
35899a2dd95SBruce Richardson 
35999a2dd95SBruce Richardson 	init_ctx = (struct rte_pktmbuf_extmem_init_ctx){
36099a2dd95SBruce Richardson 		.ext_mem = ext_mem,
36199a2dd95SBruce Richardson 		.ext_num = ext_num,
36299a2dd95SBruce Richardson 		.ext = 0,
36399a2dd95SBruce Richardson 		.off = 0,
36499a2dd95SBruce Richardson 	};
36599a2dd95SBruce Richardson 	rte_mempool_obj_iter(mp, __rte_pktmbuf_init_extmem, &init_ctx);
36699a2dd95SBruce Richardson 
36799a2dd95SBruce Richardson 	return mp;
36899a2dd95SBruce Richardson }
36999a2dd95SBruce Richardson 
37099a2dd95SBruce Richardson /* do some sanity checks on a mbuf: panic if it fails */
37199a2dd95SBruce Richardson void
rte_mbuf_sanity_check(const struct rte_mbuf * m,int is_header)37299a2dd95SBruce Richardson rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
37399a2dd95SBruce Richardson {
37499a2dd95SBruce Richardson 	const char *reason;
37599a2dd95SBruce Richardson 
37699a2dd95SBruce Richardson 	if (rte_mbuf_check(m, is_header, &reason))
37799a2dd95SBruce Richardson 		rte_panic("%s\n", reason);
37899a2dd95SBruce Richardson }
37999a2dd95SBruce Richardson 
rte_mbuf_check(const struct rte_mbuf * m,int is_header,const char ** reason)38099a2dd95SBruce Richardson int rte_mbuf_check(const struct rte_mbuf *m, int is_header,
38199a2dd95SBruce Richardson 		   const char **reason)
38299a2dd95SBruce Richardson {
38399a2dd95SBruce Richardson 	unsigned int nb_segs, pkt_len;
38499a2dd95SBruce Richardson 
38599a2dd95SBruce Richardson 	if (m == NULL) {
38699a2dd95SBruce Richardson 		*reason = "mbuf is NULL";
38799a2dd95SBruce Richardson 		return -1;
38899a2dd95SBruce Richardson 	}
38999a2dd95SBruce Richardson 
39099a2dd95SBruce Richardson 	/* generic checks */
39199a2dd95SBruce Richardson 	if (m->pool == NULL) {
39299a2dd95SBruce Richardson 		*reason = "bad mbuf pool";
39399a2dd95SBruce Richardson 		return -1;
39499a2dd95SBruce Richardson 	}
395d5d9e8feSThomas Monjalon 	if (RTE_IOVA_IN_MBUF && rte_mbuf_iova_get(m) == 0) {
39699a2dd95SBruce Richardson 		*reason = "bad IO addr";
39799a2dd95SBruce Richardson 		return -1;
39899a2dd95SBruce Richardson 	}
39999a2dd95SBruce Richardson 	if (m->buf_addr == NULL) {
40099a2dd95SBruce Richardson 		*reason = "bad virt addr";
40199a2dd95SBruce Richardson 		return -1;
40299a2dd95SBruce Richardson 	}
40399a2dd95SBruce Richardson 
40499a2dd95SBruce Richardson 	uint16_t cnt = rte_mbuf_refcnt_read(m);
40599a2dd95SBruce Richardson 	if ((cnt == 0) || (cnt == UINT16_MAX)) {
40699a2dd95SBruce Richardson 		*reason = "bad ref cnt";
40799a2dd95SBruce Richardson 		return -1;
40899a2dd95SBruce Richardson 	}
40999a2dd95SBruce Richardson 
41099a2dd95SBruce Richardson 	/* nothing to check for sub-segments */
41199a2dd95SBruce Richardson 	if (is_header == 0)
41299a2dd95SBruce Richardson 		return 0;
41399a2dd95SBruce Richardson 
41499a2dd95SBruce Richardson 	/* data_len is supposed to be not more than pkt_len */
41599a2dd95SBruce Richardson 	if (m->data_len > m->pkt_len) {
41699a2dd95SBruce Richardson 		*reason = "bad data_len";
41799a2dd95SBruce Richardson 		return -1;
41899a2dd95SBruce Richardson 	}
41999a2dd95SBruce Richardson 
42099a2dd95SBruce Richardson 	nb_segs = m->nb_segs;
42199a2dd95SBruce Richardson 	pkt_len = m->pkt_len;
42299a2dd95SBruce Richardson 
42399a2dd95SBruce Richardson 	do {
42499a2dd95SBruce Richardson 		if (m->data_off > m->buf_len) {
42599a2dd95SBruce Richardson 			*reason = "data offset too big in mbuf segment";
42699a2dd95SBruce Richardson 			return -1;
42799a2dd95SBruce Richardson 		}
42899a2dd95SBruce Richardson 		if (m->data_off + m->data_len > m->buf_len) {
42999a2dd95SBruce Richardson 			*reason = "data length too big in mbuf segment";
43099a2dd95SBruce Richardson 			return -1;
43199a2dd95SBruce Richardson 		}
43299a2dd95SBruce Richardson 		nb_segs -= 1;
43399a2dd95SBruce Richardson 		pkt_len -= m->data_len;
43499a2dd95SBruce Richardson 	} while ((m = m->next) != NULL);
43599a2dd95SBruce Richardson 
43699a2dd95SBruce Richardson 	if (nb_segs) {
43799a2dd95SBruce Richardson 		*reason = "bad nb_segs";
43899a2dd95SBruce Richardson 		return -1;
43999a2dd95SBruce Richardson 	}
44099a2dd95SBruce Richardson 	if (pkt_len) {
44199a2dd95SBruce Richardson 		*reason = "bad pkt_len";
44299a2dd95SBruce Richardson 		return -1;
44399a2dd95SBruce Richardson 	}
44499a2dd95SBruce Richardson 
44599a2dd95SBruce Richardson 	return 0;
44699a2dd95SBruce Richardson }
44799a2dd95SBruce Richardson 
44899a2dd95SBruce Richardson /**
44999a2dd95SBruce Richardson  * @internal helper function for freeing a bulk of packet mbuf segments
45099a2dd95SBruce Richardson  * via an array holding the packet mbuf segments from the same mempool
45199a2dd95SBruce Richardson  * pending to be freed.
45299a2dd95SBruce Richardson  *
45399a2dd95SBruce Richardson  * @param m
45499a2dd95SBruce Richardson  *  The packet mbuf segment to be freed.
45599a2dd95SBruce Richardson  * @param pending
45699a2dd95SBruce Richardson  *  Pointer to the array of packet mbuf segments pending to be freed.
45799a2dd95SBruce Richardson  * @param nb_pending
45899a2dd95SBruce Richardson  *  Pointer to the number of elements held in the array.
45999a2dd95SBruce Richardson  * @param pending_sz
46099a2dd95SBruce Richardson  *  Number of elements the array can hold.
46199a2dd95SBruce Richardson  *  Note: The compiler should optimize this parameter away when using a
46299a2dd95SBruce Richardson  *  constant value, such as RTE_PKTMBUF_FREE_PENDING_SZ.
46399a2dd95SBruce Richardson  */
46499a2dd95SBruce Richardson static void
__rte_pktmbuf_free_seg_via_array(struct rte_mbuf * m,struct rte_mbuf ** const pending,unsigned int * const nb_pending,const unsigned int pending_sz)46599a2dd95SBruce Richardson __rte_pktmbuf_free_seg_via_array(struct rte_mbuf *m,
46699a2dd95SBruce Richardson 	struct rte_mbuf ** const pending, unsigned int * const nb_pending,
46799a2dd95SBruce Richardson 	const unsigned int pending_sz)
46899a2dd95SBruce Richardson {
46999a2dd95SBruce Richardson 	m = rte_pktmbuf_prefree_seg(m);
47099a2dd95SBruce Richardson 	if (likely(m != NULL)) {
47199a2dd95SBruce Richardson 		if (*nb_pending == pending_sz ||
47299a2dd95SBruce Richardson 		    (*nb_pending > 0 && m->pool != pending[0]->pool)) {
47399a2dd95SBruce Richardson 			rte_mempool_put_bulk(pending[0]->pool,
47499a2dd95SBruce Richardson 					(void **)pending, *nb_pending);
47599a2dd95SBruce Richardson 			*nb_pending = 0;
47699a2dd95SBruce Richardson 		}
47799a2dd95SBruce Richardson 
47899a2dd95SBruce Richardson 		pending[(*nb_pending)++] = m;
47999a2dd95SBruce Richardson 	}
48099a2dd95SBruce Richardson }
48199a2dd95SBruce Richardson 
48299a2dd95SBruce Richardson /**
48399a2dd95SBruce Richardson  * Size of the array holding mbufs from the same mempool pending to be freed
48499a2dd95SBruce Richardson  * in bulk.
48599a2dd95SBruce Richardson  */
48699a2dd95SBruce Richardson #define RTE_PKTMBUF_FREE_PENDING_SZ 64
48799a2dd95SBruce Richardson 
48899a2dd95SBruce Richardson /* Free a bulk of packet mbufs back into their original mempools. */
rte_pktmbuf_free_bulk(struct rte_mbuf ** mbufs,unsigned int count)48999a2dd95SBruce Richardson void rte_pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int count)
49099a2dd95SBruce Richardson {
49199a2dd95SBruce Richardson 	struct rte_mbuf *m, *m_next, *pending[RTE_PKTMBUF_FREE_PENDING_SZ];
49299a2dd95SBruce Richardson 	unsigned int idx, nb_pending = 0;
49399a2dd95SBruce Richardson 
49499a2dd95SBruce Richardson 	for (idx = 0; idx < count; idx++) {
49599a2dd95SBruce Richardson 		m = mbufs[idx];
49699a2dd95SBruce Richardson 		if (unlikely(m == NULL))
49799a2dd95SBruce Richardson 			continue;
49899a2dd95SBruce Richardson 
49999a2dd95SBruce Richardson 		__rte_mbuf_sanity_check(m, 1);
50099a2dd95SBruce Richardson 
50199a2dd95SBruce Richardson 		do {
50299a2dd95SBruce Richardson 			m_next = m->next;
50399a2dd95SBruce Richardson 			__rte_pktmbuf_free_seg_via_array(m,
50499a2dd95SBruce Richardson 					pending, &nb_pending,
50599a2dd95SBruce Richardson 					RTE_PKTMBUF_FREE_PENDING_SZ);
50699a2dd95SBruce Richardson 			m = m_next;
50799a2dd95SBruce Richardson 		} while (m != NULL);
50899a2dd95SBruce Richardson 	}
50999a2dd95SBruce Richardson 
51099a2dd95SBruce Richardson 	if (nb_pending > 0)
51199a2dd95SBruce Richardson 		rte_mempool_put_bulk(pending[0]->pool, (void **)pending, nb_pending);
51299a2dd95SBruce Richardson }
51399a2dd95SBruce Richardson 
51499a2dd95SBruce Richardson /* Creates a shallow copy of mbuf */
51599a2dd95SBruce Richardson struct rte_mbuf *
rte_pktmbuf_clone(struct rte_mbuf * md,struct rte_mempool * mp)51699a2dd95SBruce Richardson rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp)
51799a2dd95SBruce Richardson {
51899a2dd95SBruce Richardson 	struct rte_mbuf *mc, *mi, **prev;
51999a2dd95SBruce Richardson 	uint32_t pktlen;
52099a2dd95SBruce Richardson 	uint16_t nseg;
52199a2dd95SBruce Richardson 
52299a2dd95SBruce Richardson 	mc = rte_pktmbuf_alloc(mp);
52399a2dd95SBruce Richardson 	if (unlikely(mc == NULL))
52499a2dd95SBruce Richardson 		return NULL;
52599a2dd95SBruce Richardson 
52699a2dd95SBruce Richardson 	mi = mc;
52799a2dd95SBruce Richardson 	prev = &mi->next;
52899a2dd95SBruce Richardson 	pktlen = md->pkt_len;
52999a2dd95SBruce Richardson 	nseg = 0;
53099a2dd95SBruce Richardson 
53199a2dd95SBruce Richardson 	do {
53299a2dd95SBruce Richardson 		nseg++;
53399a2dd95SBruce Richardson 		rte_pktmbuf_attach(mi, md);
53499a2dd95SBruce Richardson 		*prev = mi;
53599a2dd95SBruce Richardson 		prev = &mi->next;
53699a2dd95SBruce Richardson 	} while ((md = md->next) != NULL &&
53799a2dd95SBruce Richardson 	    (mi = rte_pktmbuf_alloc(mp)) != NULL);
53899a2dd95SBruce Richardson 
53999a2dd95SBruce Richardson 	*prev = NULL;
54099a2dd95SBruce Richardson 	mc->nb_segs = nseg;
54199a2dd95SBruce Richardson 	mc->pkt_len = pktlen;
54299a2dd95SBruce Richardson 
54399a2dd95SBruce Richardson 	/* Allocation of new indirect segment failed */
54499a2dd95SBruce Richardson 	if (unlikely(mi == NULL)) {
54599a2dd95SBruce Richardson 		rte_pktmbuf_free(mc);
54699a2dd95SBruce Richardson 		return NULL;
54799a2dd95SBruce Richardson 	}
54899a2dd95SBruce Richardson 
54999a2dd95SBruce Richardson 	__rte_mbuf_sanity_check(mc, 1);
55099a2dd95SBruce Richardson 	return mc;
55199a2dd95SBruce Richardson }
55299a2dd95SBruce Richardson 
55399a2dd95SBruce Richardson /* convert multi-segment mbuf to single mbuf */
55499a2dd95SBruce Richardson int
__rte_pktmbuf_linearize(struct rte_mbuf * mbuf)55599a2dd95SBruce Richardson __rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
55699a2dd95SBruce Richardson {
55799a2dd95SBruce Richardson 	size_t seg_len, copy_len;
55899a2dd95SBruce Richardson 	struct rte_mbuf *m;
55999a2dd95SBruce Richardson 	struct rte_mbuf *m_next;
56099a2dd95SBruce Richardson 	char *buffer;
56199a2dd95SBruce Richardson 
56299a2dd95SBruce Richardson 	/* Extend first segment to the total packet length */
56399a2dd95SBruce Richardson 	copy_len = rte_pktmbuf_pkt_len(mbuf) - rte_pktmbuf_data_len(mbuf);
56499a2dd95SBruce Richardson 
56599a2dd95SBruce Richardson 	if (unlikely(copy_len > rte_pktmbuf_tailroom(mbuf)))
56699a2dd95SBruce Richardson 		return -1;
56799a2dd95SBruce Richardson 
56899a2dd95SBruce Richardson 	buffer = rte_pktmbuf_mtod_offset(mbuf, char *, mbuf->data_len);
56999a2dd95SBruce Richardson 	mbuf->data_len = (uint16_t)(mbuf->pkt_len);
57099a2dd95SBruce Richardson 
57199a2dd95SBruce Richardson 	/* Append data from next segments to the first one */
57299a2dd95SBruce Richardson 	m = mbuf->next;
57399a2dd95SBruce Richardson 	while (m != NULL) {
57499a2dd95SBruce Richardson 		m_next = m->next;
57599a2dd95SBruce Richardson 
57699a2dd95SBruce Richardson 		seg_len = rte_pktmbuf_data_len(m);
57799a2dd95SBruce Richardson 		rte_memcpy(buffer, rte_pktmbuf_mtod(m, char *), seg_len);
57899a2dd95SBruce Richardson 		buffer += seg_len;
57999a2dd95SBruce Richardson 
58099a2dd95SBruce Richardson 		rte_pktmbuf_free_seg(m);
58199a2dd95SBruce Richardson 		m = m_next;
58299a2dd95SBruce Richardson 	}
58399a2dd95SBruce Richardson 
58499a2dd95SBruce Richardson 	mbuf->next = NULL;
58599a2dd95SBruce Richardson 	mbuf->nb_segs = 1;
58699a2dd95SBruce Richardson 
58799a2dd95SBruce Richardson 	return 0;
58899a2dd95SBruce Richardson }
58999a2dd95SBruce Richardson 
59099a2dd95SBruce Richardson /* Create a deep copy of mbuf */
59199a2dd95SBruce Richardson struct rte_mbuf *
rte_pktmbuf_copy(const struct rte_mbuf * m,struct rte_mempool * mp,uint32_t off,uint32_t len)59299a2dd95SBruce Richardson rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
59399a2dd95SBruce Richardson 		 uint32_t off, uint32_t len)
59499a2dd95SBruce Richardson {
59599a2dd95SBruce Richardson 	const struct rte_mbuf *seg = m;
59699a2dd95SBruce Richardson 	struct rte_mbuf *mc, *m_last, **prev;
59799a2dd95SBruce Richardson 
59899a2dd95SBruce Richardson 	/* garbage in check */
59999a2dd95SBruce Richardson 	__rte_mbuf_sanity_check(m, 1);
60099a2dd95SBruce Richardson 
60199a2dd95SBruce Richardson 	/* check for request to copy at offset past end of mbuf */
60299a2dd95SBruce Richardson 	if (unlikely(off >= m->pkt_len))
60399a2dd95SBruce Richardson 		return NULL;
60499a2dd95SBruce Richardson 
60599a2dd95SBruce Richardson 	mc = rte_pktmbuf_alloc(mp);
60699a2dd95SBruce Richardson 	if (unlikely(mc == NULL))
60799a2dd95SBruce Richardson 		return NULL;
60899a2dd95SBruce Richardson 
60999a2dd95SBruce Richardson 	/* truncate requested length to available data */
61099a2dd95SBruce Richardson 	if (len > m->pkt_len - off)
61199a2dd95SBruce Richardson 		len = m->pkt_len - off;
61299a2dd95SBruce Richardson 
61399a2dd95SBruce Richardson 	__rte_pktmbuf_copy_hdr(mc, m);
61499a2dd95SBruce Richardson 
61599a2dd95SBruce Richardson 	/* copied mbuf is not indirect or external */
616daa02b5cSOlivier Matz 	mc->ol_flags = m->ol_flags & ~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);
61799a2dd95SBruce Richardson 
61899a2dd95SBruce Richardson 	prev = &mc->next;
61999a2dd95SBruce Richardson 	m_last = mc;
62099a2dd95SBruce Richardson 	while (len > 0) {
62199a2dd95SBruce Richardson 		uint32_t copy_len;
62299a2dd95SBruce Richardson 
62399a2dd95SBruce Richardson 		/* skip leading mbuf segments */
62499a2dd95SBruce Richardson 		while (off >= seg->data_len) {
62599a2dd95SBruce Richardson 			off -= seg->data_len;
62699a2dd95SBruce Richardson 			seg = seg->next;
62799a2dd95SBruce Richardson 		}
62899a2dd95SBruce Richardson 
62999a2dd95SBruce Richardson 		/* current buffer is full, chain a new one */
63099a2dd95SBruce Richardson 		if (rte_pktmbuf_tailroom(m_last) == 0) {
63199a2dd95SBruce Richardson 			m_last = rte_pktmbuf_alloc(mp);
63299a2dd95SBruce Richardson 			if (unlikely(m_last == NULL)) {
63399a2dd95SBruce Richardson 				rte_pktmbuf_free(mc);
63499a2dd95SBruce Richardson 				return NULL;
63599a2dd95SBruce Richardson 			}
63699a2dd95SBruce Richardson 			++mc->nb_segs;
63799a2dd95SBruce Richardson 			*prev = m_last;
63899a2dd95SBruce Richardson 			prev = &m_last->next;
63999a2dd95SBruce Richardson 		}
64099a2dd95SBruce Richardson 
64199a2dd95SBruce Richardson 		/*
64299a2dd95SBruce Richardson 		 * copy the min of data in input segment (seg)
64399a2dd95SBruce Richardson 		 * vs space available in output (m_last)
64499a2dd95SBruce Richardson 		 */
64599a2dd95SBruce Richardson 		copy_len = RTE_MIN(seg->data_len - off, len);
64699a2dd95SBruce Richardson 		if (copy_len > rte_pktmbuf_tailroom(m_last))
64799a2dd95SBruce Richardson 			copy_len = rte_pktmbuf_tailroom(m_last);
64899a2dd95SBruce Richardson 
64999a2dd95SBruce Richardson 		/* append from seg to m_last */
65099a2dd95SBruce Richardson 		rte_memcpy(rte_pktmbuf_mtod_offset(m_last, char *,
65199a2dd95SBruce Richardson 						   m_last->data_len),
65299a2dd95SBruce Richardson 			   rte_pktmbuf_mtod_offset(seg, char *, off),
65399a2dd95SBruce Richardson 			   copy_len);
65499a2dd95SBruce Richardson 
65599a2dd95SBruce Richardson 		/* update offsets and lengths */
65699a2dd95SBruce Richardson 		m_last->data_len += copy_len;
65799a2dd95SBruce Richardson 		mc->pkt_len += copy_len;
65899a2dd95SBruce Richardson 		off += copy_len;
65999a2dd95SBruce Richardson 		len -= copy_len;
66099a2dd95SBruce Richardson 	}
66199a2dd95SBruce Richardson 
66299a2dd95SBruce Richardson 	/* garbage out check */
66399a2dd95SBruce Richardson 	__rte_mbuf_sanity_check(mc, 1);
66499a2dd95SBruce Richardson 	return mc;
66599a2dd95SBruce Richardson }
66699a2dd95SBruce Richardson 
66799a2dd95SBruce Richardson /* dump a mbuf on console */
66899a2dd95SBruce Richardson void
rte_pktmbuf_dump(FILE * f,const struct rte_mbuf * m,unsigned dump_len)66999a2dd95SBruce Richardson rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
67099a2dd95SBruce Richardson {
67199a2dd95SBruce Richardson 	unsigned int len;
67299a2dd95SBruce Richardson 	unsigned int nb_segs;
67399a2dd95SBruce Richardson 
67499a2dd95SBruce Richardson 	__rte_mbuf_sanity_check(m, 1);
67599a2dd95SBruce Richardson 
676e811e2d7SShijith Thotton 	fprintf(f, "dump mbuf at %p, iova=%#" PRIx64 ", buf_len=%u\n", m, rte_mbuf_iova_get(m),
677e811e2d7SShijith Thotton 		m->buf_len);
67899a2dd95SBruce Richardson 	fprintf(f, "  pkt_len=%u, ol_flags=%#"PRIx64", nb_segs=%u, port=%u",
67999a2dd95SBruce Richardson 		m->pkt_len, m->ol_flags, m->nb_segs, m->port);
68099a2dd95SBruce Richardson 
6814d75f3fcSBen Magistro 	if (m->ol_flags & (RTE_MBUF_F_RX_QINQ | RTE_MBUF_F_TX_QINQ))
6824d75f3fcSBen Magistro 		fprintf(f, ", vlan_tci_outer=%u", m->vlan_tci_outer);
6834d75f3fcSBen Magistro 
684daa02b5cSOlivier Matz 	if (m->ol_flags & (RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_TX_VLAN))
68599a2dd95SBruce Richardson 		fprintf(f, ", vlan_tci=%u", m->vlan_tci);
68699a2dd95SBruce Richardson 
68799a2dd95SBruce Richardson 	fprintf(f, ", ptype=%#"PRIx32"\n", m->packet_type);
68899a2dd95SBruce Richardson 
68999a2dd95SBruce Richardson 	nb_segs = m->nb_segs;
69099a2dd95SBruce Richardson 
69199a2dd95SBruce Richardson 	while (m && nb_segs != 0) {
69299a2dd95SBruce Richardson 		__rte_mbuf_sanity_check(m, 0);
69399a2dd95SBruce Richardson 
69499a2dd95SBruce Richardson 		fprintf(f, "  segment at %p, data=%p, len=%u, off=%u, refcnt=%u\n",
69599a2dd95SBruce Richardson 			m, rte_pktmbuf_mtod(m, void *),
69699a2dd95SBruce Richardson 			m->data_len, m->data_off, rte_mbuf_refcnt_read(m));
69799a2dd95SBruce Richardson 
69899a2dd95SBruce Richardson 		len = dump_len;
69999a2dd95SBruce Richardson 		if (len > m->data_len)
70099a2dd95SBruce Richardson 			len = m->data_len;
70199a2dd95SBruce Richardson 		if (len != 0)
70299a2dd95SBruce Richardson 			rte_hexdump(f, NULL, rte_pktmbuf_mtod(m, void *), len);
70399a2dd95SBruce Richardson 		dump_len -= len;
70499a2dd95SBruce Richardson 		m = m->next;
70599a2dd95SBruce Richardson 		nb_segs --;
70699a2dd95SBruce Richardson 	}
70799a2dd95SBruce Richardson }
70899a2dd95SBruce Richardson 
70999a2dd95SBruce Richardson /* read len data bytes in a mbuf at specified offset (internal) */
__rte_pktmbuf_read(const struct rte_mbuf * m,uint32_t off,uint32_t len,void * buf)71099a2dd95SBruce Richardson const void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off,
71199a2dd95SBruce Richardson 	uint32_t len, void *buf)
71299a2dd95SBruce Richardson {
71399a2dd95SBruce Richardson 	const struct rte_mbuf *seg = m;
71499a2dd95SBruce Richardson 	uint32_t buf_off = 0, copy_len;
71599a2dd95SBruce Richardson 
71699a2dd95SBruce Richardson 	if (off + len > rte_pktmbuf_pkt_len(m))
71799a2dd95SBruce Richardson 		return NULL;
71899a2dd95SBruce Richardson 
71999a2dd95SBruce Richardson 	while (off >= rte_pktmbuf_data_len(seg)) {
72099a2dd95SBruce Richardson 		off -= rte_pktmbuf_data_len(seg);
72199a2dd95SBruce Richardson 		seg = seg->next;
72299a2dd95SBruce Richardson 	}
72399a2dd95SBruce Richardson 
72499a2dd95SBruce Richardson 	if (off + len <= rte_pktmbuf_data_len(seg))
72599a2dd95SBruce Richardson 		return rte_pktmbuf_mtod_offset(seg, char *, off);
72699a2dd95SBruce Richardson 
72799a2dd95SBruce Richardson 	/* rare case: header is split among several segments */
72899a2dd95SBruce Richardson 	while (len > 0) {
72999a2dd95SBruce Richardson 		copy_len = rte_pktmbuf_data_len(seg) - off;
73099a2dd95SBruce Richardson 		if (copy_len > len)
73199a2dd95SBruce Richardson 			copy_len = len;
73299a2dd95SBruce Richardson 		rte_memcpy((char *)buf + buf_off,
73399a2dd95SBruce Richardson 			rte_pktmbuf_mtod_offset(seg, char *, off), copy_len);
73499a2dd95SBruce Richardson 		off = 0;
73599a2dd95SBruce Richardson 		buf_off += copy_len;
73699a2dd95SBruce Richardson 		len -= copy_len;
73799a2dd95SBruce Richardson 		seg = seg->next;
73899a2dd95SBruce Richardson 	}
73999a2dd95SBruce Richardson 
74099a2dd95SBruce Richardson 	return buf;
74199a2dd95SBruce Richardson }
74299a2dd95SBruce Richardson 
74399a2dd95SBruce Richardson /*
74499a2dd95SBruce Richardson  * Get the name of a RX offload flag. Must be kept synchronized with flag
74599a2dd95SBruce Richardson  * definitions in rte_mbuf.h.
74699a2dd95SBruce Richardson  */
rte_get_rx_ol_flag_name(uint64_t mask)74799a2dd95SBruce Richardson const char *rte_get_rx_ol_flag_name(uint64_t mask)
74899a2dd95SBruce Richardson {
74999a2dd95SBruce Richardson 	switch (mask) {
750daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_VLAN: return "RTE_MBUF_F_RX_VLAN";
751daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_RSS_HASH: return "RTE_MBUF_F_RX_RSS_HASH";
752daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_FDIR: return "RTE_MBUF_F_RX_FDIR";
753daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_L4_CKSUM_BAD: return "RTE_MBUF_F_RX_L4_CKSUM_BAD";
754daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_L4_CKSUM_GOOD: return "RTE_MBUF_F_RX_L4_CKSUM_GOOD";
755daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_L4_CKSUM_NONE: return "RTE_MBUF_F_RX_L4_CKSUM_NONE";
756daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_IP_CKSUM_BAD: return "RTE_MBUF_F_RX_IP_CKSUM_BAD";
757daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_IP_CKSUM_GOOD: return "RTE_MBUF_F_RX_IP_CKSUM_GOOD";
758daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_IP_CKSUM_NONE: return "RTE_MBUF_F_RX_IP_CKSUM_NONE";
759daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD: return "RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD";
760daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_VLAN_STRIPPED: return "RTE_MBUF_F_RX_VLAN_STRIPPED";
761daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_IEEE1588_PTP: return "RTE_MBUF_F_RX_IEEE1588_PTP";
762daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_IEEE1588_TMST: return "RTE_MBUF_F_RX_IEEE1588_TMST";
763daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_FDIR_ID: return "RTE_MBUF_F_RX_FDIR_ID";
764daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_FDIR_FLX: return "RTE_MBUF_F_RX_FDIR_FLX";
765daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_QINQ_STRIPPED: return "RTE_MBUF_F_RX_QINQ_STRIPPED";
766daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_QINQ: return "RTE_MBUF_F_RX_QINQ";
767daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_LRO: return "RTE_MBUF_F_RX_LRO";
768daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_SEC_OFFLOAD: return "RTE_MBUF_F_RX_SEC_OFFLOAD";
769daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_SEC_OFFLOAD_FAILED: return "RTE_MBUF_F_RX_SEC_OFFLOAD_FAILED";
770daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD: return "RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD";
771daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_OUTER_L4_CKSUM_GOOD: return "RTE_MBUF_F_RX_OUTER_L4_CKSUM_GOOD";
772daa02b5cSOlivier Matz 	case RTE_MBUF_F_RX_OUTER_L4_CKSUM_INVALID:
773daa02b5cSOlivier Matz 		return "RTE_MBUF_F_RX_OUTER_L4_CKSUM_INVALID";
77499a2dd95SBruce Richardson 
77599a2dd95SBruce Richardson 	default: return NULL;
77699a2dd95SBruce Richardson 	}
77799a2dd95SBruce Richardson }
77899a2dd95SBruce Richardson 
77999a2dd95SBruce Richardson struct flag_mask {
78099a2dd95SBruce Richardson 	uint64_t flag;
78199a2dd95SBruce Richardson 	uint64_t mask;
78299a2dd95SBruce Richardson 	const char *default_name;
78399a2dd95SBruce Richardson };
78499a2dd95SBruce Richardson 
78599a2dd95SBruce Richardson /* write the list of rx ol flags in buffer buf */
78699a2dd95SBruce Richardson int
rte_get_rx_ol_flag_list(uint64_t mask,char * buf,size_t buflen)78799a2dd95SBruce Richardson rte_get_rx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
78899a2dd95SBruce Richardson {
78999a2dd95SBruce Richardson 	const struct flag_mask rx_flags[] = {
790daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_VLAN, RTE_MBUF_F_RX_VLAN, NULL },
791daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_RSS_HASH, RTE_MBUF_F_RX_RSS_HASH, NULL },
792daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_FDIR, RTE_MBUF_F_RX_FDIR, NULL },
793daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_L4_CKSUM_BAD, RTE_MBUF_F_RX_L4_CKSUM_MASK, NULL },
794daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_L4_CKSUM_GOOD, RTE_MBUF_F_RX_L4_CKSUM_MASK, NULL },
795daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_L4_CKSUM_NONE, RTE_MBUF_F_RX_L4_CKSUM_MASK, NULL },
796daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_L4_CKSUM_UNKNOWN, RTE_MBUF_F_RX_L4_CKSUM_MASK,
797daa02b5cSOlivier Matz 		  "RTE_MBUF_F_RX_L4_CKSUM_UNKNOWN" },
798daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_IP_CKSUM_BAD, RTE_MBUF_F_RX_IP_CKSUM_MASK, NULL },
799daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_IP_CKSUM_GOOD, RTE_MBUF_F_RX_IP_CKSUM_MASK, NULL },
800daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_IP_CKSUM_NONE, RTE_MBUF_F_RX_IP_CKSUM_MASK, NULL },
801daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN, RTE_MBUF_F_RX_IP_CKSUM_MASK,
802daa02b5cSOlivier Matz 		  "RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN" },
803daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD, RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD, NULL },
804daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_VLAN_STRIPPED, RTE_MBUF_F_RX_VLAN_STRIPPED, NULL },
805daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_IEEE1588_PTP, RTE_MBUF_F_RX_IEEE1588_PTP, NULL },
806daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_IEEE1588_TMST, RTE_MBUF_F_RX_IEEE1588_TMST, NULL },
807daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_FDIR_ID, RTE_MBUF_F_RX_FDIR_ID, NULL },
808daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_FDIR_FLX, RTE_MBUF_F_RX_FDIR_FLX, NULL },
809daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_QINQ_STRIPPED, RTE_MBUF_F_RX_QINQ_STRIPPED, NULL },
810daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_LRO, RTE_MBUF_F_RX_LRO, NULL },
811daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_SEC_OFFLOAD, RTE_MBUF_F_RX_SEC_OFFLOAD, NULL },
812daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_SEC_OFFLOAD_FAILED, RTE_MBUF_F_RX_SEC_OFFLOAD_FAILED, NULL },
813daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_QINQ, RTE_MBUF_F_RX_QINQ, NULL },
814daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD, RTE_MBUF_F_RX_OUTER_L4_CKSUM_MASK, NULL },
815daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_OUTER_L4_CKSUM_GOOD, RTE_MBUF_F_RX_OUTER_L4_CKSUM_MASK,
81699a2dd95SBruce Richardson 		  NULL },
817daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_OUTER_L4_CKSUM_INVALID, RTE_MBUF_F_RX_OUTER_L4_CKSUM_MASK,
81899a2dd95SBruce Richardson 		  NULL },
819daa02b5cSOlivier Matz 		{ RTE_MBUF_F_RX_OUTER_L4_CKSUM_UNKNOWN, RTE_MBUF_F_RX_OUTER_L4_CKSUM_MASK,
820daa02b5cSOlivier Matz 		  "RTE_MBUF_F_RX_OUTER_L4_CKSUM_UNKNOWN" },
82199a2dd95SBruce Richardson 	};
82299a2dd95SBruce Richardson 	const char *name;
82399a2dd95SBruce Richardson 	unsigned int i;
82499a2dd95SBruce Richardson 	int ret;
82599a2dd95SBruce Richardson 
82699a2dd95SBruce Richardson 	if (buflen == 0)
82799a2dd95SBruce Richardson 		return -1;
82899a2dd95SBruce Richardson 
82999a2dd95SBruce Richardson 	buf[0] = '\0';
83099a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(rx_flags); i++) {
83199a2dd95SBruce Richardson 		if ((mask & rx_flags[i].mask) != rx_flags[i].flag)
83299a2dd95SBruce Richardson 			continue;
83399a2dd95SBruce Richardson 		name = rte_get_rx_ol_flag_name(rx_flags[i].flag);
83499a2dd95SBruce Richardson 		if (name == NULL)
83599a2dd95SBruce Richardson 			name = rx_flags[i].default_name;
83699a2dd95SBruce Richardson 		ret = snprintf(buf, buflen, "%s ", name);
83799a2dd95SBruce Richardson 		if (ret < 0)
83899a2dd95SBruce Richardson 			return -1;
83999a2dd95SBruce Richardson 		if ((size_t)ret >= buflen)
84099a2dd95SBruce Richardson 			return -1;
84199a2dd95SBruce Richardson 		buf += ret;
84299a2dd95SBruce Richardson 		buflen -= ret;
84399a2dd95SBruce Richardson 	}
84499a2dd95SBruce Richardson 
84599a2dd95SBruce Richardson 	return 0;
84699a2dd95SBruce Richardson }
84799a2dd95SBruce Richardson 
84899a2dd95SBruce Richardson /*
84999a2dd95SBruce Richardson  * Get the name of a TX offload flag. Must be kept synchronized with flag
85099a2dd95SBruce Richardson  * definitions in rte_mbuf.h.
85199a2dd95SBruce Richardson  */
rte_get_tx_ol_flag_name(uint64_t mask)85299a2dd95SBruce Richardson const char *rte_get_tx_ol_flag_name(uint64_t mask)
85399a2dd95SBruce Richardson {
85499a2dd95SBruce Richardson 	switch (mask) {
855daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_VLAN: return "RTE_MBUF_F_TX_VLAN";
856daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_IP_CKSUM: return "RTE_MBUF_F_TX_IP_CKSUM";
857daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TCP_CKSUM: return "RTE_MBUF_F_TX_TCP_CKSUM";
858daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_SCTP_CKSUM: return "RTE_MBUF_F_TX_SCTP_CKSUM";
859daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_UDP_CKSUM: return "RTE_MBUF_F_TX_UDP_CKSUM";
860daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_IEEE1588_TMST: return "RTE_MBUF_F_TX_IEEE1588_TMST";
861daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TCP_SEG: return "RTE_MBUF_F_TX_TCP_SEG";
862daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_IPV4: return "RTE_MBUF_F_TX_IPV4";
863daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_IPV6: return "RTE_MBUF_F_TX_IPV6";
864daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_OUTER_IP_CKSUM: return "RTE_MBUF_F_TX_OUTER_IP_CKSUM";
865daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_OUTER_IPV4: return "RTE_MBUF_F_TX_OUTER_IPV4";
866daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_OUTER_IPV6: return "RTE_MBUF_F_TX_OUTER_IPV6";
867daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_VXLAN: return "RTE_MBUF_F_TX_TUNNEL_VXLAN";
868daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_GTP: return "RTE_MBUF_F_TX_TUNNEL_GTP";
869daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_GRE: return "RTE_MBUF_F_TX_TUNNEL_GRE";
870daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_IPIP: return "RTE_MBUF_F_TX_TUNNEL_IPIP";
871daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_GENEVE: return "RTE_MBUF_F_TX_TUNNEL_GENEVE";
872daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_MPLSINUDP: return "RTE_MBUF_F_TX_TUNNEL_MPLSINUDP";
873daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE: return "RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE";
874daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_IP: return "RTE_MBUF_F_TX_TUNNEL_IP";
875daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_TUNNEL_UDP: return "RTE_MBUF_F_TX_TUNNEL_UDP";
876daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_QINQ: return "RTE_MBUF_F_TX_QINQ";
877daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_MACSEC: return "RTE_MBUF_F_TX_MACSEC";
878daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_SEC_OFFLOAD: return "RTE_MBUF_F_TX_SEC_OFFLOAD";
879daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_UDP_SEG: return "RTE_MBUF_F_TX_UDP_SEG";
880daa02b5cSOlivier Matz 	case RTE_MBUF_F_TX_OUTER_UDP_CKSUM: return "RTE_MBUF_F_TX_OUTER_UDP_CKSUM";
88199a2dd95SBruce Richardson 	default: return NULL;
88299a2dd95SBruce Richardson 	}
88399a2dd95SBruce Richardson }
88499a2dd95SBruce Richardson 
88599a2dd95SBruce Richardson /* write the list of tx ol flags in buffer buf */
88699a2dd95SBruce Richardson int
rte_get_tx_ol_flag_list(uint64_t mask,char * buf,size_t buflen)88799a2dd95SBruce Richardson rte_get_tx_ol_flag_list(uint64_t mask, char *buf, size_t buflen)
88899a2dd95SBruce Richardson {
88999a2dd95SBruce Richardson 	const struct flag_mask tx_flags[] = {
890daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_VLAN, RTE_MBUF_F_TX_VLAN, NULL },
891daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_IP_CKSUM, RTE_MBUF_F_TX_IP_CKSUM, NULL },
892daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TCP_CKSUM, RTE_MBUF_F_TX_L4_MASK, NULL },
893daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_SCTP_CKSUM, RTE_MBUF_F_TX_L4_MASK, NULL },
894daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_UDP_CKSUM, RTE_MBUF_F_TX_L4_MASK, NULL },
895daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_L4_NO_CKSUM, RTE_MBUF_F_TX_L4_MASK, "RTE_MBUF_F_TX_L4_NO_CKSUM" },
896daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_IEEE1588_TMST, RTE_MBUF_F_TX_IEEE1588_TMST, NULL },
897daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TCP_SEG, RTE_MBUF_F_TX_TCP_SEG, NULL },
898daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_IPV4, RTE_MBUF_F_TX_IPV4, NULL },
899daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_IPV6, RTE_MBUF_F_TX_IPV6, NULL },
900daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_OUTER_IP_CKSUM, RTE_MBUF_F_TX_OUTER_IP_CKSUM, NULL },
901daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_OUTER_IPV4, RTE_MBUF_F_TX_OUTER_IPV4, NULL },
902daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_OUTER_IPV6, RTE_MBUF_F_TX_OUTER_IPV6, NULL },
903daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_VXLAN, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
904daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_GTP, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
905daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_GRE, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
906daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_IPIP, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
907daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_GENEVE, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
908daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_MPLSINUDP, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
909daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
910daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_IP, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
911daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_TUNNEL_UDP, RTE_MBUF_F_TX_TUNNEL_MASK, NULL },
912daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_QINQ, RTE_MBUF_F_TX_QINQ, NULL },
913daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_MACSEC, RTE_MBUF_F_TX_MACSEC, NULL },
914daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_SEC_OFFLOAD, RTE_MBUF_F_TX_SEC_OFFLOAD, NULL },
915daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_UDP_SEG, RTE_MBUF_F_TX_UDP_SEG, NULL },
916daa02b5cSOlivier Matz 		{ RTE_MBUF_F_TX_OUTER_UDP_CKSUM, RTE_MBUF_F_TX_OUTER_UDP_CKSUM, NULL },
91799a2dd95SBruce Richardson 	};
91899a2dd95SBruce Richardson 	const char *name;
91999a2dd95SBruce Richardson 	unsigned int i;
92099a2dd95SBruce Richardson 	int ret;
92199a2dd95SBruce Richardson 
92299a2dd95SBruce Richardson 	if (buflen == 0)
92399a2dd95SBruce Richardson 		return -1;
92499a2dd95SBruce Richardson 
92599a2dd95SBruce Richardson 	buf[0] = '\0';
92699a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(tx_flags); i++) {
92799a2dd95SBruce Richardson 		if ((mask & tx_flags[i].mask) != tx_flags[i].flag)
92899a2dd95SBruce Richardson 			continue;
92999a2dd95SBruce Richardson 		name = rte_get_tx_ol_flag_name(tx_flags[i].flag);
93099a2dd95SBruce Richardson 		if (name == NULL)
93199a2dd95SBruce Richardson 			name = tx_flags[i].default_name;
93299a2dd95SBruce Richardson 		ret = snprintf(buf, buflen, "%s ", name);
93399a2dd95SBruce Richardson 		if (ret < 0)
93499a2dd95SBruce Richardson 			return -1;
93599a2dd95SBruce Richardson 		if ((size_t)ret >= buflen)
93699a2dd95SBruce Richardson 			return -1;
93799a2dd95SBruce Richardson 		buf += ret;
93899a2dd95SBruce Richardson 		buflen -= ret;
93999a2dd95SBruce Richardson 	}
94099a2dd95SBruce Richardson 
94199a2dd95SBruce Richardson 	return 0;
94299a2dd95SBruce Richardson }
943