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