1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
11 #include <rte_compat.h>
12 #include <rte_service.h>
13 #include <rte_service_component.h>
16 #include <rte_lcore.h>
17 #include <rte_common.h>
18 #include <rte_debug.h>
19 #include <rte_cycles.h>
20 #include <rte_atomic.h>
21 #include <rte_memory.h>
22 #include <rte_malloc.h>
23 #include <rte_spinlock.h>
25 #include "eal_private.h"
27 #define RTE_SERVICE_NUM_MAX 64
29 #define SERVICE_F_REGISTERED (1 << 0)
30 #define SERVICE_F_STATS_ENABLED (1 << 1)
31 #define SERVICE_F_START_CHECK (1 << 2)
33 /* runstates for services and lcores, denoting if they are active or not */
34 #define RUNSTATE_STOPPED 0
35 #define RUNSTATE_RUNNING 1
37 /* internal representation of a service */
38 struct rte_service_spec_impl {
39 /* public part of the struct */
40 struct rte_service_spec spec;
42 /* spin lock that when set indicates a service core is currently
43 * running this service callback. When not set, a core may take the
44 * lock and then run the service callback.
46 rte_spinlock_t execute_lock;
48 /* API set/get-able variables */
51 uint8_t internal_flags;
53 /* per service statistics */
54 /* Indicates how many cores the service is mapped to run on.
55 * It does not indicate the number of cores the service is running
58 uint32_t num_mapped_cores;
60 uint64_t cycles_spent;
61 } __rte_cache_aligned;
63 /* the internal values of a service core */
65 /* map of services IDs are run on this core */
66 uint64_t service_mask;
67 uint8_t runstate; /* running or stopped */
68 uint8_t is_service_core; /* set if core is currently a service core */
69 uint8_t service_active_on_lcore[RTE_SERVICE_NUM_MAX];
71 uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
72 } __rte_cache_aligned;
74 static uint32_t rte_service_count;
75 static struct rte_service_spec_impl *rte_services;
76 static struct core_state *lcore_states;
77 static uint32_t rte_service_library_initialized;
80 rte_service_init(void)
82 if (rte_service_library_initialized) {
84 "service library init() called, init flag %d\n",
85 rte_service_library_initialized);
89 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
90 sizeof(struct rte_service_spec_impl),
93 RTE_LOG(ERR, EAL, "error allocating rte services array\n");
97 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
98 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
100 RTE_LOG(ERR, EAL, "error allocating core states array\n");
106 struct rte_config *cfg = rte_eal_get_configuration();
107 for (i = 0; i < RTE_MAX_LCORE; i++) {
108 if (lcore_config[i].core_role == ROLE_SERVICE) {
109 if ((unsigned int)i == cfg->master_lcore)
111 rte_service_lcore_add(i);
116 rte_service_library_initialized = 1;
119 rte_free(rte_services);
120 rte_free(lcore_states);
125 rte_service_finalize(void)
127 if (!rte_service_library_initialized)
130 rte_service_lcore_reset_all();
131 rte_eal_mp_wait_lcore();
133 rte_free(rte_services);
134 rte_free(lcore_states);
136 rte_service_library_initialized = 0;
139 /* returns 1 if service is registered and has not been unregistered
140 * Returns 0 if service never registered, or has been unregistered
143 service_valid(uint32_t id)
145 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
148 static struct rte_service_spec_impl *
149 service_get(uint32_t id)
151 return &rte_services[id];
154 /* validate ID and retrieve service pointer, or return error value */
155 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
156 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
158 service = &rte_services[id]; \
161 /* returns 1 if statistics should be collected for service
162 * Returns 0 if statistics should not be collected for service
165 service_stats_enabled(struct rte_service_spec_impl *impl)
167 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
171 service_mt_safe(struct rte_service_spec_impl *s)
173 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
177 rte_service_set_stats_enable(uint32_t id, int32_t enabled)
179 struct rte_service_spec_impl *s;
180 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
183 s->internal_flags |= SERVICE_F_STATS_ENABLED;
185 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
191 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
193 struct rte_service_spec_impl *s;
194 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
197 s->internal_flags |= SERVICE_F_START_CHECK;
199 s->internal_flags &= ~(SERVICE_F_START_CHECK);
205 rte_service_get_count(void)
207 return rte_service_count;
211 rte_service_get_by_name(const char *name, uint32_t *service_id)
217 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
218 if (service_valid(i) &&
219 strcmp(name, rte_services[i].spec.name) == 0) {
229 rte_service_get_name(uint32_t id)
231 struct rte_service_spec_impl *s;
232 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
237 rte_service_probe_capability(uint32_t id, uint32_t capability)
239 struct rte_service_spec_impl *s;
240 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
241 return !!(s->spec.capabilities & capability);
245 rte_service_component_register(const struct rte_service_spec *spec,
249 int32_t free_slot = -1;
251 if (spec->callback == NULL || strlen(spec->name) == 0)
254 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
255 if (!service_valid(i)) {
261 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
264 struct rte_service_spec_impl *s = &rte_services[free_slot];
266 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
277 rte_service_component_unregister(uint32_t id)
280 struct rte_service_spec_impl *s;
281 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
285 s->internal_flags &= ~(SERVICE_F_REGISTERED);
287 /* clear the run-bit in all cores */
288 for (i = 0; i < RTE_MAX_LCORE; i++)
289 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
291 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
297 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
299 struct rte_service_spec_impl *s;
300 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
302 /* comp_runstate act as the guard variable. Use store-release
303 * memory order. This synchronizes with load-acquire in
304 * service_run and service_runstate_get function.
307 __atomic_store_n(&s->comp_runstate, RUNSTATE_RUNNING,
310 __atomic_store_n(&s->comp_runstate, RUNSTATE_STOPPED,
317 rte_service_runstate_set(uint32_t id, uint32_t runstate)
319 struct rte_service_spec_impl *s;
320 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
322 /* app_runstate act as the guard variable. Use store-release
323 * memory order. This synchronizes with load-acquire in
324 * service_run runstate_get function.
327 __atomic_store_n(&s->app_runstate, RUNSTATE_RUNNING,
330 __atomic_store_n(&s->app_runstate, RUNSTATE_STOPPED,
337 rte_service_runstate_get(uint32_t id)
339 struct rte_service_spec_impl *s;
340 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
342 /* comp_runstate and app_runstate act as the guard variables.
343 * Use load-acquire memory order. This synchronizes with
344 * store-release in service state set functions.
346 if (__atomic_load_n(&s->comp_runstate, __ATOMIC_ACQUIRE) ==
348 __atomic_load_n(&s->app_runstate, __ATOMIC_ACQUIRE) ==
350 int check_disabled = !(s->internal_flags &
351 SERVICE_F_START_CHECK);
352 int lcore_mapped = (__atomic_load_n(&s->num_mapped_cores,
353 __ATOMIC_RELAXED) > 0);
355 return (check_disabled | lcore_mapped);
362 service_runner_do_callback(struct rte_service_spec_impl *s,
363 struct core_state *cs, uint32_t service_idx)
365 void *userdata = s->spec.callback_userdata;
367 if (service_stats_enabled(s)) {
368 uint64_t start = rte_rdtsc();
369 s->spec.callback(userdata);
370 uint64_t end = rte_rdtsc();
371 s->cycles_spent += end - start;
372 cs->calls_per_service[service_idx]++;
375 s->spec.callback(userdata);
379 /* Expects the service 's' is valid. */
381 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask,
382 struct rte_service_spec_impl *s, uint32_t serialize_mt_unsafe)
387 /* comp_runstate and app_runstate act as the guard variables.
388 * Use load-acquire memory order. This synchronizes with
389 * store-release in service state set functions.
391 if (__atomic_load_n(&s->comp_runstate, __ATOMIC_ACQUIRE) !=
393 __atomic_load_n(&s->app_runstate, __ATOMIC_ACQUIRE) !=
395 !(service_mask & (UINT64_C(1) << i))) {
396 cs->service_active_on_lcore[i] = 0;
400 cs->service_active_on_lcore[i] = 1;
402 if ((service_mt_safe(s) == 0) && (serialize_mt_unsafe == 1)) {
403 if (!rte_spinlock_trylock(&s->execute_lock))
406 service_runner_do_callback(s, cs, i);
407 rte_spinlock_unlock(&s->execute_lock);
409 service_runner_do_callback(s, cs, i);
415 rte_service_may_be_active(uint32_t id)
417 uint32_t ids[RTE_MAX_LCORE] = {0};
418 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
421 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))
424 for (i = 0; i < lcore_count; i++) {
425 if (lcore_states[ids[i]].service_active_on_lcore[id])
433 rte_service_run_iter_on_app_lcore(uint32_t id, uint32_t serialize_mt_unsafe)
435 struct core_state *cs = &lcore_states[rte_lcore_id()];
436 struct rte_service_spec_impl *s;
438 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
440 /* Increment num_mapped_cores to reflect that this core is
441 * now mapped capable of running the service.
443 __atomic_add_fetch(&s->num_mapped_cores, 1, __ATOMIC_RELAXED);
445 int ret = service_run(id, cs, UINT64_MAX, s, serialize_mt_unsafe);
447 __atomic_sub_fetch(&s->num_mapped_cores, 1, __ATOMIC_RELAXED);
453 service_runner_func(void *arg)
457 const int lcore = rte_lcore_id();
458 struct core_state *cs = &lcore_states[lcore];
460 /* runstate act as the guard variable. Use load-acquire
461 * memory order here to synchronize with store-release
462 * in runstate update functions.
464 while (__atomic_load_n(&cs->runstate, __ATOMIC_ACQUIRE) ==
466 const uint64_t service_mask = cs->service_mask;
468 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
469 if (!service_valid(i))
471 /* return value ignored as no change to code flow */
472 service_run(i, cs, service_mask, service_get(i), 1);
482 rte_service_lcore_count(void)
486 for (i = 0; i < RTE_MAX_LCORE; i++)
487 count += lcore_states[i].is_service_core;
492 rte_service_lcore_list(uint32_t array[], uint32_t n)
494 uint32_t count = rte_service_lcore_count();
503 for (i = 0; i < RTE_MAX_LCORE; i++) {
504 struct core_state *cs = &lcore_states[i];
505 if (cs->is_service_core) {
515 rte_service_lcore_count_services(uint32_t lcore)
517 if (lcore >= RTE_MAX_LCORE)
520 struct core_state *cs = &lcore_states[lcore];
521 if (!cs->is_service_core)
524 return __builtin_popcountll(cs->service_mask);
528 rte_service_start_with_defaults(void)
530 /* create a default mapping from cores to services, then start the
531 * services to make them transparent to unaware applications.
535 uint32_t count = rte_service_get_count();
537 int32_t lcore_iter = 0;
538 uint32_t ids[RTE_MAX_LCORE] = {0};
539 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
541 if (lcore_count == 0)
544 for (i = 0; (int)i < lcore_count; i++)
545 rte_service_lcore_start(ids[i]);
547 for (i = 0; i < count; i++) {
548 /* do 1:1 core mapping here, with each service getting
549 * assigned a single core by default. Adding multiple services
550 * should multiplex to a single core, or 1:1 if there are the
551 * same amount of services as service-cores
553 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
558 if (lcore_iter >= lcore_count)
561 ret = rte_service_runstate_set(i, 1);
570 service_update(uint32_t sid, uint32_t lcore, uint32_t *set, uint32_t *enabled)
572 /* validate ID, or return error value */
573 if (sid >= RTE_SERVICE_NUM_MAX || !service_valid(sid) ||
574 lcore >= RTE_MAX_LCORE || !lcore_states[lcore].is_service_core)
577 uint64_t sid_mask = UINT64_C(1) << sid;
579 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
582 if (*set && !lcore_mapped) {
583 lcore_states[lcore].service_mask |= sid_mask;
584 __atomic_add_fetch(&rte_services[sid].num_mapped_cores,
585 1, __ATOMIC_RELAXED);
587 if (!*set && lcore_mapped) {
588 lcore_states[lcore].service_mask &= ~(sid_mask);
589 __atomic_sub_fetch(&rte_services[sid].num_mapped_cores,
590 1, __ATOMIC_RELAXED);
595 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
601 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
603 uint32_t on = enabled > 0;
604 return service_update(id, lcore, &on, 0);
608 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
611 int ret = service_update(id, lcore, 0, &enabled);
618 set_lcore_state(uint32_t lcore, int32_t state)
620 /* mark core state in hugepage backed config */
621 struct rte_config *cfg = rte_eal_get_configuration();
622 cfg->lcore_role[lcore] = state;
624 /* mark state in process local lcore_config */
625 lcore_config[lcore].core_role = state;
627 /* update per-lcore optimized state tracking */
628 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
632 rte_service_lcore_reset_all(void)
634 /* loop over cores, reset all to mask 0 */
636 for (i = 0; i < RTE_MAX_LCORE; i++) {
637 if (lcore_states[i].is_service_core) {
638 lcore_states[i].service_mask = 0;
639 set_lcore_state(i, ROLE_RTE);
640 /* runstate act as guard variable Use
641 * store-release memory order here to synchronize
642 * with load-acquire in runstate read functions.
644 __atomic_store_n(&lcore_states[i].runstate,
645 RUNSTATE_STOPPED, __ATOMIC_RELEASE);
648 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
649 __atomic_store_n(&rte_services[i].num_mapped_cores, 0,
656 rte_service_lcore_add(uint32_t lcore)
658 if (lcore >= RTE_MAX_LCORE)
660 if (lcore_states[lcore].is_service_core)
663 set_lcore_state(lcore, ROLE_SERVICE);
665 /* ensure that after adding a core the mask and state are defaults */
666 lcore_states[lcore].service_mask = 0;
667 /* Use store-release memory order here to synchronize with
668 * load-acquire in runstate read functions.
670 __atomic_store_n(&lcore_states[lcore].runstate, RUNSTATE_STOPPED,
673 return rte_eal_wait_lcore(lcore);
677 rte_service_lcore_del(uint32_t lcore)
679 if (lcore >= RTE_MAX_LCORE)
682 struct core_state *cs = &lcore_states[lcore];
683 if (!cs->is_service_core)
686 /* runstate act as the guard variable. Use load-acquire
687 * memory order here to synchronize with store-release
688 * in runstate update functions.
690 if (__atomic_load_n(&cs->runstate, __ATOMIC_ACQUIRE) !=
694 set_lcore_state(lcore, ROLE_RTE);
701 rte_service_lcore_start(uint32_t lcore)
703 if (lcore >= RTE_MAX_LCORE)
706 struct core_state *cs = &lcore_states[lcore];
707 if (!cs->is_service_core)
710 /* runstate act as the guard variable. Use load-acquire
711 * memory order here to synchronize with store-release
712 * in runstate update functions.
714 if (__atomic_load_n(&cs->runstate, __ATOMIC_ACQUIRE) ==
718 /* set core to run state first, and then launch otherwise it will
719 * return immediately as runstate keeps it in the service poll loop
721 /* Use load-acquire memory order here to synchronize with
722 * store-release in runstate update functions.
724 __atomic_store_n(&cs->runstate, RUNSTATE_RUNNING, __ATOMIC_RELEASE);
726 int ret = rte_eal_remote_launch(service_runner_func, 0, lcore);
727 /* returns -EBUSY if the core is already launched, 0 on success */
732 rte_service_lcore_stop(uint32_t lcore)
734 if (lcore >= RTE_MAX_LCORE)
737 /* runstate act as the guard variable. Use load-acquire
738 * memory order here to synchronize with store-release
739 * in runstate update functions.
741 if (__atomic_load_n(&lcore_states[lcore].runstate, __ATOMIC_ACQUIRE) ==
746 uint64_t service_mask = lcore_states[lcore].service_mask;
747 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
748 int32_t enabled = service_mask & (UINT64_C(1) << i);
749 int32_t service_running = rte_service_runstate_get(i);
750 int32_t only_core = (1 ==
751 __atomic_load_n(&rte_services[i].num_mapped_cores,
754 /* if the core is mapped, and the service is running, and this
755 * is the only core that is mapped, the service would cease to
756 * run if this core stopped, so fail instead.
758 if (enabled && service_running && only_core)
762 /* Use store-release memory order here to synchronize with
763 * load-acquire in runstate read functions.
765 __atomic_store_n(&lcore_states[lcore].runstate, RUNSTATE_STOPPED,
772 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint64_t *attr_value)
774 struct rte_service_spec_impl *s;
775 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
781 case RTE_SERVICE_ATTR_CYCLES:
782 *attr_value = s->cycles_spent;
784 case RTE_SERVICE_ATTR_CALL_COUNT:
785 *attr_value = s->calls;
793 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
794 uint64_t *attr_value)
796 struct core_state *cs;
798 if (lcore >= RTE_MAX_LCORE || !attr_value)
801 cs = &lcore_states[lcore];
802 if (!cs->is_service_core)
806 case RTE_SERVICE_LCORE_ATTR_LOOPS:
807 *attr_value = cs->loops;
815 service_dump_one(FILE *f, struct rte_service_spec_impl *s, uint32_t reset)
817 /* avoid divide by zero */
831 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
832 PRIu64"\tavg: %"PRIu64"\n",
833 s->spec.name, service_stats_enabled(s), s->calls,
834 s->cycles_spent, s->cycles_spent / calls);
838 rte_service_attr_reset_all(uint32_t id)
840 struct rte_service_spec_impl *s;
841 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
844 service_dump_one(NULL, s, reset);
849 rte_service_lcore_attr_reset_all(uint32_t lcore)
851 struct core_state *cs;
853 if (lcore >= RTE_MAX_LCORE)
856 cs = &lcore_states[lcore];
857 if (!cs->is_service_core)
866 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
869 struct core_state *cs = &lcore_states[lcore];
871 fprintf(f, "%02d\t", lcore);
872 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
873 if (!service_valid(i))
875 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
877 cs->calls_per_service[i] = 0;
883 rte_service_dump(FILE *f, uint32_t id)
886 int print_one = (id != UINT32_MAX);
888 /* print only the specified service */
890 struct rte_service_spec_impl *s;
891 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
892 fprintf(f, "Service %s Summary\n", s->spec.name);
894 service_dump_one(f, s, reset);
898 /* print all services, as UINT32_MAX was passed as id */
899 fprintf(f, "Services Summary\n");
900 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
901 if (!service_valid(i))
904 service_dump_one(f, &rte_services[i], reset);
907 fprintf(f, "Service Cores Summary\n");
908 for (i = 0; i < RTE_MAX_LCORE; i++) {
909 if (lcore_config[i].core_role != ROLE_SERVICE)
913 service_dump_calls_per_lcore(f, i, reset);