service: use id in get by name function
authorHarry van Haaren <harry.van.haaren@intel.com>
Mon, 21 Aug 2017 12:58:08 +0000 (13:58 +0100)
committerThomas Monjalon <thomas@monjalon.net>
Fri, 15 Sep 2017 11:46:13 +0000 (13:46 +0200)
This commit reworks the service_get_by_name() function to
accept an integer, and removes the service_get_by_id() function.

All functions now accept an integer argument representing the
service, so it is no longer required to expose the service_spec
pointers to the application.

Signed-off-by: Harry van Haaren <harry.van.haaren@intel.com>
Acked-by: Pavan Nikhilesh <pbhagavatula@caviumnetworks.com>
lib/librte_eal/bsdapp/eal/rte_eal_version.map
lib/librte_eal/common/include/rte_service.h
lib/librte_eal/common/rte_service.c
lib/librte_eal/linuxapp/eal/rte_eal_version.map
test/test/test_service_cores.c

index a7aa481..b705e28 100644 (file)
@@ -218,6 +218,7 @@ EXPERIMENTAL {
        rte_service_get_by_id;
        rte_service_get_by_name;
        rte_service_get_count;
+       rte_service_get_name;
        rte_service_lcore_add;
        rte_service_lcore_count;
        rte_service_lcore_count_services;
index 59b21c6..21da739 100644 (file)
@@ -61,9 +61,6 @@ extern "C" {
 
 #include <rte_lcore.h>
 
-/* forward declaration only. Definition in rte_service_private.h */
-struct rte_service_spec;
-
 #define RTE_SERVICE_NAME_MAX 32
 
 /* Capabilities of a service.
@@ -89,40 +86,32 @@ struct rte_service_spec;
  */
 uint32_t rte_service_get_count(void);
 
-
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice
  *
- * Return the specification of a service by integer id.
- *
- * This function provides the specification of a service. This can be used by
- * the application to understand what the service represents. The service
- * must not be modified by the application directly, only passed to the various
- * rte_service_* functions.
- *
- * @param id The integer id of the service to retrieve
- * @retval non-zero A valid pointer to the service_spec
- * @retval NULL Invalid *id* provided.
- */
-struct rte_service_spec *rte_service_get_by_id(uint32_t id);
-
-/**
- * @warning
- * @b EXPERIMENTAL: this API may change without prior notice
+ * Return the id of a service by name.
  *
- * Return the specification of a service by name.
+ * This function provides the id of the service using the service name as
+ * lookup key. The service id is to be passed to other functions in the
+ * rte_service_* API.
  *
- * This function provides the specification of a service using the service name
- * as lookup key. This can be used by the application to understand what the
- * service represents. The service must not be modified by the application
- * directly, only passed to the various rte_service_* functions.
+ * Example usage:
+ * @code
+ *      uint32_t service_id;
+ *      int32_t ret = rte_service_get_by_name("service_X", &service_id);
+ *      if (ret) {
+ *              // handle error
+ *      }
+ * @endcode
  *
  * @param name The name of the service to retrieve
- * @retval non-zero A valid pointer to the service_spec
- * @retval NULL Invalid *name* provided.
+ * @param[out] service_id A pointer to a uint32_t, to be filled in with the id.
+ * @retval 0 Success. The service id is provided in *service_id*.
+ * @retval -EINVAL Null *service_id* pointer provided
+ * @retval -ENODEV No such service registered
  */
-struct rte_service_spec *rte_service_get_by_name(const char *name);
+int32_t rte_service_get_by_name(const char *name, uint32_t *service_id);
 
 /**
  * @warning
index e650d96..06fe6cc 100644 (file)
@@ -185,29 +185,21 @@ rte_service_get_count(void)
        return rte_service_count;
 }
 
-struct rte_service_spec *
-rte_service_get_by_id(uint32_t id)
+int32_t rte_service_get_by_name(const char *name, uint32_t *service_id)
 {
-       struct rte_service_spec *service = NULL;
-       if (id < rte_service_count)
-               service = (struct rte_service_spec *)&rte_services[id];
-
-       return service;
-}
+       if (!service_id)
+               return -EINVAL;
 
-struct rte_service_spec *rte_service_get_by_name(const char *name)
-{
-       struct rte_service_spec *service = NULL;
        int i;
        for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
                if (service_valid(i) &&
                                strcmp(name, rte_services[i].spec.name) == 0) {
-                       service = (struct rte_service_spec *)&rte_services[i];
-                       break;
+                       *service_id = i;
+                       return 0;
                }
        }
 
-       return service;
+       return -ENODEV;
 }
 
 const char *
@@ -420,10 +412,6 @@ rte_service_start_with_defaults(void)
                rte_service_lcore_start(ids[i]);
 
        for (i = 0; i < count; i++) {
-               struct rte_service_spec *s = rte_service_get_by_id(i);
-               if (!s)
-                       return -EINVAL;
-
                /* do 1:1 core mapping here, with each service getting
                 * assigned a single core by default. Adding multiple services
                 * should multiplex to a single core, or 1:1 if there are the
index ffbc655..f94bdb1 100644 (file)
@@ -223,6 +223,7 @@ EXPERIMENTAL {
        rte_service_get_by_id;
        rte_service_get_by_name;
        rte_service_get_count;
+       rte_service_get_name;
        rte_service_lcore_add;
        rte_service_lcore_count;
        rte_service_lcore_count_services;
index e650b20..5ae7b20 100644 (file)
@@ -178,9 +178,13 @@ service_get_by_name(void)
 {
        unregister_all();
 
-       /* ensure with no services registered returns NULL */
-       TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
-                       "Service get by name should return NULL");
+       uint32_t sid;
+       TEST_ASSERT_EQUAL(-ENODEV,
+                       rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
+                       "get by name with invalid name should return -ENODEV");
+       TEST_ASSERT_EQUAL(-EINVAL,
+                       rte_service_get_by_name(DUMMY_SERVICE_NAME, 0x0),
+                       "get by name with NULL ptr should return -ENODEV");
 
        /* register service */
        struct rte_service_spec service;
@@ -196,16 +200,19 @@ service_get_by_name(void)
        TEST_ASSERT_EQUAL(0, rte_service_component_register(&service, NULL),
                        "Failed to register valid service");
 
-       /* ensure with dummy services registered returns same ptr as ID */
-       struct rte_service_spec *s_by_id = rte_service_get_by_id(0);
-       TEST_ASSERT_EQUAL(s_by_id, rte_service_get_by_name(DUMMY_SERVICE_NAME),
-                       "Service get_by_name should equal get_by_id()");
+       /* we unregistered all service, now registering 1, should be id 0 */
+       uint32_t service_id_as_expected = 0;
+       TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
+                       "Service get_by_name should return 0 on valid inputs");
+       TEST_ASSERT_EQUAL(service_id_as_expected, sid,
+                       "Service get_by_name should equal expected id");
 
        unregister_all();
 
        /* ensure after unregister, get_by_name returns NULL */
-       TEST_ASSERT_EQUAL(0, rte_service_get_by_name(DUMMY_SERVICE_NAME),
-                       "get by name should return NULL after unregister");
+       TEST_ASSERT_EQUAL(-ENODEV,
+                       rte_service_get_by_name(DUMMY_SERVICE_NAME, &sid),
+                       "get by name should return -ENODEV after unregister");
 
        return TEST_SUCCESS;
 }
@@ -249,9 +256,8 @@ service_probe_capability(void)
 static int
 service_name(void)
 {
-       struct rte_service_spec *service = rte_service_get_by_id(0);
-
-       int equal = strcmp(service->name, DUMMY_SERVICE_NAME);
+       const char *name = rte_service_get_name(0);
+       int equal = strcmp(name, DUMMY_SERVICE_NAME);
        TEST_ASSERT_EQUAL(0, equal, "Error: Service name not correct");
 
        return unregister_all();