From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <keith.wiles@intel.com>
Received: from mga02.intel.com (mga02.intel.com [134.134.136.20])
 by dpdk.org (Postfix) with ESMTP id 4C4125B16
 for <dev@dpdk.org>; Thu, 27 Dec 2018 19:17:27 +0100 (CET)
X-Amp-Result: SKIPPED(no attachment in message)
X-Amp-File-Uploaded: False
Received: from orsmga006.jf.intel.com ([10.7.209.51])
 by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;
 27 Dec 2018 10:17:26 -0800
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.56,406,1539673200"; d="scan'208";a="103748561"
Received: from kwyoon-mobl.amr.corp.intel.com ([10.251.151.199])
 by orsmga006.jf.intel.com with ESMTP; 27 Dec 2018 10:17:25 -0800
From: Keith Wiles <keith.wiles@intel.com>
To: dev@dpdk.org
Cc: olivier.matz@6wind.com
Date: Thu, 27 Dec 2018 12:17:22 -0600
Message-Id: <20181227181722.26685-1-keith.wiles@intel.com>
X-Mailer: git-send-email 2.18.0
In-Reply-To: <20181216172721.91042-1-keith.wiles@intel.com>⁩>
References: <20181216172721.91042-1-keith.wiles@intel.com>⁩>
Subject: [dpdk-dev] [PATCH v5] ring:add ring walk routine
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Thu, 27 Dec 2018 18:17:27 -0000

Add a ring walk routine for debugging and other uses.
This routine walks the list of rings and allows the
called function to stop, remove ring from list or continue.

Signed-off-by: Keith Wiles <keith.wiles@intel.com>
---
V6
  Remove warning
V5
  Fixup checkpatch warning on commit message
V4
  Add TAILQ_FOREACH_SAFE and have function being called return a value
  to be used to stop walking, free tailq_entry or continue walking.
V3
  Fix checkpatch warnings adding a commit message.
  Must be using a different checkpatch then on my Ubuntu 18.04 system 
V2
  Fix checkpatch warnings.

 lib/librte_ring/rte_ring.c           | 29 ++++++++++++++++++++++++++++
 lib/librte_ring/rte_ring.h           | 17 ++++++++++++++++
 lib/librte_ring/rte_ring_version.map |  7 +++++++
 3 files changed, 53 insertions(+)

diff --git a/lib/librte_ring/rte_ring.c b/lib/librte_ring/rte_ring.c
index d215acecc..17c9780df 100644
--- a/lib/librte_ring/rte_ring.c
+++ b/lib/librte_ring/rte_ring.c
@@ -280,3 +280,32 @@ rte_ring_lookup(const char *name)
 
 	return r;
 }
+
+void
+rte_ring_walk(int (*func)(struct rte_ring *r, void *arg), void *arg)
+{
+	const struct rte_tailq_entry *te, *tv;
+	struct rte_ring_list *ring_list;
+
+	if (!func)
+		return;
+
+	ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
+
+	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+
+	TAILQ_FOREACH_SAFE(te, ring_list, next, tv) {
+		int ret;
+
+		ret = func((struct rte_ring *) te->data, arg);
+		if (ret < 0)
+			break;
+
+		if (ret > 0) {
+			TAILQ_REMOVE(ring_list, te, next);
+			rte_free((void *)(uintptr_t)te);
+		}
+	}
+
+	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+}
diff --git a/lib/librte_ring/rte_ring.h b/lib/librte_ring/rte_ring.h
index af5444a9f..ed6a44773 100644
--- a/lib/librte_ring/rte_ring.h
+++ b/lib/librte_ring/rte_ring.h
@@ -769,6 +769,23 @@ rte_ring_get_capacity(const struct rte_ring *r)
  */
 void rte_ring_list_dump(FILE *f);
 
+/**
+ * Walk the list of ring entries and call the function provided.
+ *
+ * @param func
+ *   The function to call for each ring entry using the following prototype.
+ *     If the function returns < 0 then stop walking the list.
+ *     If the function returns > 0 then remove and free the tailq entry.
+ *       and do not free the tailq entry ring pointer. Leave ring pointer
+ *       to the function to handle the ring pointer freeing.
+ *     If the function returns 0 then continue walking the list.
+ *   The RTE_EAL_TAILQ_RWLOCK write lock is taken while walking the list.
+ * @param arg
+ *   argument for the call to function.
+ */
+void __rte_experimental
+rte_ring_walk(int (*func)(struct rte_ring *r, void *arg), void *arg);
+
 /**
  * Search a ring from its name
  *
diff --git a/lib/librte_ring/rte_ring_version.map b/lib/librte_ring/rte_ring_version.map
index d935efd0d..17c05c1b2 100644
--- a/lib/librte_ring/rte_ring_version.map
+++ b/lib/librte_ring/rte_ring_version.map
@@ -17,3 +17,10 @@ DPDK_2.2 {
 	rte_ring_free;
 
 } DPDK_2.0;
+
+EXPERIMENTAL {
+	global:
+
+	rte_ring_walk;
+};
+
-- 
2.17.1