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 "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>
24 #define RTE_SERVICE_NUM_MAX 64
26 #define SERVICE_F_REGISTERED (1 << 0)
27 #define SERVICE_F_STATS_ENABLED (1 << 1)
28 #define SERVICE_F_START_CHECK (1 << 2)
30 /* runstates for services and lcores, denoting if they are active or not */
31 #define RUNSTATE_STOPPED 0
32 #define RUNSTATE_RUNNING 1
34 /* internal representation of a service */
35 struct rte_service_spec_impl {
36 /* public part of the struct */
37 struct rte_service_spec spec;
39 /* atomic lock that when set indicates a service core is currently
40 * running this service callback. When not set, a core may take the
41 * lock and then run the service callback.
43 rte_atomic32_t execute_lock;
45 /* API set/get-able variables */
48 uint8_t internal_flags;
50 /* per service statistics */
51 rte_atomic32_t num_mapped_cores;
53 uint64_t cycles_spent;
54 } __rte_cache_aligned;
56 /* the internal values of a service core */
58 /* map of services IDs are run on this core */
59 uint64_t service_mask;
60 uint8_t runstate; /* running or stopped */
61 uint8_t is_service_core; /* set if core is currently a service core */
62 uint8_t service_active_on_lcore[RTE_SERVICE_NUM_MAX];
64 uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
65 } __rte_cache_aligned;
67 static uint32_t rte_service_count;
68 static struct rte_service_spec_impl *rte_services;
69 static struct core_state *lcore_states;
70 static uint32_t rte_service_library_initialized;
73 rte_service_init(void)
75 if (rte_service_library_initialized) {
77 "service library init() called, init flag %d\n",
78 rte_service_library_initialized);
82 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
83 sizeof(struct rte_service_spec_impl),
86 RTE_LOG(ERR, EAL, "error allocating rte services array\n");
90 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
91 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
93 RTE_LOG(ERR, EAL, "error allocating core states array\n");
99 struct rte_config *cfg = rte_eal_get_configuration();
100 for (i = 0; i < RTE_MAX_LCORE; i++) {
101 if (lcore_config[i].core_role == ROLE_SERVICE) {
102 if ((unsigned int)i == cfg->master_lcore)
104 rte_service_lcore_add(i);
109 rte_service_library_initialized = 1;
112 rte_free(rte_services);
113 rte_free(lcore_states);
118 rte_service_finalize(void)
120 if (!rte_service_library_initialized)
123 rte_free(rte_services);
124 rte_free(lcore_states);
126 rte_service_library_initialized = 0;
129 /* returns 1 if service is registered and has not been unregistered
130 * Returns 0 if service never registered, or has been unregistered
133 service_valid(uint32_t id)
135 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
138 /* validate ID and retrieve service pointer, or return error value */
139 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
140 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
142 service = &rte_services[id]; \
145 /* returns 1 if statistics should be collected for service
146 * Returns 0 if statistics should not be collected for service
149 service_stats_enabled(struct rte_service_spec_impl *impl)
151 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
155 service_mt_safe(struct rte_service_spec_impl *s)
157 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
161 rte_service_set_stats_enable(uint32_t id, int32_t enabled)
163 struct rte_service_spec_impl *s;
164 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
167 s->internal_flags |= SERVICE_F_STATS_ENABLED;
169 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
175 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
177 struct rte_service_spec_impl *s;
178 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
181 s->internal_flags |= SERVICE_F_START_CHECK;
183 s->internal_flags &= ~(SERVICE_F_START_CHECK);
189 rte_service_get_count(void)
191 return rte_service_count;
195 rte_service_get_by_name(const char *name, uint32_t *service_id)
201 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
202 if (service_valid(i) &&
203 strcmp(name, rte_services[i].spec.name) == 0) {
213 rte_service_get_name(uint32_t id)
215 struct rte_service_spec_impl *s;
216 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
221 rte_service_probe_capability(uint32_t id, uint32_t capability)
223 struct rte_service_spec_impl *s;
224 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
225 return !!(s->spec.capabilities & capability);
229 rte_service_component_register(const struct rte_service_spec *spec,
233 int32_t free_slot = -1;
235 if (spec->callback == NULL || strlen(spec->name) == 0)
238 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
239 if (!service_valid(i)) {
245 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
248 struct rte_service_spec_impl *s = &rte_services[free_slot];
250 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
262 rte_service_component_unregister(uint32_t id)
265 struct rte_service_spec_impl *s;
266 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
271 s->internal_flags &= ~(SERVICE_F_REGISTERED);
273 /* clear the run-bit in all cores */
274 for (i = 0; i < RTE_MAX_LCORE; i++)
275 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
277 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
283 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
285 struct rte_service_spec_impl *s;
286 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
289 s->comp_runstate = RUNSTATE_RUNNING;
291 s->comp_runstate = RUNSTATE_STOPPED;
298 rte_service_runstate_set(uint32_t id, uint32_t runstate)
300 struct rte_service_spec_impl *s;
301 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
304 s->app_runstate = RUNSTATE_RUNNING;
306 s->app_runstate = RUNSTATE_STOPPED;
313 rte_service_runstate_get(uint32_t id)
315 struct rte_service_spec_impl *s;
316 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
319 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
320 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
322 return (s->app_runstate == RUNSTATE_RUNNING) &&
323 (s->comp_runstate == RUNSTATE_RUNNING) &&
324 (check_disabled | lcore_mapped);
328 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
329 struct core_state *cs, uint32_t service_idx)
331 void *userdata = s->spec.callback_userdata;
333 if (service_stats_enabled(s)) {
334 uint64_t start = rte_rdtsc();
335 s->spec.callback(userdata);
336 uint64_t end = rte_rdtsc();
337 s->cycles_spent += end - start;
338 cs->calls_per_service[service_idx]++;
341 s->spec.callback(userdata);
345 static inline int32_t
346 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask)
348 if (!service_valid(i))
350 struct rte_service_spec_impl *s = &rte_services[i];
351 if (s->comp_runstate != RUNSTATE_RUNNING ||
352 s->app_runstate != RUNSTATE_RUNNING ||
353 !(service_mask & (UINT64_C(1) << i))) {
354 cs->service_active_on_lcore[i] = 0;
358 cs->service_active_on_lcore[i] = 1;
360 /* check do we need cmpset, if MT safe or <= 1 core
361 * mapped, atomic ops are not required.
363 const int use_atomics = (service_mt_safe(s) == 0) &&
364 (rte_atomic32_read(&s->num_mapped_cores) > 1);
366 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
369 rte_service_runner_do_callback(s, cs, i);
370 rte_atomic32_clear(&s->execute_lock);
372 rte_service_runner_do_callback(s, cs, i);
378 rte_service_may_be_active(uint32_t id)
380 uint32_t ids[RTE_MAX_LCORE] = {0};
381 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
384 if (!service_valid(id))
387 for (i = 0; i < lcore_count; i++) {
388 if (lcore_states[i].service_active_on_lcore[id])
396 rte_service_run_iter_on_app_lcore(uint32_t id, uint32_t serialize_mt_unsafe)
398 /* run service on calling core, using all-ones as the service mask */
399 if (!service_valid(id))
402 struct core_state *cs = &lcore_states[rte_lcore_id()];
403 struct rte_service_spec_impl *s = &rte_services[id];
405 /* Atomically add this core to the mapped cores first, then examine if
406 * we can run the service. This avoids a race condition between
407 * checking the value, and atomically adding to the mapped count.
409 if (serialize_mt_unsafe)
410 rte_atomic32_inc(&s->num_mapped_cores);
412 if (service_mt_safe(s) == 0 &&
413 rte_atomic32_read(&s->num_mapped_cores) > 1) {
414 if (serialize_mt_unsafe)
415 rte_atomic32_dec(&s->num_mapped_cores);
419 int ret = service_run(id, cs, UINT64_MAX);
421 if (serialize_mt_unsafe)
422 rte_atomic32_dec(&s->num_mapped_cores);
428 rte_service_runner_func(void *arg)
432 const int lcore = rte_lcore_id();
433 struct core_state *cs = &lcore_states[lcore];
435 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
436 const uint64_t service_mask = cs->service_mask;
438 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
439 /* return value ignored as no change to code flow */
440 service_run(i, cs, service_mask);
448 lcore_config[lcore].state = WAIT;
454 rte_service_lcore_count(void)
458 for (i = 0; i < RTE_MAX_LCORE; i++)
459 count += lcore_states[i].is_service_core;
464 rte_service_lcore_list(uint32_t array[], uint32_t n)
466 uint32_t count = rte_service_lcore_count();
475 for (i = 0; i < RTE_MAX_LCORE; i++) {
476 struct core_state *cs = &lcore_states[i];
477 if (cs->is_service_core) {
487 rte_service_lcore_count_services(uint32_t lcore)
489 if (lcore >= RTE_MAX_LCORE)
492 struct core_state *cs = &lcore_states[lcore];
493 if (!cs->is_service_core)
496 return __builtin_popcountll(cs->service_mask);
500 rte_service_start_with_defaults(void)
502 /* create a default mapping from cores to services, then start the
503 * services to make them transparent to unaware applications.
507 uint32_t count = rte_service_get_count();
509 int32_t lcore_iter = 0;
510 uint32_t ids[RTE_MAX_LCORE] = {0};
511 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
513 if (lcore_count == 0)
516 for (i = 0; (int)i < lcore_count; i++)
517 rte_service_lcore_start(ids[i]);
519 for (i = 0; i < count; i++) {
520 /* do 1:1 core mapping here, with each service getting
521 * assigned a single core by default. Adding multiple services
522 * should multiplex to a single core, or 1:1 if there are the
523 * same amount of services as service-cores
525 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
530 if (lcore_iter >= lcore_count)
533 ret = rte_service_runstate_set(i, 1);
542 service_update(struct rte_service_spec *service, uint32_t lcore,
543 uint32_t *set, uint32_t *enabled)
548 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
549 if ((struct rte_service_spec *)&rte_services[i] == service &&
556 if (sid == -1 || lcore >= RTE_MAX_LCORE)
559 if (!lcore_states[lcore].is_service_core)
562 uint64_t sid_mask = UINT64_C(1) << sid;
564 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
567 if (*set && !lcore_mapped) {
568 lcore_states[lcore].service_mask |= sid_mask;
569 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
571 if (!*set && lcore_mapped) {
572 lcore_states[lcore].service_mask &= ~(sid_mask);
573 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
578 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
586 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
588 struct rte_service_spec_impl *s;
589 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
590 uint32_t on = enabled > 0;
591 return service_update(&s->spec, lcore, &on, 0);
595 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
597 struct rte_service_spec_impl *s;
598 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
600 int ret = service_update(&s->spec, lcore, 0, &enabled);
607 set_lcore_state(uint32_t lcore, int32_t state)
609 /* mark core state in hugepage backed config */
610 struct rte_config *cfg = rte_eal_get_configuration();
611 cfg->lcore_role[lcore] = state;
613 /* mark state in process local lcore_config */
614 lcore_config[lcore].core_role = state;
616 /* update per-lcore optimized state tracking */
617 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
621 rte_service_lcore_reset_all(void)
623 /* loop over cores, reset all to mask 0 */
625 for (i = 0; i < RTE_MAX_LCORE; i++) {
626 if (lcore_states[i].is_service_core) {
627 lcore_states[i].service_mask = 0;
628 set_lcore_state(i, ROLE_RTE);
629 lcore_states[i].runstate = RUNSTATE_STOPPED;
632 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
633 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
641 rte_service_lcore_add(uint32_t lcore)
643 if (lcore >= RTE_MAX_LCORE)
645 if (lcore_states[lcore].is_service_core)
648 set_lcore_state(lcore, ROLE_SERVICE);
650 /* ensure that after adding a core the mask and state are defaults */
651 lcore_states[lcore].service_mask = 0;
652 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
656 return rte_eal_wait_lcore(lcore);
660 rte_service_lcore_del(uint32_t lcore)
662 if (lcore >= RTE_MAX_LCORE)
665 struct core_state *cs = &lcore_states[lcore];
666 if (!cs->is_service_core)
669 if (cs->runstate != RUNSTATE_STOPPED)
672 set_lcore_state(lcore, ROLE_RTE);
679 rte_service_lcore_start(uint32_t lcore)
681 if (lcore >= RTE_MAX_LCORE)
684 struct core_state *cs = &lcore_states[lcore];
685 if (!cs->is_service_core)
688 if (cs->runstate == RUNSTATE_RUNNING)
691 /* set core to run state first, and then launch otherwise it will
692 * return immediately as runstate keeps it in the service poll loop
694 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
696 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
697 /* returns -EBUSY if the core is already launched, 0 on success */
702 rte_service_lcore_stop(uint32_t lcore)
704 if (lcore >= RTE_MAX_LCORE)
707 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
711 uint64_t service_mask = lcore_states[lcore].service_mask;
712 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
713 int32_t enabled = service_mask & (UINT64_C(1) << i);
714 int32_t service_running = rte_service_runstate_get(i);
715 int32_t only_core = (1 ==
716 rte_atomic32_read(&rte_services[i].num_mapped_cores));
718 /* if the core is mapped, and the service is running, and this
719 * is the only core that is mapped, the service would cease to
720 * run if this core stopped, so fail instead.
722 if (enabled && service_running && only_core)
726 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
732 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint64_t *attr_value)
734 struct rte_service_spec_impl *s;
735 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
741 case RTE_SERVICE_ATTR_CYCLES:
742 *attr_value = s->cycles_spent;
744 case RTE_SERVICE_ATTR_CALL_COUNT:
745 *attr_value = s->calls;
753 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
754 uint64_t *attr_value)
756 struct core_state *cs;
758 if (lcore >= RTE_MAX_LCORE || !attr_value)
761 cs = &lcore_states[lcore];
762 if (!cs->is_service_core)
766 case RTE_SERVICE_LCORE_ATTR_LOOPS:
767 *attr_value = cs->loops;
775 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
776 uint64_t all_cycles, uint32_t reset)
778 /* avoid divide by zero */
795 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
796 PRIu64"\tavg: %"PRIu64"\n",
797 s->spec.name, service_stats_enabled(s), s->calls,
798 s->cycles_spent, s->cycles_spent / calls);
802 rte_service_attr_reset_all(uint32_t id)
804 struct rte_service_spec_impl *s;
805 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
808 rte_service_dump_one(NULL, s, 0, reset);
813 rte_service_lcore_attr_reset_all(uint32_t lcore)
815 struct core_state *cs;
817 if (lcore >= RTE_MAX_LCORE)
820 cs = &lcore_states[lcore];
821 if (!cs->is_service_core)
830 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
833 struct core_state *cs = &lcore_states[lcore];
835 fprintf(f, "%02d\t", lcore);
836 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
837 if (!service_valid(i))
839 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
841 cs->calls_per_service[i] = 0;
847 rte_service_dump(FILE *f, uint32_t id)
850 int print_one = (id != UINT32_MAX);
852 uint64_t total_cycles = 0;
854 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
855 if (!service_valid(i))
857 total_cycles += rte_services[i].cycles_spent;
860 /* print only the specified service */
862 struct rte_service_spec_impl *s;
863 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
864 fprintf(f, "Service %s Summary\n", s->spec.name);
866 rte_service_dump_one(f, s, total_cycles, reset);
870 /* print all services, as UINT32_MAX was passed as id */
871 fprintf(f, "Services Summary\n");
872 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
873 if (!service_valid(i))
876 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
879 fprintf(f, "Service Cores Summary\n");
880 for (i = 0; i < RTE_MAX_LCORE; i++) {
881 if (lcore_config[i].core_role != ROLE_SERVICE)
885 service_dump_calls_per_lcore(f, i, reset);