service: add function to run on app lcore
[dpdk.git] / lib / librte_eal / common / rte_service.c
index ddcf3a8..4e27f75 100644 (file)
@@ -71,7 +71,8 @@ struct rte_service_spec_impl {
        rte_atomic32_t execute_lock;
 
        /* API set/get-able variables */
-       int32_t runstate;
+       int8_t app_runstate;
+       int8_t comp_runstate;
        uint8_t internal_flags;
 
        /* per service statistics */
@@ -272,6 +273,21 @@ rte_service_component_unregister(uint32_t id)
        return 0;
 }
 
+int32_t
+rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
+{
+       struct rte_service_spec_impl *s;
+       SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
+
+       if (runstate)
+               s->comp_runstate = RUNSTATE_RUNNING;
+       else
+               s->comp_runstate = RUNSTATE_STOPPED;
+
+       rte_smp_wmb();
+       return 0;
+}
+
 int32_t
 rte_service_runstate_set(uint32_t id, uint32_t runstate)
 {
@@ -279,9 +295,9 @@ rte_service_runstate_set(uint32_t id, uint32_t runstate)
        SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
 
        if (runstate)
-               s->runstate = RUNSTATE_RUNNING;
+               s->app_runstate = RUNSTATE_RUNNING;
        else
-               s->runstate = RUNSTATE_STOPPED;
+               s->app_runstate = RUNSTATE_STOPPED;
 
        rte_smp_wmb();
        return 0;
@@ -292,8 +308,10 @@ rte_service_runstate_get(uint32_t id)
 {
        struct rte_service_spec_impl *s;
        SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
-
-       return (s->runstate == RUNSTATE_RUNNING) && (s->num_mapped_cores > 0);
+       rte_smp_rmb();
+       return (s->app_runstate == RUNSTATE_RUNNING) &&
+               (s->comp_runstate == RUNSTATE_RUNNING) &&
+               (s->num_mapped_cores > 0);
 }
 
 static inline void
@@ -313,6 +331,42 @@ rte_service_runner_do_callback(struct rte_service_spec_impl *s,
                s->spec.callback(userdata);
 }
 
+
+static inline int32_t
+service_run(uint32_t i, struct core_state *cs, uint64_t service_mask)
+{
+       if (!service_valid(i))
+               return -EINVAL;
+       struct rte_service_spec_impl *s = &rte_services[i];
+       if (s->comp_runstate != RUNSTATE_RUNNING ||
+                       s->app_runstate != RUNSTATE_RUNNING ||
+                       !(service_mask & (UINT64_C(1) << i)))
+               return -ENOEXEC;
+
+       /* check do we need cmpset, if MT safe or <= 1 core
+        * mapped, atomic ops are not required.
+        */
+       const int use_atomics = (service_mt_safe(s) == 0) &&
+                               (s->num_mapped_cores > 1);
+       if (use_atomics) {
+               if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
+                       return -EBUSY;
+
+               rte_service_runner_do_callback(s, cs, i);
+               rte_atomic32_clear(&s->execute_lock);
+       } else
+               rte_service_runner_do_callback(s, cs, i);
+
+       return 0;
+}
+
+int32_t rte_service_run_iter_on_app_lcore(uint32_t id)
+{
+       /* run service on calling core, using all-ones as the service mask */
+       struct core_state *cs = &lcore_states[rte_lcore_id()];
+       return service_run(id, cs, UINT64_MAX);
+}
+
 static int32_t
 rte_service_runner_func(void *arg)
 {
@@ -325,26 +379,8 @@ rte_service_runner_func(void *arg)
                const uint64_t service_mask = cs->service_mask;
 
                for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
-                       if (!service_valid(i))
-                               continue;
-                       struct rte_service_spec_impl *s = &rte_services[i];
-                       if (s->runstate != RUNSTATE_RUNNING ||
-                                       !(service_mask & (UINT64_C(1) << i)))
-                               continue;
-
-                       /* check do we need cmpset, if MT safe or <= 1 core
-                        * mapped, atomic ops are not required.
-                        */
-                       const int use_atomics = (service_mt_safe(s) == 0) &&
-                                               (s->num_mapped_cores > 1);
-                       if (use_atomics) {
-                               uint32_t *lock = (uint32_t *)&s->execute_lock;
-                               if (rte_atomic32_cmpset(lock, 0, 1)) {
-                                       rte_service_runner_do_callback(s, cs, i);
-                                       rte_atomic32_clear(&s->execute_lock);
-                               }
-                       } else
-                               rte_service_runner_do_callback(s, cs, i);
+                       /* return value ignored as no change to code flow */
+                       service_run(i, cs, service_mask);
                }
 
                rte_smp_rmb();
@@ -412,7 +448,7 @@ rte_service_start_with_defaults(void)
        uint32_t count = rte_service_get_count();
 
        int32_t lcore_iter = 0;
-       uint32_t ids[RTE_MAX_LCORE];
+       uint32_t ids[RTE_MAX_LCORE] = {0};
        int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
 
        if (lcore_count == 0)
@@ -606,11 +642,10 @@ rte_service_lcore_stop(uint32_t lcore)
                return -EALREADY;
 
        uint32_t i;
+       uint64_t service_mask = lcore_states[lcore].service_mask;
        for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
-               int32_t enabled =
-                       lcore_states[i].service_mask & (UINT64_C(1) << i);
-               int32_t service_running = rte_services[i].runstate !=
-                                               RUNSTATE_STOPPED;
+               int32_t enabled = service_mask & (UINT64_C(1) << i);
+               int32_t service_running = rte_service_runstate_get(i);
                int32_t only_core = rte_services[i].num_mapped_cores == 1;
 
                /* if the core is mapped, and the service is running, and this