xref: /dpdk/lib/ring/rte_ring.c (revision 08966fe7f79fdaa5019b7559eebe84a1e3787b89)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  *
399a2dd95SBruce Richardson  * Copyright (c) 2010-2015 Intel Corporation
499a2dd95SBruce Richardson  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
599a2dd95SBruce Richardson  * All rights reserved.
699a2dd95SBruce Richardson  * Derived from FreeBSD's bufring.h
799a2dd95SBruce Richardson  * Used as BSD-3 Licensed with permission from Kip Macy.
899a2dd95SBruce Richardson  */
999a2dd95SBruce Richardson 
10*08966fe7STyler Retzlaff #include <stdalign.h>
1199a2dd95SBruce Richardson #include <stdio.h>
1299a2dd95SBruce Richardson #include <string.h>
1399a2dd95SBruce Richardson #include <stdint.h>
1499a2dd95SBruce Richardson #include <inttypes.h>
1599a2dd95SBruce Richardson #include <errno.h>
1699a2dd95SBruce Richardson #include <sys/queue.h>
1799a2dd95SBruce Richardson 
1899a2dd95SBruce Richardson #include <rte_common.h>
1999a2dd95SBruce Richardson #include <rte_log.h>
2099a2dd95SBruce Richardson #include <rte_memzone.h>
2199a2dd95SBruce Richardson #include <rte_malloc.h>
2299a2dd95SBruce Richardson #include <rte_eal_memconfig.h>
2399a2dd95SBruce Richardson #include <rte_errno.h>
2499a2dd95SBruce Richardson #include <rte_string_fns.h>
2599a2dd95SBruce Richardson #include <rte_tailq.h>
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson #include "rte_ring.h"
2899a2dd95SBruce Richardson #include "rte_ring_elem.h"
2999a2dd95SBruce Richardson 
30d60c7b91SStephen Hemminger RTE_LOG_REGISTER_DEFAULT(ring_logtype, INFO);
31d60c7b91SStephen Hemminger #define RTE_LOGTYPE_RING ring_logtype
3297433132SDavid Marchand #define RING_LOG(level, ...) \
3397433132SDavid Marchand 	RTE_LOG_LINE(level, RING, "" __VA_ARGS__)
34d60c7b91SStephen Hemminger 
3599a2dd95SBruce Richardson TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson static struct rte_tailq_elem rte_ring_tailq = {
3899a2dd95SBruce Richardson 	.name = RTE_TAILQ_RING_NAME,
3999a2dd95SBruce Richardson };
4099a2dd95SBruce Richardson EAL_REGISTER_TAILQ(rte_ring_tailq)
4199a2dd95SBruce Richardson 
4299a2dd95SBruce Richardson /* mask of all valid flag values to ring_create() */
4399a2dd95SBruce Richardson #define RING_F_MASK (RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ | \
4499a2dd95SBruce Richardson 		     RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ |	       \
4599a2dd95SBruce Richardson 		     RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ)
4699a2dd95SBruce Richardson 
4799a2dd95SBruce Richardson /* true if x is a power of 2 */
4899a2dd95SBruce Richardson #define POWEROF2(x) ((((x)-1) & (x)) == 0)
4999a2dd95SBruce Richardson 
5099a2dd95SBruce Richardson /* by default set head/tail distance as 1/8 of ring capacity */
5199a2dd95SBruce Richardson #define HTD_MAX_DEF	8
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson /* return the size of memory occupied by a ring */
5499a2dd95SBruce Richardson ssize_t
5599a2dd95SBruce Richardson rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
5699a2dd95SBruce Richardson {
5799a2dd95SBruce Richardson 	ssize_t sz;
5899a2dd95SBruce Richardson 
5999a2dd95SBruce Richardson 	/* Check if element size is a multiple of 4B */
6099a2dd95SBruce Richardson 	if (esize % 4 != 0) {
61ae67895bSDavid Marchand 		RING_LOG(ERR, "element size is not a multiple of 4");
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson 		return -EINVAL;
6499a2dd95SBruce Richardson 	}
6599a2dd95SBruce Richardson 
6699a2dd95SBruce Richardson 	/* count must be a power of 2 */
6799a2dd95SBruce Richardson 	if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
68ae67895bSDavid Marchand 		RING_LOG(ERR,
69ae67895bSDavid Marchand 			"Requested number of elements is invalid, must be power of 2, and not exceed %u",
7099a2dd95SBruce Richardson 			RTE_RING_SZ_MASK);
7199a2dd95SBruce Richardson 
7299a2dd95SBruce Richardson 		return -EINVAL;
7399a2dd95SBruce Richardson 	}
7499a2dd95SBruce Richardson 
750e4dc6afSZhihong Wang 	sz = sizeof(struct rte_ring) + (ssize_t)count * esize;
7699a2dd95SBruce Richardson 	sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
7799a2dd95SBruce Richardson 	return sz;
7899a2dd95SBruce Richardson }
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson /* return the size of memory occupied by a ring */
8199a2dd95SBruce Richardson ssize_t
8299a2dd95SBruce Richardson rte_ring_get_memsize(unsigned int count)
8399a2dd95SBruce Richardson {
8499a2dd95SBruce Richardson 	return rte_ring_get_memsize_elem(sizeof(void *), count);
8599a2dd95SBruce Richardson }
8699a2dd95SBruce Richardson 
8799a2dd95SBruce Richardson /*
8899a2dd95SBruce Richardson  * internal helper function to reset prod/cons head-tail values.
8999a2dd95SBruce Richardson  */
9099a2dd95SBruce Richardson static void
9199a2dd95SBruce Richardson reset_headtail(void *p)
9299a2dd95SBruce Richardson {
9399a2dd95SBruce Richardson 	struct rte_ring_headtail *ht;
9499a2dd95SBruce Richardson 	struct rte_ring_hts_headtail *ht_hts;
9599a2dd95SBruce Richardson 	struct rte_ring_rts_headtail *ht_rts;
9699a2dd95SBruce Richardson 
9799a2dd95SBruce Richardson 	ht = p;
9899a2dd95SBruce Richardson 	ht_hts = p;
9999a2dd95SBruce Richardson 	ht_rts = p;
10099a2dd95SBruce Richardson 
10199a2dd95SBruce Richardson 	switch (ht->sync_type) {
10299a2dd95SBruce Richardson 	case RTE_RING_SYNC_MT:
10399a2dd95SBruce Richardson 	case RTE_RING_SYNC_ST:
10499a2dd95SBruce Richardson 		ht->head = 0;
10599a2dd95SBruce Richardson 		ht->tail = 0;
10699a2dd95SBruce Richardson 		break;
10799a2dd95SBruce Richardson 	case RTE_RING_SYNC_MT_RTS:
10899a2dd95SBruce Richardson 		ht_rts->head.raw = 0;
10999a2dd95SBruce Richardson 		ht_rts->tail.raw = 0;
11099a2dd95SBruce Richardson 		break;
11199a2dd95SBruce Richardson 	case RTE_RING_SYNC_MT_HTS:
11299a2dd95SBruce Richardson 		ht_hts->ht.raw = 0;
11399a2dd95SBruce Richardson 		break;
11499a2dd95SBruce Richardson 	default:
11599a2dd95SBruce Richardson 		/* unknown sync mode */
11699a2dd95SBruce Richardson 		RTE_ASSERT(0);
11799a2dd95SBruce Richardson 	}
11899a2dd95SBruce Richardson }
11999a2dd95SBruce Richardson 
12099a2dd95SBruce Richardson void
12199a2dd95SBruce Richardson rte_ring_reset(struct rte_ring *r)
12299a2dd95SBruce Richardson {
12399a2dd95SBruce Richardson 	reset_headtail(&r->prod);
12499a2dd95SBruce Richardson 	reset_headtail(&r->cons);
12599a2dd95SBruce Richardson }
12699a2dd95SBruce Richardson 
12799a2dd95SBruce Richardson /*
12899a2dd95SBruce Richardson  * helper function, calculates sync_type values for prod and cons
12999a2dd95SBruce Richardson  * based on input flags. Returns zero at success or negative
13099a2dd95SBruce Richardson  * errno value otherwise.
13199a2dd95SBruce Richardson  */
13299a2dd95SBruce Richardson static int
13399a2dd95SBruce Richardson get_sync_type(uint32_t flags, enum rte_ring_sync_type *prod_st,
13499a2dd95SBruce Richardson 	enum rte_ring_sync_type *cons_st)
13599a2dd95SBruce Richardson {
13699a2dd95SBruce Richardson 	static const uint32_t prod_st_flags =
13799a2dd95SBruce Richardson 		(RING_F_SP_ENQ | RING_F_MP_RTS_ENQ | RING_F_MP_HTS_ENQ);
13899a2dd95SBruce Richardson 	static const uint32_t cons_st_flags =
13999a2dd95SBruce Richardson 		(RING_F_SC_DEQ | RING_F_MC_RTS_DEQ | RING_F_MC_HTS_DEQ);
14099a2dd95SBruce Richardson 
14199a2dd95SBruce Richardson 	switch (flags & prod_st_flags) {
14299a2dd95SBruce Richardson 	case 0:
14399a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_MT;
14499a2dd95SBruce Richardson 		break;
14599a2dd95SBruce Richardson 	case RING_F_SP_ENQ:
14699a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_ST;
14799a2dd95SBruce Richardson 		break;
14899a2dd95SBruce Richardson 	case RING_F_MP_RTS_ENQ:
14999a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_MT_RTS;
15099a2dd95SBruce Richardson 		break;
15199a2dd95SBruce Richardson 	case RING_F_MP_HTS_ENQ:
15299a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_MT_HTS;
15399a2dd95SBruce Richardson 		break;
15499a2dd95SBruce Richardson 	default:
15599a2dd95SBruce Richardson 		return -EINVAL;
15699a2dd95SBruce Richardson 	}
15799a2dd95SBruce Richardson 
15899a2dd95SBruce Richardson 	switch (flags & cons_st_flags) {
15999a2dd95SBruce Richardson 	case 0:
16099a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_MT;
16199a2dd95SBruce Richardson 		break;
16299a2dd95SBruce Richardson 	case RING_F_SC_DEQ:
16399a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_ST;
16499a2dd95SBruce Richardson 		break;
16599a2dd95SBruce Richardson 	case RING_F_MC_RTS_DEQ:
16699a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_MT_RTS;
16799a2dd95SBruce Richardson 		break;
16899a2dd95SBruce Richardson 	case RING_F_MC_HTS_DEQ:
16999a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_MT_HTS;
17099a2dd95SBruce Richardson 		break;
17199a2dd95SBruce Richardson 	default:
17299a2dd95SBruce Richardson 		return -EINVAL;
17399a2dd95SBruce Richardson 	}
17499a2dd95SBruce Richardson 
17599a2dd95SBruce Richardson 	return 0;
17699a2dd95SBruce Richardson }
17799a2dd95SBruce Richardson 
17899a2dd95SBruce Richardson int
17999a2dd95SBruce Richardson rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
18099a2dd95SBruce Richardson 	unsigned int flags)
18199a2dd95SBruce Richardson {
18299a2dd95SBruce Richardson 	int ret;
18399a2dd95SBruce Richardson 
18499a2dd95SBruce Richardson 	/* compilation-time checks */
18599a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
18699a2dd95SBruce Richardson 			  RTE_CACHE_LINE_MASK) != 0);
18799a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
18899a2dd95SBruce Richardson 			  RTE_CACHE_LINE_MASK) != 0);
18999a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
19099a2dd95SBruce Richardson 			  RTE_CACHE_LINE_MASK) != 0);
19199a2dd95SBruce Richardson 
19299a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
19399a2dd95SBruce Richardson 		offsetof(struct rte_ring_hts_headtail, sync_type));
19499a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
19599a2dd95SBruce Richardson 		offsetof(struct rte_ring_hts_headtail, ht.pos.tail));
19699a2dd95SBruce Richardson 
19799a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
19899a2dd95SBruce Richardson 		offsetof(struct rte_ring_rts_headtail, sync_type));
19999a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
20099a2dd95SBruce Richardson 		offsetof(struct rte_ring_rts_headtail, tail.val.pos));
20199a2dd95SBruce Richardson 
20299a2dd95SBruce Richardson 	/* future proof flags, only allow supported values */
20399a2dd95SBruce Richardson 	if (flags & ~RING_F_MASK) {
204ae67895bSDavid Marchand 		RING_LOG(ERR,
205ae67895bSDavid Marchand 			"Unsupported flags requested %#x", flags);
20699a2dd95SBruce Richardson 		return -EINVAL;
20799a2dd95SBruce Richardson 	}
20899a2dd95SBruce Richardson 
20999a2dd95SBruce Richardson 	/* init the ring structure */
21099a2dd95SBruce Richardson 	memset(r, 0, sizeof(*r));
21199a2dd95SBruce Richardson 	ret = strlcpy(r->name, name, sizeof(r->name));
21299a2dd95SBruce Richardson 	if (ret < 0 || ret >= (int)sizeof(r->name))
21399a2dd95SBruce Richardson 		return -ENAMETOOLONG;
21499a2dd95SBruce Richardson 	r->flags = flags;
21599a2dd95SBruce Richardson 	ret = get_sync_type(flags, &r->prod.sync_type, &r->cons.sync_type);
21699a2dd95SBruce Richardson 	if (ret != 0)
21799a2dd95SBruce Richardson 		return ret;
21899a2dd95SBruce Richardson 
21999a2dd95SBruce Richardson 	if (flags & RING_F_EXACT_SZ) {
22099a2dd95SBruce Richardson 		r->size = rte_align32pow2(count + 1);
22199a2dd95SBruce Richardson 		r->mask = r->size - 1;
22299a2dd95SBruce Richardson 		r->capacity = count;
22399a2dd95SBruce Richardson 	} else {
22499a2dd95SBruce Richardson 		if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
225ae67895bSDavid Marchand 			RING_LOG(ERR,
226ae67895bSDavid Marchand 				"Requested size is invalid, must be power of 2, and not exceed the size limit %u",
22799a2dd95SBruce Richardson 				RTE_RING_SZ_MASK);
22899a2dd95SBruce Richardson 			return -EINVAL;
22999a2dd95SBruce Richardson 		}
23099a2dd95SBruce Richardson 		r->size = count;
23199a2dd95SBruce Richardson 		r->mask = count - 1;
23299a2dd95SBruce Richardson 		r->capacity = r->mask;
23399a2dd95SBruce Richardson 	}
23499a2dd95SBruce Richardson 
23599a2dd95SBruce Richardson 	/* set default values for head-tail distance */
23699a2dd95SBruce Richardson 	if (flags & RING_F_MP_RTS_ENQ)
23799a2dd95SBruce Richardson 		rte_ring_set_prod_htd_max(r, r->capacity / HTD_MAX_DEF);
23899a2dd95SBruce Richardson 	if (flags & RING_F_MC_RTS_DEQ)
23999a2dd95SBruce Richardson 		rte_ring_set_cons_htd_max(r, r->capacity / HTD_MAX_DEF);
24099a2dd95SBruce Richardson 
24199a2dd95SBruce Richardson 	return 0;
24299a2dd95SBruce Richardson }
24399a2dd95SBruce Richardson 
24499a2dd95SBruce Richardson /* create the ring for a given element size */
24599a2dd95SBruce Richardson struct rte_ring *
24699a2dd95SBruce Richardson rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
24799a2dd95SBruce Richardson 		int socket_id, unsigned int flags)
24899a2dd95SBruce Richardson {
24999a2dd95SBruce Richardson 	char mz_name[RTE_MEMZONE_NAMESIZE];
25099a2dd95SBruce Richardson 	struct rte_ring *r;
25199a2dd95SBruce Richardson 	struct rte_tailq_entry *te;
25299a2dd95SBruce Richardson 	const struct rte_memzone *mz;
25399a2dd95SBruce Richardson 	ssize_t ring_size;
25499a2dd95SBruce Richardson 	int mz_flags = 0;
25599a2dd95SBruce Richardson 	struct rte_ring_list* ring_list = NULL;
25699a2dd95SBruce Richardson 	const unsigned int requested_count = count;
25799a2dd95SBruce Richardson 	int ret;
25899a2dd95SBruce Richardson 
25999a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
26099a2dd95SBruce Richardson 
26199a2dd95SBruce Richardson 	/* for an exact size ring, round up from count to a power of two */
26299a2dd95SBruce Richardson 	if (flags & RING_F_EXACT_SZ)
26399a2dd95SBruce Richardson 		count = rte_align32pow2(count + 1);
26499a2dd95SBruce Richardson 
26599a2dd95SBruce Richardson 	ring_size = rte_ring_get_memsize_elem(esize, count);
26699a2dd95SBruce Richardson 	if (ring_size < 0) {
267074717beSYunjian Wang 		rte_errno = -ring_size;
26899a2dd95SBruce Richardson 		return NULL;
26999a2dd95SBruce Richardson 	}
27099a2dd95SBruce Richardson 
27199a2dd95SBruce Richardson 	ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
27299a2dd95SBruce Richardson 		RTE_RING_MZ_PREFIX, name);
27399a2dd95SBruce Richardson 	if (ret < 0 || ret >= (int)sizeof(mz_name)) {
27499a2dd95SBruce Richardson 		rte_errno = ENAMETOOLONG;
27599a2dd95SBruce Richardson 		return NULL;
27699a2dd95SBruce Richardson 	}
27799a2dd95SBruce Richardson 
27899a2dd95SBruce Richardson 	te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
27999a2dd95SBruce Richardson 	if (te == NULL) {
280ae67895bSDavid Marchand 		RING_LOG(ERR, "Cannot reserve memory for tailq");
28199a2dd95SBruce Richardson 		rte_errno = ENOMEM;
28299a2dd95SBruce Richardson 		return NULL;
28399a2dd95SBruce Richardson 	}
28499a2dd95SBruce Richardson 
28599a2dd95SBruce Richardson 	rte_mcfg_tailq_write_lock();
28699a2dd95SBruce Richardson 
28799a2dd95SBruce Richardson 	/* reserve a memory zone for this ring. If we can't get rte_config or
28899a2dd95SBruce Richardson 	 * we are secondary process, the memzone_reserve function will set
28923f3dac4SStephen Hemminger 	 * rte_errno for us appropriately - hence no check in this function
29023f3dac4SStephen Hemminger 	 */
29199a2dd95SBruce Richardson 	mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
292*08966fe7STyler Retzlaff 					 mz_flags, alignof(typeof(*r)));
29399a2dd95SBruce Richardson 	if (mz != NULL) {
29499a2dd95SBruce Richardson 		r = mz->addr;
29599a2dd95SBruce Richardson 		/* no need to check return value here, we already checked the
29699a2dd95SBruce Richardson 		 * arguments above */
29799a2dd95SBruce Richardson 		rte_ring_init(r, name, requested_count, flags);
29899a2dd95SBruce Richardson 
29999a2dd95SBruce Richardson 		te->data = (void *) r;
30099a2dd95SBruce Richardson 		r->memzone = mz;
30199a2dd95SBruce Richardson 
30299a2dd95SBruce Richardson 		TAILQ_INSERT_TAIL(ring_list, te, next);
30399a2dd95SBruce Richardson 	} else {
30499a2dd95SBruce Richardson 		r = NULL;
305ae67895bSDavid Marchand 		RING_LOG(ERR, "Cannot reserve memory");
30699a2dd95SBruce Richardson 		rte_free(te);
30799a2dd95SBruce Richardson 	}
30899a2dd95SBruce Richardson 	rte_mcfg_tailq_write_unlock();
30999a2dd95SBruce Richardson 
31099a2dd95SBruce Richardson 	return r;
31199a2dd95SBruce Richardson }
31299a2dd95SBruce Richardson 
31399a2dd95SBruce Richardson /* create the ring */
31499a2dd95SBruce Richardson struct rte_ring *
31599a2dd95SBruce Richardson rte_ring_create(const char *name, unsigned int count, int socket_id,
31699a2dd95SBruce Richardson 		unsigned int flags)
31799a2dd95SBruce Richardson {
31899a2dd95SBruce Richardson 	return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
31999a2dd95SBruce Richardson 		flags);
32099a2dd95SBruce Richardson }
32199a2dd95SBruce Richardson 
32299a2dd95SBruce Richardson /* free the ring */
32399a2dd95SBruce Richardson void
32499a2dd95SBruce Richardson rte_ring_free(struct rte_ring *r)
32599a2dd95SBruce Richardson {
32699a2dd95SBruce Richardson 	struct rte_ring_list *ring_list = NULL;
32799a2dd95SBruce Richardson 	struct rte_tailq_entry *te;
32899a2dd95SBruce Richardson 
32999a2dd95SBruce Richardson 	if (r == NULL)
33099a2dd95SBruce Richardson 		return;
33199a2dd95SBruce Richardson 
33299a2dd95SBruce Richardson 	/*
33399a2dd95SBruce Richardson 	 * Ring was not created with rte_ring_create,
33499a2dd95SBruce Richardson 	 * therefore, there is no memzone to free.
33599a2dd95SBruce Richardson 	 */
33699a2dd95SBruce Richardson 	if (r->memzone == NULL) {
337ae67895bSDavid Marchand 		RING_LOG(ERR,
338ae67895bSDavid Marchand 			"Cannot free ring, not created with rte_ring_create()");
33999a2dd95SBruce Richardson 		return;
34099a2dd95SBruce Richardson 	}
34199a2dd95SBruce Richardson 
34299a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
34399a2dd95SBruce Richardson 	rte_mcfg_tailq_write_lock();
34499a2dd95SBruce Richardson 
34599a2dd95SBruce Richardson 	/* find out tailq entry */
34699a2dd95SBruce Richardson 	TAILQ_FOREACH(te, ring_list, next) {
34799a2dd95SBruce Richardson 		if (te->data == (void *) r)
34899a2dd95SBruce Richardson 			break;
34999a2dd95SBruce Richardson 	}
35099a2dd95SBruce Richardson 
35199a2dd95SBruce Richardson 	if (te == NULL) {
35299a2dd95SBruce Richardson 		rte_mcfg_tailq_write_unlock();
35399a2dd95SBruce Richardson 		return;
35499a2dd95SBruce Richardson 	}
35599a2dd95SBruce Richardson 
35699a2dd95SBruce Richardson 	TAILQ_REMOVE(ring_list, te, next);
35799a2dd95SBruce Richardson 
35899a2dd95SBruce Richardson 	rte_mcfg_tailq_write_unlock();
35999a2dd95SBruce Richardson 
360ce4bd6e1SYunjian Wang 	if (rte_memzone_free(r->memzone) != 0)
361ae67895bSDavid Marchand 		RING_LOG(ERR, "Cannot free memory");
362ce4bd6e1SYunjian Wang 
36399a2dd95SBruce Richardson 	rte_free(te);
36499a2dd95SBruce Richardson }
36599a2dd95SBruce Richardson 
36699a2dd95SBruce Richardson /* dump the status of the ring on the console */
36799a2dd95SBruce Richardson void
36899a2dd95SBruce Richardson rte_ring_dump(FILE *f, const struct rte_ring *r)
36999a2dd95SBruce Richardson {
37099a2dd95SBruce Richardson 	fprintf(f, "ring <%s>@%p\n", r->name, r);
37199a2dd95SBruce Richardson 	fprintf(f, "  flags=%x\n", r->flags);
37299a2dd95SBruce Richardson 	fprintf(f, "  size=%"PRIu32"\n", r->size);
37399a2dd95SBruce Richardson 	fprintf(f, "  capacity=%"PRIu32"\n", r->capacity);
37499a2dd95SBruce Richardson 	fprintf(f, "  ct=%"PRIu32"\n", r->cons.tail);
37599a2dd95SBruce Richardson 	fprintf(f, "  ch=%"PRIu32"\n", r->cons.head);
37699a2dd95SBruce Richardson 	fprintf(f, "  pt=%"PRIu32"\n", r->prod.tail);
37799a2dd95SBruce Richardson 	fprintf(f, "  ph=%"PRIu32"\n", r->prod.head);
37899a2dd95SBruce Richardson 	fprintf(f, "  used=%u\n", rte_ring_count(r));
37999a2dd95SBruce Richardson 	fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
38099a2dd95SBruce Richardson }
38199a2dd95SBruce Richardson 
38299a2dd95SBruce Richardson /* dump the status of all rings on the console */
38399a2dd95SBruce Richardson void
38499a2dd95SBruce Richardson rte_ring_list_dump(FILE *f)
38599a2dd95SBruce Richardson {
38699a2dd95SBruce Richardson 	const struct rte_tailq_entry *te;
38799a2dd95SBruce Richardson 	struct rte_ring_list *ring_list;
38899a2dd95SBruce Richardson 
38999a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
39099a2dd95SBruce Richardson 
39199a2dd95SBruce Richardson 	rte_mcfg_tailq_read_lock();
39299a2dd95SBruce Richardson 
39399a2dd95SBruce Richardson 	TAILQ_FOREACH(te, ring_list, next) {
39499a2dd95SBruce Richardson 		rte_ring_dump(f, (struct rte_ring *) te->data);
39599a2dd95SBruce Richardson 	}
39699a2dd95SBruce Richardson 
39799a2dd95SBruce Richardson 	rte_mcfg_tailq_read_unlock();
39899a2dd95SBruce Richardson }
39999a2dd95SBruce Richardson 
40099a2dd95SBruce Richardson /* search a ring from its name */
40199a2dd95SBruce Richardson struct rte_ring *
40299a2dd95SBruce Richardson rte_ring_lookup(const char *name)
40399a2dd95SBruce Richardson {
40499a2dd95SBruce Richardson 	struct rte_tailq_entry *te;
40599a2dd95SBruce Richardson 	struct rte_ring *r = NULL;
40699a2dd95SBruce Richardson 	struct rte_ring_list *ring_list;
40799a2dd95SBruce Richardson 
40899a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
40999a2dd95SBruce Richardson 
41099a2dd95SBruce Richardson 	rte_mcfg_tailq_read_lock();
41199a2dd95SBruce Richardson 
41299a2dd95SBruce Richardson 	TAILQ_FOREACH(te, ring_list, next) {
41399a2dd95SBruce Richardson 		r = (struct rte_ring *) te->data;
41499a2dd95SBruce Richardson 		if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
41599a2dd95SBruce Richardson 			break;
41699a2dd95SBruce Richardson 	}
41799a2dd95SBruce Richardson 
41899a2dd95SBruce Richardson 	rte_mcfg_tailq_read_unlock();
41999a2dd95SBruce Richardson 
42099a2dd95SBruce Richardson 	if (te == NULL) {
42199a2dd95SBruce Richardson 		rte_errno = ENOENT;
42299a2dd95SBruce Richardson 		return NULL;
42399a2dd95SBruce Richardson 	}
42499a2dd95SBruce Richardson 
42599a2dd95SBruce Richardson 	return r;
42699a2dd95SBruce Richardson }
427