xref: /dpdk/lib/ring/rte_ring.c (revision 700989f512bbc2ee9758a8a9cb6973cfdeda6f27)
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 
1008966fe7STyler 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>
2636e5c1b9SJie Hai #include <rte_telemetry.h>
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson #include "rte_ring.h"
2999a2dd95SBruce Richardson #include "rte_ring_elem.h"
3099a2dd95SBruce Richardson 
31d60c7b91SStephen Hemminger RTE_LOG_REGISTER_DEFAULT(ring_logtype, INFO);
32d60c7b91SStephen Hemminger #define RTE_LOGTYPE_RING ring_logtype
3397433132SDavid Marchand #define RING_LOG(level, ...) \
3497433132SDavid Marchand 	RTE_LOG_LINE(level, RING, "" __VA_ARGS__)
35d60c7b91SStephen Hemminger 
3699a2dd95SBruce Richardson TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson static struct rte_tailq_elem rte_ring_tailq = {
3999a2dd95SBruce Richardson 	.name = RTE_TAILQ_RING_NAME,
4099a2dd95SBruce Richardson };
4199a2dd95SBruce Richardson EAL_REGISTER_TAILQ(rte_ring_tailq)
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson /* mask of all valid flag values to ring_create() */
4499a2dd95SBruce Richardson #define RING_F_MASK (RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ | \
4599a2dd95SBruce Richardson 		     RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ |	       \
4699a2dd95SBruce Richardson 		     RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ)
4799a2dd95SBruce Richardson 
4899a2dd95SBruce Richardson /* true if x is a power of 2 */
4999a2dd95SBruce Richardson #define POWEROF2(x) ((((x)-1) & (x)) == 0)
5099a2dd95SBruce Richardson 
5199a2dd95SBruce Richardson /* by default set head/tail distance as 1/8 of ring capacity */
5299a2dd95SBruce Richardson #define HTD_MAX_DEF	8
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson /* return the size of memory occupied by a ring */
5599a2dd95SBruce Richardson ssize_t
5699a2dd95SBruce Richardson rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
5799a2dd95SBruce Richardson {
5899a2dd95SBruce Richardson 	ssize_t sz;
5999a2dd95SBruce Richardson 
6099a2dd95SBruce Richardson 	/* Check if element size is a multiple of 4B */
6199a2dd95SBruce Richardson 	if (esize % 4 != 0) {
62ae67895bSDavid Marchand 		RING_LOG(ERR, "element size is not a multiple of 4");
6399a2dd95SBruce Richardson 
6499a2dd95SBruce Richardson 		return -EINVAL;
6599a2dd95SBruce Richardson 	}
6699a2dd95SBruce Richardson 
6799a2dd95SBruce Richardson 	/* count must be a power of 2 */
6899a2dd95SBruce Richardson 	if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
69ae67895bSDavid Marchand 		RING_LOG(ERR,
70ae67895bSDavid Marchand 			"Requested number of elements is invalid, must be power of 2, and not exceed %u",
7199a2dd95SBruce Richardson 			RTE_RING_SZ_MASK);
7299a2dd95SBruce Richardson 
7399a2dd95SBruce Richardson 		return -EINVAL;
7499a2dd95SBruce Richardson 	}
7599a2dd95SBruce Richardson 
760e4dc6afSZhihong Wang 	sz = sizeof(struct rte_ring) + (ssize_t)count * esize;
7799a2dd95SBruce Richardson 	sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
7899a2dd95SBruce Richardson 	return sz;
7999a2dd95SBruce Richardson }
8099a2dd95SBruce Richardson 
8199a2dd95SBruce Richardson /* return the size of memory occupied by a ring */
8299a2dd95SBruce Richardson ssize_t
8399a2dd95SBruce Richardson rte_ring_get_memsize(unsigned int count)
8499a2dd95SBruce Richardson {
8599a2dd95SBruce Richardson 	return rte_ring_get_memsize_elem(sizeof(void *), count);
8699a2dd95SBruce Richardson }
8799a2dd95SBruce Richardson 
8899a2dd95SBruce Richardson /*
8999a2dd95SBruce Richardson  * internal helper function to reset prod/cons head-tail values.
9099a2dd95SBruce Richardson  */
9199a2dd95SBruce Richardson static void
9299a2dd95SBruce Richardson reset_headtail(void *p)
9399a2dd95SBruce Richardson {
9499a2dd95SBruce Richardson 	struct rte_ring_headtail *ht;
9599a2dd95SBruce Richardson 	struct rte_ring_hts_headtail *ht_hts;
9699a2dd95SBruce Richardson 	struct rte_ring_rts_headtail *ht_rts;
9799a2dd95SBruce Richardson 
9899a2dd95SBruce Richardson 	ht = p;
9999a2dd95SBruce Richardson 	ht_hts = p;
10099a2dd95SBruce Richardson 	ht_rts = p;
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	switch (ht->sync_type) {
10399a2dd95SBruce Richardson 	case RTE_RING_SYNC_MT:
10499a2dd95SBruce Richardson 	case RTE_RING_SYNC_ST:
10599a2dd95SBruce Richardson 		ht->head = 0;
10699a2dd95SBruce Richardson 		ht->tail = 0;
10799a2dd95SBruce Richardson 		break;
10899a2dd95SBruce Richardson 	case RTE_RING_SYNC_MT_RTS:
10999a2dd95SBruce Richardson 		ht_rts->head.raw = 0;
11099a2dd95SBruce Richardson 		ht_rts->tail.raw = 0;
11199a2dd95SBruce Richardson 		break;
11299a2dd95SBruce Richardson 	case RTE_RING_SYNC_MT_HTS:
11399a2dd95SBruce Richardson 		ht_hts->ht.raw = 0;
11499a2dd95SBruce Richardson 		break;
11599a2dd95SBruce Richardson 	default:
11699a2dd95SBruce Richardson 		/* unknown sync mode */
11799a2dd95SBruce Richardson 		RTE_ASSERT(0);
11899a2dd95SBruce Richardson 	}
11999a2dd95SBruce Richardson }
12099a2dd95SBruce Richardson 
12199a2dd95SBruce Richardson void
12299a2dd95SBruce Richardson rte_ring_reset(struct rte_ring *r)
12399a2dd95SBruce Richardson {
12499a2dd95SBruce Richardson 	reset_headtail(&r->prod);
12599a2dd95SBruce Richardson 	reset_headtail(&r->cons);
12699a2dd95SBruce Richardson }
12799a2dd95SBruce Richardson 
12899a2dd95SBruce Richardson /*
12999a2dd95SBruce Richardson  * helper function, calculates sync_type values for prod and cons
13099a2dd95SBruce Richardson  * based on input flags. Returns zero at success or negative
13199a2dd95SBruce Richardson  * errno value otherwise.
13299a2dd95SBruce Richardson  */
13399a2dd95SBruce Richardson static int
13499a2dd95SBruce Richardson get_sync_type(uint32_t flags, enum rte_ring_sync_type *prod_st,
13599a2dd95SBruce Richardson 	enum rte_ring_sync_type *cons_st)
13699a2dd95SBruce Richardson {
13799a2dd95SBruce Richardson 	static const uint32_t prod_st_flags =
13899a2dd95SBruce Richardson 		(RING_F_SP_ENQ | RING_F_MP_RTS_ENQ | RING_F_MP_HTS_ENQ);
13999a2dd95SBruce Richardson 	static const uint32_t cons_st_flags =
14099a2dd95SBruce Richardson 		(RING_F_SC_DEQ | RING_F_MC_RTS_DEQ | RING_F_MC_HTS_DEQ);
14199a2dd95SBruce Richardson 
14299a2dd95SBruce Richardson 	switch (flags & prod_st_flags) {
14399a2dd95SBruce Richardson 	case 0:
14499a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_MT;
14599a2dd95SBruce Richardson 		break;
14699a2dd95SBruce Richardson 	case RING_F_SP_ENQ:
14799a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_ST;
14899a2dd95SBruce Richardson 		break;
14999a2dd95SBruce Richardson 	case RING_F_MP_RTS_ENQ:
15099a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_MT_RTS;
15199a2dd95SBruce Richardson 		break;
15299a2dd95SBruce Richardson 	case RING_F_MP_HTS_ENQ:
15399a2dd95SBruce Richardson 		*prod_st = RTE_RING_SYNC_MT_HTS;
15499a2dd95SBruce Richardson 		break;
15599a2dd95SBruce Richardson 	default:
15699a2dd95SBruce Richardson 		return -EINVAL;
15799a2dd95SBruce Richardson 	}
15899a2dd95SBruce Richardson 
15999a2dd95SBruce Richardson 	switch (flags & cons_st_flags) {
16099a2dd95SBruce Richardson 	case 0:
16199a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_MT;
16299a2dd95SBruce Richardson 		break;
16399a2dd95SBruce Richardson 	case RING_F_SC_DEQ:
16499a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_ST;
16599a2dd95SBruce Richardson 		break;
16699a2dd95SBruce Richardson 	case RING_F_MC_RTS_DEQ:
16799a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_MT_RTS;
16899a2dd95SBruce Richardson 		break;
16999a2dd95SBruce Richardson 	case RING_F_MC_HTS_DEQ:
17099a2dd95SBruce Richardson 		*cons_st = RTE_RING_SYNC_MT_HTS;
17199a2dd95SBruce Richardson 		break;
17299a2dd95SBruce Richardson 	default:
17399a2dd95SBruce Richardson 		return -EINVAL;
17499a2dd95SBruce Richardson 	}
17599a2dd95SBruce Richardson 
17699a2dd95SBruce Richardson 	return 0;
17799a2dd95SBruce Richardson }
17899a2dd95SBruce Richardson 
17999a2dd95SBruce Richardson int
18099a2dd95SBruce Richardson rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
18199a2dd95SBruce Richardson 	unsigned int flags)
18299a2dd95SBruce Richardson {
18399a2dd95SBruce Richardson 	int ret;
18499a2dd95SBruce Richardson 
18599a2dd95SBruce Richardson 	/* compilation-time checks */
18699a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
18799a2dd95SBruce Richardson 			  RTE_CACHE_LINE_MASK) != 0);
18899a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
18999a2dd95SBruce Richardson 			  RTE_CACHE_LINE_MASK) != 0);
19099a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
19199a2dd95SBruce Richardson 			  RTE_CACHE_LINE_MASK) != 0);
19299a2dd95SBruce Richardson 
19399a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
19499a2dd95SBruce Richardson 		offsetof(struct rte_ring_hts_headtail, sync_type));
19599a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
19699a2dd95SBruce Richardson 		offsetof(struct rte_ring_hts_headtail, ht.pos.tail));
19799a2dd95SBruce Richardson 
19899a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, sync_type) !=
19999a2dd95SBruce Richardson 		offsetof(struct rte_ring_rts_headtail, sync_type));
20099a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(offsetof(struct rte_ring_headtail, tail) !=
20199a2dd95SBruce Richardson 		offsetof(struct rte_ring_rts_headtail, tail.val.pos));
20299a2dd95SBruce Richardson 
20399a2dd95SBruce Richardson 	/* future proof flags, only allow supported values */
20499a2dd95SBruce Richardson 	if (flags & ~RING_F_MASK) {
205ae67895bSDavid Marchand 		RING_LOG(ERR,
206ae67895bSDavid Marchand 			"Unsupported flags requested %#x", flags);
20799a2dd95SBruce Richardson 		return -EINVAL;
20899a2dd95SBruce Richardson 	}
20999a2dd95SBruce Richardson 
21099a2dd95SBruce Richardson 	/* init the ring structure */
21199a2dd95SBruce Richardson 	memset(r, 0, sizeof(*r));
21299a2dd95SBruce Richardson 	ret = strlcpy(r->name, name, sizeof(r->name));
21399a2dd95SBruce Richardson 	if (ret < 0 || ret >= (int)sizeof(r->name))
21499a2dd95SBruce Richardson 		return -ENAMETOOLONG;
21599a2dd95SBruce Richardson 	r->flags = flags;
21699a2dd95SBruce Richardson 	ret = get_sync_type(flags, &r->prod.sync_type, &r->cons.sync_type);
21799a2dd95SBruce Richardson 	if (ret != 0)
21899a2dd95SBruce Richardson 		return ret;
21999a2dd95SBruce Richardson 
22099a2dd95SBruce Richardson 	if (flags & RING_F_EXACT_SZ) {
22199a2dd95SBruce Richardson 		r->size = rte_align32pow2(count + 1);
22299a2dd95SBruce Richardson 		r->mask = r->size - 1;
22399a2dd95SBruce Richardson 		r->capacity = count;
22499a2dd95SBruce Richardson 	} else {
22599a2dd95SBruce Richardson 		if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
226ae67895bSDavid Marchand 			RING_LOG(ERR,
227ae67895bSDavid Marchand 				"Requested size is invalid, must be power of 2, and not exceed the size limit %u",
22899a2dd95SBruce Richardson 				RTE_RING_SZ_MASK);
22999a2dd95SBruce Richardson 			return -EINVAL;
23099a2dd95SBruce Richardson 		}
23199a2dd95SBruce Richardson 		r->size = count;
23299a2dd95SBruce Richardson 		r->mask = count - 1;
23399a2dd95SBruce Richardson 		r->capacity = r->mask;
23499a2dd95SBruce Richardson 	}
23599a2dd95SBruce Richardson 
23699a2dd95SBruce Richardson 	/* set default values for head-tail distance */
23799a2dd95SBruce Richardson 	if (flags & RING_F_MP_RTS_ENQ)
23899a2dd95SBruce Richardson 		rte_ring_set_prod_htd_max(r, r->capacity / HTD_MAX_DEF);
23999a2dd95SBruce Richardson 	if (flags & RING_F_MC_RTS_DEQ)
24099a2dd95SBruce Richardson 		rte_ring_set_cons_htd_max(r, r->capacity / HTD_MAX_DEF);
24199a2dd95SBruce Richardson 
24299a2dd95SBruce Richardson 	return 0;
24399a2dd95SBruce Richardson }
24499a2dd95SBruce Richardson 
24599a2dd95SBruce Richardson /* create the ring for a given element size */
24699a2dd95SBruce Richardson struct rte_ring *
24799a2dd95SBruce Richardson rte_ring_create_elem(const char *name, unsigned int esize, unsigned int count,
24899a2dd95SBruce Richardson 		int socket_id, unsigned int flags)
24999a2dd95SBruce Richardson {
25099a2dd95SBruce Richardson 	char mz_name[RTE_MEMZONE_NAMESIZE];
25199a2dd95SBruce Richardson 	struct rte_ring *r;
25299a2dd95SBruce Richardson 	struct rte_tailq_entry *te;
25399a2dd95SBruce Richardson 	const struct rte_memzone *mz;
25499a2dd95SBruce Richardson 	ssize_t ring_size;
25599a2dd95SBruce Richardson 	int mz_flags = 0;
25699a2dd95SBruce Richardson 	struct rte_ring_list* ring_list = NULL;
25799a2dd95SBruce Richardson 	const unsigned int requested_count = count;
25899a2dd95SBruce Richardson 	int ret;
25999a2dd95SBruce Richardson 
26099a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
26199a2dd95SBruce Richardson 
26299a2dd95SBruce Richardson 	/* for an exact size ring, round up from count to a power of two */
26399a2dd95SBruce Richardson 	if (flags & RING_F_EXACT_SZ)
26499a2dd95SBruce Richardson 		count = rte_align32pow2(count + 1);
26599a2dd95SBruce Richardson 
26699a2dd95SBruce Richardson 	ring_size = rte_ring_get_memsize_elem(esize, count);
26799a2dd95SBruce Richardson 	if (ring_size < 0) {
268074717beSYunjian Wang 		rte_errno = -ring_size;
26999a2dd95SBruce Richardson 		return NULL;
27099a2dd95SBruce Richardson 	}
27199a2dd95SBruce Richardson 
27299a2dd95SBruce Richardson 	ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
27399a2dd95SBruce Richardson 		RTE_RING_MZ_PREFIX, name);
27499a2dd95SBruce Richardson 	if (ret < 0 || ret >= (int)sizeof(mz_name)) {
27599a2dd95SBruce Richardson 		rte_errno = ENAMETOOLONG;
27699a2dd95SBruce Richardson 		return NULL;
27799a2dd95SBruce Richardson 	}
27899a2dd95SBruce Richardson 
27999a2dd95SBruce Richardson 	te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
28099a2dd95SBruce Richardson 	if (te == NULL) {
281ae67895bSDavid Marchand 		RING_LOG(ERR, "Cannot reserve memory for tailq");
28299a2dd95SBruce Richardson 		rte_errno = ENOMEM;
28399a2dd95SBruce Richardson 		return NULL;
28499a2dd95SBruce Richardson 	}
28599a2dd95SBruce Richardson 
28699a2dd95SBruce Richardson 	rte_mcfg_tailq_write_lock();
28799a2dd95SBruce Richardson 
28899a2dd95SBruce Richardson 	/* reserve a memory zone for this ring. If we can't get rte_config or
28999a2dd95SBruce Richardson 	 * we are secondary process, the memzone_reserve function will set
29023f3dac4SStephen Hemminger 	 * rte_errno for us appropriately - hence no check in this function
29123f3dac4SStephen Hemminger 	 */
29299a2dd95SBruce Richardson 	mz = rte_memzone_reserve_aligned(mz_name, ring_size, socket_id,
29308966fe7STyler Retzlaff 					 mz_flags, alignof(typeof(*r)));
29499a2dd95SBruce Richardson 	if (mz != NULL) {
29599a2dd95SBruce Richardson 		r = mz->addr;
29699a2dd95SBruce Richardson 		/* no need to check return value here, we already checked the
29799a2dd95SBruce Richardson 		 * arguments above */
29899a2dd95SBruce Richardson 		rte_ring_init(r, name, requested_count, flags);
29999a2dd95SBruce Richardson 
30099a2dd95SBruce Richardson 		te->data = (void *) r;
30199a2dd95SBruce Richardson 		r->memzone = mz;
30299a2dd95SBruce Richardson 
30399a2dd95SBruce Richardson 		TAILQ_INSERT_TAIL(ring_list, te, next);
30499a2dd95SBruce Richardson 	} else {
30599a2dd95SBruce Richardson 		r = NULL;
306ae67895bSDavid Marchand 		RING_LOG(ERR, "Cannot reserve memory");
30799a2dd95SBruce Richardson 		rte_free(te);
30899a2dd95SBruce Richardson 	}
30999a2dd95SBruce Richardson 	rte_mcfg_tailq_write_unlock();
31099a2dd95SBruce Richardson 
31199a2dd95SBruce Richardson 	return r;
31299a2dd95SBruce Richardson }
31399a2dd95SBruce Richardson 
31499a2dd95SBruce Richardson /* create the ring */
31599a2dd95SBruce Richardson struct rte_ring *
31699a2dd95SBruce Richardson rte_ring_create(const char *name, unsigned int count, int socket_id,
31799a2dd95SBruce Richardson 		unsigned int flags)
31899a2dd95SBruce Richardson {
31999a2dd95SBruce Richardson 	return rte_ring_create_elem(name, sizeof(void *), count, socket_id,
32099a2dd95SBruce Richardson 		flags);
32199a2dd95SBruce Richardson }
32299a2dd95SBruce Richardson 
32399a2dd95SBruce Richardson /* free the ring */
32499a2dd95SBruce Richardson void
32599a2dd95SBruce Richardson rte_ring_free(struct rte_ring *r)
32699a2dd95SBruce Richardson {
32799a2dd95SBruce Richardson 	struct rte_ring_list *ring_list = NULL;
32899a2dd95SBruce Richardson 	struct rte_tailq_entry *te;
32999a2dd95SBruce Richardson 
33099a2dd95SBruce Richardson 	if (r == NULL)
33199a2dd95SBruce Richardson 		return;
33299a2dd95SBruce Richardson 
33399a2dd95SBruce Richardson 	/*
33499a2dd95SBruce Richardson 	 * Ring was not created with rte_ring_create,
33599a2dd95SBruce Richardson 	 * therefore, there is no memzone to free.
33699a2dd95SBruce Richardson 	 */
33799a2dd95SBruce Richardson 	if (r->memzone == NULL) {
338ae67895bSDavid Marchand 		RING_LOG(ERR,
339ae67895bSDavid Marchand 			"Cannot free ring, not created with rte_ring_create()");
34099a2dd95SBruce Richardson 		return;
34199a2dd95SBruce Richardson 	}
34299a2dd95SBruce Richardson 
34399a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
34499a2dd95SBruce Richardson 	rte_mcfg_tailq_write_lock();
34599a2dd95SBruce Richardson 
34699a2dd95SBruce Richardson 	/* find out tailq entry */
34799a2dd95SBruce Richardson 	TAILQ_FOREACH(te, ring_list, next) {
34899a2dd95SBruce Richardson 		if (te->data == (void *) r)
34999a2dd95SBruce Richardson 			break;
35099a2dd95SBruce Richardson 	}
35199a2dd95SBruce Richardson 
35299a2dd95SBruce Richardson 	if (te == NULL) {
35399a2dd95SBruce Richardson 		rte_mcfg_tailq_write_unlock();
35499a2dd95SBruce Richardson 		return;
35599a2dd95SBruce Richardson 	}
35699a2dd95SBruce Richardson 
35799a2dd95SBruce Richardson 	TAILQ_REMOVE(ring_list, te, next);
35899a2dd95SBruce Richardson 
35999a2dd95SBruce Richardson 	rte_mcfg_tailq_write_unlock();
36099a2dd95SBruce Richardson 
361ce4bd6e1SYunjian Wang 	if (rte_memzone_free(r->memzone) != 0)
362ae67895bSDavid Marchand 		RING_LOG(ERR, "Cannot free memory");
363ce4bd6e1SYunjian Wang 
36499a2dd95SBruce Richardson 	rte_free(te);
36599a2dd95SBruce Richardson }
36699a2dd95SBruce Richardson 
367*700989f5SEimear Morrissey static const char *
368*700989f5SEimear Morrissey ring_get_sync_type(const enum rte_ring_sync_type st)
369*700989f5SEimear Morrissey {
370*700989f5SEimear Morrissey 	switch (st) {
371*700989f5SEimear Morrissey 	case RTE_RING_SYNC_ST:
372*700989f5SEimear Morrissey 		return "ST";
373*700989f5SEimear Morrissey 	case RTE_RING_SYNC_MT:
374*700989f5SEimear Morrissey 		return "MT";
375*700989f5SEimear Morrissey 	case RTE_RING_SYNC_MT_RTS:
376*700989f5SEimear Morrissey 		return "MT_RTS";
377*700989f5SEimear Morrissey 	case RTE_RING_SYNC_MT_HTS:
378*700989f5SEimear Morrissey 		return "MT_HTS";
379*700989f5SEimear Morrissey 	default:
380*700989f5SEimear Morrissey 		return "unknown";
381*700989f5SEimear Morrissey 	}
382*700989f5SEimear Morrissey }
383*700989f5SEimear Morrissey 
384*700989f5SEimear Morrissey static void
385*700989f5SEimear Morrissey ring_dump_ht_headtail(FILE *f, const char *prefix,
386*700989f5SEimear Morrissey 		const struct rte_ring_headtail *ht)
387*700989f5SEimear Morrissey {
388*700989f5SEimear Morrissey 	fprintf(f, "%ssync_type=%s\n", prefix,
389*700989f5SEimear Morrissey 			ring_get_sync_type(ht->sync_type));
390*700989f5SEimear Morrissey 	fprintf(f, "%shead=%"PRIu32"\n", prefix, ht->head);
391*700989f5SEimear Morrissey 	fprintf(f, "%stail=%"PRIu32"\n", prefix, ht->tail);
392*700989f5SEimear Morrissey }
393*700989f5SEimear Morrissey 
394*700989f5SEimear Morrissey static void
395*700989f5SEimear Morrissey ring_dump_rts_headtail(FILE *f, const char *prefix,
396*700989f5SEimear Morrissey 		const struct rte_ring_rts_headtail *rts)
397*700989f5SEimear Morrissey {
398*700989f5SEimear Morrissey 	fprintf(f, "%ssync_type=%s\n", prefix,
399*700989f5SEimear Morrissey 			ring_get_sync_type(rts->sync_type));
400*700989f5SEimear Morrissey 	fprintf(f, "%shead.pos=%"PRIu32"\n", prefix, rts->head.val.pos);
401*700989f5SEimear Morrissey 	fprintf(f, "%shead.cnt=%"PRIu32"\n", prefix, rts->head.val.cnt);
402*700989f5SEimear Morrissey 	fprintf(f, "%stail.pos=%"PRIu32"\n", prefix, rts->tail.val.pos);
403*700989f5SEimear Morrissey 	fprintf(f, "%stail.cnt=%"PRIu32"\n", prefix, rts->tail.val.cnt);
404*700989f5SEimear Morrissey 	fprintf(f, "%shtd_max=%"PRIu32"\n", prefix, rts->htd_max);
405*700989f5SEimear Morrissey }
406*700989f5SEimear Morrissey 
407*700989f5SEimear Morrissey static void
408*700989f5SEimear Morrissey ring_dump_hts_headtail(FILE *f, const char *prefix,
409*700989f5SEimear Morrissey 		const struct rte_ring_hts_headtail *hts)
410*700989f5SEimear Morrissey {
411*700989f5SEimear Morrissey 	fprintf(f, "%ssync_type=%s\n", prefix,
412*700989f5SEimear Morrissey 			ring_get_sync_type(hts->sync_type));
413*700989f5SEimear Morrissey 	fprintf(f, "%shead=%"PRIu32"\n", prefix, hts->ht.pos.head);
414*700989f5SEimear Morrissey 	fprintf(f, "%stail=%"PRIu32"\n", prefix, hts->ht.pos.tail);
415*700989f5SEimear Morrissey }
416*700989f5SEimear Morrissey 
417*700989f5SEimear Morrissey void
418*700989f5SEimear Morrissey rte_ring_headtail_dump(FILE *f, const char *prefix,
419*700989f5SEimear Morrissey 		const struct rte_ring_headtail *r)
420*700989f5SEimear Morrissey {
421*700989f5SEimear Morrissey 	if (f == NULL || r == NULL)
422*700989f5SEimear Morrissey 		return;
423*700989f5SEimear Morrissey 
424*700989f5SEimear Morrissey 	prefix = (prefix != NULL) ? prefix : "";
425*700989f5SEimear Morrissey 
426*700989f5SEimear Morrissey 	switch (r->sync_type) {
427*700989f5SEimear Morrissey 	case RTE_RING_SYNC_ST:
428*700989f5SEimear Morrissey 	case RTE_RING_SYNC_MT:
429*700989f5SEimear Morrissey 		ring_dump_ht_headtail(f, prefix, r);
430*700989f5SEimear Morrissey 		break;
431*700989f5SEimear Morrissey 	case RTE_RING_SYNC_MT_RTS:
432*700989f5SEimear Morrissey 		ring_dump_rts_headtail(f, prefix,
433*700989f5SEimear Morrissey 				(const struct rte_ring_rts_headtail *)r);
434*700989f5SEimear Morrissey 		break;
435*700989f5SEimear Morrissey 	case RTE_RING_SYNC_MT_HTS:
436*700989f5SEimear Morrissey 		ring_dump_hts_headtail(f, prefix,
437*700989f5SEimear Morrissey 				(const struct rte_ring_hts_headtail *)r);
438*700989f5SEimear Morrissey 		break;
439*700989f5SEimear Morrissey 	default:
440*700989f5SEimear Morrissey 		RING_LOG(ERR, "Invalid ring sync type detected");
441*700989f5SEimear Morrissey 	}
442*700989f5SEimear Morrissey }
443*700989f5SEimear Morrissey 
44499a2dd95SBruce Richardson /* dump the status of the ring on the console */
44599a2dd95SBruce Richardson void
44699a2dd95SBruce Richardson rte_ring_dump(FILE *f, const struct rte_ring *r)
44799a2dd95SBruce Richardson {
448*700989f5SEimear Morrissey 	if (f == NULL || r == NULL)
449*700989f5SEimear Morrissey 		return;
450*700989f5SEimear Morrissey 
45199a2dd95SBruce Richardson 	fprintf(f, "ring <%s>@%p\n", r->name, r);
45299a2dd95SBruce Richardson 	fprintf(f, "  flags=%x\n", r->flags);
45399a2dd95SBruce Richardson 	fprintf(f, "  size=%"PRIu32"\n", r->size);
45499a2dd95SBruce Richardson 	fprintf(f, "  capacity=%"PRIu32"\n", r->capacity);
45599a2dd95SBruce Richardson 	fprintf(f, "  used=%u\n", rte_ring_count(r));
45699a2dd95SBruce Richardson 	fprintf(f, "  avail=%u\n", rte_ring_free_count(r));
457*700989f5SEimear Morrissey 
458*700989f5SEimear Morrissey 	rte_ring_headtail_dump(f, "  cons.", &(r->cons));
459*700989f5SEimear Morrissey 	rte_ring_headtail_dump(f, "  prod.", &(r->prod));
46099a2dd95SBruce Richardson }
46199a2dd95SBruce Richardson 
46299a2dd95SBruce Richardson /* dump the status of all rings on the console */
46399a2dd95SBruce Richardson void
46499a2dd95SBruce Richardson rte_ring_list_dump(FILE *f)
46599a2dd95SBruce Richardson {
46699a2dd95SBruce Richardson 	const struct rte_tailq_entry *te;
46799a2dd95SBruce Richardson 	struct rte_ring_list *ring_list;
46899a2dd95SBruce Richardson 
46999a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
47099a2dd95SBruce Richardson 
47199a2dd95SBruce Richardson 	rte_mcfg_tailq_read_lock();
47299a2dd95SBruce Richardson 
47399a2dd95SBruce Richardson 	TAILQ_FOREACH(te, ring_list, next) {
47499a2dd95SBruce Richardson 		rte_ring_dump(f, (struct rte_ring *) te->data);
47599a2dd95SBruce Richardson 	}
47699a2dd95SBruce Richardson 
47799a2dd95SBruce Richardson 	rte_mcfg_tailq_read_unlock();
47899a2dd95SBruce Richardson }
47999a2dd95SBruce Richardson 
48099a2dd95SBruce Richardson /* search a ring from its name */
48199a2dd95SBruce Richardson struct rte_ring *
48299a2dd95SBruce Richardson rte_ring_lookup(const char *name)
48399a2dd95SBruce Richardson {
48499a2dd95SBruce Richardson 	struct rte_tailq_entry *te;
48599a2dd95SBruce Richardson 	struct rte_ring *r = NULL;
48699a2dd95SBruce Richardson 	struct rte_ring_list *ring_list;
48799a2dd95SBruce Richardson 
48899a2dd95SBruce Richardson 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
48999a2dd95SBruce Richardson 
49099a2dd95SBruce Richardson 	rte_mcfg_tailq_read_lock();
49199a2dd95SBruce Richardson 
49299a2dd95SBruce Richardson 	TAILQ_FOREACH(te, ring_list, next) {
49399a2dd95SBruce Richardson 		r = (struct rte_ring *) te->data;
49499a2dd95SBruce Richardson 		if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
49599a2dd95SBruce Richardson 			break;
49699a2dd95SBruce Richardson 	}
49799a2dd95SBruce Richardson 
49899a2dd95SBruce Richardson 	rte_mcfg_tailq_read_unlock();
49999a2dd95SBruce Richardson 
50099a2dd95SBruce Richardson 	if (te == NULL) {
50199a2dd95SBruce Richardson 		rte_errno = ENOENT;
50299a2dd95SBruce Richardson 		return NULL;
50399a2dd95SBruce Richardson 	}
50499a2dd95SBruce Richardson 
50599a2dd95SBruce Richardson 	return r;
50699a2dd95SBruce Richardson }
50736e5c1b9SJie Hai 
50836e5c1b9SJie Hai static void
50936e5c1b9SJie Hai ring_walk(void (*func)(struct rte_ring *, void *), void *arg)
51036e5c1b9SJie Hai {
51136e5c1b9SJie Hai 	struct rte_ring_list *ring_list;
51236e5c1b9SJie Hai 	struct rte_tailq_entry *tailq_entry;
51336e5c1b9SJie Hai 
51436e5c1b9SJie Hai 	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
51536e5c1b9SJie Hai 	rte_mcfg_tailq_read_lock();
51636e5c1b9SJie Hai 
51736e5c1b9SJie Hai 	TAILQ_FOREACH(tailq_entry, ring_list, next) {
51836e5c1b9SJie Hai 		(*func)((struct rte_ring *) tailq_entry->data, arg);
51936e5c1b9SJie Hai 	}
52036e5c1b9SJie Hai 
52136e5c1b9SJie Hai 	rte_mcfg_tailq_read_unlock();
52236e5c1b9SJie Hai }
52336e5c1b9SJie Hai 
52436e5c1b9SJie Hai static void
52536e5c1b9SJie Hai ring_list_cb(struct rte_ring *r, void *arg)
52636e5c1b9SJie Hai {
52736e5c1b9SJie Hai 	struct rte_tel_data *d = (struct rte_tel_data *)arg;
52836e5c1b9SJie Hai 
52936e5c1b9SJie Hai 	rte_tel_data_add_array_string(d, r->name);
53036e5c1b9SJie Hai }
53136e5c1b9SJie Hai 
53236e5c1b9SJie Hai static int
53336e5c1b9SJie Hai ring_handle_list(const char *cmd __rte_unused,
53436e5c1b9SJie Hai 		const char *params __rte_unused, struct rte_tel_data *d)
53536e5c1b9SJie Hai {
53636e5c1b9SJie Hai 	rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
53736e5c1b9SJie Hai 	ring_walk(ring_list_cb, d);
53836e5c1b9SJie Hai 	return 0;
53936e5c1b9SJie Hai }
54036e5c1b9SJie Hai 
5412e99bd65SJie Hai static const char *
5422e99bd65SJie Hai ring_prod_sync_type_to_name(struct rte_ring *r)
5432e99bd65SJie Hai {
5442e99bd65SJie Hai 	switch (r->prod.sync_type) {
5452e99bd65SJie Hai 	case RTE_RING_SYNC_MT:
5462e99bd65SJie Hai 		return "MP";
5472e99bd65SJie Hai 	case RTE_RING_SYNC_ST:
5482e99bd65SJie Hai 		return "SP";
5492e99bd65SJie Hai 	case RTE_RING_SYNC_MT_RTS:
5502e99bd65SJie Hai 		return "MP_RTS";
5512e99bd65SJie Hai 	case RTE_RING_SYNC_MT_HTS:
5522e99bd65SJie Hai 		return "MP_HTS";
5532e99bd65SJie Hai 	default:
5542e99bd65SJie Hai 		return "Unknown";
5552e99bd65SJie Hai 	}
5562e99bd65SJie Hai }
5572e99bd65SJie Hai 
5582e99bd65SJie Hai static const char *
5592e99bd65SJie Hai ring_cons_sync_type_to_name(struct rte_ring *r)
5602e99bd65SJie Hai {
5612e99bd65SJie Hai 	switch (r->cons.sync_type) {
5622e99bd65SJie Hai 	case RTE_RING_SYNC_MT:
5632e99bd65SJie Hai 		return "MC";
5642e99bd65SJie Hai 	case RTE_RING_SYNC_ST:
5652e99bd65SJie Hai 		return "SC";
5662e99bd65SJie Hai 	case RTE_RING_SYNC_MT_RTS:
5672e99bd65SJie Hai 		return "MC_RTS";
5682e99bd65SJie Hai 	case RTE_RING_SYNC_MT_HTS:
5692e99bd65SJie Hai 		return "MC_HTS";
5702e99bd65SJie Hai 	default:
5712e99bd65SJie Hai 		return "Unknown";
5722e99bd65SJie Hai 	}
5732e99bd65SJie Hai }
5742e99bd65SJie Hai 
5752e99bd65SJie Hai struct ring_info_cb_arg {
5762e99bd65SJie Hai 	char *ring_name;
5772e99bd65SJie Hai 	struct rte_tel_data *d;
5782e99bd65SJie Hai };
5792e99bd65SJie Hai 
5802e99bd65SJie Hai static void
5812e99bd65SJie Hai ring_info_cb(struct rte_ring *r, void *arg)
5822e99bd65SJie Hai {
5832e99bd65SJie Hai 	struct ring_info_cb_arg *ring_arg = (struct ring_info_cb_arg *)arg;
5842e99bd65SJie Hai 	struct rte_tel_data *d = ring_arg->d;
5852e99bd65SJie Hai 	const struct rte_memzone *mz;
5862e99bd65SJie Hai 
5872e99bd65SJie Hai 	if (strncmp(r->name, ring_arg->ring_name, RTE_RING_NAMESIZE))
5882e99bd65SJie Hai 		return;
5892e99bd65SJie Hai 
5902e99bd65SJie Hai 	rte_tel_data_add_dict_string(d, "name", r->name);
5912e99bd65SJie Hai 	rte_tel_data_add_dict_int(d, "socket", r->memzone->socket_id);
5922e99bd65SJie Hai 	rte_tel_data_add_dict_int(d, "flags", r->flags);
5932e99bd65SJie Hai 	rte_tel_data_add_dict_string(d, "producer_type",
5942e99bd65SJie Hai 		ring_prod_sync_type_to_name(r));
5952e99bd65SJie Hai 	rte_tel_data_add_dict_string(d, "consumer_type",
5962e99bd65SJie Hai 		ring_cons_sync_type_to_name(r));
5972e99bd65SJie Hai 	rte_tel_data_add_dict_uint(d, "size", r->size);
5982e99bd65SJie Hai 	rte_tel_data_add_dict_uint_hex(d, "mask", r->mask, 0);
5992e99bd65SJie Hai 	rte_tel_data_add_dict_uint(d, "capacity", r->capacity);
6002e99bd65SJie Hai 	rte_tel_data_add_dict_uint(d, "used_count", rte_ring_count(r));
6012e99bd65SJie Hai 
6022e99bd65SJie Hai 	mz = r->memzone;
6032e99bd65SJie Hai 	if (mz == NULL)
6042e99bd65SJie Hai 		return;
6052e99bd65SJie Hai 	rte_tel_data_add_dict_string(d, "mz_name", mz->name);
6062e99bd65SJie Hai 	rte_tel_data_add_dict_uint(d, "mz_len", mz->len);
6072e99bd65SJie Hai 	rte_tel_data_add_dict_uint(d, "mz_hugepage_sz", mz->hugepage_sz);
6082e99bd65SJie Hai 	rte_tel_data_add_dict_int(d, "mz_socket_id", mz->socket_id);
6092e99bd65SJie Hai 	rte_tel_data_add_dict_uint_hex(d, "mz_flags", mz->flags, 0);
6102e99bd65SJie Hai }
6112e99bd65SJie Hai 
6122e99bd65SJie Hai static int
6132e99bd65SJie Hai ring_handle_info(const char *cmd __rte_unused, const char *params,
6142e99bd65SJie Hai 		struct rte_tel_data *d)
6152e99bd65SJie Hai {
6162e99bd65SJie Hai 	char name[RTE_RING_NAMESIZE] = {0};
6172e99bd65SJie Hai 	struct ring_info_cb_arg ring_arg;
6182e99bd65SJie Hai 
6192e99bd65SJie Hai 	if (params == NULL || strlen(params) == 0 ||
6202e99bd65SJie Hai 		strlen(params) >= RTE_RING_NAMESIZE)
6212e99bd65SJie Hai 		return -EINVAL;
6222e99bd65SJie Hai 
6232e99bd65SJie Hai 	rte_strlcpy(name, params, RTE_RING_NAMESIZE);
6242e99bd65SJie Hai 
6252e99bd65SJie Hai 	ring_arg.ring_name = name;
6262e99bd65SJie Hai 	ring_arg.d = d;
6272e99bd65SJie Hai 
6282e99bd65SJie Hai 	rte_tel_data_start_dict(d);
6292e99bd65SJie Hai 	ring_walk(ring_info_cb, &ring_arg);
6302e99bd65SJie Hai 
6312e99bd65SJie Hai 	return 0;
6322e99bd65SJie Hai }
6332e99bd65SJie Hai 
63436e5c1b9SJie Hai RTE_INIT(ring_init_telemetry)
63536e5c1b9SJie Hai {
63636e5c1b9SJie Hai 	rte_telemetry_register_cmd("/ring/list", ring_handle_list,
63736e5c1b9SJie Hai 		"Returns list of available rings. Takes no parameters");
6382e99bd65SJie Hai 	rte_telemetry_register_cmd("/ring/info", ring_handle_info,
6392e99bd65SJie Hai 		"Returns ring info. Parameters: ring_name.");
64036e5c1b9SJie Hai }
641