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);
478 lcore_config[lcore].state = WAIT;
484 rte_service_lcore_count(void)
488 for (i = 0; i < RTE_MAX_LCORE; i++)
489 count += lcore_states[i].is_service_core;
494 rte_service_lcore_list(uint32_t array[], uint32_t n)
496 uint32_t count = rte_service_lcore_count();
505 for (i = 0; i < RTE_MAX_LCORE; i++) {
506 struct core_state *cs = &lcore_states[i];
507 if (cs->is_service_core) {
517 rte_service_lcore_count_services(uint32_t lcore)
519 if (lcore >= RTE_MAX_LCORE)
522 struct core_state *cs = &lcore_states[lcore];
523 if (!cs->is_service_core)
526 return __builtin_popcountll(cs->service_mask);
530 rte_service_start_with_defaults(void)
532 /* create a default mapping from cores to services, then start the
533 * services to make them transparent to unaware applications.
537 uint32_t count = rte_service_get_count();
539 int32_t lcore_iter = 0;
540 uint32_t ids[RTE_MAX_LCORE] = {0};
541 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
543 if (lcore_count == 0)
546 for (i = 0; (int)i < lcore_count; i++)
547 rte_service_lcore_start(ids[i]);
549 for (i = 0; i < count; i++) {
550 /* do 1:1 core mapping here, with each service getting
551 * assigned a single core by default. Adding multiple services
552 * should multiplex to a single core, or 1:1 if there are the
553 * same amount of services as service-cores
555 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
560 if (lcore_iter >= lcore_count)
563 ret = rte_service_runstate_set(i, 1);
572 service_update(uint32_t sid, uint32_t lcore, uint32_t *set, uint32_t *enabled)
574 /* validate ID, or return error value */
575 if (sid >= RTE_SERVICE_NUM_MAX || !service_valid(sid) ||
576 lcore >= RTE_MAX_LCORE || !lcore_states[lcore].is_service_core)
579 uint64_t sid_mask = UINT64_C(1) << sid;
581 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
584 if (*set && !lcore_mapped) {
585 lcore_states[lcore].service_mask |= sid_mask;
586 __atomic_add_fetch(&rte_services[sid].num_mapped_cores,
587 1, __ATOMIC_RELAXED);
589 if (!*set && lcore_mapped) {
590 lcore_states[lcore].service_mask &= ~(sid_mask);
591 __atomic_sub_fetch(&rte_services[sid].num_mapped_cores,
592 1, __ATOMIC_RELAXED);
597 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
603 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
605 uint32_t on = enabled > 0;
606 return service_update(id, lcore, &on, 0);
610 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
613 int ret = service_update(id, lcore, 0, &enabled);
620 set_lcore_state(uint32_t lcore, int32_t state)
622 /* mark core state in hugepage backed config */
623 struct rte_config *cfg = rte_eal_get_configuration();
624 cfg->lcore_role[lcore] = state;
626 /* mark state in process local lcore_config */
627 lcore_config[lcore].core_role = state;
629 /* update per-lcore optimized state tracking */
630 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
634 rte_service_lcore_reset_all(void)
636 /* loop over cores, reset all to mask 0 */
638 for (i = 0; i < RTE_MAX_LCORE; i++) {
639 if (lcore_states[i].is_service_core) {
640 lcore_states[i].service_mask = 0;
641 set_lcore_state(i, ROLE_RTE);
642 /* runstate act as guard variable Use
643 * store-release memory order here to synchronize
644 * with load-acquire in runstate read functions.
646 __atomic_store_n(&lcore_states[i].runstate,
647 RUNSTATE_STOPPED, __ATOMIC_RELEASE);
650 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
651 __atomic_store_n(&rte_services[i].num_mapped_cores, 0,
658 rte_service_lcore_add(uint32_t lcore)
660 if (lcore >= RTE_MAX_LCORE)
662 if (lcore_states[lcore].is_service_core)
665 set_lcore_state(lcore, ROLE_SERVICE);
667 /* ensure that after adding a core the mask and state are defaults */
668 lcore_states[lcore].service_mask = 0;
669 /* Use store-release memory order here to synchronize with
670 * load-acquire in runstate read functions.
672 __atomic_store_n(&lcore_states[lcore].runstate, RUNSTATE_STOPPED,
675 return rte_eal_wait_lcore(lcore);
679 rte_service_lcore_del(uint32_t lcore)
681 if (lcore >= RTE_MAX_LCORE)
684 struct core_state *cs = &lcore_states[lcore];
685 if (!cs->is_service_core)
688 /* runstate act as the guard variable. Use load-acquire
689 * memory order here to synchronize with store-release
690 * in runstate update functions.
692 if (__atomic_load_n(&cs->runstate, __ATOMIC_ACQUIRE) !=
696 set_lcore_state(lcore, ROLE_RTE);
703 rte_service_lcore_start(uint32_t lcore)
705 if (lcore >= RTE_MAX_LCORE)
708 struct core_state *cs = &lcore_states[lcore];
709 if (!cs->is_service_core)
712 /* runstate act as the guard variable. Use load-acquire
713 * memory order here to synchronize with store-release
714 * in runstate update functions.
716 if (__atomic_load_n(&cs->runstate, __ATOMIC_ACQUIRE) ==
720 /* set core to run state first, and then launch otherwise it will
721 * return immediately as runstate keeps it in the service poll loop
723 /* Use load-acquire memory order here to synchronize with
724 * store-release in runstate update functions.
726 __atomic_store_n(&cs->runstate, RUNSTATE_RUNNING, __ATOMIC_RELEASE);
728 int ret = rte_eal_remote_launch(service_runner_func, 0, lcore);
729 /* returns -EBUSY if the core is already launched, 0 on success */
734 rte_service_lcore_stop(uint32_t lcore)
736 if (lcore >= RTE_MAX_LCORE)
739 /* runstate act as the guard variable. Use load-acquire
740 * memory order here to synchronize with store-release
741 * in runstate update functions.
743 if (__atomic_load_n(&lcore_states[lcore].runstate, __ATOMIC_ACQUIRE) ==
748 uint64_t service_mask = lcore_states[lcore].service_mask;
749 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
750 int32_t enabled = service_mask & (UINT64_C(1) << i);
751 int32_t service_running = rte_service_runstate_get(i);
752 int32_t only_core = (1 ==
753 __atomic_load_n(&rte_services[i].num_mapped_cores,
756 /* if the core is mapped, and the service is running, and this
757 * is the only core that is mapped, the service would cease to
758 * run if this core stopped, so fail instead.
760 if (enabled && service_running && only_core)
764 /* Use store-release memory order here to synchronize with
765 * load-acquire in runstate read functions.
767 __atomic_store_n(&lcore_states[lcore].runstate, RUNSTATE_STOPPED,
774 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint64_t *attr_value)
776 struct rte_service_spec_impl *s;
777 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
783 case RTE_SERVICE_ATTR_CYCLES:
784 *attr_value = s->cycles_spent;
786 case RTE_SERVICE_ATTR_CALL_COUNT:
787 *attr_value = s->calls;
795 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
796 uint64_t *attr_value)
798 struct core_state *cs;
800 if (lcore >= RTE_MAX_LCORE || !attr_value)
803 cs = &lcore_states[lcore];
804 if (!cs->is_service_core)
808 case RTE_SERVICE_LCORE_ATTR_LOOPS:
809 *attr_value = cs->loops;
817 service_dump_one(FILE *f, struct rte_service_spec_impl *s, uint32_t reset)
819 /* avoid divide by zero */
833 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
834 PRIu64"\tavg: %"PRIu64"\n",
835 s->spec.name, service_stats_enabled(s), s->calls,
836 s->cycles_spent, s->cycles_spent / calls);
840 rte_service_attr_reset_all(uint32_t id)
842 struct rte_service_spec_impl *s;
843 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
846 service_dump_one(NULL, s, reset);
851 rte_service_lcore_attr_reset_all(uint32_t lcore)
853 struct core_state *cs;
855 if (lcore >= RTE_MAX_LCORE)
858 cs = &lcore_states[lcore];
859 if (!cs->is_service_core)
868 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
871 struct core_state *cs = &lcore_states[lcore];
873 fprintf(f, "%02d\t", lcore);
874 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
875 if (!service_valid(i))
877 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
879 cs->calls_per_service[i] = 0;
885 rte_service_dump(FILE *f, uint32_t id)
888 int print_one = (id != UINT32_MAX);
890 /* print only the specified service */
892 struct rte_service_spec_impl *s;
893 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
894 fprintf(f, "Service %s Summary\n", s->spec.name);
896 service_dump_one(f, s, reset);
900 /* print all services, as UINT32_MAX was passed as id */
901 fprintf(f, "Services Summary\n");
902 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
903 if (!service_valid(i))
906 service_dump_one(f, &rte_services[i], reset);
909 fprintf(f, "Service Cores Summary\n");
910 for (i = 0; i < RTE_MAX_LCORE; i++) {
911 if (lcore_config[i].core_role != ROLE_SERVICE)
915 service_dump_calls_per_lcore(f, i, reset);