From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <harry.van.haaren@intel.com>
Received: from mga05.intel.com (mga05.intel.com [192.55.52.43])
 by dpdk.org (Postfix) with ESMTP id 6AF067CFC
 for <dev@dpdk.org>; Mon, 21 Aug 2017 14:58:27 +0200 (CEST)
Received: from fmsmga002.fm.intel.com ([10.253.24.26])
 by fmsmga105.fm.intel.com with ESMTP; 21 Aug 2017 05:58:26 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.41,409,1498546800"; d="scan'208";a="1208383259"
Received: from silpixa00398672.ir.intel.com ([10.237.223.128])
 by fmsmga002.fm.intel.com with ESMTP; 21 Aug 2017 05:58:25 -0700
From: Harry van Haaren <harry.van.haaren@intel.com>
To: dev@dpdk.org
Cc: Harry van Haaren <harry.van.haaren@intel.com>
Date: Mon, 21 Aug 2017 13:58:02 +0100
Message-Id: <1503320296-51122-2-git-send-email-harry.van.haaren@intel.com>
X-Mailer: git-send-email 2.7.4
In-Reply-To: <1503320296-51122-1-git-send-email-harry.van.haaren@intel.com>
References: <1502800360-15782-1-git-send-email-harry.van.haaren@intel.com>
 <1503320296-51122-1-git-send-email-harry.van.haaren@intel.com>
Subject: [dpdk-dev] [PATCH v2 01/15] service: rework probe and get name to
	use ids
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Mon, 21 Aug 2017 12:58:27 -0000

This commit adds a macro to easily validate a service ID, and then
lookup the service pointer, or return a user-specified error code.
This marco will be heavily used in the following patches as it will
be ID based instead of pointer-based.

The probe_capability function is reworked to use an integer ID instead
of a pointer. Rework the service_get_name() function is updated to use
IDs. Unit tests are updated to keep things compiling after each commit.

Signed-off-by: Harry van Haaren <harry.van.haaren@intel.com>
---
 lib/librte_eal/common/include/rte_service.h |  5 ++---
 lib/librte_eal/common/rte_service.c         | 20 +++++++++++++++-----
 test/test/test_service_cores.c              |  7 +++----
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/lib/librte_eal/common/include/rte_service.h b/lib/librte_eal/common/include/rte_service.h
index 7c6f738..bed1a61 100644
--- a/lib/librte_eal/common/include/rte_service.h
+++ b/lib/librte_eal/common/include/rte_service.h
@@ -133,7 +133,7 @@ struct rte_service_spec *rte_service_get_by_name(const char *name);
  * @return A pointer to the name of the service. The returned pointer remains
  *         in ownership of the service, and the application must not free it.
  */
-const char *rte_service_get_name(const struct rte_service_spec *service);
+const char *rte_service_get_name(uint32_t id);
 
 /**
  * @warning
@@ -146,8 +146,7 @@ const char *rte_service_get_name(const struct rte_service_spec *service);
  * @retval 1 Capability supported by this service instance
  * @retval 0 Capability not supported by this service instance
  */
-int32_t rte_service_probe_capability(const struct rte_service_spec *service,
-				     uint32_t capability);
+int32_t rte_service_probe_capability(uint32_t id, uint32_t capability);
 
 /**
  * @warning
diff --git a/lib/librte_eal/common/rte_service.c b/lib/librte_eal/common/rte_service.c
index 7efb76d..c969406 100644
--- a/lib/librte_eal/common/rte_service.c
+++ b/lib/librte_eal/common/rte_service.c
@@ -144,6 +144,13 @@ service_valid(uint32_t id)
 	return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
 }
 
+/* validate ID and retrieve service pointer, or return error value */
+#define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do {          \
+	if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))            \
+		return retval;                                          \
+	service = &rte_services[id];                                    \
+} while (0)
+
 /* returns 1 if statistics should be colleced for service
  * Returns 0 if statistics should not be collected for service
  */
@@ -207,16 +214,19 @@ struct rte_service_spec *rte_service_get_by_name(const char *name)
 }
 
 const char *
-rte_service_get_name(const struct rte_service_spec *service)
+rte_service_get_name(uint32_t id)
 {
-	return service->name;
+	struct rte_service_spec_impl *s;
+	SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
+	return s->spec.name;
 }
 
 int32_t
-rte_service_probe_capability(const struct rte_service_spec *service,
-			     uint32_t capability)
+rte_service_probe_capability(uint32_t id, uint32_t capability)
 {
-	return service->capabilities & capability;
+	struct rte_service_spec_impl *s;
+	SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
+	return s->spec.capabilities & capability;
 }
 
 int32_t
diff --git a/test/test/test_service_cores.c b/test/test/test_service_cores.c
index 88fac8f..940bc62 100644
--- a/test/test/test_service_cores.c
+++ b/test/test/test_service_cores.c
@@ -225,8 +225,8 @@ service_probe_capability(void)
 			"Register of MT SAFE service failed");
 
 	/* verify flag is enabled */
-	struct rte_service_spec *s = rte_service_get_by_id(0);
-	int32_t mt = rte_service_probe_capability(s, RTE_SERVICE_CAP_MT_SAFE);
+	const uint32_t sid = 0;
+	int32_t mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
 	TEST_ASSERT_EQUAL(1, mt, "MT SAFE capability flag not set.");
 
 
@@ -239,8 +239,7 @@ service_probe_capability(void)
 			"Register of non-MT safe service failed");
 
 	/* verify flag is enabled */
-	s = rte_service_get_by_id(0);
-	mt = rte_service_probe_capability(s, RTE_SERVICE_CAP_MT_SAFE);
+	mt = rte_service_probe_capability(sid, RTE_SERVICE_CAP_MT_SAFE);
 	TEST_ASSERT_EQUAL(0, mt, "MT SAFE cap flag set on non MT SAFE service");
 
 	return unregister_all();
-- 
2.7.4