From b57a22942d27636291889298a3e186fde0f5225e Mon Sep 17 00:00:00 2001
From: Jie Hai <haijie1@huawei.com>
Date: Mon, 19 Feb 2024 16:32:53 +0800
Subject: [PATCH 18/30] ring: add telemetry command for ring info
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

[ upstream commit 2e99bd65ca788f7f540b2c155208dae2b0128b36 ]

This patch supports dump of ring information by its name.
An example using this command is shown below:

--> /ring/info,MP_mb_pool_0
{
  "/ring/info": {
    "name": "MP_mb_pool_0",
    "socket": 0,
    "flags": 0,
    "producer_type": "MP",
    "consumer_type": "MC",
    "size": 262144,
    "mask": "0x3ffff",
    "capacity": 262143,
    "used_count": 153197,
    "mz_name": "RG_MP_mb_pool_0",
    "mz_len": 2097536,
    "mz_hugepage_sz": 1073741824,
    "mz_socket_id": 0,
    "mz_flags": "0x0"
  }
}

Signed-off-by: Jie Hai <haijie1@huawei.com>
Reviewed-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
Acked-by: Konstantin Ananyev <konstantin.v.ananyev@yandex.ru>
Acked-by: Huisong Li <lihuisong@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 lib/ring/rte_ring.c | 95 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c
index 6a10280..ec56b38 100644
--- a/lib/ring/rte_ring.c
+++ b/lib/ring/rte_ring.c
@@ -453,8 +453,103 @@ ring_handle_list(const char *cmd __rte_unused,
 	return 0;
 }
 
+static const char *
+ring_prod_sync_type_to_name(struct rte_ring *r)
+{
+	switch (r->prod.sync_type) {
+	case RTE_RING_SYNC_MT:
+		return "MP";
+	case RTE_RING_SYNC_ST:
+		return "SP";
+	case RTE_RING_SYNC_MT_RTS:
+		return "MP_RTS";
+	case RTE_RING_SYNC_MT_HTS:
+		return "MP_HTS";
+	default:
+		return "Unknown";
+	}
+}
+
+static const char *
+ring_cons_sync_type_to_name(struct rte_ring *r)
+{
+	switch (r->cons.sync_type) {
+	case RTE_RING_SYNC_MT:
+		return "MC";
+	case RTE_RING_SYNC_ST:
+		return "SC";
+	case RTE_RING_SYNC_MT_RTS:
+		return "MC_RTS";
+	case RTE_RING_SYNC_MT_HTS:
+		return "MC_HTS";
+	default:
+		return "Unknown";
+	}
+}
+
+struct ring_info_cb_arg {
+	char *ring_name;
+	struct rte_tel_data *d;
+};
+
+static void
+ring_info_cb(struct rte_ring *r, void *arg)
+{
+	struct ring_info_cb_arg *ring_arg = (struct ring_info_cb_arg *)arg;
+	struct rte_tel_data *d = ring_arg->d;
+	const struct rte_memzone *mz;
+
+	if (strncmp(r->name, ring_arg->ring_name, RTE_RING_NAMESIZE))
+		return;
+
+	rte_tel_data_add_dict_string(d, "name", r->name);
+	rte_tel_data_add_dict_int(d, "socket", r->memzone->socket_id);
+	rte_tel_data_add_dict_int(d, "flags", r->flags);
+	rte_tel_data_add_dict_string(d, "producer_type",
+		ring_prod_sync_type_to_name(r));
+	rte_tel_data_add_dict_string(d, "consumer_type",
+		ring_cons_sync_type_to_name(r));
+	rte_tel_data_add_dict_uint(d, "size", r->size);
+	rte_tel_data_add_dict_uint_hex(d, "mask", r->mask, 0);
+	rte_tel_data_add_dict_uint(d, "capacity", r->capacity);
+	rte_tel_data_add_dict_uint(d, "used_count", rte_ring_count(r));
+
+	mz = r->memzone;
+	if (mz == NULL)
+		return;
+	rte_tel_data_add_dict_string(d, "mz_name", mz->name);
+	rte_tel_data_add_dict_uint(d, "mz_len", mz->len);
+	rte_tel_data_add_dict_uint(d, "mz_hugepage_sz", mz->hugepage_sz);
+	rte_tel_data_add_dict_int(d, "mz_socket_id", mz->socket_id);
+	rte_tel_data_add_dict_uint_hex(d, "mz_flags", mz->flags, 0);
+}
+
+static int
+ring_handle_info(const char *cmd __rte_unused, const char *params,
+		struct rte_tel_data *d)
+{
+	char name[RTE_RING_NAMESIZE] = {0};
+	struct ring_info_cb_arg ring_arg;
+
+	if (params == NULL || strlen(params) == 0 ||
+		strlen(params) >= RTE_RING_NAMESIZE)
+		return -EINVAL;
+
+	rte_strlcpy(name, params, RTE_RING_NAMESIZE);
+
+	ring_arg.ring_name = name;
+	ring_arg.d = d;
+
+	rte_tel_data_start_dict(d);
+	ring_walk(ring_info_cb, &ring_arg);
+
+	return 0;
+}
+
 RTE_INIT(ring_init_telemetry)
 {
 	rte_telemetry_register_cmd("/ring/list", ring_handle_list,
 		"Returns list of available rings. Takes no parameters");
+	rte_telemetry_register_cmd("/ring/info", ring_handle_info,
+		"Returns ring info. Parameters: ring_name.");
 }
-- 
2.33.0

