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 36799a2dd95SBruce Richardson /* dump the status of the ring on the console */ 36899a2dd95SBruce Richardson void 36999a2dd95SBruce Richardson rte_ring_dump(FILE *f, const struct rte_ring *r) 37099a2dd95SBruce Richardson { 37199a2dd95SBruce Richardson fprintf(f, "ring <%s>@%p\n", r->name, r); 37299a2dd95SBruce Richardson fprintf(f, " flags=%x\n", r->flags); 37399a2dd95SBruce Richardson fprintf(f, " size=%"PRIu32"\n", r->size); 37499a2dd95SBruce Richardson fprintf(f, " capacity=%"PRIu32"\n", r->capacity); 37599a2dd95SBruce Richardson fprintf(f, " ct=%"PRIu32"\n", r->cons.tail); 37699a2dd95SBruce Richardson fprintf(f, " ch=%"PRIu32"\n", r->cons.head); 37799a2dd95SBruce Richardson fprintf(f, " pt=%"PRIu32"\n", r->prod.tail); 37899a2dd95SBruce Richardson fprintf(f, " ph=%"PRIu32"\n", r->prod.head); 37999a2dd95SBruce Richardson fprintf(f, " used=%u\n", rte_ring_count(r)); 38099a2dd95SBruce Richardson fprintf(f, " avail=%u\n", rte_ring_free_count(r)); 38199a2dd95SBruce Richardson } 38299a2dd95SBruce Richardson 38399a2dd95SBruce Richardson /* dump the status of all rings on the console */ 38499a2dd95SBruce Richardson void 38599a2dd95SBruce Richardson rte_ring_list_dump(FILE *f) 38699a2dd95SBruce Richardson { 38799a2dd95SBruce Richardson const struct rte_tailq_entry *te; 38899a2dd95SBruce Richardson struct rte_ring_list *ring_list; 38999a2dd95SBruce Richardson 39099a2dd95SBruce Richardson ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list); 39199a2dd95SBruce Richardson 39299a2dd95SBruce Richardson rte_mcfg_tailq_read_lock(); 39399a2dd95SBruce Richardson 39499a2dd95SBruce Richardson TAILQ_FOREACH(te, ring_list, next) { 39599a2dd95SBruce Richardson rte_ring_dump(f, (struct rte_ring *) te->data); 39699a2dd95SBruce Richardson } 39799a2dd95SBruce Richardson 39899a2dd95SBruce Richardson rte_mcfg_tailq_read_unlock(); 39999a2dd95SBruce Richardson } 40099a2dd95SBruce Richardson 40199a2dd95SBruce Richardson /* search a ring from its name */ 40299a2dd95SBruce Richardson struct rte_ring * 40399a2dd95SBruce Richardson rte_ring_lookup(const char *name) 40499a2dd95SBruce Richardson { 40599a2dd95SBruce Richardson struct rte_tailq_entry *te; 40699a2dd95SBruce Richardson struct rte_ring *r = NULL; 40799a2dd95SBruce Richardson struct rte_ring_list *ring_list; 40899a2dd95SBruce Richardson 40999a2dd95SBruce Richardson ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list); 41099a2dd95SBruce Richardson 41199a2dd95SBruce Richardson rte_mcfg_tailq_read_lock(); 41299a2dd95SBruce Richardson 41399a2dd95SBruce Richardson TAILQ_FOREACH(te, ring_list, next) { 41499a2dd95SBruce Richardson r = (struct rte_ring *) te->data; 41599a2dd95SBruce Richardson if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0) 41699a2dd95SBruce Richardson break; 41799a2dd95SBruce Richardson } 41899a2dd95SBruce Richardson 41999a2dd95SBruce Richardson rte_mcfg_tailq_read_unlock(); 42099a2dd95SBruce Richardson 42199a2dd95SBruce Richardson if (te == NULL) { 42299a2dd95SBruce Richardson rte_errno = ENOENT; 42399a2dd95SBruce Richardson return NULL; 42499a2dd95SBruce Richardson } 42599a2dd95SBruce Richardson 42699a2dd95SBruce Richardson return r; 42799a2dd95SBruce Richardson } 42836e5c1b9SJie Hai 42936e5c1b9SJie Hai static void 43036e5c1b9SJie Hai ring_walk(void (*func)(struct rte_ring *, void *), void *arg) 43136e5c1b9SJie Hai { 43236e5c1b9SJie Hai struct rte_ring_list *ring_list; 43336e5c1b9SJie Hai struct rte_tailq_entry *tailq_entry; 43436e5c1b9SJie Hai 43536e5c1b9SJie Hai ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list); 43636e5c1b9SJie Hai rte_mcfg_tailq_read_lock(); 43736e5c1b9SJie Hai 43836e5c1b9SJie Hai TAILQ_FOREACH(tailq_entry, ring_list, next) { 43936e5c1b9SJie Hai (*func)((struct rte_ring *) tailq_entry->data, arg); 44036e5c1b9SJie Hai } 44136e5c1b9SJie Hai 44236e5c1b9SJie Hai rte_mcfg_tailq_read_unlock(); 44336e5c1b9SJie Hai } 44436e5c1b9SJie Hai 44536e5c1b9SJie Hai static void 44636e5c1b9SJie Hai ring_list_cb(struct rte_ring *r, void *arg) 44736e5c1b9SJie Hai { 44836e5c1b9SJie Hai struct rte_tel_data *d = (struct rte_tel_data *)arg; 44936e5c1b9SJie Hai 45036e5c1b9SJie Hai rte_tel_data_add_array_string(d, r->name); 45136e5c1b9SJie Hai } 45236e5c1b9SJie Hai 45336e5c1b9SJie Hai static int 45436e5c1b9SJie Hai ring_handle_list(const char *cmd __rte_unused, 45536e5c1b9SJie Hai const char *params __rte_unused, struct rte_tel_data *d) 45636e5c1b9SJie Hai { 45736e5c1b9SJie Hai rte_tel_data_start_array(d, RTE_TEL_STRING_VAL); 45836e5c1b9SJie Hai ring_walk(ring_list_cb, d); 45936e5c1b9SJie Hai return 0; 46036e5c1b9SJie Hai } 46136e5c1b9SJie Hai 462*2e99bd65SJie Hai static const char * 463*2e99bd65SJie Hai ring_prod_sync_type_to_name(struct rte_ring *r) 464*2e99bd65SJie Hai { 465*2e99bd65SJie Hai switch (r->prod.sync_type) { 466*2e99bd65SJie Hai case RTE_RING_SYNC_MT: 467*2e99bd65SJie Hai return "MP"; 468*2e99bd65SJie Hai case RTE_RING_SYNC_ST: 469*2e99bd65SJie Hai return "SP"; 470*2e99bd65SJie Hai case RTE_RING_SYNC_MT_RTS: 471*2e99bd65SJie Hai return "MP_RTS"; 472*2e99bd65SJie Hai case RTE_RING_SYNC_MT_HTS: 473*2e99bd65SJie Hai return "MP_HTS"; 474*2e99bd65SJie Hai default: 475*2e99bd65SJie Hai return "Unknown"; 476*2e99bd65SJie Hai } 477*2e99bd65SJie Hai } 478*2e99bd65SJie Hai 479*2e99bd65SJie Hai static const char * 480*2e99bd65SJie Hai ring_cons_sync_type_to_name(struct rte_ring *r) 481*2e99bd65SJie Hai { 482*2e99bd65SJie Hai switch (r->cons.sync_type) { 483*2e99bd65SJie Hai case RTE_RING_SYNC_MT: 484*2e99bd65SJie Hai return "MC"; 485*2e99bd65SJie Hai case RTE_RING_SYNC_ST: 486*2e99bd65SJie Hai return "SC"; 487*2e99bd65SJie Hai case RTE_RING_SYNC_MT_RTS: 488*2e99bd65SJie Hai return "MC_RTS"; 489*2e99bd65SJie Hai case RTE_RING_SYNC_MT_HTS: 490*2e99bd65SJie Hai return "MC_HTS"; 491*2e99bd65SJie Hai default: 492*2e99bd65SJie Hai return "Unknown"; 493*2e99bd65SJie Hai } 494*2e99bd65SJie Hai } 495*2e99bd65SJie Hai 496*2e99bd65SJie Hai struct ring_info_cb_arg { 497*2e99bd65SJie Hai char *ring_name; 498*2e99bd65SJie Hai struct rte_tel_data *d; 499*2e99bd65SJie Hai }; 500*2e99bd65SJie Hai 501*2e99bd65SJie Hai static void 502*2e99bd65SJie Hai ring_info_cb(struct rte_ring *r, void *arg) 503*2e99bd65SJie Hai { 504*2e99bd65SJie Hai struct ring_info_cb_arg *ring_arg = (struct ring_info_cb_arg *)arg; 505*2e99bd65SJie Hai struct rte_tel_data *d = ring_arg->d; 506*2e99bd65SJie Hai const struct rte_memzone *mz; 507*2e99bd65SJie Hai 508*2e99bd65SJie Hai if (strncmp(r->name, ring_arg->ring_name, RTE_RING_NAMESIZE)) 509*2e99bd65SJie Hai return; 510*2e99bd65SJie Hai 511*2e99bd65SJie Hai rte_tel_data_add_dict_string(d, "name", r->name); 512*2e99bd65SJie Hai rte_tel_data_add_dict_int(d, "socket", r->memzone->socket_id); 513*2e99bd65SJie Hai rte_tel_data_add_dict_int(d, "flags", r->flags); 514*2e99bd65SJie Hai rte_tel_data_add_dict_string(d, "producer_type", 515*2e99bd65SJie Hai ring_prod_sync_type_to_name(r)); 516*2e99bd65SJie Hai rte_tel_data_add_dict_string(d, "consumer_type", 517*2e99bd65SJie Hai ring_cons_sync_type_to_name(r)); 518*2e99bd65SJie Hai rte_tel_data_add_dict_uint(d, "size", r->size); 519*2e99bd65SJie Hai rte_tel_data_add_dict_uint_hex(d, "mask", r->mask, 0); 520*2e99bd65SJie Hai rte_tel_data_add_dict_uint(d, "capacity", r->capacity); 521*2e99bd65SJie Hai rte_tel_data_add_dict_uint(d, "used_count", rte_ring_count(r)); 522*2e99bd65SJie Hai 523*2e99bd65SJie Hai mz = r->memzone; 524*2e99bd65SJie Hai if (mz == NULL) 525*2e99bd65SJie Hai return; 526*2e99bd65SJie Hai rte_tel_data_add_dict_string(d, "mz_name", mz->name); 527*2e99bd65SJie Hai rte_tel_data_add_dict_uint(d, "mz_len", mz->len); 528*2e99bd65SJie Hai rte_tel_data_add_dict_uint(d, "mz_hugepage_sz", mz->hugepage_sz); 529*2e99bd65SJie Hai rte_tel_data_add_dict_int(d, "mz_socket_id", mz->socket_id); 530*2e99bd65SJie Hai rte_tel_data_add_dict_uint_hex(d, "mz_flags", mz->flags, 0); 531*2e99bd65SJie Hai } 532*2e99bd65SJie Hai 533*2e99bd65SJie Hai static int 534*2e99bd65SJie Hai ring_handle_info(const char *cmd __rte_unused, const char *params, 535*2e99bd65SJie Hai struct rte_tel_data *d) 536*2e99bd65SJie Hai { 537*2e99bd65SJie Hai char name[RTE_RING_NAMESIZE] = {0}; 538*2e99bd65SJie Hai struct ring_info_cb_arg ring_arg; 539*2e99bd65SJie Hai 540*2e99bd65SJie Hai if (params == NULL || strlen(params) == 0 || 541*2e99bd65SJie Hai strlen(params) >= RTE_RING_NAMESIZE) 542*2e99bd65SJie Hai return -EINVAL; 543*2e99bd65SJie Hai 544*2e99bd65SJie Hai rte_strlcpy(name, params, RTE_RING_NAMESIZE); 545*2e99bd65SJie Hai 546*2e99bd65SJie Hai ring_arg.ring_name = name; 547*2e99bd65SJie Hai ring_arg.d = d; 548*2e99bd65SJie Hai 549*2e99bd65SJie Hai rte_tel_data_start_dict(d); 550*2e99bd65SJie Hai ring_walk(ring_info_cb, &ring_arg); 551*2e99bd65SJie Hai 552*2e99bd65SJie Hai return 0; 553*2e99bd65SJie Hai } 554*2e99bd65SJie Hai 55536e5c1b9SJie Hai RTE_INIT(ring_init_telemetry) 55636e5c1b9SJie Hai { 55736e5c1b9SJie Hai rte_telemetry_register_cmd("/ring/list", ring_handle_list, 55836e5c1b9SJie Hai "Returns list of available rings. Takes no parameters"); 559*2e99bd65SJie Hai rte_telemetry_register_cmd("/ring/info", ring_handle_info, 560*2e99bd65SJie Hai "Returns ring info. Parameters: ring_name."); 56136e5c1b9SJie Hai } 562