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